3

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'
1
  • I am saddened that there is no proper answer for this. I have just run into the same issue, where I need to target GLES SL 1.0 in order to get an application to work over RDP. It seems the only solution is to #undef mediump; ugh. Commented Nov 17, 2020 at 20:39

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this was indeed the reason I guess that my drivers also got updated along with the system which made this issue pop up now
1

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.

6 Comments

Qt adds defines that map precision qualifiers to empty strings on desktop builds so adding a default precision wouldn't help for the same reason the original shaders don't compile, adding a #version 110 was the solution since then the precision qualifiers aren't needed
@RaduChivu I doubt it does that, because that would create a lot of porting issues, wrong line numbers, etc. You can query the shader string from GL glGetShaderSource (...) if you really think that happens.
You can see that in the qglshaderprogram source qt.gitorious.org/qt/qt/source/… just look for #define mediump
@RaduChivu Hmm, you are right and it adds this stuff without adding an appropriate #line directive to correctly re-numbe.r lines in the GLSL compiler's error logs. It is a miracle your code is producing meaningful line numbers, they should each be off by 3. Actually, nevermind I took a closer look at the shader log and the errors are off by 3; evil Qt :-/
@RaduChivu To avoid issues that Qt is going to create for you in the future by silently adding those 3 lines after your #version directive, I would suggest adding #line 1 immediately after the #version line. That way you will not have to remember to subtract 3 from any line number the GLSL compiler generates. This is actually something Qt should be doing itself if it is going to mess with your shader source code like that.
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.