latkit 0.2.0

Columnar data drawn as vertices, edges, lines, and areas on the GPU.
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
//! A frame's passes: compute evaluates dirty channels over the row blocks that changed, eases what
//! marks show toward what changed, reduces fitted domains, numbers segments, sums
//! face normals when their corners moved, and measures extents when asked; render draws the
//! backdrop, every mark's primitive and ports in scene order with depth, the labels, and the
//! overlay; a probe, when one waits, reads what the frame drew through the same geometry.

use super::resources::{EyeSlot, MARK_STRIDE};
use super::{Frame, View};
use crate::Space;
use crate::derive::stamp_of;
use crate::gpu::Inner;
use crate::gpu::pipelines::Variant;
use crate::gpu::readback::Readback;
use crate::gpu::shaders::{self as wgsl, Shader};
use crate::gpu::work::Count;
use crate::scene::mark::{MarkKind, Primitive};

const WORKGROUP: u32 = 256;
/// Workgroups one dimension of a dispatch holds on every device.
const MAX_GROUPS: u32 = 65_535;

/// The target pixels of `area`: a plot's lines draw only inside its axes.
fn scissor(area: super::camera::Area, frame: &Frame<'_>) -> [u32; 4] {
    let [w, h] = frame.size.map(f64::from);
    let r = f64::from(frame.pixel_ratio);
    let x0 = (area[0] * r).floor().clamp(0.0, w);
    let y0 = (area[1] * r).floor().clamp(0.0, h);
    let x1 = (area[2] * r).ceil().clamp(x0, w);
    let y1 = (area[3] * r).ceil().clamp(y0, h);
    [x0 as u32, y0 as u32, (x1 - x0) as u32, (y1 - y0) as u32]
}

/// The main render pass: cleared to `background`, multisampled into `view` and resolved into
/// `resolve` when there is one, its depth kept when `probing` reads it.
fn main_pass<'e>(
    encoder: &'e mut wgpu::CommandEncoder,
    view: &wgpu::TextureView,
    resolve: Option<&wgpu::TextureView>,
    depth: &wgpu::TextureView,
    background: [f64; 4],
    probing: bool,
) -> wgpu::RenderPass<'e> {
    let [r, g, b, a] = background;
    encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
        label: Some("latkit"),
        color_attachments: &[Some(wgpu::RenderPassColorAttachment {
            view,
            depth_slice: None,
            resolve_target: resolve,
            ops: wgpu::Operations {
                load: wgpu::LoadOp::Clear(wgpu::Color { r, g, b, a }),
                store: if resolve.is_some() { wgpu::StoreOp::Discard } else { wgpu::StoreOp::Store },
            },
        })],
        depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
            view: depth,
            depth_ops: Some(wgpu::Operations {
                load: wgpu::LoadOp::Clear(0.0),
                store: if probing { wgpu::StoreOp::Store } else { wgpu::StoreOp::Discard },
            }),
            stencil_ops: None,
        }),
        timestamp_writes: None,
        occlusion_query_set: None,
        multiview_mask: None,
    })
}

/// The draws a frame recorded, and the instances they draw: at most, for a draw its segment list
/// counts on the GPU.
#[derive(Clone, Copy, Default, Debug)]
pub(super) struct Drawn {
    pub(super) draws: u64,
    pub(super) instances: u64,
}

impl Drawn {
    /// Draws `instances` of `corners` corners each, counted.
    pub(super) fn draw(&mut self, pass: &mut wgpu::RenderPass<'_>, corners: u32, instances: u32) {
        pass.draw(0..corners, 0..instances);
        self.count(instances);
    }

    fn count(&mut self, instances: u32) {
        self.draws += 1;
        self.instances += u64::from(instances);
    }
}

/// What every mark's draw in the main pass shares: the device and target, the bind groups, the
/// lists buffer, and a plot's axes, which its lines clip to.
struct Marks<'a> {
    device: &'a wgpu::Device,
    format: wgpu::TextureFormat,
    samples: u32,
    groups: &'a super::resources::Groups,
    passes: &'a [(Shader, wgpu::BindGroup)],
    lists: &'a wgpu::Buffer,
    clip: Option<[u32; 4]>,
    size: [u32; 2],
}

