glUniformBlockBinding用作什么? [英] What's glUniformBlockBinding used for?

查看:472
本文介绍了glUniformBlockBinding用作什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个着色器程序,其索引为0,具有UniformBlock.

Assuming I have a shader program with a UniformBlock at index 0.

绑定UniformBuffer,以下显然足以将UniformBuffer绑定到块:

Binding the UniformBuffer the following is apparently enough to bind a UniformBuffer to the block:

glUseProgram(program);
glBindBuffer(GL_UNIFORM_BUFFER, buffer);
glBindBufferBase(GL_UNIFORM_BUFFER, 0, buffer);

仅在将缓冲区绑定到与着色器程序中使用的索引不同的索引时,才需要使用glUniformBlockBinding.

I only have to use glUniformBlockBinding when I bind the buffer to a different index than used in the shader program.

//...
glBindBufferBase(GL_UNIFORM_BUFFER, 1, buffer)
glUniformBlockBinding(program, 0, 1); // bind uniform block 1 to index 0

我理解正确吗?如果我在适当的块具有不同索引的不同程序中使用缓冲区,是否只需要使用glUniformBlockBinding?

Did I understand it right? Would I only have to use glUniformBlockBinding if I use use the buffer in different programs where the appropriate blocks have different indices?

推荐答案

每个程序的活动统一块索引与全局绑定位置不同.

这里的总体思路是,假设您使用正确的布局,则可以将统一的缓冲区绑定到GL中的一个位置,并在多个GLSL程序中使用它.但是,该命令需要建立每个程序的单独缓冲区块索引与GL的全局绑定点之间的映射.

Per-program active uniform block indices differ from global binding locations.

The general idea here is that assuming you use the proper layout, you can bind a uniform buffer to one location in GL and use it in multiple GLSL programs. But the mapping between each program's individual buffer block indices and GL's global binding points needs to be established by this command.

采样器的统一位置与其他统一位置相同,但是该位置实际上与采样器使用的纹理图像单元无关.例如,您仍然将纹理绑定到GL_TEXTURE7而不是采样器统一的位置.

Samplers have a uniform location the same as any other uniform, but that location actually says nothing about the texture image unit the sampler uses. You still bind your textures to GL_TEXTURE7 for instance instead of the location of the sampler uniform.

在这方面,采样器和统一缓冲区之间唯一的概念差异是您没有使用glUniform1i (...)设置索引来分配绑定位置.有一个特殊的命令可以对统一缓冲区执行此操作.

The only conceptual difference between samplers and uniform buffers in this respect is that you do not assign the binding location using glUniform1i (...) to set the index. There is a special command that does this for uniform buffers.

从GLSL 4.20开始(并由GL_ARB_shading_language_420pack ),还可以从着色器内部显式建立统一块的绑定位置.

Beginning with GLSL 4.20 (and applied retroactively by GL_ARB_shading_language_420pack), you can also establish a uniform block's binding location explicitly from within the shader.

layout (std140, binding = 0) uniform MyUniformBlock
{
  vec4 foo;
  vec4 bar;
};

通过这种方式,您不必确定 MyUniformBlock 的统一块索引;该链接块在链接时将被绑定为 0 .

Done this way, you never have to determine the uniform block index for MyUniformBlock; this block will be bound to 0 at link-time.

这篇关于glUniformBlockBinding用作什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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