Important: Please read the Qt Code of Conduct -

Precision identifiers in GLSL Fragment shader code generate errors

  • Hello all,

    I am developing an application using the QWindow. In doing so, I started learning OpenGL ES 2.0. I have been learning about shaders and precision. I have the following code for a fragment shader:

    precision highp float;
    precision mediump float;

    varying vec4 color;
    void main()
    gl_FragColor = color;

    I get the following error when I compile this shader code:

    QOpenGLShader::compile(Fragment): ERROR: 0:7: 'precision' : syntax error syntax error

    Can anyone explain why? According to a book on this type of OpenGL, that is valid code (as a matter of fact, I am borrowing from that book).

    Thanks in advance.

  • Be aware of that QOpenGLShader will insert

    @#define highp
    #define mediump
    #define lowp@

    at the top of your glsl code when you are compiling for desktop. And on desktop, 'precision' is not a valid token until one of the later versions (exactly which one I can't remember right now).

    So based on that, I'd say that you are compiling for desktop and it fails on line 4 (7 - 3 inserted lines) of your code because 'precision' is not a valid keyword.

    QOpenGLShaderProgram tries to help in this regard by defining the precision specifiers to empty on desktop, so if you change your code to:

    @lowp varying vec4 color;
    void main() {
    gl_FragColor = color;

    it will compile for both desktop and es.

Log in to reply