Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In OpenGL ES 2.0 / GLSL, where do you need precision specifiers?

People also ask

How do I optimize GLSL shader?

One way to speed up GLSL code, is by marking some variables constant at compile-time. This way the compiler may optimize code (e.g. unroll loops) and remove unused code (e.g. if hard shadows are disabled). The drawback is that changing these constant variables requires that the GLSL code is compiled again.

What is GLSL used for?

About GLSL These shading languages are used to program shaders (i.e. more or less small programs) that are executed on a GPU (graphics processing unit), i.e. the processor of the graphics system of a computer – as opposed to the CPU (central processing unit) of a computer.

What is GLSL written in?

GLSL. Shaders are written in the C-like language GLSL.

Does GLSL have structs?

GLSL does not support anonymous structures (ie: structs without a type name), and structs must have at least one member declaration. Structs cannot be defined within another struct, but one struct can use another previously defined struct as a member.


  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.

  3. 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)

  4. 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): http://www.khronos.org/registry/gles/specs/2.0/GLSL_ES_Specification_1.0.17.pdf