Skip to content

Commit 1d3db1d

Browse files
HnMesh: fixed expected geometry data size computation
1 parent ea9b1dc commit 1d3db1d

File tree

3 files changed

+47
-26
lines changed

3 files changed

+47
-26
lines changed

Hydrogent/interface/HnMesh.hpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,6 @@ class HnMesh final : public pxr::HdMesh
164164
// The total number of supported primvars
165165
Uint32 Count = 0;
166166

167-
// The total expected size of the data for all primvars
168-
size_t ExpectedDataSize = 0;
169-
170167
// Dirty primvars arranged by name.
171168
// Typically, the name is the same as the primvar descriptor name,
172169
// but it may be different if the primvar is found using the role.
@@ -179,7 +176,7 @@ class HnMesh final : public pxr::HdMesh
179176
const pxr::SdfPath& Id,
180177
const pxr::TfToken& Name,
181178
const pxr::HdPrimvarDescriptor& PrimDesc,
182-
size_t Size);
179+
const pxr::TfToken& Role);
183180
};
184181
void GetPrimvarsInfo(pxr::HdSceneDelegate& SceneDelegate,
185182
pxr::HdDirtyBits& DirtyBits,

Hydrogent/src/HnGeometryPool.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,8 @@ void HnGeometryPool::AllocateVertices(const std::string& Name,
924924
m_UseVertexPool ? &m_ResMgr : nullptr,
925925
DisallowPoolAllocationReuse, ExistingData);
926926

927-
m_PendingVertexDataSize.fetch_add(static_cast<Int64>(Data->GetTotalSize()));
927+
size_t TotalSize = Data->GetTotalSize();
928+
m_PendingVertexDataSize.fetch_add(static_cast<Int64>(TotalSize));
928929

929930
{
930931
std::lock_guard<std::mutex> Guard{m_PendingVertexDataMtx};

Hydrogent/src/HnMesh.cpp

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -322,17 +322,21 @@ struct HnMesh::StagingVertexData
322322
std::map<pxr::TfToken, std::shared_ptr<pxr::HdBufferSource>> Sources;
323323
};
324324

325-
static size_t GetPrimvarElementSize(const pxr::TfToken& Name, const pxr::TfToken& Role)
325+
static size_t GetPrimvarElementSize(const HnRenderDelegate* RenderDelegate, const pxr::TfToken& Name, const pxr::TfToken& Role)
326326
{
327327
if (Name == pxr::HdTokens->points ||
328328
Role == pxr::HdPrimvarSchemaTokens->point)
329329
{
330-
return sizeof(float3);
330+
return RenderDelegate->GetUSDRenderer()->GetSettings().VertexPosPackMode == PBR_Renderer::VERTEX_POS_PACK_MODE_64_BIT ?
331+
sizeof(uint2) : // Vertex positions are packed into two 32-bit uints, see PBR_Renderer::VERTEX_POS_PACK_MODE_64_BIT
332+
sizeof(float3);
331333
}
332334
else if (Name == pxr::HdTokens->normals ||
333335
Role == pxr::HdPrimvarSchemaTokens->normal)
334336
{
335-
return sizeof(float3);
337+
return RenderDelegate->GetUSDRenderer()->GetSettings().PackVertexNormals ?
338+
sizeof(uint) : // Normals are packed into a single uint
339+
sizeof(float3);
336340
}
337341
else if (Name == pxr::HdTokens->displayColor ||
338342
Role == pxr::HdPrimvarSchemaTokens->color)
@@ -387,6 +391,7 @@ bool HnMesh::UpdateRepr(pxr::HdSceneDelegate& SceneDelegate,
387391

388392
PrimvarsInfo VertexPrimvarsInfo;
389393
PrimvarsInfo FacePrimvarsInfo;
394+
size_t ExpectedVertedDataSize = 0;
390395
if (TopologyDirty || AnyPrimvarDirty)
391396
{
392397
GetPrimvarsInfo(SceneDelegate, DirtyBits, VertexPrimvarsInfo, FacePrimvarsInfo);
@@ -396,22 +401,43 @@ bool HnMesh::UpdateRepr(pxr::HdSceneDelegate& SceneDelegate,
396401
m_HasFaceVaryingPrimvars = HasFaceVaryingPrimvars;
397402
IndexDataDirty = true;
398403
}
404+
405+
const size_t ExpectedNumElements = m_HasFaceVaryingPrimvars ? m_Topology.GetNumFaceVaryings() : m_Topology.GetNumPoints();
406+
for (const PrimvarsInfo& Primvars : {VertexPrimvarsInfo, FacePrimvarsInfo})
407+
{
408+
for (const auto& it : Primvars.Dirty)
409+
{
410+
const pxr::HdPrimvarDescriptor& PrimDesc = it.second;
411+
const size_t ElementSize = GetPrimvarElementSize(RenderDelegate, PrimDesc.name, PrimDesc.role);
412+
ExpectedVertedDataSize += ExpectedNumElements * ElementSize;
413+
}
414+
}
399415
}
400416

401417
const bool UseLineStrip = RenderDelegate->AllowPrimitiveRestart();
402418

403-
size_t ExpectedGeometryDataSize = VertexPrimvarsInfo.ExpectedDataSize + FacePrimvarsInfo.ExpectedDataSize;
419+
size_t ExpectedTriangleIndexDataSize = 0;
420+
size_t ExpectedEdgeIndexDataSize = 0;
421+
size_t ExpectedPointIndexDataSize = 0;
404422
if (IndexDataDirty)
405423
{
406424
HnMeshUtils MeshUtils{m_Topology, GetId()};
407425

408-
const HnMeshUtils::GET_TOTAL_INDEX_COUNT_FLAGS IndexCountFlags =
409-
HnMeshUtils::GET_TOTAL_INDEX_COUNT_FLAG_TRIANGLES |
410-
HnMeshUtils::GET_TOTAL_INDEX_COUNT_FLAG_POINTS |
411-
(UseLineStrip ? HnMeshUtils::GET_TOTAL_INDEX_COUNT_FLAG_EDGES_STRIP : HnMeshUtils::GET_TOTAL_INDEX_COUNT_FLAG_EDGES_LIST);
412-
ExpectedGeometryDataSize += MeshUtils.GetTotalIndexCount(IndexCountFlags) * sizeof(Uint32);
426+
ExpectedTriangleIndexDataSize = MeshUtils.GetTotalIndexCount(HnMeshUtils::GET_TOTAL_INDEX_COUNT_FLAG_TRIANGLES) * sizeof(Uint32);
427+
ExpectedEdgeIndexDataSize =
428+
MeshUtils.GetTotalIndexCount(UseLineStrip ?
429+
HnMeshUtils::GET_TOTAL_INDEX_COUNT_FLAG_EDGES_STRIP :
430+
HnMeshUtils::GET_TOTAL_INDEX_COUNT_FLAG_EDGES_LIST) *
431+
sizeof(Uint32);
432+
ExpectedPointIndexDataSize = MeshUtils.GetTotalIndexCount(HnMeshUtils::GET_TOTAL_INDEX_COUNT_FLAG_POINTS) * sizeof(Uint32);
413433
}
414434

435+
size_t ExpectedGeometryDataSize =
436+
ExpectedVertedDataSize +
437+
ExpectedTriangleIndexDataSize +
438+
ExpectedEdgeIndexDataSize +
439+
ExpectedPointIndexDataSize;
440+
415441
HnGeometryPool::ReservedSpace ReservedSpace = GeometryPool.ReserveSpace(ExpectedGeometryDataSize);
416442
if (GeometryLoadBudget > 0 && ReservedSpace.GetTotalPendingSize() > GeometryLoadBudget)
417443
{
@@ -769,27 +795,25 @@ void HnMesh::PrimvarsInfo::AddDirtyPrimvar(pxr::HdDirtyBits& Dirty
769795
const pxr::SdfPath& Id,
770796
const pxr::TfToken& Name,
771797
const pxr::HdPrimvarDescriptor& PrimDesc,
772-
size_t Size)
798+
const pxr::TfToken& Role)
773799
{
774800
if (pxr::HdChangeTracker::IsPrimvarDirty(DirtyBits, Id, PrimDesc.name))
775801
{
776-
Dirty.emplace(Name, PrimDesc);
802+
auto it_inserted = Dirty.emplace(Name, PrimDesc);
803+
it_inserted.first->second.role = Role;
777804
}
778805

779806
++Count;
780-
ExpectedDataSize += Size;
781807
}
782808

783809
void HnMesh::GetPrimvarsInfo(pxr::HdSceneDelegate& SceneDelegate,
784810
pxr::HdDirtyBits& DirtyBits,
785811
PrimvarsInfo& VertexPrimvarsInfo,
786812
PrimvarsInfo& FacePrimvarsInfo) const
787813
{
788-
const pxr::SdfPath& Id = GetId();
789-
790-
HnRenderPass::SupportedVertexInputsMapType SupportedPrimvars = GetSupportedPrimvars(SceneDelegate.GetRenderIndex(), GetMaterialId(), m_Topology);
791-
792-
const size_t ExpectedNumElements = m_HasFaceVaryingPrimvars ? m_Topology.GetNumFaceVaryings() : m_Topology.GetNumPoints();
814+
const pxr::SdfPath& Id = GetId();
815+
const pxr::HdRenderIndex& RenderIndex = SceneDelegate.GetRenderIndex();
816+
HnRenderPass::SupportedVertexInputsMapType SupportedPrimvars = GetSupportedPrimvars(RenderIndex, GetMaterialId(), m_Topology);
793817

794818
std::unordered_map<pxr::TfToken, pxr::HdPrimvarDescriptor, pxr::TfToken::HashFunctor> SkippedPrimvarsByRole;
795819

@@ -811,8 +835,8 @@ void HnMesh::GetPrimvarsInfo(pxr::HdSceneDelegate& SceneDelegate,
811835
}
812836

813837
// Use role provided by GetSupportedPrimvars, not the role from the primvar descriptor.
814-
const size_t DataSize = GetPrimvarElementSize(PrimDesc.name, PrimvarIt->second) * ExpectedNumElements;
815-
PrimvarsInfo.AddDirtyPrimvar(DirtyBits, Id, PrimDesc.name, PrimDesc, DataSize);
838+
const pxr::TfToken& Role = PrimvarIt->second;
839+
PrimvarsInfo.AddDirtyPrimvar(DirtyBits, Id, PrimDesc.name, PrimDesc, Role);
816840
SupportedPrimvars.erase(PrimDesc.name);
817841
}
818842
};
@@ -849,8 +873,7 @@ void HnMesh::GetPrimvarsInfo(pxr::HdSceneDelegate& SceneDelegate,
849873
const pxr::HdPrimvarDescriptor& PrimDesc = PrimIt->second;
850874
LOG_WARNING_MESSAGE("Primvar '", PrimDesc.name, "' in mesh ", Id, " is not recognized by the material, but matches primvar '",
851875
Name, "' by role '", PrimDesc.role, "'.");
852-
const size_t DataSize = GetPrimvarElementSize(PrimDesc.name, Role) * ExpectedNumElements;
853-
(PrimDesc.interpolation == pxr::HdInterpolationFaceVarying ? FacePrimvarsInfo : VertexPrimvarsInfo).AddDirtyPrimvar(DirtyBits, Id, Name, PrimDesc, DataSize);
876+
(PrimDesc.interpolation == pxr::HdInterpolationFaceVarying ? FacePrimvarsInfo : VertexPrimvarsInfo).AddDirtyPrimvar(DirtyBits, Id, Name, PrimDesc, Role);
854877
}
855878
}
856879
}

0 commit comments

Comments
 (0)