From 4fec79513b0c7de3983f53cafdf03c95effdce4b Mon Sep 17 00:00:00 2001 From: Isaac Marovitz Date: Wed, 1 Jan 2025 15:35:22 +0000 Subject: [PATCH] Mildly improve the leak situation --- core/rend/metal/metal_buffer.h | 2 ++ core/rend/metal/metal_context.cpp | 2 ++ core/rend/metal/metal_context.h | 1 + core/rend/metal/metal_driver.h | 4 +--- core/rend/metal/metal_pipeline.h | 19 ------------------- core/rend/metal/metal_renderer.cpp | 30 +++--------------------------- core/rend/metal/metal_renderer.h | 1 - core/rend/metal/metal_shaders.h | 12 ------------ 8 files changed, 9 insertions(+), 62 deletions(-) diff --git a/core/rend/metal/metal_buffer.h b/core/rend/metal/metal_buffer.h index 259b48eac2..567d55c792 100644 --- a/core/rend/metal/metal_buffer.h +++ b/core/rend/metal/metal_buffer.h @@ -25,7 +25,9 @@ struct MetalBufferData MetalBufferData(u64 size); ~MetalBufferData() { + buffer->setPurgeableState(MTL::PurgeableStateEmpty); buffer->release(); + buffer = nullptr; } void upload(u32 size, const void *data, u32 bufOffset = 0) const diff --git a/core/rend/metal/metal_context.cpp b/core/rend/metal/metal_context.cpp index cfa7b57ddb..ac0f19e666 100644 --- a/core/rend/metal/metal_context.cpp +++ b/core/rend/metal/metal_context.cpp @@ -25,6 +25,7 @@ Copyright 2024 flyinghead MetalContext *MetalContext::contextInstance; bool MetalContext::init() { + pool = NS::AutoreleasePool::alloc()->init(); GraphicsContext::instance = this; #if defined(USE_SDL) @@ -69,6 +70,7 @@ void MetalContext::Present() { void MetalContext::term() { GraphicsContext::instance = nullptr; imguiDriver.reset(); + pool->release(); } MetalContext::MetalContext() { diff --git a/core/rend/metal/metal_context.h b/core/rend/metal/metal_context.h index a20412c853..f9a66353e5 100644 --- a/core/rend/metal/metal_context.h +++ b/core/rend/metal/metal_context.h @@ -56,6 +56,7 @@ class MetalContext : public GraphicsContext static MetalContext* Instance() { return contextInstance; } private: + NS::AutoreleasePool *pool; MTL::Device* device = MTL::CreateSystemDefaultDevice(); MTL::CommandQueue *queue = nullptr; CA::MetalLayer* layer; diff --git a/core/rend/metal/metal_driver.h b/core/rend/metal/metal_driver.h index e84b09f1e6..789e374f4f 100644 --- a/core/rend/metal/metal_driver.h +++ b/core/rend/metal/metal_driver.h @@ -54,7 +54,7 @@ class MetalDriver final : public ImGuiDriver { ImGui_ImplMetal_NewFrame(descriptor); - // descriptor->release(); + descriptor->release(); } void renderDrawData(ImDrawData *drawData, bool gui_open) override { @@ -67,8 +67,6 @@ class MetalDriver final : public ImGuiDriver { buffer->presentDrawable(drawable); buffer->commit(); - buffer->release(); - commandEncoder->release(); commandEncoder = nullptr; context->commandBuffer = context->GetQueue()->commandBuffer(); diff --git a/core/rend/metal/metal_pipeline.h b/core/rend/metal/metal_pipeline.h index 61c19c4719..61332ff08a 100644 --- a/core/rend/metal/metal_pipeline.h +++ b/core/rend/metal/metal_pipeline.h @@ -34,28 +34,9 @@ class MetalPipelineManager void term() { - for (auto [i, pipeline] : pipelines) { - pipeline->release(); - } - pipelines.clear(); - - for (auto [i, pipeline] : depthPassPipelines) { - pipeline->release(); - } - depthPassPipelines.clear(); - - for (auto [i, state] : depthStencilStates) { - state->release(); - } - depthStencilStates.clear(); - - for (auto [i, state] : depthPassDepthStencilStates) { - state->release(); - } - depthPassDepthStencilStates.clear(); } diff --git a/core/rend/metal/metal_renderer.cpp b/core/rend/metal/metal_renderer.cpp index 64ba1a1f94..3bddb03fb1 100644 --- a/core/rend/metal/metal_renderer.cpp +++ b/core/rend/metal/metal_renderer.cpp @@ -378,6 +378,8 @@ void MetalRenderer::UploadMainBuffer(const VertexShaderUniforms &vertexUniforms, } bool MetalRenderer::Draw(const MetalTexture *fogTexture, const MetalTexture *paletteTexture) { + NS::AutoreleasePool *pool = NS::AutoreleasePool::alloc()->init(); + FragmentShaderUniforms fragUniforms = MakeFragmentUniforms(); dithering = config::EmulateFramebuffer && pvrrc.fb_W_CTRL.fb_dither && pvrrc.fb_W_CTRL.fb_packmode <= 3; if (dithering) { @@ -471,25 +473,6 @@ bool MetalRenderer::Draw(const MetalTexture *fogTexture, const MetalTexture *pal encoder->setFragmentTexture(paletteTexture->texture, 3); } - MTL::CaptureManager *captureManager; - - // if (pvrrc.render_passes.size() > 0 && frameIndex >= 0) { - // MTL::CaptureDescriptor *capture = MTL::CaptureDescriptor::alloc()->init(); - // capture->setCaptureObject(MetalContext::Instance()->GetDevice()); - // capture->setDestination(MTL::CaptureDestinationGPUTraceDocument); - // std::string filePath = "/Users/isaacmarovitz/Documents/TRACES/flycast-" + std::to_string(frameIndex) + ".gputrace"; - // capture->setOutputURL(NS::URL::fileURLWithPath(NS::String::string(filePath.c_str(), NS::UTF8StringEncoding))); - // - // captureManager = MTL::CaptureManager::sharedCaptureManager(); - // - // NS::Error *error = nullptr; - // if (!captureManager->startCapture(capture, &error)) { - // ERROR_LOG(RENDERER, "Failed to start capture, %s", error->localizedDescription()->utf8String()); - // } - // } - // - // frameIndex++; - // Upload vertex and index buffers VertexShaderUniforms vtxUniforms; vtxUniforms.ndcMat = matrices.GetNormalMatrix(); @@ -522,14 +505,7 @@ bool MetalRenderer::Draw(const MetalTexture *fogTexture, const MetalTexture *pal encoder->endEncoding(); buffer->presentDrawable(drawable); buffer->commit(); - - // - // if (pvrrc.render_passes.size() > 0) { - // captureManager->stopCapture(); - // } - - buffer->release(); - encoder->release(); + pool->release(); DEBUG_LOG(RENDERER, "Render command buffer released"); diff --git a/core/rend/metal/metal_renderer.h b/core/rend/metal/metal_renderer.h index 4c20b1893b..121f64f750 100644 --- a/core/rend/metal/metal_renderer.h +++ b/core/rend/metal/metal_renderer.h @@ -138,5 +138,4 @@ class MetalRenderer final : public Renderer MetalSamplers samplers; bool frameRendered = false; bool dithering = false; - s64 frameIndex = -350; }; diff --git a/core/rend/metal/metal_shaders.h b/core/rend/metal/metal_shaders.h index 04e15dd2f5..812a7e59ce 100644 --- a/core/rend/metal/metal_shaders.h +++ b/core/rend/metal/metal_shaders.h @@ -89,21 +89,9 @@ class MetalShaders void term() { - for (auto &[u, func] : vertexShaders) { - func->release(); - } - vertexShaders.clear(); - - for (auto &[u, func] : fragmentShaders) { - func->release(); - } - fragmentShaders.clear(); - vertexShaderLibrary->release(); - fragmentShaderLibrary->release(); - vertexShaderConstants->release(); fragmentShaderConstants->release(); }