fix: buffer allocator

This commit is contained in:
Samuliak 2024-08-03 15:01:52 +02:00
parent 406a85672d
commit fe3b84b4a7
4 changed files with 17 additions and 17 deletions

View File

@ -3,15 +3,16 @@
const size_t BUFFER_ALLOCATION_SIZE = 8 * 1024 * 1024; const size_t BUFFER_ALLOCATION_SIZE = 8 * 1024 * 1024;
// TODO: uncomment everything MetalBufferAllocation MetalBufferAllocator::GetBufferAllocation(size_t size, size_t alignment)
MetalBufferAllocation MetalBufferAllocator::GetBufferAllocation(size_t size)
{ {
// Align the size
size = (size + alignment - 1) & ~(alignment - 1);
// First, try to find a free range // First, try to find a free range
/*
for (uint32 i = 0; i < m_freeBufferRanges.size(); i++) for (uint32 i = 0; i < m_freeBufferRanges.size(); i++)
{ {
auto& range = m_freeBufferRanges[i]; auto& range = m_freeBufferRanges[i];
if (range.size >= size) if (size <= range.size)
{ {
MetalBufferAllocation allocation; MetalBufferAllocation allocation;
allocation.bufferIndex = range.bufferIndex; allocation.bufferIndex = range.bufferIndex;
@ -29,10 +30,9 @@ MetalBufferAllocation MetalBufferAllocator::GetBufferAllocation(size_t size)
return allocation; return allocation;
} }
} }
*/
// If no free range was found, allocate a new buffer // 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; MetalBufferAllocation allocation;
allocation.bufferIndex = m_buffers.size(); allocation.bufferIndex = m_buffers.size();
@ -42,7 +42,6 @@ MetalBufferAllocation MetalBufferAllocator::GetBufferAllocation(size_t size)
m_buffers.push_back(buffer); m_buffers.push_back(buffer);
// If the buffer is larger than the requested size, add the remaining space to the free buffer ranges // If the buffer is larger than the requested size, add the remaining space to the free buffer ranges
/*
if (size < BUFFER_ALLOCATION_SIZE) if (size < BUFFER_ALLOCATION_SIZE)
{ {
MetalBufferRange range; MetalBufferRange range;
@ -52,7 +51,6 @@ MetalBufferAllocation MetalBufferAllocator::GetBufferAllocation(size_t size)
m_freeBufferRanges.push_back(range); m_freeBufferRanges.push_back(range);
} }
*/
return allocation; return allocation;
} }

View File

@ -41,7 +41,7 @@ public:
return m_buffers[bufferIndex]; return m_buffers[bufferIndex];
} }
MetalBufferAllocation GetBufferAllocation(size_t size); MetalBufferAllocation GetBufferAllocation(size_t size, size_t alignment);
private: private:
class MetalRenderer* m_mtlr; class MetalRenderer* m_mtlr;
@ -68,9 +68,9 @@ public:
return m_bufferAllocator/*s[bufferAllocatorIndex]*/.GetBuffer(bufferIndex); 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); //allocation.bufferIndex |= (m_bufferAllocatorIndex << bufferAllocatorIndexShift);
return allocation; return allocation;

View File

@ -129,6 +129,7 @@ MTL::RenderPipelineState* MetalPipelineCache::GetPipelineState(const LatteFetchS
{ {
auto texture = static_cast<LatteTextureViewMtl*>(activeFBO->depthBuffer.texture); auto texture = static_cast<LatteTextureViewMtl*>(activeFBO->depthBuffer.texture);
desc->setDepthAttachmentPixelFormat(texture->GetTexture()->pixelFormat()); desc->setDepthAttachmentPixelFormat(texture->GetTexture()->pixelFormat());
// TODO: stencil pixel format
} }
NS::Error* error = nullptr; NS::Error* error = nullptr;

View File

@ -16,6 +16,7 @@
#include "Cemu/Logging/CemuDebugLogging.h" #include "Cemu/Logging/CemuDebugLogging.h"
#include "HW/Latte/Core/Latte.h" #include "HW/Latte/Core/Latte.h"
#include "gui/guiWrapper.h" #include "gui/guiWrapper.h"
#include <stdexcept>
extern bool hasValidFramebufferAttached; extern bool hasValidFramebufferAttached;
@ -143,6 +144,9 @@ void MetalRenderer::SwapBuffers(bool swapTV, bool swapDRC)
m_drawableAcquired = false; m_drawableAcquired = false;
CommitCommandBuffer(); CommitCommandBuffer();
// Reset temporary buffers
m_memoryManager->ResetTemporaryBuffers();
} }
void MetalRenderer::DrawBackbufferQuad(LatteTextureView* texView, RendererOutputShader* shader, bool useLinearTexFilter, 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); auto mtlIndexType = GetMtlIndexType(hostIndexType);
MTL::Buffer* indexBuffer = m_memoryManager->GetBuffer(indexBufferIndex); 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 } else
{ {
renderCommandEncoder->drawPrimitives(mtlPrimitiveType, baseVertex, count, instanceCount, baseInstance); 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) 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; offset = allocation.bufferOffset;
bufferIndex = allocation.bufferIndex; bufferIndex = allocation.bufferIndex;
@ -684,7 +688,7 @@ void* MetalRenderer::indexData_reserveIndexMemory(uint32 size, uint32& offset, u
void MetalRenderer::indexData_uploadIndexMemory(uint32 offset, uint32 size) 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() void MetalRenderer::EnsureCommandBuffer()
@ -816,9 +820,6 @@ void MetalRenderer::CommitCommandBuffer()
m_commandBuffer->release(); m_commandBuffer->release();
m_commandBuffer = nullptr; m_commandBuffer = nullptr;
// Reset temporary buffers
m_memoryManager->ResetTemporaryBuffers();
// Debug // Debug
m_commandQueue->insertDebugCaptureBoundary(); m_commandQueue->insertDebugCaptureBoundary();
} }