mod scatter;
use glam::DVec3;
use pointblitz_io::slots::{self, ADD_BYTES, Diff, MOVE_BYTES, SlotError};
use scatter::Scatter;
use std::collections::BTreeMap;
pub const SLOT_BYTES: usize = 16;
pub const FLAG_SHOWN: u8 = 1;
const MIN_SLOTS: u32 = 1024;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum UploadMethod {
#[default]
Mirror,
Scatter,
}
impl UploadMethod {
pub fn as_str(self) -> &'static str {
match self {
Self::Mirror => "mirror",
Self::Scatter => "scatter",
}
}
pub fn supported(self, device: &wgpu::Device) -> bool {
match self {
Self::Mirror => true,
Self::Scatter => Scatter::supported(device),
}
}
}
impl std::str::FromStr for UploadMethod {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"mirror" | "a" => Ok(Self::Mirror),
"scatter" | "b" => Ok(Self::Scatter),
_ => Err(format!("upload method {s:?}: mirror|scatter")),
}
}
}
impl std::fmt::Display for UploadMethod {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Applied {
Shown(Vec<u32>),
Held,
Stale,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ApplyError {
Message(SlotError),
TooManySlots {
capacity: u32,
max: u64,
},
}
impl From<SlotError> for ApplyError {
fn from(e: SlotError) -> Self {
Self::Message(e)
}
}
impl std::fmt::Display for ApplyError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Message(e) => e.fmt(f),
Self::TooManySlots { capacity, max } => {
write!(f, "{capacity} slots, the device holds at most {max}")
}
}
}
}
impl std::error::Error for ApplyError {}
pub struct SlotScene {
origin: DVec3,
requested: UploadMethod,
method: UploadMethod,
mirror: Vec<u8>,
shown: Vec<u64>,
extent: Option<([f32; 3], [f32; 3])>,
scatter: Option<Scatter>,
scatter_cap: Option<(u64, u32)>,
buffer: Option<wgpu::Buffer>,
allocated: u32,
capacity: u32,
points: u64,
applied: Option<u32>,
next: u32,
held: BTreeMap<u32, Vec<u8>>,
last_upload: u64,
}
impl SlotScene {
pub fn new(origin: [f64; 3]) -> Self {
Self {
origin: DVec3::from(origin),
requested: UploadMethod::Mirror,
method: UploadMethod::Mirror,
mirror: Vec::new(),
shown: Vec::new(),
extent: None,
scatter: None,
scatter_cap: None,
buffer: None,
allocated: 0,
capacity: 0,
points: 0,
applied: None,
next: 1,
held: BTreeMap::new(),
last_upload: 0,
}
}
pub fn with_upload(mut self, method: UploadMethod) -> Self {
self.set_upload(method);
self
}
pub fn set_upload(&mut self, method: UploadMethod) {
self.requested = method;
if self.buffer.is_none() {
self.method = method;
}
}
pub fn upload(&self) -> UploadMethod {
self.method
}
#[doc(hidden)]
pub fn with_scatter_limits(mut self, binding_bytes: u64, groups: u32) -> Self {
self.scatter_cap = Some((binding_bytes, groups));
self.scatter = None;
self
}
pub fn origin(&self) -> DVec3 {
self.origin
}
pub fn capacity(&self) -> u32 {
self.capacity
}
pub fn points(&self) -> u64 {
self.points
}
pub fn seq(&self) -> Option<u32> {
self.applied
}
pub fn held(&self) -> usize {
self.held.len()
}
pub fn gpu_bytes(&self) -> u64 {
self.buffer.as_ref().map_or(0, wgpu::Buffer::size)
}
pub fn last_upload_bytes(&self) -> u64 {
self.last_upload
}
pub(crate) fn buffer(&self) -> Option<&wgpu::Buffer> {
self.buffer.as_ref()
}
pub fn clear(&mut self) {
let mut fresh = Self::new(self.origin.to_array()).with_upload(self.requested);
fresh.scatter = self.scatter.take();
fresh.scatter_cap = self.scatter_cap;
*self = fresh;
}
pub fn expect(&mut self, seq: u32) {
self.next = seq;
self.held.retain(|s, _| *s > seq);
}
pub fn apply(
&mut self,
device: &wgpu::Device,
queue: &wgpu::Queue,
msg: &[u8],
) -> Result<Applied, ApplyError> {
let header = slots::validate(msg)?;
let max = device.limits().max_buffer_size / SLOT_BYTES as u64;
if u64::from(header.capacity) > max {
return Err(ApplyError::TooManySlots {
capacity: header.capacity,
max,
});
}
if header.seq < self.next {
return Ok(Applied::Stale);
}
if header.seq > self.next {
self.held.insert(header.seq, msg.to_vec());
return Ok(Applied::Held);
}
Ok(self.apply_in_order(device, queue, msg, header.seq, max))
}
pub fn apply_state(
&mut self,
device: &wgpu::Device,
queue: &wgpu::Queue,
msg: &[u8],
) -> Result<Applied, ApplyError> {
let header = slots::validate(msg)?;
let max = device.limits().max_buffer_size / SLOT_BYTES as u64;
if u64::from(header.capacity) > max {
return Err(ApplyError::TooManySlots {
capacity: header.capacity,
max,
});
}
let held = std::mem::take(&mut self.held);
self.clear();
self.held = held;
self.expect(header.seq);
Ok(self.apply_in_order(device, queue, msg, header.seq, max))
}
fn apply_in_order(
&mut self,
device: &wgpu::Device,
queue: &wgpu::Queue,
msg: &[u8],
seq: u32,
max: u64,
) -> Applied {
let mut capacity = self
.capacity
.max(slots::decode_header(msg).map_or(0, |h| h.capacity));
let mut next = seq.checked_add(1);
while let Some(m) = next.and_then(|n| self.held.get(&n)) {
capacity = capacity.max(slots::decode_header(m).map_or(0, |h| h.capacity));
next = next.and_then(|n| n.checked_add(1));
}
if self.buffer.is_none() {
self.method = if self.requested.supported(device) {
self.requested
} else {
UploadMethod::Mirror
};
}
self.grow(capacity);
self.reserve(device, queue, max);
self.last_upload = 0;
let mut dirty: Option<(u32, u32)> = None;
self.take(device, queue, msg, &mut dirty);
let mut shown = vec![seq];
while let Some(m) = self.held.remove(&self.next) {
self.take(device, queue, &m, &mut dirty);
shown.push(self.applied.expect("set by take"));
}
if let (Some((lo, hi)), Some(buffer)) = (dirty, &self.buffer) {
let range = lo as usize * SLOT_BYTES..(hi as usize + 1) * SLOT_BYTES;
self.last_upload = range.len() as u64;
queue.write_buffer(buffer, range.start as u64, &self.mirror[range]);
}
Applied::Shown(shown)
}
fn grow(&mut self, capacity: u32) {
if capacity > self.capacity {
self.capacity = capacity;
match self.method {
UploadMethod::Mirror => {
let len = capacity as usize * SLOT_BYTES;
self.mirror.resize(len, 0);
}
UploadMethod::Scatter => self.shown.resize(capacity.div_ceil(64) as usize, 0),
}
}
}
fn take(
&mut self,
device: &wgpu::Device,
queue: &wgpu::Queue,
msg: &[u8],
dirty: &mut Option<(u32, u32)>,
) {
let d = Diff::parse(msg).expect("validated before");
match self.method {
UploadMethod::Mirror => self.write_mirror(&d, dirty),
UploadMethod::Scatter => {
self.mark(&d);
let Some(buffer) = self.buffer.as_ref() else {
self.applied = Some(d.header.seq);
self.next = d.header.seq + 1;
return;
};
let cap = self.scatter_cap;
let scatter = self
.scatter
.get_or_insert_with(|| Scatter::new(device, cap));
self.last_upload +=
scatter.apply(device, queue, buffer, self.capacity, &d.header, msg);
}
}
self.applied = Some(d.header.seq);
self.next = d.header.seq + 1;
}
fn write_mirror(&mut self, d: &Diff<'_>, dirty: &mut Option<(u32, u32)>) {
let mut touch = |id: u32| {
*dirty = Some(dirty.map_or((id, id), |(lo, hi)| (lo.min(id), hi.max(id))));
};
let mut points = self.points;
let adds = d.adds.as_chunks::<ADD_BYTES>().0;
let moves = d.moves.as_chunks::<MOVE_BYTES>().0;
for rec in adds.iter().chain(moves) {
let id = u32::from_le_bytes(rec[..4].try_into().unwrap());
touch(id);
let s = &mut self.mirror[id as usize * SLOT_BYTES..][..SLOT_BYTES];
s[..15].copy_from_slice(&rec[4..19]);
points += u64::from(s[15] & FLAG_SHOWN == 0);
s[15] |= FLAG_SHOWN;
}
for id in d.remove_ids() {
touch(id);
let flags = &mut self.mirror[id as usize * SLOT_BYTES + 15];
points -= u64::from(*flags & FLAG_SHOWN != 0);
*flags &= !FLAG_SHOWN;
}
self.points = points;
}
fn mark(&mut self, d: &Diff<'_>) {
let mut points = self.points;
let (mut lo, mut hi) = self
.extent
.unwrap_or(([f32::INFINITY; 3], [f32::NEG_INFINITY; 3]));
let adds = d.adds.as_chunks::<ADD_BYTES>().0;
let moves = d.moves.as_chunks::<MOVE_BYTES>().0;
for rec in adds.iter().chain(moves) {
let id = u32::from_le_bytes(rec[..4].try_into().unwrap()) as usize;
let (word, bit) = (&mut self.shown[id / 64], 1u64 << (id % 64));
points += u64::from(*word & bit == 0);
*word |= bit;
for a in 0..3 {
let v = f32::from_le_bytes(rec[4 + 4 * a..8 + 4 * a].try_into().unwrap());
lo[a] = lo[a].min(v);
hi[a] = hi[a].max(v);
}
}
for id in d.remove_ids() {
let id = id as usize;
let (word, bit) = (&mut self.shown[id / 64], 1u64 << (id % 64));
points -= u64::from(*word & bit != 0);
*word &= !bit;
}
if !adds.is_empty() || !moves.is_empty() {
self.extent = Some((lo, hi));
}
self.points = points;
}
fn reserve(&mut self, device: &wgpu::Device, queue: &wgpu::Queue, max: u64) {
if self.capacity <= self.allocated {
return;
}
let want = u64::from(
self.capacity
.max(self.allocated.saturating_mul(2))
.max(MIN_SLOTS),
)
.min(max)
.max(u64::from(self.capacity)) as u32;
let mut usage = wgpu::BufferUsages::VERTEX
| wgpu::BufferUsages::COPY_DST
| wgpu::BufferUsages::COPY_SRC;
if self.method == UploadMethod::Scatter {
usage |= wgpu::BufferUsages::STORAGE;
}
let buffer = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("slots"),
size: u64::from(want) * SLOT_BYTES as u64,
usage,
mapped_at_creation: false,
});
if let Some(old) = &self.buffer {
let mut enc = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("slots grow"),
});
enc.copy_buffer_to_buffer(old, 0, &buffer, 0, old.size());
queue.submit([enc.finish()]);
}
self.buffer = Some(buffer);
self.allocated = want;
}
#[cfg(not(target_arch = "wasm32"))]
pub fn read_slots(&self, device: &wgpu::Device, queue: &wgpu::Queue) -> Vec<u8> {
let len = u64::from(self.capacity) * SLOT_BYTES as u64;
let Some(buffer) = self.buffer.as_ref().filter(|_| len > 0) else {
return Vec::new();
};
let readback = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("slots readback"),
size: len,
usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ,
mapped_at_creation: false,
});
let mut enc = device.create_command_encoder(&Default::default());
enc.copy_buffer_to_buffer(buffer, 0, &readback, 0, len);
let index = queue.submit([enc.finish()]);
readback.map_async(wgpu::MapMode::Read, .., |r| r.expect("map slots"));
device
.poll(wgpu::PollType::Wait {
submission_index: Some(index),
timeout: None,
})
.expect("GPU wait");
let out = readback.get_mapped_range(..).expect("mapped").to_vec();
readback.unmap();
out
}
pub fn bounds(&self) -> Option<([f64; 3], [f64; 3])> {
if self.points == 0 {
return None;
}
let (min, max) = match self.method {
UploadMethod::Scatter => {
let (lo, hi) = self.extent?;
(lo.map(f64::from), hi.map(f64::from))
}
UploadMethod::Mirror => {
let mut min = [f64::INFINITY; 3];
let mut max = [f64::NEG_INFINITY; 3];
for s in self.mirror.as_chunks::<SLOT_BYTES>().0 {
if s[15] & FLAG_SHOWN == 0 {
continue;
}
for a in 0..3 {
let v =
f64::from(f32::from_le_bytes(s[4 * a..4 * a + 4].try_into().unwrap()));
min[a] = min[a].min(v);
max[a] = max[a].max(v);
}
}
(min, max)
}
};
let o = self.origin.to_array();
Some((
[0, 1, 2].map(|a| o[a] + min[a]),
[0, 1, 2].map(|a| o[a] + max[a]),
))
}
}