#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ToneMapping {
Reinhard,
Aces,
#[default]
KhronosNeutral,
}
#[doc(hidden)]
pub const INTERIM_EXPOSURE_BOOST: f32 = 1.2;
#[doc(hidden)]
pub const METER_CALIBRATION_K: f32 = 12.5;
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ExposureMode {
Manual {
ev: f32,
},
PhysicalCamera {
aperture: f32,
shutter: f32,
iso: f32,
},
Automatic(AutoExposure),
}
impl ExposureMode {
pub const NEUTRAL: ExposureMode = ExposureMode::Manual { ev: 0.0 };
}
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct AutoExposure {
pub min_ev: f32,
pub max_ev: f32,
pub speed_up: f32,
pub speed_down: f32,
pub dt: f32,
pub low_percent: f32,
pub high_percent: f32,
pub center_weight: f32,
pub adaptation: f32,
}
impl Default for AutoExposure {
fn default() -> Self {
Self {
min_ev: -8.0,
max_ev: 18.0,
speed_up: 3.0,
speed_down: 1.0,
dt: 0.0,
low_percent: 0.65,
high_percent: 0.95,
center_weight: 0.85,
adaptation: 1.0,
}
}
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ExposureSettings {
pub mode: ExposureMode,
pub compensation: f32,
}
impl Default for ExposureSettings {
fn default() -> Self {
Self {
mode: ExposureMode::NEUTRAL,
compensation: 0.0,
}
}
}
impl ExposureSettings {
pub fn from_mode(mode: ExposureMode) -> Self {
Self {
mode,
compensation: 0.0,
}
}
pub fn with_compensation(mut self, stops: f32) -> Self {
self.compensation = stops;
self
}
pub fn manual(ev: f32) -> Self {
Self {
mode: ExposureMode::Manual { ev },
compensation: 0.0,
}
}
pub fn physical(aperture: f32, shutter: f32, iso: f32) -> Self {
Self {
mode: ExposureMode::PhysicalCamera {
aperture,
shutter,
iso,
},
compensation: 0.0,
}
}
pub fn automatic() -> Self {
Self {
mode: ExposureMode::Automatic(AutoExposure::default()),
compensation: 0.0,
}
}
#[doc(hidden)]
pub fn base_ev100(&self) -> Option<f32> {
match self.mode {
ExposureMode::Manual { ev } => Some(ev),
ExposureMode::PhysicalCamera {
aperture,
shutter,
iso,
} => Some(ev100_from_physical(aperture, shutter, iso)),
ExposureMode::Automatic(_) => None,
}
}
#[doc(hidden)]
pub fn manual_multiplier(&self) -> Option<f32> {
self.base_ev100()
.map(|ev| ev100_to_exposure(ev - self.compensation))
}
}
pub(crate) fn ev100_from_physical(aperture: f32, shutter: f32, iso: f32) -> f32 {
(aperture * aperture / shutter).log2() + (100.0 / iso).log2()
}
pub(crate) fn ev100_to_exposure(ev100: f32) -> f32 {
let max_lum = 1.2 * 2.0_f32.powf(ev100);
INTERIM_EXPOSURE_BOOST / max_lum
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum PipelineMode {
#[default]
Hdr,
Direct,
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct DisplaySettings {
pub mode: PipelineMode,
pub exposure: ExposureSettings,
pub operator: ToneMapping,
}
impl Default for DisplaySettings {
fn default() -> Self {
Self {
mode: PipelineMode::Hdr,
exposure: ExposureSettings::default(),
operator: ToneMapping::default(),
}
}
}
impl DisplaySettings {
pub fn is_hdr(&self) -> bool {
matches!(self.mode, PipelineMode::Hdr)
}
}
#[non_exhaustive]
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct PostProcessSettings {
pub ssao: bool,
pub bloom: BloomSettings,
pub fxaa: bool,
pub ssaa_factor: u32,
pub contact_shadows: ContactShadowSettings,
pub edl: EdlSettings,
pub dof: DofSettings,
}
impl Default for PostProcessSettings {
fn default() -> Self {
Self {
ssao: false,
bloom: BloomSettings::default(),
fxaa: false,
ssaa_factor: 1,
contact_shadows: ContactShadowSettings::default(),
edl: EdlSettings::default(),
dof: DofSettings::default(),
}
}
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BloomSettings {
pub enabled: bool,
pub threshold: f32,
pub intensity: f32,
pub max_brightness: f32,
}
impl Default for BloomSettings {
fn default() -> Self {
Self {
enabled: false,
threshold: 1.0,
intensity: 0.1,
max_brightness: 4.0,
}
}
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ContactShadowSettings {
pub enabled: bool,
pub max_distance: f32,
pub steps: u32,
pub thickness: f32,
}
impl Default for ContactShadowSettings {
fn default() -> Self {
Self {
enabled: false,
max_distance: 0.5,
steps: 16,
thickness: 0.1,
}
}
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct EdlSettings {
pub enabled: bool,
pub radius: f32,
pub strength: f32,
}
impl Default for EdlSettings {
fn default() -> Self {
Self {
enabled: false,
radius: 1.0,
strength: 1.0,
}
}
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct DofSettings {
pub enabled: bool,
pub focal_distance: f32,
pub focal_range: f32,
pub max_blur_radius: f32,
}
impl Default for DofSettings {
fn default() -> Self {
Self {
enabled: false,
focal_distance: 5.0,
focal_range: 1.0,
max_blur_radius: 8.0,
}
}
}
#[cfg(test)]
mod exposure_tests {
use super::*;
fn approx(a: f32, b: f32, eps: f32) -> bool {
(a - b).abs() <= eps
}
#[test]
fn physical_ev100_matches_hand_computed() {
assert!(approx(ev100_from_physical(1.0, 1.0, 100.0), 0.0, 1e-4));
assert!(approx(ev100_from_physical(2.0, 1.0, 100.0), 2.0, 1e-4));
assert!(approx(ev100_from_physical(2.0, 1.0, 200.0), 1.0, 1e-4));
assert!(approx(ev100_from_physical(1.0, 0.5, 100.0), 1.0, 1e-4));
}
#[test]
fn sunny_sixteen_is_about_ev15() {
let ev = ev100_from_physical(16.0, 1.0 / 125.0, 100.0);
assert!(approx(ev, 15.0, 0.1), "sunny-16 EV100 was {ev}");
}
#[test]
fn ev0_is_unit_multiplier() {
assert!(approx(ev100_to_exposure(0.0), 1.0, 1e-4));
}
#[test]
fn exposure_halves_per_stop_and_is_monotonic() {
let e0 = ev100_to_exposure(0.0);
let e1 = ev100_to_exposure(1.0);
let e2 = ev100_to_exposure(2.0);
assert!(approx(e1, e0 * 0.5, 1e-4));
assert!(approx(e2, e0 * 0.25, 1e-4));
assert!(e2 < e1 && e1 < e0);
}
#[test]
fn compensation_brightens() {
let base = ExposureSettings::manual(0.0);
let plus = ExposureSettings {
mode: ExposureMode::Manual { ev: 0.0 },
compensation: 1.0,
};
let b = base.manual_multiplier().unwrap();
let p = plus.manual_multiplier().unwrap();
assert!(approx(p, b * 2.0, 1e-4), "b={b} p={p}");
}
#[test]
fn automatic_has_no_manual_multiplier() {
let auto = ExposureSettings::automatic();
assert!(auto.manual_multiplier().is_none());
assert!(auto.base_ev100().is_none());
assert!(matches!(auto.mode, ExposureMode::Automatic(_)));
}
#[test]
fn physical_mode_multiplier_tracks_ev() {
let s = ExposureSettings::physical(2.0, 1.0, 100.0);
let expected = ev100_to_exposure(2.0);
assert!(approx(s.manual_multiplier().unwrap(), expected, 1e-4));
}
}