Fixed SHVkBuffer move and reworked batching buffers to be host visible

This commit is contained in:
Kah Wei 2022-09-19 20:15:39 +08:00
parent 4023b9d136
commit 9e09896984
12 changed files with 95 additions and 33 deletions

View File

@ -301,7 +301,7 @@ namespace SHADE
: vkBuffer{std::move (rhs.vkBuffer)}
, stagingBuffer{ std::move (rhs.stagingBuffer)}
, sizeStored{ std::move (rhs.sizeStored) }
, mappedPtr{ nullptr }
, mappedPtr{ std::move(rhs.mappedPtr) }
, alloc{ std::move (rhs.alloc) }
, randomAccessOptimized{ rhs.randomAccessOptimized }
, boundToCoherent{ rhs.boundToCoherent}
@ -322,7 +322,7 @@ namespace SHADE
vkBuffer = std::move(rhs.vkBuffer);
stagingBuffer = std::move(rhs.stagingBuffer);
sizeStored = std::move(rhs.sizeStored);
mappedPtr = nullptr;
mappedPtr = std::move(rhs.mappedPtr);
alloc = std::move(rhs.alloc);
randomAccessOptimized = rhs.randomAccessOptimized;
boundToCoherent = rhs.boundToCoherent;

View File

@ -86,7 +86,7 @@ namespace SHADE
matPropsBuffer.Free();
}
void SHBatch::Build(Handle<SHVkLogicalDevice> device, Handle<SHVkCommandBuffer> cmdBuffer)
void SHBatch::Build(Handle<SHVkLogicalDevice> device)
{
// No need to build as there are no changes
if (!isDirty)
@ -159,25 +159,37 @@ 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::EnsureBufferAndCopyData
SHVkUtil::EnsureBufferAndCopyHostVisibleData
(
device, cmdBuffer, drawDataBuffer, drawData.data(), DRAW_DATA_BYTES,
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::EnsureBufferAndCopyData
SHVkUtil::EnsureBufferAndCopyHostVisibleData
(
device, cmdBuffer, transformDataBuffer, transformData.data(), TF_DATA_BYTES,
device, transformDataBuffer, transformData.data(), TF_DATA_BYTES,
BuffUsage::eVertexBuffer
);
transformDataBuffer->Map();
// - Material Properties Buffer
SHVkUtil::EnsureBufferAndCopyData
(
device, cmdBuffer, matPropsBuffer, matPropsData.get(), static_cast<uint32_t>(matPropTotalBytes),
BuffUsage::eStorageBuffer
);
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;
}
@ -187,6 +199,7 @@ namespace SHADE
/*---------------------------------------------------------------------------------*/
void SHBatch::Draw(Handle<SHVkCommandBuffer> cmdBuffer)
{
cmdBuffer->BindPipeline(pipeline);
cmdBuffer->BindVertexBuffer(SHGraphicsConstants::VertexBufferBindings::TRANSFORM, transformDataBuffer, 0);
cmdBuffer->DrawMultiIndirect(drawDataBuffer, static_cast<uint32_t>(drawData.size()));
}

View File

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

View File

@ -91,12 +91,12 @@ namespace SHADE
(*superBatch)->Remove(renderable);
}
void SHBatcher::FinaliseBatches(Handle<SHVkLogicalDevice> device, Handle<SHVkCommandBuffer> cmdBuffer)
void SHBatcher::FinaliseBatches(Handle<SHVkLogicalDevice> device)
{
// Build SuperBatches
for (auto& batch : superBatches)
{
batch->Build(device, cmdBuffer);
batch->Build(device);
}
}

View File

@ -51,7 +51,7 @@ namespace SHADE
void PrepareBatches();
void AddToBatch(SHRenderable const* renderable);
void RemoveFromBatch(SHRenderable const* renderable);
void FinaliseBatches(Handle<SHVkLogicalDevice> device, Handle<SHVkCommandBuffer> cmdBuffer);
void FinaliseBatches(Handle<SHVkLogicalDevice> device);
void ClearBatches();
void RegisterSuperBatch(Handle<SHSuperBatch> superBatch);
void DeregisterSuperBatch(Handle<SHSuperBatch> superBatch);

View File

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

View File

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

View File

@ -152,10 +152,10 @@ namespace SHADE
}
// Create Debug Renderers
debugScreenRenderer = defaultViewport->AddRenderer(resourceManager, worldRenderGraph);
/*debugScreenRenderer = defaultViewport->AddRenderer(resourceManager, worldRenderGraph);
debugScreenRenderer->SetCamera(screenCamera);
debugWorldRenderer = defaultViewport->AddRenderer(resourceManager, worldRenderGraph);
debugWorldRenderer->SetCamera(worldCamera);
debugWorldRenderer->SetCamera(worldCamera);*/
// Add world renderer to default viewport
worldRenderer = defaultViewport->AddRenderer(resourceManager, worldRenderGraph);
@ -227,7 +227,7 @@ namespace SHADE
(
{ rg->GetCommandBuffer(renderContext.GetCurrentFrame()) },
{ (vpIndex == viewports.size() - 1 && renIndex == renderers.size() - 1) ? frameData.semRenderFinishHdl : graphSemaphores[!semIndex]},
{ (vpIndex == 0 && renIndex == 0) ? frameData.semImgAvailableHdl : graphSemaphores[semIndex]},
{ (vpIndex == 0 && renIndex == 0) ? frameData.semImgAvailableHdl : graphSemaphores[semIndex ]},
{}
);
@ -266,7 +266,7 @@ namespace SHADE
for (auto vp : viewports)
for (auto renderer : vp->GetRenderers())
{
renderer->GetRenderGraph()->FinaliseBatch(renderContext.GetCurrentFrame());
renderer->GetRenderGraph()->FinaliseBatch();
}
// Resize

