Added potential fix for Vulkan buffer creation crashes from SHBatch

This commit is contained in:
Kah Wei 2022-11-23 19:30:51 +08:00
parent 29b50bd255
commit 6261661ef6
1 changed files with 34 additions and 31 deletions

View File

@ -340,7 +340,7 @@ namespace SHADE
} }
// Transfer to GPU // Transfer to GPU
if (transformDataBuffer[frameIndex]) if (transformDataBuffer[frameIndex] && !drawData.empty())
transformDataBuffer[frameIndex]->WriteToMemory(transformData.data(), static_cast<uint32_t>(transformData.size() * sizeof(SHMatrix)), 0, 0); transformDataBuffer[frameIndex]->WriteToMemory(transformData.data(), static_cast<uint32_t>(transformData.size() * sizeof(SHMatrix)), 0, 0);
} }
@ -369,7 +369,7 @@ namespace SHADE
} }
// Transfer to GPU // Transfer to GPU
if (instancedIntegerBuffer[frameIndex]) if (instancedIntegerBuffer[frameIndex] && !drawData.empty())
instancedIntegerBuffer[frameIndex]->WriteToMemory(instancedIntegerData.data(), static_cast<uint32_t>(instancedIntegerData.size() * sizeof(SHInstancedIntegerData)), 0, 0); instancedIntegerBuffer[frameIndex]->WriteToMemory(instancedIntegerData.data(), static_cast<uint32_t>(instancedIntegerData.size() * sizeof(SHInstancedIntegerData)), 0, 0);
} }
@ -507,33 +507,36 @@ namespace SHADE
isCPUBuffersDirty = false; isCPUBuffersDirty = false;
} }
// Send all buffered data to the GPU buffers // Send all buffered data to the GPU buffers if there is anything to render
using BuffUsage = vk::BufferUsageFlagBits; if (!drawData.empty())
// - Draw Data {
const uint32_t DRAW_DATA_BYTES = static_cast<uint32_t>(drawData.size() * sizeof(vk::DrawIndexedIndirectCommand)); using BuffUsage = vk::BufferUsageFlagBits;
SHVkUtil::EnsureBufferAndCopyHostVisibleData // - Draw Data
( const uint32_t DRAW_DATA_BYTES = static_cast<uint32_t>(drawData.size() * sizeof(vk::DrawIndexedIndirectCommand));
device, drawDataBuffer[frameIndex], drawData.data(), DRAW_DATA_BYTES, SHVkUtil::EnsureBufferAndCopyHostVisibleData
BuffUsage::eIndirectBuffer, (
"Batch Draw Data Buffer" device, drawDataBuffer[frameIndex], drawData.data(), DRAW_DATA_BYTES,
); BuffUsage::eIndirectBuffer,
// - Transform Buffer "Batch Draw Data Buffer"
const uint32_t TF_DATA_BYTES = static_cast<uint32_t>(transformData.size() * sizeof(SHMatrix)); );
SHVkUtil::EnsureBufferAndCopyHostVisibleData // - Transform Buffer
( const uint32_t TF_DATA_BYTES = static_cast<uint32_t>(transformData.size() * sizeof(SHMatrix));
device, transformDataBuffer[frameIndex], transformData.data(), TF_DATA_BYTES, SHVkUtil::EnsureBufferAndCopyHostVisibleData
BuffUsage::eVertexBuffer, (
"Batch Transform Buffer" device, transformDataBuffer[frameIndex], transformData.data(), TF_DATA_BYTES,
); BuffUsage::eVertexBuffer,
const uint32_t EID_DATA_BYTES = static_cast<uint32_t>(instancedIntegerData.size() * sizeof(SHInstancedIntegerData)); "Batch Transform Buffer"
SHVkUtil::EnsureBufferAndCopyHostVisibleData );
( const uint32_t EID_DATA_BYTES = static_cast<uint32_t>(instancedIntegerData.size() * sizeof(SHInstancedIntegerData));
device, instancedIntegerBuffer[frameIndex], instancedIntegerData.data(), EID_DATA_BYTES, SHVkUtil::EnsureBufferAndCopyHostVisibleData
BuffUsage::eVertexBuffer, (
"Batch Instance Data Buffer" device, instancedIntegerBuffer[frameIndex], instancedIntegerData.data(), EID_DATA_BYTES,
); BuffUsage::eVertexBuffer,
// - Material Properties Buffer "Batch Instance Data Buffer"
rebuildMaterialBuffers(frameIndex, descPool); );
// - Material Properties Buffer
rebuildMaterialBuffers(frameIndex, descPool);
}
// Mark this frame as no longer dirty // Mark this frame as no longer dirty
isDirty[frameIndex] = false; isDirty[frameIndex] = false;
@ -551,7 +554,7 @@ namespace SHADE
} }
// Nothing to draw // Nothing to draw
if (subBatches.empty()) if (drawData.empty())
return; return;
// Bind all required objects before drawing // Bind all required objects before drawing
@ -586,7 +589,7 @@ namespace SHADE
void SHBatch::rebuildMaterialBuffers(uint32_t frameIndex, Handle<SHVkDescriptorPool> descPool) void SHBatch::rebuildMaterialBuffers(uint32_t frameIndex, Handle<SHVkDescriptorPool> descPool)
{ {
if (matPropsData) if (matPropsData && !drawData.empty())
{ {
SHVkUtil::EnsureBufferAndCopyHostVisibleData SHVkUtil::EnsureBufferAndCopyHostVisibleData
( (