Fleshed out SHAnimationComponent more and added preliminary implementation of SHBatch for bone data

This commit is contained in:
Kah Wei 2022-11-21 15:09:15 +08:00
parent 1ff8c9715d
commit 611744f5d4
4 changed files with 177 additions and 109 deletions

View File

@ -13,6 +13,9 @@ of DigiPen Institute of Technology is prohibited.
#include "SHpch.h" #include "SHpch.h"
// Primary Include // Primary Include
#include "SHAnimatorComponent.h" #include "SHAnimatorComponent.h"
// Project Includes
#include "SHRig.h"
#include "Math/SHMatrix.h"
namespace SHADE namespace SHADE
{ {

View File

@ -11,26 +11,48 @@ of DigiPen Institute of Technology is prohibited.
*//*************************************************************************************/ *//*************************************************************************************/
#pragma once #pragma once
// STL Includes
#include <vector>
// External Dependencies // External Dependencies
#include <rttr/registration> #include <rttr/registration>
// Project Includes // Project Includes
#include "ECS_Base/Components/SHComponent.h" #include "ECS_Base/Components/SHComponent.h"
#include "Resource/SHHandle.h"
namespace SHADE namespace SHADE
{ {
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
/* Forward Declarations */ /* Forward Declarations */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
class SHRig;
struct SHMatrix;
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
/* Type Definitions */ /* Type Definitions */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
/// <summary>
/// Component that holds and controls the animation related properties of a skinned
/// mesh.
/// </summary>
class SH_API SHAnimatorComponent final : public SHComponent class SH_API SHAnimatorComponent final : public SHComponent
{ {
public:
/*---------------------------------------------------------------------------------*/
/* Getter Functions */
/*---------------------------------------------------------------------------------*/
const std::vector<SHMatrix>& GetBoneMatrices() const noexcept { return boneMatrices; }
private: private:
/*-------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Data Members */ /* Data Members */
/*-------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
Handle<SHRig> rig;
std::vector<SHMatrix> boneMatrices;
float currPlaybackTime = 0.0f;
/*---------------------------------------------------------------------------------*/
/* RTTR */
/*---------------------------------------------------------------------------------*/
RTTR_ENABLE() RTTR_ENABLE()
}; };
} }

View File

@ -29,12 +29,13 @@ of DigiPen Institute of Technology is prohibited.
#include "Graphics/Descriptors/SHVkDescriptorPool.h" #include "Graphics/Descriptors/SHVkDescriptorPool.h"
#include "Scene/SHSceneManager.h" #include "Scene/SHSceneManager.h"
#include "UI/SHUIComponent.h" #include "UI/SHUIComponent.h"
#include "Animation/SHAnimatorComponent.h"
namespace SHADE namespace SHADE
{ {
/*---------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
/* SHBatch - Usage Functions */ /* SHBatch - Constructors/Destructors */
/*---------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
SHBatch::SHBatch(Handle<SHVkPipeline> pipeline) SHBatch::SHBatch(Handle<SHVkPipeline> pipeline)
: pipeline{ pipeline } : pipeline{ pipeline }
{ {
@ -124,6 +125,9 @@ namespace SHADE
} }
} }
/*-----------------------------------------------------------------------------------*/
/* SHBatch - Usage Functions */
/*-----------------------------------------------------------------------------------*/
void SHBatch::Add(const SHRenderable* renderable) void SHBatch::Add(const SHRenderable* renderable)
{ {
// Ignore if null // Ignore if null
@ -407,8 +411,6 @@ namespace SHADE
// - EID data // - EID data
instancedIntegerData.reserve(numTotalElements); instancedIntegerData.reserve(numTotalElements);
instancedIntegerData.clear(); instancedIntegerData.clear();
// - Material Properties Data // - Material Properties Data
const Handle<SHShaderBlockInterface> SHADER_INFO = pipeline->GetPipelineLayout()->GetShaderBlockInterface const Handle<SHShaderBlockInterface> SHADER_INFO = pipeline->GetPipelineLayout()->GetShaderBlockInterface
( (
@ -429,6 +431,10 @@ namespace SHADE
matPropsDataSize = matPropTotalBytes; matPropsDataSize = matPropTotalBytes;
} }
} }
// - Bone Data
boneMatrixData.clear();
boneMatrixIndices.clear();
boneMatrixIndices.reserve(numTotalElements);
// Build Sub Batches // Build Sub Batches
uint32_t nextInstanceIndex = 0; uint32_t nextInstanceIndex = 0;
@ -499,6 +505,16 @@ namespace SHADE
//propsCurrPtr += singleMatPropAlignedSize; //propsCurrPtr += singleMatPropAlignedSize;
propsCurrPtr += singleMatPropSize; propsCurrPtr += singleMatPropSize;
} }
// Bone Data
auto animator = SHComponentManager::GetComponent_s<SHAnimatorComponent>(rendId);
if (animator)
{
boneMatrixIndices.emplace_back(static_cast<uint32_t>(boneMatrixData.size()));
const auto& BONE_MATRICES = animator->GetBoneMatrices();
boneMatrixData.insert(boneMatrixData.end(), BONE_MATRICES.cbegin(), BONE_MATRICES.cend());
}
} }
} }
@ -533,6 +549,24 @@ namespace SHADE
); );
// - Material Properties Buffer // - Material Properties Buffer
rebuildMaterialBuffers(frameIndex, descPool); rebuildMaterialBuffers(frameIndex, descPool);
// - Bone Buffers
if (!boneMatrixIndices.empty())
{
const uint32_t BONE_IDX_DATA_BYTES = static_cast<uint32_t>(boneMatrixIndices.size() * sizeof(uint32_t));
SHVkUtil::EnsureBufferAndCopyHostVisibleData
(
device, boneFirstIndexBuffer[frameIndex], boneMatrixIndices.data(), BONE_IDX_DATA_BYTES,
BuffUsage::eVertexBuffer,
"Batch Bone Index Buffer"
);
const uint32_t BONE_MTX_DATA_BYTES = static_cast<uint32_t>(boneMatrixData.size() * sizeof(uint32_t));
SHVkUtil::EnsureBufferAndCopyHostVisibleData
(
device, boneMatrixBuffer[frameIndex], boneMatrixData.data(), BONE_MTX_DATA_BYTES,
BuffUsage::eStorageBuffer,
"Batch Bone Matrix Buffer"
);
}
// Mark this frame as no longer dirty // Mark this frame as no longer dirty
isDirty[frameIndex] = false; isDirty[frameIndex] = false;
@ -569,6 +603,11 @@ namespace SHADE
dynamicOffset dynamicOffset
); );
} }
if (boneMatrixBuffer[frameIndex] && boneFirstIndexBuffer[frameIndex])
{
cmdBuffer->BindVertexBuffer(SHGraphicsConstants::VertexBufferBindings::BONE_INDICES, boneFirstIndexBuffer[frameIndex], 0);
// TODO: Bind storage buffer
}
cmdBuffer->DrawMultiIndirect(drawDataBuffer[frameIndex], static_cast<uint32_t>(drawData.size())); cmdBuffer->DrawMultiIndirect(drawDataBuffer[frameIndex], static_cast<uint32_t>(drawData.size()));
cmdBuffer->EndLabeledSegment(); cmdBuffer->EndLabeledSegment();
} }

