简体   繁体   English

有没有办法在 GLSL 中为统一缓冲区强制执行 16 字节对齐?

[英]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.我通过 wgpu 以 WebGL 为目标,但遇到了统一缓冲区对齐的问题。

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 .我收到错误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.与 wgpu 的维护者核实后,我得知这是因为 WebGPU 使用的 GLSL 的风格,并且我的着色器中的统一缓冲区必须是 16 字节对齐的。

I can solve this by padding the struct out to have a 16 byte alignment:我可以通过填充结构使其具有 16 字节对齐来解决此问题:

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?有什么方法可以设置TexSize的对齐方式而无需添加其他成员来填充它?

No. uniforms require 16 byte (4 float) spacing.号制服需要 16 字节(4 个浮点数)间距。 you need to add padding.你需要添加填充。

Address Space Layout Constraints of wgsl specification: https://www.w3.org/TR/WGSL/#address-space-layout-constraints wgsl 规范的地址空间布局约束: 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. GPU 需要在 16 字节浮点数上分配 3 或 4 维矩阵算法。 This is why GPUs are heavily optimised for this type of Calculations.这就是为什么 GPU 针对此类计算进行了大量优化的原因。 And this is why The Spacing of uniforms is 4 X 16Byte Float.这就是为什么制服的间距是 4 X 16Byte Float。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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