Skip to content

Commit 61cddbe

Browse files
authored
Improve on spirv generation compile option (shader-slang#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.
1 parent b8d50b5 commit 61cddbe

File tree

5 files changed

+71
-12
lines changed

5 files changed

+71
-12
lines changed

examples/hello-world/main.cpp

+9-1
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,19 @@ int HelloWorldExample::createComputePipelineFromShader()
117117
slang::TargetDesc targetDesc = {};
118118
targetDesc.format = SLANG_SPIRV;
119119
targetDesc.profile = slangGlobalSession->findProfile("spirv_1_5");
120-
targetDesc.flags = SLANG_TARGET_FLAG_GENERATE_SPIRV_DIRECTLY;
120+
targetDesc.flags = 0;
121+
121122

122123
sessionDesc.targets = &targetDesc;
123124
sessionDesc.targetCount = 1;
124125

126+
std::vector<slang::CompilerOptionEntry> options;
127+
options.push_back(
128+
{slang::CompilerOptionName::EmitSpirvDirectly,
129+
{slang::CompilerOptionValueKind::Int, 1, 0, nullptr, nullptr}});
130+
sessionDesc.compilerOptionEntries = options.data();
131+
sessionDesc.compilerOptionEntryCount = options.size();
132+
125133
ComPtr<slang::ISession> session;
126134
RETURN_ON_FAIL(slangGlobalSession->createSession(sessionDesc, session.writeRef()));
127135

include/slang.h

+14-2
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,7 @@ typedef uint32_t SlangSizeT;
713713
SLANG_TARGET_FLAG_DUMP_IR = 1 << 9,
714714

715715
/* When set, will generate SPIRV directly rather than via glslang. */
716+
// This flag will be deprecated, use CompilerOption instead.
716717
SLANG_TARGET_FLAG_GENERATE_SPIRV_DIRECTLY = 1 << 10,
717718
};
718719
constexpr static SlangTargetFlags kDefaultTargetFlags =
@@ -845,6 +846,13 @@ typedef uint32_t SlangSizeT;
845846
or may involve severe space-vs-speed tradeoffs */
846847
};
847848

849+
enum SlangEmitSpirvMethod
850+
{
851+
SLANG_EMIT_SPIRV_DEFAULT = 0,
852+
SLANG_EMIT_SPIRV_VIA_GLSL,
853+
SLANG_EMIT_SPIRV_DIRECTLY,
854+
};
855+
848856
// All compiler option names supported by Slang.
849857
namespace slang
850858
{
@@ -914,8 +922,8 @@ typedef uint32_t SlangSizeT;
914922
GLSLForceScalarLayout, // bool
915923
EnableEffectAnnotations, // bool
916924

917-
EmitSpirvViaGLSL, // bool
918-
EmitSpirvDirectly, // bool
925+
EmitSpirvViaGLSL, // bool (will be deprecated)
926+
EmitSpirvDirectly, // bool (will be deprecated)
919927
SPIRVCoreGrammarJSON, // stringValue0: json path
920928
IncompleteLibrary, // bool, when set, will not issue an error when the linked program has
921929
// unresolved extern function symbols.
@@ -991,6 +999,10 @@ typedef uint32_t SlangSizeT;
991999
// precompiled modules if it is up-to-date with its source.
9921000
EmbedDownstreamIR, // bool
9931001
ForceDXLayout, // bool
1002+
1003+
// Add this new option to the end of the list to avoid breaking ABI as much as possible.
1004+
// Setting of EmitSpirvDirectly or EmitSpirvViaGLSL will turn into this option internally.
1005+
EmitSpirvMethod, // enum SlangEmitSpirvMethod
9941006
CountOf,
9951007
};
9961008

source/slang/slang-compiler-options.cpp

+30-5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,22 @@ void CompilerOptionSet::load(uint32_t count, slang::CompilerOptionEntry* entries
1818
value.stringValue2 = entries[i].value.stringValue1;
1919
}
2020
add(entries[i].name, value);
21+
22+
// When we see option EmitSpirvDirectly or EmitSpirvViaGLSL, we will need to
23+
// translate them to EmitSpirvMethod.
24+
if (entries[i].name == slang::CompilerOptionName::EmitSpirvDirectly && value.intValue)
25+
{
26+
set(slang::CompilerOptionName::EmitSpirvMethod, SLANG_EMIT_SPIRV_DIRECTLY);
27+
}
28+
else if (entries[i].name == slang::CompilerOptionName::EmitSpirvViaGLSL && value.intValue)
29+
{
30+
SlangEmitSpirvMethod current =
31+
getEnumOption<SlangEmitSpirvMethod>(slang::CompilerOptionName::EmitSpirvMethod);
32+
if (current != SLANG_EMIT_SPIRV_DEFAULT)
33+
{
34+
set(CompilerOptionName::EmitSpirvMethod, SLANG_EMIT_SPIRV_VIA_GLSL);
35+
}
36+
}
2137
}
2238
}
2339

