Skip to content

The animation engine for agents: blazingly fast math and 3D videos with Manim's Python API, on a Rust GPU core.

Python

Encode video frames with x264

apoogdkeb3cdde6e41c
diff --git a/rust/engine/src/encode.rs b/rust/engine/src/encode.rs
new file mode 100644
index 00000000..b0e86e5b
--- /dev/null
+++ b/rust/engine/src/encode.rs
@@ -0,0 +1,141 @@
+//! H.264 through x264 (the `x264` crate: x264 built from its source), told which macroblocks did
+//! not change since the previous frame, into an MP4.
+//!
+//! The film knows what changed; x264 honors `X264_MBINFO_CONSTANT` per macroblock (with
+//! `analyse.b_mb_info`) and skips its analysis there. A held picture is one sample that lasts
+//! longer: nothing is encoded for it, its duration is the gap to the next pts. x264 writes each
+//! picture as NAL units prefixed by their sizes — an MP4 sample as it is.
+
+use crate::mp4::{Audio, Mp4};
+
+/// A frame as the encoder receives it: what changed since the previous one — the converted
+/// macroblocks (`listed`, and their NV12 `packets`, 384 bytes each: luma 16×16, then chroma 8
+/// rows of 16) — and for how many frames it shows. The encoder keeps the picture.
+pub struct Change {
+ pub listed: Vec<u32>,
+ pub packets: Vec<u8>,
+ pub first: bool,
+ pub repeat: u32,
+ /// A keyframe: a player can start here (a section's first frame).
+ pub key: bool,
+}
+
+/// One changed macroblock into the picture: the part of it inside the picture (a picture whose
+/// sides are not multiples of 16 ends in partial macroblocks, right and bottom).
+fn scatter(packet: &[u8], mb: usize, columns: usize, width: usize, height: usize, nv12: &mut [u8]) {
+ let (x, y) = ((mb % columns) * 16, (mb / columns) * 16);
+ let inside = 16.min(width - x); // bytes of a row: luma, or chroma's interleaved pairs
+ for row in 0..16.min(height.saturating_sub(y)) {
+ let at = (y + row) * width + x;
+ nv12[at..at + inside].copy_from_slice(&packet[row * 16..row * 16 + inside]);
+ }
+ let luma = width * height;
+ for row in 0..8.min((height / 2).saturating_sub(y / 2)) {
+ let at = luma + (y / 2 + row) * width + x;
+ nv12[at..at + inside].copy_from_slice(&packet[256 + row * 16..256 + row * 16 + inside]);
+ }
+}
+
+/// An encoder on its own thread, keeping the picture: changes go in through a channel, so
+/// drawing, converting and encoding overlap, and a frame costs the thread that draws it only
+/// what changed. `join` writes the file and returns (bytes written, seconds spent in x264);
+/// `abort` leaves no file.
+pub struct Worker {
+ frames: std::sync::mpsc::SyncSender<Change>,
+ sound: std::sync::mpsc::Sender<Option<Audio>>,
+ thread: std::thread::JoinHandle<Result<(u64, f64), String>>,
+ aborted: std::sync::Arc<std::sync::atomic::AtomicBool>,
+}
+
+impl Worker {
+ pub fn new(mut encoder: Encoder, width: usize, height: usize) -> Self {
+ let (frames, inbox) = std::sync::mpsc::sync_channel::<Change>(8);
+ let (sound, heard) = std::sync::mpsc::channel::<Option<Audio>>();
+ let aborted = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
+ let stop = aborted.clone();
+ let thread = std::thread::spawn(move || {
+ let mut busy = 0.0;
+ let mut nv12 = vec![0u8; width * height * 3 / 2];
+ let (columns, rows) = (width.div_ceil(16), height.div_ceil(16));
+ for change in inbox {
+ if stop.load(std::sync::atomic::Ordering::Acquire) {
+ break;
+ }
+ let t0 = std::time::Instant::now();
+ let mut constant = vec![1u8; columns * rows];
+ for (k, &mb) in change.listed.iter().enumerate() {
+ scatter(&change.packets[k * 384..k * 384 + 384], mb as usize, columns, width, height, &mut nv12);
+ constant[mb as usize] = 0;
+ }
+ encoder.encode(&nv12, (!change.first).then_some(&constant[..]), change.repeat, change.key)?;
+ busy += t0.elapsed().as_secs_f64();
+ }
+ if stop.load(std::sync::atomic::Ordering::Acquire) {
+ return Err("the video was abandoned".into());
+ }
+ let audio = heard.recv().ok().flatten();
+ let t0 = std::time::Instant::now();
+ let bytes = encoder.finish(audio.as_ref())?;
+ Ok((bytes, busy + t0.elapsed().as_secs_f64()))
+ });
+ Self { frames, sound, thread, aborted }
+ }
+
+ /// Stop without writing the file: its partial samples are deleted by the time this returns
+ /// (the thread ends at its next frame and drops the encoder, and the encoder its MP4).
+ pub fn abort(self) {
+ self.aborted.store(true, std::sync::atomic::Ordering::Release);
+ let _ = self.join(None);
+ }
+
+ pub fn send(&self, change: Change) -> Result<(), String> {
+ self.frames.send(change).map_err(|_| "the encoder stopped".to_string())
+ }
+
+ /// Write the file, with `audio` beside the video, if any.
+ pub fn join(self, audio: Option<Audio>) -> Result<(u64, f64), String> {
+ let _ = self.sound.send(audio);
+ drop(self.frames);
+ self.thread.join().map_err(|_| "the encoder panicked".to_string())?
+ }
+}
+
+pub struct Encoder {
+ x264: x264::Encoder,
+ mp4: Mp4,
+ pts: i64,
+}
+
+impl Encoder {
+ /// `options`: x264's own (name, value) settings, applied over the preset (as `-x264-params`).
+ #[allow(clippy::too_many_arguments)]
+ pub fn new(width: u32, height: u32, fps: u32, preset: &str, crf: f32, options: &[(String, String)], path: &str) -> Result<Self, String> {
+ let x264 = x264::Encoder::new(width, height, fps, preset, crf, options)?;
+ let mp4 = Mp4::new(path, width, height, fps, x264.sps.clone(), x264.pps.clone()).map_err(|e| e.to_string())?;
+ Ok(Self { x264, mp4, pts: 0 })
+ }
+
+ /// Encode one NV12 frame shown for `duration` frames; `constant` (one byte per macroblock,
+ /// nonzero = unchanged since the previous frame) lets x264 skip those macroblocks; `key`
+ /// makes it a keyframe.
+ pub fn encode(&mut self, nv12: &[u8], constant: Option<&[u8]>, duration: u32, key: bool) -> Result<(), String> {
+ let pts = self.pts;
+ self.pts += duration as i64;
+ if let Some(sample) = self.x264.encode(Some(nv12), pts, constant, key)? {
+ self.mp4.sample(sample.data, sample.pts, sample.dts, sample.key).map_err(|e| e.to_string())?;
+ }
+ Ok(())
+ }
+
+ /// Drain the frames x264 still holds and write the file, with `audio` if any; returns its
+ /// size.
+ pub fn finish(self, audio: Option<&Audio>) -> Result<u64, String> {
+ let Self { mut x264, mut mp4, pts } = self;
+ while x264.delayed() {
+ if let Some(sample) = x264.encode(None, 0, None, false)? {
+ mp4.sample(sample.data, sample.pts, sample.dts, sample.key).map_err(|e| e.to_string())?;
+ }
+ }
+ mp4.finish(pts, audio).map_err(|e| e.to_string())
+ }
+}
diff --git a/rust/engine/src/nv12.wgsl b/rust/engine/src/nv12.wgsl
new file mode 100644
index 00000000..5a42469f
--- /dev/null
+++ b/rust/engine/src/nv12.wgsl
@@ -0,0 +1,52 @@
+// The frame's changed macroblocks as NV12, packed: only what changed is converted and read back.
+// Per macroblock 96 words: luma 16 rows × 16 bytes, then chroma 8 rows × (8 U,V pairs).
+// BT.601 over limited range, applied to the sRGB bytes (what the encoder is told).
+
+struct Grid {
+ size: vec4<u32>, // macroblocks per row, macroblocks listed, unused, unused
+};
+
+@group(0) @binding(0) var frame: texture_2d<f32>;
+@group(0) @binding(1) var<storage, read> listed: array<u32>;
+@group(0) @binding(2) var<storage, read_write> packed: array<u32>;
+@group(0) @binding(3) var<uniform> grid: Grid;
+
+fn luma(c: vec3<f32>) -> u32 {
+ return u32(clamp(round(16.0 + dot(c, vec3<f32>(65.481, 128.553, 24.966))), 0.0, 255.0));
+}
+
+fn chroma(c: vec3<f32>) -> u32 {
+ let u = u32(clamp(round(128.0 + dot(c, vec3<f32>(-37.797, -74.203, 112.0))), 0.0, 255.0));
+ let v = u32(clamp(round(128.0 + dot(c, vec3<f32>(112.0, -93.786, -18.214))), 0.0, 255.0));
+ return u | (v << 8u);
+}
+
+// One invocation: a 4×2 block of pixels (two luma words, one chroma word of two U,V pairs).
+@compute @workgroup_size(4, 8)
+fn cs_macroblocks(@builtin(workgroup_id) group: vec3<u32>, @builtin(local_invocation_id) local: vec3<u32>) {
+ let slot = group.x;
+ if (slot >= grid.size.y) {
+ return;
+ }
+ let mb = listed[slot];
+ let origin = vec2<i32>(i32((mb % grid.size.x) * 16u + local.x * 4u), i32((mb / grid.size.x) * 16u + local.y * 2u));
+ var top = 0u;
+ var bottom = 0u;
+ var pairs = 0u;
+ for (var k = 0; k < 2; k++) {
+ var sum = vec3<f32>(0.0);
+ for (var dx = 0; dx < 2; dx++) {
+ let x = k * 2 + dx;
+ let a = textureLoad(frame, origin + vec2<i32>(x, 0), 0).rgb;
+ let b = textureLoad(frame, origin + vec2<i32>(x, 1), 0).rgb;
+ top |= luma(a) << (8u * u32(x));
+ bottom |= luma(b) << (8u * u32(x));
+ sum += a + b;
+ }
+ pairs |= chroma(sum * 0.25) << (16u * u32(k));
+ }
+ let base = slot * 96u;
+ packed[base + local.y * 8u + local.x] = top;
+ packed[base + local.y * 8u + 4u + local.x] = bottom;
+ packed[base + 64u + local.y * 4u + local.x] = pairs;
+}
diff --git a/rust/engine/src/pack.rs b/rust/engine/src/pack.rs
new file mode 100644
index 00000000..7affb372
--- /dev/null
+++ b/rust/engine/src/pack.rs
@@ -0,0 +1,317 @@
+//! Versioned arrays: how a take codes what it uploads (see `take`).
+//!
+//! A take's uploads are arrays — a mesh's points, uvs, normals and triangles; a path's control
+//! points and subpaths; a cloud's points; a brush's rows; an image's pixels; a frame's records —
+//! each under a key of its kind and content, sent once. An array that replaces another (the same
+//! object's, in the same role, the frame before: its base) is coded against it: a film's shapes
+//! change a little from frame to frame, so what is sent is their change. Of these codings, the
+//! smallest is kept:
+//!
+//! - `ALONE`: the array itself;
+//! - `FIRST`: its difference from the base;
+//! - `SECOND`: its difference from the linear prediction `2 base − base's base` (floats: what
+//! moves smoothly moves on);
+//! - `TAIL`: the base is its start, and the rest is new.
+//!
+//! Floats are differenced as the integers that order as they do, so a small change is a small
+//! integer. The integers are laid out lane by lane (x's, then y's, then z's) and byte by byte,
+//! then compressed with zstd. Floats go at the precision the GPU draws them: a mesh's float64
+//! points are rounded to float32 on the way in (measured on the example films: about one pixel
+//! in 200,000 changes, by one level). A chain of versions is cut every `DEPTH` (a keyframe), so
+//! any version decodes from a keyframe and a few differences.
+
+use bytemuck::Pod;
+
+pub(crate) const ALONE: u8 = 0;
+pub(crate) const FIRST: u8 = 1;
+pub(crate) const SECOND: u8 = 2;
+pub(crate) const TAIL: u8 = 3;
+
+/// Versions in a chain before a keyframe.
+#[cfg(any(feature = "python", test))]
+pub(crate) const DEPTH: u8 = 32;
+
+/// What an array holds: its elements' type and how many lanes make one (coded lane by lane).
+#[derive(Clone, Copy, PartialEq, Eq, Debug)]
+#[repr(u8)]
+pub(crate) enum Kind {
+ MeshPoints, // float32 x, y, z (float64 in, rounded)
+ MeshUvs, // float32 u, v (float64 in, rounded)
+ MeshNormals, // float32 x, y, z (float64 in, rounded)
+ MeshTriangles, // u32
+ PathPoints, // float64 x, y, z: control points, four per curve
+ PathSubpaths, // u32 × 4
+ Points, // float32 × 4
+ Rows, // float32 × 4
+ Texture, // u8 × 4
+ Records, // u32 × 84: a frame's records (`take::RECORD` bytes each)
+}
+
+impl Kind {
+ #[cfg(any(feature = "player", test))]
+ pub(crate) fn from_u8(k: u8) -> Result<Kind, String> {
+ use Kind::*;
+ [MeshPoints, MeshUvs, MeshNormals, MeshTriangles, PathPoints, PathSubpaths, Points, Rows, Texture, Records].get(k as usize).copied().ok_or_else(|| format!("unknown array kind {k}"))
+ }
+
+ /// Bytes an element takes, as the take carries it.
+ pub(crate) fn width(self) -> usize {
+ match self {
+ Kind::PathPoints => 8,
+ Kind::Texture => 1,
+ _ => 4,
+ }
+ }
+
+ fn lanes(self) -> usize {
+ match self {
+ Kind::MeshPoints | Kind::MeshNormals | Kind::PathPoints => 3,
+ Kind::MeshUvs => 2,
+ Kind::PathSubpaths | Kind::Points | Kind::Rows | Kind::Texture => 4,
+ Kind::MeshTriangles => 1,
+ Kind::Records => crate::take::RECORD / 4,
+ }
+ }
+
+ fn float(self) -> bool {
+ !matches!(self, Kind::MeshTriangles | Kind::PathSubpaths | Kind::Texture | Kind::Records)
+ }
+
+ /// Whether it arrives as float64 and goes as float32.
+ #[cfg(any(feature = "python", test))]
+ pub(crate) fn rounded(self) -> bool {
+ matches!(self, Kind::MeshPoints | Kind::MeshUvs | Kind::MeshNormals)
+ }
+}
+
+/// The array as a take carries it: a mesh's float64s as float32s, anything else as it is.
+#[cfg(any(feature = "python", test))]
+pub(crate) fn canonical(kind: Kind, data: &[u8]) -> Vec<u8> {
+ if kind.rounded() {
+ let f: Vec<f64> = bytemuck::pod_collect_to_vec(data);
+ return bytemuck::cast_slice(&f.iter().map(|&x| x as f32).collect::<Vec<f32>>()).to_vec();
+ }
+ data.to_vec()
+}
+
+/// The key an array is sent under: of its kind and content (equal bytes of two kinds differ).
+#[cfg(any(feature = "python", test))]
+pub(crate) fn key(kind: Kind, data: &[u8]) -> u64 {
+ crate::digest([&[kind as u8][..], data])
+}
+
+/// An element as the unsigned integer the coding differences.
+trait Lane: Pod {
+ fn ordered(self, float: bool) -> Self;
+ #[cfg_attr(not(any(feature = "player", test)), allow(dead_code))]
+ fn unordered(self, float: bool) -> Self;
+ #[cfg_attr(not(any(feature = "python", test)), allow(dead_code))]
+ fn sub(self, other: Self) -> Self;
+ #[cfg_attr(not(any(feature = "player", test)), allow(dead_code))]
+ fn add(self, other: Self) -> Self;
+ /// `2 b − o`, as floats (None: not finite).
+ fn predict(b: Self, o: Self) -> Option<Self>;
+}
+
+macro_rules! lane {
+ ($u:ty, $f:ty) => {
+ impl Lane for $u {
+ fn ordered(self, float: bool) -> Self {
+ // floats as integers that order as they do: sign-magnitude to offset
+ const TOP: $u = 1 << (<$u>::BITS - 1);
+ if !float { self } else if self & TOP != 0 { !self } else { self | TOP }
+ }
+ fn unordered(self, float: bool) -> Self {
+ const TOP: $u = 1 << (<$u>::BITS - 1);
+ if !float { self } else if self & TOP != 0 { self ^ TOP } else { !self }
+ }
+ fn sub(self, other: Self) -> Self {
+ self.wrapping_sub(other)
+ }
+ fn add(self, other: Self) -> Self {
+ self.wrapping_add(other)
+ }
+ fn predict(b: Self, o: Self) -> Option<Self> {
+ let p = (2.0 * <$f>::from_bits(b) as f64 - <$f>::from_bits(o) as f64) as $f;
+ p.is_finite().then(|| p.to_bits())
+ }
+ }
+ };
+}
+lane!(u32, f32);
+lane!(u64, f64);
+
+impl Lane for u8 {
+ fn ordered(self, _: bool) -> Self {
+ self
+ }
+ fn unordered(self, _: bool) -> Self {
+ self
+ }
+ fn sub(self, other: Self) -> Self {
+ self.wrapping_sub(other)
+ }
+ fn add(self, other: Self) -> Self {
+ self.wrapping_add(other)
+ }
+ fn predict(_: Self, _: Self) -> Option<Self> {
+ None
+ }
+}
+
+/// Elements lane by lane (all x's, then all y's, …), then byte by byte: what zstd compresses.
+#[cfg(any(feature = "python", test))]
+fn planes<T: Pod>(values: &[T], lanes: usize) -> Vec<u8> {
+ let lanes = if lanes > 1 && values.len().is_multiple_of(lanes) { lanes } else { 1 };
+ let (n, w) = (values.len(), size_of::<T>());
+ let per = n / lanes.max(1);
+ let bytes: &[u8] = bytemuck::cast_slice(values);
+ let mut out = vec![0u8; bytes.len()];
+ for i in 0..n {
+ // element i is lane i % lanes of item i / lanes: its place lane-major
+ let at = (i % lanes) * per + i / lanes;
+ for b in 0..w {
+ out[b * n + at] = bytes[i * w + b];
+ }
+ }
+ out
+}
+
+#[cfg(any(feature = "player", test))]
+fn unplanes<T: Pod>(bytes: &[u8], lanes: usize) -> Vec<T> {
+ let w = size_of::<T>();
+ let n = bytes.len() / w;
+ let lanes = if lanes > 1 && n.is_multiple_of(lanes) { lanes } else { 1 };
+ let per = n / lanes.max(1);
+ let mut out = vec![0u8; bytes.len()];
+ for i in 0..n {
+ let at = (i % lanes) * per + i / lanes;
+ for b in 0..w {
+ out[i * w + b] = bytes[b * n + at];
+ }
+ }
+ bytemuck::pod_collect_to_vec(&out)
+}
+
+/// Code `data` (canonical bytes) against its base (and the base's base), if any: the smallest of
+/// the codings that apply. Returns (mode, compressed payload).
+#[cfg(any(feature = "python", test))]
+pub(crate) fn encode(kind: Kind, data: &[u8], base: Option<&[u8]>, older: Option<&[u8]>) -> (u8, Vec<u8>) {
+ match kind.width() {
+ 8 => encode_lanes::<u64>(kind, data, base, older),
+ 1 => encode_lanes::<u8>(kind, data, base, older),
+ _ => encode_lanes::<u32>(kind, data, base, older),
+ }
+}
+
+#[cfg(any(feature = "python", test))]
+fn encode_lanes<T: Lane>(kind: Kind, data: &[u8], base: Option<&[u8]>, older: Option<&[u8]>) -> (u8, Vec<u8>) {
+ let (float, lanes) = (kind.float(), kind.lanes());
+ let a: Vec<T> = bytemuck::pod_collect_to_vec(data);
+ let ord = |v: &[T]| v.iter().map(|&x| x.ordered(float)).collect::<Vec<T>>();
+ let mut choices: Vec<(u8, Vec<T>)> = vec![(ALONE, ord(&a))];
+ if let Some(base) = base {
+ let b: Vec<T> = bytemuck::pod_collect_to_vec(base);
+ if b.len() == a.len() {
+ let (oa, ob) = (ord(&a), ord(&b));
+ choices.push((FIRST, oa.iter().zip(&ob).map(|(&x, &y)| x.sub(y)).collect()));
+ if let Some(older) = older.filter(|o| float && o.len() == base.len()) {
+ let o: Vec<T> = bytemuck::pod_collect_to_vec(older);
+ let predicted: Option<Vec<T>> = b.iter().zip(&o).map(|(&b, &o)| T::predict(b, o)).collect();
+ if let Some(p) = predicted {
+ choices.push((SECOND, oa.iter().zip(ord(&p)).map(|(&x, y)| x.sub(y)).collect()));
+ }
+ }
+ } else if b.len() < a.len() && base == &data[..base.len()] {
+ choices.push((TAIL, ord(&a[b.len()..])));
+ }
+ }
+ choices
+ .into_iter()
+ .map(|(mode, values)| (mode, zstd::bulk::compress(&planes(&values, lanes), 3).expect("zstd compresses")))
+ .min_by_key(|(_, blob)| blob.len())
+ .expect("a coding")
+}
+
+/// The canonical bytes of an array coded as `mode` against its base (and the base's base).
+#[cfg(any(feature = "player", test))]
+pub(crate) fn decode(kind: Kind, mode: u8, blob: &[u8], base: Option<&[u8]>, older: Option<&[u8]>) -> Result<Vec<u8>, String> {
+ use std::io::Read;
+ let mut bytes = Vec::new();
+ ruzstd::decoding::StreamingDecoder::new(blob).map_err(|e| e.to_string())?.read_to_end(&mut bytes).map_err(|e| e.to_string())?;
+ match kind.width() {
+ 8 => decode_lanes::<u64>(kind, mode, &bytes, base, older),
+ 1 => decode_lanes::<u8>(kind, mode, &bytes, base, older),
+ _ => decode_lanes::<u32>(kind, mode, &bytes, base, older),
+ }
+}
+
+#[cfg(any(feature = "player", test))]
+fn decode_lanes<T: Lane>(kind: Kind, mode: u8, bytes: &[u8], base: Option<&[u8]>, older: Option<&[u8]>) -> Result<Vec<u8>, String> {
+ let (float, lanes) = (kind.float(), kind.lanes());
+ let values: Vec<T> = unplanes(bytes, lanes);
+ let base = || -> Result<Vec<T>, String> { base.map(bytemuck::pod_collect_to_vec).ok_or_else(|| "an array's base is missing".into()) };
+ let ord = |v: &[T]| v.iter().map(|&x| x.ordered(float)).collect::<Vec<T>>();
+ let out: Vec<T> = match mode {
+ ALONE => values.iter().map(|&x| x.unordered(float)).collect(),
+ FIRST => {
+ let b = base()?;
+ ord(&b).iter().zip(&values).map(|(&y, &r)| y.add(r).unordered(float)).collect()
+ }
+ SECOND => {
+ let (b, o): (Vec<T>, Vec<T>) = (base()?, older.map(bytemuck::pod_collect_to_vec).ok_or("an array's second base is missing")?);
+ let p: Vec<T> = b.iter().zip(&o).map(|(&b, &o)| T::predict(b, o)).collect::<Option<_>>().ok_or("a prediction is not finite")?;
+ ord(&p).iter().zip(&values).map(|(&y, &r)| y.add(r).unordered(float)).collect()
+ }
+ TAIL => {
+ let mut b = base()?;
+ b.extend(values.iter().map(|&x| x.unordered(float)));
+ b
+ }
+ m => return Err(format!("unknown array coding {m}")),
+ };
+ Ok(bytemuck::cast_slice(&out).to_vec())
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ fn floats(n: usize, t: f32) -> Vec<u8> {
+ let v: Vec<f32> = (0..n).map(|i| (i as f32 * 0.37 + t).sin() * 3.0 + t * 0.01).collect();
+ bytemuck::cast_slice(&v).to_vec()
+ }
+
+ /// Every coding decodes to the array it coded, exactly; and a slowly changing array codes
+ /// much smaller against its base than alone.
+ #[test]
+ fn codings_round_trip() {
+ let (older, base, now) = (floats(3000, 0.0), floats(3000, 0.01), floats(3000, 0.02));
+ let (mode, blob) = encode(Kind::MeshPoints, &now, Some(&base), Some(&older));
+ assert!(mode == FIRST || mode == SECOND);
+ assert_eq!(decode(Kind::MeshPoints, mode, &blob, Some(&base), Some(&older)).unwrap(), now);
+ let (alone, alone_blob) = encode(Kind::MeshPoints, &now, None, None);
+ assert_eq!(alone, ALONE);
+ assert!(blob.len() * 2 < alone_blob.len(), "{} against {}", blob.len(), alone_blob.len());
+ assert_eq!(decode(Kind::MeshPoints, ALONE, &alone_blob, None, None).unwrap(), now);
+ // a longer array whose start is its base: the tail
+ let grown: Vec<u8> = [&base[..], &floats(300, 5.0)[..]].concat();
+ let (mode, blob) = encode(Kind::PathSubpaths, &grown, Some(&base), None);
+ assert_eq!(mode, TAIL);
+ assert_eq!(decode(Kind::PathSubpaths, mode, &blob, Some(&base), None).unwrap(), grown);
+ // float64 control points, integers, bytes: each its own width
+ let f64s: Vec<u8> = bytemuck::cast_slice(&(0..999).map(|i| i as f64 * 1.5 - 7.0).collect::<Vec<f64>>()).to_vec();
+ for (kind, data) in [(Kind::PathPoints, f64s), (Kind::Texture, (0..4096).map(|i| (i * 7 % 256) as u8).collect()), (Kind::Records, vec![9u8; crate::take::RECORD * 5])] {
+ let (mode, blob) = encode(kind, &data, None, None);
+ assert_eq!(decode(kind, mode, &blob, None, None).unwrap(), data, "{kind:?}");
+ }
+ }
+
+ #[test]
+ fn floats_round_to_the_gpus_precision() {
+ let f64s: Vec<u8> = bytemuck::cast_slice(&[0.1f64, -2.5, 1e-9]).to_vec();
+ let c = canonical(Kind::MeshPoints, &f64s);
+ assert_eq!(bytemuck::cast_slice::<u8, f32>(&c), &[0.1f32, -2.5, 1e-9]);
+ assert_ne!(key(Kind::MeshPoints, &[]), key(Kind::MeshTriangles, &[]));
+ }
+}