kova-native-render 0.3.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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
//! The scene: a flat, ordered list of GPU-ready primitives for one frame.
//!
//! All coordinates are in *device pixels*. The element tree converts logical
//! coordinates (applying the window scale factor and pixel snapping) while
//! painting.

use crate::atlas::{AtlasKind, AtlasTile};
use crate::shaders::ShaderId;
use bytemuck::{Pod, Zeroable};
use kova_native_core::{Bounds, Color, Corners, Edges, Fill, Point, Transform2D};
use std::ops::Range;

pub(crate) const KIND_QUAD: u32 = 0;
pub(crate) const KIND_SHADOW: u32 = 1;
pub(crate) const KIND_INSET_SHADOW: u32 = 2;
pub(crate) const KIND_MONO_SPRITE: u32 = 3;
pub(crate) const KIND_POLY_SPRITE: u32 = 4;
pub(crate) const KIND_BACKDROP: u32 = 5;
pub(crate) const KIND_LAYER: u32 = 6;
pub(crate) const KIND_CUSTOM: u32 = 7;

const FLAG_GRADIENT: u32 = 1;
const FLAG_GRAYSCALE: u32 = 2;
const MASK_GRADIENT: u32 = 1;
const MASK_MONO: u32 = 2;
const MASK_COLOR: u32 = 4;

/// One GPU instance. Layout must match `InstanceIn` in `shader.wgsl`.
#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub(crate) struct Instance {
    pub bounds: [f32; 4],
    pub radii: [f32; 4],
    pub border_widths: [f32; 4],
    pub uv: [f32; 4],
    pub clip_bounds: [f32; 4],
    pub clip_radii: [f32; 4],
    pub transform: [f32; 4],
    pub translate_params: [f32; 4],
    pub params: [f32; 4],
    pub color0: u32,
    pub color1: u32,
    pub border_color: u32,
    pub kind: u32,
}

/// Clip region (device pixels), optionally with rounded corners.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ContentMask {
    pub bounds: Bounds,
    pub radii: Corners<f32>,
}

impl ContentMask {
    pub const NONE: ContentMask = ContentMask {
        bounds: Bounds::INFINITE,
        radii: Corners::ZERO,
    };

    pub fn new(bounds: Bounds) -> Self {
        ContentMask {
            bounds,
            radii: Corners::ZERO,
        }
    }

    pub fn rounded(bounds: Bounds, radii: Corners<f32>) -> Self {
        ContentMask {
            bounds,
            radii: radii.clamp_to(bounds.size),
        }
    }

    /// Intersection of two masks. Only one set of corner radii can be
    /// represented, so the radii of the smaller (inner) mask are kept.
    pub fn intersect(&self, other: &ContentMask) -> ContentMask {
        let bounds = self.bounds.intersect(&other.bounds);
        let self_area = self.bounds.size.width * self.bounds.size.height;
        let other_area = other.bounds.size.width * other.bounds.size.height;
        let radii = if other_area <= self_area {
            other.radii
        } else {
            self.radii
        };
        ContentMask { bounds, radii }
    }
}

/// Transform, clip and group opacity applied to a primitive.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct DrawState {
    pub transform: Transform2D,
    pub clip: ContentMask,
    pub opacity: f32,
}

impl Default for DrawState {
    fn default() -> Self {
        DrawState {
            transform: Transform2D::IDENTITY,
            clip: ContentMask::NONE,
            opacity: 1.0,
        }
    }
}

/// A rounded rectangle with fill and border.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Quad {
    pub bounds: Bounds,
    pub radii: Corners<f32>,
    pub fill: Fill,
    pub border_widths: Edges<f32>,
    pub border_color: Color,
}

impl Quad {
    pub fn filled(bounds: Bounds, fill: impl Into<Fill>) -> Self {
        Quad {
            bounds,
            radii: Corners::ZERO,
            fill: fill.into(),
            border_widths: Edges::ZERO,
            border_color: Color::TRANSPARENT,
        }
    }

