着色器不会链接,但没有错误显示

时间:2016-01-06 13:27:22

标签: lwjgl

嗯,标题几乎说明了一切,我的着色器赢得了链接。

输出:

 [INFO|2016-01-06 18:21:27|Renderer Thread (Test window)] Shader compile status is 1, log: 
 [INFO|2016-01-06 18:21:27|Renderer Thread (Test window)] Shader compile status is 1, log: 
 [INFO|2016-01-06 18:21:27|Renderer Thread (Test window)] Link status: 0
 [INFO|2016-01-06 18:21:27|Renderer Thread (Test window)] Link errors:

链接状态0显然意味着失败。但为什么?显然,glUseProgram因错误1282而失败,因为程序无法链接。

链接代码:

    int vshId = this.loadShader(this.getClass().getResource("/shaders/blocks.vsh"), GL20.GL_VERTEX_SHADER);
    int fshId = this.loadShader(this.getClass().getResource("/shaders/blocks.fsh"), GL20.GL_FRAGMENT_SHADER);
    int programId = GL20.glCreateProgram();
    GL20.glAttachShader(programId, vshId);
    GL20.glAttachShader(programId, fshId);
    GL20.glLinkProgram(programId);
    GL20.glValidateProgram(programId);
    IntBuffer buf = BufferUtils.createIntBuffer(1);
    GL20.glGetProgramiv(programId, GL20.GL_LINK_STATUS, buf);
    int linkStatus = buf.get();
    Logger.getInstance().logf("Link status: %s", linkStatus);
    Logger.getInstance().logf("Link errors: %s", GL20.glGetProgramInfoLog(programId));

loadShader函数:

public int loadShader(URL resource, int type) {
    InputStream stream;
    try {
        stream = resource.openStream();
    } catch (IOException e) {
        e.printStackTrace();
        return -1;
    }

    String s = "";
    byte[] b = new byte[256];
    try {
        while (stream.read(b) > -1) {
            s += new String(b);
        }
    } catch (IOException e) {
        e.printStackTrace();
        return -2;
    }
    String shaderSrc = s;
    int shaderID = GL20.glCreateShader(type);
    GL20.glShaderSource(shaderID, shaderSrc);
    GL20.glCompileShader(shaderID);
    int status = GL20.glGetShaderi(shaderID, GL20.GL_COMPILE_STATUS);
    Logger.getInstance().logf("Shader compile status is %s, log: %s", status, GL20.glGetShaderInfoLog(shaderID));
    return shaderID;
}

这两个片段是SpriteManager类(https://gitlab.com/MRebhan/RetroEngine/blob/master/src/de/marco_rebhan/retroengine/texture/SpriteManager.java

的一部分

片段着色器:

#version 130

in vec2 tex_coord;
uniform sampler2D tex;

void main(void) {
  gl_FragColor = texture(tex, tex_coord);
}

顶点着色器:

#version 130

in vec2 position;

void main(void) {
  gl_Position = vec4(position, 0.0, 1.0);
}

为什么会这样?我没有看到任何错误。当我将垃圾线放入着色器源时,它会显示0:3(1): error: syntax error, unexpected NEW_IDENTIFIER。我明白为什么不能成功编译,但我不明白为什么正确的着色器不能正常工作......请帮助。

可以在https://gitlab.com/MRebhan/RetroEngine

查看整个项目

1 个答案:

答案 0 :(得分:0)

我现在感到很愚蠢。首先,在glValidateProgam出现错误之前调用glGetProgrami,即顶点着色器中缺少out vec2 tex_coord。 DERP。现在它正在运作。 我将把固定版本放在这里:

顶点着色器:

#version 130

in vec2 position;
out vec2 tex_coord;

void main(void) {
  gl_Position = vec4(position, 0.0, 1.0);
  tex_coord = position;
}

链接着色器代码:

int vshId = this.loadShader(this.getClass().getResource("/shaders/blocks.vsh"), GL20.GL_VERTEX_SHADER);
int fshId = this.loadShader(this.getClass().getResource("/shaders/blocks.fsh"), GL20.GL_FRAGMENT_SHADER);
int programId = GL20.glCreateProgram();
GL20.glAttachShader(programId, vshId);
GL20.glAttachShader(programId, fshId);
GL20.glLinkProgram(programId);
int linkStatus = GL20.glGetProgrami(programId, GL20.GL_LINK_STATUS);
GL20.glValidateProgram(programId);
Logger.getInstance().logf("Link status: %s", linkStatus);
Logger.getInstance().logf("Link errors: %s", GL20.glGetProgramInfoLog(programId));