Shader Dynamic branch not working?

Some info from chat gpt…

When Dynamic Branching Can Hurt Performance:

  1. Branch Divergence:
  • GPUs execute shaders in groups of pixels (called warps or wavefronts). If pixels in the same group take different branches, the GPU must evaluate both branches, leading to reduced efficiency. This is known as branch divergence.
  1. Overhead of Branching:
  • Adding branches introduces some overhead due to the additional instructions and potential pipeline stalls. If the branch conditions are not effective in skipping significant work, this overhead might outweigh the benefits.


Coherent Branches:

  • Ensure that the branches are likely to be coherent within a small area of the screen. For example, using dynamic branching based on screen-space coordinates can lead to divergence, but using it based on material properties (like roughness or metallic) can be more coherent.

Since custom expression supports multiple outputs, most of the whine regarding lack of flow control in that thread becomes obsolete. Put what you need to branch in custom node.

dont really know how to write code for that. Can do bare minimum. Are you saying if i input 2 seperate branches of nodes into the custom node with 2 inputs … thatn i can output whichever i decide by a simple say third input like a bool…? And it would work to save performance?

in the material graph → right click → custom

click the node

on the left panel, add yer inputs, outputs, and stuff your custom-code in the big-box

further up the thread is an example of just this + a couple videos, one of them is a how-to

tl;dr put ALL your custom-code, with the outer-branch in the custom-node, since your alpha is always-run to determine what path to take it will be run in the main-graph and passed as an input to the custom-node. the actual switching of one path or the other is inside the custom-node so that is where your either-this or that code goes, what actually executes based on the-test

i just tested the dynamic branch node with profile gpu with the following settings…

r.ShowMaterialDrawEvents 1
r.EmitMeshDrawEvents 1
r.Nanite.ShowMeshDrawEvents 1

This displayed the amount of ms … basically with dynamic branching its .19ms without its .15ms…

So the dynamic branching node actually seems to be more expensive according to profile gpu. So does not seem to work…

I do not know how to write an entire shader branch in code so i cant really do that…

The way I taught myself the little I know is to make something basic like UVs, texture-sampler, do a multiply for tint. Compile and extract the HLSL from unreal to see how it’s compiled down. Add things and see how the compiled code changes.

Stuff like this guy’s channel also help:

Look for examples of custom-code on the forums. It can help to see multiple examples too.

IF you can code in general, it’s not hard, but like any language, learning the specific-syntax, etc.

I’m NOT an expert, but good tech-support skills, so I can take something functional apart, see where it’s a loop, a this, or a that, and then reassemble as you might need. GL!