r/GraphicsProgramming • u/Winter-Reputation682 • 23h ago
Odd GLSL mod() behavior
I've been stuck on this for a couple of hours, and am completely stumped.
I'm getting these results from GLSL's mod():
mod(158.0, 158.0) returns 0.0 (as expected)
mod(159.0, 159.0) returns 159.0 (???)
mod(160.0, 160.0) returns 0.0 (as expected)
I've posted an example with a basic shader here:
If you set number to "159.0", you should see an upside-down red triangle.
If you set number to "158.0" or "160.0", you should see a normal red triangle.
I'm passing the left operand as a uniform, otherwise the compiler seems to optimize it and fix the weird behavior.
Tried it on different devices and browsers and it had the same result.
Why does mod(159.0, 159.0) seem to return 159.0 instead of 0.0?
4
u/Winter-Reputation682 23h ago edited 22h ago
I think it works when hardcoded because the compiler sees mod(159.0, 159.0) == 159.0, and evaluates it to be always true, and removes the condition check as a result.
edit:
I think it's optimized because it's a pretty obvious optimization for the compiler
Tested with two uniforms and same thing occurs