From 6c8947d0e5f95941cecc7456d087ab3d976b2761 Mon Sep 17 00:00:00 2001 From: Samo Z Date: Mon, 26 Aug 2024 08:07:49 +0200 Subject: [PATCH] fix: temporary buffer allocator --- .../HW/Latte/Renderer/Metal/MetalBufferAllocator.h | 12 +++++++++++- src/Cafe/HW/Latte/Renderer/Metal/MetalRenderer.cpp | 9 +++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/Cafe/HW/Latte/Renderer/Metal/MetalBufferAllocator.h b/src/Cafe/HW/Latte/Renderer/Metal/MetalBufferAllocator.h index d3b0cc5e..96724e88 100644 --- a/src/Cafe/HW/Latte/Renderer/Metal/MetalBufferAllocator.h +++ b/src/Cafe/HW/Latte/Renderer/Metal/MetalBufferAllocator.h @@ -152,7 +152,7 @@ struct MetalSyncedBuffer class MetalTemporaryBufferAllocator : public MetalBufferAllocator { public: - MetalTemporaryBufferAllocator(class MetalRenderer* metalRenderer) : MetalBufferAllocator(metalRenderer, metalRenderer->GetOptimalBufferStorageMode()) {} + MetalTemporaryBufferAllocator(class MetalRenderer* metalRenderer) : MetalBufferAllocator(metalRenderer, MTL::ResourceStorageModeShared) {} void SetActiveCommandBuffer(MTL::CommandBuffer* commandBuffer) { @@ -170,6 +170,16 @@ public: { if (buffer.m_commandBuffers.size() == 1) { + // First remove any free ranges that use this buffer + for (uint32 k = 0; k < m_freeBufferRanges.size(); k++) + { + if (m_freeBufferRanges[k].bufferIndex == i) + { + m_freeBufferRanges.erase(m_freeBufferRanges.begin() + k); + k--; + } + } + // All command buffers using it have finished execution, we can use it again m_freeBufferRanges.push_back({i, 0, buffer.m_buffer->length()}); diff --git a/src/Cafe/HW/Latte/Renderer/Metal/MetalRenderer.cpp b/src/Cafe/HW/Latte/Renderer/Metal/MetalRenderer.cpp index b01e4148..854a6e00 100644 --- a/src/Cafe/HW/Latte/Renderer/Metal/MetalRenderer.cpp +++ b/src/Cafe/HW/Latte/Renderer/Metal/MetalRenderer.cpp @@ -509,8 +509,7 @@ void MetalRenderer::texture_loadSlice(LatteTexture* hostTexture, sint32 width, s auto blitCommandEncoder = GetBlitCommandEncoder(); // Allocate a temporary buffer - // HACK: use the persistent buffer allocator so as to avoid any issues - auto& bufferAllocator = m_memoryManager->GetBufferAllocator(); + auto& bufferAllocator = m_memoryManager->GetTemporaryBufferAllocator(); auto allocation = bufferAllocator.GetBufferAllocation(compressedImageSize); auto buffer = bufferAllocator.GetBuffer(allocation.bufferIndex); @@ -1192,9 +1191,11 @@ void* MetalRenderer::indexData_reserveIndexMemory(uint32 size, uint32& offset, u void MetalRenderer::indexData_uploadIndexMemory(uint32 bufferIndex, uint32 offset, uint32 size) { - auto buffer = m_memoryManager->GetTemporaryBufferAllocator().GetBuffer(bufferIndex); if (!HasUnifiedMemory()) + { + auto buffer = m_memoryManager->GetTemporaryBufferAllocator().GetBuffer(bufferIndex); buffer->didModifyRange(NS::Range(offset, size)); + } } void MetalRenderer::SetBuffer(MTL::RenderCommandEncoder* renderCommandEncoder, MetalShaderType shaderType, MTL::Buffer* buffer, size_t offset, uint32 index) @@ -1459,7 +1460,7 @@ void MetalRenderer::CommitCommandBuffer() auto& commandBuffer = m_commandBuffers.back(); if (!commandBuffer.m_commited) { - commandBuffer.m_commandBuffer->addCompletedHandler(^(MTL::CommandBuffer* cmd) { + commandBuffer.m_commandBuffer->addCompletedHandler(^(MTL::CommandBuffer*) { m_memoryManager->GetTemporaryBufferAllocator().CommandBufferFinished(commandBuffer.m_commandBuffer); });