From fe4907d0ffbe762ba2cb7e8b62fc0bd30f815b02 Mon Sep 17 00:00:00 2001 From: Ben Doherty Date: Thu, 29 Nov 2018 10:35:37 -0800 Subject: [PATCH] Remove use of iostream in filamat (#549) --- libs/filamat/src/GLSLPostProcessor.cpp | 26 +++++++++------- libs/filamat/src/sca/ASTHelpers.cpp | 43 +++++++++++++------------- libs/filamat/src/sca/GLSLTools.cpp | 36 +++++++++++---------- libs/utils/include/utils/Log.h | 3 ++ 4 files changed, 58 insertions(+), 50 deletions(-) diff --git a/libs/filamat/src/GLSLPostProcessor.cpp b/libs/filamat/src/GLSLPostProcessor.cpp index 9b48dacb97..f4856df3f6 100644 --- a/libs/filamat/src/GLSLPostProcessor.cpp +++ b/libs/filamat/src/GLSLPostProcessor.cpp @@ -16,7 +16,6 @@ #include "GLSLPostProcessor.h" -#include #include #include @@ -29,6 +28,8 @@ #include "sca/builtinResource.h" #include "sca/GLSLTools.h" +#include + using namespace glslang; using namespace spirv_cross; using namespace spvtools; @@ -54,7 +55,7 @@ static uint32_t shaderVersionFromModel(filament::driver::ShaderModel model) { } static void errorHandler(const std::string& str) { - std::cerr << str << std::endl; + utils::slog.e << str << utils::io::endl; } static std::string stringifySpvOptimizerMessage(spv_message_level_t level, const char* source, @@ -136,7 +137,7 @@ bool GLSLPostProcessor::process(const std::string& inputShader, if (targetApi == TargetApi::OPENGL && mOptimization == MaterialBuilder::Optimization::NONE) { *outputGlsl = inputShader; if (mPrintShaders) { - std::cout << *outputGlsl << std::endl; + utils::slog.i << *outputGlsl << utils::io::endl; } return true; } @@ -164,7 +165,7 @@ bool GLSLPostProcessor::process(const std::string& inputShader, EShMessages msg = GLSLTools::glslangFlagsFromTargetApi(targetApi); bool ok = tShader.parse(&DefaultTBuiltInResource, mLangVersion, false, msg); if (!ok) { - std::cerr << tShader.getInfoLog() << std::endl; + utils::slog.e << tShader.getInfoLog() << utils::io::endl; return false; } @@ -173,7 +174,7 @@ bool GLSLPostProcessor::process(const std::string& inputShader, // SPIR-V types bool linkOk = program.link(msg); if (!linkOk) { - std::cerr << tShader.getInfoLog() << std::endl; + utils::slog.e << tShader.getInfoLog() << utils::io::endl; return false; } @@ -182,8 +183,8 @@ bool GLSLPostProcessor::process(const std::string& inputShader, if (mSpirvOutput) { GlslangToSpv(*program.getIntermediate(mShLang), *mSpirvOutput); } else { - std::cerr << "GLSL post-processor invoked with optimization level NONE" - << std::endl; + utils::slog.e << "GLSL post-processor invoked with optimization level NONE" + << utils::io::endl; } break; case MaterialBuilder::Optimization::PREPROCESSOR: @@ -198,7 +199,7 @@ bool GLSLPostProcessor::process(const std::string& inputShader, if (mGlslOutput) { *mGlslOutput = shrinkString(*mGlslOutput); if (mPrintShaders) { - std::cout << *mGlslOutput << std::endl; + utils::slog.i << *mGlslOutput << utils::io::endl; } } return true; @@ -218,7 +219,7 @@ void GLSLPostProcessor::preprocessOptimization(glslang::TShader& tShader, msg, &glsl, forbidIncluder); if (!ok) { - std::cerr << tShader.getInfoLog() << std::endl; + utils::slog.e << tShader.getInfoLog() << utils::io::endl; } if (mSpirvOutput) { @@ -233,7 +234,7 @@ void GLSLPostProcessor::preprocessOptimization(glslang::TShader& tShader, // SPIR-V types bool linkOk = program.link(msg); if (!ok || !linkOk) { - std::cerr << spirvShader.getInfoLog() << std::endl; + utils::slog.e << spirvShader.getInfoLog() << utils::io::endl; } else { GlslangToSpv(*program.getIntermediate(mShLang), *mSpirvOutput); } @@ -255,7 +256,8 @@ void GLSLPostProcessor::fullOptimization(const TShader& tShader, Optimizer optimizer(SPV_ENV_UNIVERSAL_1_3); optimizer.SetMessageConsumer([](spv_message_level_t level, const char* source, const spv_position_t& position, const char* message) { - std::cerr << stringifySpvOptimizerMessage(level, source, position, message) << std::endl; + utils::slog.e << stringifySpvOptimizerMessage(level, source, position, message) + << utils::io::endl; }); if (mOptimization == MaterialBuilder::Optimization::SIZE) { @@ -265,7 +267,7 @@ void GLSLPostProcessor::fullOptimization(const TShader& tShader, } if (!optimizer.Run(spirv.data(), spirv.size(), &spirv)) { - std::cerr << "SPIR-V optimizer pass failed" << std::endl; + utils::slog.e << "SPIR-V optimizer pass failed" << utils::io::endl; return; } diff --git a/libs/filamat/src/sca/ASTHelpers.cpp b/libs/filamat/src/sca/ASTHelpers.cpp index f03e579ccb..cf9e2091ee 100644 --- a/libs/filamat/src/sca/ASTHelpers.cpp +++ b/libs/filamat/src/sca/ASTHelpers.cpp @@ -18,10 +18,11 @@ #include "GLSLTools.h" -#include - #include #include + +#include + using namespace glslang; namespace ASTUtils { @@ -232,67 +233,67 @@ public: void pad() { for (int i = 0; i < depth; ++i) { - std::cerr << " "; + utils::slog.e << " "; } } bool visitBinary(TVisit, TIntermBinary* node) override { pad(); - std::cerr << "Binary " << op2Str(node->getOp()); - std::cerr << std::endl; + utils::slog.e << "Binary " << op2Str(node->getOp()); + utils::slog.e << utils::io::endl; return true; } bool visitUnary(TVisit, TIntermUnary* node) override { pad(); - std::cerr << "Unary" << op2Str(node->getOp()); - std::cerr << std::endl; + utils::slog.e << "Unary" << op2Str(node->getOp()); + utils::slog.e << utils::io::endl; return true; } bool visitAggregate(TVisit, TIntermAggregate* node) override { pad(); - std::cerr << "Aggregate" << op2Str(node->getOp()); - std::cerr << std::endl; + utils::slog.e << "Aggregate" << op2Str(node->getOp()); + utils::slog.e << utils::io::endl; return true; } bool visitSelection(TVisit, TIntermSelection* node) override { pad(); - std::cerr << "Selection"; - std::cerr << std::endl; + utils::slog.e << "Selection"; + utils::slog.e << utils::io::endl; return true; } void visitConstantUnion(TIntermConstantUnion* node) override { pad(); - std::cerr << "ConstantUnion"; - std::cerr << std::endl; + utils::slog.e << "ConstantUnion"; + utils::slog.e << utils::io::endl; } void visitSymbol(TIntermSymbol* node) override { pad(); - std::cerr << "Symbol " << node->getAsSymbolNode()->getName().c_str(); - std::cerr << std::endl; + utils::slog.e << "Symbol " << node->getAsSymbolNode()->getName().c_str(); + utils::slog.e << utils::io::endl; } bool visitLoop(TVisit, TIntermLoop* node) override { pad(); - std::cerr << "Loop"; - std::cerr << std::endl; + utils::slog.e << "Loop"; + utils::slog.e << utils::io::endl; return true; } bool visitBranch(TVisit, TIntermBranch* node) override { pad(); - std::cerr << "Branch"; - std::cerr << std::endl; + utils::slog.e << "Branch"; + utils::slog.e << utils::io::endl; return true; } bool visitSwitch(TVisit, TIntermSwitch* node) override { - std::cerr << "Binary "; - std::cerr << std::endl; + utils::slog.e << "Binary "; + utils::slog.e << utils::io::endl; return true; } }; diff --git a/libs/filamat/src/sca/GLSLTools.cpp b/libs/filamat/src/sca/GLSLTools.cpp index aede8425c3..7da17da0b7 100644 --- a/libs/filamat/src/sca/GLSLTools.cpp +++ b/libs/filamat/src/sca/GLSLTools.cpp @@ -66,8 +66,8 @@ bool GLSLTools::analyzeFragmentShader(const std::string& shaderCode, ShaderModel EShMessages msg = glslangFlagsFromTargetApi(targetApi); bool ok = tShader.parse(&DefaultTBuiltInResource, version, false, msg); if (!ok) { - std::cerr << "ERROR: Unable to parse fragment shader:" << std::endl; - std::cerr << tShader.getInfoLog() << std::flush; + utils::slog.e << "ERROR: Unable to parse fragment shader:" << utils::io::endl; + utils::slog.e << tShader.getInfoLog() << utils::io::flush; return false; } @@ -75,8 +75,8 @@ bool GLSLTools::analyzeFragmentShader(const std::string& shaderCode, ShaderModel // Check there is a material function definition in this shader. TIntermNode* materialFctNode = ASTUtils::getFunctionByNameOnly("material", *root); if (materialFctNode == nullptr) { - std::cerr << "ERROR: Invalid fragment shader:" << std::endl; - std::cerr << "ERROR: Unable to find material() function" << std::endl; + utils::slog.e << "ERROR: Invalid fragment shader:" << utils::io::endl; + utils::slog.e << "ERROR: Unable to find material() function" << utils::io::endl; return false; } @@ -84,8 +84,8 @@ bool GLSLTools::analyzeFragmentShader(const std::string& shaderCode, ShaderModel TIntermAggregate* prepareMaterialNode = ASTUtils::getFunctionByNameOnly("prepareMaterial", *root); if (prepareMaterialNode == nullptr) { - std::cerr << "ERROR: Invalid fragment shader:" << std::endl; - std::cerr << "ERROR: Unable to find prepareMaterial() function" << std::endl; + utils::slog.e << "ERROR: Invalid fragment shader:" << utils::io::endl; + utils::slog.e << "ERROR: Unable to find prepareMaterial() function" << utils::io::endl; return false; } @@ -93,8 +93,8 @@ bool GLSLTools::analyzeFragmentShader(const std::string& shaderCode, ShaderModel bool prepareMaterialCalled = isFunctionCalled(prepareMaterialSignature, *materialFctNode, *root); if (!prepareMaterialCalled) { - std::cerr << "ERROR: Invalid fragment shader:" << std::endl; - std::cerr << "ERROR: prepareMaterial() is not called" << std::endl; + utils::slog.e << "ERROR: Invalid fragment shader:" << utils::io::endl; + utils::slog.e << "ERROR: prepareMaterial() is not called" << utils::io::endl; return false; } @@ -115,8 +115,8 @@ bool GLSLTools::analyzeVertexShader(const std::string& shaderCode, ShaderModel m EShMessages msg = glslangFlagsFromTargetApi(targetApi); bool ok = tShader.parse(&DefaultTBuiltInResource, version, false, msg); if (!ok) { - std::cerr << "ERROR: Unable to parse vertex shader" << std::endl; - std::cerr << tShader.getInfoLog() << std::flush; + utils::slog.e << "ERROR: Unable to parse vertex shader" << utils::io::endl; + utils::slog.e << tShader.getInfoLog() << utils::io::flush; return false; } @@ -124,8 +124,8 @@ bool GLSLTools::analyzeVertexShader(const std::string& shaderCode, ShaderModel m // Check there is a material function definition in this shader. TIntermNode* materialFctNode = ASTUtils::getFunctionByNameOnly("materialVertex", *root); if (materialFctNode == nullptr) { - std::cerr << "ERROR: Invalid vertex shader" << std::endl; - std::cerr << "ERROR: Unable to find materialVertex() function" << std::endl; + utils::slog.e << "ERROR: Invalid vertex shader" << utils::io::endl; + utils::slog.e << "ERROR: Unable to find materialVertex() function" << utils::io::endl; return false; } @@ -167,7 +167,7 @@ bool GLSLTools::findProperties(const filamat::MaterialBuilder& builderIn, if (!ok) { // Even with all properties set the shader doesn't build. This is likely a syntax error // with user provided code. - std::cerr << tShader.getInfoLog() << std::endl; + utils::slog.e << tShader.getInfoLog() << utils::io::endl; return false; } @@ -184,7 +184,8 @@ bool GLSLTools::findPropertyWritesOperations(const std::string& functionName, si glslang::TIntermAggregate* functionMaterialDef = ASTUtils::getFunctionBySignature(functionName, *rootNode); if (functionMaterialDef == nullptr) { - std::cerr << "Unable to find function '" << functionName << "' definition." << std::endl; + utils::slog.e << "Unable to find function '" << functionName << "' definition." + << utils::io::endl; return false; } @@ -192,8 +193,8 @@ bool GLSLTools::findPropertyWritesOperations(const std::string& functionName, si ASTUtils::getFunctionParameters(functionMaterialDef, functionMaterialParameters); if (functionMaterialParameters.size() <= parameterIdx) { - std::cerr << "Unable to find function '" << functionName << "' parameterIndex: " << - parameterIdx << std::endl; + utils::slog.e << "Unable to find function '" << functionName << "' parameterIndex: " << + parameterIdx << utils::io::endl; return false; } @@ -207,7 +208,8 @@ bool GLSLTools::findPropertyWritesOperations(const std::string& functionName, si std::deque symbols; bool ok = findSymbolsUsage(functionName, *rootNode, symbols); if (!ok) { - std::cerr << "Unable to trace usage of symbols in function '" << functionName << std::endl; + utils::slog.e << "Unable to trace usage of symbols in function '" << functionName + << utils::io::endl; return false; } diff --git a/libs/utils/include/utils/Log.h b/libs/utils/include/utils/Log.h index 673ba6b244..eaeb35be93 100644 --- a/libs/utils/include/utils/Log.h +++ b/libs/utils/include/utils/Log.h @@ -86,6 +86,7 @@ private: friend ostream& hex(ostream& s) noexcept; friend ostream& dec(ostream& s) noexcept; friend ostream& endl(ostream& s) noexcept; + friend ostream& flush(ostream& s) noexcept; enum type { SHORT, USHORT, INT, UINT, LONG, ULONG, LONG_LONG, ULONG_LONG, DOUBLE, LONG_DOUBLE @@ -132,6 +133,7 @@ inline ostream& operator<<(ostream& stream, const VECTOR& v) { inline ostream& hex(ostream& s) noexcept { return s.hex(); } inline ostream& dec(ostream& s) noexcept { return s.dec(); } inline ostream& endl(ostream& s) noexcept { s << "\n"; return s.flush(); } +inline ostream& flush(ostream& s) noexcept { return s.flush(); } #else // UTILS_TINY_IO @@ -139,6 +141,7 @@ using ostream = std::ostream; inline ostream& hex(ostream& s) noexcept { return s << std::hex; } inline ostream& dec(ostream& s) noexcept { return s << std::dec; } inline ostream& endl(ostream& s) noexcept { return s << std::endl; } +inline ostream& flush(ostream& s) noexcept { return s << std::flush; } #endif // UTILS_TINY_IO } // namespace io