Regarding arrays in layout std140 uniform block for OpenGL

后端 未结 2 902
被撕碎了的回忆
被撕碎了的回忆 2021-01-19 19:32

According to specification:

If the member is an array of scalars or vectors, the base alignment + * and array stride are set to match the bas

相关标签:
2条回答
  • 2021-01-19 20:07

    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

    0 讨论(0)
  • 2021-01-19 20:21

    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.

    0 讨论(0)
提交回复
热议问题