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
use std::collections::HashMap;
use std::ops::Index;

pub struct ImageBind {
    pub bind: wgpu::BindGroup,
    pub size: wgpu::Extent3d,
}

#[cfg_attr(feature = "bevy", derive(bevy::prelude::Resource))]
pub struct Images<Key> {
    pub images: HashMap<Key, ImageBind>,
    pub layout: wgpu::BindGroupLayout,
    pub default_sampler: wgpu::Sampler,
}

impl<Key: Eq + std::hash::Hash> Index<&Key> for Images<Key> {
    type Output = ImageBind;
    fn index(&self, index: &Key) -> &Self::Output {
        self.images.index(index)
    }
}

impl<Key: Eq + std::hash::Hash> Images<Key> {
    pub fn new(device: &wgpu::Device) -> Self {
        Self {
            images: HashMap::default(),
            layout: device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
                label: Some("reui image bind group"),
                entries: &[
                    wgpu::BindGroupLayoutEntry {
                        binding: 0,
                        visibility: wgpu::ShaderStages::FRAGMENT,
                        ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
                        count: None,
                    },
                    wgpu::BindGroupLayoutEntry {
                        binding: 1,
                        visibility: wgpu::ShaderStages::FRAGMENT,
                        ty: wgpu::BindingType::Texture {
                            sample_type: wgpu::TextureSampleType::Float { filterable: true },
                            view_dimension: wgpu::TextureViewDimension::D2,
                            multisampled: false,
                        },
                        count: None,
                    },
                ],
            }),
            default_sampler: device.create_sampler(&wgpu::SamplerDescriptor {
                label: Some("reui default sampler"),
                address_mode_u: wgpu::AddressMode::ClampToEdge,
                address_mode_v: wgpu::AddressMode::ClampToEdge,
                address_mode_w: wgpu::AddressMode::ClampToEdge,
                mag_filter: wgpu::FilterMode::Linear,
                min_filter: wgpu::FilterMode::Linear,
                mipmap_filter: wgpu::FilterMode::Linear,
                lod_min_clamp: 0.0,
                lod_max_clamp: 100.0,
                compare: None,
                anisotropy_clamp: 1,
                border_color: None,
            }),
        }
    }

    pub fn get(&self, image: &Key) -> Option<&ImageBind> {
        self.images.get(image)
    }

    pub fn remove(&mut self, key: Key) -> Option<ImageBind> {
        self.images.remove(&key)
    }

    pub fn upload(
        &mut self,
        device: &wgpu::Device,
        queue: &wgpu::Queue,
        key: Key,
        label: Option<&str>,
        width: u32,
        height: u32,
        data: &[u8],
        sampler: Option<&wgpu::Sampler>,
    ) -> Option<ImageBind> {
        let size = wgpu::Extent3d {
            width,
            height,
            depth_or_array_layers: 1,
        };

        let texture = device.create_texture(&wgpu::TextureDescriptor {
            label,
            size,
            mip_level_count: 1,
            sample_count: 1,
            dimension: wgpu::TextureDimension::D2,
            format: wgpu::TextureFormat::Rgba8UnormSrgb,
            usage: wgpu::TextureUsages::COPY_DST | wgpu::TextureUsages::TEXTURE_BINDING,
            view_formats: &[],
        });

        let copy_texture = wgpu::ImageCopyTexture {
            texture: &texture,
            mip_level: 0,
            origin: wgpu::Origin3d::ZERO,
            aspect: wgpu::TextureAspect::All,
        };

        let data_layout = wgpu::ImageDataLayout {
            offset: 0,
            bytes_per_row: Some(4 * width),
            rows_per_image: None,
        };

        queue.write_texture(copy_texture, data, data_layout, size);

        let desc = wgpu::TextureViewDescriptor::default();
        let view = texture.create_view(&desc);

        self.insert(device, key, &view, size, sampler)
    }

    pub fn insert(
        &mut self,
        device: &wgpu::Device,
        key: Key,
        view: &wgpu::TextureView,
        size: wgpu::Extent3d,
        sampler: Option<&wgpu::Sampler>,
    ) -> Option<ImageBind> {
        let sampler = sampler.unwrap_or(&self.default_sampler);

        let bind = device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("image bind group"),
            layout: &self.layout,
            entries: &[
                wgpu::BindGroupEntry {
                    binding: 0,
                    resource: wgpu::BindingResource::Sampler(sampler),
                },
                wgpu::BindGroupEntry {
                    binding: 1,
                    resource: wgpu::BindingResource::TextureView(view),
                },
            ],
        });

        self.images.insert(key, ImageBind { bind, size })
    }
}