Files
vxls/shaders/voxel_tracing.slang
T
2026-11-09 21:14:59 +01:00

86 lines
2.0 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;
}
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;
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);
Cell cell;
h.hit = false;
h.hit_location = float3(0.f);
h.child_index = 0;
h.node_index = 0;
while (this.structure.next(cursor, cell))
{
if (cache.trace(cell.cache_pointer, thread_id, cell.local_ray, lod, cell.t, h))
{
return true;
}
}
return false;
}
}