viewport-lib-types 0.3.0

Pure-data types for viewport-lib: submission payloads, handles, and frame config, free of any GPU dependency.
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
580
581
582
583
584
585
586
587
588
589
590
//! Time-varying animation parameters for overlay items.

/// Easing curve applied to an [`AnimTrack`]'s normalised parameter `t`.
///
/// The named variants are presets; [`OverlayEasing::CubicBezier`] is the
/// general case, with CSS `cubic-bezier()` semantics. A CSS cubic-bezier is a
/// single segment that is monotone in x, so it expresses overshoot ([`Back`])
/// but cannot express multi-oscillation curves: [`Bounce`] and [`Elastic`] are
/// evaluated natively for that reason, the same reason CSS added `linear()`.
///
/// [`Back`]: OverlayEasing::Back
/// [`Bounce`]: OverlayEasing::Bounce
/// [`Elastic`]: OverlayEasing::Elastic
#[derive(Debug, Clone, Copy, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub enum OverlayEasing {
    /// Returns `t` unchanged. Constant speed.
    #[default]
    Linear,
    /// `t * t`. Starts slow, accelerates.
    EaseIn,
    /// `1 - (1 - t)^2`. Starts fast, decelerates.
    EaseOut,
    /// Smoothstep: `3t^2 - 2t^3`. Slow start, fast middle, slow end.
    EaseInOut,
    /// Sinusoidal half-wave: `sin(t * PI)`. Returns to 0 at both ends; peaks
    /// at the midpoint. Combine with [`RepeatMode::Loop`] for a continuous
    /// pulse.
    Pulse,
    /// Pulls back before moving, and overshoots at the end. A preset over
    /// `CubicBezier { x1: 0.68, y1: -0.55, x2: 0.265, y2: 1.55 }`, which is
    /// CSS's `ease-in-out-back`.
    Back,
    /// Settles with decaying bounces, like something dropped on a surface.
    /// Not expressible as a cubic-bezier: it changes direction several times.
    Bounce,
    /// Overshoots and oscillates into place with decaying amplitude. Also not
    /// expressible as a cubic-bezier.
    Elastic,
    /// The general case: a cubic Bezier from `(0, 0)` to `(1, 1)` with control
    /// points `(x1, y1)` and `(x2, y2)`, matching CSS `cubic-bezier()`.
    ///
    /// `x1` and `x2` are clamped to `[0, 1]` so the curve stays a function of
    /// time; `y1` and `y2` are unbounded, which is what allows overshoot.
    /// Evaluating it solves for the curve parameter at the given `x` and then
    /// reads `y`, so the cost is a handful of Newton steps.
    CubicBezier {
        /// x of the first control point, clamped to `[0, 1]`.
        x1: f32,
        /// y of the first control point. Values outside `[0, 1]` overshoot.
        y1: f32,
        /// x of the second control point, clamped to `[0, 1]`.
        x2: f32,
        /// y of the second control point. Values outside `[0, 1]` overshoot.
        y2: f32,
    },
}

/// How an [`AnimTrack`] handles time past the end of its duration.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[non_exhaustive]
pub enum RepeatMode {
    /// Run the track once and hold the final value. Default.
    #[default]
    Once,
    /// Restart the track from `from` each cycle.
    Loop,
    /// Reverse direction at each end so the value oscillates between
    /// `from` and `to`.
    PingPong,
}

/// A single animation track interpolating one channel from `from` to `to`
/// over `duration` seconds, with optional easing and repeat mode.
///
/// Resolved during `prepare()` using `OverlayFrame::time`. Times share the
/// same application-defined epoch as the rest of the overlay animation
/// system. Negative or zero `duration` snaps directly to `to`.
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub struct AnimTrack<T: Copy> {
    /// Absolute time at which the track starts.
    pub start_time: f64,
    /// Length of one cycle in seconds.
    pub duration: f32,
    /// Value at `start_time` (or each loop restart).
    pub from: T,
    /// Value at `start_time + duration`.
    pub to: T,
    /// Curve applied to the normalised parameter before interpolation.
    pub easing: OverlayEasing,
    /// What happens past the end of one cycle.
    pub repeat: RepeatMode,
}

