shader-slang / slang

Making it easier to work with shaders
http://shader-slang.com
MIT License
2.17k stars 187 forks source link

WGSL: WGSL doesn't support arrays in uniform memory, with element strides less than 16 bytes #4985

Closed aleino-nv closed 2 days ago

aleino-nv commented 2 months ago

The tests/compute/buffer-layout.slang sample contains a struct with an element of type float2 c[2];, and the struct is used in a constant buffer.

The corresponding WGSL member declaration is array<vec2<f32>, i32(2)>, so the array element stride is 8. WGSL requires that array elements have alignment 16, but it doesn't silently change the layout of the array. Instead it prints an error message suggesting to use vec4 in the array type, instead.

Error while parsing WGSL: :14:22 error: 'uniform' storage requires that array elements are aligned to 16 
bytes, but array element of type 'vec2<f32>' has a stride of 8 bytes. Consider using a vec4 instead.
    @align(16) c_0 : array<vec2<f32>, i32(2)>,
                     ^^^^^^^^^^^^^^^^^^^^^^^^

We should work around this. If we instead emit array<S, i32(2)> where S is a struct containing the vec2<f32> (or whatever other type with stride less than 16), then it should work because the size of S will get aligned up to 16.

Affected tests:

aleino-nv commented 1 month ago

@bmillsNV Can we find someone to work on this for Sprint 13?

aleino-nv commented 1 month ago

@bmillsNV Can we find someone to work on this for Sprint 13?

As discussed during yesterday's sync meeting, this is not needed for WGSL milestone 1.