Skip to content

Commit

Permalink
HnMesh: some updates to SRB cache
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMostDiligent committed Nov 24, 2024
1 parent 8c206b0 commit 957b53a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 18 deletions.
1 change: 1 addition & 0 deletions Hydrogent/interface/HnMaterial.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class HnMaterial final : public pxr::HdMaterial
/// Creates an SRB cache that should be passed to UpdateSRB().
static RefCntAutoPtr<IObject> CreateSRBCache();

static void InitSRBCache(HnRenderDelegate& RenderDelegate);
static void BeginResourceUpdate(HnRenderDelegate& RenderDelegate);
bool UpdateSRB(HnRenderDelegate& RenderDelegate);
static void EndResourceUpdate(HnRenderDelegate& RenderDelegate);
Expand Down
70 changes: 52 additions & 18 deletions Hydrogent/src/HnMaterial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,12 @@ class HnMaterialSRBCache : public ObjectBase<IObject>

IMPLEMENT_QUERY_INTERFACE_IN_PLACE(IID_HnMaterialSRBCache, ObjectBase<IObject>)

void Initialize(HnRenderDelegate& RenderDelegate)
{
m_ConstantBufferOffsetAlignment = RenderDelegate.GetDevice()->GetAdapterInfo().Buffer.ConstantBufferOffsetAlignment;
m_MaxAttribsDataSize = RenderDelegate.GetUSDRenderer()->GetPBRMaterialAttribsSize(PBR_Renderer::PSO_FLAG_ALL);
}

/// SRB cache key
///
/// The key is the combination of unique IDs of the texture objects used by the SRB.
Expand Down Expand Up @@ -697,14 +703,16 @@ class HnMaterialSRBCache : public ObjectBase<IObject>
return it->second;
}

Uint32 AllocateBufferOffset(Uint32 Size, Uint32 Alignment, Uint32 MaxAttribsDataSize)
Uint32 AllocateBufferOffset(Uint32 Size)
{
std::lock_guard<std::mutex> Lock{m_CurrBufferOffsetMtx};

const Uint32 Offset = AlignUp(m_CurrBufferOffset, Alignment);
VERIFY(m_ConstantBufferOffsetAlignment != 0 && m_MaxAttribsDataSize != 0, "The cache is not initialized");

const Uint32 Offset = AlignUp(m_CurrBufferOffset, m_ConstantBufferOffsetAlignment);
m_CurrBufferOffset = Offset + Size;
// Reserve enough space for the maximum possible attribs data size.
m_RequiredBufferSize = AlignUp(Offset + MaxAttribsDataSize, Alignment);
m_RequiredBufferSize = AlignUp(Offset + m_MaxAttribsDataSize, m_ConstantBufferOffsetAlignment);
return Offset;
}

Expand Down Expand Up @@ -760,6 +768,11 @@ class HnMaterialSRBCache : public ObjectBase<IObject>
return pBuffer;
}

Uint32 GetMaxAttribsDataSize() const
{
return m_MaxAttribsDataSize;
}

Uint32 GetMaterialAttribsBufferVersion() const
{
return m_MaterialAttribsBuffer.GetVersion();
Expand All @@ -784,6 +797,9 @@ class HnMaterialSRBCache : public ObjectBase<IObject>

std::unordered_map<ShaderTextureIndexingIdType, const StaticShaderTextureIdsArrayType&> m_IdToIndexing;

Uint32 m_ConstantBufferOffsetAlignment = 0;
Uint32 m_MaxAttribsDataSize = 0;

std::mutex m_CurrBufferOffsetMtx;
Uint32 m_CurrBufferOffset = 0;
Uint32 m_RequiredBufferSize = 0;
Expand All @@ -810,9 +826,7 @@ void HnMaterial::AllocateBufferSpace(HnRenderDelegate& RenderDelegate)
RefCntAutoPtr<HnMaterialSRBCache> SRBCache{RenderDelegate.GetMaterialSRBCache(), IID_HnMaterialSRBCache};
VERIFY_EXPR(SRBCache);

const Uint32 ConstantBufferOffsetAlignment = RenderDelegate.GetDevice()->GetAdapterInfo().Buffer.ConstantBufferOffsetAlignment;
const Uint32 MaxAttribsDataSize = UsdRenderer.GetPBRMaterialAttribsSize(PBR_Renderer::PSO_FLAG_ALL);
m_PBRMaterialAttribsBufferOffset = SRBCache->AllocateBufferOffset(m_PBRMaterialAttribsSize, ConstantBufferOffsetAlignment, MaxAttribsDataSize);
m_PBRMaterialAttribsBufferOffset = SRBCache->AllocateBufferOffset(m_PBRMaterialAttribsSize);
}
}

Expand Down Expand Up @@ -1246,7 +1260,7 @@ bool HnMaterial::UpdateSRB(HnRenderDelegate& RenderDelegate)
{
IBuffer* pMaterialAttribsBuffer = SRBCache->GetMaterialAttribsBuffer();
// Bind maximum possible buffer range
const Uint32 PBRMaterialAttribsMaxSize = UsdRenderer.GetPBRMaterialAttribsSize(PBR_Renderer::PSO_FLAG_ALL);
const Uint32 PBRMaterialAttribsMaxSize = SRBCache->GetMaxAttribsDataSize();
m_MaterialAttribsVar->SetBufferRange(pMaterialAttribsBuffer, 0, PBRMaterialAttribsMaxSize);
}

Expand All @@ -1272,23 +1286,43 @@ bool HnMaterial::UpdateSRB(HnRenderDelegate& RenderDelegate)
return true;
}

void HnMaterial::BeginResourceUpdate(HnRenderDelegate& RenderDelegate)
void HnMaterial::InitSRBCache(HnRenderDelegate& RenderDelegate)
{
RefCntAutoPtr<HnMaterialSRBCache> SRBCache{RenderDelegate.GetMaterialSRBCache(), IID_HnMaterialSRBCache};
VERIFY_EXPR(SRBCache);
if (RefCntAutoPtr<HnMaterialSRBCache> SRBCache{RenderDelegate.GetMaterialSRBCache(), IID_HnMaterialSRBCache})
{
SRBCache->Initialize(RenderDelegate);
}
else
{
UNEXPECTED("Material SRB cache must not be null");
}
}

SRBCache->PrepareMaterialAttribsBuffer(RenderDelegate.GetDevice(), RenderDelegate.GetDeviceContext());
void HnMaterial::BeginResourceUpdate(HnRenderDelegate& RenderDelegate)
{
if (RefCntAutoPtr<HnMaterialSRBCache> SRBCache{RenderDelegate.GetMaterialSRBCache(), IID_HnMaterialSRBCache})
{
SRBCache->PrepareMaterialAttribsBuffer(RenderDelegate.GetDevice(), RenderDelegate.GetDeviceContext());
}
else
{
UNEXPECTED("Material SRB cache must not be null");
}
}

void HnMaterial::EndResourceUpdate(HnRenderDelegate& RenderDelegate)
{
RefCntAutoPtr<HnMaterialSRBCache> SRBCache{RenderDelegate.GetMaterialSRBCache(), IID_HnMaterialSRBCache};
VERIFY_EXPR(SRBCache);

IDeviceContext* pContext = RenderDelegate.GetDeviceContext();
IBuffer* pBuffer = SRBCache->CommitUpdates(RenderDelegate.GetDevice(), pContext);
StateTransitionDesc Barrier{pBuffer, RESOURCE_STATE_UNKNOWN, RESOURCE_STATE_CONSTANT_BUFFER, STATE_TRANSITION_FLAG_UPDATE_STATE};
pContext->TransitionResourceStates(1, &Barrier);
if (RefCntAutoPtr<HnMaterialSRBCache> SRBCache{RenderDelegate.GetMaterialSRBCache(), IID_HnMaterialSRBCache})
{
IDeviceContext* pContext = RenderDelegate.GetDeviceContext();
IBuffer* pBuffer = SRBCache->CommitUpdates(RenderDelegate.GetDevice(), pContext);
StateTransitionDesc Barrier{pBuffer, RESOURCE_STATE_UNKNOWN, RESOURCE_STATE_CONSTANT_BUFFER, STATE_TRANSITION_FLAG_UPDATE_STATE};
pContext->TransitionResourceStates(1, &Barrier);
}
else
{
UNEXPECTED("Material SRB cache must not be null");
}
}

} // namespace USD
Expand Down
2 changes: 2 additions & 0 deletions Hydrogent/src/HnRenderDelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,8 @@ HnRenderDelegate::HnRenderDelegate(const CreateInfo& CI) :
},
m_ShadowMapManager{CreateShadowMapManager(CI)}
{
HnMaterial::InitSRBCache(*this);

const Uint32 ConstantBufferOffsetAlignment = m_pDevice->GetAdapterInfo().Buffer.ConstantBufferOffsetAlignment;

m_MainPassFrameAttribsAlignedSize = AlignUpNonPw2(m_USDRenderer->GetPRBFrameAttribsSize(), ConstantBufferOffsetAlignment);
Expand Down

0 comments on commit 957b53a

Please sign in to comment.