euv-core 0.47.1

Core cross-platform runtime for euv featuring arena-based fine-grained reactivity, deterministic layout, flat scene graph, multi-mode rendering, and direct OS platform adapters.
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
use super::*;

/// Default empty pre-warmed optical liquid renderer.
impl Default for LiquidOpticalRenderer {
    /// Creates a `LiquidOpticalRenderer` ready to allocate field scratch buffers on first render.
    ///
    /// # Returns
    ///
    /// - `Self` - Default optical liquid renderer.
    fn default() -> Self {
        Self::new_renderer()
    }
}

/// Scalar field splatting, separable Gaussian smoothing, gradient/caustic synthesis, and parallel optical shading for `LiquidOpticalRenderer`.
impl LiquidOpticalRenderer {
    /// Constructs a new `LiquidOpticalRenderer` with pre-allocated default field storage.
    ///
    /// # Returns
    ///
    /// - `Self` - Ready optical liquid renderer.
    #[must_use]
    pub fn new_renderer() -> Self {
        let init_w: usize = 144;
        let init_h: usize = 108;
        let len: usize = init_w * init_h;
        Self {
            field_w: init_w,
            field_h: init_h,
            phi: vec![0.0; len],
            phi_tmp: vec![0.0; len],
            thickness: vec![0.0; len],
            thick_tmp: vec![0.0; len],
            foam: vec![0.0; len],
            grad_x: vec![0.0; len],
            grad_y: vec![0.0; len],
            caustic: vec![0.0; len],
            last_width: 0,
            last_height: 0,
        }
    }

    /// Computes capped optical render dimensions `(render_w, render_h)` preserving viewport aspect ratio for arbitrary resize and fullscreen modes.
    ///
    /// # Arguments
    ///
    /// - `u32` - Target viewport width in logical pixels.
    /// - `u32` - Target viewport height in logical pixels.
    ///
    /// # Returns
    ///
    /// - `(u32, u32)` - Optimal internal optical RGBA8 `(width, height)` clamped to `[FLUID_RENDER_MIN_DIM, FLUID_RENDER_MAX_DIM]`.
    #[must_use]
    pub fn resolve_render_dimensions(viewport_w: u32, viewport_h: u32) -> (u32, u32) {
        let vw: f32 = (viewport_w.max(FLUID_RENDER_MIN_DIM)) as f32;
        let vh: f32 = (viewport_h.max(FLUID_RENDER_MIN_DIM)) as f32;
        let max_dim: f32 = FLUID_RENDER_MAX_DIM as f32;
        let max_pixels: f32 = (FLUID_RENDER_MAX_DIM as f32) * 420.0;
        let dim_scale: f32 = (max_dim / vw.max(vh)).min(1.0);
        let area_scale: f32 = (max_pixels / (vw * vh).max(1.0)).sqrt().min(1.0);
        let scale: f32 = dim_scale.min(area_scale);
        let rw: u32 =
            ((vw * scale).round() as u32).clamp(FLUID_RENDER_MIN_DIM, FLUID_RENDER_MAX_DIM);
        let rh: u32 =
            ((vh * scale).round() as u32).clamp(FLUID_RENDER_MIN_DIM, FLUID_RENDER_MAX_DIM);
        (rw, rh)
    }

    /// Ensures internal scalar field arrays match `(fw, fh)` without reallocating when dimensions are unchanged.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable optical renderer.
    /// - `usize` - Target field width in cells.
    /// - `usize` - Target field height in cells.
    fn ensure_field_capacity(&mut self, fw: usize, fh: usize) {
        let len: usize = fw * fh;
        if self.get_field_w() == fw && self.get_field_h() == fh && self.get_phi().len() == len {
            return;
        }
        self.set_field_w(fw);
        self.set_field_h(fh);
        let Self {
            phi,
            phi_tmp,
            thickness,
            thick_tmp,
            foam,
            grad_x,
            grad_y,
            caustic,
            ..
        } = self;
        phi.resize(len, 0.0);
        phi_tmp.resize(len, 0.0);
        thickness.resize(len, 0.0);
        thick_tmp.resize(len, 0.0);
        foam.resize(len, 0.0);
        grad_x.resize(len, 0.0);
        grad_y.resize(len, 0.0);
        caustic.resize(len, 0.0);
    }

    /// Applies one separable 5-tap binomial Gaussian pass (`[1, 4, 6, 4, 1] / 16`) across `src` using `tmp`.
    ///
    /// # Arguments
    ///
    /// - `&mut [f32]` - Input and output field slice.
    /// - `&mut [f32]` - Horizontal pass scratch slice.
    /// - `usize` - Field width in cells.
    /// - `usize` - Field height in cells.
    fn blur_field_separable(src: &mut [f32], tmp: &mut [f32], w: usize, h: usize) {
        if w < 5 || h < 5 {
            return;
        }
        let inv16: f32 = 1.0 / 16.0;
        for y in 0..h {
            let row: usize = y * w;
            tmp[row] = (src[row] * 11.0 + src[row + 1] * 4.0 + src[row + 2]) * inv16;
            tmp[row + 1] =
                (src[row] * 5.0 + src[row + 1] * 6.0 + src[row + 2] * 4.0 + src[row + 3]) * inv16;
            for x in 2..(w - 2) {
                let idx: usize = row + x;
                tmp[idx] = (src[idx - 2]
                    + src[idx - 1] * 4.0
                    + src[idx] * 6.0
                    + src[idx + 1] * 4.0
                    + src[idx + 2])
                    * inv16;
            }
            let r2: usize = row + w - 2;
            tmp[r2] = (src[r2 - 2] + src[r2 - 1] * 4.0 + src[r2] * 6.0 + src[r2 + 1] * 5.0) * inv16;
            let r1: usize = row + w - 1;
            tmp[r1] = (src[r1 - 2] + src[r1 - 1] * 4.0 + src[r1] * 11.0) * inv16;
        }
        for y in 0..h {
            let ym2: usize = y.saturating_sub(2) * w;
            let ym1: usize = y.saturating_sub(1) * w;
            let y0: usize = y * w;
            let yp1: usize = (y + 1).min(h - 1) * w;
            let yp2: usize = (y + 2).min(h - 1) * w;
            for x in 0..w {
                src[y0 + x] = (tmp[ym2 + x]
                    + tmp[ym1 + x] * 4.0
                    + tmp[y0 + x] * 6.0
                    + tmp[yp1 + x] * 4.0
                    + tmp[yp2 + x])
                    * inv16;
            }
        }
    }

    /// Splats fluid particles and bubbles onto scalar fields, runs separable smoothing, and derives analytical surface normals and optical caustics.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable optical renderer.
    /// - `&FluidSimSnapshot` - Interpolated simulation snapshot to splat.
    fn build_optical_fields(&mut self, snapshot: &FluidSimSnapshot) {
        let fw: usize = self.get_field_w();
        let fh: usize = self.get_field_h();
        let len: usize = fw * fh;
        let vw: f32 = snapshot.get_vessel_width_m().max(1.0e-4);
        let vh: f32 = snapshot.get_vessel_height_m().max(1.0e-4);
        let n: usize = snapshot.get_particle_count().min(FLUID_MAX_PARTICLES);
        let h_m: f32 = snapshot.get_kernel_radius_m().max(0.003);
        let scale_x: f32 = ((fw.saturating_sub(1)) as f32) / vw;
        let scale_y: f32 = ((fh.saturating_sub(1)) as f32) / vh;
        let splat_rx: f32 = (h_m * 0.78 * scale_x).clamp(2.2, 7.5);
        let splat_ry: f32 = (h_m * 0.78 * scale_y).clamp(2.2, 7.5);
        let irx: i32 = splat_rx.ceil() as i32;
        let iry: i32 = splat_ry.ceil() as i32;
        let inv_rx: f32 = 1.0 / splat_rx;
        let inv_ry: f32 = 1.0 / splat_ry;
        let px: &[f32] = snapshot.get_pos_x();
        let py: &[f32] = snapshot.get_pos_y();
        let vx: &[f32] = snapshot.get_vel_x();
        let vy: &[f32] = snapshot.get_vel_y();
        let dens_ratio: &[f32] = snapshot.get_density_ratio();
        let fw_i: i32 = fw as i32;
        let fh_i: i32 = fh as i32;
        let Self {
            phi,
            phi_tmp,
            thickness,
            thick_tmp,
            foam,
            grad_x,
            grad_y,
            caustic,
            ..
        } = self;
        phi[..len].fill(0.0);
        thickness[..len].fill(0.0);
        foam[..len].fill(0.0);
        for i in 0..n {
            let fx: f32 = px[i] * scale_x;
            let fy: f32 = py[i] * scale_y;
            let cx: i32 = fx.round() as i32;
            let cy: i32 = fy.round() as i32;
            let x0: i32 = (cx - irx).max(0);
            let x1: i32 = (cx + irx).min(fw_i - 1);
            let y0: i32 = (cy - iry).max(0);
            let y1: i32 = (cy + iry).min(fh_i - 1);
            let spd2: f32 = vx[i] * vx[i] + vy[i] * vy[i];
            let foam_emit: f32 = if spd2 > 0.06 && dens_ratio[i] < 0.88 {
                ((spd2.sqrt() - 0.24) * 1.15).clamp(0.0, 0.95)
            } else {
                0.0
            };
            for gy_i in y0..=y1 {
                let dy: f32 = ((gy_i as f32) - fy) * inv_ry;
                let dy2: f32 = dy * dy;
                if dy2 >= 1.0 {
                    continue;
                }
                let row: usize = (gy_i as usize) * fw;
                for gx_i in x0..=x1 {
                    let dx: f32 = ((gx_i as f32) - fx) * inv_rx;
                    let q2: f32 = dx * dx + dy2;
                    if q2 < 1.0 {
                        let omt: f32 = 1.0 - q2;
                        let w_kern: f32 = omt * omt;
                        let idx: usize = row + (gx_i as usize);
                        phi[idx] += w_kern * 0.42;
                        thickness[idx] += w_kern * 0.28;
                        if foam_emit > 0.0 {
                            foam[idx] += w_kern * foam_emit * 0.45;
                        }
                    }
                }
            }
        }
        let bub_cnt: usize = snapshot.get_bubble_count().min(FLUID_MAX_BUBBLES);
        for bub in snapshot.get_bubbles().iter().take(bub_cnt) {
            if bub.get_foam_factor() > 0.35 {
                let bx: usize =
                    ((bub.get_x() * scale_x).round() as i32).clamp(0, fw_i - 1) as usize;
                let by: usize =
                    ((bub.get_y() * scale_y).round() as i32).clamp(0, fh_i - 1) as usize;
                let fade: f32 =
                    (1.0 - bub.get_age() / bub.get_lifetime().max(0.05)).clamp(0.0, 1.0);
                foam[by * fw + bx] += bub.get_foam_factor() * fade * 0.65;
            }
        }
        Self::blur_field_separable(phi, phi_tmp, fw, fh);
        Self::blur_field_separable(phi, phi_tmp, fw, fh);
        Self::blur_field_separable(thickness, thick_tmp, fw, fh);
        Self::blur_field_separable(foam, phi_tmp, fw, fh);
        let inv_fw: f32 = 1.0 / (fw.max(1) as f32);
        let inv_fh: f32 = 1.0 / (fh.max(1) as f32);
        let shimmer_phase: f32 = snapshot.get_sim_time_secs() * 2.15;
        for y in 0..fh {
            let ym: usize = y.saturating_sub(1) * fw;
            let y0: usize = y * fw;
            let yp: usize = (y + 1).min(fh - 1) * fw;
            let v_f: f32 = ((y as f32) + 0.5) * inv_fh;
            let cos_va: f32 = (v_f * 22.0 - shimmer_phase * 0.8).cos();
            let sin_vb: f32 = (v_f * 30.0 + shimmer_phase * 0.9).sin();
            for x in 0..fw {
                let xm: usize = x.saturating_sub(1);
                let xp: usize = (x + 1).min(fw - 1);
                let idx: usize = y0 + x;
                let p_c: f32 = phi[idx];
                let p_l: f32 = phi[y0 + xm];
                let p_r: f32 = phi[y0 + xp];
                let p_u: f32 = phi[ym + x];
                let p_d: f32 = phi[yp + x];
                let gx_v: f32 = (p_r - p_l) * 0.5;
                let gy_v: f32 = (p_d - p_u) * 0.5;
                grad_x[idx] = gx_v;
                grad_y[idx] = gy_v;
                let lap: f32 = (p_l + p_r + p_u + p_d) - 4.0 * p_c;
                let slope_sq: f32 = gx_v * gx_v + gy_v * gy_v;
                let light_focus: f32 = (-gx_v * 0.35 - gy_v * 0.80).max(0.0);
                let u_f: f32 = ((x as f32) + 0.5) * inv_fw;
                let wave_a: f32 = ((u_f - gx_v * 0.14) * 26.0 + shimmer_phase).sin() * cos_va;
                let wave_b: f32 = ((u_f + gy_v * 0.12) * 34.0 - shimmer_phase * 0.7).cos() * sin_vb;
                let shim: f32 = ((wave_a + wave_b) * 0.5).max(0.0);
                let focus: f32 = ((-lap * 2.4).max(0.0)
                    + light_focus * 1.15
                    + slope_sq * 1.8
                    + shim * shim * (0.20 + thickness[idx] * 0.25))
                    .clamp(0.0, 1.35);
                caustic[idx] = focus;
            }
        }
    }

