euv-core 0.48.0

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
use super::*;

/// Computes a compile-time IEEE-754 `f32` square root via Newton-Raphson iteration for LUT generation.
///
/// # Arguments
///
/// - `f32` - Non-negative input value.
///
/// # Returns
///
/// - `f32` - Square root of `value`.
#[must_use]
pub(crate) const fn const_sqrt_f32(value: f32) -> f32 {
    if value <= 0.0 {
        return 0.0;
    }
    let mut guess: f32 = if value > 1.0 { value * 0.5 } else { 0.5 };
    let mut iter: usize = 0;
    while iter < 14 {
        guess = 0.5 * (guess + value / guess);
        iter += 1;
    }
    guess
}

/// Builds the 256-entry compile-time sRGB byte to gamma-2.0 linear-light lookup table.
///
/// # Returns
///
/// - `[f32; 256]` - Normalized linear-light luminance for each `u8` channel value.
#[must_use]
pub(crate) const fn build_srgb_to_lin_lut() -> [f32; 256] {
    let mut table: [f32; 256] = [0.0; 256];
    let inv_255: f32 = 1.0 / 255.0;
    let mut idx: usize = 0;
    while idx < 256 {
        let norm: f32 = (idx as f32) * inv_255;
        table[idx] = norm * norm;
        idx += 1;
    }
    table
}

/// Builds the 4096-entry compile-time linear-light to Q12.4 fixed-point sRGB lookup table (1/16 LSB precision for sub-LSB dithering).
///
/// # Returns
///
/// - `[u16; 4096]` - Fixed-point `sRGB * 16` values covering linear `[0.0, 1.0]`.
#[must_use]
pub(crate) const fn build_lin_to_srgb_q4_lut() -> [u16; 4096] {
    let mut table: [u16; 4096] = [0; 4096];
    let inv_4095: f32 = 1.0 / 4095.0;
    let mut idx: usize = 0;
    while idx < 4096 {
        let lin: f32 = (idx as f32) * inv_4095;
        let scaled: f32 = const_sqrt_f32(lin) * 255.0 * 16.0 + 0.5;
        let clamped: f32 = if scaled < 0.0 {
            0.0
        } else if scaled > 4080.0 {
            4080.0
        } else {
            scaled
        };
        table[idx] = clamped as u16;
        idx += 1;
    }
    table
}

/// Precomputes bit-exact per-channel `[u8; 256]` sRGB linear-light source-over lookup tables for constant translucent `color`.
///
/// # Arguments
///
/// - `Color` - Foreground translucent color (`0 < a < 255`).
///
/// # Returns
///
/// - `[[u8; 256]; 4]` - Lookup tables `[lut_r, lut_g, lut_b, lut_a]` mapping destination channel bytes to blended output bytes.
#[must_use]
pub(crate) fn build_linear_blend_luts(color: Color) -> [[u8; 256]; 4] {
    let sa: u8 = color.get_a();
    let a: f32 = (sa as f32) * (1.0 / 255.0);
    let inv_a: f32 = 1.0 - a;
    let sr_a: f32 = SRGB_TO_LIN_LUT[color.get_r() as usize] * a;
    let sg_a: f32 = SRGB_TO_LIN_LUT[color.get_g() as usize] * a;
    let sb_a: f32 = SRGB_TO_LIN_LUT[color.get_b() as usize] * a;
    let sa_u32: u32 = sa as u32;
    let inv_a_u32: u32 = 255 - sa_u32;
    let same_rg: bool = color.get_r() == color.get_g();
    let same_rb: bool = color.get_r() == color.get_b();
    let mut lut_r: [u8; 256] = [0; 256];
    let mut lut_g: [u8; 256] = [0; 256];
    let mut lut_b: [u8; 256] = [0; 256];
    let mut lut_a: [u8; 256] = [0; 256];
    for d in 0..256 {
        let lin_inv: f32 = SRGB_TO_LIN_LUT[d] * inv_a;
        let ir: usize = ((sr_a + lin_inv) * 4095.0).clamp(0.0, 4095.0) as usize;
        let out_r: u8 = ((LIN_TO_SRGB_Q4_LUT[ir] + 8) >> 4).min(255) as u8;
        lut_r[d] = out_r;
        lut_g[d] = if same_rg {
            out_r
        } else {
            let ig: usize = ((sg_a + lin_inv) * 4095.0).clamp(0.0, 4095.0) as usize;
            ((LIN_TO_SRGB_Q4_LUT[ig] + 8) >> 4).min(255) as u8
        };
        lut_b[d] = if same_rb {
            out_r
        } else {
            let ib: usize = ((sb_a + lin_inv) * 4095.0).clamp(0.0, 4095.0) as usize;
            ((LIN_TO_SRGB_Q4_LUT[ib] + 8) >> 4).min(255) as u8
        };
        let va: u32 = (d as u32) * inv_a_u32 + 128;
        lut_a[d] = (sa_u32 + ((va + (va >> 8)) >> 8)).min(255) as u8;
    }
    [lut_r, lut_g, lut_b, lut_a]
}

