taim 0.15.0-rc.2

Taim.rs: a GPU-native, fine-grained reactive GUI framework for Rust, for desktop, mobile and the web. By theaimartco.
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
//! CPU/GPU conformance for the display transform.
//!
//! `core/src/color/display.rs` and `wgpu/src/shader/display.wgsl` implement
//! the same transform twice: once for screenshots, goldens, and any caller
//! reasoning about color in Rust, and once for the pixels a person actually
//! looks at. Two implementations of the same arithmetic drift, and a drift
//! here is invisible -- the frame still renders, the goldens still pass
//! (they go through `tiny-skia`), and the only symptom is that the panel is
//! wrong in a way nobody can measure without a camera.
//!
//! These tests run a sweep of scene-referred values through the shader on a
//! real device and require the answers to match the Rust implementation.
//!
//! Skipped rather than failed when no adapter exists: a machine with no GPU
//! cannot answer the question, and a suite that goes red on such a machine
//! teaches its readers to ignore it.

use taim_core::color::display::{Display, DisplayTransform, GamutMap, ToneMap};
use taim_core::color::{Color, Radiance};
use taim_wgpu::display::Pipeline;
use taim_wgpu::wgpu;

/// Full precision in and out: the point is to compare arithmetic, not to
/// measure how much a half-float target rounds it.
const FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Rgba32Float;

/// A device and queue, or `None` when no adapter exists.
fn device() -> Option<(wgpu::Device, wgpu::Queue)> {
    let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
        backends: wgpu::Backends::from_env().unwrap_or(wgpu::Backends::PRIMARY),
        flags: wgpu::InstanceFlags::empty(),
        ..wgpu::InstanceDescriptor::new_without_display_handle()
    });

    taim_futures::futures::executor::block_on(async {
        let adapter = instance
            .request_adapter(&wgpu::RequestAdapterOptions {
                power_preference: wgpu::PowerPreference::HighPerformance,
                force_fallback_adapter: false,
                compatible_surface: None,
            })
            .await
            .ok()?;

        adapter
            .request_device(&wgpu::DeviceDescriptor {
                label: Some("display_transform.test"),
                ..Default::default()
            })
            .await
            .ok()
    })
}

macro_rules! needs_gpu {
    ($device:ident, $queue:ident) => {
        let Some(($device, $queue)) = device() else {
            eprintln!("skipped: no wgpu adapter available");
            return;
        };
    };
}

/// Runs `inputs` through the shader, one texel each, and reads the result.
fn on_gpu(
    device: &wgpu::Device,
    queue: &wgpu::Queue,
    transform: &DisplayTransform,
    inputs: &[Radiance],
) -> Vec<[f32; 4]> {
    let width = inputs.len() as u32;

    let descriptor = |usage| wgpu::TextureDescriptor {
        label: Some("display_transform.test.texture"),
        size: wgpu::Extent3d {
            width,
            height: 1,
            depth_or_array_layers: 1,
        },
        mip_level_count: 1,
        sample_count: 1,
        dimension: wgpu::TextureDimension::D2,
        format: FORMAT,
        usage,
        view_formats: &[],
    };

    let source = device.create_texture(&descriptor(
        wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
    ));
    let target = device.create_texture(&descriptor(
        wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_SRC,
    ));

    // `bytemuck` is not a dependency here, and a hand-rolled conversion is
    // clearer than adding one for sixteen bytes a texel.
    let texels: Vec<u8> = inputs
        .iter()
        .flat_map(|radiance| [radiance.red, radiance.green, radiance.blue, radiance.alpha])
        .flat_map(f32::to_ne_bytes)
        .collect();

    queue.write_texture(
        source.as_image_copy(),
        &texels,
        wgpu::TexelCopyBufferLayout {
            offset: 0,
            bytes_per_row: Some(width * 16),
            rows_per_image: Some(1),
        },
        source.size(),
    );

    // 256-byte row alignment is a copy requirement, not a texture one.
    let bytes_per_row = (width * 16).div_ceil(256) * 256;

    let readback = device.create_buffer(&wgpu::BufferDescriptor {
        label: Some("display_transform.test.readback"),
        size: u64::from(bytes_per_row),
        usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ,
        mapped_at_creation: false,
    });

    let pipeline = Pipeline::new(device, FORMAT);

    let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
        label: Some("display_transform.test.encoder"),
    });

    pipeline.render(
        device,
        queue,
        &mut encoder,
        &source.create_view(&wgpu::TextureViewDescriptor::default()),
        &target.create_view(&wgpu::TextureViewDescriptor::default()),
        transform,
    );

    encoder.copy_texture_to_buffer(
        target.as_image_copy(),
        wgpu::TexelCopyBufferInfo {
            buffer: &readback,
            layout: wgpu::TexelCopyBufferLayout {
                offset: 0,
                bytes_per_row: Some(bytes_per_row),
                rows_per_image: Some(1),
            },
        },
        target.size(),
    );

    let index = queue.submit([encoder.finish()]);

    let slice = readback.slice(..);
    slice.map_async(wgpu::MapMode::Read, |result| {
        result.expect("the readback buffer must map");
    });

    let _ = device.poll(wgpu::PollType::Wait {
        submission_index: Some(index),
        timeout: None,
    });

    let data = slice.get_mapped_range();

    let floats: Vec<f32> = data
        .chunks_exact(4)
        .map(|bytes| f32::from_ne_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]))
        .collect();

    let out = (0..inputs.len())
        .map(|index| {
            [
                floats[index * 4],
                floats[index * 4 + 1],
                floats[index * 4 + 2],
                floats[index * 4 + 3],
            ]
        })
        .collect();

    drop(data);
    readback.unmap();

    out
}

/// The sweep both implementations must agree on.
fn sweep() -> Vec<Radiance> {
    let mut values = vec![
        Radiance::BLACK,
        Radiance::from(Color::WHITE),
        Radiance::from(Color::from_rgb8(0x00, 0xA8, 0xFF)),
        Radiance::from(Color::from_rgb8(0xFF, 0x33, 0x66)),
        Radiance::from(Color::from_rgba8(0x40, 0x80, 0xC0, 0.5)),
        // Saturated primaries, which are where gamut mapping does its work.
        Radiance::new(1.0, 0.0, 0.0, 1.0),
        Radiance::new(0.0, 1.0, 0.0, 1.0),
        Radiance::new(0.0, 0.0, 1.0, 1.0),
    ];

    // A luminance ramp straddling the knee, where tone mapping does its work.
    for nits in [1.0, 50.0, 203.0, 400.0, 1_000.0, 2_000.0, 4_000.0] {
        values.push(Radiance::from(Color::WHITE).at_nits(nits));
        values.push(Radiance::from(Color::from_rgb8(0xFF, 0x80, 0x20)).at_nits(nits));
    }

    values
}

/// Asserts the shader and the Rust implementation agree across the sweep.
#[track_caller]
fn assert_agrees(
    device: &wgpu::Device,
    queue: &wgpu::Queue,
    transform: &DisplayTransform,
    label: &str,
) {
    let inputs = sweep();
    let gpu = on_gpu(device, queue, transform, &inputs);

    for (input, actual) in inputs.iter().zip(gpu) {
        let expected = transform.apply(*input);

        for channel in 0..4 {
            let difference = (actual[channel] - expected[channel]).abs();
            let tolerance = expected[channel].abs().max(1.0) * 2e-3;

            assert!(
                difference <= tolerance,
                "{label}: channel {channel} of {input:?}\n  \
                 gpu  = {:?}\n  cpu  = {:?}",
                actual,
                expected
            );
        }
    }
}

#[test]
fn the_shader_agrees_with_the_reference_on_an_sdr_panel() {
    needs_gpu!(device, queue);

    assert_agrees(
        &device,
        &queue,
        &DisplayTransform::new(Display::SDR).with_source_peak_nits(4_000.0),
        "sdr",
    );
}

#[test]
fn the_shader_agrees_with_the_reference_on_an_hdr_panel() {
    needs_gpu!(device, queue);

    assert_agrees(
        &device,
        &queue,
        &DisplayTransform::new(Display::HDR10_1000).with_source_peak_nits(4_000.0),
        "hdr10",
    );
}

#[test]
fn the_shader_agrees_with_the_reference_on_an_extended_surface() {
    needs_gpu!(device, queue);

    assert_agrees(
        &device,
        &queue,
        &DisplayTransform::new(Display::P3_EXTENDED).with_source_peak_nits(4_000.0),
        "p3-extended",
    );
}

#[test]
fn the_shader_agrees_with_the_reference_for_every_operator() {
    needs_gpu!(device, queue);

    for tone_map in [ToneMap::Bt2390, ToneMap::Clip] {
        for gamut_map in [GamutMap::Desaturate, GamutMap::Clip] {
            assert_agrees(
                &device,
                &queue,
                &DisplayTransform::new(Display::HDR10_1000)
                    .with_source_peak_nits(4_000.0)
                    .with_tone_map(tone_map)
                    .with_gamut_map(gamut_map),
                &format!("{tone_map:?}/{gamut_map:?}"),
            );
        }
    }
}

#[test]
fn a_highlight_reaches_an_hdr_panel_brighter_than_white() {
    // The gate, measured on a real device rather than in arithmetic: the
    // shader must put a 1,000-nit highlight above diffuse white in the signal
    // it hands the panel.
    needs_gpu!(device, queue);

    let transform = DisplayTransform::new(Display::HDR10_1000).with_source_peak_nits(1_000.0);

    let inputs = vec![
        Radiance::from(Color::WHITE),
        Radiance::from(Color::WHITE).at_nits(1_000.0),
    ];

    let output = on_gpu(&device, &queue, &transform, &inputs);

    let white = Display::HDR10_1000.decode_nits(output[0][0]);
    let highlight = Display::HDR10_1000.decode_nits(output[1][0]);

    assert!(
        (white - 203.0).abs() < 5.0,
        "diffuse white reached the panel at {white} nits"
    );
    assert!(
        highlight > 900.0,
        "the highlight reached the panel at only {highlight} nits"
    );
}

mod present_path {
    use super::*;

    use taim_core::Renderer as _;
    use taim_core::{Rectangle, Size};
    use taim_wgpu::graphics::{Shell, Viewport};
    use taim_wgpu::{Engine, Renderer};

    const WIDTH: u32 = 64;
    const HEIGHT: u32 = 64;

    /// A renderer whose pipelines are built for `format`.
    ///
    /// The headless constructor pins itself to `Rgba8UnormSrgb`, and the HDR
    /// path needs a float frame format, so this builds the `Engine` directly.
    pub fn renderer_for(format: wgpu::TextureFormat) -> Option<Renderer> {
        let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
            backends: wgpu::Backends::from_env().unwrap_or(wgpu::Backends::PRIMARY),
            flags: wgpu::InstanceFlags::empty(),
            ..wgpu::InstanceDescriptor::new_without_display_handle()
        });

        taim_futures::futures::executor::block_on(async {
            let adapter = instance
                .request_adapter(&wgpu::RequestAdapterOptions {
                    power_preference: wgpu::PowerPreference::HighPerformance,
                    force_fallback_adapter: false,
                    compatible_surface: None,
                })
                .await
                .ok()?;

            let (device, queue) = adapter
                .request_device(&wgpu::DeviceDescriptor {
                    label: Some("display_transform.present.test"),
                    required_features: wgpu::Features::empty(),
                    required_limits: wgpu::Limits {
                        max_bind_groups: 2,
                        ..wgpu::Limits::default()
                    },
                    memory_hints: wgpu::MemoryHints::MemoryUsage,
                    trace: wgpu::Trace::Off,
                    experimental_features: wgpu::ExperimentalFeatures::disabled(),
                })
                .await
                .ok()?;

            let engine = Engine::new(&adapter, device, queue, format, None, Shell::headless());

            Some(Renderer::new(
                engine,
                taim_core::renderer::Settings::default(),
            ))
        })
    }

    /// The same scene every time: white, and a mid-grey patch, which is where
    /// a double-encode shows up.
    fn scene(renderer: &mut Renderer) {
        renderer.reset(Rectangle::with_size(Size::new(WIDTH as f32, HEIGHT as f32)));

        for (offset, color) in [(8.0, Color::WHITE), (32.0, Color::from_rgb(0.5, 0.5, 0.5))] {
            renderer.fill_quad(
                taim_core::renderer::Quad {
                    bounds: Rectangle {
                        x: offset,
                        y: offset,
                        width: 24.0,
                        height: 24.0,
                    },
                    ..Default::default()
                },
                color,
            );
        }
    }

    /// Presents the frame already recorded on `renderer` and reads the
    /// surface back as raw bytes. The caller owns the scene.
    pub fn present_and_read(
        renderer: &mut Renderer,
        format: wgpu::TextureFormat,
        bytes_per_texel: u32,
        display: Display,
        hdr: bool,
    ) -> Vec<u8> {
        let viewport = Viewport::with_physical_size(Size::new(WIDTH, HEIGHT), 1.0);

        let device = renderer.device().clone();
        let queue = renderer.queue().clone();

        let surface = device.create_texture(&wgpu::TextureDescriptor {
            label: Some("display_transform.present.surface"),
            size: wgpu::Extent3d {
                width: WIDTH,
                height: HEIGHT,
                depth_or_array_layers: 1,
            },
            mip_level_count: 1,
            sample_count: 1,
            dimension: wgpu::TextureDimension::D2,
            format,
            usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_SRC,
            view_formats: &[],
        });

        let view = surface.create_view(&wgpu::TextureViewDescriptor::default());

        if hdr {
            let _ = renderer.present_hdr(Some(Color::BLACK), &view, format, &viewport, display);
        } else {
            let _ = renderer.present(Some(Color::BLACK), format, &view, &surface, &viewport);
        }

        let bytes_per_row = (WIDTH * bytes_per_texel).div_ceil(256) * 256;

        let readback = device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("display_transform.present.readback"),
            size: u64::from(bytes_per_row * HEIGHT),
            usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ,
            mapped_at_creation: false,
        });

        let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
            label: Some("display_transform.present.readback_encoder"),
        });

        encoder.copy_texture_to_buffer(
            surface.as_image_copy(),
            wgpu::TexelCopyBufferInfo {
                buffer: &readback,
                layout: wgpu::TexelCopyBufferLayout {
                    offset: 0,
                    bytes_per_row: Some(bytes_per_row),
                    rows_per_image: Some(HEIGHT),
                },
            },
            surface.size(),
        );

        let index = queue.submit([encoder.finish()]);

        let slice = readback.slice(..);
        slice.map_async(wgpu::MapMode::Read, |result| {
            result.expect("the readback buffer must map");
        });

        let _ = device.poll(wgpu::PollType::Wait {
            submission_index: Some(index),
            timeout: None,
        });

        let data = slice.get_mapped_range();

        let row_bytes = (WIDTH * bytes_per_texel) as usize;
        let mut pixels = Vec::with_capacity(row_bytes * HEIGHT as usize);

        for row in 0..HEIGHT {
            let start = (row * bytes_per_row) as usize;
            pixels.extend_from_slice(&data[start..start + row_bytes]);
        }

        drop(data);
        readback.unmap();

        pixels
    }

    /// Decodes half-float bytes into `f32`.
    ///
    /// Hand-rolled rather than pulling in `half` for sixteen bits: IEEE 754
    /// binary16 is one sign bit, five exponent bits, ten mantissa bits.
    pub fn halves(bytes: &[u8]) -> Vec<f32> {
        bytes
            .chunks_exact(2)
            .map(|pair| {
                let bits = u16::from_le_bytes([pair[0], pair[1]]);

                let sign = if bits >> 15 == 1 { -1.0 } else { 1.0 };
                let exponent = i32::from((bits >> 10) & 0x1f);
                let mantissa = f32::from(bits & 0x3ff);

                if exponent == 0 {
                    // Subnormal: no implicit leading one.
                    sign * mantissa * 2.0f32.powi(-24)
                } else if exponent == 0x1f {
                    sign * f32::INFINITY
                } else {
                    sign * (1.0 + mantissa / 1024.0) * 2.0f32.powi(exponent - 15)
                }
            })
            .collect()
    }

    #[test]
    fn the_hdr_path_reproduces_the_standard_path_on_content_with_no_hdr_in_it() {
        // The invariant that makes this safe to ship: for a scene containing
        // nothing brighter than diffuse white -- which is every scene in the
        // framework today -- routing through a half-float intermediate and a
        // display transform must produce the image the direct path produces.
        //
        // It also catches a double-encode. `Display::SDR` carries the sRGB
        // transfer and an sRGB surface applies it in hardware too, so a
        // transform that ignored that would push mid-grey from 0.5 to 0.74
        // while leaving black and white exactly right.
        let Some(mut sdr) = renderer_for(wgpu::TextureFormat::Rgba8UnormSrgb) else {
            eprintln!("skipped: no wgpu adapter available");
            return;
        };
        let Some(mut hdr) = renderer_for(wgpu::TextureFormat::Rgba16Float) else {
            eprintln!("skipped: no wgpu adapter available");
            return;
        };

        scene(&mut sdr);

        let direct = present_and_read(
            &mut sdr,
            wgpu::TextureFormat::Rgba8UnormSrgb,
            4,
            Display::SDR,
            false,
        );

        scene(&mut hdr);

        // Resolved onto a half-float surface, so the shader does the sRGB
        // encode itself and the two are directly comparable.
        let through = halves(&present_and_read(
            &mut hdr,
            wgpu::TextureFormat::Rgba16Float,
            8,
            Display::SDR,
            true,
        ));

        assert_eq!(direct.len(), through.len());

        let mut worst = 0.0f32;

        for (index, (byte, value)) in direct.iter().zip(&through).enumerate() {
            let expected = f32::from(*byte) / 255.0;
            let difference = (expected - value).abs();

            worst = worst.max(difference);

            assert!(
                difference < 0.02,
                "component {index}: direct {expected}, hdr path {value}"
            );
        }

        eprintln!("worst component difference between the two paths: {worst}");
    }

    #[test]
    fn the_hdr_path_leaves_headroom_above_diffuse_white() {
        // The property the intermediate exists for. On an extended surface
        // the encode is relative to SDR white, so ordinary white lands at
        // 1.0 with the panel's range still above it -- rather than at the
        // top of a buffer that clamps.
        let Some(mut renderer) = renderer_for(wgpu::TextureFormat::Rgba16Float) else {
            eprintln!("skipped: no wgpu adapter available");
            return;
        };

        scene(&mut renderer);

        let pixels = halves(&present_and_read(
            &mut renderer,
            wgpu::TextureFormat::Rgba16Float,
            8,
            Display::P3_EXTENDED,
            true,
        ));

        // The white square starts at (8, 8); sample its middle.
        let index = ((20 * WIDTH + 20) * 4) as usize;
        let white = pixels[index];

        assert!(
            (white - 1.0).abs() < 0.05,
            "diffuse white landed at {white} on an extended surface"
        );
    }

    #[test]
    fn a_renderer_that_cannot_hold_a_highlight_is_not_asked_to() {
        // The gate the compositor applies. An 8-bit engine must never be
        // routed through `present_hdr`, because its pipelines cannot draw
        // into a float intermediate at all -- the failure is a render-pass
        // validation error, not a wrong color.
        assert!(!taim_wgpu::display::Target::carries_hdr(
            wgpu::TextureFormat::Rgba8UnormSrgb
        ));
        assert!(taim_wgpu::display::Target::carries_hdr(
            wgpu::TextureFormat::Rgba16Float
        ));
    }
}

