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"
// Primary Include
#include "SHAnimatorComponent.h"
// Project Includes
#include "SHRig.h"
#include "Math/SHMatrix.h"
namespace SHADE
{

View File

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

View File

@ -29,12 +29,13 @@ of DigiPen Institute of Technology is prohibited.
#include "Graphics/Descriptors/SHVkDescriptorPool.h"
#include "Scene/SHSceneManager.h"
#include "UI/SHUIComponent.h"
#include "Animation/SHAnimatorComponent.h"
namespace SHADE
{
/*---------------------------------------------------------------------------------*/
/* SHBatch - Usage Functions */
/*---------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------*/
/* SHBatch - Constructors/Destructors */
/*-----------------------------------------------------------------------------------*/
SHBatch::SHBatch(Handle<SHVkPipeline> pipeline)
: pipeline{ pipeline }
{
@ -124,6 +125,9 @@ namespace SHADE
}
}
/*-----------------------------------------------------------------------------------*/
/* SHBatch - Usage Functions */
/*-----------------------------------------------------------------------------------*/
void SHBatch::Add(const SHRenderable* renderable)
{
// Ignore if null
@ -407,8 +411,6 @@ namespace SHADE
// - EID data
instancedIntegerData.reserve(numTotalElements);
instancedIntegerData.clear();
// - Material Properties Data
const Handle<SHShaderBlockInterface> SHADER_INFO = pipeline->GetPipelineLayout()->GetShaderBlockInterface
(
@ -429,6 +431,10 @@ namespace SHADE
matPropsDataSize = matPropTotalBytes;
}
}
// - Bone Data
boneMatrixData.clear();
boneMatrixIndices.clear();
boneMatrixIndices.reserve(numTotalElements);
// Build Sub Batches
uint32_t nextInstanceIndex = 0;
@ -499,6 +505,16 @@ namespace SHADE
//propsCurrPtr += singleMatPropAlignedSize;
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
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
isDirty[frameIndex] = false;
@ -569,6 +603,11 @@ namespace SHADE
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->EndLabeledSegment();
}

View File

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