@@ -95,6 +95,26 @@ CHLSLCompiler::~CHLSLCompiler()
9595 delete m_dxcCompilerTypes;
9696}
9797
98+ static bool fixup_spirv_target_ver (std::vector<std::wstring>& arguments, system::logger_opt_ptr& logger)
99+ {
100+ constexpr std::wstring_view Prefix = L" -fspv-target-env=" ;
101+ const std::set<std::wstring_view> AllowedSuffices = {L" vulkan1.1spirv1.4" ,L" vulkan1.2" ,L" vulkan1.3" };
102+
103+ for (auto targetEnvArgumentPos=arguments.begin (); targetEnvArgumentPos!=arguments.end (); targetEnvArgumentPos++)
104+ if (targetEnvArgumentPos->find (Prefix)==0 )
105+ {
106+ const auto suffix = targetEnvArgumentPos->substr (Prefix.length ());
107+ const auto found = AllowedSuffices.find (suffix);
108+ if (found!=AllowedSuffices.end ())
109+ return true ;
110+ logger.log (" Compile flag error: Required compile flag not found -fspv-target-env=. Force enabling -fspv-target-env= found but with unsupported value `%s`." , system::ILogger::ELL_ERROR, " TODO: write wchar to char convert usage" );
111+ return false ;
112+ }
113+
114+ logger.log (" Compile flag error: Required compile flag not found -fspv-target-env=. Force enabling -fspv-target-env=vulkan1.3, as it is required by Nabla." , system::ILogger::ELL_WARNING);
115+ arguments.push_back (L" -fspv-target-env=vulkan1.3" );
116+ return true ;
117+ }
98118
99119static void try_upgrade_hlsl_version (std::vector<std::wstring>& arguments, system::logger_opt_ptr& logger)
100120{
@@ -418,35 +438,49 @@ core::smart_refctd_ptr<ICPUShader> CHLSLCompiler::compileToSPIRV_impl(const std:
418438 std::wstring targetProfile (SHADER_MODEL_PROFILE);
419439
420440 std::vector<std::wstring> arguments = {};
421- if (dxc_compile_flags.size ()) { // #pragma dxc_compile_flags takes priority
441+ if (! dxc_compile_flags.empty ()) // #pragma dxc_compile_flags takes priority
422442 populate_arguments_with_type_conversion (arguments, dxc_compile_flags, logger);
423- }
424- else if (hlslOptions.dxcOptions .size ()) { // second in order of priority is command line arguments
425- populate_arguments_with_type_conversion (arguments, hlslOptions.dxcOptions , logger);
426- }
427- else { // lastly default arguments
428- const auto required = CHLSLCompiler::getRequiredArguments ();
429- arguments = {};
430- for (size_t i = 0 ; i < required.size (); i++)
431- arguments.push_back (required[i]);
432- arguments.push_back (L" -HV" );
433- arguments.push_back (L" 202x" );
434- // TODO: add this to `CHLSLCompiler::SOptions` and handle it properly in `dxc_compile_flags.empty()`
435- if (stage != asset::IShader::E_SHADER_STAGE::ESS_ALL_OR_LIBRARY) {
436- arguments.push_back (L" -E" );
437- arguments.push_back (L" main" );
438- }
439- // If a custom SPIR-V optimizer is specified, use that instead of DXC's spirv-opt.
440- // This is how we can get more optimizer options.
441- //
442- // Optimization is also delegated to SPIRV-Tools. Right now there are no difference between
443- // optimization levels greater than zero; they will all invoke the same optimization recipe.
444- // https://github.com/Microsoft/DirectXShaderCompiler/blob/main/docs/SPIR-V.rst#optimization
445- if (hlslOptions.spirvOptimizer )
446- arguments.push_back (L" -O0" );
447- }
448- if (dxc_compile_flags.empty ())
443+ else
449444 {
445+ if (hlslOptions.dxcOptions .size ()) // second in order of priority is command line arguments
446+ populate_arguments_with_type_conversion (arguments, hlslOptions.dxcOptions , logger);
447+ else // lastly default arguments from polymorphic options
448+ {
449+ const auto required = CHLSLCompiler::getRequiredArguments ();
450+ arguments = {};
451+ for (size_t i = 0 ; i < required.size (); i++)
452+ arguments.push_back (required[i]);
453+ //
454+ switch (options.targetSpirvVersion )
455+ {
456+ case hlsl::SpirvVersion::ESV_1_4:
457+ arguments.push_back (L" -fspv-target-env=vulkan1.1spirv1.4" );
458+ case hlsl::SpirvVersion::ESV_1_5:
459+ arguments.push_back (L" -fspv-target-env=vulkan1.2" );
460+ break ;
461+ case hlsl::SpirvVersion::ESV_1_6:
462+ arguments.push_back (L" -fspv-target-env=vulkan1.3" );
463+ break ;
464+ default :
465+ logger.log (" Invalid `IShaderCompiler::SCompilerOptions::targetSpirvVersion`" , system::ILogger::ELL_ERROR);
466+ return nullptr ;
467+ break ;
468+ }
469+ // TODO: add entry point to `CHLSLCompiler::SOptions` and handle it properly in `dxc_compile_flags.empty()`
470+ if (stage != asset::IShader::E_SHADER_STAGE::ESS_ALL_OR_LIBRARY) {
471+ arguments.push_back (L" -E" );
472+ arguments.push_back (L" main" );
473+ }
474+ // If a custom SPIR-V optimizer is specified, use that instead of DXC's spirv-opt.
475+ // This is how we can get more optimizer options.
476+ //
477+ // Optimization is also delegated to SPIRV-Tools. Right now there are no difference between
478+ // optimization levels greater than zero; they will all invoke the same optimization recipe.
479+ // https://github.com/Microsoft/DirectXShaderCompiler/blob/main/docs/SPIR-V.rst#optimization
480+ if (hlslOptions.spirvOptimizer )
481+ arguments.push_back (L" -O0" );
482+ }
483+ //
450484 auto set = std::unordered_set<std::wstring>();
451485 for (int i = 0 ; i < arguments.size (); i++)
452486 set.insert (arguments[i]);
@@ -469,6 +503,9 @@ core::smart_refctd_ptr<ICPUShader> CHLSLCompiler::compileToSPIRV_impl(const std:
469503 add_if_missing (L" -fspv-debug=vulkan-with-source" );
470504 }
471505
506+ if (!fixup_spirv_target_ver (arguments, logger))
507+ return nullptr ;
508+
472509 try_upgrade_shader_stage (arguments, stage, logger);
473510 try_upgrade_hlsl_version (arguments, logger);
474511
0 commit comments