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

/// Sanitizes an HDR linear RGB triplet by clamping non-finite or negative values to `[0.0, POSTFX_MAX_HDR_RADIANCE]`.
///
/// # Arguments
///
/// - `[f32; 3]` - Raw HDR linear RGB pixel.
///
/// # Returns
///
/// - `[f32; 3]` - Strictly finite, non-negative clamped HDR RGB triplet.
#[must_use]
pub fn sanitize_hdr_rgb(rgb: [f32; 3]) -> [f32; 3] {
    let clean: fn(f32) -> f32 = |v: f32| -> f32 {
        if v.is_finite() {
            v.clamp(0.0, POSTFX_MAX_HDR_RADIANCE)
        } else {
            0.0
        }
    };
    [clean(rgb[0]), clean(rgb[1]), clean(rgb[2])]
}

/// Computes Rec.709 linear luminance `0.2126 R + 0.7152 G + 0.0722 B`.
///
/// # Arguments
///
/// - `[f32; 3]` - Linear RGB color triplet.
///
/// # Returns
///
/// - `f32` - Non-negative linear luminance scalar.
#[must_use]
pub fn postfx_rgb_luminance(rgb: [f32; 3]) -> f32 {
    (0.2126 * rgb[0] + 0.7152 * rgb[1] + 0.0722 * rgb[2]).max(0.0)
}

/// Applies luminance-preserving extended Reinhard HDR tone mapping.
///
/// # Arguments
///
/// - `Vec3` - Linear scene-referred HDR RGB radiance.
///
/// # Returns
///
/// - `Vec3` - Display-referred linear RGB color in `[0.0, 1.0]`.
#[must_use]
pub fn reinhard_tonemap(hdr: Vec3) -> Vec3 {
    let r: f32 = hdr.get_x().max(0.0);
    let g: f32 = hdr.get_y().max(0.0);
    let b: f32 = hdr.get_z().max(0.0);
    let lum: f32 = postfx_rgb_luminance([r, g, b]);
    if lum <= 1.0e-6 {
        return Vec3::zero();
    }
    let white_sq: f32 = 16.0;
    let mapped_lum: f32 = (lum * (1.0 + lum / white_sq)) / (1.0 + lum);
    let scale: f32 = mapped_lum / lum;
    Vec3::new(
        (r * scale).clamp(0.0, 1.0),
        (g * scale).clamp(0.0, 1.0),
        (b * scale).clamp(0.0, 1.0),
    )
}

/// Applies the selected `ToneMapOperator3D` (`AgX`, `AcesFilmic`, or `Reinhard`) to linear HDR RGB.
///
/// # Arguments
///
/// - `Vec3` - Linear scene-referred HDR RGB radiance.
/// - `ToneMapOperator3D` - Selected tone-mapping curve.
///
/// # Returns
///
/// - `Vec3` - Finite display-referred linear RGB color in `[0.0, 1.0]`.
#[must_use]
pub fn apply_tone_map(hdr: Vec3, operator: ToneMapOperator3D) -> Vec3 {
    let safe: [f32; 3] = sanitize_hdr_rgb([hdr.get_x(), hdr.get_y(), hdr.get_z()]);
    let safe_v: Vec3 = Vec3::from_array(safe);
    let mapped: Vec3 = match operator {
        ToneMapOperator3D::AgX => agx_tonemap(safe_v),
        ToneMapOperator3D::AcesFilmic => aces_filmic_tonemap(safe_v),
        ToneMapOperator3D::Reinhard => reinhard_tonemap(safe_v),
    };
    Vec3::new(
        if mapped.get_x().is_finite() {
            mapped.get_x().clamp(0.0, 1.0)
        } else {
            0.0
        },
        if mapped.get_y().is_finite() {
            mapped.get_y().clamp(0.0, 1.0)
        } else {
            0.0
        },
        if mapped.get_z().is_finite() {
            mapped.get_z().clamp(0.0, 1.0)
        } else {
            0.0
        },
    )
}

/// Converts a linear display channel in `[0.0, 1.0]` to an IEC 61966-2-1 sRGB `[0.0, 1.0]` value.
///
/// # Arguments
///
/// - `f32` - Linear display channel in `[0.0, 1.0]`.
///
/// # Returns
///
/// - `f32` - sRGB gamma-encoded value in `[0.0, 1.0]`.
#[must_use]
pub fn linear_to_srgb_f32(linear_ch: f32) -> f32 {
    let c: f32 = if linear_ch.is_finite() {
        linear_ch.clamp(0.0, 1.0)
    } else {
        0.0
    };
    if c <= 0.003_130_8 {
        c * 12.92
    } else {
        (1.055 * c.powf(1.0 / 2.4) - 0.055).clamp(0.0, 1.0)
    }
}

/// Converts an sRGB `[0.0, 1.0]` scalar to a quantized `u8` byte in `0..=255`.
///
/// # Arguments
///
/// - `f32` - sRGB scalar in `[0.0, 1.0]`.
///
/// # Returns
///
/// - `u8` - Quantized sRGB channel byte.
#[must_use]
pub fn srgb_f32_to_u8(srgb_ch: f32) -> u8 {
    let clamped: f32 = if srgb_ch.is_finite() {
        srgb_ch.clamp(0.0, 1.0)
    } else {
        0.0
    };
    (clamped * 255.0 + 0.5).floor().clamp(0.0, 255.0) as u8
}

/// Applies Jimenez soft-knee quadratic threshold prefiltering to extract HDR bloom highlights.
///
/// # Arguments
///
/// - `[f32; 3]` - Sanitized linear HDR RGB pixel.
/// - `f32` - Luminance threshold.
/// - `f32` - Soft-knee width factor in `[0.0, 1.0]`.
///
/// # Returns
///
/// - `[f32; 3]` - Thresholded bloom contribution with total energy strictly `<= rgb`.
#[must_use]
pub fn soft_knee_bloom_prefilter(rgb: [f32; 3], threshold: f32, soft_knee: f32) -> [f32; 3] {
    let brightness: f32 = rgb[0].max(rgb[1]).max(rgb[2]);
    if brightness <= 1.0e-6 {
        return [0.0, 0.0, 0.0];
    }
    let thresh: f32 = threshold.max(0.0);
    let knee: f32 = thresh * soft_knee.clamp(0.0, 1.0) + 1.0e-5;
    let soft: f32 = (brightness - thresh + knee).clamp(0.0, 2.0 * knee);
    let soft_term: f32 = (soft * soft) / (4.0 * knee);
    let contribution: f32 = soft_term.max(brightness - thresh).clamp(0.0, brightness);
    let scale: f32 = (contribution / brightness).clamp(0.0, 1.0);
    [rgb[0] * scale, rgb[1] * scale, rgb[2] * scale]
}

/// Applies linear HDR color grading (temperature/tint, contrast, saturation, lift/gamma/gain) prior to tone mapping.
///
/// # Arguments
///
/// - `Vec3` - Exposed linear HDR RGB color.
/// - `&ColorGradingConfig3D` - Color grading parameters.
///
/// # Returns
///
/// - `Vec3` - Graded finite non-negative linear HDR RGB color.
#[must_use]
pub fn apply_color_grading(hdr: Vec3, grading: &ColorGradingConfig3D) -> Vec3 {
    let temp: f32 = grading.get_temperature().clamp(-1.0, 1.0);
    let tint: f32 = grading.get_tint().clamp(-1.0, 1.0);
    let wb_r: f32 = (1.0 + 0.18 * temp + 0.08 * tint).clamp(0.5, 1.6);
    let wb_g: f32 = (1.0 - 0.12 * tint).clamp(0.5, 1.6);
    let wb_b: f32 = (1.0 - 0.18 * temp + 0.08 * tint).clamp(0.5, 1.6);
    let mut r: f32 = (hdr.get_x() * wb_r).max(0.0);
    let mut g: f32 = (hdr.get_y() * wb_g).max(0.0);
    let mut b: f32 = (hdr.get_z() * wb_b).max(0.0);
    let contrast: f32 = grading.get_contrast().clamp(0.2, 3.0);
    let pivot: f32 = 0.18;
    r = ((r - pivot) * contrast + pivot).max(0.0);
    g = ((g - pivot) * contrast + pivot).max(0.0);
    b = ((b - pivot) * contrast + pivot).max(0.0);
    let sat: f32 = grading.get_saturation().clamp(0.0, 3.0);
    let lum: f32 = postfx_rgb_luminance([r, g, b]);
    r = (lum + (r - lum) * sat).max(0.0);
    g = (lum + (g - lum) * sat).max(0.0);
    b = (lum + (b - lum) * sat).max(0.0);
    let lift: Vec3 = grading.get_lift();
    let gain: Vec3 = grading.get_gain();
    let gamma: Vec3 = grading.get_gamma();
    r = ((r + lift.get_x()) * gain.get_x().max(0.0)).max(0.0);
    g = ((g + lift.get_y()) * gain.get_y().max(0.0)).max(0.0);
    b = ((b + lift.get_z()) * gain.get_z().max(0.0)).max(0.0);
    let gx: f32 = 1.0 / gamma.get_x().clamp(0.2, 4.0);
    let gy: f32 = 1.0 / gamma.get_y().clamp(0.2, 4.0);
    let gz: f32 = 1.0 / gamma.get_z().clamp(0.2, 4.0);
    Vec3::new(
        if r > 0.0 { r.powf(gx) } else { 0.0 },
        if g > 0.0 { g.powf(gy) } else { 0.0 },
        if b > 0.0 { b.powf(gz) } else { 0.0 },
    )
}