GLSL mod() function acting up?
Posted: Fri Jul 28, 2017 11:36 am
Hi guys, I'm trying to write some shaders here, but I'm running into some funny behaviour with the glsl mod() function. I was hoping someone with more knowledge of Videocore than me could tell me what's going on here.
The code below is run over the whole screen a la shadertoy.com, and explains itself in the comments:
Is it just me or does this make no sense? I'm aware of floating point inaccuracy, but as I understand it the values 1.0 and 2.0 can both be represented exactly. I'm using highp floats.
It works as expected on shadertoy.com too. What's going on?
The code below is run over the whole screen a la shadertoy.com, and explains itself in the comments:
Code: Select all
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec3 green = vec3(0., 1., 0.);
vec3 red = vec3(1., 0., 0.);
// a = 1.0, b = 2.0
float a = 1.;
float b = a + a;
// split the screen into 4 parts.
if (fragCoord.x < 1920./4.) {
// shows green, so b == 2.
if (b == 2.) {
fragColor.rgb = green;
} else {
fragColor.rgb = red;
}
} else if (fragCoord.x < 2.*1920./4.) {
// shows green, so mod(2., 2.) == 0
if (mod(2., 2.) == 0.) {
fragColor.rgb = green;
} else {
fragColor.rgb = red;
}
} else if (fragCoord.x < 3.*1920./4.) {
// shows red, so mod(b, 2.) != 0, even though b == 2.
if (mod(b, 2.) == 0.) {
fragColor.rgb = green;
} else {
fragColor.rgb = red;
}
} else {
// shows green, so mod(b, 2.) == 2
if (mod(b, 2.) == 2.) {
fragColor.rgb = green;
} else {
fragColor.rgb = red;
}
// so maybe this has to do with == being more about closeness
// than exact equality? I don't know.
}
}
It works as expected on shadertoy.com too. What's going on?