GLSL lowp:语法错误

时间:2011-01-29 07:30:58

标签: opengl-es glsl opengl-es-2.0

为什么以下着色器,而不是在OpenGL(桌面)上进行编译 - 但在OpenGL ES 2.0(iPhone)上它运行良好(我使用相同的c / c ++代码来加载,编译和链接两个平台上的着色器)。 我的FragmentShader:

varying lowp vec4 colorVarying;

void main()
{
    gl_FragColor = colorVarying;
}

顶点着色器:

attribute vec4 position;
attribute vec4 color;

varying vec4 colorVarying;

uniform float translate;

void main()
{
    gl_Position = position;
    gl_Position.y += sin(translate) / 2.0;

    colorVarying = color;
}

编译日志如下所示:

Shader compile log:
ERROR: 0:9: 'lowp' : syntax error syntax error
ERROR: Parser found no code to compile in source strings.
Failed to compile fragment shaderProgram validate log:
Validation Failed: Program is not successfully linked.
Failed to validate program: 1

有什么想法吗?

1 个答案:

答案 0 :(得分:5)

通过GLSL 1.1,GLSL规范中未提及lowp。 在GLSL 1.2(与OpenGL 2.1一起使用)中,lowp保留用于实现。 在GLSL 1.3中(开始允许使用OpenGL lowp(但没有意义)。

因此,您在桌面上使用的任何内容似乎都只识别旧版本的GLSL。显而易见的解决方法是删除它,或者为桌面添加一个宏,如:

#ifdef DESKTOP
#define lowp
#endif

DESKTOP替换为您在桌面上使用的任何环境中定义的某个标识符,而不是您用于iPhone的任何环境。

编辑:将其纳入源代码本身并非易事。一种方式是这样的:

char const *shader = 

#ifdef DESKTOP
    "#define lowp\n"
#endif

"varying lowp vec4 colorVarying;\n"

/* ... */
;

这样,当且仅当在主机环境中设置了DESKTOP时,“lowp”才会在着色器中定义为空。