Skip to content

Commit 674da45

Browse files
authored
Merge branch 'AcademySoftwareFoundation:main' into node_editor_traversal
2 parents 2155b85 + 05854f8 commit 674da45

10 files changed

Lines changed: 91 additions & 77 deletions

File tree

javascript/MaterialXView/source/viewer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ export class Scene
182182

183183
if (!child.geometry.attributes.normal)
184184
{
185-
var startNormalTime = performance.new();
185+
var startNormalTime = performance.now();
186186
child.geometry.computeVertexNormals();
187187
normalTime += performance.now() - startNormalTime;
188188
}

libraries/pbrlib/genglsl/lib/mx_environment_fis.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ vec3 mx_environment_radiance(vec3 N, vec3 V, vec3 X, vec2 alpha, int distributio
5959
radiance /= G1V * float(envRadianceSamples);
6060

6161
// Return the final radiance.
62-
return radiance * $envLightIntensity;
62+
return ($envRadianceSamples == 0 ? vec3(0.0) : radiance) * $envLightIntensity;
6363
}
6464

6565
vec3 mx_environment_irradiance(vec3 N)

libraries/stdlib/genmsl/lib/mx_math.metal

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,27 @@
66
#define mx_asin metal::asin
77
#define mx_acos metal::acos
88

9-
vec2 mx_matrix_mul(vec2 v, mat2 m) { return v * m; }
10-
vec3 mx_matrix_mul(vec3 v, mat3 m) { return v * m; }
11-
vec4 mx_matrix_mul(vec4 v, mat4 m) { return v * m; }
12-
vec2 mx_matrix_mul(mat2 m, vec2 v) { return m * v; }
13-
vec3 mx_matrix_mul(mat3 m, vec3 v) { return m * v; }
14-
vec4 mx_matrix_mul(mat4 m, vec4 v) { return m * v; }
15-
mat2 mx_matrix_mul(mat2 m1, mat2 m2) { return m1 * m2; }
16-
mat3 mx_matrix_mul(mat3 m1, mat3 m2) { return m1 * m2; }
17-
mat4 mx_matrix_mul(mat4 m1, mat4 m2) { return m1 * m2; }
9+
float2 mx_matrix_mul(float2 v, float2x2 m) { return v * m; }
10+
float3 mx_matrix_mul(float3 v, float3x3 m) { return v * m; }
11+
float4 mx_matrix_mul(float4 v, float4x4 m) { return v * m; }
12+
float2 mx_matrix_mul(float2x2 m, float2 v) { return m * v; }
13+
float3 mx_matrix_mul(float3x3 m, float3 v) { return m * v; }
14+
float4 mx_matrix_mul(float4x4 m, float4 v) { return m * v; }
15+
float2x2 mx_matrix_mul(float2x2 m1, float2x2 m2) { return m1 * m2; }
16+
float3x3 mx_matrix_mul(float3x3 m1, float3x3 m2) { return m1 * m2; }
17+
float4x4 mx_matrix_mul(float4x4 m1, float4x4 m2) { return m1 * m2; }
1818

1919
float mx_square(float x)
2020
{
2121
return x*x;
2222
}
2323

24-
vec2 mx_square(vec2 x)
24+
float2 mx_square(float2 x)
2525
{
2626
return x*x;
2727
}
2828

29-
vec3 mx_square(vec3 x)
29+
float3 mx_square(float3 x)
3030
{
3131
return x*x;
3232
}
@@ -118,17 +118,17 @@ float mx_atan(float y, float x)
118118
return metal::atan2(y, x);
119119
}
120120

121-
vec2 mx_atan(vec2 y, vec2 x)
121+
float2 mx_atan(float2 y, float2 x)
122122
{
123123
return metal::atan2(y, x);
124124
}
125125

126-
vec3 mx_atan(vec3 y, vec3 x)
126+
float3 mx_atan(float3 y, float3 x)
127127
{
128128
return metal::atan2(y, x);
129129
}
130130

