Download

Why IF material node gets converted to A = B ? C : D

Hey Epic guys!

I just read the HLSL code generated by the material compiler in UE-4.7 and I noticed that IF nodes generate code like “LocalXX = Condition ? LocalYY : LocalZZ”

Shader fully evaluates both LocalYY and LocalZZ before assigning them to LocalXX even if one of the values is not used. I don’t know if GPUs like branching but this seems like a waste of instructions to me! I haven’t checked this on newer versions of the engine yet but if it is not fixed yet, I hope that it will be one day!

Robert.

I don’t know how things are in directx 12 but previously all branches where evaluated, this is one of the reasons why using IF statement should be avoided.

Well, any way to create a condition that doesn’t evaluate all branches?

You mean the hardware works that way?

Both hardware and compiler are involved:

this one is specific to UE4:

@BoredEngineer
Okay I got it, it seems like it is not that easy to branch on a GPU.

Thank you for the information!

If you really need it, you can use a Custom Expression node and use a [branch] statement before your conditional.

Don’t assume that it will be faster, though, because it’s not always the case.

@TS100101
Thanks for the link! I will test this, probably it can actually improve performance in cases like if (…) { /* ton of code / } else { / ton of code */ }

But I wonder if this is going to be included in the Material Editor one day.