From ec3241a86901c0c986cbc23e11e6f696d844a072 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Fri, 13 Apr 2018 17:12:43 -0700 Subject: [PATCH] shaderc: Remove unused uniforms from SPIRV shader. --- tools/shaderc/shaderc_spirv.cpp | 50 +++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/tools/shaderc/shaderc_spirv.cpp b/tools/shaderc/shaderc_spirv.cpp index a3fcc3c84..0bbe4eea5 100644 --- a/tools/shaderc/shaderc_spirv.cpp +++ b/tools/shaderc/shaderc_spirv.cpp @@ -533,7 +533,7 @@ namespace bgfx { namespace spirv // fprintf(stderr, "%s\n", _message); // } - static bool compile(const Options& _options, uint32_t _version, const std::string& _code, bx::WriterI* _writer) + static bool compile(const Options& _options, uint32_t _version, const std::string& _code, bx::WriterI* _writer, bool _firstPass) { BX_UNUSED(_version); @@ -627,6 +627,52 @@ namespace bgfx { namespace spirv uint16_t size = 0; program->buildReflection(); + + if (_firstPass) + { + const size_t strLength = bx::strLen("uniform"); + + // first time through, we just find unused uniforms and get rid of them + std::string output; + bx::Error err; + LineReader reader(_code.c_str() ); + while (err.isOk() ) + { + char str[4096]; + int32_t len = bx::read(&reader, str, BX_COUNTOF(str), &err); + if (err.isOk() ) + { + std::string strLine(str, len); + + for (int32_t ii = 0, num = program->getNumLiveUniformVariables(); ii < num; ++ii) + { + size_t index = strLine.find("uniform "); + if (index == std::string::npos) + { + continue; + } + + // matching lines like: uniform u_name; + // we want to replace "uniform" with "static" so that it's no longer + // included in the uniform blob that the application must upload + // we can't just remove them, because unused functions might still reference + // them and cause a compile error when they're gone + if (!!bx::findIdentifierMatch(strLine.c_str(), program->getUniformName(ii) ) ) + { + break; + } + + strLine = strLine.replace(index, strLength, "static"); + } + + output += strLine; + } + } + + // recompile with the unused uniforms converted to statics + return compile(_options, _version, output.c_str(), _writer, false); + } + { uint16_t count = (uint16_t)program->getNumLiveUniformVariables(); bx::write(_writer, count); @@ -723,7 +769,7 @@ namespace bgfx { namespace spirv bool compileSPIRVShader(const Options& _options, uint32_t _version, const std::string& _code, bx::WriterI* _writer) { - return spirv::compile(_options, _version, _code, _writer); + return spirv::compile(_options, _version, _code, _writer, true); } } // namespace bgfx