Fixed move constructor and assignment for SHBatch and added a check for if a batch is animated

This commit is contained in:
Kah Wei 2023-01-17 21:54:53 +08:00
parent 2d898851c5
commit 20ffd67fcc
2 changed files with 77 additions and 58 deletions

View File

@ -37,40 +37,50 @@ namespace SHADE
/* SHBatch - Constructors/Destructors */ /* SHBatch - Constructors/Destructors */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
SHBatch::SHBatch(Handle<SHVkPipeline> pipeline) SHBatch::SHBatch(Handle<SHVkPipeline> pipeline)
: pipeline{ pipeline } : pipeline{ pipeline }
{ {
if (!pipeline) if (!pipeline)
throw std::invalid_argument("Attempted to create a SHBatch with an invalid SHPipeline!"); 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 // Mark all as dirty
setAllDirtyFlags(); setAllDirtyFlags();
} }
SHBatch::SHBatch(SHBatch&& rhs) SHBatch::SHBatch(SHBatch&& rhs)
: device { rhs.device } : device { rhs.device }
, pipeline { rhs.pipeline } , isAnimated { rhs.isAnimated }
, referencedMatInstances { std::move(rhs.referencedMatInstances) } , pipeline { rhs.pipeline }
, matBufferDirty { std::move(rhs.matBufferDirty) } , referencedMatInstances { std::move(rhs.referencedMatInstances) }
, subBatches { std::move(rhs.subBatches) } , matBufferDirty { std::move(rhs.matBufferDirty) }
, isDirty { std::move(rhs.isDirty) } , subBatches { std::move(rhs.subBatches) }
, drawData { std::move(rhs.drawData) } , isDirty { std::move(rhs.isDirty) }
, transformData { std::move(rhs.transformData) } , drawData { std::move(rhs.drawData) }
, instancedIntegerData { std::move(rhs.instancedIntegerData) } , transformData { std::move(rhs.transformData) }
, matPropsData { std::move(rhs.matPropsData) } , instancedIntegerData { std::move(rhs.instancedIntegerData) }
, matPropsDataSize { rhs.matPropsDataSize } , matPropsData { std::move(rhs.matPropsData) }
, singleMatPropAlignedSize { rhs.singleMatPropAlignedSize } , matPropsDataSize { rhs.matPropsDataSize }
, singleMatPropSize { rhs.singleMatPropSize } , singleMatPropAlignedSize { rhs.singleMatPropAlignedSize }
, isCPUBuffersDirty { rhs.isCPUBuffersDirty } , singleMatPropSize { rhs.singleMatPropSize }
, drawDataBuffer { rhs.drawDataBuffer } , boneMatrixData { std::move(rhs.boneMatrixData) }
, transformDataBuffer { rhs.transformDataBuffer } , boneMatrixIndices { std::move(rhs.boneMatrixIndices) }
, instancedIntegerBuffer { rhs.instancedIntegerBuffer } , isCPUBuffersDirty { rhs.isCPUBuffersDirty }
, matPropsBuffer { rhs.matPropsBuffer } , drawDataBuffer { rhs.drawDataBuffer }
, instanceDataDescSet { rhs.instanceDataDescSet } , transformDataBuffer { rhs.transformDataBuffer }
, instancedIntegerBuffer { rhs.instancedIntegerBuffer }
, matPropsBuffer { rhs.matPropsBuffer }
, boneMatrixBuffer { rhs.boneMatrixBuffer }
, boneMatrixFirstIndexBuffer { rhs.boneMatrixFirstIndexBuffer }
, instanceDataDescSet { rhs.instanceDataDescSet }
{ {
rhs.drawDataBuffer = {}; rhs.drawDataBuffer = {};
rhs.transformDataBuffer = {}; rhs.transformDataBuffer = {};
rhs.instancedIntegerBuffer = {}; rhs.instancedIntegerBuffer = {};
rhs.matPropsBuffer = {}; rhs.matPropsBuffer = {};
rhs.boneMatrixBuffer = {};
rhs.boneMatrixFirstIndexBuffer = {};
rhs.instanceDataDescSet = {}; rhs.instanceDataDescSet = {};
} }
@ -79,31 +89,38 @@ namespace SHADE
if (this == &rhs) if (this == &rhs)
return *this; return *this;
device = rhs.device ; device = rhs.device ;
pipeline = rhs.pipeline ; isAnimated = rhs.isAnimated ;
referencedMatInstances = std::move(rhs.referencedMatInstances); pipeline = rhs.pipeline ;
matBufferDirty = std::move(rhs.matBufferDirty) ; referencedMatInstances = std::move(rhs.referencedMatInstances);
subBatches = std::move(rhs.subBatches) ; matBufferDirty = std::move(rhs.matBufferDirty) ;
isDirty = std::move(rhs.isDirty) ; subBatches = std::move(rhs.subBatches) ;
drawData = std::move(rhs.drawData) ; isDirty = std::move(rhs.isDirty) ;
transformData = std::move(rhs.transformData) ; drawData = std::move(rhs.drawData) ;
instancedIntegerData = std::move(rhs.instancedIntegerData) ; transformData = std::move(rhs.transformData) ;
matPropsData = std::move(rhs.matPropsData) ; instancedIntegerData = std::move(rhs.instancedIntegerData) ;
matPropsDataSize = rhs.matPropsDataSize ; matPropsData = std::move(rhs.matPropsData) ;
singleMatPropAlignedSize = rhs.singleMatPropAlignedSize ; matPropsDataSize = rhs.matPropsDataSize ;
singleMatPropSize = rhs.singleMatPropSize ; singleMatPropAlignedSize = rhs.singleMatPropAlignedSize ;
isCPUBuffersDirty = rhs.isCPUBuffersDirty ; singleMatPropSize = rhs.singleMatPropSize ;
drawDataBuffer = rhs.drawDataBuffer ; boneMatrixData = std::move(rhs.boneMatrixData) ;
transformDataBuffer = rhs.transformDataBuffer ; boneMatrixIndices = std::move(rhs.boneMatrixIndices) ;
instancedIntegerBuffer = rhs.instancedIntegerBuffer ; isCPUBuffersDirty = rhs.isCPUBuffersDirty ;
matPropsBuffer = rhs.matPropsBuffer ; drawDataBuffer = rhs.drawDataBuffer ;
instanceDataDescSet = rhs.instanceDataDescSet ; transformDataBuffer = rhs.transformDataBuffer ;
instancedIntegerBuffer = rhs.instancedIntegerBuffer ;
matPropsBuffer = rhs.matPropsBuffer ;
boneMatrixBuffer = rhs.boneMatrixBuffer ;
boneMatrixFirstIndexBuffer = rhs.boneMatrixFirstIndexBuffer ;
instanceDataDescSet = rhs.instanceDataDescSet ;
// Unset values // Unset values
rhs.drawDataBuffer = {}; rhs.drawDataBuffer = {};
rhs.transformDataBuffer = {}; rhs.transformDataBuffer = {};
rhs.instancedIntegerBuffer = {}; rhs.instancedIntegerBuffer = {};
rhs.matPropsBuffer = {}; rhs.matPropsBuffer = {};
rhs.boneMatrixBuffer = {};
rhs.boneMatrixFirstIndexBuffer = {};
rhs.instanceDataDescSet = {}; rhs.instanceDataDescSet = {};
return *this; return *this;
@ -363,21 +380,19 @@ namespace SHADE
// Populate on the CPU // Populate on the CPU
for (auto& subBatch : subBatches) for (auto& subBatch : subBatches)
for (auto rendId : subBatch.Renderables) for (auto rendId : subBatch.Renderables)
{
auto* renderable = SHComponentManager::GetComponent<SHRenderable>(rendId);
instancedIntegerData.emplace_back(SHInstancedIntegerData
{ {
auto* renderable = SHComponentManager::GetComponent<SHRenderable>(rendId); rendId,
instancedIntegerData.emplace_back(SHInstancedIntegerData renderable->GetLightLayer()
{ });
rendId, }
renderable->GetLightLayer()
}
);
}
// Transfer to GPU // Transfer to GPU
if (instancedIntegerBuffer[frameIndex] && !drawData.empty()) if (instancedIntegerBuffer[frameIndex] && !drawData.empty())
instancedIntegerBuffer[frameIndex]->WriteToMemory(instancedIntegerData.data(), static_cast<uint32_t>(instancedIntegerData.size() * sizeof(SHInstancedIntegerData)), 0, 0); instancedIntegerBuffer[frameIndex]->WriteToMemory(instancedIntegerData.data(), static_cast<uint32_t>(instancedIntegerData.size() * sizeof(SHInstancedIntegerData)), 0, 0);
} }
void SHBatch::UpdateAnimationBuffer(uint32_t frameIndex) void SHBatch::UpdateAnimationBuffer(uint32_t frameIndex)

View File

@ -97,6 +97,7 @@ namespace SHADE
bool IsEmpty() const noexcept { return subBatches.empty(); } bool IsEmpty() const noexcept { return subBatches.empty(); }
Handle<SHVkBuffer> GetTransformBuffer(uint32_t frameIndex) const noexcept; Handle<SHVkBuffer> GetTransformBuffer(uint32_t frameIndex) const noexcept;
Handle<SHVkBuffer> GetMDIBuffer(uint32_t frameIndex) const noexcept; Handle<SHVkBuffer> GetMDIBuffer(uint32_t frameIndex) const noexcept;
bool IsAnimated() const noexcept { return isAnimated; }
private: private:
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
@ -111,6 +112,8 @@ namespace SHADE
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
// Resources // Resources
Handle<SHVkLogicalDevice> device; Handle<SHVkLogicalDevice> device;
// Config
bool isAnimated; // Whether the material supports animation
// Batch Properties // Batch Properties
Handle<SHVkPipeline> pipeline; Handle<SHVkPipeline> pipeline;
std::unordered_set<Handle<SHMaterialInstance>> referencedMatInstances; std::unordered_set<Handle<SHMaterialInstance>> referencedMatInstances;
@ -126,7 +129,7 @@ namespace SHADE
Byte matPropsDataSize = 0; Byte matPropsDataSize = 0;
Byte singleMatPropAlignedSize = 0; Byte singleMatPropAlignedSize = 0;
Byte singleMatPropSize = 0; Byte singleMatPropSize = 0;
std::vector<SHMatrix> boneMatrixData; std::vector<SHMatrix> boneMatrixData; // 0th element is always an identity matrix
std::vector<uint32_t> boneMatrixIndices; std::vector<uint32_t> boneMatrixIndices;
bool isCPUBuffersDirty = true; bool isCPUBuffersDirty = true;
// GPU Buffers // GPU Buffers
@ -143,5 +146,6 @@ namespace SHADE
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
void setAllDirtyFlags(); void setAllDirtyFlags();
void rebuildDescriptorSetBuffers(uint32_t frameIndex, Handle<SHVkDescriptorPool> descPool); void rebuildDescriptorSetBuffers(uint32_t frameIndex, Handle<SHVkDescriptorPool> descPool);
static bool checkIfIsAnimatedPipeline(Handle<SHVkPipeline> pipeline);
}; };
} }