Skip to content

Commit

Permalink
HnMaterial: group shader variables into a struct
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMostDiligent committed Nov 25, 2024
1 parent 47cbb7c commit 60823ed
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
22 changes: 13 additions & 9 deletions Hydrogent/interface/HnMaterial.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,28 +87,28 @@ class HnMaterial final : public pxr::HdMaterial
IShaderResourceBinding* GetSRB() const { return m_SRB; }
IShaderResourceBinding* GetSRB(Uint32 PrimitiveAttribsOffset) const
{
VERIFY_EXPR(m_PrimitiveAttribsVar != nullptr);
m_PrimitiveAttribsVar->SetBufferOffset(PrimitiveAttribsOffset);
VERIFY_EXPR(m_SRBVars.PrimitiveAttribs != nullptr);
m_SRBVars.PrimitiveAttribs->SetBufferOffset(PrimitiveAttribsOffset);
return m_SRB;
}
void ApplyMaterialAttribsBufferOffset(Uint32& CurrentOffset) const
{
VERIFY_EXPR(m_MaterialAttribsVar != nullptr);
VERIFY_EXPR(m_SRBVars.MaterialAttribs != nullptr);
VERIFY_EXPR(m_PBRMaterialAttribsBufferOffset != ~0u);
if (CurrentOffset != m_PBRMaterialAttribsBufferOffset)
{
m_MaterialAttribsVar->SetBufferOffset(m_PBRMaterialAttribsBufferOffset);
m_SRBVars.MaterialAttribs->SetBufferOffset(m_PBRMaterialAttribsBufferOffset);
CurrentOffset = m_PBRMaterialAttribsBufferOffset;
}
}
void SetJointsBufferOffset(Uint32 Offset) const
{
if (m_JointTransformsVar == nullptr)
if (m_SRBVars.JointTransforms == nullptr)
{
UNEXPECTED("Joint transforms variable is not initialized, which indicates that skinning is not enabled in the renderer.");
return;
}
m_JointTransformsVar->SetBufferOffset(Offset);
m_SRBVars.JointTransforms->SetBufferOffset(Offset);
}

const GLTF::Material& GetMaterialData() const { return m_MaterialData; }
Expand Down Expand Up @@ -176,9 +176,13 @@ class HnMaterial final : public pxr::HdMaterial
std::unordered_map<pxr::TfToken, HnTextureRegistry::TextureHandleSharedPtr, pxr::TfToken::HashFunctor> m_Textures;

RefCntAutoPtr<IShaderResourceBinding> m_SRB;
IShaderResourceVariable* m_PrimitiveAttribsVar = nullptr; // cbPrimitiveAttribs
IShaderResourceVariable* m_MaterialAttribsVar = nullptr; // cbMaterialAttribs
IShaderResourceVariable* m_JointTransformsVar = nullptr; // cbJointTransforms
struct SRBVariables
{
IShaderResourceVariable* PrimitiveAttribs = nullptr; // cbPrimitiveAttribs
IShaderResourceVariable* MaterialAttribs = nullptr; // cbMaterialAttribs
IShaderResourceVariable* JointTransforms = nullptr; // cbJointTransforms
};
SRBVariables m_SRBVars;

GLTF::Material m_MaterialData;

Expand Down
24 changes: 11 additions & 13 deletions Hydrogent/src/HnMaterial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -995,9 +995,7 @@ bool HnMaterial::UpdateSRB(HnRenderDelegate& RenderDelegate)
if (m_ResourceCacheVersion != ResourceCacheVersion)
{
m_SRB.Release();
m_PrimitiveAttribsVar = nullptr;
m_MaterialAttribsVar = nullptr;
m_JointTransformsVar = nullptr;
m_SRBVars = {};
m_ResourceCacheVersion = ResourceCacheVersion;
}

Expand Down Expand Up @@ -1305,28 +1303,28 @@ bool HnMaterial::UpdateSRB(HnRenderDelegate& RenderDelegate)

if (m_SRB)
{
m_MaterialAttribsVar = m_SRB->GetVariableByName(SHADER_TYPE_PIXEL, "cbMaterialAttribs");
VERIFY_EXPR(m_MaterialAttribsVar != nullptr);
if (m_MaterialAttribsVar->Get() == nullptr)
m_SRBVars.MaterialAttribs = m_SRB->GetVariableByName(SHADER_TYPE_PIXEL, "cbMaterialAttribs");
VERIFY_EXPR(m_SRBVars.MaterialAttribs != nullptr);
if (m_SRBVars.MaterialAttribs->Get() == nullptr)
{
IBuffer* pMaterialAttribsBuffer = SRBCache->GetMaterialAttribsBuffer();
// Bind maximum possible buffer range
const Uint32 PBRMaterialAttribsMaxSize = SRBCache->GetMaxAttribsDataSize();
m_MaterialAttribsVar->SetBufferRange(pMaterialAttribsBuffer, 0, PBRMaterialAttribsMaxSize);
m_SRBVars.MaterialAttribs->SetBufferRange(pMaterialAttribsBuffer, 0, PBRMaterialAttribsMaxSize);
}

m_JointTransformsVar = m_SRB->GetVariableByName(SHADER_TYPE_VERTEX, UsdRenderer.GetJointTransformsVarName());
VERIFY_EXPR(m_JointTransformsVar != nullptr || RendererSettings.MaxJointCount == 0);
m_SRBVars.JointTransforms = m_SRB->GetVariableByName(SHADER_TYPE_VERTEX, UsdRenderer.GetJointTransformsVarName());
VERIFY_EXPR(m_SRBVars.JointTransforms != nullptr || RendererSettings.MaxJointCount == 0);

const Uint32 PBRPrimitiveAttribsSize = UsdRenderer.GetPBRPrimitiveAttribsSize(PBR_Renderer::PSO_FLAG_ALL);
const Uint32 PrimitiveArraySize = std::max(UsdRenderer.GetSettings().PrimitiveArraySize, 1u);
m_PBRPrimitiveAttribsBufferRange = PBRPrimitiveAttribsSize * PrimitiveArraySize;

m_PrimitiveAttribsVar = m_SRB->GetVariableByName(SHADER_TYPE_PIXEL, "cbPrimitiveAttribs");
VERIFY_EXPR(m_PrimitiveAttribsVar != nullptr);
if (m_PrimitiveAttribsVar->Get() == nullptr)
m_SRBVars.PrimitiveAttribs = m_SRB->GetVariableByName(SHADER_TYPE_PIXEL, "cbPrimitiveAttribs");
VERIFY_EXPR(m_SRBVars.PrimitiveAttribs != nullptr);
if (m_SRBVars.PrimitiveAttribs->Get() == nullptr)
{
m_PrimitiveAttribsVar->SetBufferRange(UsdRenderer.GetPBRPrimitiveAttribsCB(), 0, m_PBRPrimitiveAttribsBufferRange);
m_SRBVars.PrimitiveAttribs->SetBufferRange(UsdRenderer.GetPBRPrimitiveAttribsCB(), 0, m_PBRPrimitiveAttribsBufferRange);
}
}
else
Expand Down

0 comments on commit 60823ed

Please sign in to comment.