kova-native-render 0.2.0

GPU scene renderer for Kova Native (wgpu).
Documentation
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
//! Texture atlases for glyphs, images and rasterized vectors.
//!
//! Two atlases exist: a single channel coverage atlas (`Mono`, glyph masks,
//! tintable icons) and a premultiplied RGBA atlas (`Color`, images, emoji).
//! Each is a GPU texture array; when a layer fills up a new layer is added,
//! so the whole UI can still be drawn with one bind group and one draw call.

use etagere::{AllocId, BucketedAtlasAllocator, size2};
use kova_native_core::ImageId;
use kova_native_text::GlyphKey;
use rustc_hash::FxHashMap;
use std::borrow::Cow;

/// Edge length of every atlas layer, in texels.
pub const ATLAS_SIZE: u32 = 2048;
/// Empty texels around every tile so bilinear filtering never bleeds.
const PADDING: u32 = 1;

/// What is stored in the atlas.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum AtlasKey {
    Glyph(GlyphKey),
    Image(ImageId),
    /// A vector image rasterized at a specific device pixel size.
    Vector {
        id: ImageId,
        width: u32,
        height: u32,
        mono: bool,
    },
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum AtlasKind {
    /// 8-bit coverage, tinted by the primitive color.
    Mono,
    /// Premultiplied RGBA.
    Color,
}

impl AtlasKind {
    fn bytes_per_pixel(self) -> u32 {
        match self {
            AtlasKind::Mono => 1,
            AtlasKind::Color => 4,
        }
    }
}

/// Location of an image inside the atlas.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct AtlasTile {
    pub kind: AtlasKind,
    pub layer: u32,
    pub x: u32,
    pub y: u32,
    pub width: u32,
    pub height: u32,
    /// Offset of the bitmap from its anchor point (e.g. glyph origin), copied
    /// from [`AtlasImage::origin`].
    pub origin: (i32, i32),
}

impl AtlasTile {
    /// Normalized texture coordinates `[u0, v0, u1, v1]`.
    pub fn uv(&self) -> [f32; 4] {
        let s = ATLAS_SIZE as f32;
        [
            self.x as f32 / s,
            self.y as f32 / s,
            (self.x + self.width) as f32 / s,
            (self.y + self.height) as f32 / s,
        ]
    }
}

/// Pixel data to insert into the atlas.
pub struct AtlasImage<'a> {
    pub kind: AtlasKind,
    pub width: u32,
    pub height: u32,
    /// Tightly packed rows (`width * bytes_per_pixel` bytes each).
    pub data: Cow<'a, [u8]>,
    /// Offset of the bitmap's top-left corner from its anchor point (glyphs:
    /// `(left, -top)` relative to the pen position on the baseline).
    pub origin: (i32, i32),
}

pub(crate) struct Upload {
    pub kind: AtlasKind,
    pub layer: u32,
    pub x: u32,
    pub y: u32,
    pub width: u32,
    pub height: u32,
    pub data: Vec<u8>,
}

#[derive(Default)]
struct Layers {
    allocators: Vec<BucketedAtlasAllocator>,
}

impl Layers {
    fn allocate(&mut self, width: u32, height: u32) -> Option<(u32, AllocId, u32, u32)> {
        let size = size2((width + 2 * PADDING) as i32, (height + 2 * PADDING) as i32);
        for (layer, alloc) in self.allocators.iter_mut().enumerate() {
            if let Some(a) = alloc.allocate(size) {
                return Some((
                    layer as u32,
                    a.id,
                    a.rectangle.min.x as u32,
                    a.rectangle.min.y as u32,
                ));
            }
        }
        let mut alloc = BucketedAtlasAllocator::new(size2(ATLAS_SIZE as i32, ATLAS_SIZE as i32));
        let a = alloc.allocate(size)?;
        self.allocators.push(alloc);
        Some((
            (self.allocators.len() - 1) as u32,
            a.id,
            a.rectangle.min.x as u32,
            a.rectangle.min.y as u32,
        ))
    }
}

/// CPU side of the atlases: allocation, key lookup and pending uploads.
#[derive(Default)]
pub struct Atlas {
    tiles: FxHashMap<AtlasKey, Option<(AtlasTile, AllocId)>>,
    mono: Layers,
    color: Layers,
    pending: Vec<Upload>,
}

impl Atlas {
    pub fn new() -> Self {
        Self::default()
    }

