module ray_cast; struct ByteColor { uint32_t byte_color; property uint32_t byte_r { get { return byte_color & 0xFF; } } property uint32_t byte_g { get { return (byte_color >> 8) & 0xFF; } } property uint32_t byte_b { get { return (byte_color >> 16) & 0xFF; } } property uint32_t byte_a { get { return byte_color >> 24; } } property float4 float_color { get { return float4( float(byte_r) / 255., float(byte_g) / 255., float(byte_b) / 255., float(byte_a) / 255.); } } } public struct StructurePointer { uint32_t value; bool subdivided() { return (this.value & 0x80000000) != 0; } bool pointer_valid() { return (this.value & 0x40000000) != 0; } bool subdivided_valid() { return (this.value & 0xC0000000) == 0xC0000000; } uint32_t pointer() { return this.value & 0x3FFFFFFF; } } public struct StructurePoolElement { public uint32_t occupancy_low; public uint32_t occupancy_high; public StructurePointer pointers[64]; } public struct RequestBufferElement { public Atomic requests[64]; } public struct VoxelCache { public RWStructuredBuffer structure_pool; public RWStructuredBuffer attributes_pool[2]; public RWStructuredBuffer request_buffer; public RWStructuredBuffer usage_buffer; public RWStructuredBuffer structure_table_pointer; public RWStructuredBuffer> structure_table_request_buffer; } ByteColor fetch_color(VoxelCache cache, uint32_t index, uint32_t child_index) { return ByteColor(cache.attributes_pool[0][index * 64 + child_index]); } uint32_t3 get_children_pos(float3 position, uint32_t scale_exp) { return (asuint(position) >> scale_exp) & 3; } uint32_t get_children_index(float3 position, uint32_t scale_exp) { // Get mantissa bits for this scale exp an retain bits for the specific children uint32_t3 cell_position = (asuint(position) >> scale_exp) & 3; return cell_position.x + cell_position.y * 4 + cell_position.z * 4 * 4; } uint64_t get_child_mask(uint32_t low, uint32_t high) { return ((uint64_t)high << 32) | (uint64_t)low; } float3 floor_scale(float3 position, uint32_t scale_exp) { uint32_t mask = ~0u << scale_exp; return asfloat(asuint(position) & mask); } public struct HitInformation { public bool hit; public float3 hit_pos; public uint32_t node_index; public uint32_t child_index; public float4 color; } groupshared uint32_t stack[256 * 5]; HitInformation chunk_ray_march( VoxelCache cache, float3 real_ray_direction, float3 ray_origin, uint32_t root_id, float dist_offset, uint32_t stack_index, uint32_t frame_timestamp, bool report, ) { // Cone tracing info float fov_deg = 100. / 1920.; float fov_rad = (float.getPi() * fov_deg) / 180.; float cone_factor = tan(fov_rad / 2.) * 2; // Horizontal size of pixel let st_pointer = cache.structure_table_pointer[root_id]; if (!st_pointer.subdivided()) { var hit : HitInformation; hit.hit = false; return hit; } if (!st_pointer.pointer_valid()) { // Record request if (report) { cache.structure_table_request_buffer[root_id].add(1); } var hit : HitInformation; hit.hit = false; return hit; } // Ray mirroring ray_origin += float3(1.); ray_origin = clamp(ray_origin, float(1.), asfloat(0x3fffffff)); ray_origin = select(real_ray_direction > 0., asfloat(asuint(ray_origin) ^ 0x007fffff), ray_origin); uint32_t child_mirror = 0; if (real_ray_direction.x > 0.) { child_mirror |= 3; } if (real_ray_direction.y > 0.) { child_mirror |= 3 << 2; } if (real_ray_direction.z > 0.) { child_mirror |= 3 << 4; } let ray_direction = -abs(real_ray_direction); float3 pos = ray_origin; uint32_t scale_exp = 23 - 2; uint32_t current_node_index = cache.structure_table_pointer[root_id].pointer(); if (report) { cache.usage_buffer[current_node_index] = frame_timestamp; } stack[stack_index * 5 + 10 - scale_exp / 2] = current_node_index; [loop] for (uint32_t iter = 0; iter < 500; iter++) { uint32_t child_index = get_children_index(pos, scale_exp) ^ child_mirror; StructurePointer current_node = cache.structure_pool[current_node_index].pointers[child_index]; // Scale computations let cone_size = (length(ray_origin - pos) + dist_offset) * cone_factor; let exponent = select( cone_size == 0., 0, 23 - (127 - (asuint(cone_size) >> 23))); while ( current_node.subdivided_valid() && scale_exp - 2 > exponent) { scale_exp -= 2; current_node_index = current_node.pointer(); stack[stack_index * 5 + 10 - scale_exp / 2] = current_node_index; child_index = get_children_index(pos, scale_exp) ^ child_mirror; current_node = cache.structure_pool[current_node_index].pointers[child_index]; // Write usage if (report) { cache.usage_buffer[current_node_index] = frame_timestamp; } } // Request subdiv if (report && current_node.subdivided() && !current_node.pointer_valid()) { cache.request_buffer[current_node_index].requests[child_index].add(1); } uint64_t occupancy = get_child_mask(cache.structure_pool[current_node_index].occupancy_low, cache.structure_pool[current_node_index].occupancy_high); let occupied = ((occupancy >> child_index) & 1) != 0; // if (occupied) if (fetch_color(cache, current_node_index, child_index).byte_a != 0) { var hit : HitInformation; hit.hit = true; hit.node_index = current_node_index; hit.child_index = child_index; hit.hit_pos = select( real_ray_direction > 0., asfloat(asuint(pos) ^ 0x007fffff), pos) - float3(1.); return hit; } uint32_t adv_scale_exp = scale_exp; if (((occupancy >> (child_index & 0b101010)) & 0x00330033) == 0) { adv_scale_exp++; } // Perform dda // Compute correct exponent, and shift it into the exponent part of floatt let child_pos : float3 = floor_scale(pos, adv_scale_exp); // Intersection t let inter_ts : float3 = (child_pos - ray_origin) / ray_direction; float inter_t = min(inter_ts.x, min(inter_ts.y, inter_ts.z)); // return float4(inter_t); // Perform dda step // let neighbor_max = asint(child_pos) + select(inter_t == inter_ts, -1, (1 << adv_scale_exp) - 1); let neighbor_max = asint(child_pos) + select(inter_t == inter_ts, -1, (1 << adv_scale_exp) - 1); pos = min(ray_origin + ray_direction * inter_t, asfloat(neighbor_max)); // Find most common ancestor uint32_t3 diffs = asuint(child_pos) ^ asuint(pos); uint32_t diff = (diffs.x | diffs.y | diffs.z); int32_t common_depth = (1 + (22 - firstbithigh(diff)) / 2) * 2; if (common_depth <= 0) { break; } scale_exp = 23 - common_depth; current_node_index = stack[stack_index * 5 + 10 - scale_exp / 2]; } var hit : HitInformation; hit.hit = false; return hit; } public struct RayCastContext { public float3 ray_direction; public float3 ray_origin; public uint32_t3 bounds; public uint32_t frame_timestamp; public bool cache_report; } public HitInformation ray_cast( VoxelCache cache, RayCastContext context, uint32_t thread_id) { let stack_index = thread_id; // Intersect with bounds var inter = box_intersect(context.ray_origin, context.ray_direction, float3(0.), float3(context.bounds.x, context.bounds.y, context.bounds.z)); // No hit if out of box if (inter.y <= inter.x || inter.y <= 0.) { var hit : HitInformation; hit.hit = false; return hit; } inter.x = max(0., inter.x); // Now march inside chunk bounds let start_position = inter.x * context.ray_direction + context.ray_origin; // FVT int32_t3 current_voxel = clamp( int32_t3(floor(start_position)), int32_t3(0), int32_t3(context.bounds.x - 1, context.bounds.y - 1, context.bounds.z - 1)); // int32_t3 offset = int32_t3(sign(ray_direction)); // float3 delta = abs(1. / ray_direction); float3 t = select( context.ray_direction > 0., current_voxel + int32_t3(1) - start_position, start_position - current_voxel) / abs(context.ray_direction); t += inter.x; [loop] while (true) { if (any(current_voxel < 0) || any(current_voxel >= int32_t3(context.bounds.x, context.bounds.y, context.bounds.z))) { var hit : HitInformation; hit.hit = false; return hit; } { let off = t - abs(1. / context.ray_direction); let t_adv = max(max(off.x, max(off.y, off.z)), 0.); let chunk_index = current_voxel.y + current_voxel.z * context.bounds.y + current_voxel.x * context.bounds.y * context.bounds.z; let position = context.ray_origin + context.ray_direction * t_adv; var hit = chunk_ray_march( cache, context.ray_direction, position - float3(current_voxel), chunk_index, max(t_adv, 0.01), stack_index, context.frame_timestamp, context.cache_report); if (hit.hit) { hit.hit_pos += float3(current_voxel); return hit; } } let min_mask = min_mask(t); t += select(min_mask, abs(1. / context.ray_direction), float3(0.)); current_voxel += select(min_mask, int32_t3(sign(context.ray_direction)), int32_t3(0)); } } bool3 min_mask(float3 val) { let min_val = min(val.x, min(val.y, val.z)); return val == min_val; } 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); }