Skip to content

Commit

Permalink
Hydrogent: a few changes needed to enable async texture loading
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMostDiligent committed Oct 14, 2024
1 parent 7478acc commit 07b075b
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Hydrogent/include/HnRenderParam.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class HnRenderParam final : public pxr::HdRenderParam
Count
};
uint32_t GetAttribVersion(GlobalAttrib Attrib) const { return m_GlobalAttribVersions[static_cast<size_t>(Attrib)].load(); }
void MakeAttribDirty(GlobalAttrib Attrib) { m_GlobalAttribVersions[static_cast<size_t>(Attrib)].fetch_add(1); }
uint32_t MakeAttribDirty(GlobalAttrib Attrib) { return m_GlobalAttribVersions[static_cast<size_t>(Attrib)].fetch_add(1) + 1; }

PBR_Renderer::DebugViewType GetDebugView() const { return m_DebugView; }

Expand Down
3 changes: 3 additions & 0 deletions Hydrogent/interface/HnRenderDelegate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,8 @@ class HnRenderDelegate final : public pxr::HdRenderDelegate

IObject* GetMaterialSRBCache() const { return m_MaterialSRBCache; }

HnMaterial* GetFallbackMaterial() const { return m_FallbackMaterial; }

private:
static const pxr::TfTokenVector SupportedRPrimTypes;
static const pxr::TfTokenVector SupportedSPrimTypes;
Expand Down Expand Up @@ -407,6 +409,7 @@ class HnRenderDelegate final : public pxr::HdRenderDelegate

std::mutex m_MaterialsMtx;
std::unordered_set<HnMaterial*> m_Materials;
HnMaterial* m_FallbackMaterial = nullptr;

std::mutex m_LightsMtx;
std::unordered_set<HnLight*> m_Lights;
Expand Down
6 changes: 3 additions & 3 deletions Hydrogent/interface/HnRenderPass.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ class HnRenderPass final : public pxr::HdRenderPass

const HnDrawItem& DrawItem;
const HnMesh& Mesh;
const HnMaterial& Material;

IPipelineState* pPSO = nullptr;
const HnMaterial* pMaterial = nullptr;
IPipelineState* pPSO = nullptr;

const entt::entity MeshEntity;
const float MeshUID;
Expand Down Expand Up @@ -184,7 +184,7 @@ class HnRenderPass final : public pxr::HdRenderPass

operator bool() const noexcept
{
return pPSO != nullptr && NumVertices > 0;
return pPSO != nullptr && pMaterial != nullptr && NumVertices > 0;
}
};

Expand Down
6 changes: 3 additions & 3 deletions Hydrogent/src/HnRenderDelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -492,12 +492,12 @@ pxr::HdSprim* HnRenderDelegate::CreateFallbackSprim(const pxr::TfToken& TypeId)
pxr::HdSprim* SPrim = nullptr;
if (TypeId == pxr::HdPrimTypeTokens->material)
{
HnMaterial* Mat = HnMaterial::CreateFallback(m_TextureRegistry, *m_USDRenderer);
m_FallbackMaterial = HnMaterial::CreateFallback(m_TextureRegistry, *m_USDRenderer);
{
std::lock_guard<std::mutex> Guard{m_MaterialsMtx};
m_Materials.emplace(Mat);
m_Materials.emplace(m_FallbackMaterial);
}
SPrim = Mat;
SPrim = m_FallbackMaterial;
}
else if (TypeId == pxr::HdPrimTypeTokens->camera ||
TypeId == pxr::HdPrimTypeTokens->simpleLight ||
Expand Down
30 changes: 15 additions & 15 deletions Hydrogent/src/HnRenderPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ pxr::HdRenderPassSharedPtr HnRenderPass::Create(pxr::HdRenderIndex* pI
return pxr::HdRenderPassSharedPtr{new HnRenderPass{pIndex, Collection}};
}

HnRenderPass::DrawListItem::DrawListItem(HnRenderDelegate& RenderDelegate, const HnDrawItem& Item) noexcept :
HnRenderPass::DrawListItem::DrawListItem(HnRenderDelegate& RenderDelegate,
const HnDrawItem& Item) noexcept :
DrawItem{Item},
Mesh{Item.GetMesh()},
Material{*Item.GetMaterial()},
MeshEntity{Mesh.GetEntity()},
MeshUID{static_cast<float>(Mesh.GetUID())},
RenderStateID{0},
Expand Down Expand Up @@ -521,7 +521,7 @@ HnRenderPass::EXECUTE_RESULT HnRenderPass::Execute(HnRenderPassState& RPState, c
FirstMultiDrawItem.ListItem.IndexBuffer == ListItem.IndexBuffer &&
FirstMultiDrawItem.ListItem.NumVertexBuffers == ListItem.NumVertexBuffers &&
FirstMultiDrawItem.ListItem.VertexBuffers == ListItem.VertexBuffers &&
FirstMultiDrawItem.ListItem.Material.GetSRB() == ListItem.Material.GetSRB());
FirstMultiDrawItem.ListItem.pMaterial->GetSRB() == ListItem.pMaterial->GetSRB());
VERIFY_EXPR(AttribsBufferOffset + ListItem.ShaderAttribsDataSize <= AttribsBuffDesc.Size);

