diff --git a/SHADE_Engine/src/Animation/SHAnimatorComponent.cpp b/SHADE_Engine/src/Animation/SHAnimatorComponent.cpp index 271129f1..3b4524e1 100644 --- a/SHADE_Engine/src/Animation/SHAnimatorComponent.cpp +++ b/SHADE_Engine/src/Animation/SHAnimatorComponent.cpp @@ -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 { diff --git a/SHADE_Engine/src/Animation/SHAnimatorComponent.h b/SHADE_Engine/src/Animation/SHAnimatorComponent.h index 86744aec..be1bc742 100644 --- a/SHADE_Engine/src/Animation/SHAnimatorComponent.h +++ b/SHADE_Engine/src/Animation/SHAnimatorComponent.h @@ -11,26 +11,48 @@ of DigiPen Institute of Technology is prohibited. *//*************************************************************************************/ #pragma once +// STL Includes +#include // External Dependencies #include // Project Includes #include "ECS_Base/Components/SHComponent.h" +#include "Resource/SHHandle.h" namespace SHADE { /*-----------------------------------------------------------------------------------*/ /* Forward Declarations */ /*-----------------------------------------------------------------------------------*/ + class SHRig; + struct SHMatrix; /*-----------------------------------------------------------------------------------*/ /* Type Definitions */ /*-----------------------------------------------------------------------------------*/ + /// + /// Component that holds and controls the animation related properties of a skinned + /// mesh. + /// class SH_API SHAnimatorComponent final : public SHComponent { + public: + /*---------------------------------------------------------------------------------*/ + /* Getter Functions */ + /*---------------------------------------------------------------------------------*/ + const std::vector& GetBoneMatrices() const noexcept { return boneMatrices; } + private: - /*-------------------------------------------------------------------------------*/ - /* Data Members */ - /*-------------------------------------------------------------------------------*/ + /*---------------------------------------------------------------------------------*/ + /* Data Members */ + /*---------------------------------------------------------------------------------*/ + Handle rig; + std::vector boneMatrices; + float currPlaybackTime = 0.0f; + + /*---------------------------------------------------------------------------------*/ + /* RTTR */ + /*---------------------------------------------------------------------------------*/ RTTR_ENABLE() }; } \ No newline at end of file diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.cpp index 9b4b02b0..6b7b9111 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.cpp @@ -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 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 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(rendId); + if (animator) + { + boneMatrixIndices.emplace_back(static_cast(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(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(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(drawData.size())); cmdBuffer->EndLabeledSegment(); } diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.h b/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.h index dd4d33fd..08f07ea6 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.h @@ -27,114 +27,118 @@ of DigiPen Institute of Technology is prohibited. namespace SHADE { + /*-----------------------------------------------------------------------------------*/ + /* Forward Declarations */ + /*-----------------------------------------------------------------------------------*/ + class SHVkBuffer; + class SHVkCommandBuffer; + class SHVkPipeline; + class SHMesh; + class SHRenderable; + class SHVkLogicalDevice; + class SHMaterialInstance; + class SHVkDescriptorSetGroup; + class SHVkDescriptorPool; + + /*-----------------------------------------------------------------------------------*/ + /* Type Definitions */ + /*-----------------------------------------------------------------------------------*/ + /*************************************************************************************/ + /*! + \brief + Describes a segment of the sub batch operation. + */ + /*************************************************************************************/ + struct SHSubBatch + { + public: /*---------------------------------------------------------------------------------*/ - /* Forward Declarations */ + /* Data Members */ /*---------------------------------------------------------------------------------*/ - class SHVkBuffer; - class SHVkCommandBuffer; - class SHVkPipeline; - class SHMesh; - class SHRenderable; - class SHVkLogicalDevice; - class SHMaterialInstance; - class SHVkDescriptorSetGroup; - class SHVkDescriptorPool; + Handle Mesh; + std::unordered_set Renderables; + }; + /*************************************************************************************/ + /*! + \brief + Describes a segment of the sub batch operation. + */ + /*************************************************************************************/ + class SHBatch + { + public: + /*---------------------------------------------------------------------------------*/ + /* Constructor/Destructors */ + /*---------------------------------------------------------------------------------*/ + SHBatch(Handle pipeline); + SHBatch(const SHBatch&) = delete; + SHBatch(SHBatch&& rhs); + SHBatch& operator=(const SHBatch&) = delete; + SHBatch& operator=(SHBatch&& rhs); + ~SHBatch(); /*---------------------------------------------------------------------------------*/ - /* Type Definitions */ + /* Usage Functions */ /*---------------------------------------------------------------------------------*/ - /***********************************************************************************/ - /*! - \brief - Describes a segment of the sub batch operation. - */ - /***********************************************************************************/ - struct SHSubBatch - { - public: - /*-----------------------------------------------------------------------------*/ - /* Data Members */ - /*-----------------------------------------------------------------------------*/ - Handle Mesh; - std::unordered_set Renderables; - }; - /***********************************************************************************/ - /*! - \brief - Describes a segment of the sub batch operation. - */ - /***********************************************************************************/ - class SHBatch - { - public: - /*-----------------------------------------------------------------------------*/ - /* Constructor/Destructors */ - /*-----------------------------------------------------------------------------*/ - SHBatch(Handle pipeline); - SHBatch(const SHBatch&) = delete; - SHBatch(SHBatch&& rhs); - SHBatch& operator=(const SHBatch&) = delete; - SHBatch& operator=(SHBatch&& rhs); - ~SHBatch(); + void Add(const SHRenderable* renderable); + void Remove(const SHRenderable* renderable); + void Clear(); + void UpdateMaterialBuffer(uint32_t frameIndex, Handle descPool); + void UpdateTransformBuffer(uint32_t frameIndex); + void UpdateInstancedIntegerBuffer(uint32_t frameIndex); + void Build(Handle device, Handle descPool, uint32_t frameIndex); + void Draw(Handle cmdBuffer, uint32_t frameIndex); - /*-----------------------------------------------------------------------------*/ - /* Usage Functions */ - /*-----------------------------------------------------------------------------*/ - void Add(const SHRenderable* renderable); - void Remove(const SHRenderable* renderable); - void Clear(); - void UpdateMaterialBuffer(uint32_t frameIndex, Handle descPool); - void UpdateTransformBuffer(uint32_t frameIndex); - void UpdateInstancedIntegerBuffer(uint32_t frameIndex); - void Build(Handle device, Handle descPool, uint32_t frameIndex) ; - void Draw(Handle cmdBuffer, uint32_t frameIndex); + /*---------------------------------------------------------------------------------*/ + /* Getter Functions */ + /*---------------------------------------------------------------------------------*/ + Handle GetPipeline() const noexcept { return pipeline; }; + bool IsEmpty() const noexcept { return subBatches.empty(); } - /*-----------------------------------------------------------------------------*/ - /* Getter Functions */ - /*-----------------------------------------------------------------------------*/ - Handle GetPipeline() const noexcept { return pipeline; }; - bool IsEmpty() const noexcept { return subBatches.empty(); } + private: + /*---------------------------------------------------------------------------------*/ + /* Type Definition */ + /*---------------------------------------------------------------------------------*/ + using TripleBool = std::array; + using TripleBuffer = std::array, SHGraphicsConstants::NUM_FRAME_BUFFERS>; + using TripleDescSet = std::array, SHGraphicsConstants::NUM_FRAME_BUFFERS>; - private: - /*-----------------------------------------------------------------------------*/ - /* Type Definition */ - /*-----------------------------------------------------------------------------*/ - using TripleBool = std::array; - using TripleBuffer = std::array, SHGraphicsConstants::NUM_FRAME_BUFFERS>; - using TripleDescSet = std::array, SHGraphicsConstants::NUM_FRAME_BUFFERS>; + /*---------------------------------------------------------------------------------*/ + /* Data Members */ + /*---------------------------------------------------------------------------------*/ + // Resources + Handle device; + // Batch Properties + Handle pipeline; + std::unordered_set> referencedMatInstances; + TripleBool matBufferDirty; + // Batch Tree + std::vector subBatches; + TripleBool isDirty; + // CPU Buffers + std::vector drawData; + std::vector transformData; + std::vector instancedIntegerData; + std::unique_ptr matPropsData; + Byte matPropsDataSize = 0; + Byte singleMatPropAlignedSize = 0; + Byte singleMatPropSize = 0; + std::vector boneMatrixData; + std::vector boneMatrixIndices; + bool isCPUBuffersDirty = true; + // GPU Buffers + TripleBuffer drawDataBuffer; + TripleBuffer transformDataBuffer; + TripleBuffer instancedIntegerBuffer; + TripleBuffer matPropsBuffer; + TripleDescSet matPropsDescSet; + TripleBuffer boneMatrixBuffer; + TripleBuffer boneFirstIndexBuffer; - /*-----------------------------------------------------------------------------*/ - /* Data Members */ - /*-----------------------------------------------------------------------------*/ - // Resources - Handle device; - // Batch Properties - Handle pipeline; - std::unordered_set> referencedMatInstances; - TripleBool matBufferDirty; - // Batch Tree - std::vector subBatches; - TripleBool isDirty; - // CPU Buffers - std::vector drawData; - std::vector transformData; - std::vector instancedIntegerData; - std::unique_ptr matPropsData; - Byte matPropsDataSize = 0; - Byte singleMatPropAlignedSize = 0; - Byte singleMatPropSize = 0; - bool isCPUBuffersDirty = true; - // GPU Buffers - TripleBuffer drawDataBuffer; - TripleBuffer transformDataBuffer; - TripleBuffer instancedIntegerBuffer; - TripleBuffer matPropsBuffer; - TripleDescSet matPropsDescSet; - - /*-----------------------------------------------------------------------------*/ - /* Helper Functions */ - /*-----------------------------------------------------------------------------*/ - void setAllDirtyFlags(); - void rebuildMaterialBuffers(uint32_t frameIndex, Handle descPool); - }; + /*-----------------------------------------------------------------------------*/ + /* Helper Functions */ + /*-----------------------------------------------------------------------------*/ + void setAllDirtyFlags(); + void rebuildMaterialBuffers(uint32_t frameIndex, Handle descPool); + }; }