1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
use super::{Arrayed, Dimensionality, ImageFormat};
use crate::{integer::Integer, scalar::Scalar, vector::Vector, vector::VectorTruncateInto};

/// Marker trait for arguments that accept single scalar values or vectors
/// of scalars. Defines 2-, 3- and 4-component vector types based on the sample type.
pub trait SampleType<const FORMAT: u32, const COMPONENTS: u32>: Scalar {
    /// The default vector/scalar of ths sample type
    type SampleResult: Default;

    /// A 2-component vector of this sample type
    type Vec2: Default;

    /// A 3-component vector of this sample type
    type Vec3: Default;

    /// A 4-component vector of this sample type
    type Vec4: Default + VectorTruncateInto<Self::SampleResult>;
}

/// Helper macro to implement `SampleType` of various formats for various scalar types.
macro_rules! sample_type_impls {
    ($($fmt:ident : $n:tt*$s:ty => ($v1:ty, $v2:ty, $v3:ty, $v4:ty)),+ $(,)?) => {
        $(sample_type_impls!{@single_rule, $fmt : $n*$s => ($v1,$v2,$v3,$v4)})+
    };
    (@single_rule, $fmt:ident : n*$s:ty => ($v1:ty, $v2:ty, $v3:ty, $v4:ty)) => {
        impl SampleType<{ ImageFormat::$fmt as u32 }, 1> for $s {
            type SampleResult = $v1;
            type Vec2 = $v2;
            type Vec3 = $v3;
            type Vec4 = $v4;
        }
        impl SampleType<{ ImageFormat::$fmt as u32 }, 2> for $s {
            type SampleResult = $v2;
            type Vec2 = $v2;
            type Vec3 = $v3;
            type Vec4 = $v4;
        }
        impl SampleType<{ ImageFormat::$fmt as u32 }, 3> for $s {
            type SampleResult = $v3;
            type Vec2 = $v2;
            type Vec3 = $v3;
            type Vec4 = $v4;
        }
        impl SampleType<{ ImageFormat::$fmt as u32 }, 4> for $s {
            type SampleResult = $v4;
            type Vec2 = $v2;
            type Vec3 = $v3;
            type Vec4 = $v4;
        }
    };
    (@single_rule, $($fmt:ident : 1*$s:ty => ($v1:ty, $v2:ty, $v3:ty, $v4:ty)),+ $(,)?) => {
        $(
            impl SampleType<{ ImageFormat::$fmt as u32 }, 1> for $s {
                type SampleResult = $v1;
                type Vec2 = $v2;
                type Vec3 = $v3;
                type Vec4 = $v4;
            }
        )+
    };
    (@single_rule, $($fmt:ident : 2*$s:ty => ($v1:ty, $v2:ty, $v3:ty, $v4:ty)),+ $(,)?) => {
        $(
            impl SampleType<{ ImageFormat::$fmt as u32 }, 2> for $s {
                type SampleResult = $v2;
                type Vec2 = $v2;
                type Vec3 = $v3;
                type Vec4 = $v4;
            }
        )+
    };
    (@single_rule, $($fmt:ident : 3*$s:ty => ($v1:ty, $v2:ty, $v3:ty, $v4:ty)),+ $(,)?) => {
        $(
            impl SampleType<{ ImageFormat::$fmt as u32 }, 3> for $s {
                type SampleResult = $v3;
                type Vec2 = $v2;
                type Vec3 = $v3;
                type Vec4 = $v4;
            }
        )+
    };
    (@single_rule, $($fmt:ident : 4*$s:ty => ($v1:ty, $v2:ty, $v3:ty, $v4:ty)),+ $(,)?) => {
        $(
            impl SampleType<{ ImageFormat::$fmt as u32 }, 4> for $s {
                type SampleResult = $v4;
                type Vec2 = $v2;
                type Vec3 = $v3;
                type Vec4 = $v4;
            }
        )+
    };
}

