Talk:Uniform Buffer Object

From OpenGL Wiki
Revision as of 18:06, 8 December 2011 by Alfonse (talk | contribs)
Jump to navigation Jump to search


I removed this:

When uploading matrices into your UBO, if your layout is std140, your matrix rows might need to be padded to a specific size, just like how vec3s might need to be be padded to vec4s. For example, when uploading a mat3, send a mat4 and let your shader cast it back to a mat3 in the uniform block declaration.

This is not true. This may be in reference to some driver bug that someone encountered. If so, then it should be explicitly marked as such, with a driver version and the hardware it affects (NVIDIA/ATI/etc).

OpenGL is very clear on how std140 works. There is no ambiguity on the matter; there is no "might" anything. That's the whole point: to know up-front how big stuff is. This page is for describing how OpenGL works. Alfonse 11:24, 7 December 2011 (PST)

Hi Alfonse, thanks for your response. I used "might" in the context of "this might be your problem." I agree it was a poor choice of words. Maybe you can help me clarify what the Red Book says about matrices in std140?:
Stored as an array of C vectors each with R components, and potentially padded like other arrays. If the variable is an array of M column-major matrices, it is stored as an array of M C vectors each with R components (and potentially padded).
Does this mean that a mat3 is treated as an array of vec3s, and is therefore padded like vec3s are padded to vec4s? Thanks in advance for clearing this up. Amoffat 16:17, 7 December 2011 (PST)
The best place to learn how std140 works is to look at the actual specification. The pertinent rules are:
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 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 compo-nents each, according to rule (4).
So a mat3, under std140, will effectively use a vec4 for each column. But so will a mat2, since it rounds the alignment and stride up to that of a vec4.