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

/// Encodes a linear display channel in `[0.0, 1.0]` to an 8-bit IEC 61966-2-1 sRGB byte.
///
/// # Arguments
///
/// - `f32` - Linear display color channel.
///
/// # Returns
///
/// - `u8` - Quantized sRGB byte in `0..=255`.
#[must_use]
pub fn linear_to_srgb_u8(linear: f32) -> u8 {
    let clamped: f32 = if linear.is_finite() {
        linear.clamp(0.0, 1.0)
    } else {
        0.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.clamp(0.0, 1.0) * 255.0 + 0.5) as u8
}

/// Applies exposure scaling, display tone mapping (`AgX`, `ACES`, or `Linear`), and sRGB encoding to a single HDR pixel.
///
/// # Arguments
///
/// - `Vec3` - Linear scene-referred HDR RGB radiance.
/// - `RtTonemapMode` - Selected tone-mapping operator.
/// - `f32` - Positive linear exposure multiplier.
///
/// # Returns
///
/// - `[u8; 4]` - Encoded opaque sRGB `[R, G, B, 255]` pixel.
#[must_use]
pub fn tonemap_pixel(hdr: Vec3, mode: RtTonemapMode, exposure: f32) -> [u8; 4] {
    let exp_scale: f32 = exposure.max(0.0);
    let exposed: Vec3 = Vec3::new(
        if hdr.get_x().is_finite() {
            (hdr.get_x() * exp_scale).max(0.0)
        } else {
            0.0
        },
        if hdr.get_y().is_finite() {
            (hdr.get_y() * exp_scale).max(0.0)
        } else {
            0.0
        },
        if hdr.get_z().is_finite() {
            (hdr.get_z() * exp_scale).max(0.0)
        } else {
            0.0
        },
    );
    let mapped: Vec3 = match mode {
        RtTonemapMode::Agx => agx_tonemap(exposed),
        RtTonemapMode::Aces => aces_filmic_tonemap(exposed),
        RtTonemapMode::Linear => Vec3::new(
            exposed.get_x().clamp(0.0, 1.0),
            exposed.get_y().clamp(0.0, 1.0),
            exposed.get_z().clamp(0.0, 1.0),
        ),
    };
    [
        linear_to_srgb_u8(mapped.get_x()),
        linear_to_srgb_u8(mapped.get_y()),
        linear_to_srgb_u8(mapped.get_z()),
        255,
    ]
}

/// Tone-maps an entire linear HDR RGB buffer into a pre-allocated sRGB RGBA8 slice with zero heap allocation.
///
/// # Arguments
///
/// - `&[Vec3]` - Input linear HDR pixels.
/// - `RtTonemapMode` - Tone-mapping operator.
/// - `f32` - Linear exposure multiplier.
/// - `&mut [u8]` - Destination RGBA8 byte slice (`len >= hdr.len() * 4`).
pub fn tonemap_hdr_to_rgba8(
    hdr: &[Vec3],
    mode: RtTonemapMode,
    exposure: f32,
    out_rgba8: &mut [u8],
) {
    let count: usize = hdr.len().min(out_rgba8.len() / 4);
    for (idx, &px) in hdr.iter().take(count).enumerate() {
        let rgba: [u8; 4] = tonemap_pixel(px, mode, exposure);
        let base: usize = idx * 4;
        out_rgba8[base] = rgba[0];
        out_rgba8[base + 1] = rgba[1];
        out_rgba8[base + 2] = rgba[2];
        out_rgba8[base + 3] = rgba[3];
    }
}

/// Dispatches a primary ray to the integrator selected by `config`.
///
/// # Arguments
///
/// - `&RaytraceConfig` - Active session configuration.
/// - `&RaytraceScene` - Scene to trace.
/// - `&Ray3D` - Primary camera ray.
/// - `&mut RtRng` - Deterministic per-pixel RNG.
/// - `&mut RtRayCounter` - Step ray telemetry counter.
///
/// # Returns
///
/// - `RtSampleOutput` - Traced radiance and primary G-buffer record.
#[must_use]
pub(crate) fn dispatch_integrator_ray(
    config: &RaytraceConfig,
    scene: &RaytraceScene,
    ray: &Ray3D,
    rng: &mut RtRng,
    counter: &mut RtRayCounter,
) -> RtSampleOutput {
    match config.get_integrator() {
        RtIntegratorMode::Whitted => config.get_whitted().trace_ray(scene, ray, rng, counter),
        RtIntegratorMode::PathTrace => config.get_path_trace().trace_ray(scene, ray, rng, counter),
        RtIntegratorMode::Rtao => config.get_rtao().trace_ray(scene, ray, rng, counter),
        RtIntegratorMode::HybridPreview => config
            .get_hybrid_preview()
            .trace_ray(scene, ray, rng, counter),
    }
}

/// Captures a cross-platform `RtTimer` start timestamp for wall-clock step telemetry.
///
/// # Returns
///
/// - `RtTimer` - Started timer token.
#[must_use]
pub(crate) fn rt_now() -> RtTimer {
    #[cfg(not(target_arch = "wasm32"))]
    {
        RtTimer {
            start: Instant::now(),
        }
    }
    #[cfg(target_arch = "wasm32")]
    {
        RtTimer { start: 0 }
    }
}

/// Computes elapsed wall-clock milliseconds since `timer` was created (`0.0` on `wasm32`).
///
/// # Arguments
///
/// - `RtTimer` - Previously captured timer token.
///
/// # Returns
///
/// - `f32` - Elapsed duration in milliseconds.
#[must_use]
pub(crate) fn rt_elapsed_ms(timer: RtTimer) -> f32 {
    #[cfg(not(target_arch = "wasm32"))]
    {
        let RtTimer { start } = timer;
        start.elapsed().as_secs_f32() * 1000.0
    }
    #[cfg(target_arch = "wasm32")]
    {
        let RtTimer { start } = timer;
        start as f32
    }
}