euv-core 0.53.1

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::*;

/// Converts an 8-bit sRGB channel `[0..255]` into linear light `[0.0..1.0]`.
///
/// # Arguments
///
/// - `u8` - sRGB channel byte.
///
/// # Returns
///
/// - `f32` - Linear-light scalar in `[0.0, 1.0]`.
#[cfg_attr(not(target_arch = "wasm32"), inline)]
#[must_use]
pub fn srgb_u8_to_linear(channel: u8) -> f32 {
    let norm: f32 = f32::from(channel) / 255.0;
    if norm <= 0.040_45 {
        norm / 12.92
    } else {
        ((norm + 0.055) / 1.055).powf(2.4)
    }
}

/// Converts a linear-light scalar `[0.0..1.0]` into an 8-bit sRGB channel `[0..255]`.
///
/// # Arguments
///
/// - `f32` - Linear-light channel value.
///
/// # Returns
///
/// - `u8` - sRGB-encoded byte value.
#[cfg_attr(not(target_arch = "wasm32"), inline)]
#[must_use]
pub fn linear_to_srgb_u8(linear: f32) -> u8 {
    let clamped: f32 = linear.clamp(0.0, 1.0);
    let encoded: f32 = if clamped <= 0.003_130_8 {
        clamped * 12.92
    } else {
        1.055 * clamped.powf(1.0 / 2.4) - 0.055
    };
    (encoded * 255.0).round().clamp(0.0, 255.0) as u8
}

/// Applies `wrap_mode` to a normalized texture coordinate `coord` producing `[0.0, 1.0]`.
///
/// # Arguments
///
/// - `f32` - Unwrapped texture coordinate.
/// - `TextureWrapMode` - Wrapping policy.
///
/// # Returns
///
/// - `f32` - Wrapped coordinate in `[0.0, 1.0]`.
#[cfg_attr(not(target_arch = "wasm32"), inline)]
#[must_use]
pub fn wrap_texture_coord(coord: f32, wrap_mode: TextureWrapMode) -> f32 {
    match wrap_mode {
        TextureWrapMode::ClampToEdge => coord.clamp(0.0, 1.0),
        TextureWrapMode::Repeat => coord - coord.floor(),
        TextureWrapMode::MirroredRepeat => {
            let tile: f32 = coord.floor();
            let frac: f32 = coord - tile;
            let odd: bool = (tile as i64).rem_euclid(2) == 1;
            if odd { 1.0 - frac } else { frac }
        }
    }
}

/// Downsamples a single mip level by 2x respecting `color_space` (linear sRGB box filter or re-normalized normals).
///
/// # Arguments
///
/// - `&TextureMipLevel3D` - Source higher-resolution mip level.
/// - `TextureColorSpace` - Color-space filtering policy.
///
/// # Returns
///
/// - `TextureMipLevel3D` - Half-resolution filtered child mip level.
#[must_use]
pub fn downsample_mip_level(
    src: &TextureMipLevel3D,
    color_space: TextureColorSpace,
) -> TextureMipLevel3D {
    let src_w: usize = src.get_width().max(1) as usize;
    let src_h: usize = src.get_height().max(1) as usize;
    let dst_w: usize = (src_w / 2).max(1);
    let dst_h: usize = (src_h / 2).max(1);
    let pixels: &[u8] = src.get_pixels().as_slice();
    let mut out: Vec<u8> = vec![0u8; dst_w * dst_h * 4];
    for dy in 0..dst_h {
        let sy0: usize = (dy * 2).min(src_h - 1);
        let sy1: usize = (dy * 2 + 1).min(src_h - 1);
        for dx in 0..dst_w {
            let sx0: usize = (dx * 2).min(src_w - 1);
            let sx1: usize = (dx * 2 + 1).min(src_w - 1);
            let coords: [(usize, usize); 4] = [(sx0, sy0), (sx1, sy0), (sx0, sy1), (sx1, sy1)];
            let dst_off: usize = (dy * dst_w + dx) * 4;
            match color_space {
                TextureColorSpace::Srgb => {
                    let mut sum_r: f32 = 0.0;
                    let mut sum_g: f32 = 0.0;
                    let mut sum_b: f32 = 0.0;
                    let mut sum_a: f32 = 0.0;
                    for &(cx, cy) in coords.iter() {
                        let off: usize = (cy * src_w + cx) * 4;
                        sum_r += srgb_u8_to_linear(pixels[off]);
                        sum_g += srgb_u8_to_linear(pixels[off + 1]);
                        sum_b += srgb_u8_to_linear(pixels[off + 2]);
                        sum_a += f32::from(pixels[off + 3]) / 255.0;
                    }
                    out[dst_off] = linear_to_srgb_u8(sum_r * 0.25);
                    out[dst_off + 1] = linear_to_srgb_u8(sum_g * 0.25);
                    out[dst_off + 2] = linear_to_srgb_u8(sum_b * 0.25);
                    out[dst_off + 3] = (sum_a * 0.25 * 255.0).round().clamp(0.0, 255.0) as u8;
                }
                TextureColorSpace::Linear => {
                    let mut sums: [u32; 4] = [0u32; 4];
                    for &(cx, cy) in coords.iter() {
                        let off: usize = (cy * src_w + cx) * 4;
                        sums[0] += u32::from(pixels[off]);
                        sums[1] += u32::from(pixels[off + 1]);
                        sums[2] += u32::from(pixels[off + 2]);
                        sums[3] += u32::from(pixels[off + 3]);
                    }
                    out[dst_off] = ((sums[0] + 2) >> 2) as u8;
                    out[dst_off + 1] = ((sums[1] + 2) >> 2) as u8;
                    out[dst_off + 2] = ((sums[2] + 2) >> 2) as u8;
                    out[dst_off + 3] = ((sums[3] + 2) >> 2) as u8;
                }
                TextureColorSpace::NormalTangent => {
                    let mut nx: f32 = 0.0;
                    let mut ny: f32 = 0.0;
                    let mut nz: f32 = 0.0;
                    for &(cx, cy) in coords.iter() {
                        let off: usize = (cy * src_w + cx) * 4;
                        nx += f32::from(pixels[off]) / 127.5 - 1.0;
                        ny += f32::from(pixels[off + 1]) / 127.5 - 1.0;
                        nz += f32::from(pixels[off + 2]) / 127.5 - 1.0;
                    }
                    let unit: Vec3 = Vec3::new(nx, ny, nz).normalize();
                    out[dst_off] = ((unit.get_x() * 0.5 + 0.5) * 255.0)
                        .round()
                        .clamp(0.0, 255.0) as u8;
                    out[dst_off + 1] = ((unit.get_y() * 0.5 + 0.5) * 255.0)
                        .round()
                        .clamp(0.0, 255.0) as u8;
                    out[dst_off + 2] = ((unit.get_z() * 0.5 + 0.5) * 255.0)
                        .round()
                        .clamp(0.0, 255.0) as u8;
                    out[dst_off + 3] = 255;
                }
            }
        }
    }
    TextureMipLevel3D {
        width: dst_w as u32,
        height: dst_h as u32,
        pixels: out,
    }
}