Repository navigation
Allow deriving VectorOrScalar #410
Description
Activity
BTW, would it be difficult to implement this trait on things like
[u32; 2]or more generally<const N: usize> [T; N] where T: Scalar?
It'd simplify some of the code I'm writing a lot.[u32; 2]is anOpTypeArray %u32 2, not anOpTypeVector %u32 2, so it wouldn't work unless we change arrays to vectors for small array sizes.I've been thinking about maybe a trait + derive macro specifically for subgroup intrinsics that would allow decomposing arbitrary types into the plain primitives and then apply the operation for each of those. That should be relatively easy to implement, next to what #411 offers. That would also solve the array case, since it's just a bunch of primitives.
@nazar-pc Made you #441 with a new
trait ScalarOrVectorComposite:rust-gpu/tests/compiletests/ui/arch/subgroup/subgroup_composite.rs
Lines 10 to 46 in ce822f1
#[derive(Copy, Clone, ScalarOrVectorComposite)] pub struct MyStruct { a: f32, b: UVec3, c: Nested, d: Zst, } #[derive(Copy, Clone, ScalarOrVectorComposite)] pub struct Nested(i32); #[derive(Copy, Clone, ScalarOrVectorComposite)] pub struct Zst; #[spirv(compute(threads(32)))] pub fn main( #[spirv(local_invocation_index)] inv_id: UVec3, #[spirv(descriptor_set = 0, binding = 0, storage_buffer)] output: &mut UVec3, ) { unsafe { let my_struct = MyStruct { a: 1., b: inv_id, c: Nested(-42), d: Zst, }; let mut out = UVec3::ZERO; out += subgroup_broadcast(my_struct, 19).b; out += subgroup_broadcast_first(my_struct).b; out += subgroup_shuffle(my_struct, 2).b; out += subgroup_shuffle_xor(my_struct, 4).b; out += subgroup_shuffle_up(my_struct, 5).b; out += subgroup_shuffle_down(my_struct, 7).b; *output = out; } }
The trait is unsafe and currently says:
Fair enough, but not particularly useful for someone who would like to implement it.
It'd be great if there was a way to derive its implementation with a derive macro. Very helpful for new types, structs with two numeric fields, etc. Without this capability things like
subgroup_shuffle()currently require substantial boilerplate.