94 lines
2.2 KiB
Plaintext
94 lines
2.2 KiB
Plaintext
module voxel_tracing;
|
|
|
|
public struct Ray
|
|
{
|
|
public float3 origin;
|
|
public float3 direction;
|
|
public float3 invDirection;
|
|
}
|
|
|
|
public struct Span
|
|
{
|
|
public float t0;
|
|
public float t1;
|
|
}
|
|
|
|
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, Span s);
|
|
|
|
/// 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
|
|
associatedtype GroupSharedData;
|
|
public bool trace<L : ILodPolicy>(uint32_t root, inout GroupSharedData data, 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(inout C.GroupSharedData gsData, out Hit h, Ray r, Span s)
|
|
{
|
|
/* loop: next cell -> cache.trace -> early out */
|
|
S.Cursor cursor = this.structure.begin(r, s);
|
|
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, gsData, cell.local_ray, lod, cell.t, h))
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|
|
|