呃呃,看到高级GLSL语言了,但是在看Uniform块布局的时候有点云里雾里的,基本对齐量和对齐偏移量概念不清楚,上网找了找大佬的文章,下面贴上,做个小记录。
基本定义
When using the “std140” storage layout, structures will be laid out in
buffer storage with its members stored in monotonically increasing order
based on their location in the declaration. A structure and each
structure member have a base offset and a base alignment, from which an
aligned offset is computed by rounding the base offset up to a multiple of
the base alignment. The base offset of the first member of a structure is
taken from the aligned offset of the structure itself. The base offset of
all other structure members is derived by taking the offset of the last
basic machine unit consumed by the previous member and adding one.
Each structure member is stored in memory at its aligned offset. The members
of a top-level uniform block are laid out in buffer storage by treating
the uniform block as a structure with a base offset of zero.
- 一个ubo结构的"std140"存储布局
- 一个ubo结构和其中的结构成员存在一个基本偏移量(Base Offset)和一个基本对齐量(Base Alignment)。
- 结构成员的对齐偏移量 = 【基本偏移量的值四舍五入,变为基本对齐量的倍数(0,1,2…)】
- 一个ubo结构的第一个成员的基本偏移量 = 这个ubo结构相对于起始ubo结构的对齐偏移量
- 一个ubo结构的非第一个成员的基本偏移量=上一个成员的对齐偏移量 + 上一个成员的真正占用量
- 一个ubo结构的占用量按照下述规则计算出来后,还需要约束成vec4占用量的倍数
详见下图:
GLSL中各种变量的布局规则: