Radu Chivu
Radu Chivu

Reputation: 1065

Qt desktop shader compilation issue

I am writing a game using Qt and I have implemented some post processing shaders to make it look more interesting. It worked ok until I upgraded to linux mint 16, now I always get an error requiring a precision specifier, this only happens when compiling fragment shaders. Because the code compiles and works ok on an android build, I think this happens because QGLShaderProgram adds mediump, highp and lowp as defines to the beginning of the shader when it's compiled in a desktop build.

So my question is: Is there any way to make the glsl compiler to stop requiring precision specifiers, or should I look into how to disable those defines from being added ?

Code:

    isOk = m_displaceShader.addShaderFromSourceCode(QGLShader::Vertex,
        "#version 100                                   \n"
        "attribute mediump vec4 in_verts;               \n"
        "varying mediump vec2 out_tex;                  \n"
        "void main(void)                                \n"
        "{                                              \n"
        "   gl_Position = in_verts;                     \n"
        "   out_tex = (in_verts.xy + vec2(1.0)) * 0.5;  \n"
        "}                                              \n");
    qDebug() << m_displaceShader.log();
    isOk &= m_displaceShader.addShaderFromSourceCode(   QGLShader::Fragment,
        "#version 100                                                               \n"
        "uniform sampler2D inCol;                                                   \n"
        "uniform sampler2D inDisplace;                                              \n"
        "varying mediump vec2 out_tex;                                              \n"
        "void main(void)                                                            \n"
        "{                                                                          \n"
        "   mediump vec4 displaceCol = texture2D(inDisplace, out_tex);              \n"
        "   mediump float dispX = (displaceCol.r - 0.5) * 0.2;                      \n"
        "   mediump float dispY = (displaceCol.g - 0.5) * 0.2;                      \n"
        "   mediump vec2 texPlace = out_tex + vec2(dispX, dispY);                   \n"
        "   gl_FragColor = texture2D(inCol, texPlace);                              \n"
#ifdef DEBUG_SHADERS
        "   gl_FragColor = 0.5 * pow(texture2D(inCol, texPlace), vec4(1.0/2.2)) + 0.5 * displaceCol;            \n"
#endif
        "}                                                                          \n"
    );

Errors:

QGLShader::compile(Fragment): 0:7(16): error: no precision specified this scope for type `vec2'
0:10(9): error: no precision specified this scope for type `vec4'
0:11(10): error: no precision specified this scope for type `float'
0:12(10): error: no precision specified this scope for type `float'
0:13(9): error: no precision specified this scope for type `vec2'

Upvotes: 3

Views: 2940

Answers (2)

Andon M. Coleman
Andon M. Coleman

Reputation: 43319

If you establish a default precision in your GLSL ES fragment shader, the problem will work itself out.

Unlike vertex shaders, fragment shaders have no default. If you add precision mediump float; to the top of your fragment shader (after the #version directive), that will fix your problem.

You could also actually compile a desktop GL shader (e.g. #version 110), but then your precision qualifiers will generate parse errors so you will need to remove them.

Upvotes: 1

derhass
derhass

Reputation: 45322

You are using #version 100, which is not a valid desktop GLSL version directive. See the following quote from the GLSL 4.40 spec (emphasis mine):

Any number representing a version of the language a compiler does not support will cause a compile-time error to be generated. Version 1.10 of the language does not require shaders to include this directive, and shaders that do not include a #version directive will be treated as targeting version 1.10. Shaders that specify #version 100 will be treated as targeting version 1.00 of the OpenGL ES Shading Language.

This is meant for compatibility with GLES, and GLES SL requires these precision specifiers, so your compiler is right.

Upvotes: 5

Related Questions