    /// Stamps crisp translucent rising underwater micro-bubbles with refracted dark bottoms and bright specular crescents.
    ///
    /// # Arguments
    ///
    /// - `&FluidSimSnapshot` - Interpolated simulation snapshot containing active bubbles.
    /// - `usize` - Framebuffer width in pixels.
    /// - `usize` - Framebuffer height in pixels.
    /// - `&mut [u8]` - Destination RGBA8 framebuffer slice.
    fn stamp_rising_bubbles(
        snapshot: &FluidSimSnapshot,
        out_w: usize,
        out_h: usize,
        rgba: &mut [u8],
    ) {
        let bub_cnt: usize = snapshot.get_bubble_count().min(FLUID_MAX_BUBBLES);
        if bub_cnt == 0 {
            return;
        }
        let vw: f32 = snapshot.get_vessel_width_m().max(1.0e-4);
        let vh: f32 = snapshot.get_vessel_height_m().max(1.0e-4);
        let px_per_m_x: f32 = (out_w as f32) / vw;
        let px_per_m_y: f32 = (out_h as f32) / vh;
        let px_per_m: f32 = px_per_m_x.min(px_per_m_y);
        let max_x: i32 = (out_w as i32) - 1;
        let max_y: i32 = (out_h as i32) - 1;
        for bub in snapshot.get_bubbles().iter().take(bub_cnt) {
            let fade: f32 = (1.0 - bub.get_age() / bub.get_lifetime().max(0.05)).clamp(0.0, 1.0);
            if fade <= 0.04 {
                continue;
            }
            let cx: f32 = bub.get_x() * px_per_m_x;
            let cy: f32 = bub.get_y() * px_per_m_y;
            let rad_px: f32 = (bub.get_radius() * px_per_m).clamp(1.6, 5.5);
            let ir: i32 = (rad_px + 1.5).ceil() as i32;
            let x0: i32 = ((cx as i32) - ir).max(3);
            let x1: i32 = ((cx as i32) + ir).min(max_x - 3);
            let y0: i32 = ((cy as i32) - ir).max(3);
            let y1: i32 = ((cy as i32) + ir).min(max_y - 3);
            let inv_r: f32 = 1.0 / rad_px;
            let foam_f: f32 = bub.get_foam_factor();
            for py in y0..=y1 {
                let dy: f32 = ((py as f32) + 0.5 - cy) * inv_r;
                let dy2: f32 = dy * dy;
                if dy2 >= 1.2 {
                    continue;
                }
                let row: usize = (py as usize) * out_w * 4;
                for px in x0..=x1 {
                    let dx: f32 = ((px as f32) + 0.5 - cx) * inv_r;
                    let d2: f32 = dx * dx + dy2;
                    if d2 >= 1.05 {
                        continue;
                    }
                    let dist: f32 = d2.sqrt();
                    let edge: f32 = 1.0 - fluid_smoothstep(0.75, 1.02, dist);
                    let ring: f32 = fluid_smoothstep(0.45, 0.85, dist) * edge;
                    let spec_dx: f32 = dx + 0.36;
                    let spec_dy: f32 = dy + 0.38;
                    let spec: f32 = (1.0 - (spec_dx * spec_dx + spec_dy * spec_dy) * 3.8).max(0.0);
                    let alpha: f32 = ((ring * 0.48 + spec * 0.85 + foam_f * edge * 0.45) * fade)
                        .clamp(0.0, 0.92);
                    if alpha <= 0.01 {
                        continue;
                    }
                    let dst: usize = row + (px as usize) * 4;
                    let inv_a: f32 = 1.0 - alpha;
                    let tint_r: f32 = 212.0 + 43.0 * spec;
                    let tint_g: f32 = 242.0 + 13.0 * spec;
                    let tint_b: f32 = 255.0;
                    rgba[dst] = ((rgba[dst] as f32) * inv_a + tint_r * alpha) as u8;
                    rgba[dst + 1] = ((rgba[dst + 1] as f32) * inv_a + tint_g * alpha) as u8;
                    rgba[dst + 2] = ((rgba[dst + 2] as f32) * inv_a + tint_b * alpha) as u8;
                }
            }
        }
    }

