Shader optimizations (skipping AND operands etc.)

Yes @Chosker you are correct, static switches don’t really help in my case, thank you for clarifying that in my absence. Also, I believe this issue would also be valid for decals or regular mesh materials, not just a Post Process.

Thank you for the link, I will dig into it. Also, in the meantime, I asked the same question on Asher Zhu’s Discord, where Ryan DowlingSoka from The Coalition provided an exhaustive explanation, with tl;dr being (with my limited understanding) basically that there’s no easy/obvious/platform-agnostic way to do it either in the graph or in HLSL with guarantee of it being optimized (Condition B not being executed if not necessary). I may ask him for permission to repost his explanation here, as it is a gold mine.

Also, I’ve edited my original post to clarify that Condition B in the screenshot is just gibberish; imagine any expensive calculations in there that actually make sense :slight_smile:


Thank you @midgunner66, I will study your solution and try to cross-reference it with Ryan’s explanations (which also included branching) as I’m still pretty new to the deeper mechanisms of shaders. And yes, I sometimes offset calculations that only need to be done once per frame to BPs and feed them via parameters, but I also know in some cases this is already optimized in GPU (these are called constants, right?) so I wasn’t sure if by offsetting it to CPU I’m not actually harming performance.


Hmm, I don’t see where it is wrong… Like I wrote in the graph’s comments, both conditions need to return 1 for the purple effect to pass. Condition A returns 1 when the given pixel is within defined range from SomeLocation. Condition B returns 1 when the pixel satisfies some more complicated condition (it could be a more complex procedural pattern, for example).

Edit: oh and thank you [USER=“3140864”]MostHost LA[/USER] for your custom node IF tip. I’ve also seen it in the linked thread, still trying to make some sense of this with my very limited understanding of HLSL and how GPUs work.

Edit2: Ryan kindly allowed me to post his explanation, so here it is:

](filedata/fetch?id=1871253&d=1615748049)

Please disregard the “messed up Condition B” part, as the screenshot here on the UE4 forums is already fixed. Also, here’s my “bonus question” screenshot from the discussion: