Early lobe pruning during ShaderGraph construction#2844
Early lobe pruning during ShaderGraph construction#2844ppenenko wants to merge 2 commits intoAcademySoftwareFoundation:mainfrom
Conversation
Signed-off-by: Pavlo Penenko <pavlo.penenko@autodesk.com>
1f86c5d to
27d07e4
Compare
Resolve conflicts in ShaderGraph.h/.cpp: keep both NodeGraphPermutation forward declaration and NodeGraphTopology.h include (from this PR) alongside ShaderGraphRefactor forward declaration and include (from AcademySoftwareFoundation#2832).
|
Thanks for putting together this proposal, @ppenenko! The shader source size and compile-time reductions are great to see, and the benefits of this change are clear. One choice that I'd like to discuss before we proceed further along this path: What are your thoughts on organizing this as a new Here are a few potential benefits of this approach:
I think the strongest argument for your construction-time approach is that lobe pruning is call-site-specialized in a way the existing refactor passes aren't. The same NodeGraph needs to yield different ShaderGraphs depending on the parent material's inputs. That's not trivial, but I don't think it's blocking; a refactor pass running inside The remaining benefit of your proposed approach is avoiding Let me know your thoughts! |
Summary
This PR partially implements #2680 - an optional early pruning optimization to MaterialX shader generation. The implementation is heavily inspired by the OpenUSD PR PixarAnimationStudios/OpenUSD#3525
When
GenOptions::enableLobePruningis enabled, the system analyzes NodeGraph topology to identify topological ("permutation-defining") inputs (mix weights, multiply factors) that, when hardcoded to 0 or 1 at a call site, allow entire dead branches to be pruned. In contrast to #2499 and #2832 which rely on rewriting the already-constructed ShaderGraph in theoptimizecall, this PR applies pruning at an earlier stage - during ShaderGraph construction, during the NodeGraph traversal. If a sub-node is found to be prunable, the traversal skips it entirely. This allows us to save time even at the codegen stage.Key components
NodeGraphTopology(new): Statically analyzes a NodeGraph to identify topological inputs and which internal nodes each can eliminate. Results are cached per NodeGraph inGenContextviaNodeGraphTopologyCache.NodeGraphPermutation(new): Lightweight object representing a specific pruning configuration for a call site, produced byNodeGraphTopology::createPermutation().CompoundNode/ ShaderGenerator changes: Thread the permutation throughcreateShaderNodeImplForNodeGraph()→ShaderGraph::create()→createConnectedNodes(), whereshouldPrune()skips dead nodes before they are instantiated.GenOptions::enableLobePruning: Runtime flag (default off) to enable the optimization. Wired through test suite options.Measurement infrastructure
The optimization was measured with a set of companion PRs that add profiling and comparison tooling to the MaterialX test suite:
envSampleCountandframesPerMaterialtest suite optionsdiff_test_runs)MX_TRACE_ASYNCResults
Measured on the MaterialX test suite (baseline vs.
enableLobePruning=true).GPU frame time has not improved noticeably.
Shader compilation time shows clear improvement for materials with prunable lobes:
The lines of code metric also shows a clear reduction, reflecting the decreased shader complexity:

Further work
This PR performs what is effectively early dead code elimination (DCE) based on input values that are known to be hardcoded for all possible instances of the material - for example, unconnected subnode inputs assuming default values. This is the same optimization that the downstream shader compiler (e.g.
glslang+spirv-optor the video driver compiler) would eventually perform, resulting in an equally optimized final shader binary, which explains the lack of improvement in GPU frame time. However, DCE performed earlier, during MaterialX codegen is cheaper and saves the more expensive time overhead of DCE in the compiler.A natural follow-up is to proactively generate more optimal shader permutations by specializing on the topological input values encountered at run time, similar to Jerry's approach in PixarAnimationStudios/OpenUSD#3525.