From aad0810e11c1cea4c5584c7cd8bbd37dda0ad941 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Mon, 29 Mar 2021 18:28:06 -0300 Subject: [PATCH] metal: initial (bad/slow) setShader tracking --- src/modules/graphics/metal/Graphics.h | 3 +++ src/modules/graphics/metal/Graphics.mm | 11 ++++++++-- src/modules/graphics/metal/Shader.mm | 4 +++- src/modules/graphics/vertex.cpp | 28 ++++++++++++++++++++++++++ src/modules/graphics/vertex.h | 2 ++ 5 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/modules/graphics/metal/Graphics.h b/src/modules/graphics/metal/Graphics.h index ec4e3d956..3883ba94a 100644 --- a/src/modules/graphics/metal/Graphics.h +++ b/src/modules/graphics/metal/Graphics.h @@ -96,6 +96,8 @@ public: bool usesGLSLES() const override; RendererInfo getRendererInfo() const override; + void attachShader(love::graphics::Shader *shader); + id useCommandBuffer(); id getCommandBuffer() const { return commandBuffer; } void submitCommandBuffer(); @@ -194,6 +196,7 @@ private: MTLRenderPassDescriptor *passDesc; uint32 dirtyRenderState; + VertexAttributes lastVertexAttributes; bool windowHasStencil; StreamBuffer *uniformBuffer; diff --git a/src/modules/graphics/metal/Graphics.mm b/src/modules/graphics/metal/Graphics.mm index aa1f11770..e0721a6b1 100644 --- a/src/modules/graphics/metal/Graphics.mm +++ b/src/modules/graphics/metal/Graphics.mm @@ -381,6 +381,11 @@ void Graphics::setActive(bool enable) active = enable; } +void Graphics::attachShader(love::graphics::Shader *shader) +{ + dirtyRenderState |= STATE_SHADER; +} + id Graphics::useCommandBuffer() { if (commandBuffer == nil) @@ -620,9 +625,10 @@ void Graphics::applyRenderState(id encoder, const Verte // TODO } - // TODO: attributes - if ((dirtyState & pipelineStateBits) != 0 || true) + if ((dirtyState & pipelineStateBits) != 0 || !(attributes == lastVertexAttributes)) { + lastVertexAttributes = attributes; + // Shader *shader = (Shader *) state.shader.get(); Shader *shader = (Shader *) Shader::current; id pipeline = nil; @@ -923,6 +929,7 @@ void Graphics::setRenderTargetsInternal(const RenderTargets &rts, int w, int h, projectionMatrix = Matrix4::ortho(0.0, (float) w, (float) h, 0.0, -10.0f, 10.0f); dirtyRenderState = STATEBIT_ALL; + lastVertexAttributes = VertexAttributes(); }} void Graphics::endPass() diff --git a/src/modules/graphics/metal/Shader.mm b/src/modules/graphics/metal/Shader.mm index 1c218e056..fb5fc59c5 100644 --- a/src/modules/graphics/metal/Shader.mm +++ b/src/modules/graphics/metal/Shader.mm @@ -441,7 +441,9 @@ void Shader::attach() { if (current != this) { - Graphics::flushBatchedDrawsGlobal(); + Graphics *gfx = Module::getInstance(Module::M_GRAPHICS); + gfx->flushBatchedDrawsGlobal(); + gfx->attachShader(this); current = this; } } diff --git a/src/modules/graphics/vertex.cpp b/src/modules/graphics/vertex.cpp index 99178ff9e..77ebf0d80 100644 --- a/src/modules/graphics/vertex.cpp +++ b/src/modules/graphics/vertex.cpp @@ -322,6 +322,34 @@ void VertexAttributes::setCommonFormat(CommonFormat format, uint8 bufferindex) } } +bool VertexAttributes::operator == (const VertexAttributes &other) const +{ + if (enableBits != other.enableBits || instanceBits != other.instanceBits) + return false; + + uint32 allbits = enableBits; + uint32 i = 0; + + while (allbits) + { + if (isEnabled(i)) + { + const auto &a = attribs[i]; + const auto &b = other.attribs[i]; + if (a.bufferIndex != b.bufferIndex || a.format != b.format || a.offsetFromVertex != b.offsetFromVertex) + return false; + + if (bufferLayouts[a.bufferIndex].stride != other.bufferLayouts[a.bufferIndex].stride) + return false; + } + + i++; + allbits >>= 1; + } + + return true; +} + STRINGMAP_BEGIN(BuiltinVertexAttribute, ATTRIB_MAX_ENUM, attribName) { { "VertexPosition", ATTRIB_POS }, diff --git a/src/modules/graphics/vertex.h b/src/modules/graphics/vertex.h index 34d30bbe1..abea55d30 100644 --- a/src/modules/graphics/vertex.h +++ b/src/modules/graphics/vertex.h @@ -371,6 +371,8 @@ struct VertexAttributes } void setCommonFormat(CommonFormat format, uint8 bufferindex); + + bool operator == (const VertexAttributes &other) const; }; size_t getFormatStride(CommonFormat format);