From fe3b84b4a7566c3bf2564051a9d0ad496a77d167 Mon Sep 17 00:00:00 2001 From: Samuliak Date: Sat, 3 Aug 2024 15:01:52 +0200 Subject: [PATCH] fix: buffer allocator --- .../HW/Latte/Renderer/Metal/MetalMemoryManager.cpp | 14 ++++++-------- .../HW/Latte/Renderer/Metal/MetalMemoryManager.h | 6 +++--- .../HW/Latte/Renderer/Metal/MetalPipelineCache.cpp | 1 + src/Cafe/HW/Latte/Renderer/Metal/MetalRenderer.cpp | 13 +++++++------ 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/Cafe/HW/Latte/Renderer/Metal/MetalMemoryManager.cpp b/src/Cafe/HW/Latte/Renderer/Metal/MetalMemoryManager.cpp index 53dd8b10..9e615c22 100644 --- a/src/Cafe/HW/Latte/Renderer/Metal/MetalMemoryManager.cpp +++ b/src/Cafe/HW/Latte/Renderer/Metal/MetalMemoryManager.cpp @@ -3,15 +3,16 @@ const size_t BUFFER_ALLOCATION_SIZE = 8 * 1024 * 1024; -// TODO: uncomment everything -MetalBufferAllocation MetalBufferAllocator::GetBufferAllocation(size_t size) +MetalBufferAllocation MetalBufferAllocator::GetBufferAllocation(size_t size, size_t alignment) { + // Align the size + size = (size + alignment - 1) & ~(alignment - 1); + // First, try to find a free range - /* for (uint32 i = 0; i < m_freeBufferRanges.size(); i++) { auto& range = m_freeBufferRanges[i]; - if (range.size >= size) + if (size <= range.size) { MetalBufferAllocation allocation; allocation.bufferIndex = range.bufferIndex; @@ -29,10 +30,9 @@ MetalBufferAllocation MetalBufferAllocator::GetBufferAllocation(size_t size) return allocation; } } - */ // If no free range was found, allocate a new buffer - MTL::Buffer* buffer = m_mtlr->GetDevice()->newBuffer(/*std::max(*/size/*, BUFFER_ALLOCATION_SIZE)*/, MTL::ResourceStorageModeShared); + MTL::Buffer* buffer = m_mtlr->GetDevice()->newBuffer(std::max(size, BUFFER_ALLOCATION_SIZE), MTL::ResourceStorageModeShared); MetalBufferAllocation allocation; allocation.bufferIndex = m_buffers.size(); @@ -42,7 +42,6 @@ MetalBufferAllocation MetalBufferAllocator::GetBufferAllocation(size_t size) m_buffers.push_back(buffer); // If the buffer is larger than the requested size, add the remaining space to the free buffer ranges - /* if (size < BUFFER_ALLOCATION_SIZE) { MetalBufferRange range; @@ -52,7 +51,6 @@ MetalBufferAllocation MetalBufferAllocator::GetBufferAllocation(size_t size) m_freeBufferRanges.push_back(range); } - */ return allocation; } diff --git a/src/Cafe/HW/Latte/Renderer/Metal/MetalMemoryManager.h b/src/Cafe/HW/Latte/Renderer/Metal/MetalMemoryManager.h index c099360f..58096eab 100644 --- a/src/Cafe/HW/Latte/Renderer/Metal/MetalMemoryManager.h +++ b/src/Cafe/HW/Latte/Renderer/Metal/MetalMemoryManager.h @@ -41,7 +41,7 @@ public: return m_buffers[bufferIndex]; } - MetalBufferAllocation GetBufferAllocation(size_t size); + MetalBufferAllocation GetBufferAllocation(size_t size, size_t alignment); private: class MetalRenderer* m_mtlr; @@ -68,9 +68,9 @@ public: return m_bufferAllocator/*s[bufferAllocatorIndex]*/.GetBuffer(bufferIndex); } - MetalBufferAllocation GetBufferAllocation(size_t size) + MetalBufferAllocation GetBufferAllocation(size_t size, size_t alignment) { - auto allocation = m_bufferAllocator/*s[m_bufferAllocatorIndex]*/.GetBufferAllocation(size); + auto allocation = m_bufferAllocator/*s[m_bufferAllocatorIndex]*/.GetBufferAllocation(size, alignment); //allocation.bufferIndex |= (m_bufferAllocatorIndex << bufferAllocatorIndexShift); return allocation; diff --git a/src/Cafe/HW/Latte/Renderer/Metal/MetalPipelineCache.cpp b/src/Cafe/HW/Latte/Renderer/Metal/MetalPipelineCache.cpp index 28f32193..328fd6d0 100644 --- a/src/Cafe/HW/Latte/Renderer/Metal/MetalPipelineCache.cpp +++ b/src/Cafe/HW/Latte/Renderer/Metal/MetalPipelineCache.cpp @@ -129,6 +129,7 @@ MTL::RenderPipelineState* MetalPipelineCache::GetPipelineState(const LatteFetchS { auto texture = static_cast(activeFBO->depthBuffer.texture); desc->setDepthAttachmentPixelFormat(texture->GetTexture()->pixelFormat()); + // TODO: stencil pixel format } NS::Error* error = nullptr; diff --git a/src/Cafe/HW/Latte/Renderer/Metal/MetalRenderer.cpp b/src/Cafe/HW/Latte/Renderer/Metal/MetalRenderer.cpp index 5e1a74ec..efc9233a 100644 --- a/src/Cafe/HW/Latte/Renderer/Metal/MetalRenderer.cpp +++ b/src/Cafe/HW/Latte/Renderer/Metal/MetalRenderer.cpp @@ -16,6 +16,7 @@ #include "Cemu/Logging/CemuDebugLogging.h" #include "HW/Latte/Core/Latte.h" #include "gui/guiWrapper.h" +#include extern bool hasValidFramebufferAttached; @@ -143,6 +144,9 @@ void MetalRenderer::SwapBuffers(bool swapTV, bool swapDRC) m_drawableAcquired = false; CommitCommandBuffer(); + + // Reset temporary buffers + m_memoryManager->ResetTemporaryBuffers(); } void MetalRenderer::DrawBackbufferQuad(LatteTextureView* texView, RendererOutputShader* shader, bool useLinearTexFilter, @@ -661,7 +665,7 @@ void MetalRenderer::draw_execute(uint32 baseVertex, uint32 baseInstance, uint32 { auto mtlIndexType = GetMtlIndexType(hostIndexType); MTL::Buffer* indexBuffer = m_memoryManager->GetBuffer(indexBufferIndex); - renderCommandEncoder->drawIndexedPrimitives(mtlPrimitiveType, hostIndexCount, mtlIndexType, indexBuffer, 0, instanceCount, baseVertex, baseInstance); + renderCommandEncoder->drawIndexedPrimitives(mtlPrimitiveType, hostIndexCount, mtlIndexType, indexBuffer, indexBufferOffset, instanceCount, baseVertex, baseInstance); } else { renderCommandEncoder->drawPrimitives(mtlPrimitiveType, baseVertex, count, instanceCount, baseInstance); @@ -675,7 +679,7 @@ void MetalRenderer::draw_endSequence() void* MetalRenderer::indexData_reserveIndexMemory(uint32 size, uint32& offset, uint32& bufferIndex) { - auto allocation = m_memoryManager->GetBufferAllocation(size); + auto allocation = m_memoryManager->GetBufferAllocation(size, 4); offset = allocation.bufferOffset; bufferIndex = allocation.bufferIndex; @@ -684,7 +688,7 @@ void* MetalRenderer::indexData_reserveIndexMemory(uint32 size, uint32& offset, u void MetalRenderer::indexData_uploadIndexMemory(uint32 offset, uint32 size) { - debug_printf("MetalRenderer::indexData_uploadIndexMemory not implemented\n"); + // Do nothing, since the buffer has shared storage mode } void MetalRenderer::EnsureCommandBuffer() @@ -816,9 +820,6 @@ void MetalRenderer::CommitCommandBuffer() m_commandBuffer->release(); m_commandBuffer = nullptr; - // Reset temporary buffers - m_memoryManager->ResetTemporaryBuffers(); - // Debug m_commandQueue->insertDebugCaptureBoundary(); }