feat: Starting bvh raytracing

This commit is contained in:
2026-11-12 22:47:17 +01:00
parent 27f660f6fb
commit e7fbeb7887
7 changed files with 501 additions and 147 deletions
+305
View File
@@ -0,0 +1,305 @@
import voxel_tracing;
import voxel_cache;
public struct Aabb
{
public float min_x;
public float min_y;
public float min_z;
uint32_t _pad0;
public float max_x;
public float max_y;
public float max_z;
uint32_t _pad1;
public float3 min()
{
return float3(this.min_x, this.min_y, this.min_z);
}
public float3 max()
{
return float3(this.max_x, this.max_y, this.max_z);
}
}
public struct BvhNode
{
public Aabb aabb;
public uint32_t count;
public uint32_t first_or_left;
public bool is_leaf()
{
return this.count != 0;
}
public bool is_internal()
{
return this.count == 0;
}
}
public struct BvhObject
{
public float4x4 local_to_world;
public float4x4 world_to_local;
public uint32_t table_slot;
uint32_t _pad0;
uint32_t _pad1;
uint32_t _pad2;
}
public struct BvhCursor
{
Ray ray;
// One bit per level, bit 0 means left child unexplored, bit one, child explored
uint64_t bit_stack;
// Where to write the node when descending
uint32_t stack_head;
uint32_t stack_count;
uint32_t current_object;
uint32_t current_node;
uint32_t depth;
}
public struct BvhParameters
{
StructuredBuffer<BvhNode> nodes;
StructuredBuffer<BvhObject> objects;
RWStructuredBuffer<StructurePointer> slots;
RWStructuredBuffer<Atomic<uint32_t>> requests;
}
static const uint32_t STACK_SIZE = 4;
static const uint32_t STACK_MASK = 0b11;
groupshared uint32_t stack[STACK_SIZE * 256];
groupshared uint32_t stack_depth[STACK_SIZE * 256];
public struct Bvh : IStructure
{
typedef BvhCursor Cursor;
public BvhParameters parameters;
public uint32_t thread_index;
public BvhCursor begin(Ray world_ray)
{
BvhCursor c;
c.ray = world_ray;
c.bit_stack = 0;
c.stack_head = 0;
c.stack_count = 0;
c.current_object = 0;
c.current_node = 0;
c.depth = 0;
this.push(0, c);
return c;
}
public bool next(inout BvhCursor c, out Cell cell)
{
uint32_t node_index = 0;
uint64_t out_b = 0;
while (true && out_b < 80)
{
out_b += 1;
uint32_t b = 0;
while (parameters.nodes[node_index].is_internal() && b < 80)
{
b++;
uint32_t left = parameters.nodes[node_index].first_or_left;
float2 inter1 = box_intersect(c.ray.origin, c.ray.direction, parameters.nodes[left].aabb.min(), parameters.nodes[left].aabb.max());
float2 inter2 = box_intersect(c.ray.origin, c.ray.direction, parameters.nodes[left + 1].aabb.min(), parameters.nodes[left + 1].aabb.max());
bool hit1 = inter1.x <= inter1.y && inter1.y >= 0;
bool hit2 = inter2.x <= inter2.y && inter2.y >= 0;
if (hit1 && hit2)
{
let near = select(inter1.x < inter2.x, left, left + 1);
let far = select(inter1.x < inter2.x, left + 1, left);
// Push far
this.push(far, c);
// Go to near
c.depth += 1;
node_index = near;
}
else if (hit1 || hit2)
{
// Go to hit
c.depth += 1;
node_index = select(hit1, left, left + 1);
}
else
{
// none hit, go to next
if (!this.pop(node_index, c))
{
return false;
}
}
}
if (parameters.nodes[node_index].is_leaf())
{
let first = parameters.nodes[node_index].first_or_left;
for (uint32_t i = first; i < first + parameters.nodes[node_index].count; i++)
{
if (this.objectHit(i, c.ray, cell))
{
return true;
}
}
}
}
return false;
}
bool objectHit(uint32_t object_index, Ray r, out Cell c)
{
// Project direction and view in local space
let direction = mul(parameters.objects[object_index].world_to_local, float4(r.direction, 0));
let origin = mul(parameters.objects[object_index].world_to_local, float4(r.origin, 1));
float2 inter = box_intersect(origin.xyz, direction.xyz, float3(0.), float3(1.));
return inter.x <= inter.y && inter.y >= 0.;
}
void push(uint32_t node, inout BvhCursor c)
{
stack[(c.stack_head & STACK_MASK) * 256 + this.thread_index] = node;
stack_depth[(c.stack_head & STACK_MASK) * 256 + this.thread_index] = c.depth;
c.stack_head += 1;
c.stack_count = max(c.stack_count + 1, STACK_SIZE);
}
bool pop(out uint32_t node, inout BvhCursor c)
{
if (c.stack_count > 0)
{
c.stack_head -= 1;
c.stack_count -= 1;
node = stack[(c.stack_head & STACK_MASK) * 256 + this.thread_index];
c.depth = stack_depth[(c.stack_head & STACK_MASK) * 256 + this.thread_index];
// This node is done, mark it ok, remove bits of deeper levels, they're done
c.bit_stack = (c.bit_stack | 1 << c.depth) & ((2 << c.depth) - 1);
return true;
}
return false;
// Stack is underflowed, we need to restart
uint64_t above = (~c.bit_stack) & ((1 << c.depth) - 1);
if (above == 0)
{
// Then all upper levels are one, meaning they've been popped once
// Meaning we were in a far->far->far... ->far path.
// so we have explored everything
return false;
}
// Select the depth at which we want to go back down, which is the first 0
// bit
uint32_t return_depth = firstbithigh(above);
// Since we visited that level, we mark it visited
c.bit_stack = (c.bit_stack | 1 << return_depth) & ((2 << return_depth) - 1);
// Reset stack
c.stack_head = 0;
c.stack_count = 0;
// Travel back down to level using the bit stack
node = 0;
for (uint32_t l = 0; l < return_depth; l++)
{
bool go_far = ((c.bit_stack >> l) & 1) == 1;
uint32_t left = parameters.nodes[node].first_or_left;
float2 inter1 = box_intersect(c.ray.origin, c.ray.direction, parameters.nodes[left].aabb.min(), parameters.nodes[left].aabb.max());
float2 inter2 = box_intersect(c.ray.origin, c.ray.direction, parameters.nodes[left + 1].aabb.min(), parameters.nodes[left + 1].aabb.max());
bool hit1 = inter1.x <= inter1.y && inter1.y >= 0;
bool hit2 = inter2.x <= inter2.y && inter2.y >= 0;
if (hit1 && hit2)
{
let near = select(inter1.x < inter2.x, left, left + 1);
let far = select(inter1.x < inter2.x, left + 1, left);
node = select(go_far, far, near);
if (!go_far)
{
this.push(far, c);
}
}
else if (hit1 || hit2)
{
// At this level, we must have chosen the one that hit
node = select(hit1, left, left + 1);
}
}
// Hit the far node
{
uint32_t left = parameters.nodes[node].first_or_left;
float2 inter1 = box_intersect(c.ray.origin, c.ray.direction, parameters.nodes[left].aabb.min(), parameters.nodes[left].aabb.max());
float2 inter2 = box_intersect(c.ray.origin, c.ray.direction, parameters.nodes[left + 1].aabb.min(), parameters.nodes[left + 1].aabb.max());
bool hit1 = inter1.x <= inter1.y && inter1.y >= 0;
bool hit2 = inter2.x <= inter2.y && inter2.y >= 0;
if (hit1 && hit2)
{
node = select(inter1.x < inter2.x, left + 1, left);
}
else if (hit1 || hit2)
{
// At this level, we must have chosen the one that hit
node = select(hit1, left, left + 1);
}
else
{
// Not hit, we can pop
if (c.stack_count == 0)
{
return false;
}
else
{
c.stack_head -= 1;
c.stack_count -= 1;
node = stack[(c.stack_head & STACK_MASK) * 256 + this.thread_index];
c.depth = stack_depth[(c.stack_head & STACK_MASK) * 256 + this.thread_index];
// This node is done, mark it ok, remove bits of deeper levels, they're done
c.bit_stack = (c.bit_stack | 1 << c.depth) & ((2 << c.depth) - 1);
}
}
}
return true;
}
}
float2 box_intersect(float3 origin, float3 ray_direction, float3 box_min, float3 box_max)
{
let min_ts = (box_min - origin) / ray_direction;
let max_ts = (box_max - origin) / ray_direction;
let far_ts = max(min_ts, max_ts);
let near_ts = min(min_ts, max_ts);
let far_t = min(far_ts.x, min(far_ts.y, far_ts.z));
let near_t = max(near_ts.x, max(near_ts.y, near_ts.z));
return float2(near_t, far_t);
}