将块索引用作UniformBufferObject,ShaderStorageBufferObjects等的绑定点是否安全? [英] Is it safe to use the block index as the binding point for UniformBufferObject, ShaderStorageBufferObjects, etc?

查看:142
本文介绍了将块索引用作UniformBufferObject,ShaderStorageBufferObjects等的绑定点是否安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇几个OpenGL缓冲区对象相关函数中使用的*BlockBinding参数.

I'm curious about the *BlockBinding argument used in several of OpenGLs buffer object related functions.

例如,glUniformBlockBinding中的uniformBlockBinding参数,glShaderStorageBlockBinding中的storageBlockBinding​以及glBindBufferRangeglBindBufferBase中的相应index参数.

For example the uniformBlockBinding parameter in glUniformBlockBinding, storageBlockBinding​ in glShaderStorageBlockBinding, and the corresponding index parameter in glBindBufferRange and glBindBufferBase.

我知道,如果使用布局限定符在着色器中设置绑定点,则不必调用glUniformBlockBindingglShaderStorageBlockBinding:

I know that calls to glUniformBlockBinding and glShaderStorageBlockBinding aren't necessary if binding points are set in the shaders using layout qualifiers such as:

layout (binding = 0) blockName {...}

在我的机器上进行测试后,我注意到了三件事:

and from testing around on my machine I've noticed three things:

1)使用glUniformBlockBindingglShaderStorageBlockBinding设置绑定点会覆盖使用布局限定符在着色器中设置的绑定点.

1) setting binding points with glUniformBlockBinding and glShaderStorageBlockBinding override binding points set in the shader using layout qualifiers.

2)对于相同类型的每个块,从glGetUniformBlockIndexglGetProgramResourceIndex返回的块索引按从0到n的顺序排列.例如,如果着色器包含3个统一块和2个缓冲块,则返回的索引分别为[0,1,2]和[0,1].

2) block indices returned from glGetUniformBlockIndex and glGetProgramResourceIndex are ordered 0 to n for each block of the same type. For example if the shader contains 3 uniform blocks and 2 buffer blocks the indices returned would be [0,1,2] and [0,1] respectively.

3)绑定点(以任何一种方式设置)都不会在类型之间发生冲突.例如,将统一块设置为binding = 0并将缓冲块设置为binding = 0是完全安全的.

3) binding points, set either way, do not conflict across types. For example, setting a uniform block to binding = 0 and a buffer block to binding = 0 is completely safe.

请牢记这些假设(如果有必要不一定正确且只是巧合,请纠正我),是否有任何原因使我不应只是让我的代码自动设置相应块索引的参数,省去了自己通过gl*BlockBinding或使用布局限定符手动指定它们的麻烦.

With these assumptions in mind (please correct me if any aren't necessarily true and are simply coincidence), are there any reasons why I shouldn't just have my code automatically set the *BlockBinding argument to the corresponding block index and save myself the trouble of ever specifying them manually via gl*BlockBinding or with layout qualifiers.

推荐答案

请牢记这些假设(如果有必要不一定正确并且只是巧合,请纠正我),是否有任何原因使我不只是让我的代码自动将*BlockBinding参数设置为相应的块索引并省去了通过gl*BlockBinding或使用布局限定符手动指定它们的麻烦.

With these assumptions in mind (please correct me if any aren't necessarily true and are simply coincidence), are there any reasons why I shouldn't just have my code automatically set the *BlockBinding argument to the corresponding block index and save myself the trouble of ever specifying them manually via gl*BlockBinding or with layout qualifiers.

因为那将完全没有用.

将块索引任意分配给特定块.在一个平台上,特定块的索引可能为0,而另一个平台则对名称进行排序,使其索引为2.因此,您的代码将必须查询计划使用的每个块的块索引.

The block index is arbitrarily assigned to a particular block. One one platform, a particular block's index could be 0, while another sorts the names such that it's index 2. So your code will have to query the block indices for each block you plan to use.

如果您指定要用于特定块的绑定索引,则不必查询任何内容.您的代码知道绑定索引0是您的矩阵所在的位置,绑定索引1是您的照明数据所在的位置,等等.

Whereas if you specify what binding indices to use for a particular block, you don't have to query anything. Your code knows that binding index 0 is where your matrices go, binding index 1 is where your lighting data goes, etc.

更糟糕的是,您可能有多个使用同一块的着色器.但是他们极不可能拥有相同的区块 index .而如果给他们分配一个绑定索引,则可以给他们相同的绑定索引.因此,您不必在此类程序的更改之间重新绑定缓冲区.

What's worse is that you may have multiple shaders that use the same block. But they're highly unlikely to have the same block index. Whereas if you assign them a binding index, you can give them the same binding index. And therefore, you don't have to re-bind buffers between changes to such programs.

这篇关于将块索引用作UniformBufferObject,ShaderStorageBufferObjects等的绑定点是否安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