    pub fn radii(mut self, radii: impl Into<Corners<f32>>) -> Self {
        self.radii = radii.into();
        self
    }

    pub fn border(mut self, widths: impl Into<Edges<f32>>, color: Color) -> Self {
        self.border_widths = widths.into();
        self.border_color = color;
        self
    }
}

/// A box shadow (CSS semantics: `blur` is the blur radius, `spread` grows
/// the shadow shape).
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Shadow {
    /// Bounds of the element casting (or containing, for inset) the shadow.
    pub bounds: Bounds,
    pub radii: Corners<f32>,
    pub color: Color,
    pub offset: Point,
    pub blur: f32,
    pub spread: f32,
    pub inset: bool,
}

/// Effects that split the scene into multiple render passes.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Effect {
    /// Copy what has been drawn so far and blur it with this radius (device
    /// px); following backdrop primitives sample the result.
    CaptureBackdrop { blur: f32 },
}

/// How a layer opened with [`Scene::push_layer`] is composited onto what
/// lies below it.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum LayerMask {
    /// No mask: the layer is composited with group opacity only.
    None,
    /// Alpha ramps from `from` to `to` along `angle` (degrees, CSS
    /// convention: 0 = upwards, 90 = to the right) between the fractional
    /// positions `start` and `end` of the gradient line.
    LinearGradient {
        angle: f32,
        from: f32,
        to: f32,
        start: f32,
        end: f32,
    },
    /// An atlas tile stretched over the layer bounds. Mono tiles (glyphs,
    /// rasterized SVG masks) use their coverage, color tiles their alpha.
    Tile(AtlasTile),
}

/// One step of rendering a scene, in order.
#[derive(Clone, Debug, PartialEq)]
pub(crate) enum Op {
    /// Draws instances into the current target with the built-in pipeline
    /// (`shader == None`) or a registered custom shader.
    Draw {
        instances: Range<u32>,
        shader: Option<ShaderId>,
    },
    /// Blurs the current target into the backdrop texture.
    Effect(Effect),
    /// Starts drawing into a new transparent layer.
    PushLayer,
    /// Ends the current layer and composites it onto its parent with the
    /// instance at `composite` (if it is visible).
    PopLayer { composite: Option<u32> },
}

/// Statistics about a built scene.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct SceneStats {
    pub primitives: usize,
    pub culled: usize,
    pub draw_calls: usize,
}

/// An ordered list of primitives in painter's order.
#[derive(Default)]
pub struct Scene {
    pub(crate) instances: Vec<Instance>,
    ops: Vec<Op>,
    /// First instance of the run not yet recorded in `ops`.
    run_start: u32,
    /// Pipeline of the open run.
    run_shader: Option<ShaderId>,
    layer_depth: u32,
    culled: usize,
}

