use crate::camera::Camera;
use crate::layers::math::fog_params;
use crate::scene::Scene;
use crate::slots::{SLOT_BYTES, SlotScene};
use bytemuck::{Pod, Zeroable};
pub const DEPTH_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Depth32Float;
#[repr(C)]
#[derive(Clone, Copy, Pod, Zeroable)]
struct CameraUniform {
view_proj: [[f32; 4]; 4],
viewport_px: [f32; 2],
point_px: f32,
point_m: f32,
fog_color: [f32; 4],
params: [f32; 4],
}
#[repr(C)]
#[derive(Clone, Copy, Pod, Zeroable)]
struct ChunkUniform {
offset: [f32; 4],
scale: [f32; 4],
}
pub struct Renderer {
pipelines: [wgpu::RenderPipeline; 6],
camera_buf: wgpu::Buffer,
camera_bg: wgpu::BindGroup,
chunk_layout: wgpu::BindGroupLayout,
chunk_buf: wgpu::Buffer,
chunk_bg: wgpu::BindGroup,
chunk_capacity: usize,
chunk_stride: u64,
pub point_px: f32,
timestamps: Option<(wgpu::QuerySet, u32)>,
point_m: f32,
prepared: Vec<(usize, usize)>,
prepared_slots: Vec<usize>,
}
fn pipeline_index(kind: usize, faded: bool) -> usize {
kind + 3 * usize::from(faded)
}
impl Renderer {
pub fn new(device: &wgpu::Device, color_format: wgpu::TextureFormat) -> Self {
let source = format!(
"{}\n{}",
include_str!("color.wgsl"),
include_str!("points.wgsl")
);
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: Some("pointblitz points"),
source: wgpu::ShaderSource::Wgsl(source.into()),
});
let uniform_entry = |dynamic: bool| wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::VERTEX,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: dynamic,
min_binding_size: None,
},
count: None,
};
let camera_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("camera"),
entries: &[uniform_entry(false)],
});
let chunk_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("chunk"),
entries: &[uniform_entry(true)],
});
let layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("points"),
bind_group_layouts: &[Some(&camera_layout), Some(&chunk_layout)],
immediate_size: 0,
});
let faded = wgpu::BlendComponent {
src_factor: wgpu::BlendFactor::Constant,
dst_factor: wgpu::BlendFactor::OneMinusConstant,
operation: wgpu::BlendOperation::Add,
};
let faded = Some(wgpu::BlendState {
color: faded,
alpha: faded,
});
let attrs12 = wgpu::vertex_attr_array![0 => Uint16x4, 1 => Unorm8x4];
let attrs8 = wgpu::vertex_attr_array![0 => Uint16x4];
let attrs16 = wgpu::vertex_attr_array![0 => Float32x3, 1 => Uint8x4];
let pipeline = |stride: u64,
entry: &str,
attributes: &[wgpu::VertexAttribute],
blend: Option<wgpu::BlendState>| {
device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("points"),
layout: Some(&layout),
vertex: wgpu::VertexState {
module: &shader,
entry_point: Some(entry),
compilation_options: Default::default(),
buffers: &[Some(wgpu::VertexBufferLayout {
array_stride: stride,
step_mode: wgpu::VertexStepMode::Instance,
attributes,
})],
},
primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::TriangleStrip,
..Default::default()
},
depth_stencil: Some(wgpu::DepthStencilState {
format: DEPTH_FORMAT,
depth_write_enabled: Some(true),
depth_compare: Some(wgpu::CompareFunction::Greater),
stencil: Default::default(),
bias: Default::default(),
}),
multisample: Default::default(),
fragment: Some(wgpu::FragmentState {
module: &shader,
entry_point: Some("fs"),
compilation_options: Default::default(),
targets: &[Some(wgpu::ColorTargetState {
format: color_format,
blend,
write_mask: wgpu::ColorWrites::ALL,
})],
}),
multiview_mask: None,
cache: None,
})
};
let slot = SLOT_BYTES as u64;
let pipelines = [
pipeline(12, "vs12", &attrs12, None),
pipeline(8, "vs8", &attrs8, None),
pipeline(slot, "vs16", &attrs16, None),
pipeline(12, "vs12", &attrs12, faded),
pipeline(8, "vs8", &attrs8, faded),
pipeline(slot, "vs16", &attrs16, faded),
];
let camera_buf = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("camera"),
size: size_of::<CameraUniform>() as u64,
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
let camera_bg = device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("camera"),
layout: &camera_layout,
entries: &[wgpu::BindGroupEntry {
binding: 0,
resource: camera_buf.as_entire_binding(),
}],
});
let align = u64::from(device.limits().min_uniform_buffer_offset_alignment);
let chunk_stride = (size_of::<ChunkUniform>() as u64).div_ceil(align) * align;
let (chunk_buf, chunk_bg) = Self::chunk_slots(device, &chunk_layout, chunk_stride, 64);
Self {
pipelines,
camera_buf,
camera_bg,
chunk_layout,
chunk_buf,
chunk_bg,
chunk_capacity: 64,
chunk_stride,
point_px: 2.0,
timestamps: None,
point_m: 0.0,
prepared: Vec::new(),
prepared_slots: Vec::new(),
}
}
fn chunk_slots(
device: &wgpu::Device,
layout: &wgpu::BindGroupLayout,
stride: u64,
capacity: usize,
) -> (wgpu::Buffer, wgpu::BindGroup) {
let buf = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("chunk offsets"),
size: stride * capacity as u64,
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
let bg = device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("chunk offsets"),
layout,
entries: &[wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding {
buffer: &buf,
offset: 0,
size: wgpu::BufferSize::new(size_of::<ChunkUniform>() as u64),
}),
}],
});
(buf, bg)
}
pub fn set_timestamp_writes(&mut self, writes: Option<(wgpu::QuerySet, u32)>) {
self.timestamps = writes;
}
#[allow(clippy::too_many_arguments)]
pub fn render(
&mut self,
device: &wgpu::Device,
queue: &wgpu::Queue,
encoder: &mut wgpu::CommandEncoder,
color: &wgpu::TextureView,
depth: &wgpu::TextureView,
size: [u32; 2],
camera: &Camera,
scene: &Scene,
) {
self.render_with_slots(
device,
queue,
encoder,
color,
depth,
size,
camera,
&[scene],
&[],
);
}
#[allow(clippy::too_many_arguments)]
pub fn render_with_slots(
&mut self,
device: &wgpu::Device,
queue: &wgpu::Queue,
encoder: &mut wgpu::CommandEncoder,
color: &wgpu::TextureView,
depth: &wgpu::TextureView,
size: [u32; 2],
camera: &Camera,
scenes: &[&Scene],
slots: &[&SlotScene],
) {
self.write_frame(device, queue, size, camera, scenes, slots, None);
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("points"),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: color,
depth_slice: None,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color::BLACK),
store: wgpu::StoreOp::Store,
},
})],
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
view: depth,
depth_ops: Some(wgpu::Operations {
load: wgpu::LoadOp::Clear(0.0),
store: wgpu::StoreOp::Discard,
}),
stencil_ops: None,
}),
timestamp_writes: self.timestamps.as_ref().map(|(set, index)| {
wgpu::RenderPassTimestampWrites {
query_set: set,
beginning_of_pass_write_index: Some(*index),
end_of_pass_write_index: Some(*index + 1),
}
}),
..Default::default()
});
self.draw(&mut pass, scenes, None);
self.draw_slots(&mut pass, slots, None);
}
#[allow(clippy::too_many_arguments)]
fn write_frame(
&mut self,
device: &wgpu::Device,
queue: &wgpu::Queue,
size: [u32; 2],
camera: &Camera,
scenes: &[&Scene],
slot_scenes: &[&SlotScene],
fog: Option<crate::layers::Fog>,
) {
let (w, h) = (size[0].max(1), size[1].max(1));
let aspect = f64::from(w) / f64::from(h);
let f = fog_params(fog);
let cam = CameraUniform {
view_proj: camera.view_proj(aspect).to_cols_array_2d(),
viewport_px: [w as f32, h as f32],
point_px: self.point_px,
point_m: self.point_m,
fog_color: f.color,
params: [f.scale, f.bias, focal_px(camera.fov_y_deg, h) as f32, 0.0],
};
queue.write_buffer(&self.camera_buf, 0, bytemuck::bytes_of(&cam));
let counts: Vec<usize> = scenes.iter().map(|s| s.visible_chunks().len()).collect();
self.prepared = slot_ranges(&counts);
let chunks_total: usize = counts.iter().sum();
self.prepared_slots = (chunks_total..chunks_total + slot_scenes.len()).collect();
let total = chunks_total + slot_scenes.len();
if total > self.chunk_capacity {
self.chunk_capacity = total.next_power_of_two();
(self.chunk_buf, self.chunk_bg) = Self::chunk_slots(
device,
&self.chunk_layout,
self.chunk_stride,
self.chunk_capacity,
);
}
let mut slots = vec![0u8; self.chunk_stride as usize * total];
let chunks = scenes.iter().flat_map(|s| s.visible_chunks());
for (i, c) in chunks.enumerate() {
let h = &c.header;
let min = glam::DVec3::from(h.bbox_min.map(f64::from));
let size = glam::DVec3::from(h.bbox_max.map(f64::from)) - min;
let d = c.origin + min - camera.eye;
let s = size / 65535.0;
let u = ChunkUniform {
offset: [d.x as f32, d.y as f32, d.z as f32, 0.0],
scale: [s.x as f32, s.y as f32, s.z as f32, 0.0],
};
let at = i * self.chunk_stride as usize;
slots[at..at + size_of::<ChunkUniform>()].copy_from_slice(bytemuck::bytes_of(&u));
}
for (k, s) in slot_scenes.iter().enumerate() {
let d = s.origin() - camera.eye;
let u = ChunkUniform {
offset: [d.x as f32, d.y as f32, d.z as f32, 0.0],
scale: [1.0, 1.0, 1.0, 0.0],
};
let at = (chunks_total + k) * self.chunk_stride as usize;
slots[at..at + size_of::<ChunkUniform>()].copy_from_slice(bytemuck::bytes_of(&u));
}
if !slots.is_empty() {
queue.write_buffer(&self.chunk_buf, 0, &slots);
}
}
pub fn set_point_size(&mut self, size: PointSize) {
match size {
PointSize::Pixels(px) => {
self.point_px = px;
self.point_m = 0.0;
}
PointSize::Metres(m) => self.point_m = m.max(0.0),
}
}
#[allow(clippy::too_many_arguments)]
pub fn prepare(
&mut self,
device: &wgpu::Device,
queue: &wgpu::Queue,
size: [u32; 2],
camera: &Camera,
scenes: &[&Scene],
fog: Option<crate::layers::Fog>,
) {
self.write_frame(device, queue, size, camera, scenes, &[], fog);
}
#[allow(clippy::too_many_arguments)]
pub fn prepare_with_slots(
&mut self,
device: &wgpu::Device,
queue: &wgpu::Queue,
size: [u32; 2],
camera: &Camera,
scenes: &[&Scene],
slots: &[&SlotScene],
fog: Option<crate::layers::Fog>,
) {
self.write_frame(device, queue, size, camera, scenes, slots, fog);
}
pub fn draw_slots(
&self,
pass: &mut wgpu::RenderPass<'_>,
slots: &[&SlotScene],
alpha: Option<&[f32]>,
) {
pass.set_bind_group(0, &self.camera_bg, &[]);
for (k, s) in slots.iter().enumerate() {
let Some(&slot) = self.prepared_slots.get(k) else {
break;
};
let Some(buffer) = s.buffer().filter(|_| s.capacity() > 0) else {
continue;
};
let faded = match fade(alpha, k) {
Fade::Skip => continue,
Fade::Opaque => false,
Fade::Blend(a) => {
let a = f64::from(a);
pass.set_blend_constant(wgpu::Color {
r: a,
g: a,
b: a,
a,
});
true
}
};
pass.set_pipeline(&self.pipelines[pipeline_index(2, faded)]);
let at = slot as u64 * self.chunk_stride;
pass.set_bind_group(1, &self.chunk_bg, &[at as u32]);
let n = s.capacity();
pass.set_vertex_buffer(0, buffer.slice(..u64::from(n) * SLOT_BYTES as u64));
pass.draw(0..4, 0..n);
}
}
pub fn draw(&self, pass: &mut wgpu::RenderPass<'_>, scenes: &[&Scene], alpha: Option<&[f32]>) {
pass.set_bind_group(0, &self.camera_bg, &[]);
let mut bound = None;
for (s, scene) in scenes.iter().enumerate() {
let Some(&(first, count)) = self.prepared.get(s) else {
break;
};
let faded = match fade(alpha, s) {
Fade::Skip => continue,
Fade::Opaque => false,
Fade::Blend(a) => {
let a = f64::from(a);
pass.set_blend_constant(wgpu::Color {
r: a,
g: a,
b: a,
a,
});
true
}
};
for (i, c) in scene.visible_chunks().iter().take(count).enumerate() {
let p = pipeline_index(usize::from(c.header.stride != 12), faded);
if bound != Some(p) {
pass.set_pipeline(&self.pipelines[p]);
bound = Some(p);
}
let slot = (first + i) as u64 * self.chunk_stride;
pass.set_bind_group(1, &self.chunk_bg, &[slot as u32]);
pass.set_vertex_buffer(0, c.buffer.slice(..));
pass.draw(0..4, 0..c.points);
}
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum PointSize {
Pixels(f32),
Metres(f32),
}
fn focal_px(fov_y_deg: f64, height: u32) -> f64 {
f64::from(height) / 2.0 / (fov_y_deg.to_radians() / 2.0).tan()
}
#[cfg_attr(not(test), allow(dead_code))]
fn diameter_px(size: PointSize, focal_px: f32, depth: f32) -> f32 {
match size {
PointSize::Pixels(px) => px,
PointSize::Metres(m) if m > 0.0 => (m * focal_px / depth.max(1e-6)).clamp(1.0, 512.0),
PointSize::Metres(_) => 0.0,
}
}
fn slot_ranges(counts: &[usize]) -> Vec<(usize, usize)> {
let mut first = 0;
counts
.iter()
.map(|&n| {
let r = (first, n);
first += n;
r
})
.collect()
}
#[derive(Debug, Clone, Copy, PartialEq)]
enum Fade {
Skip,
Opaque,
Blend(f32),
}
fn fade(alpha: Option<&[f32]>, scene: usize) -> Fade {
match alpha.and_then(|a| a.get(scene)).copied() {
None => Fade::Opaque,
Some(a) if a >= 1.0 => Fade::Opaque,
Some(a) if a > 0.0 => Fade::Blend(a),
Some(_) => Fade::Skip,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn camera_uniform_layout_matches_the_wgsl_struct() {
assert_eq!(size_of::<CameraUniform>(), 112);
assert_eq!(size_of::<ChunkUniform>(), 32);
}
#[test]
fn focal_length_matches_the_projection() {
assert!((focal_px(90.0, 1000) - 500.0).abs() < 1e-9);
let cam = Camera::new([0.0, -10.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 1.0], 50.0);
let a = cam.project_px([0.0, 0.0, 0.0], 800, 600).unwrap();
let b = cam.project_px([1.0, 0.0, 0.0], 800, 600).unwrap();
assert!((b[0] - a[0] - focal_px(50.0, 600) / 10.0).abs() < 1e-6);
}
#[test]
fn point_sizes_in_pixels_and_metres() {
assert_eq!(diameter_px(PointSize::Pixels(2.0), 500.0, 1000.0), 2.0);
assert_eq!(diameter_px(PointSize::Metres(0.5), 500.0, 10.0), 25.0);
assert_eq!(diameter_px(PointSize::Metres(0.5), 500.0, 20.0), 12.5);
assert_eq!(diameter_px(PointSize::Metres(0.5), 500.0, 1e6), 1.0);
assert_eq!(diameter_px(PointSize::Metres(0.5), 500.0, 1e-3), 512.0);
}
#[test]
fn scenes_get_back_to_back_slot_ranges() {
assert_eq!(slot_ranges(&[3, 0, 2]), vec![(0, 3), (3, 0), (3, 2)]);
assert!(slot_ranges(&[]).is_empty());
}
#[test]
fn per_scene_alpha_picks_opaque_blend_or_skip() {
assert_eq!(fade(None, 0), Fade::Opaque);
let a = [1.0, 0.25, 0.0, f32::NAN, 2.0];
assert_eq!(fade(Some(&a), 0), Fade::Opaque);
assert_eq!(fade(Some(&a), 1), Fade::Blend(0.25));
assert_eq!(fade(Some(&a), 2), Fade::Skip);
assert_eq!(fade(Some(&a), 3), Fade::Skip);
assert_eq!(fade(Some(&a), 4), Fade::Opaque);
assert_eq!(fade(Some(&a), 9), Fade::Opaque);
}
}