From 11ba8de2d19df27c63f0015a1dabaac7b2fd1f02 Mon Sep 17 00:00:00 2001 From: Catalin Moldovan Date: Mon, 4 Apr 2022 22:52:44 +0300 Subject: [PATCH] Shaderc console out (#2758) * Add support to output compiled shader to stdout * Whitespace formating fixes * Updated shaderc cli stdout output option to "--stdout" --- tools/shaderc/shaderc.cpp | 44 +++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/tools/shaderc/shaderc.cpp b/tools/shaderc/shaderc.cpp index 5d92a8f06..536ef0d78 100644 --- a/tools/shaderc/shaderc.cpp +++ b/tools/shaderc/shaderc.cpp @@ -1007,6 +1007,7 @@ namespace bgfx " -f Input file path.\n" " -i Include path (for multiple paths use -i multiple times).\n" " -o Output file path.\n" + " --stdout Output to console.\n" " --bin2c [array name] Generate C header file. If array name is not specified base file name will be used as name.\n" " --depends Generate makefile style depends file.\n" " --platform Target platform.\n" @@ -1068,7 +1069,7 @@ namespace bgfx return word; } - bool compileShader(const char* _varying, const char* _comment, char* _shader, uint32_t _shaderLen, Options& _options, bx::FileWriter* _writer) + bool compileShader(const char* _varying, const char* _comment, char* _shader, uint32_t _shaderLen, Options& _options, bx::WriterI* _writer) { uint32_t profile_id = 0; @@ -2633,10 +2634,11 @@ namespace bgfx return bx::kExitFailure; } + bool consoleOut = cmdLine.hasArg("stdout"); const char* outFilePath = cmdLine.findOption('o'); - if (NULL == outFilePath) + if (NULL == outFilePath && !consoleOut) { - help("Output file name must be specified."); + help("Output file name must be specified or use \"--stdout\" to output to stdout."); return bx::kExitFailure; } @@ -2649,7 +2651,7 @@ namespace bgfx Options options; options.inputFilePath = filePath; - options.outputFilePath = outFilePath; + options.outputFilePath = consoleOut ? "" : outFilePath; options.shaderType = bx::toLower(type[0]); options.disasm = cmdLine.hasArg('\0', "disasm"); @@ -2815,25 +2817,31 @@ namespace bgfx bx::FileWriter* writer = NULL; - if (!bin2c.isEmpty() ) + if (!consoleOut) { - writer = new Bin2cWriter(bin2c); - } - else - { - writer = new bx::FileWriter; + if (!bin2c.isEmpty()) + { + writer = new Bin2cWriter(bin2c); + } + else + { + writer = new bx::FileWriter; + } + + if (!bx::open(writer, outFilePath)) + { + bx::printf("Unable to open output file '%s'.\n", outFilePath); + return bx::kExitFailure; + } } - if (!bx::open(writer, outFilePath) ) + compiled = compileShader(varying, commandLineComment.c_str(), data, size, options, consoleOut ? bx::getStdOut() : writer); + + if (!consoleOut) { - bx::printf("Unable to open output file '%s'.\n", outFilePath); - return bx::kExitFailure; + bx::close(writer); + delete writer; } - - compiled = compileShader(varying, commandLineComment.c_str(), data, size, options, writer); - - bx::close(writer); - delete writer; } if (compiled)