Opengl Unifrom缓冲区对象中成员的对齐

Opengl Unifrom缓冲区对象中成员的对齐,opengl,Opengl,我有统一的缓冲区对象: layout (std140) uniform ubo{ vec3 A; float B; vec4 C; vec4 D; vec4 E; vec4 F; float G; }; 我假设它们的偏移量为A:0,B:12,C:16,D:32e:48f:64g:80 但如果我把它们都用在向量机上,情况就不一样了,一切都很好。 它们各自的偏移量是多少 我尝试了这些新的偏移: A:0,B:16,C:32,D:48 E:64

我有统一的缓冲区对象:

layout (std140) uniform ubo{
    vec3 A;
    float B;
    vec4 C;
    vec4 D;
    vec4 E;
    vec4 F;
    float G;
};
我假设它们的偏移量为A:0,B:12,C:16,D:32e:48f:64g:80

但如果我把它们都用在向量机上,情况就不一样了,一切都很好。 它们各自的偏移量是多少

我尝试了这些新的偏移:
A:0,B:16,C:32,D:48 E:64 F:80 G:96,但从ARB\u uniform\u buffer\u对象来看,它仍然不起作用

(1)如果成员是消耗基本机器单元的标量,则
基础对齐是非常重要的。
(2) 如果成员是包含组件的两个或四个组件向量
使用基本机器单元时,基本对齐为2或
分别为4。
(3) 如果成员是三分量向量,且分量消耗
基本机器单元,基本校准为4。
(4) 如果成员是标量或向量数组,则基对齐
和阵列跨距设置为与单个阵列的基本对齐方式相匹配
数组元素,并根据规则(1)、(2)和(3)向上取整
到vec4的基准对齐。该数组的底部可能有填充
终止数组后面的成员的基偏移向上舍入
到基准路线的下一个倍数。
(5) 如果成员是包含列和的列主矩阵
行中,矩阵的存储方式与列的数组相同
根据第(4)条的规定,每个向量都有分量。
(6) 如果成员是列主矩阵的数组,则
列和行中,矩阵的存储方式与
*根据规则,每个组件都包含列向量
(4).
(7) 如果成员是具有列和行的行主矩阵,
矩阵以与行向量数组相同的方式存储
根据第(4)条的规定,每个组件都有。
(8) 如果成员是具有列的行主矩阵数组
和行,矩阵的存储方式与*
根据第(4)条的规定,每个行向量都包含组件。
(9) 如果杆件是结构,则结构的基准对齐为
,其中是其任意位置的最大基准对齐值
成员,并四舍五入到vec4的基本对齐方式。这个
然后,将为该子结构的各个构件指定偏移
通过递归应用这组规则,其中
子结构的第一个构件等于对齐偏移
这是结构的一部分。该结构的末端可能有衬垫;这个
子结构后面的杆件的基准偏移向上舍入
到结构的基础路线的下一个倍数。
(10) 如果成员是结构数组,则
根据规则(9),阵列按顺序排列。

根据规范,每个vec3都算作vec4。我想这是给您带来麻烦的唯一惊喜。

Oops。我在那里犯了一个错误,vec3的对齐方式是16,但大小仍然是12,下面的float的对齐方式是4,这使得vec3+float适合16字节。这意味着您最初的偏移计算完全有效。这就留下了一个问题,为什么它不起作用呢。您可以测试将vec3更改为vec4,然后查看是否可以使用与16对齐的所有对象。我已经使用了所有vec4,但问题是当前的vec4有什么问题?vec4和access w作为单独的标量更容易实现,更便于移植。vec4抗体;VEC4C。。。然后做vec3 A=vec3(AB.xyz);浮点数B=AB.w;这和Nicol在我还在思考为什么偏移量对你来说是错误的时候发布的想法是一样的。请注意,我没有使用结构将数据传递给GPU。我使用带有偏移量的void指针“实现支持是模糊的”
  (1) If the member is a scalar consuming <N> basic machine units, the
      base alignment is <N>.

  (2) If the member is a two- or four-component vector with components
      consuming <N> basic machine units, the base alignment is 2<N> or
      4<N>, respectively.

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

  (4) 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.

  (5) If the member is a column-major matrix with <C> columns and <R>
      rows, the matrix is stored identically to an array of <C> column
      vectors with <R> components each, according to rule (4).

  (6) If the member is an array of <S> column-major matrices with <C>
      columns and <R> rows, the matrix is stored identically to a row of
      <S>*<C> column vectors with <R> components each, according to rule
      (4).

  (7) If the member is a row-major matrix with <C> columns and <R> rows,
      the matrix is stored identically to an array of <R> row vectors
      with <C> components each, according to rule (4).

  (8) If the member is an array of <S> row-major matrices with <C> columns
      and <R> rows, the matrix is stored identically to a row of <S>*<R>
      row vectors with <C> components each, according to rule (4).

  (9) If the member is a structure, the base alignment of the structure is
      <N>, where <N> is the largest base alignment value of any of its
      members, and rounded up to the base alignment of a vec4. The
      individual members of this sub-structure are then assigned offsets 
      by applying this set of rules recursively, where the base offset of
      the first member of the sub-structure is equal to the aligned offset
      of the structure. The structure may have padding at the end; the 
      base offset of the member following the sub-structure is rounded up
      to the next multiple of the base alignment of the structure.

  (10) If the member is an array of <S> structures, the <S> elements of
       the array are laid out in order, according to rule (9).