@@ -256,6 +256,15 @@ core::smart_refctd_ptr<ICPUShader> CHLSLCompiler::compileToSPIRV(const char* cod
256
256
auto newCode = preprocessShader (code, stage, hlslOptions.preprocessorOptions );
257
257
258
258
// Suffix is the shader model version
259
+ // TODO: Figure out a way to get the shader model version automatically
260
+ //
261
+ // We can't get it from the DXC library itself, as the different versions and the parsing
262
+ // use a weird lexer based system that resolves to a hash, and all of that is in a scoped variable
263
+ // (lib/DXIL/DxilShaderModel.cpp:83)
264
+ //
265
+ // Another option is trying to fetch it from the commandline tool, either from parsing the help message
266
+ // or from brute forcing every -T option until one isn't accepted
267
+ //
259
268
std::wstring targetProfile (L" XX_6_2" );
260
269
261
270
// Set profile two letter prefix based on stage
@@ -289,29 +298,40 @@ core::smart_refctd_ptr<ICPUShader> CHLSLCompiler::compileToSPIRV(const char* cod
289
298
return nullptr ;
290
299
};
291
300
292
- LPCWSTR arguments[] = {
293
- // These will always be present
301
+ std::vector<LPCWSTR> arguments = {
294
302
L" -spirv" ,
295
303
L" -HV" , L" 2021" ,
296
304
L" -T" , targetProfile.c_str (),
297
-
298
- // These are debug only
299
- DXC_ARG_DEBUG,
300
- L" -Qembed_debug" ,
301
- L" -fspv-debug=vulkan-with-source" ,
302
- L" -fspv-debug=file"
303
305
};
304
306
305
- const uint32_t nonDebugArgs = 5 ;
306
- const uint32_t allArgs = nonDebugArgs + 4 ;
307
+ // If a custom SPIR-V optimizer is specified, use that instead of DXC's spirv-opt.
308
+ // This is how we can get more optimizer options.
309
+ //
310
+ // Optimization is also delegated to SPIRV-Tools. Right now there are no difference between
311
+ // optimization levels greater than zero; they will all invoke the same optimization recipe.
312
+ // https://github.com/Microsoft/DirectXShaderCompiler/blob/main/docs/SPIR-V.rst#optimization
313
+ if (hlslOptions.spirvOptimizer )
314
+ {
315
+ arguments.push_back (L" -O0" );
316
+ }
317
+
318
+ // Debug only values
319
+ if (hlslOptions.genDebugInfo )
320
+ {
321
+ arguments.insert (arguments.end (), {
322
+ DXC_ARG_DEBUG,
323
+ L" -Qembed_debug" ,
324
+ L" -fspv-debug=vulkan-with-source" ,
325
+ L" -fspv-debug=file"
326
+ });
327
+ }
307
328
308
- // const CHLSLCompiler* compiler, nbl::asset::hlsl::impl::DXC* compilerTypes, std::string& source, LPCWSTR* args, uint32_t argCount, const CHLSLCompiler::SOptions& options
309
329
auto compileResult = dxcCompile (
310
330
this ,
311
331
m_dxcCompilerTypes,
312
332
newCode,
313
- &arguments[0 ],
314
- hlslOptions. genDebugInfo ? allArgs : nonDebugArgs,
333
+ &arguments[0 ],
334
+ arguments. size (),
315
335
hlslOptions
316
336
);
317
337
@@ -321,6 +341,11 @@ core::smart_refctd_ptr<ICPUShader> CHLSLCompiler::compileToSPIRV(const char* cod
321
341
}
322
342
323
343
auto outSpirv = core::make_smart_refctd_ptr<ICPUBuffer>(compileResult.objectBlob ->GetBufferSize ());
344
+
345
+ // Optimizer step
346
+ if (hlslOptions.spirvOptimizer )
347
+ outSpirv = hlslOptions.spirvOptimizer ->optimize (outSpirv.get (), hlslOptions.preprocessorOptions .logger );
348
+
324
349
memcpy (outSpirv->getPointer (), compileResult.objectBlob ->GetBufferPointer (), compileResult.objectBlob ->GetBufferSize ());
325
350
326
351
return core::make_smart_refctd_ptr<asset::ICPUShader>(std::move (outSpirv), stage, IShader::E_CONTENT_TYPE::ECT_SPIRV, hlslOptions.preprocessorOptions .sourceIdentifier .data ());
0 commit comments