Regarding arrays in layout std140 uniform block for OpenGL

旧巷老猫 提交于 2019-12-31 00:44:08

问题


According to 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.

Does this mean that if I had an array of size 3 of a (float)vec3, would it be

vec3,vec3,vec3, (12 empty bytes to reach a vec4 multiple), (16 empty bytes because of the last sentence)

or

vec3, (4 empty bytes),vec3,(4 empty bytes)vec3,(4 empty bytes), (16 empty bytes because of the last sentence)


回答1:


From the actual 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.

So a vec3 has a base alignment of 4*4. The base alignment and array stride of an array of vec3's is therefore 4*4. The stride is the number of bytes from one element to the next. So each element is 16 bytes in size, with the first 12 being the actual vec3 data.

Finally, there is padding equal to the base alignment at the end, so there is empty space from that.

Or, in diagram form, a vec3[3] looks like this:

|#|#|#|0|#|#|#|0|#|#|#|0| 

Where each cell is 4 bytes, # is actual data, and 0 is unused data.




回答2:


Neither.

The appendix L from the redbook states this:

  • An array of scalars or vectors -> Each element in the array is the size of the underlying type (sizeof(vec4) for vec3), and the offset of any element is its index (using zero-based indexing) times the elements size (again sizeof(vec4)). The entire array is padded to be a multiple of the size of a vec4.

So the correct answer is vec3, (4 empty), vec3, (4 empty),vec3, (4 empty) -> 48 bytes



来源:https://stackoverflow.com/questions/15616558/regarding-arrays-in-layout-std140-uniform-block-for-opengl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!