opengl - 关于 OpenGL 布局 std140 统一 block 中的数组

标签 opengl graphics shader render

根据 specification :

If the member is an array of scalars or vectors, the base alignment + * and array stride are set to match the base alignment of a single + * array element, according to rules (1), (2), and (3), and rounded up + * to the base alignment of a vec4. The array may have padding at the + * end; the base offset of the member following the array is rounded up + * to the next multiple of the base alignment.



这是否意味着如果我有一个 (float)vec3 的大小为 3 的数组,它会是

vec3,vec3,vec3, (12 个空字节达到 vec4 的倍数), (16 个空字节因为最后一句)

或者

vec3, (4 个空字节),vec3,(4 个空字节)vec3,(4 个空字节), (16 个空字节因为最后一句)

最佳答案

来自实际OpenGL Specification, version 4.3 (PDF) :

3: If the member is a three-component vector with components consuming N basic machine units, the base alignment is 4N.

4: If the member is an array of scalars or vectors, the base alignment and array stride are set tomatch the base alignment of a single array element, according to rules (1), (2), and (3), and rounded up to the base alignment of a vec4. The array may have padding at the end; the base offset of the member following the array is rounded up to the next multiple of the base alignment.



所以一个 vec3具有 4*4 的基本对齐。 vec3 数组的基本对齐方式和数组步长's 因此是 4*4。步幅是从一个元素到下一个元素的字节数。所以每个元素的大小为 16 个字节,前 12 个是实际的 vec3数据。

最后,末尾的填充等于基本对齐,因此那里有空白空间。

或者,以图表的形式,vec3[3]看起来像这样:
|#|#|#|0|#|#|#|0|#|#|#|0| 

其中每个单元格为 4 个字节,#是实际数据,0是未使用的数据。

关于opengl - 关于 OpenGL 布局 std140 统一 block 中的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15616558/

相关文章:

c++ - glReadPixels 从错误的位置读取

c++ - 设置 OpenGL - 它不会工作

c++ - 找不到我的着色器(Obj-C++、ios、opengles2.0)

Java 多次 repaint() 调用被忽略

Java 图形对象未更新?

c# - Unity 中的闪光效果

java - 组成两个着色器(拾色器)

c++ - 调整我简单的 OpenGL/GLUT 窗口的大小会产生奇怪的线条

opengl - gl_ClipVertex 相对于 gl_ClipDistance 如何工作?

c# - 重新加载后图片框丢失绘图