From d73712b8f6c9047b09e99614e20d456d5ada2390 Mon Sep 17 00:00:00 2001 From: Sharo Date: Fri, 8 Mar 2024 00:02:45 +0000 Subject: [PATCH] Add flags for outputting absolute paths for messages (#3467) Uses std::filesystem to create absolute paths. Also adds "shaderFileName" to TSinkBase so it can be used during message outputs. --- StandAlone/StandAlone.cpp | 9 ++++- glslang/CInterface/glslang_c_interface.cpp | 1 + glslang/Include/InfoSink.h | 24 ++++++++++-- glslang/Include/glslang_c_shader_types.h | 33 ++++++++-------- .../MachineIndependent/ParseContextBase.cpp | 2 +- glslang/MachineIndependent/ShaderLang.cpp | 6 ++- glslang/Public/ShaderLang.h | 38 ++++++++++--------- 7 files changed, 73 insertions(+), 40 deletions(-) diff --git a/StandAlone/StandAlone.cpp b/StandAlone/StandAlone.cpp index ed3deccd0..9cccdf6fb 100644 --- a/StandAlone/StandAlone.cpp +++ b/StandAlone/StandAlone.cpp @@ -182,6 +182,7 @@ bool HlslEnable16BitTypes = false; bool HlslDX9compatible = false; bool HlslDxPositionW = false; bool EnhancedMsgs = false; +bool AbsolutePath = false; bool DumpBuiltinSymbols = false; std::vector IncludeDirectoryList; @@ -727,6 +728,8 @@ void ProcessArguments(std::vector>& workItem HlslDxPositionW = true; } else if (lowerword == "enhanced-msgs") { EnhancedMsgs = true; + } else if (lowerword == "absolute-path") { + AbsolutePath = true; } else if (lowerword == "auto-sampled-textures") { autoSampledTextures = true; } else if (lowerword == "invert-y" || // synonyms @@ -1159,6 +1162,8 @@ void SetMessageOptions(EShMessages& messages) messages = (EShMessages)(messages | EShMsgBuiltinSymbolTable); if (EnhancedMsgs) messages = (EShMessages)(messages | EShMsgEnhanced); + if (AbsolutePath) + messages = (EShMessages)(messages | EShMsgAbsolutePath); } // @@ -1190,6 +1195,7 @@ void CompileShaders(glslang::TWorklist& worklist) if (compiler == nullptr) return; + CompileFile(workItem->name.c_str(), compiler); if (! (Options & EOptionSuppressInfolog)) @@ -1878,7 +1884,7 @@ void CompileFile(const char* fileName, ShHandle compiler) for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j) { // ret = ShCompile(compiler, shaderStrings, NumShaderStrings, lengths, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages); ret = ShCompile(compiler, &shaderString, 1, nullptr, EShOptNone, GetResources(), 0, - (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages); + (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages, fileName); // const char* multi[12] = { "# ve", "rsion", " 300 e", "s", "\n#err", // "or should be l", "ine 1", "string 5\n", "float glo", "bal", // ";\n#error should be line 2\n void main() {", "global = 2.3;}" }; @@ -1993,6 +1999,7 @@ void usage() " without explicit bindings\n" " --auto-map-locations | --aml automatically locate input/output lacking\n" " 'location' (fragile, not cross stage)\n" + " --absolute-path Prints absolute path for messages\n" " --auto-sampled-textures Removes sampler variables and converts\n" " existing textures to sampled textures\n" " --client {vulkan|opengl} see -V and -G\n" diff --git a/glslang/CInterface/glslang_c_interface.cpp b/glslang/CInterface/glslang_c_interface.cpp index 870698f2a..cea965d40 100644 --- a/glslang/CInterface/glslang_c_interface.cpp +++ b/glslang/CInterface/glslang_c_interface.cpp @@ -205,6 +205,7 @@ static int c_shader_messages(glslang_messages_t messages) CONVERT_MSG(GLSLANG_MSG_HLSL_LEGALIZATION_BIT, EShMsgHlslLegalization); CONVERT_MSG(GLSLANG_MSG_HLSL_DX9_COMPATIBLE_BIT, EShMsgHlslDX9Compatible); CONVERT_MSG(GLSLANG_MSG_BUILTIN_SYMBOL_TABLE_BIT, EShMsgBuiltinSymbolTable); + CONVERT_MSG(GLSLANG_MSG_ABSOLUTE_PATH, EShMsgAbsolutePath); return res; #undef CONVERT_MSG } diff --git a/glslang/Include/InfoSink.h b/glslang/Include/InfoSink.h index dceb603cf..23f495dcb 100644 --- a/glslang/Include/InfoSink.h +++ b/glslang/Include/InfoSink.h @@ -36,6 +36,7 @@ #define _INFOSINK_INCLUDED_ #include "../Include/Common.h" +#include #include namespace glslang { @@ -67,7 +68,7 @@ enum TOutputStream { // class TInfoSinkBase { public: - TInfoSinkBase() : outputStream(4) {} + TInfoSinkBase() : outputStream(4), shaderFileName(nullptr) {} void erase() { sink.erase(); } TInfoSinkBase& operator<<(const TPersistString& t) { append(t); return *this; } TInfoSinkBase& operator<<(char c) { append(1, c); return *this; } @@ -94,11 +95,22 @@ public: default: append("UNKNOWN ERROR: "); break; } } - void location(const TSourceLoc& loc) { + void location(const TSourceLoc& loc, bool absolute = false) { const int maxSize = 24; char locText[maxSize]; snprintf(locText, maxSize, ":%d", loc.line); - append(loc.getStringNameOrNum(false).c_str()); + + if(loc.getFilename() == nullptr && shaderFileName != nullptr && absolute) { + append(std::filesystem::absolute(shaderFileName).string()); + } else { + std::string location = loc.getStringNameOrNum(false); + if (absolute) { + append(std::filesystem::absolute(location).string()); + } else { + append(location); + } + } + append(locText); append(": "); } @@ -119,6 +131,11 @@ public: outputStream = output; } + void setShaderFileName(const char* file = nullptr) + { + shaderFileName = file; + } + protected: void append(const char* s); @@ -131,6 +148,7 @@ protected: void appendToStream(const char* s); TPersistString sink; int outputStream; + const char* shaderFileName; }; } // end namespace glslang diff --git a/glslang/Include/glslang_c_shader_types.h b/glslang/Include/glslang_c_shader_types.h index 9bc211496..51f5642ab 100644 --- a/glslang/Include/glslang_c_shader_types.h +++ b/glslang/Include/glslang_c_shader_types.h @@ -157,23 +157,24 @@ typedef enum { /* EShMessages counterpart */ typedef enum { - GLSLANG_MSG_DEFAULT_BIT = 0, - GLSLANG_MSG_RELAXED_ERRORS_BIT = (1 << 0), - GLSLANG_MSG_SUPPRESS_WARNINGS_BIT = (1 << 1), - GLSLANG_MSG_AST_BIT = (1 << 2), - GLSLANG_MSG_SPV_RULES_BIT = (1 << 3), - GLSLANG_MSG_VULKAN_RULES_BIT = (1 << 4), - GLSLANG_MSG_ONLY_PREPROCESSOR_BIT = (1 << 5), - GLSLANG_MSG_READ_HLSL_BIT = (1 << 6), - GLSLANG_MSG_CASCADING_ERRORS_BIT = (1 << 7), - GLSLANG_MSG_KEEP_UNCALLED_BIT = (1 << 8), - GLSLANG_MSG_HLSL_OFFSETS_BIT = (1 << 9), - GLSLANG_MSG_DEBUG_INFO_BIT = (1 << 10), + GLSLANG_MSG_DEFAULT_BIT = 0, + GLSLANG_MSG_RELAXED_ERRORS_BIT = (1 << 0), + GLSLANG_MSG_SUPPRESS_WARNINGS_BIT = (1 << 1), + GLSLANG_MSG_AST_BIT = (1 << 2), + GLSLANG_MSG_SPV_RULES_BIT = (1 << 3), + GLSLANG_MSG_VULKAN_RULES_BIT = (1 << 4), + GLSLANG_MSG_ONLY_PREPROCESSOR_BIT = (1 << 5), + GLSLANG_MSG_READ_HLSL_BIT = (1 << 6), + GLSLANG_MSG_CASCADING_ERRORS_BIT = (1 << 7), + GLSLANG_MSG_KEEP_UNCALLED_BIT = (1 << 8), + GLSLANG_MSG_HLSL_OFFSETS_BIT = (1 << 9), + GLSLANG_MSG_DEBUG_INFO_BIT = (1 << 10), GLSLANG_MSG_HLSL_ENABLE_16BIT_TYPES_BIT = (1 << 11), - GLSLANG_MSG_HLSL_LEGALIZATION_BIT = (1 << 12), - GLSLANG_MSG_HLSL_DX9_COMPATIBLE_BIT = (1 << 13), - GLSLANG_MSG_BUILTIN_SYMBOL_TABLE_BIT = (1 << 14), - GLSLANG_MSG_ENHANCED = (1 << 15), + GLSLANG_MSG_HLSL_LEGALIZATION_BIT = (1 << 12), + GLSLANG_MSG_HLSL_DX9_COMPATIBLE_BIT = (1 << 13), + GLSLANG_MSG_BUILTIN_SYMBOL_TABLE_BIT = (1 << 14), + GLSLANG_MSG_ENHANCED = (1 << 15), + GLSLANG_MSG_ABSOLUTE_PATH = (1 << 16), LAST_ELEMENT_MARKER(GLSLANG_MSG_COUNT), } glslang_messages_t; diff --git a/glslang/MachineIndependent/ParseContextBase.cpp b/glslang/MachineIndependent/ParseContextBase.cpp index 6e3d088a6..591dfc718 100644 --- a/glslang/MachineIndependent/ParseContextBase.cpp +++ b/glslang/MachineIndependent/ParseContextBase.cpp @@ -59,7 +59,7 @@ void TParseContextBase::outputMessage(const TSourceLoc& loc, const char* szReaso safe_vsprintf(szExtraInfo, maxSize, szExtraInfoFormat, args); infoSink.info.prefix(prefix); - infoSink.info.location(loc); + infoSink.info.location(loc, messages & EShMsgAbsolutePath); infoSink.info << "'" << szToken << "' : " << szReason << " " << szExtraInfo << "\n"; if (prefix == EPrefixError) { diff --git a/glslang/MachineIndependent/ShaderLang.cpp b/glslang/MachineIndependent/ShaderLang.cpp index a793a5df7..29d5d5f87 100644 --- a/glslang/MachineIndependent/ShaderLang.cpp +++ b/glslang/MachineIndependent/ShaderLang.cpp @@ -1434,7 +1434,8 @@ int ShCompile( int /*debugOptions*/, int defaultVersion, // use 100 for ES environment, 110 for desktop bool forwardCompatible, // give errors for use of deprecated features - EShMessages messages // warnings/errors/AST; things to print out + EShMessages messages, // warnings/errors/AST; things to print out, + const char *shaderFileName // the filename ) { // Map the generic handle to the C++ object @@ -1450,6 +1451,9 @@ int ShCompile( compiler->infoSink.info.erase(); compiler->infoSink.debug.erase(); + compiler->infoSink.info.setShaderFileName(shaderFileName); + compiler->infoSink.debug.setShaderFileName(shaderFileName); + TIntermediate intermediate(compiler->getLanguage()); TShader::ForbidIncluder includer; diff --git a/glslang/Public/ShaderLang.h b/glslang/Public/ShaderLang.h index e0ec47f8f..046fd917c 100644 --- a/glslang/Public/ShaderLang.h +++ b/glslang/Public/ShaderLang.h @@ -252,23 +252,24 @@ typedef enum { // Message choices for what errors and warnings are given. // enum EShMessages : unsigned { - EShMsgDefault = 0, // default is to give all required errors and extra warnings - EShMsgRelaxedErrors = (1 << 0), // be liberal in accepting input - EShMsgSuppressWarnings = (1 << 1), // suppress all warnings, except those required by the specification - EShMsgAST = (1 << 2), // print the AST intermediate representation - EShMsgSpvRules = (1 << 3), // issue messages for SPIR-V generation - EShMsgVulkanRules = (1 << 4), // issue messages for Vulkan-requirements of GLSL for SPIR-V - EShMsgOnlyPreprocessor = (1 << 5), // only print out errors produced by the preprocessor - EShMsgReadHlsl = (1 << 6), // use HLSL parsing rules and semantics - EShMsgCascadingErrors = (1 << 7), // get cascading errors; risks error-recovery issues, instead of an early exit - EShMsgKeepUncalled = (1 << 8), // for testing, don't eliminate uncalled functions - EShMsgHlslOffsets = (1 << 9), // allow block offsets to follow HLSL rules instead of GLSL rules - EShMsgDebugInfo = (1 << 10), // save debug information - EShMsgHlslEnable16BitTypes = (1 << 11), // enable use of 16-bit types in SPIR-V for HLSL - EShMsgHlslLegalization = (1 << 12), // enable HLSL Legalization messages - EShMsgHlslDX9Compatible = (1 << 13), // enable HLSL DX9 compatible mode (for samplers and semantics) - EShMsgBuiltinSymbolTable = (1 << 14), // print the builtin symbol table - EShMsgEnhanced = (1 << 15), // enhanced message readability + EShMsgDefault = 0, // default is to give all required errors and extra warnings + EShMsgRelaxedErrors = (1 << 0), // be liberal in accepting input + EShMsgSuppressWarnings = (1 << 1), // suppress all warnings, except those required by the specification + EShMsgAST = (1 << 2), // print the AST intermediate representation + EShMsgSpvRules = (1 << 3), // issue messages for SPIR-V generation + EShMsgVulkanRules = (1 << 4), // issue messages for Vulkan-requirements of GLSL for SPIR-V + EShMsgOnlyPreprocessor = (1 << 5), // only print out errors produced by the preprocessor + EShMsgReadHlsl = (1 << 6), // use HLSL parsing rules and semantics + EShMsgCascadingErrors = (1 << 7), // get cascading errors; risks error-recovery issues, instead of an early exit + EShMsgKeepUncalled = (1 << 8), // for testing, don't eliminate uncalled functions + EShMsgHlslOffsets = (1 << 9), // allow block offsets to follow HLSL rules instead of GLSL rules + EShMsgDebugInfo = (1 << 10), // save debug information + EShMsgHlslEnable16BitTypes = (1 << 11), // enable use of 16-bit types in SPIR-V for HLSL + EShMsgHlslLegalization = (1 << 12), // enable HLSL Legalization messages + EShMsgHlslDX9Compatible = (1 << 13), // enable HLSL DX9 compatible mode (for samplers and semantics) + EShMsgBuiltinSymbolTable = (1 << 14), // print the builtin symbol table + EShMsgEnhanced = (1 << 15), // enhanced message readability + EShMsgAbsolutePath = (1 << 16), // Output Absolute path for messages LAST_ELEMENT_MARKER(EShMsgCount), }; @@ -335,7 +336,8 @@ GLSLANG_EXPORT int ShCompile(const ShHandle, const char* const shaderStrings[], int, // debugOptions unused int defaultVersion = 110, // use 100 for ES environment, overridden by #version in shader bool forwardCompatible = false, // give errors for use of deprecated features - EShMessages messages = EShMsgDefault // warnings and errors + EShMessages messages = EShMsgDefault, // warnings and errors + const char* fileName = nullptr ); GLSLANG_EXPORT int ShLinkExt(