From 759937a458c4c32d5bf370582532c05cf0c34248 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Fri, 24 Dec 2021 22:20:25 -0400 Subject: [PATCH] metal: fix depth24stencil8 fallback on macOS --- src/modules/graphics/metal/Graphics.mm | 16 +++++++++------- src/modules/graphics/metal/Metal.h | 2 +- src/modules/graphics/metal/Metal.mm | 7 +++++-- src/modules/graphics/metal/Shader.mm | 4 ++-- src/modules/graphics/metal/Texture.mm | 2 +- 5 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/modules/graphics/metal/Graphics.mm b/src/modules/graphics/metal/Graphics.mm index 155918d77..78efeeaf8 100644 --- a/src/modules/graphics/metal/Graphics.mm +++ b/src/modules/graphics/metal/Graphics.mm @@ -837,7 +837,6 @@ void Graphics::applyRenderState(id encoder, const Verte { lastVertexAttributes = attributes; -// Shader *shader = (Shader *) state.shader.get(); Shader *shader = (Shader *) Shader::current; id pipeline = nil; @@ -849,18 +848,21 @@ void Graphics::applyRenderState(id encoder, const Verte key.blend = state.blend; key.colorChannelMask = state.colorMask; - const auto &rts = state.renderTargets.colors; - - for (size_t i = 0; i < rts.size(); i++) - key.colorRenderTargetFormats |= (rts[i].texture->getPixelFormat()) << (8 * i); - if (state.renderTargets.getFirstTarget().texture.get() == nullptr) { key.colorRenderTargetFormats = isGammaCorrect() ? PIXELFORMAT_BGRA8_UNORM_sRGB : PIXELFORMAT_BGRA8_UNORM; key.depthStencilFormat = backbufferDepthStencil->getPixelFormat(); } + else + { + const auto &rts = state.renderTargets.colors; + for (size_t i = 0; i < rts.size(); i++) + key.colorRenderTargetFormats |= (rts[i].texture->getPixelFormat()) << (8 * i); - // TODO: depth/stencil + // TODO: automatic depth/stencil (state doesn't store it). + if (state.renderTargets.depthStencil.texture.get()) + key.depthStencilFormat = state.renderTargets.depthStencil.texture->getPixelFormat(); + } pipeline = shader->getCachedRenderPipeline(key); } diff --git a/src/modules/graphics/metal/Metal.h b/src/modules/graphics/metal/Metal.h index 81d763c51..dba4c8233 100644 --- a/src/modules/graphics/metal/Metal.h +++ b/src/modules/graphics/metal/Metal.h @@ -44,7 +44,7 @@ public: }; API_AVAILABLE(macos(10.15), ios(13.0)) - static PixelFormatDesc convertPixelFormat(PixelFormat format, bool &isSRGB); + static PixelFormatDesc convertPixelFormat(id device, PixelFormat format, bool &isSRGB); }; // Metal diff --git a/src/modules/graphics/metal/Metal.mm b/src/modules/graphics/metal/Metal.mm index 471ec0fb9..7fdc125b4 100644 --- a/src/modules/graphics/metal/Metal.mm +++ b/src/modules/graphics/metal/Metal.mm @@ -28,7 +28,7 @@ namespace graphics namespace metal { -Metal::PixelFormatDesc Metal::convertPixelFormat(PixelFormat format, bool &isSRGB) +Metal::PixelFormatDesc Metal::convertPixelFormat(id device, PixelFormat format, bool &isSRGB) { MTLPixelFormat mtlformat = MTLPixelFormatInvalid; PixelFormatDesc desc = {}; @@ -192,7 +192,10 @@ Metal::PixelFormatDesc Metal::convertPixelFormat(PixelFormat format, bool &isSRG #ifdef LOVE_IOS mtlformat = MTLPixelFormatDepth32Float_Stencil8; #else - mtlformat = MTLPixelFormatDepth24Unorm_Stencil8; + if ([device isDepth24Stencil8PixelFormatSupported]) + mtlformat = MTLPixelFormatDepth24Unorm_Stencil8; + else + mtlformat = MTLPixelFormatDepth32Float_Stencil8; #endif break; case PIXELFORMAT_DEPTH32_FLOAT_STENCIL8: diff --git a/src/modules/graphics/metal/Shader.mm b/src/modules/graphics/metal/Shader.mm index deb2af50d..f97f74ec9 100644 --- a/src/modules/graphics/metal/Shader.mm +++ b/src/modules/graphics/metal/Shader.mm @@ -931,7 +931,7 @@ id Shader::getCachedRenderPipeline(const RenderPipelineK // We already don't really support metal on older systems, this just // silences a compiler warning about it. bool isSRGB = false; - auto formatdesc = Metal::convertPixelFormat(format, isSRGB); + auto formatdesc = Metal::convertPixelFormat(device, format, isSRGB); attachment.pixelFormat = formatdesc.format; } @@ -969,7 +969,7 @@ id Shader::getCachedRenderPipeline(const RenderPipelineK // We already don't really support metal on older systems, this just // silences a compiler warning about it. bool isSRGB = false; - auto formatdesc = Metal::convertPixelFormat(dsformat, isSRGB); + auto formatdesc = Metal::convertPixelFormat(device, dsformat, isSRGB); if (isPixelFormatDepth(dsformat)) desc.depthAttachmentPixelFormat = formatdesc.format; if (isPixelFormatStencil(dsformat)) diff --git a/src/modules/graphics/metal/Texture.mm b/src/modules/graphics/metal/Texture.mm index ba2148c67..1bc681fa4 100644 --- a/src/modules/graphics/metal/Texture.mm +++ b/src/modules/graphics/metal/Texture.mm @@ -62,7 +62,7 @@ Texture::Texture(love::graphics::Graphics *gfx, id device, const Sett { // We already don't really support metal on older systems, this just // silences a compiler warning about it. - auto formatdesc = Metal::convertPixelFormat(format, sRGB); + auto formatdesc = Metal::convertPixelFormat(device, format, sRGB); desc.pixelFormat = formatdesc.format; if (formatdesc.swizzled) desc.swizzle = formatdesc.swizzle;