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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
//!
//! Different types of textures used by the GPU to read from and write to.
//!
mod texture2d;
#[doc(inline)]
pub use texture2d::*;

mod texture_cube_map;
#[doc(inline)]
pub use texture_cube_map::*;

mod depth_texture2d;
#[doc(inline)]
pub use depth_texture2d::*;

mod texture2d_array;
#[doc(inline)]
pub use texture2d_array::*;

mod texture2d_multisample;
#[doc(inline)]
pub(in crate::core) use texture2d_multisample::*;

mod texture3d;
#[doc(inline)]
pub use texture3d::*;

mod depth_texture2d_array;
#[doc(inline)]
pub use depth_texture2d_array::*;

mod depth_texture_cube_map;
#[doc(inline)]
pub use depth_texture_cube_map::*;

mod depth_texture2d_multisample;
#[doc(inline)]
pub(in crate::core) use depth_texture2d_multisample::*;

use data_type::*;
pub use three_d_asset::texture::{
    Interpolation, Texture2D as CpuTexture, Texture3D as CpuTexture3D, TextureData, Wrapping,
};

/// The basic data type used for each channel of each pixel in a texture.
pub trait TextureDataType: DataType {}
impl TextureDataType for u8 {}
impl TextureDataType for f16 {}
impl TextureDataType for f32 {}

impl<T: TextureDataType + PrimitiveDataType> TextureDataType for Vector2<T> {}
impl<T: TextureDataType + PrimitiveDataType> TextureDataType for Vector3<T> {}
impl<T: TextureDataType + PrimitiveDataType> TextureDataType for Vector4<T> {}
impl<T: TextureDataType + PrimitiveDataType> TextureDataType for [T; 2] {}
impl<T: TextureDataType + PrimitiveDataType> TextureDataType for [T; 3] {}
impl<T: TextureDataType + PrimitiveDataType> TextureDataType for [T; 4] {}

impl TextureDataType for Quat {}

impl<T: TextureDataType + ?Sized> TextureDataType for &T {}

/// The basic data type used for each pixel in a depth texture.
pub trait DepthTextureDataType: DepthDataType {}

/// 24 bit float which can be used as [DepthTextureDataType].
#[allow(non_camel_case_types)]
#[derive(Clone, Copy, Default, Debug)]
pub struct f24 {}

impl DepthTextureDataType for f16 {}
impl DepthTextureDataType for f24 {}
impl DepthTextureDataType for f32 {}

///
/// A reference to some type of texture containing colors.
///
#[derive(Clone, Copy)]
#[allow(missing_docs)]
pub enum ColorTexture<'a> {
    /// A single 2D texture.
    Single(&'a Texture2D),
    /// An array of 2D textures and a set of indices into the array.
    Array {
        texture: &'a Texture2DArray,
        layers: &'a [u32],
    },
    /// A cube map texture and a set of [CubeMapSide]s indicating the sides to use.
    CubeMap {
        texture: &'a TextureCubeMap,
        sides: &'a [CubeMapSide],
    },
}

impl ColorTexture<'_> {
    ///
    /// Returns the width of the color texture in texels.
    ///
    pub fn width(&self) -> u32 {
        match self {
            ColorTexture::Single(texture) => texture.width(),
            ColorTexture::Array { texture, .. } => texture.width(),
            ColorTexture::CubeMap { texture, .. } => texture.width(),
        }
    }

    ///
    /// Returns the height of the color texture in texels.
    ///
    pub fn height(&self) -> u32 {
        match self {
            ColorTexture::Single(texture) => texture.height(),
            ColorTexture::Array { texture, .. } => texture.height(),
            ColorTexture::CubeMap { texture, .. } => texture.height(),
        }
    }

    ///
    /// Returns the fragment shader source for using this texture in a shader.
    ///
    pub fn fragment_shader_source(&self) -> String {
        match self {
            Self::Single(_) => "
                uniform sampler2D colorMap;
                vec4 sample_color(vec2 uv)
                {
                    return texture(colorMap, uv);
                }"
            .to_owned(),
            Self::Array { .. } => "
                uniform sampler2DArray colorMap;
                uniform int colorLayers[4];
                vec4 sample_color(vec2 uv)
                {
                    return texture(colorMap, vec3(uv, colorLayers[0]));
                }
                vec4 sample_layer(vec2 uv, int index)
                {
                    return texture(colorMap, vec3(uv, colorLayers[index]));
                }"
            .to_owned(),
            Self::CubeMap { .. } => todo!(),
        }
    }

    ///
    /// Returns a unique ID for each variation of the shader source returned from [ColorTexture::fragment_shader_source].
    ///
    pub fn id(&self) -> u16 {
        match self {
            Self::Single { .. } => 1u16 << 3,
            Self::Array { .. } => 10u16 << 3,
            Self::CubeMap { .. } => {
                todo!()
            }
        }
    }

    ///
    /// Sends the uniform data needed for this texture to the fragment shader.
    ///
    pub fn use_uniforms(&self, program: &Program) {
        match self {
            Self::Single(texture) => program.use_texture("colorMap", texture),
            Self::Array { texture, layers } => {
                let mut la: [i32; 4] = [0; 4];
                layers
                    .iter()
                    .enumerate()
                    .for_each(|(i, l)| la[i] = *l as i32);
                program.use_uniform_array("colorLayers", &la);
                program.use_texture_array("colorMap", texture);
            }
            Self::CubeMap { .. } => todo!(),
        }
    }
}