/// Precomputes a 256-entry Q12.4 sRGB linear-light source-over table for one constant shadow channel and opacity.
///
/// # Arguments
///
/// - `u8` - Source shadow sRGB channel byte.
/// - `f32` - Clamped continuous shadow opacity in `[0.0, 1.0]`.
///
/// # Returns
///
/// - `[u16; 256]` - Q12.4 (`sRGB * 16`, in `0..=4080`) output table indexed by destination sRGB byte.
#[must_use]
pub(crate) fn build_shadow_q4_lut_channel(src_ch: u8, alpha: f32) -> [u16; 256] {
    let inv_a: f32 = 1.0 - alpha;
    let src_lin_a: f32 = SRGB_TO_LIN_LUT[src_ch as usize] * alpha;
    let mut table: [u16; 256] = [0; 256];
    for (d, slot) in table.iter_mut().enumerate() {
        let lin_idx: usize =
            ((src_lin_a + SRGB_TO_LIN_LUT[d] * inv_a) * 4095.0).clamp(0.0, 4095.0) as usize;
        *slot = LIN_TO_SRGB_Q4_LUT[lin_idx];
    }
    table
}

/// Composites one RGBA8 pixel over `px` with SWAR `(R, B)` packing and opaque-destination alpha fast path.
///
/// # Arguments
///
/// - `&mut [u8; 4]` - Mutable destination RGBA8 pixel.
/// - `u32` - Packed source red/blue channels `sr | (sb << 16)`.
/// - `u32` - Source green channel `sg` in `0..=255`.
/// - `u32` - Effective source alpha in `1..=254`.
#[inline(always)]
fn blend_glyph_pixel_swar(px: &mut [u8; 4], sr_sb: u32, sg: u32, sa_u32: u32) {
    let inv_a: u32 = 255 - sa_u32;
    let dst: u32 = u32::from_le_bytes(*px);
    let dst_rb: u32 = dst & 0x00FF_00FF;
    let dst_g: u32 = (dst >> 8) & 0xFF;
    let rb_raw: u32 = sr_sb * sa_u32 + dst_rb * inv_a + 0x0080_0080;
    let rb_out: u32 = ((rb_raw + ((rb_raw >> 8) & 0x00FF_00FF)) & 0xFF00_FF00) >> 8;
    let vg: u32 = sg * sa_u32 + dst_g * inv_a + 128;
    let g_out: u32 = (vg + (vg >> 8)) & 0xFF00;
    let a_out: u32 = if dst >= 0xFF00_0000 {
        0xFF00_0000
    } else {
        let va: u32 = (dst >> 24) * inv_a + 128;
        (sa_u32 + ((va + (va >> 8)) >> 8)).min(255) << 24
    };
    *px = (rb_out | g_out | a_out).to_le_bytes();
}

