Skip to content

Commit bc3f8ea

Browse files
authored
Replace OSL C++ Material node with data driven implementation (AcademySoftwareFoundation#2477)
Remove OSL C++ Material node in favour of data driven implementation. Simplification of code export allows ignoring of node classification, which the OSL code generator doesn't need to emit the correct code.
1 parent 0888099 commit bc3f8ea

File tree

7 files changed

+29
-156
lines changed

7 files changed

+29
-156
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
void mx_surfacematerial(surfaceshader surface, surfaceshader back, displacementshader disp, output MATERIAL result)
2+
{
3+
float opacity_weight = clamp(surface.opacity, 0.0, 1.0);
4+
result = (surface.bsdf + surface.edf) * opacity_weight + transparent() * (1.0 - opacity_weight);
5+
}

libraries/stdlib/genosl/stdlib_genosl_impl.mtlx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<!-- ======================================================================== -->
1313

1414
<!-- <surfacematerial> -->
15-
<implementation name="IM_surfacematerial_genosl" nodedef="ND_surfacematerial" target="genosl" />
15+
<implementation name="IM_surfacematerial_genosl" nodedef="ND_surfacematerial" file="mx_surfacematerial.osl" function="mx_surfacematerial" target="genosl" />
1616

1717
<!-- <surface_unlit> -->
1818
<implementation name="IM_surface_unlit_genosl" nodedef="ND_surface_unlit" file="mx_surface_unlit.osl" function="mx_surface_unlit" target="genosl" />

source/MaterialXGenOsl/Nodes/MaterialNodeOsl.cpp

Lines changed: 0 additions & 72 deletions
This file was deleted.

source/MaterialXGenOsl/Nodes/MaterialNodeOsl.h

Lines changed: 0 additions & 27 deletions
This file was deleted.

source/MaterialXGenOsl/OslShaderGenerator.cpp

Lines changed: 14 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include <MaterialXGenShader/Nodes/SourceCodeNode.h>
1414

1515
#include <MaterialXGenOsl/Nodes/BlurNodeOsl.h>
16-
#include <MaterialXGenOsl/Nodes/MaterialNodeOsl.h>
1716

1817
MATERIALX_NAMESPACE_BEGIN
1918

@@ -35,12 +34,6 @@ OslShaderGenerator::OslShaderGenerator(TypeSystemPtr typeSystem) :
3534
registerImplementation("IM_blur_vector2_" + OslShaderGenerator::TARGET, BlurNodeOsl::create);
3635
registerImplementation("IM_blur_vector3_" + OslShaderGenerator::TARGET, BlurNodeOsl::create);
3736
registerImplementation("IM_blur_vector4_" + OslShaderGenerator::TARGET, BlurNodeOsl::create);
38-
39-
// <!-- <surface> -->
40-
registerImplementation("IM_surface_" + OslShaderGenerator::TARGET, SourceCodeNode::create);
41-
42-
// <!-- <surfacematerial> -->
43-
registerImplementation("IM_surfacematerial_" + OslShaderGenerator::TARGET, MaterialNodeOsl::create);
4437
}
4538

4639
ShaderPtr OslShaderGenerator::generate(const string& name, ElementPtr element, GenContext& context) const
@@ -58,6 +51,7 @@ ShaderPtr OslShaderGenerator::generate(const string& name, ElementPtr element, G
5851
// Add global constants and type definitions
5952
emitTypeDefinitions(context, stage);
6053
emitLine("#define M_FLOAT_EPS 1e-8", stage, false);
54+
emitLine("closure color null_closure() { closure color null_closure = 0; return null_closure; } ", stage, false);
6155
emitLineBreak(stage);
6256

6357
// Set the include file to use for uv transformations,
@@ -180,22 +174,16 @@ ShaderPtr OslShaderGenerator::generate(const string& name, ElementPtr element, G
180174
}
181175
}
182176

183-
// Emit all texturing nodes. These are inputs to any
184-
// closure/shader nodes and need to be emitted first.
185-
emitFunctionCalls(graph, context, stage, ShaderNode::Classification::TEXTURE);
186-
187-
// Emit function calls for "root" closure/shader nodes.
188-
// These will internally emit function calls for any dependent closure nodes upstream.
177+
// Emit function calls for all nodes in the graph, starting each output
178+
// port and walking backwards along the incoming connections
189179
for (ShaderGraphOutputSocket* socket : graph.getOutputSockets())
190180
{
191181
if (socket->getConnection())
192182
{
193183
const ShaderNode* upstream = socket->getConnection()->getNode();
194-
if (upstream->getParent() == &graph &&
195-
(upstream->hasClassification(ShaderNode::Classification::CLOSURE) ||
196-
upstream->hasClassification(ShaderNode::Classification::SHADER)))
184+
if (upstream->getParent() == &graph)
197185
{
198-
emitFunctionCall(*upstream, context, stage);
186+
emitAllDependentFunctionCalls(*upstream, context, stage);
199187
}
200188
}
201189
}
@@ -304,38 +292,21 @@ ShaderPtr OslShaderGenerator::createShader(const string& name, ElementPtr elemen
304292
return shader;
305293
}
306294

