Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor TypeDesc and StructTypeDesc storage #2107

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ jobs:
compiler: xcode
compiler_version: "14.3"
python: 3.11
test_shaders: ON
static_analysis: ON
cmake_config: -DCMAKE_EXPORT_COMPILE_COMMANDS=ON

Expand Down Expand Up @@ -229,12 +230,14 @@ jobs:
run: |
vcpkg/vcpkg install glslang --triplet=x64-windows
glslangValidator.exe -v
python python/Scripts/generateshader.py resources/Materials/TestSuite/stdlib/structs --target glsl --validator glslangValidator.exe
python python/Scripts/generateshader.py resources/Materials/Examples --target glsl --validator glslangValidator.exe
python python/Scripts/generateshader.py resources/Materials/Examples --target essl --validator glslangValidator.exe

- name: Shader Validation Tests (MacOS)
if: matrix.test_shaders == 'ON' && runner.os == 'macOS'
run: |
python python/Scripts/generateshader.py resources/Materials/TestSuite/stdlib/structs --target msl --validator "xcrun metal --language=metal" --validatorArgs="-w"
python python/Scripts/generateshader.py resources/Materials/Examples --target msl --validator "xcrun metal --language=metal" --validatorArgs="-w"
python python/Scripts/generateshader.py resources/Materials/TestSuite/stdlib --target msl --validator "xcrun metal --language=metal" --validatorArgs="-w"

Expand Down
1 change: 1 addition & 0 deletions python/MaterialXTest/genshader.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def test_ShaderInterface(self):
self.assertTrue(foundTarget)
context = mx_gen_shader.GenContext(shadergen)
context.registerSourceCodeSearchPath(searchPath)
shadergen.registerTypeDefs(doc, context)

# Test generator with complete mode
context.getOptions().shaderInterfaceType = mx_gen_shader.ShaderInterfaceType.SHADER_INTERFACE_COMPLETE;
Expand Down
1 change: 1 addition & 0 deletions python/Scripts/generateshader.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ def main():
codeSearchPath.append(os.path.dirname(inputFilename))
context = mx_gen_shader.GenContext(shadergen)
context.registerSourceCodeSearchPath(codeSearchPath)
shadergen.registerTypeDefs(doc, context)

# If we're generating Vulkan-compliant GLSL then set the binding context
if opts.vulkanCompliantGlsl:
Expand Down
39 changes: 39 additions & 0 deletions resources/Materials/TestSuite/stdlib/structs/struct_texcoord.mtlx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0"?>
<materialx version="1.39">

<!-- TODO - Decide if perhaps this structs test folder belongs in a different parent folder
its not really testing any stdlib node.
Its more like testing Core MaterialX features -->

<typedef name="texcoord_struct">
<member name="ss" type="float" value="0.5"/>
<member name="tt" type="float" value="0.5"/>
</typedef>

<nodedef name="ND_extract_s_texcoord" node="extracts" nodegroup="shader" >
<input name="in" type="texcoord_struct" value="{0.1;0.1}" />
<output name="out" type="float" value="0.0" />
</nodedef>

<implementation name="IM_extract_s_texcoord_genmsl" nodedef="ND_extract_s_texcoord" target="genmsl" sourcecode="{{in}}.ss" />
<implementation name="IM_extract_s_texcoord_genosl" nodedef="ND_extract_s_texcoord" target="genosl" sourcecode="{{in}}.ss" />
<implementation name="IM_extract_s_texcoord_genmdl" nodedef="ND_extract_s_texcoord" target="genmdl" sourcecode="{{in}}.ss" />
<implementation name="IM_extract_s_texcoord_genglsl" nodedef="ND_extract_s_texcoord" target="genglsl" sourcecode="{{in}}.ss" />

<nodegraph name="test_struct_texcoord">
<extracts name="extracts" type="float">
<input name="in" type="texcoord_struct" value="{0.01;0.5}"/>
</extracts>
<convert name="float_to_color3" type="color3">
<input name="in" type="float" nodename="extracts"/>
</convert>
<oren_nayar_diffuse_bsdf name="diffuse_brdf1" type="BSDF">
<input name="color" type="color3" nodename="float_to_color3" />
</oren_nayar_diffuse_bsdf>
<surface name="surface1" type="surfaceshader">
<input name="bsdf" type="BSDF" nodename="diffuse_brdf1" />
<input name="opacity" type="float" value="1.0" />
</surface>
<output name="out" type="surfaceshader" nodename="surface1" />
</nodegraph>
</materialx>
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0"?>
<materialx version="1.38">

<typedef name="texcoord_struct">
<member name="ss" type="float" value="0.5"/>
<member name="tt" type="float" value="0.5"/>
</typedef>
<typedef name="texcoordGroup_struct">
<member name="st_0" type="texcoord_struct" value="{0.1;0.1}"/>
<member name="st_1" type="texcoord_struct" value="{0.5;0.5}"/>
<member name="st_2" type="texcoord_struct" value="{0.9;0.9}"/>
</typedef>

<nodedef name="ND_extract_first_s_texcoordGroup" node="extractfirstsgroup" nodegroup="shader" >
<input name="in" type="texcoordGroup_struct" value="{{0.1;0.2};{0.3;0.4};{0.5;0.6}}"/>
<output name="out" type="float" value="0.0" />
</nodedef>

<implementation name="IM_extract_first_s_texcoordGroup_genglsl" nodedef="ND_extract_first_s_texcoordGroup" target="genglsl" sourcecode="{{in}}.st_0.ss">
<input name="in" type="texcoordGroup_struct" implname="data" />
</implementation>
<implementation name="IM_extract_first_s_texcoordGroup_genmdl" nodedef="ND_extract_first_s_texcoordGroup" target="genmdl" sourcecode="{{in}}.st_0.ss">
<input name="in" type="texcoordGroup_struct" implname="data" />
</implementation>
<implementation name="IM_extract_first_s_texcoordGroup_genmsl" nodedef="ND_extract_first_s_texcoordGroup" target="genmsl" sourcecode="{{in}}.st_0.ss">
<input name="in" type="texcoordGroup_struct" implname="data" />
</implementation>
<implementation name="IM_extract_first_s_texcoordGroup_genosl" nodedef="ND_extract_first_s_texcoordGroup" target="genosl" sourcecode="{{in}}.st_0.ss">
<input name="in" type="texcoordGroup_struct" implname="data" />
</implementation>

<nodegraph name="test_struct_texcoordgroup">
<extractfirstsgroup name="extractfirstsgroup" type="float">
<input name="in" type="texcoordGroup_struct" value="{{0.1;0.2};{0.3;0.4};{0.5;0.6}}"/>
</extractfirstsgroup>
<convert name="float_to_color3" type="color3">
<input name="in" type="float" nodename="extractfirstsgroup"/>
</convert>
<oren_nayar_diffuse_bsdf name="diffuse_brdf1" type="BSDF">
<input name="color" type="color3" nodename="float_to_color3" />
</oren_nayar_diffuse_bsdf>
<surface name="surface1" type="surfaceshader">
<input name="bsdf" type="BSDF" nodename="diffuse_brdf1" />
<input name="opacity" type="float" value="1.0" />
</surface>
<output name="out" type="surfaceshader" nodename="surface1" />
</nodegraph>
</materialx>
8 changes: 4 additions & 4 deletions source/MaterialXGenGlsl/GlslShaderGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ void GlslShaderGenerator::emitPixelStage(const ShaderGraph& graph, GenContext& c
else
{
string outputValue = outputSocket->getValue() ?
_syntax->getValue(outputSocket->getType(), *outputSocket->getValue()) :
_syntax->getValue(outputSocket->getType(), *outputSocket->getValue(), context) :
_syntax->getDefaultValue(outputSocket->getType());
if (!outputSocket->getType().isFloat4())
{
Expand Down Expand Up @@ -689,7 +689,7 @@ void GlslShaderGenerator::toVec4(TypeDesc type, string& variable)
}

void GlslShaderGenerator::emitVariableDeclaration(const ShaderPort* variable, const string& qualifier,
GenContext&, ShaderStage& stage,
GenContext& context, ShaderStage& stage,
bool assignValue) const
{
// A file texture input needs special handling on GLSL
Expand Down Expand Up @@ -724,7 +724,7 @@ void GlslShaderGenerator::emitVariableDeclaration(const ShaderPort* variable, co
if (assignValue)
{
const string valueStr = (variable->getValue() ?
_syntax->getValue(variable->getType(), *variable->getValue(), true) :
_syntax->getValue(variable->getType(), *variable->getValue(), context, true) :
_syntax->getDefaultValue(variable->getType(), true));
str += valueStr.empty() ? EMPTY_STRING : " = " + valueStr;
}
Expand Down Expand Up @@ -756,7 +756,7 @@ ShaderNodeImplPtr GlslShaderGenerator::getImplementation(const NodeDef& nodedef,
throw ExceptionShaderGenError("NodeDef '" + nodedef.getName() + "' has no outputs defined");
}

const TypeDesc outputType = TypeDesc::get(outputs[0]->getType());
const TypeDesc outputType = context.getTypeDesc(outputs[0]->getType());

if (implElement->isA<NodeGraph>())
{
Expand Down
11 changes: 6 additions & 5 deletions source/MaterialXGenGlsl/GlslSyntax.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <MaterialXGenGlsl/GlslSyntax.h>

#include <MaterialXGenShader/GenContext.h>
#include <MaterialXGenShader/ShaderGenerator.h>

MATERIALX_NAMESPACE_BEGIN
Expand All @@ -20,7 +21,7 @@ class GlslStringTypeSyntax : public StringTypeSyntax
GlslStringTypeSyntax() :
StringTypeSyntax("int", "0", "0") { }

string getValue(const Value& /*value*/, bool /*uniform*/) const override
string getValue(const Value& /*value*/, const GenContext& /*context*/, bool /*uniform*/) const override
{
return "0";
}
Expand All @@ -34,7 +35,7 @@ class GlslArrayTypeSyntax : public ScalarTypeSyntax
{
}

string getValue(const Value& value, bool /*uniform*/) const override
string getValue(const Value& value, const GenContext& /*context*/, bool /*uniform*/) const override
{
size_t arraySize = getSize(value);
if (arraySize > 0)
Expand Down Expand Up @@ -388,7 +389,7 @@ StructTypeSyntaxPtr GlslSyntax::createStructSyntax(const string& structTypeName,
typeDefinition);
}

string GlslStructTypeSyntax::getValue(const Value& value, bool /* uniform */) const
string GlslStructTypeSyntax::getValue(const Value& value, const GenContext& context, bool /*uniform*/) const
{
const AggregateValue& aggValue = static_cast<const AggregateValue&>(value);

Expand All @@ -401,10 +402,10 @@ string GlslStructTypeSyntax::getValue(const Value& value, bool /* uniform */) co
separator = ",";

auto memberTypeName = memberValue->getTypeString();
auto memberTypeDesc = TypeDesc::get(memberTypeName);
auto memberTypeDesc = context.getTypeDesc(memberTypeName);

// Recursively use the syntax to generate the output, so we can supported nested structs.
result += _parentSyntax->getValue(memberTypeDesc, *memberValue, true);
result += _parentSyntax->getValue(memberTypeDesc, *memberValue, context, true);
}

result += ")";
Expand Down
2 changes: 1 addition & 1 deletion source/MaterialXGenGlsl/GlslSyntax.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class MX_GENGLSL_API GlslStructTypeSyntax : public StructTypeSyntax
public:
using StructTypeSyntax::StructTypeSyntax;

string getValue(const Value& value, bool uniform) const override;
string getValue(const Value& value, const GenContext& context, bool uniform) const override;
};

MATERIALX_NAMESPACE_END
Expand Down
6 changes: 3 additions & 3 deletions source/MaterialXGenGlsl/Nodes/GeomColorNodeGlsl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ ShaderNodeImplPtr GeomColorNodeGlsl::create()
return std::make_shared<GeomColorNodeGlsl>();
}

void GeomColorNodeGlsl::createVariables(const ShaderNode& node, GenContext&, Shader& shader) const
void GeomColorNodeGlsl::createVariables(const ShaderNode& node, GenContext& context, Shader& shader) const
{
const ShaderInput* indexInput = node.getInput(INDEX);
const string index = indexInput ? indexInput->getValue()->getValueString() : "0";

ShaderStage& vs = shader.getStage(Stage::VERTEX);
ShaderStage& ps = shader.getStage(Stage::PIXEL);
addStageInput(HW::VERTEX_INPUTS, Type::COLOR4, HW::T_IN_COLOR + "_" + index, vs);
addStageConnector(HW::VERTEX_DATA, Type::COLOR4, HW::T_COLOR + "_" + index, vs, ps);
addStageInput(HW::VERTEX_INPUTS, Type::COLOR4, context, HW::T_IN_COLOR + "_" + index, vs);
addStageConnector(HW::VERTEX_DATA, Type::COLOR4, context, HW::T_COLOR + "_" + index, vs, ps);
}

void GeomColorNodeGlsl::emitFunctionCall(const ShaderNode& node, GenContext& context, ShaderStage& stage) const
Expand Down
10 changes: 5 additions & 5 deletions source/MaterialXGenGlsl/Nodes/GeomPropValueNodeGlsl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ ShaderNodeImplPtr GeomPropValueNodeGlsl::create()
return std::make_shared<GeomPropValueNodeGlsl>();
}

void GeomPropValueNodeGlsl::createVariables(const ShaderNode& node, GenContext&, Shader& shader) const
void GeomPropValueNodeGlsl::createVariables(const ShaderNode& node, GenContext& context, Shader& shader) const
{
const ShaderInput* geomPropInput = node.getInput(GEOMPROP);
if (!geomPropInput || !geomPropInput->getValue())
Expand All @@ -27,8 +27,8 @@ void GeomPropValueNodeGlsl::createVariables(const ShaderNode& node, GenContext&,
ShaderStage& vs = shader.getStage(Stage::VERTEX);
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_IN_GEOMPROP + "_" + geomProp, vs, ps);
addStageInput(HW::VERTEX_INPUTS, output->getType(), context, HW::T_IN_GEOMPROP + "_" + geomProp, vs);
addStageConnector(HW::VERTEX_DATA, output->getType(), context, HW::T_IN_GEOMPROP + "_" + geomProp, vs, ps);
}

void GeomPropValueNodeGlsl::emitFunctionCall(const ShaderNode& node, GenContext& context, ShaderStage& stage) const
Expand Down Expand Up @@ -72,7 +72,7 @@ ShaderNodeImplPtr GeomPropValueNodeGlslAsUniform::create()
return std::make_shared<GeomPropValueNodeGlslAsUniform>();
}

void GeomPropValueNodeGlslAsUniform::createVariables(const ShaderNode& node, GenContext&, Shader& shader) const
void GeomPropValueNodeGlslAsUniform::createVariables(const ShaderNode& node, GenContext& context, Shader& shader) const
{
const ShaderInput* geomPropInput = node.getInput(GEOMPROP);
if (!geomPropInput || !geomPropInput->getValue())
Expand All @@ -81,7 +81,7 @@ void GeomPropValueNodeGlslAsUniform::createVariables(const ShaderNode& node, Gen
}
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);
ShaderPort* uniform = addStageUniform(HW::PRIVATE_UNIFORMS, node.getOutput()->getType(), context, HW::T_GEOMPROP + "_" + geomProp, ps);
uniform->setPath(geomPropInput->getPath());
}

Expand Down
2 changes: 1 addition & 1 deletion source/MaterialXGenGlsl/Nodes/LightCompoundNodeGlsl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void LightCompoundNodeGlsl::initialize(const InterfaceElement& element, GenConte
NodeDefPtr nodeDef = graph.getNodeDef();
for (InputPtr input : nodeDef->getActiveInputs())
{
_lightUniforms.add(TypeDesc::get(input->getType()), input->getName());
_lightUniforms.add(context.getTypeDesc(input->getType()), context, input->getName());
}
}

Expand Down
6 changes: 3 additions & 3 deletions source/MaterialXGenGlsl/Nodes/LightNodeGlsl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ void LightNodeGlsl::createVariables(const ShaderNode&, GenContext& context, Shad

// Create uniform for intensity, exposure and direction
VariableBlock& lightUniforms = ps.getUniformBlock(HW::LIGHT_DATA);
lightUniforms.add(Type::FLOAT, "intensity", Value::createValue<float>(1.0f));
lightUniforms.add(Type::FLOAT, "exposure", Value::createValue<float>(0.0f));
lightUniforms.add(Type::VECTOR3, "direction", Value::createValue<Vector3>(Vector3(0.0f, 1.0f, 0.0f)));
lightUniforms.add(Type::FLOAT, context, "intensity", Value::createValue<float>(1.0f));
lightUniforms.add(Type::FLOAT, context, "exposure", Value::createValue<float>(0.0f));
lightUniforms.add(Type::VECTOR3, context, "direction", Value::createValue<Vector3>(Vector3(0.0f, 1.0f, 0.0f)));

const GlslShaderGenerator& shadergen = static_cast<const GlslShaderGenerator&>(context.getShaderGenerator());
shadergen.addStageLightingUniforms(context, ps);
Expand Down
4 changes: 2 additions & 2 deletions source/MaterialXGenGlsl/Nodes/LightShaderNodeGlsl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void LightShaderNodeGlsl::initialize(const InterfaceElement& element, GenContext
NodeDefPtr nodeDef = impl.getNodeDef();
for (InputPtr input : nodeDef->getActiveInputs())
{
_lightUniforms.add(TypeDesc::get(input->getType()), input->getName(), input->getValue());
_lightUniforms.add(context.getTypeDesc(input->getType()), context, input->getName(), input->getValue());
}
}

Expand All @@ -57,7 +57,7 @@ void LightShaderNodeGlsl::createVariables(const ShaderNode&, GenContext& context
for (size_t i = 0; i < _lightUniforms.size(); ++i)
{
const ShaderPort* u = _lightUniforms[i];
lightData.add(u->getType(), u->getName());
lightData.add(u->getType(), context, u->getName());
}

const GlslShaderGenerator& shadergen = static_cast<const GlslShaderGenerator&>(context.getShaderGenerator());
Expand Down
4 changes: 2 additions & 2 deletions source/MaterialXGenGlsl/Nodes/NumLightsNodeGlsl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ ShaderNodeImplPtr NumLightsNodeGlsl::create()
return std::make_shared<NumLightsNodeGlsl>();
}

void NumLightsNodeGlsl::createVariables(const ShaderNode&, GenContext&, Shader& shader) const
void NumLightsNodeGlsl::createVariables(const ShaderNode&, GenContext& context, Shader& shader) const
{
// Create uniform for number of active light sources
ShaderStage& ps = shader.getStage(Stage::PIXEL);
ShaderPort* numActiveLights = addStageUniform(HW::PRIVATE_UNIFORMS, Type::INTEGER, HW::T_NUM_ACTIVE_LIGHT_SOURCES, ps);
ShaderPort* numActiveLights = addStageUniform(HW::PRIVATE_UNIFORMS, Type::INTEGER, context, HW::T_NUM_ACTIVE_LIGHT_SOURCES, ps);
numActiveLights->setValue(Value::createValue<int>(0));
}

Expand Down
12 changes: 6 additions & 6 deletions source/MaterialXGenGlsl/Nodes/SurfaceNodeGlsl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ void SurfaceNodeGlsl::createVariables(const ShaderNode&, GenContext& context, Sh
ShaderStage& vs = shader.getStage(Stage::VERTEX);
ShaderStage& ps = shader.getStage(Stage::PIXEL);

addStageInput(HW::VERTEX_INPUTS, Type::VECTOR3, HW::T_IN_POSITION, vs);
addStageInput(HW::VERTEX_INPUTS, Type::VECTOR3, HW::T_IN_NORMAL, vs);
addStageUniform(HW::PRIVATE_UNIFORMS, Type::MATRIX44, HW::T_WORLD_INVERSE_TRANSPOSE_MATRIX, vs);
addStageInput(HW::VERTEX_INPUTS, Type::VECTOR3, context, HW::T_IN_POSITION, vs);
addStageInput(HW::VERTEX_INPUTS, Type::VECTOR3, context, HW::T_IN_NORMAL, vs);
addStageUniform(HW::PRIVATE_UNIFORMS, Type::MATRIX44, context, HW::T_WORLD_INVERSE_TRANSPOSE_MATRIX, vs);

addStageConnector(HW::VERTEX_DATA, Type::VECTOR3, HW::T_POSITION_WORLD, vs, ps);
addStageConnector(HW::VERTEX_DATA, Type::VECTOR3, HW::T_NORMAL_WORLD, vs, ps);
addStageConnector(HW::VERTEX_DATA, Type::VECTOR3, context, HW::T_POSITION_WORLD, vs, ps);
addStageConnector(HW::VERTEX_DATA, Type::VECTOR3, context, HW::T_NORMAL_WORLD, vs, ps);

addStageUniform(HW::PRIVATE_UNIFORMS, Type::VECTOR3, HW::T_VIEW_POSITION, ps);
addStageUniform(HW::PRIVATE_UNIFORMS, Type::VECTOR3, context, HW::T_VIEW_POSITION, ps);

const GlslShaderGenerator& shadergen = static_cast<const GlslShaderGenerator&>(context.getShaderGenerator());
shadergen.addStageLightingUniforms(context, ps);
Expand Down
Loading