Skip to content

Clatify constant detection / folding logic for shader generation #2748

@kwokcb

Description

@kwokcb

Proposal

It would be useful to clarify the logic for how nodes, and nodedefs can be classified as "constants" and hence allow constant folding.
( This is related to this issue)

Exploration

Currently the code for shader generation relies on the "classification" flags set on a shader node.

  • All nodes are classified as "textures" (TEXTURE) already.
  • dot nodes have a DOT classification
  • constant nodes have a CONSTANT classification

There is a mechanism to "promote" child shader node classifications to parent "compound" nodes. Compound nodes may contain children which are compounds or not.

This makes sense for example for classifying if something can be "sampled" as a texture.

It should be determined if this makes sense when used for constant promotion where:

  • A compound can be both a CONSTANT and other classifications (including non-CONSTANT)

and constant folding where:

  • The classification is used to perform a bypass of the node if it is at least a CONSTANT (not exclusively one)
  • The node is non-compound with at least 1 input resulting in a passthrough of a single input value
  • A node with 0 inputs is invalid but not tested.
  • A node with multiple inputs is unknown.

The relevant code to examine is the:

  • initial code to set node classifications (ShaderNode)
  • the graph "finalize" code which includs (ShaderGraph)
    • execution of classification promotion (CompoundNode and other nodes)
    • execution of constant folding (ShaderGraph)

It seems that the desired effect is to elide only atomic node values which can in turn be performed on parent CONSTANT compounds as well, though the 2nd case is a supposition. Basically you can collapse a nested hierarchy if all descendants are constants ?

A possible change in logic would be to only allow promotion of child classifications for constants if still only a constant
after promotion, and for folding to test that the node does not have any non-constant classifications.

There is also a implied understanding that there is a single input which is marked as uniform. This is
not strictly tested anywhere for folding or promotion.

See this PR for some exploration on this.
The firewall check is only to avoid folding when there are no inputs on the node to fold.

Leaving this note with: @lkerley , @jstone-lucasfilm, @bowald, @niklasharrysson, and @JGamache-autodesk (as Jerry did recent constant folding work).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions