133 lines
3.5 KiB
Plaintext
133 lines
3.5 KiB
Plaintext
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;
|
|
}
|
|
|
|
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;
|
|
|
|
/// 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;
|
|
}
|
|
|
|
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 struct Tracer<S : IStructure, C : IVoxelCache, L : ILodPolicy>
|
|
{
|
|
|
|
public S structure;
|
|
public C cache;
|
|
public L lod;
|
|
|
|
/// Traces the closest hit at or past `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)
|
|
{
|
|
h.hit = false;
|
|
h.hit_location = float3(0.f);
|
|
h.child_index = 0;
|
|
h.node_index = 0;
|
|
h.t = 0.f;
|
|
|
|
// Start just past the previous hit.
|
|
Ray adjusted = r;
|
|
adjusted.origin += r.direction * t_min;
|
|
|
|
float best_t = asfloat(0x7f7fffff); // FLT_MAX
|
|
Hit best_hit = h;
|
|
bool found = false;
|
|
|
|
S.Cursor cursor = this.structure.begin(adjusted);
|
|
Cell cell;
|
|
|
|
[loop]
|
|
while (this.structure.next(cursor, cell))
|
|
{
|
|
// `cell.t` is relative to the advanced origin, `best_t` is not.
|
|
float cell_t = t_min + cell.t;
|
|
|
|
// No hit inside this cell can be closer than the current best.
|
|
if (cell_t >= best_t)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
Hit candidate = h;
|
|
if (cache.trace(
|
|
cell.cache_pointer,
|
|
thread_id,
|
|
cell.local_ray,
|
|
lod,
|
|
cell_t,
|
|
candidate))
|
|
{
|
|
if (candidate.t < best_t)
|
|
{
|
|
best_t = candidate.t;
|
|
best_hit = candidate;
|
|
found = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (found)
|
|
{
|
|
h = best_hit;
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|
|
|