1use std::mem::replace;
2
3#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
4pub struct AtlasRect {
5 pub x: u32,
6 pub y: u32,
7 pub width: u32,
8 pub height: u32,
9}
10
11struct Shelf {
12 y: u32,
13 height: u32,
14 cursor: u32,
15}
16
17pub struct Atlas {
18 height: u32,
19 pixels: Vec<u8>,
20 shelves: Vec<Shelf>,
21 dirty_rows: Option<(u32, u32)>,
22 uploaded: u32,
23}
24
25#[derive(Clone, Debug, Default, PartialEq)]
26pub struct AtlasUploads {
27 pub height: u32,
28 pub start: u32,
29 pub rows: Vec<u8>,
30}
31
32impl AtlasUploads {
33 pub fn is_empty(&self) -> bool {
34 self.rows.is_empty()
35 }
36}
37
38pub(crate) const INITIAL_HEIGHT: u32 = 512;
39const PADDING: u32 = 1;
40
41impl Default for Atlas {
42 fn default() -> Self {
43 Self::new()
44 }
45}
46
47impl Atlas {
48 pub const MAX_HEIGHT: u32 = 8192;
49 pub const WIDTH: u32 = 2048;
50
51 pub fn new() -> Self {
52 Atlas {
53 height: INITIAL_HEIGHT,
54 pixels: vec![0; (Self::WIDTH * INITIAL_HEIGHT) as usize],
55 shelves: Vec::new(),
56 dirty_rows: None,
57 uploaded: 0,
58 }
59 }
60
61 pub fn size(&self) -> (u32, u32) {
62 (Self::WIDTH, self.height)
63 }
64
65 pub fn take_uploads(&mut self) -> AtlasUploads {
66 let (start, end) = self.dirty_rows.take().unwrap_or_default();
67 let start = if replace(&mut self.uploaded, self.height) == self.height {
68 start
69 } else {
70 0
71 };
72 AtlasUploads {
73 height: self.height,
74 start,
75 rows: self.pixels[(start * Self::WIDTH) as usize..(end * Self::WIDTH) as usize]
76 .to_vec(),
77 }
78 }
79
80 pub fn insert(&mut self, width: u32, height: u32, data: &[u8]) -> Option<AtlasRect> {
81 assert_eq!(data.len(), (width * height) as usize, "mask size mismatch");
82 if width == 0 || height == 0 || width + PADDING > Self::WIDTH {
83 return None;
84 }
85 let (x, y) = self.allocate(width, height)?;
86 for (row, line) in data.chunks(width as usize).enumerate() {
87 let dst = ((y + row as u32) * Self::WIDTH + x) as usize;
88 self.pixels[dst..dst + width as usize].copy_from_slice(line);
89 }
90 let (lo, hi) = self.dirty_rows.unwrap_or((y, y + height));
91 self.dirty_rows = Some((lo.min(y), hi.max(y + height)));
92 Some(AtlasRect {
93 x,
94 y,
95 width,
96 height,
97 })
98 }
99
100 fn allocate(&mut self, width: u32, height: u32) -> Option<(u32, u32)> {
101 let best = self
102 .shelves
103 .iter_mut()
104 .filter(|s| {
105 s.height >= height
106 && s.height <= height + height / 4 + 2
107 && s.cursor + width <= Self::WIDTH
108 })
109 .min_by_key(|s| s.height);
110 if let Some(shelf) = best {
111 let x = shelf.cursor;
112 shelf.cursor += width + PADDING;
113 return Some((x, shelf.y));
114 }
115 let y = self.shelves.last().map_or(0, |s| s.y + s.height + PADDING);
116 while y + height > self.height {
117 if self.height >= Self::MAX_HEIGHT {
118 return None;
119 }
120 self.height = (self.height * 2).min(Self::MAX_HEIGHT);
121 self.pixels.resize((Self::WIDTH * self.height) as usize, 0);
122 }
123 self.shelves.push(Shelf {
124 y,
125 height,
126 cursor: width + PADDING,
127 });
128 Some((0, y))
129 }
130}