131-
vec4 mx_atan(vec4 y, vec4 x)
131+
float4 mx_atan(float4 y, float4 x)
132132
{
133133
return metal::atan2(y, x);
134134
}
@@ -138,7 +138,7 @@ float mx_radians(float degree)
138138
return (degree * M_PI_F / 180.0f);
139139
}
140140

141-
vec2 mx_radians(vec2 degree)
141+
float2 mx_radians(float2 degree)
142142
{
143143
return (degree * M_PI_F / 180.0f);
144144
}

source/MaterialXGenHw/HwConstants.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,10 @@ const string ENV_IRRADIANCE_SAMPLER2D_SPLIT = "sampler2D(u_envIradiance_textur
126126

127127
const string TEX_SAMPLER_SAMPLER2D = "tex_sampler";
128128
const string TEX_SAMPLER_SAMPLER2D_SPLIT = "sampler2D(tex_texture, tex_sampler)";
129+
const string TEX_SAMPLER_SAMPLER2D_MSL = "tex_sampler";
129130
const string TEX_SAMPLER_SIGNATURE = "sampler2D tex_sampler";
130131
const string TEX_SAMPLER_SIGNATURE_SPLIT = "texture2D tex_texture, sampler tex_sampler";
132+
const string TEX_SAMPLER_SIGNATURE_MSL = "MetalTexture tex_sampler";
131133

132134
const string ENV_LIGHT_INTENSITY = "u_envLightIntensity";
133135
const string ENV_PREFILTER_MIP = "u_envPrefilterMip";

source/MaterialXGenHw/HwConstants.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,10 @@ extern MX_GENHW_API const string LIGHT_DATA_MAX_LIGHT_SOURCES;
213213
/// Texture sampler parameters (for both combined and separate values)
214214
extern MX_GENHW_API const string TEX_SAMPLER_SAMPLER2D;
215215
extern MX_GENHW_API const string TEX_SAMPLER_SAMPLER2D_SPLIT;
216+
extern MX_GENHW_API const string TEX_SAMPLER_SAMPLER2D_MSL;
216217
extern MX_GENHW_API const string TEX_SAMPLER_SIGNATURE;
217218
extern MX_GENHW_API const string TEX_SAMPLER_SIGNATURE_SPLIT;
219+
extern MX_GENHW_API const string TEX_SAMPLER_SIGNATURE_MSL;
218220

219221
/// Variable blocks names.
220222
extern MX_GENHW_API const string VERTEX_INPUTS; // Geometric inputs for vertex stage.

