From 61cddbee405935fa8391a757a08bcbe4ea7ac98f Mon Sep 17 00:00:00 2001 From: kaizhangNV <149626564+kaizhangNV@users.noreply.github.com> Date: Fri, 1 Nov 2024 18:12:33 -0500 Subject: Improve on spirv generation compile option (#5479) CompilerOptionName::EmitSpirvViaGLSL and CompilerOptionName::EmitSpirvDirectly options are not mutually exclusive, but due to compatible reason, we cannot delete those options. Instead, this change makes the effort to create a new option name EmitSpirvMethod, and we will turn those two options into the new one internally. Also, we put a priority implicitly on those two options, where EmitSpirvDirectly always win if it's set. We have another location that can setup the same option, where is through SlangTargetFlags::SLANG_TARGET_FLAG_GENERATE_SPIRV_DIRECTLY. We should definitely deprecate this flag to avoid more confusing. But for the same compatible reason, we cannot do that in this PR. Again, we will encourage people to not use this flag, but using the CompilerOptionName instead. In this PR, we will also implicitly give CompilerOptionName higher priority, it means that as long as user setup the CompilerOptionName for emit spirv method, it always take higher priority for the final decision. --- source/slang/slang-compiler-options.cpp | 35 ++++++++++++++++++++++++++++----- source/slang/slang-compiler-options.h | 7 ++++--- source/slang/slang-options.cpp | 15 +++++++++++++- 3 files changed, 48 insertions(+), 9 deletions(-) (limited to 'source') diff --git a/source/slang/slang-compiler-options.cpp b/source/slang/slang-compiler-options.cpp index c01d3fb9c..4350bcf0d 100644 --- a/source/slang/slang-compiler-options.cpp +++ b/source/slang/slang-compiler-options.cpp @@ -18,6 +18,22 @@ void CompilerOptionSet::load(uint32_t count, slang::CompilerOptionEntry* entries value.stringValue2 = entries[i].value.stringValue1; } add(entries[i].name, value); + + // When we see option EmitSpirvDirectly or EmitSpirvViaGLSL, we will need to + // translate them to EmitSpirvMethod. + if (entries[i].name == slang::CompilerOptionName::EmitSpirvDirectly && value.intValue) + { + set(slang::CompilerOptionName::EmitSpirvMethod, SLANG_EMIT_SPIRV_DIRECTLY); + } + else if (entries[i].name == slang::CompilerOptionName::EmitSpirvViaGLSL && value.intValue) + { + SlangEmitSpirvMethod current = + getEnumOption(slang::CompilerOptionName::EmitSpirvMethod); + if (current != SLANG_EMIT_SPIRV_DEFAULT) + { + set(CompilerOptionName::EmitSpirvMethod, SLANG_EMIT_SPIRV_VIA_GLSL); + } + } } } @@ -181,10 +197,10 @@ SlangTargetFlags CompilerOptionSet::getTargetFlags() result |= SLANG_TARGET_FLAG_DUMP_IR; if (getBoolOption(CompilerOptionName::GenerateWholeProgram)) result |= SLANG_TARGET_FLAG_GENERATE_WHOLE_PROGRAM; - if (!getBoolOption(CompilerOptionName::EmitSpirvViaGLSL)) - result |= SLANG_TARGET_FLAG_GENERATE_SPIRV_DIRECTLY; if (getBoolOption(CompilerOptionName::ParameterBlocksUseRegisterSpaces)) result |= SLANG_TARGET_FLAG_PARAMETER_BLOCKS_USE_REGISTER_SPACES; + if (shouldEmitSPIRVDirectly()) + result |= SLANG_TARGET_FLAG_GENERATE_SPIRV_DIRECTLY; return result; } @@ -193,10 +209,19 @@ void CompilerOptionSet::setTargetFlags(SlangTargetFlags flags) set(CompilerOptionName::DumpIr, (flags & SLANG_TARGET_FLAG_DUMP_IR) != 0); set(CompilerOptionName::GenerateWholeProgram, (flags & SLANG_TARGET_FLAG_GENERATE_WHOLE_PROGRAM) != 0); + if ((flags & SLANG_TARGET_FLAG_GENERATE_SPIRV_DIRECTLY) != 0) - set(CompilerOptionName::EmitSpirvViaGLSL, false); + set(CompilerOptionName::EmitSpirvMethod, SLANG_EMIT_SPIRV_DIRECTLY); else - set(CompilerOptionName::EmitSpirvViaGLSL, true); + { + // We allow to set this flag only when users are not setting the + // the spirv emit method via CompilerOptionName. + SlangEmitSpirvMethod current = + getEnumOption(CompilerOptionName::EmitSpirvMethod); + if (current != SLANG_EMIT_SPIRV_DIRECTLY) + set(CompilerOptionName::EmitSpirvMethod, SLANG_EMIT_SPIRV_VIA_GLSL); + } + set(CompilerOptionName::ParameterBlocksUseRegisterSpaces, (flags & SLANG_TARGET_FLAG_PARAMETER_BLOCKS_USE_REGISTER_SPACES) != 0); } @@ -210,7 +235,7 @@ void CompilerOptionSet::addTargetFlags(SlangTargetFlags flags) set(CompilerOptionName::GenerateWholeProgram, true); if ((flags & SLANG_TARGET_FLAG_GENERATE_SPIRV_DIRECTLY) != 0) - set(CompilerOptionName::EmitSpirvDirectly, true); + set(CompilerOptionName::EmitSpirvMethod, SLANG_EMIT_SPIRV_DIRECTLY); if ((flags & SLANG_TARGET_FLAG_PARAMETER_BLOCKS_USE_REGISTER_SPACES) != 0) set(CompilerOptionName::ParameterBlocksUseRegisterSpaces, true); diff --git a/source/slang/slang-compiler-options.h b/source/slang/slang-compiler-options.h index 3c1c76816..c28bbe83d 100644 --- a/source/slang/slang-compiler-options.h +++ b/source/slang/slang-compiler-options.h @@ -334,9 +334,10 @@ struct CompilerOptionSet bool shouldEmitSPIRVDirectly() { - if (getBoolOption(CompilerOptionName::EmitSpirvViaGLSL)) - return false; - return true; + SlangEmitSpirvMethod emitSpvMethod = + getEnumOption(CompilerOptionName::EmitSpirvMethod); + + return (emitSpvMethod != SlangEmitSpirvMethod::SLANG_EMIT_SPIRV_VIA_GLSL); } bool shouldUseScalarLayout() diff --git a/source/slang/slang-options.cpp b/source/slang/slang-options.cpp index 969ce4143..bc83afe65 100644 --- a/source/slang/slang-options.cpp +++ b/source/slang/slang-options.cpp @@ -2728,7 +2728,20 @@ SlangResult OptionsParser::_parse(int argc, char const* const* argv) case OptionKind::EmitSpirvViaGLSL: case OptionKind::EmitSpirvDirectly: { - getCurrentTarget()->optionSet.add(optionKind, true); + SlangEmitSpirvMethod selectMethod = (optionKind == OptionKind::EmitSpirvViaGLSL) + ? SLANG_EMIT_SPIRV_VIA_GLSL + : SLANG_EMIT_SPIRV_DIRECTLY; + + SlangEmitSpirvMethod currentMethod = + getCurrentTarget()->optionSet.getEnumOption( + OptionKind::EmitSpirvMethod); + // When both flag turns on, spirv-direcly mode will always take higher priority. + // By default (value 0), spirv-via-glsl mode is used, and any input flag can + // override the default value. + if (selectMethod > currentMethod) + { + getCurrentTarget()->optionSet.set(OptionKind::EmitSpirvMethod, selectMethod); + } } break; case OptionKind::SPIRVCoreGrammarJSON: -- cgit v1.2.3