mirror_graphics/texture/
rgba.rs

1use super::Texture2DSample;
2
3use mirror_common::Size;
4use wgpu::{Device, Texture, TextureAspect, TextureFormat};
5
6/// RGBA stands for red green blue alpha. While it is sometimes described as a
7/// color space, it is actually a three-channel RGB color model supplemented
8/// with a fourth alpha channel. Alpha indicates how opaque each pixel is and
9/// allows an image to be combined over others using alpha compositing, with
10/// transparent areas and anti-aliasing of the edges of opaque regions. Each
11/// pixel is a 4D vector.
12///
13/// The term does not define what RGB color space is being used. It also does
14/// not state whether or not the colors are premultiplied by the alpha value,
15/// and if they are it does not state what color space that premultiplication
16/// was done in. This means more information than just "RGBA" is needed to
17/// determine how to handle an image.
18///
19/// In some contexts the abbreviation "RGBA" means a specific memory layout
20/// (called RGBA8888 below), with other terms such as "BGRA" used for
21/// alternatives. In other contexts "RGBA" means any layout.
22pub struct Rgba(Texture);
23
24impl Rgba {
25    pub(crate) fn new(device: &Device, size: Size) -> Self {
26        Self(Self::create(device, size).next().unwrap())
27    }
28}
29
30impl Texture2DSample for Rgba {
31    fn create_texture_descriptor(size: Size) -> impl IntoIterator<Item = (Size, TextureFormat)> {
32        [(size, TextureFormat::Rgba8Unorm)]
33    }
34
35    fn views_descriptors<'a>(
36        &'a self,
37        texture: Option<&'a Texture>,
38    ) -> impl IntoIterator<Item = (&'a Texture, TextureFormat, TextureAspect)> {
39        [(
40            texture.unwrap_or_else(|| &self.0),
41            TextureFormat::Rgba8Unorm,
42            TextureAspect::All,
43        )]
44    }
45
46    fn copy_buffer_descriptors<'a>(
47        &self,
48        buffers: &'a [&'a [u8]],
49    ) -> impl IntoIterator<Item = (&'a [u8], &Texture, TextureAspect, Size)> {
50        let size = self.0.size();
51        [(
52            buffers[0],
53            &self.0,
54            TextureAspect::All,
55            Size {
56                width: size.width * 4,
57                height: size.height,
58            },
59        )]
60    }
61}