Skip to main content

game_toolkit_gfx/
texture.rs

1use std::collections::HashMap;
2use std::path::Path;
3
4use anyhow::{Context, Result};
5
6#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
7pub struct TextureId(pub u32);
8
9pub struct Texture {
10    pub size: [u32; 2],
11    pub(crate) bind_group: wgpu::BindGroup,
12    #[allow(dead_code)]
13    pub(crate) texture: wgpu::Texture,
14    #[allow(dead_code)]
15    pub(crate) view: wgpu::TextureView,
16}
17
18pub(crate) struct TextureRegistry {
19    map: HashMap<TextureId, Texture>,
20    next: u32,
21    pub(crate) layout: wgpu::BindGroupLayout,
22    sampler: wgpu::Sampler,
23    white: TextureId,
24}
25
26impl TextureRegistry {
27    pub fn new(device: &wgpu::Device, queue: &wgpu::Queue) -> Self {
28        let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
29            label: Some("sprite.texture_bgl"),
30            entries: &[
31                wgpu::BindGroupLayoutEntry {
32                    binding: 0,
33                    visibility: wgpu::ShaderStages::FRAGMENT,
34                    ty: wgpu::BindingType::Texture {
35                        sample_type: wgpu::TextureSampleType::Float { filterable: true },
36                        view_dimension: wgpu::TextureViewDimension::D2,
37                        multisampled: false,
38                    },
39                    count: None,
40                },
41                wgpu::BindGroupLayoutEntry {
42                    binding: 1,
43                    visibility: wgpu::ShaderStages::FRAGMENT,
44                    ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
45                    count: None,
46                },
47            ],
48        });
49
50        let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
51            label: Some("sprite.sampler"),
52            address_mode_u: wgpu::AddressMode::ClampToEdge,
53            address_mode_v: wgpu::AddressMode::ClampToEdge,
54            address_mode_w: wgpu::AddressMode::ClampToEdge,
55            mag_filter: wgpu::FilterMode::Nearest,
56            min_filter: wgpu::FilterMode::Nearest,
57            mipmap_filter: wgpu::MipmapFilterMode::Nearest,
58            ..Default::default()
59        });
60
61        let mut me = Self {
62            map: HashMap::new(),
63            next: 1,
64            layout,
65            sampler,
66            white: TextureId(0),
67        };
68        me.white = me.create_from_rgba(device, queue, 1, 1, &[255, 255, 255, 255], Some("white"));
69        me
70    }
71
72    pub fn white(&self) -> TextureId {
73        self.white
74    }
75
76    pub fn bind_group(&self, id: TextureId) -> &wgpu::BindGroup {
77        &self
78            .map
79            .get(&id)
80            .unwrap_or_else(|| self.map.get(&self.white).expect("white texture"))
81            .bind_group
82    }
83
84    /// Reserved accessor: look up a texture by id (e.g. to inspect its size).
85    /// Kept as part of the registry's intended API though not yet wired up.
86    #[allow(dead_code)]
87    pub fn get(&self, id: TextureId) -> Option<&Texture> {
88        self.map.get(&id)
89    }
90
91    pub fn load_file(
92        &mut self,
93        device: &wgpu::Device,
94        queue: &wgpu::Queue,
95        path: impl AsRef<Path>,
96    ) -> Result<TextureId> {
97        let path = path.as_ref();
98        let img = image::open(path).with_context(|| format!("loading {}", path.display()))?;
99        let rgba = img.to_rgba8();
100        let (w, h) = rgba.dimensions();
101        Ok(self.create_from_rgba(
102            device,
103            queue,
104            w,
105            h,
106            rgba.as_raw(),
107            path.file_name().and_then(|s| s.to_str()),
108        ))
109    }
110
111    /// Replace the contents of `id` with a freshly decoded copy of `path`. Keeps the same
112    /// `TextureId` so anything already holding it stays valid.
113    pub fn reload(
114        &mut self,
115        device: &wgpu::Device,
116        queue: &wgpu::Queue,
117        id: TextureId,
118        path: impl AsRef<Path>,
119    ) -> Result<()> {
120        let path = path.as_ref();
121        let img = image::open(path).with_context(|| format!("reloading {}", path.display()))?;
122        let rgba = img.to_rgba8();
123        let (w, h) = rgba.dimensions();
124        self.replace(
125            device,
126            queue,
127            id,
128            w,
129            h,
130            rgba.as_raw(),
131            path.file_name().and_then(|s| s.to_str()),
132        );
133        Ok(())
134    }
135
136    #[allow(clippy::too_many_arguments)]
137    pub fn replace(
138        &mut self,
139        device: &wgpu::Device,
140        queue: &wgpu::Queue,
141        id: TextureId,
142        width: u32,
143        height: u32,
144        rgba: &[u8],
145        label: Option<&str>,
146    ) {
147        let new_id = self.create_from_rgba(device, queue, width, height, rgba, label);
148        if let Some(new_tex) = self.map.remove(&new_id) {
149            self.map.insert(id, new_tex);
150        }
151    }
152
153    pub fn create_from_rgba(
154        &mut self,
155        device: &wgpu::Device,
156        queue: &wgpu::Queue,
157        width: u32,
158        height: u32,
159        rgba: &[u8],
160        label: Option<&str>,
161    ) -> TextureId {
162        let size = wgpu::Extent3d {
163            width,
164            height,
165            depth_or_array_layers: 1,
166        };
167        let texture = device.create_texture(&wgpu::TextureDescriptor {
168            label,
169            size,
170            mip_level_count: 1,
171            sample_count: 1,
172            dimension: wgpu::TextureDimension::D2,
173            format: wgpu::TextureFormat::Rgba8UnormSrgb,
174            usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
175            view_formats: &[],
176        });
177        queue.write_texture(
178            wgpu::TexelCopyTextureInfo {
179                texture: &texture,
180                mip_level: 0,
181                origin: wgpu::Origin3d::ZERO,
182                aspect: wgpu::TextureAspect::All,
183            },
184            rgba,
185            wgpu::TexelCopyBufferLayout {
186                offset: 0,
187                bytes_per_row: Some(4 * width),
188                rows_per_image: Some(height),
189            },
190            size,
191        );
192        let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
193        let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
194            label,
195            layout: &self.layout,
196            entries: &[
197                wgpu::BindGroupEntry {
198                    binding: 0,
199                    resource: wgpu::BindingResource::TextureView(&view),
200                },
201                wgpu::BindGroupEntry {
202                    binding: 1,
203                    resource: wgpu::BindingResource::Sampler(&self.sampler),
204                },
205            ],
206        });
207
208        let id = TextureId(self.next);
209        self.next += 1;
210        self.map.insert(
211            id,
212            Texture {
213                size: [width, height],
214                bind_group,
215                texture,
216                view,
217            },
218        );
219        id
220    }
221}