#[non_exhaustive]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ToneMapping {
Reinhard,
Aces,
#[default]
KhronosNeutral,
Agx,
AcesRrt,
Generic,
Custom {
lut: crate::ids::TextureId,
},
}
impl ToneMapping {
#[doc(hidden)]
pub const fn composite_index(self) -> u32 {
match self {
ToneMapping::Reinhard => 0,
ToneMapping::Aces => 1,
ToneMapping::KhronosNeutral => 2,
ToneMapping::Agx => 3,
ToneMapping::AcesRrt => 4,
ToneMapping::Generic => 5,
ToneMapping::Custom { .. } => 6,
}
}
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum AgxLook {
#[default]
None,
Punchy,
Golden,
}
impl AgxLook {
pub fn cdl(self) -> ([f32; 3], [f32; 3], f32) {
match self {
AgxLook::None => ([1.0; 3], [1.0; 3], 1.0),
AgxLook::Punchy => ([1.0; 3], [1.35; 3], 1.4),
AgxLook::Golden => ([1.0, 0.9, 0.5], [0.8; 3], 1.3),
}
}
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct GenericToneCurve {
pub contrast: f32,
pub mid_grey_in: f32,
pub mid_grey_out: f32,
pub hdr_max: f32,
}
impl Default for GenericToneCurve {
fn default() -> Self {
Self {
contrast: 1.55,
mid_grey_in: 0.18,
mid_grey_out: 0.215,
hdr_max: 10.0,
}
}
}
impl GenericToneCurve {
pub fn new(contrast: f32, mid_grey_in: f32, mid_grey_out: f32, hdr_max: f32) -> Self {
Self {
contrast,
mid_grey_in,
mid_grey_out,
hdr_max,
}
}
pub fn scales(&self) -> (f32, f32, f32) {
let c = self.contrast.max(1e-5);
let mid_in = self.mid_grey_in.clamp(1e-5, 1.0);
let mid_out = self.mid_grey_out.clamp(1e-5, 1.0);
let hdr_max = self.hdr_max.max(1.0);
let a = mid_in.powf(c);
let b = hdr_max.powf(c);
let k = a - mid_out * b;
(c, a * b * (mid_out - 1.0) / k, mid_out * (a - b) / k)
}
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum WorkingSpace {
#[default]
Rec709,
Rec2020,
}
pub const PASSTHROUGH_EV100: f32 = -0.263_034_4;
#[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 {
Passthrough,
Manual {
ev: f32,
},
PhysicalCamera {
aperture: f32,
shutter: f32,
iso: f32,
},
Automatic(AutoExposure),
}
impl ExposureMode {
pub const NEUTRAL: ExposureMode = ExposureMode::Passthrough;
}
#[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,
#[cfg_attr(feature = "serde", serde(alias = "center_weight"))]
pub centre_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,
centre_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 passthrough() -> Self {
Self {
mode: ExposureMode::Passthrough,
compensation: 0.0,
}
}
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::Passthrough => Some(PASSTHROUGH_EV100),
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 {
1.0 / (1.2 * 2.0_f32.powf(ev100))
}
#[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,
pub vignette: VignetteSettings,
pub grade_lut: Option<crate::ids::TextureId>,
#[cfg_attr(feature = "serde", serde(default))]
pub grading_space: GradingSpace,
#[cfg_attr(feature = "serde", serde(default))]
pub agx_look: AgxLook,
#[cfg_attr(feature = "serde", serde(default))]
pub generic_tone_curve: GenericToneCurve,
#[cfg_attr(feature = "serde", serde(default))]
pub working_space: WorkingSpace,
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum GradingSpace {
#[default]
Scene,
Display,
}
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(),
vignette: VignetteSettings::default(),
grade_lut: None,
grading_space: GradingSpace::Scene,
agx_look: AgxLook::None,
generic_tone_curve: GenericToneCurve::default(),
working_space: WorkingSpace::Rec709,
}
}
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct VignetteSettings {
pub enabled: bool,
pub amount: f32,
pub radius: f32,
pub softness: f32,
}
impl Default for VignetteSettings {
fn default() -> Self {
Self {
enabled: false,
amount: 0.5,
radius: 0.6,
softness: 0.45,
}
}
}
#[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_the_saturation_based_multiplier() {
assert!(approx(ev100_to_exposure(0.0), 1.0 / 1.2, 1e-4));
}
#[test]
fn passthrough_is_unit_multiplier() {
assert!(approx(ev100_to_exposure(PASSTHROUGH_EV100), 1.0, 1e-5));
let s = ExposureSettings::default();
assert!(matches!(s.mode, ExposureMode::Passthrough));
assert!(approx(s.manual_multiplier().unwrap(), 1.0, 1e-5));
let plus = ExposureSettings::passthrough().with_compensation(1.0);
assert!(approx(plus.manual_multiplier().unwrap(), 2.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));
}
}