source/MaterialXGenHw/Nodes/HwSurfaceNode.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ void HwSurfaceNode::emitFunctionCall(const ShaderNode& node, GenContext& context
114114
shadergen.emitComment("Shadow occlusion", stage);
115115
if (context.getOptions().hwShadowMap)
116116
{
117-
shadergen.emitLine("vec3 shadowCoord = mx_matrix_mul(" + HW::T_SHADOW_MATRIX + ", vec4(" + prefix + HW::T_POSITION_WORLD + ", 1.0)).xyz", stage);
117+
shadergen.emitLine("vec4 shadowCoord4 = mx_matrix_mul(" + HW::T_SHADOW_MATRIX + ", vec4(" + prefix + HW::T_POSITION_WORLD + ", 1.0))", stage);
118+
shadergen.emitLine("vec3 shadowCoord = shadowCoord4.xyz / shadowCoord4.w;", stage);
118119
shadergen.emitLine("shadowCoord = shadowCoord * 0.5 + 0.5", stage);
119120
shadergen.emitLine("vec2 shadowMoments = texture(" + HW::T_SHADOW_MAP + ", shadowCoord.xy).xy", stage);
120121
shadergen.emitLine("occlusion = mx_variance_shadow_occlusion(shadowMoments, shadowCoord.z)", stage);
@@ -270,6 +271,11 @@ void HwSurfaceNode::emitLightLoop(const ShaderNode& node, GenContext& context, S
270271
shadergen.emitComment("Accumulate the light's contribution", stage);
271272
shadergen.emitLine(outColor + " += lightShader.intensity * " + bsdf->getOutput()->getVariable() + ".response", stage);
272273

274+
shadergen.emitLineBreak(stage);
275+
276+
shadergen.emitComment("Clear shadow factor for next light", stage);
277+
shadergen.emitLine("occlusion = 1.0", stage);
278+
273279
shadergen.emitScopeEnd(stage);
274280
shadergen.emitLineBreak(stage);
275281
}

source/MaterialXGenMsl/MslShaderGenerator.cpp

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ MslShaderGenerator::MslShaderGenerator(TypeSystemPtr typeSystem) :
129129
_lightSamplingNodes.push_back(ShaderNode::create(nullptr, "sampleLightSource", HwLightSamplerNode::create()));
130130

131131
_tokenSubstitutions[HW::T_CLOSURE_DATA_CONSTRUCTOR] = "{closureType, L, V, N, P, occlusion}";
132+
133+
_tokenSubstitutions[HW::T_TEX_SAMPLER_SAMPLER2D] = HW::TEX_SAMPLER_SAMPLER2D_MSL;
134+
_tokenSubstitutions[HW::T_TEX_SAMPLER_SIGNATURE] = HW::TEX_SAMPLER_SIGNATURE_MSL;
132135
}
133136

134137
ShaderPtr MslShaderGenerator::generate(const string& name, ElementPtr element, GenContext& context) const
@@ -157,6 +160,8 @@ ShaderPtr MslShaderGenerator::generate(const string& name, ElementPtr element, G
157160
emitVertexStage(shader->getGraph(), context, vs);
158161
replaceTokens(_tokenSubstitutions, vs);
159162

163+
MetalizeGeneratedShader(vs);
164+
160165
// Emit code for pixel shader stage
161166
ShaderStage& ps = shader->getStage(Stage::PIXEL);
162167
emitPixelStage(shader->getGraph(), context, ps);
@@ -222,7 +227,7 @@ void MslShaderGenerator::MetalizeGeneratedShader(ShaderStage& shaderStage) const
222227
}
223228
else
224229
{
225-
sourceCode.replace(beg, typename_end - beg, "thread " + typeName + "&");
230+
sourceCode.replace(beg, typename_end - beg, "thread " + typeName + " &");
226231
}
227232
}
228233
pos = sourceCode.find(keyword, pos);
@@ -236,14 +241,30 @@ void MslShaderGenerator::MetalizeGeneratedShader(ShaderStage& shaderStage) const
236241
replaceTokens["dFdy"] = "dfdy";
237242
replaceTokens["dFdx"] = "dfdx";
238243

244+
replaceTokens["vec2"] = "float2";
245+
replaceTokens["vec3"] = "float3";
246+
replaceTokens["vec4"] = "float4";
247+
replaceTokens["ivec2"] = "int2";
248+
replaceTokens["ivec3"] = "int3";
249+
replaceTokens["ivec4"] = "int4";
250+
replaceTokens["uvec2"] = "uint2";
251+
replaceTokens["uvec3"] = "uint3";
252+
replaceTokens["uvec4"] = "uint4";
253+
replaceTokens["bvec2"] = "bool2";
254+
replaceTokens["bvec3"] = "bool3";
255+
replaceTokens["bvec4"] = "bool4";
256+
replaceTokens["mat2"] = "float2x2";
257+
replaceTokens["mat3"] = "float3x3";
258+
replaceTokens["mat4"] = "float4x4";
259+
239260
auto isAllowedAfterToken = [](char ch) -> bool
240261
{
241262
return std::isspace(ch) || ch == '(' || ch == ')' || ch == ',';
242263
};
243264

244265
auto isAllowedBeforeToken = [](char ch) -> bool
245266
{
246-
return std::isspace(ch) || ch == '(' || ch == ',' || ch == '-';
267+
return std::isspace(ch) || ch == '(' || ch == ',' || ch == '+' || ch == '-';
247268
};
248269

