Skip to main content

rgpui_wgpu/
wgpu_atlas.rs

1use anyhow::{Context as _, Result};
2use etagere::{BucketedAtlasAllocator, size2};
3use parking_lot::Mutex;
4use rgpui::collections::FxHashMap;
5use rgpui::{
6    AtlasKey, AtlasTextureId, AtlasTextureKind, AtlasTextureList, AtlasTile, Bounds, DevicePixels,
7    PlatformAtlas, Point, Size,
8};
9use std::{borrow::Cow, ops, sync::Arc};
10
11use crate::WgpuContext;
12
13/// 将 DevicePixels 尺寸转换为 etagere 所需的尺寸格式
14fn device_size_to_etagere(size: Size<DevicePixels>) -> etagere::Size {
15    size2(size.width.0, size.height.0)
16}
17
18/// 将 etagere 坐标点转换为 DevicePixels 坐标点
19fn etagere_point_to_device(point: etagere::Point) -> Point<DevicePixels> {
20    Point {
21        x: DevicePixels(point.x),
22        y: DevicePixels(point.y),
23    }
24}
25
26/// wgpu 纹理图集实现,用于缓存和渲染纹理
27pub struct WgpuAtlas(Mutex<WgpuAtlasState>);
28
29/// 待上传的纹理数据
30struct PendingUpload {
31    id: AtlasTextureId,
32    bounds: Bounds<DevicePixels>,
33    data: Vec<u8>,
34}
35
36/// 图集内部状态
37struct WgpuAtlasState {
38    device: Arc<wgpu::Device>,
39    queue: Arc<wgpu::Queue>,
40    max_texture_size: u32,
41    color_texture_format: wgpu::TextureFormat,
42    storage: WgpuAtlasStorage,
43    tiles_by_key: FxHashMap<AtlasKey, AtlasTile>,
44    pending_uploads: Vec<PendingUpload>,
45}
46
47/// wgpu 纹理信息,包含纹理视图
48pub struct WgpuTextureInfo {
49    pub view: wgpu::TextureView,
50}
51
52impl WgpuAtlas {
53    /// 创建新的纹理图集
54    pub fn new(
55        device: Arc<wgpu::Device>,
56        queue: Arc<wgpu::Queue>,
57        color_texture_format: wgpu::TextureFormat,
58    ) -> Self {
59        let max_texture_size = device.limits().max_texture_dimension_2d;
60        WgpuAtlas(Mutex::new(WgpuAtlasState {
61            device,
62            queue,
63            max_texture_size,
64            color_texture_format,
65            storage: WgpuAtlasStorage::default(),
66            tiles_by_key: Default::default(),
67            pending_uploads: Vec::new(),
68        }))
69    }
70
71    /// 从 WgpuContext 创建纹理图集
72    pub fn from_context(context: &WgpuContext) -> Self {
73        Self::new(
74            context.device.clone(),
75            context.queue.clone(),
76            context.color_texture_format(),
77        )
78    }
79
80    /// 在每帧渲染前刷新待上传的纹理数据
81    pub fn before_frame(&self) {
82        let mut lock = self.0.lock();
83        lock.flush_uploads();
84    }
85
86    /// 获取指定纹理 ID 的纹理信息
87    pub fn get_texture_info(&self, id: AtlasTextureId) -> WgpuTextureInfo {
88        let lock = self.0.lock();
89        let texture = &lock.storage[id];
90        WgpuTextureInfo {
91            view: texture.view.clone(),
92        }
93    }
94
95    /// 清除所有缓存的纹理和图块,强制它们重新创建。
96    /// 当设备仍然有效时,用于增量恢复。
97    pub fn clear(&self) {
98        let mut lock = self.0.lock();
99        lock.storage = WgpuAtlasStorage::default();
100        lock.tiles_by_key.clear();
101        lock.pending_uploads.clear();
102    }
103
104    /// 处理设备丢失,清除所有纹理和缓存的图块。
105    /// 图集将在后续帧中按需惰性重新创建纹理。
106    pub fn handle_device_lost(&self, context: &WgpuContext) {
107        let mut lock = self.0.lock();
108        lock.device = context.device.clone();
109        lock.queue = context.queue.clone();
110        lock.color_texture_format = context.color_texture_format();
111        lock.storage = WgpuAtlasStorage::default();
112        lock.tiles_by_key.clear();
113        lock.pending_uploads.clear();
114    }
115}
116
117impl PlatformAtlas for WgpuAtlas {
118    fn get_or_insert_with<'a>(
119        &self,
120        key: &AtlasKey,
121        build: &mut dyn FnMut() -> Result<Option<(Size<DevicePixels>, Cow<'a, [u8]>)>>,
122    ) -> Result<Option<AtlasTile>> {
123        let mut lock = self.0.lock();
124        if let Some(tile) = lock.tiles_by_key.get(key) {
125            Ok(Some(*tile))
126        } else {
127            profiling::scope!("new tile");
128            let Some((size, bytes)) = build()? else {
129                return Ok(None);
130            };
131            let tile = lock
132                .allocate(size, key.texture_kind())
133                .context("failed to allocate")?;
134            lock.upload_texture(tile.texture_id, tile.bounds, &bytes);
135            lock.tiles_by_key.insert(key.clone(), tile);
136            Ok(Some(tile))
137        }
138    }
139
140    fn remove(&self, key: &AtlasKey) {
141        let mut lock = self.0.lock();
142
143        let Some(id) = lock.tiles_by_key.remove(key).map(|tile| tile.texture_id) else {
144            return;
145        };
146
147        let Some(texture_slot) = lock.storage[id.kind].textures.get_mut(id.index as usize) else {
148            return;
149        };
150
151        if let Some(mut texture) = texture_slot.take() {
152            texture.decrement_ref_count();
153            if texture.is_unreferenced() {
154                lock.pending_uploads
155                    .retain(|upload| upload.id != texture.id);
156                lock.storage[id.kind]
157                    .free_list
158                    .push(texture.id.index as usize);
159            } else {
160                *texture_slot = Some(texture);
161            }
162        }
163    }
164}
165
166impl WgpuAtlasState {
167    /// 分配图集空间,如果现有纹理无法容纳则创建新纹理
168    fn allocate(
169        &mut self,
170        size: Size<DevicePixels>,
171        texture_kind: AtlasTextureKind,
172    ) -> Option<AtlasTile> {
173        {
174            let textures = &mut self.storage[texture_kind];
175
176            if let Some(tile) = textures
177                .iter_mut()
178                .rev()
179                .find_map(|texture| texture.allocate(size))
180            {
181                return Some(tile);
182            }
183        }
184
185        let texture = self.push_texture(size, texture_kind);
186        texture.allocate(size)
187    }
188
189    /// 创建新纹理并添加到图集中
190    fn push_texture(
191        &mut self,
192        min_size: Size<DevicePixels>,
193        kind: AtlasTextureKind,
194    ) -> &mut WgpuAtlasTexture {
195        const DEFAULT_ATLAS_SIZE: Size<DevicePixels> = Size {
196            width: DevicePixels(1024),
197            height: DevicePixels(1024),
198        };
199        let max_texture_size = self.max_texture_size as i32;
200        let max_atlas_size = Size {
201            width: DevicePixels(max_texture_size),
202            height: DevicePixels(max_texture_size),
203        };
204
205        let size = min_size.min(&max_atlas_size).max(&DEFAULT_ATLAS_SIZE);
206        let format = match kind {
207            AtlasTextureKind::Monochrome => wgpu::TextureFormat::R8Unorm,
208            AtlasTextureKind::Subpixel | AtlasTextureKind::Polychrome => self.color_texture_format,
209        };
210
211        let texture = self.device.create_texture(&wgpu::TextureDescriptor {
212            label: Some("atlas"),
213            size: wgpu::Extent3d {
214                width: size.width.0 as u32,
215                height: size.height.0 as u32,
216                depth_or_array_layers: 1,
217            },
218            mip_level_count: 1,
219            sample_count: 1,
220            dimension: wgpu::TextureDimension::D2,
221            format,
222            usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
223            view_formats: &[],
224        });
225
226        let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
227
228        let texture_list = &mut self.storage[kind];
229        let index = texture_list.free_list.pop();
230
231        let atlas_texture = WgpuAtlasTexture {
232            id: AtlasTextureId {
233                index: index.unwrap_or(texture_list.textures.len()) as u32,
234                kind,
235            },
236            allocator: BucketedAtlasAllocator::new(device_size_to_etagere(size)),
237            format,
238            texture,
239            view,
240            live_atlas_keys: 0,
241        };
242
243        if let Some(ix) = index {
244            texture_list.textures[ix] = Some(atlas_texture);
245            texture_list
246                .textures
247                .get_mut(ix)
248                .and_then(|t| t.as_mut())
249                .expect("texture must exist")
250        } else {
251            texture_list.textures.push(Some(atlas_texture));
252            texture_list
253                .textures
254                .last_mut()
255                .and_then(|t| t.as_mut())
256                .expect("texture must exist")
257        }
258    }
259
260    /// 将纹理数据加入待上传队列
261    fn upload_texture(&mut self, id: AtlasTextureId, bounds: Bounds<DevicePixels>, bytes: &[u8]) {
262        let data = self
263            .storage
264            .get(id)
265            .map(|texture| swizzle_upload_data(bytes, texture.format))
266            .unwrap_or_else(|| bytes.to_vec());
267
268        self.pending_uploads
269            .push(PendingUpload { id, bounds, data });
270    }
271
272    /// 将所有待上传的纹理数据写入 GPU
273    fn flush_uploads(&mut self) {
274        for upload in self.pending_uploads.drain(..) {
275            let Some(texture) = self.storage.get(upload.id) else {
276                continue;
277            };
278            let bytes_per_pixel = texture.bytes_per_pixel();
279
280            self.queue.write_texture(
281                wgpu::TexelCopyTextureInfo {
282                    texture: &texture.texture,
283                    mip_level: 0,
284                    origin: wgpu::Origin3d {
285                        x: upload.bounds.origin.x.0 as u32,
286                        y: upload.bounds.origin.y.0 as u32,
287                        z: 0,
288                    },
289                    aspect: wgpu::TextureAspect::All,
290                },
291                &upload.data,
292                wgpu::TexelCopyBufferLayout {
293                    offset: 0,
294                    bytes_per_row: Some(upload.bounds.size.width.0 as u32 * bytes_per_pixel as u32),
295                    rows_per_image: None,
296                },
297                wgpu::Extent3d {
298                    width: upload.bounds.size.width.0 as u32,
299                    height: upload.bounds.size.height.0 as u32,
300                    depth_or_array_layers: 1,
301                },
302            );
303        }
304    }
305}
306
307#[derive(Default)]
308/// 图集纹理存储,按类型分类管理
309struct WgpuAtlasStorage {
310    monochrome_textures: AtlasTextureList<WgpuAtlasTexture>,
311    subpixel_textures: AtlasTextureList<WgpuAtlasTexture>,
312    polychrome_textures: AtlasTextureList<WgpuAtlasTexture>,
313}
314
315impl ops::Index<AtlasTextureKind> for WgpuAtlasStorage {
316    type Output = AtlasTextureList<WgpuAtlasTexture>;
317    fn index(&self, kind: AtlasTextureKind) -> &Self::Output {
318        match kind {
319            AtlasTextureKind::Monochrome => &self.monochrome_textures,
320            AtlasTextureKind::Subpixel => &self.subpixel_textures,
321            AtlasTextureKind::Polychrome => &self.polychrome_textures,
322        }
323    }
324}
325
326impl ops::IndexMut<AtlasTextureKind> for WgpuAtlasStorage {
327    fn index_mut(&mut self, kind: AtlasTextureKind) -> &mut Self::Output {
328        match kind {
329            AtlasTextureKind::Monochrome => &mut self.monochrome_textures,
330            AtlasTextureKind::Subpixel => &mut self.subpixel_textures,
331            AtlasTextureKind::Polychrome => &mut self.polychrome_textures,
332        }
333    }
334}
335
336impl WgpuAtlasStorage {
337    /// 根据纹理 ID 获取纹理引用
338    fn get(&self, id: AtlasTextureId) -> Option<&WgpuAtlasTexture> {
339        self[id.kind]
340            .textures
341            .get(id.index as usize)
342            .and_then(|t| t.as_ref())
343    }
344}
345
346impl ops::Index<AtlasTextureId> for WgpuAtlasStorage {
347    type Output = WgpuAtlasTexture;
348    fn index(&self, id: AtlasTextureId) -> &Self::Output {
349        let textures = match id.kind {
350            AtlasTextureKind::Monochrome => &self.monochrome_textures,
351            AtlasTextureKind::Subpixel => &self.subpixel_textures,
352            AtlasTextureKind::Polychrome => &self.polychrome_textures,
353        };
354        textures[id.index as usize]
355            .as_ref()
356            .expect("texture must exist")
357    }
358}
359
360/// 图集纹理,包含分配器和 GPU 资源
361struct WgpuAtlasTexture {
362    id: AtlasTextureId,
363    allocator: BucketedAtlasAllocator,
364    texture: wgpu::Texture,
365    view: wgpu::TextureView,
366    format: wgpu::TextureFormat,
367    live_atlas_keys: u32,
368}
369
370impl WgpuAtlasTexture {
371    /// 在图集中分配指定大小的空间
372    fn allocate(&mut self, size: Size<DevicePixels>) -> Option<AtlasTile> {
373        let allocation = self.allocator.allocate(device_size_to_etagere(size))?;
374        let tile = AtlasTile {
375            texture_id: self.id,
376            tile_id: allocation.id.into(),
377            padding: 0,
378            bounds: Bounds {
379                origin: etagere_point_to_device(allocation.rectangle.min),
380                size,
381            },
382        };
383        self.live_atlas_keys += 1;
384        Some(tile)
385    }
386
387    /// 获取每个像素占用的字节数
388    fn bytes_per_pixel(&self) -> u8 {
389        match self.format {
390            wgpu::TextureFormat::R8Unorm => 1,
391            wgpu::TextureFormat::Bgra8Unorm | wgpu::TextureFormat::Rgba8Unorm => 4,
392            _ => 4,
393        }
394    }
395
396    /// 减少引用计数
397    fn decrement_ref_count(&mut self) {
398        self.live_atlas_keys -= 1;
399    }
400
401    /// 检查是否无引用
402    fn is_unreferenced(&self) -> bool {
403        self.live_atlas_keys == 0
404    }
405}
406
407/// 转换上传数据的通道顺序,将 RGBA 转换为 BGRA
408fn swizzle_upload_data(bytes: &[u8], format: wgpu::TextureFormat) -> Vec<u8> {
409    match format {
410        wgpu::TextureFormat::Rgba8Unorm => {
411            let mut data = bytes.to_vec();
412            for pixel in data.chunks_exact_mut(4) {
413                pixel.swap(0, 2);
414            }
415            data
416        }
417        _ => bytes.to_vec(),
418    }
419}
420
421#[cfg(all(test, not(target_family = "wasm")))]
422mod tests {
423    use super::*;
424    use rgpui::block_on;
425    use rgpui::{ImageId, RenderImageParams};
426    use std::sync::Arc;
427
428    /// 创建测试用的 wgpu 设备和队列
429    fn test_device_and_queue() -> anyhow::Result<(Arc<wgpu::Device>, Arc<wgpu::Queue>)> {
430        block_on(async {
431            let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
432                backends: wgpu::Backends::all(),
433                flags: wgpu::InstanceFlags::default(),
434                backend_options: wgpu::BackendOptions::default(),
435                memory_budget_thresholds: wgpu::MemoryBudgetThresholds::default(),
436                display: None,
437            });
438            let adapter = instance
439                .request_adapter(&wgpu::RequestAdapterOptions {
440                    power_preference: wgpu::PowerPreference::LowPower,
441                    compatible_surface: None,
442                    force_fallback_adapter: false,
443                })
444                .await
445                .map_err(|error| anyhow::anyhow!("failed to request adapter: {error}"))?;
446            let (device, queue) = adapter
447                .request_device(&wgpu::DeviceDescriptor {
448                    label: Some("wgpu_atlas_test_device"),
449                    required_features: wgpu::Features::empty(),
450                    required_limits: wgpu::Limits::downlevel_defaults()
451                        .using_resolution(adapter.limits())
452                        .using_alignment(adapter.limits()),
453                    memory_hints: wgpu::MemoryHints::MemoryUsage,
454                    trace: wgpu::Trace::Off,
455                    experimental_features: wgpu::ExperimentalFeatures::disabled(),
456                })
457                .await
458                .map_err(|error| anyhow::anyhow!("failed to request device: {error}"))?;
459            Ok((Arc::new(device), Arc::new(queue)))
460        })
461    }
462
463    #[test]
464    fn before_frame_skips_uploads_for_removed_texture() -> anyhow::Result<()> {
465        let (device, queue) = test_device_and_queue()?;
466
467        let atlas = WgpuAtlas::new(device, queue, wgpu::TextureFormat::Bgra8Unorm);
468        let key = AtlasKey::Image(RenderImageParams {
469            image_id: ImageId(1),
470            frame_index: 0,
471        });
472        let size = Size {
473            width: DevicePixels(1),
474            height: DevicePixels(1),
475        };
476        let mut build = || Ok(Some((size, Cow::Owned(vec![0, 0, 0, 255]))));
477
478        // 回归测试:修复前会在 flush_uploads 中 panic
479        atlas
480            .get_or_insert_with(&key, &mut build)?
481            .expect("tile should be created");
482        atlas.remove(&key);
483        atlas.before_frame();
484        Ok(())
485    }
486
487    #[test]
488    fn swizzle_upload_data_preserves_bgra_uploads() {
489        let input = vec![0x10, 0x20, 0x30, 0x40];
490        assert_eq!(
491            swizzle_upload_data(&input, wgpu::TextureFormat::Bgra8Unorm),
492            input
493        );
494    }
495
496    #[test]
497    fn swizzle_upload_data_converts_bgra_to_rgba() {
498        let input = vec![0x10, 0x20, 0x30, 0x40, 0xAA, 0xBB, 0xCC, 0xDD];
499        assert_eq!(
500            swizzle_upload_data(&input, wgpu::TextureFormat::Rgba8Unorm),
501            vec![0x30, 0x20, 0x10, 0x40, 0xCC, 0xBB, 0xAA, 0xDD]
502        );
503    }
504}