GLSL ES precision errors and overflows
Asked Answered
T

1

7

I have the following fragment shader:

precision highp float; 
varying highp vec2 vTexCoord; 
uniform sampler2D uColorTexture; 
void main () {
   highp vec4 tmp;
   tmp = ((texture2D (uColorTexture, vTexCoord) + texture2D (uColorTexture, vTexCoord)) / 2.0);   
    gl_FragColor = tmp; 
}

I know this shader does not make much sense but it should still run correct and I try to reproduce a problem with it. When I analyze this shader with the Xcode OpenGL-ES analyzer it shows an error:

Overflow in implicit conversion, minimum range for lowp float is (-2,2)

and it not only shows this error, also the rendering output is broken do to overflows. So it's not just a false positive from the analyzer it actually overflows.

Can anyone explain to me why dis produces an overflow although I chose highp everywhere?

Toon answered 25/7, 2013 at 15:0 Comment(0)
S
14

You didn't really choose highp everywhere; from the GLSL ES spec chapter 8 (Built-in Functions):

Precision qualifiers for parameters and return values are not shown. For the texture functions, the precision of the return type matches the precision of the sampler type.

and from 4.5.3 (Default Precision Qualifiers):

The fragment language has the following predeclared globally scoped default precision statements: ... precision lowp sampler2D; ...

Which means that in your code texture2D (uColorTexture, vTexCoord) will return a lowp, you are adding two of them, potentially resulting in a value of 2.0.

From 4.5.2 (Precision Qualifiers):

The required minimum ranges and precisions for precision qualifiers are: ... lowp (−2,2) ...

The parentheses in (-2,2) indicate an open range, meaning it includes values up to (but not including) 2.

So I think the fact that you're adding two lowp's together means you're overflowing. Try changing that line to:

tmp = texture2D(uColorTexture, vTexCoord)/2.0 + texture2D(uColorTexture, vTexCoord)/2.0;
Siobhansion answered 30/7, 2013 at 15:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.