Skip to content

Commit

Permalink
Hydrogent: batch joint transform uploads
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMostDiligent committed Sep 22, 2024
1 parent b891e68 commit bd8b975
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 77 deletions.
10 changes: 10 additions & 0 deletions Hydrogent/interface/HnMaterial.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@ class HnMaterial final : public pxr::HdMaterial
m_PrimitiveAttribsVar->SetBufferOffset(PrimitiveAttribsOffset);
return m_SRB;
}
void SetJointsBufferOffset(Uint32 Offset) const
{
if (m_JointTransformsVar == nullptr)
{
UNEXPECTED("Joint transforms variable is not initialized, which indicates that skinning is not enabled in the renderer.");
return;
}
m_JointTransformsVar->SetBufferOffset(Offset);
}

const GLTF::Material& GetMaterialData() const { return m_MaterialData; }

Expand Down Expand Up @@ -147,6 +156,7 @@ class HnMaterial final : public pxr::HdMaterial

RefCntAutoPtr<IShaderResourceBinding> m_SRB;
IShaderResourceVariable* m_PrimitiveAttribsVar = nullptr; // cbPrimitiveAttribs
IShaderResourceVariable* m_JointTransformsVar = nullptr; // cbJointTransforms

GLTF::Material m_MaterialData;

Expand Down
2 changes: 1 addition & 1 deletion Hydrogent/interface/HnRenderDelegate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ class HnRenderDelegate final : public pxr::HdRenderDelegate
/// The maximum number of joints.
///
/// If set to 0, skinning will be disabled.
Uint32 MaxJointCount = 64;
Uint32 MaxJointCount = 128;
};
static std::unique_ptr<HnRenderDelegate> Create(const CreateInfo& CI);

Expand Down
10 changes: 7 additions & 3 deletions Hydrogent/interface/HnRenderPass.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,18 +212,22 @@ class HnRenderPass final : public pxr::HdRenderPass
struct PendingDrawItem
{
const DrawListItem& ListItem;
const Uint32 BufferOffset;
Uint32 DrawCount = 1;
const Uint32 AttribsBufferOffset;
Uint32 JointsBufferOffset = ~0u;
Uint32 DrawCount = 1;
};

// Draw list items to be rendered in the current batch.
std::vector<PendingDrawItem> m_PendingDrawItems;
// Rendering order of the draw list items sorted by the PSO.
std::vector<Uint32> m_RenderOrder;

// Scratch space prepare data for the primitive attributes buffer.
// Scratch space to prepare data for the primitive attributes buffer.
std::vector<Uint8> m_PrimitiveAttribsData;

// Scratch space to prepare data for the joints buffer.
std::vector<Uint8> m_JointsData;

// Scratch space for the MultiDraw/MultiDrawIndexed command items.
std::vector<Uint8> m_ScratchSpace;

Expand Down
3 changes: 3 additions & 0 deletions Hydrogent/src/HnMaterial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,7 @@ void HnMaterial::UpdateSRB(HnRenderDelegate& RendererDelegate)
{
m_SRB.Release();
m_PrimitiveAttribsVar = nullptr;
m_JointTransformsVar = nullptr;
m_PBRPrimitiveAttribsBufferRange = 0;
m_AtlasVersion = AtlasVersion;
}
Expand Down Expand Up @@ -1009,6 +1010,8 @@ void HnMaterial::UpdateSRB(HnRenderDelegate& RendererDelegate)
const Uint32 PBRPrimitiveAttribsSize = UsdRenderer.GetPBRPrimitiveAttribsSize(PSOFlags);
const Uint32 PrimitiveArraySize = std::max(UsdRenderer.GetSettings().PrimitiveArraySize, 1u);
SRBCache->UpdatePrimitiveAttribsBufferRange(m_SRB, PBRPrimitiveAttribsSize * PrimitiveArraySize);
m_JointTransformsVar = m_SRB->GetVariableByName(SHADER_TYPE_VERTEX, "cbJointTransforms");
VERIFY_EXPR(m_JointTransformsVar != nullptr || RendererSettings.MaxJointCount == 0);
}
else
{
Expand Down
20 changes: 19 additions & 1 deletion Hydrogent/src/HnRenderDelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,25 @@ static RefCntAutoPtr<IBuffer> CreatePrimitiveAttribsCB(IRenderDevice* pDevice)
}
// Allocate a large buffer to batch primitive draw calls
RefCntAutoPtr<IBuffer> PrimitiveAttribsCB;
CreateUniformBuffer(pDevice, Size, "PBR frame attribs CB", &PrimitiveAttribsCB, Usage);
CreateUniformBuffer(pDevice, Size, "PBR primitive attribs", &PrimitiveAttribsCB, Usage);
return PrimitiveAttribsCB;
}

static RefCntAutoPtr<IBuffer> CreateJointsCB(IRenderDevice* pDevice)
{
Uint64 Size = 65536;
USAGE Usage = USAGE_DYNAMIC;
if (pDevice->GetDeviceInfo().IsGLDevice())
{
// On OpenGL, use USAGE_DEFAULT buffer and update it
// with UpdateBuffer() method.
Usage = USAGE_DEFAULT;
}
RefCntAutoPtr<IBuffer> JointsCB;
CreateUniformBuffer(pDevice, Size, "Joint transforms", &JointsCB, Usage);
return JointsCB;
}

static std::shared_ptr<USD_Renderer> CreateUSDRenderer(const HnRenderDelegate::CreateInfo& RenderDelegateCI,
IBuffer* pPrimitiveAttribsCB,
IObject* MaterialSRBCache)
Expand Down Expand Up @@ -209,6 +224,9 @@ static std::shared_ptr<USD_Renderer> CreateUSDRenderer(const HnRenderDelegate::C

USDRendererCI.pPrimitiveAttribsCB = pPrimitiveAttribsCB;

RefCntAutoPtr<IBuffer> pJointsCB = CreateJointsCB(RenderDelegateCI.pDevice);
USDRendererCI.pJointsBuffer = pJointsCB;

return std::make_shared<USD_Renderer>(RenderDelegateCI.pDevice, RenderDelegateCI.pRenderStateCache, RenderDelegateCI.pContext, USDRendererCI);
}

Expand Down
Loading

0 comments on commit bd8b975

Please sign in to comment.