diff --git a/shaders/gi.slang b/shaders/gi.slang index efc6493..d9d881a 100644 --- a/shaders/gi.slang +++ b/shaders/gi.slang @@ -9,6 +9,10 @@ struct PushConstants uint32_t chunk_width; uint32_t chunk_height; uint32_t chunk_alt; + + uint32_t table_base_slot_x; + uint32_t table_base_slot_y; + uint32_t table_base_slot_z; } [[vk::push_constant]] diff --git a/shaders/out/gi.spv b/shaders/out/gi.spv index 4b93ff2..8b6a48c 100644 Binary files a/shaders/out/gi.spv and b/shaders/out/gi.spv differ diff --git a/shaders/out/voxel.spv b/shaders/out/voxel.spv index a938871..a4054d7 100644 Binary files a/shaders/out/voxel.spv and b/shaders/out/voxel.spv differ diff --git a/shaders/toroidal_structure_table.slang b/shaders/toroidal_structure_table.slang index c88bb99..b47a5b6 100644 --- a/shaders/toroidal_structure_table.slang +++ b/shaders/toroidal_structure_table.slang @@ -12,21 +12,24 @@ public struct ToroidalStructureTable public struct ToroidalStructureTableChunksCursor { - // Offset of the block of chuks + /// World-space minimum corner of the visible chunk block. int32_t3 offset; + + /// Current chunk in world space. This is deliberately *not* wrapped: it is + /// used for the block bounds and to compute the ray position inside the + /// chunk. int32_t3 current_voxel; - float3 t_off; - float3 t; + + /// Current chunk in table-slot space. Always in [0, extent), advanced + /// incrementally so the hot loop never needs an integer modulo. + int32_t3 local_voxel; + + float3 t_off; + float3 t; Ray r; } -int32_t imod(int32_t x, int32_t n) -{ - int32_t r = x % n; - return r < 0 ? r + n : r; -} - /// Uses a fixed structure table as a dense array of chunks public struct ToroidalStructureTableTracer : IStructure { @@ -35,7 +38,12 @@ public struct ToroidalStructureTableTracer : IStructure public ToroidalStructureTable structure_table; public uint32_t3 extent; - public ToroidalStructureTableChunksCursor begin(Ray worldRay, Span s) + /// Canonical table slot of the block minimum corner: + /// `imod(current_offset, extent)`. Computed on the CPU once per table move + /// so the shader does not need integer modulo anywhere. + public uint32_t3 base_slot; + + public ToroidalStructureTableChunksCursor begin(Ray worldRay) { ToroidalStructureTableChunksCursor cursor; // Now march inside chunk bounds @@ -45,7 +53,17 @@ public struct ToroidalStructureTableTracer : IStructure cursor.current_voxel = int32_t3(floor(start_position)); cursor.offset = cursor.current_voxel - int32_t3(this.extent) / 2; - cursor.t_off = abs(worldRay.invDirection); + // The block is exactly one table period wide. Starting from the base + // slot of the block corner, the slot of the current chunk is the + // block-local coordinate plus that base, wrapped once. + let block_local = cursor.current_voxel - cursor.offset; + cursor.local_voxel = block_local + int32_t3(this.base_slot); + cursor.local_voxel = select( + cursor.local_voxel >= int32_t3(this.extent), + cursor.local_voxel - int32_t3(this.extent), + cursor.local_voxel); + + cursor.t_off = abs(1. / worldRay.direction); // Take t of next boundary cursor.t = select( @@ -66,12 +84,10 @@ public struct ToroidalStructureTableTracer : IStructure // Current hit let off = c.t - c.t_off; let t_adv = max(max(off.x, max(off.y, off.z)), 0.); - let local_voxel = - int32_t3( - imod(c.current_voxel.x, int32_t(this.extent.x)), - imod(c.current_voxel.y, int32_t(this.extent.y)), - imod(c.current_voxel.z, int32_t(this.extent.z))); - let chunk_index = local_voxel.x + local_voxel.z * this.extent.x + local_voxel.y * this.extent.x * this.extent.z; + let chunk_index = + c.local_voxel.x + + c.local_voxel.z * this.extent.x + + c.local_voxel.y * this.extent.x * this.extent.z; let position = c.r.origin + c.r.direction * t_adv; var hit = false; @@ -80,7 +96,6 @@ public struct ToroidalStructureTableTracer : IStructure cell.cache_pointer = this.structure_table.structure_table_pointer[chunk_index].pointer(); cell.local_ray.origin = position - float3(c.current_voxel); cell.local_ray.direction = c.r.direction; - cell.local_ray.invDirection = c.r.invDirection; cell.t = t_adv; hit = true; } @@ -90,10 +105,24 @@ public struct ToroidalStructureTableTracer : IStructure hit = false; } - // Advance - let min_mask = min_mask(c.t); - c.t += select(min_mask, c.t_off, float3(0.)); - c.current_voxel += select(min_mask, int32_t3(sign(c.r.direction)), int32_t3(0)); + // Advance. One axis may step by +/-1 (or several axes on a tie). + let advance = min_mask(c.t); + let step = select(advance, int32_t3(sign(c.r.direction)), int32_t3(0)); + c.t += select(advance, c.t_off, float3(0.)); + c.current_voxel += step; + + // Keep the table slot wrapped incrementally. Since `local_voxel` + // is in [0, extent) and `step` is at most one per axis, a single + // conditional add/sub is enough. + c.local_voxel += step; + c.local_voxel = select( + c.local_voxel >= int32_t3(this.extent), + c.local_voxel - int32_t3(this.extent), + c.local_voxel); + c.local_voxel = select( + c.local_voxel < int32_t3(0), + c.local_voxel + int32_t3(this.extent), + c.local_voxel); if (hit) { diff --git a/shaders/voxel.slang b/shaders/voxel.slang index e81a363..aa4331b 100644 --- a/shaders/voxel.slang +++ b/shaders/voxel.slang @@ -1,6 +1,6 @@ // import ray_cast; import voxel_cache; -import fixed_structure_table; +// import fixed_structure_table; import toroidal_structure_table; import voxel_tracing; import lod_policies; @@ -16,6 +16,12 @@ struct PushConstants uint32_t chunk_width; uint32_t chunk_height; uint32_t chunk_alt; + + // Canonical table slot of the visible block's minimum corner. Computed on + // the CPU from the toroidal table's current offset. + uint32_t table_base_slot_x; + uint32_t table_base_slot_y; + uint32_t table_base_slot_z; } public struct VertexOutput @@ -115,8 +121,6 @@ void gi_march(uint32_t3 location: SV_DispatchThreadID, uint32_t3 local_location: } } -groupshared VoxelTraceStack stacks[256]; - [shader("compute")] [numthreads(16, 16, 1)] void ray_march_compute(uint32_t3 location: SV_DispatchThreadID, uint32_t3 local_location: SV_GroupThreadID) @@ -137,14 +141,18 @@ void ray_march_compute(uint32_t3 location: SV_DispatchThreadID, uint32_t3 local_ ToroidalStructureTableTracer table_tracer; table_tracer.structure_table = table_parameters; - table_tracer.extent = uint32_t3(constants.chunk_width, constants.chunk_alt, constants.chunk_width); + table_tracer.extent = + uint32_t3(constants.chunk_width, constants.chunk_alt, constants.chunk_height); + table_tracer.base_slot = uint32_t3( + constants.table_base_slot_x, + constants.table_base_slot_y, + constants.table_base_slot_z); let ray_direction = get_ray_direction(location.xy); Ray r; r.origin = constants.cam_pos; r.direction = ray_direction; - r.invDirection = 1.f / ray_direction; PixelFootprintLod lod = PixelFootprintLod.create((100.f * 3.14f) / 180.f, 1080.f, 2.f); @@ -154,10 +162,9 @@ void ray_march_compute(uint32_t3 location: SV_DispatchThreadID, uint32_t3 local_ tracer.lod = lod; Hit h; - Span s; - bool hit = tracer.trace(stack_index, h, r, s); + bool hit = tracer.trace(stack_index, h, r); - ToroidalStructureTableTracer.Cursor cursor = table_tracer.begin(r, s); + ToroidalStructureTableTracer.Cursor cursor = table_tracer.begin(r); Cell cell; if (hit) diff --git a/shaders/voxel_cache.slang b/shaders/voxel_cache.slang index 6f13d94..efc2769 100644 --- a/shaders/voxel_cache.slang +++ b/shaders/voxel_cache.slang @@ -90,11 +90,6 @@ public struct VoxelCacheParameters public RWStructuredBuffer attributes_pool[N]; } -public struct VoxelTraceStack -{ - public uint32_t stack[5]; -} - uint32_t3 get_children_pos(float3 position, uint32_t scale_exp) { return (asuint(position) >> scale_exp) & 3; @@ -139,8 +134,6 @@ groupshared uint32_t stack[5 * WG_COUNT]; public struct VoxelCacheTracer> : IVoxelCache { - typedef VoxelTraceStack GroupSharedData; - public bool report_usage; public bool report_requests; public uint32_t frame_timestamp; diff --git a/shaders/voxel_tracing.slang b/shaders/voxel_tracing.slang index fb3a9d3..58d3aa6 100644 --- a/shaders/voxel_tracing.slang +++ b/shaders/voxel_tracing.slang @@ -4,13 +4,6 @@ public struct Ray { public float3 origin; public float3 direction; - public float3 invDirection; -} - -public struct Span -{ - public float t0; - public float t1; } public struct Cell @@ -31,7 +24,7 @@ public interface IStructure public associatedtype Cursor; /// Begins tracing a certain ray, with a span - public Cursor begin(Ray worldRay, Span s); + public Cursor begin(Ray worldRay); /// Returns whether it hits, updating the state, and returning a cell structure if hit. public bool next(inout Cursor c, out Cell cell); // next populated cell along the ray @@ -67,10 +60,10 @@ public struct Tracer public C cache; public L lod; - public bool trace(uint32_t thread_id, out Hit h, Ray r, Span s) + public bool trace(uint32_t thread_id, out Hit h, Ray r) { /* loop: next cell -> cache.trace -> early out */ - S.Cursor cursor = this.structure.begin(r, s); + S.Cursor cursor = this.structure.begin(r); Cell cell; h.hit = false; diff --git a/src/main.rs b/src/main.rs index 7f7f25c..ac0e5b5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -173,6 +173,8 @@ struct Immediates chunk_width: u32, chunk_height: u32, chunk_alt: u32, + + table_base_slot: [u32; 3], } #[derive(Debug, Clone, Copy, Zeroable, Pod)] @@ -1130,6 +1132,7 @@ impl State &[], ); renderpass.set_bind_group(2, Some(&surface_bg), &[]); + let table_base_slot = self.structure_table.lock().base_slot(); let imm = [Immediates { view_proj: self.camera.view_proj().inverse(), cam_pos: self.camera.position, @@ -1143,6 +1146,8 @@ impl State chunk_width: self.terrain_width as u32, chunk_height: self.terrain_height as u32, chunk_alt: self.terrain_alt as u32, + + table_base_slot, }]; renderpass.set_immediates(0, unsafe { as_raw_bytes(&imm) }); renderpass.dispatch_workgroups( diff --git a/src/terrain.rs b/src/terrain.rs index 47f4a48..f208a0c 100644 --- a/src/terrain.rs +++ b/src/terrain.rs @@ -191,6 +191,21 @@ impl ToroidalStructureTable (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), structure_id: usize,