Why it is necessary to set precision for the fragment shader?

20,574

Solution 1

No default precision exists for fp types in fragment shaders in OpenGL ES 2.0.

In vertex shaders, if you do not explicitly set the default precision for floating-point types, it defaults to highp. However, if the fragment shader were to default to highp as well, that would cause issues since OpenGL ES 2.0 does not require support for high precision floating-point types in the fragment shader stage.

OpenGL ES Shading Language - 4. Variables and Types - pp. 35-36

The fragment language has no default precision qualifier for floating point types. Hence for float, floating point vector and matrix variable declarations, either the declaration must include a precision qualifier or the default float precision must have been previously declared.

4.5.4 Available Precision Qualifiers

The built-in macro GL_FRAGMENT_PRECISION_HIGH is defined to one on systems supporting highp precision in the fragment language

#define GL_FRAGMENT_PRECISION_HIGH 1

and is not defined on systems not supporting highp precision in the fragment language. When defined, this macro is available in both the vertex and fragment languages. The highp qualifier is an optional feature in the fragment language and is not enabled by #extension.

Solution 2

The simple answer is that the spec does not define a default float precision for fragment shaders, so you have to specify it yourself.

I always thought it was kind of odd that there was no default. Everything else has a default precision. The default could not be highp, since that's not guaranteed to be available in fragment shaders. But I don't see a good reason why it couldn't be mediump by default. mediump is the default for int, and it could just as well be for float.

The explanation becomes fairly clear in section 10 ("Issues") of the spec. This section contains various questions that were open while the spec was discussed, and were then answered. There is often some explanation with reasoning why the answer was chosen.

Particularly, "10.3 Precision Qualifiers" deals with this. One of the questions and answers (page 85) is:

Should there be a default precision? It would make sense to specify the default vertex precision as highp as that it what is currently specified. There is no agreement on what the default precision for the fragment side should be.

RESOLUTION: highp for the vertex shader, no default precision for the fragment shader.

I think the key part in this is: "There is no agreement". It sounds like they wanted to define a default precision, but different vendors could not agree on what it should be, and they were so deadlocked that they ended up not defining one at all.

Share:
20,574

Related videos on Youtube

Andrey Bushman
Author by

Andrey Bushman

Programmer. My BitBuket account is here.

Updated on July 09, 2022

Comments

  • Andrey Bushman
    Andrey Bushman almost 2 years

    I learn WebGL. Next shaders work fine:

    // vertex.shader
    // precision mediump float;
    attribute vec4 a_Position;
    attribute float a_PointSize;
    
    void main(){
      gl_Position = a_Position;
      gl_PointSize = a_PointSize;
    }
    

    and

    // fragment.shader
    precision mediump float;
    uniform vec4 u_FragColor;
    
    void main(){
      gl_FragColor = u_FragColor;
    }
    

    Why it is necessary to set precission for the fragment shader? Vertex shader works without this, but fragment shader doesn't work without this code row (as I see). Why the different behaviour exist?

    I read this before, but it didn't help me.

    • keltar
      keltar about 9 years
      Because GLSL ES standard says so. 'Big' GLSL (not ES) doesn't have this requirement.
    • jwlaughton
      jwlaughton about 9 years
      @keltar thanks for the info. I've never used precision statements in my GLSL shaders, and was baffled myself at this question. Just another bit of knowledge for me. Actually, from what I see precision statements (like highp) might cause a problem depending on the GPU.
    • Summer Sun
      Summer Sun over 4 years
      I was having problem without precision setting and completely have no idea where the error comes from... unit view this post