pub(crate) mod bindings;
pub(crate) mod cached_group;
pub(crate) mod cascades;
pub(crate) mod clusters;
pub(crate) mod culling;
pub(crate) mod draw_list;
pub(crate) mod effective;
pub(crate) mod frame;
pub(crate) mod history;
pub(crate) mod lod;
pub(crate) mod pipelines;
pub(crate) mod population;
pub(crate) mod post_fx;
pub(crate) mod reflection_camera;
pub(crate) mod targets;
pub(crate) mod trace_paths;
use crate::FrameInput;
use crate::content::irradiance_volume::IrradianceVolume;
use crate::content::lighting::{Backdrop, DirectionalLight, DirectionalShadow};
use crate::scene::dynamic_gi::ProbePlacement;
use crate::scene::static_lighting::StaticLighting;
use crate::shading::uniforms::{
DIRECTIONAL_LIGHT_SHADOW, DirectionalLightUniform, FRAME_BACKDROP_COLOR, FRAME_BAKED_LIGHTING,
FRAME_DYNAMIC_GI, FRAME_FOG, FRAME_HARDWARE_SHADOW_FILTER, FRAME_IRRADIANCE_ATLAS,
FRAME_IRRADIANCE_VOLUME, FRAME_TEMPORAL_SHADOW_FILTER, FrameUniform, ShadowCascadeUniform,
VIEW_PROBE_CAPTURE, ViewUniform,
};
use bytemuck::Zeroable;
use cascades::{Cascades, MAX_SHADOW_CASCADES};
use effective::ShadowFilter;
use glam::camera;
use glam::{Mat4, Vec3};
pub(crate) const LOCAL_SHADOW_NEAR: f32 = 0.02;
const LOCAL_SHADOW_FACES: [(Vec3, Vec3); 6] = [
(Vec3::X, Vec3::NEG_Y),
(Vec3::NEG_X, Vec3::NEG_Y),
(Vec3::Y, Vec3::Z),
(Vec3::NEG_Y, Vec3::NEG_Z),
(Vec3::Z, Vec3::NEG_Y),
(Vec3::NEG_Z, Vec3::NEG_Y),
];
const PROBE_FACES: [(Vec3, Vec3); 6] = [
(Vec3::X, Vec3::Y),
(Vec3::NEG_X, Vec3::Y),
(Vec3::Y, Vec3::Z),
(Vec3::NEG_Y, Vec3::NEG_Z),
(Vec3::NEG_Z, Vec3::Y),
(Vec3::Z, Vec3::Y),
];
fn flag(on: bool, flag: u32) -> u32 {
if on { flag } else { 0 }
}
fn disc_radius(angular_diameter: f32) -> f32 {
let angle = if angular_diameter.is_nan() {
0.
} else {
angular_diameter.clamp(0., std::f32::consts::FRAC_PI_2)
};
(angle * 0.5).tan()
}
fn directional_light(input: &FrameInput, index: usize) -> Option<DirectionalLight> {
input.directional_lights[index].filter(DirectionalLight::is_on)
}
pub(crate) fn directional_shadow(input: &FrameInput) -> Option<(usize, DirectionalShadow)> {
(0..input.directional_lights.len()).find_map(|index| {
directional_light(input, index)?
.shadow
.map(|shadow| (index, shadow))
})
}
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct FrameShadow {
pub light: Option<usize>,
pub cascades: Cascades,
pub filter: ShadowFilter,
pub frame_count: u32,
}
impl FrameShadow {
pub fn camera(
input: &FrameInput,
map_size: u32,
filter: ShadowFilter,
frame_count: u32,
origin: glam::DVec3,
) -> Self {
let camera = input.camera;
let (light, cascades) = Self::fit(input, |direction, shadow| {
Cascades::camera(
camera.view,
camera.projection,
direction,
shadow,
map_size,
origin,
)
})
.map_or((None, Cascades::default()), |(light, cascades)| {
(Some(light), cascades)
});
Self {
light,
cascades,
filter,
frame_count,
}
}
pub fn capture(input: &FrameInput, center: Vec3, map_size: u32, origin: glam::DVec3) -> Self {
Self::fit(input, |direction, shadow| {
Cascades::capture(center, direction, shadow, map_size, origin)
})
.map_or_else(Self::default, |(light, cascades)| Self {
light: Some(light),
cascades,
..Self::default()
})
}
fn fit(
input: &FrameInput,
fit: impl FnOnce(Vec3, &DirectionalShadow) -> Cascades,
) -> Option<(usize, Cascades)> {
let (light, shadow) = directional_shadow(input)?;
let direction = input.directional_lights[light]?.direction;
Some((light, fit(direction, &shadow)))
}
}
pub(crate) fn frame_uniform(
input: &FrameInput,
(baked, irradiance_volume): (&StaticLighting, Option<IrradianceVolume>),
shadow: &FrameShadow,
fog: bool,
dynamic_gi: Option<&ProbePlacement>,
) -> FrameUniform {
let irradiance_volume = irradiance_volume.filter(|_| input.baked_lighting);
let cascades = shadow.cascades.as_slice();
let directional_lights = std::array::from_fn(|index| {
directional_light(input, index).map_or(DirectionalLightUniform::zeroed(), |light| {
DirectionalLightUniform {
direction_to_light: (-light.direction).to_array(),
flags: flag(shadow.light == Some(index), DIRECTIONAL_LIGHT_SHADOW),
color: light.color,
illuminance: light.illuminance,
fog_energy: if light.fog_energy.is_finite() && light.fog_energy > 0. {
light.fog_energy
} else {
0.
},
shadow_opacity: if light.shadow_opacity.is_finite() {
light.shadow_opacity.clamp(0., 1.)
} else {
0.
},
disc_radius: disc_radius(light.angular_diameter),
padding: 0.,
}
})
});
let shadow_cascades: [ShadowCascadeUniform; MAX_SHADOW_CASCADES] =
std::array::from_fn(|index| {
cascades
.get(index)
.map_or(ShadowCascadeUniform::zeroed(), |cascade| {
ShadowCascadeUniform {
clip_from_world: cascade.clip_from_world.to_cols_array_2d(),
texel_size: cascade.texel_size,
far_bound: cascade.far_bound,
padding: [0.; 2],
}
})
});
let (backdrop_color, backdrop_yaw, backdrop_brightness) = match input.backdrop {
Backdrop::Environment { yaw, brightness } => ([0.; 3], yaw, brightness),
Backdrop::Color(color) => (color, 0., 0.),
};
let hemisphere = input.hemisphere_light;
let mist = input.mist;
FrameUniform {
directional_lights,
shadow_cascades,
hemisphere_sky_color: hemisphere.sky_color,
hemisphere_intensity: hemisphere.intensity,
hemisphere_ground_color: hemisphere.ground_color,
diffuse_environment_yaw: input.diffuse_environment.yaw,
backdrop_color,
diffuse_environment_intensity: input.diffuse_environment.intensity,
mist_thin_color: mist.thin_color,
mist_opacity: mist.opacity,
mist_dense_color: mist.dense_color,
backdrop_yaw,
mist_size: [mist.width, mist.height],
mist_drift: mist.drift,
backdrop_brightness,
fog_inverse_length: input.fog.length.recip(),
fog_inverse_detail_spread: crate::shading::fog::DETAIL_SPREAD.recip(),
reflection_yaw: input.reflection_environment.yaw,
reflection_intensity: input.reflection_environment.intensity,
elapsed_seconds: input.elapsed_seconds as f32,
fixed_irradiance_scale: baked.atlas_scale,
visibility_mask: input.visibility_mask,
lightmap_chart: baked.lightmap_chart,
flags: flag(fog, FRAME_FOG)
| flag(input.baked_lighting, FRAME_BAKED_LIGHTING)
| flag(baked.atlas_installed, FRAME_IRRADIANCE_ATLAS)
| flag(
matches!(input.backdrop, Backdrop::Color(_)),
FRAME_BACKDROP_COLOR,
)
| flag(
shadow.filter == ShadowFilter::Temporal,
FRAME_TEMPORAL_SHADOW_FILTER,
)
| flag(
shadow.filter == ShadowFilter::Hardware,
FRAME_HARDWARE_SHADOW_FILTER,
)
| flag(dynamic_gi.is_some(), FRAME_DYNAMIC_GI)
| flag(irradiance_volume.is_some(), FRAME_IRRADIANCE_VOLUME),
shadow_cascade_count: cascades.len() as u32,
frame_count: shadow.frame_count,
animation_phase: crate::shading::material::animation_phase(input.elapsed_seconds),
dynamic_gi_origin: dynamic_gi
.map_or([0.; 3], |placement| placement.volume.origin.to_array()),
padding_origin: 0.,
dynamic_gi_spacing: dynamic_gi
.map_or([0.; 3], |placement| placement.volume.spacing.to_array()),
padding_spacing: 0.,
dynamic_gi_probes: dynamic_gi.map_or([0; 3], |placement| placement.volume.probes),
padding_probes: 0,
dynamic_gi_scroll: dynamic_gi.map_or([0; 3], |placement| placement.scroll),
padding_scroll: 0,
irradiance_volume_origin: irradiance_volume
.map_or([0.; 3], |volume| volume.origin.to_array()),
padding_volume_origin: 0.,
irradiance_volume_cell_size: irradiance_volume
.map_or([0.; 3], |volume| volume.cell_size.to_array()),
padding_cell_size: 0.,
irradiance_volume_cells: irradiance_volume.map_or([0; 3], |volume| volume.cells),
padding_cells: 0,
}
}
#[derive(Clone, Copy, Debug)]
pub(crate) struct Jitter {
pub ndc: [f32; 2],
pub mip_bias: f32,
}
#[derive(Clone, Copy)]
pub(crate) struct View {
pub uniform: ViewUniform,
}
impl View {
pub fn camera(uniform: ViewUniform) -> Self {
Self { uniform }
}
pub fn probe_face(center: Vec3, face: u8) -> Self {
let (direction, up) = PROBE_FACES[usize::from(face)];
let projection = crate::perspective(std::f32::consts::FRAC_PI_2, 1., 0.1);
let view = camera::rh::view::look_at_mat4(center, center + direction, up);
let view_projection = projection * view;
Self {
uniform: ViewUniform {
view: view.to_cols_array_2d(),
projection: projection.to_cols_array_2d(),
view_projection: view_projection.to_cols_array_2d(),
inverse_view_projection: view_projection.inverse().to_cols_array_2d(),
stable_view_projection: view_projection.to_cols_array_2d(),
previous_view_projection: view_projection.to_cols_array_2d(),
eye: center.to_array(),
mip_bias: 0.,
jitter: [0.; 2],
viewport: [0.; 2],
flags: VIEW_PROBE_CAPTURE,
padding: [0; 3],
},
}
}
pub fn shadow_cascade(clip_from_world: Mat4) -> Self {
let clip = clip_from_world.to_cols_array_2d();
Self {
uniform: ViewUniform {
view: Mat4::IDENTITY.to_cols_array_2d(),
projection: clip,
view_projection: clip,
..ViewUniform::zeroed()
},
}
}
pub fn local_shadow_face(face: u8, position: Vec3, range: f32) -> Self {
let (direction, up) = LOCAL_SHADOW_FACES[usize::from(face)];
Self::local_shadow(position, direction, up, std::f32::consts::FRAC_PI_2, range)
}
pub fn spot_shadow(position: Vec3, direction: Vec3, outer_angle: f32, range: f32) -> Self {
let up = if direction.y.abs() < 0.9 {
Vec3::Y
} else {
Vec3::X
};
Self::local_shadow(position, direction, up, 2. * outer_angle, range)
}
fn local_shadow(position: Vec3, direction: Vec3, up: Vec3, fov: f32, range: f32) -> Self {
let projection = camera::rh::proj::directx::perspective(fov, 1., range, LOCAL_SHADOW_NEAR);
let view = camera::rh::view::look_to_mat4(position, direction, up);
Self {
uniform: ViewUniform {
view: view.to_cols_array_2d(),
projection: projection.to_cols_array_2d(),
view_projection: (projection * view).to_cols_array_2d(),
eye: position.to_array(),
..ViewUniform::zeroed()
},
}
}
pub fn view_projection(&self) -> Mat4 {
Mat4::from_cols_array_2d(&self.uniform.view_projection)
}
pub fn frustum(&self, pose: Mat4) -> culling::Frustum {
culling::Frustum::new(
Mat4::from_cols_array_2d(&self.uniform.view),
Mat4::from_cols_array_2d(&self.uniform.projection),
pose,
self.uniform.jitter,
)
}
}
pub(crate) struct ViewSlot {
pub view: View,
pub buffer: wgpu::Buffer,
pub list: draw_list::gpu::GpuList,
#[cfg_attr(not(feature = "diagnostics"), allow(dead_code))]
build_ms: std::cell::Cell<f64>,
encode_ms: std::cell::Cell<f64>,
}
impl ViewSlot {
fn new(device: &wgpu::Device, label: &str) -> Self {
let view = View {
uniform: ViewUniform::zeroed(),
};
Self {
buffer: crate::scene::buffer(
device,
label,
bytemuck::bytes_of(&view.uniform),
wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
),
view,
list: draw_list::gpu::GpuList::new(device),
build_ms: std::cell::Cell::new(0.),
encode_ms: std::cell::Cell::new(0.),
}
}
pub fn built(&mut self, started: crate::counters::Moment) {
self.build_ms.set(started.elapsed_ms());
self.encode_ms.set(0.);
}
pub fn culled_since(&self, started: crate::counters::Moment) {
self.build_ms
.set(self.build_ms.get() + started.elapsed_ms());
}
pub fn recorded_since(&self, started: crate::counters::Moment) {
self.encode_ms
.set(self.encode_ms.get() + started.elapsed_ms());
}
#[cfg(feature = "diagnostics")]
pub fn cpu_ms(&self) -> crate::diagnostics::ViewTime {
crate::diagnostics::ViewTime {
build_ms: self.build_ms.get(),
encode_ms: self.encode_ms.get(),
}
}
pub fn set(&mut self, queue: &wgpu::Queue, view: View) {
self.view = view;
crate::counters::write_buffer(queue, &self.buffer, 0, bytemuck::bytes_of(&view.uniform));
}
}
pub(crate) struct FrameViews {
pub camera: ViewSlot,
pub blended: draw_list::DrawList,
pub clusters: clusters::Clusters,
pub ray_lists: clusters::Clusters,
pub volume_lists: clusters::Clusters,
pub cascades: [ViewSlot; MAX_SHADOW_CASCADES],
pub cascade_count: usize,
pub reflection_camera: reflection_camera::Camera,
pub instances: draw_list::DrawInstances,
}
impl FrameViews {
pub fn new(device: &wgpu::Device) -> Self {
Self {
camera: ViewSlot::new(device, "camera view"),
blended: draw_list::DrawList::default(),
clusters: clusters::Clusters::new(device, "camera clusters"),
ray_lists: clusters::Clusters::new(device, "ray hit lights and decals"),
volume_lists: clusters::Clusters::new(device, "dynamic GI probe hit lights and decals"),
cascades: std::array::from_fn(|_| ViewSlot::new(device, "directional shadow cascade")),
cascade_count: 0,
reflection_camera: reflection_camera::Camera::new(Mat4::IDENTITY, Mat4::IDENTITY),
instances: draw_list::DrawInstances::default(),
}
}
}