OpenGL Shader Compilation Issue — Unexpected EOF

前端 未结 3 1682
误落风尘
误落风尘 2021-02-08 20:00

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. T

相关标签:
3条回答
  • 2021-02-08 20:06

    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.

    0 讨论(0)
  • 2021-02-08 20:12

    Although an answer already has peen provided and accepted, I'll just write here how I'd prefer it:

    // Create shader from one or multiple source files
    private int CreateCompiledShader(File[] source_files, int shader, GL3 gl){
        int shaderloc = gl.glCreateShader(shader);
        int nSources = source_files.size();
    
        // the number of shader sources it known from the beginning
        // so we can allocate the arrays right here
        String[] sources = new String[nSources];
        int[] sources_lengths = new int[nSources];
        for(int i = 0; i < nSources; i++) {
            // We don't need a buffered reader as we're reading the file as a whole
            FileReader input = new FileReader(source_file[i]);
            String buffer;
    
            buffer = input.read();
            sources[i] = buffer;
            sources_lengths[i] = buffer.length();
    
            if(input != null){
                input.close();
            }
        }
    
        // Frankly I really don't understand why you have to pass sources_lengths here
        // It would take only 3 LOC in the binding's code
        // to extract that from the sources array, Oh, well...
        gl.glShaderSource(shaderloc, nSources, sources, sources_lengths, 0);
        gl.glCompileShader(shaderloc);
    
        // Actually if glGetError() returns an error you must call it in a loop
        // as OpenGL errors can accumulate, and you have to pop them all from the list.
        int error = gl.glGetError();
        if(error != GL3.GL_NO_ERROR){
            Logger.log(new GLU().gluErrorString(error), Logger.ERROR, "Shader compilation");
        }
    
        return shaderloc;
    }
    

    I took the liberty to remove all try/catch/finally blocks, as they were misplaced a bit: If reading any source file fails, shader loading cannot complete, so it makes no sense to continur gracefully. The propper way to deal with this would be a large try/catch block arround this, that cleans up OpenGL objects of shader compilation does not complete.

    0 讨论(0)
  • 2021-02-08 20:16

    Try moving the comment to after the #version declaration. It shouldn't matter, but there can be driver bugs.

    Also, try putting an extra empty line at the end of the file. Again, just to be sure.

    Lastly, make sure that you are in fact loading the string properly. Check it in the debugger. And make sure you're passing the string to OpenGL correctly. What does that code look like?

    0 讨论(0)
提交回复
热议问题