No mod in structure table

This commit is contained in:
2026-11-09 21:14:59 +01:00
parent 918eb97342
commit a39256a0f1
9 changed files with 93 additions and 47 deletions
+4
View File
@@ -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]]
Binary file not shown.
Binary file not shown.
+51 -22
View File
@@ -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)
{
+15 -8
View File
@@ -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)
-7
View File
@@ -90,11 +90,6 @@ public struct VoxelCacheParameters<let N : uint>
public RWStructuredBuffer<uint32_t> 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<let N : int, H : IVoxelHit<N>> : IVoxelCache
{
typedef VoxelTraceStack GroupSharedData;
public bool report_usage;
public bool report_requests;
public uint32_t frame_timestamp;
+3 -10
View File
@@ -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<S : IStructure, C : IVoxelCache, L : ILodPolicy>
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;
+5
View File
@@ -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(
+15
View File
@@ -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,