Skip to content

Commit

Permalink
Mildly improve the leak situation
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacMarovitz committed Jan 1, 2025
1 parent bfc303b commit 4fec795
Show file tree
Hide file tree
Showing 8 changed files with 9 additions and 62 deletions.
2 changes: 2 additions & 0 deletions core/rend/metal/metal_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions core/rend/metal/metal_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -69,6 +70,7 @@ void MetalContext::Present() {
void MetalContext::term() {
GraphicsContext::instance = nullptr;
imguiDriver.reset();
pool->release();
}

MetalContext::MetalContext() {
Expand Down
1 change: 1 addition & 0 deletions core/rend/metal/metal_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 1 addition & 3 deletions core/rend/metal/metal_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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();
Expand Down
19 changes: 0 additions & 19 deletions core/rend/metal/metal_pipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
30 changes: 3 additions & 27 deletions core/rend/metal/metal_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<FragmentShaderUniforms>();
dithering = config::EmulateFramebuffer && pvrrc.fb_W_CTRL.fb_dither && pvrrc.fb_W_CTRL.fb_packmode <= 3;
if (dithering) {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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");

Expand Down
1 change: 0 additions & 1 deletion core/rend/metal/metal_renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,4 @@ class MetalRenderer final : public Renderer
MetalSamplers samplers;
bool frameRendered = false;
bool dithering = false;
s64 frameIndex = -350;
};
12 changes: 0 additions & 12 deletions core/rend/metal/metal_shaders.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down

0 comments on commit 4fec795

Please sign in to comment.