alpha2-render 0.4.4

Render graph and effect registry for alpha2.
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
//! The internal passes: the blur and glow passes, shape-mask combines and the track matte.

use super::effects::{
    centre, effect_row, grow, luminance, margin, premultiplied, saturate, straight,
};
use super::{CpuKernel, KernelContext, transition_row};
use crate::builtins::{GAUSSIAN_BLUR_PASS_ID, GLOW_COMBINE_ID, GLOW_THRESHOLD_ID};
use crate::cpu::curves;
use crate::cpu::half;
use crate::cpu::image::{CpuImage, PixelBox, Pixels};
use crate::masking::{
    MASK_COMBINE_EFFECT_IDS, MASK_COMBINE_NAMES, MAX_SHAPE_MASKS, TRACK_MATTE_EFFECT_ID,
};

/// The kernel for the built-in `id` (version 1), if this module has it.
pub(super) fn kernel(id: &str) -> Option<&'static dyn CpuKernel> {
    Some(match id {
        GAUSSIAN_BLUR_PASS_ID => &BlurPass,
        GLOW_THRESHOLD_ID => &GlowThreshold,
        GLOW_COMBINE_ID => &GlowCombine,
        TRACK_MATTE_EFFECT_ID => &TrackMatte,
        _ if MASK_COMBINE_EFFECT_IDS.contains(&id) => &MaskCombine,
        _ => return None,
    })
}

/// Taps a blur pass reads on either side at most (gaussian-blur.wgsl).
const PASS_REACH: i32 = 12;

/// One blur pass's taps: its reach and the Gaussian's weights, by tap
/// index plus [`PASS_REACH`].
struct Taps {
    reach: i32,
    weights: [f32; 2 * PASS_REACH as usize + 1],
    sum: f32,
}

impl Taps {
    #[allow(
        clippy::cast_possible_truncation,
        clippy::cast_precision_loss,
        clippy::cast_sign_loss
    )]
    fn new(context: &KernelContext<'_>) -> Self {
        let sigma = context.float("sigma").max(0.0001);
        let budget = ((context.max_filter_samples() * 0.5).floor() as i32).max(1);
        let reach = ((sigma * 3.0).ceil() as i32).min(PASS_REACH.min(budget));
        let mut weights = [0.0; 2 * PASS_REACH as usize + 1];
        let mut sum = 0.0;
        // Summed in the shader's order, from -12 up.
        for index in -reach..=reach {
            let position = index as f32 / sigma;
            let weight = libm::expf(-0.5 * position * position);
            weights[(index + PASS_REACH) as usize] = weight;
            sum += weight;
        }
        Self {
            reach,
            weights,
            sum,
        }
    }

    fn weight(&self, index: i32) -> f32 {
        #[allow(clippy::cast_sign_loss)]
        self.weights[(index + PASS_REACH) as usize]
    }
}

