Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lobe pruning shader optimization #4016

Merged

Conversation

JGamache-autodesk
Copy link
Collaborator

Introduces the lobe pruning concept to MaterialX shaders generated by either MayaUSD or LookdevX.

We look into shader graph implemnetation and find where we can cut the graph to simplify it based on the value of the main lobe attributes.

This means a shader with a subsurface weight of zero will completely prune the evaluation of the subsurface, while a value of 1 will completely prune the evaluation of the dielectric base.

This provides interesting performance values:

MayaUSD: Tumbling speed of a stage with a 11x11x11 set of cubes with a red OpenPBR shader

                initial  optimized
Default light   434fps   441fps
Dome light       17fps   115fps

LookdevX/MaterialX: Tumbling speed of a heavy geometry scene from MAYA-136105 using OpenPBR surface

                initial  optimized
Default light   192fps   476fps
Dome light      7.6fps    65fps

Technical side: The code looks for nodes in the nodegraph implementation and checks any node that directly connects to the interface on a 0-1 slider:

  • Mix nodes: If the mix value if 0 or 1, forward the value/connection of the bg or fg port to the node directly upstream. Delete node.
  • Multiply nodes: If any value is zero, write zero to the corresponding port on the upstream node. Delete node.
  • PBR nodes: If the weight parameter is zero, substitute with a no-op PBR node. Delete node.

This makes sure the ShaderGen works on a reduced codebase and greatly reduces code size and complexity by disconnecting unused subgraphs and skipping black PBR calls.

Introduces the lobe pruning concept to MaterialX shaders generated by
either MayaUSD or LookdevX.

We look into shader graph implemnetation and find where we can cut the
graph to simplify it based on the value of the main lobe attributes.

This means a shader with a subsurface weight of zero will completely
prune the evaluation of the subsurface, while a value of 1 will
completely prune the evaluation of the dielectric base.

This provides interesting performance values:

```
MayaUSD: Tumbling speed of a stage with a 11x11x11 set of cubes with a red OpenPBR shader

                initial  optimized
Default light   434fps   441fps
Dome light       17fps   115fps

LookdevX/MaterialX: Tumbling speed of a heavy geometry scene from MAYA-136105 using OpenPBR surface

                initial  optimized
Default light   192fps   476fps
Dome light      7.6fps    65fps
```

Technical side: The code looks for nodes in the nodegraph implementation and checks any node that directly connects to the interface on a 0-1 slider:
- Mix nodes: If the mix value if 0 or 1, forward the value/connection of the bg or fg port to the node directly upstream. Delete node.
- Multiply nodes: If any value is zero, write zero to the corresponding port on the upstream node. Delete node.
- PBR nodes: If the weight parameter is zero, substitute with a no-op PBR node. Delete node.
This makes sure the ShaderGen works on a reduced codebase and greatly reduces code size and complexity by disconnecting unused subgraphs and skipping black PBR calls.
Copy link
Collaborator

@frohnej-adsk frohnej-adsk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice!

I left a few comments, but they are mostly questions or very minor. Not sure if any of them are blocking. Please double check and make any changes you deem necessary. I'll approve after.

lib/mayaUsd/render/MaterialXGenOgsXml/LobePruner.cpp Outdated Show resolved Hide resolved
lib/mayaUsd/render/MaterialXGenOgsXml/LobePruner.cpp Outdated Show resolved Hide resolved
lib/mayaUsd/render/MaterialXGenOgsXml/LobePruner.cpp Outdated Show resolved Hide resolved
@@ -0,0 +1,606 @@
#include "LobePruner.h"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class can be copied to USD in HdStorm to allow the same optimizations for usdview or other Storm clients.

Requires the dark closure node as well in the USD shadergen.

lib/mayaUsd/render/MaterialXGenOgsXml/LobePruner.cpp Outdated Show resolved Hide resolved
lib/mayaUsd/render/MaterialXGenOgsXml/LobePruner.cpp Outdated Show resolved Hide resolved
Copy link
Collaborator

@frohnej-adsk frohnej-adsk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thank you for the explanations!

@JGamache-autodesk JGamache-autodesk added the ready-for-merge Development process is finished, PR is ready for merge label Dec 2, 2024
class MAYAUSD_API_PUBLIC TopoNeutralGraph
{
public:
TopoNeutralGraph(const MaterialX::ElementPtr&);
TopoNeutralGraph(const MaterialX::ElementPtr&, const LobePruner::Ptr& lobePruner);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JGamache-autodesk Can you confirm that adding this overload does not break binary compatibility?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I confirm it does not break since it is not a virtual function.

* \return a string vector containing all paths to attibutes that were used to optimize nodes in
* this graph.
*/
const MaterialX::StringVec& getOptimizedAttributes() const;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. Not virtual. And since the actual implementation is hidden underneath an Impl pointer, all changes to the core class are unexposed and non-breaking.

@seando-adsk seando-adsk added the vp2renderdelegate Related to VP2RenderDelegate label Dec 3, 2024
@seando-adsk seando-adsk merged commit 8883de1 into dev Dec 3, 2024
11 checks passed
@seando-adsk seando-adsk deleted the gamaj/MAYA-136105/optimize_materialx_via_lobe_pruning branch December 3, 2024 16:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready-for-merge Development process is finished, PR is ready for merge vp2renderdelegate Related to VP2RenderDelegate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants