daegun 1.1.5

Your all-in-one text engine. Rust, no_std, zero dependencies.
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
use daegun::daecore::daemachine::subpixel::{StripeOrder, SubpixelLayout};

fn alloc_fmt(r: &d3d12::Renderer) -> String {
    let kind = if r.is_software() { "software" } else { "hardware" };
    format!("{} ({kind}), feature level {}", r.device_name(), r.feature_level())
}
use daegun::daerizer::daegpu::{GpuBatch, SubpixelParams, d3d12, eval};

fn renderer() -> Option<d3d12::Renderer> {
    match d3d12::Renderer::new() {
        Ok(r) => Some(r),
        Err(d3d12::Error::NoDevice) => None,
        Err(e) => panic!("the backend tried and failed rather than reporting no device: {e}"),
    }
}

#[test]
fn a_renderer_is_created_or_absent_and_never_broken() {
    match d3d12::Renderer::new() {
        Ok(renderer) => {
            let name = alloc_fmt(&renderer);
            assert!(!name.is_empty(), "a device was created but reports no name");
            std::eprintln!("d3d12: {name}");
        }
        Err(d3d12::Error::NoDevice) => {
            std::eprintln!("d3d12: no device on this machine, which is a valid outcome");
        }
        Err(other) => panic!("the backend tried and failed rather than reporting no device: {other}"),
    }
}

#[test]
fn a_new_target_reads_back_transparent_black() {
    let Some(r) = renderer() else { return };
    let mut t = r.target(64, 48).expect("target");
    assert_eq!(t.width(), 64);
    assert_eq!(t.height(), 48);

    let pixels = r.read_pixels(&mut t).expect("read");
    assert_eq!(pixels.len(), 64 * 48 * 4, "RGBA8, tightly packed");
    assert!(pixels.iter().all(|&b| b == 0), "a cleared target read back non-zero");
}

#[test]
fn an_awkward_width_is_repacked_rather_than_sheared() {
    let Some(r) = renderer() else { return };
    let face = super::face::Face::load("eb-garamond/EBGaramond.ttf");
    let mut batch = GpuBatch::new();
    let slot = face.glyph(&mut batch, 36).expect("a glyph with an outline");
    let geometry = r.geometry(&batch).expect("geometry");

    let (w, h) = (100u32, 40u32);
    let mut t = r.target(w, h).expect("target");
    let px = 32.0;
    let inst = slot.instance([6.0, 4.0], px, [px, px], [1.0, 1.0, 1.0, 1.0]);
    r.draw(&mut t, &geometry, &[inst], &SubpixelParams::default(), d3d12::Mode::Grayscale)
        .expect("draw");
    let pixels = r.read_pixels(&mut t).expect("read");
    assert_eq!(pixels.len() as u32, w * h * 4);

    let mut first = w;
    let mut last = 0u32;
    for y in 0..h {
        for x in 0..w {
            if pixels[((y * w + x) * 4 + 3) as usize] > 0 {
                first = first.min(x);
                last = last.max(x);
            }
        }
    }
    assert!(last >= first, "the draw produced no ink at all");
    assert!(
        last - first < 48,
        "ink spans columns {first}..={last}, which is wider than the glyph — the rows are sheared"
    );
}

#[test]
fn a_target_with_no_area_is_refused() {
    let Some(r) = renderer() else { return };
    assert!(matches!(r.target(0, 16), Err(d3d12::Error::BadTarget)));
    assert!(matches!(r.target(16, 0), Err(d3d12::Error::BadTarget)));
}

#[test]
fn a_target_may_outlive_its_renderer() {
    let Some(r) = renderer() else { return };
    let mut t = r.target(32, 32).expect("target");
    assert!(r.read_pixels(&mut t).is_ok());
    drop(r);
    drop(t);
}