/// One row of a picture, for reading texels by column.
#[derive(Copy, Clone)]
enum Row<'a> {
    /// Outside the content: transparent.
    Empty,
    Half(u32, &'a [[u16; 4]]),
    Unorm8(u32, &'a [[u8; 4]]),
}

impl<'a> Row<'a> {
    /// Row `y` (inside the picture) of `image`.
    fn of(image: &'a CpuImage, y: u32) -> Self {
        let bounds = image.bounds();
        if y < bounds.y || y >= bounds.bottom() {
            return Self::Empty;
        }
        let width = bounds.width as usize;
        let start = (y - bounds.y) as usize * width;
        match image.pixels() {
            Pixels::Half(pixels) => Self::Half(bounds.x, &pixels[start..start + width]),
            Pixels::Unorm8(pixels) => Self::Unorm8(bounds.x, &pixels[start..start + width]),
        }
    }

    /// The texels of columns `column..column + out.len()`, transparent
    /// outside the content.
    fn decode(self, column: u32, out: &mut [[f32; 4]]) {
        out.fill([0.0; 4]);
        let (first, length) = match self {
            Self::Empty => return,
            Self::Half(first, pixels) => (first, pixels.len()),
            Self::Unorm8(first, pixels) => (first, pixels.len()),
        };
        // The content's columns within the requested ones.
        let from = first.max(column) as usize;
        let to = (first as usize + length).min(column as usize + out.len());
        if from >= to {
            return;
        }
        let out = &mut out[from - column as usize..to - column as usize];
        let start = from - first as usize;
        match self {
            Self::Empty => {}
            Self::Half(_, pixels) => {
                // The `half` crate converts a slice with F16C where the CPU
                // has it; the conversion is exact either way.
                use ::half::slice::{HalfBitsSliceExt as _, HalfFloatSliceExt as _};
                let pixels: &[::half::f16] = pixels[start..start + out.len()]
                    .as_flattened()
                    .reinterpret_cast();
                pixels.convert_to_f32_slice(out.as_flattened_mut());
            }
            Self::Unorm8(_, pixels) => {
                for (out, pixel) in out.iter_mut().zip(&pixels[start..]) {
                    *out = pixel.map(half::unorm8_value);
                }
            }
        }
    }
}

/// Where tap `index` lands, `index * spacing` texels from the pixel's
/// centre: the whole texels before it and the weight of the next texel.
/// A tap more than `extent` texels away reads the edge as one `extent + 1`
/// away does, so the offset is kept within that.
#[allow(clippy::cast_possible_truncation)]
fn tap_offset(spacing: f32, index: i32, extent: u32) -> (i64, f32) {
    let limit = i64::from(extent) + 1;
    let offset = f64::from(spacing) * f64::from(index);
    let whole = offset.floor();
    ((whole as i64).clamp(-limit, limit), (offset - whole) as f32)
}

#[inline]
fn add_weighted(sum: &mut [f32; 4], sample: [f32; 4], weight: f32) {
    for channel in 0..4 {
        sum[channel] += sample[channel] * weight;
    }
}

#[inline]
fn add_between(sum: &mut [f32; 4], a: [f32; 4], b: [f32; 4], t: f32, weight: f32) {
    for channel in 0..4 {
        sum[channel] += (a[channel] + (b[channel] - a[channel]) * t) * weight;
    }
}

/// gaussian-blur.wgsl: one axis of a Gaussian, taps `step` apart in uv.
///
/// The taps are the shader's, read bilinearly with the edge repeated and
/// summed in the shader's order. A tap lands `index * step` from the pixel
/// centre exactly, as the sampler's fixed-point coordinates place it, not
/// after a round trip through `uv` in f32 (a ten-thousandth of a texel
/// apart). So every pixel of a row reads the same two texels around its
/// own column (or row) with the same weights, and a tap is one pass over a
/// decoded row.
struct BlurPass;

impl CpuKernel for BlurPass {
    #[allow(clippy::cast_precision_loss)]
    fn bounds(&self, context: &KernelContext<'_>) -> PixelBox {
        let taps = Taps::new(context);
        let step = context.vec2("step");
        let reach = taps.reach as f32;
        let size = context.size;
        grow(
            context.source_a.bounds(),
            margin(reach * step[0] * size.width as f32),
            margin(reach * step[1] * size.height as f32),
            size,
        )
    }

    #[allow(clippy::cast_precision_loss)]
    fn row(&self, context: &KernelContext<'_>, y: u32, x0: u32, out: &mut [[f32; 4]]) {
        let taps = Taps::new(context);
        let step = context.vec2("step");
        let size = context.size;
        // The passes along one axis read the source's rows as the output's, which they are
        // only at the output's size; another size is sampled in uv, as the shader samples it.
        let texels = context.source_a.size() == size;
        if texels && step[1] == 0.0 {
            horizontal(context, &taps, step[0] * size.width as f32, y, x0, out);
        } else if texels && step[0] == 0.0 {
            vertical(context, &taps, step[1] * size.height as f32, y, x0, out);
        } else {
            super::shade_row(context, y, x0, out, |_, uv| {
                let mut result = [0.0_f32; 4];
                for index in -taps.reach..=taps.reach {
                    let offset = index as f32;
                    let sample =
                        context.sample_src([uv[0] + step[0] * offset, uv[1] + step[1] * offset]);
                    add_weighted(&mut result, sample, taps.weight(index));
                }
                result.map(|value| value / taps.sum)
            });
        }
    }
}

/// A pass along x, taps `spacing` texels apart: every tap reads row `y`.
#[allow(
    clippy::cast_possible_truncation,
    clippy::cast_possible_wrap,
    clippy::cast_sign_loss
)]
fn horizontal(
    context: &KernelContext<'_>,
    taps: &Taps,
    spacing: f32,
    y: u32,
    x0: u32,
    out: &mut [[f32; 4]],
) {
    let width = context.size.width as usize;
    let offsets = (-taps.reach..=taps.reach)
        .map(|index| tap_offset(spacing, index, context.size.width))
        .collect::<Vec<_>>();
    // The row with its edge texels repeated past both ends, as far as a tap
    // reads: no tap needs clamping.
    let pad = offsets
        .iter()
        .map(|&(whole, _)| whole.unsigned_abs() as usize + 1)
        .max()
        .unwrap_or(1);
    let mut line = vec![[0.0_f32; 4]; width + 2 * pad];
    Row::of(context.source_a, y).decode(0, &mut line[pad..pad + width]);
    let (left, right) = (line[pad], line[pad + width - 1]);
    line[..pad].fill(left);
    line[pad + width..].fill(right);
    out.fill([0.0; 4]);
    for (index, &(whole, fraction)) in (-taps.reach..=taps.reach).zip(&offsets) {
        let weight = taps.weight(index);
        let first = (i64::from(x0) + whole + pad as i64) as usize;
        let texels = &line[first..=first + out.len()];
        if fraction == 0.0 {
            for (sum, texel) in out.iter_mut().zip(texels) {
                add_weighted(sum, *texel, weight);
            }
        } else {
            for (sum, pair) in out.iter_mut().zip(texels.windows(2)) {
                add_between(sum, pair[0], pair[1], fraction, weight);
            }
        }
    }
    for pixel in out.iter_mut() {
        *pixel = pixel.map(|value| value / taps.sum);
    }
}

/// A pass along y, taps `spacing` rows apart: each tap reads one or two
/// rows at the pixels' own columns.
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
fn vertical(
    context: &KernelContext<'_>,
    taps: &Taps,
    spacing: f32,
    y: u32,
    x0: u32,
    out: &mut [[f32; 4]],
) {
    let image = context.source_a;
    let last = i64::from(context.size.height) - 1;
    let mut rows = vec![[0.0_f32; 4]; out.len() * 2];
    let (upper, lower) = rows.split_at_mut(out.len());
    out.fill([0.0; 4]);
    for index in -taps.reach..=taps.reach {
        let (whole, fraction) = tap_offset(spacing, index, context.size.height);
        let top = i64::from(y) + whole;
        let above = Row::of(image, top.clamp(0, last) as u32);
        let below = Row::of(image, (top + 1).clamp(0, last) as u32);
        let weight = taps.weight(index);
        if fraction == 0.0 {
            if matches!(above, Row::Empty) {
                continue;
            }
            above.decode(x0, upper);
            for (sum, texel) in out.as_flattened_mut().iter_mut().zip(upper.as_flattened()) {
                *sum += texel * weight;
            }
        } else {
            if matches!((above, below), (Row::Empty, Row::Empty)) {
                continue;
            }
            above.decode(x0, upper);
            below.decode(x0, lower);
            for ((sum, a), b) in out.iter_mut().zip(upper.iter()).zip(lower.iter()) {
                add_between(sum, *a, *b, fraction, weight);
            }
        }
    }
    for pixel in out.iter_mut() {
        *pixel = pixel.map(|value| value / taps.sum);
    }
}

/// glow-threshold.wgsl: what of each pixel glows, opaque. It draws every
/// pixel, transparent ones as opaque black.
struct GlowThreshold;

impl CpuKernel for GlowThreshold {
    fn row(&self, context: &KernelContext<'_>, y: u32, x0: u32, out: &mut [[f32; 4]]) {
        let threshold = context.float("threshold");
        effect_row(context, y, x0, out, |_, _, src| {
            let rgb = straight(src, 0.0001).map(|value| (value - threshold).max(0.0));
            [rgb[0], rgb[1], rgb[2], 1.0]
        });
    }
}

/// The pixels a two-input pass whose body keeps `from`'s alpha can draw,
/// through the transition wrapper.
fn wrapped_bounds(context: &KernelContext<'_>, body: PixelBox) -> PixelBox {
    if context.progress() <= 0.0 {
        context.source_a.bounds()
    } else if context.progress() >= 1.0 {
        context.source_b.bounds()
    } else {
        body
    }
}

/// glow-combine.wgsl: the source with its bloom added.
struct GlowCombine;

impl CpuKernel for GlowCombine {
    fn bounds(&self, context: &KernelContext<'_>) -> PixelBox {
        wrapped_bounds(context, context.source_a.bounds())
    }

    fn row(&self, context: &KernelContext<'_>, y: u32, x0: u32, out: &mut [[f32; 4]]) {
        let intensity = context.float("intensity");
        let size = context.size;
        transition_row(context, y, x0, out, |x, _| {
            let src = centre(context.source_a, size, x, y);
            let bloom = centre(context.source_b, size, x, y);
            let base = straight(src, 0.0001);
            let rgb = [0, 1, 2].map(|c| saturate(base[c] + bloom[c] * intensity));
            premultiplied(rgb, src[3])
        });
    }
}

