我正在使用 Qt 编写一个游戏,我已经实现了一些后期处理着色器以使其看起来更有趣。它工作正常,直到我升级到 linux mint 16,现在我总是收到一个需要精度说明符的错误,这只发生在编译片段着色器时。 因为代码在 android 构建上编译和工作正常,我认为这是因为 QGLShaderProgram 在桌面构建中编译时将 mediump、highp 和 lowp 作为定义添加到着色器的开头。
所以我的问题是:有没有办法让 glsl 编译器不再需要精度说明符,或者我应该研究如何禁止添加这些定义?
代码:
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"
);
错误:
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'
最佳答案
您正在使用 #version 100
,不是有效的桌面 GLSL 版本指令。请参阅 GLSL 4.40 spec 中的以下引述(强调我的):
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.
这是为了与 GLES 兼容,而 GLES SL 需要这些精度说明符,因此您的编译器是正确的。
关于c++ - Qt桌面着色器编译问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23853345/