# 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:** 1

<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:** [June 19, 2024, 2:27pm UTC](https://forums.unrealengine.com/t/shader-dynamic-branch-not-working/1909078/1 "2024-06-19T14:27:33Z")

</div>

I have set up this dynamic branching test

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/c/5/ec5cbfa8568a79053064535ad86e5cf17094cfb8.png)  
When toggled to Off, a frame takes 124 ms.  
When toggled to On, a frame still takes 124 ms.  
How come there is no performance improvement when the dynamic branch is set to off?

---

<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:** [June 19, 2024, 3:42pm UTC](https://forums.unrealengine.com/t/shader-dynamic-branch-not-working/1909078/2 "2024-06-19T15:42:48Z")

</div>

Update: If I change the order of the inputs, so that the more expensive input is declared last

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/a/2/2a2014adb217a1da8b383eea8c5afb9565aa74e4.png)

the frame time now drops from 124ms to 70ms when the branch is toggled to off.  
Thats almost 50% savings, I was hoping for more like 95% savings though, can anyone explain this behavior?

---

<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:** [June 19, 2024, 4:54pm UTC](https://forums.unrealengine.com/t/shader-dynamic-branch-not-working/1909078/3 "2024-06-19T16:54:55Z")

</div>

As far as I know, dynamic branching requires you to put all your logic for all branches inside the HLSL custom node.

So… I’m surprised you even got 50% savings. I would expect zero.

---

<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:** [June 19, 2024, 6:23pm UTC](https://forums.unrealengine.com/t/shader-dynamic-branch-not-working/1909078/4 "2024-06-19T18:23:20Z")

</div>

Just my raised hand, but I too would like to understand this better.

I know a few years ago dynamic-branching was a very popular topic but I am still unaware of the in’s and out’s. ref: [Dynamic flow control in materials](https://forums.unrealengine.com/t/dynamic-flow-control-in-materials/68393) (Ty @DeathreyCG for this and your ocean-thingie)

I don’t even know how to get it set up, if it requires a custom-node, or if there are best-practices…

Anyone that wants to chime in on this is welcome (please?). 😃

edit: Only guy I found on youtube, 6ys old… ref: [https://www.youtube.com/@MPRX87](https://www.youtube.com/@MPRX87)

He seems to be doing what you are doing OP, just-embedding an IF statement in the custom-node…

---

<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:** [June 19, 2024, 7:51pm UTC](https://forums.unrealengine.com/t/shader-dynamic-branch-not-working/1909078/5 "2024-06-19T19:51:31Z")

</div>

Thanks, I read through [Dynamic flow control in materials](https://forums.unrealengine.com/t/dynamic-flow-control-in-materials/68393). The take away from that thread seems to be pretty much what Arkiras said… Which kinda sucks. Custom nodes are horrible to work with unless your using them for short snippets of code.

One possible way of working around this, would be to make an HLSL code generator. The input would be material functions, and the output would be a hlsl custom node. However this wouldnt be easy to do, and you may end up with somewhat inefficient code.

---

<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:** [June 20, 2024, 5:53pm UTC](https://forums.unrealengine.com/t/shader-dynamic-branch-not-working/1909078/6 "2024-06-20T17:53:14Z")

</div>

This is my understanding as well. As far as HLSL code generators go… You can already access the shader code in HLSL from the material window. It’s a little tedious since it contains a ton of other crap, but you could take your string of nodes, plug it into the emissive or something, then access the generated HLSL. Find the emissive section in the generated code and simply copy the code from there. It would still require some work to set up the inputs and outputs. Not sure if it’d save that much time over just writing the HLSL.

---

<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 4, 2024, 11:07am UTC](https://forums.unrealengine.com/t/shader-dynamic-branch-not-working/1909078/7 "2024-07-04T11:07:17Z")

</div>

Isn’t there already a dynamic branching mode in unreals material editor built in? I’m using it right now… not sure if it saves on performance or not.

---

<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:** [July 4, 2024, 2:56pm UTC](https://forums.unrealengine.com/t/shader-dynamic-branch-not-working/1909078/8 "2024-07-04T14:56:51Z")

</div>

If you’re referring to the “if” node then that doesn’t do dynamic branching, it’s just a ternary.

If you’re talking about the “dynamic branch” node, it’s literally just the “if” node inside of another material function.

---

<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 5, 2024, 2:08pm UTC](https://forums.unrealengine.com/t/shader-dynamic-branch-not-working/1909078/9 "2024-07-05T14:08:52Z")

</div>

No im saying it has a  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/c/b/e/cbe8c405d9c682a7c4a4bf821ae527fd8481e509.png)

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/d/2/d/d2d191fa1c14de01d6fc5bbcb27a3bf0ad05d7c8.png)

---

<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:** [July 5, 2024, 2:10pm UTC](https://forums.unrealengine.com/t/shader-dynamic-branch-not-working/1909078/10 "2024-07-05T14:10:54Z")

</div>

> [@kurylo3d](#):
>
> No im saying it has a

Double click it and look at what is inside

> **Spoiler**
>
> ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/0/8/e082eea46b8b5674065594d49706f15d10690fd9.jpeg)

---

<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 5, 2024, 2:11pm UTC](https://forums.unrealengine.com/t/shader-dynamic-branch-not-working/1909078/11 "2024-07-05T14:11:45Z")

</div>

so why does it say its best used for 20 instructions or a texture sample? If it does nothing.

---

<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:** [July 5, 2024, 2:14pm UTC](https://forums.unrealengine.com/t/shader-dynamic-branch-not-working/1909078/12 "2024-07-05T14:14:20Z")

</div>

Beats me, that’s a question for Epic.

It is genuinely just a ternary operator though, you can just look at the generated HLSL code if you don’t believe me.

---

<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 7, 2024, 2:13pm UTC](https://forums.unrealengine.com/t/shader-dynamic-branch-not-working/1909078/13 "2024-07-07T14:13:30Z")

</div>

i just tested using the custom node logic. Dynamic branching definently works there for sure.

EDIT … i just also tested the dynamic branching node we talked about… it works as well…

i made a test and checked the gpu ms… made one with like 8 textures and over 400 instructions and it goes from like 7.1 ms gpu to like 6.4…

and thats me running an rtx 3090… well 2… so im sure it would work even better for lower end.

---

<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:** [July 7, 2024, 2:26pm UTC](https://forums.unrealengine.com/t/shader-dynamic-branch-not-working/1909078/14 "2024-07-07T14:26:01Z")

</div>

> [@kurylo3d](#):
>
> EDIT … i just also tested the dynamic branching node we talked about… it works as well…

How are you testing it?

---

<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 7, 2024, 2:27pm UTC](https://forums.unrealengine.com/t/shader-dynamic-branch-not-working/1909078/15 "2024-07-07T14:27:50Z")

</div>

just a poly plane in a scene with a random stupid material with loads of texture samplers and math. Did stat unit to check the gpu ms… id go back and forth on a material instance.

---

<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:** [July 7, 2024, 2:28pm UTC](https://forums.unrealengine.com/t/shader-dynamic-branch-not-working/1909078/16 "2024-07-07T14:28:27Z")

</div>

Post the material you’re testing

---

<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 7, 2024, 2:28pm UTC](https://forums.unrealengine.com/t/shader-dynamic-branch-not-working/1909078/17 "2024-07-07T14:28:55Z")

</div>

![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/9/d/a/9da21670aa338a0058b5a560fa56c81ae8052ebf.png)

---

<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 7, 2024, 2:29pm UTC](https://forums.unrealengine.com/t/shader-dynamic-branch-not-working/1909078/18 "2024-07-07T14:29:33Z")

</div>

as i said id just alternate to a solid scalar of 1 vs that entire node chain… using a material instance to go back and forth with a parameter.

---

<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 7, 2024, 2:30pm UTC](https://forums.unrealengine.com/t/shader-dynamic-branch-not-working/1909078/19 "2024-07-07T14:30:27Z")

</div>

![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/6/3/e/63ed7573a49bef13eb9feed66c7fb0872132db8f.png)

i tried both the dynamic branch node and the custom node… both with same results of performance saving.

---

<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:** [July 7, 2024, 2:34pm UTC](https://forums.unrealengine.com/t/shader-dynamic-branch-not-working/1909078/20 "2024-07-07T14:34:06Z")

</div>

> [@kurylo3d](#):
>
> both with same results of performance saving.

Wouldn’t expect either of these to save anything at all. I can only assume it has something to do with your use of a static parameter for the alpha, it could just be compiling two versions of the shader.

This is my test:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/9/5/d/95dd8c5734f8cac9c2d3dd3d071f58228693dba5.png)

Zero performance improvement:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/c/e/a/cea5e34b802ddddf4b25a93970e13ca9e205d439.jpeg)

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