Skip to content

Commit f68ad87

Browse files
HazarduAnastaZIuk
authored andcommitted
dxc compile flags initial draft
1 parent da5fec0 commit f68ad87

File tree

3 files changed

+88
-55
lines changed

3 files changed

+88
-55
lines changed

include/nbl/asset/utils/CHLSLCompiler.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ class NBL_API2 CHLSLCompiler final : public IShaderCompiler
4747
//}
4848

4949
std::string preprocessShader(std::string&& code, IShader::E_SHADER_STAGE& stage, const SPreprocessorOptions& preprocessOptions) const override;
50-
50+
std::string preprocessShader(std::string&& code, IShader::E_SHADER_STAGE& stage, std::vector<std::string>& dxc_compile_flags_override, const SPreprocessorOptions& preprocessOptions) const;
51+
5152
void insertIntoStart(std::string& code, std::ostringstream&& ins) const override;
5253
protected:
5354

src/nbl/asset/utils/CHLSLCompiler.cpp

Lines changed: 72 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -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<LPCWSTR> extra_dxc_compile_flags = {};
210+
// return preprocessShader(code, stage, extra_dxc_compile_flags, preprocessOptions);
211+
//}
201212

202213
core::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
@@ -227,36 +238,44 @@ core::smart_refctd_ptr<ICPUShader> CHLSLCompiler::compileToSPIRV(const char* cod
227238
// Set profile two letter prefix based on stage
228239
switch (stage)
229240
{
230-
case asset::IShader::ESS_VERTEX:
231-
targetProfile.replace(0, 2, L"vs");
232-
break;
233-
case asset::IShader::ESS_TESSELLATION_CONTROL:
234-
targetProfile.replace(0, 2, L"ds");
235-
break;
236-
case asset::IShader::ESS_TESSELLATION_EVALUATION:
237-
targetProfile.replace(0, 2, L"hs");
238-
break;
239-
case asset::IShader::ESS_GEOMETRY:
240-
targetProfile.replace(0, 2, L"gs");
241-
break;
242-
case asset::IShader::ESS_FRAGMENT:
243-
targetProfile.replace(0, 2, L"ps");
244-
break;
245-
case asset::IShader::ESS_COMPUTE:
246-
targetProfile.replace(0, 2, L"cs");
247-
break;
248-
case asset::IShader::ESS_TASK:
249-
targetProfile.replace(0, 2, L"as");
250-
break;
251-
case asset::IShader::ESS_MESH:
252-
targetProfile.replace(0, 2, L"ms");
253-
break;
254-
default:
255-
hlslOptions.preprocessorOptions.logger.log("invalid shader stage %i", system::ILogger::ELL_ERROR, stage);
256-
return nullptr;
241+
case asset::IShader::ESS_VERTEX:
242+
targetProfile.replace(0, 2, L"vs");
243+
break;
244+
case asset::IShader::ESS_TESSELLATION_CONTROL:
245+
targetProfile.replace(0, 2, L"ds");
246+
break;
247+
case asset::IShader::ESS_TESSELLATION_EVALUATION:
248+
targetProfile.replace(0, 2, L"hs");
249+
break;
250+
case asset::IShader::ESS_GEOMETRY:
251+
targetProfile.replace(0, 2, L"gs");
252+
break;
253+
case asset::IShader::ESS_FRAGMENT:
254+
targetProfile.replace(0, 2, L"ps");
255+
break;
256+
case asset::IShader::ESS_COMPUTE:
257+
targetProfile.replace(0, 2, L"cs");
258+
break;
259+
case asset::IShader::ESS_TASK:
260+
targetProfile.replace(0, 2, L"as");
261+
break;
262+
case asset::IShader::ESS_MESH:
263+
targetProfile.replace(0, 2, L"ms");
264+
break;
265+
default:
266+
hlslOptions.preprocessorOptions.logger.log("invalid shader stage %i", system::ILogger::ELL_ERROR, stage);
267+
return nullptr;
257268
};
258269

259-
std::vector<LPCWSTR> arguments = {
270+
271+
std::vector<LPCWSTR> arguments;
272+
if (dxc_compile_flags.size()) {
273+
arguments = {};
274+
for (size_t i = 0; i < dxc_compile_flags.size(); i++)
275+
arguments.push_back(LPCWSTR(dxc_compile_flags[i].c_str()));
276+
}
277+
else {
278+
arguments = {
260279
L"-spirv",
261280
L"-HV", L"202x",
262281
L"-T", targetProfile.c_str(),
@@ -267,31 +286,31 @@ core::smart_refctd_ptr<ICPUShader> CHLSLCompiler::compileToSPIRV(const char* cod
267286
L"-Wno-c++1z-extensions",
268287
L"-Wno-gnu-static-float-init",
269288
L"-fspv-target-env=vulkan1.3"
270-
};
289+
};
290+
// If a custom SPIR-V optimizer is specified, use that instead of DXC's spirv-opt.
291+
// This is how we can get more optimizer options.
292+
//
293+
// Optimization is also delegated to SPIRV-Tools. Right now there are no difference between
294+
// optimization levels greater than zero; they will all invoke the same optimization recipe.
295+
// https://github.com/Microsoft/DirectXShaderCompiler/blob/main/docs/SPIR-V.rst#optimization
296+
if (hlslOptions.spirvOptimizer)
297+
{
298+
arguments.push_back(L"-O0");
299+
}
271300

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");
301+
// Debug only values
302+
if (hlslOptions.debugInfoFlags.hasFlags(E_DEBUG_INFO_FLAGS::EDIF_FILE_BIT))
303+
arguments.push_back(L"-fspv-debug=file");
304+
if (hlslOptions.debugInfoFlags.hasFlags(E_DEBUG_INFO_FLAGS::EDIF_SOURCE_BIT))
305+
arguments.push_back(L"-fspv-debug=source");
306+
if (hlslOptions.debugInfoFlags.hasFlags(E_DEBUG_INFO_FLAGS::EDIF_LINE_BIT))
307+
arguments.push_back(L"-fspv-debug=line");
308+
if (hlslOptions.debugInfoFlags.hasFlags(E_DEBUG_INFO_FLAGS::EDIF_TOOL_BIT))
309+
arguments.push_back(L"-fspv-debug=tool");
310+
if (hlslOptions.debugInfoFlags.hasFlags(E_DEBUG_INFO_FLAGS::EDIF_NON_SEMANTIC_BIT))
311+
arguments.push_back(L"-fspv-debug=vulkan-with-source");
281312
}
282313

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-
295314
auto compileResult = dxcCompile(
296315
this,
297316
m_dxcCompilerTypes,

src/nbl/asset/utils/waveContext.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ struct load_to_string final
4747
struct preprocessing_hooks final : public boost::wave::context_policies::default_preprocessing_hooks
4848
{
4949
preprocessing_hooks(const IShaderCompiler::SPreprocessorOptions& _preprocessOptions)
50-
: m_includeFinder(_preprocessOptions.includeFinder), m_logger(_preprocessOptions.logger), m_pragmaStage(IShader::ESS_UNKNOWN) {}
50+
: m_includeFinder(_preprocessOptions.includeFinder), m_logger(_preprocessOptions.logger), m_pragmaStage(IShader::ESS_UNKNOWN), m_dxc_compile_flags_override() {}
5151

5252
template <typename ContextT>
5353
bool locate_include_file(ContextT& ctx, std::string& file_path, bool is_system, char const* current_name, std::string& dir_path, std::string& native_name)
@@ -97,6 +97,18 @@ struct preprocessing_hooks final : public boost::wave::context_policies::default
9797
m_pragmaStage = found->second;
9898
return true;
9999
}
100+
101+
if (strcmp(optionStr, "dxc_compile_flags") == 0) {
102+
m_dxc_compile_flags_override.clear();
103+
for (auto valueIter = values.begin(); valueIter != values.end(); valueIter++) {
104+
std::string compiler_option_s = std::string(valueIter->get_value().c_str());
105+
if (compiler_option_s[0] == '"' && compiler_option_s[compiler_option_s.length() - 1] == '"')
106+
compiler_option_s = compiler_option_s.substr(1, compiler_option_s.length() - 2);
107+
m_dxc_compile_flags_override.push_back(compiler_option_s);
108+
}
109+
110+
}
111+
100112
return false;
101113
}
102114

@@ -114,6 +126,7 @@ struct preprocessing_hooks final : public boost::wave::context_policies::default
114126
const IShaderCompiler::CIncludeFinder* m_includeFinder;
115127
system::logger_opt_ptr m_logger;
116128
IShader::E_SHADER_STAGE m_pragmaStage;
129+
std::vector<std::string> m_dxc_compile_flags_override;
117130
};
118131

119132
class context : private boost::noncopyable

0 commit comments

Comments
 (0)