Skip to content

Commit

Permalink
Implemented recent optimizations in GPU and moved features to relevan…
Browse files Browse the repository at this point in the history
…t modules
  • Loading branch information
davids91 committed Jun 25, 2024
1 parent c2b64e0 commit b7f132d
Show file tree
Hide file tree
Showing 8 changed files with 7,457 additions and 117 deletions.
93 changes: 57 additions & 36 deletions assets/shaders/viewport_render.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ struct Cube {
}

const FLOAT_ERROR_TOLERANCE = 0.00001;
const OOB_OCTANT = 8u;

//crate::spatial::raytracing::Cube::contains_point
fn cube_contains_point(cube: Cube, p: vec3f) -> bool{
let min_cn = p >= cube.min_position - FLOAT_ERROR_TOLERANCE;
Expand Down Expand Up @@ -269,6 +271,20 @@ fn get_node_occupancy_bitmap(sized_node_meta: u32) -> u32 {
return (0x000000FF & sized_node_meta);
}

//crate::spatial::math::step_octant
fn step_octant(octant: u32, step: vec3f) -> u32 {
let octant_pos_in_32bits = 4 * octant;
return ((OCTANT_STEP_RESULT_LUT[u32(sign(step.x) + 1)][u32(sign(step.y) + 1)][u32(sign(step.z) + 1)]
& (0x0Fu << octant_pos_in_32bits))
>> octant_pos_in_32bits) & 0x0Fu;
}

//crate::spatial::math::hash_direction
fn hash_direction(direction: vec3f) -> u32 {
let offset = vec3f(1.) + normalize(direction);
return hash_region(offset, 2.);
}

// Functionality-wise this function is more generic, than its counterpart
// and is used in voxel brick mapping too
//crate::spatial::math::flat_projection
Expand Down Expand Up @@ -301,12 +317,6 @@ fn get_occupancy_in_bitmap_64bits(
return 0 < (bitmap_msb & pos_mask);
}

//crate::spatial::math::hash_direction
fn hash_direction(direction: vec3f) -> u32 {
let offset = vec3f(1.) + normalize(direction);
return hash_region(offset, 2.);
}

struct BrickHit{
hit: bool,
index: vec3u
Expand Down Expand Up @@ -459,8 +469,7 @@ fn get_by_ray(ray_: Line) -> OctreeRayIntersection{
);
node_stack_i = 1;
}

while(0 < node_stack_i && node_stack_i < max_depth) { // until there are items on the stack
while(0 < node_stack_i && node_stack_i < max_depth) {
var current_bounds = node_stack[node_stack_i - 1].bounds;
var current_node = nodes[node_stack[node_stack_i - 1].node]; //!NOTE: should be const, but then it can not be indexed dynamically
var target_octant = node_stack[node_stack_i - 1].target_octant;
Expand All @@ -471,22 +480,17 @@ fn get_by_ray(ray_: Line) -> OctreeRayIntersection{
ray, &ray_current_distance, current_node.voxels_start_at,
children_buffer[current_node.children_starts_at],
children_buffer[current_node.children_starts_at + 1],
current_bounds, ray_scale_factors, direction_lut_index,
current_bounds, ray_scale_factors, direction_lut_index,
unit_in_bitmap_space, dimension
);
if leaf_brick_hit.hit == true {
let hit_in_voxels = (
current_node.voxels_start_at
+ u32(flat_projection(
leaf_brick_hit.index,
vec2u(dimension, dimension)
))
+ u32(flat_projection( leaf_brick_hit.index, vec2u(dimension, dimension )))
);
current_bounds.size /= f32(dimension);
current_bounds.min_position = (
current_bounds.min_position
+ vec3f(leaf_brick_hit.index) * current_bounds.size
);
current_bounds.min_position = current_bounds.min_position
+ vec3f(leaf_brick_hit.index) * current_bounds.size;
result.hit = true;
result.albedo = voxels[hit_in_voxels].albedo;
result.content = voxels[hit_in_voxels].content;
Expand All @@ -496,15 +500,13 @@ fn get_by_ray(ray_: Line) -> OctreeRayIntersection{
}
leaf_miss = true;
}

//TODO: integrate changes..
if( leaf_miss
||(!cube_contains_point(current_bounds, node_stack[node_stack_i - 1].child_center))
|| target_octant == OOB_OCTANT
|| ( 0 == (
get_node_occupancy_bitmap(current_node.sized_node_meta)
| RAY_TO_NODE_OCCUPANCY_BITMASK_LUT[target_octant][direction_lut_index]
))
|| 0 == get_node_occupancy_bitmap(node_stack[node_stack_i - 1].sized_node_meta)
|| 0 == get_node_occupancy_bitmap(current_node.sized_node_meta)
){
// POP
let popped_target = node_stack[node_stack_i - 1];
Expand All @@ -516,7 +518,10 @@ fn get_by_ray(ray_: Line) -> OctreeRayIntersection{
popped_target.bounds,
ray_scale_factors
);
node_stack[node_stack_i - 1] = add_point_to(node_stack[node_stack_i - 1], step_vec);
node_stack[node_stack_i - 1].target_octant = step_octant(
node_stack[node_stack_i - 1].target_octant,
step_vec
);
}
continue;
}
Expand All @@ -533,41 +538,37 @@ fn get_by_ray(ray_: Line) -> OctreeRayIntersection{
)
);

let target_hit = cube_intersect_ray(target_bounds, ray);
if(!target_is_empty && target_hit.hit) {
if !target_is_empty {
// PUSH
let child_target_octant = hash_region(
(point_in_ray_at_distance(ray, ray_current_distance) - target_bounds.min_position),
target_bounds.size
);
node_stack[node_stack_i] = new_node_stack_item(
target_bounds, target_hit,
target_bounds,
target_child_key,
nodes[target_child_key].sized_node_meta,
child_target_octant
);
node_stack_i += 1;
} else {
// ADVANCE
loop{
loop {
let step_vec = dda_step_to_next_sibling(
ray,
&ray_current_distance,
target_bounds,
ray_scale_factors
);
if target_hit.hit == true {
ray_current_distance = target_hit.exit_distance;
target_octant = step_octant(target_octant, step_vec);
if OOB_OCTANT != target_octant {
target_bounds = child_bounds_for(current_bounds, target_octant);
target_child_key =
children_buffer[current_node.children_starts_at + target_octant];
}
node_stack[node_stack_i - 1] = add_point_to(node_stack[node_stack_i - 1], step_vec);
target_octant = node_stack[node_stack_i - 1].target_octant;
target_bounds = child_bounds_for(
current_bounds,
node_stack[node_stack_i - 1].target_octant
);
target_child_key = children_buffer[current_node.children_starts_at + target_octant];

if (
(!cube_contains_point(current_bounds, node_stack[node_stack_i - 1].child_center))
target_octant == OOB_OCTANT
|| (
target_child_key < node_count //crate::object_pool::key_is_valid
&& 0 != (
Expand All @@ -583,6 +584,7 @@ fn get_by_ray(ray_: Line) -> OctreeRayIntersection{
)
)
){
node_stack[node_stack_i - 1].target_octant = target_octant;
break;
}
}
Expand Down Expand Up @@ -684,6 +686,25 @@ fn update(
textureStore(output_texture, pixel_location, vec4f(rgb_result, 1.));
}

// Note: should be const
var<private> OCTANT_STEP_RESULT_LUT: array<array<array<u32, 3>, 3>, 3> = array<array<array<u32, 3>, 3>, 3>(
array<array<u32, 3>, 3>(
array<u32, 3>(143165576,671647880,2284357768),
array<u32, 3>(1216874632,1749559304,2288551976),
array<u32, 3>(2290632840,2290640968,2290649192)
),
array<array<u32, 3>, 3>(
array<u32, 3>(277383304,839944328,2285013128),
array<u32, 3>(1418203272,1985229328,2289469490),
array<u32, 3>(2290635912,2290644564,2290649206)
),
array<array<u32, 3>, 3>(
array<u32, 3>(2173208712,2206304392,2290321544),
array<u32, 3>(2240315784,2273674113,2290583683),
array<u32, 3>(2290648456,2290648965,2290649223)
)
);

// Note: should be const
var<private> RAY_TO_NODE_OCCUPANCY_BITMASK_LUT: array<array<u32, 8>, 8> = array<array<u32, 8>, 8>(
array<u32, 8>(1, 3, 5, 15, 17, 51, 85, 255),
Expand Down
Loading

0 comments on commit b7f132d

Please sign in to comment.