Shader optimizations (skipping AND operands etc.)

Hmm, I just started watching Epic’s stream about the Custom node. They show the documentation page where it warns that using the Custom node prevents constant folding. I checked and the warning is still there

I’m at a smaller studio and so far, in the past 4 years, I was the only person doing our materials, and in case someone ever needs to take over I always comment everything and keep it tidy, so if there is a node group labeled e.g. “Range Mask 0/1”, and another labeled “Stencil Test 0/1”, both leading into a multiply, I think it’s readable enough. But I get your point :slight_smile: I’ll probably switch back to IFs.

I remember vaguely that if A==B is connected, the material is converted into HLSL differently (probably because the equals condition has a threshold).

UE4 If threshold.png

I thought Saturate would be faster than a full IF…Still I guess you make a valid point about the division.

UE4 Saturate.png

Yeah actually I’ve been testing stuff a lot lately, doing small builds and writing averaged performance measurements down for comparison, while learning how to optimize our project in terms of stuff like shader complexity, LODs, dynamic shadow casting settings, culling etc. As there’s no clear, universal guidelines fitting all projects, I arrived at the same conclusion as you: just test and find out. Even did it once with HLSL, actually: I was curious if the Clamp(0,1) node would compile into the same code as Saturate, so I checked in RenderDoc and it did. Once I changed (0,1) to something else, it produced more code.

Still, this topic can be a bit overwhelming, and my testing methodology could be flawed so I preferred to ask first to gain some foundations rather than to wander in the dark. So thank you for the explanation, I just started educating myself on fragment shaders etc. and it’s slowly starting to make sense.

Really appreciate all your answers!

1 Like