From 9296a44f5ecfbf3a7ec3f5b6a39e208aad87dce6 Mon Sep 17 00:00:00 2001 From: junjie020 Date: Wed, 26 Dec 2018 14:19:07 +0800 Subject: [PATCH] fix metal shader lack of texutre uniform variable --- tools/shaderc/shaderc_glsl.cpp | 54 ++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/tools/shaderc/shaderc_glsl.cpp b/tools/shaderc/shaderc_glsl.cpp index 2cb3d2372..c07a4afbb 100644 --- a/tools/shaderc/shaderc_glsl.cpp +++ b/tools/shaderc/shaderc_glsl.cpp @@ -258,6 +258,60 @@ namespace bgfx { namespace glsl parse = eol.getPtr() + 1; } } + + bx::StringView mainEntry("xlatMtlShaderOutput xlatMtlMain ("); + parse = bx::strFind(optimizedShader, mainEntry); + end = parse; + if (!parse.isEmpty()) + { + parse.set(parse.getPtr() + mainEntry.getLength(), optShader.getTerm()); + end = bx::strFind(parse, "{"); + } + + while (parse.getPtr() < end.getPtr() + && !parse.isEmpty()) + { + parse.set(bx::strLTrimSpace(parse).getPtr(), optShader.getTerm()); + const bx::StringView textureNameMark("[[texture("); + const bx::StringView textureName = bx::strFind(parse, textureNameMark); + + if (!textureName.isEmpty()) + { + Uniform un; + un.type = nameToUniformTypeEnum("int"); // int for sampler + const char* varNameEnd = textureName.getPtr() - 1; + parse.set(parse.getPtr(), varNameEnd - 1); + const char* varNameBeg = parse.getPtr(); + for (int ii = parse.getLength() - 1; 0 <= ii; --ii) + { + if (varNameBeg[ii] == ' ') + { + parse.set(varNameBeg + ii + 1, varNameEnd); + break; + } + } + char uniformName[256]; + bx::strCopy(uniformName, parse.getLength() + 1, parse); + un.name = uniformName; + const char* regIndexBeg = textureName.getPtr() + textureNameMark.getLength(); + bx::StringView regIndex = bx::strFind(regIndexBeg, ")"); + + regIndex.set(regIndexBeg, regIndex.getPtr()); + uint32_t tmp; + bx::fromString(&tmp, regIndex); + un.regIndex = uint16_t(tmp); + un.num = 1; + un.regCount = 1; + + uniforms.push_back(un); + + parse = regIndex.getPtr() + 1; + } + else + { + parse = textureName; + } + } } uint16_t count = (uint16_t)uniforms.size();