diff --git a/src/Cafe/HW/Latte/Renderer/Metal/MetalRenderer.cpp b/src/Cafe/HW/Latte/Renderer/Metal/MetalRenderer.cpp index d1b8f268..f28b79b8 100644 --- a/src/Cafe/HW/Latte/Renderer/Metal/MetalRenderer.cpp +++ b/src/Cafe/HW/Latte/Renderer/Metal/MetalRenderer.cpp @@ -27,6 +27,8 @@ #include "imgui/imgui_extension.h" #include "imgui/imgui_impl_metal.h" +#define EVENT_VALUE_WRAP 4096 + extern bool hasValidFramebufferAttached; float supportBufferData[512 * 4]; @@ -48,6 +50,9 @@ MetalRenderer::MetalRenderer() CheckForPixelFormatSupport(m_pixelFormatSupport); + // Synchronization resources + m_event = m_device->newEvent(); + // Resources MTL::SamplerDescriptor* samplerDescriptor = MTL::SamplerDescriptor::alloc()->init(); #ifdef CEMU_DEBUG_ASSERT @@ -161,6 +166,8 @@ MetalRenderer::~MetalRenderer() m_occlusionQuery.m_resultBuffer->release(); + m_event->release(); + m_commandQueue->release(); m_device->release(); } @@ -1509,6 +1516,10 @@ MTL::CommandBuffer* MetalRenderer::GetCommandBuffer() MTL::CommandBuffer* mtlCommandBuffer = m_commandQueue->commandBuffer(); m_currentCommandBuffer = {mtlCommandBuffer}; + // Wait for the previous command buffer + if (m_eventValue != -1) + mtlCommandBuffer->encodeWait(m_event, m_eventValue); + m_recordedDrawcalls = 0; m_commitTreshold = m_defaultCommitTreshlod; @@ -1682,6 +1693,10 @@ void MetalRenderer::CommitCommandBuffer() // m_memoryManager->GetTemporaryBufferAllocator().CommandBufferFinished(commandBuffer.m_commandBuffer); //}); + // Signal event + m_eventValue = (m_eventValue + 1) % EVENT_VALUE_WRAP; + m_currentCommandBuffer.m_commandBuffer->encodeSignalEvent(m_event, m_eventValue); + m_currentCommandBuffer.m_commandBuffer->commit(); m_currentCommandBuffer.m_commandBuffer->release(); m_currentCommandBuffer.m_commited = true; diff --git a/src/Cafe/HW/Latte/Renderer/Metal/MetalRenderer.h b/src/Cafe/HW/Latte/Renderer/Metal/MetalRenderer.h index 16235611..c272c729 100644 --- a/src/Cafe/HW/Latte/Renderer/Metal/MetalRenderer.h +++ b/src/Cafe/HW/Latte/Renderer/Metal/MetalRenderer.h @@ -6,6 +6,7 @@ #include "Cafe/HW/Latte/Renderer/Metal/MetalPerformanceMonitor.h" #include "Cafe/HW/Latte/Renderer/Metal/MetalOutputShaderCache.h" #include "Cafe/HW/Latte/Renderer/Metal/MetalAttachmentsInfo.h" +#include struct MetalBufferAllocation { @@ -459,6 +460,10 @@ private: // Void vertex pipelines class MetalVoidVertexPipeline* m_copyBufferToBufferPipeline; + // Synchronization resources + MTL::Event* m_event; + int32_t m_eventValue = -1; + // Resources MTL::SamplerState* m_nearestSampler; MTL::SamplerState* m_linearSampler;