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
+139 -13
View File
@@ -16,6 +16,13 @@ public struct Cell
/// How much distance from the camera
public float t;
/// Rotation/scale from the cell's unit-cube space to world space.
/// Identity for axis-aligned chunks.
public float3x3 cube_to_world;
/// World-space position of the cell's unit-cube origin.
public float3 world_origin;
}
public interface IStructure
@@ -43,18 +50,99 @@ public struct Hit
// Cache location
public uint32_t node_index;
public uint32_t child_index;
public float3 hit_location;
public float3 hit_location; // world space
/// Rotation applied to the hit's local attribute normals.
public float3x3 normal_to_world;
/// Distance of the hit from the ray origin, in the units of the ray
/// direction. Used to order hits and to restart the traversal past a hit.
public float t;
}
/// Identity matrix helper (scalar-to-matrix casts splat instead of making an
/// identity in Slang).
public float3x3 mat3_identity()
{
return float3x3(
1., 0., 0.,
0., 1., 0.,
0., 0., 1.);
}
/// Per-ray parameters for a trace call.
///
/// Primary and secondary rays use the same tracer; only these values differ.
/// `t_min`/`t_max` are world-space distances along the (normalised) ray
/// direction. A hit at exactly `t_max` is excluded, so `t_max` is an open
/// interval end.
public struct TraceSettings
{
/// Start of the search interval. Re-traces set this just past the
/// previous hit.
public float t_min;
/// End of the search interval. Use FLT_MAX for unbounded traces.
public float t_max;
/// Maximum number of structure cells to visit.
public uint32_t max_cells;
/// Maximum number of DDA iterations inside one cache cell.
public uint32_t max_steps;
/// Stop at the first acceptable hit instead of accumulating the nearest
/// one. Meant for shadow/visibility rays.
public bool any_hit;
/// Refresh the LRU timestamp of every node the ray touches.
public bool report_usage;
/// Emit cache detail requests for nodes the ray reaches at the LOD.
public bool report_requests;
}
/// Per-ray stack access owned by the caller.
///
/// The trace core never stores the stack itself, so a front-end chooses where
/// it lives. [`LocalTraceStack`] keeps it in per-thread storage. A primary pass
/// that wants groupshared memory implements this interface over a module-scope
/// `groupshared` array plus a thread slot instead; no trace-core code changes.
///
/// The depth index (`10 - scale_exp / 2`) assumes branching 4 and
/// `chunk_power = 5`, so implementations store five entries.
public interface ITraceStack
{
public uint32_t get(uint32_t depth);
[mutating] public void set(uint32_t depth, uint32_t node);
}
public struct LocalTraceStack : ITraceStack
{
public uint32_t entries[5];
public uint32_t get(uint32_t depth)
{
return this.entries[depth];
}
[mutating]
public void set(uint32_t depth, uint32_t node)
{
this.entries[depth] = node;
}
}
public interface IVoxelCache
{
// The voxel cache tracer needs to store a small stack.
// Keeping it in registers is wastefull thus using groupshared memory is the best option
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);
}
public struct Tracer<S : IStructure, C : IVoxelCache, L : ILodPolicy>
@@ -64,40 +152,56 @@ public struct Tracer<S : IStructure, C : IVoxelCache, L : ILodPolicy>
public C cache;
public L lod;
/// Traces the closest hit at or past `t_min`.
/// Traces the closest hit (or the first acceptable hit when
/// `settings.any_hit`) at or past `settings.t_min`.
///
/// The structure may return cells in any order (the BVH restart trail is
/// not distance ordered). Instead of returning on the first cache hit, we
/// keep the closest hit found so far and skip cells that start beyond it:
/// every hit inside a cell is at least `cell.t` away, so a cell with
/// `entry >= best_t` cannot improve the result.
public bool trace(uint32_t thread_id, float t_min, out Hit h, Ray r)
/// `entry >= best_t` cannot improve the result. Because the cells are not
/// ordered, such cells are skipped and never used to break the loop.
///
/// `stack` is caller-owned trace state. Keeping it out of the cache tracer
/// lets primary and secondary front-ends choose their own storage.
public bool trace<St : ITraceStack>(
out Hit h,
Ray r,
TraceSettings settings,
inout St stack)
{
h.hit = false;
h.hit_location = float3(0.f);
h.child_index = 0;
h.node_index = 0;
h.normal_to_world = mat3_identity();
h.t = 0.f;
// Start just past the previous hit.
Ray adjusted = r;
adjusted.origin += r.direction * t_min;
adjusted.origin += r.direction * settings.t_min;
float best_t = asfloat(0x7f7fffff); // FLT_MAX
float best_t = settings.t_max;
Hit best_hit = h;
bool found = false;
S.Cursor cursor = this.structure.begin(adjusted);
Cell cell;
uint32_t visited = 0;
[loop]
while (this.structure.next(cursor, cell))
while (visited++ < settings.max_cells && this.structure.next(cursor, cell))
{
// `cell.t` is relative to the advanced origin, `best_t` is not.
float cell_t = t_min + cell.t;
float cell_t = settings.t_min + cell.t;
// No hit inside this cell can be closer than the current best.
if (cell_t >= best_t)
// Cells are not ordered, so skip instead of breaking.
if (cell_t >= settings.t_max)
{
continue;
}
if (!settings.any_hit && cell_t >= best_t)
{
continue;
}
@@ -105,12 +209,34 @@ public struct Tracer<S : IStructure, C : IVoxelCache, L : ILodPolicy>
Hit candidate = h;
if (cache.trace(
cell.cache_pointer,
thread_id,
cell.local_ray,
lod,
settings,
cell_t,
stack,
candidate))
{
// The cache enforces `t_max`; the guard keeps a misbehaving
// implementation from accepting an out-of-range hit.
if (candidate.t >= settings.t_max)
{
continue;
}
// The cache reports hits in its unit-cube space. Bring the
// position and the local attribute normal into world space so
// front-ends can use the hit directly, including as the origin
// of secondary rays.
candidate.hit_location =
cell.world_origin + mul(cell.cube_to_world, candidate.hit_location);
candidate.normal_to_world = cell.cube_to_world;
if (settings.any_hit)
{
h = candidate;
return true;
}
if (candidate.t < best_t)
{
best_t = candidate.t;