sinomo-ui-render-wgpu 0.3.0

wgpu renderer boundary for SUI scene frames.
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
use crate::WgpuRenderer;
use crate::output::DebugCaptureEncoding;
use crate::output::DebugCaptureRequest;
use crate::output::DebugCaptureStage;
use crate::output::DebugSdrVisualization;
use crate::output::DisplayColorPrimaries;
use crate::surface::normalize_framebuffer_size;
use half::f16;
use sui_core::Error;
use sui_core::Result;
use sui_core::WindowId;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RgbaImage {
    pub(crate) width: u32,
    pub(crate) height: u32,
    pub(crate) pixels: Vec<u8>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct HdrRgbaImage {
    pub(crate) width: u32,
    pub(crate) height: u32,
    pub(crate) pixels: Vec<f32>,
}

#[derive(Debug, Clone, PartialEq)]
pub enum DebugCaptureArtifact {
    SdrRgba8(RgbaImage),
    HdrLinearRgbaF32(HdrRgbaImage),
}

impl RgbaImage {
    pub fn new(width: u32, height: u32, pixels: Vec<u8>) -> Result<Self> {
        let expected_len = width as usize * height as usize * 4;
        if pixels.len() != expected_len {
            return Err(Error::new(format!(
                "RGBA image pixel buffer length {} does not match {}x{} image size",
                pixels.len(),
                width,
                height
            )));
        }

        Ok(Self {
            width,
            height,
            pixels,
        })
    }

    pub const fn width(&self) -> u32 {
        self.width
    }

    pub const fn height(&self) -> u32 {
        self.height
    }

    pub fn pixels(&self) -> &[u8] {
        &self.pixels
    }

    pub fn into_pixels(self) -> Vec<u8> {
        self.pixels
    }
}

impl HdrRgbaImage {
    pub fn new(width: u32, height: u32, pixels: Vec<f32>) -> Result<Self> {
        let expected_len = width as usize * height as usize * 4;
        if pixels.len() != expected_len {
            return Err(Error::new(format!(
                "HDR RGBA image pixel buffer length {} does not match {}x{} image size",
                pixels.len(),
                width,
                height
            )));
        }

        Ok(Self {
            width,
            height,
            pixels,
        })
    }

    pub const fn width(&self) -> u32 {
        self.width
    }

    pub const fn height(&self) -> u32 {
        self.height
    }

    pub fn pixels(&self) -> &[f32] {
        &self.pixels
    }

    pub fn into_pixels(self) -> Vec<f32> {
        self.pixels
    }
}

pub(crate) fn strip_padded_readback_rows(
    mapped: &[u8],
    bytes_per_row: usize,
    padded_bytes_per_row: usize,
    rows: usize,
) -> Vec<u8> {
    let mut tightly_packed = Vec::with_capacity(bytes_per_row * rows);
    for row in 0..rows {
        let start = row * padded_bytes_per_row;
        tightly_packed.extend_from_slice(&mapped[start..start + bytes_per_row]);
    }
    tightly_packed
}

pub(crate) fn decode_rgba16f_pixels(raw: &[u8]) -> Vec<f32> {
    let mut pixels = Vec::with_capacity(raw.len() / 2);
    for chunk in raw.chunks_exact(2) {
        pixels.push(f16::from_bits(u16::from_le_bytes([chunk[0], chunk[1]])).to_f32());
    }
    pixels
}

pub(crate) fn hdr_image_to_sdr_rgba(
    image: &HdrRgbaImage,
    visualization: DebugSdrVisualization,
    reference_white: f32,
    output_primaries: DisplayColorPrimaries,
) -> Result<RgbaImage> {
    let reference_white = if reference_white.is_finite() && reference_white > 0.0 {
        reference_white
    } else {
        1.0
    };
    let mut pixels = Vec::with_capacity((image.width() * image.height() * 4) as usize);
    for rgba in image.pixels().chunks_exact(4) {
        let normalized = linear_output_primaries_to_srgb(
            [
                rgba[0] / reference_white,
                rgba[1] / reference_white,
                rgba[2] / reference_white,
            ],
            output_primaries,
        );
        match visualization {
            DebugSdrVisualization::ToneMappedColor => {
                // Native HDR final targets store SDR white above 1.0. Divide that
                // headroom back out, convert the output primaries to linear sRGB,
                // then clamp during sRGB encoding. SDR-authored sRGB colors round
                // back to their original PNG bytes; HDR values overflow to white.
                pixels.extend_from_slice(&[
                    linear_to_srgb_capture_u8(normalized[0]),
                    linear_to_srgb_capture_u8(normalized[1]),
                    linear_to_srgb_capture_u8(normalized[2]),
                    linear_alpha_to_capture_u8(rgba[3]),
                ]);
            }
            DebugSdrVisualization::LuminanceHeatmap => {
                let luminance =
                    (normalized[0] * 0.2126 + normalized[1] * 0.7152 + normalized[2] * 0.0722)
                        .max(0.0);
                let normalized = (luminance / (1.0 + luminance)).clamp(0.0, 1.0);
                let value = (normalized * 255.0).round() as u8;
                pixels.extend_from_slice(&[value, value, value, 255]);
            }
            DebugSdrVisualization::HeadroomHeatmap => {
                let headroom = normalized[0].max(normalized[1]).max(normalized[2]).max(0.0);
                let normalized = (headroom / (1.0 + headroom)).clamp(0.0, 1.0);
                let red = (normalized * 255.0).round() as u8;
                let blue = ((1.0 - normalized) * 96.0).round() as u8;
                pixels.extend_from_slice(&[red, 32, blue, 255]);
            }
            DebugSdrVisualization::ClipMask => {
                let clipped = normalized[0].max(normalized[1]).max(normalized[2]) > 1.0;
                if clipped {
                    pixels.extend_from_slice(&[255, 64, 64, 255]);
                } else {
                    pixels.extend_from_slice(&[0, 0, 0, 255]);
                }
            }
        }
    }
    RgbaImage::new(image.width(), image.height(), pixels)
}

pub(crate) fn linear_output_primaries_to_srgb(
    color: [f32; 3],
    output_primaries: DisplayColorPrimaries,
) -> [f32; 3] {
    match output_primaries {
        DisplayColorPrimaries::Srgb => color,
        DisplayColorPrimaries::DisplayP3 => {
            let det = (0.822_461_96 * 0.966_805_76) - (0.177_538_02 * 0.033_194_2);
            let red = (0.966_805_76 * color[0] - 0.177_538_02 * color[1]) / det;
            let green = (-0.033_194_2 * color[0] + 0.822_461_96 * color[1]) / det;
            let blue = (color[2] - (0.017_082_63 * red) - (0.072_397_43 * green)) / 0.910_519_96;
            [red, green, blue]
        }
    }
}

pub(crate) fn encode_hdr_debug_artifact(
    image: HdrRgbaImage,
    request: DebugCaptureRequest,
    sdr_reference_white: f32,
    output_primaries: DisplayColorPrimaries,
) -> Result<DebugCaptureArtifact> {
    match request.encoding {
        DebugCaptureEncoding::Exr => Ok(DebugCaptureArtifact::HdrLinearRgbaF32(image)),
        DebugCaptureEncoding::Png => hdr_image_to_sdr_rgba(
            &image,
            request.sdr_visualization,
            sdr_reference_white,
            output_primaries,
        )
        .map(DebugCaptureArtifact::SdrRgba8),
    }
}

pub(crate) fn linear_to_srgb_capture_u8(channel: f32) -> u8 {
    let value = channel.clamp(0.0, 1.0);
    let encoded = if value <= 0.003_130_8 {
        value * 12.92
    } else {
        (1.055 * value.powf(1.0 / 2.4)) - 0.055
    };
    (encoded.clamp(0.0, 1.0) * 255.0).round() as u8
}

pub(crate) fn linear_alpha_to_capture_u8(alpha: f32) -> u8 {
    (alpha.clamp(0.0, 1.0) * 255.0).round() as u8
}
impl WgpuRenderer {
    pub fn capture_last_frame_rgba(&mut self, window_id: WindowId) -> Result<RgbaImage> {
        match self.capture_last_frame_debug(window_id, DebugCaptureRequest::default())? {
            DebugCaptureArtifact::SdrRgba8(image) => Ok(image),
            DebugCaptureArtifact::HdrLinearRgbaF32(_) => Err(Error::new(format!(
                "window {} returned an HDR debug artifact when SDR RGBA capture was requested",
                window_id.get()
            ))),
        }
    }

    pub fn capture_last_frame_debug(
        &mut self,
        window_id: WindowId,
        request: DebugCaptureRequest,
    ) -> Result<DebugCaptureArtifact> {
        let frame = self.last_frames.get(&window_id).cloned().ok_or_else(|| {
            Error::new(format!(
                "window {} does not have a previously rendered frame available for capture",
                window_id.get()
            ))
        })?;

        let size = normalize_framebuffer_size(frame.surface_size).ok_or_else(|| {
            Error::new(format!(
                "window {} last rendered frame has an invalid framebuffer size",
                window_id.get()
            ))
        })?;

        self.render_debug_capture_stage(&frame, size, request)?;
        self.capture_debug_frame(window_id, request)
    }

    pub fn capture_debug_frame(
        &self,
        window_id: WindowId,
        request: DebugCaptureRequest,
    ) -> Result<DebugCaptureArtifact> {
        match request.stage {
            DebugCaptureStage::FinalComposed => {
                self.capture_final_composed_debug_artifact(window_id, request)
            }
            DebugCaptureStage::HdrIntermediate => {
                let image = self.capture_hdr_intermediate_rgba_f32(window_id)?;
                encode_hdr_debug_artifact(image, request, 1.0, DisplayColorPrimaries::Srgb)
            }
        }
    }

    pub fn capture_rgba(&self, window_id: WindowId) -> Result<RgbaImage> {
        let target = self.offscreen_targets.get(&window_id).ok_or_else(|| {
            Error::new(format!(
                "window {} does not have an offscreen render target available for screenshot capture",
                window_id.get()
            ))
        })?;
        let raw = self.readback_target_bytes(
            &target.texture,
            target.size,
            4,
            "SUI screenshot readback",
            "SUI screenshot readback encoder",
        )?;

        let mut pixels = Vec::with_capacity((target.size.0 * target.size.1 * 4) as usize);
        for chunk in raw.chunks_exact(4) {
            pixels.extend_from_slice(&[chunk[2], chunk[1], chunk[0], chunk[3]]);
        }

        RgbaImage::new(target.size.0, target.size.1, pixels)
    }

    pub fn capture_hdr_intermediate_rgba_f32(&self, window_id: WindowId) -> Result<HdrRgbaImage> {
        let target = self.intermediate_targets.get(&window_id).ok_or_else(|| {
            Error::new(format!(
                "window {} does not have an HDR intermediate target available for debug capture",
                window_id.get()
            ))
        })?;
        let raw = self.readback_target_bytes(
            &target.texture,
            target.size,
            8,
            "SUI HDR intermediate readback",
            "SUI HDR intermediate readback encoder",
        )?;
        let pixels = decode_rgba16f_pixels(&raw);
        HdrRgbaImage::new(target.size.0, target.size.1, pixels)
    }

    pub(crate) fn capture_final_composed_debug_artifact(
        &self,
        window_id: WindowId,
        request: DebugCaptureRequest,
    ) -> Result<DebugCaptureArtifact> {
        let target = self.offscreen_targets.get(&window_id).ok_or_else(|| {
            Error::new(format!(
                "window {} does not have an offscreen target available for final composed debug capture",
                window_id.get()
            ))
        })?;

        match target.format {
            wgpu::TextureFormat::Bgra8UnormSrgb => self
                .capture_rgba(window_id)
                .map(DebugCaptureArtifact::SdrRgba8),
            wgpu::TextureFormat::Rgba16Float => {
                let image = self.capture_hdr_offscreen_rgba_f32(window_id)?;
                encode_hdr_debug_artifact(
                    image,
                    request,
                    self.final_composed_sdr_reference_white(window_id),
                    self.final_composed_output_primaries(window_id),
                )
            }
            other => Err(Error::new(format!(
                "window {} uses unsupported final composed debug capture format {other:?}",
                window_id.get()
            ))),
        }
    }

    pub(crate) fn capture_hdr_offscreen_rgba_f32(
        &self,
        window_id: WindowId,
    ) -> Result<HdrRgbaImage> {
        let target = self.offscreen_targets.get(&window_id).ok_or_else(|| {
            Error::new(format!(
                "window {} does not have an offscreen HDR target available for debug capture",
                window_id.get()
            ))
        })?;
        let raw = self.readback_target_bytes(
            &target.texture,
            target.size,
            8,
            "SUI HDR final readback",
            "SUI HDR final readback encoder",
        )?;
        let pixels = decode_rgba16f_pixels(&raw);
        HdrRgbaImage::new(target.size.0, target.size.1, pixels)
    }

    pub(crate) fn final_composed_sdr_reference_white(&self, window_id: WindowId) -> f32 {
        self.surfaces
            .get(&window_id)
            .map(|surface| {
                crate::output::output_sdr_content_scale(
                    surface.output_strategy,
                    surface.color_management.sdr_content_brightness_nits,
                    surface.display_capabilities.sdr_white_nits,
                )
            })
            .filter(|scale| scale.is_finite() && *scale > 0.0)
            .unwrap_or(1.0)
    }

    pub(crate) fn final_composed_output_primaries(
        &self,
        window_id: WindowId,
    ) -> DisplayColorPrimaries {
        self.surfaces
            .get(&window_id)
            .map(|surface| crate::output::output_primaries(surface.output_strategy))
            .unwrap_or(DisplayColorPrimaries::Srgb)
    }

    pub(crate) fn readback_target_bytes(
        &self,
        texture: &wgpu::Texture,
        size: (u32, u32),
        bytes_per_pixel: u32,
        buffer_label: &'static str,
        encoder_label: &'static str,
    ) -> Result<Vec<u8>> {
        let shared = self
            .shared
            .as_ref()
            .ok_or_else(|| Error::new("renderer has not initialized a wgpu device yet"))?;
        let bytes_per_row = size.0 * bytes_per_pixel;
        let padded_bytes_per_row = bytes_per_row.div_ceil(wgpu::COPY_BYTES_PER_ROW_ALIGNMENT)
            * wgpu::COPY_BYTES_PER_ROW_ALIGNMENT;
        let buffer_size = padded_bytes_per_row as u64 * size.1 as u64;
        let buffer = shared.device.create_buffer(&wgpu::BufferDescriptor {
            label: Some(buffer_label),
            size: buffer_size,
            usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ,
            mapped_at_creation: false,
        });

        let mut encoder = shared
            .device
            .create_command_encoder(&wgpu::CommandEncoderDescriptor {
                label: Some(encoder_label),
            });
        encoder.copy_texture_to_buffer(
            wgpu::TexelCopyTextureInfo {
                texture,
                mip_level: 0,
                origin: wgpu::Origin3d::ZERO,
                aspect: wgpu::TextureAspect::All,
            },
            wgpu::TexelCopyBufferInfo {
                buffer: &buffer,
                layout: wgpu::TexelCopyBufferLayout {
                    offset: 0,
                    bytes_per_row: Some(padded_bytes_per_row),
                    rows_per_image: Some(size.1),
                },
            },
            wgpu::Extent3d {
                width: size.0,
                height: size.1,
                depth_or_array_layers: 1,
            },
        );
        shared.queue.submit([encoder.finish()]);

        let (sender, receiver) = std::sync::mpsc::channel();
        let slice = buffer.slice(..);
        slice.map_async(wgpu::MapMode::Read, move |result| {
            let _ = sender.send(result);
        });
        shared
            .device
            .poll(wgpu::PollType::wait_indefinitely())
            .map_err(|error| {
                Error::new(format!(
                    "failed to poll device for screenshot capture: {error}"
                ))
            })?;
        receiver
            .recv()
            .map_err(|error| {
                Error::new(format!(
                    "failed to receive screenshot readback completion: {error}"
                ))
            })?
            .map_err(|error| {
                Error::new(format!("failed to map screenshot readback buffer: {error}"))
            })?;

        let mapped = slice.get_mapped_range().map_err(|error| {
            Error::new(format!(
                "failed to access mapped screenshot readback buffer: {error}"
            ))
        })?;
        let tightly_packed = strip_padded_readback_rows(
            &mapped,
            bytes_per_row as usize,
            padded_bytes_per_row as usize,
            size.1 as usize,
        );
        drop(mapped);
        buffer.unmap();
        Ok(tightly_packed)
    }
}