View File

@ -27,9 +27,9 @@ of DigiPen Institute of Technology is prohibited.
namespace SHADE namespace SHADE
{ {
/*---------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
/* Forward Declarations */ /* Forward Declarations */
/*---------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
class SHVkBuffer; class SHVkBuffer;
class SHVkCommandBuffer; class SHVkCommandBuffer;
class SHVkPipeline; class SHVkPipeline;
@ -40,36 +40,36 @@ namespace SHADE
class SHVkDescriptorSetGroup; class SHVkDescriptorSetGroup;
class SHVkDescriptorPool; class SHVkDescriptorPool;
/*---------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
/* Type Definitions */ /* Type Definitions */
/*---------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
/***********************************************************************************/ /*************************************************************************************/
/*! /*!
\brief \brief
Describes a segment of the sub batch operation. Describes a segment of the sub batch operation.
*/ */
/***********************************************************************************/ /*************************************************************************************/
struct SHSubBatch struct SHSubBatch
{ {
public: public:
/*-----------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Data Members */ /* Data Members */
/*-----------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
Handle<SHMesh> Mesh; Handle<SHMesh> Mesh;
std::unordered_set<EntityID> Renderables; std::unordered_set<EntityID> Renderables;
}; };
/***********************************************************************************/ /*************************************************************************************/
/*! /*!
\brief \brief
Describes a segment of the sub batch operation. Describes a segment of the sub batch operation.
*/ */
/***********************************************************************************/ /*************************************************************************************/
class SHBatch class SHBatch
{ {
public: public:
/*-----------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Constructor/Destructors */ /* Constructor/Destructors */
/*-----------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
SHBatch(Handle<SHVkPipeline> pipeline); SHBatch(Handle<SHVkPipeline> pipeline);
SHBatch(const SHBatch&) = delete; SHBatch(const SHBatch&) = delete;
SHBatch(SHBatch&& rhs); SHBatch(SHBatch&& rhs);
@ -77,9 +77,9 @@ namespace SHADE
SHBatch& operator=(SHBatch&& rhs); SHBatch& operator=(SHBatch&& rhs);
~SHBatch(); ~SHBatch();
/*-----------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Usage Functions */ /* Usage Functions */
/*-----------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
void Add(const SHRenderable* renderable); void Add(const SHRenderable* renderable);
void Remove(const SHRenderable* renderable); void Remove(const SHRenderable* renderable);
void Clear(); void Clear();
@ -89,23 +89,23 @@ namespace SHADE
void Build(Handle<SHVkLogicalDevice> device, Handle<SHVkDescriptorPool> descPool, uint32_t frameIndex); void Build(Handle<SHVkLogicalDevice> device, Handle<SHVkDescriptorPool> descPool, uint32_t frameIndex);
void Draw(Handle<SHVkCommandBuffer> cmdBuffer, uint32_t frameIndex); void Draw(Handle<SHVkCommandBuffer> cmdBuffer, uint32_t frameIndex);
/*-----------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Getter Functions */ /* Getter Functions */
/*-----------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
Handle<SHVkPipeline> GetPipeline() const noexcept { return pipeline; }; Handle<SHVkPipeline> GetPipeline() const noexcept { return pipeline; };
bool IsEmpty() const noexcept { return subBatches.empty(); } bool IsEmpty() const noexcept { return subBatches.empty(); }
private: private:
/*-----------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Type Definition */ /* Type Definition */
/*-----------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
using TripleBool = std::array<bool, SHGraphicsConstants::NUM_FRAME_BUFFERS>; using TripleBool = std::array<bool, SHGraphicsConstants::NUM_FRAME_BUFFERS>;
using TripleBuffer = std::array<Handle<SHVkBuffer>, SHGraphicsConstants::NUM_FRAME_BUFFERS>; using TripleBuffer = std::array<Handle<SHVkBuffer>, SHGraphicsConstants::NUM_FRAME_BUFFERS>;
using TripleDescSet = std::array<Handle<SHVkDescriptorSetGroup>, SHGraphicsConstants::NUM_FRAME_BUFFERS>; using TripleDescSet = std::array<Handle<SHVkDescriptorSetGroup>, SHGraphicsConstants::NUM_FRAME_BUFFERS>;
/*-----------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Data Members */ /* Data Members */
/*-----------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
// Resources // Resources
Handle<SHVkLogicalDevice> device; Handle<SHVkLogicalDevice> device;
// Batch Properties // Batch Properties
@ -123,6 +123,8 @@ 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<uint32_t> boneMatrixIndices;
bool isCPUBuffersDirty = true; bool isCPUBuffersDirty = true;
// GPU Buffers // GPU Buffers
TripleBuffer drawDataBuffer; TripleBuffer drawDataBuffer;
@ -130,6 +132,8 @@ namespace SHADE
TripleBuffer instancedIntegerBuffer; TripleBuffer instancedIntegerBuffer;
TripleBuffer matPropsBuffer; TripleBuffer matPropsBuffer;
TripleDescSet matPropsDescSet; TripleDescSet matPropsDescSet;
TripleBuffer boneMatrixBuffer;
TripleBuffer boneFirstIndexBuffer;
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
/* Helper Functions */ /* Helper Functions */