关于在OpenGL中链接计算着色器程序的问题

发布于 2025-02-04 09:03:17 字数 1114 浏览 3 评论 0原文

我正在尝试创建一个计算着色器程序Computepragram,并在其上附加两个源代码。这是我的代码:

    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);
    }

我获得了这种类型的链接错误信息:

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

我确实在两个着色器源代码中都有MAIM函数,我明白为什么这不起作用,因为只有一个main在一个程序中预期的功能。但是我的问题来了:如果我试图将顶点着色器源和片段着色源链接到单个程序,例如RenderProgram,也有两个main功能,一个在顶点着色器中,一个在碎片着色器中。但是,如果我链接这两个,则可以以某种方式工作。

为什么会发生这种差异?而且,如果我想使用这两个计算着色器,我应该创建两个计算程序以避免重复main函数吗?

任何帮助将受到赞赏!!

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!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱情眠于流年 2025-02-11 09:03:17

为什么会发生这种差异?

当您将顶点着色器和片段着色器链接到同一着色器程序时,那两个(如其名称所暗示的)在不同的着色器阶段。每个着色器阶段都期望Main()函数的一个 一个

当您连接两个在同一着色器阶段的着色器(例如两个计算着色器对象>(计算)。这是行不通的。

,如果我想使用这两个计算着色器,我应该创建两个计算程序以避免重复主函数吗?

是的。当您有两个计算着着色器时,每个计算着色器都根据主()函数来定义自己的功能,然后创建两个链接到它的着色器对象的阴影程序程序。特别是当您的两个着色器与主机(例如SSBOS或Samplers/Images)具有完全不同的接口时。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文