opengl - glBindAttribLocation,着色器中不存在名称参数

标签 opengl glsl shader

我一直在阅读这个问题和此处接受的答案:Explicit vs Automatic attribute location binding for OpenGL shaders

我存储了一个硬编码的字符串数组来表示可供顶点着色器使用的可用属性。在加载着色器期间,我指定属性位置,如下所示:

for ( int i = 0; i < Attribute::eCount; ++i )
{
    const char* name = attributeTable.Find( i );

    glBindAttribLocation( program, i, (const GLchar*)name );
}

我想知道当提供的名称参数在着色器中的任何位置都不存在时调用 glBindAttribLocation 是否合适。

如果我们读到这里:https://www.khronos.org/opengles/sdk/docs/man/xhtml/glBindAttribLocation.xml ,它指出“也允许将通用属性索引绑定(bind)到从未在顶点着色器中使用的属性变量名称。”

听起来如果在着色器中定义了某个属性但未使用该属性,那么调用 glBindAttribLocation 是可以的。我想知道如果该属性根本不存在是否可以。我的预感是这很好,因为 glsl 编译器会积极删除未使用的代码,但我似乎无法验证这一点。

最佳答案

为不存在的属性调用 glBindAttribLocation() 是完全合法的,而不是错误。 OpenGL 规范中特别提到了这一点。以下内容摘自 OpenGL 4.5 规范第 365 页,第 11.1.1 节“顶点属性”:

BindAttribLocation may be issued before any vertex shader objects are attached to a program object. Hence it is allowed to bind any name to an index, including a name that is never used as an attribute in any vertex shader object. Assigned bindings for attribute variables that do not exist or are not active are ignored.

关于opengl - glBindAttribLocation,着色器中不存在名称参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29616603/

相关文章:

opengl - 使用 gl_FragColor 与 out vec4 颜色?

ios - 在 IOS 上的 OpenGL ES 2.0 着色器中绘制简单线条

c++ - 如何在 C++ 中使用 GLUT/OpenGL 绘制文本?

c++ - OpenGL glDeleteTextures 导致内存泄漏?

macos - 如何在 Mac OS 上使用 glGetIntegerv(GL_TEXTURE_BINDING_2D, ...) ?

c++ - 为什么法线使用相机空间而不是模型空间?

c - 为什么我使用此 opengl 应用程序会出现黑屏?

java - 如何从 OpenGL ES 2.0 中的数组中读取超过 4 个顶点数据?

unity3d - 在 Unity 着色器中波动浮点值

c++ - ID3D11ShaderResourceView 的尺寸