Added skeletal animations and other fixes
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
#include <destrum/Graphics/Pipelines/SkinningPipeline.h>
|
||||
|
||||
#include "destrum/FS/AssetFS.h"
|
||||
#include "destrum/Graphics/MeshCache.h"
|
||||
#include "destrum/Graphics/MeshDrawCommand.h"
|
||||
|
||||
void SkinningPipeline::init(GfxDevice& gfxDevice) {
|
||||
const auto& device = gfxDevice.getDevice();
|
||||
|
||||
const auto pushConstant = VkPushConstantRange{
|
||||
.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT,
|
||||
.offset = 0,
|
||||
.size = sizeof(PushConstants),
|
||||
};
|
||||
|
||||
const auto pushConstants = std::array{pushConstant};
|
||||
|
||||
VkPipelineLayoutCreateInfo pipelineLayoutInfo{};
|
||||
pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
|
||||
|
||||
pipelineLayoutInfo.pushConstantRangeCount = 1;
|
||||
pipelineLayoutInfo.pPushConstantRanges = pushConstants.data();
|
||||
|
||||
if (vkCreatePipelineLayout(gfxDevice.getDevice().device, &pipelineLayoutInfo, nullptr, &m_pipelineLayout) != VK_SUCCESS) {
|
||||
throw std::runtime_error("Could not make pipleine layout");
|
||||
}
|
||||
|
||||
|
||||
ComputePipelineConfigInfo pipelineConfig{};
|
||||
pipelineConfig.name = "skinning compute pipeline";
|
||||
pipelineConfig.pipelineLayout = m_pipelineLayout;
|
||||
|
||||
const auto SkinningShaderPath = AssetFS::GetInstance().GetCookedPathForFile("engine://shaders/skinning.comp");
|
||||
|
||||
skinningPipeline = std::make_unique<ComputePipeline>(
|
||||
gfxDevice,
|
||||
SkinningShaderPath.generic_string(),
|
||||
pipelineConfig
|
||||
);
|
||||
|
||||
|
||||
for (std::size_t i = 0; i < FRAMES_IN_FLIGHT; ++i) {
|
||||
auto& jointMatricesBuffer = framesData[i].jointMatricesBuffer;
|
||||
jointMatricesBuffer.capacity = MAX_JOINT_MATRICES;
|
||||
jointMatricesBuffer.buffer = gfxDevice.createBuffer(
|
||||
MAX_JOINT_MATRICES * sizeof(glm::mat4),
|
||||
VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT);
|
||||
}
|
||||
}
|
||||
|
||||
void SkinningPipeline::cleanup(GfxDevice& gfxDevice) {
|
||||
for (auto& frame : framesData) {
|
||||
gfxDevice.destroyBuffer(frame.jointMatricesBuffer.buffer);
|
||||
}
|
||||
vkDestroyPipelineLayout(gfxDevice.getDevice().device, m_pipelineLayout, nullptr);
|
||||
skinningPipeline.reset();
|
||||
}
|
||||
|
||||
void SkinningPipeline::doSkinning(VkCommandBuffer cmd, std::size_t frameIndex, const MeshCache& meshCache, const MeshDrawCommand& dc) {
|
||||
|
||||
skinningPipeline->bind(cmd);
|
||||
|
||||
const auto& mesh = meshCache.getMesh(dc.meshId);
|
||||
assert(mesh.hasSkeleton);
|
||||
assert(dc.skinnedMesh);
|
||||
|
||||
const auto cs = PushConstants{
|
||||
.jointMatricesBuffer = getCurrentFrameData(frameIndex).jointMatricesBuffer.buffer.address,
|
||||
.jointMatricesStartIndex = static_cast<std::uint32_t>(dc.jointMatricesStartIndex), // explicit cast
|
||||
.numVertices = mesh.numVertices,
|
||||
.inputBuffer = mesh.vertexBuffer.address,
|
||||
.skinningData = mesh.skinningDataBuffer.address,
|
||||
.outputBuffer = dc.skinnedMesh->skinnedVertexBuffer.address,
|
||||
};
|
||||
vkCmdPushConstants(cmd, m_pipelineLayout, VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(PushConstants), &cs);
|
||||
|
||||
static const auto workgroupSize = 256;
|
||||
// const auto groupSizeX = (std::uint32_t)std::ceil(mesh.numVertices / (float)workgroupSize);
|
||||
const auto groupSizeX = static_cast<std::uint32_t>(
|
||||
std::ceil(mesh.numVertices / (float)workgroupSize));
|
||||
vkCmdDispatch(cmd, groupSizeX, 1, 1);
|
||||
}
|
||||
|
||||
void SkinningPipeline::beginDrawing(std::size_t frameIndex) {
|
||||
getCurrentFrameData(frameIndex).jointMatricesBuffer.clear();
|
||||
}
|
||||
|
||||
std::size_t SkinningPipeline::appendJointMatrices(std::span<const glm::mat4> jointMatrices, std::size_t frameIndex) {
|
||||
|
||||
auto& jointMatricesBuffer = getCurrentFrameData(frameIndex).jointMatricesBuffer;
|
||||
const auto startIndex = jointMatricesBuffer.size;
|
||||
jointMatricesBuffer.append(jointMatrices);
|
||||
return startIndex;
|
||||
}
|
||||
|
||||
SkinningPipeline::PerFrameData& SkinningPipeline::getCurrentFrameData(std::size_t frameIndex) {
|
||||
return framesData[frameIndex % FRAMES_IN_FLIGHT];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user