上传统一块的正确顺序是什么? [英] What is the correct sequence for uploading a uniform block?

查看:37
本文介绍了上传统一块的正确顺序是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

https://www.lighthouse3d.com 的示例页面中/tutorials/glsl-tutorial/uniform-b​​locks/ 有这个:

  1. uniformBlockBinding()
  2. bindBuffer()
  3. bufferData()
  4. bindBufferBase()

但从概念上讲,这不是更正确吗?

But conceptually, wouldn't this be more correct?

  1. bindBuffer()
  2. bufferData()
  3. uniformBlockBinding()
  4. bindBufferBase()

上传到缓冲区 (bindBuffer+bufferData) 的想法应该不知道缓冲区将用于什么 - 然后,分别地,uniformBlockBinding()+bindBufferBase() 将用于更新这些制服,每个着色器,当相关缓冲区发生变化时?

The idea being that uploading to a buffer (bindBuffer+bufferData) should be agnostic about what the buffer will be used for - and then, separately, uniformBlockBinding()+bindBufferBase() would be used to update those uniforms, per shader, when the relevant buffer has changed?

推荐答案

添加答案,因为接受的答案有很多与 WebGL2 无关的信息

Adding answer since the accepted answer has lots of info irrelevant to WebGL2

在初始化时调用uniformBlockBinding.对于给定的程序,它设置特定程序将从哪个统一缓冲区索引绑定点获得特定的统一缓冲区.

At init time you call uniformBlockBinding. For the given program it sets up which uniform buffer index bind point that particular program will get a particular uniform buffer from.

在渲染时调用 bindBufferRangebindBufferBase 将特定缓冲区绑定到特定统一缓冲区索引绑定点

At render time you call bindBufferRange or bindBufferBase to bind a specific buffer to a specific uniform buffer index bind point

如果您还需要将新数据上传到该缓冲区,则可以调用 bufferData

If you also need to upload new data to that buffer you can then call bufferData

伪代码

// at init time

for each uniform block
   gl.uniformBlockBinding(program, indexOfBlock, indexOfBindPoint)

// at render time

for each uniform block
   gl.bindBufferRange(gl.UNIFORM_BUFFER, indexOfBindPoint, buffer, offset, size)
   if (need to update data in buffer)
      gl.bufferData/gl.bufferSubData(gl.UNIFORM_BUFFER, data, ...)

请注意,没有正确"的顺序.这里的问题是如何更新缓冲区完全取决于您.由于您可能将多个统一缓冲区数据以不同的偏移量存储在单个缓冲区中,因此像上面这样调用 gl.bufferData/gl.bufferSubData 确实不正确",这只是 100 秒的一种方式.

Note that there is no "correct" sequence. The issue here is that how you update your buffers is really up to you. Since you might store multiple uniform buffer datas in a single buffer at different offsets then calling gl.bufferData/gl.bufferSubData like above is really not "correct", it’s just one way of 100s.

WebGL2 (GLES 3.0 ES) 不支持接受的答案中提到的 layout(binding = x).WebGL2 中也没有 glGenBuffers 之类的东西

WebGL2 (GLES 3.0 ES) does not support the layout(binding = x) mentioned in the accepted answer. There is also no such thing as glGenBuffers in WebGL2

这篇关于上传统一块的正确顺序是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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