release resources

This commit is contained in:
Samuliak 2024-07-07 12:34:04 +02:00
parent 3431f6d192
commit 23af64ade8
6 changed files with 44 additions and 19 deletions

View file

@ -18,7 +18,7 @@ public:
BlitPipelineCache() = default; BlitPipelineCache() = default;
~BlitPipelineCache() { ~BlitPipelineCache() {
clear(); reset();
vertexFunction->release(); vertexFunction->release();
fragmentFunction->release(); fragmentFunction->release();
} }
@ -55,7 +55,7 @@ public:
return pipeline; return pipeline;
} }
void clear() { void reset() {
for (auto& pair : pipelineCache) { for (auto& pair : pipelineCache) {
pair.second->release(); pair.second->release();
} }

View file

@ -18,7 +18,7 @@ public:
DepthStencilCache() = default; DepthStencilCache() = default;
~DepthStencilCache() { ~DepthStencilCache() {
clear(); reset();
} }
void set(MTL::Device* dev) { void set(MTL::Device* dev) {
@ -70,7 +70,7 @@ public:
return depthStencilState; return depthStencilState;
} }
void clear() { void reset() {
for (auto& pair : depthStencilCache) { for (auto& pair : depthStencilCache) {
pair.second->release(); pair.second->release();
} }

View file

@ -32,7 +32,7 @@ public:
DrawPipelineCache() = default; DrawPipelineCache() = default;
~DrawPipelineCache() { ~DrawPipelineCache() {
clear(); reset();
vertexDescriptor->release(); vertexDescriptor->release();
vertexFunction->release(); vertexFunction->release();
} }
@ -107,7 +107,7 @@ public:
return pipeline; return pipeline;
} }
void clear() { void reset() {
for (auto& pair : pipelineCache) { for (auto& pair : pipelineCache) {
pair.second->release(); pair.second->release();
} }

View file

@ -20,7 +20,7 @@ public:
VertexBufferCache() = default; VertexBufferCache() = default;
~VertexBufferCache() { ~VertexBufferCache() {
clear(); reset();
} }
void set(MTL::Device* dev) { void set(MTL::Device* dev) {
@ -57,7 +57,7 @@ public:
return BufferHandle{buffer, oldPtr}; return BufferHandle{buffer, oldPtr};
} }
void clear() { void reset() {
endFrame(); endFrame();
buffer->release(); buffer->release();
} }

View file

@ -42,6 +42,9 @@ class RendererMTL final : public Renderer {
MTL::Device* device; MTL::Device* device;
MTL::CommandQueue* commandQueue; MTL::CommandQueue* commandQueue;
// Libraries
MTL::Library* library;
// Caches // Caches
SurfaceCache<Metal::ColorRenderTarget, 16, true> colorRenderTargetCache; SurfaceCache<Metal::ColorRenderTarget, 16, true> colorRenderTargetCache;
SurfaceCache<Metal::DepthStencilRenderTarget, 16, true> depthStencilRenderTargetCache; SurfaceCache<Metal::DepthStencilRenderTarget, 16, true> depthStencilRenderTargetCache;
@ -105,10 +108,12 @@ class RendererMTL final : public Renderer {
void commitCommandBuffer() { void commitCommandBuffer() {
if (renderCommandEncoder) { if (renderCommandEncoder) {
renderCommandEncoder->endEncoding(); renderCommandEncoder->endEncoding();
renderCommandEncoder->release();
renderCommandEncoder = nullptr; renderCommandEncoder = nullptr;
} }
if (commandBuffer) { if (commandBuffer) {
commandBuffer->commit(); commandBuffer->commit();
commandBuffer->release();
commandBuffer = nullptr; commandBuffer = nullptr;
} }
} }

View file

@ -39,12 +39,13 @@ RendererMTL::RendererMTL(GPU& gpu, const std::array<u32, regNum>& internalRegs,
RendererMTL::~RendererMTL() {} RendererMTL::~RendererMTL() {}
void RendererMTL::reset() { void RendererMTL::reset() {
vertexBufferCache.reset();
depthStencilCache.reset();
drawPipelineCache.reset();
blitPipelineCache.reset();
textureCache.reset();
depthStencilRenderTargetCache.reset();
colorRenderTargetCache.reset(); colorRenderTargetCache.reset();
depthStencilRenderTargetCache.reset();
textureCache.reset();
// TODO: implement
Helpers::warn("RendererMTL::reset not implemented");
} }
void RendererMTL::display() { void RendererMTL::display() {
@ -85,6 +86,7 @@ void RendererMTL::display() {
nextRenderPassName = "Display"; nextRenderPassName = "Display";
beginRenderPassIfNeeded(renderPassDescriptor, false, drawable->texture()); beginRenderPassIfNeeded(renderPassDescriptor, false, drawable->texture());
renderPassDescriptor->release();
renderCommandEncoder->setRenderPipelineState(displayPipeline); renderCommandEncoder->setRenderPipelineState(displayPipeline);
renderCommandEncoder->setFragmentSamplerState(nearestSampler, 0); renderCommandEncoder->setFragmentSamplerState(nearestSampler, 0);
@ -112,6 +114,9 @@ void RendererMTL::display() {
// Inform the vertex buffer cache that the frame ended // Inform the vertex buffer cache that the frame ended
vertexBufferCache.endFrame(); vertexBufferCache.endFrame();
// Release
drawable->release();
} }
void RendererMTL::initGraphicsContext(SDL_Window* window) { void RendererMTL::initGraphicsContext(SDL_Window* window) {
@ -153,7 +158,7 @@ void RendererMTL::initGraphicsContext(SDL_Window* window) {
// Load shaders // Load shaders
auto mtlResources = cmrc::RendererMTL::get_filesystem(); 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")); MTL::Library* copyToLutTextureLibrary = loadLibrary(device, mtlResources.open("metal_copy_to_lut_texture.metallib"));
// Display // Display
@ -172,6 +177,9 @@ void RendererMTL::initGraphicsContext(SDL_Window* window) {
if (error) { if (error) {
Helpers::panic("Error creating display pipeline state: %s", error->description()->cString(NS::ASCIIStringEncoding)); Helpers::panic("Error creating display pipeline state: %s", error->description()->cString(NS::ASCIIStringEncoding));
} }
displayPipelineDescriptor->release();
vertexDisplayFunction->release();
fragmentDisplayFunction->release();
// Blit // Blit
MTL::Function* vertexBlitFunction = library->newFunction(NS::String::string("vertexBlit", NS::ASCIIStringEncoding)); MTL::Function* vertexBlitFunction = library->newFunction(NS::String::string("vertexBlit", NS::ASCIIStringEncoding));
@ -262,6 +270,8 @@ void RendererMTL::initGraphicsContext(SDL_Window* window) {
if (error) { if (error) {
Helpers::panic("Error creating copy_to_lut_texture pipeline state: %s", error->description()->cString(NS::ASCIIStringEncoding)); Helpers::panic("Error creating copy_to_lut_texture pipeline state: %s", error->description()->cString(NS::ASCIIStringEncoding));
} }
copyToLutTexturePipelineDescriptor->release();
vertexCopyToLutTextureFunction->release();
// Depth stencil cache // Depth stencil cache
depthStencilCache.set(device); depthStencilCache.set(device);
@ -273,6 +283,10 @@ void RendererMTL::initGraphicsContext(SDL_Window* window) {
MTL::DepthStencilDescriptor* depthStencilDescriptor = MTL::DepthStencilDescriptor::alloc()->init(); MTL::DepthStencilDescriptor* depthStencilDescriptor = MTL::DepthStencilDescriptor::alloc()->init();
depthStencilDescriptor->setLabel(toNSString("Default depth stencil state")); depthStencilDescriptor->setLabel(toNSString("Default depth stencil state"));
defaultDepthStencilState = device->newDepthStencilState(depthStencilDescriptor); defaultDepthStencilState = device->newDepthStencilState(depthStencilDescriptor);
depthStencilDescriptor->release();
// Release
copyToLutTextureLibrary->release();
} }
void RendererMTL::clearBuffer(u32 startAddress, u32 endAddress, u32 value, u32 control) { 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() { void RendererMTL::deinitGraphicsContext() {
colorRenderTargetCache.reset(); reset();
depthStencilRenderTargetCache.reset();
textureCache.reset();
// TODO: implement // Release
Helpers::warn("RendererMTL::deinitGraphicsContext not implemented"); copyToLutTexturePipeline->release();
displayPipeline->release();
defaultDepthStencilState->release();
lightLUTTextureArray->release();
linearSampler->release();
nearestSampler->release();
library->release();
commandQueue->release();
device->release();
} }
std::optional<Metal::ColorRenderTarget> RendererMTL::getColorRenderTarget( std::optional<Metal::ColorRenderTarget> RendererMTL::getColorRenderTarget(