简体   繁体   中英

Is there any way to enforce a 16 byte alignment for a uniform buffer in GLSL?

I'm targeting WebGL via wgpu and am running into an issue with uniform buffer alignment.

I am trying to use this uniform:

layout(set=0, binding=2, std140)
uniform TexSize {
    ivec2 dimensions;
};

And I get an error BUFFER_BINDINGS_NOT_16_BYTE_ALIGNED .

Checking with the maintainers of wgpu, I was informed this was because of the flavor of GLSL used by WebGPU, and that the uniform buffer in my shader must be 16-byte-aligned.

I can solve this by padding the struct out to have a 16 byte alignment:

layout(set=0, binding=2, std140)
uniform TexSize {
    ivec2 dimensions;
    ivec2 padding;
};

But this seems rather inelegant. Is there any way to set the alignment of TexSize without just adding other members to pad it out?

No. uniforms require 16 byte (4 float) spacing. you need to add padding.

Address Space Layout Constraints of wgsl specification: https://www.w3.org/TR/WGSL/#address-space-layout-constraints

GPUs need to do allot of 3 or 4 Dimensional Matrix arithmetic on 16Byte floats. This is why GPUs are heavily optimised for this type of Calculations. And this is why The Spacing of uniforms is 4 X 16Byte Float.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM