Skip to content

Commit

Permalink
Merge 8aefb5b into f2ee096
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasharrysson authored Jun 16, 2020
2 parents f2ee096 + 8aefb5b commit 355609b
Show file tree
Hide file tree
Showing 23 changed files with 184 additions and 151 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0"?>
<materialx version="1.37" xmlns:xi="http://www.w3.org/2001/XInclude">
<nodedef name="ND_MtlxShader" node="MtlxShader">
<output name="base_color_output" type="color3" />
<output name="normal_output" type="vector3" />
</nodedef>
<nodegraph name="Shader_graph" nodedef="ND_MtlxShader">
<constant name="node_color_0" type="color3">
<parameter name="value" type="color3" value=" 0.2, 0.2, 0.8" />
</constant>
<texcoord name="t1" type="vector2"/>
<multiply name="m1" type="vector2">
<input name="in1" type="vector2" nodename="t1"/>
<input name="in2" type="float" value="50"/>
</multiply>
<noise2d name="noise2d_float" type="float">
<input name="texcoord" type="vector2" nodename="m1"/>
<parameter name="amplitude" type="float" value="1.0" />
</noise2d>
<heighttonormal name="node_heighttonormal_2" type="vector3">
<input name="in" type="float" nodename="noise2d_float" />
<parameter name="scale" type="float" value="1.0" />
</heighttonormal>
<output name="base_color_output" type="color3" nodename="node_color_0" />
<output name="normal_output" type="vector3" nodename="node_heighttonormal_2" />
</nodegraph>
<MtlxShader name="GraphInst" type="multioutput" />
<output name="GraphInst_base_color_output" type="color3" nodename="GraphInst" output="base_color_output" />
<output name="GraphInst_normal_output" type="vector3" nodename="GraphInst" output="normal_output" />
<material name="heighttonormal_in_nodegraph">
<shaderref name="Shader" node="standard_surface">
<bindinput name="base" type="float" value="0.8" />
<bindinput name="base_color" type="color3" output="GraphInst_base_color_output" />
<bindinput name="normal" type="vector3" output="GraphInst_normal_output" />
</shaderref>
</material>
</materialx>
7 changes: 0 additions & 7 deletions source/MaterialXGenGlsl/GlslShaderGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -504,13 +504,6 @@ void GlslShaderGenerator::emitPixelStage(const ShaderGraph& graph, GenContext& c
emitInclude("pbrlib/" + GlslShaderGenerator::LANGUAGE + "/lib/mx_math.glsl", context, stage);
emitLineBreak(stage);

// Emit texture sampling code
if (graph.hasClassification(ShaderNode::Classification::CONVOLUTION2D))
{
emitInclude("stdlib/" + GlslShaderGenerator::LANGUAGE + "/lib/mx_sampling.glsl", context, stage);
emitLineBreak(stage);
}

// Emit lighting and shadowing code
if (lighting)
{
Expand Down
1 change: 1 addition & 0 deletions source/MaterialXGenGlsl/GlslSyntax.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,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::SOURCE_FILE_EXTENSION = ".glsl";
const StringVec GlslSyntax::VEC2_MEMBERS = { ".x", ".y" };
const StringVec GlslSyntax::VEC3_MEMBERS = { ".x", ".y", ".z" };
const StringVec GlslSyntax::VEC4_MEMBERS = { ".x", ".y", ".z", ".w" };
Expand Down
2 changes: 2 additions & 0 deletions source/MaterialXGenGlsl/GlslSyntax.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class GlslSyntax : public Syntax
const string& getOutputQualifier() const override { return OUTPUT_QUALIFIER; }
const string& getConstantQualifier() const override { return CONSTANT_QUALIFIER; };
const string& getUniformQualifier() const override { return UNIFORM_QUALIFIER; };
const string& getSourceFileExtension() const override { return SOURCE_FILE_EXTENSION; };

bool typeSupported(const TypeDesc* type) const override;

Expand All @@ -37,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 SOURCE_FILE_EXTENSION;

static const StringVec VEC2_MEMBERS;
static const StringVec VEC3_MEMBERS;
Expand Down
10 changes: 10 additions & 0 deletions source/MaterialXGenGlsl/Nodes/HeightToNormalNodeGlsl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ bool HeightToNormalNodeGlsl::acceptsInputType(const TypeDesc* type) const
return (type == Type::FLOAT && type->isScalar());
}

void HeightToNormalNodeGlsl::emitFunctionDefinition(const ShaderNode&, GenContext& context, ShaderStage& stage) const
{
BEGIN_SHADER_STAGE(stage, Stage::PIXEL)
// Emit sampling functions
const ShaderGenerator& shadergen = context.getShaderGenerator();
shadergen.emitInclude("stdlib/" + shadergen.getLanguage() + "/lib/mx_sampling.glsl", context, stage);
shadergen.emitLineBreak(stage);
END_SHADER_STAGE(shader, Stage::PIXEL)
}

void HeightToNormalNodeGlsl::emitFunctionCall(const ShaderNode& node, GenContext& context, ShaderStage& stage) const
{
BEGIN_SHADER_STAGE(stage, Stage::PIXEL)
Expand Down
1 change: 1 addition & 0 deletions source/MaterialXGenGlsl/Nodes/HeightToNormalNodeGlsl.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class HeightToNormalNodeGlsl : public ConvolutionNode
public:
static ShaderNodeImplPtr create();

void emitFunctionDefinition(const ShaderNode& node, GenContext& context, ShaderStage& stage) const override;
void emitFunctionCall(const ShaderNode& node, GenContext& context, ShaderStage& stage) const override;

const string& getLanguage() const override;
Expand Down
5 changes: 0 additions & 5 deletions source/MaterialXGenMdl/MdlShaderGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,6 @@ ShaderPtr MdlShaderGenerator::generate(const string& name, ElementPtr element, G
{
emitLine(module, stage);
}
if (graph.hasClassification(ShaderNode::Classification::CONVOLUTION2D))
{
// TODO: create a package with the convolution functions
// emitLine("import materialx::convolution::*", stage);
}

// Add global constants and type definitions
emitTypeDefinitions(context, stage);
Expand Down
1 change: 1 addition & 0 deletions source/MaterialXGenMdl/MdlSyntax.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ class MdlEnumSyntax : public AggregateTypeSyntax

const string MdlSyntax::CONST_QUALIFIER = "";
const string MdlSyntax::UNIFORM_QUALIFIER = "uniform";
const string MdlSyntax::SOURCE_FILE_EXTENSION = ".mdl";
const StringVec MdlSyntax::VECTOR2_MEMBERS = { ".x", ".y" };
const StringVec MdlSyntax::VECTOR3_MEMBERS = { ".x", ".y", ".z" };
const StringVec MdlSyntax::VECTOR4_MEMBERS = { ".x", ".y", ".z", ".w" };
Expand Down
2 changes: 2 additions & 0 deletions source/MaterialXGenMdl/MdlSyntax.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class MdlSyntax : public Syntax

const string& getConstantQualifier() const override { return CONST_QUALIFIER; };
const string& getUniformQualifier() const override { return UNIFORM_QUALIFIER; };
const string& getSourceFileExtension() const override { return SOURCE_FILE_EXTENSION; };

string getSwizzledVariable(const string& srcName, const TypeDesc* srcType, const string& channels, const TypeDesc* dstType) const override;

Expand All @@ -41,6 +42,7 @@ class MdlSyntax : public Syntax

static const string CONST_QUALIFIER;
static const string UNIFORM_QUALIFIER;
static const string SOURCE_FILE_EXTENSION;
static const StringVec VECTOR2_MEMBERS;
static const StringVec VECTOR3_MEMBERS;
static const StringVec VECTOR4_MEMBERS;
Expand Down
209 changes: 107 additions & 102 deletions source/MaterialXGenMdl/Nodes/BlurNodeMdl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,131 +61,136 @@ void BlurNodeMdl::outputSampleArray(const ShaderGenerator& shadergen, ShaderStag
shadergen.emitScopeEnd(stage, true);
}

// No definitions needed for blur in MDL so override the base class with an empty function definition emitter.
void BlurNodeMdl::emitFunctionDefinition(const ShaderNode&, GenContext&, ShaderStage&) const
{
}

void BlurNodeMdl::emitFunctionCall(const ShaderNode& node, GenContext& context, ShaderStage& stage) const
{
BEGIN_SHADER_STAGE(stage, Stage::PIXEL)
const ShaderGenerator& shadergen = context.getShaderGenerator();

const ShaderInput* inInput = node.getInput(IN_STRING);
const ShaderInput* inInput = node.getInput(IN_STRING);

const Syntax& syntax = shadergen.getSyntax();
const Syntax& syntax = shadergen.getSyntax();

// Get input type name string
const string& inputTypeString = inInput && acceptsInputType(inInput->getType()) ?
syntax.getTypeName(inInput->getType()) : EMPTY_STRING;
// Get input type name string
const string& inputTypeString = inInput && acceptsInputType(inInput->getType()) ?
syntax.getTypeName(inInput->getType()) : EMPTY_STRING;

const ShaderInput* filterTypeInput = node.getInput(FILTER_TYPE_STRING);
if (!filterTypeInput || inputTypeString.empty())
{
throw ExceptionShaderGenError("Node '" + node.getName() + "' is not a valid Blur node");
}
const ShaderInput* filterTypeInput = node.getInput(FILTER_TYPE_STRING);
if (!filterTypeInput || inputTypeString.empty())
{
throw ExceptionShaderGenError("Node '" + node.getName() + "' is not a valid Blur node");
}

// Compute width of filter. Default is 1 which just means one 1x1 upstream samples
const ShaderInput* sizeInput = node.getInput(FILTER_SIZE_STRING);
unsigned int filterWidth = 1;
unsigned int arrayOffset = 0;
if (sizeInput)
{
float sizeInputValue = sizeInput->getValue()->asA<float>();
if (sizeInputValue > 0.0f)
// Compute width of filter. Default is 1 which just means one 1x1 upstream samples
const ShaderInput* sizeInput = node.getInput(FILTER_SIZE_STRING);
unsigned int filterWidth = 1;
unsigned int arrayOffset = 0;
if (sizeInput)
{
if (sizeInputValue <= 0.333f)
float sizeInputValue = sizeInput->getValue()->asA<float>();
if (sizeInputValue > 0.0f)
{
filterWidth = 3;
arrayOffset = 1;
}
else if (sizeInputValue <= 0.666f)
{
filterWidth = 5;
arrayOffset = 10;
}
else
{
filterWidth = 7;
arrayOffset = 35;
if (sizeInputValue <= 0.333f)
{
filterWidth = 3;
arrayOffset = 1;
}
else if (sizeInputValue <= 0.666f)
{
filterWidth = 5;
arrayOffset = 10;
}
else
{
filterWidth = 7;
arrayOffset = 35;
}
}
}
}

// Sample count is square of filter size
const unsigned int sampleCount = filterWidth * filterWidth;

// Emit samples
// Note: The maximum sample count MX_MAX_SAMPLE_COUNT is defined in the shader code and
// is assumed to be 49 (7x7 kernel). If this changes the filter size logic here
// needs to be adjusted.
//
StringVec sampleStrings;
emitInputSamplesUV(node, sampleCount, filterWidth,
_filterSize, _filterOffset, _sampleSizeFunctionUV,
context, stage, sampleStrings);

// There should always be at least 1 sample
if (sampleStrings.empty())
{
throw ExceptionShaderGenError("Node '" + node.getName() + "' cannot compute upstream samples");
}
// Sample count is square of filter size
const unsigned int sampleCount = filterWidth * filterWidth;

const ShaderOutput* output = node.getOutput();

if (sampleCount > 1)
{
const string MX_WEIGHT_ARRAY_SIZE_STRING("MX_WEIGHT_ARRAY_SIZE");
const string MX_CONVOLUTION_PREFIX_STRING("mx_convolution_");
const string SAMPLES_POSTFIX_STRING("_samples");
const string WEIGHT_POSTFIX_STRING("_weights");

// Set up sample array
string sampleName(output->getVariable() + SAMPLES_POSTFIX_STRING);
outputSampleArray(shadergen, stage, inInput->getType(), sampleName, sampleStrings);

// Emit code to evaluate using input sample and weight arrays.
// The function to call depends on input type.
// Emit samples
// Note: The maximum sample count MX_MAX_SAMPLE_COUNT is defined in the shader code and
// is assumed to be 49 (7x7 kernel). If this changes the filter size logic here
// needs to be adjusted.
//
shadergen.emitLineBegin(stage);
shadergen.emitOutput(output, true, false, context, stage);
StringVec sampleStrings;
emitInputSamplesUV(node, sampleCount, filterWidth,
_filterSize, _filterOffset, _sampleSizeFunctionUV,
context, stage, sampleStrings);

// Emit branching code to compute result based on filter type
//
shadergen.emitString(" = ", stage);
shadergen.emitInput(filterTypeInput, context, stage);
// Remap enumeration for comparison as needed
std::pair<const TypeDesc*, ValuePtr> result;
string emitValue = "\"" + GAUSSIAN_FILTER + "\"";
if (syntax.remapEnumeration(GAUSSIAN_FILTER, Type::STRING, FILTER_LIST, result))
// There should always be at least 1 sample
if (sampleStrings.empty())
{
emitValue = syntax.getValue(result.first, *(result.second));
throw ExceptionShaderGenError("Node '" + node.getName() + "' cannot compute upstream samples");
}
shadergen.emitString(" == " + emitValue + " ? ", stage);

const ShaderOutput* output = node.getOutput();

if (sampleCount > 1)
{
string filterFunctionName = MX_CONVOLUTION_PREFIX_STRING + inputTypeString;
shadergen.emitString(filterFunctionName + "(" + sampleName + ", " +
GAUSSIAN_WEIGHTS_VARIABLE + ", " +
std::to_string(arrayOffset) + ", " +
std::to_string(sampleCount) +
")", stage);
const string MX_WEIGHT_ARRAY_SIZE_STRING("MX_WEIGHT_ARRAY_SIZE");
const string MX_CONVOLUTION_PREFIX_STRING("mx_convolution_");
const string SAMPLES_POSTFIX_STRING("_samples");
const string WEIGHT_POSTFIX_STRING("_weights");

// Set up sample array
string sampleName(output->getVariable() + SAMPLES_POSTFIX_STRING);
outputSampleArray(shadergen, stage, inInput->getType(), sampleName, sampleStrings);

// Emit code to evaluate using input sample and weight arrays.
// The function to call depends on input type.
//
shadergen.emitLineBegin(stage);
shadergen.emitOutput(output, true, false, context, stage);

// Emit branching code to compute result based on filter type
//
shadergen.emitString(" = ", stage);
shadergen.emitInput(filterTypeInput, context, stage);
// Remap enumeration for comparison as needed
std::pair<const TypeDesc*, ValuePtr> result;
string emitValue = "\"" + GAUSSIAN_FILTER + "\"";
if (syntax.remapEnumeration(GAUSSIAN_FILTER, Type::STRING, FILTER_LIST, result))
{
emitValue = syntax.getValue(result.first, *(result.second));
}
shadergen.emitString(" == " + emitValue + " ? ", stage);
{
string filterFunctionName = MX_CONVOLUTION_PREFIX_STRING + inputTypeString;
shadergen.emitString(filterFunctionName + "(" + sampleName + ", " +
GAUSSIAN_WEIGHTS_VARIABLE + ", " +
std::to_string(arrayOffset) + ", " +
std::to_string(sampleCount) +
")", stage);
}
shadergen.emitString(" : ", stage);
{
string filterFunctionName = MX_CONVOLUTION_PREFIX_STRING + inputTypeString;
shadergen.emitString(filterFunctionName + "(" + sampleName + ", " +
BOX_WEIGHTS_VARIABLE + ", " +
std::to_string(arrayOffset) + ", " +
std::to_string(sampleCount) +
")", stage);
}
shadergen.emitLineEnd(stage);
}
shadergen.emitString(" : ", stage);
else
{
string filterFunctionName = MX_CONVOLUTION_PREFIX_STRING + inputTypeString;
shadergen.emitString(filterFunctionName + "(" + sampleName + ", " +
BOX_WEIGHTS_VARIABLE + ", " +
std::to_string(arrayOffset) + ", " +
std::to_string(sampleCount) +
")", stage);
// This is just a pass-through of the upstream sample if any,
// or the constant value on the node.
//
shadergen.emitLineBegin(stage);
shadergen.emitOutput(output, true, false, context, stage);
shadergen.emitString(" = " + sampleStrings[0], stage);
shadergen.emitLineEnd(stage);
}
shadergen.emitLineEnd(stage);
}
else
{
// This is just a pass-through of the upstream sample if any,
// or the constant value on the node.
//
shadergen.emitLineBegin(stage);
shadergen.emitOutput(output, true, false, context, stage);
shadergen.emitString(" = " + sampleStrings[0], stage);
shadergen.emitLineEnd(stage);
}
END_SHADER_STAGE(stage, Stage::PIXEL)
}

Expand Down
1 change: 1 addition & 0 deletions source/MaterialXGenMdl/Nodes/BlurNodeMdl.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class BlurNodeMdl : public BlurNode
public:
static ShaderNodeImplPtr create();

void emitFunctionDefinition(const ShaderNode& node, GenContext& context, ShaderStage& stage) const override;
void emitFunctionCall(const ShaderNode& node, GenContext& context, ShaderStage& stage) const override;

protected:
Expand Down
8 changes: 0 additions & 8 deletions source/MaterialXGenOgsFx/OgsFxShaderGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,14 +304,6 @@ void OgsFxShaderGenerator::emitPixelStage(const ShaderGraph& graph, GenContext&
emitInclude("pbrlib/" + GlslShaderGenerator::LANGUAGE + "/lib/mx_math.glsl", context, stage);
emitLineBreak(stage);

// Emit sampling code if needed
if (graph.hasClassification(ShaderNode::Classification::CONVOLUTION2D))
{
// Emit sampling functions
emitInclude("stdlib/" + GlslShaderGenerator::LANGUAGE + "/lib/mx_sampling.glsl", context, stage);
emitLineBreak(stage);
}

// Set the include file to use for uv transformations,
// depending on the vertical flip flag.
if (context.getOptions().fileTextureVerticalFlip)
Expand Down
Loading

0 comments on commit 355609b

Please sign in to comment.