From 8070b2dafa5cb47c65dd68d2f881356dec8dbc8b Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Mon, 19 Sep 2022 00:11:44 +0800 Subject: [PATCH] Accounted for Shaders with no SHShaderBlockInterface --- .../Graphics/MiddleEnd/Batching/SHBatch.cpp | 22 +++++++++++------- .../MiddleEnd/Interface/SHGraphicsConstants.h | 11 +++++++++ .../MiddleEnd/Interface/SHMaterial.cpp | 23 ++++++++++++++----- 3 files changed, 42 insertions(+), 14 deletions(-) diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.cpp index e7c2f9ef..036e97c7 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.cpp @@ -107,18 +107,20 @@ namespace SHADE transformData.reserve(numTotalElements); transformData.clear(); // - Material Properties Data - const Byte SINGLE_MAT_PROPS_SIZE = pipeline->GetPipelineLayout()->GetShaderBlockInterface - ( - SHGraphicsConstants::DescriptorSetIndex::PER_INSTANCE, - SHGraphicsConstants::DescriptorSetBindings::BATCHED_PER_INST_DATA, - vk::ShaderStageFlagBits::eFragment - )->GetBytesRequired(); + const Handle SHADER_INFO = pipeline->GetPipelineLayout()->GetShaderBlockInterface + ( + SHGraphicsConstants::DescriptorSetIndex::PER_INSTANCE, + SHGraphicsConstants::DescriptorSetBindings::BATCHED_PER_INST_DATA, + vk::ShaderStageFlagBits::eFragment + ); + const Byte SINGLE_MAT_PROPS_SIZE = SHADER_INFO->GetBytesRequired(); const Byte MATPROPS_TOTAL_BYTES = drawData.size() * SINGLE_MAT_PROPS_SIZE; if (matPropsDataSize < MATPROPS_TOTAL_BYTES) { matPropsData.reset(new char[MATPROPS_TOTAL_BYTES]); matPropsDataSize = MATPROPS_TOTAL_BYTES; } + const bool EMPTY_MAT_PROPS = MATPROPS_TOTAL_BYTES <= 0; // Build Sub Batches uint32_t nextInstanceIndex = 0; @@ -141,8 +143,11 @@ namespace SHADE // Transform transformData.emplace_back(renderable->TransformMatrix); // Material Properties - renderable->GetMaterial()->ExportProperties(propsCurrPtr); - propsCurrPtr += SINGLE_MAT_PROPS_SIZE; + if (!EMPTY_MAT_PROPS) + { + renderable->GetMaterial()->ExportProperties(propsCurrPtr); + propsCurrPtr += SINGLE_MAT_PROPS_SIZE; + } } } @@ -177,6 +182,7 @@ namespace SHADE /*---------------------------------------------------------------------------------*/ void SHBatch::Draw(Handle cmdBuffer) { + cmdBuffer->BindVertexBuffer(SHGraphicsConstants::VertexBufferBindings::TRANSFORM, transformDataBuffer, 0); cmdBuffer->DrawMultiIndirect(drawDataBuffer, static_cast(drawData.size())); } } diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsConstants.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsConstants.h index b3bd52d0..648be460 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsConstants.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsConstants.h @@ -71,6 +71,17 @@ namespace SHADE static constexpr uint32_t BATCHED_PER_INST_DATA = 0; }; + struct VertexBufferBindings + { + /***************************************************************************/ + /*! + \brief + Vertex buffer bindings for the transform matrix buffer. + */ + /***************************************************************************/ + static constexpr uint32_t TRANSFORM = 4; + }; + /*******************************************************************************/ /*! \brief diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHMaterial.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHMaterial.cpp index 71e72331..36e30010 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHMaterial.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHMaterial.cpp @@ -22,9 +22,17 @@ namespace SHADE return; } - // Allocate memory for properties - propMemorySize = getShaderBlockInterface()->GetBytesRequired(); - propMemory.reset(new char[propMemorySize]); + // Allocate memory for properties + const Handle SHADER_INFO = getShaderBlockInterface(); + propMemorySize = SHADER_INFO ? SHADER_INFO->GetBytesRequired() : 0; + if (propMemorySize <= 0) + { + propMemory.reset(); + } + else + { + propMemory.reset(new char[propMemorySize]); + } ResetProperties(); } @@ -39,17 +47,20 @@ namespace SHADE void SHMaterial::ResetProperties() { // Reset all the properties to default values - memset(propMemory.get(), 0, propMemorySize); + if (propMemory) + memset(propMemory.get(), 0, propMemorySize); } void SHMaterial::ExportProperties(void* dest) const noexcept { - memcpy(dest, propMemory.get(), propMemorySize); + if (propMemory) + memcpy(dest, propMemory.get(), propMemorySize); } size_t SHMaterial::GetPropertiesMemorySize() const noexcept { - return getShaderBlockInterface()->GetBytesRequired(); + const Handle SHADER_INFO = getShaderBlockInterface(); + return SHADER_INFO ? SHADER_INFO->GetBytesRequired() : 0; } /*---------------------------------------------------------------------------------*/