    /// Returns the tile for `key`, rasterizing it with `build` on first use.
    /// `build` returning `None` (e.g. whitespace glyphs) is cached as well.
    pub fn get_or_insert_with<'a>(
        &mut self,
        key: AtlasKey,
        build: impl FnOnce() -> Option<AtlasImage<'a>>,
    ) -> Option<AtlasTile> {
        if let Some(entry) = self.tiles.get(&key) {
            return entry.map(|(tile, _)| tile);
        }
        let entry = build().and_then(|image| self.insert(image));
        self.tiles.insert(key, entry);
        entry.map(|(tile, _)| tile)
    }

    fn insert(&mut self, image: AtlasImage<'_>) -> Option<(AtlasTile, AllocId)> {
        if image.width == 0 || image.height == 0 {
            return None;
        }
        if image.width + 2 * PADDING > ATLAS_SIZE || image.height + 2 * PADDING > ATLAS_SIZE {
            log::warn!(
                "kova-native atlas: {}x{} image exceeds atlas size {ATLAS_SIZE}",
                image.width,
                image.height
            );
            return None;
        }
        let expected = (image.width * image.height * image.kind.bytes_per_pixel()) as usize;
        if image.data.len() < expected {
            log::error!(
                "kova-native atlas: image data too short ({} < {expected})",
                image.data.len()
            );
            return None;
        }
        let layers = match image.kind {
            AtlasKind::Mono => &mut self.mono,
            AtlasKind::Color => &mut self.color,
        };
        let (layer, id, x, y) = layers.allocate(image.width, image.height)?;
        let tile = AtlasTile {
            kind: image.kind,
            layer,
            x: x + PADDING,
            y: y + PADDING,
            width: image.width,
            height: image.height,
            origin: image.origin,
        };
        self.pending.push(Upload {
            kind: image.kind,
            layer,
            x: tile.x,
            y: tile.y,
            width: image.width,
            height: image.height,
            data: image.data[..expected].to_vec(),
        });
        Some((tile, id))
    }

    /// Frees the tile for `key` (e.g. when an image is dropped).
    pub fn remove(&mut self, key: &AtlasKey) {
        if let Some(Some((tile, id))) = self.tiles.remove(key) {
            let layers = match tile.kind {
                AtlasKind::Mono => &mut self.mono,
                AtlasKind::Color => &mut self.color,
            };
            if let Some(alloc) = layers.allocators.get_mut(tile.layer as usize) {
                alloc.deallocate(id);
            }
        }
    }

    pub fn contains(&self, key: &AtlasKey) -> bool {
        self.tiles.contains_key(key)
    }

    pub fn layer_count(&self, kind: AtlasKind) -> u32 {
        match kind {
            AtlasKind::Mono => self.mono.allocators.len() as u32,
            AtlasKind::Color => self.color.allocators.len() as u32,
        }
    }

    pub fn tile_count(&self) -> usize {
        self.tiles.len()
    }

    pub(crate) fn take_uploads(&mut self) -> Vec<Upload> {
        std::mem::take(&mut self.pending)
    }
}

/// GPU textures backing the atlas.
pub(crate) struct AtlasTextures {
    pub mono: GpuLayers,
    pub color: GpuLayers,
}

pub(crate) struct GpuLayers {
    pub texture: wgpu::Texture,
    pub view: wgpu::TextureView,
    pub layers: u32,
    format: wgpu::TextureFormat,
}

impl GpuLayers {
    fn new(device: &wgpu::Device, format: wgpu::TextureFormat, layers: u32, label: &str) -> Self {
        let layers = layers.max(1);
        let texture = device.create_texture(&wgpu::TextureDescriptor {
            label: Some(label),
            size: wgpu::Extent3d {
                width: ATLAS_SIZE,
                height: ATLAS_SIZE,
                depth_or_array_layers: layers,
            },
            mip_level_count: 1,
            sample_count: 1,
            dimension: wgpu::TextureDimension::D2,
            format,
            usage: wgpu::TextureUsages::TEXTURE_BINDING
                | wgpu::TextureUsages::COPY_DST
                | wgpu::TextureUsages::COPY_SRC,
            view_formats: &[],
        });
        let view = texture.create_view(&wgpu::TextureViewDescriptor {
            label: Some(label),
            dimension: Some(wgpu::TextureViewDimension::D2Array),
            ..Default::default()
        });
        GpuLayers {
            texture,
            view,
            layers,
            format,
        }
    }

