euv-core 0.56.0

Core cross-platform runtime for euv featuring arena-based fine-grained reactivity, deterministic layout, flat scene graph, multi-mode rendering, and direct OS platform adapters.
Documentation
use super::*;

/// Advances a SplitMix64 mixing step used to expand a 64-bit seed into Xoshiro256** state.
///
/// # Arguments
///
/// - `&mut u64` - Mutable SplitMix64 state updated in place.
///
/// # Returns
///
/// - `u64` - High-entropy mixed 64-bit output.
pub(crate) fn splitmix64_next(state: &mut u64) -> u64 {
    *state = state.wrapping_add(0x9E37_79B9_7F4A_7C15);
    let mut z: u64 = *state;
    z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
    z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
    z ^ (z >> 31)
}

/// Quintic Perlin fade curve `6t^5 - 15t^4 + 10t^3`.
///
/// # Arguments
///
/// - `f32` - Fractional coordinate in `[0.0, 1.0]`.
///
/// # Returns
///
/// - `f32` - Smooth C2-continuous interpolation weight.
pub(crate) fn noise_fade(t: f32) -> f32 {
    t * t * t * (t * (t * 6.0 - 15.0) + 10.0)
}

/// Linearly interpolates between `a` and `b` by factor `t`.
///
/// # Arguments
///
/// - `f32` - Start scalar.
/// - `f32` - End scalar.
/// - `f32` - Blend factor.
///
/// # Returns
///
/// - `f32` - Interpolated scalar.
pub(crate) fn noise_lerp(a: f32, b: f32, t: f32) -> f32 {
    a + t * (b - a)
}

/// Evaluates 2D Perlin gradient dot product from hash value `hash` and offset `(x, y)`.
///
/// # Arguments
///
/// - `u8` - Permutation hash byte.
/// - `f32` - Relative X offset.
/// - `f32` - Relative Y offset.
///
/// # Returns
///
/// - `f32` - Gradient dot product.
pub(crate) fn grad_2d(hash: u8, x: f32, y: f32) -> f32 {
    match hash & 7 {
        0 => x + y,
        1 => -x + y,
        2 => x - y,
        3 => -x - y,
        4 => x,
        5 => -x,
        6 => y,
        _ => -y,
    }
}

/// Evaluates 3D Perlin gradient dot product from hash value `hash` and offset `(x, y, z)`.
///
/// # Arguments
///
/// - `u8` - Permutation hash byte.
/// - `f32` - Relative X offset.
/// - `f32` - Relative Y offset.
/// - `f32` - Relative Z offset.
///
/// # Returns
///
/// - `f32` - Gradient dot product.
pub(crate) fn grad_3d(hash: u8, x: f32, y: f32, z: f32) -> f32 {
    let h: u8 = hash & 15;
    let u: f32 = if h < 8 { x } else { y };
    let v: f32 = if h < 4 {
        y
    } else if h == 12 || h == 14 {
        x
    } else {
        z
    };
    let first: f32 = if (h & 1) == 0 { u } else { -u };
    let second: f32 = if (h & 2) == 0 { v } else { -v };
    first + second
}

/// Deterministically hashes a 2D integer cell plus seed into a uniform feature point in `[0, 1)^2`.
///
/// # Arguments
///
/// - `i32` - Cell X index.
/// - `i32` - Cell Y index.
/// - `u64` - Noise seed.
///
/// # Returns
///
/// - `(f32, f32)` - Feature point offset within `[0.0, 1.0)` along both axes.
pub(crate) fn worley_hash_2d(cx: i32, cy: i32, seed: u64) -> (f32, f32) {
    let mut s: u64 = seed
        ^ (cx as i64 as u64).wrapping_mul(0x9E37_79B9_7F4A_7C15)
        ^ (cy as i64 as u64).wrapping_mul(0xBF58_476D_1CE4_E5B9);
    let h1: u64 = splitmix64_next(&mut s);
    let h2: u64 = splitmix64_next(&mut s);
    let fx: f32 = ((h1 >> 40) as f32) * (1.0 / 16_777_216.0);
    let fy: f32 = ((h2 >> 40) as f32) * (1.0 / 16_777_216.0);
    (fx, fy)
}

/// Deterministically hashes a 3D integer cell plus seed into a uniform feature point in `[0, 1)^3`.
///
/// # Arguments
///
/// - `i32` - Cell X index.
/// - `i32` - Cell Y index.
/// - `i32` - Cell Z index.
/// - `u64` - Noise seed.
///
/// # Returns
///
/// - `(f32, f32, f32)` - Feature point offset within `[0.0, 1.0)` along all three axes.
pub(crate) fn worley_hash_3d(cx: i32, cy: i32, cz: i32, seed: u64) -> (f32, f32, f32) {
    let mut s: u64 = seed
        ^ (cx as i64 as u64).wrapping_mul(0x9E37_79B9_7F4A_7C15)
        ^ (cy as i64 as u64).wrapping_mul(0xBF58_476D_1CE4_E5B9)
        ^ (cz as i64 as u64).wrapping_mul(0x94D0_49BB_1331_11EB);
    let h1: u64 = splitmix64_next(&mut s);
    let h2: u64 = splitmix64_next(&mut s);
    let h3: u64 = splitmix64_next(&mut s);
    let inv: f32 = 1.0 / 16_777_216.0;
    (
        ((h1 >> 40) as f32) * inv,
        ((h2 >> 40) as f32) * inv,
        ((h3 >> 40) as f32) * inv,
    )
}

/// Computes 2D metric distance between a relative vector `(dx, dy)` using `metric`.
///
/// # Arguments
///
/// - `f32` - Delta X.
/// - `f32` - Delta Y.
/// - `WorleyMetric` - Metric selector.
///
/// # Returns
///
/// - `f32` - Non-negative metric distance.
pub(crate) fn worley_dist_2d(dx: f32, dy: f32, metric: WorleyMetric) -> f32 {
    match metric {
        WorleyMetric::Euclidean => (dx * dx + dy * dy).sqrt(),
        WorleyMetric::Manhattan => dx.abs() + dy.abs(),
        WorleyMetric::Chebyshev => dx.abs().max(dy.abs()),
    }
}

/// Computes 3D metric distance for `(dx, dy, dz)` using `metric`.
///
/// # Arguments
///
/// - `f32` - Delta X.
/// - `f32` - Delta Y.
/// - `f32` - Delta Z.
/// - `WorleyMetric` - Metric selector.
///
/// # Returns
///
/// - `f32` - Non-negative metric distance.
pub(crate) fn worley_dist_3d(dx: f32, dy: f32, dz: f32, metric: WorleyMetric) -> f32 {
    match metric {
        WorleyMetric::Euclidean => (dx * dx + dy * dy + dz * dz).sqrt(),
        WorleyMetric::Manhattan => dx.abs() + dy.abs() + dz.abs(),
        WorleyMetric::Chebyshev => dx.abs().max(dy.abs()).max(dz.abs()),
    }
}