Home > other >  Question about linking compute shader program in OpenGL
Question about linking compute shader program in OpenGL

Time:06-04

I'm trying to create a single compute a shader program computeProgram and attach two source codes on it. Here are my codes:

    unsigned int computeProgram = glCreateProgram();
    glAttachShader(computeProgram, MyFirstComputeShaderSourceCode);
    glAttachShader(computeProgram, MySecondComputeShaderSourceCode);
    glLinkProgram(computeProgram);
    

    glGetProgramiv(computeProgram, GL_LINK_STATUS, &success);
    if (!success) {
        glGetProgramInfoLog(computeProgram, 512, NULL, infoLog);
        std::cout << "ERROR::SHADER::COMPUTE_PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
        exit(1);
    }

I get this type of linking error information:

ERROR::SHADER::COMPUTE_PROGRAM::LINKING_FAILED
ERROR: Duplicate function definitions for "main"; prototype: "main()" found.

I do have main functions in both shader source codes, and I understand why this is not gonna work cause there is only one main function expected in one program. But here comes my question: If I'm trying to link a vertex shader source and a fragment shader source to a single program, say, renderProgram, there are also two main functions, one in vertex shader, one in fragment shader. However, if I link these two, it somehow works fine.

Why is this difference happen? And if I want to use these two compute shaders, am I supposed to create two compute programs in order to avoid duplication of the main function?

Any help is appreciated!!

CodePudding user response:

Why is this difference happen?

When you link a vertex shader and a fragment shader to the same shader program, then those two (as their names imply) are in different shader stages. Every shader stage expects exactly one definition of the main() function.

When you attach two shaders that are in the same shader stage, such as your two compute shader objects, then those get linked into the same shader stage (compute). And that does not work.

And if I want to use these two compute shaders, am I supposed to create two compute programs in order to avoid duplication of the main function?

Yes. When you have two compute shaders that each define their own functionality in terms of a main() function, then creating two shader programs each with one of the shader objects linked to it would work. Especially, when your two shaders have completely different interfaces with the host, such as SSBOs or samplers/images.

  • Related