From fbff26d5e5943ce20e4e67b2247100bbfb216ad1 Mon Sep 17 00:00:00 2001 From: Daniele Bartolini Date: Fri, 9 Jan 2026 20:15:32 +0100 Subject: [PATCH] Cleanup. (#3540) --- tools/shaderc/shaderc.cpp | 224 +++++++++++++++++++------------------- 1 file changed, 110 insertions(+), 114 deletions(-) diff --git a/tools/shaderc/shaderc.cpp b/tools/shaderc/shaderc.cpp index 6dcc050a6..cde970656 100644 --- a/tools/shaderc/shaderc.cpp +++ b/tools/shaderc/shaderc.cpp @@ -21,100 +21,6 @@ extern "C" #define BGFX_SHADERC_VERSION_MAJOR 1 #define BGFX_SHADERC_VERSION_MINOR 18 -namespace bx -{ -/// Find substring in string. Only match substrings that appear outside C/C++ style comment blocks. -const char* strFindUncommented(const char* _str, int32_t _strMax, const char* _find, int32_t _findMax) -{ - int32_t i = 0; - bool inBlock = false; // inside C style comment. - bool inLine = false; // inside C++ style comment. - - while (i < _strMax) - { - if (!inBlock && !inLine) - { - // Look for comment blocks. - if (i + 1 < _strMax && '/' == _str[i] && '/' == _str[i + 1]) - { - inLine = true; - i += 2; - continue; - } - - if (i + 1 < _strMax && '/' == _str[i] && '*' == _str[i + 1]) - { - inBlock = true; - i += 2; - continue; - } - - // Outside comment block. - if (i + _findMax > _strMax) - { - return NULL; - } - else - { - if (0 == strCmp(_str + i, _find, _findMax) ) - { - return _str + i; - } - } - - ++i; - } - else if (inBlock) - { - if (i + 1 < _strMax && '*' == _str[i] && '/' == _str[i + 1]) - { - inBlock = false; - i += 2; - } - else - { - ++i; - } - } - else if (inLine) - { - if ('\n' == _str[i]) - { - inLine = false; - } - - ++i; - } - else - { - ++i; - } - } - - return NULL; -} - -bx::StringView strFindUncommented(const bx::StringView &_str, const bx::StringView& _find, int32_t _num = INT32_MAX) -{ - int32_t len = min(_find.getLength(), _num); - - const char* ptr = strFindUncommented( - _str.getPtr() - , _str.getLength() - , _find.getPtr() - , len - ); - - if (NULL == ptr) - { - return bx::StringView(_str.getTerm(), _str.getTerm() ); - } - - return bx::StringView(ptr, len); -} - -} - namespace bgfx { bool g_verbose = false; @@ -805,6 +711,96 @@ namespace bgfx strReplace(_str, "\r", "\n"); } + /// Find substring in string. Only match substrings that appear outside C/C++ style comment blocks. + const char* strFindUncommented(const char* _str, int32_t _strMax, const char* _find, int32_t _findMax) + { + int32_t i = 0; + bool inBlock = false; // inside C style comment. + bool inLine = false; // inside C++ style comment. + + while (i < _strMax) + { + if (!inBlock && !inLine) + { + // Look for comment blocks. + if (i + 1 < _strMax && '/' == _str[i] && '/' == _str[i + 1]) + { + inLine = true; + i += 2; + continue; + } + + if (i + 1 < _strMax && '/' == _str[i] && '*' == _str[i + 1]) + { + inBlock = true; + i += 2; + continue; + } + + // Outside comment block. + if (i + _findMax > _strMax) + { + return NULL; + } + else + { + if (0 == bx::strCmp(_str + i, _find, _findMax) ) + { + return _str + i; + } + } + + ++i; + } + else if (inBlock) + { + if (i + 1 < _strMax && '*' == _str[i] && '/' == _str[i + 1]) + { + inBlock = false; + i += 2; + } + else + { + ++i; + } + } + else if (inLine) + { + if ('\n' == _str[i]) + { + inLine = false; + } + + ++i; + } + else + { + ++i; + } + } + + return NULL; + } + + bx::StringView strFindUncommented(const bx::StringView &_str, const bx::StringView& _find, int32_t _num = INT32_MAX) + { + int32_t len = bx::min(_find.getLength(), _num); + + const char* ptr = strFindUncommented( + _str.getPtr() + , _str.getLength() + , _find.getPtr() + , len + ); + + if (NULL == ptr) + { + return bx::StringView(_str.getTerm(), _str.getTerm() ); + } + + return bx::StringView(ptr, len); + } + void printCode(const char* _code, int32_t _line, int32_t _start, int32_t _end, int32_t _column) { bx::printf("Code:\n---\n"); @@ -1695,7 +1691,7 @@ namespace bgfx } else if ('c' == _options.shaderType) // Compute { - bx::StringView entry = bx::strFindUncommented(input, "void main()"); + bx::StringView entry = strFindUncommented(input, "void main()"); if (entry.isEmpty() ) { bx::write(_messageWriter, &messageErr, "Shader entry point 'void main()' is not found.\n"); @@ -1738,10 +1734,10 @@ namespace bgfx uint32_t arg = 0; - const bool hasLocalInvocationID = !bx::strFindUncommented(input, "gl_LocalInvocationID").isEmpty(); - const bool hasLocalInvocationIndex = !bx::strFindUncommented(input, "gl_LocalInvocationIndex").isEmpty(); - const bool hasGlobalInvocationID = !bx::strFindUncommented(input, "gl_GlobalInvocationID").isEmpty(); - const bool hasWorkGroupID = !bx::strFindUncommented(input, "gl_WorkGroupID").isEmpty(); + const bool hasLocalInvocationID = !strFindUncommented(input, "gl_LocalInvocationID").isEmpty(); + const bool hasLocalInvocationIndex = !strFindUncommented(input, "gl_LocalInvocationIndex").isEmpty(); + const bool hasGlobalInvocationID = !strFindUncommented(input, "gl_GlobalInvocationID").isEmpty(); + const bool hasWorkGroupID = !strFindUncommented(input, "gl_WorkGroupID").isEmpty(); if (hasLocalInvocationID) { @@ -1872,7 +1868,7 @@ namespace bgfx else // Vertex/Fragment { bx::StringView shader(input); - bx::StringView entry = bx::strFindUncommented(shader, "void main()"); + bx::StringView entry = strFindUncommented(shader, "void main()"); if (entry.isEmpty() ) { bx::write(_messageWriter, &messageErr, "Shader entry point 'void main()' is not found.\n"); @@ -1896,14 +1892,14 @@ namespace bgfx if (profile->lang == ShadingLang::ESSL && profile->id >= 300) { - const bool hasFragColor = !bx::strFindUncommented(input, "gl_FragColor").isEmpty(); + const bool hasFragColor = !strFindUncommented(input, "gl_FragColor").isEmpty(); bool hasFragData[8] = {}; uint32_t numFragData = 0; for (uint32_t ii = 0; ii < BX_COUNTOF(hasFragData); ++ii) { char temp[32]; bx::snprintf(temp, BX_COUNTOF(temp), "gl_FragData[%d]", ii); - hasFragData[ii] = !bx::strFindUncommented(input, temp).isEmpty(); + hasFragData[ii] = !strFindUncommented(input, temp).isEmpty(); numFragData += hasFragData[ii]; } if (hasFragColor) @@ -2004,17 +2000,17 @@ namespace bgfx if ('f' == _options.shaderType) { - bx::StringView insert = bx::strFindUncommented(bx::StringView(entry.getPtr(), shader.getTerm() ), "{"); + bx::StringView insert = strFindUncommented(bx::StringView(entry.getPtr(), shader.getTerm() ), "{"); if (!insert.isEmpty() ) { insert = strInsert(const_cast(insert.getPtr()+1), "\nvec4 bgfx_VoidFrag = vec4_splat(0.0);\n"); } - const bool hasFragColor = !bx::strFindUncommented(input, "gl_FragColor").isEmpty(); - const bool hasFragCoord = !bx::strFindUncommented(input, "gl_FragCoord").isEmpty() || profile->id >= 400; - const bool hasFragDepth = !bx::strFindUncommented(input, "gl_FragDepth").isEmpty(); - const bool hasFrontFacing = !bx::strFindUncommented(input, "gl_FrontFacing").isEmpty(); - const bool hasPrimitiveId = !bx::strFindUncommented(input, "gl_PrimitiveID").isEmpty() && BGFX_CAPS_PRIMITIVE_ID; + const bool hasFragColor = !strFindUncommented(input, "gl_FragColor").isEmpty(); + const bool hasFragCoord = !strFindUncommented(input, "gl_FragCoord").isEmpty() || profile->id >= 400; + const bool hasFragDepth = !strFindUncommented(input, "gl_FragDepth").isEmpty(); + const bool hasFrontFacing = !strFindUncommented(input, "gl_FrontFacing").isEmpty(); + const bool hasPrimitiveId = !strFindUncommented(input, "gl_PrimitiveID").isEmpty() && BGFX_CAPS_PRIMITIVE_ID; if (!hasPrimitiveId) { @@ -2027,7 +2023,7 @@ namespace bgfx { char temp[32]; bx::snprintf(temp, BX_COUNTOF(temp), "gl_FragData[%d]", ii); - hasFragData[ii] = !bx::strFindUncommented(input, temp).isEmpty(); + hasFragData[ii] = !strFindUncommented(input, temp).isEmpty(); numFragData += hasFragData[ii]; } @@ -2152,12 +2148,12 @@ namespace bgfx } else if ('v' == _options.shaderType) { - const bool hasVertexId = !bx::strFindUncommented(input, "gl_VertexID").isEmpty(); - const bool hasInstanceId = !bx::strFindUncommented(input, "gl_InstanceID").isEmpty(); - const bool hasViewportId = !bx::strFindUncommented(input, "gl_ViewportIndex").isEmpty(); - const bool hasLayerId = !bx::strFindUncommented(input, "gl_Layer").isEmpty(); + const bool hasVertexId = !strFindUncommented(input, "gl_VertexID").isEmpty(); + const bool hasInstanceId = !strFindUncommented(input, "gl_InstanceID").isEmpty(); + const bool hasViewportId = !strFindUncommented(input, "gl_ViewportIndex").isEmpty(); + const bool hasLayerId = !strFindUncommented(input, "gl_Layer").isEmpty(); - bx::StringView brace = bx::strFindUncommented(bx::StringView(entry.getPtr(), shader.getTerm() ), "{"); + bx::StringView brace = strFindUncommented(bx::StringView(entry.getPtr(), shader.getTerm() ), "{"); if (!brace.isEmpty() ) { bx::StringView block = bx::strFindBlock(bx::StringView(brace.getPtr(), shader.getTerm() ), '{', '}');