根据 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/