From 20ffd67fcc81f2def90ee756424b25f3c563c681 Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Tue, 17 Jan 2023 21:54:53 +0800 Subject: [PATCH] Fixed move constructor and assignment for SHBatch and added a check for if a batch is animated --- .../Graphics/MiddleEnd/Batching/SHBatch.cpp | 129 ++++++++++-------- .../src/Graphics/MiddleEnd/Batching/SHBatch.h | 6 +- 2 files changed, 77 insertions(+), 58 deletions(-) diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.cpp index fdbbad24..f281fdb0 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.cpp @@ -37,40 +37,50 @@ namespace SHADE /* SHBatch - Constructors/Destructors */ /*-----------------------------------------------------------------------------------*/ SHBatch::SHBatch(Handle pipeline) - : pipeline{ pipeline } + : pipeline{ pipeline } { if (!pipeline) throw std::invalid_argument("Attempted to create a SHBatch with an invalid SHPipeline!"); + // Check the pipeline and flag it depending on whether or not it is animated + isAnimated = checkIfIsAnimatedPipeline(pipeline); + // Mark all as dirty setAllDirtyFlags(); } SHBatch::SHBatch(SHBatch&& rhs) - : device { rhs.device } - , pipeline { rhs.pipeline } - , referencedMatInstances { std::move(rhs.referencedMatInstances) } - , matBufferDirty { std::move(rhs.matBufferDirty) } - , subBatches { std::move(rhs.subBatches) } - , isDirty { std::move(rhs.isDirty) } - , drawData { std::move(rhs.drawData) } - , transformData { std::move(rhs.transformData) } - , instancedIntegerData { std::move(rhs.instancedIntegerData) } - , matPropsData { std::move(rhs.matPropsData) } - , matPropsDataSize { rhs.matPropsDataSize } - , singleMatPropAlignedSize { rhs.singleMatPropAlignedSize } - , singleMatPropSize { rhs.singleMatPropSize } - , isCPUBuffersDirty { rhs.isCPUBuffersDirty } - , drawDataBuffer { rhs.drawDataBuffer } - , transformDataBuffer { rhs.transformDataBuffer } - , instancedIntegerBuffer { rhs.instancedIntegerBuffer } - , matPropsBuffer { rhs.matPropsBuffer } - , instanceDataDescSet { rhs.instanceDataDescSet } + : device { rhs.device } + , isAnimated { rhs.isAnimated } + , pipeline { rhs.pipeline } + , referencedMatInstances { std::move(rhs.referencedMatInstances) } + , matBufferDirty { std::move(rhs.matBufferDirty) } + , subBatches { std::move(rhs.subBatches) } + , isDirty { std::move(rhs.isDirty) } + , drawData { std::move(rhs.drawData) } + , transformData { std::move(rhs.transformData) } + , instancedIntegerData { std::move(rhs.instancedIntegerData) } + , matPropsData { std::move(rhs.matPropsData) } + , matPropsDataSize { rhs.matPropsDataSize } + , singleMatPropAlignedSize { rhs.singleMatPropAlignedSize } + , singleMatPropSize { rhs.singleMatPropSize } + , boneMatrixData { std::move(rhs.boneMatrixData) } + , boneMatrixIndices { std::move(rhs.boneMatrixIndices) } + , isCPUBuffersDirty { rhs.isCPUBuffersDirty } + , drawDataBuffer { rhs.drawDataBuffer } + , transformDataBuffer { rhs.transformDataBuffer } + , instancedIntegerBuffer { rhs.instancedIntegerBuffer } + , matPropsBuffer { rhs.matPropsBuffer } + , boneMatrixBuffer { rhs.boneMatrixBuffer } + , boneMatrixFirstIndexBuffer { rhs.boneMatrixFirstIndexBuffer } + , instanceDataDescSet { rhs.instanceDataDescSet } { - rhs.drawDataBuffer = {}; - rhs.transformDataBuffer = {}; - rhs.instancedIntegerBuffer = {}; - rhs.matPropsBuffer = {}; + rhs.drawDataBuffer = {}; + rhs.transformDataBuffer = {}; + rhs.instancedIntegerBuffer = {}; + rhs.matPropsBuffer = {}; + rhs.boneMatrixBuffer = {}; + rhs.boneMatrixFirstIndexBuffer = {}; rhs.instanceDataDescSet = {}; } @@ -79,31 +89,38 @@ namespace SHADE if (this == &rhs) return *this; - device = rhs.device ; - pipeline = rhs.pipeline ; - referencedMatInstances = std::move(rhs.referencedMatInstances); - matBufferDirty = std::move(rhs.matBufferDirty) ; - subBatches = std::move(rhs.subBatches) ; - isDirty = std::move(rhs.isDirty) ; - drawData = std::move(rhs.drawData) ; - transformData = std::move(rhs.transformData) ; - instancedIntegerData = std::move(rhs.instancedIntegerData) ; - matPropsData = std::move(rhs.matPropsData) ; - matPropsDataSize = rhs.matPropsDataSize ; - singleMatPropAlignedSize = rhs.singleMatPropAlignedSize ; - singleMatPropSize = rhs.singleMatPropSize ; - isCPUBuffersDirty = rhs.isCPUBuffersDirty ; - drawDataBuffer = rhs.drawDataBuffer ; - transformDataBuffer = rhs.transformDataBuffer ; - instancedIntegerBuffer = rhs.instancedIntegerBuffer ; - matPropsBuffer = rhs.matPropsBuffer ; - instanceDataDescSet = rhs.instanceDataDescSet ; + device = rhs.device ; + isAnimated = rhs.isAnimated ; + pipeline = rhs.pipeline ; + referencedMatInstances = std::move(rhs.referencedMatInstances); + matBufferDirty = std::move(rhs.matBufferDirty) ; + subBatches = std::move(rhs.subBatches) ; + isDirty = std::move(rhs.isDirty) ; + drawData = std::move(rhs.drawData) ; + transformData = std::move(rhs.transformData) ; + instancedIntegerData = std::move(rhs.instancedIntegerData) ; + matPropsData = std::move(rhs.matPropsData) ; + matPropsDataSize = rhs.matPropsDataSize ; + singleMatPropAlignedSize = rhs.singleMatPropAlignedSize ; + singleMatPropSize = rhs.singleMatPropSize ; + boneMatrixData = std::move(rhs.boneMatrixData) ; + boneMatrixIndices = std::move(rhs.boneMatrixIndices) ; + isCPUBuffersDirty = rhs.isCPUBuffersDirty ; + drawDataBuffer = rhs.drawDataBuffer ; + transformDataBuffer = rhs.transformDataBuffer ; + instancedIntegerBuffer = rhs.instancedIntegerBuffer ; + matPropsBuffer = rhs.matPropsBuffer ; + boneMatrixBuffer = rhs.boneMatrixBuffer ; + boneMatrixFirstIndexBuffer = rhs.boneMatrixFirstIndexBuffer ; + instanceDataDescSet = rhs.instanceDataDescSet ; // Unset values - rhs.drawDataBuffer = {}; - rhs.transformDataBuffer = {}; - rhs.instancedIntegerBuffer = {}; - rhs.matPropsBuffer = {}; + rhs.drawDataBuffer = {}; + rhs.transformDataBuffer = {}; + rhs.instancedIntegerBuffer = {}; + rhs.matPropsBuffer = {}; + rhs.boneMatrixBuffer = {}; + rhs.boneMatrixFirstIndexBuffer = {}; rhs.instanceDataDescSet = {}; return *this; @@ -363,21 +380,19 @@ namespace SHADE // Populate on the CPU for (auto& subBatch : subBatches) - for (auto rendId : subBatch.Renderables) + for (auto rendId : subBatch.Renderables) + { + auto* renderable = SHComponentManager::GetComponent(rendId); + instancedIntegerData.emplace_back(SHInstancedIntegerData { - auto* renderable = SHComponentManager::GetComponent(rendId); - instancedIntegerData.emplace_back(SHInstancedIntegerData - { - rendId, - renderable->GetLightLayer() - } - ); - } + rendId, + renderable->GetLightLayer() + }); + } // Transfer to GPU if (instancedIntegerBuffer[frameIndex] && !drawData.empty()) instancedIntegerBuffer[frameIndex]->WriteToMemory(instancedIntegerData.data(), static_cast(instancedIntegerData.size() * sizeof(SHInstancedIntegerData)), 0, 0); - } void SHBatch::UpdateAnimationBuffer(uint32_t frameIndex) diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.h b/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.h index 913a7a04..86e17035 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.h @@ -97,6 +97,7 @@ namespace SHADE bool IsEmpty() const noexcept { return subBatches.empty(); } Handle GetTransformBuffer(uint32_t frameIndex) const noexcept; Handle GetMDIBuffer(uint32_t frameIndex) const noexcept; + bool IsAnimated() const noexcept { return isAnimated; } private: /*---------------------------------------------------------------------------------*/ @@ -111,6 +112,8 @@ namespace SHADE /*---------------------------------------------------------------------------------*/ // Resources Handle device; + // Config + bool isAnimated; // Whether the material supports animation // Batch Properties Handle pipeline; std::unordered_set> referencedMatInstances; @@ -126,7 +129,7 @@ namespace SHADE Byte matPropsDataSize = 0; Byte singleMatPropAlignedSize = 0; Byte singleMatPropSize = 0; - std::vector boneMatrixData; + std::vector boneMatrixData; // 0th element is always an identity matrix std::vector boneMatrixIndices; bool isCPUBuffersDirty = true; // GPU Buffers @@ -143,5 +146,6 @@ namespace SHADE /*-----------------------------------------------------------------------------*/ void setAllDirtyFlags(); void rebuildDescriptorSetBuffers(uint32_t frameIndex, Handle descPool); + static bool checkIfIsAnimatedPipeline(Handle pipeline); }; }