sample_type_impls! {
    Unknown: n*i8 => (i32, glam::IVec2, glam::IVec3, glam::IVec4),
    Unknown: n*i16 => (i32, glam::IVec2, glam::IVec3, glam::IVec4),
    Unknown: n*i32 => (i32, glam::IVec2, glam::IVec3, glam::IVec4),
    Unknown: n*i64 => (i32, glam::IVec2, glam::IVec3, glam::IVec4),
    Unknown: n*u8 => (u32, glam::UVec2, glam::UVec3, glam::UVec4),
    Unknown: n*u16 => (u32, glam::UVec2, glam::UVec3, glam::UVec4),
    Unknown: n*u32 => (u32, glam::UVec2, glam::UVec3, glam::UVec4),
    Unknown: n*u64 => (u32, glam::UVec2, glam::UVec3, glam::UVec4),
    Unknown: n*f32 => (f32, glam::Vec2, glam::Vec3, glam::Vec4),
    Unknown: n*f64 => (f64, glam::DVec2, glam::DVec3, glam::DVec4),
    Rgba32f: 4*f32 => (f32, glam::Vec2, glam::Vec3, glam::Vec4),
    Rgba16f: 4*f32 => (f32, glam::Vec2, glam::Vec3, glam::Vec4),
    R32f: 1*f32 => (f32, glam::Vec2, glam::Vec3, glam::Vec4),
    Rgba8: 4*f32 => (f32, glam::Vec2, glam::Vec3, glam::Vec4),
    Rgba8Snorm: 4*f32 => (f32, glam::Vec2, glam::Vec3, glam::Vec4),
    Rg32f: 2*f32 => (f32, glam::Vec2, glam::Vec3, glam::Vec4),
    Rg16f: 2*f32 => (f32, glam::Vec2, glam::Vec3, glam::Vec4),
    R11fG11fB10f: 3*f32 => (f32, glam::Vec2, glam::Vec3, glam::Vec4),
    R16f: 1*f32 => (f32, glam::Vec2, glam::Vec3, glam::Vec4),
    Rgba16: 4*f32 => (f32, glam::Vec2, glam::Vec3, glam::Vec4),
    Rgb10A2: 4*f32 => (f32, glam::Vec2, glam::Vec3, glam::Vec4),
    Rg16: 2*f32 => (f32, glam::Vec2, glam::Vec3, glam::Vec4),
    Rg8: 2*f32 => (f32, glam::Vec2, glam::Vec3, glam::Vec4),
    R16: 1*f32 => (f32, glam::Vec2, glam::Vec3, glam::Vec4),
    R8: 1*f32 => (f32, glam::Vec2, glam::Vec3, glam::Vec4),
    Rgba16Snorm: 4*f32 => (f32, glam::Vec2, glam::Vec3, glam::Vec4),
    Rg16Snorm: 2*f32 => (f32, glam::Vec2, glam::Vec3, glam::Vec4),
    Rg8Snorm: 2*f32 => (f32, glam::Vec2, glam::Vec3, glam::Vec4),
    R16Snorm: 1*f32 => (f32, glam::Vec2, glam::Vec3, glam::Vec4),
    R8Snorm: 1*f32 => (f32, glam::Vec2, glam::Vec3, glam::Vec4),
    Rgba32i: 4*i32 => (i32, glam::IVec2, glam::IVec3, glam::IVec4),
    Rgba16i: 4*i32 => (i32, glam::IVec2, glam::IVec3, glam::IVec4),
    Rgba8i: 4*i32 => (i32, glam::IVec2, glam::IVec3, glam::IVec4),
    R32i: 1*i32 => (i32, glam::IVec2, glam::IVec3, glam::IVec4),
    Rg32i: 2*i32 => (i32, glam::IVec2, glam::IVec3, glam::IVec4),
    Rg16i: 2*i32 => (i32, glam::IVec2, glam::IVec3, glam::IVec4),
    Rg8i: 2*i32 => (i32, glam::IVec2, glam::IVec3, glam::IVec4),
    R16i: 1*i32 => (i32, glam::IVec2, glam::IVec3, glam::IVec4),
    R8i: 1*i32 => (i32, glam::IVec2, glam::IVec3, glam::IVec4),
    Rgba32ui: 4*u32 => (u32, glam::UVec2, glam::UVec3, glam::UVec4),
    Rgba16ui: 4*u32 => (u32, glam::UVec2, glam::UVec3, glam::UVec4),
    Rgba8ui: 4*u32 => (u32, glam::UVec2, glam::UVec3, glam::UVec4),
    R32ui: 1*u32 => (u32, glam::UVec2, glam::UVec3, glam::UVec4),
    Rgb10A2ui: 4*u32 => (u32, glam::UVec2, glam::UVec3, glam::UVec4),
    Rg32ui: 2*u32 => (u32, glam::UVec2, glam::UVec3, glam::UVec4),
    Rg16ui: 2*u32 => (u32, glam::UVec2, glam::UVec3, glam::UVec4),
    Rg8ui: 2*u32 => (u32, glam::UVec2, glam::UVec3, glam::UVec4),
    R16ui: 1*u32 => (u32, glam::UVec2, glam::UVec3, glam::UVec4),
    R8ui: 1*u32 => (u32, glam::UVec2, glam::UVec3, glam::UVec4),
    R64ui: 1*u64 => (u32, glam::UVec2, glam::UVec3, glam::UVec4),
    R64i: 1*i64 => (i32, glam::IVec2, glam::IVec3, glam::IVec4),
}