fn pack(c: Color) -> u32 {
    u32::from_le_bytes(c.to_rgba8())
}

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

    pub fn clear(&mut self) {
        self.instances.clear();
        self.ops.clear();
        self.run_start = 0;
        self.run_shader = None;
        self.layer_depth = 0;
        self.culled = 0;
    }

    /// Number of layers currently open (see [`Scene::push_layer`]).
    pub fn layer_depth(&self) -> u32 {
        self.layer_depth
    }

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

    pub fn is_empty(&self) -> bool {
        self.instances.is_empty()
    }

    pub fn stats(&self) -> SceneStats {
        let draw_calls = self
            .ops()
            .iter()
            .filter(|op| matches!(op, Op::Draw { .. } | Op::PopLayer { composite: Some(_) }))
            .count();
        SceneStats {
            primitives: self.instances.len(),
            culled: self.culled,
            draw_calls,
        }
    }

    /// The rendering steps, including the still open run of instances.
    pub(crate) fn ops(&self) -> Vec<Op> {
        let mut ops = self.ops.clone();
        let end = self.instances.len() as u32;
        if end > self.run_start {
            ops.push(Op::Draw {
                instances: self.run_start..end,
                shader: self.run_shader,
            });
        }
        ops
    }

    fn close_run(&mut self) {
        let end = self.instances.len() as u32;
        if end > self.run_start {
            self.ops.push(Op::Draw {
                instances: self.run_start..end,
                shader: self.run_shader,
            });
        }
        self.run_start = end;
    }

    /// Makes the next instance part of a run drawn with `shader`.
    fn begin(&mut self, shader: Option<ShaderId>) {
        if shader != self.run_shader {
            self.close_run();
            self.run_shader = shader;
        }
    }

    fn push(&mut self, inst: Instance) {
        self.begin(None);
        self.instances.push(inst);
    }

    /// Returns `false` (and counts the primitive as culled) if `local` bounds
    /// transformed by `state` fall entirely outside the clip.
    fn visible(&mut self, local: &Bounds, state: &DrawState) -> bool {
        if state.opacity <= 0.0 || state.clip.bounds.is_empty() {
            self.culled += 1;
            return false;
        }
        let world = state.transform.apply_bounds(local);
        if !world.intersects(&state.clip.bounds) {
            self.culled += 1;
            return false;
        }
        true
    }

    fn base(state: &DrawState, kind: u32) -> Instance {
        let t = &state.transform;
        let c = &state.clip;
        Instance {
            bounds: [0.0; 4],
            radii: [0.0; 4],
            border_widths: [0.0; 4],
            uv: [0.0; 4],
            clip_bounds: [
                c.bounds.origin.x,
                c.bounds.origin.y,
                c.bounds.size.width,
                c.bounds.size.height,
            ],
            clip_radii: [
                c.radii.top_left,
                c.radii.top_right,
                c.radii.bottom_right,
                c.radii.bottom_left,
            ],
            transform: [t.a, t.b, t.c, t.d],
            translate_params: [t.tx, t.ty, 0.0, 0.0],
            params: [0.0; 4],
            color0: 0,
            color1: 0,
            border_color: 0,
            kind,
        }
    }

    pub fn push_quad(&mut self, quad: &Quad, state: &DrawState) {
        if quad.bounds.is_empty() || !self.visible(&quad.bounds, state) {
            return;
        }
        let border_visible = !quad.border_widths.is_zero() && !quad.border_color.is_transparent();
        if quad.fill.is_transparent() && !border_visible {
            return;
        }
        let radii = quad.radii.clamp_to(quad.bounds.size);
        let mut inst = Self::base(state, KIND_QUAD);
        inst.bounds = rect(&quad.bounds);
        inst.radii = corners(&radii);
        if border_visible {
            let b = quad.border_widths;
            inst.border_widths = [b.top, b.right, b.bottom, b.left];
            inst.border_color = pack(quad.border_color.fade(state.opacity));
        }
        match quad.fill {
            Fill::Solid(c) => inst.color0 = pack(c.fade(state.opacity)),
            Fill::LinearGradient(g) => {
                inst.kind |= FLAG_GRADIENT << 8;
                inst.color0 = pack(g.from.color.fade(state.opacity));
                inst.color1 = pack(g.to.color.fade(state.opacity));
                inst.translate_params[2] = g.angle.to_radians();
                inst.translate_params[3] = g.from.position;
                inst.params[0] = g.to.position;
            }
        }
        self.push(inst);
    }

    pub fn push_shadow(&mut self, shadow: &Shadow, state: &DrawState) {
        if shadow.color.is_transparent() || shadow.bounds.is_empty() {
            return;
        }
        let sigma = (shadow.blur * 0.5).max(0.0);
        if shadow.inset {
            if !self.visible(&shadow.bounds, state) {
                return;
            }
            let radii = shadow.radii.clamp_to(shadow.bounds.size);
            let hole = shadow
                .bounds
                .translate(shadow.offset)
                .inflate(-shadow.spread);
            let hole_radii = radii.map(|r| (r - shadow.spread).max(0.0));
            let mut inst = Self::base(state, KIND_INSET_SHADOW);
            inst.bounds = rect(&shadow.bounds);
            inst.radii = corners(&radii);
            inst.uv = rect(&hole);
            inst.border_widths = corners(&hole_radii);
            inst.translate_params[2] = sigma;
            inst.color0 = pack(shadow.color.fade(state.opacity));
            self.push(inst);
        } else {
            let shape = shadow
                .bounds
                .translate(shadow.offset)
                .inflate(shadow.spread);
            if shape.is_empty() {
                return;
            }
            let radii = shadow
                .radii
                .clamp_to(shadow.bounds.size)
                .map(|r| (r + shadow.spread).max(0.0));
            if !self.visible(&shape.inflate(sigma * 3.0), state) {
                return;
            }
            let mut inst = Self::base(state, KIND_SHADOW);
            inst.bounds = rect(&shape);
            inst.radii = corners(&radii.clamp_to(shape.size));
            inst.translate_params[2] = sigma;
            inst.color0 = pack(shadow.color.fade(state.opacity));
            self.push(inst);
        }
    }

    /// Draws a coverage-mask sprite (glyph, tintable icon) in `color`.
    pub fn push_mono_sprite(
        &mut self,
        bounds: Bounds,
        tile: &AtlasTile,
        color: Color,
        state: &DrawState,
    ) {
        if color.is_transparent() || !self.visible(&bounds, state) {
            return;
        }
        let mut inst = Self::base(state, KIND_MONO_SPRITE | (tile.layer << 16));
        inst.bounds = rect(&bounds);
        inst.uv = tile.uv();
        inst.color0 = pack(color.fade(state.opacity));
        self.push(inst);
    }

    /// Draws a full color sprite (image, emoji) with optional rounded corners.
    pub fn push_poly_sprite(
        &mut self,
        bounds: Bounds,
        tile: &AtlasTile,
        radii: Corners<f32>,
        grayscale: bool,
        state: &DrawState,
    ) {
        if !self.visible(&bounds, state) {
            return;
        }
        let flags = if grayscale { FLAG_GRAYSCALE } else { 0 };
        let mut inst = Self::base(state, KIND_POLY_SPRITE | (flags << 8) | (tile.layer << 16));
        inst.bounds = rect(&bounds);
        inst.radii = corners(&radii.clamp_to(bounds.size));
        inst.uv = tile.uv();
        inst.color0 = pack(Color::WHITE.fade(state.opacity));
        self.push(inst);
    }

    /// Frosted glass: blurs everything drawn so far behind `bounds` and draws
    /// it clipped to the rounded rect, tinted with `tint`.
    pub fn push_backdrop_blur(
        &mut self,
        bounds: Bounds,
        radii: Corners<f32>,
        blur: f32,
        tint: Color,
        state: &DrawState,
    ) {
        if !self.visible(&bounds, state) {
            return;
        }
        let effect = Effect::CaptureBackdrop {
            blur: blur.max(0.0),
        };
        self.close_run();
        // Reuse the previous capture when nothing was drawn in between.
        if self.ops.last() != Some(&Op::Effect(effect)) {
            self.ops.push(Op::Effect(effect));
        }
        let mut inst = Self::base(state, KIND_BACKDROP);
        inst.bounds = rect(&bounds);
        inst.radii = corners(&radii.clamp_to(bounds.size));
        inst.color0 = pack(tint);
        inst.params[0] = state.opacity;
        self.push(inst);
    }

    /// Starts a layer: everything pushed until the matching
    /// [`Scene::pop_layer`] is drawn into a transparent offscreen target,
    /// then composited through a mask. Layers nest.
    pub fn push_layer(&mut self) {
        self.close_run();
        self.ops.push(Op::PushLayer);
        self.layer_depth += 1;
    }

    /// Ends the innermost layer and composites it inside `bounds` (device px,
    /// transformed and clipped by `state`) with `mask` and `state.opacity`.
    /// Content of the layer outside `bounds` is discarded, like CSS masks.
    pub fn pop_layer(&mut self, bounds: Bounds, mask: LayerMask, state: &DrawState) {
        if self.layer_depth == 0 {
            debug_assert!(false, "pop_layer without push_layer");
            return;
        }
        self.layer_depth -= 1;
        self.close_run();
        if bounds.is_empty() || !self.visible(&bounds, state) {
            self.ops.push(Op::PopLayer { composite: None });
            return;
        }
        let (flags, layer) = match mask {
            LayerMask::None => (0, 0),
            LayerMask::LinearGradient { .. } => (MASK_GRADIENT, 0),
            LayerMask::Tile(tile) => match tile.kind {
                AtlasKind::Mono => (MASK_MONO, tile.layer),
                AtlasKind::Color => (MASK_COLOR, tile.layer),
            },
        };
        let mut inst = Self::base(state, KIND_LAYER | (flags << 8) | (layer << 16));
        inst.bounds = rect(&bounds);
        let opacity = state.opacity.clamp(0.0, 1.0);
        inst.color0 = pack(Color::WHITE.with_alpha(opacity));
        match mask {
            LayerMask::None => {}
            LayerMask::LinearGradient {
                angle,
                from,
                to,
                start,
                end,
            } => {
                inst.color0 = pack(Color::WHITE.with_alpha(from.clamp(0.0, 1.0) * opacity));
                inst.color1 = pack(Color::WHITE.with_alpha(to.clamp(0.0, 1.0) * opacity));
                inst.translate_params[2] = angle.to_radians();
                inst.translate_params[3] = start;
                inst.params[0] = end;
            }
            LayerMask::Tile(tile) => inst.uv = tile.uv(),
        }
        let index = self.instances.len() as u32;
        self.instances.push(inst);
        self.run_start = self.instances.len() as u32;
        self.ops.push(Op::PopLayer {
            composite: Some(index),
        });
    }

    /// Draws `bounds` (rounded by `radii`) with a registered custom shader.
    /// `params` reach the shader as `input.params0`/`params1`, `time` as
    /// `input.time`, `color` as `input.color`; `scale` is the device scale
    /// (device px per logical px).
    #[allow(clippy::too_many_arguments)]
    pub fn push_custom(
        &mut self,
        shader: ShaderId,
        bounds: Bounds,
        radii: Corners<f32>,
        params: [f32; 8],
        time: f32,
        color: Color,
        scale: f32,
        state: &DrawState,
    ) {
        if bounds.is_empty() || !self.visible(&bounds, state) {
            return;
        }
        let mut inst = Self::base(state, KIND_CUSTOM);
        inst.bounds = rect(&bounds);
        inst.radii = corners(&radii.clamp_to(bounds.size));
        inst.uv = [params[0], params[1], params[2], params[3]];
        inst.border_widths = [params[4], params[5], params[6], params[7]];
        inst.translate_params[2] = time;
        inst.translate_params[3] = scale;
        inst.color0 = pack(Color::WHITE.with_alpha(state.opacity.clamp(0.0, 1.0)));
        inst.color1 = pack(color);
        self.begin(Some(shader));
        self.instances.push(inst);
    }
}