/// The track matte (masking.rs `track_matte_definition`).
struct TrackMatte;

impl CpuKernel for TrackMatte {
    // Without inversion the matte's coverage is zero where it is
    // transparent.
    fn bounds(&self, context: &KernelContext<'_>) -> PixelBox {
        let picture = context.source_a.bounds();
        let body = if context.boolean("inverted") {
            picture
        } else {
            picture.intersect(context.source_b.bounds())
        };
        wrapped_bounds(context, body)
    }

    fn row(&self, context: &KernelContext<'_>, y: u32, x0: u32, out: &mut [[f32; 4]]) {
        let luma = context.boolean("luma");
        let inverted = context.boolean("inverted");
        let size = context.size;
        transition_row(context, y, x0, out, |x, _| {
            let picture = centre(context.source_a, size, x, y);
            let matte = centre(context.source_b, size, x, y);
            let mut coverage = matte[3];
            if luma {
                let alpha = matte[3].max(0.000_001);
                let linear = [0, 1, 2].map(|c| curves::srgb_to_linear(saturate(matte[c] / alpha)));
                coverage = luminance(linear) * matte[3];
            }
            if inverted {
                coverage = 1.0 - coverage;
            }
            let coverage = saturate(coverage);
            picture.map(|value| value * coverage)
        });
    }
}

/// The shape-mask combine for one to eight masks (masking.rs
/// `mask_combine_definition`): one kernel, the mask count read from the
/// definition.
struct MaskCombine;

/// One mask of a combine: its coverage picture (unbound samples
/// transparent), inversion and mode.
#[derive(Copy, Clone)]
struct Mask<'a> {
    image: Option<&'a CpuImage>,
    invert: bool,
    mode: u32,
}

impl MaskCombine {
    fn masks<'a>(context: &KernelContext<'a>) -> ([Mask<'a>; MAX_SHAPE_MASKS], usize) {
        let count = context.definition.inputs.len().min(MAX_SHAPE_MASKS);
        let mut masks = [Mask {
            image: None,
            invert: false,
            mode: 0,
        }; MAX_SHAPE_MASKS];
        for (mask, names) in masks.iter_mut().zip(&MASK_COMBINE_NAMES[..count]) {
            *mask = Mask {
                image: context
                    .inputs
                    .iter()
                    .find(|(name, _)| *name == names.input)
                    .map(|(_, image)| *image),
                invert: context.boolean(names.invert),
                mode: context.choice(names.mode),
            };
        }
        (masks, count)
    }
}

/// `builtin_mask_step`.
#[inline]
fn mask_step(coverage: f32, mask: f32, invert: bool, mode: u32) -> f32 {
    let mut m = saturate(mask);
    if invert {
        m = 1.0 - m;
    }
    match mode {
        1 => coverage * (1.0 - m),
        2 => coverage * m,
        _ => coverage + m - coverage * m,
    }
}

impl CpuKernel for MaskCombine {
    // Coverage that starts empty only grows where an added mask covers
    // something: subtracting and intersecting never raise it.
    fn bounds(&self, context: &KernelContext<'_>) -> PixelBox {
        let source = context.source_a.bounds();
        if context.choice("mode0") != 0 {
            return source;
        }
        let (masks, count) = Self::masks(context);
        let mut covered = PixelBox::EMPTY;
        for mask in masks[..count].iter().filter(|mask| mask.mode == 0) {
            match mask.image {
                Some(image) if !mask.invert => covered = covered.union(image.bounds()),
                // An inverted added mask can cover everything.
                _ => return source,
            }
        }
        source.intersect(covered.expand(1, context.size))
    }

    fn row(&self, context: &KernelContext<'_>, y: u32, x0: u32, out: &mut [[f32; 4]]) {
        let (masks, count) = Self::masks(context);
        let masks = &masks[..count];
        let start = if context.choice("mode0") == 0 {
            0.0
        } else {
            1.0
        };
        let size = context.size;
        effect_row(context, y, x0, out, |x, _, src| {
            let mut coverage = start;
            for mask in masks {
                let value = mask.image.map_or(0.0, |image| centre(image, size, x, y)[3]);
                coverage = mask_step(coverage, value, mask.invert, mask.mode);
            }
            let coverage = saturate(coverage);
            src.map(|value| value * coverage)
        });
    }
}