bevy icon indicating copy to clipboard operation
bevy copied to clipboard

Add padded static size and recursive padding to crevice and use it

Open superdump opened this issue 4 years ago • 2 comments

This appears to work just fine for rounding up the sizes. We had the code already for rounding up sizes to the struct alignment in UniformVec for item sizes so I just moved that into crevice as a std140_padded_size_static and tested it - it returns what wgpu expects for wgsl for GpuLights and ViewUniform min_binding_size at least. I'll let them decide if it is fully correct, but if it isn't then we need to fix UniformVec too I expect.

superdump avatar Jul 01 '21 10:07 superdump

Looking at the wgsl spec: https://www.w3.org/TR/WGSL/#alignment-and-size

For wgsl: it looks like a struct's alignment is the maximum alignment of its members. And a struct's size is the offset of the last member of the struct, plus the size of the last member of the struct (which is what std140_size_static() returns, unless I am mistaken) rounded up to the alignment of the struct, which is what the Std140Type::ALIGNMENT is. So I think this should be correct for structs.

I think implicit-stride arrays work the same way as glsl arrays, except that glsl seems to say things about aligning to vec4 unconditionally in some situations:

  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 2N or 4N, respectively.
  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.

It's not exactly clear to me what the rounded up to the base alignment of a vec4 means. Is it talking about the size?

superdump avatar Jul 01 '21 10:07 superdump

I'll keep the discussion going over there, but I found an error case: https://github.com/LPGhatguy/crevice/issues/29#issuecomment-872282150

superdump avatar Jul 01 '21 14:07 superdump