fn rect(b: &Bounds) -> [f32; 4] {
    [b.origin.x, b.origin.y, b.size.width, b.size.height]
}

fn corners(c: &Corners<f32>) -> [f32; 4] {
    [c.top_left, c.top_right, c.bottom_right, c.bottom_left]
}

#[cfg(test)]
mod tests {
    use super::*;
    use kova_native_core::{bounds, rgb};

    #[test]
    fn culls_offscreen_and_clipped() {
        let mut scene = Scene::new();
        let state = DrawState {
            clip: ContentMask::new(bounds(0.0, 0.0, 100.0, 100.0)),
            ..Default::default()
        };
        scene.push_quad(
            &Quad::filled(bounds(10.0, 10.0, 20.0, 20.0), rgb(0xff0000)),
            &state,
        );
        scene.push_quad(
            &Quad::filled(bounds(200.0, 10.0, 20.0, 20.0), rgb(0xff0000)),
            &state,
        );
        scene.push_quad(
            &Quad::filled(bounds(10.0, 10.0, 20.0, 20.0), Color::TRANSPARENT),
            &state,
        );
        assert_eq!(scene.len(), 1);
        assert_eq!(scene.stats().culled, 1);
        assert_eq!(scene.stats().draw_calls, 1);
    }

    #[test]
    fn mixed_primitives_share_one_segment() {
        let mut scene = Scene::new();
        let state = DrawState::default();
        let tile = AtlasTile {
            kind: crate::AtlasKind::Mono,
            layer: 0,
            x: 1,
            y: 1,
            width: 8,
            height: 8,
            origin: (0, 0),
        };
        for i in 0..1000 {
            let b = bounds(i as f32, 0.0, 10.0, 10.0);
            scene.push_quad(&Quad::filled(b, rgb(0x223344)).radii(4.0), &state);
            scene.push_mono_sprite(b, &tile, Color::WHITE, &state);
        }
        assert_eq!(scene.len(), 2000);
        assert_eq!(
            scene.stats().draw_calls,
            1,
            "thousands of primitives, one draw call"
        );
    }

