Skip to content

Commit 14abb7a

Browse files
authored
Consolidate toVec4 method in HW generators (#2702)
While looking at some of the GLSL vs MSL differences @bernardkwok raised in #2697. I noticed that the `GLSLShaderGenerator::toVec4()` and the `MSLShaderGenerator::toVec4()` have gotten out of sync. I think it makes sense to continue the consolidation work and move this to `HWShaderGenerator` to be more easily maintained. I think there could be future work here to follow the pattern introduced in #2661, and instead of having the conversion shader code hard-coded in the shader generator - we could add a new node to the data library that performs the conversion and just update the shader generator to insert it when necessary.
1 parent f50c349 commit 14abb7a

File tree

6 files changed

+29
-56
lines changed

6 files changed

+29
-56
lines changed

source/MaterialXGenGlsl/GlslShaderGenerator.cpp

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -621,31 +621,6 @@ void GlslShaderGenerator::emitLightFunctionDefinitions(const ShaderGraph& graph,
621621
}
622622
}
623623

624-
void GlslShaderGenerator::toVec4(TypeDesc type, string& variable)
625-
{
626-
if (type.isFloat3())
627-
{
628-
variable = "vec4(" + variable + ", 1.0)";
629-
}
630-
else if (type.isFloat2())
631-
{
632-
variable = "vec4(" + variable + ", 0.0, 1.0)";
633-
}
634-
else if (type == Type::FLOAT || type == Type::INTEGER || type == Type::BOOLEAN)
635-
{
636-
variable = "vec4(" + variable + ", " + variable + ", " + variable + ", 1.0)";
637-
}
638-
else if (type == Type::BSDF || type == Type::EDF)
639-
{
640-
variable = "vec4(" + variable + ", 1.0)";
641-
}
642-
else
643-
{
644-
// Can't understand other types. Just return black.
645-
variable = "vec4(0.0, 0.0, 0.0, 1.0)";
646-
}
647-
}
648-
649624
void GlslShaderGenerator::emitVariableDeclaration(const ShaderPort* variable, const string& qualifier,
650625
GenContext&, ShaderStage& stage,
651626
bool assignValue) const

source/MaterialXGenGlsl/GlslShaderGenerator.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,6 @@ class MX_GENGLSL_API GlslShaderGenerator : public HwShaderGenerator
8282
/// Emit function definitions for lighting code
8383
virtual void emitLightFunctionDefinitions(const ShaderGraph& graph, GenContext& context, ShaderStage& stage) const;
8484

85-
static void toVec4(TypeDesc type, string& variable);
86-
[[deprecated]] static void toVec4(const TypeDesc* type, string& variable) { toVec4(*type, variable); }
87-
8885
/// Nodes used internally for light sampling.
8986
vector<ShaderNodePtr> _lightSamplingNodes;
9087
};

source/MaterialXGenHw/HwShaderGenerator.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,4 +424,31 @@ void HwShaderGenerator::emitClosureDataParameter(const ShaderNode& node, GenCont
424424
}
425425
}
426426

427+
void HwShaderGenerator::toVec4(TypeDesc type, string& variable) const
428+
{
429+
const string& vec4 = _syntax->getTypeName(Type::VECTOR4);
430+
431+
if (type.isFloat3())
432+
{
433+
variable = vec4+"(" + variable + ", 1.0)";
434+
}
435+
else if (type.isFloat2())
436+
{
437+
variable = vec4+"(" + variable + ", 0.0, 1.0)";
438+
}
439+
else if (type == Type::FLOAT || type == Type::INTEGER || type == Type::BOOLEAN)
440+
{
441+
variable = vec4+"(" + variable + ", " + variable + ", " + variable + ", 1.0)";
442+
}
443+
else if (type == Type::BSDF || type == Type::EDF)
444+
{
445+
variable = vec4+"(" + variable + ", 1.0)";
446+
}
447+
else
448+
{
449+
// Can't understand other types. Just return black.
450+
variable = vec4+"(0.0, 0.0, 0.0, 1.0)";
451+
}
452+
}
453+
427454
MATERIALX_NAMESPACE_END

source/MaterialXGenHw/HwShaderGenerator.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ class MX_GENHW_API HwShaderGenerator : public ShaderGenerator
8686

8787
/// Create and initialize a new HW shader for shader generation.
8888
virtual ShaderPtr createShader(const string& name, ElementPtr element, GenContext& context) const;
89+
90+
void toVec4(TypeDesc type, string& variable) const;
8991
};
9092

9193
MATERIALX_NAMESPACE_END

source/MaterialXGenMsl/MslShaderGenerator.cpp

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,31 +1140,6 @@ void MslShaderGenerator::emitLightFunctionDefinitions(const ShaderGraph& graph,
11401140
}
11411141
}
11421142

1143-
void MslShaderGenerator::toVec4(TypeDesc type, string& variable)
1144-
{
1145-
if (type.isFloat3())
1146-
{
1147-
variable = "float4(" + variable + ", 1.0)";
1148-
}
1149-
else if (type.isFloat2())
1150-
{
1151-
variable = "float4(" + variable + ", 0.0, 1.0)";
1152-
}
1153-
else if (type == Type::FLOAT || type == Type::INTEGER)
1154-
{
1155-
variable = "float4(" + variable + ", " + variable + ", " + variable + ", 1.0)";
1156-
}
1157-
else if (type == Type::BSDF || type == Type::EDF)
1158-
{
1159-
variable = "float4(" + variable + ", 1.0)";
1160-
}
1161-
else
1162-
{
1163-
// Can't understand other types. Just return black.
1164-
variable = "float4(0.0, 0.0, 0.0, 1.0)";
1165-
}
1166-
}
1167-
11681143
void MslShaderGenerator::emitVariableDeclaration(const ShaderPort* variable, const string& qualifier,
11691144
GenContext&, ShaderStage& stage,
11701145
bool assignValue) const

source/MaterialXGenMsl/MslShaderGenerator.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,6 @@ class MX_GENMSL_API MslShaderGenerator : public HwShaderGenerator
109109
/// Emit function definitions for lighting code
110110
virtual void emitLightFunctionDefinitions(const ShaderGraph& graph, GenContext& context, ShaderStage& stage) const;
111111

112-
static void toVec4(TypeDesc type, string& variable);
113-
[[deprecated]] static void toVec4(const TypeDesc* type, string& variable) { toVec4(*type, variable); }
114-
115112
/// Nodes used internally for light sampling.
116113
vector<ShaderNodePtr> _lightSamplingNodes;
117114
};

0 commit comments

Comments
 (0)