mod authoring {
    use super::present_path::*;
    use super::*;

    use taim_core::Renderer as _;
    use taim_core::{Rectangle, Size};
    use taim_design::tokens::LuminanceTokens;

    #[test]
    fn an_authored_highlight_reaches_the_surface_brighter_than_white() {
        // The end of the chain, and the first point at which any of this is
        // reachable from something a designer writes.
        //
        // A design token says 1,000 nits. That becomes a multiplier on a
        // `Quad`. The quad is drawn by the ordinary renderer into a
        // half-float frame, resolved through the display transform, and lands
        // on the surface still carrying five times the light of the white
        // square beside it -- which is a thing no `Color` in this framework
        // can express, because `Color` is bounded at 1.0 by construction.
        let Some(mut renderer) = renderer_for(wgpu::TextureFormat::Rgba16Float) else {
            eprintln!("skipped: no wgpu adapter available");
            return;
        };

        renderer.reset(Rectangle::with_size(Size::new(64.0, 64.0)));

        // Ordinary white, for comparison.
        renderer.fill_quad(
            taim_core::renderer::Quad {
                bounds: Rectangle {
                    x: 4.0,
                    y: 4.0,
                    width: 20.0,
                    height: 20.0,
                },
                ..Default::default()
            },
            Color::WHITE,
        );

        // The same white, emitting.
        renderer.fill_quad(
            taim_core::renderer::Quad {
                bounds: Rectangle {
                    x: 36.0,
                    y: 36.0,
                    width: 20.0,
                    height: 20.0,
                },
                luminance: LuminanceTokens::multiplier(LuminanceTokens::HDR.highlight),
                ..Default::default()
            },
            Color::WHITE,
        );

        let pixels = halves(&present_and_read(
            &mut renderer,
            wgpu::TextureFormat::Rgba16Float,
            8,
            Display::P3_EXTENDED,
            true,
        ));

        let sample = |x: u32, y: u32| pixels[((y * 64 + x) * 4) as usize];

        let plain = sample(14, 14);
        let emitting = sample(46, 46);

        assert!(
            (plain - 1.0).abs() < 0.05,
            "ordinary white should land at 1.0, got {plain}"
        );

        if !taim_wgpu::graphics::color::GAMMA_CORRECTION {
            // `web-colors` is a *default* feature of the `taim` crate, and
            // the root crate lists itself as a dev-dependency with defaults
            // on, so there is no way to build this suite without it today.
            // Under it the renderer blends sRGB-encoded components directly:
            // there is no linear light in the pipeline, and a luminance
            // multiplier has nothing meaningful to scale.
            //
            // The requirement is therefore that it be *ignored*, not applied
            // wrongly -- an emitting quad must look exactly like an ordinary
            // one rather than hue-shifted. Flipping that default is a
            // deliberate change that moves every blended pixel in the
            // framework, so it is not something this test should presume.
            assert!(
                (emitting - plain).abs() < 0.05,
                "without linear compositing a luminance multiplier must be \
                 ignored, not applied: plain {plain}, emitting {emitting}"
            );

            eprintln!(
                "skipped the HDR half: web-colors is on, so compositing is \
                 not linear and Quad::luminance is inert"
            );

            return;
        }

        assert!(
            emitting > 4.0,
            "a 1000-nit highlight should land near 4.93, got {emitting}"
        );

        let nits = Display::P3_EXTENDED.decode_nits(emitting);

        assert!(
            (nits - 1_000.0).abs() < 60.0,
            "the highlight reached the surface at {nits} nits"
        );
    }

    #[test]
    fn the_default_luminance_emits_nothing_extra() {
        // The safety half: a quad that says nothing about luminance must be
        // bit-identical to one that never had the field.
        assert!((taim_core::renderer::Quad::default().luminance - 1.0).abs() < f32::EPSILON);
        assert!(!LuminanceTokens::default().is_hdr());
    }
}