OpenGL Shader Compilation Issue -- Unexpected EOF

rjacks picture rjacks · Jun 19, 2011 · Viewed 10.4k times · Source

So I decided to try writing a simple OpenGL app using Java, just to see how it compared to my other efforts, and I'm running into an issue where my shaders refuse to compile. They really couldn't get much simpler, here's my vertex shader to demonstrate what I mean:

//Minimal vertex shader

#version 330

in vec3 in_Position;
in vec3 in_Color;
out vec3 var_Color;

void main(void)
{
    gl_Position = vec4(in_Position, 1.0);
    var_Color = in_Color;
}

The fragment shader is just as simple, so I won't bother posting it unless someone asks for it. When I check the logs, I get back the following error (for both shaders):

(0) : error C0000: syntax error, unexpected $end at token "<EOF>"

I'm not sure this is relevant... but I'm developing on Linux (Ubuntu 11.04) using Java. The only libraries I'm using are JOGL (for the openGL bindings) and the standard Java library (if that even counts...) My graphics card is a Nvidia GeForce 9600M GS, and I checked the extensions and it has full support for OpenGL 3.3.

Help me Stack Overflow, you're my only hope.

EDIT:

As requested, here is the function that is responsible for loading and compiling the shader source. Also, when it comes to GLSL I'm a super n00b, so I really don't know what to look for as far as making sure things are formatted properly for OpenGL. A link to a recent (i.e. dealing with OpenGL 3.x) tutorial on the subject would be appreciated.

private int CreateCompiledShader(File source, int shader, GL3 gl){
        int shaderloc = gl.glCreateShader(shader);

        BufferedReader input = null;
        ArrayList<String> lines = new ArrayList<String>();
        ArrayList<Integer> lengths = new ArrayList<Integer>();
        try{
            input = new BufferedReader(new FileReader(source));
            String buffer;

            while(input.ready()){
                buffer = input.readLine();
                lines.add(buffer);
                lengths.add(buffer.length());
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            if(input != null){
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        int[] iLengths = new int[lengths.size()];
        for(int i = 0; i < lengths.size(); i++){
        iLengths[i] = lengths.get(i).intValue();
        }

        gl.glShaderSource(shaderloc, lines.size(), lines.toArray(new String[0]), iLengths, 0);
        gl.glCompileShader(shaderloc);

        int error = gl.glGetError();
        if(error != GL3.GL_NO_ERROR){
            Logger.log(new GLU().gluErrorString(error), Logger.ERROR, "Shader compilation");
        }

        return shaderloc;
    }

As an aside, the if statement towards the end where I check glGetError() isn't actually where the error gets caught, that doesn't happen until execution returns to the calling function and I check the shader logs. Could be relevant, but then again I could also be rambling.

Answer

Nicol Bolas picture Nicol Bolas · Jun 19, 2011

OK, now I see the problem. Your loading code doesn't work. But don't worry; a lot of people get confused when they see that glShaderSource takes an array of strings. I'm guessing that you saw someone write their shaders in C/C++ like this:

const char *myShader[] = {
    "#version 330\n",
    "\n",
    "in vec3 in_position;\n",
    ...
};

And they uploaded the shader with, glShaderSource(shader, ARRAY_COUNT(myShader), myShader, NULL, 0);

While this is legal, that's not really what the feature is for. Because GLSL does not have a #include mechanism, glShaderSource can take multiple strings. Each string is intended to be a shader file. The shader compiler then effectively concatenates the strings together like #include does before compiling.

Now, because of this, you can have each line as a separate string. However, look back that that C/C++ code. See what is at the end of each line? That '\n' character.

That's what is not at the end of the lines you are loading. Because I'm pretty sure BufferedReader.readline does not keep the end-of-line character. So your shader looks like:

//Minimal vertex shader#version 330in vec3 in_Position;in vec3 in_Color;out vec3 var_Color;...

Your entire shader is seen as one big single-line comment. Hence the unexpected EOF: OpenGL never saw anything to compile ;)

You shouldn't be reading the file line by line. Just load the whole thing up into a single string. Then pass it to OpenGL. Alternatively, you can look at this previous answer about JOGL; it should show you how to do it correctly (though I would hope that BufferedReader would have some way to read the whole file as a string rather than line-by-line.