Skip to content

Commit

Permalink
Added support for macro definitions. (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
Slin authored Apr 9, 2020
1 parent 8f1c534 commit 971319a
Showing 1 changed file with 41 additions and 3 deletions.
44 changes: 41 additions & 3 deletions Source/Tools/ShaderConductorCmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ int main(int argc, char** argv)
("I,input", "Input file name", cxxopts::value<std::string>())("O,output", "Output file name", cxxopts::value<std::string>())
("S,stage", "Shader stage: vs, ps, gs, hs, ds, cs", cxxopts::value<std::string>())
("T,target", "Target shading language: dxil, spirv, hlsl, glsl, essl, msl_macos, msl_ios", cxxopts::value<std::string>()->default_value("dxil"))
("V,version", "The version of target shading language", cxxopts::value<std::string>()->default_value(""));
("V,version", "The version of target shading language", cxxopts::value<std::string>()->default_value(""))
("D,define", "Macro define as name=value", cxxopts::value<std::vector<std::string>>());

// clang-format on

auto opts = options.parse(argc, argv);
Expand Down Expand Up @@ -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<MacroDefine> macroDefines;
std::vector<std::string> macroStrings;
if (numberOfDefines > 0)
{
macroDefines.reserve(numberOfDefines);
macroStrings.reserve(numberOfDefines * 2);
auto& defines = opts["define"].as<std::vector<std::string>>();
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<uint32_t>(macroDefines.size());
}

try
{
Expand All @@ -178,7 +216,7 @@ int main(int argc, char** argv)
if (result.errorWarningMsg != nullptr)
{
const char* msg = reinterpret_cast<const char*>(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)
Expand Down

0 comments on commit 971319a

Please sign in to comment.