#[test]
fn a_batch_becomes_geometry() {
    let Some(r) = renderer() else { return };
    let face = super::face::Face::load("inter/InterVariable.ttf");
    let mut batch = GpuBatch::new();
    let n = face.fill(&mut batch, 1..200, 7);
    assert!(n > 10, "only {n} glyphs made it into the batch");

    let g = r.geometry(&batch).expect("upload");
    assert_eq!(g.revision(), batch.revision(), "geometry records the batch it was built from");

    let empty = GpuBatch::new();
    let ge = r.geometry(&empty).expect("an empty batch is not an error");
    assert_eq!(ge.revision(), empty.revision());
}

#[test]
fn a_draw_inks_the_target_the_right_way_up() {
    let Some(r) = renderer() else { return };
    let face = super::face::Face::load("eb-garamond/EBGaramond.ttf");
    let mut batch = GpuBatch::new();
    let slot = face.glyph(&mut batch, 36).expect("a glyph with an outline");
    let geometry = r.geometry(&batch).expect("geometry");

    let (w, h) = (96u32, 128u32);
    let mut t = r.target(w, h).expect("target");
    let px = 48.0;
    let inst = slot.instance([8.0, 8.0], px, [px, px], [1.0, 1.0, 1.0, 1.0]);
    r.draw(&mut t, &geometry, &[inst], &SubpixelParams::default(), d3d12::Mode::Grayscale)
        .expect("draw");
    let pixels = r.read_pixels(&mut t).expect("read");

    let (mut inked, mut top, mut bottom) = (0usize, 0usize, 0usize);
    for y in 0..h {
        for x in 0..w {
            if pixels[((y * w + x) * 4 + 3) as usize] > 0 {
                inked += 1;
                if y < h / 2 { top += 1 } else { bottom += 1 }
            }
        }
    }
    assert!(inked > 200, "only {inked} pixels took ink, so the draw produced nothing");
    assert!(
        bottom > top * 4,
        "{top} inked pixels above the midline against {bottom} below: the projection is flipped"
    );
}

#[test]
fn a_draw_with_no_instances_leaves_the_target_empty() {
    let Some(r) = renderer() else { return };
    let batch = GpuBatch::new();
    let geometry = r.geometry(&batch).expect("geometry");
    let mut t = r.target(32, 32).expect("target");
    r.draw(&mut t, &geometry, &[], &SubpixelParams::default(), d3d12::Mode::Grayscale)
        .expect("draw");
    let pixels = r.read_pixels(&mut t).expect("read");
    assert!(pixels.iter().all(|&b| b == 0), "an empty draw put ink on the target");
}

#[test]
fn growing_instance_counts_regrow_the_buffer() {
    let Some(r) = renderer() else { return };
    let face = super::face::Face::load("inter/InterVariable.ttf");
    let mut batch = GpuBatch::new();
    let slot = face.glyph(&mut batch, 36).expect("a glyph with an outline");
    let geometry = r.geometry(&batch).expect("geometry");
    let params = SubpixelParams::default();

    let (w, h) = (256u32, 64u32);
    let mut t = r.target(w, h).expect("target");
    let mut previous = 0usize;
    for n in 1..=8usize {
        let insts: Vec<_> = (0..n)
            .map(|i| {
                slot.instance([4.0 + i as f32 * 28.0, 8.0], 24.0, [24.0, 24.0], [1.0, 1.0, 1.0, 1.0])
            })
            .collect();
        r.draw(&mut t, &geometry, &insts, &params, d3d12::Mode::Grayscale).expect("draw");
        let inked = r.read_pixels(&mut t).expect("read")
            .chunks_exact(4)
            .filter(|p| p[3] > 0)
            .count();
        assert!(inked > previous, "{n} glyphs inked {inked} pixels, no more than {n} minus one did");
        previous = inked;
    }
}

#[test]
fn subpixel_mode_gives_the_channels_different_coverage() {
    let Some(r) = renderer() else { return };
    let face = super::face::Face::load("eb-garamond/EBGaramond.ttf");
    let mut batch = GpuBatch::new();
    let slot = face.glyph(&mut batch, 36).expect("a glyph with an outline");
    let geometry = r.geometry(&batch).expect("geometry");

    let mut t = r.target(64, 64).expect("target");
    let inst = slot.instance([8.0, 8.0], 40.0, [40.0, 40.0], [1.0, 1.0, 1.0, 1.0]);
    let layout = SubpixelLayout::horizontal(StripeOrder::Rgb);
    r.draw(&mut t, &geometry, &[inst], &SubpixelParams::from_layout(&layout), d3d12::Mode::Subpixel)
        .expect("draw");
    let pixels = r.read_pixels(&mut t).expect("read");

    let differing = pixels.chunks_exact(4).filter(|p| p[0] != p[1] || p[1] != p[2]).count();
    assert!(
        differing > 20,
        "only {differing} pixels have per-channel coverage, so dual-source did nothing"
    );
}

#[test]
fn a_target_crosses_between_renderers_exactly_when_the_device_is_shared() {
    let (Some(a), Some(b)) = (renderer(), renderer()) else { return };
    let batch = GpuBatch::new();
    let g = a.geometry(&batch).expect("geometry");
    let mut t = a.target(16, 16).expect("target");

    let drew = b.draw(&mut t, &g, &[], &SubpixelParams::default(), d3d12::Mode::Grayscale);
    let read = b.read_pixels(&mut t);
    match (&drew, &read) {
        (Ok(()), Ok(_)) => {}
        (Err(d3d12::Error::BadTarget), Err(d3d12::Error::BadTarget)) => {}
        _ => panic!("draw and read disagreed about the same target: draw {drew:?}"),
    }
    assert!(a.read_pixels(&mut t).is_ok());
}

#[test]
fn a_target_drawn_by_one_renderer_waits_correctly_in_another() {
    let (Some(drawer), Some(reader)) = (renderer(), renderer()) else { return };

    let face = super::face::Face::load("eb-garamond/EBGaramond.ttf");
    let mut batch = GpuBatch::new();
    let slot = face.glyph(&mut batch, 37).expect("a glyph with an outline");
    let geometry = drawer.geometry(&batch).expect("geometry");
    let inst = slot.instance([8.0, 8.0], 48.0, [48.0, 48.0], [1.0; 4]);

    let mut t = drawer.target(64, 64).expect("target");
    if drawer.draw(&mut t, &geometry, &[inst], &SubpixelParams::default(), d3d12::Mode::Grayscale)
        .is_err()
    {
        return;
    }

    let Ok(pixels) = reader.read_pixels(&mut t) else {
        panic!("a draw was accepted on this target but the read was refused, which cannot both be right")
    };
    let inked = pixels.iter().filter(|&&b| b > 0).count();
    assert!(inked > 200, "only {inked} bytes took ink, so the read raced the draw or drew nothing");

    assert!(reader.wait(&mut t).is_ok(), "wait after read_pixels must be a no-op");
    assert!(drawer.wait(&mut t).is_ok(), "wait after read_pixels must be a no-op");

    let again = drawer.read_pixels(&mut t).expect("read");
    assert_eq!(again.iter().filter(|&&b| b > 0).count(), inked, "a second read disagreed");
}

#[test]
fn d3d12_grayscale_matches_the_reference_evaluator() {
    let Some(gpu) = renderer() else { return };
    let face = super::face::Face::load("eb-garamond/EBGaramond.ttf");
    let mut batch = GpuBatch::new();
    let params = SubpixelParams::default();
    let (w, h) = (128u32, 128u32);
    let px = 96.0f32;
    let inv_px = 1.0f32 / px;
    let mut target = gpu.target(w, h).expect("target");

    let (mut checked, mut worst, mut inked) = (0usize, 0i32, 0usize);
    let (mut sum, mut over) = (0f64, 0usize);
    for gid in [36u16, 50, 37, 74, 90, 25] {
        let Some(slot) = face.glyph(&mut batch, gid) else { continue };
        let geometry = gpu.geometry(&batch).expect("geometry");
        let inst = slot.instance([16.0, 16.0], px, [px, px], [1.0, 1.0, 1.0, 1.0]);

        gpu.draw(&mut target, &geometry, &[inst], &params, d3d12::Mode::Grayscale).expect("draw");
        let pixels = gpu.read_pixels(&mut target).expect("read").to_vec();

        for y in 0..h {
            for x in 0..w {
                let em = [
                    (x as f32 + 0.5 - 16.0) * inv_px,
                    ((h - 1 - y) as f32 + 0.5 - 16.0) * inv_px,
                ];
                let want = (eval::coverage(&batch, &slot, em, [px, px]) * 255.0).round() as i32;
                let got = i32::from(pixels[((y * w + x) * 4 + 3) as usize]);
                let delta = (got - want).abs();
                worst = worst.max(delta);
                sum += f64::from(delta);
                if delta > 16 { over += 1 }
                checked += 1;
                if got > 0 { inked += 1 }
            }
        }
    }
    assert!(checked > 50_000, "only {checked} pixels compared");
    assert!(inked > 2_000, "only {inked} pixels took ink, so the draw produced nothing to grade");

    let mean = sum / checked as f64;
    let rate = over as f64 / checked as f64;
    std::eprintln!("d3d12-vs-eval: mean {mean:.5}  over-16 rate {:.4}%  worst {worst}", rate * 100.0);
    assert!(mean < 0.003, "mean |delta| is {mean:.5} of 255; the interpolated coordinate scored 0.00425 on Apple and 0.00456 on AMD");
    assert!(rate < 0.0002, "{over} of {checked} pixels ({:.4}%) differ by more than 16", rate * 100.0);
    assert!(worst <= 2, "a pixel differs by {worst} of 255; this should be 0");
}

#[test]
fn a_d3d12_adapter_reports_what_kind_it_is() {
    use daegun::daerizer::draw::{DeviceKind, Policy, Rendered, Request, route};

    let Some(r) = renderer() else {
        eprintln!("D3D12 ADAPTER: none on this machine");
        return;
    };
    let profile = r.profile();
    eprintln!(
        "D3D12 ADAPTER: name={:?} kind={:?} routes={:?}",
        profile.name,
        profile.kind,
        route(Ok(()), &Request::at(32.0), Some(&profile), &Policy::default()),
    );

    assert_eq!(profile.name, r.device_name(), "the profile renamed the adapter");
    assert!(!profile.name.is_empty(), "the adapter named itself as nothing");

    let decided = route(Ok(()), &Request::at(32.0), Some(&profile), &Policy::default());
    let expected = if profile.kind == DeviceKind::Software { Rendered::Cpu } else { Rendered::Gpu };
    assert_eq!(decided, expected, "a {:?} adapter routed to {decided:?}", profile.kind);
}

#[test]
fn the_native_handle_is_stable_and_distinct() {
    let Some(r) = renderer() else { return };
    let (mut a, b) = (r.target(64, 48).expect("target"), r.target(64, 48).expect("target"));

    let h = unsafe { a.texture() };
    assert!(!h.0.is_null(), "the target handed out a null texture");
    assert_eq!(h, unsafe { a.texture() }, "the handle changed between two calls");
    assert_ne!(h, unsafe { b.texture() }, "two targets named one texture");

    r.read_pixels(&mut a).expect("read");
    assert_eq!(h, unsafe { a.texture() }, "a read replaced the texture");
}

#[test]
fn the_handle_reports_the_state_the_resource_is_in() {
    let Some(r) = renderer() else { return };
    let mut t = r.target(64, 48).expect("target");

    let (_, fresh) = unsafe { t.texture() };
    assert_eq!(
        fresh, d3d12::RESOURCE_STATE_COPY_SOURCE,
        "a new target does not rest where `target` leaves it",
    );

    r.read_pixels(&mut t).expect("read");
    let (_, after) = unsafe { t.texture() };
    assert_eq!(
        after, d3d12::RESOURCE_STATE_COPY_SOURCE,
        "a read left the resource somewhere the accessor does not report",
    );
    assert_ne!(
        d3d12::RESOURCE_STATE_COPY_SOURCE, d3d12::RESOURCE_STATE_RENDER_TARGET,
        "the two states this backend moves between are the same value, so nothing above tests anything",
    );
}

fn one_glyph(batch: &mut GpuBatch) -> daegun::GlyphInstance {
    let face = super::face::Face::load("eb-garamond/EBGaramond.ttf");
    let slot = face.glyph(batch, 37).expect("a glyph with an outline");
    // Three different channels, or a byte swap would be invisible.
    slot.instance([8.0, 8.0], 64.0, [64.0, 64.0], [1.0, 0.15, 0.0, 1.0])
}

// A DXGI swapchain is BGRA, so the two byte orders have to put the same colors in the same places.
#[test]
fn the_two_byte_orders_agree_once_swapped() {
    let Some(r) = renderer() else { return };
    let mut batch = GpuBatch::new();
    let inst = one_glyph(&mut batch);
    let geometry = r.geometry(&batch).expect("geometry");
    let params = SubpixelParams::default();
    let (w, h) = (96u32, 96u32);

    let shot = |format| {
        let mut t = r.target_with_format(w, h, format).expect("target");
        r.draw(&mut t, &geometry, &[inst], &params, d3d12::Mode::Grayscale).expect("draw");
        r.read_pixels(&mut t).expect("read").to_vec()
    };
    let rgba = shot(d3d12::Format::Rgba8Unorm);
    let bgra = shot(d3d12::Format::Bgra8Unorm);

    assert_eq!(rgba.len(), bgra.len(), "the two targets are different sizes");
    let mut inked = 0usize;
    for (i, (a, b)) in rgba.chunks_exact(4).zip(bgra.chunks_exact(4)).enumerate() {
        assert_eq!(
            [b[0], b[1], b[2], b[3]],
            [a[2], a[1], a[0], a[3]],
            "pixel {i} did not come back byte-swapped",
        );
        if a[3] > 0 {
            inked += 1;
        }
    }
    assert!(inked > 300, "only {inked} pixels took ink, so the comparison proved little");
}

#[test]
fn a_target_clears_to_the_color_it_was_given() {
    let Some(r) = renderer() else { return };
    let mut batch = GpuBatch::new();
    let _ = one_glyph(&mut batch);
    let geometry = r.geometry(&batch).expect("geometry");
    let mut t = r.target(64, 64).expect("target");

    let transparent = daegun::paint::Rgba { r: 0, g: 0, b: 0, a: 0 };
    assert_eq!(t.clear(), Some(transparent), "the default clear is no longer transparent black");

    let slate = daegun::paint::Rgba { r: 20, g: 40, b: 60, a: 255 };
    t.set_clear(Some(slate));
    r.draw(&mut t, &geometry, &[], &SubpixelParams::default(), d3d12::Mode::Grayscale)
        .expect("draw");
    let px = r.read_pixels(&mut t).expect("read");
    assert!(
        px.chunks_exact(4).all(|p| p == [20, 40, 60, 255]),
        "the target did not clear to the color it was given",
    );
}

// Every draw cleared, so a second geometry erased the first. Not clearing is what lets them layer.
#[test]
fn a_second_geometry_can_draw_over_the_first() {
    let Some(r) = renderer() else { return };
    let face = super::face::Face::load("eb-garamond/EBGaramond.ttf");
    let (w, h) = (128u32, 64u32);
    let params = SubpixelParams::default();

    let mut left_batch = GpuBatch::new();
    let left = face
        .glyph(&mut left_batch, 37)
        .expect("glyph")
        .instance([6.0, 6.0], 48.0, [48.0, 48.0], [1.0; 4]);
    let left_geo = r.geometry(&left_batch).expect("geometry");

    let mut right_batch = GpuBatch::new();
    let right = face
        .glyph(&mut right_batch, 50)
        .expect("glyph")
        .instance([72.0, 6.0], 48.0, [48.0, 48.0], [1.0; 4]);
    let right_geo = r.geometry(&right_batch).expect("geometry");

    let halves = |px: &[u8]| {
        let (mut l, mut rt) = (0usize, 0usize);
        for y in 0..h as usize {
            for x in 0..w as usize {
                if px[(y * w as usize + x) * 4 + 3] > 0 {
                    if x < 64 { l += 1 } else { rt += 1 }
                }
            }
        }
        (l, rt)
    };

    let mut layered = r.target(w, h).expect("target");
    r.draw(&mut layered, &left_geo, &[left], &params, d3d12::Mode::Grayscale).expect("first");
    layered.set_clear(None);
    r.draw(&mut layered, &right_geo, &[right], &params, d3d12::Mode::Grayscale).expect("second");
    let (l, rt) = halves(r.read_pixels(&mut layered).expect("read"));
    assert!(l > 0 && rt > 0, "layering lost a draw: left {l}, right {rt}");

    let mut cleared = r.target(w, h).expect("target");
    r.draw(&mut cleared, &left_geo, &[left], &params, d3d12::Mode::Grayscale).expect("first");
    r.draw(&mut cleared, &right_geo, &[right], &params, d3d12::Mode::Grayscale).expect("second");
    let (l, rt) = halves(r.read_pixels(&mut cleared).expect("read"));
    assert_eq!(l, 0, "a clearing draw no longer wipes what came before it");
    assert!(rt > 0, "the second draw did not land");
}

// A swapchain backbuffer belongs to the device its swapchain was made on, so drawing into one needs
// the caller's device rather than the one daegun would have created.
#[test]
fn a_renderer_can_adopt_a_device_it_did_not_create() {
    let Some(owner) = renderer() else { return };
    let mut batch = GpuBatch::new();
    let inst = one_glyph(&mut batch);
    let params = SubpixelParams::default();

    let shot = |r: &d3d12::Renderer| {
        let geometry = r.geometry(&batch).expect("geometry");
        let mut t = r.target(96, 96).expect("target");
        r.draw(&mut t, &geometry, &[inst], &params, d3d12::Mode::Grayscale).expect("draw");
        r.read_pixels(&mut t).expect("read").to_vec()
    };
    let from_owner = shot(&owner);

    let (device, queue) = unsafe { owner.handles() };
    let adopted = unsafe { d3d12::Renderer::from_device(device, queue) }.expect("adopts");
    assert_eq!(adopted.device_name(), owner.device_name(), "adopted a different device");
    assert_eq!(shot(&adopted), from_owner, "the adopted device drew something else");
    assert!(from_owner.chunks_exact(4).any(|p| p[3] > 0), "neither renderer drew anything");
}

#[test]
fn adopting_a_null_device_is_refused_rather_than_crashing() {
    assert!(
        unsafe { d3d12::Renderer::from_device(core::ptr::null_mut(), core::ptr::null_mut()) }
            .is_err(),
        "a null device was accepted",
    );
}

// A borrowed target has no staging of its own, so it is pointed at the texture of an owned one: the
// draw goes through the borrowed path and the pixels come back through the owner.
#[test]
fn a_borrowed_texture_draws_what_an_owned_one_does() {
    let Some(r) = renderer() else { return };
    let mut batch = GpuBatch::new();
    let inst = one_glyph(&mut batch);
    let geometry = r.geometry(&batch).expect("geometry");
    let params = SubpixelParams::default();
    let (w, h) = (96u32, 96u32);

    for format in [d3d12::Format::Rgba8Unorm, d3d12::Format::Bgra8Unorm] {
        let mut owned = r.target_with_format(w, h, format).expect("owned target");
        r.draw(&mut owned, &geometry, &[inst], &params, d3d12::Mode::Grayscale).expect("draw");
        let direct = r.read_pixels(&mut owned).expect("read").to_vec();

        // D3D12 hands back the resource with its current state; only the resource is needed here.
        let (resource, _state) = unsafe { owned.texture() };
        let mut borrowed = unsafe { r.target_from_texture(resource, w, h, format) }
            .expect("a target over the owned texture");
        assert!(
            r.read_pixels(&mut borrowed).is_err(),
            "a borrowed target claimed to have pixels to read back",
        );
        r.draw(&mut borrowed, &geometry, &[inst], &params, d3d12::Mode::Grayscale).expect("draw");
        drop(borrowed);

        let through_borrow = r.read_pixels(&mut owned).expect("read").to_vec();
        assert_eq!(direct, through_borrow, "{format:?}: the borrowed path drew something else");
        assert!(direct.chunks_exact(4).any(|p| p[3] > 0), "{format:?}: nothing was drawn at all");
    }
}