    /// Grows the texture array to `needed` layers, preserving contents.
    /// Returns `true` if the texture was recreated.
    fn ensure(
        &mut self,
        device: &wgpu::Device,
        queue: &wgpu::Queue,
        needed: u32,
        label: &str,
    ) -> bool {
        if needed <= self.layers {
            return false;
        }
        let new_layers = needed.max(self.layers * 2);
        let new = GpuLayers::new(device, self.format, new_layers, label);
        let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
            label: Some("kova-native atlas grow"),
        });
        encoder.copy_texture_to_texture(
            wgpu::TexelCopyTextureInfo {
                texture: &self.texture,
                mip_level: 0,
                origin: wgpu::Origin3d::ZERO,
                aspect: wgpu::TextureAspect::All,
            },
            wgpu::TexelCopyTextureInfo {
                texture: &new.texture,
                mip_level: 0,
                origin: wgpu::Origin3d::ZERO,
                aspect: wgpu::TextureAspect::All,
            },
            wgpu::Extent3d {
                width: ATLAS_SIZE,
                height: ATLAS_SIZE,
                depth_or_array_layers: self.layers,
            },
        );
        // Submit right away: queued texture writes issued afterwards are
        // ordered after this copy and cannot be overwritten by it.
        queue.submit(Some(encoder.finish()));
        *self = new;
        true
    }
}

impl AtlasTextures {
    pub fn new(device: &wgpu::Device) -> Self {
        AtlasTextures {
            mono: GpuLayers::new(
                device,
                wgpu::TextureFormat::R8Unorm,
                1,
                "kova-native mono atlas",
            ),
            color: GpuLayers::new(
                device,
                wgpu::TextureFormat::Rgba8Unorm,
                1,
                "kova-native color atlas",
            ),
        }
    }

    /// Uploads pending tiles. Returns `true` if textures were recreated (the
    /// bind group must then be rebuilt).
    pub fn sync(&mut self, device: &wgpu::Device, queue: &wgpu::Queue, atlas: &mut Atlas) -> bool {
        let mut recreated = self.mono.ensure(
            device,
            queue,
            atlas.layer_count(AtlasKind::Mono),
            "kova-native mono atlas",
        );
        recreated |= self.color.ensure(
            device,
            queue,
            atlas.layer_count(AtlasKind::Color),
            "kova-native color atlas",
        );
        for upload in atlas.take_uploads() {
            let target = match upload.kind {
                AtlasKind::Mono => &self.mono,
                AtlasKind::Color => &self.color,
            };
            queue.write_texture(
                wgpu::TexelCopyTextureInfo {
                    texture: &target.texture,
                    mip_level: 0,
                    origin: wgpu::Origin3d {
                        x: upload.x,
                        y: upload.y,
                        z: upload.layer,
                    },
                    aspect: wgpu::TextureAspect::All,
                },
                &upload.data,
                wgpu::TexelCopyBufferLayout {
                    offset: 0,
                    bytes_per_row: Some(upload.width * upload.kind.bytes_per_pixel()),
                    rows_per_image: Some(upload.height),
                },
                wgpu::Extent3d {
                    width: upload.width,
                    height: upload.height,
                    depth_or_array_layers: 1,
                },
            );
        }
        recreated
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn allocation_and_caching() {
        let mut atlas = Atlas::new();
        let key = AtlasKey::Image(ImageId(1));
        let mut builds = 0;
        for _ in 0..3 {
            let tile = atlas.get_or_insert_with(key, || {
                builds += 1;
                Some(AtlasImage {
                    kind: AtlasKind::Color,
                    width: 4,
                    height: 2,
                    data: Cow::Owned(vec![255; 32]),
                    origin: (0, 0),
                })
            });
            let tile = tile.unwrap();
            assert_eq!((tile.width, tile.height), (4, 2));
            assert!(tile.x >= PADDING && tile.y >= PADDING);
        }
        assert_eq!(builds, 1);
        assert_eq!(atlas.take_uploads().len(), 1);

        // Negative results are cached too.
        let empty = AtlasKey::Image(ImageId(2));
        assert!(atlas.get_or_insert_with(empty, || None).is_none());
        assert!(atlas.contains(&empty));
    }

    #[test]
    fn grows_new_layers_when_full() {
        let mut atlas = Atlas::new();
        let big = (ATLAS_SIZE - 2 * PADDING) as usize;
        for i in 0..3 {
            let tile = atlas
                .get_or_insert_with(AtlasKey::Image(ImageId(100 + i)), || {
                    Some(AtlasImage {
                        kind: AtlasKind::Mono,
                        width: big as u32,
                        height: big as u32,
                        data: Cow::Owned(vec![0; big * big]),
                        origin: (0, 0),
                    })
                })
                .unwrap();
            assert_eq!(tile.layer, i as u32);
        }
        assert_eq!(atlas.layer_count(AtlasKind::Mono), 3);
        atlas.remove(&AtlasKey::Image(ImageId(100)));
        assert!(!atlas.contains(&AtlasKey::Image(ImageId(100))));
    }
}