diff options
| -rw-r--r-- | examples/hello-world/main.cpp | 10 | ||||
| -rw-r--r-- | include/slang.h | 16 | ||||
| -rw-r--r-- | source/slang/slang-compiler-options.cpp | 35 | ||||
| -rw-r--r-- | source/slang/slang-compiler-options.h | 7 | ||||
| -rw-r--r-- | source/slang/slang-options.cpp | 15 |
5 files changed, 71 insertions, 12 deletions
diff --git a/examples/hello-world/main.cpp b/examples/hello-world/main.cpp index e9585bde9..c6774b64f 100644 --- a/examples/hello-world/main.cpp +++ b/examples/hello-world/main.cpp @@ -117,11 +117,19 @@ int HelloWorldExample::createComputePipelineFromShader() slang::TargetDesc targetDesc = {}; targetDesc.format = SLANG_SPIRV; targetDesc.profile = slangGlobalSession->findProfile("spirv_1_5"); - targetDesc.flags = SLANG_TARGET_FLAG_GENERATE_SPIRV_DIRECTLY; + targetDesc.flags = 0; + sessionDesc.targets = &targetDesc; sessionDesc.targetCount = 1; + std::vector<slang::CompilerOptionEntry> options; + options.push_back( + {slang::CompilerOptionName::EmitSpirvDirectly, + {slang::CompilerOptionValueKind::Int, 1, 0, nullptr, nullptr}}); + sessionDesc.compilerOptionEntries = options.data(); + sessionDesc.compilerOptionEntryCount = options.size(); + ComPtr<slang::ISession> session; RETURN_ON_FAIL(slangGlobalSession->createSession(sessionDesc, session.writeRef())); diff --git a/include/slang.h b/include/slang.h index c40ef1c7b..2999996e9 100644 --- a/include/slang.h +++ b/include/slang.h @@ -713,6 +713,7 @@ typedef uint32_t SlangSizeT; SLANG_TARGET_FLAG_DUMP_IR = 1 << 9, /* When set, will generate SPIRV directly rather than via glslang. */ + // This flag will be deprecated, use CompilerOption instead. SLANG_TARGET_FLAG_GENERATE_SPIRV_DIRECTLY = 1 << 10, }; constexpr static SlangTargetFlags kDefaultTargetFlags = @@ -845,6 +846,13 @@ typedef uint32_t SlangSizeT; or may involve severe space-vs-speed tradeoffs */ }; + enum SlangEmitSpirvMethod + { + SLANG_EMIT_SPIRV_DEFAULT = 0, + SLANG_EMIT_SPIRV_VIA_GLSL, + SLANG_EMIT_SPIRV_DIRECTLY, + }; + // All compiler option names supported by Slang. namespace slang { @@ -914,8 +922,8 @@ typedef uint32_t SlangSizeT; GLSLForceScalarLayout, // bool EnableEffectAnnotations, // bool - EmitSpirvViaGLSL, // bool - EmitSpirvDirectly, // bool + EmitSpirvViaGLSL, // bool (will be deprecated) + EmitSpirvDirectly, // bool (will be deprecated) SPIRVCoreGrammarJSON, // stringValue0: json path IncompleteLibrary, // bool, when set, will not issue an error when the linked program has // unresolved extern function symbols. @@ -991,6 +999,10 @@ typedef uint32_t SlangSizeT; // precompiled modules if it is up-to-date with its source. EmbedDownstreamIR, // bool ForceDXLayout, // bool + + // Add this new option to the end of the list to avoid breaking ABI as much as possible. + // Setting of EmitSpirvDirectly or EmitSpirvViaGLSL will turn into this option internally. + EmitSpirvMethod, // enum SlangEmitSpirvMethod CountOf, }; 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<SlangEmitSpirvMethod>(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<SlangEmitSpirvMethod>(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<SlangEmitSpirvMethod>(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<SlangEmitSpirvMethod>( + 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: |
