three-rs 0.3.0

A port of three.js core and its WebGPURenderer to Rust on wgpu, graded against three.js's own end-to-end pixel comparison.
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
//! Port of `three.js/examples/jsm/tsl/display/TRAANode.js` — temporal
//! reprojection anti-aliasing. The helpers it shares with `TAAUNode`
//! (`examples/jsm/tsl/utils/TAAUtils.js`) live in `taa_utils.rs`.
//!
//! Every frame the camera is jittered by a sub-pixel Halton offset, the scene
//! is rendered once, and a resolve quad blends the new frame into a history
//! texture reprojected along the scene's `velocity` attachment. Over a few
//! frames the jitter averages into an anti-aliased image.
//!
//! Three's node is a `TempNode` with `updateBeforeType = FRAME`; so is this
//! one ([`TraaState`] implements [`NodeUpdate`] and is registered as the
//! updater of the resolve texture, the way `passTexture( this, … )` makes it
//! one in three). The jitter is the part that cannot live in the node itself:
//! three installs it as `OnBeforeRenderPipeline` / `OnAfterRenderPipeline`
//! callbacks from `setup()`, guarded by `renderPipelineState.viewOffsetOwner`
//! so a pipeline with two TRAA nodes jitters once. The port has no
//! `setup()`-time access to the pipeline, so [`TraaNode::attach`] does the
//! same with [`RenderPipeline::on_before_render`] and the same guard.
//!
//! **Where the previous frame lives.** Two kinds of "previous":
//!
//! - the velocity attachment's previous matrices belong to the global
//!   `velocity` node (#163), which TRAA only tells to ignore the jitter
//!   ([`Renderer::set_velocity_projection_matrix`]);
//! - TRAA's own: the camera's previous world matrix and previous inverse
//!   projection, which roll over at the top of [`TraaState::update_before`]
//!   — once per frame, before the new values are written — exactly where
//!   three copies `_cameraWorldMatrix` into `_previousCameraWorldMatrix`.
//!   The history colour and depth are copied at the bottom of the same call,
//!   after the resolve has read them.
//!
//! **Order within a frame.** Three's `TRAANode.setup()` builds the depth,
//! velocity and beauty nodes before itself, so the scene pass is earlier in
//! the frame's update-before list and has rendered by the time TRAA's
//! `updateBefore()` runs (three's own command dump: scene pass, then the
//! history restart copy, the resolve, and the two history copies). The port
//! asks for the pass explicitly at the top of `update_before()`; the frame
//! guard makes the pass's own later reach a no-op.
//!
//! Not ported: an orthographic camera (`viewZToOrthographicDepth`), a
//! logarithmic or reversed depth buffer, a beauty node that is an `RTTNode`
//! rather than a pass attachment, and a `velocity` other than the global one
//! (`builder.context.velocity`).

use std::cell::{Cell, RefCell};
use std::rc::Rc;

use crate::cameras::{PerspectiveCamera, RenderCamera};
use crate::materials::MeshBasicNodeMaterial;
use crate::math::Matrix4;
use crate::nodes::frame::register_texture_update;
use crate::nodes::node::{SettableValue, TextureSource, Type};
use crate::nodes::tsl::{
    all, block, call, float, int, ivec2, length, max, mix, shader_fn, struct_get, texture_load,
    texture_load_offset, texture_sample, texture_size, texture_uv, to_var, uniform_settable, uv,
    vec2,
};
use crate::nodes::{NodeRef, NodeUpdate, NodeUpdateType};
use crate::objects::QuadMesh;
use crate::renderer::{RenderPipeline, RenderTarget, RenderTargetOptions, Renderer};
use crate::textures::{DepthTexture, Texture, TextureFilter, TextureType};

use super::taa_utils::{
    clip_aabb, current_depth_struct, flicker_reduction, halton_offsets, mat4_values,
    sample_current_depth, sample_previous_depth, JITTER_COUNT,
};

/// `TRAANode.depthThreshold`: how far the current depth may sit in front of
/// the reprojected one before the history counts as disoccluded.
const DEPTH_THRESHOLD: f64 = 0.0005;
/// `TRAANode.edgeDepthDiff`: a 3×3 depth range wider than this is an edge,
/// where disocclusion is not trusted.
const EDGE_DEPTH_DIFF: f64 = 0.001;
/// `TRAANode.maxVelocityLength`, in pixels: the motion at which the current
/// frame's weight saturates.
const MAX_VELOCITY_LENGTH: f64 = 128.0;

/// `traa( beautyNode, depthNode, velocityNode, camera )`.
///
/// As with the other display nodes, the inputs are the textures three's
/// `convertToTexture()` / `getTextureNode()` would hand over: the scene pass's
/// `output` attachment, its depth texture and its `velocity` attachment. The
/// pass must have an MRT with `velocity` on it, and the camera must be the
/// pass's.
pub fn traa(
    beauty: &Texture,
    depth: &DepthTexture,
    velocity: &Texture,
    camera: Rc<RefCell<PerspectiveCamera>>,
) -> TraaNode {
    TraaNode::new(beauty, depth, velocity, camera)
}

/// `TRAANode` — see the module docs.
pub struct TraaNode(Rc<TraaState>);

/// The node's state, shared with the renderer's update-before registry.
pub(crate) struct TraaState {
    /// `this.beautyNode`'s texture, whose pass renders the scene.
    beauty: Texture,
    /// `this.depthNode.value`, the pass's depth attachment.
    depth: DepthTexture,
    /// `this.camera`.
    camera: Rc<RefCell<PerspectiveCamera>>,
    /// `this._historyRenderTarget`, with its `DepthTexture`. Never drawn
    /// into: both attachments are filled by copies.
    history: RenderTarget,
    /// `this._resolveRenderTarget`.
    resolve: RenderTarget,
    /// `_quadMesh` with `this._resolveMaterial`, rebuilt when
    /// `useSubpixelCorrection` changes.
    quad: RefCell<QuadMesh>,
    /// `this.useSubpixelCorrection`.
    use_subpixel_correction: Cell<bool>,
    /// `this.velocityNode`'s texture, kept for a rebuild.
    velocity: Texture,
    /// The node halves of the four uniforms, kept for a rebuild.
    uniform_nodes: UniformNodes,
    /// `this._textureNode` — `passTexture( this, resolve.texture )`.
    node: NodeRef,
    /// `this._jitterIndex`.
    jitter_index: Cell<usize>,
    /// `this._cameraNearFar`.
    near_far: SettableValue,
    /// `this._cameraWorldMatrix`. A uniform in three that the resolve shader
    /// never reads; here only the value that rolls into
    /// [`previous_world`](Self::previous_world).
    world: Cell<Matrix4>,
    /// `this._cameraWorldMatrixInverse`.
    world_inverse: SettableValue,
    /// `this._cameraProjectionMatrixInverse` — like [`world`](Self::world),
    /// read only to roll over.
    projection_inverse: Cell<Matrix4>,
    /// `this._previousCameraWorldMatrix`.
    previous_world: SettableValue,
    /// `this._previousCameraProjectionMatrixInverse`.
    previous_projection_inverse: SettableValue,
}

impl TraaNode {
    /// `new TRAANode( beautyNode, depthNode, velocityNode, camera )`, with
    /// the material `setup()` gives it.
    pub fn new(
        beauty: &Texture,
        depth: &DepthTexture,
        velocity: &Texture,
        camera: Rc<RefCell<PerspectiveCamera>>,
    ) -> Self {
        let options = || RenderTargetOptions {
            texture_type: TextureType::HalfFloat,
            samples: 0,
            depth_buffer: false,
            min_filter: TextureFilter::Linear,
            mag_filter: TextureFilter::Linear,
        };
        // `new RenderTarget( 1, 1, { depthBuffer: false, type: HalfFloatType,
        // depthTexture: new DepthTexture() } )`.
        let history = RenderTarget::new_with_options(1, 1, options())
            .expect("three-rs: the TRAA history target is a colour type");
        history.set_depth_texture(DepthTexture::new());
        history.set_copy_destination();
        let resolve = RenderTarget::new_with_options(1, 1, options())
            .expect("three-rs: the TRAA resolve target is a colour type");
        // The history depth is written by a copy out of the pass's depth.
        depth.set_copyable();
        history
            .depth_texture()
            .expect("three-rs: set just above")
            .set_copyable();

        let (near_far, near_far_value) = uniform_settable(Type::Vec2, vec![0.0, 0.0]);
        let identity = mat4_values(&Matrix4::identity());
        let (world_inverse, world_inverse_value) = uniform_settable(Type::Mat4, identity.clone());
        let (previous_world, previous_world_value) = uniform_settable(Type::Mat4, identity.clone());
        let (previous_projection_inverse, previous_projection_inverse_value) =
            uniform_settable(Type::Mat4, identity);

        let uniform_nodes = UniformNodes {
            near_far,
            world_inverse,
            previous_world,
            previous_projection_inverse,
        };
        let material = resolve_material(beauty, depth, velocity, &history, &uniform_nodes, true);

        let node = to_var(None, texture_uv(&resolve.texture(), uv()));

        let state = Rc::new(TraaState {
            beauty: beauty.clone(),
            depth: depth.clone(),
            camera,
            history,
            resolve,
            quad: RefCell::new(QuadMesh::new(material)),
            use_subpixel_correction: Cell::new(true),
            velocity: velocity.clone(),
            uniform_nodes,
            node,
            jitter_index: Cell::new(0),
            near_far: near_far_value,
            world: Cell::new(Matrix4::identity()),
            world_inverse: world_inverse_value,
            projection_inverse: Cell::new(Matrix4::identity()),
            previous_world: previous_world_value,
            previous_projection_inverse: previous_projection_inverse_value,
        });
        register_texture_update(state.resolve.texture().id(), &state);
        Self(state)
    }

    /// `traaNode.getTextureNode()` — the resolved frame, for the graph
    /// downstream (usually the pipeline's `outputNode`).
    pub fn node(&self) -> NodeRef {
        self.0.node.clone()
    }

    /// The `TRAA.resolve` quad material, for `examples/dump_wgsl.rs` and the
    /// dump gate.
    #[doc(hidden)]
    pub fn quad_material(&self) -> MeshBasicNodeMaterial {
        self.0.quad.borrow().material.clone()
    }

    /// `traaNode.useSubpixelCorrection = value` — whether the minimum
    /// weight of the current frame rises with how sub-pixel the velocity
    /// is. On by default; `webgpu_postprocessing_ao` turns it off. Three
    /// reads the flag in `setup()`, so the resolve shader is rebuilt here.
    pub fn set_use_subpixel_correction(&self, on: bool) {
        let state = &self.0;
        if state.use_subpixel_correction.replace(on) == on {
            return;
        }
        state.quad.borrow_mut().material = resolve_material(
            &state.beauty,
            &state.depth,
            &state.velocity,
            &state.history,
            &state.uniform_nodes,
            on,
        );
    }

    /// The `OnBeforeRenderPipeline` / `OnAfterRenderPipeline` half of
    /// `TRAANode.setup()`: jitter the camera before the pipeline renders,
    /// clear the jitter after. Call once, on the pipeline whose output this
    /// node feeds.
    ///
    /// Three's `viewOffsetOwner` guard is kept: when the pipeline already has
    /// an owner (a second TRAA node), nothing is installed and that node's
    /// jitter serves both.
    pub fn attach(&self, pipeline: &mut RenderPipeline) {
        if !pipeline.claim_view_offset() {
            return;
        }
        let before = self.0.clone();
        pipeline.on_before_render(Box::new(move |renderer: &mut Renderer| {
            let (width, height) = renderer.drawing_buffer_size();
            before.set_view_offset(renderer, width, height);
        }));
        let after = self.0.clone();
        pipeline.on_after_render(Box::new(move |renderer: &mut Renderer| {
            after.clear_view_offset(renderer);
        }));
    }
}