307-
void OslShaderGenerator::emitFunctionCalls(const ShaderGraph& graph, GenContext& context, ShaderStage& stage, uint32_t classification) const
295+
// TODO - determine it's better if this lives in ShaderGenerator as a useful API function
296+
void OslShaderGenerator::emitAllDependentFunctionCalls(const ShaderNode& node, GenContext& context, ShaderStage& stage) const
308297
{
309-
// Special handling for closures functions.
310-
if ((classification & ShaderNode::Classification::CLOSURE) != 0)
298+
// Check if it's emitted already.
299+
if (!stage.isEmitted(node, context))
311300
{
312-
// Emit function calls for closures connected to the outputs.
313-
// These will internally emit other closure function calls
314-
// for upstream nodes if needed.
315-
for (ShaderGraphOutputSocket* outputSocket : graph.getOutputSockets())
301+
// Emit function calls for upstream connected nodes
302+
for (const auto& input : node.getInputs())
316303
{
317-
const ShaderNode* upstream = outputSocket->getConnection() ? outputSocket->getConnection()->getNode() : nullptr;
318-
if (upstream && upstream->hasClassification(classification))
304+
if (const auto& upstream = input->getConnectedSibling())
319305
{
320-
emitFunctionCall(*upstream, context, stage);
306+
emitAllDependentFunctionCalls(*upstream, context, stage);
321307
}
322308
}
323-
}
324-
else
325-
{
326-
// Not a closures graph so just generate all
327-
// function calls in order.
328-
ShaderGenerator::emitFunctionCalls(graph, context, stage, classification);
329-
}
330-
}
331-
332-
void OslShaderGenerator::emitFunctionBodyBegin(const ShaderNode& node, GenContext&, ShaderStage& stage, Syntax::Punctuation punc) const
333-
{
334-
emitScopeBegin(stage, punc);
335-
336-
if (node.hasClassification(ShaderNode::Classification::SHADER) || node.hasClassification(ShaderNode::Classification::CLOSURE))
337-
{
338-
emitLine("closure color null_closure = 0", stage);
309+
stage.addFunctionCall(node, context);
339310
}
340311
}
341312

source/MaterialXGenOsl/OslShaderGenerator.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,8 @@ class MX_GENOSL_API OslShaderGenerator : public ShaderGenerator
4343
/// the element and all dependencies upstream into shader code.
4444
ShaderPtr generate(const string& name, ElementPtr element, GenContext& context) const override;
4545

46-
/// Add all function calls for a graph. If a classification mask is given only functions for
47-
/// nodes matching this classification will be emitted.
48-
void emitFunctionCalls(const ShaderGraph& graph, GenContext& context, ShaderStage& stage, uint32_t classification = 0u) const override;
49-
50-
/// Emit code for starting a new function body.
51-
void emitFunctionBodyBegin(const ShaderNode& node, GenContext& context, ShaderStage& stage, Syntax::Punctuation punc = Syntax::CURLY_BRACKETS) const override;
46+
/// Add all function calls for a node, and all upstream nodes.
47+
void emitAllDependentFunctionCalls(const ShaderNode& node, GenContext& context, ShaderStage& stage) const;
5248

5349
/// Unique identifier for this generator target
5450
static const string TARGET;

source/MaterialXGenOsl/OslSyntax.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ OslSyntax::OslSyntax(TypeSystemPtr typeSystem) : Syntax(typeSystem)
388388
std::make_shared<ScalarTypeSyntax>(
389389
this,
390390
"BSDF",
391-
"null_closure",
391+
"null_closure()",
392392
"0",
393393
"closure color",
394394
"#define BSDF closure color"));
@@ -398,7 +398,7 @@ OslSyntax::OslSyntax(TypeSystemPtr typeSystem) : Syntax(typeSystem)
398398
std::make_shared<ScalarTypeSyntax>(
399399
this,
400400
"EDF",
401-
"null_closure",
401+
"null_closure()",
402402
"0",
403403
"closure color",
404404
"#define EDF closure color"));
@@ -408,7 +408,7 @@ OslSyntax::OslSyntax(TypeSystemPtr typeSystem) : Syntax(typeSystem)
408408
std::make_shared<ScalarTypeSyntax>(
409409
this,
410410
"VDF",
411-
"null_closure",
411+
"null_closure()",
412412
"0",
413413
"closure color",
414414
"#define VDF closure color"));
@@ -418,7 +418,7 @@ OslSyntax::OslSyntax(TypeSystemPtr typeSystem) : Syntax(typeSystem)
418418
std::make_shared<AggregateTypeSyntax>(
419419
this,
420420
"surfaceshader",
421-
"surfaceshader(null_closure, null_closure, 1.0)",
421+
"surfaceshader(null_closure(), null_closure(), 1.0)",
422422
"{ 0, 0, 1.0 }",
423423
"closure color",
424424
"struct surfaceshader { closure color bsdf; closure color edf; float opacity; };"));
@@ -428,7 +428,7 @@ OslSyntax::OslSyntax(TypeSystemPtr typeSystem) : Syntax(typeSystem)
428428
std::make_shared<ScalarTypeSyntax>(
429429
this,
430430
"volumeshader",
431-
"null_closure",
431+
"null_closure()",
432432
"0",
433433
"closure color",
434434
"#define volumeshader closure color"));
@@ -448,7 +448,7 @@ OslSyntax::OslSyntax(TypeSystemPtr typeSystem) : Syntax(typeSystem)
448448
std::make_shared<ScalarTypeSyntax>(
449449
this,
450450
"lightshader",
451-
"null_closure",
451+
"null_closure()",
452452
"0",
453453
"closure color",
454454
"#define lightshader closure color"));
@@ -458,7 +458,7 @@ OslSyntax::OslSyntax(TypeSystemPtr typeSystem) : Syntax(typeSystem)
458458
std::make_shared<ScalarTypeSyntax>(
459459
this,
460460
"MATERIAL",
461-
"null_closure",
461+
"null_closure()",
462462
"0",
463463
"closure color",
464464
"#define MATERIAL closure color"));

0 commit comments

Comments
 (0)