Skip to content

Commit 5bb8c6a

Browse files
authored
Merge pull request #984 from Devsh-Graphics-Programming/nsc_macro_def_fix
2 parents 8cbfa75 + ca783f8 commit 5bb8c6a

2 files changed

Lines changed: 53 additions & 4 deletions

File tree

src/nbl/asset/utils/CWaveStringResolver.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,21 @@ namespace nbl::wave
5858
// now define them as "NBL_GLSL_LIMIT_MAX_IMAGE_DIMENSION_1D=32768"
5959
// to match boost wave syntax
6060
// https://www.boost.org/doc/libs/1_82_0/libs/wave/doc/class_reference_context.html#:~:text=Maintain%20defined%20macros-,add_macro_definition,-bool%20add_macro_definition
61-
for (const auto& define : preprocessOptions.extraDefines)
62-
context.add_macro_definition(define.identifier.data() + core::string("=") + define.definition.data());
6361

6462
// preprocess
6563
core::string resolvedString;
6664
try
6765
{
66+
for (const auto& define : preprocessOptions.extraDefines)
67+
{
68+
std::string macroDefinition(define.identifier);
69+
macroDefinition.push_back('=');
70+
macroDefinition.append(define.definition);
71+
context.add_macro_definition(macroDefinition);
72+
}
73+
6874
auto stream = std::stringstream();
69-
for (auto i= context.begin(); i!= context.end(); i++)
75+
for (auto i = context.begin(); i != context.end(); i++)
7076
stream << i->get_value();
7177
resolvedString = stream.str();
7278
}
@@ -85,4 +91,4 @@ namespace nbl::wave
8591

8692
return resolvedString;
8793
}
88-
}
94+
}

tools/nsc/main.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,47 @@ class ShaderCompiler final : public IApplicationFramework
472472
for (const auto& p : m_include_search_paths)
473473
includeFinder->addSearchPath(p, includeLoader);
474474

475+
// need this struct becuase fields of IShaderCompiler::SMacroDefinition are string views
476+
struct SMacroDefinitionBuffer
477+
{
478+
std::string identifier;
479+
std::string definition;
480+
};
481+
482+
core::vector<SMacroDefinitionBuffer> macroDefinitionBuffers;
483+
core::vector<IShaderCompiler::SMacroDefinition> macroDefinitions;
484+
{
485+
for (const auto& argument : m_arguments)
486+
{
487+
if (argument.rfind("-D", 0) != 0)
488+
continue;
489+
490+
std::string argumentTmp = argument.substr(2);
491+
492+
std::string identifier;
493+
std::string definition;
494+
495+
const size_t equalPos = argumentTmp.find('=');
496+
if (equalPos == std::string::npos)
497+
{
498+
identifier = argumentTmp;
499+
definition = "1";
500+
}
501+
else
502+
{
503+
identifier = argumentTmp.substr(0, equalPos);
504+
definition = argumentTmp.substr(equalPos + 1);
505+
}
506+
507+
macroDefinitionBuffers.emplace_back(identifier, definition);
508+
}
509+
510+
macroDefinitions.reserve(macroDefinitionBuffers.size());
511+
512+
for (const auto& macroDefinitionBuffer : macroDefinitionBuffers)
513+
macroDefinitions.emplace_back(macroDefinitionBuffer.identifier, macroDefinitionBuffer.definition);
514+
}
515+
475516
if (preprocessOnly)
476517
{
477518
CHLSLCompiler::SPreprocessorOptions opt = {};
@@ -480,6 +521,7 @@ class ShaderCompiler final : public IApplicationFramework
480521
opt.includeFinder = includeFinder.get();
481522
opt.depfile = dep.enabled;
482523
opt.depfilePath = dep.path;
524+
opt.extraDefines = macroDefinitions;
483525

484526
const char* codePtr = (const char*)shader->getContent()->getPointer();
485527
std::string_view code(codePtr, std::strlen(codePtr));
@@ -497,6 +539,7 @@ class ShaderCompiler final : public IApplicationFramework
497539
opt.preprocessorOptions.includeFinder = includeFinder.get();
498540
opt.preprocessorOptions.depfile = dep.enabled;
499541
opt.preprocessorOptions.depfilePath = dep.path;
542+
opt.preprocessorOptions.extraDefines = macroDefinitions;
500543
opt.debugInfoFlags = bitflag<IShaderCompiler::E_DEBUG_INFO_FLAGS>(IShaderCompiler::E_DEBUG_INFO_FLAGS::EDIF_TOOL_BIT);
501544
opt.dxcOptions = std::span<std::string>(m_arguments);
502545

0 commit comments

Comments
 (0)