From 74d6e5cee7ae9c4b075fd742acfbade289fb8834 Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Fri, 13 Jan 2023 18:14:40 +0800 Subject: [PATCH] SHPipelineLibrary now sets the pipeline layouts correctly --- .../Graphics/MiddleEnd/Batching/SHBatch.cpp | 15 ++++--- .../GlobalData/SHGraphicsPredefinedData.cpp | 44 +++++++++++-------- .../GlobalData/SHGraphicsPredefinedData.h | 15 ++++--- .../GlobalData/SHPredefinedDescriptorTypes.h | 2 +- .../MiddleEnd/Interface/SHGraphicsConstants.h | 2 +- .../MiddleEnd/Pipeline/SHPipelineLibrary.cpp | 4 +- .../MiddleEnd/Pipeline/SHPipelineLibrary.h | 4 +- .../RenderGraph/SHRenderGraphNode.cpp | 28 +++++++++++- 8 files changed, 75 insertions(+), 39 deletions(-) diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.cpp index 33a51cbb..824be2b7 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.cpp @@ -581,7 +581,11 @@ namespace SHADE return; // Bind all required objects before drawing - static std::array dynamicOffset{ 0U, static_cast(boneMatrixData.size() * sizeof(SHMatrix)) }; + std::vector dynamicOffset{ 0 }; + if (!boneMatrixData.empty()) + { + dynamicOffset.emplace_back(0); + } cmdBuffer->BeginLabeledSegment("SHBatch for Pipeline #" + std::to_string(pipeline.GetId().Data.Index)); cmdBuffer->BindPipeline(pipeline); cmdBuffer->BindVertexBuffer(SHGraphicsConstants::VertexBufferBindings::TRANSFORM, transformDataBuffer[frameIndex], 0); @@ -649,17 +653,14 @@ namespace SHADE static constexpr uint32_t MATERIAL_DESC_SET_INDEX = 0; /* Create Descriptor Sets if Needed */ - std::vector varDescCounts; PreDefDescLayoutType layoutTypes = {}; if (matPropsData) { - layoutTypes |= PreDefDescLayoutType::MATERIALS; - varDescCounts.push_back(0); + layoutTypes = PreDefDescLayoutType::MATERIALS; } if (!boneMatrixData.empty()) { - layoutTypes |= PreDefDescLayoutType::BONES; - varDescCounts.push_back(0); + layoutTypes = PreDefDescLayoutType::MATERIAL_AND_BONES; } if (matPropsData || !boneMatrixData.empty()) @@ -670,7 +671,7 @@ namespace SHADE instanceDataDescSet[frameIndex] = descPool->Allocate ( SHGraphicsPredefinedData::GetPredefinedDescSetLayouts(layoutTypes), - varDescCounts + { 0 } ); #ifdef _DEBUG const auto& DESC_SETS = instanceDataDescSet[frameIndex]->GetVkHandle(); diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/GlobalData/SHGraphicsPredefinedData.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/GlobalData/SHGraphicsPredefinedData.cpp index bbd1a1b1..35e407e3 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/GlobalData/SHGraphicsPredefinedData.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/GlobalData/SHGraphicsPredefinedData.cpp @@ -20,13 +20,20 @@ namespace SHADE //SHGraphicsPredefinedData::PerSystem SHGraphicsPredefinedData::renderGraphNodeComputeData; void SHGraphicsPredefinedData::InitDescMappings(void) noexcept - { + { perSystemData[SHUtilities::ConvertEnum(SystemType::BATCHING)].descMappings.AddMappings ({ {SHPredefinedDescriptorTypes::STATIC_DATA, 0}, {SHPredefinedDescriptorTypes::CAMERA, 1}, {SHPredefinedDescriptorTypes::PER_INSTANCE_BATCH, 2}, - }); + }); + + perSystemData[SHUtilities::ConvertEnum(SystemType::BATCHING_ANIM)].descMappings.AddMappings + ({ + {SHPredefinedDescriptorTypes::STATIC_DATA, 0}, + {SHPredefinedDescriptorTypes::CAMERA, 1}, + {SHPredefinedDescriptorTypes::PER_INSTANCE_ANIM_BATCH, 2}, + }); perSystemData[SHUtilities::ConvertEnum(SystemType::TEXT_RENDERING)].descMappings.AddMappings ({ @@ -129,14 +136,7 @@ namespace SHADE .BindPoint = SHGraphicsConstants::DescriptorSetBindings::PER_INST_MATERIAL_DATA, .DescriptorCount = 1, }; - SHVkDescriptorSetLayout::Binding boneDataBinding - { - .Type = vk::DescriptorType::eStorageBufferDynamic, - .Stage = vk::ShaderStageFlagBits::eVertex, - .BindPoint = SHGraphicsConstants::DescriptorSetBindings::PER_INST_BONE_DATA, - .DescriptorCount = 1, - }; - Handle materialDataPerInstanceLayout = logicalDevice->CreateDescriptorSetLayout({ materialDataBinding, boneDataBinding }); + Handle materialDataPerInstanceLayout = logicalDevice->CreateDescriptorSetLayout({ materialDataBinding }); SET_VK_OBJ_NAME(logicalDevice, vk::ObjectType::eDescriptorSetLayout, materialDataPerInstanceLayout->GetVkHandle(), "[Descriptor Set Layout] Material Globals"); // font bitmap data (texture) @@ -160,17 +160,16 @@ namespace SHADE Handle fontDataDescSetLayout = logicalDevice->CreateDescriptorSetLayout({ fontBitmapBinding, fontMatrixBinding }); SET_VK_OBJ_NAME(logicalDevice, vk::ObjectType::eDescriptorSetLayout, fontDataDescSetLayout->GetVkHandle(), "[Descriptor Set Layout] Font Data"); - // Bone matrix data - SHVkDescriptorSetLayout::Binding boneMatrixBinding + // For per instance data (transforms, materials, etc.) + SHVkDescriptorSetLayout::Binding boneDataBinding { - .Type = vk::DescriptorType::eStorageBuffer, + .Type = vk::DescriptorType::eStorageBufferDynamic, .Stage = vk::ShaderStageFlagBits::eVertex, - .BindPoint = SHGraphicsConstants::DescriptorSetBindings::BONE_MATRIX_DATA, + .BindPoint = SHGraphicsConstants::DescriptorSetBindings::PER_INST_BONE_DATA, .DescriptorCount = 1, }; - - Handle boneMatricesDescSetLayout = logicalDevice->CreateDescriptorSetLayout({ boneMatrixBinding }); - SET_VK_OBJ_NAME(logicalDevice, vk::ObjectType::eDescriptorSetLayout, boneMatricesDescSetLayout->GetVkHandle(), "[Descriptor Set Layout] Bone Matrix Data"); + Handle materialBoneDataPerInstanceLayout = logicalDevice->CreateDescriptorSetLayout({ materialDataBinding, boneDataBinding }); + SET_VK_OBJ_NAME(logicalDevice, vk::ObjectType::eDescriptorSetLayout, materialBoneDataPerInstanceLayout->GetVkHandle(), "[Descriptor Set Layout] Material and Bone Globals"); predefinedLayouts.push_back(staticGlobalLayout); predefinedLayouts.push_back(lightDataDescSetLayout); @@ -178,13 +177,20 @@ namespace SHADE predefinedLayouts.push_back(materialDataPerInstanceLayout); predefinedLayouts.push_back(fontDataDescSetLayout); predefinedLayouts.push_back({}); - predefinedLayouts.push_back(boneMatricesDescSetLayout); + predefinedLayouts.push_back(materialBoneDataPerInstanceLayout); perSystemData[SHUtilities::ConvertEnum(SystemType::BATCHING)].descSetLayouts = GetPredefinedDescSetLayouts ( SHGraphicsPredefinedData::PredefinedDescSetLayoutTypes::STATIC_DATA | SHGraphicsPredefinedData::PredefinedDescSetLayoutTypes::CAMERA | - SHGraphicsPredefinedData::PredefinedDescSetLayoutTypes::MATERIALS + SHGraphicsPredefinedData::PredefinedDescSetLayoutTypes::MATERIALS + ); + + perSystemData[SHUtilities::ConvertEnum(SystemType::BATCHING_ANIM)].descSetLayouts = GetPredefinedDescSetLayouts + ( + SHGraphicsPredefinedData::PredefinedDescSetLayoutTypes::STATIC_DATA | + SHGraphicsPredefinedData::PredefinedDescSetLayoutTypes::CAMERA | + SHGraphicsPredefinedData::PredefinedDescSetLayoutTypes::MATERIAL_AND_BONES ); perSystemData[SHUtilities::ConvertEnum(SystemType::TEXT_RENDERING)].descSetLayouts = GetPredefinedDescSetLayouts diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/GlobalData/SHGraphicsPredefinedData.h b/SHADE_Engine/src/Graphics/MiddleEnd/GlobalData/SHGraphicsPredefinedData.h index 8c304e4c..c346dc9b 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/GlobalData/SHGraphicsPredefinedData.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/GlobalData/SHGraphicsPredefinedData.h @@ -23,18 +23,19 @@ namespace SHADE // This enum is mainly to initialize a bit field to retrieve bit fields from SHPRedefinedData enum class PredefinedDescSetLayoutTypes : uint64_t { - STATIC_DATA = 0b0000001, - LIGHTS = 0b0000010, - CAMERA = 0b0000100, - MATERIALS = 0b0001000, - FONT = 0b0010000, - SHADOW = 0b0100000, - BONES = 0b1000000, + STATIC_DATA = 0b00000001, + LIGHTS = 0b00000010, + CAMERA = 0b00000100, + MATERIALS = 0b00001000, + FONT = 0b00010000, + SHADOW = 0b00100000, + MATERIAL_AND_BONES = 0b01000000 }; enum class SystemType { BATCHING = 0, + BATCHING_ANIM, TEXT_RENDERING, RENDER_GRAPH_NODE_COMPUTE, NUM_TYPES diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/GlobalData/SHPredefinedDescriptorTypes.h b/SHADE_Engine/src/Graphics/MiddleEnd/GlobalData/SHPredefinedDescriptorTypes.h index 7a7bb826..ffb4685b 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/GlobalData/SHPredefinedDescriptorTypes.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/GlobalData/SHPredefinedDescriptorTypes.h @@ -12,7 +12,7 @@ namespace SHADE LIGHTS, CAMERA, PER_INSTANCE_BATCH, - BONES, + PER_INSTANCE_ANIM_BATCH, FONT, RENDER_GRAPH_RESOURCE, RENDER_GRAPH_NODE_COMPUTE_RESOURCE, diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsConstants.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsConstants.h index 3e8e379e..e967312f 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsConstants.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsConstants.h @@ -173,7 +173,7 @@ namespace SHADE */ /***************************************************************************/ - static constexpr uint32_t BONE_MATRIX_DATA = 0; + static constexpr uint32_t BONE_MATRIX_DATA = 1; }; struct VertexBufferBindings diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Pipeline/SHPipelineLibrary.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Pipeline/SHPipelineLibrary.cpp index baf09a2d..907a094e 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Pipeline/SHPipelineLibrary.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Pipeline/SHPipelineLibrary.cpp @@ -8,12 +8,12 @@ namespace SHADE { - Handle SHPipelineLibrary::CreateGraphicsPipelines(std::pair, Handle> const& vsFsPair, Handle renderpass, Handle subpass) noexcept + Handle SHPipelineLibrary::CreateGraphicsPipelines(std::pair, Handle> const& vsFsPair, Handle renderpass, Handle subpass, SHGraphicsPredefinedData::SystemType systemType) noexcept { SHPipelineLayoutParams params { .shaderModules = {vsFsPair.first, vsFsPair.second}, - .predefinedDescSetLayouts = SHGraphicsPredefinedData::GetSystemData(SHGraphicsPredefinedData::SystemType::BATCHING).descSetLayouts + .predefinedDescSetLayouts = SHGraphicsPredefinedData::GetSystemData(systemType).descSetLayouts }; // Create the pipeline layout diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Pipeline/SHPipelineLibrary.h b/SHADE_Engine/src/Graphics/MiddleEnd/Pipeline/SHPipelineLibrary.h index 5085f21f..ff7d8485 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Pipeline/SHPipelineLibrary.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Pipeline/SHPipelineLibrary.h @@ -3,6 +3,7 @@ #include #include "Graphics/Shaders/SHVkShaderModule.h" #include "Graphics/Pipeline/SHVkPipeline.h" +#include "Graphics/MiddleEnd/GlobalData/SHGraphicsPredefinedData.h" namespace SHADE { @@ -32,7 +33,8 @@ namespace SHADE Handle CreateGraphicsPipelines ( std::pair, Handle> const& vsFsPair, Handle renderpass, - Handle subpass + Handle subpass, + SHGraphicsPredefinedData::SystemType systemType ) noexcept; Handle GetGraphicsPipeline (std::pair, Handle> const& vsFsPair) noexcept; bool CheckGraphicsPipelineExistence (std::pair, Handle> const& vsFsPair) noexcept; diff --git a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNode.cpp b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNode.cpp index 3c412645..1603563e 100644 --- a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNode.cpp +++ b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNode.cpp @@ -583,11 +583,37 @@ namespace SHADE Handle pipeline = pipelineLibrary.GetGraphicsPipeline(vsFsPair); if (!pipeline) { + // default to batching system type + SHGraphicsPredefinedData::SystemType systemType{ SHGraphicsPredefinedData::SystemType::BATCHING }; + auto const& REFLECTED_SETS = vsFsPair.first->GetReflectedData().GetDescriptorBindingInfo().GetReflectedSets(); + + // look for animation set binding in the shader (set 2 binding 1) + for (auto const& set : REFLECTED_SETS) + { + auto const mappings = SHGraphicsPredefinedData::GetMappings(SHGraphicsPredefinedData::SystemType::BATCHING_ANIM); + + // Look for set 2 + if (set->set == mappings.at(SHPredefinedDescriptorTypes::PER_INSTANCE_ANIM_BATCH)) + { + for (int i = 0; i < set->binding_count; i++) + { + // look for binding 1. if found use BATCHING_ANIM system type + if (set->bindings[i]->binding == SHGraphicsConstants::DescriptorSetBindings::BONE_MATRIX_DATA) + { + systemType = SHGraphicsPredefinedData::SystemType::BATCHING_ANIM; + break; + } + } + break; + } + } + pipeline = pipelineLibrary.CreateGraphicsPipelines ( vsFsPair, renderpass, - subpass + subpass, + systemType ); }