    #[test]
    fn backdrop_splits_runs() {
        let mut scene = Scene::new();
        let state = DrawState::default();
        scene.push_quad(
            &Quad::filled(bounds(0.0, 0.0, 10.0, 10.0), rgb(0x112233)),
            &state,
        );
        scene.push_backdrop_blur(
            bounds(0.0, 0.0, 10.0, 10.0),
            Corners::all(4.0),
            12.0,
            Color::TRANSPARENT,
            &state,
        );
        scene.push_quad(
            &Quad::filled(bounds(0.0, 0.0, 5.0, 5.0), rgb(0x112233)),
            &state,
        );
        let ops = scene.ops();
        assert_eq!(
            ops,
            vec![
                Op::Draw {
                    instances: 0..1,
                    shader: None
                },
                Op::Effect(Effect::CaptureBackdrop { blur: 12.0 }),
                Op::Draw {
                    instances: 1..3,
                    shader: None
                },
            ]
        );
        assert_eq!(scene.stats().draw_calls, 2);
    }

    #[test]
    fn layers_and_custom_shaders_split_runs() {
        let mut scene = Scene::new();
        let state = DrawState::default();
        let quad = Quad::filled(bounds(0.0, 0.0, 10.0, 10.0), rgb(0x112233));
        let shader = ShaderId::from_raw(0);
        scene.push_quad(&quad, &state);
        scene.push_layer();
        scene.push_quad(&quad, &state);
        scene.push_quad(&quad, &state);
        scene.pop_layer(
            bounds(0.0, 0.0, 10.0, 10.0),
            LayerMask::LinearGradient {
                angle: 180.0,
                from: 1.0,
                to: 0.0,
                start: 0.0,
                end: 1.0,
            },
            &state,
        );
        scene.push_custom(
            shader,
            bounds(0.0, 0.0, 4.0, 4.0),
            Corners::ZERO,
            [0.0; 8],
            0.0,
            Color::WHITE,
            1.0,
            &state,
        );
        scene.push_custom(
            shader,
            bounds(4.0, 0.0, 4.0, 4.0),
            Corners::ZERO,
            [0.0; 8],
            0.0,
            Color::WHITE,
            1.0,
            &state,
        );
        scene.push_quad(&quad, &state);
        assert_eq!(scene.layer_depth(), 0);
        assert_eq!(
            scene.ops(),
            vec![
                Op::Draw {
                    instances: 0..1,
                    shader: None
                },
                Op::PushLayer,
                Op::Draw {
                    instances: 1..3,
                    shader: None
                },
                Op::PopLayer { composite: Some(3) },
                Op::Draw {
                    instances: 4..6,
                    shader: Some(shader)
                },
                Op::Draw {
                    instances: 6..7,
                    shader: None
                },
            ]
        );
    }

    #[test]
    fn instance_layout_is_stable() {
        assert_eq!(std::mem::size_of::<Instance>(), 160);
    }
}