67

Does the variable that you're stuffing values into dictate what precision you're working with, to the right of the equals sign?

For example, is there any difference, of meaning, to the precision specifier here:

gl_FragColor = lowp vec4(1);

Here's another example:

lowp float floaty = 1. * 2.;
floaty = lowp 1. * lowp 2.;

And if you take some floats, and create a vector or matrix from them, will that vector or matrix take on the precision of the values you stuff it with, or will those values transform into another precision level?

I think optimizing this would best answer the question:

dot(gl_LightSource[0].position.xyz, gl_NormalMatrix * gl_Normal)

I mean, does it need to go this far, if you want it as fast as possible, or is some of it useless?

lowp dot(lowp gl_LightSource[0].position.xyz, lowp gl_NormalMatrix * lowp gl_Normal)

I know you can define the default precision for float, and that this supposedly is used for vectors and matrices afterwards. Assume for the purpose of education, that we had defined this previously:

precision highp float;

1 Answer 1

91
  1. You don't need precision specifiers on constants/literals since those get compile time evaluated to whatever they are being assigned to.

  2. In vertex shaders, the following precisions are declared by default: ( 4.5.3 Default Precision Qualifiers)

     precision highp float;
     precision highp int;
     precision lowp sampler2D;
     precision lowp samplerCube;
    

And in fragment shaders you get:

    precision mediump int;
    precision lowp sampler2D;
    precision lowp samplerCube;

This means that if you declare a float in a fragment shader, you have to say whether it is a lowp or a mediump. The default float/int precisions also extend to matrices/vectors.

  1. highp is only supported on systems that have the GL_FRAGMENT_PRECISION_HIGH macro defined to 1; on the rest you'll get a compiler error. (4.5.4 Available Precision Qualifiers)

  2. The rule for precision in an expression is that they get cast automatically to the type of the assignment / parameter they are bound to. So for your dot, it would use the precision of the input types by default and the additional lowp's are unnecessary (and syntactically incorrect). If you want to down-cast a type to a lower precision, the only way to do it is to explicitly assign it to a lower precision.

These answers are all from the Khronos GLSL spec, which you can find here (relevant sections are 4.5.2 and 4.5.3): https://www.khronos.org/registry/OpenGL/specs/es/2.0/GLSL_ES_Specification_1.00.pdf

In more recent versions of the spec, those sections are 4.7.3 and 4.7.4. Nothing has changed, except adding a default precision highp atomic_uint; to both lists. See https://www.khronos.org/registry/OpenGL/specs/es/3.2/GLSL_ES_Specification_3.20.pdf

3
  • 6
    "Moreover, since gl_FragColor's precision is already defined based on the depth of the render target" what is your source for this statement? I don't see any information about this in the spec.
    – eodabash
    Commented Feb 1, 2013 at 10:10
  • 1
    This part of the answer is incorrect; it definitely matters. I've corrected point 1 in the answer.
    – solidpixel
    Commented Dec 17, 2019 at 17:32
  • Yes, you do need precision specifiers on constants/literals!! I just fixed a very-hard-to-find bug that involved this very error. Please correct your answer before all games out there break on some devices.
    – l33t
    Commented Apr 11 at 17:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.