@@ -83,7 +83,7 @@ DxcCompilationResult dxcCompile(const CHLSLCompiler* compiler, nbl::asset::impl:
8383 if ((options.debugInfoFlags .value & sourceEmittingFlags) != CHLSLCompiler::E_DEBUG_INFO_FLAGS::EDIF_NONE)
8484 {
8585 std::ostringstream insertion;
86- insertion << " // #pragma compile_flags " ;
86+ insertion << " #pragma compile_flags " ;
8787
8888 std::wstring_convert<std::codecvt_utf8<wchar_t >, wchar_t > conv;
8989 for (uint32_t arg = 0 ; arg < argCount; arg ++)
@@ -149,7 +149,8 @@ DxcCompilationResult dxcCompile(const CHLSLCompiler* compiler, nbl::asset::impl:
149149
150150#include " nbl/asset/utils/waveContext.h"
151151
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
153154{
154155 nbl::wave::context context (code.begin (),code.end (),preprocessOptions.sourceIdentifier .data (),{preprocessOptions});
155156 context.add_macro_definition (" __HLSL_VERSION" );
@@ -192,12 +193,22 @@ std::string CHLSLCompiler::preprocessShader(std::string&& code, IShader::E_SHADE
192193 }
193194 }
194195
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+
195199 if (context.get_hooks ().m_pragmaStage != IShader::ESS_UNKNOWN)
196200 stage = context.get_hooks ().m_pragmaStage ;
197201
202+
203+
198204 return resolvedString;
199205}
200206
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+ }
201212
202213core::smart_refctd_ptr<ICPUShader> CHLSLCompiler::compileToSPIRV (const char * code, const IShaderCompiler::SCompilerOptions& options) const
203214{
@@ -208,9 +219,9 @@ core::smart_refctd_ptr<ICPUShader> CHLSLCompiler::compileToSPIRV(const char* cod
208219 hlslOptions.preprocessorOptions .logger .log (" code is nullptr" , system::ILogger::ELL_ERROR);
209220 return nullptr ;
210221 }
211-
222+ std::vector<std::string> dxc_compile_flags = {};
212223 auto stage = hlslOptions.stage ;
213- auto newCode = preprocessShader (code, stage, hlslOptions.preprocessorOptions );
224+ auto newCode = preprocessShader (code, stage, dxc_compile_flags, hlslOptions.preprocessorOptions );
214225
215226 // Suffix is the shader model version
216227 // 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
256267 return nullptr ;
257268 };
258269
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 = {
260285 L" -spirv" ,
261286 L" -HV" , L" 202x" ,
262287 L" -T" , targetProfile.c_str (),
@@ -267,40 +292,43 @@ core::smart_refctd_ptr<ICPUShader> CHLSLCompiler::compileToSPIRV(const char* cod
267292 L" -Wno-c++1z-extensions" ,
268293 L" -Wno-gnu-static-float-init" ,
269294 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+ }
271306
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" );
281318 }
282319
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-
295320 auto compileResult = dxcCompile (
296321 this ,
297322 m_dxcCompilerTypes,
298323 newCode,
299- & arguments[ 0 ] ,
324+ arguments. data () ,
300325 arguments.size (),
301326 hlslOptions
302327 );
303328
329+ if (arg_storage)
330+ delete[] arg_storage;
331+
304332 if (!compileResult.objectBlob )
305333 {
306334 return nullptr ;
@@ -322,4 +350,6 @@ void CHLSLCompiler::insertIntoStart(std::string& code, std::ostringstream&& ins)
322350{
323351 code.insert (0u , ins.str ());
324352}
353+
354+
325355#endif
0 commit comments