refactor: Splitting into crates
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
[package]
|
||||
name = "vxls-terrain"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
bytemuck.workspace = true
|
||||
glam.workspace = true
|
||||
wgpu.workspace = true
|
||||
vxls-gpu = { path = "../vxls-gpu" }
|
||||
vxls-profiler = { path = "../vxls-profiler" }
|
||||
vxls-voxel-cache = { path = "../vxls-voxel-cache" }
|
||||
@@ -0,0 +1,365 @@
|
||||
#![feature(generic_const_exprs)]
|
||||
|
||||
use std::iter::FusedIterator;
|
||||
|
||||
use bytemuck::Pod;
|
||||
use bytemuck::Zeroable;
|
||||
use glam::Vec3;
|
||||
use wgpu::CommandEncoder;
|
||||
use wgpu::include_wgsl;
|
||||
|
||||
use vxls_gpu::create_multi_buffers_bind_group_layout;
|
||||
use vxls_voxel_cache;
|
||||
use vxls_voxel_cache::schema::StructureAnchor;
|
||||
use vxls_voxel_cache::tables::FixedStructureTable;
|
||||
|
||||
/// A torroidal structure table for "infinite" world purposes.
|
||||
/// Allows rendering a X*Y*Z block of chunks arround a position, like for example arround a player.
|
||||
///
|
||||
/// When the player moves, crossing a chunk boundary, chunks behind disapear, chunks in front
|
||||
/// appear. The torroidal nature of the table (modulo on the coordinates) allows reusing the
|
||||
/// disapearing chunks for the new chunks appearing in front.
|
||||
pub struct ToroidalStructureTable
|
||||
{
|
||||
/// Size of the torroidal structure : how many chunks in each direction can be rendered at once
|
||||
extent: (usize, usize, usize),
|
||||
|
||||
fixed_structure_table: FixedStructureTable,
|
||||
position: Vec3,
|
||||
current_offset: (isize, isize, isize),
|
||||
|
||||
invalidation_pass: wgpu::ComputePipeline,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, Zeroable, Pod)]
|
||||
#[repr(C)]
|
||||
struct ToroidalInvalidationImmediates
|
||||
{
|
||||
extent: [u32; 3],
|
||||
_pad1: u32,
|
||||
previous_offset: [i32; 3],
|
||||
_pad2: u32,
|
||||
offset: [i32; 3],
|
||||
_pad3: u32,
|
||||
}
|
||||
|
||||
impl ToroidalStructureTable
|
||||
{
|
||||
pub fn new(
|
||||
device: &wgpu::Device,
|
||||
extent: (usize, usize, usize),
|
||||
initial_position: Vec3,
|
||||
max_requests: usize,
|
||||
producer_id: u32,
|
||||
) -> Self
|
||||
{
|
||||
let size = extent.0 * extent.1 * extent.2;
|
||||
assert!(size <= (u32::MAX as usize));
|
||||
|
||||
let current_offset = Self::offset_from_center(extent, initial_position);
|
||||
|
||||
// Initial anchor of every slot is its world position in the starting
|
||||
// window. Structure ids are table-internal and start at `0..size`.
|
||||
let anchors = (0..size)
|
||||
.map(|slot| {
|
||||
let (x, y, z) = Self::position_for_offset(extent, slot, current_offset);
|
||||
StructureAnchor::from_world(x as i32, y as i32, z as i32, 0)
|
||||
})
|
||||
.collect();
|
||||
|
||||
let bind_group_layout = create_multi_buffers_bind_group_layout(
|
||||
device,
|
||||
1,
|
||||
Some("ToroidalStructureTable::bind_group_layout"),
|
||||
);
|
||||
|
||||
let invalidation_pipeline_layout =
|
||||
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||
label: Some("ToroidalStructureTable::invalidation_pipeline_layout"),
|
||||
bind_group_layouts: &[Some(&bind_group_layout)],
|
||||
immediate_size: size_of::<ToroidalInvalidationImmediates>() as u32,
|
||||
});
|
||||
|
||||
let shader_module =
|
||||
device.create_shader_module(include_wgsl!("./shaders/toroidal_structure.wgsl"));
|
||||
|
||||
let invalidation_pass = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
|
||||
label: Some("ToroidalStructureTable::invalidation_pass"),
|
||||
layout: Some(&invalidation_pipeline_layout),
|
||||
module: &shader_module,
|
||||
entry_point: Some("invalidate_new"),
|
||||
compilation_options: Default::default(),
|
||||
cache: None,
|
||||
});
|
||||
|
||||
ToroidalStructureTable {
|
||||
extent,
|
||||
fixed_structure_table: FixedStructureTable::new_with_anchors(
|
||||
device,
|
||||
size,
|
||||
max_requests,
|
||||
producer_id,
|
||||
anchors,
|
||||
),
|
||||
position: initial_position,
|
||||
current_offset,
|
||||
invalidation_pass,
|
||||
}
|
||||
}
|
||||
|
||||
/// Moves the center position to `new_position`.
|
||||
/// This requires updating the structure table to remove disapearing chunks.
|
||||
///
|
||||
/// Slots whose world position changed get a new anchor. Anchors that remain
|
||||
/// in view keep their identity; anchors that leave are retired. The
|
||||
/// invalidation pass resets the pointers of moved slots so their roots are
|
||||
/// requested again.
|
||||
///
|
||||
/// Returns the retired anchors. The host may use them to cancel speculative
|
||||
/// queue work; correctness does not depend on it because GPU insertion
|
||||
/// validates the anchor/slot.
|
||||
pub fn move_position(
|
||||
&mut self,
|
||||
queue: &wgpu::Queue,
|
||||
encoder: &mut CommandEncoder,
|
||||
new_position: Vec3,
|
||||
) -> Vec<StructureAnchor>
|
||||
{
|
||||
let previous_offset = self.current_offset;
|
||||
let new_offset = Self::offset_from_center(self.extent, new_position);
|
||||
self.current_offset = new_offset;
|
||||
self.position = new_position;
|
||||
|
||||
let mut retired = Vec::new();
|
||||
if previous_offset != new_offset
|
||||
{
|
||||
let size = self.extent.0 * self.extent.1 * self.extent.2;
|
||||
let new_anchors: Vec<StructureAnchor> = (0..size)
|
||||
.map(|slot| {
|
||||
let (x, y, z) = Self::position_for_offset(self.extent, slot, new_offset);
|
||||
StructureAnchor::from_world(x as i32, y as i32, z as i32, 0)
|
||||
})
|
||||
.collect();
|
||||
|
||||
retired = self
|
||||
.fixed_structure_table
|
||||
.reassign_slots(queue, &new_anchors);
|
||||
}
|
||||
|
||||
let invalidation_parameters = ToroidalInvalidationImmediates {
|
||||
extent: [
|
||||
self.extent.0 as u32,
|
||||
self.extent.1 as u32,
|
||||
self.extent.2 as u32,
|
||||
],
|
||||
_pad1: 0,
|
||||
previous_offset: [
|
||||
previous_offset.0 as i32,
|
||||
previous_offset.1 as i32,
|
||||
previous_offset.2 as i32,
|
||||
],
|
||||
_pad2: 0,
|
||||
offset: [
|
||||
self.current_offset.0 as i32,
|
||||
self.current_offset.1 as i32,
|
||||
self.current_offset.2 as i32,
|
||||
],
|
||||
_pad3: 0,
|
||||
};
|
||||
|
||||
let mut invalidation_pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
|
||||
label: Some("ToroidalStructureTable::invalidation_pass"),
|
||||
timestamp_writes: None,
|
||||
});
|
||||
|
||||
invalidation_pass.set_pipeline(&self.invalidation_pass);
|
||||
invalidation_pass.set_bind_group(0, self.fixed_structure_table.pointers_bind_group(), &[]);
|
||||
invalidation_pass.set_immediates(0, bytemuck::bytes_of(&invalidation_parameters));
|
||||
invalidation_pass.dispatch_workgroups(
|
||||
self.extent.0.div_ceil(4) as u32,
|
||||
self.extent.1.div_ceil(4) as u32,
|
||||
self.extent.2.div_ceil(4) as u32,
|
||||
);
|
||||
|
||||
retired
|
||||
}
|
||||
|
||||
/// Returns the world position encoded in a table anchor.
|
||||
pub fn position_for_anchor(&self, anchor: StructureAnchor) -> (isize, isize, isize)
|
||||
{
|
||||
let (x, y, z, _version) = anchor.world();
|
||||
(x as isize, y as isize, z as isize)
|
||||
}
|
||||
|
||||
/// Canonical table slot of the visible block's minimum corner.
|
||||
///
|
||||
/// The render shader uses this to advance its table slot incrementally, so
|
||||
/// the ray traversal does not have to perform an integer modulo per chunk.
|
||||
pub fn base_slot(&self) -> [u32; 3]
|
||||
{
|
||||
let (ox, oy, oz) = self.current_offset;
|
||||
let (ex, ey, ez) = self.extent;
|
||||
[
|
||||
ox.rem_euclid(ex as isize) as u32,
|
||||
oy.rem_euclid(ey as isize) as u32,
|
||||
oz.rem_euclid(ez as isize) as u32,
|
||||
]
|
||||
}
|
||||
|
||||
fn position_for_offset(
|
||||
extent: (usize, usize, usize),
|
||||
slot: usize,
|
||||
offset: (isize, isize, isize),
|
||||
) -> (isize, isize, isize)
|
||||
{
|
||||
let (ex, ey, ez) = (extent.0 as isize, extent.1 as isize, extent.2 as isize);
|
||||
let id = slot as isize;
|
||||
|
||||
let sx = id % ex;
|
||||
let sz = (id / ex) % ez;
|
||||
let sy = id / (ex * ez);
|
||||
|
||||
let (ox, oy, oz) = offset;
|
||||
|
||||
(
|
||||
ox + (sx - ox).rem_euclid(ex),
|
||||
oy + (sy - oy).rem_euclid(ey),
|
||||
oz + (sz - oz).rem_euclid(ez),
|
||||
)
|
||||
}
|
||||
|
||||
fn offset_from_center(extent: (usize, usize, usize), position: Vec3) -> (isize, isize, isize)
|
||||
{
|
||||
(
|
||||
position.x.floor() as isize - (extent.0 as isize / 2),
|
||||
position.y.floor() as isize - (extent.1 as isize / 2),
|
||||
position.z.floor() as isize - (extent.2 as isize / 2),
|
||||
)
|
||||
}
|
||||
|
||||
// Function calls through to fixed_structure_table
|
||||
|
||||
pub fn raytrace_bind_group(&self) -> &wgpu::BindGroup
|
||||
{
|
||||
self.fixed_structure_table.raytrace_bind_group()
|
||||
}
|
||||
|
||||
pub fn raytrace_bind_group_layout(&self) -> &wgpu::BindGroupLayout
|
||||
{
|
||||
self.fixed_structure_table.raytrace_bind_group_layout()
|
||||
}
|
||||
|
||||
pub fn insert<const N: usize>(
|
||||
&self,
|
||||
device: &wgpu::Device,
|
||||
encoder: &mut vxls_profiler::ScopeEncoder<wgpu::CommandEncoder>,
|
||||
insertion: &vxls_voxel_cache::insertion::CacheInsertionData<N>,
|
||||
) where
|
||||
[(); N * N * N]:,
|
||||
{
|
||||
self.fixed_structure_table
|
||||
.insert(device, encoder, insertion);
|
||||
}
|
||||
|
||||
pub fn gather_requests(
|
||||
&self,
|
||||
device: &wgpu::Device,
|
||||
encoder: &mut vxls_profiler::ScopeEncoder<wgpu::CommandEncoder>,
|
||||
request_buffer: &vxls_voxel_cache::requests::CacheRequestBuffer,
|
||||
)
|
||||
{
|
||||
self.fixed_structure_table
|
||||
.gather_requests(device, encoder, request_buffer);
|
||||
}
|
||||
}
|
||||
|
||||
impl<const N: usize> vxls_voxel_cache::CacheInvalidable<N> for ToroidalStructureTable
|
||||
where
|
||||
[(); N * N * N]:,
|
||||
{
|
||||
fn validate_insertion(
|
||||
&mut self,
|
||||
device: &wgpu::Device,
|
||||
encoder: &mut vxls_profiler::ScopeEncoder<CommandEncoder>,
|
||||
insertion: &vxls_voxel_cache::insertion::CacheInsertionData<N>,
|
||||
) {
|
||||
self.fixed_structure_table
|
||||
.validate_insertion(device, encoder, insertion);
|
||||
}
|
||||
|
||||
fn invalidate(
|
||||
&mut self,
|
||||
encoder: &mut vxls_profiler::ScopeEncoder<CommandEncoder>,
|
||||
cache_evictor: &vxls_voxel_cache::eviction::CacheEvictor,
|
||||
invalid_timestamp: u32,
|
||||
)
|
||||
{
|
||||
self.fixed_structure_table
|
||||
.invalidate(encoder, cache_evictor, invalid_timestamp);
|
||||
}
|
||||
}
|
||||
|
||||
pub struct CenterChunkIter
|
||||
{
|
||||
center_chunk: (isize, isize, isize),
|
||||
extent: (usize, usize, usize),
|
||||
block_corner: (isize, isize, isize),
|
||||
current: usize,
|
||||
length: usize,
|
||||
}
|
||||
|
||||
impl CenterChunkIter
|
||||
{
|
||||
pub fn new(center_chunk: (isize, isize, isize), extent: (usize, usize, usize)) -> Self
|
||||
{
|
||||
let half_extent = (extent.0 / 2, extent.1 / 2, extent.2 / 2);
|
||||
let block_corner = (
|
||||
center_chunk.0 - half_extent.0 as isize,
|
||||
center_chunk.1 - half_extent.1 as isize,
|
||||
center_chunk.2 - half_extent.2 as isize,
|
||||
);
|
||||
|
||||
Self {
|
||||
center_chunk,
|
||||
extent,
|
||||
block_corner,
|
||||
current: 0,
|
||||
length: extent.0 * extent.1 * extent.2,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FusedIterator for CenterChunkIter {}
|
||||
impl ExactSizeIterator for CenterChunkIter {}
|
||||
|
||||
impl Iterator for CenterChunkIter
|
||||
{
|
||||
type Item = (isize, isize, isize);
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item>
|
||||
{
|
||||
if self.current >= self.length
|
||||
{
|
||||
None
|
||||
}
|
||||
else
|
||||
{
|
||||
let x = self.current % self.extent.0;
|
||||
let y = (self.current / self.extent.0) % self.extent.1;
|
||||
let z = self.current / (self.extent.0 * self.extent.1);
|
||||
self.current += 1;
|
||||
|
||||
Some((
|
||||
x as isize + self.block_corner.0,
|
||||
y as isize + self.block_corner.1,
|
||||
z as isize + self.block_corner.2,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> (usize, Option<usize>)
|
||||
{
|
||||
let remaining = self.length - self.current;
|
||||
(remaining, Some(remaining))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
@group(0) @binding(0) var<storage, read_write> table_pointers: array<u32>;
|
||||
|
||||
struct InvalidationImmediates
|
||||
{
|
||||
extent: vec3<u32>,
|
||||
pevious_offset: vec3<i32>,
|
||||
offset: vec3<i32>,
|
||||
}
|
||||
|
||||
var<immediate> immediates: InvalidationImmediates;
|
||||
|
||||
|
||||
@compute
|
||||
@workgroup_size(4, 4, 4)
|
||||
fn invalidate_new(@builtin(global_invocation_id) gid: vec3<u32>)
|
||||
{
|
||||
if(any(gid >= immediates.extent))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Check whether this invocation lies within a pointer to be renewed
|
||||
let igid = vec3<i32>(gid);
|
||||
let previous_pos = get_position(igid, immediates.pevious_offset);
|
||||
let pos = get_position(igid, immediates.offset);
|
||||
|
||||
if(any(previous_pos != pos))
|
||||
{
|
||||
// Invalidate pointer
|
||||
let index = gid.x + gid.z * immediates.extent.x + gid.y * immediates.extent.x * immediates.extent.z;
|
||||
table_pointers[index] = u32(1) << 31;
|
||||
}
|
||||
}
|
||||
|
||||
fn get_position(block_position: vec3<i32>, offset: vec3<i32>) -> vec3<i32>
|
||||
{
|
||||
return offset + imodv((block_position - offset), vec3<i32>(immediates.extent));
|
||||
}
|
||||
|
||||
fn imod(x: i32, n: i32) -> i32 {
|
||||
return ((x % n) + n) % n;
|
||||
}
|
||||
|
||||
fn imodv(x: vec3<i32>, n: vec3<i32>) -> vec3<i32> {
|
||||
return vec3<i32>(imod(x.x, n.x), imod(x.y, n.y), imod(x.z, n.z));
|
||||
}
|
||||
Reference in New Issue
Block a user