@@ -83,7 +83,7 @@ DxcCompilationResult dxcCompile(const CHLSLCompiler* compiler, nbl::asset::impl:
83
83
if ((options.debugInfoFlags .value & sourceEmittingFlags) != CHLSLCompiler::E_DEBUG_INFO_FLAGS::EDIF_NONE)
84
84
{
85
85
std::ostringstream insertion;
86
- insertion << " // #pragma compile_flags " ;
86
+ insertion << " #pragma compile_flags " ;
87
87
88
88
std::wstring_convert<std::codecvt_utf8<wchar_t >, wchar_t > conv;
89
89
for (uint32_t arg = 0 ; arg < argCount; arg ++)
@@ -149,7 +149,8 @@ DxcCompilationResult dxcCompile(const CHLSLCompiler* compiler, nbl::asset::impl:
149
149
150
150
#include " nbl/asset/utils/waveContext.h"
151
151
152
- std::string CHLSLCompiler::preprocessShader (std::string&& code, IShader::E_SHADER_STAGE& stage, const SPreprocessorOptions& preprocessOptions) const
152
+
153
+ std::string CHLSLCompiler::preprocessShader (std::string&& code, IShader::E_SHADER_STAGE& stage, std::vector<std::string>& dxc_compile_flags_override, const SPreprocessorOptions& preprocessOptions) const
153
154
{
154
155
nbl::wave::context context (code.begin (),code.end (),preprocessOptions.sourceIdentifier .data (),{preprocessOptions});
155
156
context.add_macro_definition (" __HLSL_VERSION" );
@@ -192,12 +193,22 @@ std::string CHLSLCompiler::preprocessShader(std::string&& code, IShader::E_SHADE
192
193
}
193
194
}
194
195
196
+ if (context.get_hooks ().m_dxc_compile_flags_override .size () != 0 )
197
+ dxc_compile_flags_override = context.get_hooks ().m_dxc_compile_flags_override ;
198
+
195
199
if (context.get_hooks ().m_pragmaStage != IShader::ESS_UNKNOWN)
196
200
stage = context.get_hooks ().m_pragmaStage ;
197
201
202
+
203
+
198
204
return resolvedString;
199
205
}
200
206
207
+ std::string CHLSLCompiler::preprocessShader (std::string&& code, IShader::E_SHADER_STAGE& stage, const SPreprocessorOptions& preprocessOptions) const
208
+ {
209
+ std::vector<std::string> extra_dxc_compile_flags = {};
210
+ return preprocessShader (std::move (code), stage, extra_dxc_compile_flags, preprocessOptions);
211
+ }
201
212
202
213
core::smart_refctd_ptr<ICPUShader> CHLSLCompiler::compileToSPIRV (const char * code, const IShaderCompiler::SCompilerOptions& options) const
203
214
{
@@ -208,9 +219,9 @@ core::smart_refctd_ptr<ICPUShader> CHLSLCompiler::compileToSPIRV(const char* cod
208
219
hlslOptions.preprocessorOptions .logger .log (" code is nullptr" , system::ILogger::ELL_ERROR);
209
220
return nullptr ;
210
221
}
211
-
222
+ std::vector<std::string> dxc_compile_flags = {};
212
223
auto stage = hlslOptions.stage ;
213
- auto newCode = preprocessShader (code, stage, hlslOptions.preprocessorOptions );
224
+ auto newCode = preprocessShader (code, stage, dxc_compile_flags, hlslOptions.preprocessorOptions );
214
225
215
226
// Suffix is the shader model version
216
227
// TODO: Figure out a way to get the shader model version automatically
@@ -256,7 +267,21 @@ core::smart_refctd_ptr<ICPUShader> CHLSLCompiler::compileToSPIRV(const char* cod
256
267
return nullptr ;
257
268
};
258
269
259
- std::vector<LPCWSTR> arguments = {
270
+ std::wstring* arg_storage = NULL ;
271
+ std::vector<LPCWSTR> arguments;
272
+
273
+ if (dxc_compile_flags.size ()) { // #pragma wave overrides compile flags
274
+ size_t arg_size = dxc_compile_flags.size ();
275
+ arguments = {};
276
+ arguments.reserve (arg_size);
277
+ arg_storage = new std::wstring[arg_size]; // prevent deallocation before shader compilation
278
+ for (size_t i = 0 ; i < dxc_compile_flags.size (); i++) {
279
+ arg_storage[i] = std::wstring (dxc_compile_flags[i].begin (), dxc_compile_flags[i].end ());
280
+ arguments.push_back (arg_storage[i].c_str ());
281
+ }
282
+ }
283
+ else {
284
+ arguments = {
260
285
L" -spirv" ,
261
286
L" -HV" , L" 202x" ,
262
287
L" -T" , targetProfile.c_str (),
@@ -267,40 +292,43 @@ core::smart_refctd_ptr<ICPUShader> CHLSLCompiler::compileToSPIRV(const char* cod
267
292
L" -Wno-c++1z-extensions" ,
268
293
L" -Wno-gnu-static-float-init" ,
269
294
L" -fspv-target-env=vulkan1.3"
270
- };
295
+ };
296
+ // If a custom SPIR-V optimizer is specified, use that instead of DXC's spirv-opt.
297
+ // This is how we can get more optimizer options.
298
+ //
299
+ // Optimization is also delegated to SPIRV-Tools. Right now there are no difference between
300
+ // optimization levels greater than zero; they will all invoke the same optimization recipe.
301
+ // https://github.com/Microsoft/DirectXShaderCompiler/blob/main/docs/SPIR-V.rst#optimization
302
+ if (hlslOptions.spirvOptimizer )
303
+ {
304
+ arguments.push_back (L" -O0" );
305
+ }
271
306
272
- // If a custom SPIR-V optimizer is specified, use that instead of DXC's spirv-opt.
273
- // This is how we can get more optimizer options.
274
- //
275
- // Optimization is also delegated to SPIRV-Tools. Right now there are no difference between
276
- // optimization levels greater than zero; they will all invoke the same optimization recipe.
277
- // https://github.com/Microsoft/DirectXShaderCompiler/blob/main/docs/SPIR-V.rst#optimization
278
- if (hlslOptions.spirvOptimizer )
279
- {
280
- arguments.push_back (L" -O0" );
307
+ // Debug only values
308
+ if (hlslOptions.debugInfoFlags .hasFlags (E_DEBUG_INFO_FLAGS::EDIF_FILE_BIT))
309
+ arguments.push_back (L" -fspv-debug=file" );
310
+ if (hlslOptions.debugInfoFlags .hasFlags (E_DEBUG_INFO_FLAGS::EDIF_SOURCE_BIT))
311
+ arguments.push_back (L" -fspv-debug=source" );
312
+ if (hlslOptions.debugInfoFlags .hasFlags (E_DEBUG_INFO_FLAGS::EDIF_LINE_BIT))
313
+ arguments.push_back (L" -fspv-debug=line" );
314
+ if (hlslOptions.debugInfoFlags .hasFlags (E_DEBUG_INFO_FLAGS::EDIF_TOOL_BIT))
315
+ arguments.push_back (L" -fspv-debug=tool" );
316
+ if (hlslOptions.debugInfoFlags .hasFlags (E_DEBUG_INFO_FLAGS::EDIF_NON_SEMANTIC_BIT))
317
+ arguments.push_back (L" -fspv-debug=vulkan-with-source" );
281
318
}
282
319
283
- // Debug only values
284
- if (hlslOptions.debugInfoFlags .hasFlags (E_DEBUG_INFO_FLAGS::EDIF_FILE_BIT))
285
- arguments.push_back (L" -fspv-debug=file" );
286
- if (hlslOptions.debugInfoFlags .hasFlags (E_DEBUG_INFO_FLAGS::EDIF_SOURCE_BIT))
287
- arguments.push_back (L" -fspv-debug=source" );
288
- if (hlslOptions.debugInfoFlags .hasFlags (E_DEBUG_INFO_FLAGS::EDIF_LINE_BIT))
289
- arguments.push_back (L" -fspv-debug=line" );
290
- if (hlslOptions.debugInfoFlags .hasFlags (E_DEBUG_INFO_FLAGS::EDIF_TOOL_BIT))
291
- arguments.push_back (L" -fspv-debug=tool" );
292
- if (hlslOptions.debugInfoFlags .hasFlags (E_DEBUG_INFO_FLAGS::EDIF_NON_SEMANTIC_BIT))
293
- arguments.push_back (L" -fspv-debug=vulkan-with-source" );
294
-
295
320
auto compileResult = dxcCompile (
296
321
this ,
297
322
m_dxcCompilerTypes,
298
323
newCode,
299
- & arguments[ 0 ] ,
324
+ arguments. data () ,
300
325
arguments.size (),
301
326
hlslOptions
302
327
);
303
328
329
+ if (arg_storage)
330
+ delete[] arg_storage;
331
+
304
332
if (!compileResult.objectBlob )
305
333
{
306
334
return nullptr ;
@@ -322,4 +350,6 @@ void CHLSLCompiler::insertIntoStart(std::string& code, std::ostringstream&& ins)
322
350
{
323
351
code.insert (0u , ins.str ());
324
352
}
353
+
354
+
325
355
#endif
0 commit comments