import voxel_tracing; import voxel_cache; public struct Aabb { public float3 min; uint32_t _pad0; // std430 aligns float3 to 16 bytes public float3 max; uint32_t _pad1; } 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; float t; } public struct BvhParameters { StructuredBuffer nodes; StructuredBuffer objects; RWStructuredBuffer slots; RWStructuredBuffer> requests; } static const uint32_t STACK_SIZE = 8; static const uint32_t STACK_MASK = 7; 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 outer_bound = 0; while (outer_bound < 300) { outer_bound++; uint32_t inner_bound = 0; if (parameters.nodes[c.current_node].is_internal() && inner_bound < 300) { inner_bound++; let left_index = parameters.nodes[c.current_node].first_or_left; float2 left = box_intersect(c.ray.origin, c.ray.direction, parameters.nodes[left_index].aabb.min, parameters.nodes[left_index].aabb.max); float2 right = box_intersect(c.ray.origin, c.ray.direction, parameters.nodes[left_index + 1].aabb.min, parameters.nodes[left_index + 1].aabb.max); bool left_hit = left.y >= left.x && left.y >= 0.; bool right_hit = right.y >= right.x && right.y >= 0.; if (left_hit && right_hit) { // Select closest let go_left = left.y < right.y; c.current_node = select(go_left, left_index, left_index + 1); this.push(select(go_left, left_index + 1, left_index), c); c.depth += 1; } else if (left_hit || right_hit) { c.current_node = select(left_hit, left_index, left_index + 1); c.bit_stack |= 1 << c.depth; c.depth += 1; } else { if (!this.pop(c.current_node, c)) { return false; } } } if (parameters.nodes[c.current_node].is_leaf()) { // We are on a leaf uint32_t offset = parameters.nodes[c.current_node].first_or_left; uint32_t count = parameters.nodes[c.current_node].count; while (c.current_object < count) { if (objectHit(offset + c.current_object, c.ray, cell)) { return true; } c.current_object += 1; } // Finisehd objects of this node c.current_object = 0; if (!this.pop(c.current_node, c)) { return false; } } } return true; } 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 intersection = box_intersect(origin.xyz, direction.xyz, float3(0.), float3(1.)); return intersection.y >= intersection.x && intersection.y >= 0.; } void push(uint32_t node, inout BvhCursor c) { stack[c.stack_head * 256 + this.thread_index] = node; stack_depth[c.stack_head * 256 + this.thread_index] = c.depth; c.stack_head += 1; c.stack_count += 1; } bool pop(out uint32_t node, inout BvhCursor c) { if (c.stack_count == 0) { return false; } c.stack_head -= 1; c.stack_count -= 1; node = stack[c.stack_head * 256 + this.thread_index]; c.depth = stack_depth[c.stack_head * 256 + this.thread_index]; 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); }