++FirstMultiDrawItem.DrawCount;
Expand Down Expand Up @@ -637,7 +637,7 @@ HnRenderPass::EXECUTE_RESULT HnRenderPass::Execute(HnRenderPassState& RPState, c
// Note: if the material changes in the mesh, the mesh material version and/or
// global material version will be updated, and the draw list item GPU
// resources will be updated.
const GLTF::Material& MaterialData = ListItem.Material.GetMaterialData();
const GLTF::Material& MaterialData = ListItem.pMaterial->GetMaterialData();
GLTF_PBR_Renderer::WritePBRPrimitiveShaderAttribs(pCurrPrimitive, AttribsData, State.USDRenderer.GetSettings().TextureAttribIndices,
MaterialData, /*TransposeMatrices = */ !PackMatrixRowMajor);

Expand Down Expand Up @@ -782,8 +782,7 @@ void HnRenderPass::UpdateDrawListGPUResources(RenderState& State)
m_PendingPSOs.clear();
}

if (State.RenderParam.GetAsyncShaderCompilation() &&
m_FallbackPSO == nullptr &&
if (m_FallbackPSO == nullptr &&
(m_Params.UsdPsoFlags & USD_Renderer::USD_PSO_FLAG_ENABLE_COLOR_OUTPUT) != 0 &&
m_RenderMode == HN_RENDER_MODE_SOLID)
{
Expand Down Expand Up @@ -824,7 +823,7 @@ void HnRenderPass::UpdateDrawListGPUResources(RenderState& State)
State.Hash = ComputeHash(State.Item.pPSO,
State.Item.IndexBuffer,
State.Item.NumVertexBuffers,
State.Item.Material.GetSRB());
State.Item.pMaterial->GetSRB());
for (Uint32 i = 0; i < State.Item.NumVertexBuffers; ++i)
{
HashCombine(State.Hash, State.Item.VertexBuffers[i]);
Expand All @@ -843,7 +842,7 @@ void HnRenderPass::UpdateDrawListGPUResources(RenderState& State)
if (Item.pPSO != rhs.Item.pPSO ||
Item.IndexBuffer != rhs.Item.IndexBuffer ||
Item.NumVertexBuffers != rhs.Item.NumVertexBuffers ||
Item.Material.GetSRB() != rhs.Item.Material.GetSRB())
Item.pMaterial->GetSRB() != rhs.Item.pMaterial->GetSRB())
return false;
// clang-format on

Expand Down Expand Up @@ -899,9 +898,9 @@ void HnRenderPass::UpdateDrawListGPUResources(RenderState& State)
Item0PrecedesItem1 = true;
else if (Item0.pPSO > Item1.pPSO)
Item0PrecedesItem1 = false;
else if (Item0.Material.GetSRB() < Item1.Material.GetSRB())
else if (Item0.pMaterial->GetSRB() < Item1.pMaterial->GetSRB())
Item0PrecedesItem1 = true;
else if (Item0.Material.GetSRB() > Item1.Material.GetSRB())
else if (Item0.pMaterial->GetSRB() > Item1.pMaterial->GetSRB())
Item0PrecedesItem1 = false;
else
Item0PrecedesItem1 = Item0.MeshEntity < Item1.MeshEntity;
Expand Down Expand Up @@ -1003,6 +1002,7 @@ void HnRenderPass::UpdateDrawListItemGPUResources(DrawListItem& ListItem, Render
const HnDrawItem::GeometryData& Geo = DrawItem.GetGeometryData();
const HnMaterial* pMaterial = DrawItem.GetMaterial();
VERIFY(pMaterial != nullptr, "Material is null");
ListItem.pMaterial = pMaterial;

const CULL_MODE CullMode = ListItem.Mesh.GetCullMode();

Expand Down Expand Up @@ -1166,12 +1166,12 @@ void HnRenderPass::RenderPendingDrawItems(RenderState& State)

State.SetPipelineState(m_UseFallbackPSO ? m_FallbackPSO : ListItem.pPSO);

IShaderResourceBinding* pSRB = ListItem.Material.GetSRB(PendingItem.AttribsBufferOffset);
IShaderResourceBinding* pSRB = ListItem.pMaterial->GetSRB(PendingItem.AttribsBufferOffset);
VERIFY(pSRB != nullptr, "Material SRB is null. This may happen if UpdateSRB was not called for this material.");
if (PendingItem.JointsBufferOffset != ~0u && PendingItem.JointsBufferOffset != JointsBufferOffset)
{
JointsBufferOffset = PendingItem.JointsBufferOffset;
ListItem.Material.SetJointsBufferOffset(JointsBufferOffset);
ListItem.pMaterial->SetJointsBufferOffset(JointsBufferOffset);
}
State.CommitShaderResources(pSRB);

Expand All @@ -1184,15 +1184,15 @@ void HnRenderPass::RenderPendingDrawItems(RenderState& State)
VERIFY_EXPR(item_idx + PendingItem.DrawCount <= m_PendingDrawItems.size());
for (size_t i = 1; i < PendingItem.DrawCount; ++i)
{
const auto& BatchItem = m_PendingDrawItems[item_idx + i];
const auto& BatchListItem = BatchItem.ListItem;
const PendingDrawItem& BatchItem = m_PendingDrawItems[item_idx + i];
const DrawListItem& BatchListItem = BatchItem.ListItem;
// clang-format off
VERIFY_EXPR(BatchListItem.RenderStateID == ListItem.RenderStateID &&
BatchListItem.pPSO == ListItem.pPSO &&
BatchListItem.IndexBuffer == ListItem.IndexBuffer &&
BatchListItem.NumVertexBuffers == ListItem.NumVertexBuffers &&
BatchListItem.VertexBuffers == ListItem.VertexBuffers &&
BatchListItem.DrawItem.GetMaterial()->GetSRB() == ListItem.DrawItem.GetMaterial()->GetSRB() &&
BatchListItem.pMaterial->GetSRB() == ListItem.pMaterial->GetSRB() &&
BatchItem.JointsBufferOffset == PendingItem.JointsBufferOffset);
// clang-format on
}
Expand Down

0 comments on commit 07b075b

Please sign in to comment.