impl TraaState {
    /// `TRAANode.setViewOffset( width, height )`.
    fn set_view_offset(&self, renderer: &mut Renderer, width: u32, height: u32) {
        let mut camera = self.camera.borrow_mut();
        // Save the original, unjittered projection matrix for the velocity
        // pass.
        camera.update_projection_matrix();
        renderer.set_velocity_projection_matrix(Some(camera.projection_matrix));

        let (jitter_x, jitter_y) = halton_offsets()[self.jitter_index.get()];
        let (width, height) = (width as f64, height as f64);
        camera.set_view_offset(width, height, jitter_x - 0.5, jitter_y - 0.5, width, height);
    }

    /// `TRAANode.clearViewOffset()`.
    fn clear_view_offset(&self, renderer: &mut Renderer) {
        self.camera.borrow_mut().clear_view_offset();
        renderer.set_velocity_projection_matrix(None);
        self.jitter_index
            .set((self.jitter_index.get() + 1) % JITTER_COUNT);
    }

    /// `TRAANode.setSize( width, height )`.
    fn set_size(&self, width: u32, height: u32) {
        self.history.set_size(width, height);
        self.resolve.set_size(width, height);
    }
}

impl NodeUpdate for TraaState {
    /// `this.updateBeforeType = NodeUpdateType.FRAME`.
    fn update_before_type(&self) -> NodeUpdateType {
        NodeUpdateType::Frame
    }

    /// `TRAANode.updateBefore( frame )`.
    fn update_before(&self, renderer: &mut Renderer) -> bool {
        // The scene pass first — see the module docs.
        if let Some(pass) = crate::nodes::frame::texture_update(self.beauty.id()) {
            renderer.update_before_node(&pass);
        }

        // Store the previous frame's matrices before updating the current
        // ones.
        self.previous_world.set(mat4_values(&self.world.get()));
        self.previous_projection_inverse
            .set(mat4_values(&self.projection_inverse.get()));

        {
            let camera = self.camera.borrow();
            self.near_far.set(vec![camera.near, camera.far]);
            self.world.set(RenderCamera::matrix_world(&*camera));
            self.world_inverse
                .set(mat4_values(&camera.matrix_world_inverse));
            self.projection_inverse
                .set(camera.projection_matrix_inverse);
        }

        // Keep the TRAA in sync with the dimensions of the beauty node.
        let (width, height) = self.beauty.size();

        // `_rendererState = RendererUtils.resetRendererState( renderer, … )`.
        let mut renderer = renderer.reset_state();

        let needs_restart = self.history.size() != (width, height);
        self.set_size(width, height);

        // Every time the dimensions change the history needs fresh data.
        if needs_restart {
            // Make sure the targets are allocated after the resize, which
            // dropped their GPU textures.
            renderer.init_render_target(&self.history);
            renderer.init_render_target(&self.resolve);
            // Seed the history with the beauty buffer, or the frames after a
            // resize fade in from the black the history was cleared with.
            renderer.copy_render_texture(&self.beauty, &self.history.texture());
        }

        // Resolve.
        renderer.set_render_target(Some(self.resolve.clone()));
        renderer.render_quad(&self.quad.borrow());
        renderer.set_render_target(None);

        // Update the history.
        renderer.copy_render_texture(&self.resolve.texture(), &self.history.texture());

        // Copy the current depth to the previous depth buffer — only when the
        // history matches the drawing buffer, and so the scene's depth.
        if self.history.size() == renderer.drawing_buffer_size() {
            let history_depth = self
                .history
                .depth_texture()
                .expect("three-rs: the history target carries a DepthTexture");
            renderer.copy_depth_texture(&self.depth, &history_depth);
        }

        // `RendererUtils.restoreRendererState( renderer, _rendererState )`.
        drop(renderer);
        true
    }
}

