# Shader Dynamic branch not working?

**URL:** <https://forums.unrealengine.com/t/shader-dynamic-branch-not-working/1909078>\
**Category:** Rendering\
**Tags:** branch, unreal-engine, shader\
**Created:** [June 19, 2024, 2:27pm UTC](https://forums.unrealengine.com/t/shader-dynamic-branch-not-working/1909078 "2024-06-19T14:27:33Z")\
**Posts on this page:** 20\
**Page:** 3

<div class="post-metadata">

**Author:** ![kurylo3d](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/kurylo3d/32/1150889_2.png) [@kurylo3d](https://forums.unrealengine.com/u/kurylo3d)\
**Post date:** [July 10, 2024, 6:57pm UTC](https://forums.unrealengine.com/t/shader-dynamic-branch-not-working/1909078/41 "2024-07-10T18:57:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Deathrey](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Deathrey](https://forums.unrealengine.com/u/Deathrey)\
**Post date:** [July 19, 2024, 11:27am UTC](https://forums.unrealengine.com/t/shader-dynamic-branch-not-working/1909078/42 "2024-07-19T11:27:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kurylo3d](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/kurylo3d/32/1150889_2.png) [@kurylo3d](https://forums.unrealengine.com/u/kurylo3d)\
**Post date:** [July 19, 2024, 2:41pm UTC](https://forums.unrealengine.com/t/shader-dynamic-branch-not-working/1909078/43 "2024-07-19T14:41:21Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Frenetic](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/frenetic/32/1142542_2.png) [@Frenetic](https://forums.unrealengine.com/u/Frenetic)\
**Post date:** [July 19, 2024, 2:43pm UTC](https://forums.unrealengine.com/t/shader-dynamic-branch-not-working/1909078/44 "2024-07-19T14:43:48Z")

</div>

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

I seem to be incorrect, see below. ~~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~~

---

<div class="post-metadata">

**Author:** ![kurylo3d](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/kurylo3d/32/1150889_2.png) [@kurylo3d](https://forums.unrealengine.com/u/kurylo3d)\
**Post date:** [July 21, 2024, 4:03pm UTC](https://forums.unrealengine.com/t/shader-dynamic-branch-not-working/1909078/45 "2024-07-21T16:03:38Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![kurylo3d](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/kurylo3d/32/1150889_2.png) [@kurylo3d](https://forums.unrealengine.com/u/kurylo3d)\
**Post date:** [July 21, 2024, 4:04pm UTC](https://forums.unrealengine.com/t/shader-dynamic-branch-not-working/1909078/46 "2024-07-21T16:04:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Frenetic](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/frenetic/32/1142542_2.png) [@Frenetic](https://forums.unrealengine.com/u/Frenetic)\
**Post date:** [July 21, 2024, 5:10pm UTC](https://forums.unrealengine.com/t/shader-dynamic-branch-not-working/1909078/47 "2024-07-21T17:10:58Z")

</div>

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: [https://www.youtube.com/watch?v=bp7REZBV4P4](https://www.youtube.com/watch?v=bp7REZBV4P4)

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!

---

<div class="post-metadata">

**Author:** ![Frenetic](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/frenetic/32/1142542_2.png) [@Frenetic](https://forums.unrealengine.com/u/Frenetic)\
**Post date:** [September 10, 2024, 4:01pm UTC](https://forums.unrealengine.com/t/shader-dynamic-branch-not-working/1909078/48 "2024-09-10T16:01:52Z")

</div>

In practice, this seems to disagree.

It’s Bezier-grass so the mesh is shaped in the shader. As well I have meshes painting into an RVT roughness channel to act as a grass-mask. Pics below but we can see the stuff getting masked out by the red thing, are not being formed as they should be if wind, etc are being applied. We can see they are still just the raw square-thing I used as the base mesh.

My two WPO paths are distinct from one another and it seems only one is executing, whilst both are outside the custom-node.

> **Output plus shader**
>
> ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/f/a/b/fab12a943386a2c5c68045e668750e3e79eb7867.jpeg)  
> ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/5/6/4/564d8caf4f8a490123c8d67d348936e16dbb616a.jpeg)

For the guy asking how to write this, this is the custom-node:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/d/f/edfb841073113be2cb0e33328e9de253fbc08984.png)

Hope this helps.

---

<div class="post-metadata">

**Author:** ![Arkiras](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/arkiras/32/1008648_2.png) [@Arkiras](https://forums.unrealengine.com/u/Arkiras)\
**Post date:** [September 10, 2024, 4:23pm UTC](https://forums.unrealengine.com/t/shader-dynamic-branch-not-working/1909078/49 "2024-09-10T16:23:06Z")

</div>

> [@Frenetic](#):
>
> Pics below but we can see the stuff getting masked out by the red thing, are not being formed as they should be if wind, etc are being applied

Forgive me but I’m not sure I understand what this is supposed to be demonstrating. I expect every method in this thread to execute correctly and produce the same output, the question is do any of the methods actually using dynamic branching to produce that output?

---

<div class="post-metadata">

**Author:** ![Frenetic](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/frenetic/32/1142542_2.png) [@Frenetic](https://forums.unrealengine.com/u/Frenetic)\
**Post date:** [September 10, 2024, 4:34pm UTC](https://forums.unrealengine.com/t/shader-dynamic-branch-not-working/1909078/50 "2024-09-10T16:34:38Z")

</div>

My understanding is there was some confusion around _where_ to put code being branched.

Unless I am mistaken, and I often am, the two paths tehre are mutually exclusive, as borne out in the pic. The flat-unshaped mesh is -200 units under the landscape, so it’s path is correctly executing. The path that shapes the mesh and does the wind-etc is also correctly executing b/c we can see the meshes above ground.

My point was it seems, at least in this case, that the stuff you want to switch between doesn’t seem to have to be _in_ the custom-node. The custom-code is just the switch.

Again, and it’s hard to emote across text, I could be wrong, I Really believe that! So if I am, or I erred, please tell me, but my test?

---

<div class="post-metadata">

**Author:** ![Arkiras](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/arkiras/32/1008648_2.png) [@Arkiras](https://forums.unrealengine.com/u/Arkiras)\
**Post date:** [September 10, 2024, 4:52pm UTC](https://forums.unrealengine.com/t/shader-dynamic-branch-not-working/1909078/51 "2024-09-10T16:52:33Z")

</div>

> [@Frenetic](#):
>
> My understanding is there was some confusion around _where_ to put code being branched.

The question is about whether a “dynamic branch” is happening in the various contexts in which you can construct branching logic in the material editor.

A dynamic branch meaning, a boolean/expression is used to determine whether to do A or B. It only does one.

Most kinds of branching that you can create in the material editor however don’t do that, they evaluate both A and B, then check the boolean/expression and throws away one of them.

For a branch to be an optimization, like the OP wanted, it would need to be a dynamic branch. But as the discourse here has shown… it can be difficult to figure out if/when the material editor is actually creating a real dynamic branch. (dynamic branching isn’t always a performance optimization, sometimes its faster to evaluate both A+B, but that’s an entirely different topic)

> [@Frenetic](#):
>
> My point was it seems, at least in this case, that the stuff you want to switch between doesn’t seem to have to be _in_ the custom-node.

If your only concern is that the output looks like it is supposed to then yes, having an HLSL block with nothing but a branch in it will work fine. As will all the other options, using a ternary, a step function, boolean math, an if node, an if node wrapped in a material function named “dynamic branch”, even a lerp will work. They’ll all get there, but not all of them will actually use a dynamic branch to do it.

---

<div class="post-metadata">

**Author:** ![somawheels](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@somawheels](https://forums.unrealengine.com/u/somawheels)\
**Post date:** [September 19, 2024, 9:13am UTC](https://forums.unrealengine.com/t/shader-dynamic-branch-not-working/1909078/52 "2024-09-19T09:13:36Z")

</div>

It would be great to hear from Epic as to why dynamic branching is not properly implemented in material graphs. Its been like this for so long, and I don’t understand why.

---

<div class="post-metadata">

**Author:** ![Frenetic](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/frenetic/32/1142542_2.png) [@Frenetic](https://forums.unrealengine.com/u/Frenetic)\
**Post date:** [December 1, 2025, 5:36pm UTC](https://forums.unrealengine.com/t/shader-dynamic-branch-not-working/1909078/53 "2025-12-01T17:36:10Z")

</div>

As of 5.7 there does appear to be support for dynamic branching: [https://youtu.be/KYmd\_LNlw2c?t=2290](https://youtu.be/KYmd_LNlw2c?t=2290)

---

<div class="post-metadata">

**Author:** ![Arkiras](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/arkiras/32/1008648_2.png) [@Arkiras](https://forums.unrealengine.com/u/Arkiras)\
**Post date:** [December 1, 2025, 6:00pm UTC](https://forums.unrealengine.com/t/shader-dynamic-branch-not-working/1909078/54 "2025-12-01T18:00:41Z")

</div>

That’s not in 5.7 according to the presentation, it’s future looking as part of the new translator.

Interestingly there is no mention of support for loops which is what I would consider the primary use case for dynamic branching.

---

<div class="post-metadata">

**Author:** ![Frenetic](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/frenetic/32/1142542_2.png) [@Frenetic](https://forums.unrealengine.com/u/Frenetic)\
**Post date:** [December 1, 2025, 6:10pm UTC](https://forums.unrealengine.com/t/shader-dynamic-branch-not-working/1909078/55 "2025-12-01T18:10:08Z")

</div>

My mistake, you are correct. I was searching for it and didn’t realize it came after the future-translator. Good to know it’s on the horizon though!

I can see big wins in Substrate for avoiding the cost of a layer/slab of material if the Alpha is 1 or 0.

---

<div class="post-metadata">

**Author:** ![Arkiras](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/arkiras/32/1008648_2.png) [@Arkiras](https://forums.unrealengine.com/u/Arkiras)\
**Post date:** [December 1, 2025, 6:23pm UTC](https://forums.unrealengine.com/t/shader-dynamic-branch-not-working/1909078/56 "2025-12-01T18:23:18Z")

</div>

Substrate already does this for horizontal blends, you only pay the cost for evaluating both BSDFs at the transition region where the values are between 0-1

And I _think_ it also does this with coverage weighting for vertical layers but I’m not 100% sure.

---

<div class="post-metadata">

**Author:** ![BananableOffense](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@BananableOffense](https://forums.unrealengine.com/u/BananableOffense)\
**Post date:** [December 2, 2025, 4:48am UTC](https://forums.unrealengine.com/t/shader-dynamic-branch-not-working/1909078/57 "2025-12-02T04:48:29Z")

</div>

Yeah, I’ll be thrilled when/if they can eventually support loops. That’ll eliminate basically all of my remaining custom node usage needs. If they can’t do that, hopefully they could at least make calling functions inside of a custom node more intuitive. You have to do some janky stuff using multiple custom nodes just to declare a function so you can loop it.

One important use case for dynamic branching outside of loops is material scalability. Lets say you want the player to have the ability to disable or enable POM on a material via a settings menu. Without a dynamic branch, you must have a separate permutation (for example by using the engine’s built in quality switch) for each material that uses POM - both with and without POM. Or you could set the parameters to 0 but you’d still be paying all the instruction costs, even if you avoid all the POM samples.

But with dynamic branching you can have just one shader permutation that branches to desired version as needed without needing to compile two permutations. It might be slightly more instructions in both cases but that could easily be worthwhile.

---

<div class="post-metadata">

**Author:** ![Frenetic](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/frenetic/32/1142542_2.png) [@Frenetic](https://forums.unrealengine.com/u/Frenetic)\
**Post date:** [December 3, 2025, 5:29pm UTC](https://forums.unrealengine.com/t/shader-dynamic-branch-not-working/1909078/58 "2025-12-03T17:29:15Z")

</div>

> [@BananableOffense](#):
>
> One important use case for dynamic branching outside of loops is material scalability.

Indeed, I can see this cutting down on shader-permutation overhead? If you can have proper-branching, would it not invite the removal of switches in favor of incorporating that branching-behavior. Less complexity in that arena is always a gain.

Ultimately, my takeaway is that we will always be able to help-ensure the _execution path_ is always most efficient. I am with you in loading up a singluar, more-monolithic shader and then letting the most efficient path for that pixel run. I could even see a/the standard of a thing trending towards a ‘solid’ shader, a ‘liquid’ shader, ‘gas’ shader, etc vs just having plant, rock, person, etc…

---

<div class="post-metadata">

**Author:** ![Arkiras](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/arkiras/32/1008648_2.png) [@Arkiras](https://forums.unrealengine.com/u/Arkiras)\
**Post date:** [December 3, 2025, 9:20pm UTC](https://forums.unrealengine.com/t/shader-dynamic-branch-not-working/1909078/59 "2025-12-03T21:20:13Z")

</div>

The issue is dynamic branching isn’t always going to be a performance win which is why I mentioned loops being the most important use case for them. For scalability POM’s iteration/step count can be set outside of the loop which doesn’t require dynamic branching. But when you’re inside of the loop you _need_ a dynamic branch to return early, there’s no way around that.

This makes me wonder because I don’t actually know if a dynamic branch would be faster than just setting the iterations/steps to 0 on lower scalability. Neither would require a new shader permutation, and both would skip the loop. Would gating it behind a dynamic branch be faster? No idea…

There’s also already a scalability switch node but I haven’t looked into how it actually works behind the scenes; Is it already using dynamic branching or is it just a ternary or static switch in a trenchcoat? I don’t know…

The major cases where I think this is important, there’s already workarounds for. Setting POM steps as a param outside of the custom node is one example but another is volumetric cloud materials, they are crazy expensive without dynamic branching (especially with raymarched shadows), and to address this Epic added a conservative density input so you can skip the expensive raymarching loop for empty space.

Basically there is probably value here, but I suspect it’s not going to be as big as people are hoping.

---

<div class="post-metadata">

**Author:** ![BananableOffense](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@BananableOffense](https://forums.unrealengine.com/u/BananableOffense)\
**Post date:** [December 4, 2025, 12:48am UTC](https://forums.unrealengine.com/t/shader-dynamic-branch-not-working/1909078/60 "2025-12-04T00:48:51Z")

</div>

Quality switch is just a static switch linked to the built in scalability presets.

I think the rule of thumb I learned was that if a branch saved roughly 12 instructions it was better to branch. But that might be outdated. Even without a loop POM is pretty instruction heavy.

But you’re right that step count and many other settings don’t need branching. In my projects I have a custom class of the Game User Settings that stores such things as a user config the material to read.

[Previous page](https://forums.unrealengine.com/t/shader-dynamic-branch-not-working/1909078.md?page=2)