impl Marks<'_> {
    /// Draws mark `i`, `shown`: every instance, or with `lit` the rows its focus lights, glowing.
    fn draw(
        &self,
        pass: &mut wgpu::RenderPass<'_>,
        drawn: &mut Drawn,
        inner: &mut Inner,
        i: usize,
        shown: &super::marks::Shown,
        lit: bool,
    ) {
        let Some(compiled) = &shown.compiled else { return };
        let custom = compiled.drawing.markers.custom.is_some();
        let areas = shown.path() == wgsl::PATH_GEOMETRY;
        let (shader, corners) = match (compiled.drawing.primitive, lit) {
            (Primitive::Points, false) if custom => (Shader::PointsCustom, 4),
            (Primitive::Points, false) => (Shader::Points, 4),
            (Primitive::Points, true) if custom => (Shader::PointsCustomFocus, 4),
            (Primitive::Points, true) => (Shader::PointsFocus, 4),
            (Primitive::Strokes, false) => (Shader::Strokes, 4),
            (Primitive::Strokes, true) => (Shader::StrokesFocus, 4),
            (Primitive::Triangles, false) if areas => (Shader::Areas, 3),
            (Primitive::Triangles, false) => (Shader::Triangles, 3),
            (Primitive::Triangles, true) if areas => (Shader::AreasFocus, 3),
            // A face lights by its color alone.
            (Primitive::Triangles, true) => return,
        };
        let pipeline = if let Some(source) = &compiled.drawing.markers.custom {
            inner.pipelines.custom(self.device, source, shader, self.format, self.samples)
        } else {
            let variant = Variant::draw(shader, self.format, self.samples).path(shown.path());
            Some(inner.pipelines.render_pipeline(self.device, variant))
        };
        let Some(pipeline) = pipeline else { return };
        let group = &self.passes.iter().find(|(s, _)| *s == shader).expect("listed").1;
        pass.set_bind_group(1, &self.groups.mark, &[offset(i)]);
        let clip = self.clip.filter(|_| shown.kind() == MarkKind::Lines);
        if let Some([x, y, w, h]) = clip {
            pass.set_scissor_rect(x, y, w, h);
        }
        pass.set_pipeline(&pipeline);
        pass.set_bind_group(2, group, &[]);
        if lit {
            drawn.draw(pass, corners, shown.lit.rows * shown.lit.pieces);
        } else {
            shown.record(pass, drawn, corners, self.lists);
        }
        if clip.is_some() {
            pass.set_scissor_rect(0, 0, self.size[0], self.size[1]);
        }
    }

    /// Draws the ports of every row of mark `i`, `shown`, where its rows have them: after its rows, and
    /// again after the rows its focus lights, which they sit on.
    fn ports(
        &self,
        pass: &mut wgpu::RenderPass<'_>,
        drawn: &mut Drawn,
        inner: &mut Inner,
        i: usize,
        shown: &super::marks::Shown,
    ) {
        let ports = shown.port_count();
        if ports == 0 || !shown.compiled.as_ref().is_some_and(|c| c.drawing.primitive == Primitive::Points) {
            return;
        }
        let group = &self.passes.iter().find(|(s, _)| *s == Shader::PointsPorts).expect("listed").1;
        pass.set_bind_group(1, &self.groups.mark, &[offset(i)]);
        pass.set_pipeline(&inner.pipelines.render(self.device, Shader::PointsPorts, self.format, self.samples));
        pass.set_bind_group(2, group, &[]);
        drawn.draw(pass, 4, (shown.rows + shown.exits) * ports);
    }
}

/// What a frame's compute pass does.
struct Work {
    evaluated: Vec<usize>,
    /// Marks whose ease pass writes what they show from their frozen and settled state.
    eased: Vec<usize>,
    segments: Vec<usize>,
    faces: Vec<usize>,
    measured: Vec<usize>,
    /// Marks whose comets carry their travel over to rows a transition moved, and that travel.
    carried: Vec<usize>,
    travelled: Vec<usize>,
}

/// Dispatches a workgroup for every `WORKGROUP` of `items`, if there are any; past one dimension's
/// workgroups, blocks of `BLOCK_ROWS` items along the second, as `item` in `lib/types.wesl` reads them.
pub(super) fn dispatch(pass: &mut wgpu::ComputePass<'_>, items: u32) {
    let groups = items.div_ceil(WORKGROUP);
    if groups > MAX_GROUPS {
        pass.dispatch_workgroups(wgsl::BLOCK_ROWS / WORKGROUP, items.div_ceil(wgsl::BLOCK_ROWS), 1);
    } else if groups > 0 {
        pass.dispatch_workgroups(groups, 1, 1);
    }
}

