Icon ASCII : A Love Letter


Icon My Neural Network isn't working! What should I do?


Icon Phase-Functioned Neural Networks for Character Control


Icon 17 Line Markov Chain


Icon 14 Character Random Number Generator


Icon Simple Two Joint IK


Icon Generating Icons with Pixel Sorting


Icon Neural Network Ambient Occlusion


Icon Three Short Stories about the East Coast Main Line


Icon The New Alphabet


Icon "The Color Munifni Exists"


Icon A Deep Learning Framework For Character Motion Synthesis and Editing


Icon The Halting Problem and The Moral Arbitrator


Icon The Witness


Icon Four Seasons Crisp Omelette


Icon At the Bottom of the Elevator


Icon Tracing Functions in Python


Icon Still Things and Moving Things


Icon water.cpp


Icon Making Poetry in Piet


Icon Learning Motion Manifolds with Convolutional Autoencoders


Icon Learning an Inverse Rig Mapping for Character Animation


Icon Infinity Doesn't Exist


Icon Polyconf


Icon Raleigh


Icon The Skagerrak


Icon Printing a Stack Trace with MinGW


Icon The Border Pines


Icon You could have invented Parser Combinators


Icon Ready for the Fight


Icon Earthbound


Icon Turing Drawings


Icon Lost Child Announcement


Icon Shelter


Icon Data Science, how hard can it be?


Icon Denki Furo


Icon In Defence of the Unitype


Icon Maya Velocity Node


Icon Sandy Denny


Icon What type of Machine is the C Preprocessor?


Icon Which AI is more human?


Icon Gone Home


Icon Thoughts on Japan


Icon Can Computers Think?


Icon Counting Sheep & Infinity


Icon How Nature Builds Computers


Icon Painkillers


Icon Correct Box Sphere Intersection


Icon Avoiding Shader Conditionals


Icon Writing Portable OpenGL


Icon The Only Cable Car in Ireland


Icon Is the C Preprocessor Turing Complete?


Icon The aesthetics of code


Icon Issues with SDL on iOS and Android


Icon How I learned to stop worrying and love statistics


Icon PyMark


Icon AutoC Tools


Icon Scripting xNormal with Python


Icon Six Myths About Ray Tracing


Icon The Web Giants Will Fall


Icon PyAutoC


Icon The Pirate Song


Icon Dear Esther


Icon Unsharp Anti Aliasing


Icon The First Boy


Icon Parallel programming isn't hard, optimisation is.


Icon Skyrim


Icon Recognizing a language is solving a problem


Icon Could an animal learn to program?




Icon Pure Depth SSAO


Icon Synchronized in Python


Icon 3d Printing


Icon Real Time Graphics is Virtual Reality


Icon Painting Style Renderer


Icon A very hard problem


Icon Indie Development vs Modding


Icon Corange


Icon 3ds Max PLY Exporter


Icon A Case for the Technical Artist


Icon Enums


Icon Scorpions have won evolution


Icon Dirt and Ashes


Icon Lazy Python


Icon Subdivision Modelling


Icon The Owl


Icon Mouse Traps


Icon Updated Art Reel


Icon Tech Reel


Icon Graphics Aren't the Enemy


Icon On Being A Games Artist


Icon The Bluebird


Icon Everything2


Icon Duck Engine


Icon Boarding Preview


Icon Sailing Preview


Icon Exodus Village Flyover


Icon Art Reel




Icon One Cat Just Leads To Another

Avoiding Shader Conditionals

Created on March 29, 2013, 2:09 p.m.

Having conditionals in shaders can often have a serious performance impact on the code. Compilers can sometimes find clever ways to optimize them away, but if your shader code really does have to branch and perform comparisons the speed can drop off significantly. Most of the time the GPU will simply perform the operations encoded by both branches and throw away one of the results. Some modern GPUs can actually branch properly, but doing this is often equally slow unless the branch contains a lot of code.

To avoid these situations you can design a number of functions which allow you to do things often done using conditionals. They perform some comparison and then return 1.0 on true and 0.0 on false. This can be useful, for example, if you want to add a number to another number, but only when some condition is true.

if (x == 0) {
  y += 5;

This can be transformed to the following.

y += 5 * when_eq(x, 0)

It doesn't cover all cases but most of the time some clever thinking can transform the comparison into this kind of operation.

The full set of functions are defined here:

vec4 when_eq(vec4 x, vec4 y) {
  return 1.0 - abs(sign(x - y));

vec4 when_neq(vec4 x, vec4 y) {
  return abs(sign(x - y));

vec4 when_gt(vec4 x, vec4 y) {
  return max(sign(x - y), 0.0);

vec4 when_lt(vec4 x, vec4 y) {
  return max(sign(y - x), 0.0);

vec4 when_ge(vec4 x, vec4 y) {
  return 1.0 - when_lt(x, y);

vec4 when_le(vec4 x, vec4 y) {
  return 1.0 - when_gt(x, y);

These are defined in GLSL, and used on the vec4 types. Using vec4 the comparison is done component-wise, which means you can do four comparisons at once! Similar logic can also be used for any other shading language or vector/float type.

The logic behind the functions is fairly simple, and underlies the actual maths behind comparison which computers use. This is exactly the kind of transformation a smart compiler would do to optimize conditionals away from your code naturally.

As a bonus here are a set of logical operators you can use together with the outputs from these comparisons.

vec4 and(vec4 a, vec4 b) {
  return a * b;

vec4 or(vec4 a, vec4 b) {
  return min(a + b, 1.0);

vec4 xor(vec4 a, vec4 b) {
  return (a + b) % 2.0;

vec4 not(vec4 a) {
  return 1.0 - a;

Hopefully this has helped speed up your shaders by reducing unneeded conditionals. Happy shading!

github twitter rss