Store brickmap index in HitInfo to avoid recalculating it later

This commit is contained in:
Jarrod Doyle 2023-05-04 10:59:12 +01:00
parent 53ccebf4a6
commit a01848a7f7
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
1 changed files with 8 additions and 11 deletions

View File

@ -31,6 +31,7 @@ struct WorldState {
struct HitInfo { struct HitInfo {
hit: bool, hit: bool,
hit_pos: vec3<i32>, hit_pos: vec3<i32>,
brickmap_idx: u32,
mask: vec3<bool>, mask: vec3<bool>,
}; };
@ -64,13 +65,9 @@ fn to_1d_index(p: vec3<i32>, dims: vec3<i32>) -> u32 {
return u32(p.x + p.y * dims.x + p.z * dims.x * dims.y); return u32(p.x + p.y * dims.x + p.z * dims.x * dims.y);
} }
fn get_shading_offset(p: vec3<i32>) -> u32 { fn get_shading_offset(hit: HitInfo) -> u32 {
// What brickmap are we in? let brickmap = &brickmap_cache[hit.brickmap_idx];
let brickgrid_index = to_1d_index(p / 8, vec3<i32>(world_state.brickmap_cache_dims)); let local_index = to_1d_index(hit.hit_pos % 8, vec3<i32>(8));
let brick_ptr = brickgrid[brickgrid_index];
let brickmap = &brickmap_cache[brick_ptr >> 8u];
let local_index = to_1d_index(p % 8, vec3<i32>(8));
let bitmask_index = local_index / 32u; let bitmask_index = local_index / 32u;
var map_voxel_idx = 0u; var map_voxel_idx = 0u;
for (var i: i32 = 0; i < i32(bitmask_index); i++) { for (var i: i32 = 0; i < i32(bitmask_index); i++) {
@ -114,8 +111,7 @@ fn brick_ray_cast(
orig_ray_pos: vec3<f32>, orig_ray_pos: vec3<f32>,
ray_dir: vec3<f32> ray_dir: vec3<f32>
) -> HitInfo { ) -> HitInfo {
var hit_info = HitInfo(false, vec3<i32>(0), vec3<bool>(false)); var hit_info = HitInfo(false, vec3<i32>(0), 0u, vec3<bool>(false));
var ray_pos = orig_ray_pos * 8.0; var ray_pos = orig_ray_pos * 8.0;
@ -147,6 +143,7 @@ fn brick_ray_cast(
if (voxel_hit(brickmap_idx, map_pos)){ if (voxel_hit(brickmap_idx, map_pos)){
hit_info.hit = true; hit_info.hit = true;
hit_info.hit_pos = map_pos; hit_info.hit_pos = map_pos;
hit_info.brickmap_idx = brickmap_idx;
break; break;
} }
@ -181,7 +178,7 @@ fn brick_ray_cast(
} }
fn grid_cast_ray(orig_ray_pos: vec3<f32>, ray_dir: vec3<f32>) -> HitInfo { fn grid_cast_ray(orig_ray_pos: vec3<f32>, ray_dir: vec3<f32>) -> HitInfo {
var hit_info = HitInfo(false, vec3<i32>(0), vec3<bool>(false)); var hit_info = HitInfo(false, vec3<i32>(0), 0u, vec3<bool>(false));
let min = vec3<f32>(0.0); let min = vec3<f32>(0.0);
let max = min + vec3<f32>(world_state.brickmap_cache_dims); let max = min + vec3<f32>(world_state.brickmap_cache_dims);
@ -315,7 +312,7 @@ fn compute(@builtin(global_invocation_id) global_id: vec3<u32>) {
// else { // else {
// color = vec4<f32>(1.0); // color = vec4<f32>(1.0);
// } // }
let offset = get_shading_offset(hit_info.hit_pos); let offset = get_shading_offset(hit_info);
let raw_color = shading_table[offset].albedo; let raw_color = shading_table[offset].albedo;
color.x = f32((raw_color >> 24u) & 255u) / 255.0; color.x = f32((raw_color >> 24u) & 255u) / 255.0;
color.y = f32((raw_color >> 16u) & 255u) / 255.0; color.y = f32((raw_color >> 16u) & 255u) / 255.0;