3

I'm trying to compile GLSL shaders for use in LWJGL with OpenGL. I'm on macOS Sierra.

I get the following error when trying to compile the shaders:

ERROR: 0:1: '' :  version '400' is not supported
ERROR: 0:1: '' : syntax error: #version

The shader code works on Windows, but when testing on macOS it doesn't. Here's the shader code:

#version 400 core

in vec3 position;
in vec2 textureCoords;

out vec2 pass_textureCoords;

uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;

void main(void)
{
    gl_Position = projectionMatrix * viewMatrix * vec4(position, 1.0);
    pass_textureCoords = textureCoords;
}

Here is how i'm loading the shader...

    private static int loadShader(String file, int type) throws IOException {
        StringBuilder shaderSource = new StringBuilder();
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(
                    ShaderProgram.class.getClassLoader().getResourceAsStream("shaders/" + file))
            )) {

            String line;
            while ((line = reader.readLine()) != null) {
                shaderSource.append(line).append('\n');
            }

            System.out.println(shaderSource.toString());

            int shaderID = GL20.glCreateShader(type);

            GL20.glShaderSource(shaderID, shaderSource);
            GL20.glCompileShader(shaderID);

            if (GL20.glGetShaderi(shaderID, GL20.GL_COMPILE_STATUS) == GL11.GL_FALSE) {
                System.out.println(GL20.glGetShaderInfoLog(shaderID, 500));
                System.err.println("Error compiling shader " + file);
                System.exit(-1);
            }

            return shaderID;
        }
    }

The print statement returns the correct output, yet it still gives me the error.

I've searched all over stackoverflow for a solution, yet everyone says that they're compiling it without line breaks, which i have. I'm confused as to why this is occurring.

genpfault
  • 51,148
  • 11
  • 85
  • 139
Erouax
  • 163
  • 2
  • 10
  • 3
    Why do people always think that reading files line-by-line is a good way to read files? There are [easier ways.](https://stackoverflow.com/a/326440/734069) – Nicol Bolas Nov 12 '17 at 23:29
  • @NicolBolas Well i mean that doesn't help the question at hand, however Files.readAllBytes (performance wise) is no different to reading line by line, it's only really shorter to write it out. – Erouax Nov 13 '17 at 01:19
  • 3
    what's the value of `glGetString(GL_SHADING_LANGUAGE_VERSION)` (or equivalent in JWJGL)? Maybe the context just doesn't support GLSL 4.0? – MuertoExcobito Nov 13 '17 at 03:55
  • @Erouax: The problem is that reading line by line has a terrible performance. The string will be reallocated and copied everytime append is called. – BDL Nov 13 '17 at 08:48
  • @Erouax: if you're not explicitely creating a forward-compatible core profile context, you will be limited to GL2.1 / GLSL1.20 on OSX. – derhass Nov 13 '17 at 20:04
  • @MuertoExcobito It prints 1.20, i tried using #version 120 but it still gives a syntax error. – Erouax Nov 15 '17 at 22:37

2 Answers2

2

Hello not an java expert here, i did some experiments using JOGL, so maybe i cant give you a hint to work in the right direction.

An issue with new line on MAC How to append a newline to StringBuilder

Second:

https://www.khronos.org/registry/OpenGL/specs/gl/GLSLangSpec.4.00.pdf

The #version directive must occur in a shader before anything else, except for comments and white space.

This is what is looks like RAW in JOGL you see the newline is needed is after the version directive , everthing else has an semicolons as statement separators.

  // 
  // create fragment Shader
  int fragShader = gl4.glCreateShader(GL4.GL_FRAGMENT_SHADER);
  gl4.glShaderSource(fragShader, 1, 
    new String[]{"#version 420 \n"
    +"out vec4 fragColor;"
    +"void main(void) {"
    +"fragColor = vec4(0.2, 0.2, 0.5, 1.0);}" }, null);
    gl4.glCompileShader(fragShader);
nabr
  • 131
  • 5
  • JOGL and the library used by the original poster might handle the shader source code a very little bit differently. – gouessej Nov 14 '17 at 13:16
  • at least its OpenGL :) – nabr Nov 15 '17 at 15:20
  • Yes but as far as I remember, there is a difference between them in handling the termination character '\0', I had to take care of that when I resurrected the JOGL backend of LibGDX. – gouessej Nov 18 '17 at 10:15
  • I read this few days later and now i think he had to set the forward compatible mode for mac: OpenGL: glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); Java: https://stackoverflow.com/questions/19870053/linking-and-compiling-shaders-for-opengl-3-3-and-2-1-using-lwjgl – nabr Nov 29 '17 at 04:42
1

If the other answer doesn't solve the problem for you, I suspect you're creating an OpenGL context with the wrong OpenGL version. You will need to create a context with the Core Profile - this also means that legacy functions and features are not supported, so if you've used any of those, you'll need to replace them with newer equivalents. (The core profile is not backwards-compatible.)

pmdj
  • 22,018
  • 3
  • 52
  • 103