Alterecho
Alterecho

Reputation: 715

OpenGL/GLSL precision error

I have a simple vertex shader:

precision mediump float;

attribute vec4 position;

attribute vec4 color;
varying vec4 f_color;

uniform mat4 projection;
uniform mat4 modelView;

void main(void) {
    gl_Position = projection * modelView * position;
    f_color = color;
    f_texCoord = texCoord;
}

This fails to compile, stating (using getShaderInfoLog()):

ERROR: 0:1: 'precision' : syntax error syntax error

It compiles fine if i remove the line with the precision specifier.

System:

OS: Mac OX 10.9.2 
GPU: NVIDIA GeForce 320M 256 MB
GLSL Ver.: 1.20

Someone help me out.

Upvotes: 5

Views: 3482

Answers (3)

Engineer
Engineer

Reputation: 8865

Precision qualifiers in OpenGL 3.3 are redundant. From the OpenGL 3.3 spec, "Precision qualifiers are added for code portability with OpenGL ES, not for functionality. They have the same syntax as in OpenGL ES, as described below, but they have no semantic meaning, which includes no effect on the precision used to store or operate on variables".

Use #version 330 core for strict 3.3 with no backward compatibility. And lose the precision qualifiers if you don't need ES compatibility.

Upvotes: 0

Chris Dodd
Chris Dodd

Reputation: 126526

The precision keyword is a OpenGL/ES extension. Since you have no #version in your vertex shader, you are getting GLSL v1.1 (backwards compatible with OpenGL2.0), which doesn't support such GL/ES extensions.

Either remove the precision line (which probably does nothing on desktop GL anyways), or add a #version for some version of GLSL that supports precision.

Upvotes: 2

fintelia
fintelia

Reputation: 1211

You have already figured out how to solve your problem: just remove the line with the precision qualifier.

The bigger issue is that you haven't decided what version of OpenGL you are using. The GLSL sample you provided seems to be for an old version of the GLSL associated with OpenGL ES, which is designed for mobile devices. Since you are actually running on a desktop/laptop, you want "normal" OpenGL. The error you observed is a result of the differences between the two.

In general you want to go with the latest version of OpenGL that is support by the systems you are targeting. Right now that is probably OpenGL 3.3 (along with GLSL 3.3).

Upvotes: 4

Related Questions