///
/// A reference to some type of texture containing depths.
///
#[derive(Clone, Copy)]
#[allow(missing_docs)]
pub enum DepthTexture<'a> {
    /// A single 2D texture.
    Single(&'a DepthTexture2D),
    /// An array of 2D textures and an index into the array.
    Array {
        texture: &'a DepthTexture2DArray,
        layer: u32,
    },
    /// A cube map texture and a [CubeMapSide] indicating the side to use.
    CubeMap {
        texture: &'a DepthTextureCubeMap,
        side: CubeMapSide,
    },
}

impl DepthTexture<'_> {
    ///
    /// Returns the width of the depth texture in texels.
    ///
    pub fn width(&self) -> u32 {
        match self {
            DepthTexture::Single(texture) => texture.width(),
            DepthTexture::Array { texture, .. } => texture.width(),
            DepthTexture::CubeMap { texture, .. } => texture.width(),
        }
    }

    ///
    /// Returns the height of the depth texture in texels.
    ///
    pub fn height(&self) -> u32 {
        match self {
            DepthTexture::Single(texture) => texture.height(),
            DepthTexture::Array { texture, .. } => texture.height(),
            DepthTexture::CubeMap { texture, .. } => texture.height(),
        }
    }
    ///
    /// Returns the fragment shader source for using this texture in a shader.
    ///
    pub fn fragment_shader_source(&self) -> String {
        match self {
            Self::Single { .. } => "
                uniform sampler2D depthMap;
                float sample_depth(vec2 uv)
                {
                    return texture(depthMap, uv).x;
                }"
            .to_owned(),
            Self::Array { .. } => "
                uniform sampler2DArray depthMap;
                uniform int depthLayer;
                float sample_depth(vec2 uv)
                {
                    return texture(depthMap, vec3(uv, depthLayer)).x;
                }"
            .to_owned(),
            Self::CubeMap { .. } => {
                todo!()
            }
        }
    }

    ///
    /// Returns a unique ID for each variation of the shader source returned from [DepthTexture::fragment_shader_source].
    ///
    pub fn id(&self) -> u16 {
        match self {
            Self::Single { .. } => 1u16,
            Self::Array { .. } => 10u16,
            Self::CubeMap { .. } => {
                todo!()
            }
        }
    }

    ///
    /// Sends the uniform data needed for this texture to the fragment shader.
    ///
    pub fn use_uniforms(&self, program: &Program) {
        match self {
            Self::Single(texture) => program.use_depth_texture("depthMap", texture),
            Self::Array { texture, layer } => {
                program.use_uniform("depthLayer", layer);
                program.use_depth_texture_array("depthMap", texture);
            }
            Self::CubeMap { .. } => todo!(),
        }
    }
}

use crate::core::*;

// COMMON TEXTURE FUNCTIONS

fn generate(context: &Context) -> crate::context::Texture {
    unsafe { context.create_texture().expect("Failed creating texture") }
}

fn set_parameters(
    context: &Context,
    target: u32,
    min_filter: Interpolation,
    mag_filter: Interpolation,
    mip_map_filter: Option<Interpolation>,
    wrap_s: Wrapping,
    wrap_t: Wrapping,
    wrap_r: Option<Wrapping>,
) {
    unsafe {
        match mip_map_filter {
            None => context.tex_parameter_i32(
                target,
                crate::context::TEXTURE_MIN_FILTER,
                interpolation_from(min_filter),
            ),
            Some(Interpolation::Nearest) => {
                if min_filter == Interpolation::Nearest {
                    context.tex_parameter_i32(
                        target,
                        crate::context::TEXTURE_MIN_FILTER,
                        crate::context::NEAREST_MIPMAP_NEAREST as i32,
                    );
                } else {
                    context.tex_parameter_i32(
                        target,
                        crate::context::TEXTURE_MIN_FILTER,
                        crate::context::LINEAR_MIPMAP_NEAREST as i32,
                    )
                }
            }
            Some(Interpolation::Linear) => {
                if min_filter == Interpolation::Nearest {
                    context.tex_parameter_i32(
                        target,
                        crate::context::TEXTURE_MIN_FILTER,
                        crate::context::NEAREST_MIPMAP_LINEAR as i32,
                    );
                } else {
                    context.tex_parameter_i32(
                        target,
                        crate::context::TEXTURE_MIN_FILTER,
                        crate::context::LINEAR_MIPMAP_LINEAR as i32,
                    )
                }
            }
            _ => panic!("Can only sample textures using 'NEAREST' or 'LINEAR' interpolation"),
        }
        context.tex_parameter_i32(
            target,
            crate::context::TEXTURE_MAG_FILTER,
            interpolation_from(mag_filter),
        );
        context.tex_parameter_i32(
            target,
            crate::context::TEXTURE_WRAP_S,
            wrapping_from(wrap_s),
        );
        context.tex_parameter_i32(
            target,
            crate::context::TEXTURE_WRAP_T,
            wrapping_from(wrap_t),
        );
        if let Some(r) = wrap_r {
            context.tex_parameter_i32(target, crate::context::TEXTURE_WRAP_R, wrapping_from(r));
        }
    }
}