/// Composites a clipped glyph coverage row onto a destination RGBA8 span with SWAR blending, opaque specialization, and 8/4-byte zero-alpha fast-skip.
///
/// # Arguments
///
/// - `&mut [u8]` - Destination RGBA8 byte slice covering `coverage.len()` pixels.
/// - `&[u8]` - Clipped glyph alpha coverage bytes.
/// - `Color` - Foreground text RGBA color (`color.get_a() > 0`).
///
/// # Returns
///
/// - `usize` - Number of non-zero coverage pixels shaded.
#[inline(always)]
pub(crate) fn blend_glyph_row_span(dst_span: &mut [u8], coverage: &[u8], color: Color) -> usize {
    let color_a: u32 = color.get_a() as u32;
    let sr_u8: u8 = color.get_r();
    let sg_u8: u8 = color.get_g();
    let sb_u8: u8 = color.get_b();
    let sr_sb: u32 = (sr_u8 as u32) | ((sb_u8 as u32) << 16);
    let sg: u32 = sg_u8 as u32;
    let dst_px: &mut [[u8; 4]] = dst_span.as_chunks_mut::<4>().0;
    let len: usize = dst_px.len().min(coverage.len());
    let (dst_blocks, dst_tail): (&mut [[[u8; 4]; 8]], &mut [[u8; 4]]) =
        dst_px[..len].as_chunks_mut::<8>();
    let (cov_blocks, cov_tail): (&[[u8; 8]], &[u8]) = coverage[..len].as_chunks::<8>();
    let mut shaded: usize = 0;
    if color_a == 255 {
        let opaque_px: [u8; 4] = [sr_u8, sg_u8, sb_u8, 255];
        let opaque_block8: [[u8; 4]; 8] = [opaque_px; 8];
        let opaque_block4: [[u8; 4]; 4] = [opaque_px; 4];
        for (d_blk, c_blk) in dst_blocks.iter_mut().zip(cov_blocks.iter()) {
            let word: u64 = u64::from_ne_bytes(*c_blk);
            if word == 0 {
                continue;
            }
            if word == u64::MAX {
                *d_blk = opaque_block8;
                shaded += 8;
                continue;
            }
            for (px, alpha_byte) in d_blk.iter_mut().zip(c_blk.iter().copied()) {
                if alpha_byte == 0 {
                    continue;
                }
                if alpha_byte == 255 {
                    *px = opaque_px;
                } else {
                    blend_glyph_pixel_swar(px, sr_sb, sg, alpha_byte as u32);
                }
                shaded += 1;
            }
        }
        let (dst_t4, dst_rem): (&mut [[[u8; 4]; 4]], &mut [[u8; 4]]) =
            dst_tail.as_chunks_mut::<4>();
        let (cov_t4, cov_rem): (&[[u8; 4]], &[u8]) = cov_tail.as_chunks::<4>();
        for (d_blk, c_blk) in dst_t4.iter_mut().zip(cov_t4.iter()) {
            let word: u32 = u32::from_ne_bytes(*c_blk);
            if word == 0 {
                continue;
            }
            if word == u32::MAX {
                *d_blk = opaque_block4;
                shaded += 4;
                continue;
            }
            for (px, alpha_byte) in d_blk.iter_mut().zip(c_blk.iter().copied()) {
                if alpha_byte == 0 {
                    continue;
                }
                if alpha_byte == 255 {
                    *px = opaque_px;
                } else {
                    blend_glyph_pixel_swar(px, sr_sb, sg, alpha_byte as u32);
                }
                shaded += 1;
            }
        }
        for (px, alpha_byte) in dst_rem.iter_mut().zip(cov_rem.iter().copied()) {
            if alpha_byte == 0 {
                continue;
            }
            if alpha_byte == 255 {
                *px = opaque_px;
            } else {
                blend_glyph_pixel_swar(px, sr_sb, sg, alpha_byte as u32);
            }
            shaded += 1;
        }
        return shaded;
    }
    for (d_blk, c_blk) in dst_blocks.iter_mut().zip(cov_blocks.iter()) {
        if u64::from_ne_bytes(*c_blk) == 0 {
            continue;
        }
        for (px, alpha_byte) in d_blk.iter_mut().zip(c_blk.iter().copied()) {
            if alpha_byte == 0 {
                continue;
            }
            let prod: u32 = color_a * (alpha_byte as u32) + 128;
            let sa_u32: u32 = (prod + (prod >> 8)) >> 8;
            if sa_u32 > 0 {
                blend_glyph_pixel_swar(px, sr_sb, sg, sa_u32);
            }
            shaded += 1;
        }
    }
    for (px, alpha_byte) in dst_tail.iter_mut().zip(cov_tail.iter().copied()) {
        if alpha_byte == 0 {
            continue;
        }
        let prod: u32 = color_a * (alpha_byte as u32) + 128;
        let sa_u32: u32 = (prod + (prod >> 8)) >> 8;
        if sa_u32 > 0 {
            blend_glyph_pixel_swar(px, sr_sb, sg, sa_u32);
        }
        shaded += 1;
    }
    shaded
}

