Skip to content

Commit

Permalink
Hydrogent: set joint buffer size based on the max number of joints
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMostDiligent committed Oct 31, 2024
1 parent c6da2ba commit 386a74e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
45 changes: 26 additions & 19 deletions Hydrogent/src/HnRenderDelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ static RefCntAutoPtr<IBuffer> CreatePrimitiveAttribsCB(IRenderDevice* pDevice)
return PrimitiveAttribsCB;
}

static RefCntAutoPtr<IBuffer> CreateJointsBuffer(IRenderDevice* pDevice, BUFFER_MODE Mode)
static RefCntAutoPtr<IBuffer> CreateJointsBuffer(IRenderDevice* pDevice, USD_Renderer::JOINTS_BUFFER_MODE Mode, Uint32& MaxJointCount)
{
BufferDesc Desc;
Desc.Name = "Joint transforms";
Expand All @@ -123,15 +123,31 @@ static RefCntAutoPtr<IBuffer> CreateJointsBuffer(IRenderDevice* pDevice, BUFFER_
Desc.CPUAccessFlags = CPU_ACCESS_WRITE;
}

if (Mode == BUFFER_MODE_UNDEFINED)
auto GetMaxAllowedJointCount = [](Uint64 BufferSize) {
// (PreTransform matrix + MaxJointCount matrices) * 2
return static_cast<Uint32>(BufferSize / sizeof(float4x4) / 2 - 1);
};

if (Mode == USD_Renderer::JOINTS_BUFFER_MODE_UNIFORM)
{
Desc.BindFlags = BIND_UNIFORM_BUFFER;

const Uint32 MaxAllowedJointCount = GetMaxAllowedJointCount(Desc.Size);
if (MaxJointCount > MaxAllowedJointCount)
{
LOG_WARNING_MESSAGE("The maximum number of joints (", MaxJointCount, ") exceeds the limit (", MaxAllowedJointCount, ") for the uniform buffer. Clamping the number of joints.");
MaxJointCount = MaxAllowedJointCount;
}
}
else if (Mode == BUFFER_MODE_STRUCTURED)
else if (Mode == USD_Renderer::JOINTS_BUFFER_MODE_STRUCTURED)
{
Desc.BindFlags = BIND_SHADER_RESOURCE;
Desc.Mode = Mode;
Desc.Mode = BUFFER_MODE_STRUCTURED;
Desc.ElementByteStride = sizeof(float4x4);

Desc.Size = std::max<Uint64>(Desc.Size, USD_Renderer::GetJointsDataSize(MaxJointCount, /*UseSkinPreTransform = */ true, /*UsePrevFrameTransforms = */ true));
// In structured mode, the entire buffer may be used to store joints for a single mesh.
MaxJointCount = GetMaxAllowedJointCount(Desc.Size);
}
else
{
Expand Down Expand Up @@ -176,22 +192,13 @@ static std::shared_ptr<USD_Renderer> CreateUSDRenderer(const HnRenderDelegate::C
if (RenderDelegateCI.MaxJointCount > 0)
{
USDRendererCI.UseSkinPreTransform = true;
USDRendererCI.MaxJointCount = RenderDelegateCI.MaxJointCount;

if (DeviceInfo.IsGLDevice())
{
pJointsCB = CreateJointsBuffer(RenderDelegateCI.pDevice, BUFFER_MODE_UNDEFINED);
USDRendererCI.MaxJointCount = RenderDelegateCI.MaxJointCount;
USDRendererCI.JointsBufferMode = USD_Renderer::JOINTS_BUFFER_MODE_UNIFORM;
}
else
{
pJointsCB = CreateJointsBuffer(RenderDelegateCI.pDevice, BUFFER_MODE_STRUCTURED);
// In structured mode, the entire buffer may be used to store joints for a single mesh.
const Uint64 JointsBufferSize = pJointsCB->GetDesc().Size;
// (PreTransform matrix + MaxJointCount matrices) * 2
USDRendererCI.MaxJointCount = static_cast<Uint32>(JointsBufferSize / sizeof(float4x4) / 2 - 1);
USDRendererCI.JointsBufferMode = USD_Renderer::JOINTS_BUFFER_MODE_STRUCTURED;
}
USDRendererCI.JointsBufferMode = DeviceInfo.IsGLDevice() ?
USD_Renderer::JOINTS_BUFFER_MODE_UNIFORM :
USD_Renderer::JOINTS_BUFFER_MODE_STRUCTURED;

pJointsCB = CreateJointsBuffer(RenderDelegateCI.pDevice, USDRendererCI.JointsBufferMode, USDRendererCI.MaxJointCount);
}

USDRendererCI.ColorTargetIndex = HnFrameRenderTargets::GBUFFER_TARGET_SCENE_COLOR;
Expand Down
3 changes: 2 additions & 1 deletion PBR/src/PBR_Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#include <array>
#include <vector>
#include <limits.h>

#include "RenderStateCache.hpp"
#include "GraphicsUtilities.h"
Expand Down Expand Up @@ -1168,7 +1169,7 @@ ShaderMacroHelper PBR_Renderer::DefineMacros(const PSOKey& Key) const
const PSO_FLAGS PSOFlags = Key.GetFlags();

ShaderMacroHelper Macros;
Macros.Add("MAX_JOINT_COUNT", static_cast<int>(m_Settings.MaxJointCount));
Macros.Add("MAX_JOINT_COUNT", m_Settings.JointsBufferMode == JOINTS_BUFFER_MODE_UNIFORM ? static_cast<int>(m_Settings.MaxJointCount) : INT_MAX);
Macros.Add("JOINTS_BUFFER_MODE_UNIFORM", static_cast<int>(JOINTS_BUFFER_MODE_UNIFORM));
Macros.Add("JOINTS_BUFFER_MODE_STRUCTURED", static_cast<int>(JOINTS_BUFFER_MODE_STRUCTURED));
Macros.Add("JOINTS_BUFFER_MODE", static_cast<int>(m_Settings.JointsBufferMode));
Expand Down

0 comments on commit 386a74e

Please sign in to comment.