Skip to content

Early lobe pruning during ShaderGraph construction#2844

Open
ppenenko wants to merge 1 commit intoAcademySoftwareFoundation:mainfrom
autodesk-forks:ppenenko/lobe_pruning
Open

Early lobe pruning during ShaderGraph construction#2844
ppenenko wants to merge 1 commit intoAcademySoftwareFoundation:mainfrom
autodesk-forks:ppenenko/lobe_pruning

Conversation

@ppenenko
Copy link
Copy Markdown
Contributor

@ppenenko ppenenko commented Mar 31, 2026

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::enableLobePruning is 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 the optimize call, 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 in GenContext via NodeGraphTopologyCache.
  • NodeGraphPermutation (new): Lightweight object representing a specific pruning configuration for a call site, produced by NodeGraphTopology::createPermutation().
  • CompoundNode / ShaderGenerator changes: Thread the permutation through createShaderNodeImplForNodeGraph()ShaderGraph::create()createConnectedNodes(), where shouldPrune() 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:

Results

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:

diff_traces compilation time chart

The lines of code metric also shows a clear reduction, reflecting the decreased shader complexity:
diff_shaders LOC chart

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-opt or 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.

Signed-off-by: Pavlo Penenko <pavlo.penenko@autodesk.com>
@ppenenko ppenenko force-pushed the ppenenko/lobe_pruning branch from 1f86c5d to 27d07e4 Compare March 31, 2026 14:59
@ppenenko ppenenko marked this pull request as ready for review March 31, 2026 21:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant