alpha2-render 0.4.5

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
//! The built-in effects a render graph draws as several internal passes.
//!
//! A Gaussian is separable and the sum of Gaussians is a Gaussian, so a blur
//! of any width is a chain of narrow one-axis passes. Each pass reads at most
//! [`PASS_REACH`] taps either side, `MAX_PASS_SIGMA_TAPS` standard deviations
//! wide. The first pass samples every texel; every later pass samples taps as
//! far apart as the blur accumulated so far, a picture already smooth at that
//! scale, so no pass leaves gaps between its taps. The pass count grows with
//! the logarithm of the width: about four times wider per pass.

use std::sync::LazyLock;

use crate::{
    EffectDef, EffectId, EffectInputs, EffectParams, EffectResources, EffectRuntime, GraphError,
    ParamValue, RenderError, RenderGraph, RenderNodeId,
    builtins::{
        DIRECTIONAL_BLUR_ID, DROP_SHADOW_ID, FOCUS_COMBINE_ID, GAUSSIAN_BLUR_ID,
        GAUSSIAN_BLUR_PASS_ID, GLOW_COMBINE_ID, GLOW_ID, GLOW_THRESHOLD_ID, SHADOW_COMBINE_ID,
        SHADOW_OFFSET_ID, TILT_SHIFT_ID, UNSHARP_COMBINE_ID, UNSHARP_MASK_ID,
        directional_blur_definition, drop_shadow_definition, gaussian_blur_definition,
        glow_definition, tilt_shift_definition, unsharp_mask_definition,
    },
};

/// Taps one blur pass reads on either side of its centre.
const PASS_REACH: f32 = 12.0;

/// Widest standard deviation one pass draws, in taps: its reach covers three.
pub(crate) const MAX_PASS_SIGMA_TAPS: f32 = PASS_REACH / 3.0;

/// More passes than any canvas needs: a 4320-pixel deviation takes six.
const MAX_PASSES_PER_AXIS: usize = 16;

/// The definitions whose parameters an expanded built-in is checked
/// against, made once rather than for every graph that draws one.
static GAUSSIAN_BLUR: LazyLock<EffectDef> = LazyLock::new(gaussian_blur_definition);
static GLOW: LazyLock<EffectDef> = LazyLock::new(glow_definition);
static DIRECTIONAL_BLUR: LazyLock<EffectDef> = LazyLock::new(directional_blur_definition);
static UNSHARP_MASK: LazyLock<EffectDef> = LazyLock::new(unsharp_mask_definition);
static TILT_SHIFT: LazyLock<EffectDef> = LazyLock::new(tilt_shift_definition);
static DROP_SHADOW: LazyLock<EffectDef> = LazyLock::new(drop_shadow_definition);

/// The internal effects a built-in drawn as several passes draws with, for
/// [`RenderGraph::add_expanded_effect`]'s effects; `None` for every other.
pub(crate) fn expanded_passes(effect: &str) -> Option<&'static [&'static str]> {
    match effect {
        GAUSSIAN_BLUR_ID | DIRECTIONAL_BLUR_ID => Some(&[GAUSSIAN_BLUR_PASS_ID]),
        GLOW_ID => Some(&[GLOW_THRESHOLD_ID, GAUSSIAN_BLUR_PASS_ID, GLOW_COMBINE_ID]),
        UNSHARP_MASK_ID => Some(&[GAUSSIAN_BLUR_PASS_ID, UNSHARP_COMBINE_ID]),
        TILT_SHIFT_ID => Some(&[GAUSSIAN_BLUR_PASS_ID, FOCUS_COMBINE_ID]),
        DROP_SHADOW_ID => Some(&[SHADOW_OFFSET_ID, GAUSSIAN_BLUR_PASS_ID, SHADOW_COMBINE_ID]),
        _ => None,
    }
}

impl<'a> RenderGraph<'a> {
    /// Adds `effect` as the internal passes that draw it, when it is a
    /// built-in drawn that way; `None` for every other effect.
    pub(crate) fn add_expanded_effect(
        &mut self,
        effect: &EffectId,
        source: RenderNodeId,
        params: &EffectParams,
        runtime: EffectRuntime,
    ) -> Option<Result<RenderNodeId, GraphError>> {
        match effect.as_str() {
            GAUSSIAN_BLUR_ID => Some(self.add_gaussian_blur(source, params, runtime)),
            GLOW_ID => Some(self.add_glow(source, params, runtime)),
            DIRECTIONAL_BLUR_ID => Some(self.add_directional_blur(source, params, runtime)),
            UNSHARP_MASK_ID => Some(self.add_unsharp_mask(source, params, runtime)),
            TILT_SHIFT_ID => Some(self.add_tilt_shift(source, params, runtime)),
            DROP_SHADOW_ID => Some(self.add_drop_shadow(source, params, runtime)),
            _ => None,
        }
    }

    fn add_gaussian_blur(
        &mut self,
        source: RenderNodeId,
        params: &EffectParams,
        runtime: EffectRuntime,
    ) -> Result<RenderNodeId, GraphError> {
        let [radius] = checked_params(&GAUSSIAN_BLUR, params, ["radius"])?;
        self.add_gaussian(source, radius * 0.5, runtime)
    }

    #[allow(clippy::cast_precision_loss)]
    fn add_drop_shadow(
        &mut self,
        source: RenderNodeId,
        params: &EffectParams,
        runtime: EffectRuntime,
    ) -> Result<RenderNodeId, GraphError> {
        let [color, opacity, angle, distance, softness, shadow_only] = checked_values(
            &DROP_SHADOW,
            params,
            [
                "color",
                "opacity",
                "angle",
                "distance",
                "softness",
                "shadow_only",
            ],
        )?;
        let (
            ParamValue::Float(opacity),
            ParamValue::Float(angle),
            ParamValue::Float(distance),
            ParamValue::Float(softness),
        ) = (opacity, angle, distance, softness)
        else {
            return Err(RenderError::InvalidEffectParams.into());
        };
        // Counter-clockwise on screen, y down; the distance in heights, the offset in uv.
        let radians = angle.to_radians();
        let height_over_width = self.size.height as f32 / self.size.width as f32;
        let offset = [
            libm::cosf(radians) * distance * height_over_width,
            -libm::sinf(radians) * distance,
        ];
        let shadow = self.add_internal_effect(
            SHADOW_OFFSET_ID,
            source,
            EffectParams::new()
                .with("offset", ParamValue::Vec2(offset))
                .with("color", color)
                .with("opacity", ParamValue::Float(opacity)),
            runtime,
        )?;
        let soft = self.add_gaussian(shadow, softness * 0.5, runtime)?;
        self.add_transition(
            EffectId::from_static(SHADOW_COMBINE_ID, 1),
            source,
            soft,
            EffectParams::new().with("shadow_only", shadow_only),
            EffectRuntime {
                progress: 0.5,
                ..runtime
            },
        )
    }

    fn add_glow(
        &mut self,
        source: RenderNodeId,
        params: &EffectParams,
        runtime: EffectRuntime,
    ) -> Result<RenderNodeId, GraphError> {
        let [threshold, intensity, radius] =
            checked_params(&GLOW, params, ["threshold", "intensity", "radius"])?;
        let bright = self.add_internal_effect(
            GLOW_THRESHOLD_ID,
            source,
            EffectParams::new().with("threshold", ParamValue::Float(threshold)),
            runtime,
        )?;
        let bloom = self.add_gaussian(bright, radius * 0.5, runtime)?;
        self.add_transition(
            EffectId::from_static(GLOW_COMBINE_ID, 1),
            source,
            bloom,
            EffectParams::new().with("intensity", ParamValue::Float(intensity)),
            EffectRuntime {
                progress: 0.5,
                ..runtime
            },
        )
    }

    /// One axis of a Gaussian, `angle` degrees counter-clockwise on screen.
    #[allow(clippy::cast_precision_loss)]
    fn add_directional_blur(
        &mut self,
        source: RenderNodeId,
        params: &EffectParams,
        runtime: EffectRuntime,
    ) -> Result<RenderNodeId, GraphError> {
        let [length, angle] = checked_params(&DIRECTIONAL_BLUR, params, ["length", "angle"])?;
        // libm, so the CPU renderer's graph is the same on every target.
        let radians = angle.to_radians();
        let direction = [libm::cosf(radians), -libm::sinf(radians)];
        let sigma_pixels = length * 0.5 * self.size.height as f32;
        self.add_gaussian_axis(source, sigma_pixels, direction, runtime)
    }

    fn add_unsharp_mask(
        &mut self,
        source: RenderNodeId,
        params: &EffectParams,
        runtime: EffectRuntime,
    ) -> Result<RenderNodeId, GraphError> {
        let [amount, radius, threshold] =
            checked_params(&UNSHARP_MASK, params, ["amount", "radius", "threshold"])?;
        let blurred = self.add_gaussian(source, radius * 0.5, runtime)?;
        self.add_transition(
            EffectId::from_static(UNSHARP_COMBINE_ID, 1),
            source,
            blurred,
            EffectParams::new()
                .with("amount", ParamValue::Float(amount))
                .with("threshold", ParamValue::Float(threshold)),
            EffectRuntime {
                progress: 0.5,
                ..runtime
            },
        )
    }

    /// A blur of a quarter `blur`, and one of half `blur` drawn on from
    /// it (Gaussians add in quadrature), mixed into the source by the
    /// distance from the band.
    fn add_tilt_shift(
        &mut self,
        source: RenderNodeId,
        params: &EffectParams,
        runtime: EffectRuntime,
    ) -> Result<RenderNodeId, GraphError> {
        let [blur, position, angle, focus, falloff] = checked_params(
            &TILT_SHIFT,
            params,
            ["blur", "position", "angle", "focus", "falloff"],
        )?;
        let (narrow, wide) = (blur * 0.25, blur * 0.5);
        let soft = self.add_gaussian(source, narrow, runtime)?;
        let rest = (wide * wide - narrow * narrow).max(0.0).sqrt();
        let blurred = self.add_gaussian(soft, rest, runtime)?;
        self.add_effect_with_inputs(
            EffectId::from_static(FOCUS_COMBINE_ID, 1),
            source,
            EffectInputs::new()
                .with("soft", soft)
                .with("blurred", blurred),
            EffectParams::new()
                .with("position", ParamValue::Float(position))
                .with("angle", ParamValue::Float(angle))
                .with("focus", ParamValue::Float(focus))
                .with("falloff", ParamValue::Float(falloff)),
            EffectResources::new(),
            runtime,
        )
    }

    /// A Gaussian with a standard deviation of `sigma` output heights, as
    /// horizontal then vertical pass cascades.
    #[allow(clippy::cast_precision_loss)]
    fn add_gaussian(
        &mut self,
        source: RenderNodeId,
        sigma: f32,
        runtime: EffectRuntime,
    ) -> Result<RenderNodeId, GraphError> {
        self.add_gaussian_cascade(source, sigma * self.size.height as f32, runtime)
    }

    /// Adds a Gaussian blur of `source` whose standard deviation is
    /// `sigma_pixels` of the graph's pixels on both axes, drawn as the
    /// built-in blur draws: narrow one-axis passes, within the renderer's
    /// filter-sample budget. A mask's feather softens its coverage with it.
    /// A deviation that is not finite and positive returns `source` itself.
    ///
    /// # Errors
    /// Returns [`GraphError::InvalidDependency`] unless `source` is earlier.
    #[allow(clippy::cast_precision_loss)]
    pub fn add_gaussian_pixels(
        &mut self,
        source: RenderNodeId,
        sigma_pixels: f32,
        runtime: EffectRuntime,
    ) -> Result<RenderNodeId, GraphError> {
        if source.graph != self.id || source.index >= self.nodes.len() {
            return Err(GraphError::InvalidDependency);
        }
        if sigma_pixels.is_nan() || sigma_pixels <= 0.0 || !sigma_pixels.is_finite() {
            return Ok(source);
        }
        self.add_gaussian_cascade(source, sigma_pixels, runtime)
    }

    /// The pass cascades of a Gaussian of `sigma_pixels`; one identity pass
    /// per axis at zero.
    fn add_gaussian_cascade(
        &mut self,
        source: RenderNodeId,
        sigma_pixels: f32,
        runtime: EffectRuntime,
    ) -> Result<RenderNodeId, GraphError> {
        let node = self.add_gaussian_axis(source, sigma_pixels, [1.0, 0.0], runtime)?;
        self.add_gaussian_axis(node, sigma_pixels, [0.0, 1.0], runtime)
    }

    /// A one-axis Gaussian of `sigma_pixels` along the unit vector
    /// `direction` (y down), as a cascade of passes; one identity pass at
    /// zero. A pass's taps lie `spacing` pixels apart along `direction`
    /// whatever its angle, so a diagonal blur is as wide as an upright one.
    #[allow(clippy::cast_precision_loss)]
    pub(crate) fn add_gaussian_axis(
        &mut self,
        source: RenderNodeId,
        sigma_pixels: f32,
        direction: [f32; 2],
        runtime: EffectRuntime,
    ) -> Result<RenderNodeId, GraphError> {
        let extent = [self.size.width as f32, self.size.height as f32];
        let mut node = source;
        let mut blurred = 0.0_f32;
        let mut spacing = 1.0_f32;
        for _ in 0..MAX_PASSES_PER_AXIS {
            let remaining = (sigma_pixels * sigma_pixels - blurred * blurred)
                .max(0.0)
                .sqrt();
            let pass = remaining.min(MAX_PASS_SIGMA_TAPS * spacing);
            let step = [
                direction[0] * spacing / extent[0],
                direction[1] * spacing / extent[1],
            ];
            node = self.add_internal_effect(
                GAUSSIAN_BLUR_PASS_ID,
                node,
                EffectParams::new()
                    .with("step", ParamValue::Vec2(step))
                    .with("sigma", ParamValue::Float(pass / spacing)),
                runtime,
            )?;
            blurred = blurred.hypot(pass);
            if pass < MAX_PASS_SIGMA_TAPS * spacing {
                break;
            }
            spacing = blurred;
        }
        Ok(node)
    }

    pub(crate) fn add_internal_effect(
        &mut self,
        id: &'static str,
        source: RenderNodeId,
        params: EffectParams<'a>,
        runtime: EffectRuntime,
    ) -> Result<RenderNodeId, GraphError> {
        self.add_effect_with_resources(
            EffectId::from_static(id, 1),
            source,
            params,
            EffectResources::new(),
            runtime,
        )
    }
}

/// The named parameters of `definition` as `params` gives them, its
/// defaults filling the rest, checked as a pass would check them: of any
/// type, for an expanded effect that passes colors, switches or choices on.
pub(crate) fn checked_values<const N: usize>(
    definition: &EffectDef,
    params: &EffectParams,
    names: [&str; N],
) -> Result<[ParamValue; N], GraphError> {
    crate::runtime_effect::encode_params(&definition.params, params)?;
    let mut values = std::array::from_fn(|_| ParamValue::Float(0.0));
    for (value, name) in values.iter_mut().zip(names) {
        let spec = definition
            .params
            .iter()
            .find(|spec| spec.name == name)
            .ok_or(RenderError::InvalidEffectParams)?;
        *value = params.get(name).unwrap_or(&spec.default).clone();
    }
    Ok(values)
}

/// The named float parameters of `definition` as `params` gives them, its
/// defaults filling the rest, checked as a pass would check them.
pub(crate) fn checked_params<const N: usize>(
    definition: &EffectDef,
    params: &EffectParams,
    names: [&str; N],
) -> Result<[f32; N], GraphError> {
    crate::runtime_effect::encode_params(&definition.params, params)?;
    let mut values = [0.0; N];
    for (value, name) in values.iter_mut().zip(names) {
        let spec = definition
            .params
            .iter()
            .find(|spec| spec.name == name)
            .ok_or(RenderError::InvalidEffectParams)?;
        let ParamValue::Float(supplied) = params.get(name).unwrap_or(&spec.default) else {
            return Err(RenderError::InvalidEffectParams.into());
        };
        *value = *supplied;
    }
    Ok(values)
}

#[cfg(test)]
mod tests {
    use super::checked_values;
    use crate::{EffectParams, ParamValue, chroma_key_definition};

    #[test]
    fn checked_values_fill_defaults_and_refuse_a_wrong_type() {
        let definition = chroma_key_definition();
        let supplied = EffectParams::new()
            .with("key_color", ParamValue::Color([0.2, 0.4, 0.6, 1.0]))
            .with("distance_mode", ParamValue::Enum(1));
        let [key, mode, softness] = checked_values(
            &definition,
            &supplied,
            ["key_color", "distance_mode", "softness"],
        )
        .expect("valid parameters");
        assert_eq!(key, ParamValue::Color([0.2, 0.4, 0.6, 1.0]));
        assert_eq!(mode, ParamValue::Enum(1));
        assert_eq!(softness, ParamValue::Float(0.1));
        let wrong = EffectParams::new().with("key_color", ParamValue::Float(1.0));
        assert!(checked_values(&definition, &wrong, ["key_color"]).is_err());
        assert!(checked_values(&definition, &EffectParams::new(), ["missing"]).is_err());
    }
}