goopenglglslgo-gl

GLSL Fragment Shader Fails to Compile with no message


I'm using the go-gl bindings for OpenGl.

I've defined the source for two shaders as follows:

const vertexShaderSource = `#version 460 compatibility
layout (location = 0) in vec3 aPos;
void main()
{
    gl_Position = vec4(aPos, 1.0);
}` + "\x00"
const fragmentShaderSource = `#version 460 compatibility
out vec4 FragColor;
void main()
{
    FragColor = vec4(0.5f, 0.1f, 0.1f, 1.0f);
}` + "\x00"

The vertex shader gets created and compiled without issue.

Here's code that creates and compiles the fragment shader:

fragmentShader := gl.CreateShader(gl.FRAGMENT_SHADER)
fragmentShaderSourceC := gl.Str(fragmentShaderSource)
gl.ShaderSource(fragmentShader, 1, &fragmentShaderSourceC, nil)
gl.GetShaderiv(fragmentShader, gl.COMPILE_STATUS, &success)
if success != gl.TRUE {
    var length int32
    infoLogC := [LOGSIZE]uint8{}
    gl.GetShaderInfoLog(fragmentShader, LOGSIZE, &length, &infoLogC[0])
    infoLog := gl.GoStr(&infoLogC[0])
    fmt.Printf("Got failure from fragment shader: %s\n", infoLog)
    fmt.Printf("gl version: %s\n", gl.GoStr(gl.GetString(gl.VERSION)))
    fmt.Printf("gl vendor: %s\n", gl.GoStr(gl.GetString(gl.VENDOR)))
    return
}

Which outputs the following (frustratingly, no info log message):

Got failure from fragment shader: 
gl version: 4.6 (Compatibility Profile) Mesa 23.0.4-0ubuntu1~23.04.1
gl vendor: Intel

I've tried toggling between version core/compatibility, changing the name of FragColor, and different values in vec4.

I've also tried commenting out the if block and continuing on to create the program anyway. The block that checks the program outputs an error, which is expected since the shader didn't compile: error: linking with uncompiled/unspecialized shader

Any feedback on how to get the fragment shader to compile?


Solution

  • Calling glShaderSource() only sets the source code for the shader. It does not compile. You need to separately call glCompileShader() after.

    gl.ShaderSource(fragmentShader, 1, &fragmentShaderSourceC, nil)
    gl.CompileShader(fragmentShader)
    

    The reason gl.GetShaderiv() with gl.COMPILE_STATUS results in success being gl.FALSE. Is because gl.COMPILE_STATUS only results in gl.TRUE if the last compilation was successful. However, since you never called gl.CompileShader(), then there was no previous successful compilation, thereby you receive gl.FALSE. Additionally, since there also hasn't been any error so far, then the info log is also empty.