use super::{Alpha, GeometryPass, Variant};
use crate::Scene;
use crate::settings::DisabledLayers;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub(crate) struct LayerConstants {
pub normal_maps: bool,
pub bump_maps: bool,
pub baked_lighting: bool,
pub instance_emission: bool,
}
impl LayerConstants {
pub const ALL: Self = Self {
normal_maps: true,
bump_maps: true,
baked_lighting: true,
instance_emission: true,
};
pub fn new(disable: &DisabledLayers) -> Self {
Self {
normal_maps: !disable.normal_maps,
bump_maps: !disable.bump_maps,
baked_lighting: !disable.baked_lighting,
instance_emission: !disable.instance_emission,
}
}
fn constants(self) -> [(&'static str, f64); 4] {
[
("normal_maps_enabled", f64::from(u8::from(self.normal_maps))),
("bump_maps_enabled", f64::from(u8::from(self.bump_maps))),
(
"baked_lighting_enabled",
f64::from(u8::from(self.baked_lighting)),
),
(
"instance_emission_enabled",
f64::from(u8::from(self.instance_emission)),
),
]
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub(crate) struct LitConstants {
pub rect_lights: bool,
pub decals: bool,
}
impl LitConstants {
pub fn of(scene: &Scene) -> Self {
Self {
rect_lights: scene.lights.holds_rect(),
decals: !scene.decals.is_empty(),
}
}
pub fn constants(self) -> [(&'static str, f64); 2] {
[
("rect_lights_enabled", f64::from(u8::from(self.rect_lights))),
("decals_enabled", f64::from(u8::from(self.decals))),
]
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub(crate) struct PipelineKey {
pub(super) pass: GeometryPass,
pub(super) variant: Variant,
layers: LayerConstants,
lit: LitConstants,
}
impl PipelineKey {
pub fn new(
pass: GeometryPass,
variant: Variant,
layers: LayerConstants,
lit: LitConstants,
) -> Self {
let caster = pass.caster();
Self {
pass,
variant: Variant {
deformed: variant.deformed && pass.pulled(),
..variant
},
layers: if caster { LayerConstants::ALL } else { layers },
lit: if caster { LitConstants::default() } else { lit },
}
}
pub(super) fn constants(self) -> Vec<(&'static str, f64)> {
let masked = (
"alpha_mask",
f64::from(u8::from(self.variant.alpha == Alpha::Mask)),
);
let deformed = (
"deformed_vertices",
f64::from(u8::from(self.variant.deformed)),
);
let mut constants = Vec::new();
if !self.pass.caster() {
constants.extend(self.layers.constants());
constants.extend(self.lit.constants());
}
if !self.pass.caster() || self.variant.alpha == Alpha::Mask {
constants.push(masked);
}
if self.pass.pulled() {
constants.push(deformed);
}
constants
}
}