struct VertexOutput { @builtin(position) postion: vec4, @location(0) @interpolate(flat) chunk_index: u32, @location(1) color: vec4, @location(2) cam_pos: vec3, @location(3) world_pos: vec3, @location(4) @interpolate(flat) structure_id: u32, @location(5) chunk_position: vec3 } struct ChunkImmediate { view_proj: mat4x4, cam_pos: vec3, frame_timestamp: u32, } var constants: ChunkImmediate; //var constants: ChunkInfo; struct CacheChunkObject { transform: mat4x4, color: vec4, id: u32, pointer: u32 } struct StructurePoolElement { pointers: array } struct RequestBufferElement { requests: array, 64> } struct ColorPoolElement { colors: array } struct LocationPoolElement { structure_id: u32, structure_locator: u32 } struct SortedRequestsElement { node: u32, child: u32 } fn unpack_color(color: u32) -> vec4 { return vec4( f32(color & 0xFF) / 255., f32((color >> 8) & 0xFF) / 255., f32((color >> 16) & 0xFF) / 255., f32((color >> 24) & 0xFF) / 255. ); } @group(0) @binding(0) var structure_pool: array; @group(0) @binding(1) var color_pool: array; @group(0) @binding(2) var location_pool: array; @group(0) @binding(3) var request_buffer: array; @group(0) @binding(4) var usage_buffer: array>; @group(0) @binding(5) var structure_table_pointer: array; @group(0) @binding(6) var structure_table_request_buffer: array>; struct FragmentOutput { @location(0) color: vec4, @builtin(frag_depth) depth: f32, // Equivalent to gl_FragDepth } @vertex fn chunk(@builtin(vertex_index) index: u32, @location(0) position: vec3, @location(1) id: u32) -> VertexOutput { let cube_vertices = array, 8>( vec3(0., 0., 0.), vec3(0., 0., 1.), vec3(1., 0., 1.), vec3(1., 0., 0.), vec3(0., 1., 0.), vec3(0., 1., 1.), vec3(1., 1., 1.), vec3(1., 1., 0.), ); let cube_faces = array( // Bottom face 1, 0, 2, 3, // Top face 4, 5, 7, 6, // Side faces 0, 1, 4, 5, 1, 2, 5, 6, 2, 3, 6, 7, 3, 0, 7, 4, ); let quad_index = index / (3 * 2); let triangle_index = index % (3 * 2); let triangle_map = array( 0, 1, 2, 1, 3, 2 ); let vertex = cube_vertices[cube_faces[quad_index * 4 + triangle_map[triangle_index]]]; let output_vertex = constants.view_proj * vec4(vertex + position, 1.0f); var output: VertexOutput; output.postion = output_vertex; output.color = vec4(1.); output.chunk_index = 0; output.cam_pos = constants.cam_pos; output.world_pos = vertex + position; output.structure_id = id; output.chunk_position = position; //let output = vec4(vertex, 1.0f); return output; } struct StructureElement { children: array } struct ColorElement { children: array, 64> } struct LocationElement { children: array, 64> } struct RequestElement { children: array, 64> } fn box_inter(pos: vec3, ray_dir: vec3, box_min: vec3, box_max: vec3) -> vec2 { let box_min_t = (box_min - pos) / ray_dir; let box_max_t = (box_max - pos) / ray_dir; let near_ts = min(box_min_t, box_max_t); let far_ts = max(box_min_t, box_max_t); let far_t = min(min(far_ts.x, far_ts.y), far_ts.z); let near_t = max(max(near_ts.x, near_ts.y), near_ts.z); return vec2(near_t, far_t); } fn sdf(voxel: vec3) -> bool { let len = length(vec3(voxel) - vec3(128)) / 128.; return len <= 1.; } fn min_vec(x: vec3) -> f32 { return min(x.x, min(x.y, x.z)); } fn min_mask(x: vec3) -> vec3 { let min = min(x.x, min(x.y, x.z)); return vec3(min == x.x, min == x.y, min == x.z); } fn node_subdivided(node: u32) -> bool { return ((node >> 31) & 1) != 0; } fn node_pointer_valid(node: u32) -> bool { return ((node >> 30) & 1) != 0; } fn node_pointer(node: u32) -> u32 { return node & 0x3FFFFFFF; } fn voxel_from_wall(position: vec3, ray_dir: vec3) -> vec3 { let integers = round(position); let wall_mask = min_mask(abs(position - vec3(integers))); let offsets = select(vec3(-0.5), vec3(0.5), ray_dir > vec3(0.)); return vec3(floor(position + select(vec3(0.), offsets, wall_mask))); } struct HitResult { color: vec4, hit_pos: vec3 } fn new_traverse(ray_dir: vec3, ray_origin: vec3, root_id: u32, dist_offset: f32) -> HitResult { let max_depth = 5; let dist_offset_voxel = dist_offset * f32(1 << u32(max_depth * 2)); let fovy_deg = 100. / 1920.; let fovy_rad = (fovy_deg * 3.14) / 180.; let cone_factor = tan(fovy_rad / 2.) * 2.; let st_pointer = structure_table_pointer[root_id]; if (!node_subdivided(st_pointer)) { discard; var result: HitResult; result.color = vec4(0., 1., 0., 1.); result.hit_pos = ray_origin; return result; } if(!node_pointer_valid(st_pointer)) { // Node is subdivided, but not valid // Send request on structure table atomicAdd(&structure_table_request_buffer[root_id], 1); discard; var result: HitResult; result.color = vec4(0., 1., 0., 1.); result.hit_pos = ray_origin; return result; } //var current_node = node_pointer(st_pointer); var dfs_stack = array(node_pointer(st_pointer), 0, 0, 0, 0, 0); var current_depth = 0; var current_node = dfs_stack[current_depth]; usage_buffer[current_node] = constants.frame_timestamp; // Start location //let voxel_dir = select(vec3(-1), vec3(1), ray_dir >= vec3(0.)); var node_shift = (max_depth - current_depth) * 2; var child_size = 1 << u32(node_shift - 2); var node_size = 1 << u32(node_shift); var pos_origin = clamp(ray_origin * f32(1 << u32(max_depth * 2)), vec3(0.), vec3(f32(node_size) - 1.)); var voxel = vec3(pos_origin); var far_t = 0.; var inv_ray_dir = 1. / ray_dir; var ray_positive = ray_dir > vec3(0.); var step_dir = select(vec3(-1), vec3(1), ray_positive); for(var iter = 0; iter < 400; iter ++) { // Compute child position node_shift = (max_depth - current_depth) * 2; child_size = 1 << u32(node_shift - 2); var child_pos = (voxel >> vec3(u32(node_shift - 2))) & vec3(3); var child_index = child_pos.x + child_pos.y * 4 + child_pos.z * 4 * 4; var pointer = structure_pool[current_node].pointers[child_index]; let min_child_size = (length(vec3(voxel) - pos_origin) + dist_offset_voxel) * cone_factor; while(node_subdivided(pointer) && f32(child_size / 4) >= min_child_size ) { if(!node_pointer_valid(pointer) && node_subdivided(pointer)) { // Record request atomicAdd(&request_buffer[dfs_stack[current_depth]].requests[child_index], 1); break; } // Descend current_depth += 1; node_shift = (max_depth - current_depth) * 2; child_size = 1 << u32(node_shift - 2); child_pos = (voxel >> vec3(u32(node_shift - 2))) & vec3(3); current_node = node_pointer(pointer); dfs_stack[current_depth] = current_node; child_index = child_pos.x + child_pos.y * 4 + child_pos.z * 4 * 4; pointer = structure_pool[current_node].pointers[child_index]; // Record usage usage_buffer[current_node] = constants.frame_timestamp; } // Check color let color = color_pool[current_node].colors[child_index]; if(((color >> 24) & 0xFF) != 0) { var result: HitResult; result.color = unpack_color(color); result.hit_pos = (far_t / f32(1 << u32(max_depth * 2))) * ray_dir + ray_origin; return result; } // Advance child_pos = voxel & vec3(i32(0xFFFFFFFF << u32(node_shift - 2))); let far_wall = child_pos + select(vec3(0), vec3(child_size), ray_positive); let far_wall_inter = (vec3(far_wall) - pos_origin) * inv_ray_dir; far_t = min(min(far_wall_inter.x, far_wall_inter.y), far_wall_inter.z); // Perform dda step on the children scale //let next_child = select(child_pos, child_pos + select(vec3(-1), vec3(1), ray_dir > vec3(0.)) * vec3(child_size), vec3(far_t) == far_wall_inter); let next_child = select(child_pos, child_pos + step_dir * vec3(child_size), vec3(far_t) == far_wall_inter); let previous_voxel = voxel; voxel = clamp(vec3(pos_origin + far_t * ray_dir), next_child, next_child + vec3(child_size) - vec3(1)); if any(voxel < vec3(0)) || any(voxel >= vec3(1 << u32((max_depth * 2)))) { discard; } // We touched a voxel as if we explored blocks sized by the child size of the current node. // But we might have exited the current node. // If this is the case we have to walk back up the tree // And then back down to the next node over // As such we find the lowest ancestor that can contain both the privous voxel (in node) and the new voxel (out of node) let bit_diffs = voxel ^ previous_voxel; let bit_diffs_lowest = bit_diffs.x | bit_diffs.y | bit_diffs.z; let common_depth = ((countLeadingZeros(bit_diffs_lowest) - i32(32 - max_depth * 2)) / 2); current_depth = common_depth; current_node = dfs_stack[current_depth]; } // Iter max color var result: HitResult; result.color = vec4(1., 0., 1., 1.); result.hit_pos = (far_t / f32(1 << u32(max_depth * 2))) * ray_dir + ray_origin; return result; } @early_depth_test(less_equal) @fragment fn fragment(in: VertexOutput) -> FragmentOutput { //frag_out.color = vec4(2 * 0.01 / (100. + 0.01 - depth * (100. - 0.01))); let ray_dir = normalize(in.world_pos - in.cam_pos); let interp = box_inter(in.cam_pos - in.chunk_position, ray_dir, vec3(0.), vec3(1)); let ray_origin = (in.cam_pos - in.chunk_position) + ray_dir * (max(0., interp.x)); let result = new_traverse(ray_dir, ray_origin, in.structure_id, length(in.cam_pos - (ray_origin + in.chunk_position))); let clip_pos = constants.view_proj * vec4(result.hit_pos + in.chunk_position, 1.); let depth = clip_pos.z / clip_pos.w; var frag_out: FragmentOutput; //frag_out.color = result.color; frag_out.color = result.color; frag_out.depth = depth; return frag_out; //return vec4(ray_origin, 1.); //return frag_out; //return vec4(interp.y / 10.); } /* @fragment fn fragment(in: VertexOutput) -> @location(0) vec4 { let st = structure_table_pointer[0]; let subdivided = ((st >> 31) & 1) != 0; let pointer_valid = ((st >> 30) & 1) != 0; // Request stuff atomicAdd(&structure_table_request_buffer[0], 1); if(subdivided && !pointer_valid) { return vec4(0., 1., 0., 1.); } return vec4(1., 0., 0., 1.); } */