fn calculate_number_of_mip_maps(
    mip_map_filter: Option<Interpolation>,
    width: u32,
    height: u32,
    depth: Option<u32>,
) -> u32 {
    if mip_map_filter.is_some()
        && width == height
        && depth.map(|d| d == width).unwrap_or(true)
        && width.is_power_of_two()
    {
        (width as f64).log2() as u32 + 1
    } else {
        1
    }
}

fn wrapping_from(wrapping: Wrapping) -> i32 {
    (match wrapping {
        Wrapping::Repeat => crate::context::REPEAT,
        Wrapping::MirroredRepeat => crate::context::MIRRORED_REPEAT,
        Wrapping::ClampToEdge => crate::context::CLAMP_TO_EDGE,
    }) as i32
}

fn interpolation_from(interpolation: Interpolation) -> i32 {
    (match interpolation {
        Interpolation::Nearest => crate::context::NEAREST,
        Interpolation::Linear => crate::context::LINEAR,
        _ => panic!("Can only sample textures using 'NEAREST' or 'LINEAR' interpolation"),
    }) as i32
}

fn check_data_length<T: TextureDataType>(
    width: u32,
    height: u32,
    depth: u32,
    data_byte_size: usize,
    data_len: usize,
) {
    let expected_bytes = width as usize * height as usize * depth as usize * data_byte_size;
    let actual_bytes = data_len * std::mem::size_of::<T>();
    if expected_bytes != actual_bytes {
        panic!(
            "invalid size of texture data (expected {} bytes but got {} bytes)",
            expected_bytes, actual_bytes
        )
    }
}

fn ru8_data(t: &CpuTexture) -> &[u8] {
    if let TextureData::RU8(data) = &t.data {
        data
    } else {
        panic!("all of the images used for cube map sides must have the same texture data type")
    }
}

fn rgu8_data(t: &CpuTexture) -> &[[u8; 2]] {
    if let TextureData::RgU8(data) = &t.data {
        data
    } else {
        panic!("all of the images used for cube map sides must have the same texture data type")
    }
}

fn rgbu8_data(t: &CpuTexture) -> &[[u8; 3]] {
    if let TextureData::RgbU8(data) = &t.data {
        data
    } else {
        panic!("all of the images used for cube map sides must have the same texture data type")
    }
}

fn rgbau8_data(t: &CpuTexture) -> &[[u8; 4]] {
    if let TextureData::RgbaU8(data) = &t.data {
        data
    } else {
        panic!("all of the images used for cube map sides must have the same texture data type")
    }
}

fn rf16_data(t: &CpuTexture) -> &[f16] {
    if let TextureData::RF16(data) = &t.data {
        data
    } else {
        panic!("all of the images used for cube map sides must have the same texture data type")
    }
}

fn rgf16_data(t: &CpuTexture) -> &[[f16; 2]] {
    if let TextureData::RgF16(data) = &t.data {
        data
    } else {
        panic!("all of the images used for cube map sides must have the same texture data type")
    }
}

fn rgbf16_data(t: &CpuTexture) -> &[[f16; 3]] {
    if let TextureData::RgbF16(data) = &t.data {
        data
    } else {
        panic!("all of the images used for cube map sides must have the same texture data type")
    }
}

fn rgbaf16_data(t: &CpuTexture) -> &[[f16; 4]] {
    if let TextureData::RgbaF16(data) = &t.data {
        data
    } else {
        panic!("all of the images used for cube map sides must have the same texture data type")
    }
}

fn rf32_data(t: &CpuTexture) -> &[f32] {
    if let TextureData::RF32(data) = &t.data {
        data
    } else {
        panic!("all of the images used for cube map sides must have the same texture data type")
    }
}

fn rgf32_data(t: &CpuTexture) -> &[[f32; 2]] {
    if let TextureData::RgF32(data) = &t.data {
        data
    } else {
        panic!("all of the images used for cube map sides must have the same texture data type")
    }
}

fn rgbf32_data(t: &CpuTexture) -> &[[f32; 3]] {
    if let TextureData::RgbF32(data) = &t.data {
        data
    } else {
        panic!("all of the images used for cube map sides must have the same texture data type")
    }
}

fn rgbaf32_data(t: &CpuTexture) -> &[[f32; 4]] {
    if let TextureData::RgbaF32(data) = &t.data {
        data
    } else {
        panic!("all of the images used for cube map sides must have the same texture data type")
    }
}