Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GLSL ES precision errors and overflows

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?

like image 895
V1ru8 Avatar asked Jul 25 '13 15:07

V1ru8


1 Answers

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;
like image 109
Aert Avatar answered Sep 19 '22 01:09

Aert