/// The node halves of `this._cameraNearFar`, `this._cameraWorldMatrixInverse`,
/// `this._previousCameraWorldMatrix` and
/// `this._previousCameraProjectionMatrixInverse`.
struct UniformNodes {
    near_far: NodeRef,
    world_inverse: NodeRef,
    previous_world: NodeRef,
    previous_projection_inverse: NodeRef,
}

/// `this._resolveMaterial` with `setup()`'s `colorNode`.
fn resolve_material(
    beauty: &Texture,
    depth: &DepthTexture,
    velocity: &Texture,
    history: &RenderTarget,
    uniforms: &UniformNodes,
    use_subpixel_correction: bool,
) -> MeshBasicNodeMaterial {
    let fragment = resolve_node(&ResolveInputs {
        beauty,
        depth,
        velocity,
        history: &history.texture(),
        // `this._previousDepthNode` starts as `texture( new DepthTexture(
        // 1, 1 ) )` and is pointed at the history depth after the first
        // copy. Before that copy the history depth is a fresh,
        // zero-initialised texture, which reads the same as the 1×1 dummy,
        // so the port points at it from the start.
        previous_depth: &history
            .depth_texture()
            .expect("three-rs: the history target carries a DepthTexture"),
        near_far: uniforms.near_far.clone(),
        world_inverse: uniforms.world_inverse.clone(),
        previous_world: uniforms.previous_world.clone(),
        previous_projection_inverse: uniforms.previous_projection_inverse.clone(),
        use_subpixel_correction,
    });
    let mut material = MeshBasicNodeMaterial::new();
    material.name = "TRAA.resolve";
    material.color_node = Some(fragment);
    material
}

/// What `resolve()` closes over.
struct ResolveInputs<'a> {
    beauty: &'a Texture,
    depth: &'a DepthTexture,
    velocity: &'a Texture,
    history: &'a Texture,
    previous_depth: &'a DepthTexture,
    near_far: NodeRef,
    world_inverse: NodeRef,
    previous_world: NodeRef,
    previous_projection_inverse: NodeRef,
    use_subpixel_correction: bool,
}

/// `TRAANode.setup()`'s `resolve` `Fn()`, the resolve material's
/// `colorNode`.
fn resolve_node(inputs: &ResolveInputs) -> NodeRef {
    let uv_node = uv();
    // Assumes all the buffers share the same size.
    let texture_size = texture_size(TextureSource::Texture2D(inputs.beauty.clone()), int(0));
    let position_texel = uv_node.mul(texture_size.clone());

    // Sample the closest and farthest depths in the current buffer.
    let layout = current_depth_struct();
    let current_depth = sample_current_depth(inputs.depth, &position_texel, &layout);
    let closest_depth = struct_get(&current_depth, &layout, "closestDepth");
    let closest_position_texel = struct_get(&current_depth, &layout, "closestPositionTexel");
    let farthest_depth = struct_get(&current_depth, &layout, "farthestDepth");

    // Convert the NDC offset to a UV offset.
    let offset_uv = texture_load(inputs.velocity, closest_position_texel)
        .xy()
        .mul(vec2(0.5, -0.5));

    // Sample the previous depth.
    let history_uv = uv_node.sub(offset_uv.clone());
    let previous_depth = sample_previous_depth(
        inputs.previous_depth,
        &history_uv,
        &inputs.previous_projection_inverse,
        &inputs.previous_world,
        &inputs.world_inverse,
        &inputs.near_far,
    );

    // The history is valid when the UV is in range and there is no
    // disocclusion, except on edges.
    let is_valid_uv =
        all(history_uv.greater_than_equal(0.0)).and(all(history_uv.less_than_equal(1.0)));
    let is_edge = farthest_depth
        .sub(closest_depth.clone())
        .greater_than(EDGE_DEPTH_DIFF);
    let is_disocclusion = closest_depth
        .sub(previous_depth)
        .greater_than(DEPTH_THRESHOLD);
    let has_valid_history = is_valid_uv.and(is_edge.or(is_disocclusion.not()));

    // Sample the current and previous colours.
    let current_color = texture_uv(inputs.beauty, uv_node.clone());
    let history_color = texture_sample(inputs.history, history_uv.clone());

    // Increase the weight towards the current frame under motion.
    let motion_factor = length(uv_node.sub(history_uv).mul(texture_size.clone()))
        .div(MAX_VELOCITY_LENGTH)
        .saturate();
    // A minimum weight.
    let current_weight = to_var(None, float(0.05));
    let mut statements = vec![current_weight.clone()];
    if inputs.use_subpixel_correction {
        // `useSubpixelCorrection`: raise the minimum weight towards the
        // current frame the more sub-pixel the velocity is.
        statements.push(
            current_weight
                .add_assign(call(&subpixel_correction(), vec![offset_uv, texture_size]).mul(0.25)),
        );
    }
    statements.push(current_weight.assign(has_valid_history.select(
        current_weight.add(motion_factor.clone()).saturate(),
        float(1.0),
    )));

    // Neighbourhood clipping, by variance. A reasonable gamma range is
    // [0.75, 2].
    let variance_gamma = mix(0.5, 1.0, motion_factor.one_minus().pow2());
    let clipped_history_color = variance_clipping(
        inputs.beauty,
        &position_texel,
        &current_color,
        &history_color,
        variance_gamma,
    );

    // Flicker reduction, by luminance weighting.
    block(
        statements,
        call(
            &flicker_reduction(),
            vec![current_color, clipped_history_color, current_weight],
        ),
    )
}

/// The `varianceClipping` `Fn()`: the mean and standard deviation of the 3×3
/// neighbourhood (taps clamped at zero so a NaN cannot spread), scaled by
/// `gamma`, and the history clipped to that box.
fn variance_clipping(
    beauty: &Texture,
    position_texel: &NodeRef,
    current_color: &NodeRef,
    history_color: &NodeRef,
    gamma: NodeRef,
) -> NodeRef {
    const OFFSETS: [(i64, i64); 8] = [
        (-1, -1),
        (-1, 1),
        (1, -1),
        (1, 1),
        (1, 0),
        (0, -1),
        (0, 1),
        (-1, 0),
    ];
    let moment1 = to_var(None, current_color.clone());
    let moment2 = to_var(None, current_color.pow2());
    let mut statements = vec![moment1.clone(), moment2.clone()];
    for (x, y) in OFFSETS {
        let neighbor =
            texture_load_offset(beauty, position_texel.clone(), ivec2(int(x), int(y))).max(0.0);
        statements.push(moment1.add_assign(neighbor.clone()));
        statements.push(moment2.add_assign(neighbor.pow2()));
    }

    let n = float((OFFSETS.len() + 1) as f64);
    let mean = moment1.div(n.clone());
    let variance = moment2.div(n).sub(mean.pow2()).max(0.0).sqrt().mul(gamma);
    let min_color = mean.sub(variance.clone());
    let max_color = mean.add(variance);

    block(
        statements,
        call(
            &clip_aabb(),
            vec![
                mean.clamp(min_color.clone(), max_color.clone()),
                history_color.clone(),
                min_color,
                max_color,
            ],
        ),
    )
}

/// The `subpixelCorrection` `Fn()`, with its layout: how sub-pixel the
/// velocity is, in [0, 1].
fn subpixel_correction() -> Rc<crate::nodes::node::FnDef> {
    shader_fn(
        Some("subpixelCorrection"),
        vec![("velocityUV", Type::Vec2), ("textureSize", Type::IVec2)],
        Type::F32,
        |args| {
            let velocity_texel = args[0].mul(args[1].to(Type::Vec2));
            let phase = velocity_texel.fract().abs();
            let weight = max(phase.clone(), phase.one_minus());
            weight.x().mul(weight.y()).one_minus().div(0.75)
        },
    )
}