From d2efb37de9b2796869c340f75f7550170738040d Mon Sep 17 00:00:00 2001 From: SamG-Coder <13449963+SamG-Coder@users.noreply.github.com> Date: Sat, 14 Nov 2026 07:47:21 +1100 Subject: [PATCH] Pack pages tighter: 12-byte vertices, 3-byte triangles, filtered and fuller pages A page pool of fixed slots holds as many clusters as fit in a slot. This makes a cluster smaller and the slots fuller, as scene file version 5: - A vertex is 12 bytes, not 16: the padding goes, and the normal is octahedral in sixteen bits, the nearest of the four codes around the exact point. Positions and UVs keep every bit. - A triangle is 3 bytes, end to end within its cluster. A mesh being built keeps a word for each; pages are packed when they are made. - A cluster's vertex offset counts words of the pool, as its triangle offset did. Shaders read both through common/pool.wgsl. - Skins lie end to end behind their cluster's vertices. - A page's vertices are filtered before it is deflated, each field as its difference from the vertex before and each byte in a run of its own. Hosts that read a file hand pages over with Renderer::deliver_fetched, which undoes it. - Room left in a page is filled from the next few sets of the same mesh and round. On a 31-million-triangle garden scene: pages 1,295 MiB to 971 unpacked and 787 to 500 stored, 47.9 clusters to a slot to 67.4, the same frame times. docs/streaming.md has the measurements, and what was tried and left out. `cargo run -p solstice-asset --example page_stats -- FILE` measures a bake. A version 4 file no longer opens: scenes must be baked again. Co-Authored-By: Claude Fable 5.2 --- apps/viewer/src/main.rs | 2 +- apps/viewer/src/web.rs | 4 +- crates/solstice-asset/examples/page_stats.rs | 136 ++++++++ crates/solstice-asset/src/file.rs | 125 ++++++- crates/solstice-asset/src/gpu.rs | 194 ++++++++++- crates/solstice-asset/src/paged.rs | 25 +- crates/solstice-builder/src/dag.rs | 59 ++-- crates/solstice-builder/src/paging.rs | 323 +++++++++++++----- crates/solstice-import/src/character_tests.rs | 16 +- crates/solstice-import/src/streamed.rs | 15 +- .../solstice-render/examples/page_stress.rs | 17 +- .../solstice-render/shaders/common/pool.wgsl | 30 ++ .../solstice-render/shaders/common/scene.wgsl | 12 +- .../shaders/common/skin_vertex.wgsl | 12 +- .../solstice-render/shaders/gi/voxelize.wgsl | 29 +- .../shaders/vg/raster_common.wgsl | 10 +- .../solstice-render/shaders/vg/raster_hw.wgsl | 4 +- .../shaders/vg/raster_hw_masked.wgsl | 4 +- .../shaders/vg/raster_sw_body.wgsl | 4 +- .../solstice-render/shaders/vg/resolve.wgsl | 19 +- crates/solstice-render/src/regions.rs | 26 +- crates/solstice-render/src/renderer.rs | 13 + crates/solstice-render/src/shaders.rs | 1 + crates/solstice-render/src/streaming/mod.rs | 55 +-- docs/gameplay.md | 7 +- docs/performance-audit.md | 2 + docs/streaming.md | 94 ++++- docs/weather-effects.md | 2 +- 28 files changed, 965 insertions(+), 275 deletions(-) create mode 100644 crates/solstice-asset/examples/page_stats.rs create mode 100644 crates/solstice-render/shaders/common/pool.wgsl diff --git a/apps/viewer/src/main.rs b/apps/viewer/src/main.rs index 909cd65..436766c 100644 --- a/apps/viewer/src/main.rs +++ b/apps/viewer/src/main.rs @@ -92,7 +92,7 @@ mod native { self.running -= 1; match data { Some(data) => { - renderer.deliver(id, data); + renderer.deliver_fetched(id, data); } None => renderer.request_failed(id), } diff --git a/apps/viewer/src/web.rs b/apps/viewer/src/web.rs index d0217b8..affbf71 100644 --- a/apps/viewer/src/web.rs +++ b/apps/viewer/src/web.rs @@ -242,7 +242,7 @@ impl WebViewer { /// Takes a page of such a scene, inflated, with its cluster records. pub fn deliver_page(&mut self, id: u32, bytes: Vec, clusters: Vec) -> bool { - self.renderer.deliver_page(id, bytes, clusters) + self.renderer.deliver_fetched_page(id, bytes, clusters) } /// Call when all of such a scene's tables have been handed over. @@ -273,7 +273,7 @@ impl WebViewer { /// Takes the bytes of a request, inflated if it needed that. pub fn deliver(&mut self, id: u32, bytes: Vec) -> bool { - self.renderer.deliver(id, bytes) + self.renderer.deliver_fetched(id, bytes) } /// Reports that a request could not be fetched. diff --git a/crates/solstice-asset/examples/page_stats.rs b/crates/solstice-asset/examples/page_stats.rs new file mode 100644 index 0000000..48b9efc --- /dev/null +++ b/crates/solstice-asset/examples/page_stats.rs @@ -0,0 +1,136 @@ +//! Measures how a baked scene's pages use their room: `page_stats FILE [SAMPLE_EVERY]`. +//! +//! It reads the manifest for the sizes of every page, and unpacks every Nth page to see what +//! its clusters hold. `docs/streaming.md` has what this found for one large scene, under +//! "Tighter pages". + +use solstice_asset::file::{ + decode_header, decode_manifest, inflate, page_range, unpack_page, HEADER_BYTES, +}; +use solstice_asset::gpu::{triangle_at, triangle_words, VERTEX_WORDS}; +use solstice_asset::paged::PAGE_BYTES; +use solstice_asset::PackedVertex; +use std::fs::File; +use std::io::{Read, Seek, SeekFrom}; + +const MIB: f64 = 1_048_576.0; + +fn read_at(file: &mut File, start: u64, len: usize) -> Vec { + let mut bytes = vec![0; len]; + file.seek(SeekFrom::Start(start)).expect("seek"); + file.read_exact(&mut bytes).expect("read"); + bytes +} + +fn main() { + let mut args = std::env::args().skip(1); + let path = args.next().expect("page_stats FILE [SAMPLE_EVERY]"); + let every: usize = args.next().map_or(16, |n| n.parse().expect("a number")); + let mut file = File::open(&path).expect("open"); + let header = decode_header(&read_at(&mut file, 0, HEADER_BYTES)).expect("header"); + let manifest = read_at( + &mut file, + header.manifest_offset, + header.manifest_bytes as usize, + ); + let manifest = inflate(&manifest).expect("inflate"); + let scene = decode_manifest(&manifest, header.manifest_offset + header.manifest_bytes) + .expect("manifest"); + + let pages = scene.pages.len(); + let pinned = scene.pinned_pages as usize; + let streamed = &scene.pages[pinned..]; + let raw: u64 = scene.pages.iter().map(|p| p.raw_bytes() as u64).sum(); + let stored: u64 = scene.pages.iter().map(|p| u64::from(p.stored_bytes)).sum(); + let vertex_bytes: u64 = scene.pages.iter().map(|p| p.vertex_bytes() as u64).sum(); + let streamed_raw: u64 = streamed.iter().map(|p| p.raw_bytes() as u64).sum(); + let mut fill = [0u32; 10]; + for page in streamed { + fill[(page.raw_bytes() * 10 / (PAGE_BYTES + 1)).min(9)] += 1; + } + let clusters = scene.cluster_count(); + println!("clusters {clusters}"); + println!("sets {}", scene.sets.len()); + println!("pages {pages} ({pinned} pinned)"); + println!("page bytes, raw {:.1} MiB", raw as f64 / MIB); + println!("page bytes, stored {:.1} MiB", stored as f64 / MIB); + println!( + "vertices {:.1} MiB ({:.0}%)", + vertex_bytes as f64 / MIB, + vertex_bytes as f64 * 100.0 / raw as f64 + ); + println!( + "triangles {:.1} MiB", + (raw - vertex_bytes) as f64 / MIB + ); + println!( + "bytes a cluster {:.0} raw, {:.0} of pool with slots as full as they are", + raw as f64 / clusters as f64, + (pages * PAGE_BYTES) as f64 / clusters as f64 + ); + println!( + "slots full to {:.1}% of {} KiB", + streamed_raw as f64 * 100.0 / (streamed.len() * PAGE_BYTES) as f64, + PAGE_BYTES / 1024 + ); + println!("pages by tenths full {fill:?}"); + println!( + "clusters a page {:.1}", + (pinned..pages) + .map(|p| u64::from(scene.page_cluster_count(p as u32))) + .sum::() as f64 + / streamed.len() as f64 + ); + + // Sampled pages: every cluster is read back, which checks the page against its tables. + let (mut sampled, mut vertices, mut triangles) = (0u64, 0u64, 0u64); + for page in (pinned..pages).step_by(every) { + let (start, end) = page_range(&scene, page as u32); + let packed = read_at(&mut file, start, (end - start) as usize); + let data = unpack_page(&scene, page as u32, &packed).expect("page"); + let info = scene.pages[page]; + let (vertex_part, triangle_part) = data.split_at(info.vertex_bytes()); + let records: &[PackedVertex] = bytemuck::cast_slice(vertex_part); + let words: Vec = triangle_part + .chunks_exact(4) + .map(|w| u32::from_le_bytes([w[0], w[1], w[2], w[3]])) + .collect(); + for set in &scene.sets[info.first_set as usize..(info.first_set + info.set_count) as usize] + { + for c in set.first_cluster..set.first_cluster + set.cluster_count { + let cluster = scene.clusters[c as usize]; + let count = cluster.vertex_count(); + let first = cluster.vertex_offset / VERTEX_WORDS; + assert!( + (first + count) as usize <= records.len(), + "vertices out of the page" + ); + let triangle_count = cluster.triangle_count(); + let own = &words[cluster.triangle_offset as usize..] + [..triangle_words(triangle_count) as usize]; + for index in 0..triangle_count { + let packed = triangle_at(own, index); + assert!( + [packed & 0xff, (packed >> 8) & 0xff, packed >> 16] + .iter() + .all(|&corner| corner < count), + "a triangle names a vertex its cluster doesn't have" + ); + } + sampled += 1; + vertices += u64::from(count); + triangles += u64::from(triangle_count); + } + } + } + println!("--- every {every}th streamed page, each cluster read back ---"); + println!("clusters {sampled}"); + println!( + "vertices a cluster {:.1}", + vertices as f64 / sampled as f64 + ); + println!( + "triangles a cluster {:.1}", + triangles as f64 / sampled as f64 + ); +} diff --git a/crates/solstice-asset/src/file.rs b/crates/solstice-asset/src/file.rs index b1e3634..e8ff89b 100644 --- a/crates/solstice-asset/src/file.rs +++ b/crates/solstice-asset/src/file.rs @@ -30,7 +30,7 @@ use glam::{Mat4, Quat, Vec3}; use std::fmt; const MAGIC: [u8; 8] = *b"SOLSTICE"; -const VERSION: u32 = 4; +const VERSION: u32 = 5; /// How hard to compress when baking. Unpacking takes the same time at any level. const DEFLATE_LEVEL: u8 = 9; /// The optional section holding the scene's [`Sea`]: its level, then its ocean's text. @@ -174,7 +174,7 @@ pub fn encode(loaded: &LoadedScene) -> Vec { let mut scene = loaded.scene.clone(); let mut data = Vec::new(); for (info, raw) in scene.pages[pinned..].iter_mut().zip(&loaded.pages) { - let packed = miniz_oxide::deflate::compress_to_vec(raw, DEFLATE_LEVEL); + let packed = deflate_page(raw, info); info.offset = data.len() as u64; info.stored_bytes = packed.len() as u32; data.extend_from_slice(&packed); @@ -213,9 +213,71 @@ pub fn cluster_bytes(clusters: &[PackedCluster]) -> &[u8] { bytemuck::cast_slice(clusters) } -/// Deflates a page as [`encode`] does, for writers that lay out the data area themselves. -pub fn deflate_page(raw: &[u8]) -> Vec { - miniz_oxide::deflate::compress_to_vec(raw, DEFLATE_LEVEL) +/// Packs a page as [`encode`] does, for writers that lay out the data area themselves: +/// filtered, see [`filter_page`], and deflated. +pub fn deflate_page(raw: &[u8], info: &PageInfo) -> Vec { + miniz_oxide::deflate::compress_to_vec(&filter_page(raw, info), DEFLATE_LEVEL) +} + +/// The `u16` fields of a vertex record. +const VERTEX_FIELDS: usize = std::mem::size_of::() / 2; + +/// Rewrites a page's vertices so that they deflate smaller, to the same size and with nothing +/// lost: [`unfilter_page`] undoes it. The pages of a large garden scene stored a fifth smaller +/// for it, see `docs/streaming.md`. +/// +/// Each field of a vertex becomes its difference from the same field of the vertex before, +/// folded so that a small step either way is a small number, and each byte of each field goes +/// in a run of its own. Neighbouring vertices are close together, so the high bytes are mostly +/// nought and the low ones small. Triangles are left alone: they pack no better for it. +pub fn filter_page(raw: &[u8], info: &PageInfo) -> Vec { + let count = info.vertex_count as usize; + let vertex_bytes = info.vertex_bytes().min(raw.len()); + let mut out = raw.to_vec(); + if vertex_bytes != count * 2 * VERTEX_FIELDS { + return out; + } + let mut before = [0u16; VERTEX_FIELDS]; + for (index, vertex) in raw[..vertex_bytes] + .chunks_exact(2 * VERTEX_FIELDS) + .enumerate() + { + for field in 0..VERTEX_FIELDS { + let value = u16::from_le_bytes([vertex[field * 2], vertex[field * 2 + 1]]); + let step = value.wrapping_sub(before[field]); + let folded = (step << 1) ^ (((step as i16) >> 15) as u16); + out[field * 2 * count + index] = folded as u8; + out[(field * 2 + 1) * count + index] = (folded >> 8) as u8; + before[field] = value; + } + } + out +} + +/// Undoes [`filter_page`]: the page as the GPU reads it, from the page as a file stores it once +/// inflated. +pub fn unfilter_page(stored: &[u8], info: &PageInfo) -> Vec { + let count = info.vertex_count as usize; + let vertex_bytes = info.vertex_bytes().min(stored.len()); + let mut out = stored.to_vec(); + if vertex_bytes != count * 2 * VERTEX_FIELDS { + return out; + } + let mut before = [0u16; VERTEX_FIELDS]; + for (index, vertex) in out[..vertex_bytes] + .chunks_exact_mut(2 * VERTEX_FIELDS) + .enumerate() + { + for field in 0..VERTEX_FIELDS { + let folded = u16::from(stored[field * 2 * count + index]) + | u16::from(stored[(field * 2 + 1) * count + index]) << 8; + let step = (folded >> 1) ^ (folded & 1).wrapping_neg(); + let value = before[field].wrapping_add(step); + vertex[field * 2..field * 2 + 2].copy_from_slice(&value.to_le_bytes()); + before[field] = value; + } + } + out } /// The start of a file: its header and manifest, for a writer that has placed every page and @@ -1145,11 +1207,12 @@ pub fn inflate_limited(packed: &[u8], limit: u64) -> Result, DecodeError /// Unpacks a page and checks that it is the size the tables say. pub fn unpack_page(scene: &PagedScene, page: u32, packed: &[u8]) -> Result, DecodeError> { - let raw = inflate(packed)?; - if raw.len() != scene.pages[page as usize].raw_bytes() { + let info = &scene.pages[page as usize]; + let stored = inflate(packed)?; + if stored.len() != info.raw_bytes() { return Err(DecodeError::BadPages); } - Ok(raw) + Ok(unfilter_page(&stored, info)) } /// Checks everything the renderer would otherwise index blindly while installing pages. A bad @@ -1282,7 +1345,7 @@ mod tests { fn sample_scene() -> LoadedScene { let page_bytes = |vertex: u8| { let vertices = [PackedVertex { - data: [u16::from(vertex); 8], + data: [u16::from(vertex); 6], }; 3]; let mut bytes: Vec = bytemuck::cast_slice(&vertices).to_vec(); bytes.extend_from_slice(bytemuck::bytes_of(&pack_triangle(0, 1, 2))); @@ -1290,7 +1353,7 @@ mod tests { }; let info = PageInfo { vertex_count: 3, - triangle_count: 1, + triangle_words: 1, set_count: 1, ..Default::default() }; @@ -1479,10 +1542,12 @@ mod tests { } #[test] - fn a_version_4_file_with_neither_section_still_loads() { + fn a_file_with_neither_section_still_loads() { let loaded = sample_scene(); let bytes = encode(&loaded); - assert_eq!(bytes[8..12], 4u32.to_le_bytes(), "the version hasn't moved"); + // Version 5 is version 4 with pages laid out more tightly: optional sections came and + // went without moving it. + assert_eq!(bytes[8..12], 5u32.to_le_bytes(), "the version hasn't moved"); let manifest = encode_manifest(&loaded.scene); assert!(!manifest .windows(4) @@ -2061,6 +2126,42 @@ mod tests { assert!(decode_manifest(&manifest, 0).is_err()); } + #[test] + fn filtering_a_page_loses_nothing_and_packs_smaller() { + // A strip of vertices a step apart, as neighbours in a mesh are, then some triangles. + let vertices: Vec = (0..600u16) + .map(|i| PackedVertex { + data: [ + 1000 + i * 3, + 40_000 - i * 5, + i.wrapping_mul(977) % 64, + i.wrapping_mul(31_337), + i * 100, + 65_535 - i * 7, + ], + }) + .collect(); + let mut raw: Vec = bytemuck::cast_slice(&vertices).to_vec(); + raw.extend((0..300u32).flat_map(|i| i.wrapping_mul(0x0101_0101).to_le_bytes())); + let info = PageInfo { + vertex_count: vertices.len() as u32, + triangle_words: 300, + ..Default::default() + }; + assert_eq!(raw.len(), info.raw_bytes()); + let stored = filter_page(&raw, &info); + assert_eq!(stored.len(), raw.len()); + assert_ne!(stored, raw); + assert_eq!(unfilter_page(&stored, &info), raw); + assert!( + deflate_page(&raw, &info).len() * 2 + < miniz_oxide::deflate::compress_to_vec(&raw, DEFLATE_LEVEL).len() + ); + // A page the tables are wrong about comes back as it went in, to be refused for its size. + let short = &raw[..100]; + assert_eq!(unfilter_page(&filter_page(short, &info), &info), short); + } + #[test] fn a_page_and_a_mip_can_be_read_without_the_rest() { let loaded = sample_scene(); diff --git a/crates/solstice-asset/src/gpu.rs b/crates/solstice-asset/src/gpu.rs index 955ad0f..5707617 100644 --- a/crates/solstice-asset/src/gpu.rs +++ b/crates/solstice-asset/src/gpu.rs @@ -47,17 +47,71 @@ pub const NODE_INSTALLED: u32 = 1 << 30; /// The bits of [`PackedNode::is_leaf`] holding a page. pub const NODE_PAGE_MASK: u32 = NODE_INSTALLED - 1; -/// A vertex compressed to 16 bytes. +/// A vertex compressed to 12 bytes: three words of a page. /// /// Positions sit on a single mesh-wide 16-bit grid. Using one grid rather than /// one per cluster is what keeps neighbouring clusters crack-free: a shared /// vertex always decodes to the identical position. /// -/// Layout, as little-endian `u16`s: `x y z nx ny u v _`. +/// Layout, as little-endian `u16`s: `x y z n u v`. The normal is octahedral, a signed byte for +/// each coordinate, see [`encode_normal`]: a page pool holds a third more vertices than it did +/// with sixteen bits for each and a word of padding. #[repr(C)] #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Pod, Zeroable)] pub struct PackedVertex { - pub data: [u16; 8], + pub data: [u16; 6], +} + +/// Words of a page one vertex takes. +pub const VERTEX_WORDS: u32 = (std::mem::size_of::() / 4) as u32; + +fn octahedral(n: [f32; 3]) -> [f32; 2] { + let sum = n[0].abs() + n[1].abs() + n[2].abs(); + let p = [n[0] / sum, n[1] / sum]; + if n[2] < 0.0 { + [ + (1.0 - p[1].abs()) * if p[0] < 0.0 { -1.0 } else { 1.0 }, + (1.0 - p[0].abs()) * if p[1] < 0.0 { -1.0 } else { 1.0 }, + ] + } else { + p + } +} + +/// The unit vector a packed normal stands for. `shaders/common/scene.wgsl` has a twin. +pub fn decode_normal(packed: u16) -> [f32; 3] { + let x = f32::from(packed as u8 as i8) / 127.0; + let y = f32::from((packed >> 8) as u8 as i8) / 127.0; + let z = 1.0 - x.abs() - y.abs(); + let fold = (-z).max(0.0); + let v = [ + x + if x >= 0.0 { -fold } else { fold }, + y + if y >= 0.0 { -fold } else { fold }, + z, + ]; + let length = (v[0] * v[0] + v[1] * v[1] + v[2] * v[2]).sqrt(); + [v[0] / length, v[1] / length, v[2] / length] +} + +/// Packs a unit vector into sixteen bits, as octahedral coordinates of a signed byte each. +/// +/// Rounding each coordinate to the nearest step is not always the nearest direction, so the four +/// steps around the exact point are all tried and the one that decodes closest is kept. +pub fn encode_normal(normal: [f32; 3]) -> u16 { + let exact = octahedral(normal); + let floor = [(exact[0] * 127.0).floor(), (exact[1] * 127.0).floor()]; + let mut best = (f32::NEG_INFINITY, 0u16); + for corner in 0..4 { + let x = (floor[0] + (corner & 1) as f32).clamp(-127.0, 127.0) as i8; + let y = (floor[1] + (corner >> 1) as f32).clamp(-127.0, 127.0) as i8; + let packed = u16::from(x as u8) | u16::from(y as u8) << 8; + let decoded = decode_normal(packed); + let alike = decoded[0] * normal[0] + decoded[1] * normal[1] + decoded[2] * normal[2]; + if alike > best.0 { + best = (alike, packed); + } + } + best.1 } /// How many bones a cluster's list can name. A cluster using more is drawn with the bounds of its @@ -178,11 +232,34 @@ impl PackedSkin { } } +/// Words of a page one skin takes. +pub const SKIN_WORDS: u32 = (std::mem::size_of::() / 4) as u32; + /// How many [`PackedVertex`]-sized records hold the skins of a skinned cluster's vertices. They -/// follow the cluster's vertices, two skins a record, so only skinned meshes pay for them, and a -/// page needs no separate part for them. +/// follow the cluster's vertices, two words a skin with no gaps between them, so only skinned +/// meshes pay for them, and a page needs no separate part for them. pub const fn skin_records(vertex_count: u32) -> u32 { - vertex_count.div_ceil(2) + (vertex_count * SKIN_WORDS).div_ceil(VERTEX_WORDS) +} + +/// The records that hold some vertices' skins, to follow those vertices. See [`skin_records`]. +pub fn pack_skins(skins: &[PackedSkin]) -> Vec { + let mut bytes: Vec = bytemuck::cast_slice(skins).to_vec(); + bytes.resize( + skin_records(skins.len() as u32) as usize * std::mem::size_of::(), + 0, + ); + bytes + .chunks_exact(std::mem::size_of::()) + .map(bytemuck::pod_read_unaligned) + .collect() +} + +/// One of the skins [`pack_skins`] packed into `records`. +pub fn skin_at(records: &[PackedVertex], index: usize) -> PackedSkin { + let bytes: &[u8] = bytemuck::cast_slice(records); + let size = std::mem::size_of::(); + bytemuck::pod_read_unaligned(&bytes[index * size..(index + 1) * size]) } /// One leaf of the LOD DAG: up to [`MAX_CLUSTER_TRIANGLES`] triangles. @@ -261,8 +338,43 @@ pub fn unpack_triangle(packed: u32) -> [u8; 3] { [packed as u8, (packed >> 8) as u8, (packed >> 16) as u8] } +/// Words of a page that a cluster's triangles take: three bytes each, end to end, see +/// [`pack_triangle_words`]. +pub const fn triangle_words(triangle_count: u32) -> u32 { + (triangle_count * 3).div_ceil(4) +} + +/// Lays a cluster's triangles, each as [`pack_triangle`] made it, end to end at three bytes +/// each, and appends them to `out` as whole words. A mesh being built keeps a word for each +/// triangle; a page holds them like this, a quarter smaller. +pub fn pack_triangle_words(triangles: &[u32], out: &mut Vec) { + let mut bytes = Vec::with_capacity(triangles.len() * 3 + 3); + for triangle in triangles { + bytes.extend_from_slice(&triangle.to_le_bytes()[..3]); + } + bytes.resize(triangle_words(triangles.len() as u32) as usize * 4, 0); + out.extend( + bytes + .chunks_exact(4) + .map(|word| u32::from_le_bytes([word[0], word[1], word[2], word[3]])), + ); +} + +/// One triangle of those [`pack_triangle_words`] packed, as [`pack_triangle`] made it. The +/// shaders' `load_triangle` is its twin. +pub fn triangle_at(words: &[u32], index: u32) -> u32 { + let bit = index * 24; + let word = (bit / 32) as usize; + let shift = bit % 32; + let mut packed = words[word] >> shift; + if shift > 8 { + packed |= words[word + 1] << (32 - shift); + } + packed & 0x00ff_ffff +} + const _: () = { - assert!(std::mem::size_of::() == 16); + assert!(std::mem::size_of::() == 12); assert!(std::mem::size_of::() == 48); assert!(std::mem::size_of::() == 48); assert!(std::mem::size_of::() == 8); @@ -331,7 +443,71 @@ mod tests { ..Default::default() }; assert_eq!(cluster.vertex_records(false), 7); - assert_eq!(cluster.vertex_records(true), 11); - assert_eq!(skin_records(8), 4); + assert_eq!(cluster.vertex_records(true), 12); + assert_eq!(skin_records(8), 6); + assert_eq!(skin_records(3), 2); + } + + #[test] + fn skins_pack_end_to_end_and_read_back() { + let skins: Vec = (0..5u8) + .map(|i| PackedSkin::new([i, i + 1, 0, 0], [0.25, 0.75, 0.0, 0.0])) + .collect(); + let records = pack_skins(&skins); + assert_eq!(records.len(), skin_records(5) as usize); + for (index, skin) in skins.iter().enumerate() { + assert_eq!(skin_at(&records, index), *skin); + } + } + + #[test] + fn triangles_pack_at_three_bytes_and_read_back() { + for count in [0u32, 1, 2, 3, 4, 5, 127, 128] { + let triangles: Vec = (0..count) + .map(|i| pack_triangle(i as u8, (i * 7 + 1) as u8, (255 - i) as u8)) + .collect(); + let mut words = vec![0xdead_beef]; + pack_triangle_words(&triangles, &mut words); + assert_eq!(words.len() as u32, 1 + triangle_words(count)); + for (index, &triangle) in triangles.iter().enumerate() { + assert_eq!(triangle_at(&words[1..], index as u32), triangle); + } + } + assert_eq!(triangle_words(128), 96); + } + + #[test] + fn a_packed_normal_is_within_a_degree_and_usually_far_closer() { + let mut worst = 0.0f32; + let mut sum = 0.0f64; + let mut count = 0u32; + // A spiral over the whole sphere, and the axes, where folding is most awkward. + let mut normals: Vec<[f32; 3]> = (0..20_000) + .map(|i| { + let z = 1.0 - (i as f32 + 0.5) / 10_000.0; + let r = (1.0 - z * z).sqrt(); + let a = i as f32 * 2.399_963; + [r * a.cos(), r * a.sin(), z] + }) + .collect(); + normals.extend([ + [1.0, 0.0, 0.0], + [-1.0, 0.0, 0.0], + [0.0, 1.0, 0.0], + [0.0, -1.0, 0.0], + [0.0, 0.0, 1.0], + [0.0, 0.0, -1.0], + ]); + for normal in normals { + let back = decode_normal(encode_normal(normal)); + let alike = (back[0] * normal[0] + back[1] * normal[1] + back[2] * normal[2]).min(1.0); + let degrees = alike.acos().to_degrees(); + worst = worst.max(degrees); + sum += f64::from(degrees); + count += 1; + } + let mean = sum / f64::from(count); + assert!(worst < 1.0, "the worst normal is {worst} degrees out"); + assert!(mean < 0.35, "the mean is {mean} degrees out"); } } diff --git a/crates/solstice-asset/src/paged.rs b/crates/solstice-asset/src/paged.rs index e7e6096..bc5ac14 100644 --- a/crates/solstice-asset/src/paged.rs +++ b/crates/solstice-asset/src/paged.rs @@ -7,7 +7,8 @@ //! //! See `docs/streaming.md` for the rules that keep a partly loaded scene watertight. -use crate::gpu::{PackedCluster, PackedNode, PackedVertex}; +use crate::gpu::{PackedCluster, PackedNode, PackedVertex, VERTEX_WORDS}; + use crate::scene::{ GameFile, Instance, Light, Material, Sea, TextureEncoding, TextureLayer, Viewpoint, }; @@ -17,9 +18,7 @@ use glam::Vec3; /// Capacity of one page, and so the size of one slot in the GPU's page pool. It must hold the /// largest possible sibling set. pub const PAGE_BYTES: usize = 128 * 1024; -/// Vertices a page could hold if it held nothing else. -pub const PAGE_VERTICES: u32 = (PAGE_BYTES / std::mem::size_of::()) as u32; -/// Words of triangle data a page could hold if it held nothing else. +/// Words a page can hold: a cluster's vertices and triangles are found by the word. pub const PAGE_WORDS: u32 = (PAGE_BYTES / 4) as u32; /// Texture mips no larger than this travel with the manifest, so every material can draw from @@ -57,8 +56,10 @@ pub struct PageInfo { pub offset: u64, /// Size of the compressed page. Zero for a pinned page, which is held in the manifest. pub stored_bytes: u32, + /// Vertex records, the skins of skinned clusters among them. pub vertex_count: u32, - pub triangle_count: u32, + /// Words of triangles, which are packed at three bytes each within a cluster. + pub triangle_words: u32, pub first_set: u32, pub set_count: u32, pub _pad: u32, @@ -67,8 +68,17 @@ pub struct PageInfo { impl PageInfo { /// Size of the page once unpacked: all its vertices, then all its triangles. pub fn raw_bytes(&self) -> usize { + self.vertex_bytes() + self.triangle_words as usize * 4 + } + + /// Size of the page's vertices, which its triangles follow. + pub fn vertex_bytes(&self) -> usize { self.vertex_count as usize * std::mem::size_of::() - + self.triangle_count as usize * 4 + } + + /// Words of the page's vertices: where its triangles begin, counted in words. + pub fn vertex_words(&self) -> u32 { + self.vertex_count * VERTEX_WORDS } } @@ -89,7 +99,8 @@ pub struct MeshInfo { /// The resident part of a scene: all of it is uploaded when the scene opens. #[derive(Clone, Debug, Default)] pub struct PagedScene { - /// Offsets are relative to the start of the cluster's page and rewritten on install. + /// Offsets are in words: a cluster's vertices from the start of its page, its triangles + /// from the end of the page's vertices. They are rewritten on install. pub clusters: Vec, pub nodes: Vec, /// Sorted by page. diff --git a/crates/solstice-builder/src/dag.rs b/crates/solstice-builder/src/dag.rs index 53dea85..a11432c 100644 --- a/crates/solstice-builder/src/dag.rs +++ b/crates/solstice-builder/src/dag.rs @@ -13,8 +13,8 @@ use glam::{Vec2, Vec3}; use rayon::prelude::*; use rustc_hash::FxHashMap; use solstice_asset::gpu::{ - cluster_bones, encode_uv_density, pack_triangle, ClusterBones, CLUSTER_UV_DENSITY_SHIFT, - MAX_CLUSTER_TRIANGLES, MAX_CLUSTER_VERTICES, ROOT_PARENT_ERROR, + cluster_bones, encode_normal, encode_uv_density, pack_skins, pack_triangle, ClusterBones, + CLUSTER_UV_DENSITY_SHIFT, MAX_CLUSTER_TRIANGLES, MAX_CLUSTER_VERTICES, ROOT_PARENT_ERROR, }; use solstice_asset::paged::{ClusterSet, ROOT_ROUND}; use solstice_asset::{PackedCluster, PackedSkin, PackedVertex, VirtualMesh}; @@ -517,7 +517,7 @@ fn weld(mesh: &Mesh, input_skins: Option<&[PackedSkin]>, margin: f32) -> WeldedM // A skin belongs to a position, so vertices that differ in skin are different positions, // free to part in a pose. let mut position_ids: FxHashMap<([u16; 3], PackedSkin), u32> = FxHashMap::default(); - let mut attribute_ids: FxHashMap<[u32; 4], u32> = FxHashMap::default(); + let mut attribute_ids: FxHashMap<[u32; 3], u32> = FxHashMap::default(); let mut positions = Vec::new(); let mut skins = input_skins.map(|_| Vec::new()); let mut attributes = Vec::new(); @@ -538,14 +538,8 @@ fn weld(mesh: &Mesh, input_skins: Option<&[PackedSkin]>, margin: f32) -> WeldedM positions.len() as u32 - 1 }); - let octahedral = encode_octahedral(vertex.normal); let uv = ((vertex.uv - uv_min) / uv_step).round().as_uvec2(); - let attribute_key = [ - u32::from(octahedral[0] as u16), - u32::from(octahedral[1] as u16), - uv.x, - uv.y, - ]; + let attribute_key = [u32::from(encode_octahedral(vertex.normal)), uv.x, uv.y]; let attribute = *attribute_ids.entry(attribute_key).or_insert_with(|| { attributes.push(Attribute { normal: vertex.normal.normalize_or(Vec3::Y), @@ -589,22 +583,9 @@ fn weld(mesh: &Mesh, input_skins: Option<&[PackedSkin]>, margin: f32) -> WeldedM } } -/// Encodes a unit vector to 16-bit signed octahedral coordinates. -fn encode_octahedral(normal: Vec3) -> [i16; 2] { - let n = normal.normalize_or(Vec3::Y); - let p = Vec2::new(n.x, n.y) / (n.x.abs() + n.y.abs() + n.z.abs()); - let folded = if n.z < 0.0 { - Vec2::new( - (1.0 - p.y.abs()) * p.x.signum(), - (1.0 - p.x.abs()) * p.y.signum(), - ) - } else { - p - }; - [ - (folded.x.clamp(-1.0, 1.0) * 32767.0).round() as i16, - (folded.y.clamp(-1.0, 1.0) * 32767.0).round() as i16, - ] +/// Encodes a unit vector as a vertex holds it: octahedral coordinates, a signed byte each. +fn encode_octahedral(normal: Vec3) -> u16 { + encode_normal(normal.normalize_or(Vec3::Y).to_array()) } /// Orders clusters by sibling set and emits the GPU representation. @@ -735,11 +716,8 @@ fn pack_cluster( triangles.push(pack_triangle(indices[0], indices[1], indices[2])); } if welded.skins.is_some() { - // Two to a record, right behind the cluster's vertices. - for pair in skins.chunks(2) { - let second = pair.get(1).copied().unwrap_or_default(); - vertices.push(bytemuck::cast([pair[0], second])); - } + // End to end, right behind the cluster's vertices. + vertices.extend(pack_skins(&skins)); bones.push(cluster_bones( skins.iter().flat_map(|s| s.bones()).collect(), )); @@ -781,17 +759,14 @@ fn pack_vertex(welded: &WeldedMesh, corner: Corner) -> PackedVertex { let uv = ((attribute.uv - welded.uv_origin) / welded.uv_step) .round() .as_uvec2(); - let normal = encode_octahedral(attribute.normal); PackedVertex { data: [ grid.x as u16, grid.y as u16, grid.z as u16, - normal[0] as u16, - normal[1] as u16, + encode_octahedral(attribute.normal), uv.x as u16, uv.y as u16, - 0, ], } } @@ -1282,10 +1257,11 @@ pub(crate) mod tests { fn skinned_vertex(mesh: &VirtualMesh, cluster: u32, local: u32) -> ([u16; 3], PackedSkin) { let cluster = mesh.clusters[cluster as usize]; let data = mesh.vertices[(cluster.vertex_offset + local) as usize].data; - let record = - mesh.vertices[(cluster.vertex_offset + cluster.vertex_count() + local / 2) as usize]; - let pair: [PackedSkin; 2] = bytemuck::cast(record); - ([data[0], data[1], data[2]], pair[(local % 2) as usize]) + let skins = &mesh.vertices[(cluster.vertex_offset + cluster.vertex_count()) as usize..]; + ( + [data[0], data[1], data[2]], + solstice_asset::gpu::skin_at(skins, local as usize), + ) } #[test] @@ -1428,7 +1404,10 @@ pub(crate) mod tests { let bytes: usize = mesh.clusters[set.first_cluster as usize..] [..set.cluster_count as usize] .iter() - .map(|c| c.vertex_records(true) as usize * 16 + c.triangle_count() as usize * 4) + .map(|c| { + c.vertex_records(true) as usize * std::mem::size_of::() + + solstice_asset::gpu::triangle_words(c.triangle_count()) as usize * 4 + }) .sum(); assert!(bytes <= page, "a set takes {bytes} bytes, more than a page"); } diff --git a/crates/solstice-builder/src/paging.rs b/crates/solstice-builder/src/paging.rs index d2271d2..38c1c73 100644 --- a/crates/solstice-builder/src/paging.rs +++ b/crates/solstice-builder/src/paging.rs @@ -6,7 +6,7 @@ use rustc_hash::FxHashMap; use solstice_asset::{ - gpu::{CLUSTER_CHILDREN_MISSING, NODE_LEAF}, + gpu::{pack_triangle_words, triangle_words, CLUSTER_CHILDREN_MISSING, NODE_LEAF, VERTEX_WORDS}, paged::{ upper_mip_count, ClusterSet, LoadedScene, MeshInfo, PageInfo, PagedScene, PAGE_BYTES, ROOT_ROUND, @@ -17,6 +17,9 @@ use std::ops::Range; /// Identical payloads may share an offset within a page. Keys compare every byte, so this /// preserves quantized positions, attributes, skin weights and triangle order exactly. +/// +/// `data` is the page as it will be stored: its triangles are words of three-byte triangles, +/// where the clusters coming in have a word for each. #[derive(Default)] struct PageRecords { data: ClusterRecords, @@ -50,18 +53,20 @@ impl PageRecords { offset } }; - let triangles = &source.triangles[range.triangles.clone()]; - let triangle_offset = match self.triangles.get(triangles) { + let mut triangles = Vec::new(); + pack_triangle_words(&source.triangles[range.triangles.clone()], &mut triangles); + let triangle_offset = match self.triangles.get(&triangles) { Some(&offset) => offset, None => { let offset = self.data.triangles.len() as u32; - self.data.triangles.extend_from_slice(triangles); - self.triangles.insert(triangles.to_vec(), offset); + self.data.triangles.extend_from_slice(&triangles); + self.triangles.insert(triangles, offset); offset } }; + // Both in words, as the shaders find them. let cluster = &mut out.clusters[range.id as usize]; - cluster.vertex_offset = vertex_offset; + cluster.vertex_offset = vertex_offset * VERTEX_WORDS; cluster.triangle_offset = triangle_offset; } } @@ -134,7 +139,7 @@ impl ClusterStore for InMemory<'_> { /// Divides `scene` into pages. Textures split into the base levels and those above. pub fn paginate(scene: &SceneAsset) -> LoadedScene { let mut pages = Vec::new(); - let Ok(mut out) = paginate_from(scene, &mut InMemory(&scene.meshes), &mut |page| { + let Ok(mut out) = paginate_from(scene, &mut InMemory(&scene.meshes), &mut |page, _| { pages.push(page); Ok::<(), std::convert::Infallible>(()) }); @@ -147,16 +152,19 @@ pub fn paginate(scene: &SceneAsset) -> LoadedScene { } } +/// Receives each page that is not pinned as it is finished, in order: its bytes and its sizes. +pub type PageSink<'a, E> = dyn FnMut(Vec, &PageInfo) -> Result<(), E> + 'a; + /// Divides the meshes of `scene` into pages, without ever holding their vertices and triangles /// together: `store` supplies them a cluster at a time, and `sink` receives each page that is not -/// pinned, in order. Meshes may have left their `vertices` and `triangles` empty. An error from -/// `sink` stops the work and is returned. +/// pinned, in order, with its sizes. Meshes may have left their `vertices` and `triangles` empty. +/// An error from `sink` stops the work and is returned. /// /// Textures and collision are left to the caller, which may not want them in memory either. pub fn paginate_from( scene: &SceneAsset, store: &mut dyn ClusterStore, - sink: &mut dyn FnMut(Vec) -> Result<(), E>, + sink: &mut PageSink<'_, E>, ) -> Result { let mut out = PagedScene { materials: scene.materials.clone(), @@ -233,45 +241,123 @@ pub fn paginate_from( } } - // Coarsest first. The roots lead, as `ROOT_ROUND` is the largest round. + // Coarsest first. The roots lead, as `ROOT_ROUND` is the largest round. Within a round + // meshes keep the order they were made in, which puts like beside like: sorted instead by + // where their instances stand, every view of the scene this was measured on wanted 3 to 4% + // more pages (`docs/streaming.md`). items.sort_by_key(|item| (std::cmp::Reverse(item.round), item.mesh, item.curve)); - let mut records = PageRecords::default(); - let mut incoming = ClusterRecords::default(); - let mut ranges = Vec::new(); - let mut current = PageInfo::default(); + let mut filling = Filling::default(); let mut current_round = None; - for item in &items { - let mesh = &scene.meshes[item.mesh as usize]; - let local = |c: u32| &mesh.clusters[(c - item.cluster_base) as usize]; - let range = item.set.first_cluster..item.set.first_cluster + item.set.cluster_count; - let skinned = mesh.skinned(); - let extra_vertices: u32 = range - .clone() - .map(|c| local(c).vertex_records(skinned)) - .sum(); - let extra_triangles: u32 = range.clone().map(|c| local(c).triangle_count()).sum(); - let extra_bytes = extra_vertices as usize * std::mem::size_of::() - + extra_triangles as usize * 4; - assert!( - extra_bytes <= PAGE_BYTES, - "a sibling set must fit in a page" - ); - + let mut placed = vec![false; items.len()]; + for index in 0..items.len() { + if placed[index] { + continue; + } + let item = &items[index]; if current_round.is_some_and(|round| round != item.round) - || records.clusters + range.len() > MAX_PAGE_CLUSTERS + || filling.records.clusters + item.set.cluster_count as usize > MAX_PAGE_CLUSTERS { - finish_page(&mut out, &mut records, &mut current, current_round, sink)?; + finish_page(&mut out, &mut filling, current_round, sink)?; } current_round = Some(item.round); - incoming.vertices.clear(); - incoming.triangles.clear(); - ranges.clear(); - for c in range { - let cluster = *local(c); - let vertex_start = incoming.vertices.len(); - let triangle_start = incoming.triangles.len(); + // Try the whole sibling set before closing a page: shared payloads can make it fit + // where its unshared size would not. An overflowing set moves intact to the next page. + if !filling.place(scene, item, store, &mut out) { + // First, though, the room it leaves is filled from the few sets that follow it in + // its mesh and round, where any are small enough: a pool of fixed slots holds only + // as much as its pages are full. Only the few: they lie beside it, so a view that + // wants this page wants them too. Filled from further off, pages were fuller and + // every view wanted a quarter more of them. + for later in index + 1..items.len().min(index + 1 + FILL_WINDOW) { + let room = PAGE_BYTES - filling.current.raw_bytes(); + let candidate = &items[later]; + if candidate.round != item.round + || candidate.mesh != item.mesh + || room < SMALLEST_FILL + { + break; + } + if !placed[later] + && unshared_bytes(scene, candidate) <= room + && filling.records.clusters + candidate.set.cluster_count as usize + <= MAX_PAGE_CLUSTERS + { + placed[later] = filling.place(scene, candidate, store, &mut out); + } + } + finish_page(&mut out, &mut filling, current_round, sink)?; + let fits = filling.place(scene, item, store, &mut out); + assert!(fits, "a sibling set must fit in a page"); + } + } + if filling.current.set_count > 0 { + finish_page(&mut out, &mut filling, current_round, sink)?; + } + + // Nothing is installed yet: leaves know their page, and generated clusters stand in for + // the sets simplified into them. + for set in &out.sets { + out.nodes[set.leaf_node as usize].is_leaf = NODE_LEAF | set.page; + } + for &cluster in &out.generated { + out.clusters[cluster as usize].counts |= CLUSTER_CHILDREN_MISSING; + } + Ok(out) +} + +/// How many of the sets that follow one that won't fit are looked through for some that will. +const FILL_WINDOW: usize = 12; +/// Room in a page not worth looking for a set to fill. +const SMALLEST_FILL: usize = 512; + +/// What a set takes of a page if it shares nothing with what is already there. +fn unshared_bytes(scene: &SceneAsset, item: &Item) -> usize { + let mesh = &scene.meshes[item.mesh as usize]; + let skinned = mesh.skinned(); + let first = (item.set.first_cluster - item.cluster_base) as usize; + mesh.clusters[first..first + item.set.cluster_count as usize] + .iter() + .map(|cluster| { + cluster.vertex_records(skinned) as usize * std::mem::size_of::() + + triangle_words(cluster.triangle_count()) as usize * 4 + }) + .sum() +} + +/// The page being filled. +#[derive(Default)] +struct Filling { + records: PageRecords, + current: PageInfo, + incoming: ClusterRecords, + ranges: Vec, +} + +impl Filling { + /// Adds a set to the page, unless the page would be too large with it: then the page is + /// left as it was, and this returns false. + fn place( + &mut self, + scene: &SceneAsset, + item: &Item, + store: &mut dyn ClusterStore, + out: &mut PagedScene, + ) -> bool { + assert!( + unshared_bytes(scene, item) <= PAGE_BYTES, + "a sibling set must fit in a page" + ); + let mesh = &scene.meshes[item.mesh as usize]; + let skinned = mesh.skinned(); + self.incoming.vertices.clear(); + self.incoming.triangles.clear(); + self.ranges.clear(); + for c in item.set.first_cluster..item.set.first_cluster + item.set.cluster_count { + let cluster = mesh.clusters[(c - item.cluster_base) as usize]; + let vertex_start = self.incoming.vertices.len(); + let triangle_start = self.incoming.triangles.len(); store.read( item.mesh, ClusterRange { @@ -280,30 +366,30 @@ pub fn paginate_from( first_triangle: cluster.triangle_offset as usize, triangles: cluster.triangle_count() as usize, }, - &mut incoming, + &mut self.incoming, ); - ranges.push(PageCluster { + self.ranges.push(PageCluster { id: c, - vertices: vertex_start..incoming.vertices.len(), - triangles: triangle_start..incoming.triangles.len(), + vertices: vertex_start..self.incoming.vertices.len(), + triangles: triangle_start..self.incoming.triangles.len(), }); } - // Try the whole sibling set before closing a page: shared payloads can make it fit - // where its unshared size would not. An overflowing set moves intact to the next page. - let previous_clusters = records.clusters; - records.append(&incoming, &ranges, &mut out); - if records.data.vertices.len() * 16 + records.data.triangles.len() * 4 > PAGE_BYTES { - records.rollback( - current.vertex_count as usize, - current.triangle_count as usize, + let previous_clusters = self.records.clusters; + self.records.append(&self.incoming, &self.ranges, out); + if self.records.data.vertices.len() * std::mem::size_of::() + + self.records.data.triangles.len() * 4 + > PAGE_BYTES + { + self.records.rollback( + self.current.vertex_count as usize, + self.current.triangle_words as usize, previous_clusters, ); - finish_page(&mut out, &mut records, &mut current, current_round, sink)?; - records.append(&incoming, &ranges, &mut out); + return false; } - current.vertex_count = records.data.vertices.len() as u32; - current.triangle_count = records.data.triangles.len() as u32; - current.set_count += 1; + self.current.vertex_count = self.records.data.vertices.len() as u32; + self.current.triangle_words = self.records.data.triangles.len() as u32; + self.current.set_count += 1; out.sets.push(ClusterSet { page: out.pages.len() as u32, ..item.set @@ -311,29 +397,18 @@ pub fn paginate_from( if item.round == ROOT_ROUND { out.pinned_pages = out.pages.len() as u32 + 1; } + true } - if current.set_count > 0 { - finish_page(&mut out, &mut records, &mut current, current_round, sink)?; - } - - // Nothing is installed yet: leaves know their page, and generated clusters stand in for - // the sets simplified into them. - for set in &out.sets { - out.nodes[set.leaf_node as usize].is_leaf = NODE_LEAF | set.page; - } - for &cluster in &out.generated { - out.clusters[cluster as usize].counts |= CLUSTER_CHILDREN_MISSING; - } - Ok(out) } fn finish_page( out: &mut PagedScene, - records: &mut PageRecords, - current: &mut PageInfo, + filling: &mut Filling, round: Option, - sink: &mut dyn FnMut(Vec) -> Result<(), E>, + sink: &mut PageSink<'_, E>, ) -> Result<(), E> { + let current = &mut filling.current; + let records = &mut filling.records; let mut bytes = Vec::with_capacity(current.raw_bytes()); bytes.extend_from_slice(bytemuck::cast_slice(&records.data.vertices)); bytes.extend_from_slice(bytemuck::cast_slice(&records.data.triangles)); @@ -341,7 +416,7 @@ fn finish_page( if round == Some(ROOT_ROUND) { out.pinned_data.extend_from_slice(&bytes); } else { - sink(bytes)?; + sink(bytes, current)?; } current.first_set = out.sets.len() as u32 - current.set_count; out.pages.push(*current); @@ -371,6 +446,15 @@ fn leaf_nodes(mesh: &VirtualMesh) -> Vec { /// Places each set on a Morton curve through the mesh's bounds. fn space_curve(mesh: &VirtualMesh, leaf_of_set: &[u32]) -> Vec { + let centers: Vec<[f32; 3]> = leaf_of_set + .iter() + .map(|&leaf| mesh.nodes[leaf as usize].center) + .collect(); + morton_codes(¢ers) +} + +/// Where each point is along a Morton curve through the box that holds them all. +fn morton_codes(centers: &[[f32; 3]]) -> Vec { fn spread(mut x: u32) -> u32 { x &= 0x3ff; x = (x | x << 16) & 0x030000ff; @@ -378,13 +462,9 @@ fn space_curve(mesh: &VirtualMesh, leaf_of_set: &[u32]) -> Vec { x = (x | x << 4) & 0x030c30c3; (x | x << 2) & 0x09249249 } - let centers: Vec<[f32; 3]> = leaf_of_set - .iter() - .map(|&leaf| mesh.nodes[leaf as usize].center) - .collect(); let mut low = [f32::MAX; 3]; let mut high = [f32::MIN; 3]; - for center in ¢ers { + for center in centers { for axis in 0..3 { low[axis] = low[axis].min(center[axis]); high[axis] = high[axis].max(center[axis]); @@ -431,7 +511,7 @@ mod tests { use crate::Sphere; use crate::{build_virtual_mesh, BuildOptions}; use glam::{Mat4, Vec3}; - use solstice_asset::gpu::NODE_PAGE_MASK; + use solstice_asset::gpu::{triangle_at, NODE_PAGE_MASK}; use solstice_asset::{unpack_triangle, Instance, Material}; fn scene(subdivisions: &[u32]) -> SceneAsset { @@ -464,7 +544,10 @@ mod tests { assert_eq!(first.vertex_offset, second.vertex_offset); assert_eq!(first.triangle_offset, second.triangle_offset); assert_eq!(loaded.scene.pages[0].vertex_count, first.vertex_count()); - assert_eq!(loaded.scene.pages[0].triangle_count, first.triangle_count()); + assert_eq!( + loaded.scene.pages[0].triangle_words, + triangle_words(first.triangle_count()) + ); // One bit of a packed normal must prevent vertex sharing. Topology can still share. source.meshes[1].vertices[0].data[3] ^= 1; let loaded = paginate(&source); @@ -523,13 +606,12 @@ mod tests { for set in &paged.sets { let page = loaded.page(set.page); let info = paged.pages[set.page as usize]; - let records: &[PackedVertex] = - bytemuck::cast_slice(&page[..info.vertex_count as usize * 16]); + let records: &[PackedVertex] = bytemuck::cast_slice(&page[..info.vertex_bytes()]); for c in set.first_cluster..set.first_cluster + set.cluster_count { let cluster = paged.clusters[c as usize]; let source_cluster = scene.meshes[0].clusters[c as usize]; let (at, from) = ( - cluster.vertex_offset as usize, + (cluster.vertex_offset / VERTEX_WORDS) as usize, source_cluster.vertex_offset as usize, ); let count = cluster.vertex_records(true) as usize; @@ -552,8 +634,15 @@ mod tests { for c in set.first_cluster..set.first_cluster + set.cluster_count { seen[c as usize] += 1; let cluster = paged.clusters[c as usize]; - assert!(cluster.vertex_offset + cluster.vertex_count() <= page.vertex_count); - assert!(cluster.triangle_offset + cluster.triangle_count() <= page.triangle_count); + assert_eq!(cluster.vertex_offset % VERTEX_WORDS, 0); + assert!( + cluster.vertex_offset / VERTEX_WORDS + cluster.vertex_count() + <= page.vertex_count + ); + assert!( + cluster.triangle_offset + triangle_words(cluster.triangle_count()) + <= page.triangle_words + ); } } assert!(seen.iter().all(|&count| count == 1)); @@ -573,20 +662,19 @@ mod tests { .unwrap(); let bytes = loaded.page(set.page); let page = paged.pages[set.page as usize]; - let vertex_bytes = page.vertex_count as usize * 16; - let (v, t) = bytes.split_at(vertex_bytes); + let (v, t) = bytes.split_at(page.vertex_bytes()); let v: &[PackedVertex] = bytemuck::cast_slice(v); let t: Vec = t .chunks_exact(4) .map(|w| u32::from_le_bytes(w.try_into().unwrap())) .collect(); for i in 0..original.triangle_count() { - let a = t[(cluster.triangle_offset + i) as usize]; + let a = triangle_at(&t[cluster.triangle_offset as usize..], i); let b = mesh.triangles[(original.triangle_offset + i) as usize]; assert_eq!(a, b); for corner in unpack_triangle(a) { assert_eq!( - v[cluster.vertex_offset as usize + corner as usize], + v[(cluster.vertex_offset / VERTEX_WORDS) as usize + corner as usize], mesh.vertices[original.vertex_offset as usize + corner as usize] ); } @@ -596,6 +684,59 @@ mod tests { } } + #[test] + fn room_left_in_a_page_is_filled_from_the_sets_that_follow() { + let source = scene(&[6, 5, 4, 2]); + let loaded = paginate(&source); + let paged = &loaded.scene; + // Which mesh a set belongs to: each mesh's clusters follow the one before's. + let mesh_of = |set: &ClusterSet| { + let mut first = 0; + source + .meshes + .iter() + .position(|mesh| { + first += mesh.clusters.len() as u32; + set.first_cluster < first + }) + .unwrap() + }; + let sets_of = |page: &PageInfo| { + &paged.sets[page.first_set as usize..(page.first_set + page.set_count) as usize] + }; + let size = |set: &ClusterSet| -> usize { + paged.clusters[set.first_cluster as usize..][..set.cluster_count as usize] + .iter() + .map(|c| { + c.vertex_count() as usize * std::mem::size_of::() + + triangle_words(c.triangle_count()) as usize * 4 + }) + .sum() + }; + let mut pairs = 0; + for pair in paged.pages.windows(2) { + let (page, next) = (&pair[0], &pair[1]); + if sets_of(page)[0].round != sets_of(next)[0].round { + continue; + } + // The next page opens with the set that would not fit. The set after it there, if + // of the same mesh, was looked at before this page closed, and would have gone in + // it had there been room. + let room = PAGE_BYTES - page.raw_bytes(); + let following = sets_of(next); + if room >= SMALLEST_FILL + && (page.set_count as usize) < FILL_WINDOW + && following.len() > 1 + && mesh_of(&following[0]) == mesh_of(&following[1]) + { + pairs += 1; + let set = &following[1]; + assert!(size(set) > room, "a set of {} fits {room}", size(set)); + } + } + assert!(pairs > 0, "the scene is too small to test this"); + } + #[test] fn pages_fit_and_hold_one_round() { let loaded = paginate(&scene(&[6, 2])); diff --git a/crates/solstice-import/src/character_tests.rs b/crates/solstice-import/src/character_tests.rs index 69c6b3f..5cf6a0f 100644 --- a/crates/solstice-import/src/character_tests.rs +++ b/crates/solstice-import/src/character_tests.rs @@ -302,10 +302,10 @@ fn importing_a_skin_keeps_its_bones_and_weights() { assert!(mesh.skinned()); let cluster = mesh.clusters[0]; assert_eq!(cluster.vertex_count(), 4); - let skins: Vec = mesh.vertices - [cluster.vertex_offset as usize + 4..cluster.vertex_offset as usize + 6] - .iter() - .flat_map(|record| bytemuck::cast::<_, [solstice_asset::PackedSkin; 2]>(*record)) + // The four vertices' skins follow them, end to end. + let records = &mesh.vertices[cluster.vertex_offset as usize + 4..]; + let skins: Vec = (0..4) + .map(|vertex| solstice_asset::gpu::skin_at(records, vertex)) .collect(); // Vertices may be listed in any order, but each of the file's four skins must be there. for (joints, weights) in Character::default().weights { @@ -336,10 +336,10 @@ fn weights_follow_their_bones_when_bones_are_renumbered() { assert_eq!(bones[1].parent, Some(0)); let mesh = &scene.meshes[0]; let cluster = mesh.clusters[0]; - let skins: Vec = mesh.vertices - [cluster.vertex_offset as usize + 4..cluster.vertex_offset as usize + 6] - .iter() - .flat_map(|record| bytemuck::cast::<_, [solstice_asset::PackedSkin; 2]>(*record)) + // The four vertices' skins follow them, end to end. + let records = &mesh.vertices[cluster.vertex_offset as usize + 4..]; + let skins: Vec = (0..4) + .map(|vertex| solstice_asset::gpu::skin_at(records, vertex)) .collect(); // In this file joint 0 is the spine (bone 2), 1 the pelvis (bone 1) and 2 the root (bone 0). for (joints, weights) in Character::default().weights { diff --git a/crates/solstice-import/src/streamed.rs b/crates/solstice-import/src/streamed.rs index 63588f0..7c42d23 100644 --- a/crates/solstice-import/src/streamed.rs +++ b/crates/solstice-import/src/streamed.rs @@ -20,7 +20,7 @@ use glam::{Mat3, Mat4, Quat, Vec3}; use gltf::{buffer, Document, Gltf, Node, Primitive}; use rayon::prelude::*; use solstice_asset::file::{deflate_page, encode_header_and_manifest}; -use solstice_asset::paged::{upper_mip_count, PagedScene}; +use solstice_asset::paged::{upper_mip_count, PageInfo, PagedScene}; use solstice_asset::scene::TextureEncoding; use solstice_asset::{ Card, CardImage, CardVertex, Cards, GlowMaps, Instance, Material, PackedVertex, SceneAsset, @@ -823,10 +823,13 @@ fn bake_inner( BufWriter::with_capacity(1 << 22, File::create(data_path).map_err(|e| e.to_string())?); let mut placed: Vec<(u64, u32)> = Vec::new(); let mut data_at = 0u64; - let mut pending: Vec> = Vec::new(); + let mut pending: Vec<(Vec, PageInfo)> = Vec::new(); let mut since_check = 0u64; - let mut flush = |pending: &mut Vec>, placed: &mut Vec<(u64, u32)>| { - let packed: Vec> = pending.par_iter().map(|raw| deflate_page(raw)).collect(); + let mut flush = |pending: &mut Vec<(Vec, PageInfo)>, placed: &mut Vec<(u64, u32)>| { + let packed: Vec> = pending + .par_iter() + .map(|(raw, info)| deflate_page(raw, info)) + .collect(); pending.clear(); for page in packed { placed.push((data_at, page.len() as u32)); @@ -840,8 +843,8 @@ fn bake_inner( } Ok::<(), String>(()) }; - let mut paged = paginate_from(&scene, &mut store, &mut |page| { - pending.push(page); + let mut paged = paginate_from(&scene, &mut store, &mut |page, info| { + pending.push((page, *info)); if pending.len() >= 1024 { flush(&mut pending, &mut placed)?; } diff --git a/crates/solstice-render/examples/page_stress.rs b/crates/solstice-render/examples/page_stress.rs index 357624f..f4615af 100644 --- a/crates/solstice-render/examples/page_stress.rs +++ b/crates/solstice-render/examples/page_stress.rs @@ -72,7 +72,8 @@ fn main() { for page in 0..loaded.scene.pages.len() as u32 { let bytes = loaded.page(page); raw += bytes.len(); - deflated += solstice_asset::file::deflate_page(bytes).len(); + let info = &loaded.scene.pages[page as usize]; + deflated += solstice_asset::file::deflate_page(bytes, info).len(); } // Every cluster must still decode to exactly the original packed values, including // triangle ordering. This checks the thing the GPU consumes, not just byte counts. @@ -84,20 +85,22 @@ fn main() { let after = &loaded.scene.clusters[c as usize]; let vs = before.vertex_count() as usize; let ts = before.triangle_count() as usize; - let vertex_at = after.vertex_offset as usize * 16; - let triangle_at = - info.vertex_count as usize * 16 + after.triangle_offset as usize * 4; + // Offsets are in words: a vertex is three, a triangle three bytes. + let vertex_at = after.vertex_offset as usize * 4; + let triangle_at = info.vertex_bytes() + after.triangle_offset as usize * 4; let source_vertices = &scene.meshes[0].vertices[before.vertex_offset as usize..][..vs]; let source_triangles = &scene.meshes[0].triangles[before.triangle_offset as usize..][..ts]; assert_eq!( - &bytes[vertex_at..][..vs * 16], + &bytes[vertex_at..][..vs * 12], bytemuck::cast_slice(source_vertices) ); + let mut packed = Vec::new(); + solstice_asset::gpu::pack_triangle_words(source_triangles, &mut packed); assert_eq!( - &bytes[triangle_at..][..ts * 4], - bytemuck::cast_slice(source_triangles) + &bytes[triangle_at..][..packed.len() * 4], + bytemuck::cast_slice(&packed) ); } } diff --git a/crates/solstice-render/shaders/common/pool.wgsl b/crates/solstice-render/shaders/common/pool.wgsl new file mode 100644 index 0000000..0fa5861 --- /dev/null +++ b/crates/solstice-render/shaders/common/pool.wgsl @@ -0,0 +1,30 @@ +// Reads a cluster's vertices and triangles out of the page pool. Both are found by the word: a +// cluster's `vertex_offset` and `triangle_offset` are words of the pool. A shader that includes +// this defines `vertex_word(word: u32) -> u32` and `triangle_word(word: u32) -> u32`, which read +// the pool through whichever of its bindings holds each. Twin of solstice-asset's `gpu` module. + +// A vertex is three words. +fn load_vertex(cluster: Cluster, local: u32) -> PackedVertex { + let word = cluster.vertex_offset + local * 3u; + return PackedVertex(vertex_word(word), vertex_word(word + 1u), vertex_word(word + 2u)); +} + +// A skinned cluster's vertices are followed by their skins, two words each. +fn vertex_skin(cluster: Cluster, local: u32) -> vec2u { + let word = cluster.vertex_offset + cluster_vertex_count(cluster) * 3u + local * 2u; + return vec2u(vertex_word(word), vertex_word(word + 1u)); +} + +// A triangle is three bytes, the places of its corners among its cluster's vertices, and a +// cluster's triangles lie end to end from the word they begin at. One that starts in the third +// or fourth byte of a word ends in the next word. +fn load_triangle(cluster: Cluster, index: u32) -> u32 { + let bit = index * 24u; + let word = cluster.triangle_offset + (bit >> 5u); + let shift = bit & 31u; + var packed = triangle_word(word) >> shift; + if shift > 8u { + packed |= triangle_word(word + 1u) << (32u - shift); + } + return packed & 0xffffffu; +} diff --git a/crates/solstice-render/shaders/common/scene.wgsl b/crates/solstice-render/shaders/common/scene.wgsl index 5b98566..1a0ca19 100644 --- a/crates/solstice-render/shaders/common/scene.wgsl +++ b/crates/solstice-render/shaders/common/scene.wgsl @@ -23,12 +23,11 @@ struct Node { is_leaf: u32, // 0 for an interior node. See the NODE_ constants in vg/cull.wgsl. } -// x y | z nx | ny u | v _ +// x y | z n | u v, sixteen bits each: three words of the page pool. See common/pool.wgsl. struct PackedVertex { a: u32, b: u32, c: u32, - d: u32, } struct Instance { @@ -162,15 +161,16 @@ fn decode_position(i: Instance, v: PackedVertex) -> vec3f { return i.quant_origin + grid * i.quant_step; } -fn sign_extend_16(x: u32) -> f32 { - return f32(i32(x << 16u) >> 16u); +fn sign_extend_8(x: u32) -> f32 { + return f32(i32(x << 24u) >> 24u); } +// The normal is octahedral, a signed byte for each coordinate. fn decode_normal(v: PackedVertex) -> vec3f { - let e = vec2f(sign_extend_16(v.b >> 16u), sign_extend_16(v.c & 0xffffu)) / 32767.0; + let e = vec2f(sign_extend_8(v.b >> 16u), sign_extend_8(v.b >> 24u)) / 127.0; return oct_decode(e); } fn decode_uv(i: Instance, v: PackedVertex) -> vec2f { - return i.uv_origin + vec2f(f32(v.c >> 16u), f32(v.d & 0xffffu)) * i.uv_step; + return i.uv_origin + vec2f(f32(v.c & 0xffffu), f32(v.c >> 16u)) * i.uv_step; } diff --git a/crates/solstice-render/shaders/common/skin_vertex.wgsl b/crates/solstice-render/shaders/common/skin_vertex.wgsl index ea2f899..63b5140 100644 --- a/crates/solstice-render/shaders/common/skin_vertex.wgsl +++ b/crates/solstice-render/shaders/common/skin_vertex.wgsl @@ -1,13 +1,7 @@ -// Reads a skinned vertex's skin. A shader that includes this defines `load_vertex`. +// Poses a skinned vertex. Its skin is read from the page pool: see common/pool.wgsl for what a +// shader that includes this must define. -// A skinned cluster's vertices are followed by their skins, two to a record. -fn vertex_skin(cluster: Cluster, local: u32) -> vec2u { - let record = load_vertex(cluster.vertex_offset + cluster_vertex_count(cluster) + local / 2u); - if (local & 1u) == 0u { - return vec2u(record.a, record.b); - } - return vec2u(record.c, record.d); -} +#include "common/pool.wgsl" // Where a vertex of a cluster goes in the current pose, in the mesh's own space. Rigid instances // stay as they are. diff --git a/crates/solstice-render/shaders/gi/voxelize.wgsl b/crates/solstice-render/shaders/gi/voxelize.wgsl index c2b6d6b..81b3e5a 100644 --- a/crates/solstice-render/shaders/gi/voxelize.wgsl +++ b/crates/solstice-render/shaders/gi/voxelize.wgsl @@ -12,6 +12,7 @@ #include "common/math.wgsl" #include "common/scene.wgsl" +#include "common/pool.wgsl" #include "vg/pass_view.wgsl" struct Params { @@ -26,8 +27,18 @@ struct Params { @group(0) @binding(0) var params: Params; @group(0) @binding(1) var instances: array; @group(0) @binding(2) var clusters: array; -@group(0) @binding(3) var vertices: array; +@group(0) @binding(3) var vertices: array; @group(0) @binding(4) var triangles: array; + +// The page pool, bound twice. See common/pool.wgsl. +fn vertex_word(word: u32) -> u32 { + return vertices[word]; +} + +fn triangle_word(word: u32) -> u32 { + return triangles[word]; +} + @group(0) @binding(5) var visible: array; @group(0) @binding(6) var state: CullStateRead; @group(0) @binding(7) var materials: array; @@ -249,10 +260,10 @@ fn voxelize_speck(entry: VisibleCluster) { let cluster = clusters[entry.cluster]; let instance = instances[entry.instance]; let material = materials[instance_material(instance)]; - let packed = triangles[cluster.triangle_offset]; - let vertex0 = vertices[cluster.vertex_offset + (packed & 0xffu)]; - let vertex1 = vertices[cluster.vertex_offset + ((packed >> 8u) & 0xffu)]; - let vertex2 = vertices[cluster.vertex_offset + ((packed >> 16u) & 0xffu)]; + let packed = load_triangle(cluster, 0u); + let vertex0 = load_vertex(cluster, packed & 0xffu); + let vertex1 = load_vertex(cluster, (packed >> 8u) & 0xffu); + let vertex2 = load_vertex(cluster, (packed >> 16u) & 0xffu); let world0 = transform_point(instance, decode_position(instance, vertex0)); let world1 = transform_point(instance, decode_position(instance, vertex1)); let world2 = transform_point(instance, decode_position(instance, vertex2)); @@ -296,10 +307,10 @@ fn voxelize_cluster_triangle(item: u32, t: u32) { let inv_voxel = 1.0 / params.voxel_size; { - let packed = triangles[cluster.triangle_offset + t]; - let vertex0 = vertices[cluster.vertex_offset + (packed & 0xffu)]; - let vertex1 = vertices[cluster.vertex_offset + ((packed >> 8u) & 0xffu)]; - let vertex2 = vertices[cluster.vertex_offset + ((packed >> 16u) & 0xffu)]; + let packed = load_triangle(cluster, t); + let vertex0 = load_vertex(cluster, packed & 0xffu); + let vertex1 = load_vertex(cluster, (packed >> 8u) & 0xffu); + let vertex2 = load_vertex(cluster, (packed >> 16u) & 0xffu); let world0 = transform_point(instance, decode_position(instance, vertex0)); let world1 = transform_point(instance, decode_position(instance, vertex1)); let world2 = transform_point(instance, decode_position(instance, vertex2)); diff --git a/crates/solstice-render/shaders/vg/raster_common.wgsl b/crates/solstice-render/shaders/vg/raster_common.wgsl index 1a31639..ab4e7d6 100644 --- a/crates/solstice-render/shaders/vg/raster_common.wgsl +++ b/crates/solstice-render/shaders/vg/raster_common.wgsl @@ -30,9 +30,13 @@ fn place_vertex(instance: Instance, posed: vec3f) -> vec3f { return swayed_position(instance, posed, world, view.camera_position); } -fn load_vertex(index: u32) -> PackedVertex { - let word = index * 4u; - return PackedVertex(pool[word], pool[word + 1u], pool[word + 2u], pool[word + 3u]); +// One binding holds vertices and triangles alike. See common/pool.wgsl. +fn vertex_word(word: u32) -> u32 { + return pool[word]; +} + +fn triangle_word(word: u32) -> u32 { + return pool[word]; } // Visibility ID: visible-cluster index in the high bits, triangle in the low 7. diff --git a/crates/solstice-render/shaders/vg/raster_hw.wgsl b/crates/solstice-render/shaders/vg/raster_hw.wgsl index 1fd2141..217aa7d 100644 --- a/crates/solstice-render/shaders/vg/raster_hw.wgsl +++ b/crates/solstice-render/shaders/vg/raster_hw.wgsl @@ -24,11 +24,11 @@ fn cluster_vertex(vertex_index: u32, visible_index: u32) -> VertexOutput { } let instance = instances[entry.instance]; - let packed_triangle = pool[cluster.triangle_offset + triangle]; + let packed_triangle = load_triangle(cluster, triangle); var clip: array; for (var corner = 0u; corner < 3u; corner++) { let local = local_vertex(packed_triangle, corner); - let vertex = load_vertex(cluster.vertex_offset + local); + let vertex = load_vertex(cluster, local); let posed = posed_position(instance, cluster, local, decode_position(instance, vertex)); let world = place_vertex(instance, posed); clip[corner] = view.raster_view_proj * vec4f(world, 1.0); diff --git a/crates/solstice-render/shaders/vg/raster_hw_masked.wgsl b/crates/solstice-render/shaders/vg/raster_hw_masked.wgsl index deda582..c20ade0 100644 --- a/crates/solstice-render/shaders/vg/raster_hw_masked.wgsl +++ b/crates/solstice-render/shaders/vg/raster_hw_masked.wgsl @@ -29,8 +29,8 @@ fn vs_masked(@builtin(vertex_index) v: u32, @builtin(instance_index) i: u32) -> if triangle < cluster_triangle_count(cluster) { let instance = instances[entry.instance]; out.material = instance_material(instance); - let packed = pool[cluster.triangle_offset + triangle]; - let vertex = load_vertex(cluster.vertex_offset + local_vertex(packed, v % 3u)); + let packed = load_triangle(cluster, triangle); + let vertex = load_vertex(cluster, local_vertex(packed, v % 3u)); out.uv = decode_uv(instance, vertex) * materials[out.material].uv_scale; } return out; diff --git a/crates/solstice-render/shaders/vg/raster_sw_body.wgsl b/crates/solstice-render/shaders/vg/raster_sw_body.wgsl index a3509de..28311bc 100644 --- a/crates/solstice-render/shaders/vg/raster_sw_body.wgsl +++ b/crates/solstice-render/shaders/vg/raster_sw_body.wgsl @@ -46,7 +46,7 @@ fn main( let vertex_count = cluster_vertex_count(cluster); for (var v = lane; v < vertex_count; v += GROUP_SIZE) { - let packed = load_vertex(cluster.vertex_offset + v); + let packed = load_vertex(cluster, v); let posed = posed_position(instance, cluster, v, decode_position(instance, packed)); let world = place_vertex(instance, posed); let clip = view.raster_view_proj * vec4f(world, 1.0); @@ -61,7 +61,7 @@ fn main( workgroupBarrier(); if lane < cluster_triangle_count(cluster) { - let packed = pool[cluster.triangle_offset + lane]; + let packed = load_triangle(cluster, lane); let corners = vec3u(local_vertex(packed, 0u), local_vertex(packed, 1u), local_vertex(packed, 2u)); rasterize_triangle( screen[corners.x], diff --git a/crates/solstice-render/shaders/vg/resolve.wgsl b/crates/solstice-render/shaders/vg/resolve.wgsl index 56b28f8..bcba5b4 100644 --- a/crates/solstice-render/shaders/vg/resolve.wgsl +++ b/crates/solstice-render/shaders/vg/resolve.wgsl @@ -20,15 +20,20 @@ @group(0) @binding(3) var visible: array; @group(0) @binding(4) var instances: array; @group(0) @binding(5) var clusters: array; -@group(0) @binding(6) var vertices: array; +@group(0) @binding(6) var vertices: array; @group(0) @binding(7) var triangles: array; @group(0) @binding(8) var materials: array; @group(0) @binding(9) var bone_texture: texture_2d; // The wind, for what sways in it: see common/sway_vertex.wgsl. @group(0) @binding(10) var wind_field: texture_2d; -fn load_vertex(index: u32) -> PackedVertex { - return vertices[index]; +// The page pool, bound twice. See common/pool.wgsl. +fn vertex_word(word: u32) -> u32 { + return vertices[word]; +} + +fn triangle_word(word: u32) -> u32 { + return triangles[word]; } @group(1) @binding(0) var out_albedo: texture_storage_2d; @@ -231,10 +236,10 @@ fn main(@builtin(global_invocation_id) id: vec3u) { let cluster = clusters[entry.cluster]; let material = materials[instance_material(instance)]; - let packed = triangles[cluster.triangle_offset + triangle_index]; - let v0 = vertices[cluster.vertex_offset + (packed & 0xffu)]; - let v1 = vertices[cluster.vertex_offset + ((packed >> 8u) & 0xffu)]; - let v2 = vertices[cluster.vertex_offset + ((packed >> 16u) & 0xffu)]; + let packed = load_triangle(cluster, triangle_index); + let v0 = load_vertex(cluster, packed & 0xffu); + let v1 = load_vertex(cluster, (packed >> 8u) & 0xffu); + let v2 = load_vertex(cluster, (packed >> 16u) & 0xffu); var local0 = decode_position(instance, v0); var local1 = decode_position(instance, v1); diff --git a/crates/solstice-render/src/regions.rs b/crates/solstice-render/src/regions.rs index 3f104f8..eed3cdf 100644 --- a/crates/solstice-render/src/regions.rs +++ b/crates/solstice-render/src/regions.rs @@ -324,12 +324,6 @@ fn pool_word(address: u32) -> u32 {{ ) } -/// Vertex numbers to word addresses, keeping the band. -fn vertex_word() -> String { - let low = (1u32 << BAND_SHIFT) - 1; - format!("(index & {}u) | ((index & {low}u) * 4u)", !low) -} - /// Rewrites an expanded shader for a scene that needs regions, with this many pools. pub fn rewrite_shader(source: &str, cluster_chunk: u32, pools: usize) -> String { let mut binding = next_binding(source); @@ -358,17 +352,10 @@ fn cluster_at(index: u32) -> Cluster {{ }; const POOL: &str = "var pool: array;"; - const VERTICES: &str = "var vertices: array;"; + const VERTICES: &str = "var vertices: array;"; const TRIANGLES: &str = "var triangles: array;"; - let vertex_word = vertex_word(); if out.contains(POOL) { // One binding of words: the rasterizers. - const WORD: &str = "let word = index * 4u;"; - assert!( - out.contains(WORD), - "the rasterizer's load_vertex has changed" - ); - out = out.replace(WORD, &format!("let word = {vertex_word};")); out = replace_indexing(&out, "pool", "pool_word"); out = out.replace(POOL, "var pool_near: array;"); for band in 1..pools { @@ -377,7 +364,7 @@ fn cluster_at(index: u32) -> Cluster {{ extra += &pool_word(pools); } else if out.contains(VERTICES) { // A binding each for vertices and triangles, which carry the first two bands here. - out = replace_indexing(&out, "vertices", "region_vertex"); + out = replace_indexing(&out, "vertices", "pool_word"); out = replace_indexing(&out, "triangles", "pool_word"); out = out.replace(VERTICES, "var pool_near: array;"); assert!(out.contains(TRIANGLES), "the triangles binding has changed"); @@ -386,15 +373,6 @@ fn cluster_at(index: u32) -> Cluster {{ declare(band, &mut extra); } extra += &pool_word(pools); - extra += &format!( - " -fn region_vertex(index: u32) -> PackedVertex {{ - let word = {vertex_word}; - return PackedVertex( - pool_word(word), pool_word(word + 1u), pool_word(word + 2u), pool_word(word + 3u)); -}} -" - ); } out + &extra } diff --git a/crates/solstice-render/src/renderer.rs b/crates/solstice-render/src/renderer.rs index c212756..31e7787 100644 --- a/crates/solstice-render/src/renderer.rs +++ b/crates/solstice-render/src/renderer.rs @@ -811,6 +811,19 @@ impl Renderer { self.streamer.deliver_request(id, data) } + /// As [`Self::deliver`], for bytes just as a scene file stores them, inflated if they were + /// deflated. A host reading a file hands them over with this: a stored page has its vertices + /// filtered to deflate smaller, which this undoes. [`Self::deliver`] takes a page as the GPU + /// reads it, as a scene held in memory has them. + pub fn deliver_fetched(&mut self, id: u32, data: Vec) -> bool { + self.streamer.deliver_fetched(id, data, Vec::new()) + } + + /// As [`Self::deliver_page`], for a page just as a scene file stores it, inflated. + pub fn deliver_fetched_page(&mut self, id: u32, data: Vec, clusters: Vec) -> bool { + self.streamer.deliver_fetched(id, data, clusters) + } + /// Reports that a request could not be fetched, so what it holds may be requested again. pub fn request_failed(&mut self, id: u32) { self.streamer.request_failed(id); diff --git a/crates/solstice-render/src/shaders.rs b/crates/solstice-render/src/shaders.rs index a103d75..4d39d01 100644 --- a/crates/solstice-render/src/shaders.rs +++ b/crates/solstice-render/src/shaders.rs @@ -33,6 +33,7 @@ pub const SOURCES: &[(&str, &str)] = shader_table![ "lightning/bolt.wgsl", "common/scene.wgsl", "common/skin.wgsl", + "common/pool.wgsl", "common/skin_vertex.wgsl", "common/frame.wgsl", "common/wetness.wgsl", diff --git a/crates/solstice-render/src/streaming/mod.rs b/crates/solstice-render/src/streaming/mod.rs index 3ed218a..f04443b 100644 --- a/crates/solstice-render/src/streaming/mod.rs +++ b/crates/solstice-render/src/streaming/mod.rs @@ -17,7 +17,7 @@ use feedback::RequestFeedback; use residency::{Residency, Step}; use solstice_asset::gpu::{CLUSTER_CHILDREN_MISSING, NODE_INSTALLED, NODE_LEAF}; use solstice_asset::paged::{ - upper_mip_count, ClusterSet, PageInfo, PagedScene, BASE_TEXTURE_EDGE, PAGE_VERTICES, PAGE_WORDS, + upper_mip_count, ClusterSet, PageInfo, PagedScene, BASE_TEXTURE_EDGE, PAGE_WORDS, }; use solstice_asset::{Material, PackedCluster, PackedNode, TextureEncoding}; use std::collections::HashMap; @@ -509,6 +509,22 @@ impl Streamer { self.deliver_page(page, data, Vec::new()) } + /// Takes the bytes of a request as a scene file stores them, inflated if they were deflated: + /// a page is still filtered as `solstice_asset::file::filter_page` left it, and is put back + /// as the GPU reads it here. A texture level is stored as it is used. + pub fn deliver_fetched(&mut self, id: u32, data: Vec, clusters: Vec) -> bool { + if id & TEXTURE_REQUEST_BIT != 0 { + return self.deliver_request(id, data); + } + match self.pages.get(id as usize) { + Some(info) if info.raw_bytes() == data.len() => { + let data = solstice_asset::file::unfilter_page(&data, info); + self.deliver_page(id, data, clusters) + } + _ => false, + } + } + /// As [`Self::deliver`], with the page's cluster records, for a scene that keeps them outside /// its manifest: the bytes [`StreamRequest::clusters`] names. pub fn deliver_page(&mut self, page: u32, data: Vec, clusters: Vec) -> bool { @@ -574,7 +590,7 @@ impl Streamer { data, ); let info = self.pages[page as usize]; - let (vertex_base, triangle_base) = slot_bases(band, local, info.vertex_count); + let (vertex_base, triangle_base) = slot_bases(band, local, info.vertex_words()); let chunk = self.regions.map_or(u32::MAX, |r| r.cluster_chunk); let installed = NODE_LEAF | NODE_INSTALLED | page; match &mut self.clusters { @@ -641,7 +657,7 @@ impl Streamer { self.slots[page as usize] = u32::MAX; let info = self.pages[page as usize]; let (band, local) = locate_slot(self.regions.as_ref(), slot); - let (vertex_base, triangle_base) = slot_bases(band, local, info.vertex_count); + let (vertex_base, triangle_base) = slot_bases(band, local, info.vertex_words()); if let ClusterCopy::Resident(resident) = &mut self.clusters { resident.records[slot as usize] = Vec::new(); } @@ -942,15 +958,14 @@ fn locate_slot(regions: Option<&RegionPlan>, slot: u32) -> (usize, u32) { panic!("slot {slot} is outside the pool"); } -/// Where a slot's vertices and triangles begin, in units of vertices and of triangle words. -/// Triangles start right after the page's own vertices. The band goes in the top three bits, which -/// is zero in a scene with one pool. -fn slot_bases(band: usize, local: u32, vertex_count: u32) -> (u32, u32) { +/// Where a slot's vertices and triangles begin, both in words of the pool. Triangles start +/// right after the page's own vertices, which take `vertex_words`. The band goes in the top +/// three bits, which is zero in a scene with one pool. +fn slot_bases(band: usize, local: u32, vertex_words: u32) -> (u32, u32) { let band = (band as u32) << crate::regions::BAND_SHIFT; ( - band | (local * PAGE_VERTICES), - band | (local * PAGE_WORDS - + vertex_count * (std::mem::size_of::() as u32 / 4)), + band | (local * PAGE_WORDS), + band | (local * PAGE_WORDS + vertex_words), ) } @@ -980,33 +995,29 @@ mod tests { #[test] fn triangles_follow_a_pages_vertices_within_its_slot() { - assert_eq!(slot_bases(0, 0, 10), (0, 40)); - assert_eq!( - slot_bases(0, 2, 10), - (2 * PAGE_VERTICES, 2 * PAGE_WORDS + 40) - ); + assert_eq!(slot_bases(0, 0, 30), (0, 30)); + assert_eq!(slot_bases(0, 2, 30), (2 * PAGE_WORDS, 2 * PAGE_WORDS + 30)); } #[test] fn a_band_is_carried_above_the_place_in_its_buffer() { - let (vertices, triangles) = slot_bases(2, 3, 10); + let (vertices, triangles) = slot_bases(2, 3, 30); use crate::regions::{BANDS, BAND_SHIFT}; let low = (1 << BAND_SHIFT) - 1; assert_eq!(vertices >> BAND_SHIFT, 2); - assert_eq!(vertices & low, 3 * PAGE_VERTICES); + assert_eq!(vertices & low, 3 * PAGE_WORDS); assert_eq!(triangles >> BAND_SHIFT, 2); - assert_eq!(triangles & low, 3 * PAGE_WORDS + 40); + assert_eq!(triangles & low, 3 * PAGE_WORDS + 30); - // The last slot of the last band a binding can hold still fits beside its band, with - // vertex numbers counted in words as the shaders do. + // The last slot of the last band a binding can hold still fits beside its band. let last = (crate::regions::binding_slots(&wgpu::Limits { max_storage_buffer_binding_size: 2_147_483_644, max_buffer_size: 1 << 31, ..Default::default() })) - 1; - let (vertices, triangles) = slot_bases(BANDS - 1, last, PAGE_VERTICES); + let (vertices, triangles) = slot_bases(BANDS - 1, last, PAGE_WORDS); assert_eq!(vertices >> BAND_SHIFT, BANDS as u32 - 1); - assert!(u64::from(vertices & low) * 4 + u64::from(PAGE_WORDS) <= 1 << BAND_SHIFT); + assert!(u64::from(vertices & low) + u64::from(PAGE_WORDS) <= 1 << BAND_SHIFT); assert_eq!(triangles >> BAND_SHIFT, BANDS as u32 - 1); // A device that allows a larger binding still gets pools an address can reach into. diff --git a/docs/gameplay.md b/docs/gameplay.md index 2a95462..012ffc7 100644 --- a/docs/gameplay.md +++ b/docs/gameplay.md @@ -58,8 +58,9 @@ Not in the first version, with room left for each: Each of these has to be told when something moves, or ignore things that move. - **Two-phase occlusion culling** (`vg/culling.rs`) already copes with motion: anything the old depth hid wrongly is retried against the new depth in the same frame. -- **Vertices** are 16 bytes on one 16-bit grid per mesh: position, a packed normal, UV, and one - spare 16-bit value (`x y z nx ny u v _`, `solstice-asset/src/gpu.rs`). The grid has one step +- **Vertices** are 12 bytes on one 16-bit grid per mesh: position, a packed normal and UV + (`x y z n u v`, `solstice-asset/src/gpu.rs`; 16 bytes with a spare value until file version 5, + see `docs/streaming.md`). The grid has one step for all three axes, a 65,535th of the mesh's longest side (`Mesh::grid_step`), so a mesh that is very long keeps its points coarsely however thin it is: a kilometre of road as one mesh, to 15 mm. There are no bone @@ -877,7 +878,7 @@ checked on the keeper (38,462 triangles, 628 clusters in 12 meshes): - **A vertex names its bones directly,** by the skeleton's own numbers, rather than through its cluster's list. Skeletons have at most 256 bones, so 8 bits is enough, and resolve and the rasterizers skip a lookup per influence. A skin is 8 bytes behind its cluster's vertices in the - page, two to a 16-byte record, so a page needs no new part. + page, end to end (two to a 16-byte record until file version 5), so a page needs no new part. - **The cluster's list is only for culling,** and a cluster's bounds stay its bind-pose sphere: the skinned position lies inside the sphere moved by each of its bones, so a union of those moved spheres contains it. No per-bone radii are stored. diff --git a/docs/performance-audit.md b/docs/performance-audit.md index c039d97..f25f319 100644 --- a/docs/performance-audit.md +++ b/docs/performance-audit.md @@ -158,6 +158,8 @@ validation: data further, but they change decode/upload work and need a format/version plan. Current vertices already occupy 16 bytes, triangle indices fit in three bytes of a four-byte word, and clusters allow 128 triangles and 255 vertices. + (Since done in part, as file version 5: 12-byte vertices and the three-byte + triangle stream. See "Tighter pages" in `docs/streaming.md`.) * The planner still scans page-sized arrays each plan. A sparse changed-request set could reduce that cost further, provided retention, cached-view requests, and deterministic priority semantics are preserved. diff --git a/docs/streaming.md b/docs/streaming.md index 6f63a35..a365cbd 100644 --- a/docs/streaming.md +++ b/docs/streaming.md @@ -143,13 +143,13 @@ maps to a tier and slot: and serves ranges from memory, so nothing breaks. `scripts/serve.py` replaces the plain server in `launch.json`, adding Range support and a `--throttle-mbps` option for measuring. -File v4: +File v4 (version 5 packs its pages tighter, see [Tighter pages](#tighter-pages-file-version-5)): ``` header magic, version, sizes and offsets manifest deflate: meshes, nodes, clusters, page table, set links, materials, instances, lights, viewpoint, root pages, texture mips of 128 and below -pages each deflated on its own +pages each deflated on its own (filtered first, from version 5) textures raw, by layer and mip ``` @@ -208,6 +208,96 @@ the file once, in that test 226 MB, and streams from memory. Range was tested on Pages with a `.gz` file only. The on-screen frame rate looked the same as before in the browser, at 102 to 110 fps, but with no controlled comparison. +## Tighter pages (file version 5) + +A pool of fixed slots holds as many clusters as fit in a slot, so this made what a cluster takes +smaller, and the slots fuller. Measured on a castle in its gardens, a scene that is not in this +repository (31.2 million unique triangles in 960 meshes, most of it leaf-by-leaf planting, +541,000 clusters, 4.1 billion triangles placed), with +`cargo run --release -p solstice-asset --example page_stats -- FILE`, which measures any bake. + +**What the pages held.** Vertices were 82% of them, at 129 to a cluster of 118 triangles: leaves +and petals share few corners. Of a vertex's 16 bytes two were padding, and its normal had 32 bits. +Of a triangle's 4 bytes one was unused. Slots were 91.6% full, as a page closed at the first +sibling set that would not fit. + +**What changed.** + +- **A vertex is 12 bytes**, `x y z n u v` at sixteen bits each. The normal is octahedral with a + signed byte for each coordinate, as Nanite's default is, and the four codes around the exact + point are tried for the nearest (`gpu::encode_normal`): 0.31 degrees out on average, under one + at worst. Positions and UVs keep all their bits, so nothing moves and no seam can open. +- **A triangle is 3 bytes**, end to end within its cluster (`gpu::pack_triangle_words`). A mesh + being built still has a word for each; pages are packed when they are made. +- **Offsets are words.** A cluster's `vertex_offset` counts words of the pool, as its + `triangle_offset` always did, so a slot need not be a whole number of vertices and a banded + pool needs no special arithmetic for them. Shaders read both through `common/pool.wgsl`. +- **Skins lie end to end** behind their cluster's vertices, two words each: a skinned vertex is + 20 bytes where it was 24. +- **A page is filtered before it is deflated** (`file::filter_page`): each field of a vertex as + its difference from the vertex before, one byte of it to a run. The host undoes it on arrival + (`Renderer::deliver_fetched`). Triangles are left alone, as they packed no better for it. +- **Room left in a page is filled** from the next twelve sets of the same mesh and round, where + any fit. + +| | Version 4 | Version 5 | +|---|---|---| +| Vertex, triangle | 16 and 4 bytes | **12 and 3** | +| Pages, unpacked | 1,294.6 MiB | **970.8 MiB** | +| Pages, as stored | 787.3 MiB | **499.9 MiB** | +| The scene file | 1,062.7 MB | **761.2 MB** | +| Pages | 11,309 | **8,033** | +| Slots full to | 91.6% | **96.7%** | +| Clusters to a slot | 47.9 | **67.4** | +| Pool bytes a cluster | 2,736 | **1,945** | + +So a pool of the same size holds 41% more clusters, and a page is 36% less to download for +what it holds. + +**What views want**, at 1600x900, after 400 frames with the default 128 MiB pool and 600 with a +pool that holds everything: + +| View | Short of room in 128 MiB, v4 | v5 | Installed with room for all, v4 | v5 | +|---|---|---|---|---| +| South gate, up the avenue | 1,153 | **643** | 2,759 (345 MiB) | **2,080 (260 MiB)** | +| From the air | 204 | **0** | 1,329 | **1,057** | +| In the maze | 658 | **272** | 2,368 | **1,788** | +| The great hall | 0 | 0 | 2,401 | **1,874** | +| By the lake | | | 2,643 | **1,982** | +| The rose garden | | | 2,584 | **1,967** | + +A scene this dense still wants more than the default pool: at 128 MiB its opening view is +short, and its own page asks for 320 with `?geometry-mb=320`. + +**It costs nothing to draw.** The same three views, whole scene loaded, 400 frames: 5.56 ms a +frame before and 5.53 after from the gate (raster 2.03 and 2.00, resolve 0.30 and 0.30), 3.52 and +3.46 in the maze, 2.94 and 2.92 in the hall. A triangle that straddles two words is a second +read, and it does not show. + +**The picture changes a little.** A version 5 render differs from the version 4 one by 35 to +37 dB, where two renders of one file differ by 44 to 47 dB. The difference is soft and wide, +bounced light a few levels brighter or darker in places, not edges: normals a fraction of a +degree apart now weld into one vertex, so meshes simplify slightly differently and the voxels +light is traced through differ with them. Side by side they look the same. All of +`cargo test` passes, the GPU tests among them. + +**Two things tried and left out**, both for the same reason: what matters is how many pages a +view wants, and that is set by how well a page keeps to one place. + +- *Filling pages from the next 256 sets of any mesh* made slots 99.0% full and views want a + quarter more pages: from the gate, 1,058 short of room where 643 had been. +- *Ordering meshes by where their instances stand*, so that small meshes sharing a page would be + neighbours, made every view want 3 to 4% more pages. The order meshes are made in already puts + like beside like. + +**A version 4 file does not open**: the reader says so, and the scene must be baked again. The +surfaces in `assets/content` are scene files too, and `scripts/build-web.sh` makes them again +when they are missing. + +**Not measured:** a browser on a real network, scenes with skinned meshes or regions (their +tests pass, but no such scene was baked for sizes), and whether deflate at a lower level would +now do as well. + ## Known limits - **Most of the viewer's memory isn't the scene.** The rest is render targets, cull lists and the GI diff --git a/docs/weather-effects.md b/docs/weather-effects.md index bfe0b4a..c0d96bc 100644 --- a/docs/weather-effects.md +++ b/docs/weather-effects.md @@ -37,7 +37,7 @@ code and its module comments are the reference, and the notes below say so. - **Foliage is real geometry in the virtual-geometry pipeline**: grass patches of folded blades (`solstice-procgen/src/foliage/grass.rs`, 2 m patches in the Forest) and trees grown as tubes of stems with leaf cards (`foliage/tree.rs`), with the `FOLIAGE` material flag (two-sided, transmission). - Vertices are 16 bytes: position, normal, UV. There is no per-vertex room for wind data. + Vertices are 12 bytes: position, normal, UV. There is no per-vertex room for wind data. - **Skinning** (PR #50/#54) already deforms virtual geometry "where it is used": cull bounds (`posed_sphere`), both rasterizers (`posed_position`), and resolve (positions, normals, and last frame's pose for motion vectors). Bones live in a texture, uploaded from the CPU.