module voxel_tracing; public struct Ray { public float3 origin; public float3 direction; } public struct Cell { /// Entry pointer into the cache public uint32_t cache_pointer; // entry into the cache /// Ray to be traced inside the cache public Ray local_ray; /// 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 { /// A cursor structure to keep ray state public associatedtype Cursor; /// Begins tracing a certain ray, with a span 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 } public interface ILodPolicy { /// cone / pixel-footprint termination public bool shouldStop(float t, float nodeSize); } public struct Hit { public bool hit; // Cache location public uint32_t node_index; public uint32_t child_index; 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 { public bool trace( uint32_t root, Ray r, L lod, TraceSettings settings, float offset_t, inout St stack, out Hit v); } public struct Tracer { public S structure; public C cache; public L lod; /// 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. 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( 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 * settings.t_min; 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 (visited++ < settings.max_cells && this.structure.next(cursor, cell)) { // `cell.t` is relative to the advanced origin, `best_t` is not. float cell_t = settings.t_min + cell.t; // No hit inside this cell can be closer than the current best. // 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; } Hit candidate = h; if (cache.trace( cell.cache_pointer, 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; best_hit = candidate; found = true; } } } if (found) { h = best_hit; return true; } return false; } }