/// Dispatches over `blocks` listed row blocks of a mark of `rows` rows: a block's rows wide.
fn dispatch_blocks(pass: &mut wgpu::ComputePass<'_>, rows: u32, blocks: u32) {
    let wide = rows.min(wgsl::BLOCK_ROWS).div_ceil(WORKGROUP);
    if wide > 0 && blocks > 0 {
        pass.dispatch_workgroups(wide, blocks, 1);
    }
}

/// Mark `index`'s dynamic offset in the marks' uniforms.
pub(super) fn offset(index: usize) -> u32 {
    (index as u64 * MARK_STRIDE) as u32
}

impl super::marks::Shown {
    /// Segments of each row's path, for strokes whose rows all have as many.
    pub(crate) fn segments_per_row(&self) -> u32 {
        if self.compiled.is_none() {
            return 0;
        }
        match self.kind() {
            // A trace's points count the corners steps make.
            MarkKind::Lines => self.tables.traced().saturating_sub(1),
            _ => 1,
        }
    }

    /// Instances its primitive draws: rows and exits, or every segment of their paths; at most what
    /// its segment list keeps.
    pub(crate) fn instances(&self) -> u32 {
        if let Some(list) = &self.segments.list {
            return list.kept;
        }
        let rows = self.rows + self.exits;
        match self.compiled.as_ref().map(|c| c.drawing.primitive) {
            Some(Primitive::Strokes) => rows * self.segments_per_row(),
            Some(_) => rows,
            None => 0,
        }
    }

    /// Instances a probe or a measure reaches: live rows and their ports only.
    pub(crate) fn live_instances(&self) -> u32 {
        if let Some(list) = &self.segments.list {
            return list.kept;
        }
        match self.compiled.as_ref().map(|c| c.drawing.primitive) {
            Some(Primitive::Strokes) => self.rows * self.segments_per_row(),
            Some(Primitive::Points) => self.rows * (1 + self.port_count()),
            Some(_) => self.rows,
            None => 0,
        }
    }

    /// The code of the primitive it draws.
    pub(crate) fn primitive(&self) -> u32 {
        self.compiled.as_ref().map_or(0, |c| c.drawing.primitive.code())
    }

    /// Records the draw of its instances: as many as its segment list holds, when it has one.
    pub(super) fn record(
        &self,
        pass: &mut wgpu::RenderPass<'_>,
        drawn: &mut Drawn,
        corners: u32,
        lists: &wgpu::Buffer,
    ) {
        match &self.segments.list {
            Some(list) => {
                pass.draw_indirect(lists, u64::from(list.header + wgsl::LIST_DRAW) * 4);
                drawn.count(list.kept);
            }
            None => drawn.draw(pass, corners, self.instances()),
        }
    }
}

impl View {
    /// Evaluates the dirty channels of marks `work`, each over its dirty row blocks, its fitted
    /// domains reduced first where they changed.
    fn evaluate(
        &mut self,
        pass: &mut wgpu::ComputePass<'_>,
        inner: &mut Inner,
        group: &wgpu::BindGroup,
        work: &[usize],
    ) {
        if work.is_empty() {
            return;
        }
        let device = &self.gpu.0.device;
        let evaluate = inner.pipelines.compute(device, Shader::Evaluate, "evaluate");
        // Only marks fitting domains reduce.
        let reducing = work.iter().any(|&i| self.marks[i].reduce && self.marks[i].rows > 0);
        let reduce = reducing.then(|| inner.pipelines.compute(device, Shader::Evaluate, "reduce"));
        for &i in work {
            let shown = &mut self.marks[i];
            pass.set_bind_group(1, group, &[offset(i)]);
            if let Some(reduce) = reduce.as_ref().filter(|_| shown.reduce && shown.rows > 0) {
                pass.set_pipeline(reduce);
                dispatch(pass, shown.rows);
            }
            pass.set_pipeline(&evaluate);
            let rows = shown.rows + shown.exits;
            dispatch_blocks(pass, rows, shown.block_count);
            let blocks = shown.blocks.of(rows);
            let evaluated = blocks.iter().map(|&b| u64::from((rows - b * wgsl::BLOCK_ROWS).min(wgsl::BLOCK_ROWS)));
            self.gpu.0.counters.add(Count::Evaluated, evaluated.sum());
            shown.dirty = 0;
            shown.blocks = super::marks::Blocks::default();
            shown.reduce = false;
        }
    }

