1use crate::atlas::AtlasRect;
2use crate::image::{ImageData, ImageFormat};
3use crate::{Color, Corners, Edges, Point, Rect, Size};
4use bytemuck::{Pod, Zeroable};
5use slotmap::{SlotMap, new_key_type};
6use std::ops::Range;
7use std::sync::atomic::{AtomicU64, Ordering};
8use wgpu::{VertexAttribute, vertex_attr_array};
9
10static NEXT_LIST: AtomicU64 = AtomicU64::new(1);
11
12new_key_type! {
13 pub struct ListKey;
14}
15
16#[derive(Default)]
17pub struct DrawLists(SlotMap<ListKey, DrawList>);
18
19impl DrawLists {
20 pub fn new() -> Self {
21 Self::default()
22 }
23
24 pub fn insert(&mut self, list: DrawList) -> ListKey {
25 self.0.insert(list)
26 }
27
28 pub fn get(&self, key: ListKey) -> Option<&DrawList> {
29 self.0.get(key)
30 }
31
32 pub fn get_mut(&mut self, key: ListKey) -> Option<&mut DrawList> {
33 self.0.get_mut(key)
34 }
35
36 pub fn remove(&mut self, key: ListKey) -> Option<DrawList> {
37 self.0.remove(key)
38 }
39}
40
41#[derive(Clone, Copy, Debug, PartialEq)]
42pub struct Quad {
43 pub bounds: Rect,
44 pub clip: Rect,
45 pub background: Color,
46 pub border_color: Color,
47 pub corner_radii: Corners,
48 pub border_widths: Edges,
49 pub backing: bool,
50}
51
52impl Quad {
53 pub const fn new(bounds: Rect, background: Color) -> Self {
54 Quad {
55 bounds,
56 clip: Rect::EVERYTHING,
57 background,
58 border_color: Color::TRANSPARENT,
59 corner_radii: Corners::ZERO,
60 border_widths: Edges::ZERO,
61 backing: false,
62 }
63 }
64
65 pub const fn backing(bounds: Rect) -> Self {
66 Quad {
67 backing: true,
68 ..Quad::new(bounds, Color::TRANSPARENT)
69 }
70 }
71
72 pub fn line(bounds: Rect, color: Color) -> Self {
73 let Size { width, height } = bounds.size;
74 let border_widths = if height <= width {
75 Edges {
76 top: height,
77 ..Edges::ZERO
78 }
79 } else {
80 Edges {
81 left: width,
82 ..Edges::ZERO
83 }
84 };
85 Quad {
86 border_color: color,
87 border_widths,
88 ..Quad::new(bounds, Color::TRANSPARENT)
89 }
90 }
91}
92
93#[derive(Clone, Copy, Debug, PartialEq)]
94pub struct Shadow {
95 pub bounds: Rect,
96 pub clip: Rect,
97 pub corner_radii: Corners,
98 pub color: Color,
99 pub blur_radius: f32,
100 pub backing: bool,
101}
102
103impl Shadow {
104 pub const fn new(bounds: Rect, color: Color, blur_radius: f32) -> Self {
105 Shadow {
106 bounds,
107 clip: Rect::EVERYTHING,
108 corner_radii: Corners::ZERO,
109 color,
110 blur_radius,
111 backing: false,
112 }
113 }
114}
115
116#[derive(Clone, Copy, Debug, PartialEq)]
117pub struct Glyph {
118 pub origin: Point,
119 pub clip: Rect,
120 pub color: Color,
121 pub mask: AtlasRect,
122}
123
124#[derive(Clone, Debug, PartialEq)]
125pub struct ImageQuad {
126 pub bounds: Rect,
127 pub clip: Rect,
128 pub corner_radii: Corners,
129 pub source: Rect,
130 pub tint: Color,
131 pub image: ImageData,
132}
133
134impl ImageQuad {
135 pub const fn new(bounds: Rect, image: ImageData) -> Self {
136 ImageQuad {
137 bounds,
138 clip: Rect::EVERYTHING,
139 corner_radii: Corners::ZERO,
140 source: Rect::new(0.0, 0.0, 1.0, 1.0),
141 tint: Color::WHITE,
142 image,
143 }
144 }
145}
146
147pub(crate) struct Batch {
148 pub image: Option<ImageData>,
149 pub instances: Range<u32>,
150}
151
152const SHADOW: u32 = 1;
153const GLYPH: u32 = 2;
154const IMAGE: u32 = 3;
155const LAYER_FILL: u32 = 4;
156const LAYER_RADII: u32 = 8;
157const ALPHA_MASK: u32 = 16;
158
159#[repr(C)]
160#[derive(Clone, Copy, Default, Pod, Zeroable)]
161pub(crate) struct Instance {
162 bounds: [f32; 4],
163 clip: [f32; 4],
164 radii: [f32; 4],
165 params: [f32; 4],
166 color: [u16; 4],
167 border_color: [u16; 4],
168 flags: u32,
169}
170
171impl Instance {
172 pub(crate) const ATTRIBUTES: [VertexAttribute; 7] = vertex_attr_array![
173 0 => Float32x4,
174 1 => Float32x4,
175 2 => Float32x4,
176 3 => Float32x4,
177 4 => Unorm16x4,
178 5 => Unorm16x4,
179 6 => Uint32,
180 ];
181 pub(crate) const SIZE: u64 = size_of::<Instance>() as u64;
182}
183
184fn unorm(color: Color) -> [u16; 4] {
185 color
186 .to_array()
187 .map(|channel| (channel.clamp(0.0, 1.0) * f32::from(u16::MAX)).round() as u16)
188}
189
190pub struct DrawList {
191 id: u64,
192 instances: Vec<Instance>,
193 batches: Vec<Batch>,
194 extent: Option<Rect>,
195 revision: u64,
196}
197
198impl Default for DrawList {
199 fn default() -> Self {
200 DrawList {
201 id: NEXT_LIST.fetch_add(1, Ordering::Relaxed),
202 instances: Vec::new(),
203 batches: Vec::new(),
204 extent: None,
205 revision: 0,
206 }
207 }
208}
209
210impl DrawList {
211 pub fn new() -> Self {
212 Self::default()
213 }
214
215 pub fn clear(&mut self) {
216 self.instances.clear();
217 self.batches.clear();
218 self.extent = None;
219 self.revision += 1;
220 }
221
222 pub fn replace(&mut self, other: DrawList) {
223 self.instances = other.instances;
224 self.batches = other.batches;
225 self.extent = other.extent;
226 self.revision += 1;
227 }
228
229 pub(crate) fn id(&self) -> u64 {
230 self.id
231 }
232
233 pub fn extent(&self) -> Option<Rect> {
234 self.extent
235 }
236
237 pub fn revision(&self) -> u64 {
238 self.revision
239 }
240
241 pub fn is_empty(&self) -> bool {
242 self.instances.is_empty()
243 }
244
245 pub fn len(&self) -> usize {
246 self.instances.len()
247 }
248
249 pub fn push_quad(&mut self, quad: Quad) {
250 if quad.bounds.is_empty() || quad.clip.intersect(&quad.bounds).is_empty() {
251 return;
252 }
253 let backing = if quad.backing {
254 LAYER_FILL | LAYER_RADII
255 } else {
256 0
257 };
258 self.push(
259 None,
260 quad.bounds.inflate(1.0).intersect(&quad.clip),
261 Instance {
262 bounds: quad.bounds.to_array(),
263 clip: quad.clip.to_array(),
264 radii: quad.corner_radii.to_array(),
265 params: quad.border_widths.to_array(),
266 color: unorm(quad.background),
267 border_color: unorm(quad.border_color),
268 flags: backing,
269 },
270 );
271 }
272
273 pub fn push_shadow(&mut self, shadow: Shadow) {
274 if shadow.color.a <= 0.0 {
275 return;
276 }
277 let reach = shadow.blur_radius.max(0.0) * 1.5 + 2.0;
278 self.push(
279 None,
280 shadow.bounds.inflate(reach).intersect(&shadow.clip),
281 Instance {
282 bounds: shadow.bounds.to_array(),
283 clip: shadow.clip.to_array(),
284 radii: shadow.corner_radii.to_array(),
285 params: [shadow.blur_radius.max(0.0) * 0.5, 0.0, 0.0, 0.0],
286 color: unorm(shadow.color),
287 flags: SHADOW | if shadow.backing { LAYER_RADII } else { 0 },
288 ..Default::default()
289 },
290 );
291 }
292
293 pub fn push_glyph(&mut self, glyph: Glyph) {
294 if glyph.color.a <= 0.0 {
295 return;
296 }
297 let mask = glyph.mask;
298 let reach = Rect {
299 origin: glyph.origin,
300 size: Size::new(mask.width as f32, mask.height as f32),
301 };
302 self.push(
303 None,
304 reach.inflate(1.0).intersect(&glyph.clip),
305 Instance {
306 bounds: [glyph.origin.x, glyph.origin.y, 0.0, 0.0],
307 clip: glyph.clip.to_array(),
308 params: [
309 mask.x as f32,
310 mask.y as f32,
311 mask.width as f32,
312 mask.height as f32,
313 ],
314 color: unorm(glyph.color),
315 flags: GLYPH,
316 ..Default::default()
317 },
318 );
319 }
320
321 pub fn push_image(&mut self, image: ImageQuad) {
322 let ImageQuad {
323 bounds,
324 clip,
325 corner_radii,
326 source,
327 tint,
328 image,
329 } = image;
330 if bounds.is_empty() || clip.intersect(&bounds).is_empty() || tint.a <= 0.0 {
331 return;
332 }
333 let mask = if image.format() == ImageFormat::Alpha8 {
334 ALPHA_MASK
335 } else {
336 0
337 };
338 self.push(
339 Some(image),
340 bounds.inflate(1.0).intersect(&clip),
341 Instance {
342 bounds: bounds.to_array(),
343 clip: clip.to_array(),
344 radii: corner_radii.to_array(),
345 params: source.to_array(),
346 color: unorm(tint),
347 flags: IMAGE | mask,
348 ..Default::default()
349 },
350 );
351 }
352
353 fn push(&mut self, image: Option<ImageData>, reach: Rect, instance: Instance) {
354 let index = self.instances.len() as u32;
355 match self.batches.last_mut().filter(|batch| batch.image == image) {
356 Some(batch) => batch.instances.end = index + 1,
357 None => self.batches.push(Batch {
358 image,
359 instances: index..index + 1,
360 }),
361 }
362 self.instances.push(instance);
363 self.extent = Some(self.extent.map_or(reach, |extent| extent.union(&reach)));
364 self.revision += 1;
365 }
366
367 pub(crate) fn instances(&self) -> &[Instance] {
368 &self.instances
369 }
370
371 pub(crate) fn batches(&self) -> &[Batch] {
372 &self.batches
373 }
374}