fix: replace runtime waitIdle calls with a deletion queue
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
#include <../../include/destrum/Graphics/Caches/MaterialCache.h>
|
||||
#include <destrum/Graphics/Caches/MaterialCache.h>
|
||||
|
||||
#include <destrum/Graphics/GfxDevice.h>
|
||||
#include <destrum/Graphics/Util.h>
|
||||
@@ -19,7 +19,8 @@ void MaterialCache::init(
|
||||
|
||||
materialDataBuffer = gfxDevice.createBuffer(
|
||||
MAX_MATERIALS * sizeof(MaterialData),
|
||||
VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT,
|
||||
VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT |
|
||||
VK_BUFFER_USAGE_TRANSFER_DST_BIT,
|
||||
VMA_MEMORY_USAGE_CPU_TO_GPU);
|
||||
|
||||
vkutil::addDebugLabel(
|
||||
@@ -51,12 +52,6 @@ void MaterialCache::cleanup(GfxDevice& gfxDevice)
|
||||
|
||||
MaterialID MaterialCache::addMaterial(Material material)
|
||||
{
|
||||
if (!materials.empty() && device != nullptr) {
|
||||
// The material buffer is shared by all in-flight frames. A newly
|
||||
// appended entry may be written while an older frame is reading it.
|
||||
device->waitIdle();
|
||||
}
|
||||
|
||||
MaterialData* data = static_cast<MaterialData*>(materialDataBuffer.info.pMappedData);
|
||||
|
||||
const auto id = getFreeMaterialId();
|
||||
@@ -67,17 +62,14 @@ MaterialID MaterialCache::addMaterial(Material material)
|
||||
throw std::runtime_error("MaterialCache material buffer is not mapped");
|
||||
}
|
||||
|
||||
// Safe without waiting: no frame in flight can reference an id that didn't exist yet.
|
||||
// Changing an existing material goes through GameRenderer::updateMaterialGPU.
|
||||
data[id] = toMaterialData(material);
|
||||
device->getMemoryManager().flushAllocation(
|
||||
materialDataBuffer,
|
||||
static_cast<VkDeviceSize>(id) * sizeof(MaterialData),
|
||||
sizeof(MaterialData));
|
||||
|
||||
// The caller may update multiple materials in one frame; the range flush is
|
||||
// cheap for coherent allocations and required for non-coherent memory.
|
||||
// The cache does not own a device reference, so the renderer flushes the
|
||||
// exact range after batching updates.
|
||||
|
||||
const auto materialId = static_cast<MaterialID>(materials.size());
|
||||
std::string key;
|
||||
if (!material.assetReference.empty()) {
|
||||
@@ -210,17 +202,8 @@ Material& MaterialCache::getMaterialMutable(MaterialID id)
|
||||
return materials.at(id);
|
||||
}
|
||||
|
||||
void MaterialCache::updateMaterialGPU(MaterialID id)
|
||||
MaterialData MaterialCache::getMaterialData(MaterialID id) const
|
||||
{
|
||||
assert(id < materials.size());
|
||||
assert(materialDataBuffer.info.pMappedData && "materialDataBuffer must be mapped");
|
||||
|
||||
MaterialData* data =
|
||||
reinterpret_cast<MaterialData*>(materialDataBuffer.info.pMappedData);
|
||||
|
||||
data[id] = toMaterialData(materials[id]);
|
||||
device->getMemoryManager().flushAllocation(
|
||||
materialDataBuffer,
|
||||
static_cast<VkDeviceSize>(id) * sizeof(MaterialData),
|
||||
sizeof(MaterialData));
|
||||
return toMaterialData(materials[id]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user