From fc8742d59f137e2385d01012902207dd1bbdc9ae Mon Sep 17 00:00:00 2001 From: Jerry Gamache Date: Tue, 1 Dec 2020 15:44:15 -0500 Subject: [PATCH 1/6] GLSL codegen implementation of geompropvalue as varying parameter --- .../stdlib/geometric/geompropvalue.mtlx | 2 +- .../Nodes/GeomPropValueNodeGlsl.cpp | 44 ++++++++++++++----- .../Nodes/GeomPropValueNodeGlsl.h | 2 + source/MaterialXGenOgsXml/OgsFragment.h | 6 +++ source/MaterialXGenOgsXml/OgsXmlGenerator.cpp | 15 ++++++- .../MaterialXGenShader/HwShaderGenerator.cpp | 3 ++ source/MaterialXGenShader/HwShaderGenerator.h | 2 + .../PyMaterialXGenOgsXml/PyOgsFragment.cpp | 1 + 8 files changed, 62 insertions(+), 13 deletions(-) diff --git a/resources/Materials/TestSuite/stdlib/geometric/geompropvalue.mtlx b/resources/Materials/TestSuite/stdlib/geometric/geompropvalue.mtlx index 28cc80328a..2936e4b956 100644 --- a/resources/Materials/TestSuite/stdlib/geometric/geompropvalue.mtlx +++ b/resources/Materials/TestSuite/stdlib/geometric/geompropvalue.mtlx @@ -8,7 +8,7 @@ - + diff --git a/source/MaterialXGenGlsl/Nodes/GeomPropValueNodeGlsl.cpp b/source/MaterialXGenGlsl/Nodes/GeomPropValueNodeGlsl.cpp index 48ec4fe0c6..2276c78afd 100644 --- a/source/MaterialXGenGlsl/Nodes/GeomPropValueNodeGlsl.cpp +++ b/source/MaterialXGenGlsl/Nodes/GeomPropValueNodeGlsl.cpp @@ -23,24 +23,48 @@ void GeomPropValueNodeGlsl::createVariables(const ShaderNode& node, GenContext&, throw ExceptionShaderGenError("No 'geomprop' parameter found on geompropvalue node '" + node.getName() + "', don't know what property to bind"); } const string geomProp = geomPropInput->getValue()->getValueString(); + const ShaderOutput* output = node.getOutput(); + if (output->getType() == Type::BOOLEAN || output->getType() == Type::STRING) { + throw ExceptionShaderGenError("GLSL: unsupported type for geompropvalue node '" + node.getName() + "', can not generate code."); + } + + ShaderStage& vs = shader.getStage(Stage::VERTEX); ShaderStage& ps = shader.getStage(Stage::PIXEL); - ShaderPort* uniform = addStageUniform(HW::PRIVATE_UNIFORMS, node.getOutput()->getType(), HW::T_GEOMPROP + "_" + geomProp, ps); - uniform->setPath(geomPropInput->getPath()); + + addStageInput(HW::VERTEX_INPUTS, output->getType(), HW::T_IN_GEOMPROP + "_" + geomProp, vs); + addStageConnector(HW::VERTEX_DATA, output->getType(), HW::T_GEOMPROP + "_" + geomProp, vs, ps); } void GeomPropValueNodeGlsl::emitFunctionCall(const ShaderNode& node, GenContext& context, ShaderStage& stage) const { - BEGIN_SHADER_STAGE(stage, Stage::PIXEL) - const ShaderGenerator& shadergen = context.getShaderGenerator(); - const ShaderInput* geomPropInput = node.getInput(GEOMPROP); - if (!geomPropInput) + const ShaderGenerator& shadergen = context.getShaderGenerator(); + + const ShaderInput* geomPropInput = node.getInput(GEOMPROP); + if (!geomPropInput) + { + throw ExceptionShaderGenError("No 'geomprop' parameter found on geompropvalue node '" + node.getName() + "', don't know what property to bind"); + } + const string geomname = geomPropInput->getValue()->getValueString(); + const string variable = HW::T_GEOMPROP + "_" + geomname; + + BEGIN_SHADER_STAGE(stage, Stage::VERTEX) + VariableBlock& vertexData = stage.getOutputBlock(HW::VERTEX_DATA); + const string prefix = vertexData.getInstance() + "."; + ShaderPort* geomprop = vertexData[variable]; + if (!geomprop->isEmitted()) { - throw ExceptionShaderGenError("No 'geomprop' parameter found on geompropvalue node '" + node.getName() + "', don't know what property to bind"); + shadergen.emitLine(prefix + geomprop->getVariable() + " = " + HW::T_IN_GEOMPROP + "_" + geomname, stage); + geomprop->setEmitted(); } - const string attrName = geomPropInput->getValue()->getValueString(); - shadergen.emitLineBegin(stage); + END_SHADER_STAGE(shader, Stage::VERTEX) + + BEGIN_SHADER_STAGE(stage, Stage::PIXEL) + VariableBlock& vertexData = stage.getInputBlock(HW::VERTEX_DATA); + const string prefix = vertexData.getInstance() + "."; + ShaderPort* geomprop = vertexData[variable]; + shadergen.emitLineBegin(stage); shadergen.emitOutput(node.getOutput(), true, false, context, stage); - shadergen.emitString(" = " + HW::T_GEOMPROP + "_" + attrName, stage); + shadergen.emitString(" = " + prefix + geomprop->getVariable(), stage); shadergen.emitLineEnd(stage); END_SHADER_STAGE(shader, Stage::PIXEL) } diff --git a/source/MaterialXGenGlsl/Nodes/GeomPropValueNodeGlsl.h b/source/MaterialXGenGlsl/Nodes/GeomPropValueNodeGlsl.h index 838ffa8461..64c9882f1f 100644 --- a/source/MaterialXGenGlsl/Nodes/GeomPropValueNodeGlsl.h +++ b/source/MaterialXGenGlsl/Nodes/GeomPropValueNodeGlsl.h @@ -20,6 +20,8 @@ class GeomPropValueNodeGlsl : public GlslImplementation void createVariables(const ShaderNode& node, GenContext& context, Shader& shader) const override; void emitFunctionCall(const ShaderNode& node, GenContext& context, ShaderStage& stage) const override; + + bool isEditable(const ShaderInput& /*input*/) const override { return false; } }; } // namespace MaterialX diff --git a/source/MaterialXGenOgsXml/OgsFragment.h b/source/MaterialXGenOgsXml/OgsFragment.h index 939c4b0cd4..cd7f833805 100644 --- a/source/MaterialXGenOgsXml/OgsFragment.h +++ b/source/MaterialXGenOgsXml/OgsFragment.h @@ -48,6 +48,12 @@ class OgsFragment return _element ? _element->getDocument() : mx::DocumentPtr(); } + /// Get the GLSL shader generated for this fragment + mx::ShaderPtr getShader() const + { + return _glslShader; + } + /// Return the source of the OGS fragment as a string. const std::string& getFragmentSource() const; diff --git a/source/MaterialXGenOgsXml/OgsXmlGenerator.cpp b/source/MaterialXGenOgsXml/OgsXmlGenerator.cpp index d51150d324..118dcb02bc 100644 --- a/source/MaterialXGenOgsXml/OgsXmlGenerator.cpp +++ b/source/MaterialXGenOgsXml/OgsXmlGenerator.cpp @@ -56,8 +56,6 @@ namespace { "Tm", "Tm" }, { "Bw", "Bw" }, { "Bm", "Bm" }, - { "texcoord_0", "mayaUvCoordSemantic" }, - { "color_0", "colorset" }, { "u_worldMatrix", "World" }, { "u_worldInverseMatrix", "WorldInverse" }, @@ -85,6 +83,13 @@ namespace { "u_time", "Time" } }; + static const vector > OGS_SEMANTICS_PREFIX_MAP = + { + { "texcoord_", "mayaUvCoordSemantic" }, + { "color_", "colorset" }, + { "u_geomprop_", "mayaUvCoordSemantic" } + }; + namespace OgsParameterFlags { static const std::string @@ -143,6 +148,12 @@ namespace { prop.append_attribute(SEMANTIC) = semantic->second; } + for (auto const& ogsSemantic: OGS_SEMANTICS_PREFIX_MAP) { + // STL startswith: + if (name.rfind(ogsSemantic.first, 0) == 0) { + prop.append_attribute(SEMANTIC) = ogsSemantic.second; + } + } if (!parameterFlags.empty()) { prop.append_attribute(FLAGS) = parameterFlags.c_str(); diff --git a/source/MaterialXGenShader/HwShaderGenerator.cpp b/source/MaterialXGenShader/HwShaderGenerator.cpp index aa4590d8fd..3b97ba0de4 100644 --- a/source/MaterialXGenShader/HwShaderGenerator.cpp +++ b/source/MaterialXGenShader/HwShaderGenerator.cpp @@ -24,6 +24,7 @@ namespace HW const string T_IN_NORMAL = "$inNormal"; const string T_IN_TANGENT = "$inTangent"; const string T_IN_TEXCOORD = "$inTexcoord"; + const string T_IN_GEOMPROP = "$inGeomprop"; const string T_IN_COLOR = "$inColor"; const string T_POSITION_WORLD = "$positionWorld"; const string T_NORMAL_WORLD = "$normalWorld"; @@ -75,6 +76,7 @@ namespace HW const string IN_NORMAL = "i_normal"; const string IN_TANGENT = "i_tangent"; const string IN_TEXCOORD = "i_texcoord"; + const string IN_GEOMPROP = "i_geomprop"; const string IN_COLOR = "i_color"; const string POSITION_WORLD = "positionWorld"; const string NORMAL_WORLD = "normalWorld"; @@ -166,6 +168,7 @@ HwShaderGenerator::HwShaderGenerator(SyntaxPtr syntax) : _tokenSubstitutions[HW::T_IN_NORMAL] = HW::IN_NORMAL; _tokenSubstitutions[HW::T_IN_TANGENT] = HW::IN_TANGENT; _tokenSubstitutions[HW::T_IN_TEXCOORD] = HW::IN_TEXCOORD; + _tokenSubstitutions[HW::T_IN_GEOMPROP] = HW::IN_GEOMPROP; _tokenSubstitutions[HW::T_IN_COLOR] = HW::IN_COLOR; _tokenSubstitutions[HW::T_POSITION_WORLD] = HW::POSITION_WORLD; _tokenSubstitutions[HW::T_NORMAL_WORLD] = HW::NORMAL_WORLD; diff --git a/source/MaterialXGenShader/HwShaderGenerator.h b/source/MaterialXGenShader/HwShaderGenerator.h index 0cf26e8dd4..1fac6f83eb 100644 --- a/source/MaterialXGenShader/HwShaderGenerator.h +++ b/source/MaterialXGenShader/HwShaderGenerator.h @@ -87,6 +87,7 @@ namespace HW extern const string T_IN_NORMAL; extern const string T_IN_TANGENT; extern const string T_IN_TEXCOORD; + extern const string T_IN_GEOMPROP; extern const string T_IN_COLOR; extern const string T_POSITION_WORLD; extern const string T_NORMAL_WORLD; @@ -140,6 +141,7 @@ namespace HW extern const string IN_NORMAL; extern const string IN_TANGENT; extern const string IN_TEXCOORD; + extern const string IN_GEOMPROP; extern const string IN_COLOR; extern const string POSITION_WORLD; extern const string NORMAL_WORLD; diff --git a/source/PyMaterialX/PyMaterialXGenOgsXml/PyOgsFragment.cpp b/source/PyMaterialX/PyMaterialXGenOgsXml/PyOgsFragment.cpp index 3b39749787..c2aeac8a76 100644 --- a/source/PyMaterialX/PyMaterialXGenOgsXml/PyOgsFragment.cpp +++ b/source/PyMaterialX/PyMaterialXGenOgsXml/PyOgsFragment.cpp @@ -25,6 +25,7 @@ void bindPyOgsFragment(py::module& mod) .def_property_readonly("document", &my::OgsFragment::getDocument) .def_property_readonly("fragmentSource", &my::OgsFragment::getFragmentSource) .def_property_readonly("fragmentName", &my::OgsFragment::getFragmentName) + .def_property_readonly("shader", &my::OgsFragment::getShader) .def_property_readonly("pathInputMap", &my::OgsFragment::getPathInputMap) .def_property_readonly("isElementAShader", &my::OgsFragment::isElementAShader) .def_property_readonly("isTransparent", &my::OgsFragment::isTransparent) From c0c26e3added9116b76e908a07ab3bde17a19be8 Mon Sep 17 00:00:00 2001 From: Jerry Gamache Date: Wed, 2 Dec 2020 10:43:46 -0500 Subject: [PATCH 2/6] Update unit tests. --- .../MaterialXGenGlsl/GlslShaderGenerator.cpp | 8 ++++-- source/MaterialXGenGlsl/GlslSyntax.cpp | 1 + source/MaterialXGenGlsl/GlslSyntax.h | 1 + source/MaterialXGenShader/ShaderGraph.cpp | 2 +- .../MaterialXTest/MaterialXGenGlsl/GenGlsl.h | 8 +++--- .../MaterialXGenShader/GenShaderUtil.cpp | 19 +++++++++++++- .../MaterialXRender/RenderUtil.cpp | 25 +++++++++++++++++++ .../MaterialXRender/RenderUtil.h | 5 ++++ .../MaterialXRenderGlsl/RenderGlsl.cpp | 5 ++++ 9 files changed, 67 insertions(+), 7 deletions(-) diff --git a/source/MaterialXGenGlsl/GlslShaderGenerator.cpp b/source/MaterialXGenGlsl/GlslShaderGenerator.cpp index 7816a44dba..ba73cca430 100644 --- a/source/MaterialXGenGlsl/GlslShaderGenerator.cpp +++ b/source/MaterialXGenGlsl/GlslShaderGenerator.cpp @@ -219,9 +219,8 @@ GlslShaderGenerator::GlslShaderGenerator() : registerImplementation("IM_geomcolor_color3_" + GlslShaderGenerator::LANGUAGE, GeomColorNodeGlsl::create); registerImplementation("IM_geomcolor_color4_" + GlslShaderGenerator::LANGUAGE, GeomColorNodeGlsl::create); // + // String and boolean are invalid as varying parameters. registerImplementation("IM_geompropvalue_integer_" + GlslShaderGenerator::LANGUAGE, GeomPropValueNodeGlsl::create); - registerImplementation("IM_geompropvalue_boolean_" + GlslShaderGenerator::LANGUAGE, GeomPropValueNodeGlsl::create); - registerImplementation("IM_geompropvalue_string_" + GlslShaderGenerator::LANGUAGE, GeomPropValueNodeGlsl::create); registerImplementation("IM_geompropvalue_float_" + GlslShaderGenerator::LANGUAGE, GeomPropValueNodeGlsl::create); registerImplementation("IM_geompropvalue_color2_" + GlslShaderGenerator::LANGUAGE, GeomPropValueNodeGlsl::create); registerImplementation("IM_geompropvalue_color3_" + GlslShaderGenerator::LANGUAGE, GeomPropValueNodeGlsl::create); @@ -753,6 +752,11 @@ void GlslShaderGenerator::emitVariableDeclaration(const ShaderPort* variable, co else { string str = qualifier.empty() ? EMPTY_STRING : qualifier + " "; + // Varying parameters of type int must be flat qualified on output from vertex stage and + // input to pixel stage. The only way to get these is with geompropvalue_integer nodes. + if (qualifier.empty() && variable->getType() == Type::INTEGER && !assignValue && variable->getName().rfind(HW::T_GEOMPROP, 0) == 0) { + str += GlslSyntax::FLAT_QUALIFIER + " "; + } str += _syntax->getTypeName(variable->getType()) + " " + variable->getVariable(); // If an array we need an array qualifier (suffix) for the variable name diff --git a/source/MaterialXGenGlsl/GlslSyntax.cpp b/source/MaterialXGenGlsl/GlslSyntax.cpp index 05cbc39d9c..f054629d34 100644 --- a/source/MaterialXGenGlsl/GlslSyntax.cpp +++ b/source/MaterialXGenGlsl/GlslSyntax.cpp @@ -102,6 +102,7 @@ const string GlslSyntax::INPUT_QUALIFIER = "in"; const string GlslSyntax::OUTPUT_QUALIFIER = "out"; const string GlslSyntax::UNIFORM_QUALIFIER = "uniform"; const string GlslSyntax::CONSTANT_QUALIFIER = "const"; +const string GlslSyntax::FLAT_QUALIFIER = "flat"; const string GlslSyntax::SOURCE_FILE_EXTENSION = ".glsl"; const StringVec GlslSyntax::VEC2_MEMBERS = { ".x", ".y" }; const StringVec GlslSyntax::VEC3_MEMBERS = { ".x", ".y", ".z" }; diff --git a/source/MaterialXGenGlsl/GlslSyntax.h b/source/MaterialXGenGlsl/GlslSyntax.h index 2c3e158aab..1aea7593f0 100644 --- a/source/MaterialXGenGlsl/GlslSyntax.h +++ b/source/MaterialXGenGlsl/GlslSyntax.h @@ -38,6 +38,7 @@ class GlslSyntax : public Syntax static const string OUTPUT_QUALIFIER; static const string UNIFORM_QUALIFIER; static const string CONSTANT_QUALIFIER; + static const string FLAT_QUALIFIER; static const string SOURCE_FILE_EXTENSION; static const StringVec VEC2_MEMBERS; diff --git a/source/MaterialXGenShader/ShaderGraph.cpp b/source/MaterialXGenShader/ShaderGraph.cpp index 96129f8c49..fc68d9b76f 100644 --- a/source/MaterialXGenShader/ShaderGraph.cpp +++ b/source/MaterialXGenShader/ShaderGraph.cpp @@ -1508,7 +1508,7 @@ void ShaderGraph::calculateScopes() { newScopeInfo.adjustAtConditionalInput(node, int(inputIndex), 0x12); } - else if (isSwitch) + else if (isSwitch && inputIndex != node->numInputs() - 1) { const uint32_t fullMask = (1 << node->numInputs()) - 1; newScopeInfo.adjustAtConditionalInput(node, int(inputIndex), fullMask); diff --git a/source/MaterialXTest/MaterialXGenGlsl/GenGlsl.h b/source/MaterialXTest/MaterialXGenGlsl/GenGlsl.h index 50d396448d..4b24856262 100644 --- a/source/MaterialXTest/MaterialXGenGlsl/GenGlsl.h +++ b/source/MaterialXTest/MaterialXGenGlsl/GenGlsl.h @@ -35,6 +35,8 @@ class GlslShaderGeneratorTester : public GenShaderUtil::ShaderGeneratorTester { _skipNodeDefs.insert("ND_displacement_float"); _skipNodeDefs.insert("ND_displacement_vector3"); + _skipNodeDefs.insert("ND_geompropvalue_boolean"); + _skipNodeDefs.insert("ND_geompropvalue_string"); ParentClass::addSkipNodeDefs(); } @@ -53,9 +55,9 @@ class GlslShaderGeneratorTester : public GenShaderUtil::ShaderGeneratorTester whiteList = { "ambientocclusion", "arrayappend", "backfacing", "screen", "curveadjust", "displacementshader", - "volumeshader", "IM_constant_", "IM_dot_", "IM_geompropvalue", "IM_light_genglsl", - "IM_point_light_genglsl", "IM_spot_light_genglsl", "IM_directional_light_genglsl", "IM_angle", - "surfacematerial", "volumematerial", "ND_surfacematerial", "ND_volumematerial" + "volumeshader", "IM_constant_", "IM_dot_", "IM_geompropvalue_boolean", "IM_geompropvalue_string", + "IM_light_genglsl", "IM_point_light_genglsl", "IM_spot_light_genglsl", "IM_directional_light_genglsl", + "IM_angle", "surfacematerial", "volumematerial", "ND_surfacematerial", "ND_volumematerial" }; } }; diff --git a/source/MaterialXTest/MaterialXGenShader/GenShaderUtil.cpp b/source/MaterialXTest/MaterialXGenShader/GenShaderUtil.cpp index 44971fb239..5d194a9128 100644 --- a/source/MaterialXTest/MaterialXGenShader/GenShaderUtil.cpp +++ b/source/MaterialXTest/MaterialXGenShader/GenShaderUtil.cpp @@ -8,6 +8,7 @@ #include //#include +#include #include #include @@ -761,6 +762,7 @@ void ShaderGeneratorTester::validate(const mx::GenOptions& generateOptions, cons mx::ShaderRefPtr shaderRef = targetElement->asA(); mx::NodePtr outputNode = targetElement->asA(); mx::NodeDefPtr nodeDef = nullptr; + bool containsNodedefToSkip = false; if (output) { outputNode = output->getConnectedNode(); @@ -768,6 +770,21 @@ void ShaderGeneratorTester::validate(const mx::GenOptions& generateOptions, cons if (outputNode->getType() != mx::MATERIAL_TYPE_STRING) { nodeDef = outputNode->getNodeDef(); + + // We sometimes have complex graph where the node we want to skip is upstream: + + for (mx::Edge edge : output->traverseGraph()) + { + mx::NodePtr upstreamNode = edge.getUpstreamElement()->asA(); + mx::NodeDefPtr upstreamNodeDef = upstreamNode->getNodeDef(); + const std::string upstreamNodeDefName = upstreamNodeDef->getName(); + if (_skipNodeDefs.count(upstreamNodeDefName)) + { + _logFile << ">> Contains skipped nodedef: " << upstreamNodeDefName << std::endl; + containsNodedefToSkip = true; + break; + } + } } } else if (shaderRef) @@ -791,7 +808,7 @@ void ShaderGeneratorTester::validate(const mx::GenOptions& generateOptions, cons { // Allow to skip nodedefs to test if specified const std::string nodeDefName = nodeDef->getName(); - if (_skipNodeDefs.count(nodeDefName)) + if (containsNodedefToSkip || _skipNodeDefs.count(nodeDefName)) { _logFile << ">> Skipped testing nodedef: " << nodeDefName << std::endl; continue; diff --git a/source/MaterialXTest/MaterialXRender/RenderUtil.cpp b/source/MaterialXTest/MaterialXRender/RenderUtil.cpp index 7a37fe4885..29adb848a4 100644 --- a/source/MaterialXTest/MaterialXRender/RenderUtil.cpp +++ b/source/MaterialXTest/MaterialXRender/RenderUtil.cpp @@ -161,6 +161,9 @@ bool ShaderRenderTester::validate(const mx::FilePathVec& testRootPaths, const mx // Add files to skip addSkipFiles(); + // Add nodedefs to skip + addSkipNodeDefs(); + // Library search path mx::FileSearchPath searchPath; searchPath.append(mx::FilePath::getCurrentPath() / mx::FilePath("libraries")); @@ -328,6 +331,28 @@ bool ShaderRenderTester::validate(const mx::FilePathVec& testRootPaths, const mx mx::ShaderRefPtr shaderRef = element->asA(); mx::NodePtr outputNode = element->asA(); + if (output) { + // Checks for unimplementable nodes: + bool containsNodedefToSkip = false; + for (mx::Edge edge : output->traverseGraph()) + { + mx::NodePtr upstreamNode = edge.getUpstreamElement()->asA(); + mx::NodeDefPtr upstreamNodeDef = upstreamNode->getNodeDef(); + const std::string upstreamNodeDefName = upstreamNodeDef->getName(); + if (_skipNodeDefs.count(upstreamNodeDefName)) + { + docValidLog << ">> Contains unimplementable nodedef: " << upstreamNodeDefName << std::endl; + containsNodedefToSkip = true; + break; + } + } + + if (containsNodedefToSkip) { + docValidLog << ">> Skipping: " << output->getName() << std::endl; + continue; + } + } + if (output) { outputNode = output->getConnectedNode(); diff --git a/source/MaterialXTest/MaterialXRender/RenderUtil.h b/source/MaterialXTest/MaterialXRender/RenderUtil.h index de62e232d5..d722eebf8c 100644 --- a/source/MaterialXTest/MaterialXRender/RenderUtil.h +++ b/source/MaterialXTest/MaterialXRender/RenderUtil.h @@ -179,6 +179,8 @@ class ShaderRenderTester _skipFiles.insert("light_compound_test.mtlx"); } + virtual void addSkipNodeDefs() {}; + // Load dependencies void loadDependentLibraries(GenShaderUtil::TestSuiteOptions options, mx::FileSearchPath searchPath, mx::DocumentPtr& dependLib); @@ -243,6 +245,9 @@ class ShaderRenderTester // Files to skip mx::StringSet _skipFiles; + + // Nodedefs to skip + mx::StringSet _skipNodeDefs; }; } // namespace RenderUtil diff --git a/source/MaterialXTest/MaterialXRenderGlsl/RenderGlsl.cpp b/source/MaterialXTest/MaterialXRenderGlsl/RenderGlsl.cpp index a94b76f195..2d5ff143b6 100644 --- a/source/MaterialXTest/MaterialXRenderGlsl/RenderGlsl.cpp +++ b/source/MaterialXTest/MaterialXRenderGlsl/RenderGlsl.cpp @@ -65,6 +65,11 @@ class GlslShaderRenderTester : public RenderUtil::ShaderRenderTester bool saveImage(const mx::FilePath& filePath, mx::ConstImagePtr image, bool verticalFlip) const override; + void addSkipNodeDefs() override + { + _skipNodeDefs.insert("ND_geompropvalue_boolean"); + _skipNodeDefs.insert("ND_geompropvalue_string"); + } bool canBake() const override { From 91194c7836e3983e27cb395afe03cc38997cde6b Mon Sep 17 00:00:00 2001 From: Jerry Gamache Date: Fri, 4 Dec 2020 10:01:48 -0500 Subject: [PATCH 3/6] Brought back boolean and string as uniform geompropvalues --- .../MaterialXGenGlsl/GlslShaderGenerator.cpp | 3 +- .../Nodes/GeomPropValueNodeGlsl.cpp | 38 +++++++++++++++++-- .../Nodes/GeomPropValueNodeGlsl.h | 11 ++++++ .../MaterialXTest/MaterialXGenGlsl/GenGlsl.h | 2 - .../MaterialXGenShader/GenShaderUtil.cpp | 19 +--------- .../MaterialXRender/RenderUtil.cpp | 25 ------------ .../MaterialXRender/RenderUtil.h | 5 --- .../MaterialXRenderGlsl/RenderGlsl.cpp | 6 --- 8 files changed, 49 insertions(+), 60 deletions(-) diff --git a/source/MaterialXGenGlsl/GlslShaderGenerator.cpp b/source/MaterialXGenGlsl/GlslShaderGenerator.cpp index ba73cca430..8663dffc1e 100644 --- a/source/MaterialXGenGlsl/GlslShaderGenerator.cpp +++ b/source/MaterialXGenGlsl/GlslShaderGenerator.cpp @@ -219,8 +219,9 @@ GlslShaderGenerator::GlslShaderGenerator() : registerImplementation("IM_geomcolor_color3_" + GlslShaderGenerator::LANGUAGE, GeomColorNodeGlsl::create); registerImplementation("IM_geomcolor_color4_" + GlslShaderGenerator::LANGUAGE, GeomColorNodeGlsl::create); // - // String and boolean are invalid as varying parameters. registerImplementation("IM_geompropvalue_integer_" + GlslShaderGenerator::LANGUAGE, GeomPropValueNodeGlsl::create); + registerImplementation("IM_geompropvalue_boolean_" + GlslShaderGenerator::LANGUAGE, GeomPropValueNodeGlsl_asUniform::create); + registerImplementation("IM_geompropvalue_string_" + GlslShaderGenerator::LANGUAGE, GeomPropValueNodeGlsl_asUniform::create); registerImplementation("IM_geompropvalue_float_" + GlslShaderGenerator::LANGUAGE, GeomPropValueNodeGlsl::create); registerImplementation("IM_geompropvalue_color2_" + GlslShaderGenerator::LANGUAGE, GeomPropValueNodeGlsl::create); registerImplementation("IM_geompropvalue_color3_" + GlslShaderGenerator::LANGUAGE, GeomPropValueNodeGlsl::create); diff --git a/source/MaterialXGenGlsl/Nodes/GeomPropValueNodeGlsl.cpp b/source/MaterialXGenGlsl/Nodes/GeomPropValueNodeGlsl.cpp index 2276c78afd..377e75061a 100644 --- a/source/MaterialXGenGlsl/Nodes/GeomPropValueNodeGlsl.cpp +++ b/source/MaterialXGenGlsl/Nodes/GeomPropValueNodeGlsl.cpp @@ -24,9 +24,6 @@ void GeomPropValueNodeGlsl::createVariables(const ShaderNode& node, GenContext&, } const string geomProp = geomPropInput->getValue()->getValueString(); const ShaderOutput* output = node.getOutput(); - if (output->getType() == Type::BOOLEAN || output->getType() == Type::STRING) { - throw ExceptionShaderGenError("GLSL: unsupported type for geompropvalue node '" + node.getName() + "', can not generate code."); - } ShaderStage& vs = shader.getStage(Stage::VERTEX); ShaderStage& ps = shader.getStage(Stage::PIXEL); @@ -69,4 +66,39 @@ void GeomPropValueNodeGlsl::emitFunctionCall(const ShaderNode& node, GenContext& END_SHADER_STAGE(shader, Stage::PIXEL) } +ShaderNodeImplPtr GeomPropValueNodeGlsl_asUniform::create() +{ + return std::make_shared(); +} + +void GeomPropValueNodeGlsl_asUniform::createVariables(const ShaderNode& node, GenContext&, Shader& shader) const +{ + const ShaderInput* geomPropInput = node.getInput(GEOMPROP); + if (!geomPropInput || !geomPropInput->getValue()) + { + throw ExceptionShaderGenError("No 'geomprop' parameter found on geompropvalue node '" + node.getName() + "', don't know what property to bind"); + } + const string geomProp = geomPropInput->getValue()->getValueString(); + ShaderStage& ps = shader.getStage(Stage::PIXEL); + ShaderPort* uniform = addStageUniform(HW::PRIVATE_UNIFORMS, node.getOutput()->getType(), HW::T_GEOMPROP + "_" + geomProp, ps); + uniform->setPath(geomPropInput->getPath()); +} + +void GeomPropValueNodeGlsl_asUniform::emitFunctionCall(const ShaderNode& node, GenContext& context, ShaderStage& stage) const +{ + BEGIN_SHADER_STAGE(stage, Stage::PIXEL) + const ShaderGenerator& shadergen = context.getShaderGenerator(); + const ShaderInput* geomPropInput = node.getInput(GEOMPROP); + if (!geomPropInput) + { + throw ExceptionShaderGenError("No 'geomprop' parameter found on geompropvalue node '" + node.getName() + "', don't know what property to bind"); + } + const string attrName = geomPropInput->getValue()->getValueString(); + shadergen.emitLineBegin(stage); + shadergen.emitOutput(node.getOutput(), true, false, context, stage); + shadergen.emitString(" = " + HW::T_GEOMPROP + "_" + attrName, stage); + shadergen.emitLineEnd(stage); + END_SHADER_STAGE(shader, Stage::PIXEL) +} + } // namespace MaterialX diff --git a/source/MaterialXGenGlsl/Nodes/GeomPropValueNodeGlsl.h b/source/MaterialXGenGlsl/Nodes/GeomPropValueNodeGlsl.h index 64c9882f1f..c1acea3bce 100644 --- a/source/MaterialXGenGlsl/Nodes/GeomPropValueNodeGlsl.h +++ b/source/MaterialXGenGlsl/Nodes/GeomPropValueNodeGlsl.h @@ -24,6 +24,17 @@ class GeomPropValueNodeGlsl : public GlslImplementation bool isEditable(const ShaderInput& /*input*/) const override { return false; } }; +/// GeomPropValue node non-implementation for GLSL +class GeomPropValueNodeGlsl_asUniform : public GlslImplementation +{ +public: + static ShaderNodeImplPtr create(); + + void createVariables(const ShaderNode& node, GenContext& context, Shader& shader) const override; + + void emitFunctionCall(const ShaderNode& node, GenContext& context, ShaderStage& stage) const override; +}; + } // namespace MaterialX #endif diff --git a/source/MaterialXTest/MaterialXGenGlsl/GenGlsl.h b/source/MaterialXTest/MaterialXGenGlsl/GenGlsl.h index 4b24856262..57c28c0c98 100644 --- a/source/MaterialXTest/MaterialXGenGlsl/GenGlsl.h +++ b/source/MaterialXTest/MaterialXGenGlsl/GenGlsl.h @@ -35,8 +35,6 @@ class GlslShaderGeneratorTester : public GenShaderUtil::ShaderGeneratorTester { _skipNodeDefs.insert("ND_displacement_float"); _skipNodeDefs.insert("ND_displacement_vector3"); - _skipNodeDefs.insert("ND_geompropvalue_boolean"); - _skipNodeDefs.insert("ND_geompropvalue_string"); ParentClass::addSkipNodeDefs(); } diff --git a/source/MaterialXTest/MaterialXGenShader/GenShaderUtil.cpp b/source/MaterialXTest/MaterialXGenShader/GenShaderUtil.cpp index 5d194a9128..44971fb239 100644 --- a/source/MaterialXTest/MaterialXGenShader/GenShaderUtil.cpp +++ b/source/MaterialXTest/MaterialXGenShader/GenShaderUtil.cpp @@ -8,7 +8,6 @@ #include //#include -#include #include #include @@ -762,7 +761,6 @@ void ShaderGeneratorTester::validate(const mx::GenOptions& generateOptions, cons mx::ShaderRefPtr shaderRef = targetElement->asA(); mx::NodePtr outputNode = targetElement->asA(); mx::NodeDefPtr nodeDef = nullptr; - bool containsNodedefToSkip = false; if (output) { outputNode = output->getConnectedNode(); @@ -770,21 +768,6 @@ void ShaderGeneratorTester::validate(const mx::GenOptions& generateOptions, cons if (outputNode->getType() != mx::MATERIAL_TYPE_STRING) { nodeDef = outputNode->getNodeDef(); - - // We sometimes have complex graph where the node we want to skip is upstream: - - for (mx::Edge edge : output->traverseGraph()) - { - mx::NodePtr upstreamNode = edge.getUpstreamElement()->asA(); - mx::NodeDefPtr upstreamNodeDef = upstreamNode->getNodeDef(); - const std::string upstreamNodeDefName = upstreamNodeDef->getName(); - if (_skipNodeDefs.count(upstreamNodeDefName)) - { - _logFile << ">> Contains skipped nodedef: " << upstreamNodeDefName << std::endl; - containsNodedefToSkip = true; - break; - } - } } } else if (shaderRef) @@ -808,7 +791,7 @@ void ShaderGeneratorTester::validate(const mx::GenOptions& generateOptions, cons { // Allow to skip nodedefs to test if specified const std::string nodeDefName = nodeDef->getName(); - if (containsNodedefToSkip || _skipNodeDefs.count(nodeDefName)) + if (_skipNodeDefs.count(nodeDefName)) { _logFile << ">> Skipped testing nodedef: " << nodeDefName << std::endl; continue; diff --git a/source/MaterialXTest/MaterialXRender/RenderUtil.cpp b/source/MaterialXTest/MaterialXRender/RenderUtil.cpp index 29adb848a4..7a37fe4885 100644 --- a/source/MaterialXTest/MaterialXRender/RenderUtil.cpp +++ b/source/MaterialXTest/MaterialXRender/RenderUtil.cpp @@ -161,9 +161,6 @@ bool ShaderRenderTester::validate(const mx::FilePathVec& testRootPaths, const mx // Add files to skip addSkipFiles(); - // Add nodedefs to skip - addSkipNodeDefs(); - // Library search path mx::FileSearchPath searchPath; searchPath.append(mx::FilePath::getCurrentPath() / mx::FilePath("libraries")); @@ -331,28 +328,6 @@ bool ShaderRenderTester::validate(const mx::FilePathVec& testRootPaths, const mx mx::ShaderRefPtr shaderRef = element->asA(); mx::NodePtr outputNode = element->asA(); - if (output) { - // Checks for unimplementable nodes: - bool containsNodedefToSkip = false; - for (mx::Edge edge : output->traverseGraph()) - { - mx::NodePtr upstreamNode = edge.getUpstreamElement()->asA(); - mx::NodeDefPtr upstreamNodeDef = upstreamNode->getNodeDef(); - const std::string upstreamNodeDefName = upstreamNodeDef->getName(); - if (_skipNodeDefs.count(upstreamNodeDefName)) - { - docValidLog << ">> Contains unimplementable nodedef: " << upstreamNodeDefName << std::endl; - containsNodedefToSkip = true; - break; - } - } - - if (containsNodedefToSkip) { - docValidLog << ">> Skipping: " << output->getName() << std::endl; - continue; - } - } - if (output) { outputNode = output->getConnectedNode(); diff --git a/source/MaterialXTest/MaterialXRender/RenderUtil.h b/source/MaterialXTest/MaterialXRender/RenderUtil.h index d722eebf8c..de62e232d5 100644 --- a/source/MaterialXTest/MaterialXRender/RenderUtil.h +++ b/source/MaterialXTest/MaterialXRender/RenderUtil.h @@ -179,8 +179,6 @@ class ShaderRenderTester _skipFiles.insert("light_compound_test.mtlx"); } - virtual void addSkipNodeDefs() {}; - // Load dependencies void loadDependentLibraries(GenShaderUtil::TestSuiteOptions options, mx::FileSearchPath searchPath, mx::DocumentPtr& dependLib); @@ -245,9 +243,6 @@ class ShaderRenderTester // Files to skip mx::StringSet _skipFiles; - - // Nodedefs to skip - mx::StringSet _skipNodeDefs; }; } // namespace RenderUtil diff --git a/source/MaterialXTest/MaterialXRenderGlsl/RenderGlsl.cpp b/source/MaterialXTest/MaterialXRenderGlsl/RenderGlsl.cpp index 2d5ff143b6..4852f005b8 100644 --- a/source/MaterialXTest/MaterialXRenderGlsl/RenderGlsl.cpp +++ b/source/MaterialXTest/MaterialXRenderGlsl/RenderGlsl.cpp @@ -65,12 +65,6 @@ class GlslShaderRenderTester : public RenderUtil::ShaderRenderTester bool saveImage(const mx::FilePath& filePath, mx::ConstImagePtr image, bool verticalFlip) const override; - void addSkipNodeDefs() override - { - _skipNodeDefs.insert("ND_geompropvalue_boolean"); - _skipNodeDefs.insert("ND_geompropvalue_string"); - } - bool canBake() const override { return true; From c41d65ac90b1520e1e28b772bd080bf2742dd73c Mon Sep 17 00:00:00 2001 From: Jerry Gamache Date: Fri, 4 Dec 2020 10:13:52 -0500 Subject: [PATCH 4/6] Fix bad grammar. --- source/MaterialXGenGlsl/Nodes/GeomPropValueNodeGlsl.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/source/MaterialXGenGlsl/Nodes/GeomPropValueNodeGlsl.cpp b/source/MaterialXGenGlsl/Nodes/GeomPropValueNodeGlsl.cpp index 377e75061a..bfef4b6521 100644 --- a/source/MaterialXGenGlsl/Nodes/GeomPropValueNodeGlsl.cpp +++ b/source/MaterialXGenGlsl/Nodes/GeomPropValueNodeGlsl.cpp @@ -20,7 +20,7 @@ void GeomPropValueNodeGlsl::createVariables(const ShaderNode& node, GenContext&, const ShaderInput* geomPropInput = node.getInput(GEOMPROP); if (!geomPropInput || !geomPropInput->getValue()) { - throw ExceptionShaderGenError("No 'geomprop' parameter found on geompropvalue node '" + node.getName() + "', don't know what property to bind"); + throw ExceptionShaderGenError("No 'geomprop' parameter found on geompropvalue node '" + node.getName() + "'. Don't know what property to bind"); } const string geomProp = geomPropInput->getValue()->getValueString(); const ShaderOutput* output = node.getOutput(); @@ -39,7 +39,7 @@ void GeomPropValueNodeGlsl::emitFunctionCall(const ShaderNode& node, GenContext& const ShaderInput* geomPropInput = node.getInput(GEOMPROP); if (!geomPropInput) { - throw ExceptionShaderGenError("No 'geomprop' parameter found on geompropvalue node '" + node.getName() + "', don't know what property to bind"); + throw ExceptionShaderGenError("No 'geomprop' parameter found on geompropvalue node '" + node.getName() + "'. Don't know what property to bind"); } const string geomname = geomPropInput->getValue()->getValueString(); const string variable = HW::T_GEOMPROP + "_" + geomname; @@ -76,7 +76,7 @@ void GeomPropValueNodeGlsl_asUniform::createVariables(const ShaderNode& node, Ge const ShaderInput* geomPropInput = node.getInput(GEOMPROP); if (!geomPropInput || !geomPropInput->getValue()) { - throw ExceptionShaderGenError("No 'geomprop' parameter found on geompropvalue node '" + node.getName() + "', don't know what property to bind"); + throw ExceptionShaderGenError("No 'geomprop' parameter found on geompropvalue node '" + node.getName() + "'. Don't know what property to bind"); } const string geomProp = geomPropInput->getValue()->getValueString(); ShaderStage& ps = shader.getStage(Stage::PIXEL); @@ -91,7 +91,7 @@ void GeomPropValueNodeGlsl_asUniform::emitFunctionCall(const ShaderNode& node, G const ShaderInput* geomPropInput = node.getInput(GEOMPROP); if (!geomPropInput) { - throw ExceptionShaderGenError("No 'geomprop' parameter found on geompropvalue node '" + node.getName() + "', don't know what property to bind"); + throw ExceptionShaderGenError("No 'geomprop' parameter found on geompropvalue node '" + node.getName() + "'. Don't know what property to bind"); } const string attrName = geomPropInput->getValue()->getValueString(); shadergen.emitLineBegin(stage); From 2746664ac34a3dc3e6323e891e7e6026217c2cf7 Mon Sep 17 00:00:00 2001 From: Jerry Gamache Date: Fri, 4 Dec 2020 15:40:04 -0500 Subject: [PATCH 5/6] Implemented varying stream unit tests The naming of geompropvalue inputs will now reflect the uniform vs varying nature. --- .../stdlib/geometric/geompropvalue.mtlx | 8 +- .../MaterialXGenGlsl/GlslShaderGenerator.cpp | 2 +- .../Nodes/GeomPropValueNodeGlsl.cpp | 4 +- source/MaterialXGenOgsXml/OgsXmlGenerator.cpp | 2 +- source/MaterialXRenderGlsl/GlslProgram.cpp | 15 +- .../MaterialXRenderGlsl/RenderGlsl.cpp | 140 ++++++++++++++++++ 6 files changed, 161 insertions(+), 10 deletions(-) diff --git a/resources/Materials/TestSuite/stdlib/geometric/geompropvalue.mtlx b/resources/Materials/TestSuite/stdlib/geometric/geompropvalue.mtlx index 2936e4b956..47038e43c8 100644 --- a/resources/Materials/TestSuite/stdlib/geometric/geompropvalue.mtlx +++ b/resources/Materials/TestSuite/stdlib/geometric/geompropvalue.mtlx @@ -4,10 +4,10 @@ - - - - + + + + diff --git a/source/MaterialXGenGlsl/GlslShaderGenerator.cpp b/source/MaterialXGenGlsl/GlslShaderGenerator.cpp index 8663dffc1e..e59e9334fc 100644 --- a/source/MaterialXGenGlsl/GlslShaderGenerator.cpp +++ b/source/MaterialXGenGlsl/GlslShaderGenerator.cpp @@ -755,7 +755,7 @@ void GlslShaderGenerator::emitVariableDeclaration(const ShaderPort* variable, co string str = qualifier.empty() ? EMPTY_STRING : qualifier + " "; // Varying parameters of type int must be flat qualified on output from vertex stage and // input to pixel stage. The only way to get these is with geompropvalue_integer nodes. - if (qualifier.empty() && variable->getType() == Type::INTEGER && !assignValue && variable->getName().rfind(HW::T_GEOMPROP, 0) == 0) { + if (qualifier.empty() && variable->getType() == Type::INTEGER && !assignValue && variable->getName().rfind(HW::T_IN_GEOMPROP, 0) == 0) { str += GlslSyntax::FLAT_QUALIFIER + " "; } str += _syntax->getTypeName(variable->getType()) + " " + variable->getVariable(); diff --git a/source/MaterialXGenGlsl/Nodes/GeomPropValueNodeGlsl.cpp b/source/MaterialXGenGlsl/Nodes/GeomPropValueNodeGlsl.cpp index bfef4b6521..e2761a60ed 100644 --- a/source/MaterialXGenGlsl/Nodes/GeomPropValueNodeGlsl.cpp +++ b/source/MaterialXGenGlsl/Nodes/GeomPropValueNodeGlsl.cpp @@ -29,7 +29,7 @@ void GeomPropValueNodeGlsl::createVariables(const ShaderNode& node, GenContext&, ShaderStage& ps = shader.getStage(Stage::PIXEL); addStageInput(HW::VERTEX_INPUTS, output->getType(), HW::T_IN_GEOMPROP + "_" + geomProp, vs); - addStageConnector(HW::VERTEX_DATA, output->getType(), HW::T_GEOMPROP + "_" + geomProp, vs, ps); + addStageConnector(HW::VERTEX_DATA, output->getType(), HW::T_IN_GEOMPROP + "_" + geomProp, vs, ps); } void GeomPropValueNodeGlsl::emitFunctionCall(const ShaderNode& node, GenContext& context, ShaderStage& stage) const @@ -42,7 +42,7 @@ void GeomPropValueNodeGlsl::emitFunctionCall(const ShaderNode& node, GenContext& throw ExceptionShaderGenError("No 'geomprop' parameter found on geompropvalue node '" + node.getName() + "'. Don't know what property to bind"); } const string geomname = geomPropInput->getValue()->getValueString(); - const string variable = HW::T_GEOMPROP + "_" + geomname; + const string variable = HW::T_IN_GEOMPROP + "_" + geomname; BEGIN_SHADER_STAGE(stage, Stage::VERTEX) VariableBlock& vertexData = stage.getOutputBlock(HW::VERTEX_DATA); diff --git a/source/MaterialXGenOgsXml/OgsXmlGenerator.cpp b/source/MaterialXGenOgsXml/OgsXmlGenerator.cpp index 118dcb02bc..89137e6b2a 100644 --- a/source/MaterialXGenOgsXml/OgsXmlGenerator.cpp +++ b/source/MaterialXGenOgsXml/OgsXmlGenerator.cpp @@ -87,7 +87,7 @@ namespace { { "texcoord_", "mayaUvCoordSemantic" }, { "color_", "colorset" }, - { "u_geomprop_", "mayaUvCoordSemantic" } + { "i_geomprop_", "mayaUvCoordSemantic" } }; namespace OgsParameterFlags diff --git a/source/MaterialXRenderGlsl/GlslProgram.cpp b/source/MaterialXRenderGlsl/GlslProgram.cpp index 665305a6b8..a21a025f6e 100644 --- a/source/MaterialXRenderGlsl/GlslProgram.cpp +++ b/source/MaterialXRenderGlsl/GlslProgram.cpp @@ -368,7 +368,11 @@ void GlslProgram::bindAttribute(const GlslProgram::InputMap& inputs, MeshPtr mes glEnableVertexAttribArray(location); _enabledStreamLocations.insert(location); - glVertexAttribPointer(location, stride, GL_FLOAT, GL_FALSE, 0, nullptr); + if (input.second->gltype != GL_INT) { + glVertexAttribPointer(location, stride, GL_FLOAT, GL_FALSE, 0, nullptr); + } else { + glVertexAttribIPointer(location, stride, GL_INT, 0, nullptr); + } } } @@ -463,7 +467,14 @@ void GlslProgram::bindStreams(MeshPtr mesh) bindAttribute(foundList, mesh); } - // Bind any named geometric property information + // Bind any named varying geometric property information + findInputs(HW::IN_GEOMPROP + "_", attributeList, foundList, false); + if (foundList.size()) + { + bindAttribute(foundList, mesh); + } + + // Bind any named uniform geometric property information const GlslProgram::InputMap& uniformList = getUniformsList(); findInputs(HW::GEOMPROP + "_", uniformList, foundList, false); for (const auto& input : foundList) diff --git a/source/MaterialXTest/MaterialXRenderGlsl/RenderGlsl.cpp b/source/MaterialXTest/MaterialXRenderGlsl/RenderGlsl.cpp index 4852f005b8..c28d09cb89 100644 --- a/source/MaterialXTest/MaterialXRenderGlsl/RenderGlsl.cpp +++ b/source/MaterialXTest/MaterialXRenderGlsl/RenderGlsl.cpp @@ -209,12 +209,107 @@ void addAdditionalTestStreams(mx::MeshPtr mesh) mesh->addStream(colorStream2); } + const std::string GEOM_INT_STREAM_NAME("i_" + mx::MeshStream::GEOMETRY_PROPERTY_ATTRIBUTE + "_geompropvalue_integer"); + int32_t* geomIntData = nullptr; + if (!mesh->getStream(GEOM_INT_STREAM_NAME)) + { + mx::MeshStreamPtr geomIntStream = mx::MeshStream::create(GEOM_INT_STREAM_NAME, mx::MeshStream::GEOMETRY_PROPERTY_ATTRIBUTE, 0); + geomIntStream->setStride(1); + geomIntStream->getData().resize(vertexCount); + mesh->addStream(geomIntStream); + // Float and int32 have same size. + geomIntData = reinterpret_cast(geomIntStream->getData().data()); + } + + const std::string GEOM_FLOAT_STREAM_NAME("i_" + mx::MeshStream::GEOMETRY_PROPERTY_ATTRIBUTE + "_geompropvalue_float"); + mx::MeshFloatBuffer* geomFloatData = nullptr; + if (!mesh->getStream(GEOM_FLOAT_STREAM_NAME)) + { + mx::MeshStreamPtr geomFloatStream = mx::MeshStream::create(GEOM_FLOAT_STREAM_NAME, mx::MeshStream::GEOMETRY_PROPERTY_ATTRIBUTE, 1); + geomFloatData = &(geomFloatStream->getData()); + geomFloatStream->setStride(1); + geomFloatData->resize(vertexCount); + mesh->addStream(geomFloatStream); + } + + const std::string GEOM_VECTOR2_STREAM_NAME("i_" + mx::MeshStream::GEOMETRY_PROPERTY_ATTRIBUTE + "_geompropvalue_vector2"); + mx::MeshFloatBuffer* geomVector2Data = nullptr; + if (!mesh->getStream(GEOM_VECTOR2_STREAM_NAME)) + { + mx::MeshStreamPtr geomVector2Stream = mx::MeshStream::create(GEOM_VECTOR2_STREAM_NAME, mx::MeshStream::GEOMETRY_PROPERTY_ATTRIBUTE, 1); + geomVector2Data = &(geomVector2Stream->getData()); + geomVector2Stream->setStride(2); + geomVector2Data->resize(vertexCount * 2); + mesh->addStream(geomVector2Stream); + } + + const std::string GEOM_VECTOR3_STREAM_NAME("i_" + mx::MeshStream::GEOMETRY_PROPERTY_ATTRIBUTE + "_geompropvalue_vector3"); + mx::MeshFloatBuffer* geomVector3Data = nullptr; + if (!mesh->getStream(GEOM_VECTOR3_STREAM_NAME)) + { + mx::MeshStreamPtr geomVector3Stream = mx::MeshStream::create(GEOM_VECTOR3_STREAM_NAME, mx::MeshStream::GEOMETRY_PROPERTY_ATTRIBUTE, 1); + geomVector3Data = &(geomVector3Stream->getData()); + geomVector3Stream->setStride(3); + geomVector3Data->resize(vertexCount * 3); + mesh->addStream(geomVector3Stream); + } + + const std::string GEOM_VECTOR4_STREAM_NAME("i_" + mx::MeshStream::GEOMETRY_PROPERTY_ATTRIBUTE + "_geompropvalue_vector4"); + mx::MeshFloatBuffer* geomVector4Data = nullptr; + if (!mesh->getStream(GEOM_VECTOR4_STREAM_NAME)) + { + mx::MeshStreamPtr geomVector4Stream = mx::MeshStream::create(GEOM_VECTOR4_STREAM_NAME, mx::MeshStream::GEOMETRY_PROPERTY_ATTRIBUTE, 1); + geomVector4Data = &(geomVector4Stream->getData()); + geomVector4Stream->setStride(4); + geomVector4Data->resize(vertexCount * 4); + mesh->addStream(geomVector4Stream); + } + + const std::string GEOM_COLOR2_STREAM_NAME("i_" + mx::MeshStream::GEOMETRY_PROPERTY_ATTRIBUTE + "_geompropvalue_color2"); + mx::MeshFloatBuffer* geomColor2Data = nullptr; + if (!mesh->getStream(GEOM_COLOR2_STREAM_NAME)) + { + mx::MeshStreamPtr geomColor2Stream = mx::MeshStream::create(GEOM_COLOR2_STREAM_NAME, mx::MeshStream::GEOMETRY_PROPERTY_ATTRIBUTE, 1); + geomColor2Data = &(geomColor2Stream->getData()); + geomColor2Stream->setStride(2); + geomColor2Data->resize(vertexCount * 2); + mesh->addStream(geomColor2Stream); + } + + const std::string GEOM_COLOR3_STREAM_NAME("i_" + mx::MeshStream::GEOMETRY_PROPERTY_ATTRIBUTE + "_geompropvalue_color3"); + mx::MeshFloatBuffer* geomColor3Data = nullptr; + if (!mesh->getStream(GEOM_COLOR3_STREAM_NAME)) + { + mx::MeshStreamPtr geomColor3Stream = mx::MeshStream::create(GEOM_COLOR3_STREAM_NAME, mx::MeshStream::GEOMETRY_PROPERTY_ATTRIBUTE, 1); + geomColor3Data = &(geomColor3Stream->getData()); + geomColor3Stream->setStride(3); + geomColor3Data->resize(vertexCount * 3); + mesh->addStream(geomColor3Stream); + } + + const std::string GEOM_COLOR4_STREAM_NAME("i_" + mx::MeshStream::GEOMETRY_PROPERTY_ATTRIBUTE + "_geompropvalue_color4"); + mx::MeshFloatBuffer* geomColor4Data = nullptr; + if (!mesh->getStream(GEOM_COLOR4_STREAM_NAME)) + { + mx::MeshStreamPtr geomColor4Stream = mx::MeshStream::create(GEOM_COLOR4_STREAM_NAME, mx::MeshStream::GEOMETRY_PROPERTY_ATTRIBUTE, 1); + geomColor4Data = &(geomColor4Stream->getData()); + geomColor4Stream->setStride(4); + geomColor4Data->resize(vertexCount * 4); + mesh->addStream(geomColor4Stream); + } + + const float PI = std::acosf(-1.0f); + auto sineData = [&](auto uv, auto freq){ + float angle = uv * 2 * PI * freq; + return std::sinf(angle) / 2.0f + 1.0f; + }; if (!uv.empty()) { for (size_t i = 0; i < vertexCount; i++) { const size_t i2 = 2 * i; const size_t i21 = i2 + 1; + const size_t i3 = 3 * i; const size_t i4 = 4 * i; // Fake second set of texture coordinates @@ -238,6 +333,51 @@ void addAdditionalTestStreams(mx::MeshPtr mesh) (*colorData2)[i4 + 2] = uv[i21]; (*colorData2)[i4 + 3] = 1.0f; } + if (geomIntData) + { + geomIntData[i] = static_cast(uv[i21] * 5); + } + if (geomFloatData) + { + (*geomFloatData)[i] = sineData(uv[i21], 12.0f); + } + if (geomVector2Data) + { + (*geomVector2Data)[i2] = sineData(uv[i21], 6.0f); + (*geomVector2Data)[i21] = 0.0f; + } + if (geomVector3Data) + { + (*geomVector3Data)[i3] = 0.0f; + (*geomVector3Data)[i3 + 1] = sineData(uv[i21], 8.0f); + (*geomVector3Data)[i3 + 2] = 0.0f; + } + if (geomVector4Data) + { + (*geomVector4Data)[i4] = 0.0f; + (*geomVector4Data)[i4 + 1] = 0.0f; + (*geomVector4Data)[i4 + 2] = sineData(uv[i21], 10.0f); + (*geomVector4Data)[i4 + 3] = 1.0f; + } + + if (geomColor2Data) + { + (*geomColor2Data)[i2] = sineData(uv[i2], 10.0f); + (*geomColor2Data)[i21] = 0.0f; + } + if (geomColor3Data) + { + (*geomColor3Data)[i3] = 0.0f; + (*geomColor3Data)[i3 + 1] = sineData(uv[i2], 8.0f); + (*geomColor3Data)[i3 + 2] = 0.0f; + } + if (geomColor4Data) + { + (*geomColor4Data)[i4] = 0.0f; + (*geomColor4Data)[i4 + 1] = 0.0f; + (*geomColor4Data)[i4 + 2] = sineData(uv[i2], 6.0f); + (*geomColor4Data)[i4 + 3] = 1.0f; + } } } } From 9708a4f03a140b1ef8eb68e409199b7ae71fd5ac Mon Sep 17 00:00:00 2001 From: Jerry Gamache Date: Fri, 4 Dec 2020 16:22:54 -0500 Subject: [PATCH 6/6] Fix Linux and OS/X builds --- source/MaterialXTest/MaterialXRenderGlsl/RenderGlsl.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/source/MaterialXTest/MaterialXRenderGlsl/RenderGlsl.cpp b/source/MaterialXTest/MaterialXRenderGlsl/RenderGlsl.cpp index c28d09cb89..05b9e5bc81 100644 --- a/source/MaterialXTest/MaterialXRenderGlsl/RenderGlsl.cpp +++ b/source/MaterialXTest/MaterialXRenderGlsl/RenderGlsl.cpp @@ -28,6 +28,8 @@ #include #include +#include + namespace mx = MaterialX; // @@ -298,10 +300,10 @@ void addAdditionalTestStreams(mx::MeshPtr mesh) mesh->addStream(geomColor4Stream); } - const float PI = std::acosf(-1.0f); - auto sineData = [&](auto uv, auto freq){ + auto sineData = [](float uv, float freq){ + const float PI = std::acos(-1.0f); float angle = uv * 2 * PI * freq; - return std::sinf(angle) / 2.0f + 1.0f; + return std::sin(angle) / 2.0f + 1.0f; }; if (!uv.empty()) {