layout(std140) help needed

I’m trying to get UBOs up and working. However, the returned offsets do not seem correct to me. The size metric is simply computed as the current offset minus the previous offset.

Maybe I’m doing something wrong or have misunderstood something. Any ideas?


layout(std140) uniform;

uniform DF_Globals
{
    float   Time;
    mat4    World;
    mat4    View;
    mat4    Projection;
    mat4    WorldView;
    mat4    WorldViewProj;

    vec4    BackBufferInfo;
    vec2    ViewportInfo;
};



Name:   Time
Index:  2
Offset: 0

Name:   World
Index:  5
Offset: 16
Size:   16

Name:   View
Index:  3
Offset: 80
Size:   64

Name:   Projection
Index:  1
Offset: 144
Size:   64

Name:   WorldView
Index:  6
Offset: 208
Size:   64

Name:   WorldViewProj
Index:  7
Offset: 272
Size:   64

Name:   BackBufferInfo
Index:  0
Offset: 336
Size:   64

Name:   ViewportInfo
Index:  4
Offset: 352
Size:   16

The whole point of using std140 layout is so that you don’t have to query offsets and such. The spec defines exactly where every element goes, how big it is, etc.

I understand that. However, I’m trying to run some sanity checks to ensure that things are behaving as expected. Is it not valid to query for offsets when using std140?

I guess my question is this: what exactly are you seeing that is wrong?

I guess technically nothing, after having re-read the spec again. :slight_smile: The key points being:

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

I haven’t read the spec for this, but surely it should be size = next offset - current offset, which would give:

Name:   Time (float)
Index:  2
Offset: 0
Size:   16 (12 unused bytes)

Name:   World (mat4)
Index:  5
Offset: 16
Size:   64

Name:   View (mat4)
Index:  3
Offset: 80
Size:   64

Name:   Projection (mat4)
Index:  1
Offset: 144
Size:   64

Name:   WorldView (mat4)
Index:  6
Offset: 208
Size:   64

Name:   WorldViewProj (mat4)
Index:  7
Offset: 272
Size:   64

Name:   BackBufferInfo (vec4)
Index:  0
Offset: 336
Size:   16

Name:   ViewportInfo (vec2)
Index:  4
Offset: 352
Size:   16? (guess - would be 8 unused bytes)

Dan you are correct. I realized that late last night after I posted. I think my sizes are correct, they’re just listed with the wrong variable (block). The sizes I have listed are actually for the memory block before the current block. Part of what lead to my initial confusion though. :o

Thanks for the help guys.