    /// Renders `snapshot` directly into an existing RGBA8 byte buffer `rgba_out` sized `(width, height)` with zero steady-state heap allocations.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable optical liquid renderer.
    /// - `&FluidSimSnapshot` - Interpolated simulation snapshot.
    /// - `u32` - Output width in pixels.
    /// - `u32` - Output height in pixels.
    /// - `&mut Vec<u8>` - Destination RGBA8 byte buffer resized only if needed.
    ///
    /// # Returns
    ///
    /// - `(u32, u32)` - Actual rendered `(width, height)` dimensions.
    pub fn render_snapshot_into_vec(
        &mut self,
        snapshot: &FluidSimSnapshot,
        width: u32,
        height: u32,
        rgba_out: &mut Vec<u8>,
    ) -> (u32, u32) {
        let out_w: usize = width.max(32) as usize;
        let out_h: usize = height.max(32) as usize;
        let needed: usize = out_w * out_h * 4;
        if rgba_out.len() != needed {
            rgba_out.resize(needed, 255);
        }
        let fw: usize = (out_w / 3).clamp(FLUID_FIELD_MIN_DIM, FLUID_FIELD_MAX_W);
        let fh: usize = (out_h / 3).clamp(FLUID_FIELD_MIN_DIM, FLUID_FIELD_MAX_H);
        self.ensure_field_capacity(fw, fh);
        self.build_optical_fields(snapshot);
        let gx: f32 = snapshot.get_gravity_x();
        let gy: f32 = snapshot.get_gravity_y();
        let g_mag: f32 = (gx * gx + gy * gy).sqrt();
        let (up_x, up_y): (f32, f32) = if g_mag > 0.25 {
            (-gx / g_mag, -gy / g_mag)
        } else {
            (0.0, -1.0)
        };
        let b_cnt: usize = snapshot
            .get_buoyant_body_count()
            .min(FLUID_MAX_BUOYANT_BODIES);
        let mut bodies_copy: [BuoyantBody; FLUID_MAX_BUOYANT_BODIES] =
            [BuoyantBody::default(); FLUID_MAX_BUOYANT_BODIES];
        bodies_copy[..b_cnt].copy_from_slice(&snapshot.get_buoyant_bodies()[..b_cnt]);
        let min_px: f32 = (out_w.min(out_h)) as f32;
        let params: FluidShadeParams = FluidShadeParams {
            out_w,
            out_h,
            field_w: fw,
            field_h: fh,
            vessel_w: snapshot.get_vessel_width_m().max(1.0e-4),
            vessel_h: snapshot.get_vessel_height_m().max(1.0e-4),
            up_x,
            up_y,
            bezel_px: (min_px * 0.018).clamp(4.0, 9.0),
            corner_px: (min_px * FLUID_CORNER_RADIUS_RATIO).clamp(10.0, 28.0),
            body_count: b_cnt,
            bodies: bodies_copy,
        };
        let fields: FluidFieldViews<'_> = FluidFieldViews {
            phi: self.get_phi().as_slice(),
            thickness: self.get_thickness().as_slice(),
            foam: self.get_foam().as_slice(),
            grad_x: self.get_grad_x().as_slice(),
            grad_y: self.get_grad_y().as_slice(),
            caustic: self.get_caustic().as_slice(),
        };
        let dst_ptr: usize = rgba_out.as_mut_ptr() as usize;
        let row_stride: usize = out_w * 4;
        parallel_for_ranges(out_h, FLUID_RENDER_BAND_ROWS, |y0: usize, y1: usize| {
            let base: *mut u8 = dst_ptr as *mut u8;
            for y in y0..y1 {
                // SAFETY: `parallel_for_ranges` partitions `[0, out_h)` into disjoint scanline bands within `rgba_out`.
                let row_slice: &mut [u8] =
                    unsafe { from_raw_parts_mut(base.add(y * row_stride), row_stride) };
                shade_fluid_scanline(&params, &fields, y, row_slice);
            }
        });
        Self::stamp_rising_bubbles(snapshot, out_w, out_h, rgba_out.as_mut_slice());
        self.set_last_width(out_w as u32);
        self.set_last_height(out_h as u32);
        (out_w as u32, out_h as u32)
    }

    /// Renders `snapshot` directly into a bounded zero-allocation `RenderBufferPool` slot tagged with `fence_id`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable optical liquid renderer.
    /// - `&FluidSimSnapshot` - Interpolated simulation snapshot.
    /// - `u32` - Target viewport width in logical pixels.
    /// - `u32` - Target viewport height in logical pixels.
    /// - `&RenderBufferPool` - Bounded RGBA8 frame buffer pool.
    /// - `u64` - Fence identifier guarding slot reclamation.
    ///
    /// # Returns
    ///
    /// - `Option<PooledFrameLease>` - Rendered zero-copy frame lease, or `None` if all bounded pool slots are in flight.
    #[must_use]
    pub fn render_snapshot_into_pool(
        &mut self,
        snapshot: &FluidSimSnapshot,
        viewport_w: u32,
        viewport_h: u32,
        pool: &RenderBufferPool,
        fence_id: u64,
    ) -> Option<PooledFrameLease> {
        let (rw, rh): (u32, u32) = Self::resolve_render_dimensions(viewport_w, viewport_h);
        let mut lease: PooledFrameLease = pool.acquire(rw, rh, fence_id)?;
        let (aw, ah): (u32, u32) =
            self.render_snapshot_into_vec(snapshot, rw, rh, lease.get_mut_pixels());
        lease.set_dimensions(aw, ah);
        Some(lease)
    }
}