    /// Evaluates dirty channels, and sums face normals when what they rest on changed; with
    /// `measure`, also measures every measured mark's extents and reads them back. With `travel`,
    /// comets travel on by the frame's step.
    pub(super) fn compute(
        &mut self,
        encoder: &mut wgpu::CommandEncoder,
        inner: &mut Inner,
        measure: bool,
        travel: bool,
    ) -> Option<Readback> {
        let gpu = self.gpu.clone();
        let device = &gpu.0.device;
        let Work { evaluated: work, eased, segments, faces, measured, carried, travelled } = self.work(measure, travel);
        if [&work, &eased, &segments, &faces, &measured, &carried, &travelled].iter().all(|marks| marks.is_empty()) {
            return None;
        }
        let resources = self.resources.as_mut().expect("created before passes");
        for &i in &work {
            let shown = &self.marks[i];
            if shown.reduce {
                let bytes = u64::from(shown.reductions()) * 8;
                encoder.clear_buffer(resources.domains(), u64::from(shown.domains) * 8, Some(bytes));
            }
        }
        let extent_bytes = self.marks.len() as u64 * u64::from(wgsl::EXTENT_WORDS) * 4;
        if !measured.is_empty() {
            encoder.clear_buffer(resources.extents(), 0, Some(extent_bytes));
        }
        if !faces.is_empty() {
            encoder.clear_buffer(resources.normals(), 0, None);
        }
        let groups = resources.groups(&gpu.0, inner);
        let evaluate_group = resources.pass(device, inner, Shader::Evaluate);
        let normals_group = (!faces.is_empty()).then(|| resources.pass(device, inner, Shader::Normals));
        let bounds_group = (!measured.is_empty()).then(|| resources.pass(device, inner, Shader::Bounds));
        let lists_group = (!segments.is_empty()).then(|| resources.pass(device, inner, Shader::Lists));
        for &i in &segments {
            let list = self.marks[i].segments.list.expect("listed");
            gpu.0.queue.write_buffer(
                resources.lists(),
                u64::from(list.header) * 4,
                bytemuck::cast_slice(&list.header_words(self.marks[i].corners())),
            );
        }
        {
            let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
                label: Some("latkit compute"),
                timestamp_writes: None,
            });
            pass.set_bind_group(0, &groups.view, &[EyeSlot::Main.offset()]);
            pass.set_bind_group(2, &evaluate_group, &[]);
            self.every_row(&mut pass, inner, &groups.mark, "carry", &carried);
            self.evaluate(&mut pass, inner, &groups.mark, &work);
            // After evaluating, so what a mark shows eases toward what it evaluated this frame.
            self.every_row(&mut pass, inner, &groups.mark, "ease", &eased);
            // After evaluating, so comets travel at the flow this frame shows.
            self.every_row(&mut pass, inner, &groups.mark, "travel", &travelled);
            if let Some(group) = &lists_group {
                pass.set_bind_group(2, group, &[]);
                self.count_segments(&mut pass, inner, &groups.mark, &segments);
            }
            if let Some(group) = &normals_group {
                pass.set_pipeline(&inner.pipelines.compute(device, Shader::Normals, "accumulate"));
                pass.set_bind_group(2, group, &[]);
                for &i in &faces {
                    pass.set_bind_group(1, &groups.mark, &[offset(i)]);
                    dispatch(&mut pass, self.marks[i].rows);
                }
            }
            if let Some(group) = &bounds_group {
                pass.set_bind_group(2, group, &[]);
                for &i in &measured {
                    let entry = match self.marks[i].kind() {
                        MarkKind::Vertices => "points",
                        MarkKind::Areas => "areas",
                        _ => "strokes",
                    };
                    let variant = Variant::compute(Shader::Bounds, entry).path(self.marks[i].path());
                    pass.set_pipeline(&inner.pipelines.compute_pipeline(device, variant));
                    pass.set_bind_group(1, &groups.mark, &[offset(i)]);
                    dispatch(&mut pass, self.marks[i].live_instances());
                }
            }
        }
        let resources = self.resources.as_ref().expect("created before passes");
        (!measured.is_empty()).then(|| {
            Readback::new(&inner.readbacks, device, encoder, resources.extents(), 0, extent_bytes, &self.waker)
        })
    }

    /// What this frame's compute pass does: the marks it evaluates, the segment lists it counts, the
    /// faces whose normals it sums, the marks it measures, and with `travel` the marks whose comets
    /// travel.
    fn work(&mut self, measure: bool, travel: bool) -> Work {
        for shown in &mut self.marks {
            // A mark without rows has nothing to evaluate.
            if shown.rows + shown.exits == 0 && shown.tables.synced() {
                shown.dirty = 0;
                shown.reduce = false;
            }
        }
        let work: Vec<usize> = (0..self.marks.len())
            .filter(|&i| self.marks[i].drawable() && (self.marks[i].dirty != 0 || self.marks[i].reduce))
            .collect();
        for &i in &work {
            let shown = &mut self.marks[i];
            shown.version += 1;
            if shown.dirty & shown.placing_channels() != 0 {
                shown.place_version += 1;
            }
        }
        // A mark easing toward a change writes what it shows as the clock moves, and as it evaluates.
        let eased: Vec<usize> = (0..self.marks.len())
            .filter(|&i| {
                let shown = &self.marks[i];
                let settles = shown.transition.as_ref().is_some_and(super::transition::Transition::settles);
                shown.drawable() && settles && (shown.easing || work.contains(&i))
            })
            .collect();
        for shown in &mut self.marks {
            if shown.easing {
                shown.version += 1;
                shown.easing = false;
            }
        }
        let segments = self.segments_due();
        let faces = if self.normals_due() {
            (0..self.marks.len()).filter(|&i| self.marks[i].drawable() && self.marks[i].faces()).collect()
        } else {
            Vec::new()
        };
        let measured = (0..self.marks.len())
            .filter(|&i| measure && self.marks[i].drawable() && self.marks[i].measured())
            .collect();
        let flowing = |i: &usize| self.marks[*i].drawable() && self.marks[*i].mark.flows();
        let carried = (0..self.marks.len()).filter(|i| self.marks[*i].carry && flowing(i)).collect();
        let travelled = (0..self.marks.len()).filter(|i| travel && flowing(i)).collect();
        Work { evaluated: work, eased, segments, faces, measured, carried, travelled }
    }

    /// Lists the row blocks each mark evaluates this frame where its uniform finds them: every block of
    /// a mark some channel of which changed for every row, else the blocks of the pages that changed.
    pub(super) fn lay_out_blocks(&mut self) {
        let mut words = Vec::new();
        for shown in &mut self.marks {
            let evaluates = shown.drawable() && (shown.dirty != 0 || shown.reduce);
            let blocks = if evaluates { shown.blocks.of(shown.rows + shown.exits) } else { Vec::new() };
            shown.block = words.len() as u32;
            shown.block_count = blocks.len() as u32;
            words.extend(blocks);
        }
        let gpu = self.gpu.clone();
        let resources = self.resources.as_mut().expect("created before passes");
        resources.write_blocks(&gpu.0.device, &gpu.0.queue, &words);
    }

    /// Runs the expression machine's entry point `entry` over every row and exit of each of `marks`.
    fn every_row(
        &self,
        pass: &mut wgpu::ComputePass<'_>,
        inner: &mut Inner,
        mark: &wgpu::BindGroup,
        entry: &'static str,
        marks: &[usize],
    ) {
        if marks.is_empty() {
            return;
        }
        pass.set_pipeline(&inner.pipelines.compute(&self.gpu.0.device, Shader::Evaluate, entry));
        for &i in marks {
            pass.set_bind_group(1, mark, &[offset(i)]);
            dispatch(pass, self.marks[i].rows + self.marks[i].exits);
        }
    }

    /// Records the count → scan → write of each of `marks`' segment lists.
    fn count_segments(
        &self,
        pass: &mut wgpu::ComputePass<'_>,
        inner: &mut Inner,
        mark: &wgpu::BindGroup,
        marks: &[usize],
    ) {
        let device = &self.gpu.0.device;
        for &i in marks {
            let items = self.marks[i].rows + self.marks[i].exits;
            let path = self.marks[i].path();
            pass.set_bind_group(1, mark, &[offset(i)]);
            let count = Variant::compute(Shader::Lists, "count_segments").path(path);
            pass.set_pipeline(&inner.pipelines.compute_pipeline(device, count));
            dispatch(pass, items);
            super::lists::scan(pass, &mut inner.pipelines, device, items);
            let write = Variant::compute(Shader::Lists, "write_segments").path(path);
            pass.set_pipeline(&inner.pipelines.compute_pipeline(device, write));
            dispatch(pass, items);
        }
    }

    /// Whether face normals must be summed again: a face mark or a vertex mark wrote its state since
    /// they were, or the globe turned under them.
    fn normals_due(&mut self) -> bool {
        let faces = self.marks.iter().any(|m| m.drawable() && m.faces());
        if !faces {
            return false;
        }
        let versions: Vec<u64> = self
            .marks
            .iter()
            .filter(|m| matches!(m.kind(), MarkKind::Vertices | MarkKind::Faces))
            .map(|m| m.version + u64::from(m.dirty != 0))
            .collect();
        let turned = match (&self.scene.space, self.camera) {
            (Space::Earth(_), crate::Camera::Map { center, .. }) => Some([center[0].to_bits(), center[1].to_bits()]),
            _ => None,
        };
        let stamp = stamp_of((versions, turned, self.marks.len()));
        if !self.normals.due(stamp) {
            return false;
        }
        self.normals.mark(stamp);
        true
    }

    /// Draws the backdrop, every drawable mark in scene order, then `sprites` overlay sprites, counted
    /// in `drawn`, once the text set this frame has its fields. Depth is kept when `probing` reads it.
    pub(super) fn draw(
        &mut self,
        encoder: &mut wgpu::CommandEncoder,
        inner: &mut Inner,
        drawn: &mut Drawn,
        frame: &Frame<'_>,
        sprites: u32,
        probing: bool,
    ) {
        let gpu = self.gpu.clone();
        let device = &gpu.0.device;
        let Inner { atlas, pipelines, .. } = &mut *inner;
        atlas.lay(device, &gpu.0.queue, encoder, pipelines);
        let background =
            if self.signal.transparent() { [0.0; 4] } else { self.scene.theme.background.to_array().map(f64::from) };
        let plot = matches!(self.scene.space, Space::Plot(_));
        let scissor = scissor(self.area(), frame);
        let samples = super::resources::samples(frame.pixel_ratio);
        let resources = self.resources.as_mut().expect("created before passes");
        let (color, depth) = resources.targets(device, frame.size, frame.format, samples);
        let groups = resources.groups(&gpu.0, inner);
        let lists = resources.lists().clone();
        let mut passes: Vec<(Shader, wgpu::BindGroup)> = Vec::new();
        for shader in [
            Shader::Backdrop,
            Shader::Points,
            Shader::PointsCustom,
            Shader::PointsPorts,
            Shader::Strokes,
            Shader::Triangles,
            Shader::Areas,
            Shader::PointsFocus,
            Shader::PointsCustomFocus,
            Shader::StrokesFocus,
            Shader::AreasFocus,
            Shader::Glyphs,
            Shader::GlyphsPorts,
            Shader::Overlay,
        ] {
            passes.push((shader, resources.pass(device, inner, shader)));
        }
        let group = |shader: Shader| &passes.iter().find(|(s, _)| *s == shader).expect("listed").1;
        let (view, resolve) = match &color {
            Some(color) => (color, Some(frame.target)),
            None => (frame.target, None),
        };
        let mut pass = main_pass(encoder, view, resolve, &depth, background, probing);
        pass.set_bind_group(0, &groups.view, &[EyeSlot::Main.offset()]);
        pass.set_bind_group(1, &groups.mark, &[0]);
        pass.set_pipeline(&inner.pipelines.render(device, Shader::Backdrop, frame.format, samples));
        pass.set_bind_group(2, group(Shader::Backdrop), &[]);
        drawn.draw(&mut pass, 3, 1);
        let marks = Marks {
            device,
            format: frame.format,
            samples,
            groups: &groups,
            passes: &passes,
            lists: &lists,
            clip: (plot && scissor[2] > 0 && scissor[3] > 0).then_some(scissor),
            size: frame.size,
        };
        for (i, shown) in self.marks.iter().enumerate() {
            if !shown.drawable() || shown.instances() == 0 {
                continue;
            }
            marks.draw(&mut pass, drawn, inner, i, shown, false);
            marks.ports(&mut pass, drawn, inner, i, shown);
        }
        // The rows focus lights draw again over every mark but labels, glowing: nothing covers them but
        // the ports they carry. They draw once what draws them compiled.
        for (i, shown) in self.marks.iter().enumerate() {
            if self.answers() && shown.drawable() && shown.lit.rows > 0 {
                marks.draw(&mut pass, drawn, inner, i, shown, true);
                marks.ports(&mut pass, drawn, inner, i, shown);
            }
        }
        let glyphs = [Shader::Glyphs, Shader::GlyphsPorts].map(|shader| (shader, group(shader)));
        self.draw_labels(&mut pass, drawn, inner, frame.format, samples, &groups, glyphs);
        if sprites > 0 {
            pass.set_pipeline(&inner.pipelines.render(device, Shader::Overlay, frame.format, samples));
            pass.set_bind_group(2, group(Shader::Overlay), &[]);
            drawn.draw(&mut pass, 4, sprites);
        }
    }

    /// Finds the row nearest the probe among what the frame drew, and the nearest depth drawn there,
    /// and reads them back.
    pub(super) fn probe(&mut self, encoder: &mut wgpu::CommandEncoder, inner: &mut Inner) -> Readback {
        let gpu = self.gpu.clone();
        let device = &gpu.0.device;
        let probed: Vec<usize> = (0..self.marks.len()).filter(|&i| self.marks[i].probed()).collect();
        // Every mark drawn has its probe program, which reads the depth under the probe as well as any.
        let under = self.marks.iter().position(super::marks::Shown::drawable);
        let resources = self.resources.as_mut().expect("created before passes");
        let shader = crate::gpu::pipelines::probe(resources.samples());
        let mut reset = [0_u32; wgsl::PROBE_WORDS as usize];
        reset[wgsl::PROBE_KEY as usize] = wgsl::NONE;
        gpu.0.queue.write_buffer(resources.probe(), 0, bytemuck::cast_slice(&reset));
        let groups = resources.groups(&gpu.0, inner);
        let group = resources.pass(device, inner, shader);
        {
            let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
                label: Some("latkit probe"),
                timestamp_writes: None,
            });
            pass.set_bind_group(0, &groups.view, &[EyeSlot::Main.offset()]);
            // Each step over every mark before the next: the nearest key, then its topmost instance,
            // then where that lies.
            for step in [wgsl::STEP_NEAREST, wgsl::STEP_TOPMOST, wgsl::STEP_DESCRIBE] {
                pass.set_bind_group(2, &group, &[super::resources::step(step)]);
                for &i in &probed {
                    self.probe_step(&mut pass, inner, shader, &groups.mark, i, step);
                }
            }
            if let Some(i) = under {
                pass.set_bind_group(2, &group, &[super::resources::step(wgsl::STEP_UNDER)]);
                self.probe_step(&mut pass, inner, shader, &groups.mark, i, wgsl::STEP_UNDER);
            }
        }
        let resources = self.resources.as_ref().expect("created before passes");
        Readback::new(
            &inner.readbacks,
            device,
            encoder,
            resources.probe(),
            0,
            u64::from(wgsl::PROBE_WORDS) * 4,
            &self.waker,
        )
    }

    /// Records step `step` of mark `i`'s probe program, `shader`, the step's pass group bound: over
    /// every instance it reaches, or once for a step that reads one thing.
    pub(super) fn probe_step(
        &self,
        pass: &mut wgpu::ComputePass<'_>,
        inner: &mut Inner,
        shader: Shader,
        mark: &wgpu::BindGroup,
        i: usize,
        step: u32,
    ) {
        let shown = &self.marks[i];
        let variant = Variant::probe(shader, shown.primitive()).path(shown.path());
        pass.set_pipeline(&inner.pipelines.compute_pipeline(&self.gpu.0.device, variant));
        pass.set_bind_group(1, mark, &[offset(i)]);
        if matches!(step, wgsl::STEP_DESCRIBE | wgsl::STEP_LOCATE | wgsl::STEP_UNDER) {
            pass.dispatch_workgroups(1, 1, 1);
        } else {
            dispatch(pass, shown.live_instances());
        }
    }
}