简体   繁体   English

关于OpenGL中连接compute shader程序的问题

[英]Question about linking compute shader program in OpenGL

I'm trying to create a single compute a shader program computeProgram and attach two source codes on it.我正在尝试创建一个单独的计算着色器程序computeProgram并在其上附加两个源代码。 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.我在两个着色器源代码中都有main函数,我明白为什么这行不通,因为一个程序中预期只有一个main function。 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.但是我的问题来了:如果我试图将顶点着色器源和片段着色器源链接到单个程序,比如renderProgram ,还有两个main函数,一个在顶点着色器中,一个在片段着色器中。 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?如果我想使用这两个计算着色器,我是否应该创建两个计算程序以避免main function 的重复?

Any help is appreciated!!任何帮助表示赞赏!

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.每个着色器阶段都需要 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?如果我想使用这两个计算着色器,我是否应该创建两个计算程序以避免主 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.当您有两个计算着色器,每个计算着色器都根据 main() function 定义自己的功能时,然后创建两个着色器程序,每个着色器程序都链接到一个着色器对象。 Especially, when your two shaders have completely different interfaces with the host, such as SSBOs or samplers/images.特别是当您的两个着色器与主机具有完全不同的接口时,例如 SSBO 或采样器/图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM