Skip to content

Commit

Permalink
Fix MSL render test when no lights are present (#1290)
Browse files Browse the repository at this point in the history
  • Loading branch information
Morteeza committed Mar 20, 2023
1 parent d32226b commit 9bb45dd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
12 changes: 7 additions & 5 deletions source/MaterialXGenMsl/MslShaderGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1020,15 +1020,17 @@ void MslShaderGenerator::emitPixelStage(const ShaderGraph& graph, GenContext& co
emitLightData(context, stage);
}
}

bool needsLightBuffer = lighting && context.getOptions().hwMaxActiveLightSources > 0;

emitMathMatrixScalarMathOperators(context, stage);
emitLine("struct GlobalContext", stage, false);
emitScopeBegin(stage);
{
emitLine("GlobalContext(", stage, false);
emitGlobalVariables(context, stage, EMIT_GLOBAL_SCOPE_CONTEXT_CONSTRUCTOR_ARGS, false, lighting);
emitGlobalVariables(context, stage, EMIT_GLOBAL_SCOPE_CONTEXT_CONSTRUCTOR_ARGS, false, needsLightBuffer);
emitLine(") : ", stage, false);
emitGlobalVariables(context, stage, EMIT_GLOBAL_SCOPE_CONTEXT_CONSTRUCTOR_INIT, false, lighting);
emitGlobalVariables(context, stage, EMIT_GLOBAL_SCOPE_CONTEXT_CONSTRUCTOR_INIT, false, needsLightBuffer);
emitLine("{}", stage, false);

// Add common math functions
Expand All @@ -1042,7 +1044,7 @@ void MslShaderGenerator::emitPixelStage(const ShaderGraph& graph, GenContext& co
emitTransmissionRender(context, stage);
}

emitGlobalVariables(context, stage, EMIT_GLOBAL_SCOPE_CONTEXT_MEMBER_DECL, false, lighting);
emitGlobalVariables(context, stage, EMIT_GLOBAL_SCOPE_CONTEXT_MEMBER_DECL, false, needsLightBuffer);

// Add shadowing support
bool shadowing = (lighting && context.getOptions().hwShadowMap) ||
Expand Down Expand Up @@ -1214,12 +1216,12 @@ void MslShaderGenerator::emitPixelStage(const ShaderGraph& graph, GenContext& co
setFunctionName("FragmentMain", stage);
const VariableBlock& outputs = stage.getOutputBlock(HW::PIXEL_OUTPUTS);
emitLine("fragment " + outputs.getName() + " FragmentMain(", stage, false);
emitGlobalVariables(context, stage, EMIT_GLOBAL_SCOPE_CONTEXT_ENTRY_FUNCTION_RESOURCES, false, lighting);
emitGlobalVariables(context, stage, EMIT_GLOBAL_SCOPE_CONTEXT_ENTRY_FUNCTION_RESOURCES, false, needsLightBuffer);
emitLine(")", stage, false);
emitScopeBegin(stage);
{
emitString("\tGlobalContext ctx {", stage);
emitGlobalVariables(context, stage, EMIT_GLOBAL_SCOPE_CONTEXT_MEMBER_INIT, false, lighting);
emitGlobalVariables(context, stage, EMIT_GLOBAL_SCOPE_CONTEXT_MEMBER_INIT, false, needsLightBuffer);
emitLine("}", stage, true);
emitLine("return ctx.FragmentMain()", stage, true);
}
Expand Down
15 changes: 9 additions & 6 deletions source/MaterialXTest/MaterialXRenderMsl/RenderMsl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,15 @@ void runBake(mx::DocumentPtr doc, const mx::FileSearchPath& imageSearchPath, con
_lightHandler = mx::LightHandler::create();

// Scan for lights
std::vector<mx::NodePtr> lights;
_lightHandler->findLights(document, lights);
_lightHandler->registerLights(document, lights, context);

// Set the list of lights on the with the generator
_lightHandler->setLightSources(lights);
if (options.enableDirectLighting)
{
std::vector<mx::NodePtr> lights;
_lightHandler->findLights(document, lights);
_lightHandler->registerLights(document, lights, context);

// Set the list of lights on the with the generator
_lightHandler->setLightSources(lights);
}

// Load environment lights.
mx::ImagePtr envRadiance = _renderer->getImageHandler()->acquireImage(options.radianceIBLPath);
Expand Down

0 comments on commit 9bb45dd

Please sign in to comment.