@@ -181,10 +197,10 @@ SlangTargetFlags CompilerOptionSet::getTargetFlags()
181197
result |= SLANG_TARGET_FLAG_DUMP_IR;
182198
if (getBoolOption(CompilerOptionName::GenerateWholeProgram))
183199
result |= SLANG_TARGET_FLAG_GENERATE_WHOLE_PROGRAM;
184-
if (!getBoolOption(CompilerOptionName::EmitSpirvViaGLSL))
185-
result |= SLANG_TARGET_FLAG_GENERATE_SPIRV_DIRECTLY;
186200
if (getBoolOption(CompilerOptionName::ParameterBlocksUseRegisterSpaces))
187201
result |= SLANG_TARGET_FLAG_PARAMETER_BLOCKS_USE_REGISTER_SPACES;
202+
if (shouldEmitSPIRVDirectly())
203+
result |= SLANG_TARGET_FLAG_GENERATE_SPIRV_DIRECTLY;
188204
return result;
189205
}
190206

@@ -193,10 +209,19 @@ void CompilerOptionSet::setTargetFlags(SlangTargetFlags flags)
193209
set(CompilerOptionName::DumpIr, (flags & SLANG_TARGET_FLAG_DUMP_IR) != 0);
194210
set(CompilerOptionName::GenerateWholeProgram,
195211
(flags & SLANG_TARGET_FLAG_GENERATE_WHOLE_PROGRAM) != 0);
212+
196213
if ((flags & SLANG_TARGET_FLAG_GENERATE_SPIRV_DIRECTLY) != 0)
197-
set(CompilerOptionName::EmitSpirvViaGLSL, false);
214+
set(CompilerOptionName::EmitSpirvMethod, SLANG_EMIT_SPIRV_DIRECTLY);
198215
else
199-
set(CompilerOptionName::EmitSpirvViaGLSL, true);
216+
{
217+
// We allow to set this flag only when users are not setting the
218+
// the spirv emit method via CompilerOptionName.
219+
SlangEmitSpirvMethod current =
220+
getEnumOption<SlangEmitSpirvMethod>(CompilerOptionName::EmitSpirvMethod);
221+
if (current != SLANG_EMIT_SPIRV_DIRECTLY)
222+
set(CompilerOptionName::EmitSpirvMethod, SLANG_EMIT_SPIRV_VIA_GLSL);
223+
}
224+
200225
set(CompilerOptionName::ParameterBlocksUseRegisterSpaces,
201226
(flags & SLANG_TARGET_FLAG_PARAMETER_BLOCKS_USE_REGISTER_SPACES) != 0);
202227
}
@@ -210,7 +235,7 @@ void CompilerOptionSet::addTargetFlags(SlangTargetFlags flags)
210235
set(CompilerOptionName::GenerateWholeProgram, true);
211236

212237
if ((flags & SLANG_TARGET_FLAG_GENERATE_SPIRV_DIRECTLY) != 0)
213-
set(CompilerOptionName::EmitSpirvDirectly, true);
238+
set(CompilerOptionName::EmitSpirvMethod, SLANG_EMIT_SPIRV_DIRECTLY);
214239

215240
if ((flags & SLANG_TARGET_FLAG_PARAMETER_BLOCKS_USE_REGISTER_SPACES) != 0)
216241
set(CompilerOptionName::ParameterBlocksUseRegisterSpaces, true);

source/slang/slang-compiler-options.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -334,9 +334,10 @@ struct CompilerOptionSet
334334

335335
bool shouldEmitSPIRVDirectly()
336336
{
337-
if (getBoolOption(CompilerOptionName::EmitSpirvViaGLSL))
338-
return false;
339-
return true;
337+
SlangEmitSpirvMethod emitSpvMethod =
338+
getEnumOption<SlangEmitSpirvMethod>(CompilerOptionName::EmitSpirvMethod);
339+
340+
return (emitSpvMethod != SlangEmitSpirvMethod::SLANG_EMIT_SPIRV_VIA_GLSL);
340341
}
341342

342343
bool shouldUseScalarLayout()

source/slang/slang-options.cpp

+14-1
Original file line numberDiff line numberDiff line change
@@ -2728,7 +2728,20 @@ SlangResult OptionsParser::_parse(int argc, char const* const* argv)
27282728
case OptionKind::EmitSpirvViaGLSL:
27292729
case OptionKind::EmitSpirvDirectly:
27302730
{
2731-
getCurrentTarget()->optionSet.add(optionKind, true);
2731+
SlangEmitSpirvMethod selectMethod = (optionKind == OptionKind::EmitSpirvViaGLSL)
2732+
? SLANG_EMIT_SPIRV_VIA_GLSL
2733+
: SLANG_EMIT_SPIRV_DIRECTLY;
2734+
2735+
SlangEmitSpirvMethod currentMethod =
2736+
getCurrentTarget()->optionSet.getEnumOption<SlangEmitSpirvMethod>(
2737+
OptionKind::EmitSpirvMethod);
2738+
// When both flag turns on, spirv-direcly mode will always take higher priority.
2739+
// By default (value 0), spirv-via-glsl mode is used, and any input flag can
2740+
// override the default value.
2741+
if (selectMethod > currentMethod)
2742+
{
2743+
getCurrentTarget()->optionSet.set(OptionKind::EmitSpirvMethod, selectMethod);
2744+
}
27322745
}
27332746
break;
27342747
case OptionKind::SPIRVCoreGrammarJSON:

0 commit comments

Comments
 (0)