From 971319ad0c029c5aeb9889691c5440245e121596 Mon Sep 17 00:00:00 2001 From: Nils Daumann Date: Thu, 9 Apr 2020 08:24:11 +0200 Subject: [PATCH] Added support for macro definitions. (#54) --- Source/Tools/ShaderConductorCmd.cpp | 44 +++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/Source/Tools/ShaderConductorCmd.cpp b/Source/Tools/ShaderConductorCmd.cpp index 239be581..c0c3cd11 100644 --- a/Source/Tools/ShaderConductorCmd.cpp +++ b/Source/Tools/ShaderConductorCmd.cpp @@ -48,7 +48,9 @@ int main(int argc, char** argv) ("I,input", "Input file name", cxxopts::value())("O,output", "Output file name", cxxopts::value()) ("S,stage", "Shader stage: vs, ps, gs, hs, ds, cs", cxxopts::value()) ("T,target", "Target shading language: dxil, spirv, hlsl, glsl, essl, msl_macos, msl_ios", cxxopts::value()->default_value("dxil")) - ("V,version", "The version of target shading language", cxxopts::value()->default_value("")); + ("V,version", "The version of target shading language", cxxopts::value()->default_value("")) + ("D,define", "Macro define as name=value", cxxopts::value>()); + // clang-format on auto opts = options.parse(argc, argv); @@ -169,7 +171,43 @@ int main(int argc, char** argv) } sourceDesc.source = source.c_str(); - // TODO: Support macro definition from command line + size_t numberOfDefines = opts.count("define"); + std::vector macroDefines; + std::vector macroStrings; + if (numberOfDefines > 0) + { + macroDefines.reserve(numberOfDefines); + macroStrings.reserve(numberOfDefines * 2); + auto& defines = opts["define"].as>(); + for (const auto& define : defines) + { + MacroDefine macroDefine; + macroDefine.name = nullptr; + macroDefine.value = nullptr; + + size_t splitPosition = define.find('='); + if (splitPosition != std::string::npos) + { + std::string macroName = define.substr(0, splitPosition); + std::string macroValue = define.substr(splitPosition + 1, define.size() - splitPosition - 1); + + macroStrings.push_back(macroName); + macroDefine.name = macroStrings.back().c_str(); + macroStrings.push_back(macroValue); + macroDefine.value = macroStrings.back().c_str(); + } + else + { + macroStrings.push_back(define); + macroDefine.name = macroStrings.back().c_str(); + } + + macroDefines.push_back(macroDefine); + } + + sourceDesc.defines = macroDefines.data(); + sourceDesc.numDefines = static_cast(macroDefines.size()); + } try { @@ -178,7 +216,7 @@ int main(int argc, char** argv) if (result.errorWarningMsg != nullptr) { const char* msg = reinterpret_cast(result.errorWarningMsg->Data()); - std::cerr << "Error or warning form shader compiler: " << std::endl + std::cerr << "Error or warning from shader compiler: " << std::endl << std::string(msg, msg + result.errorWarningMsg->Size()) << std::endl; } if (result.target != nullptr)