10ms raytracing
This commit is contained in:
+25
-16
@@ -217,7 +217,8 @@ fn new_traverse(ray_dir: vec3<f32>, ray_origin: vec3<f32>, root_id: u32, dist_of
|
||||
let max_depth = 5;
|
||||
let dist_offset_voxel = dist_offset * f32(1 << u32(max_depth * 2));
|
||||
let fovy_deg = 100.;
|
||||
let cone_factor = tan((fovy_deg / 180.) * 3.14159) * 2.;
|
||||
let fovy_rad = (fovy_deg / 180.) * 3.14159;
|
||||
let cone_factor = tan(fovy_rad) * 2.;
|
||||
|
||||
let st_pointer = structure_table_pointer[root_id];
|
||||
|
||||
@@ -250,25 +251,32 @@ fn new_traverse(ray_dir: vec3<f32>, ray_origin: vec3<f32>, root_id: u32, dist_of
|
||||
|
||||
// Start location
|
||||
//let voxel_dir = select(vec3(-1), vec3(1), ray_dir >= vec3(0.));
|
||||
var node_size = 1 << u32(((max_depth - current_depth) * 2));
|
||||
var child_size = node_size / 4;
|
||||
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<i32>(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_size = 1 << u32(((max_depth - current_depth) * 2));
|
||||
child_size = node_size / 4;
|
||||
var child_pos = (voxel / child_size) % 4;
|
||||
var pointer = structure_pool[dfs_stack[current_depth]].pointers[child_pos.x + child_pos.y * 4 + child_pos.z * 4 * 4];
|
||||
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 pointer = structure_pool[dfs_stack[current_depth]].pointers[child_pos.x + child_pos.y * 4 + child_pos.z * 4 * 4];
|
||||
|
||||
let min_child_size = (length(vec3<f32>(voxel) - pos_origin) + dist_offset) * cone_factor;
|
||||
while(node_subdivided(pointer) &&
|
||||
!((length(vec3<f32>(voxel) - pos_origin) + dist_offset) * cone_factor >= f32(node_size / 4))
|
||||
f32(child_size / 8) >= min_child_size
|
||||
)
|
||||
{
|
||||
|
||||
if(!node_pointer_valid(pointer) && node_subdivided(pointer))
|
||||
{
|
||||
// Record request
|
||||
@@ -279,9 +287,9 @@ fn new_traverse(ray_dir: vec3<f32>, ray_origin: vec3<f32>, root_id: u32, dist_of
|
||||
// Descend
|
||||
current_depth += 1;
|
||||
|
||||
node_size /= 4;
|
||||
child_size /= 4;
|
||||
child_pos = (voxel / child_size) % 4;
|
||||
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);
|
||||
|
||||
pointer = structure_pool[dfs_stack[current_depth]].pointers[child_pos.x + child_pos.y * 4 + child_pos.z * 4 * 4];
|
||||
@@ -302,13 +310,14 @@ fn new_traverse(ray_dir: vec3<f32>, ray_origin: vec3<f32>, root_id: u32, dist_of
|
||||
}
|
||||
|
||||
// Advance
|
||||
child_pos = (voxel / child_size) * child_size;
|
||||
let far_wall = child_pos + select(vec3(0), vec3(child_size), ray_dir > vec3(0.));
|
||||
let far_wall_inter = (vec3<f32>(far_wall) - pos_origin) / ray_dir;
|
||||
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<f32>(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 + 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<i32>(pos_origin + far_t * ray_dir), next_child, next_child + vec3(child_size) - vec3(1));
|
||||
|
||||
Reference in New Issue
Block a user