From bd7a01aa8a6a001191da4bbfdddb3fdf057912fb Mon Sep 17 00:00:00 2001 From: simon chen Date: Sat, 18 Mar 2023 10:49:42 +0800 Subject: [PATCH] fix unused uniforms replace when multi uniform defined in one line and there is some unused uniforms in that line. (#3056) --- tools/shaderc/shaderc_hlsl.cpp | 63 ++++++++++++++++++++-------------- 1 file changed, 38 insertions(+), 25 deletions(-) diff --git a/tools/shaderc/shaderc_hlsl.cpp b/tools/shaderc/shaderc_hlsl.cpp index 2397304cd..2d43e3514 100644 --- a/tools/shaderc/shaderc_hlsl.cpp +++ b/tools/shaderc/shaderc_hlsl.cpp @@ -704,39 +704,52 @@ namespace bgfx { namespace hlsl while (!reader.isDone() ) { bx::StringView strLine = reader.next(); - bool found = false; - - for (UniformNameList::iterator it = unusedUniforms.begin(), itEnd = unusedUniforms.end(); it != itEnd; ++it) + std::string replaceOutput; + bx::StringView str = strFind(strLine, "uniform "); + if (!str.isEmpty()) { - bx::StringView str = strFind(strLine, "uniform "); - if (str.isEmpty() ) + std::string lineToReplace(strLine.getPtr(), 0, strLine.getLength()); + for (UniformNameList::iterator it = unusedUniforms.begin(); it != unusedUniforms.end();) { - 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, it->c_str() ).isEmpty() ) - { - output.append(strLine.getPtr(), str.getPtr() ); - output += "static "; - output.append(str.getTerm(), strLine.getTerm() ); - output += "\n"; - found = true; - - unusedUniforms.erase(it); - break; + // 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 + auto identifier = bx::findIdentifierMatch(strLine, it->c_str()); + if (!identifier.isEmpty()) + { + bx::StringView definePrefix = bx::StringView(strLine.getPtr(), identifier.getPtr()); + bx::StringView semicolon = strRFind(definePrefix, ';'); + if (!semicolon.isEmpty()) + { + bx::StringView uniformDefine = bx::StringView(semicolon.getPtr(), strLine.getTerm()); + str = strFind(uniformDefine, "uniform "); + } + replaceOutput.clear(); + replaceOutput.append(strLine.getPtr(), str.getPtr()); + replaceOutput += "static "; + replaceOutput.append(str.getTerm(), strLine.getTerm()); + lineToReplace = replaceOutput; + strLine = bx::StringView(lineToReplace.c_str()); + it = unusedUniforms.erase(it); + } + else + { + ++it; + } } } - if (!found) + if (!replaceOutput.empty()) + { + output.append(replaceOutput); + } + else { output.append(strLine.getPtr(), strLine.getTerm() ); - output += "\n"; } + output += "\n"; } // recompile with the unused uniforms converted to statics