The Problem
I'm encountering a compilation error for my fragment shader on the following platform.
- Operating System:
macOS 10.11.6
- Open GL:
4.1 INTEL-10.14.74
My specific error output is:
QOpenGLShader::compile(Fragment): ERROR: 0:1: '' : #version required and missing.
This is followed up by:
ERROR: 0:2: ':' : syntax error: syntax error
*** Problematic Fragment shader source code ***
#define lowp
#define mediump
#define highp
#line 1
:/shaders/fragshader.glsl
I'll first show my current fragment shader code, then explain what I've tried to do in order to fix this.
Fragment Shader
#version 330 core
// Import: The vertex color to be rendered.
in vec3 vertexColor;
// Export: Fragment shader color (RGBA).
out vec4 fColor;
void main () {
// Compute the color as a function of the transformed normal vector.
fColor = vec4(normalize(vertexColor) * 0.5 + 0.5, 1.0);
}
Attempted Solutions
I've found the following SO posts and attempted the following solutions:
1: I tried fangda's solution for similar issue on this question Qt5 OpenGL GLSL version error.
QSurfaceFormat glFormat;
glFormat.setVersion(3, 3);
glFormat.setProfile(QSurfaceFormat::CoreProfile);
QSurfaceFormat::setDefaultFormat(glFormat);
This was added to my main.cpp
, but did not alter the error whatsoever.
2: I found Rodrigo Hernandez's answer to this question Serious rendering issues with OpenGL 4.1 and Qt 5
I Added some comments to your bug report and voted it up. It is indeed a Qt bug, as stated here: https://www.opengl.org/wiki/Core_Language_%28GLSL%29#Version the first preprocessor directive should be #version, but Qt adds defines for lowp, mediump and highp on its own breaking this requirement. Nvidia drivers lets you get away with it, but Intel's does not (I do not know about ATI's), so perhaps the developers in charge did not test on different enough hardware configurations.
As it is, there is no easy workaround, you could change the Qt code not to add those defines (which is a distribution nightmare), or not use the QOpenGLShader class, which means house keeping the raw OpenGL objects yourself.
However, I don't know how I would "change the Qt code to not add those defines" if it is a preprocessor task. It also seems quite extreme.
3: I have found this perfect question Qt with QOpenGLWidget Core Profile: Lots of shader compiling errors which matches my problem exactly...
But nobody has written an answer. So I'm once again unsure how to solve this.
Hernandez's link makes it clear that the error is likely being caused because symbolic constants are being inserted before my #version
line, which is not allowed:
The #version directive must appear before anything else in a shader, save for whitespace and comments. If a #version directive does not appear at the top, then it assumes 1.10, which is almost certainly not what you want.
Unfortunately, I simply do not know how I can force my installation of OpenGL with Qt to not insert those symbols constants.