Skip to content

Commit a5e05e4

Browse files
committed
Added VectorFromVector! helper macro
1 parent 3f64340 commit a5e05e4

File tree

7 files changed

+99
-96
lines changed

7 files changed

+99
-96
lines changed

crates/spirv-std/src/image.rs

+17-25
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::{
1818
float::Float,
1919
integer::Integer,
2020
vector::{Vector, VectorTypeRef},
21-
Sampler,
21+
Sampler, VectorFromVector,
2222
};
2323

2424
/// Re-export of primitive types to ensure the `Image` proc macro always points
@@ -124,10 +124,8 @@ impl<
124124
/// Fetch a single texel with a sampler set at compile time
125125
#[crate::macros::gpu_only]
126126
#[doc(alias = "OpImageFetch")]
127-
pub fn fetch<I, C, const N: usize>(
128-
&self,
129-
coordinate: C,
130-
) -> <<C>::VectorTypeLib as VectorTypeRef<SampledType, 4>>::Vector
127+
pub fn fetch<I, C, const N: usize>(&self, coordinate: C) -> VectorFromVector!(C, SampledType, 4)
128+
//<<C>::VectorTypeLib as VectorTypeRef<SampledType, 4>>::Vector
131129
where
132130
I: Integer,
133131
C: ImageCoordinate<I, DIM, ARRAYED> + Vector<I, N>,
@@ -168,7 +166,7 @@ impl<
168166
sampler: Sampler,
169167
coordinate: C,
170168
component: u32,
171-
) -> <C::VectorTypeLib as VectorTypeRef<SampledType, 4>>::Vector
169+
) -> VectorFromVector!(C, SampledType, 4)
172170
where
173171
Self: HasGather,
174172
F: Float,
@@ -201,7 +199,7 @@ impl<
201199
&self,
202200
sampler: Sampler,
203201
coord: C,
204-
) -> <C::VectorTypeLib as VectorTypeRef<SampledType, 4>>::Vector
202+
) -> VectorFromVector!(C, SampledType, 4)
205203
where
206204
F: Float,
207205
C: ImageCoordinate<F, DIM, ARRAYED> + Vector<F, N>,
@@ -234,7 +232,7 @@ impl<
234232
sampler: Sampler,
235233
coord: C,
236234
bias: f32,
237-
) -> <C::VectorTypeLib as VectorTypeRef<SampledType, 4>>::Vector
235+
) -> VectorFromVector!(C, SampledType, 4)
238236
where
239237
F: Float,
240238
C: ImageCoordinate<F, DIM, ARRAYED> + Vector<F, N>,
@@ -269,7 +267,7 @@ impl<
269267
sampler: Sampler,
270268
coordinate: C,
271269
lod: f32,
272-
) -> <C::VectorTypeLib as VectorTypeRef<SampledType, 4>>::Vector
270+
) -> VectorFromVector!(C, SampledType, 4)
273271
where
274272
F: Float,
275273
C: ImageCoordinate<F, DIM, ARRAYED> + Vector<F, N>,
@@ -304,7 +302,7 @@ impl<
304302
coordinate: C,
305303
gradient_dx: impl ImageCoordinate<F, DIM, { Arrayed::False as u32 }>,
306304
gradient_dy: impl ImageCoordinate<F, DIM, { Arrayed::False as u32 }>,
307-
) -> <C::VectorTypeLib as VectorTypeRef<SampledType, 4>>::Vector
305+
) -> VectorFromVector!(C, SampledType, 4)
308306
where
309307
F: Float,
310308
C: ImageCoordinate<F, DIM, ARRAYED> + Vector<F, N>,
@@ -463,7 +461,7 @@ impl<
463461
&self,
464462
sampler: Sampler,
465463
project_coordinate: C,
466-
) -> <C::VectorTypeLib as VectorTypeRef<SampledType, 4>>::Vector
464+
) -> VectorFromVector!(C, SampledType, 4)
467465
where
468466
F: Float,
469467
C: ImageCoordinate<F, DIM, { Arrayed::True as u32 }> + Vector<F, N>,
@@ -495,7 +493,7 @@ impl<
495493
sampler: Sampler,
496494
project_coordinate: C,
497495
lod: f32,
498-
) -> <C::VectorTypeLib as VectorTypeRef<SampledType, 4>>::Vector
496+
) -> VectorFromVector!(C, SampledType, 4)
499497
where
500498
F: Float,
501499
C: ImageCoordinate<F, DIM, { Arrayed::True as u32 }> + Vector<F, N>,
@@ -530,7 +528,7 @@ impl<
530528
project_coordinate: C,
531529
gradient_dx: impl ImageCoordinate<F, DIM, { Arrayed::False as u32 }>,
532530
gradient_dy: impl ImageCoordinate<F, DIM, { Arrayed::False as u32 }>,
533-
) -> <C::VectorTypeLib as VectorTypeRef<SampledType, 4>>::Vector
531+
) -> VectorFromVector!(C, SampledType, 4)
534532
where
535533
F: Float,
536534
C: ImageCoordinate<F, DIM, { Arrayed::True as u32 }> + Vector<F, N>,
@@ -677,10 +675,7 @@ impl<
677675
/// Read a texel from an image without a sampler.
678676
#[crate::macros::gpu_only]
679677
#[doc(alias = "OpImageRead")]
680-
pub fn read<I, C, const N: usize>(
681-
&self,
682-
coordinate: C,
683-
) -> <C::VectorTypeLib as VectorTypeRef<SampledType, 4>>::Vector
678+
pub fn read<I, C, const N: usize>(&self, coordinate: C) -> VectorFromVector!(C, SampledType, 4)
684679
where
685680
I: Integer,
686681
C: ImageCoordinate<I, DIM, ARRAYED> + Vector<I, N>,
@@ -737,10 +732,7 @@ impl<
737732
/// Read a texel from an image without a sampler.
738733
#[crate::macros::gpu_only]
739734
#[doc(alias = "OpImageRead")]
740-
pub fn read<I, C, const N: usize>(
741-
&self,
742-
coordinate: C,
743-
) -> <C::VectorTypeLib as VectorTypeRef<SampledType, 4>>::Vector
735+
pub fn read<I, C, const N: usize>(&self, coordinate: C) -> VectorFromVector!(C, SampledType, 4)
744736
where
745737
I: Integer,
746738
C: ImageCoordinate<I, DIM, ARRAYED> + Vector<I, N>,
@@ -809,7 +801,7 @@ impl<
809801
pub fn read_subpass<I, C, const N: usize>(
810802
&self,
811803
coordinate: C,
812-
) -> <C::VectorTypeLib as VectorTypeRef<SampledType, 4>>::Vector
804+
) -> VectorFromVector!(C, SampledType, 4)
813805
where
814806
I: Integer,
815807
C: ImageCoordinateSubpassData<I, ARRAYED> + Vector<I, N>,
@@ -872,7 +864,7 @@ impl<
872864
&self,
873865
sampler: Sampler,
874866
coord: C,
875-
) -> <C::VectorTypeLib as VectorTypeRef<SampledType, 2>>::Vector
867+
) -> VectorFromVector!(C, SampledType, 2)
876868
where
877869
Self: HasQueryLevels,
878870
C: ImageCoordinate<f32, DIM, { Arrayed::False as u32 }> + Vector<f32, N>,
@@ -1023,7 +1015,7 @@ impl<
10231015
pub unsafe fn sample<F, C, const N: usize>(
10241016
&self,
10251017
coord: C,
1026-
) -> <C::VectorTypeLib as VectorTypeRef<SampledType, 4>>::Vector
1018+
) -> VectorFromVector!(C, SampledType, 4)
10271019
where
10281020
F: Float,
10291021
C: ImageCoordinate<F, DIM, ARRAYED> + Vector<F, N>,
@@ -1052,7 +1044,7 @@ impl<
10521044
&self,
10531045
coord: C,
10541046
lod: f32,
1055-
) -> <C::VectorTypeLib as VectorTypeRef<SampledType, 4>>::Vector
1047+
) -> VectorFromVector!(C, SampledType, 4)
10561048
where
10571049
F: Float,
10581050
C: ImageCoordinate<F, DIM, ARRAYED> + Vector<F, N>,

crates/spirv-std/src/vector.rs

+11
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,22 @@ pub unsafe trait Vector<T: crate::scalar::Scalar, const N: usize>: Default {
2222
/// wants to return a `Vector<f32,3>` of the same library of implementations, it can find it the
2323
/// concrete vector type by doing:
2424
/// `<T::VectorTypeLib as VectorTypeRef<f32,3>>::Vector`
25+
/// Alternatively, using the `VectorFromVector!` macro:
26+
/// `VectorFromVector!(T, f32, 3)`
2527
pub trait VectorTypeRef<T: crate::scalar::Scalar, const N: usize> {
2628
/// Concrete type that implements the vector type with these generic parameters
2729
type Vector: Vector<T, N, VectorTypeLib = Self>;
2830
}
2931

32+
/// Helper macro that allows you to find a vector type with certain generic parameters
33+
/// from the same 'vector type library' as another vector type.
34+
#[macro_export]
35+
macro_rules! VectorFromVector {
36+
($vec: ty, $comp: ty, $dim: expr) => {
37+
<<$vec>::VectorTypeLib as VectorTypeRef<$comp, $dim>>::Vector
38+
};
39+
}
40+
3041
/// Glam vector type library. It implements all relevant VectorTypeRef<T,N>s
3142
pub struct GlamVectorTypeLib;
3243

0 commit comments

Comments
 (0)