From 67c153f589b80f2024056b648e9e0ac37044c6c4 Mon Sep 17 00:00:00 2001 From: halx99 Date: Fri, 14 Feb 2020 21:40:33 +0800 Subject: [PATCH] Make multi-meta-textures support works on Metal. --- .../backend/metal/CommandBufferMTL.mm | 38 ++++++++++--------- cocos/renderer/backend/metal/TextureMTL.h | 2 +- .../backend/opengl/CommandBufferGL.cpp | 3 +- 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/cocos/renderer/backend/metal/CommandBufferMTL.mm b/cocos/renderer/backend/metal/CommandBufferMTL.mm index e43760b8b4..775edcbecc 100644 --- a/cocos/renderer/backend/metal/CommandBufferMTL.mm +++ b/cocos/renderer/backend/metal/CommandBufferMTL.mm @@ -178,12 +178,12 @@ namespace return mtlDescritpor; } - id getMTLTexture(TextureBackend* texture) + id getMTLTexture(TextureBackend* texture, int index = 0) { switch (texture->getTextureType()) { case TextureType::TEXTURE_2D: - return static_cast(texture)->getMTLTexture(); + return static_cast(texture)->getMTLTexture(index); case TextureType::TEXTURE_CUBE: return static_cast(texture)->getMTLTexture(); default: @@ -422,27 +422,29 @@ void CommandBufferMTL::doSetTextures(bool isVertex) const for(const auto& iter : bindTextureInfos) { - //FIXME: should support texture array. - int i = 0; auto location = iter.first; const auto& textures = iter.second.textures; + const auto& slot = iter.second.slot; - if (isVertex) + int i = 0; + for (const auto& texture: textures) { - [_mtlRenderEncoder setVertexTexture:getMTLTexture(textures[i]) - atIndex:location]; - [_mtlRenderEncoder setVertexSamplerState:getMTLSamplerState(textures[i]) - atIndex:location]; + int metaIdx = slot[i++]; + if (isVertex) + { + [_mtlRenderEncoder setVertexTexture:getMTLTexture(texture, metaIdx) + atIndex:location]; + [_mtlRenderEncoder setVertexSamplerState:getMTLSamplerState(texture) + atIndex:location]; + } + else + { + [_mtlRenderEncoder setFragmentTexture:getMTLTexture(texture, metaIdx) + atIndex:location]; + [_mtlRenderEncoder setFragmentSamplerState:getMTLSamplerState(texture) + atIndex:location]; + } } - else - { - [_mtlRenderEncoder setFragmentTexture:getMTLTexture(textures[i]) - atIndex:location]; - [_mtlRenderEncoder setFragmentSamplerState:getMTLSamplerState(textures[i]) - atIndex:location]; - } - - ++i; } } diff --git a/cocos/renderer/backend/metal/TextureMTL.h b/cocos/renderer/backend/metal/TextureMTL.h index cd91a5d5db..0aa030ffad 100644 --- a/cocos/renderer/backend/metal/TextureMTL.h +++ b/cocos/renderer/backend/metal/TextureMTL.h @@ -124,7 +124,7 @@ public: * Get MTLTexture object. * @return A MTLTexture object. */ - inline id getMTLTexture() const { return _mtlTextures[0]; } + inline id getMTLTexture(int index = 0) const { return _mtlTextures[index]; } /** * Get MTLSamplerState object diff --git a/cocos/renderer/backend/opengl/CommandBufferGL.cpp b/cocos/renderer/backend/opengl/CommandBufferGL.cpp index e7ecfe05f9..02d94578f0 100644 --- a/cocos/renderer/backend/opengl/CommandBufferGL.cpp +++ b/cocos/renderer/backend/opengl/CommandBufferGL.cpp @@ -487,8 +487,7 @@ void CommandBufferGL::setUniforms(ProgramGL* program) const int i = 0; for (const auto& texture: textures) { - applyTexture(texture, slot[i]); - ++i; + applyTexture(texture, slot[i++]); } auto arrayCount = slot.size();