I'm working on an OpenGL based image viewer that allows users to show/hide or adjust brigthness and contrast of individual color channels.
I'm a total beginner with OpenGL shaders. I managed to get a piece of code that works as intended, but i'm still wondering if I'm doing it right. I'm worried about performances if users start displaying much larger images than my test images.
In your opinion, what it the most performant way of implementing this?
Option 1. Do the operation every time, even if the brightness value is 0 (most common case):
#version 120
uniform sampler2D tex;
uniform float alpha;
uniform float rlum;
uniform float glum;
uniform float blum;
varying vec2 coords;
void main(void) {
gl_FragColor = texture2D(tex, coords);
gl_FragColor.a *= alpha;
gl_FragColor.r += rlum;
gl_FragColor.g += glum;
gl_FragColor.b += blum;
}
Option 2. Add a conditional branch to avoid doing the operation when luminosity values are 0.
#version 120
uniform sampler2D tex;
uniform float rlum;
uniform float glum;
uniform float blum;
varying vec2 coords;
void main(void) {
gl_FragColor = texture2D(tex, coords);
gl_FragColor.a *= alpha;
if(rlum != 0.) {
gl_FragColor.r += rlum;
}
if(glum != 0.) {
gl_FragColor.g += glum;
}
if(blum != 0.) {
gl_FragColor.b += blum;
}
}
In other words, is a conditional branch more or less expensive than a float addition and value assignment?
What about the same situation for a more complex operation such as contrast adjustement?
factor = (259 * (rcontrast*255 + 255)) / (255 * (259 - rcontrast*255))
gl_FragColor.r = factor * (gl_FragColor.r - 0.5) + 0.5
In my experience, both solutions seem to work fine. I don't see a big difference when I'm moving a brightness slider, the changes are fluid. But as I said, I'm worried about the possibility of users using a much slower computer with much larger images.