git.s-ol.nu ~forks/DiligentFX / 672e747
Reworked joints buffer to be dynamic constant buffer assiduous 10 months ago
4 changed file(s) with 44 addition(s) and 37 deletion(s). Raw diff Collapse all Expand all
2929 #include <unordered_map>
3030 #include <functional>
3131 #include <mutex>
32 #include <vector>
3233
3334 #include "../../../DiligentCore/Graphics/GraphicsEngine/interface/DeviceContext.h"
3435 #include "../../../DiligentCore/Graphics/GraphicsEngine/interface/RenderDevice.h"
274275 return it != m_SRBCache.end() ? it->second.RawPtr() : nullptr;
275276 }
276277
278 void Begin(IDeviceContext* pCtx);
279
277280 private:
278281 void PrecomputeBRDF(IRenderDevice* pDevice,
279282 IDeviceContext* pCtx);
393396 const Uint32 FirstIndexLocation;
394397 const Uint32 BaseVertex;
395398
399 const GLTF::Mesh* pLastAnimatedMesh = nullptr;
400
396401 void Render(const GLTF::Node& Node,
397402 GLTF::Material::ALPHA_MODE AlphaMode);
398403 };
127127 {
128128 CreateUniformBuffer(pDevice, sizeof(GLTFNodeShaderTransforms), "GLTF node transforms CB", &m_TransformsCB);
129129 CreateUniformBuffer(pDevice, sizeof(GLTFMaterialShaderInfo) + sizeof(GLTFRendererShaderParameters), "GLTF attribs CB", &m_GLTFAttribsCB);
130
131 {
132 BufferDesc BuffDesc;
133 BuffDesc.Name = "GLTF joint tranforms";
134 BuffDesc.Usage = USAGE_DEFAULT;
135 BuffDesc.uiSizeInBytes = sizeof(float4x4) * m_Settings.MaxJointCount;
136 BuffDesc.BindFlags = BIND_SHADER_RESOURCE;
137 BuffDesc.Mode = BUFFER_MODE_STRUCTURED;
138 BuffDesc.ElementByteStride = sizeof(float4x4);
139 pDevice->CreateBuffer(BuffDesc, nullptr, &m_JointsBuffer);
140 }
130 CreateUniformBuffer(pDevice, static_cast<Uint32>(sizeof(float4x4) * m_Settings.MaxJointCount), "GLTF joint tranforms", &m_JointsBuffer);
141131
142132 // clang-format off
143133 StateTransitionDesc Barriers[] =
144134 {
145135 {m_TransformsCB, RESOURCE_STATE_UNKNOWN, RESOURCE_STATE_CONSTANT_BUFFER, true},
146136 {m_GLTFAttribsCB, RESOURCE_STATE_UNKNOWN, RESOURCE_STATE_CONSTANT_BUFFER, true},
147 {m_JointsBuffer, RESOURCE_STATE_UNKNOWN, RESOURCE_STATE_SHADER_RESOURCE, true}
137 {m_JointsBuffer, RESOURCE_STATE_UNKNOWN, RESOURCE_STATE_CONSTANT_BUFFER, true}
148138 };
149139 // clang-format on
150140 pCtx->TransitionResourceStates(_countof(Barriers), Barriers);
250240 ShaderCI.pShaderSourceStreamFactory = &DiligentFXShaderSourceStreamFactory::GetInstance();
251241
252242 ShaderMacroHelper Macros;
243 Macros.AddShaderMacro("MAX_JOINT_COUNT", m_Settings.MaxJointCount);
253244 Macros.AddShaderMacro("ALLOW_DEBUG_VIEW", m_Settings.AllowDebugView);
254245 Macros.AddShaderMacro("TONE_MAPPING_MODE", "TONE_MAPPING_MODE_UNCHARTED2");
255246 Macros.AddShaderMacro("GLTF_PBR_USE_IBL", m_Settings.UseIBL);
296287 // clang-format off
297288 std::vector<ShaderResourceVariableDesc> Vars =
298289 {
299 {SHADER_TYPE_VERTEX, "cbTransforms", SHADER_RESOURCE_VARIABLE_TYPE_STATIC},
300 {SHADER_TYPE_PIXEL, "cbGLTFAttribs", SHADER_RESOURCE_VARIABLE_TYPE_STATIC},
301 {SHADER_TYPE_VERTEX, "g_Joints", SHADER_RESOURCE_VARIABLE_TYPE_STATIC}
290 {SHADER_TYPE_VERTEX, "cbTransforms", SHADER_RESOURCE_VARIABLE_TYPE_STATIC},
291 {SHADER_TYPE_PIXEL, "cbGLTFAttribs", SHADER_RESOURCE_VARIABLE_TYPE_STATIC},
292 {SHADER_TYPE_VERTEX, "cbJointTransforms", SHADER_RESOURCE_VARIABLE_TYPE_STATIC}
302293 };
303294 // clang-format on
304295
393384 // clang-format off
394385 PSO->GetStaticVariableByName(SHADER_TYPE_VERTEX, "cbTransforms")->Set(m_TransformsCB);
395386 PSO->GetStaticVariableByName(SHADER_TYPE_PIXEL, "cbGLTFAttribs")->Set(m_GLTFAttribsCB);
396 PSO->GetStaticVariableByName(SHADER_TYPE_VERTEX, "g_Joints")->Set(m_JointsBuffer->GetDefaultView(BUFFER_VIEW_SHADER_RESOURCE));
387 PSO->GetStaticVariableByName(SHADER_TYPE_VERTEX, "cbJointTransforms")->Set(m_JointsBuffer);
397388 // clang-format on
398389 }
399390 }
795786 pTransforms->JointCount = static_cast<int>(JointCount);
796787 }
797788
798 if (JointCount != 0)
789 if (JointCount != 0 && pLastAnimatedMesh != Node._Mesh.get())
799790 {
800 // TODO: rework as dynamic buffer - need to map it every frame before the first use
801 pCtx->UpdateBuffer(Renderer.m_JointsBuffer, 0, static_cast<Uint32>(JointCount * sizeof(float4x4)), Node._Mesh->Transforms.jointMatrices.data(), RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
802 StateTransitionDesc Barrier{Renderer.m_JointsBuffer, RESOURCE_STATE_UNKNOWN, RESOURCE_STATE_SHADER_RESOURCE, true};
803 pCtx->TransitionResourceStates(1, &Barrier);
791 MapHelper<float4x4> pJoints{pCtx, Renderer.m_JointsBuffer, MAP_WRITE, MAP_FLAG_DISCARD};
792 memcpy(pJoints, Node._Mesh->Transforms.jointMatrices.data(), JointCount * sizeof(float4x4));
793 pLastAnimatedMesh = Node._Mesh.get();
804794 }
805795
806796 {
873863 }
874864 }
875865
866 void GLTF_PBR_Renderer::Begin(IDeviceContext* pCtx)
867 {
868 // In next-gen backends, dynamic buffers must be mapped before the first use in every frame
869 MapHelper<float4x4> pJoints{pCtx, m_JointsBuffer, MAP_WRITE, MAP_FLAG_DISCARD};
870 }
871
876872 void GLTF_PBR_Renderer::Render(IDeviceContext* pCtx,
877873 GLTF::Model& GLTFModel,
878874 const RenderInfo& RenderParams,
2020 GLTFNodeShaderTransforms g_Transforms;
2121 }
2222
23 struct JointTransform
23 #ifndef MAX_JOINT_COUNT
24 # define MAX_JOINT_COUNT 64
25 #endif
26
27 cbuffer cbJointTransforms
2428 {
25 float4x4 Matrix;
26 };
27 StructuredBuffer<JointTransform> g_Joints;
29 float4x4 g_Joints[MAX_JOINT_COUNT];
30 }
2831
2932 void main(in GLTF_VS_Input VSIn,
3033 out float4 ClipPos : SV_Position,
4144 {
4245 // Mesh is skinned
4346 float4x4 SkinMat =
44 VSIn.Weight0.x * g_Joints[int(VSIn.Joint0.x)].Matrix +
45 VSIn.Weight0.y * g_Joints[int(VSIn.Joint0.y)].Matrix +
46 VSIn.Weight0.z * g_Joints[int(VSIn.Joint0.z)].Matrix +
47 VSIn.Weight0.w * g_Joints[int(VSIn.Joint0.w)].Matrix;
47 VSIn.Weight0.x * g_Joints[int(VSIn.Joint0.x)] +
48 VSIn.Weight0.y * g_Joints[int(VSIn.Joint0.y)] +
49 VSIn.Weight0.z * g_Joints[int(VSIn.Joint0.z)] +
50 VSIn.Weight0.w * g_Joints[int(VSIn.Joint0.w)];
4851 Transform = mul(Transform, SkinMat);
4952 }
5053
2020 " GLTFNodeShaderTransforms g_Transforms;\n"
2121 "}\n"
2222 "\n"
23 "struct JointTransform\n"
23 "#ifndef MAX_JOINT_COUNT\n"
24 "# define MAX_JOINT_COUNT 64\n"
25 "#endif\n"
26 "\n"
27 "cbuffer cbJointTransforms\n"
2428 "{\n"
25 " float4x4 Matrix;\n"
26 "};\n"
27 "StructuredBuffer<JointTransform> g_Joints;\n"
29 " float4x4 g_Joints[MAX_JOINT_COUNT];\n"
30 "}\n"
2831 " \n"
2932 "void main(in GLTF_VS_Input VSIn,\n"
3033 " out float4 ClipPos : SV_Position,\n"
4144 " {\n"
4245 " // Mesh is skinned\n"
4346 " float4x4 SkinMat = \n"
44 " VSIn.Weight0.x * g_Joints[int(VSIn.Joint0.x)].Matrix +\n"
45 " VSIn.Weight0.y * g_Joints[int(VSIn.Joint0.y)].Matrix +\n"
46 " VSIn.Weight0.z * g_Joints[int(VSIn.Joint0.z)].Matrix +\n"
47 " VSIn.Weight0.w * g_Joints[int(VSIn.Joint0.w)].Matrix;\n"
47 " VSIn.Weight0.x * g_Joints[int(VSIn.Joint0.x)] +\n"
48 " VSIn.Weight0.y * g_Joints[int(VSIn.Joint0.y)] +\n"
49 " VSIn.Weight0.z * g_Joints[int(VSIn.Joint0.z)] +\n"
50 " VSIn.Weight0.w * g_Joints[int(VSIn.Joint0.w)];\n"
4851 " Transform = mul(Transform, SkinMat);\n"
4952 " }\n"
5053 "\n"