pub const MAX_MESH_LODS: usize = 8;
pub(crate) const MAX_MESH_SECTIONS: u32 = 65_536;
pub(crate) const SECTION_VERTICES: u32 = 384;
pub(crate) const CULL_WORKGROUP: u32 = 64;
pub(crate) const CULL_MAX_WORKGROUPS: u32 = 65_535;
pub(crate) const NO_SET: u32 = u32::MAX;
pub(crate) const NO_CHAIN: u32 = u32::MAX;
pub(crate) const SET_CASTS_DIRECTIONAL_SHADOW: u32 = 1;
pub(crate) const SET_PAIRS: u32 = 2;
pub(crate) const CULL_CAMERA: u32 = 1;
pub(crate) const CULL_FRUSTUM: u32 = 2;
pub(crate) const CULL_NEAR: u32 = 4;
pub(crate) const CULL_LOD: u32 = 8;
pub(crate) const CULL_CANDIDATE_STATISTICS: u32 = 16;
pub(crate) const CULL_PAIRED: u32 = 32;
pub(crate) const PAIRED_INDICES: [u32; SECTION_VERTICES as usize] = {
let mut indices = [0; SECTION_VERTICES as usize];
let mut at = 0;
while at < indices.len() {
let pair = (at / 6) as u32;
indices[at] = 4 * pair + [0, 1, 2, 0, 2, 3][at % 6];
at += 1;
}
indices
};
pub(crate) const OCCLUSION_EARLY: u32 = 1;
pub(crate) const DISPATCH_EARLY_SECTIONS: u32 = 0;
pub(crate) const DISPATCH_LATE_INSTANCES: u32 = 3;
pub(crate) const DISPATCH_LATE_SECTIONS: u32 = 6;
pub(crate) const CULL_DISPATCH_WORDS: u32 = 9;
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, bytemuck::Pod, bytemuck::Zeroable)]
pub(crate) struct DrawCandidate {
pub bounds_min: [f32; 3],
pub object: u32,
pub bounds_max: [f32; 3],
pub mesh: u32,
pub draw_set: u32,
pub chain: u32,
pub positions: u32,
pub padding: u32,
}
impl DrawCandidate {
pub const FREE: Self = Self {
bounds_min: [0.; 3],
object: 0,
bounds_max: [0.; 3],
mesh: 0,
draw_set: NO_SET,
chain: NO_CHAIN,
positions: crate::shading::vertex::NO_POSITIONS,
padding: 0,
};
}
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq, bytemuck::Pod, bytemuck::Zeroable)]
pub(crate) struct ChainLevel {
pub bounds_min: [f32; 3],
pub error: f32,
pub bounds_max: [f32; 3],
pub mesh: u32,
}
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, bytemuck::Pod, bytemuck::Zeroable)]
pub(crate) struct LodChain {
pub levels: [ChainLevel; MAX_MESH_LODS],
pub count: u32,
pub padding: [u32; 3],
}
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, bytemuck::Pod, bytemuck::Zeroable)]
pub(crate) struct DrawSet {
pub region: u32,
pub capacity: u32,
pub visibility_group: u32,
pub flags: u32,
}
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, bytemuck::Pod, bytemuck::Zeroable)]
pub(crate) struct CullView {
pub planes: [[f32; 4]; 6],
pub plane_errors: [[f32; 4]; 6],
pub lod_clip_from_world: [[f32; 4]; 4],
pub lod_magnitude: [[f32; 4]; 4],
pub lod_size: [f32; 2],
pub candidates: u32,
pub candidate_side: u32,
pub visibility_mask: u32,
pub flags: u32,
pub candidate_statistics: u32,
pub late_command: u32,
pub queue_capacity: u32,
pub paired_command: u32,
pub paired_region: u32,
pub padding: u32,
}
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, bytemuck::Pod, bytemuck::Zeroable)]
pub(crate) struct CullOcclusion {
pub previous: [[f32; 4]; 4],
pub current: [[f32; 4]; 4],
pub levels: u32,
pub flags: u32,
pub padding: [u32; 2],
}
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, bytemuck::Pod, bytemuck::Zeroable)]
pub(crate) struct CullListsHeader {
pub visible_count: u32,
pub visible_dispatched: u32,
pub visible_side: u32,
pub late_count: u32,
pub late_dispatched: u32,
pub late_side: u32,
pub late_visible_count: u32,
pub late_visible_dispatched: u32,
pub queue_count: u32,
pub queue_dispatched: u32,
pub late_sections_side: u32,
pub padding: u32,
}
pub(crate) fn late_entries(candidates: u32, queue: u32) -> u64 {
3 * u64::from(candidates) + u64::from(queue)
}
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, bytemuck::Pod, bytemuck::Zeroable)]
pub(crate) struct CullStatistics {
pub static_sections: u32,
pub static_triangles: u32,
pub moving_sections: u32,
pub moving_triangles: u32,
}
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, bytemuck::Pod, bytemuck::Zeroable)]
pub(crate) struct DrawCommand {
pub count: u32,
pub instance_count: u32,
pub first: u32,
pub base: u32,
pub first_instance: u32,
}
impl DrawCommand {
pub const RESET: Self = Self {
count: SECTION_VERTICES,
instance_count: 0,
first: 0,
base: 0,
first_instance: 0,
};
}
pub(crate) const CANDIDATE_STATISTICS_WORDS: u32 = 2;
pub(crate) const fn words<T>() -> u32 {
(std::mem::size_of::<T>() / 4) as u32
}
pub(crate) fn dispatch_side(workgroups: u32) -> [u32; 2] {
[
workgroups.min(CULL_MAX_WORKGROUPS),
workgroups.div_ceil(CULL_MAX_WORKGROUPS),
]
}
#[cfg(test)]
pub(crate) fn constants() -> Vec<crate::shading::layout_tests::Constant> {
use crate::shading::layout_tests::Constant;
use naga::Literal::U32;
[
("MAX_MESH_LODS", MAX_MESH_LODS as u32),
("MAX_MESH_SECTIONS", MAX_MESH_SECTIONS),
("SECTION_VERTICES", SECTION_VERTICES),
("CULL_WORKGROUP", CULL_WORKGROUP),
("CULL_MAX_WORKGROUPS", CULL_MAX_WORKGROUPS),
("NO_SET", NO_SET),
("NO_CHAIN", NO_CHAIN),
("SET_CASTS_DIRECTIONAL_SHADOW", SET_CASTS_DIRECTIONAL_SHADOW),
("SET_PAIRS", SET_PAIRS),
("CULL_CAMERA", CULL_CAMERA),
("CULL_FRUSTUM", CULL_FRUSTUM),
("CULL_NEAR", CULL_NEAR),
("CULL_LOD", CULL_LOD),
("CULL_CANDIDATE_STATISTICS", CULL_CANDIDATE_STATISTICS),
("CULL_PAIRED", CULL_PAIRED),
("OCCLUSION_EARLY", OCCLUSION_EARLY),
("DISPATCH_EARLY_SECTIONS", DISPATCH_EARLY_SECTIONS),
("DISPATCH_LATE_INSTANCES", DISPATCH_LATE_INSTANCES),
("DISPATCH_LATE_SECTIONS", DISPATCH_LATE_SECTIONS),
("CULL_DISPATCH_WORDS", CULL_DISPATCH_WORDS),
("CULL_STATISTICS_WORDS", words::<CullStatistics>()),
(
"CULL_STATIC_SECTIONS",
(std::mem::offset_of!(CullStatistics, static_sections) / 4) as u32,
),
(
"CULL_STATIC_TRIANGLES",
(std::mem::offset_of!(CullStatistics, static_triangles) / 4) as u32,
),
(
"CULL_MOVING_SECTIONS",
(std::mem::offset_of!(CullStatistics, moving_sections) / 4) as u32,
),
(
"CULL_MOVING_TRIANGLES",
(std::mem::offset_of!(CullStatistics, moving_triangles) / 4) as u32,
),
("DRAW_COMMAND_WORDS", words::<DrawCommand>()),
(
"DRAW_COMMAND_INSTANCE_COUNT",
(std::mem::offset_of!(DrawCommand, instance_count) / 4) as u32,
),
("CANDIDATE_STATISTICS_WORDS", CANDIDATE_STATISTICS_WORDS),
]
.into_iter()
.map(|(name, value)| Constant::new("cull", name, U32(value)))
.collect()
}
#[cfg(test)]
pub(crate) fn mirrors() -> [crate::shading::layout_tests::Mirror; 7] {
use crate::shading::layout_tests::mirror;
[
mirror!(
"cull",
"DrawCandidate",
DrawCandidate,
[
bounds_min, object, bounds_max, mesh, draw_set, chain, positions
]
),
mirror!(
"cull",
"ChainLevel",
ChainLevel,
[bounds_min, error, bounds_max, mesh]
),
mirror!("cull", "LodChain", LodChain, [levels, count]),
mirror!(
"cull",
"DrawSet",
DrawSet,
[region, capacity, visibility_group, flags]
),
mirror!(
"cull",
"CullView",
CullView,
[
planes,
plane_errors,
lod_clip_from_world,
lod_magnitude,
lod_size,
candidates,
candidate_side,
visibility_mask,
flags,
candidate_statistics,
late_command,
queue_capacity,
paired_command,
paired_region,
]
),
mirror!(
"cull",
"CullOcclusion",
CullOcclusion,
[previous, current, levels, flags]
),
mirror!(
"cull",
"CullLists",
CullListsHeader,
[
visible_count,
visible_dispatched,
visible_side,
late_count,
late_dispatched,
late_side,
late_visible_count,
late_visible_dispatched,
queue_count,
queue_dispatched,
late_sections_side,
]
),
]
}