@@ -52,9 +52,11 @@ const string IMPORT_ALL = " import *";
5252const string MDL_VERSION_1_6 = " 1.6" ;
5353const string MDL_VERSION_1_7 = " 1.7" ;
5454const string MDL_VERSION_1_8 = " 1.8" ;
55+ const string MDL_VERSION_1_9 = " 1.9" ;
5556const string MDL_VERSION_SUFFIX_1_6 = " 1_6" ;
5657const string MDL_VERSION_SUFFIX_1_7 = " 1_7" ;
5758const string MDL_VERSION_SUFFIX_1_8 = " 1_8" ;
59+ const string MDL_VERSION_SUFFIX_1_9 = " 1_9" ;
5860
5961} // anonymous namespace
6062
@@ -191,6 +193,27 @@ ShaderPtr MdlShaderGenerator::generate(const string& name, ElementPtr element, G
191193 emitLineBreak (stage);
192194 }
193195
196+ // Emit shader inputs that have been filtered during printing of the public interface
197+ const string uniformPrefix = _syntax->getUniformQualifier () + " " ;
198+ for (ShaderGraphInputSocket* inputSocket : graph.getInputSockets ())
199+ {
200+ if (inputSocket->getConnections ().size () &&
201+ (inputSocket->getType ().getSemantic () == TypeDesc::SEMANTIC_SHADER ||
202+ inputSocket->getType ().getSemantic () == TypeDesc::SEMANTIC_CLOSURE ||
203+ inputSocket->getType ().getSemantic () == TypeDesc::SEMANTIC_MATERIAL))
204+ {
205+ const string& qualifier = inputSocket->isUniform () || inputSocket->getType () == Type::FILENAME
206+ ? uniformPrefix
207+ : EMPTY_STRING;
208+ const string& type = _syntax->getTypeName (inputSocket->getType ());
209+
210+ emitLineBegin (stage);
211+ emitString (qualifier + type + " " + inputSocket->getVariable () + " = " , stage);
212+ emitString (_syntax->getDefaultValue (inputSocket->getType (), true ), stage);
213+ emitLineEnd (stage, true );
214+ }
215+ }
216+
194217 // Emit all texturing nodes. These are inputs to any
195218 // closure/shader nodes and need to be emitted first.
196219 emitFunctionCalls (graph, context, stage, ShaderNode::Classification::TEXTURE);
@@ -215,6 +238,7 @@ ShaderPtr MdlShaderGenerator::generate(const string& name, ElementPtr element, G
215238 const string result = getUpstreamResult (outputSocket, context);
216239
217240 const TypeDesc outputType = outputSocket->getType ();
241+ // Try to return some meaningful color in case the output is not a material
218242 if (graph.hasClassification (ShaderNode::Classification::TEXTURE))
219243 {
220244 if (outputType == Type::DISPLACEMENTSHADER)
@@ -229,7 +253,25 @@ ShaderPtr MdlShaderGenerator::generate(const string& name, ElementPtr element, G
229253 else
230254 {
231255 emitLine (" float3 displacement__ = float3(0.0)" , stage);
232- emitLine (" color finalOutput__ = mk_color3(" + result + " )" , stage);
256+ std::string finalOutput = " mk_color3(0.0)" ;
257+ if (outputType == Type::BOOLEAN)
258+ finalOutput = result + " ? mk_color3(0.0, 1.0, 0.0) : mk_color3(1.0, 0.0, 0.0)" ;
259+ else if (outputType == Type::INTEGER)
260+ finalOutput = " mk_color3(" + result + " / 100)" ; // arbitrary
261+ else if (outputType == Type::FLOAT)
262+ finalOutput = " mk_color3(" + result + " )" ;
263+ else if (outputType == Type::VECTOR2)
264+ finalOutput = " mk_color3(" + result + " .x, " + result + " .y, 0.0)" ;
265+ else if (outputType == Type::VECTOR3)
266+ finalOutput = " mk_color3(" + result + " )" ;
267+ else if (outputType == Type::COLOR3)
268+ finalOutput = result;
269+ else if (outputType == Type::COLOR4)
270+ finalOutput = result + " .rgb" ;
271+ else if (outputType == Type::MATRIX33 || outputType == Type::MATRIX44)
272+ finalOutput = " mk_color3(" + result + " [0][0], " + result + " [1][1], " + result + " [2][2])" ;
273+
274+ emitLine (" color finalOutput__ = " + finalOutput, stage);
233275 }
234276
235277 // End shader body
@@ -527,6 +569,13 @@ ShaderPtr MdlShaderGenerator::createShader(const string& name, ElementPtr elemen
527569 // and are editable by users.
528570 if (inputSocket->getConnections ().size () && graph->isEditable (*inputSocket))
529571 {
572+ if (inputSocket->getType ().getSemantic () == TypeDesc::SEMANTIC_SHADER ||
573+ inputSocket->getType ().getSemantic () == TypeDesc::SEMANTIC_CLOSURE ||
574+ inputSocket->getType ().getSemantic () == TypeDesc::SEMANTIC_MATERIAL)
575+ {
576+ continue ;
577+ }
578+
530579 inputs->add (inputSocket->getSelf ());
531580 }
532581 }
@@ -537,7 +586,7 @@ ShaderPtr MdlShaderGenerator::createShader(const string& name, ElementPtr elemen
537586 outputs->add (outputSocket->getSelf ());
538587 }
539588
540- // MDL does not allow varying data connected to transmission IOR.
589+ // MDL does not allow varying data connected to transmission IOR until MDL 1.9 .
541590 // We must find all uses of transmission IOR and make sure we don't
542591 // have a varying connection to it. If a varying connection is found
543592 // we break that connection and revert to using default value on that
@@ -552,8 +601,14 @@ ShaderPtr MdlShaderGenerator::createShader(const string& name, ElementPtr elemen
552601 // this fix will disconnect the transmission IOR on the inside, but
553602 // still support the connection to reflection IOR.
554603 //
555- if (graph->hasClassification (ShaderNode::Classification::SHADER) ||
556- graph->hasClassification (ShaderNode::Classification::CLOSURE))
604+ GenMdlOptions::MdlVersion version = getMdlVersion (context);
605+ bool uniformIorRequired =
606+ version == GenMdlOptions::MdlVersion::MDL_1_6 ||
607+ version == GenMdlOptions::MdlVersion::MDL_1_7 ||
608+ version == GenMdlOptions::MdlVersion::MDL_1_8;
609+ if (uniformIorRequired && (
610+ graph->hasClassification (ShaderNode::Classification::SHADER) ||
611+ graph->hasClassification (ShaderNode::Classification::CLOSURE)))
557612 {
558613 // Find dependencies on transmission IOR.
559614 std::set<ShaderGraph*> graphsWithIorDependency;
@@ -641,10 +696,15 @@ void MdlShaderGenerator::emitShaderInputs(const DocumentPtr doc, const VariableB
641696 }
642697}
643698
644- void MdlShaderGenerator::emitMdlVersionNumber (GenContext& context, ShaderStage& stage ) const
699+ GenMdlOptions::MdlVersion MdlShaderGenerator::getMdlVersion (GenContext& context) const
645700{
646701 GenMdlOptionsPtr options = context.getUserData <GenMdlOptions>(GenMdlOptions::GEN_CONTEXT_USER_DATA_KEY);
647- GenMdlOptions::MdlVersion version = options ? options->targetVersion : GenMdlOptions::MdlVersion::MDL_LATEST;
702+ return options ? options->targetVersion : GenMdlOptions::MdlVersion::MDL_LATEST;
703+ }
704+
705+ void MdlShaderGenerator::emitMdlVersionNumber (GenContext& context, ShaderStage& stage) const
706+ {
707+ GenMdlOptions::MdlVersion version = getMdlVersion (context);
648708
649709 emitLineBegin (stage);
650710 emitString (" mdl " , stage);
@@ -656,30 +716,35 @@ void MdlShaderGenerator::emitMdlVersionNumber(GenContext& context, ShaderStage&
656716 case GenMdlOptions::MdlVersion::MDL_1_7:
657717 emitString (MDL_VERSION_1_7, stage);
658718 break ;
719+ case GenMdlOptions::MdlVersion::MDL_1_8:
720+ emitString (MDL_VERSION_1_8, stage);
721+ break ;
659722 default :
660- // GenMdlOptions::MdlVersion::MDL_1_8
723+ // GenMdlOptions::MdlVersion::MDL_1_9
661724 // GenMdlOptions::MdlVersion::MDL_LATEST
662- emitString (MDL_VERSION_1_8 , stage);
725+ emitString (MDL_VERSION_1_9 , stage);
663726 break ;
664727 }
665728 emitLineEnd (stage, true );
666729}
667730
731+
668732const string& MdlShaderGenerator::getMdlVersionFilenameSuffix (GenContext& context) const
669733{
670- GenMdlOptionsPtr options = context.getUserData <GenMdlOptions>(GenMdlOptions::GEN_CONTEXT_USER_DATA_KEY);
671- GenMdlOptions::MdlVersion version = options ? options->targetVersion : GenMdlOptions::MdlVersion::MDL_LATEST;
734+ GenMdlOptions::MdlVersion version = getMdlVersion (context);
672735
673736 switch (version)
674737 {
675738 case GenMdlOptions::MdlVersion::MDL_1_6:
676739 return MDL_VERSION_SUFFIX_1_6;
677740 case GenMdlOptions::MdlVersion::MDL_1_7:
678741 return MDL_VERSION_SUFFIX_1_7;
742+ case GenMdlOptions::MdlVersion::MDL_1_8:
743+ return MDL_VERSION_SUFFIX_1_8;
679744 default :
680- // GenMdlOptions::MdlVersion::MDL_1_8
745+ // GenMdlOptions::MdlVersion::MDL_1_9
681746 // GenMdlOptions::MdlVersion::MDL_LATEST
682- return MDL_VERSION_SUFFIX_1_8 ;
747+ return MDL_VERSION_SUFFIX_1_9 ;
683748 }
684749}
685750
0 commit comments