impl<T: Copy + Default> Default for AnimTrack<T> {
    fn default() -> Self {
        Self {
            start_time: 0.0,
            duration: 1.0,
            from: T::default(),
            to: T::default(),
            easing: OverlayEasing::Linear,
            repeat: RepeatMode::Once,
        }
    }
}

/// Animation tracks attached to an overlay item or a retained group.
///
/// Each `Some` track replaces the matching field on the item for the frame.
/// Tracks are independent: an item can translate, scale, recolour, and rotate
/// at once.
///
/// # Why these five channels and no others
///
/// These are exactly the channels that survive compilation. A translate, a
/// rotation, a scale, an opacity, and a tint all ride the per-draw instance,
/// so they animate a retained group with no re-tessellation and animate every
/// overlay family identically. A size, a border colour, or a gradient stop
/// does not: driving one means re-emitting the geometry, which defeats
/// retention, and in immediate mode the consumer is already rebuilding the
/// item every frame and can set the field directly.
///
/// # Curves are ours; triggers are yours
///
/// viewport-lib evaluates the curve. When an animation starts, whether it
/// restarts, and what it responds to are the application's. `epoch` is where
/// the two meet: a track's `start_time` is a delay measured from it, so
/// authored content can say "0.2 seconds in" without knowing what time the
/// process will be started at.
///
/// Resolution is CPU-side in `prepare()`; the host must request continuous
/// repaints while any track is active.
#[derive(Debug, Clone, PartialEq, Default)]
#[non_exhaustive]
pub struct OverlayAnimations {
    /// Time all `start_time` values are measured from, on the same clock as
    /// `OverlayFrame::time`. `0.0` (the default) makes `start_time` an
    /// absolute time on that clock; set it when the animation begins to make
    /// `start_time` a delay instead.
    pub epoch: f64,
    /// Drives the item's overall opacity multiplier.
    pub opacity: Option<AnimTrack<f32>>,
    /// Drives `transform.translate`, the screen-pixel nudge layered on the
    /// resolved anchor.
    pub translate: Option<AnimTrack<[f32; 2]>>,
    /// Drives `transform.rotation`, in radians.
    pub rotation: Option<AnimTrack<f32>>,
    /// Drives `transform.scale`.
    pub scale: Option<AnimTrack<f32>>,
    /// Drives `tint`, the per-frame colour multiplier.
    pub tint: Option<AnimTrack<[f32; 4]>>,
}

impl OverlayAnimations {
    /// Set the epoch all `start_time` values are measured from.
    pub fn with_epoch(mut self, epoch: f64) -> Self {
        self.epoch = epoch;
        self
    }

    /// Set the `opacity` track.
    pub fn with_opacity(mut self, track: AnimTrack<f32>) -> Self {
        self.opacity = Some(track);
        self
    }

    /// Set the `translate` track.
    pub fn with_translate(mut self, track: AnimTrack<[f32; 2]>) -> Self {
        self.translate = Some(track);
        self
    }

    /// Set the `rotation` track.
    pub fn with_rotation(mut self, track: AnimTrack<f32>) -> Self {
        self.rotation = Some(track);
        self
    }

    /// Set the `scale` track.
    pub fn with_scale(mut self, track: AnimTrack<f32>) -> Self {
        self.scale = Some(track);
        self
    }

    /// Set the `tint` track.
    pub fn with_tint(mut self, track: AnimTrack<[f32; 4]>) -> Self {
        self.tint = Some(track);
        self
    }

    /// Resolve every track at `time` onto the per-frame state it drives.
    ///
    /// The one place the channel list is turned into field writes, so the
    /// immediate path, the retained path, and a consumer that wants to resolve
    /// tracks itself all agree. A track replaces the field rather than adding
    /// to it, so an item's authored `translate` is the value the track
    /// interpolates away from only if the track says so.
    pub fn apply(
        &self,
        time: f64,
        transform: &mut crate::overlay::OverlayTransform,
        opacity: &mut f32,
        tint: &mut [f32; 4],
    ) {
        let t = time - self.epoch;
        if let Some(track) = self.opacity {
            *opacity = track.sample(t);
        }
        if let Some(track) = self.translate {
            transform.translate = track.sample(t);
        }
        if let Some(track) = self.rotation {
            transform.rotation = track.sample(t);
        }
        if let Some(track) = self.scale {
            transform.scale = track.sample(t);
        }
        if let Some(track) = self.tint {
            *tint = track.sample(t);
        }
    }

    /// Whether any track is set, so a caller can skip the resolve.
    pub fn is_empty(&self) -> bool {
        self.opacity.is_none()
            && self.translate.is_none()
            && self.rotation.is_none()
            && self.scale.is_none()
            && self.tint.is_none()
    }
}

/// Trait used by [`AnimTrack`] resolution to interpolate between `from`
/// and `to`. Implemented for the channel types the overlay animation
/// system needs: `f32`, `[f32; 2]`, `[f32; 4]`.
pub trait LerpAnim: Copy {
    /// Returns `from * (1 - t) + to * t`.
    fn lerp(from: Self, to: Self, t: f32) -> Self;
}

impl LerpAnim for f32 {
    fn lerp(from: Self, to: Self, t: f32) -> Self {
        from * (1.0 - t) + to * t
    }
}

impl LerpAnim for [f32; 2] {
    fn lerp(from: Self, to: Self, t: f32) -> Self {
        [f32::lerp(from[0], to[0], t), f32::lerp(from[1], to[1], t)]
    }
}

impl LerpAnim for [f32; 4] {
    fn lerp(from: Self, to: Self, t: f32) -> Self {
        [
            f32::lerp(from[0], to[0], t),
            f32::lerp(from[1], to[1], t),
            f32::lerp(from[2], to[2], t),
            f32::lerp(from[3], to[3], t),
        ]
    }
}

impl<T: Copy> AnimTrack<T> {
    /// A track running `from` to `to` over `duration` seconds, starting at
    /// `start_time` (a delay from [`OverlayAnimations::epoch`], or an absolute
    /// time when that is left at zero). Linear, once through.
    pub fn new(start_time: f64, duration: f32, from: T, to: T) -> Self {
        Self {
            start_time,
            duration,
            from,
            to,
            easing: OverlayEasing::Linear,
            repeat: RepeatMode::Once,
        }
    }

    /// Set the easing curve.
    pub fn with_easing(mut self, easing: OverlayEasing) -> Self {
        self.easing = easing;
        self
    }

    /// Set what happens past the end of one cycle.
    pub fn with_repeat(mut self, repeat: RepeatMode) -> Self {
        self.repeat = repeat;
        self
    }

    /// Set the start time, a delay from [`OverlayAnimations::epoch`].
    pub fn with_start_time(mut self, start_time: f64) -> Self {
        self.start_time = start_time;
        self
    }
}

impl<T: Copy + LerpAnim> AnimTrack<T> {
    /// Resolve the track at `time` measured against `epoch`, which is what
    /// [`OverlayAnimations::epoch`] is for: `start_time` is then a delay
    /// rather than a date.
    pub fn sample_from(&self, time: f64, epoch: f64) -> T {
        self.sample(time - epoch)
    }

    /// Resolve the track at the given absolute time. Returns the
    /// interpolated value.
    pub fn sample(&self, time: f64) -> T {
        if self.duration <= 0.0 {
            return self.to;
        }
        let raw = ((time - self.start_time) as f32) / self.duration;
        let phase = resolve_phase(raw, self.repeat);
        let t = apply_easing(phase, self.easing);
        T::lerp(self.from, self.to, t)
    }
}

/// Map a raw normalised parameter (number of cycles since `start_time`) into
/// the canonical `[0, 1]` phase using the given repeat mode.
fn resolve_phase(raw: f32, repeat: RepeatMode) -> f32 {
    match repeat {
        RepeatMode::Once => raw.clamp(0.0, 1.0),
        RepeatMode::Loop => {
            let f = raw - raw.floor();
            if f < 0.0 { f + 1.0 } else { f }
        }
        RepeatMode::PingPong => {
            let two = (raw * 0.5).floor() * 2.0;
            let r = raw - two;
            if r > 1.0 { 2.0 - r } else { r }
        }
    }
}

/// One coordinate of a cubic Bezier from 0 to 1 with control values `a` and
/// `b`, in Bernstein form.
fn bezier_axis(t: f32, a: f32, b: f32) -> f32 {
    let u = 1.0 - t;
    3.0 * u * u * t * a + 3.0 * u * t * t * b + t * t * t
}

/// Derivative of [`bezier_axis`] with respect to `t`.
fn bezier_axis_slope(t: f32, a: f32, b: f32) -> f32 {
    let u = 1.0 - t;
    3.0 * u * u * a + 6.0 * u * t * (b - a) + 3.0 * t * t * (1.0 - b)
}

/// CSS `cubic-bezier()`: find the curve parameter whose x is `x`, then read y.
///
/// Newton from a linear guess, falling back to bisection when the slope is
/// flat enough that Newton would step wildly. Eight iterations is well past
/// convergence for the range CSS allows, and the whole thing is a handful of
/// multiplies per sample.
fn cubic_bezier_ease(x: f32, x1: f32, y1: f32, x2: f32, y2: f32) -> f32 {
    let x1 = x1.clamp(0.0, 1.0);
    let x2 = x2.clamp(0.0, 1.0);
    if x <= 0.0 {
        return 0.0;
    }
    if x >= 1.0 {
        return 1.0;
    }
    let mut t = x;
    for _ in 0..8 {
        let err = bezier_axis(t, x1, x2) - x;
        if err.abs() < 1e-5 {
            return bezier_axis(t, y1, y2);
        }
        let slope = bezier_axis_slope(t, x1, x2);
        if slope.abs() < 1e-6 {
            break;
        }
        t -= err / slope;
    }
    // Bisection fallback: guaranteed to converge because x is monotone in t.
    let (mut lo, mut hi) = (0.0f32, 1.0f32);
    let mut t = x;
    for _ in 0..24 {
        let cx = bezier_axis(t, x1, x2);
        if (cx - x).abs() < 1e-5 {
            break;
        }
        if cx < x {
            lo = t;
        } else {
            hi = t;
        }
        t = (lo + hi) * 0.5;
    }
    bezier_axis(t, y1, y2)
}

/// The standard four-segment bounce, matching the shape every easing library
/// ships under that name.
fn bounce_ease(t: f32) -> f32 {
    const N: f32 = 7.5625;
    const D: f32 = 2.75;
    if t < 1.0 / D {
        N * t * t
    } else if t < 2.0 / D {
        let t = t - 1.5 / D;
        N * t * t + 0.75
    } else if t < 2.5 / D {
        let t = t - 2.25 / D;
        N * t * t + 0.9375
    } else {
        let t = t - 2.625 / D;
        N * t * t + 0.984375
    }
}

/// Apply an easing curve to a `[0, 1]` phase.
fn apply_easing(phase: f32, easing: OverlayEasing) -> f32 {
    match easing {
        OverlayEasing::Linear => phase,
        OverlayEasing::EaseIn => phase * phase,
        OverlayEasing::EaseOut => {
            let inv = 1.0 - phase;
            1.0 - inv * inv
        }
        OverlayEasing::EaseInOut => phase * phase * (3.0 - 2.0 * phase),
        OverlayEasing::Pulse => (phase * std::f32::consts::PI).sin(),
        OverlayEasing::Back => cubic_bezier_ease(phase, 0.68, -0.55, 0.265, 1.55),
        OverlayEasing::Bounce => bounce_ease(phase),
        OverlayEasing::Elastic => {
            if phase <= 0.0 {
                0.0
            } else if phase >= 1.0 {
                1.0
            } else {
                let c = std::f32::consts::TAU / 3.0;
                (2.0f32).powf(-10.0 * phase) * ((phase * 10.0 - 0.75) * c).sin() + 1.0
            }
        }
        OverlayEasing::CubicBezier { x1, y1, x2, y2 } => cubic_bezier_ease(phase, x1, y1, x2, y2),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn close(a: f32, b: f32) -> bool {
        (a - b).abs() < 2e-3
    }

    /// A cubic-bezier easing has to agree with CSS at the ends and be monotone
    /// in x, or the x-to-t solve has no unique answer.
    #[test]
    fn cubic_bezier_matches_its_endpoints_and_stays_monotone() {
        let ease = OverlayEasing::CubicBezier {
            x1: 0.25,
            y1: 0.1,
            x2: 0.25,
            y2: 1.0,
        };
        assert!(close(apply_easing(0.0, ease), 0.0));
        assert!(close(apply_easing(1.0, ease), 1.0));
        let mut prev = -1.0;
        for i in 0..=50 {
            let v = apply_easing(i as f32 / 50.0, ease);
            assert!(v >= prev - 1e-3, "not monotone at {i}: {prev} -> {v}");
            prev = v;
        }
    }

    /// The identity control points reproduce linear, which is the cheapest
    /// check that the solve is not off by a reparametrisation.
    #[test]
    fn the_identity_bezier_is_linear() {
        let ease = OverlayEasing::CubicBezier {
            x1: 1.0 / 3.0,
            y1: 1.0 / 3.0,
            x2: 2.0 / 3.0,
            y2: 2.0 / 3.0,
        };
        for i in 0..=20 {
            let t = i as f32 / 20.0;
            assert!(close(apply_easing(t, ease), t), "at {t}");
        }
    }

    /// `Bounce` and `Elastic` land on 0 and 1 and leave the unit range in
    /// between, which is the property a cubic-bezier cannot reproduce and the
    /// reason they are native variants.
    #[test]
    fn bounce_and_elastic_are_not_expressible_as_one_bezier_segment() {
        for ease in [OverlayEasing::Bounce, OverlayEasing::Elastic] {
            assert!(close(apply_easing(0.0, ease), 0.0), "{ease:?} at 0");
            assert!(close(apply_easing(1.0, ease), 1.0), "{ease:?} at 1");
        }
        // Elastic overshoots past 1 on its way in.
        let peak = (0..100)
            .map(|i| apply_easing(i as f32 / 100.0, OverlayEasing::Elastic))
            .fold(f32::MIN, f32::max);
        assert!(peak > 1.0, "Elastic never overshoots: peak {peak}");
        // Bounce reverses direction, so it is non-monotone.
        let mut reversals = 0;
        let mut prev = apply_easing(0.0, OverlayEasing::Bounce);
        let mut rising = true;
        for i in 1..=100 {
            let v = apply_easing(i as f32 / 100.0, OverlayEasing::Bounce);
            let now = v >= prev;
            if now != rising {
                reversals += 1;
                rising = now;
            }
            prev = v;
        }
        assert!(reversals >= 3, "Bounce should reverse several times");
    }

    /// `epoch` is the whole point of authored animation: the same track data
    /// plays the same way whatever time the process was started at.
    #[test]
    fn a_start_time_is_a_delay_measured_from_the_epoch() {
        let track = AnimTrack {
            start_time: 0.5,
            duration: 1.0,
            from: 0.0f32,
            to: 1.0f32,
            ..Default::default()
        };
        // Authored "half a second in", played from two different wall clocks.
        for epoch in [0.0, 1_700_000_000.0] {
            assert!(close(track.sample_from(epoch + 0.5, epoch), 0.0));
            assert!(close(track.sample_from(epoch + 1.0, epoch), 0.5));
            assert!(close(track.sample_from(epoch + 1.5, epoch), 1.0));
        }
    }

    /// The tracks resolve onto exactly the five channels that ride the
    /// per-draw instance, and nothing else.
    #[test]
    fn apply_writes_the_instance_channels() {
        let anims = OverlayAnimations::default()
            .with_epoch(100.0)
            .with_opacity(AnimTrack {
                start_time: 0.0,
                duration: 1.0,
                from: 0.0,
                to: 1.0,
                ..Default::default()
            })
            .with_translate(AnimTrack {
                start_time: 0.0,
                duration: 1.0,
                from: [0.0, 0.0],
                to: [10.0, 20.0],
                ..Default::default()
            })
            .with_rotation(AnimTrack {
                start_time: 0.0,
                duration: 1.0,
                from: 0.0,
                to: 1.0,
                ..Default::default()
            })
            .with_scale(AnimTrack {
                start_time: 0.0,
                duration: 1.0,
                from: 1.0,
                to: 2.0,
                ..Default::default()
            })
            .with_tint(AnimTrack {
                start_time: 0.0,
                duration: 1.0,
                from: [1.0, 1.0, 1.0, 1.0],
                to: [0.0, 0.5, 1.0, 1.0],
                ..Default::default()
            });

        let mut transform = crate::overlay::OverlayTransform::IDENTITY;
        let mut opacity = 1.0f32;
        let mut tint = [1.0f32; 4];
        anims.apply(100.5, &mut transform, &mut opacity, &mut tint);

        assert!(close(opacity, 0.5));
        assert!(close(transform.translate[0], 5.0));
        assert!(close(transform.translate[1], 10.0));
        assert!(close(transform.rotation, 0.5));
        assert!(close(transform.scale, 1.5));
        assert!(close(tint[1], 0.75));
    }
}