/// Fills an RGBA8 byte span with solid `rgba` using `slice::fill` for uniform-byte colors and 32-byte wide stores otherwise.
///
/// # Arguments
///
/// - `&mut [u8]` - Target RGBA8 byte slice whose length is a multiple of 4.
/// - `[u8; 4]` - Solid RGBA8 pixel bytes to broadcast across `span`.
#[inline(always)]
pub(crate) fn fill_rgba_span_32(span: &mut [u8], rgba: [u8; 4]) {
    if rgba[0] == rgba[1] && rgba[0] == rgba[2] && rgba[0] == rgba[3] {
        span.fill(rgba[0]);
        return;
    }
    let wide32: [u8; 32] = [
        rgba[0], rgba[1], rgba[2], rgba[3], rgba[0], rgba[1], rgba[2], rgba[3], rgba[0], rgba[1],
        rgba[2], rgba[3], rgba[0], rgba[1], rgba[2], rgba[3], rgba[0], rgba[1], rgba[2], rgba[3],
        rgba[0], rgba[1], rgba[2], rgba[3], rgba[0], rgba[1], rgba[2], rgba[3], rgba[0], rgba[1],
        rgba[2], rgba[3],
    ];
    let (blocks, tail): (&mut [[u8; 32]], &mut [u8]) = span.as_chunks_mut::<32>();
    for chunk in blocks.iter_mut() {
        *chunk = wide32;
    }
    for px in tail.as_chunks_mut::<4>().0.iter_mut() {
        *px = rgba;
    }
}

/// Composites a pre-clipped RGBA8 byte span using precomputed bit-exact 256-entry linear-light channel lookup tables with 4-pixel run-skipping.
///
/// # Arguments
///
/// - `&mut [u8]` - Pre-clipped destination RGBA8 byte slice.
/// - `&[[u8; 256]; 4]` - Precomputed per-channel lookup tables `[lut_r, lut_g, lut_b, lut_a]`.
#[inline(always)]
pub(crate) fn fill_span_linear_luts_slice(span: &mut [u8], luts: &[[u8; 256]; 4]) {
    let [lut_r, lut_g, lut_b, lut_a]: &[[u8; 256]; 4] = luts;
    let dst_px: &mut [[u8; 4]] = span.as_chunks_mut::<4>().0;
    let Some(&first_px) = dst_px.first() else {
        return;
    };
    let mut prev_src: u32 = u32::from_ne_bytes(first_px);
    let mut prev_dst: [u8; 4] = [
        lut_r[first_px[0] as usize],
        lut_g[first_px[1] as usize],
        lut_b[first_px[2] as usize],
        lut_a[first_px[3] as usize],
    ];
    let mut prev_dst4: [[u8; 4]; 4] = [prev_dst; 4];
    let (blocks, tail): (&mut [[[u8; 4]; 4]], &mut [[u8; 4]]) = dst_px.as_chunks_mut::<4>();
    for blk in blocks.iter_mut() {
        let p0: [u8; 4] = blk[0];
        let p1: [u8; 4] = blk[1];
        let p2: [u8; 4] = blk[2];
        let p3: [u8; 4] = blk[3];
        let w0: u32 = u32::from_ne_bytes(p0);
        let w1: u32 = u32::from_ne_bytes(p1);
        let w2: u32 = u32::from_ne_bytes(p2);
        let w3: u32 = u32::from_ne_bytes(p3);
        let diff: u32 = (w0 ^ prev_src) | (w1 ^ prev_src) | (w2 ^ prev_src) | (w3 ^ prev_src);
        if diff == 0 {
            *blk = prev_dst4;
            continue;
        }
        let d0: [u8; 4] = [
            lut_r[p0[0] as usize],
            lut_g[p0[1] as usize],
            lut_b[p0[2] as usize],
            lut_a[p0[3] as usize],
        ];
        let d1: [u8; 4] = [
            lut_r[p1[0] as usize],
            lut_g[p1[1] as usize],
            lut_b[p1[2] as usize],
            lut_a[p1[3] as usize],
        ];
        let d2: [u8; 4] = [
            lut_r[p2[0] as usize],
            lut_g[p2[1] as usize],
            lut_b[p2[2] as usize],
            lut_a[p2[3] as usize],
        ];
        let d3: [u8; 4] = [
            lut_r[p3[0] as usize],
            lut_g[p3[1] as usize],
            lut_b[p3[2] as usize],
            lut_a[p3[3] as usize],
        ];
        *blk = [d0, d1, d2, d3];
        prev_src = w3;
        prev_dst = d3;
        prev_dst4 = [prev_dst; 4];
    }
    for px in tail.iter_mut() {
        px[0] = lut_r[px[0] as usize];
        px[1] = lut_g[px[1] as usize];
        px[2] = lut_b[px[2] as usize];
        px[3] = lut_a[px[3] as usize];
    }
}

