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
197
198
199
200
201
202
203
204
205
206
207
use std::hash::{Hash, Hasher};

pub use crate::backend::Sampler;

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde-1", derive(serde::Serialize, serde::Deserialize))]
pub enum CompareOp {
    /// Never passes.
    Never,

    /// Passes if fragment's depth is less than stored.
    Less,

    /// Passes if fragment's depth is equal to stored.
    Equal,

    /// Passes if fragment's depth is less than or equal to stored.
    LessOrEqual,

    /// Passes if fragment's depth is greater than stored.
    Greater,

    /// Passes if fragment's depth is not equal to stored.
    NotEqual,

    /// Passes if fragment's depth is greater than or equal to stored.
    GreaterOrEqual,

    /// Always passes.
    Always,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde-1", derive(serde::Serialize, serde::Deserialize))]
pub enum Filter {
    Nearest,
    Linear,
    // Cubic,
}

impl Default for Filter {
    fn default() -> Self {
        Filter::Nearest
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde-1", derive(serde::Serialize, serde::Deserialize))]
pub enum MipmapMode {
    Nearest,
    Linear,
}

impl Default for MipmapMode {
    fn default() -> Self {
        MipmapMode::Nearest
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde-1", derive(serde::Serialize, serde::Deserialize))]
pub enum SamplerAddressMode {
    Repeat,
    MirroredRepeat,
    ClampToEdge,
    ClampToBorder,
    MirrorClampToEdge,
}

impl Default for SamplerAddressMode {
    fn default() -> Self {
        SamplerAddressMode::ClampToEdge
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde-1", derive(serde::Serialize, serde::Deserialize))]
pub enum BorderColor {
    FloatTransparentBlack,
    IntTransparentBlack,
    FloatOpaqueBlack,
    IntOpaqueBlack,
    FloatOpaqueWhite,
    IntOpaqueWhite,
}

impl Default for BorderColor {
    fn default() -> Self {
        BorderColor::FloatTransparentBlack
    }
}

#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "serde-1", derive(serde::Serialize, serde::Deserialize))]
pub struct SamplerInfo {
    #[cfg_attr(feature = "serde-1", serde(default))]
    pub mag_filter: Filter,
    #[cfg_attr(feature = "serde-1", serde(default))]
    pub min_filter: Filter,
    #[cfg_attr(feature = "serde-1", serde(default))]
    pub mipmap_mode: MipmapMode,
    #[cfg_attr(feature = "serde-1", serde(default))]
    pub address_mode_u: SamplerAddressMode,
    #[cfg_attr(feature = "serde-1", serde(default))]
    pub address_mode_v: SamplerAddressMode,
    #[cfg_attr(feature = "serde-1", serde(default))]
    pub address_mode_w: SamplerAddressMode,
    #[cfg_attr(feature = "serde-1", serde(default))]
    pub mip_lod_bias: f32,
    #[cfg_attr(feature = "serde-1", serde(default))]
    pub max_anisotropy: Option<f32>,
    #[cfg_attr(feature = "serde-1", serde(default))]
    pub compare_op: Option<CompareOp>,
    #[cfg_attr(feature = "serde-1", serde(default))]
    pub min_lod: f32,
    #[cfg_attr(feature = "serde-1", serde(default))]
    pub max_lod: f32,
    #[cfg_attr(feature = "serde-1", serde(default))]
    pub border_color: BorderColor,
    #[cfg_attr(feature = "serde-1", serde(default))]
    pub unnormalized_coordinates: bool,
}

impl PartialEq for SamplerInfo {
    fn eq(&self, other: &Self) -> bool {
        self.mag_filter == other.mag_filter
            && self.min_filter == other.min_filter
            && self.mipmap_mode == other.mipmap_mode
            && self.address_mode_u == other.address_mode_u
            && self.address_mode_v == other.address_mode_v
            && self.address_mode_w == other.address_mode_w
            && f32::to_bits(self.mip_lod_bias) == f32::to_bits(other.mip_lod_bias)
            && self.max_anisotropy.map(f32::to_bits) == other.max_anisotropy.map(f32::to_bits)
            && self.compare_op == other.compare_op
            && f32::to_bits(self.min_lod) == f32::to_bits(other.min_lod)
            && f32::to_bits(self.max_lod) == f32::to_bits(other.max_lod)
            && self.border_color == other.border_color
            && self.unnormalized_coordinates == other.unnormalized_coordinates
    }
}

impl Eq for SamplerInfo {}

impl Hash for SamplerInfo {
    fn hash<H>(&self, hasher: &mut H)
    where
        H: Hasher,
    {
        Hash::hash(&self.mag_filter, hasher);
        Hash::hash(&self.min_filter, hasher);
        Hash::hash(&self.mipmap_mode, hasher);
        Hash::hash(&self.address_mode_u, hasher);
        Hash::hash(&self.address_mode_v, hasher);
        Hash::hash(&self.address_mode_w, hasher);
        Hash::hash(&f32::to_bits(self.mip_lod_bias), hasher);
        Hash::hash(&self.max_anisotropy.map(f32::to_bits), hasher);
        Hash::hash(&self.compare_op, hasher);
        Hash::hash(&f32::to_bits(self.min_lod), hasher);
        Hash::hash(&f32::to_bits(self.max_lod), hasher);
        Hash::hash(&self.border_color, hasher);
        Hash::hash(&self.unnormalized_coordinates, hasher);
    }
}

impl SamplerInfo {
    pub const fn new() -> Self {
        SamplerInfo {
            mag_filter: Filter::Nearest,
            min_filter: Filter::Nearest,
            mipmap_mode: MipmapMode::Nearest,
            address_mode_u: SamplerAddressMode::ClampToEdge,
            address_mode_v: SamplerAddressMode::ClampToEdge,
            address_mode_w: SamplerAddressMode::ClampToEdge,
            mip_lod_bias: 0.0,
            max_anisotropy: None,
            compare_op: None,
            min_lod: 0.0,
            max_lod: 0.0,
            border_color: BorderColor::FloatTransparentBlack,
            unnormalized_coordinates: false,
        }
    }

    pub const fn linear() -> Self {
        SamplerInfo {
            mag_filter: Filter::Linear,
            min_filter: Filter::Linear,
            mipmap_mode: MipmapMode::Linear,
            address_mode_u: SamplerAddressMode::ClampToEdge,
            address_mode_v: SamplerAddressMode::ClampToEdge,
            address_mode_w: SamplerAddressMode::ClampToEdge,
            mip_lod_bias: 0.0,
            max_anisotropy: None,
            compare_op: None,
            min_lod: 0.0,
            max_lod: 0.0,
            border_color: BorderColor::FloatTransparentBlack,
            unnormalized_coordinates: false,
        }
    }
}

impl Default for SamplerInfo {
    fn default() -> Self {
        SamplerInfo::new()
    }
}