/// Marker trait for arguments that accept a coordinate for an [`crate::Image`].
pub trait ImageCoordinate<T, const DIM: u32, const ARRAYED: u32> {}

impl<S: Scalar> ImageCoordinate<S, { Dimensionality::OneD as u32 }, { Arrayed::False as u32 }>
    for S
{
}
impl<S: Scalar> ImageCoordinate<S, { Dimensionality::Buffer as u32 }, { Arrayed::False as u32 }>
    for S
{
}

impl<V: Vector<S, 2>, S: Scalar>
    ImageCoordinate<S, { Dimensionality::TwoD as u32 }, { Arrayed::False as u32 }> for V
{
}
impl<V: Vector<S, 2>, S: Scalar>
    ImageCoordinate<S, { Dimensionality::Rect as u32 }, { Arrayed::False as u32 }> for V
{
}
impl<V: Vector<S, 3>, S: Scalar>
    ImageCoordinate<S, { Dimensionality::Cube as u32 }, { Arrayed::False as u32 }> for V
{
}
impl<V: Vector<S, 3>, S: Scalar>
    ImageCoordinate<S, { Dimensionality::ThreeD as u32 }, { Arrayed::False as u32 }> for V
{
}

impl<V: Vector<S, 3>, S: Scalar>
    ImageCoordinate<S, { Dimensionality::TwoD as u32 }, { Arrayed::True as u32 }> for V
{
}
impl<V: Vector<S, 3>, S: Scalar>
    ImageCoordinate<S, { Dimensionality::Rect as u32 }, { Arrayed::True as u32 }> for V
{
}
impl<V: Vector<S, 4>, S: Scalar>
    ImageCoordinate<S, { Dimensionality::Cube as u32 }, { Arrayed::True as u32 }> for V
{
}
impl<V: Vector<S, 4>, S: Scalar>
    ImageCoordinate<S, { Dimensionality::ThreeD as u32 }, { Arrayed::True as u32 }> for V
{
}

/// Marker trait for arguments that are valid for a [`crate::image::Dimensionality::SubpassData`] image query.
pub trait ImageCoordinateSubpassData<T, const ARRAYED: u32> {}
impl<V: Vector<I, 2>, I: Integer> ImageCoordinateSubpassData<I, { Arrayed::False as u32 }> for V {}
impl<V: Vector<I, 3>, I: Integer> ImageCoordinateSubpassData<I, { Arrayed::True as u32 }> for V {}