Added per-frame update of gpu transforms buffer

This commit is contained in:
Kah Wei 2022-09-22 20:04:53 +08:00
parent cb31628e66
commit 495d2b4b66
8 changed files with 45 additions and 9 deletions

View File

@ -86,6 +86,23 @@ namespace SHADE
matPropsBuffer.Free();
}
void SHBatch::UpdateTransformBuffer()
{
// Reset Transform Data
transformData.clear();
// Populate on the CPU
for (auto& subBatch : subBatches)
for (const SHRenderable* renderable : subBatch.Renderables)
{
// Transform
transformData.emplace_back(renderable->TransformMatrix);
}
// Transfer to GPU
transformDataBuffer->WriteToMemory(transformData.data(), transformData.size() * sizeof(SHMatrix), 0, 0);
}
void SHBatch::Build(Handle<SHVkLogicalDevice> device)
{
// No need to build as there are no changes
@ -159,36 +176,27 @@ namespace SHADE
// Send all buffered data to the GPU buffers
using BuffUsage = vk::BufferUsageFlagBits;
// - Draw Data
if (drawDataBuffer)
drawDataBuffer->Unmap();
const uint32_t DRAW_DATA_BYTES = static_cast<uint32_t>(drawData.size() * sizeof(vk::DrawIndexedIndirectCommand));
SHVkUtil::EnsureBufferAndCopyHostVisibleData
(
device, drawDataBuffer, drawData.data(), DRAW_DATA_BYTES,
BuffUsage::eIndirectBuffer
);
drawDataBuffer->Map();
// - Transform Buffer
if (transformDataBuffer)
transformDataBuffer->Unmap();
const uint32_t TF_DATA_BYTES = static_cast<uint32_t>(transformData.size() * sizeof(SHMatrix));
SHVkUtil::EnsureBufferAndCopyHostVisibleData
(
device, transformDataBuffer, transformData.data(), TF_DATA_BYTES,
BuffUsage::eVertexBuffer
);
transformDataBuffer->Map();
// - Material Properties Buffer
if (!EMPTY_MAT_PROPS)
{
if (matPropsBuffer)
matPropsBuffer->Unmap();
SHVkUtil::EnsureBufferAndCopyHostVisibleData
(
device, matPropsBuffer, matPropsData.get(), static_cast<uint32_t>(matPropTotalBytes),
BuffUsage::eStorageBuffer
);
matPropsBuffer->Map();
}
isDirty = false;

View File

@ -71,6 +71,7 @@ namespace SHADE
void Add(const SHRenderable* renderable);
void Remove(const SHRenderable* renderable);
void Clear();
void UpdateTransformBuffer();
void Build(Handle<SHVkLogicalDevice> device);
void Draw(Handle<SHVkCommandBuffer> cmdBuffer);

View File

@ -109,6 +109,14 @@ namespace SHADE
superBatches.clear();
}
void SHBatcher::UpdateTransformBuffer()
{
for (auto& batch : superBatches)
{
batch->UpdateTransformBuffer();
}
}
void SHBatcher::RegisterSuperBatch(Handle<SHSuperBatch> superBatch)
{
superBatches.emplace_back(superBatch);

View File

@ -53,6 +53,7 @@ namespace SHADE
void RemoveFromBatch(SHRenderable const* renderable);
void FinaliseBatches(Handle<SHVkLogicalDevice> device);
void ClearBatches();
void UpdateTransformBuffer();
void RegisterSuperBatch(Handle<SHSuperBatch> superBatch);
void DeregisterSuperBatch(Handle<SHSuperBatch> superBatch);

View File

@ -78,6 +78,14 @@ namespace SHADE
batches.clear();
}
void SHSuperBatch::UpdateTransformBuffer()
{
for (auto& batch : batches)
{
batch.UpdateTransformBuffer();
}
}
void SHSuperBatch::Build(Handle<SHVkLogicalDevice> device) noexcept
{
// Build all batches

View File

@ -55,6 +55,7 @@ namespace SHADE
void Add(const SHRenderable* renderable) noexcept;
void Remove(const SHRenderable* renderable) noexcept;
void Clear() noexcept;
void UpdateTransformBuffer();
void Build(Handle<SHVkLogicalDevice> device) noexcept;
void Draw(Handle<SHVkCommandBuffer> cmdBuffer) noexcept;

View File

@ -348,6 +348,9 @@ namespace SHADE
void SHSubpass::Execute(Handle<SHVkCommandBuffer>& commandBuffer) noexcept
{
// Ensure correct transforms are provided
superBatch->UpdateTransformBuffer();
// Draw all the batches
superBatch->Draw(commandBuffer);
@ -626,6 +629,11 @@ namespace SHADE
batcher.FinaliseBatches(logicalDeviceHdl);
}
void SHRenderGraphNode::UpdateBatchTransforms()
{
batcher.UpdateTransformBuffer();
}
/***************************************************************************/
/*!

View File

@ -231,6 +231,7 @@ namespace SHADE
void Execute (Handle<SHVkCommandBuffer>& commandBuffer, uint32_t frameIndex) noexcept;
Handle<SHVkPipeline> GetOrCreatePipeline (std::pair<Handle<SHVkShaderModule>, Handle<SHVkShaderModule>> const& vsFsPair, Handle<SHSubpass> subpass) noexcept;
void FinaliseBatch();
void UpdateBatchTransforms();
/*-----------------------------------------------------------------------*/
/* SETTERS AND GETTERS */