我一直在阅读这个问题和此处接受的答案: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/