Skip to content

Commit

Permalink
hdGatling/gi: track prototype transform in Gi object
Browse files Browse the repository at this point in the history
  • Loading branch information
pablode committed Aug 5, 2024
1 parent cadad8c commit 273c846
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 22 deletions.
1 change: 1 addition & 0 deletions src/gi/gtl/gi/Gi.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ namespace gtl
void giDestroyMaterial(GiMaterial* mat);

GiMesh* giCreateMesh(const GiMeshDesc& desc);
void giSetMeshTransform(GiMesh* mesh, float transform[3][4]);
void giDestroyMesh(GiMesh* mesh);

GiBvh* giCreateBvh(GiScene* scene, const GiBvhParams& params);
Expand Down
11 changes: 10 additions & 1 deletion src/gi/impl/Gi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ namespace gtl
struct GiMesh
{
GiMeshDataVariant data;
glm::mat3x4 transform;
bool flipFacing;
int id;
};
Expand Down Expand Up @@ -517,12 +518,18 @@ namespace gtl

GiMesh* mesh = new GiMesh {
.data = std::move(cpuData),
.transform = glm::mat3x4(1.0f),
.flipFacing = desc.isLeftHanded,
.id = desc.id
};
return mesh;
}

void giSetMeshTransform(GiMesh* mesh, float transform[3][4])
{
memcpy(glm::value_ptr(mesh->transform), transform, sizeof(float) * 12);
}

void giDestroyMesh(GiMesh* mesh)
{
if (GiMeshGpuData* data = std::get_if<GiMeshGpuData>(&mesh->data); data)
Expand Down Expand Up @@ -763,11 +770,13 @@ namespace gtl
}

// Create BLAS instance for TLAS.
glm::mat3x4 transform = glm::mat3x4(glm::mat4(mesh->transform) * glm::mat4(glm::make_mat3x4((float*) instance->transform)));

CgpuBlasInstance blasInstance;
blasInstance.as = data->blas;
blasInstance.hitGroupIndex = materialIndex * 2; // always two hit groups per material: regular & shadow
blasInstance.instanceCustomIndex = uint32_t(blasPayloads.size());
memcpy(blasInstance.transform, instance->transform, sizeof(float) * 12);
memcpy(blasInstance.transform, glm::value_ptr(transform), sizeof(float) * 12);

blasInstances.push_back(blasInstance);
blasPayloads.push_back(data->payload);
Expand Down
31 changes: 15 additions & 16 deletions src/hdGatling/mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,6 @@ namespace

HdGatlingMesh::HdGatlingMesh(const SdfPath& id)
: HdMesh(id)
, _prototypeTransform(1.0)
, _color(0.0, 0.0, 0.0)
, _hasColor(false)
{
Expand Down Expand Up @@ -408,7 +407,6 @@ void HdGatlingMesh::Sync(HdSceneDelegate* sceneDelegate,
if ((*dirtyBits & HdChangeTracker::DirtyInstancer) |
(*dirtyBits & HdChangeTracker::DirtyInstanceIndex))
{

_UpdateInstancer(sceneDelegate, &dirtyBitsCopy);

const SdfPath& instancerId = GetInstancerId();
Expand All @@ -430,24 +428,30 @@ void HdGatlingMesh::Sync(HdSceneDelegate* sceneDelegate,
_UpdateVisibility(sceneDelegate, &dirtyBitsCopy);
}

if (*dirtyBits & HdChangeTracker::DirtyTransform)
{
_prototypeTransform = sceneDelegate->GetTransform(id);
}

bool updateGeometry =
(*dirtyBits & HdChangeTracker::DirtyPoints) |
(*dirtyBits & HdChangeTracker::DirtyNormals) |
(*dirtyBits & HdChangeTracker::DirtyTopology);

*dirtyBits = HdChangeTracker::Clean;
if (updateGeometry)
{
_CreateGiMesh(sceneDelegate);
}

if (!updateGeometry)
if (_giMesh && (*dirtyBits & HdChangeTracker::DirtyTransform))
{
return;
GfMatrix4d t = sceneDelegate->GetTransform(id);

float transform[3][4] = {
{ (float) t[0][0], (float) t[1][0], (float) t[2][0], (float) t[3][0] },
{ (float) t[0][1], (float) t[1][1], (float) t[2][1], (float) t[3][1] },
{ (float) t[0][2], (float) t[1][2], (float) t[2][2], (float) t[3][2] }
};

giSetMeshTransform(_giMesh, transform);
}

_CreateGiMesh(sceneDelegate);
*dirtyBits = HdChangeTracker::Clean;
}

bool HdGatlingMesh::_FindPrimvarInterpolationByName(HdSceneDelegate* sceneDelegate,
Expand Down Expand Up @@ -800,11 +804,6 @@ GiMesh* HdGatlingMesh::GetGiMesh() const
return _giMesh;
}

const GfMatrix4d& HdGatlingMesh::GetPrototypeTransform() const
{
return _prototypeTransform;
}

const GfVec3f& HdGatlingMesh::GetColor() const
{
return _color;
Expand Down
3 changes: 0 additions & 3 deletions src/hdGatling/mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ class HdGatlingMesh final : public HdMesh

GiMesh* GetGiMesh() const;

const GfMatrix4d& GetPrototypeTransform() const;

const GfVec3f& GetColor() const;

bool HasColor() const;
Expand Down Expand Up @@ -77,7 +75,6 @@ class HdGatlingMesh final : public HdMesh

private:
GiMesh* _giMesh = nullptr;
GfMatrix4d _prototypeTransform;
GfVec3f _color;
bool _hasColor = false;
};
Expand Down
3 changes: 1 addition & 2 deletions src/hdGatling/renderPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,9 @@ void HdGatlingRenderPass::_BakeMeshes(HdRenderIndex* renderIndex,

meshes.push_back(giMesh);

const GfMatrix4d& prototypeTransform = mesh->GetPrototypeTransform();
for (size_t i = 0; i < transforms.size(); i++)
{
GfMatrix4d T = prototypeTransform * transforms[i];
GfMatrix4d T = transforms[i];

float instanceTransform[3][4] = {
{ (float) T[0][0], (float) T[1][0], (float) T[2][0], (float) T[3][0] },
Expand Down

0 comments on commit 273c846

Please sign in to comment.