diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.cpp index 08ffa88e..c2090fc0 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.cpp @@ -86,6 +86,23 @@ namespace SHADE matPropsBuffer.Free(); } + void SHBatch::UpdateTransformBuffer() + { + // Reset Transform Data + transformData.clear(); + + // Populate on the CPU + for (auto& subBatch : subBatches) + for (const SHRenderable* renderable : subBatch.Renderables) + { + // Transform + transformData.emplace_back(renderable->TransformMatrix); + } + + // Transfer to GPU + transformDataBuffer->WriteToMemory(transformData.data(), transformData.size() * sizeof(SHMatrix), 0, 0); + } + void SHBatch::Build(Handle device) { // No need to build as there are no changes @@ -159,36 +176,27 @@ namespace SHADE // Send all buffered data to the GPU buffers using BuffUsage = vk::BufferUsageFlagBits; // - Draw Data - if (drawDataBuffer) - drawDataBuffer->Unmap(); const uint32_t DRAW_DATA_BYTES = static_cast(drawData.size() * sizeof(vk::DrawIndexedIndirectCommand)); SHVkUtil::EnsureBufferAndCopyHostVisibleData ( device, drawDataBuffer, drawData.data(), DRAW_DATA_BYTES, BuffUsage::eIndirectBuffer ); - drawDataBuffer->Map(); // - Transform Buffer - if (transformDataBuffer) - transformDataBuffer->Unmap(); const uint32_t TF_DATA_BYTES = static_cast(transformData.size() * sizeof(SHMatrix)); SHVkUtil::EnsureBufferAndCopyHostVisibleData ( device, transformDataBuffer, transformData.data(), TF_DATA_BYTES, BuffUsage::eVertexBuffer ); - transformDataBuffer->Map(); // - Material Properties Buffer if (!EMPTY_MAT_PROPS) { - if (matPropsBuffer) - matPropsBuffer->Unmap(); SHVkUtil::EnsureBufferAndCopyHostVisibleData ( device, matPropsBuffer, matPropsData.get(), static_cast(matPropTotalBytes), BuffUsage::eStorageBuffer ); - matPropsBuffer->Map(); } isDirty = false; diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.h b/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.h index a5b808ff..f438d6c1 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.h @@ -71,6 +71,7 @@ namespace SHADE void Add(const SHRenderable* renderable); void Remove(const SHRenderable* renderable); void Clear(); + void UpdateTransformBuffer(); void Build(Handle device); void Draw(Handle cmdBuffer); diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatcher.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatcher.cpp index c92d9585..ea216ed5 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatcher.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatcher.cpp @@ -109,6 +109,14 @@ namespace SHADE superBatches.clear(); } + void SHBatcher::UpdateTransformBuffer() + { + for (auto& batch : superBatches) + { + batch->UpdateTransformBuffer(); + } + } + void SHBatcher::RegisterSuperBatch(Handle superBatch) { superBatches.emplace_back(superBatch); diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatcher.h b/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatcher.h index 4c8ac811..89dea2eb 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatcher.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatcher.h @@ -53,6 +53,7 @@ namespace SHADE void RemoveFromBatch(SHRenderable const* renderable); void FinaliseBatches(Handle device); void ClearBatches(); + void UpdateTransformBuffer(); void RegisterSuperBatch(Handle superBatch); void DeregisterSuperBatch(Handle superBatch); diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHSuperBatch.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHSuperBatch.cpp index 09d2a720..8b7ea619 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHSuperBatch.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHSuperBatch.cpp @@ -78,6 +78,14 @@ namespace SHADE batches.clear(); } + void SHSuperBatch::UpdateTransformBuffer() + { + for (auto& batch : batches) + { + batch.UpdateTransformBuffer(); + } + } + void SHSuperBatch::Build(Handle device) noexcept { // Build all batches diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHSuperBatch.h b/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHSuperBatch.h index b44dd349..6151e56e 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHSuperBatch.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHSuperBatch.h @@ -55,6 +55,7 @@ namespace SHADE void Add(const SHRenderable* renderable) noexcept; void Remove(const SHRenderable* renderable) noexcept; void Clear() noexcept; + void UpdateTransformBuffer(); void Build(Handle device) noexcept; void Draw(Handle cmdBuffer) noexcept; diff --git a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.cpp b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.cpp index d02e8f7d..f53f3e99 100644 --- a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.cpp +++ b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.cpp @@ -348,6 +348,9 @@ namespace SHADE void SHSubpass::Execute(Handle& commandBuffer) noexcept { + // Ensure correct transforms are provided + superBatch->UpdateTransformBuffer(); + // Draw all the batches superBatch->Draw(commandBuffer); @@ -626,6 +629,11 @@ namespace SHADE batcher.FinaliseBatches(logicalDeviceHdl); } + void SHRenderGraphNode::UpdateBatchTransforms() + { + batcher.UpdateTransformBuffer(); + } + /***************************************************************************/ /*! diff --git a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.h b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.h index cbb9586d..16770475 100644 --- a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.h +++ b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.h @@ -231,6 +231,7 @@ namespace SHADE void Execute (Handle& commandBuffer, uint32_t frameIndex) noexcept; Handle GetOrCreatePipeline (std::pair, Handle> const& vsFsPair, Handle subpass) noexcept; void FinaliseBatch(); + void UpdateBatchTransforms(); /*-----------------------------------------------------------------------*/ /* SETTERS AND GETTERS */