From 23af64ade84c1d85243bb514b72be68d6456617c Mon Sep 17 00:00:00 2001 From: Samuliak Date: Sun, 7 Jul 2024 12:34:04 +0200 Subject: [PATCH] release resources --- .../renderer_mtl/mtl_blit_pipeline_cache.hpp | 4 +- .../renderer_mtl/mtl_depth_stencil_cache.hpp | 4 +- .../renderer_mtl/mtl_draw_pipeline_cache.hpp | 4 +- .../renderer_mtl/mtl_vertex_buffer_cache.hpp | 4 +- include/renderer_mtl/renderer_mtl.hpp | 5 +++ src/core/renderer_mtl/renderer_mtl.cpp | 42 ++++++++++++++----- 6 files changed, 44 insertions(+), 19 deletions(-) diff --git a/include/renderer_mtl/mtl_blit_pipeline_cache.hpp b/include/renderer_mtl/mtl_blit_pipeline_cache.hpp index 93d847a6..e0079ce6 100644 --- a/include/renderer_mtl/mtl_blit_pipeline_cache.hpp +++ b/include/renderer_mtl/mtl_blit_pipeline_cache.hpp @@ -18,7 +18,7 @@ public: BlitPipelineCache() = default; ~BlitPipelineCache() { - clear(); + reset(); vertexFunction->release(); fragmentFunction->release(); } @@ -55,7 +55,7 @@ public: return pipeline; } - void clear() { + void reset() { for (auto& pair : pipelineCache) { pair.second->release(); } diff --git a/include/renderer_mtl/mtl_depth_stencil_cache.hpp b/include/renderer_mtl/mtl_depth_stencil_cache.hpp index 887185c9..5f6ea37f 100644 --- a/include/renderer_mtl/mtl_depth_stencil_cache.hpp +++ b/include/renderer_mtl/mtl_depth_stencil_cache.hpp @@ -18,7 +18,7 @@ public: DepthStencilCache() = default; ~DepthStencilCache() { - clear(); + reset(); } void set(MTL::Device* dev) { @@ -70,7 +70,7 @@ public: return depthStencilState; } - void clear() { + void reset() { for (auto& pair : depthStencilCache) { pair.second->release(); } diff --git a/include/renderer_mtl/mtl_draw_pipeline_cache.hpp b/include/renderer_mtl/mtl_draw_pipeline_cache.hpp index 6c6e5a83..a6965e54 100644 --- a/include/renderer_mtl/mtl_draw_pipeline_cache.hpp +++ b/include/renderer_mtl/mtl_draw_pipeline_cache.hpp @@ -32,7 +32,7 @@ public: DrawPipelineCache() = default; ~DrawPipelineCache() { - clear(); + reset(); vertexDescriptor->release(); vertexFunction->release(); } @@ -107,7 +107,7 @@ public: return pipeline; } - void clear() { + void reset() { for (auto& pair : pipelineCache) { pair.second->release(); } diff --git a/include/renderer_mtl/mtl_vertex_buffer_cache.hpp b/include/renderer_mtl/mtl_vertex_buffer_cache.hpp index 0f78d5ac..6e85805d 100644 --- a/include/renderer_mtl/mtl_vertex_buffer_cache.hpp +++ b/include/renderer_mtl/mtl_vertex_buffer_cache.hpp @@ -20,7 +20,7 @@ public: VertexBufferCache() = default; ~VertexBufferCache() { - clear(); + reset(); } void set(MTL::Device* dev) { @@ -57,7 +57,7 @@ public: return BufferHandle{buffer, oldPtr}; } - void clear() { + void reset() { endFrame(); buffer->release(); } diff --git a/include/renderer_mtl/renderer_mtl.hpp b/include/renderer_mtl/renderer_mtl.hpp index 500fa587..809833c7 100644 --- a/include/renderer_mtl/renderer_mtl.hpp +++ b/include/renderer_mtl/renderer_mtl.hpp @@ -42,6 +42,9 @@ class RendererMTL final : public Renderer { MTL::Device* device; MTL::CommandQueue* commandQueue; + // Libraries + MTL::Library* library; + // Caches SurfaceCache colorRenderTargetCache; SurfaceCache depthStencilRenderTargetCache; @@ -105,10 +108,12 @@ class RendererMTL final : public Renderer { void commitCommandBuffer() { if (renderCommandEncoder) { renderCommandEncoder->endEncoding(); + renderCommandEncoder->release(); renderCommandEncoder = nullptr; } if (commandBuffer) { commandBuffer->commit(); + commandBuffer->release(); commandBuffer = nullptr; } } diff --git a/src/core/renderer_mtl/renderer_mtl.cpp b/src/core/renderer_mtl/renderer_mtl.cpp index cd97ce0e..2666c992 100644 --- a/src/core/renderer_mtl/renderer_mtl.cpp +++ b/src/core/renderer_mtl/renderer_mtl.cpp @@ -39,12 +39,13 @@ RendererMTL::RendererMTL(GPU& gpu, const std::array& internalRegs, RendererMTL::~RendererMTL() {} void RendererMTL::reset() { + vertexBufferCache.reset(); + depthStencilCache.reset(); + drawPipelineCache.reset(); + blitPipelineCache.reset(); + textureCache.reset(); + depthStencilRenderTargetCache.reset(); colorRenderTargetCache.reset(); - depthStencilRenderTargetCache.reset(); - textureCache.reset(); - - // TODO: implement - Helpers::warn("RendererMTL::reset not implemented"); } void RendererMTL::display() { @@ -85,6 +86,7 @@ void RendererMTL::display() { nextRenderPassName = "Display"; beginRenderPassIfNeeded(renderPassDescriptor, false, drawable->texture()); + renderPassDescriptor->release(); renderCommandEncoder->setRenderPipelineState(displayPipeline); renderCommandEncoder->setFragmentSamplerState(nearestSampler, 0); @@ -112,6 +114,9 @@ void RendererMTL::display() { // Inform the vertex buffer cache that the frame ended vertexBufferCache.endFrame(); + + // Release + drawable->release(); } void RendererMTL::initGraphicsContext(SDL_Window* window) { @@ -153,7 +158,7 @@ void RendererMTL::initGraphicsContext(SDL_Window* window) { // Load shaders auto mtlResources = cmrc::RendererMTL::get_filesystem(); - MTL::Library* library = loadLibrary(device, mtlResources.open("metal_shaders.metallib")); + library = loadLibrary(device, mtlResources.open("metal_shaders.metallib")); MTL::Library* copyToLutTextureLibrary = loadLibrary(device, mtlResources.open("metal_copy_to_lut_texture.metallib")); // Display @@ -172,6 +177,9 @@ void RendererMTL::initGraphicsContext(SDL_Window* window) { if (error) { Helpers::panic("Error creating display pipeline state: %s", error->description()->cString(NS::ASCIIStringEncoding)); } + displayPipelineDescriptor->release(); + vertexDisplayFunction->release(); + fragmentDisplayFunction->release(); // Blit MTL::Function* vertexBlitFunction = library->newFunction(NS::String::string("vertexBlit", NS::ASCIIStringEncoding)); @@ -262,6 +270,8 @@ void RendererMTL::initGraphicsContext(SDL_Window* window) { if (error) { Helpers::panic("Error creating copy_to_lut_texture pipeline state: %s", error->description()->cString(NS::ASCIIStringEncoding)); } + copyToLutTexturePipelineDescriptor->release(); + vertexCopyToLutTextureFunction->release(); // Depth stencil cache depthStencilCache.set(device); @@ -273,6 +283,10 @@ void RendererMTL::initGraphicsContext(SDL_Window* window) { MTL::DepthStencilDescriptor* depthStencilDescriptor = MTL::DepthStencilDescriptor::alloc()->init(); depthStencilDescriptor->setLabel(toNSString("Default depth stencil state")); defaultDepthStencilState = device->newDepthStencilState(depthStencilDescriptor); + depthStencilDescriptor->release(); + + // Release + copyToLutTextureLibrary->release(); } void RendererMTL::clearBuffer(u32 startAddress, u32 endAddress, u32 value, u32 control) { @@ -573,12 +587,18 @@ void RendererMTL::screenshot(const std::string& name) { } void RendererMTL::deinitGraphicsContext() { - colorRenderTargetCache.reset(); - depthStencilRenderTargetCache.reset(); - textureCache.reset(); + reset(); - // TODO: implement - Helpers::warn("RendererMTL::deinitGraphicsContext not implemented"); + // Release + copyToLutTexturePipeline->release(); + displayPipeline->release(); + defaultDepthStencilState->release(); + lightLUTTextureArray->release(); + linearSampler->release(); + nearestSampler->release(); + library->release(); + commandQueue->release(); + device->release(); } std::optional RendererMTL::getColorRenderTarget(