@@ -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