From f2096dedddb9a605c467a043df569f7f3c0c212c Mon Sep 17 00:00:00 2001 From: Samuliak Date: Tue, 1 Oct 2024 18:11:05 +0200 Subject: [PATCH] remove vertex restriding code --- .../Renderer/Metal/MetalMemoryManager.cpp | 16 +++--- .../Latte/Renderer/Metal/MetalMemoryManager.h | 11 ++-- .../HW/Latte/Renderer/Metal/MetalRenderer.cpp | 51 ++++++++++++++----- .../HW/Latte/Renderer/Metal/MetalRenderer.h | 6 ++- 4 files changed, 56 insertions(+), 28 deletions(-) diff --git a/src/Cafe/HW/Latte/Renderer/Metal/MetalMemoryManager.cpp b/src/Cafe/HW/Latte/Renderer/Metal/MetalMemoryManager.cpp index 9765b24a..1c788e21 100644 --- a/src/Cafe/HW/Latte/Renderer/Metal/MetalMemoryManager.cpp +++ b/src/Cafe/HW/Latte/Renderer/Metal/MetalMemoryManager.cpp @@ -2,9 +2,8 @@ #include "Cafe/HW/Latte/Renderer/Metal/MetalMemoryManager.h" #include "Cafe/HW/Latte/Renderer/Metal/MetalVoidVertexPipeline.h" #include "Common/precompiled.h" -#include "HW/Latte/Renderer/Metal/MetalRenderer.h" -#include "Metal/MTLResource.hpp" +/* MetalVertexBufferCache::~MetalVertexBufferCache() { } @@ -28,13 +27,11 @@ MetalRestridedBufferRange MetalVertexBufferCache::RestrideBufferIfNeeded(MTL::Bu restrideInfo.allocation = m_bufferAllocator.GetBufferAllocation(newSize); buffer = m_bufferAllocator.GetBuffer(restrideInfo.allocation.bufferIndex); - /* - uint8* oldPtr = (uint8*)bufferCache->contents() + vertexBufferRange.offset; - uint8* newPtr = (uint8*)buffer->contents() + restrideInfo.allocation.offset; + //uint8* oldPtr = (uint8*)bufferCache->contents() + vertexBufferRange.offset; + //uint8* newPtr = (uint8*)buffer->contents() + restrideInfo.allocation.offset; - for (size_t elem = 0; elem < vertexBufferRange.size / stride; elem++) - memcpy(newPtr + elem * newStride, oldPtr + elem * stride, stride); - */ + //for (size_t elem = 0; elem < vertexBufferRange.size / stride; elem++) + // memcpy(newPtr + elem * newStride, oldPtr + elem * stride, stride); if (m_mtlr->GetEncoderType() == MetalEncoderType::Render) { @@ -94,6 +91,7 @@ void MetalVertexBufferCache::MemoryRangeChanged(size_t offset, size_t size) } } } +*/ MetalMemoryManager::~MetalMemoryManager() { @@ -144,7 +142,7 @@ void MetalMemoryManager::UploadToBufferCache(const void* data, size_t offset, si m_tempBufferAllocator.UnlockBuffer(allocation.bufferIndex); // Notify vertex buffer cache about the change - m_vertexBufferCache.MemoryRangeChanged(offset, size); + //m_vertexBufferCache.MemoryRangeChanged(offset, size); } void MetalMemoryManager::CopyBufferCache(size_t srcOffset, size_t dstOffset, size_t size) diff --git a/src/Cafe/HW/Latte/Renderer/Metal/MetalMemoryManager.h b/src/Cafe/HW/Latte/Renderer/Metal/MetalMemoryManager.h index a79111fd..4ea5769e 100644 --- a/src/Cafe/HW/Latte/Renderer/Metal/MetalMemoryManager.h +++ b/src/Cafe/HW/Latte/Renderer/Metal/MetalMemoryManager.h @@ -1,8 +1,8 @@ #pragma once #include "Cafe/HW/Latte/Renderer/Metal/MetalBufferAllocator.h" -#include "Metal/MTLResource.hpp" +/* struct MetalRestridedBufferRange { MTL::Buffer* buffer; @@ -54,18 +54,21 @@ private: void MemoryRangeChanged(size_t offset, size_t size); }; +*/ class MetalMemoryManager { public: - MetalMemoryManager(class MetalRenderer* metalRenderer) : m_mtlr{metalRenderer}, m_bufferAllocator(metalRenderer, m_mtlr->GetOptimalBufferStorageMode()), m_framePersistentBufferAllocator(metalRenderer, MTL::ResourceStorageModePrivate), m_tempBufferAllocator(metalRenderer), m_vertexBufferCache(metalRenderer, m_framePersistentBufferAllocator) {} + MetalMemoryManager(class MetalRenderer* metalRenderer) : m_mtlr{metalRenderer}, m_bufferAllocator(metalRenderer, m_mtlr->GetOptimalBufferStorageMode()), m_framePersistentBufferAllocator(metalRenderer, MTL::ResourceStorageModePrivate), m_tempBufferAllocator(metalRenderer)/*, m_vertexBufferCache(metalRenderer, m_framePersistentBufferAllocator)*/ {} ~MetalMemoryManager(); // Pipelines + /* void SetRestrideBufferPipeline(class MetalVoidVertexPipeline* restrideBufferPipeline) { m_vertexBufferCache.SetRestrideBufferPipeline(restrideBufferPipeline); } + */ MetalDefaultBufferAllocator& GetBufferAllocator() { @@ -95,6 +98,7 @@ public: void CopyBufferCache(size_t srcOffset, size_t dstOffset, size_t size); // Vertex buffer cache + /* void TrackVertexBuffer(uint32 bufferIndex, size_t offset, size_t size, MetalRestrideInfo* restrideInfo) { m_vertexBufferCache.TrackVertexBuffer(bufferIndex, offset, size, restrideInfo); @@ -109,6 +113,7 @@ public: { return m_vertexBufferCache.RestrideBufferIfNeeded(m_bufferCache, bufferIndex, stride, barrierBuffers); } + */ private: class MetalRenderer* m_mtlr; @@ -118,7 +123,7 @@ private: MetalDefaultBufferAllocator m_bufferAllocator; MetalDefaultBufferAllocator m_framePersistentBufferAllocator; MetalTemporaryBufferAllocator m_tempBufferAllocator; - MetalVertexBufferCache m_vertexBufferCache; + //MetalVertexBufferCache m_vertexBufferCache; MTL::Buffer* m_bufferCache = nullptr; }; diff --git a/src/Cafe/HW/Latte/Renderer/Metal/MetalRenderer.cpp b/src/Cafe/HW/Latte/Renderer/Metal/MetalRenderer.cpp index 41f48b11..7f10365b 100644 --- a/src/Cafe/HW/Latte/Renderer/Metal/MetalRenderer.cpp +++ b/src/Cafe/HW/Latte/Renderer/Metal/MetalRenderer.cpp @@ -163,10 +163,10 @@ MetalRenderer::MetalRenderer() if (m_isAppleGPU) m_copyBufferToBufferPipeline = new MetalVoidVertexPipeline(this, utilityLibrary, "vertexCopyBufferToBuffer"); //m_copyTextureToTexturePipeline = new MetalVoidVertexPipeline(this, utilityLibrary, "vertexCopyTextureToTexture"); - m_restrideBufferPipeline = new MetalVoidVertexPipeline(this, utilityLibrary, "vertexRestrideBuffer"); + //m_restrideBufferPipeline = new MetalVoidVertexPipeline(this, utilityLibrary, "vertexRestrideBuffer"); utilityLibrary->release(); - m_memoryManager->SetRestrideBufferPipeline(m_restrideBufferPipeline); + //m_memoryManager->SetRestrideBufferPipeline(m_restrideBufferPipeline); } MetalRenderer::~MetalRenderer() @@ -174,7 +174,7 @@ MetalRenderer::~MetalRenderer() if (m_isAppleGPU) delete m_copyBufferToBufferPipeline; //delete m_copyTextureToTexturePipeline; - delete m_restrideBufferPipeline; + //delete m_restrideBufferPipeline; //m_presentPipelineLinear->release(); //m_presentPipelineSRGB->release(); @@ -831,16 +831,16 @@ void MetalRenderer::buffer_bindVertexBuffer(uint32 bufferIndex, uint32 offset, u if (buffer.offset == offset && buffer.size == size) return; - if (buffer.offset != INVALID_OFFSET) - { - m_memoryManager->UntrackVertexBuffer(bufferIndex); - } + //if (buffer.offset != INVALID_OFFSET) + //{ + // m_memoryManager->UntrackVertexBuffer(bufferIndex); + //} buffer.offset = offset; buffer.size = size; - buffer.restrideInfo = {}; + //buffer.restrideInfo = {}; - m_memoryManager->TrackVertexBuffer(bufferIndex, offset, size, &buffer.restrideInfo); + //m_memoryManager->TrackVertexBuffer(bufferIndex, offset, size, &buffer.restrideInfo); } void MetalRenderer::buffer_bindUniformBuffer(LatteConst::ShaderType shaderType, uint32 bufferIndex, uint32 offset, uint32 size) @@ -1169,12 +1169,35 @@ void MetalRenderer::draw_execute(uint32 baseVertex, uint32 baseInstance, uint32 // Resources // Vertex buffers - std::vector barrierBuffers; + //std::vector barrierBuffers; for (uint8 i = 0; i < MAX_MTL_BUFFERS; i++) { auto& vertexBufferRange = m_state.m_vertexBuffers[i]; if (vertexBufferRange.offset != INVALID_OFFSET) { + /* + MTL::Buffer* buffer; + size_t offset; + + // Restride + if (usesGeometryShader) + { + // Object shaders don't need restriding, since the attributes are fetched in the shader + buffer = m_memoryManager->GetBufferCache(); + offset = m_state.m_vertexBuffers[i].offset; + } + else + { + uint32 bufferBaseRegisterIndex = mmSQ_VTX_ATTRIBUTE_BLOCK_START + i * 7; + uint32 bufferStride = (LatteGPUState.contextNew.GetRawView()[bufferBaseRegisterIndex + 2] >> 11) & 0xFFFF; + + auto restridedBuffer = m_memoryManager->RestrideBufferIfNeeded(i, bufferStride, barrierBuffers); + + buffer = restridedBuffer.buffer; + offset = restridedBuffer.offset; + } + */ + MTL::Buffer* buffer = m_memoryManager->GetBufferCache(); size_t offset = m_state.m_vertexBuffers[i].offset; @@ -1183,10 +1206,10 @@ void MetalRenderer::draw_execute(uint32 baseVertex, uint32 baseInstance, uint32 } } - if (!barrierBuffers.empty()) - { - renderCommandEncoder->memoryBarrier(barrierBuffers.data(), barrierBuffers.size(), MTL::RenderStageVertex, MTL::RenderStageVertex); - } + //if (!barrierBuffers.empty()) + //{ + // renderCommandEncoder->memoryBarrier(barrierBuffers.data(), barrierBuffers.size(), MTL::RenderStageVertex, MTL::RenderStageVertex); + //} // Render pipeline state MTL::RenderPipelineState* renderPipelineState; diff --git a/src/Cafe/HW/Latte/Renderer/Metal/MetalRenderer.h b/src/Cafe/HW/Latte/Renderer/Metal/MetalRenderer.h index 2c9b150f..526f33a5 100644 --- a/src/Cafe/HW/Latte/Renderer/Metal/MetalRenderer.h +++ b/src/Cafe/HW/Latte/Renderer/Metal/MetalRenderer.h @@ -19,19 +19,21 @@ struct MetalBufferAllocation } }; +/* struct MetalRestrideInfo { bool memoryInvalidated = true; size_t lastStride = 0; MetalBufferAllocation allocation{}; }; +*/ struct MetalBoundBuffer { size_t offset = INVALID_OFFSET; size_t size = 0; // Memory manager will write restride info to this variable - MetalRestrideInfo restrideInfo; + //MetalRestrideInfo restrideInfo; }; enum MetalGeneralShaderType @@ -473,7 +475,7 @@ private: // Hybrid pipelines class MetalVoidVertexPipeline* m_copyBufferToBufferPipeline; //class MetalVoidVertexPipeline* m_copyTextureToTexturePipeline; - class MetalVoidVertexPipeline* m_restrideBufferPipeline; + //class MetalVoidVertexPipeline* m_restrideBufferPipeline; // Resources MTL::SamplerState* m_nearestSampler;