/// Fills or composites a pre-clipped RGBA8 byte span with `color` in gamma-correct linear light.
///
/// # Arguments
///
/// - `&mut [u8]` - Pre-clipped destination RGBA8 byte slice.
/// - `Color` - Foreground RGBA color.
#[inline(always)]
pub(crate) fn fill_span_linear_slice(span: &mut [u8], color: Color) {
    let sa: u8 = color.get_a();
    if sa == 0 {
        return;
    }
    if sa == 255 {
        fill_rgba_span_32(span, [color.get_r(), color.get_g(), color.get_b(), 255]);
        return;
    }
    if span.len() >= 128 * 4 {
        let luts: [[u8; 256]; 4] = build_linear_blend_luts(color);
        fill_span_linear_luts_slice(span, &luts);
        return;
    }
    let a: f32 = (sa as f32) * (1.0 / 255.0);
    let inv_a: f32 = 1.0 - a;
    let sr_a: f32 = SRGB_TO_LIN_LUT[color.get_r() as usize] * a;
    let sg_a: f32 = SRGB_TO_LIN_LUT[color.get_g() as usize] * a;
    let sb_a: f32 = SRGB_TO_LIN_LUT[color.get_b() as usize] * a;
    let sa_u32: u32 = sa as u32;
    let inv_a_u32: u32 = 255 - sa_u32;
    for px in span.as_chunks_mut::<4>().0.iter_mut() {
        let dr: f32 = SRGB_TO_LIN_LUT[px[0] as usize];
        let dg: f32 = SRGB_TO_LIN_LUT[px[1] as usize];
        let db: f32 = SRGB_TO_LIN_LUT[px[2] as usize];
        let ir: usize = ((sr_a + dr * inv_a) * 4095.0).clamp(0.0, 4095.0) as usize;
        let ig: usize = ((sg_a + dg * inv_a) * 4095.0).clamp(0.0, 4095.0) as usize;
        let ib: usize = ((sb_a + db * inv_a) * 4095.0).clamp(0.0, 4095.0) as usize;
        let va: u32 = (px[3] as u32) * inv_a_u32 + 128;
        px[0] = ((LIN_TO_SRGB_Q4_LUT[ir] + 8) >> 4).min(255) as u8;
        px[1] = ((LIN_TO_SRGB_Q4_LUT[ig] + 8) >> 4).min(255) as u8;
        px[2] = ((LIN_TO_SRGB_Q4_LUT[ib] + 8) >> 4).min(255) as u8;
        px[3] = (sa_u32 + ((va + (va >> 8)) >> 8)).min(255) as u8;
    }
}

/// Folds one 64-byte block (`8 x u64` little-endian words) into 8 independent SIMD-friendly hash lanes with cross-lane mixing.
///
/// # Arguments
///
/// - `&mut [u64; 8]` - Mutable 8-lane hash accumulators.
/// - `&[u8; 64]` - 64-byte input pixel block.
#[inline(always)]
pub(crate) fn fold_fnv1a_64(lanes: &mut [u64; 8], blk: &[u8; 64]) {
    let (words, _): (&[[u8; 8]], &[u8]) = blk.as_chunks::<8>();
    let w0: u64 = u64::from_le_bytes(words[0]);
    let w1: u64 = u64::from_le_bytes(words[1]);
    let w2: u64 = u64::from_le_bytes(words[2]);
    let w3: u64 = u64::from_le_bytes(words[3]);
    let w4: u64 = u64::from_le_bytes(words[4]);
    let w5: u64 = u64::from_le_bytes(words[5]);
    let w6: u64 = u64::from_le_bytes(words[6]);
    let w7: u64 = u64::from_le_bytes(words[7]);
    lanes[0] = lanes[0].wrapping_add(w0).rotate_left(13) ^ w4;
    lanes[1] = lanes[1].wrapping_add(w1).rotate_left(13) ^ w5;
    lanes[2] = lanes[2].wrapping_add(w2).rotate_left(13) ^ w6;
    lanes[3] = lanes[3].wrapping_add(w3).rotate_left(13) ^ w7;
    lanes[4] = lanes[4].wrapping_add(w4).rotate_left(19) ^ w0;
    lanes[5] = lanes[5].wrapping_add(w5).rotate_left(19) ^ w1;
    lanes[6] = lanes[6].wrapping_add(w6).rotate_left(19) ^ w2;
    lanes[7] = lanes[7].wrapping_add(w7).rotate_left(19) ^ w3;
}