From 4c3c875aa93fe1638f47c959c4ae169bc4f7ba71 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Sat, 22 Jan 2022 21:29:57 -0400 Subject: [PATCH] metal: clean up some code around render pipeline states. --- src/modules/graphics/Graphics.cpp | 3 -- src/modules/graphics/metal/Graphics.h | 8 ++-- src/modules/graphics/metal/Graphics.mm | 52 +++++++++++--------------- 3 files changed, 26 insertions(+), 37 deletions(-) diff --git a/src/modules/graphics/Graphics.cpp b/src/modules/graphics/Graphics.cpp index 0291f7de7..ee43b8275 100644 --- a/src/modules/graphics/Graphics.cpp +++ b/src/modules/graphics/Graphics.cpp @@ -864,9 +864,6 @@ void Graphics::setRenderTargets(const RenderTargets &rts) throw love::Exception("Invalid slice index: %d.", slice + 1); } - int w = firsttex->getWidth(firsttarget.mipmap); - int h = firsttex->getHeight(firsttarget.mipmap); - flushBatchedDraws(); if (rts.depthStencil.texture == nullptr && rts.temporaryRTFlags != 0) diff --git a/src/modules/graphics/metal/Graphics.h b/src/modules/graphics/metal/Graphics.h index ea1c2d2f9..5f290e47d 100644 --- a/src/modules/graphics/metal/Graphics.h +++ b/src/modules/graphics/metal/Graphics.h @@ -22,6 +22,7 @@ #include "graphics/Graphics.h" #include "Metal.h" +#include "Shader.h" #include @@ -209,8 +210,8 @@ private: id getCachedDepthStencilState(const DepthState &depth, const StencilState &stencil); void applyRenderState(id renderEncoder, const VertexAttributes &attributes); - void applyShaderUniforms(id encoder, Shader *shader); - void applyShaderUniforms(id renderEncoder, Shader *shader, Texture *maintex); + void applyShaderUniforms(id encoder, love::graphics::Shader *shader); + void applyShaderUniforms(id renderEncoder, love::graphics::Shader *shader, Texture *maintex); id commandQueue; @@ -225,8 +226,7 @@ private: uint32 dirtyRenderState; CullMode lastCullMode; - VertexAttributes lastVertexAttributes; - PixelFormat activeDepthStencilFormat; + Shader::RenderPipelineKey lastRenderPipelineKey; bool windowHasStencil; int shaderSwitches; diff --git a/src/modules/graphics/metal/Graphics.mm b/src/modules/graphics/metal/Graphics.mm index 3d260b9a3..232ef1ebd 100644 --- a/src/modules/graphics/metal/Graphics.mm +++ b/src/modules/graphics/metal/Graphics.mm @@ -270,7 +270,7 @@ Graphics::Graphics() , passDesc(nil) , dirtyRenderState(STATEBIT_ALL) , lastCullMode(CULL_MAX_ENUM) - , activeDepthStencilFormat(PIXELFORMAT_UNKNOWN) + , lastRenderPipelineKey() , windowHasStencil(false) , shaderSwitches(0) , requestedBackbufferMSAA(0) @@ -626,6 +626,11 @@ id Graphics::useRenderEncoder() setAttachment(rt, passDesc.stencilAttachment, attachmentStoreActions.stencil, false); attachmentStoreActions.depth = MTLStoreActionDontCare; attachmentStoreActions.stencil = MTLStoreActionDontCare; + + auto &key = lastRenderPipelineKey; + key.colorRenderTargetFormats = isGammaCorrect() ? PIXELFORMAT_BGRA8_UNORM_sRGB : PIXELFORMAT_BGRA8_UNORM; + key.depthStencilFormat = backbufferDepthStencil->getPixelFormat(); + key.msaa = backbufferMSAA ? (uint8) backbufferMSAA->getMSAA() : 1; } renderEncoder = [useCommandBuffer() renderCommandEncoderWithDescriptor:passDesc]; @@ -891,43 +896,20 @@ void Graphics::applyRenderState(id encoder, const Verte [encoder setCullMode:mode]; } - if ((dirtyState & pipelineStateBits) != 0 || !(attributes == lastVertexAttributes)) + if ((dirtyState & pipelineStateBits) != 0 || !(attributes == lastRenderPipelineKey.vertexAttributes)) { - lastVertexAttributes = attributes; + auto &key = lastRenderPipelineKey; + + key.vertexAttributes = attributes; Shader *shader = (Shader *) Shader::current; id pipeline = nil; if (shader) { - Shader::RenderPipelineKey key; - - key.vertexAttributes = attributes; key.blend = state.blend; key.colorChannelMask = state.colorMask; - const auto &firsttarget = state.renderTargets.getFirstTarget(); - - if (firsttarget.texture.get() == nullptr) - { - key.colorRenderTargetFormats = isGammaCorrect() ? PIXELFORMAT_BGRA8_UNORM_sRGB : PIXELFORMAT_BGRA8_UNORM; - key.depthStencilFormat = backbufferDepthStencil->getPixelFormat(); - key.msaa = backbufferMSAA ? (uint8) backbufferMSAA->getMSAA() : 1; - } - else - { - const auto &rts = state.renderTargets.colors; - - for (size_t i = 0; i < rts.size(); i++) - key.colorRenderTargetFormats |= (rts[i].texture->getPixelFormat()) << (8 * i); - - // Don't query the current RTs because they don't include - // automatic depth/stencil. - key.depthStencilFormat = activeDepthStencilFormat; - - key.msaa = (uint8) firsttarget.texture->getMSAA(); - } - pipeline = shader->getCachedRenderPipeline(key); } @@ -1361,9 +1343,19 @@ void Graphics::setRenderTargetsInternal(const RenderTargets &rts, int /*pixelw*/ setAttachment(rt, passDesc.stencilAttachment, attachmentStoreActions.stencil); } - activeDepthStencilFormat = dsformat; + if (!isbackbuffer) + { + lastRenderPipelineKey.colorRenderTargetFormats = 0; + for (size_t i = 0; i < rts.colors.size(); i++) + lastRenderPipelineKey.colorRenderTargetFormats |= (rts.colors[i].texture->getPixelFormat()) << (8 * i); + + lastRenderPipelineKey.msaa = (uint8) rts.getFirstTarget().texture->getMSAA(); + } + + lastRenderPipelineKey.depthStencilFormat = dsformat; + lastRenderPipelineKey.vertexAttributes = VertexAttributes(); + dirtyRenderState = STATEBIT_ALL; - lastVertexAttributes = VertexAttributes(); }} void Graphics::endPass()