Skip to content

Commit 1d066bb

Browse files
authored
Add code generation hints support (#1954)
Initial implementation of code generation hints, as described in the 1.39 specification
1 parent 6021128 commit 1d066bb

7 files changed

Lines changed: 90 additions & 8 deletions

File tree

libraries/bxdf/open_pbr_surface.mtlx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
doc="Index of refraction of the dielectric base." />
2424
<input name="specular_roughness_anisotropy" type="float" value="0.0" uimin="0.0" uimax="1.0" uiname="Specular Anisotropy" uifolder="Specular" uiadvanced="true"
2525
doc="The directional bias of the roughness of the metal/dielectric base, resulting in increasingly stretched highlights along the tangent direction." />
26-
<input name="transmission_weight" type="float" value="0.0" uimin="0.0" uimax="1.0" uiname="Transmission Weight" uifolder="Transmission" uiadvanced="true"
26+
<input name="transmission_weight" type="float" value="0.0" uimin="0.0" uimax="1.0" uiname="Transmission Weight" uifolder="Transmission" uiadvanced="true" hint="transparency"
2727
doc="Mixture weight between the transparent and opaque dielectric base. The greater the value the more transparent the material." />
2828
<input name="transmission_color" type="color3" value="1, 1, 1" uimin="0,0,0" uimax="1,1,1" uiname="Transmission Color" uifolder="Transmission" uiadvanced="true"
2929
doc="Controls color of the transparent base due to Beer's law volumetric absorption under the surface (reverts to a non-physical tint when transmission_depth is zero)." />
@@ -75,7 +75,7 @@
7575
doc="The amount of emitted light, as a luminance in nits." />
7676
<input name="emission_color" type="color3" value="1, 1, 1" uimin="0,0,0" uimax="1,1,1" uiname="Emission Color" uifolder="Emission"
7777
doc="The color of the emitted light." />
78-
<input name="geometry_opacity" type="float" value="1" uimin="0" uimax="1" uiname="Opacity" uifolder="Geometry"
78+
<input name="geometry_opacity" type="float" value="1" uimin="0" uimax="1" uiname="Opacity" uifolder="Geometry" hint="opacity"
7979
doc="The opacity of the entire material." />
8080
<input name="geometry_thin_walled" type="boolean" value="false" uiname="Thin Walled" uifolder="Geometry" uiadvanced="true"
8181
doc="If true the surface is double-sided and represents an infinitesimally thin shell. Suitable for extremely geometrically thin objects such as leaves or paper." />
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0"?>
2+
<materialx version="1.39" colorspace="lin_rec709">
3+
<surfacematerial name="geom_hint" type="material">
4+
<input name="surfaceshader" type="surfaceshader" nodename="geometric_opacity_hint" />
5+
</surfacematerial>
6+
<open_pbr_surface name="geometric_opacity_hint" type="surfaceshader">
7+
<input name="geometry_opacity" type="float" value="0.5" />
8+
</open_pbr_surface>
9+
<surfacematerial name="transp_hint" type="material">
10+
<input name="surfaceshader" type="surfaceshader" nodename="transparency_weight_hint" />
11+
</surfacematerial>
12+
<open_pbr_surface name="transparency_weight_hint" type="surfaceshader">
13+
<input name="transmission_weight" type="float" value="0.8" />
14+
</open_pbr_surface>
15+
</materialx>

source/MaterialXCore/Definition.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,19 @@ InterfaceElementPtr NodeDef::getImplementation(const string& target) const
100100
return InterfaceElementPtr();
101101
}
102102

103+
const StringMap NodeDef::getInputHints() const
104+
{
105+
StringMap hints;
106+
for (InputPtr input : getActiveInputs())
107+
{
108+
if (input->hasHint())
109+
{
110+
hints[input->getName()] = input->getHint();
111+
}
112+
}
113+
return hints;
114+
}
115+
103116
bool NodeDef::validate(string* message) const
104117
{
105118
bool res = true;

source/MaterialXCore/Definition.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,13 @@ class MX_CORE_API NodeDef : public InterfaceElement
146146
/// an Implementation element or a NodeGraph element.
147147
InterfaceElementPtr getImplementation(const string& target = EMPTY_STRING) const;
148148

149+
/// @}
150+
/// @name Hints
151+
/// @{
152+
153+
/// Return list of input hint pairs of the form { input_name, hint_string }
154+
const StringMap getInputHints() const;
155+
149156
/// @}
150157
/// @name Validation
151158
/// @{

source/MaterialXCore/Interface.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ const string InterfaceElement::TARGET_ATTRIBUTE = "target";
2020
const string InterfaceElement::VERSION_ATTRIBUTE = "version";
2121
const string InterfaceElement::DEFAULT_VERSION_ATTRIBUTE = "isdefaultversion";
2222
const string Input::DEFAULT_GEOM_PROP_ATTRIBUTE = "defaultgeomprop";
23+
const string Input::HINT_ATTRIBUTE = "hint";
24+
const string Input::TRANSPARENCY_HINT = "transparency";
25+
const string Input::OPACITY_HINT = "opacity";
26+
const string Input::ANISOTROPY_HINT = "anisotropy";
2327
const string Output::DEFAULT_INPUT_ATTRIBUTE = "defaultinput";
2428

2529
//

source/MaterialXCore/Interface.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,28 @@ class MX_CORE_API Input : public PortElement
219219
/// for this input.
220220
InputPtr getInterfaceInput() const;
221221

222+
/// @}
223+
/// @name Hints
224+
/// @{
225+
226+
/// Return true if the input has a hint
227+
bool hasHint() const
228+
{
229+
return hasAttribute(HINT_ATTRIBUTE);
230+
}
231+
232+
/// Return the code generation hint
233+
const string& getHint() const
234+
{
235+
return getAttribute(HINT_ATTRIBUTE);
236+
}
237+
238+
// Set the code generation hint
239+
void setHint(const string& hint)
240+
{
241+
setAttribute(HINT_ATTRIBUTE, hint);
242+
}
243+
222244
/// @}
223245
/// @name Validation
224246
/// @{
@@ -232,6 +254,10 @@ class MX_CORE_API Input : public PortElement
232254
public:
233255
static const string CATEGORY;
234256
static const string DEFAULT_GEOM_PROP_ATTRIBUTE;
257+
static const string HINT_ATTRIBUTE;
258+
static const string TRANSPARENCY_HINT;
259+
static const string OPACITY_HINT;
260+
static const string ANISOTROPY_HINT;
235261
};
236262

237263
/// @class Output

source/MaterialXGenShader/Util.cpp

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ using OpaqueTestPair = std::pair<string, float>;
1616
using OpaqueTestPairList = vector<OpaqueTestPair>;
1717

1818
// Inputs on a surface shader which are checked for transparency
19-
const OpaqueTestPairList inputPairList = { { "opacity", 1.0f },
20-
{ "existence", 1.0f },
21-
{ "alpha", 1.0f },
22-
{ "transmission", 0.0f } };
19+
const OpaqueTestPairList DEFAULT_INPUT_PAIR_LIST = { { "opacity", 1.0f },
20+
{ "existence", 1.0f },
21+
{ "alpha", 1.0f },
22+
{ "transmission", 0.0f } };
2323

2424
const string MIX_CATEGORY("mix");
2525
const string MIX_FG_INPUT("fg");
@@ -116,6 +116,23 @@ bool isTransparentShaderNode(NodePtr node, NodePtr interfaceNode)
116116
return false;
117117
}
118118

119+
// Check against nodedef input hints
120+
OpaqueTestPairList inputPairList = DEFAULT_INPUT_PAIR_LIST;
121+
NodeDefPtr nodeDef = node->getNodeDef();
122+
StringMap nodeDefList = nodeDef ? nodeDef->getInputHints() : StringMap();
123+
for (auto &item : nodeDefList)
124+
{
125+
string inputName = item.first;
126+
if (item.second == Input::TRANSPARENCY_HINT)
127+
{
128+
inputPairList.push_back(std::make_pair(inputName, 0.0f) );
129+
}
130+
else if (item.second == Input::OPACITY_HINT)
131+
{
132+
inputPairList.push_back(std::make_pair(inputName, 1.0f));
133+
}
134+
}
135+
119136
// Check against the interface if a node is passed in to check against
120137
OpaqueTestPairList interfaceNames;
121138
if (interfaceNode)
@@ -167,8 +184,8 @@ bool isTransparentShaderNode(NodePtr node, NodePtr interfaceNode)
167184
NodePtr inputNode = checkInput->getConnectedNode();
168185
if (inputNode)
169186
{
170-
NodeDefPtr nodeDef = inputNode->getNodeDef();
171-
string nodeGroup = nodeDef ? nodeDef->getAttribute(NodeDef::NODE_GROUP_ATTRIBUTE) : EMPTY_STRING;
187+
NodeDefPtr inputNodeDef = inputNode->getNodeDef();
188+
string nodeGroup = inputNodeDef ? inputNodeDef->getAttribute(NodeDef::NODE_GROUP_ATTRIBUTE) : EMPTY_STRING;
172189
if (nodeGroup != NodeDef::ADJUSTMENT_NODE_GROUP &&
173190
nodeGroup != NodeDef::CHANNEL_NODE_GROUP)
174191
{

0 commit comments

Comments
 (0)