From 54c5e91a7f18e770dcbbaf40f512e8645bf96da5 Mon Sep 17 00:00:00 2001 From: Albin Chaboissier Date: Fri, 11 Sep 2026 17:12:38 +0200 Subject: [PATCH] Fix RT timer --- shaders/voxel.wgsl | 221 +++------------------------------------------ src/main.rs | 30 +++--- 2 files changed, 29 insertions(+), 222 deletions(-) diff --git a/shaders/voxel.wgsl b/shaders/voxel.wgsl index 5381edc..78d3109 100644 --- a/shaders/voxel.wgsl +++ b/shaders/voxel.wgsl @@ -225,6 +225,7 @@ fn new_traverse(ray_dir: vec3, ray_origin: vec3, root_id: u32, dist_of if (!node_subdivided(st_pointer)) { + discard; var result: HitResult; result.color = vec4(0., 1., 0., 1.); result.hit_pos = ray_origin; @@ -236,6 +237,7 @@ fn new_traverse(ray_dir: vec3, ray_origin: vec3, root_id: u32, dist_of // 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; @@ -243,11 +245,11 @@ fn new_traverse(ray_dir: vec3, ray_origin: vec3, root_id: u32, dist_of } //var current_node = node_pointer(st_pointer); - // Record usage 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[dfs_stack[current_depth]] = constants.frame_timestamp; + usage_buffer[current_node] = constants.frame_timestamp; // Start location //let voxel_dir = select(vec3(-1), vec3(1), ray_dir >= vec3(0.)); @@ -270,7 +272,8 @@ fn new_traverse(ray_dir: vec3, ray_origin: vec3, root_id: u32, dist_of child_size = 1 << u32(node_shift - 2); var child_pos = (voxel >> vec3(u32(node_shift - 2))) & vec3(3); - var pointer = structure_pool[dfs_stack[current_depth]].pointers[child_pos.x + child_pos.y * 4 + child_pos.z * 4 * 4]; + 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) && @@ -280,7 +283,7 @@ fn new_traverse(ray_dir: vec3, ray_origin: vec3, root_id: u32, dist_of if(!node_pointer_valid(pointer) && node_subdivided(pointer)) { // Record request - atomicAdd(&request_buffer[dfs_stack[current_depth]].requests[child_pos.x + child_pos.y * 4 + child_pos.z * 4 * 4], 1); + atomicAdd(&request_buffer[dfs_stack[current_depth]].requests[child_index], 1); break; } @@ -290,17 +293,19 @@ fn new_traverse(ray_dir: vec3, ray_origin: vec3, root_id: u32, dist_of node_shift = (max_depth - current_depth) * 2; child_size = 1 << u32(node_shift - 2); child_pos = (voxel >> vec3(u32(node_shift - 2))) & vec3(3); - dfs_stack[current_depth] = node_pointer(pointer); + 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[dfs_stack[current_depth]].pointers[child_pos.x + child_pos.y * 4 + child_pos.z * 4 * 4]; + pointer = structure_pool[current_node].pointers[child_index]; // Record usage - usage_buffer[dfs_stack[current_depth]] = constants.frame_timestamp; + usage_buffer[current_node] = constants.frame_timestamp; } // Check color - let color = color_pool[dfs_stack[current_depth]].colors[child_pos.x + child_pos.y * 4 + child_pos.z * 4 * 4]; + let color = color_pool[current_node].colors[child_index]; if(((color >> 24) & 0xFF) != 0) { var result: HitResult; @@ -340,7 +345,7 @@ fn new_traverse(ray_dir: vec3, ray_origin: vec3, root_id: u32, dist_of let common_depth = ((countLeadingZeros(bit_diffs_lowest) - i32(32 - max_depth * 2)) / 2); current_depth = common_depth; - //current_node = dfs_stack[current_depth]; + current_node = dfs_stack[current_depth]; } // Iter max color @@ -350,204 +355,6 @@ fn new_traverse(ray_dir: vec3, ray_origin: vec3, root_id: u32, dist_of return result; } -fn traverse(ray_dir: vec3, ray_origin: vec3, root_id: u32, dist_offset: f32) -> vec4 -{ - let st_pointer = structure_table_pointer[root_id]; - - if (!node_subdivided(st_pointer)) - { - return vec4(0., 1., 0., 1.); - } - if(!node_pointer_valid(st_pointer)) - { - atomicAdd(&structure_table_request_buffer[root_id], 1); - return vec4(0., 1., 0., 1.); - } - - let fovy_deg = 100.; - let fovy = 3.14159 * (fovy_deg / 180.); - let definition = 1920.; - let cone_fovy = fovy / definition; - - let factor = 1.; - let cone_size_factor = 2. * tan(cone_fovy) * factor; - - // Current depth of the node we are exploring - var current_depth = 0; - - // Index of the current node's data - var current_node = u32(st_pointer & 0x3FFFFFFF); - usage_buffer[current_node] = constants.frame_timestamp; - var dfs_stack = array(current_node, 0, 0, 0, 0, 0); - - - - // Lut of the node_size per depth - var node_size_lut = array( - 4 * 4 * 4 * 4 * 4, - 4 * 4 * 4 * 4, - 4 * 4 * 4, - 4 * 4, - 4, - 1, - ); - - let local_dist_offset = dist_offset * f32(node_size_lut[0]); - - - // Current node size - var node_size = node_size_lut[0]; // 128 - - // Size of a child of this node - var child_size = node_size / 4; - - // Simple FVT - let t_off = abs(1. / ray_dir); - - // Start location - let voxel_dir = select(vec3(-1), vec3(1), ray_dir >= vec3(0.)); - var pos_origin = clamp(ray_origin * f32(node_size), vec3(0.), vec3(f32(node_size) - 1.)); - var voxel = vec3(pos_origin); - var last_voxel = voxel; - - let wall_offset = select(vec3(0), vec3(1), ray_dir > vec3(0.)); - - let max_depth = u32(5); - var adaptive_depth = i32(max_depth); - var far_t = 0.; - - let ray_dir_inv = 1. / ray_dir; - let fma_offset = - pos_origin * ray_dir_inv; - - //let depth_limit = 3; - for(var iter = 0; iter < 400; iter ++) - { - - // Our ray is currently touching a voxel. - // Descend to the lowest node that contains this voxel - - // Position of the child we are in - var child_pos = (vec3(voxel) >> vec3((max_depth - u32(current_depth + 1)) * 2)) & vec3(3); // Hardcode for 4-tree - var child_index = child_pos.x + child_pos.y * 4 + child_pos.z * 4 * 4; - - // Current node has been used. report - usage_buffer[current_node] = constants.frame_timestamp; - - while( - node_subdivided(structure_pool[current_node].pointers[child_index]) && - (local_dist_offset + far_t) * cone_size_factor < f32(node_size_lut[current_depth]) - ) - { - if(!node_pointer_valid(structure_pool[current_node].pointers[child_index])) - { - atomicAdd(&request_buffer[current_node].requests[child_index], 1); - break; - } - // Child node is subdivided, we go in, save position in stack - current_node = node_pointer(structure_pool[current_node].pointers[child_index]); - usage_buffer[current_node] = constants.frame_timestamp; - current_depth += 1; - dfs_stack[current_depth] = current_node; - node_size = node_size_lut[current_depth]; - - child_pos = (vec3(voxel) >> vec3((max_depth - u32(current_depth + 1)) * 2)) & vec3(3); // Hardcode for 4-tree - child_index = child_pos.x + child_pos.y * 4 + child_pos.z * 4 * 4; - child_size = node_size / 4; - } - usage_buffer[current_node] = constants.frame_timestamp; - - - - - // At this point current_depth is the depth of the node that contains the voxel - // child_pos and child_index relate to the specific child of the node that contains this voxel - // It is guaranteed that the child is leave - - // Check current leave's color - let color = unpack_color(color_pool[current_node].colors[child_index]); - if(color.w != 0.) // Not transparent - { - /* - let k = child_pos.x + child_pos.y + child_pos.z; - let w = voxel.x + voxel.y + voxel.z; - let x = select(0.5, 1., k % 2 == 0) * select(0.8, 1., w % 2 == 0); - - var div = 1; - var overlay = 1.; - for(var i = 1; i <= 5; i++) - { - let x = (voxel.x / div + voxel.y / div + voxel.z / div) % 2 == 0; - overlay -= select(0., 1. / (f32(i) * 2.5), x); - div *= 4; - } - */ - - return color; - //return overlay * color; - } - - // Voxel and whole child containing it is empty - - // Perform a step through the children of the node - let child_position = (voxel / child_size) * child_size; - let far_corner = child_position + wall_offset * child_size; - //let far_ts = (vec3(far_corner) - pos_origin) / ray_dir; // TODO: Turn into fma - let far_ts = fma(vec3(far_corner), ray_dir_inv, fma_offset); - far_t = min(min(far_ts.x, far_ts.y), far_ts.z); - - let next_child_min = select(child_position, child_position + voxel_dir * child_size, vec3(far_t) == far_ts); - let next_child_max = next_child_min + vec3(child_size) - vec3(1); - - // The ray (far_t) is now touching the new child to explore - // Find out which actual voxel we are touching - let previous_voxel = voxel; - let float_voxel = clamp(vec3(pos_origin + far_t * ray_dir), next_child_min, next_child_max); - /* - voxel = vec3( - floor( - select( - float_voxel - vec3(0.5), - float_voxel + vec3(0.5), - ray_dir > vec3(0.) - )) - ); - */ - //voxel = vec3(round(float_voxel)); - //voxel = voxel_from_wall(float_voxel, ray_dir); - //voxel = voxel_from_wall(float_voxel, ray_dir); - voxel = float_voxel; - if(any(voxel < vec3(0)) || any(voxel >= vec3(node_size_lut[0]))) - { - //return vec4(f32(iter) / 100.); - 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 flb = ((countLeadingZeros(bit_diffs_lowest) - i32(32 - max_depth * 2)) / 2); - let common_depth = flb; - - current_depth = common_depth; - node_size = node_size_lut[current_depth]; - child_size = node_size / 4; - current_node = dfs_stack[current_depth]; - - // Figure out current voxel position - //voxel = vec3(ray_origin + ray_dir * t); - - } - return vec4(1., 0., 1., 1.); - -} - @early_depth_test(less_equal) @fragment fn fragment(in: VertexOutput) -> FragmentOutput diff --git a/src/main.rs b/src/main.rs index 1eb9efe..8479253 100644 --- a/src/main.rs +++ b/src/main.rs @@ -561,21 +561,6 @@ impl State }, ); - // ~~ Get Ray-marching timestamps, report time ~~ - let cloned_rm_time = self.rm_time.clone(); - let cloned_queue = self.queue.clone(); - DownloadBuffer::read_buffer( - &self.device, - &self.queue, - ×tamp_buffer.slice(..), - move |buffer| { - let buffer_slice = buffer.unwrap(); - let slice: &[u64] = cast_slice(&buffer_slice); - let time = (slice[1] - slice[0]) as f32 * cloned_queue.get_timestamp_period(); - *cloned_rm_time.lock() = time; - }, - ); - // ~~ Do cache managment if !self .camera @@ -592,6 +577,21 @@ impl State self.window.pre_present_notify(); self.queue.present(surface_texture); + // ~~ Get Ray-marching timestamps, report time ~~ + let cloned_rm_time = self.rm_time.clone(); + let cloned_queue = self.queue.clone(); + DownloadBuffer::read_buffer( + &self.device, + &self.queue, + ×tamp_buffer.slice(..), + move |buffer| { + let buffer_slice = buffer.unwrap(); + let slice: &[u64] = cast_slice(&buffer_slice); + let time = (slice[1] - slice[0]) as f32 * cloned_queue.get_timestamp_period(); + *cloned_rm_time.lock() = time; + }, + ); + // ~~ Do cache managment if !self .camera