Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pointer type cast in wgsl #7066

Open
mikialex opened this issue Feb 6, 2025 · 3 comments
Open

Pointer type cast in wgsl #7066

mikialex opened this issue Feb 6, 2025 · 3 comments
Labels
area: api Issues related to API surface feature: bindless Issues with the Bindless Native Feature type: enhancement New feature or request

Comments

@mikialex
Copy link

mikialex commented Feb 6, 2025

Related problem

Currently, the buffer binding array feature requires that every buffer has the same type. Is it possible to work around this limitation?

The possible solution

For some reason, if I want to bind different types of buffer into one buffer binding array, If we support some sort of pointer type cast in wgsl, can this be worked around like this?

use a u32 array as some untyped buffer. var<storage> buffer_array: array<array<u32>, BIND_ARRAY_SIZE>, then cast any sub-item pointer ptr<storage, array<u32>, read> to ptr<storage, array<MyType>, read>.

Considered alternatives

The alternative is just to mimic the entire shader type system based on var<storage> buffer_array: array<array<u32>, BIND_ARRAY_SIZE>, but it's pretty hard to implement as a general solution and error-prone to implement for specific cases.

Additional context

I guess the pointer cast is possible based on the spriv spec. but maybe I don't know what I'm talking about.

https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#OpConvertPtrToU
https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#OpConvertUToPtr

@cwfitzgerald cwfitzgerald added type: enhancement New feature or request area: api Issues related to API surface feature: bindless Issues with the Bindless Native Feature labels Feb 10, 2025
@cwfitzgerald
Copy link
Member

This is a really tricky one, we've been scheming in the general work on bindless (see the tracking issue for the new proposed spec). Pointer casts can't really work, but you can (on some platforms, vk and dx12) alias the same binding as multiple types. Metal gets significantly more complicated though, as there are type-based alias analysis to contend with, which makes that kind of cast undefined behavior. We definitely want this, as I agree, this isn't really tenable as is.

@mikialex
Copy link
Author

Thanks for researching. I think it's a typical issue for people who want to use bindless buffers.

If the user has the same typed array of items in multiple storage buffers, why do they need bindless buffer? It's ok just allocate them into one storage buffer and take care of offset and count. (on the other side, bindless texture is useful even the texture resource is homogenous, because impl texture atlas pool is complicated and inefficient(but still meaningful)). This is especially true for people expect using bindless buffer to workaround storage buffer binding count limits.

I assume the new proposed spec is in https://hackmd.io/@cwfitzgerald/wgpu-bindless#Shader-API , but I am still not sure how this issue will be solved in wgpu or if it is solvable at all. I have to pack all my different types of data into a single u32 array for now.

@cwfitzgerald
Copy link
Member

Yeah I'm not sure. The one thing I might recommend is packing everything into array<vec4<f32>> as that way you can get vectorized loads.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: api Issues related to API surface feature: bindless Issues with the Bindless Native Feature type: enhancement New feature or request
Projects
Status: Todo
Development

No branches or pull requests

2 participants