249270
for (const auto& t : replaceTokens)
@@ -295,7 +316,7 @@ void MslShaderGenerator::emitGlobalVariables(GenContext& context,
295316
{
296317
if (globalContextMembers)
297318
{
298-
emitLine("vec4 gl_FragCoord", stage);
319+
emitLine("float4 gl_FragCoord", stage);
299320
}
300321
if (globalContextConstructorInit)
301322
{
@@ -593,9 +614,9 @@ void MslShaderGenerator::emitVertexStage(const ShaderGraph& graph, GenContext& c
593614
emitString("\tGlobalContext ctx {", stage);
594615
emitGlobalVariables(context, stage, EMIT_GLOBAL_SCOPE_CONTEXT_MEMBER_INIT, true, false);
595616
emitLine("}", stage, true);
596-
emitLine(vertexData.getName() + " out = ctx.VertexMain()", stage, true);
597-
emitLine("out.pos.y = -out.pos.y", stage, true);
598-
emitLine("return out", stage, true);
617+
emitLine(vertexData.getName() + " outVertex = ctx.VertexMain()", stage, true);
618+
emitLine("outVertex.pos.y = -outVertex.pos.y", stage, true);
619+
emitLine("return outVertex", stage, true);
599620
}
600621
emitScopeEnd(stage);
601622
emitLineBreak(stage);
@@ -650,22 +671,6 @@ void MslShaderGenerator::emitDirectives(GenContext&, ShaderStage& stage) const
650671
emitLine("#include <simd/simd.h>", stage, false);
651672
emitLine("using namespace metal;", stage, false);
652673

653-
emitLine("#define vec2 float2", stage, false);
654-
emitLine("#define vec3 float3", stage, false);
655-
emitLine("#define vec4 float4", stage, false);
656-
emitLine("#define ivec2 int2", stage, false);
657-
emitLine("#define ivec3 int3", stage, false);
658-
emitLine("#define ivec4 int4", stage, false);
659-
emitLine("#define uvec2 uint2", stage, false);
660-
emitLine("#define uvec3 uint3", stage, false);
661-
emitLine("#define uvec4 uint4", stage, false);
662-
emitLine("#define bvec2 bool2", stage, false);
663-
emitLine("#define bvec3 bool3", stage, false);
664-
emitLine("#define bvec4 bool4", stage, false);
665-
emitLine("#define mat2 float2x2", stage, false);
666-
emitLine("#define mat3 float3x3", stage, false);
667-
emitLine("#define mat4 float4x4", stage, false);
668-
669674
emitLineBreak(stage);
670675
}
671676

source/MaterialXGenMsl/MslSyntax.cpp

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,9 @@ MslSyntax::MslSyntax(TypeSystemPtr typeSystem) : Syntax(typeSystem)
176176
Type::COLOR3,
177177
std::make_shared<AggregateTypeSyntax>(
178178
this,
179-
"vec3",
180-
"vec3(0.0)",
181-
"vec3(0.0)",
179+
"float3",
180+
"float3(0.0)",
181+
"float3(0.0)",
182182
EMPTY_STRING,
183183
EMPTY_STRING,
184184
VEC3_MEMBERS));
@@ -187,9 +187,9 @@ MslSyntax::MslSyntax(TypeSystemPtr typeSystem) : Syntax(typeSystem)
187187
Type::COLOR4,
188188
std::make_shared<AggregateTypeSyntax>(
189189
this,
190-
"vec4",
191-
"vec4(0.0)",
192-
"vec4(0.0)",
190+
"float4",
191+
"float4(0.0)",
192+
"float4(0.0)",
193193
EMPTY_STRING,
194194
EMPTY_STRING,
195195
VEC4_MEMBERS));
@@ -198,9 +198,9 @@ MslSyntax::MslSyntax(TypeSystemPtr typeSystem) : Syntax(typeSystem)
198198
Type::VECTOR2,
199199
std::make_shared<AggregateTypeSyntax>(
200200
this,
201-
"vec2",
202-
"vec2(0.0)",
203-
"vec2(0.0)",
201+
"float2",
202+
"float2(0.0)",
203+
"float2(0.0)",
204204
EMPTY_STRING,
205205
EMPTY_STRING,
206206
VEC2_MEMBERS));
@@ -209,9 +209,9 @@ MslSyntax::MslSyntax(TypeSystemPtr typeSystem) : Syntax(typeSystem)
209209
Type::VECTOR3,
210210
std::make_shared<AggregateTypeSyntax>(
211211
this,
212-
"vec3",
213-
"vec3(0.0)",
214-
"vec3(0.0)",
212+
"float3",
213+
"float3(0.0)",
214+
"float3(0.0)",
215215
EMPTY_STRING,
216216
EMPTY_STRING,
217217
VEC3_MEMBERS));
@@ -220,9 +220,9 @@ MslSyntax::MslSyntax(TypeSystemPtr typeSystem) : Syntax(typeSystem)
220220
Type::VECTOR4,
221221
std::make_shared<AggregateTypeSyntax>(
222222
this,
223-
"vec4",
224-
"vec4(0.0)",
225-
"vec4(0.0)",
223+
"float4",
224+
"float4(0.0)",
225+
"float4(0.0)",
226226
EMPTY_STRING,
227227
EMPTY_STRING,
228228
VEC4_MEMBERS));
@@ -231,17 +231,17 @@ MslSyntax::MslSyntax(TypeSystemPtr typeSystem) : Syntax(typeSystem)
231231
Type::MATRIX33,
232232
std::make_shared<AggregateTypeSyntax>(
233233
this,
234-
"mat3",
235-
"mat3(1.0)",
236-
"mat3(1.0)"));
234+
"float3x3",
235+
"float3x3(1.0)",
236+
"float3x3(1.0)"));
237237

238238
registerTypeSyntax(
239239
Type::MATRIX44,
240240
std::make_shared<AggregateTypeSyntax>(
241241
this,
242-
"mat4",
243-
"mat4(1.0)",
244-
"mat4(1.0)"));
242+
"float4x4",
243+
"float4x4(1.0)",
244+
"float4x4(1.0)"));
245245

246246
registerTypeSyntax(
247247
Type::STRING,

source/MaterialXGenShader/ShaderTranslator.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ void ShaderTranslator::connectTranslationInputs(NodePtr shader, NodeDefPtr trans
1919
std::set<OutputPtr> origOutputs;
2020
for (InputPtr shaderInput : origInputs)
2121
{
22-
if (translationNodeDef->getInput(shaderInput->getName()))
22+
if (translationNodeDef->getActiveInput(shaderInput->getName()))
2323
{
2424
InputPtr input = _translationNode->addInput(shaderInput->getName(), shaderInput->getType());
2525

@@ -78,15 +78,14 @@ void ShaderTranslator::connectTranslationInputs(NodePtr shader, NodeDefPtr trans
7878
void ShaderTranslator::connectTranslationOutputs(NodePtr shader)
7979
{
8080
DocumentPtr doc = shader->getDocument();
81-
InterfaceElementPtr implement = _translationNode->getImplementation();
82-
NodeGraphPtr translationGraph = implement ? implement->asA<NodeGraph>() : nullptr;
83-
if (!translationGraph)
81+
NodeDefPtr nodeDef = _translationNode->getNodeDef();
82+
if (!nodeDef)
8483
{
85-
throw Exception("No graph implementation for " + _translationNode->getCategory() + " was found");
84+
throw Exception("No nodedef for " + _translationNode->getCategory() + " was found");
8685
}
8786

8887
// Iterate through outputs of the translation graph.
89-
for (OutputPtr translationGraphOutput : translationGraph->getOutputs())
88+
for (OutputPtr translationGraphOutput : nodeDef->getActiveOutputs())
9089
{
9190
// Convert output name to input name, using a hardcoded naming convention for now.
9291
string outputName = translationGraphOutput->getName();

0 commit comments

Comments
 (0)