Early lobe pruning during ShaderGraph construction#2844
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! |
|
Hi @jstone-lucasfilm, thanks for the feedback. Let me elaborate what lead me to the current design applying the pruning during shader graph construction. First of all, let me assure you that I had initially invested a lot of time into a prototype applying the optimization at the shader graph rewriting phase, as we initially discussed. However I didn't find that all the necessary metadata was readily available at that late stage - instead it had to be passed down there from the construction phase, complicating the design.
Yes, relying on the magic strings is unfortunate. These strings are inherited from Jerry's original OpenUSD implementation PixarAnimationStudios/OpenUSD#3525 Some, but not all of them do overlap the existing MaterialX/source/MaterialXGenShader/ShaderNode.cpp Lines 288 to 297 in e8c12a2 Actually, I would argue that this is where the token (interned string) pattern would be a natural fit - this is what a compiler would do. Tokens would also help us avoid string comparison overhead and the risk of typos in other areas of the code. For example, the same string literals are compared against in Speaking of classifications, wouldn't the Other magic strings used by this optimization are Regarding reference counting in Second,
In my mind, this is another argument for shared utilities, but not necessarily for collocating the code.
I would actually argue that the early pruning is even more trivially composable with the refactoring passes than if it were a refactoring pass itself. Early pruning simplifies the graph for further downstream optimization, saving work for the refactoring passes. If whole subgraphs are pruned early, refactoring wouldn't even have to run on them.
The permutation being passed to
In my mind, this is the main argument for performing the optimization as early as possible: it saves performance downstream. Creating Shader Graphs is a non-negligible cost - see, for example, the performance capture screenshot in #2820 Construction accounts for ~80% of total codegen time. So even a small percentage reduction in construction is significant in absolute terms. On average, the savings are roughly split 50/50 between the shader graph construction and code emission stages, a few milliseconds each. I believe it's safe to say that it's always going to be strictly more performant than an equivalent graph refactoring pass. Some measurements on my machine:
Finally, I wanted to point out the optimization potential of early topology analysis beyond the current results:
|
|
Thanks for this detailed reply, @ppenenko, and I'm convinced the construction-time approach you've proposed here is worth keeping. I think the path forward is to decouple the two questions we've been bundling together. Where the pruning happens is a performance question, and your data settles it. How the analysis identifies topological inputs is a maintainability question we can resolve independently, and I like your proposal above: rather than collocating the code, let's factor the category/uimin/uimax string handling into shared parsing utilities that both A couple of separate coding issues that I'd also recommend addressing in this PR:
|
createPermutation() can return nullptr for NodeGraphs without topological inputs. Check for null before accessing getKey().
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.