use super::{Alpha, Cull, GeometryPass, Variant};
use crate::Scene;
use crate::content::identity::ShaderId;
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,
pub films: bool,
pub sheens: bool,
pub diffuse_transmission: bool,
}
impl LitConstants {
pub fn of(scene: &Scene) -> Self {
Self {
rect_lights: scene.lights.holds_rect(),
decals: !scene.decals.is_empty(),
films: scene.materials.holds_films(),
sheens: scene.materials.holds_sheens(),
diffuse_transmission: scene.materials.holds_diffuse_transmission(),
}
}
pub fn constants(self) -> [(&'static str, f64); 5] {
[
("rect_lights_enabled", f64::from(u8::from(self.rect_lights))),
("decals_enabled", f64::from(u8::from(self.decals))),
("films_enabled", f64::from(u8::from(self.films))),
("sheens_enabled", f64::from(u8::from(self.sheens))),
(
"diffuse_transmission_enabled",
f64::from(u8::from(self.diffuse_transmission)),
),
]
}
}
pub(super) type Specialisation = (LayerConstants, LitConstants, bool);
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub(crate) struct PipelineKey {
pub(super) pass: GeometryPass,
pub(super) variant: Variant,
pub(super) shader: Option<ShaderId>,
layers: LayerConstants,
lit: LitConstants,
transmission: bool,
}
impl PipelineKey {
pub fn new(
pass: GeometryPass,
variant: Variant,
shader: Option<ShaderId>,
(layers, lit): (LayerConstants, LitConstants),
transmission: bool,
) -> Self {
let constant = pass.caster() || pass.volume();
Self {
pass,
shader,
variant: Variant {
deformed: variant.deformed && pass.pulled(),
cull: if pass.volume() {
Cull::None
} else {
variant.cull
},
..variant
},
layers: if constant {
LayerConstants::ALL
} else {
layers
},
lit: if constant {
LitConstants::default()
} else {
lit
},
transmission: transmission && matches!(pass, GeometryPass::Blended { .. }),
}
}
pub(super) fn specialised(self, (layers, lit, transmission): Specialisation) -> bool {
self == Self::new(
self.pass,
self.variant,
self.shader,
(layers, lit),
transmission,
)
}
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());
let motion = !matches!(
self.pass,
GeometryPass::Blended { .. } | GeometryPass::Fsr2Composition
) && !self.pass.volume();
constants.push(("motion_vertices", f64::from(u8::from(motion))));
}
if !self.pass.caster() || self.variant.alpha == Alpha::Mask {
constants.push(masked);
}
if self.pass.pulled() {
constants.push(deformed);
}
if matches!(self.pass, GeometryPass::Blended { .. }) {
constants.push((
"transmission_enabled",
f64::from(u8::from(self.transmission)),
));
}
constants
}
}