0

I am having Uniform buffer object:

layout (std140) uniform ubo{
    vec3 A;
    float B;
    vec4 C;
    vec4 D;
    vec4 E;
    vec4 F;
    float G;
};

I am assuming offset of each of them as A: 0, B: 12, C: 16, D: 32 E: 48 F:64 G:80

But it doesn't seem so if i use all of them as vec4s everything works fine. What would be the offsets of each of them?

I tried with these new offsets: A: 0, B: 16, C: 32, D: 48 E: 64 F:80 G:96 but it still doesn't work

debonair
  • 2,505
  • 4
  • 33
  • 73

1 Answers1

-1

From ARB_uniform_buffer_object

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

Each vec3 counts as vec4 according to spec. I think that is only surprise that caused trouble for you.

Pauli Nieminen
  • 1,100
  • 8
  • 7
  • 2
    Oops. I made error there vec3 has alignment of 16 but still size of 12 and following float has alignment of 4 making vec3+float fit to 16 bytes. That means your original offset calculation was perfectly valid. That leaves a question why doesn't it work then. You could test changing vec3 to vec4 and see if you get it working then with all objects aligned to 16. – Pauli Nieminen Oct 25 '16 at 01:06
  • I already have it working with all vec4s but question is what's wrong with current one? – debonair Oct 25 '16 at 01:08
  • vec4 and access w as separate scalar is easier and more portable to implement. vec4 AB; vec4 C; ... Then do vec3 A = vec3(AB.xyz); float B = AB.w; That is same idea that Nicol's managed to post while I was still thinking why offsets are going wrong for you. – Pauli Nieminen Oct 25 '16 at 01:21