Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GLSL #version gives syntax error (LWJGL on Mac)

Specifying the GLSL version gives a syntax error when using LWJGL. I haven't tried to reproduce this issue outside LWJGL. This is happening on multiple Macs running Lion.

I've gotten both vertex and fragment shaders to work without using #version. But I'm about to use the texture function, which seems to require a #version directive.

Here's the simplest failing example:

#version 120

void main() {
  gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
}

Compiling this fragment shader and calling glGetShaderInfoLog gives this error:

ERROR: 0:1: '' : syntax error #version

Replacing 120 with anything else, such as 110, also gives an error. Curiously, though, if I use 130 or higher, it gives the same error plus a complaint about the version not beig supported. (i know my system doesn't have GLSL 1.3, but it's still weird that this error displays when the compiler is acting like it doesn't understand the version tag.)

I'm on a Mac with an ATI Radeon HD 4670. GL_VERSION is 2.1 ATI-7.12.9 and GL_SHADING_LANGUAGE_VERSION is 1.20.

Given that, I don't see any reason why GLSL 1.20 should be unavailable. And it's really weird to me that it's saying #version is a syntax error, as opposed to saying something about an unsupported GLSL version.

like image 379
rlkw1024 Avatar asked Dec 16 '11 21:12

rlkw1024


People also ask

What is GLSL used for?

About GLSL These shading languages are used to program shaders (i.e. more or less small programs) that are executed on a GPU (graphics processing unit), i.e. the processor of the graphics system of a computer – as opposed to the CPU (central processing unit) of a computer.

Is GLSL a C++?

GLSL uses the standard C/C++ set of statements.

What is the difference between OpenGL and GLSL?

The short version is: OpenGL is an API for rendering graphics, while GLSL (which stands for GL shading language) is a language that gives programmers the ability to modify pipeline shaders. To put it another way, GLSL is a (small) part of the overall OpenGL framework.

Is GLSL a programming language?

OpenGL Shading Language (GLSL) is a high-level shading language with a syntax based on the C programming language.


2 Answers

Solved! It had nothing to do with OpenGL. My file reader code was dropping all line breaks. This was fine in the body of the shader, which had semicolons. But the preprocessor directive had no semicolon to protect it from this error.

So for anyone with this problem, make sure the code you're actually passing to glShaderSource still has its linebreaks.

like image 146
rlkw1024 Avatar answered Sep 19 '22 19:09

rlkw1024


Both the vertex and the fragment shader need to have the same version. So if you add a #version 120 to the fragment shader, you should also add it to the vertex shader, too. But it's a bit strange that this is reported as a syntax error. Maybe there's another error, but both definitely have to have the same version tag.

EDIT: Also keep in mind that the version tag needs to be the first line in the shader source code (newlines and comments should be Ok by specification, but who knows what the drivers think).

like image 27
Christian Rau Avatar answered Sep 21 '22 19:09

Christian Rau