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
//!  Image storage and views.

use crate::__gl;
use crate::__gl::types::{GLenum, GLuint};

use std::ops::Range;

use crate::debug::{Object, ObjectType};
use crate::device::Device;
use crate::error::Result;
use crate::format::Format;
use crate::Extent;

/// Image resource handle.
///
/// Images ares multidimensional, formatted data arrays.
/// Images are, together with buffers, resources and are typed representations
/// of a memory slice.
///
/// Images are often also called textures. We only denote with an image
/// the actual **storage** of the data, meaning the memory with the
/// associated layout metadata.
///
/// The API only uses images directly when the function call
/// affects the underlying memory (e.g copy operations).
#[derive(Clone, Copy)]
pub struct Image {
    pub(crate) raw: GLuint,
    pub(crate) target: GLenum,
}

impl Object for Image {
    const TYPE: ObjectType = ObjectType::Image;
    fn handle(&self) -> GLuint {
        self.raw
    }
}

/// Image dimensionality type.
///
/// Layer, as in arrays or cube maps, don't affect the dimensionality type.
#[derive(Clone, Copy)]
pub enum ImageType {
    // One dimensional image.
    D1 {
        // Width.
        width: u32,

        // Number of layers.
        //
        // `1` for non-array textures.
        layers: u32,
    },
    // Two dimensional image.
    D2 {
        // Width.
        width: u32,

        // Height.
        height: u32,

        // Number of layers.
        //
        // `1` for non-array textures.
        layers: u32,

        samples: u32,
    },
    // Three dimensional image.
    D3 {
        // Width.
        width: u32,

        // Height.
        height: u32,

        // Depth.
        depth: u32,
    },
}

impl ImageType {
    /// Return the number of texels in the top layer of the image.
    pub fn num_texels(&self) -> usize {
        match *self {
            ImageType::D1 { width, .. } => width as usize,
            ImageType::D2 { width, height, .. } => width as usize * height as usize,
            ImageType::D3 {
                width,
                height,
                depth,
                ..
            } => width as usize * height as usize * depth as usize,
        }
    }

    /// Return the width of the image.
    pub fn width(&self) -> u32 {
        match *self {
            ImageType::D1 { width, .. }
            | ImageType::D2 { width, .. }
            | ImageType::D3 { width, .. } => width,
        }
    }

    /// Return the height of the image.
    pub fn height(&self) -> u32 {
        match *self {
            ImageType::D1 { .. } => 1,
            ImageType::D2 { height, .. } | ImageType::D3 { height, .. } => height,
        }
    }

    /// Return the height of the image.
    pub fn depth(&self) -> u32 {
        match *self {
            ImageType::D1 { .. } | ImageType::D2 { .. } => 1,
            ImageType::D3 { depth, .. } => depth,
        }
    }

    /// Full extent of the image dimensions for a single layer.
    pub fn full_extent(&self) -> Extent {
        Extent {
            width: self.width(),
            height: self.height(),
            depth: self.depth(),
        }
    }

    /// Return the number of samples in a texel of the image.
    pub fn samples(&self) -> u32 {
        match *self {
            ImageType::D1 { .. } | ImageType::D3 { .. } => 1,
            ImageType::D2 { samples, .. } => samples,
        }
    }

    /// Return the number of layers in the texutre.
    pub fn layers(&self) -> u32 {
        match *self {
            ImageType::D1 { layers, .. } | ImageType::D2 { layers, .. } => layers,
            ImageType::D3 { .. } => 1,
        }
    }

    fn view_ty(&self) -> ImageViewType {
        match *self {
            ImageType::D1 { layers: 1, .. } => ImageViewType::D1,
            ImageType::D1 { .. } => ImageViewType::D1Array,
            ImageType::D2 { layers: 1, .. } => ImageViewType::D2,
            ImageType::D2 { .. } => ImageViewType::D2Array,
            ImageType::D3 { .. } => ImageViewType::D3,
        }
    }
}

/// Image view handle.
///
/// Image Views denote subranges of an image storage. Pipelines will
/// only access image data via views. Views alias the memory of the associated
/// image.
#[repr(transparent)]
#[derive(Copy, Clone)]
pub struct ImageView(pub(crate) GLuint);

impl Object for ImageView {
    const TYPE: ObjectType = ObjectType::Image; // internally it's an image
    fn handle(&self) -> GLuint {
        self.0
    }
}

/// Image View type.
///
/// An `ImageViewType` maps roughly to OpenGL texture targets.
#[derive(Debug, Clone, Copy)]
pub enum ImageViewType {
    D1,
    D2,
    D3,
    Cube,
    D1Array,
    D2Array,
    CubeArray,
}

/// Subresource of an image.
#[derive(Debug, Clone)]
pub struct SubresourceRange {
    /// Range of mip levels.
    pub levels: Range<u32>,
    /// Range of array layers.
    pub layers: Range<u32>,
}

#[derive(Debug, Clone)]
pub struct SubresourceLayers {
    /// Mipmap level.
    pub level: u32,
    /// Range of array layers.
    pub layers: Range<u32>,
}

impl Device {
    ///
    pub unsafe fn create_image(&self, ty: ImageType, format: Format, levels: u32) -> Result<Image> {
        let target = match ty {
            ImageType::D1 { layers: 1, .. } => __gl::TEXTURE_1D,
            ImageType::D1 { .. } => __gl::TEXTURE_1D_ARRAY,
            ImageType::D2 {
                layers: 1,
                samples: 1,
                ..
            } => __gl::TEXTURE_2D,
            ImageType::D2 { samples: 1, .. } => __gl::TEXTURE_2D_ARRAY,
            ImageType::D2 { layers: 1, .. } => __gl::TEXTURE_2D_MULTISAMPLE,
            ImageType::D2 { .. } => __gl::TEXTURE_2D_MULTISAMPLE_ARRAY,
            ImageType::D3 { .. } => __gl::TEXTURE_3D,
        };

        let mut image = 0;
        self.0.CreateTextures(target, 1, &mut image);
        self.get_error()?;

        match ty {
            ImageType::D1 { width, layers: 1 } => {
                self.0
                    .TextureStorage1D(image, levels as _, format as _, width as _);
            }
            ImageType::D1 {
                width,
                layers: height,
            }
            | ImageType::D2 {
                width,
                height,
                layers: 1,
                samples: 1,
            } => {
                self.0
                    .TextureStorage2D(image, levels as _, format as _, width as _, height as _);
            }
            ImageType::D2 {
                width,
                height,
                layers: depth,
                samples: 1,
            }
            | ImageType::D3 {
                width,
                height,
                depth,
            } => {
                self.0.TextureStorage3D(
                    image,
                    levels as _,
                    format as _,
                    width as _,
                    height as _,
                    depth as _,
                );
            }
            _ => unimplemented!(),
        }
        self.get_error()?;

        Ok(Image { raw: image, target })
    }

    /// Delete an images.
    pub unsafe fn delete_image(&self, image: Image) {
        self.delete_images(&[image]);
    }

    /// Delete multiple images.
    pub unsafe fn delete_images(&self, images: &[Image]) {
        let images = images.iter().map(|i| i.raw).collect::<Vec<_>>();

        self.0
            .DeleteTextures(images.len() as _, images.as_ptr() as *const _);
    }

    /// Create an image view from an image.
    pub unsafe fn create_image_view(
        &self,
        image: Image,
        ty: ImageViewType,
        format: Format,
        range: SubresourceRange,
    ) -> Result<ImageView> {
        let target = match ty {
            ImageViewType::D1 => __gl::TEXTURE_1D,
            ImageViewType::D2 if image.target == __gl::TEXTURE_2D_MULTISAMPLE => {
                __gl::TEXTURE_2D_MULTISAMPLE
            }
            ImageViewType::D2 => __gl::TEXTURE_2D,
            ImageViewType::D3 => __gl::TEXTURE_3D,
            ImageViewType::Cube => __gl::TEXTURE_CUBE_MAP,
            ImageViewType::D1Array => __gl::TEXTURE_1D_ARRAY,
            ImageViewType::D2Array if image.target == __gl::TEXTURE_2D_MULTISAMPLE_ARRAY => {
                __gl::TEXTURE_2D_MULTISAMPLE_ARRAY
            }
            ImageViewType::D2Array => __gl::TEXTURE_2D_ARRAY,
            ImageViewType::CubeArray => __gl::TEXTURE_CUBE_MAP_ARRAY,
        };
        let mut view = 0;
        self.0.GenTextures(1, &mut view);
        self.get_error()?;

        self.0.TextureView(
            view,
            target,
            image.raw,
            format as _,
            range.levels.start,
            range.levels.end - range.levels.start,
            range.layers.start,
            range.layers.end - range.layers.start,
        );
        self.get_error()?;

        Ok(ImageView(view))
    }

    /// Create an image and an associated view.
    ///
    /// The image view type is derived from the `ImageType`.
    /// It creates either non-arrayed or arrayed view types.
    pub unsafe fn create_image_and_view(
        &self,
        ty: ImageType,
        format: Format,
        levels: u32,
    ) -> Result<(Image, ImageView)> {
        let image = self.create_image(ty, format, levels)?;
        let view_ty = ty.view_ty();
        let image_view = self.create_image_view(
            image,
            view_ty,
            format,
            SubresourceRange {
                levels: 0..levels,
                layers: 0..ty.layers(),
            },
        )?;

        Ok((image, image_view))
    }

    /// Delete an image views.
    pub unsafe fn delete_image_view(&self, view: ImageView) {
        self.delete_image_views(&[view]);
    }

    /// Delete multipe image views.
    pub unsafe fn delete_image_views(&self, views: &[ImageView]) {
        self.0.DeleteTextures(
            views.len() as _,
            views.as_ptr() as *const _, // newtype
        );
    }

    /// Bind image views to texture units.
    pub unsafe fn bind_image_views(&self, first: u32, views: &[ImageView]) {
        let views = views.iter().map(|view| view.0).collect::<Vec<_>>();
        self.0.BindTextures(first, views.len() as _, views.as_ptr());
    }

    /// Bind image views to storage image units.
    pub unsafe fn bind_storage_image_views(&self, first: u32, views: &[ImageView]) {
        let views = views.iter().map(|view| view.0).collect::<Vec<_>>();
        self.0
            .BindImageTextures(first, views.len() as _, views.as_ptr());
    }

    /// Generate mipmaps.
    ///
    /// This generates the remaining mipmap levels using the base layer
    /// via downscaling. The number of levels are determined on resource
    /// creation.
    ///
    /// The downscaling filter is implementation dependent!
    pub unsafe fn generate_mipmaps(&self, image: Image) {
        self.0.GenerateTextureMipmap(image.raw);
    }
}