Shaders - Avoiding Branches. Can 'true / false' be instanced as '1 / 0'?

I’m writing a frag shader for tilemap-based terrain to combine different textures in a specific way, depending on what type of terrain is around the current tile.

To this end, I have code like (neighbourTypes[2] == thisType ? 2 : 0) and if’s and branches in many more locations in the original cpu-based code.

I know branches like this are bad for performance on GPU, and want to avoid them. Does anyone know how best to avoid brances in code like this? One thing I thought of was maybe performing (neighbourTypes[2] == thisType) * 2 which only works if the true or false resulting from neighbourTypes[2] == thisType can be instanced to 1 or 0, but I have no idea if that works.

Can anyone share some tips how to avoid brances in situations like this?

Super good articles about shader optimisation : http://www.humus.name/Articles/Persson_LowlevelShaderOptimization.pdf
http://www.humus.name/Articles/Persson_LowLevelThinking.pdf
Enjoy :slight_smile: