From 061256a3b3ac5e389c84264d105f1658b2d9f919 Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Wed, 7 Oct 2020 14:45:24 -0400 Subject: [PATCH] metal: Clear the framebuffer if presenting without any other operations. --- src/render/metal/SDL_render_metal.m | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/render/metal/SDL_render_metal.m b/src/render/metal/SDL_render_metal.m index 04616e374..cb3f9cc6a 100644 --- a/src/render/metal/SDL_render_metal.m +++ b/src/render/metal/SDL_render_metal.m @@ -1518,17 +1518,21 @@ METAL_RenderPresent(SDL_Renderer * renderer) { @autoreleasepool { METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata; - METAL_ActivateRenderCommandEncoder(renderer, MTLLoadActionLoad, NULL, nil); + // If we don't have a command buffer, we can't present, so activate to get one. + if (data.mtlcmdencoder == nil) { + // We haven't even gotten a backbuffer yet? Clear it to black. Otherwise, load the existing data. + if (data.mtlbackbuffer == nil) { + MTLClearColor color = MTLClearColorMake(0.0f, 0.0f, 0.0f, 1.0f); + METAL_ActivateRenderCommandEncoder(renderer, MTLLoadActionClear, &color, nil); + } else { + METAL_ActivateRenderCommandEncoder(renderer, MTLLoadActionLoad, NULL, nil); + } + } + + [data.mtlcmdencoder endEncoding]; + [data.mtlcmdbuffer presentDrawable:data.mtlbackbuffer]; + [data.mtlcmdbuffer commit]; - if (data.mtlcmdencoder != nil) { - [data.mtlcmdencoder endEncoding]; - } - if (data.mtlbackbuffer != nil) { - [data.mtlcmdbuffer presentDrawable:data.mtlbackbuffer]; - } - if (data.mtlcmdbuffer != nil) { - [data.mtlcmdbuffer commit]; - } data.mtlcmdencoder = nil; data.mtlcmdbuffer = nil; data.mtlbackbuffer = nil;