refactor: cleaner request path + raytracing code

This commit is contained in:
2026-11-23 11:32:16 +01:00
parent dd0e102348
commit dd5fdc13b8
20 changed files with 1427 additions and 618 deletions
+30 -12
View File
@@ -129,18 +129,31 @@ public interface IVoxelHit<let N : uint32_t>
public bool shouldHit(VoxelCacheParameters<N> parameters, uint32_t node_index, uint32_t child_index);
}
static const uint32_t WG_COUNT = 256;
groupshared uint32_t stack[5 * WG_COUNT];
/// Default hit test for the demo layout: alpha != 0 means solid.
public struct ColorHit<let N : uint32_t> : IVoxelHit<N>
{
public __init() {};
public bool shouldHit(VoxelCacheParameters<N> cache, uint32_t node_index, uint32_t child_index)
{
return ByteColor(cache.attributes_pool[0][node_index * 64 + child_index]).byte_a != 0;
}
}
public struct VoxelCacheTracer<let N : int, H : IVoxelHit<N>> : IVoxelCache
{
public bool report_usage;
public bool report_requests;
public uint32_t frame_timestamp;
public H voxel_hit;
public VoxelCacheParameters<N> parameters;
public bool trace<L : ILodPolicy>(uint32_t root, uint32_t thread_id, Ray r, L lod, float offset_t, out Hit v)
public bool trace<L : ILodPolicy, St : ITraceStack>(
uint32_t root,
Ray r,
L lod,
TraceSettings settings,
float offset_t,
inout St stack,
out Hit v)
{
v.t = 0.;
@@ -168,15 +181,20 @@ public struct VoxelCacheTracer<let N : int, H : IVoxelHit<N>> : IVoxelCache
float3 pos = r.origin;
uint32_t scale_exp = 23 - 2;
uint32_t current_node_index = root;
if (this.report_usage)
if (settings.report_usage)
{
this.parameters.usage_buffer[current_node_index] = this.frame_timestamp;
}
stack[thread_id + WG_COUNT * (10 - scale_exp / 2)] = current_node_index;
stack.set(10 - scale_exp / 2, current_node_index);
[loop]
for (uint32_t iter = 0; iter < 500; iter++)
for (uint32_t iter = 0; iter < settings.max_steps; iter++)
{
if (offset_t + t >= settings.t_max)
{
break;
}
uint32_t child_index = get_children_index(pos, scale_exp) ^ child_mirror;
StructurePointer current_node = this.parameters.structure_pool[current_node_index].pointers[child_index];
@@ -184,18 +202,18 @@ public struct VoxelCacheTracer<let N : int, H : IVoxelHit<N>> : IVoxelCache
{
scale_exp -= 2;
current_node_index = current_node.pointer();
stack[thread_id + WG_COUNT * (10 - scale_exp / 2)] = current_node_index;
stack.set(10 - scale_exp / 2, current_node_index);
child_index = get_children_index(pos, scale_exp) ^ child_mirror;
current_node = this.parameters.structure_pool[current_node_index].pointers[child_index];
// Write usage
if (this.report_usage)
if (settings.report_usage)
{
this.parameters.usage_buffer[current_node_index] = frame_timestamp;
}
}
// Request subdiv
if (this.report_requests && !lod.shouldStop(t + offset_t, node_size(scale_exp)) && current_node.subdivided() && !current_node.pointer_valid())
if (settings.report_requests && !lod.shouldStop(t + offset_t, node_size(scale_exp)) && current_node.subdivided() && !current_node.pointer_valid())
{
this.parameters.request_buffer[current_node_index].requests[child_index].add(1);
}
@@ -253,7 +271,7 @@ public struct VoxelCacheTracer<let N : int, H : IVoxelHit<N>> : IVoxelCache
}
scale_exp = 23 - common_depth;
current_node_index = stack[thread_id + WG_COUNT * (10 - scale_exp / 2)];
current_node_index = stack.get(10 - scale_exp / 2);
}
v.hit = false;