View File

@ -611,9 +611,9 @@ namespace SHADE
return pipeline;
}
void SHRenderGraphNode::FinaliseBatch(Handle<SHVkCommandBuffer> commandBuffer)
void SHRenderGraphNode::FinaliseBatch()
{
batcher.FinaliseBatches(logicalDeviceHdl, commandBuffer);
batcher.FinaliseBatches(logicalDeviceHdl);
}
/***************************************************************************/
@ -1100,16 +1100,16 @@ namespace SHADE
}
for (auto& node : nodes)
node->Execute(commandBuffers[frameIndex], frameIndex);
node->Execute(cmdBuffer, frameIndex);
cmdBuffer->EndRecording();
}
void SHRenderGraph::FinaliseBatch(uint32_t frameIndex)
void SHRenderGraph::FinaliseBatch()
{
for (auto& node : nodes)
{
node->FinaliseBatch(commandBuffers[frameIndex]);
node->FinaliseBatch();
}
}

View File

@ -229,7 +229,7 @@ namespace SHADE
// TODO: RemoveSubpass()
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(Handle<SHVkCommandBuffer> commandBuffer);
void FinaliseBatch();
/*-----------------------------------------------------------------------*/
/* SETTERS AND GETTERS */
@ -297,7 +297,7 @@ namespace SHADE
Handle<SHRenderGraphNode> AddNode (std::string nodeName, std::initializer_list<std::string> resourceNames, std::initializer_list<std::string> predecessorNodes) noexcept;
void Generate (void) noexcept;
void Execute (uint32_t frameIndex, std::initializer_list<std::pair<Handle<SHVkBuffer>, uint32_t>> graphScopeBuffers) noexcept;
void FinaliseBatch(uint32_t frameIndex);
void FinaliseBatch();
/*-----------------------------------------------------------------------*/
/* SETTERS AND GETTERS */

View File

@ -48,4 +48,27 @@ namespace SHADE
// Order transfers
bufferHandle->TransferToDeviceResource(cmdBuffer);
}
void SHVkUtil::EnsureBufferAndCopyHostVisibleData(Handle<SHVkLogicalDevice> device, Handle<SHVkBuffer>& bufferHandle, void* src, uint32_t size, vk::BufferUsageFlagBits usage)
{
if (bufferHandle)
{
// Resize
bufferHandle->ResizeReplace(size, src, size); // TODO: Set to host visible method?
}
else
{
// Create new
using BuffUsage = vk::BufferUsageFlagBits;
bufferHandle = device->CreateBuffer
(
size,
src,
size,
usage,
VmaMemoryUsage::VMA_MEMORY_USAGE_AUTO,
VmaAllocationCreateFlagBits::VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT | VmaAllocationCreateFlagBits::VMA_ALLOCATION_CREATE_MAPPED_BIT
);
}
}
}

View File

@ -26,9 +26,9 @@ namespace SHADE
/*!
\brief
Ensures that the specified bufferHandle contains a buffer that fits the specified
size and creates it if it does not exist. This follows by issuing a transfer
command into the created buffer.
Ensures that the specified bufferHandle contains a non-host visible buffer that
fits the specified size and creates it if it does not exist. This follows by
issuing a transfer command into the created buffer.
All created buffers will use VMA_MEMORY_USAGE_AUTO and
VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT.
@ -50,6 +50,32 @@ namespace SHADE
*/
/***********************************************************************************/
static void EnsureBufferAndCopyData(Handle<SHVkLogicalDevice> device, Handle<SHVkCommandBuffer> cmdBuffer, Handle<SHVkBuffer>& bufferHandle, void* src, uint32_t size, vk::BufferUsageFlagBits usage);
/***********************************************************************************/
/*!
\brief
Ensures that the specified bufferHandle contains a host visible buffer that fits
the specified size and creates it if it does not exist. This follows by copying
the data from the specified source to the buffer.
All created buffers will use VMA_MEMORY_USAGE_AUTO and
VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT.
\param device
Device used to create the SHVkBuffer.
\param bufferHandle
Reference to the handle that holds any existing buffer or will be used to hold the
created buffer.
\param src
Data to copy from.
\param size
Size of data to copy.
\param usage
Usage flags for the buffer.
*/
/***********************************************************************************/
static void EnsureBufferAndCopyHostVisibleData(Handle<SHVkLogicalDevice> device, Handle<SHVkBuffer>& bufferHandle, void* src, uint32_t size, vk::BufferUsageFlagBits usage);
};
}