From a39256a0f1dffa9728f70b1631efdae3ce76f77f Mon Sep 17 00:00:00 2001 From: Albin Chaboissier Date: Mon, 9 Nov 2026 21:14:59 +0100 Subject: [PATCH] No mod in structure table --- shaders/gi.slang | 4 ++ shaders/out/gi.spv | Bin 1896 -> 2064 bytes shaders/out/voxel.spv | Bin 15616 -> 15852 bytes shaders/toroidal_structure_table.slang | 73 +++++++++++++++++-------- shaders/voxel.slang | 23 +++++--- shaders/voxel_cache.slang | 7 --- shaders/voxel_tracing.slang | 13 +---- src/main.rs | 5 ++ src/terrain.rs | 15 +++++ 9 files changed, 93 insertions(+), 47 deletions(-) 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 4b93ff24a8aa6a0192cf3d26ccf3ee5fb8b4e4f7..8b6a48cd5f9f9b50ee714b059fee58aeb3e5f18d 100644 GIT binary patch delta 179 zcmaFCH$h;72V*Q70|x^e0|Ss`U?@pU%1Mn+N-R!|FV4v?iLU^P!Q?ry$X6oCb77IM z+U&|0&1mVtzzVlQ8K^7;h+$$NV?kmWNMazPKw>$Q=Q7=7k7Z#Owy_1o3fOBfJuy8RqsA7E zNi@Y6TTDz$Os}R|Vv4a>@_qN6**&cBmm4SNKmVCCXU?2*?>?>SJF0)Fx=*Q8+OxE( zFAdLTrM{)Xsns$FZQ=YAYLA}1pths4wY|KwzIIX9k|p)+-D+J^xue6rgG>EN_L((j z;hgauor}j$+HW8Ep`|LD!Zxflpi~WZxTTG?&E@vG25ow5f($E-DD3*QmseLDYU^5? z+qydIlV3mD{>T7iATkIs=OM_Z$WUY$V!Yvn^ee{OtXr?-GMs!Jr#D|kHpX)<)r_Kj zep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~J_%@jb?oW*Owa>dT z@!V8__51Al&N%@r7y0IcjrTcYU& z-w&36`4Q{4uKe@kbEJJWKA$Ib{6C-n2Q(JezW@LL 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,