Skip to content

Commit

Permalink
Fix LOD and mix_emu errors
Browse files Browse the repository at this point in the history
  • Loading branch information
kvark committed Nov 10, 2024
1 parent 552a571 commit f7aef3c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 14 deletions.
2 changes: 1 addition & 1 deletion res/shader/terrain/voxel-bake.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

struct VoxelData {
lod_count: vec4<u32>,
lods: array<VoxelLod, 16>,
lods: array<vec4<i32>, 16>,
occupancy: array<atomic<u32>>,
}
@group(0) @binding(0) var<storage, read_write> b_VoxelGrid: VoxelData;
Expand Down
11 changes: 6 additions & 5 deletions res/shader/terrain/voxel-draw.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ struct VoxelConstants {

struct VoxelData {
lod_count: vec4<u32>,
lods: array<VoxelLod, 16>,
lods: array<vec4<i32>, 16>,
occupancy: array<u32>,
}

Expand All @@ -19,9 +19,9 @@ struct VoxelData {

fn check_occupancy(coordinates: vec3<i32>, lod: u32) -> bool {
let lod_info = b_VoxelGrid.lods[lod];
let ramainder = coordinates % lod_info.dim;
let ramainder = coordinates % lod_info.xyz;
// see https://github.com/gfx-rs/naga/issues/2122
let sanitized = ramainder + select(lod_info.dim, vec3<i32>(0), ramainder >= vec3<i32>(0));
let sanitized = ramainder + select(lod_info.xyz, vec3<i32>(0), ramainder >= vec3<i32>(0));
let addr = linearize(vec3<u32>(sanitized), lod_info);
return (b_VoxelGrid.occupancy[addr.offset] & addr.mask) != 0u;
}
Expand Down Expand Up @@ -171,7 +171,8 @@ fn cast_ray_through_voxels(base: vec3<f32>, dir: vec3<f32>) -> CastPoint {
let a = vec3<f32>((lod_voxel_pos + 0) * lod_voxel_size);
let b = vec3<f32>((lod_voxel_pos + 1) * lod_voxel_size);
// "tc" is the distance to each of the walls of the box
let tc = (select(a, b, dir > vec3<f32>(0.0)) - pos) / dir;
let c = mix(a, b, step(vec3<f32>(0.0), dir));
let tc = (c - pos) / dir;
// "t" is the closest distance to the boundary
let t = min(tc.x, min(tc.y, tc.z));
let new_pos = pos + t * dir;
Expand Down Expand Up @@ -274,7 +275,7 @@ fn vert_bound(
var index = i32(inst_index);
var coord = vec3<i32>(0);
while (lod < b_VoxelGrid.lod_count.x) {
let dim = b_VoxelGrid.lods[lod].dim;
let dim = b_VoxelGrid.lods[lod].xyz;
let total = i32(dim.x * dim.y * dim.z);
if (index < total) {
coord = vec3<i32>(index % dim.x, (index / dim.x) % dim.y, index / (dim.x * dim.y));
Expand Down
11 changes: 3 additions & 8 deletions res/shader/terrain/voxel.inc.wgsl
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
struct VoxelLod {
dim: vec3<i32>,
offset: u32,
}

struct BitAddress {
offset: u32,
mask: u32,
}

fn linearize(coords: vec3<u32>, vlod: VoxelLod) -> BitAddress {
fn linearize(coords: vec3<u32>, vlod: vec4<i32>) -> BitAddress {
let words_per_tile = `morton_tile_size` * `morton_tile_size` * `morton_tile_size` / 32u;
let tile_counts = vec3<u32>(vlod.dim - 1) / vec3<u32>(`morton_tile_size`) + 1u;
let tile_counts = vec3<u32>(vlod.xyz - 1) / vec3<u32>(`morton_tile_size`) + 1u;
let bit_index = encode_morton3(coords % vec3<u32>(`morton_tile_size`));
let tile_coord = coords / vec3<u32>(`morton_tile_size`);
let tile_index = (tile_coord.z * tile_counts.y + tile_coord.y) * tile_counts.x + tile_coord.x;
var addr: BitAddress;
addr.offset = vlod.offset + tile_index * words_per_tile + bit_index / 32u;
addr.offset = u32(vlod.w) + tile_index * words_per_tile + bit_index / 32u;
addr.mask = 1u << (bit_index & 31u);
return addr;
}

0 comments on commit f7aef3c

Please sign in to comment.