@sizeOf depending on optimize mode + how to create storrage buffers with arrays

First - is it normal that size and align of a type depends on optimize mode?
zig 0.15.2.
@sizeOf(@Vector(2, f32)) and @alignOf(@Vector(2, f32)) are 16 in debug mode, but 8 in release mode.
It kinda messes with my shaders in sdl 3 - i’m copying array of @Vector(2, f32) to gpu buffer, but in debug mode I have to switch type of field in buffer (in the shader) from vec2 to vec4. ONLY in debug mode.
Both in glsl and zig shaders (I thought at least size would be the same between normal zig code and zig shader code if I pass optimize mode to createModule for shader, but they not).


Second, while we on the topic of shaders. How do I create array with unknown size for graphics storage buffer in zig?

layout(set = 0, binding = 0) buffer uvs_buffer {
    vec2 uvs[];
};

In zig I am doing (equivalent of):

pub const uvs = @extern(*addrspace(.storage_buffer) struct{ value:[1024]@Vector(2, f32) }, .{.name="uvs"});

Which works, but:

  • I simply don’t know how large buffer might be in shader, and don’t want to depend on 1024.
  • For some reason the size of this array matters. Setting size of an array to 4 * 1024 can cause application to hang before even creating a window (and the worst part is - if I ctrl + c out of it - process is still running in the background, consuming 0.4-1GiB of memory). Setting it to 2 * 1024 causes vkCreateGraphicsPipelines Unhandled VkResult. And it doesn’t work with this particular buffer, but with other one (the only difference is - it’s on layout 2 and the type is @Vector(3, f32)).

I tried creating just array pointers (*addrspace(.storage_buffer) [1024]@Vector(2, f32)) - it doesn’t render anything, like array is undefined.

And I can’t even atemt to create many-pointer ([*]addrspace(.storage_buffer) @Vector(2, f32)) because I can’t do pointer arithmetic on spirv target.

What @Vector is, is entirely left up to the backend, they are intended for simd/avx/*what ever the equivalent term is on gpu*.

Instead, use arrays and extern structs to control the layout of your data.

I am not familiar enough with spriv/gpu programming to help with your other question, I would have assumed you could just use a slice.

1 Like

IDK Spir V, but with Cuda this is how you use the shared memory: