mod assign;
mod volumes;
use crate::content::decal::Decal;
use crate::content::identity::Identity;
use crate::content::light::Light;
use crate::scene::decals::Decals;
use crate::scene::lights::Lights;
use crate::shading::clusters::{CLUSTER_HEADER_WORDS, CLUSTER_MOST_ITEMS, ClusterGrid};
use assign::{Clusterable, Scratch};
use glam::{Mat4, UVec2, UVec3};
pub(crate) use volumes::{BoxVolume, ViewVolume};
#[derive(Clone, Copy, Debug)]
pub(crate) struct ClusterConfig {
pub total: u32,
pub z_slices: u32,
pub first_slice_depth: f32,
}
pub(crate) const CAMERA_CLUSTERS: ClusterConfig = ClusterConfig {
total: 4096,
z_slices: 48,
first_slice_depth: 5.,
};
impl ClusterConfig {
fn dimensions_for_screen_size(&self, screen_size: UVec2) -> UVec3 {
let aspect_ratio = screen_size.x as f32 / screen_size.y as f32;
let z_slices = self.z_slices.min(self.total);
let per_layer = self.total as f32 / z_slices as f32;
let y = (per_layer / aspect_ratio).sqrt();
let mut x = (y * aspect_ratio) as u32;
let mut y = y as u32;
if x == 0 {
x = 1;
y = per_layer as u32;
}
if y == 0 {
x = per_layer as u32;
y = 1;
}
UVec3::new(x, y, z_slices)
}
}
const BAKED: u32 = 1 << 31;
const DECAL: u32 = 1 << 30;
const KIND: u32 = BAKED | DECAL;
fn clusterables(
lights: &Lights,
lights_enabled: bool,
decals: &Decals,
out: &mut Vec<Clusterable>,
) {
out.clear();
if lights_enabled {
for baked in [false, true] {
out.extend(
lights
.on()
.filter(|(_, light)| light.baked == baked)
.map(|(id, light)| {
let kind = if baked { BAKED } else { 0 };
Clusterable::light(id.index() as u32 | kind, light)
}),
);
}
}
out.extend(
decals
.iter()
.map(|(id, decal)| Clusterable::decal(id.index() as u32 | DECAL, decal)),
);
}
fn pack(cluster_count: usize, pairs: &[(u32, u32)], cursor: &mut Vec<u32>, data: &mut Vec<u32>) {
data.clear();
data.resize(cluster_count * CLUSTER_HEADER_WORDS, 0);
for &(cluster, item) in pairs {
let header = &mut data[cluster as usize * CLUSTER_HEADER_WORDS..][..CLUSTER_HEADER_WORDS];
if (header[1] + header[2] + header[3]) as usize == CLUSTER_MOST_ITEMS {
continue;
}
let count = match item & KIND {
0 => 1,
BAKED => 2,
_ => 3,
};
header[count] += 1;
}
cursor.clear();
let mut offset = data.len() as u32;
for cluster in data.chunks_exact_mut(CLUSTER_HEADER_WORDS) {
cluster[0] = offset;
cursor.push(offset);
offset += cluster[1] + cluster[2] + cluster[3];
}
data.resize(offset as usize, 0);
for &(cluster, item) in pairs {
let header = &data[cluster as usize * CLUSTER_HEADER_WORDS..][..CLUSTER_HEADER_WORDS];
let end = header[0] + header[1] + header[2] + header[3];
let at = &mut cursor[cluster as usize];
if *at == end {
continue;
}
data[*at as usize] = item & !KIND;
*at += 1;
}
}
pub(crate) struct Clusters {
label: &'static str,
buffer: wgpu::Buffer,
data: Vec<u32>,
scratch: Scratch,
}
impl Clusters {
pub fn new(device: &wgpu::Device, label: &'static str) -> Self {
Self {
label,
buffer: clusters_buffer(device, label, GRID_BYTES + 16),
data: Vec::new(),
scratch: Scratch::default(),
}
}
pub fn buffer(&self) -> &wgpu::Buffer {
&self.buffer
}
#[allow(clippy::too_many_arguments)]
pub fn cluster(
&mut self,
device: &wgpu::Device,
queue: &wgpu::Queue,
lights: &Lights,
lights_enabled: bool,
decals: &Decals,
view: Mat4,
projection: Mat4,
screen: [u32; 2],
config: ClusterConfig,
) {
clusterables(lights, lights_enabled, decals, &mut self.scratch.objects);
let grid = assign::assign(
view,
projection,
UVec2::from_array(screen).max(UVec2::ONE),
config,
&mut self.scratch,
);
let [x, y, z] = grid.dimensions;
let scratch = &mut self.scratch;
pack(
(x * y * z) as usize,
&scratch.pairs,
&mut scratch.cursor,
&mut self.data,
);
self.upload(device, queue, grid);
}
#[allow(clippy::too_many_arguments)]
pub fn list(
&mut self,
device: &wgpu::Device,
queue: &wgpu::Queue,
lights: &Lights,
lights_enabled: bool,
keep_light: impl Fn(&Light) -> bool,
decals: &Decals,
keep_decal: impl Fn(&Decal) -> bool,
) {
let scratch = &mut self.scratch;
clusterables(lights, lights_enabled, decals, &mut scratch.objects);
scratch.pairs.clear();
for object in &scratch.objects {
let index = (object.index & !KIND) as usize;
let keep = if object.index & DECAL == 0 {
keep_light(lights.slots.at(index).unwrap())
} else {
keep_decal(decals.slots.at(index).unwrap())
};
if keep {
scratch.pairs.push((0, object.index));
}
}
pack(1, &scratch.pairs, &mut scratch.cursor, &mut self.data);
self.upload(device, queue, ClusterGrid::LIST);
}
fn upload(&mut self, device: &wgpu::Device, queue: &wgpu::Queue, grid: ClusterGrid) {
let bytes = GRID_BYTES + 4 * self.data.len() as u64;
if bytes > self.buffer.size() {
let limits = device.limits();
let limit = limits
.max_storage_buffer_binding_size
.min(limits.max_buffer_size);
let size = bytes.next_power_of_two().min(limit & !3);
self.buffer = clusters_buffer(device, self.label, size);
}
let room = ((self.buffer.size() - GRID_BYTES) / 4) as usize;
self.data.truncate(room);
crate::counters::write_buffer(queue, &self.buffer, 0, bytemuck::bytes_of(&grid));
if !self.data.is_empty() {
crate::counters::write_buffer(
queue,
&self.buffer,
GRID_BYTES,
bytemuck::cast_slice(&self.data),
);
}
}
}
const GRID_BYTES: u64 = std::mem::size_of::<ClusterGrid>() as u64;
fn clusters_buffer(device: &wgpu::Device, label: &str, size: u64) -> wgpu::Buffer {
crate::counters::buffer(
device,
&wgpu::BufferDescriptor {
label: Some(label),
size,
usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
},
)
}
#[cfg(all(test, not(target_arch = "wasm32")))]
mod tests;