use super::{PREVIEW_LIGHT_PITCH, PREVIEW_LIGHT_YAW, PREVIEW_LIGHTING_PRESET};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CacheKind {
Preview {
size: u32,
spp: u32,
max_bounces: u32,
},
SolidDraft {
size: u32,
},
TiltCurves,
}
#[must_use]
pub fn cache_fingerprint(kind: CacheKind, material_name: Option<&str>) -> String {
let tracer = indicatrix::BUILD_ID;
let material = material_name.unwrap_or("none");
let light = format!("{PREVIEW_LIGHT_YAW}/{PREVIEW_LIGHT_PITCH}");
match kind {
CacheKind::Preview {
size,
spp,
max_bounces,
} => format!(
"preview;tracer={tracer};size={size};spp={spp};bounces={max_bounces};\
lighting={};light={light};material={material}",
PREVIEW_LIGHTING_PRESET.label()
),
CacheKind::SolidDraft { size } => format!("solid-draft;size={size}"),
CacheKind::TiltCurves => format!("tilt;tracer={tracer};light={light};material={material}"),
}
}
#[cfg(test)]
mod tests {
use super::*;
const PREVIEW: CacheKind = CacheKind::Preview {
size: 160,
spp: 256,
max_bounces: 12,
};
#[test]
fn cache_fingerprint_is_stable_and_names_the_tracer_build() {
let fingerprint = cache_fingerprint(PREVIEW, Some("Diamond"));
assert_eq!(fingerprint, cache_fingerprint(PREVIEW, Some("Diamond")));
assert!(fingerprint.contains(indicatrix::BUILD_ID), "{fingerprint}");
assert!(
cache_fingerprint(CacheKind::TiltCurves, Some("Diamond"))
.contains(indicatrix::BUILD_ID)
);
}
#[test]
fn cache_fingerprint_changes_with_every_parameter_it_tracks() {
let base = cache_fingerprint(PREVIEW, Some("Diamond"));
let other_size = CacheKind::Preview {
size: 128,
spp: 256,
max_bounces: 12,
};
let other_spp = CacheKind::Preview {
size: 160,
spp: 64,
max_bounces: 12,
};
let other_bounces = CacheKind::Preview {
size: 160,
spp: 256,
max_bounces: 8,
};
for changed in [
cache_fingerprint(other_size, Some("Diamond")),
cache_fingerprint(other_spp, Some("Diamond")),
cache_fingerprint(other_bounces, Some("Diamond")),
cache_fingerprint(PREVIEW, Some("Sapphire")),
cache_fingerprint(PREVIEW, None),
cache_fingerprint(CacheKind::TiltCurves, Some("Diamond")),
] {
assert_ne!(changed, base);
}
assert!(base.contains(PREVIEW_LIGHTING_PRESET.label()), "{base}");
}
#[test]
fn the_tilt_fingerprint_names_no_preview_setting() {
let fingerprint = cache_fingerprint(CacheKind::TiltCurves, Some("Diamond"));
for setting in ["size=", "spp=", "bounces="] {
assert!(!fingerprint.contains(setting), "{fingerprint}");
}
}
}