chunk gen now tries nodes first, also messed around a lot w rendering

This commit is contained in:
2024-09-17 20:29:10 -04:00
parent 1fc1cd23fd
commit b68707b92c
14 changed files with 709 additions and 515 deletions

16
Cargo.lock generated
View File

@@ -84,9 +84,9 @@ dependencies = [
[[package]] [[package]]
name = "arrayref" name = "arrayref"
version = "0.3.8" version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9d151e35f61089500b617991b791fc8bfd237ae50cd5950803758a179b41e67a" checksum = "76a2e8124351fda1ef8aaaa3bbd7ebbcb486bbcd4225aca0aa0d84bb2db8fecb"
[[package]] [[package]]
name = "arrayvec" name = "arrayvec"
@@ -405,9 +405,9 @@ dependencies = [
[[package]] [[package]]
name = "cc" name = "cc"
version = "1.1.18" version = "1.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b62ac837cdb5cb22e10a256099b4fc502b1dfe560cb282963a974d7abd80e476" checksum = "2d74707dde2ba56f86ae90effb3b43ddd369504387e718014de010cec7959800"
dependencies = [ dependencies = [
"jobserver", "jobserver",
"libc", "libc",
@@ -1029,9 +1029,9 @@ checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
[[package]] [[package]]
name = "memmap2" name = "memmap2"
version = "0.9.4" version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fe751422e4a8caa417e13c3ea66452215d7d63e19e604f4980461212f3ae1322" checksum = "fd3f7eed9d3848f8b98834af67102b720745c4ec028fcd0aa0239277e7de374f"
dependencies = [ dependencies = [
"libc", "libc",
] ]
@@ -2113,9 +2113,9 @@ checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe"
[[package]] [[package]]
name = "unicode-segmentation" name = "unicode-segmentation"
version = "1.11.0" version = "1.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d4c87d22b6e3f4a18d4d40ef354e97c90fcb14dd91d7dc0aa9d8a1172ebf7202" checksum = "f6ccf251212114b54433ec949fd6a7841275f9ada20dddd2f29e9ceea4501493"
[[package]] [[package]]
name = "unicode-width" name = "unicode-width"

View File

@@ -17,8 +17,8 @@ pub use state::*;
use system::render::add_grid; use system::render::add_grid;
use crate::{ use crate::{
server::Server,
common::{ClientMessage, ServerHandle, ServerMessage}, common::{ClientMessage, ServerHandle, ServerMessage},
server::Server,
}; };
use self::{input::Input, render::Renderer, ClientState}; use self::{input::Input, render::Renderer, ClientState};
@@ -46,8 +46,9 @@ pub struct Client<'a> {
server: ServerHandle, server: ServerHandle,
server_id_map: HashMap<Entity, Entity>, server_id_map: HashMap<Entity, Entity>,
systems: ClientSystems, systems: ClientSystems,
target: Instant, frame_target: Instant,
frame_time: Duration, frame_time: Duration,
second_target: Instant,
the_thing: bool, the_thing: bool,
} }
@@ -91,8 +92,9 @@ impl Client<'_> {
world, world,
server, server,
server_id_map: HashMap::new(), server_id_map: HashMap::new(),
target: Instant::now(), frame_target: Instant::now(),
frame_time: FRAME_TIME, frame_time: FRAME_TIME,
second_target: Instant::now(),
the_thing: false, the_thing: false,
} }
} }
@@ -124,8 +126,8 @@ impl Client<'_> {
} }
} }
if now >= self.target { if now >= self.frame_target {
self.target += self.frame_time; self.frame_target += self.frame_time;
let mut commands = std::mem::take(&mut self.render_commands); let mut commands = std::mem::take(&mut self.render_commands);
let world_cmds = std::mem::take(&mut self.world.resource_mut::<RenderCommands>().0); let world_cmds = std::mem::take(&mut self.world.resource_mut::<RenderCommands>().0);
commands.extend(world_cmds); commands.extend(world_cmds);
@@ -133,6 +135,17 @@ impl Client<'_> {
self.renderer.draw(); self.renderer.draw();
} }
if now >= self.second_target {
self.second_target += Duration::from_secs(1);
// let timer = self.renderer.timer();
// println!(
// "avg: {:4?}; max: {:4?}; fps: {:4?}",
// timer.avg(),
// timer.max(),
// timer.per_sec(),
// );
}
if self.exit { if self.exit {
event_loop.exit(); event_loop.exit();
} }

View File

@@ -4,7 +4,7 @@ pub mod voxel;
pub use command::*; pub use command::*;
use super::camera::Camera; use super::camera::Camera;
use crate::client::rsc::CLEAR_COLOR; use crate::{client::rsc::CLEAR_COLOR, util::timer::Timer};
use nalgebra::Vector2; use nalgebra::Vector2;
use util::DepthTexture; use util::DepthTexture;
use voxel::VoxelPipeline; use voxel::VoxelPipeline;
@@ -114,6 +114,7 @@ impl<'a> Renderer<'a> {
let view = output let view = output
.texture .texture
.create_view(&wgpu::TextureViewDescriptor::default()); .create_view(&wgpu::TextureViewDescriptor::default());
let mut compute_pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor { let mut compute_pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
label: None, label: None,
timestamp_writes: None, timestamp_writes: None,

View File

@@ -34,7 +34,7 @@ pub struct VoxelPipeline {
} }
const RENDER_SHADER: wgpu::ShaderModuleDescriptor<'_> = include_wgsl!("shader/render.wgsl"); const RENDER_SHADER: wgpu::ShaderModuleDescriptor<'_> = include_wgsl!("shader/render.wgsl");
const COMPUTE_SHADER: wgpu::ShaderModuleDescriptor<'_> = include_wgsl!("shader/compute.wgsl"); const COMPUTE_SHADER: wgpu::ShaderModuleDescriptor<'_> = include_wgsl!("shader/compute_working.wgsl");
impl VoxelPipeline { impl VoxelPipeline {
pub fn new(device: &wgpu::Device, config: &wgpu::SurfaceConfiguration) -> Self { pub fn new(device: &wgpu::Device, config: &wgpu::SurfaceConfiguration) -> Self {
@@ -82,7 +82,7 @@ impl VoxelPipeline {
pub fn update_shader(&mut self, device: &wgpu::Device) { pub fn update_shader(&mut self, device: &wgpu::Device) {
let Ok(shader) = std::fs::read_to_string( let Ok(shader) = std::fs::read_to_string(
env!("CARGO_MANIFEST_DIR").to_owned() + "/src/client/render/voxel/ray_oct/shader/compute.wgsl", env!("CARGO_MANIFEST_DIR").to_owned() + "/src/client/render/voxel/ray_oct/shader/compute_working.wgsl",
) else { ) else {
println!("Failed to reload shader!"); println!("Failed to reload shader!");
return; return;
@@ -198,7 +198,7 @@ impl VoxelPipeline {
zoom: camera.scale, zoom: camera.scale,
transform, transform,
}; };
self.layout.view.update(device, encoder, belt, data) self.layout.view.update(device, encoder, belt, data);
} }
pub fn draw<'a>(&'a self, render_pass: &mut wgpu::RenderPass<'a>) { pub fn draw<'a>(&'a self, render_pass: &mut wgpu::RenderPass<'a>) {

View File

@@ -42,34 +42,7 @@ fn main(@builtin(global_invocation_id) cell: vec3<u32>) {
let pos = view.transform * vec4<f32>(pixel_pos, 1.0); let pos = view.transform * vec4<f32>(pixel_pos, 1.0);
let dir = view.transform * vec4<f32>(normalize(pixel_pos), 0.0); let dir = view.transform * vec4<f32>(normalize(pixel_pos), 0.0);
let start = start_ray(pos, dir); var color = trace_full(pos, dir);
var color = vec4<f32>(0.0);
if start.hit {
var res = ray_next(start.ray, LEAF_BIT);
var safe = 0;
var normals = start.normals;
while res.data != 0 {
safe += 1;
if safe > 100 {break;}
let data = res.data & LEAF_MASK;
let vcolor = get_color(data);
let diffuse = max(dot(global_lights[0].dir, normals[res.ray.axis]) + 0.1, 0.0);
let ambient = 0.2;
let lighting = max(diffuse, ambient);
let new_color = min(vcolor.xyz * lighting, vec3<f32>(1.0));
color += vec4<f32>(new_color.xyz * vcolor.a, vcolor.a) * (1.0 - color.a);
if color.a > FULL_ALPHA { break; }
let old_t = res.ray.t;
res = ray_next(res.ray, res.data);
let dist = res.ray.t - old_t;
if data == 3 {
let a = min(dist / 128.0, 1.0);
color += vec4<f32>(vec3<f32>(0.0) * a, a) * (1.0 - color.a);
}
}
// color = vec4<f32>(dir.xyz * res.ray.t / 2048.0, 1.0);
}
// var color = trace_full(pos, dir);
let light_mult = clamp((-dot(dir.xyz, global_lights[0].dir) - 0.99) * 200.0, 0.0, 1.0); let light_mult = clamp((-dot(dir.xyz, global_lights[0].dir) - 0.99) * 200.0, 0.0, 1.0);
let sun_color = light_mult * vec3<f32>(1.0, 1.0, 1.0); let sun_color = light_mult * vec3<f32>(1.0, 1.0, 1.0);
let sky_bg = vec3<f32>(0.3, 0.6, 1.0); let sky_bg = vec3<f32>(0.3, 0.6, 1.0);
@@ -89,210 +62,7 @@ const EPSILON = 0.00000000001;
const MAX_ITERS = 2000; const MAX_ITERS = 2000;
// NOTE: CANNOT GO HIGHER THAN 23 due to how floating point // NOTE: CANNOT GO HIGHER THAN 23 due to how floating point
// numbers are stored and the bit manipulation used // numbers are stored and the bit manipulation used
const MAX_SCALE: u32 = 12; const MAX_SCALE: u32 = 10;
struct Ray {
t: f32,
vox_pos: vec3<f32>,
t_inc: vec3<f32>,
scale: u32,
min_adj: vec3<f32>,
child: u32,
axis: u32,
node_start: u32,
group_offset: u32,
inv_dir_bits: u32,
parents: array<u32, MAX_SCALE>,
};
struct RayResult {
ray: Ray,
data: u32,
}
struct RayStart {
hit: bool,
ray: Ray,
normals: mat3x3<f32>,
}
fn start_ray(pos_view: vec4<f32>, dir_view: vec4<f32>) -> RayStart {
let gi = 0;
let group = voxel_groups[gi];
if group.scale == 0 {
return RayStart(false, Ray(), mat3x3<f32>());
}
let dimensions = vec3<u32>(1u << group.scale);
let dim_f = vec3<f32>(dimensions);
let dim_i = vec3<i32>(dimensions);
// transform so that group is at 0,0
let pos = (group.transform_inv * pos_view).xyz;
var dir = (group.transform_inv * dir_view).xyz;
if dir.x == 0 {dir.x = EPSILON;}
if dir.y == 0 {dir.y = EPSILON;}
if dir.z == 0 {dir.z = EPSILON;}
let dir_if = sign(dir);
let dir_uf = max(dir_if, vec3<f32>(0.0));
// calculate normals
var normals = mat3x3<f32>(
(group.transform * vec4<f32>(dir_if.x, 0.0, 0.0, 0.0)).xyz,
(group.transform * vec4<f32>(0.0, dir_if.y, 0.0, 0.0)).xyz,
(group.transform * vec4<f32>(0.0, 0.0, dir_if.z, 0.0)).xyz,
);
var axis = 0u;
// find where ray intersects with group
let pos_min = (vec3<f32>(1.0) - dir_uf) * dim_f;
// time of intersection; x = td + p, solve for t
var t_min = (pos_min - pos) / dir;
if outside3f(pos, ZERO3F, dim_f) {
// points of intersection
let px = pos + t_min.x * dir;
let py = pos + t_min.y * dir;
let pz = pos + t_min.z * dir;
// check if point is in bounds
let hit = vec3<bool>(
inside2f(px.yz, ZERO2F, dim_f.yz),
inside2f(py.xz, ZERO2F, dim_f.xz),
inside2f(pz.xy, ZERO2F, dim_f.xy),
) && (t_min > ZERO3F);
if !any(hit) {
return RayStart(false, Ray(), mat3x3<f32>());
}
axis = select(select(2u, 1u, hit.y), 0u, hit.x);
}
t_min *= f32(1u << (MAX_SCALE - group.scale));
// time to move 1 unit in each direction
let full = f32(1u << MAX_SCALE);
let t_inc = abs(1.0 / dir) * full;
let t_offset = max(max(t_min.x, t_min.y), t_min.z);
let t = max(0.0, t_offset);
let dir_i = vec3<i32>(dir_if);
let dir_u = vec3<u32>((dir_i + vec3<i32>(1)) / 2);
let dir_bits = vec_to_dir(dir_u);
let inv_dir_bits = 7 - dir_bits;
let node_start = 1u;
let scale = MAX_SCALE - 1;
let scale_exp2 = 0.5;
let parents = array<u32, MAX_SCALE>();
var child = 0u;
var vox_pos = vec3<f32>(1.0);
let t_center = t_min + scale_exp2 * t_inc;
if t > t_center.x { vox_pos.x = 1.5; child |= 4u; }
if t > t_center.y { vox_pos.y = 1.5; child |= 2u; }
if t > t_center.z { vox_pos.z = 1.5; child |= 1u; }
let min_adj = t_min - t_inc;
return RayStart(
true,
Ray(
t,
vox_pos,
t_inc,
scale,
min_adj,
child,
axis,
node_start,
group.offset,
inv_dir_bits,
parents,
),
normals
);
}
fn ray_next(ray: Ray, skip: u32) -> RayResult {
let group_offset = ray.group_offset;
let t_inc = ray.t_inc;
let min_adj = ray.min_adj;
let inv_dir_bits = ray.inv_dir_bits;
var scale = ray.scale;
var scale_exp2 = bitcast<f32>((scale + 127 - MAX_SCALE) << 23);
var vox_pos = ray.vox_pos;
var t = ray.t;
var node_start = ray.node_start;
var child = ray.child;
var parents = ray.parents;
var axis: u32;
var data = 0u;
loop {
let t_corner = vox_pos * t_inc + min_adj;
let node = voxels[group_offset + node_start + (child ^ inv_dir_bits)];
if node >= LEAF_BIT {
if node != skip {
data = node;
break;
}
// move to next time point and determine which axis to move along
let t_next = t_corner + scale_exp2 * t_inc;
t = min(min(t_next.x, t_next.y), t_next.z);
axis = select(select(0u, 1u, t == t_next.y), 2u, t == t_next.z);
let move_dir = 4u >> axis;
// check if need to pop stack
if (child & move_dir) > 0 {
// calculate new scale; first differing bit after adding
let axis_pos = vox_pos[axis];
// AWARE
let differing = bitcast<u32>(axis_pos) ^ bitcast<u32>(axis_pos + scale_exp2);
scale = (bitcast<u32>(f32(differing)) >> 23) - 127 - (23 - MAX_SCALE);
scale_exp2 = bitcast<f32>((scale + 127 - MAX_SCALE) << 23);
if scale >= MAX_SCALE { break; }
// restore & recalculate parent
let parent_info = parents[scale];
node_start = parent_info >> 3;
child = parent_info & 7;
let scale_vec = vec3<u32>(scale + 23 - MAX_SCALE);
// remove bits lower than current scale
vox_pos = bitcast<vec3<f32>>((bitcast<vec3<u32>>(vox_pos) >> scale_vec) << scale_vec);
}
// move to next child and voxel position
child += move_dir;
vox_pos[axis] += scale_exp2;
} else {
// push current node to stack
parents[scale] = (node_start << 3) + child;
scale -= 1u;
// calculate child node vars
scale_exp2 *= 0.5;
child = 0u;
let t_center = t_corner + scale_exp2 * t_inc;
if t > t_center.x { vox_pos.x += scale_exp2; child |= 4u; }
if t > t_center.y { vox_pos.y += scale_exp2; child |= 2u; }
if t > t_center.z { vox_pos.z += scale_exp2; child |= 1u; }
node_start += 8 + node;
}
}
return RayResult(
Ray(
t,
vox_pos,
t_inc,
scale,
min_adj,
child,
axis,
node_start,
group_offset,
inv_dir_bits,
parents,
),
data
);
}
fn trace_full(pos_view: vec4<f32>, dir_view: vec4<f32>) -> vec4<f32> { fn trace_full(pos_view: vec4<f32>, dir_view: vec4<f32>) -> vec4<f32> {
let gi = 0; let gi = 0;
@@ -326,14 +96,13 @@ fn trace_full(pos_view: vec4<f32>, dir_view: vec4<f32>) -> vec4<f32> {
// find where ray intersects with group // find where ray intersects with group
let pos_min = (vec3<f32>(1.0) - dir_uf) * dim_f; let pos_min = (vec3<f32>(1.0) - dir_uf) * dim_f;
var pos = pos_start;
// time of intersection; x = td + p, solve for t // time of intersection; x = td + p, solve for t
var t_min = (pos_min - pos) / dir; var t_min = (pos_min - pos_start) / dir;
if outside3f(pos, ZERO3F, dim_f) { if outside3f(pos_start, ZERO3F, dim_f) {
// points of intersection // points of intersection
let px = pos + t_min.x * dir; let px = pos_start + t_min.x * dir;
let py = pos + t_min.y * dir; let py = pos_start + t_min.y * dir;
let pz = pos + t_min.z * dir; let pz = pos_start + t_min.z * dir;
// check if point is in bounds // check if point is in bounds
let hit = vec3<bool>( let hit = vec3<bool>(
@@ -344,10 +113,10 @@ fn trace_full(pos_view: vec4<f32>, dir_view: vec4<f32>) -> vec4<f32> {
if !any(hit) { if !any(hit) {
return vec4<f32>(0.0); return vec4<f32>(0.0);
} }
pos = select(select(pz, py, hit.y), px, hit.x);
axis = select(select(2u, 1u, hit.y), 0u, hit.x); axis = select(select(2u, 1u, hit.y), 0u, hit.x);
} }
t_min *= f32(1u << (MAX_SCALE - group.scale)); let t_mult = f32(1u << (MAX_SCALE - group.scale));
t_min *= t_mult;
// time to move 1 unit in each direction // time to move 1 unit in each direction
let full = f32(1u << MAX_SCALE); let full = f32(1u << MAX_SCALE);
let inc_t = abs(1.0 / dir) * full; let inc_t = abs(1.0 / dir) * full;
@@ -362,6 +131,7 @@ fn trace_full(pos_view: vec4<f32>, dir_view: vec4<f32>) -> vec4<f32> {
var node_start = 1u; var node_start = 1u;
var scale = MAX_SCALE - 1; var scale = MAX_SCALE - 1;
var scale_exp2 = 0.5; var scale_exp2 = 0.5;
var skip = LEAF_BIT;
var color = vec4<f32>(0.0); var color = vec4<f32>(0.0);
var parents = array<u32, MAX_SCALE>(); var parents = array<u32, MAX_SCALE>();
@@ -382,13 +152,25 @@ fn trace_full(pos_view: vec4<f32>, dir_view: vec4<f32>) -> vec4<f32> {
let t_corner = vox_pos * inc_t + min_adj; let t_corner = vox_pos * inc_t + min_adj;
let node = voxels[group.offset + node_start + (child ^ inv_dir_bits)]; let node = voxels[group.offset + node_start + (child ^ inv_dir_bits)];
if node >= LEAF_BIT { if node >= LEAF_BIT {
if node != LEAF_BIT { if node != skip {
let vcolor = get_color(node & LEAF_MASK); skip = node;
let diffuse = max(dot(global_lights[0].dir, normals[axis]) + 0.1, 0.0); let normal = normals[axis];
let sun_dir = global_lights[0].dir;
let new_pos = pos_view + dir_view * t / t_mult - vec4<f32>(normals[axis] * 0.001, 0.0);
let light = trace_light(new_pos, vec4<f32>(-sun_dir, 0.0));
let diffuse = max(dot(sun_dir, normal) + 0.1, 0.0);
let ambient = 0.2; let ambient = 0.2;
let lighting = max(diffuse, ambient); let specular = (exp(max(
let new_color = min(vcolor.xyz * lighting, vec3<f32>(1.0)); -(dot(reflect(dir_view.xyz, normal), sun_dir) + 0.90) * 4.0, 0.0
color += vec4<f32>(new_color.xyz * vcolor.a, vcolor.a) * (1.0 - color.a); )) - 1.0) * light;
let lighting = max(diffuse * light.a, ambient);
let vcolor = get_color(node & LEAF_MASK);
let new_rgb = min(vcolor.xyz * lighting + specular.xyz + light.xyz * vcolor.xyz, vec3<f32>(1.0));
let new_a = min(vcolor.a + specular.a, 1.0);
let new_color = vec4<f32>(new_rgb, new_a);
color += vec4<f32>(new_color.xyz * new_color.a, new_color.a) * (1.0 - color.a);
if color.a > FULL_ALPHA { break; } if color.a > FULL_ALPHA { break; }
} }
@@ -438,6 +220,164 @@ fn trace_full(pos_view: vec4<f32>, dir_view: vec4<f32>) -> vec4<f32> {
return color; return color;
} }
fn trace_light(pos_view: vec4<f32>, dir_view: vec4<f32>) -> vec4<f32> {
let gi = 0;
let group = voxel_groups[gi];
if group.scale == 0 {
return vec4<f32>(0.0);
}
let dimensions = vec3<u32>(1u << group.scale);
let dim_f = vec3<f32>(dimensions);
let dim_i = vec3<i32>(dimensions);
// transform so that group is at 0,0
let pos_start = (group.transform_inv * pos_view).xyz;
var dir = (group.transform_inv * dir_view).xyz;
if dir.x == 0 {dir.x = EPSILON;}
if dir.y == 0 {dir.y = EPSILON;}
if dir.z == 0 {dir.z = EPSILON;}
let dir_if = sign(dir);
let dir_uf = max(dir_if, vec3<f32>(0.0));
// calculate normals
var normals = mat3x3<f32>(
(group.transform * vec4<f32>(dir_if.x, 0.0, 0.0, 0.0)).xyz,
(group.transform * vec4<f32>(0.0, dir_if.y, 0.0, 0.0)).xyz,
(group.transform * vec4<f32>(0.0, 0.0, dir_if.z, 0.0)).xyz,
);
var axis = 0u;
// find where ray intersects with group
let pos_min = (vec3<f32>(1.0) - dir_uf) * dim_f;
// time of intersection; x = td + p, solve for t
var t_min = (pos_min - pos_start) / dir;
if outside3f(pos_start, ZERO3F, dim_f) {
// points of intersection
let px = pos_start + t_min.x * dir;
let py = pos_start + t_min.y * dir;
let pz = pos_start + t_min.z * dir;
// check if point is in bounds
let hit = vec3<bool>(
inside2f(px.yz, ZERO2F, dim_f.yz),
inside2f(py.xz, ZERO2F, dim_f.xz),
inside2f(pz.xy, ZERO2F, dim_f.xy),
) && (t_min > ZERO3F);
if !any(hit) {
return vec4<f32>(0.0);
}
axis = select(select(2u, 1u, hit.y), 0u, hit.x);
}
let t_mult = f32(1u << (MAX_SCALE - group.scale));
t_min *= t_mult;
// time to move 1 unit in each direction
let full = f32(1u << MAX_SCALE);
let inc_t = abs(1.0 / dir) * full;
let t_offset = max(max(t_min.x, t_min.y), t_min.z);
var t = max(0.0, t_offset);
var old_t = t;
let dir_i = vec3<i32>(dir_if);
let dir_u = vec3<u32>((dir_i + vec3<i32>(1)) / 2);
let dir_bits = vec_to_dir(dir_u);
let inv_dir_bits = 7 - dir_bits;
var node_start = 1u;
var scale = MAX_SCALE - 1;
var scale_exp2 = 0.5;
var mask = vec4<f32>(0.0);
var skip = LEAF_BIT;
var parents = array<u32, MAX_SCALE>();
var child = 0u;
var vox_pos = vec3<f32>(1.0);
let t_center = t_min + scale_exp2 * inc_t;
if t > t_center.x { vox_pos.x = 1.5; child |= 4u; }
if t > t_center.y { vox_pos.y = 1.5; child |= 2u; }
if t > t_center.z { vox_pos.z = 1.5; child |= 1u; }
let min_adj = t_min - inc_t;
var data = 0u;
var iters = 0;
loop {
if iters == MAX_ITERS {
return vec4<f32>(1.0, 0.0, 1.0, 1.0);
}
iters += 1;
let t_corner = vox_pos * inc_t + min_adj;
let node = voxels[group.offset + node_start + (child ^ inv_dir_bits)];
if node >= LEAF_BIT {
if node != skip {
skip = node;
if data == 3 {
let dist = (t - old_t) / t_mult;
let vcolor = vec4<f32>(vec3<f32>(0.0), min(dist / 12.0, 1.0));
mask += vec4<f32>(vcolor.xyz * vcolor.a, vcolor.a) * (1.0 - mask.a);
}
data = node & LEAF_MASK;
if data != 3 && data != 0 {
let vcolor = get_color(data);
mask += vec4<f32>(vcolor.xyz * vcolor.a, vcolor.a) * (1.0 - mask.a);
}
old_t = t;
if mask.a > FULL_ALPHA { break; }
}
// move to next time point and determine which axis to move along
let t_next = t_corner + scale_exp2 * inc_t;
t = min(min(t_next.x, t_next.y), t_next.z);
axis = select(select(0u, 1u, t == t_next.y), 2u, t == t_next.z);
let move_dir = 4u >> axis;
// check if need to pop stack
if (child & move_dir) > 0 {
// calculate new scale; first differing bit after adding
let axis_pos = vox_pos[axis];
// AWARE
let differing = bitcast<u32>(axis_pos) ^ bitcast<u32>(axis_pos + scale_exp2);
scale = (bitcast<u32>(f32(differing)) >> 23) - 127 - (23 - MAX_SCALE);
scale_exp2 = bitcast<f32>((scale + 127 - MAX_SCALE) << 23);
if scale >= MAX_SCALE { break; }
// restore & recalculate parent
let parent_info = parents[scale];
node_start = parent_info >> 3;
child = parent_info & 7;
let scale_vec = vec3<u32>(scale + 23 - MAX_SCALE);
// remove bits lower than current scale
vox_pos = bitcast<vec3<f32>>((bitcast<vec3<u32>>(vox_pos) >> scale_vec) << scale_vec);
}
// move to next child and voxel position
child += move_dir;
vox_pos[axis] += scale_exp2;
} else {
// push current node to stack
parents[scale] = (node_start << 3) + child;
scale -= 1u;
// calculate child node vars
scale_exp2 *= 0.5;
child = 0u;
let t_center = t_corner + scale_exp2 * inc_t;
if t > t_center.x { vox_pos.x += scale_exp2; child |= 4u; }
if t > t_center.y { vox_pos.y += scale_exp2; child |= 2u; }
if t > t_center.z { vox_pos.z += scale_exp2; child |= 1u; }
node_start += 8 + node;
}
}
if data == 3 {
let dist = (t - old_t) / t_mult;
let vcolor = vec4<f32>(vec3<f32>(0.0), min(dist / 12.0, 1.0));
mask += vec4<f32>(vcolor.xyz * vcolor.a, vcolor.a) * (1.0 - mask.a);
}
mask.a = 1.0 - mask.a;
mask = vec4<f32>(mask.a * mask.xyz, mask.a);
return mask;
}
fn dir_to_vec(bits: u32) -> vec3<u32> { fn dir_to_vec(bits: u32) -> vec3<u32> {
return vec3<u32>(bits >> 2, (bits & 2) >> 1, bits & 1); return vec3<u32>(bits >> 2, (bits & 2) >> 1, bits & 1);
} }

View File

@@ -42,7 +42,56 @@ fn main(@builtin(global_invocation_id) cell: vec3<u32>) {
let pos = view.transform * vec4<f32>(pixel_pos, 1.0); let pos = view.transform * vec4<f32>(pixel_pos, 1.0);
let dir = view.transform * vec4<f32>(normalize(pixel_pos), 0.0); let dir = view.transform * vec4<f32>(normalize(pixel_pos), 0.0);
var color = trace_full(pos, dir); let start = start_ray(pos, dir);
var color = vec4<f32>(0.0);
let ambient = 0.2;
if start.hit {
var res = ray_next(start.ray, LEAF_BIT);
var normals = start.normals;
let specular = (exp(max(
-(dot(reflect(dir.xyz, normals[res.ray.axis]), global_lights[0].dir) + 0.90) * 4.0, 0.0
)) - 1.0);
while res.data != 0 {
let data = res.data & LEAF_MASK;
let vcolor = get_color(data);
let diffuse = max(dot(global_lights[0].dir, normals[res.ray.axis]) + 0.1, 0.0);
let light = max(diffuse, ambient);
let new_color = min(vcolor.xyz * light, vec3<f32>(1.0));
color += vec4<f32>(new_color.xyz * vcolor.a, vcolor.a) * (1.0 - color.a);
if color.a > FULL_ALPHA { break; }
let old_t = res.ray.t;
res = ray_next(res.ray, res.data);
if data == 3 {
let dist = (res.ray.t - old_t) / start.t_mult;
let a = min(dist / 12.0, 1.0);
color += vec4<f32>(vec3<f32>(0.0) * a, a) * (1.0 - color.a);
}
}
if color.a != 0 {
let pos = pos + dir * res.ray.t / start.t_mult - vec4<f32>(normals[res.ray.axis] * 0.001, 0.0);
let dir = vec4<f32>(-global_lights[0].dir, 0.0);
let start = start_ray(pos, dir);
res = ray_next(start.ray, LEAF_BIT);
var light = 1.0;
while res.data != 0 {
let data = res.data & LEAF_MASK;
let vcolor = get_color(data);
if data != 3 { light -= vcolor.a * light; }
if light <= 0 { break; }
let old_t = res.ray.t;
res = ray_next(res.ray, res.data);
if data == 3 {
let dist = (res.ray.t - old_t) / start.t_mult;
let a = min(dist / 12.0, 1.0);
light -= a;
}
}
color = vec4<f32>(color.xyz * max(light, ambient), color.a) + vec4<f32>(vec3<f32>(specular * light), 0.0);
}
// color = vec4<f32>(pos.xyz / 128.0, 1.0);
}
// var color = trace_full(pos, dir);
let light_mult = clamp((-dot(dir.xyz, global_lights[0].dir) - 0.99) * 200.0, 0.0, 1.0); let light_mult = clamp((-dot(dir.xyz, global_lights[0].dir) - 0.99) * 200.0, 0.0, 1.0);
let sun_color = light_mult * vec3<f32>(1.0, 1.0, 1.0); let sun_color = light_mult * vec3<f32>(1.0, 1.0, 1.0);
let sky_bg = vec3<f32>(0.3, 0.6, 1.0); let sky_bg = vec3<f32>(0.3, 0.6, 1.0);
@@ -52,25 +101,56 @@ fn main(@builtin(global_invocation_id) cell: vec3<u32>) {
textureStore(output, cell.xy, color); textureStore(output, cell.xy, color);
} }
const LEAF_BIT = 1u << 31u;
const LEAF_MASK = ~LEAF_BIT;
const ZERO3F = vec3<f32>(0.0); const ZERO3F = vec3<f32>(0.0);
const ZERO2F = vec2<f32>(0.0); const ZERO2F = vec2<f32>(0.0);
const FULL_ALPHA = 0.999; const FULL_ALPHA = 0.999;
const EPSILON = 0.00000000001; const EPSILON = 0.00000000001;
const MAX_ITERS = 1000; const MAX_ITERS = 2000;
// NOTE: CANNOT GO HIGHER THAN 23 due to how floating point
// numbers are stored and the bit manipulation used
const MAX_SCALE: u32 = 10; const MAX_SCALE: u32 = 10;
fn trace_full(pos_view: vec4<f32>, dir_view: vec4<f32>) -> vec4<f32> { struct Ray {
t: f32,
vox_pos: vec3<f32>,
t_inc: vec3<f32>,
scale: u32,
min_adj: vec3<f32>,
child: u32,
axis: u32,
node_start: u32,
group_offset: u32,
inv_dir_bits: u32,
parents: array<u32, MAX_SCALE>,
};
struct RayResult {
ray: Ray,
data: u32,
}
struct RayStart {
hit: bool,
ray: Ray,
normals: mat3x3<f32>,
t_mult: f32,
}
fn start_ray(pos_view: vec4<f32>, dir_view: vec4<f32>) -> RayStart {
let gi = 0; let gi = 0;
let group = voxel_groups[gi]; let group = voxel_groups[gi];
if group.scale == 0 { if group.scale == 0 {
return vec4<f32>(0.0); return RayStart();
} }
let dimensions = vec3<u32>(1u << group.scale); let dimensions = vec3<u32>(1u << group.scale);
let dim_f = vec3<f32>(dimensions); let dim_f = vec3<f32>(dimensions);
let dim_i = vec3<i32>(dimensions); let dim_i = vec3<i32>(dimensions);
// transform so that group is at 0,0 // transform so that group is at 0,0
let pos_start = (group.transform_inv * pos_view).xyz; let pos = (group.transform_inv * pos_view).xyz;
var dir = (group.transform_inv * dir_view).xyz; var dir = (group.transform_inv * dir_view).xyz;
if dir.x == 0 {dir.x = EPSILON;} if dir.x == 0 {dir.x = EPSILON;}
if dir.y == 0 {dir.y = EPSILON;} if dir.y == 0 {dir.y = EPSILON;}
@@ -91,7 +171,6 @@ fn trace_full(pos_view: vec4<f32>, dir_view: vec4<f32>) -> vec4<f32> {
// find where ray intersects with group // find where ray intersects with group
let pos_min = (vec3<f32>(1.0) - dir_uf) * dim_f; let pos_min = (vec3<f32>(1.0) - dir_uf) * dim_f;
var pos = pos_start;
// time of intersection; x = td + p, solve for t // time of intersection; x = td + p, solve for t
var t_min = (pos_min - pos) / dir; var t_min = (pos_min - pos) / dir;
if outside3f(pos, ZERO3F, dim_f) { if outside3f(pos, ZERO3F, dim_f) {
@@ -107,57 +186,81 @@ fn trace_full(pos_view: vec4<f32>, dir_view: vec4<f32>) -> vec4<f32> {
inside2f(pz.xy, ZERO2F, dim_f.xy), inside2f(pz.xy, ZERO2F, dim_f.xy),
) && (t_min > ZERO3F); ) && (t_min > ZERO3F);
if !any(hit) { if !any(hit) {
return vec4<f32>(0.0); return RayStart();
} }
pos = select(select(pz, py, hit.y), px, hit.x);
axis = select(select(2u, 1u, hit.y), 0u, hit.x); axis = select(select(2u, 1u, hit.y), 0u, hit.x);
} }
t_min *= f32(1u << (MAX_SCALE - group.scale)); let t_mult = f32(1u << (MAX_SCALE - group.scale));
t_min *= t_mult;
// time to move 1 unit in each direction // time to move 1 unit in each direction
let full = f32(1u << MAX_SCALE); let full = f32(1u << MAX_SCALE);
let inc_t = abs(1.0 / dir) * full; let t_inc = abs(1.0 / dir) * full;
let t_offset = max(max(t_min.x, t_min.y), t_min.z); let t_offset = max(max(t_min.x, t_min.y), t_min.z);
var t = max(0.0, t_offset); let t = max(0.0, t_offset);
let dir_i = vec3<i32>(dir_if); let dir_i = vec3<i32>(dir_if);
let dir_u = vec3<u32>((dir_i + vec3<i32>(1)) / 2); let dir_u = vec3<u32>((dir_i + vec3<i32>(1)) / 2);
let dir_bits = vec_to_dir(dir_u); let dir_bits = vec_to_dir(dir_u);
let inv_dir_bits = 7 - dir_bits; let inv_dir_bits = 7 - dir_bits;
var node_start = 1u; let node_start = 1u;
var scale = MAX_SCALE - 1; let scale = MAX_SCALE - 1;
var scale_exp2 = 0.5; let scale_exp2 = 0.5;
var color = vec4<f32>(0.0); let parents = array<u32, MAX_SCALE>();
var parents = array<u32, MAX_SCALE>();
var child = 0u; var child = 0u;
var vox_pos = vec3<f32>(0.0); var vox_pos = vec3<f32>(1.0);
let t_center = t_min + scale_exp2 * inc_t; let t_center = t_min + scale_exp2 * t_inc;
if t > t_center.x { vox_pos.x = 0.5; child |= 4u; } if t > t_center.x { vox_pos.x = 1.5; child |= 4u; }
if t > t_center.y { vox_pos.y = 0.5; child |= 2u; } if t > t_center.y { vox_pos.y = 1.5; child |= 2u; }
if t > t_center.z { vox_pos.z = 0.5; child |= 1u; } if t > t_center.z { vox_pos.z = 1.5; child |= 1u; }
let min_adj = t_min - t_inc;
var iters = 0; return RayStart(
loop { true,
if iters == MAX_ITERS { Ray(
return vec4<f32>(1.0, 0.0, 1.0, 1.0); t,
vox_pos,
t_inc,
scale,
min_adj,
child,
axis,
node_start,
group.offset,
inv_dir_bits,
parents,
),
normals,
t_mult,
);
} }
iters += 1;
let t_corner = vox_pos * inc_t + t_min; fn ray_next(ray: Ray, skip: u32) -> RayResult {
let node = voxels[group.offset + node_start + (child ^ inv_dir_bits)]; let group_offset = ray.group_offset;
let t_inc = ray.t_inc;
let min_adj = ray.min_adj;
let inv_dir_bits = ray.inv_dir_bits;
var scale = ray.scale;
var scale_exp2 = bitcast<f32>((scale + 127 - MAX_SCALE) << 23);
var vox_pos = ray.vox_pos;
var t = ray.t;
var node_start = ray.node_start;
var child = ray.child;
var parents = ray.parents;
var axis: u32;
var data = 0u;
loop {
let t_corner = vox_pos * t_inc + min_adj;
let node = voxels[group_offset + node_start + (child ^ inv_dir_bits)];
if node >= LEAF_BIT { if node >= LEAF_BIT {
if node != LEAF_BIT { if node != skip {
let vcolor = get_color(node & LEAF_MASK); data = node;
let diffuse = max(dot(global_lights[0].dir, normals[axis]) + 0.1, 0.0); break;
let ambient = 0.2;
let lighting = max(diffuse, ambient);
let new_color = min(vcolor.xyz * lighting, vec3<f32>(1.0));
color += vec4<f32>(new_color.xyz * vcolor.a, vcolor.a) * (1.0 - color.a);
if color.a > FULL_ALPHA { break; }
} }
// move to next time point and determine which axis to move along // move to next time point and determine which axis to move along
let t_next = t_corner + scale_exp2 * inc_t; let t_next = t_corner + scale_exp2 * t_inc;
t = min(min(t_next.x, t_next.y), t_next.z); t = min(min(t_next.x, t_next.y), t_next.z);
axis = select(select(0u, 1u, t == t_next.y), 2u, t == t_next.z); axis = select(select(0u, 1u, t == t_next.y), 2u, t == t_next.z);
let move_dir = 4u >> axis; let move_dir = 4u >> axis;
@@ -165,22 +268,23 @@ fn trace_full(pos_view: vec4<f32>, dir_view: vec4<f32>) -> vec4<f32> {
// check if need to pop stack // check if need to pop stack
if (child & move_dir) > 0 { if (child & move_dir) > 0 {
// calculate new scale; first differing bit after adding // calculate new scale; first differing bit after adding
let axis_pos = u32(vox_pos[axis] * full); let axis_pos = vox_pos[axis];
let differing = axis_pos ^ (axis_pos + u32(scale_exp2 * full)); // AWARE
scale = u32(firstLeadingBit(differing)); let differing = bitcast<u32>(axis_pos) ^ bitcast<u32>(axis_pos + scale_exp2);
if scale == MAX_SCALE { break; } scale = (bitcast<u32>(f32(differing)) >> 23) - 127 - (23 - MAX_SCALE);
scale_exp2 = bitcast<f32>((scale + 127 - MAX_SCALE) << 23);
if scale >= MAX_SCALE { break; }
// restore & recalculate parent // restore & recalculate parent
let parent_info = parents[scale]; let parent_info = parents[scale];
node_start = parent_info >> 3; node_start = parent_info >> 3;
child = parent_info & 7; child = parent_info & 7;
let scale_vec = vec3<u32>(scale); let scale_vec = vec3<u32>(scale + 23 - MAX_SCALE);
// remove lower scale bits // remove bits lower than current scale
vox_pos = vec3<f32>((vec3<i32>(vox_pos * full) >> scale_vec) << scale_vec) / full; vox_pos = bitcast<vec3<f32>>((bitcast<vec3<u32>>(vox_pos) >> scale_vec) << scale_vec);
scale_exp2 = 1.0 / f32(1u << (MAX_SCALE - scale));
} }
// move to next child and voxel position // move to next child and voxel position
child ^= move_dir; child += move_dir;
vox_pos[axis] += scale_exp2; vox_pos[axis] += scale_exp2;
} else { } else {
// push current node to stack // push current node to stack
@@ -190,20 +294,31 @@ fn trace_full(pos_view: vec4<f32>, dir_view: vec4<f32>) -> vec4<f32> {
// calculate child node vars // calculate child node vars
scale_exp2 *= 0.5; scale_exp2 *= 0.5;
child = 0u; child = 0u;
let t_center = t_corner + scale_exp2 * inc_t; let t_center = t_corner + scale_exp2 * t_inc;
if t > t_center.x { vox_pos.x += scale_exp2; child |= 4u; } if t > t_center.x { vox_pos.x += scale_exp2; child |= 4u; }
if t > t_center.y { vox_pos.y += scale_exp2; child |= 2u; } if t > t_center.y { vox_pos.y += scale_exp2; child |= 2u; }
if t > t_center.z { vox_pos.z += scale_exp2; child |= 1u; } if t > t_center.z { vox_pos.z += scale_exp2; child |= 1u; }
node_start += 8 + node; node_start += 8 + node;
} }
} }
// return vec4<f32>(f32(iters) / f32(MAX_ITERS), 0.0, 0.0, 1.0); return RayResult(
return color; Ray(
t,
vox_pos,
t_inc,
scale,
min_adj,
child,
axis,
node_start,
group_offset,
inv_dir_bits,
parents,
),
data
);
} }
const LEAF_BIT = 1u << 31u;
const LEAF_MASK = ~LEAF_BIT;
fn dir_to_vec(bits: u32) -> vec3<u32> { fn dir_to_vec(bits: u32) -> vec3<u32> {
return vec3<u32>(bits >> 2, (bits & 2) >> 1, bits & 1); return vec3<u32>(bits >> 2, (bits & 2) >> 1, bits & 1);
} }

View File

@@ -52,11 +52,16 @@ fn main(@builtin(global_invocation_id) cell: vec3<u32>) {
textureStore(output, cell.xy, color); textureStore(output, cell.xy, color);
} }
const LEAF_BIT = 1u << 31u;
const LEAF_MASK = ~LEAF_BIT;
const ZERO3F = vec3<f32>(0.0); const ZERO3F = vec3<f32>(0.0);
const ZERO2F = vec2<f32>(0.0); const ZERO2F = vec2<f32>(0.0);
const FULL_ALPHA = 0.999; const FULL_ALPHA = 0.999;
const EPSILON = 0.00000000001; const EPSILON = 0.00000000001;
const MAX_ITERS = 1000; const MAX_ITERS = 2000;
// NOTE: CANNOT GO HIGHER THAN 23 due to how floating point
// numbers are stored and the bit manipulation used
const MAX_SCALE: u32 = 10; const MAX_SCALE: u32 = 10;
fn trace_full(pos_view: vec4<f32>, dir_view: vec4<f32>) -> vec4<f32> { fn trace_full(pos_view: vec4<f32>, dir_view: vec4<f32>) -> vec4<f32> {
@@ -91,16 +96,13 @@ fn trace_full(pos_view: vec4<f32>, dir_view: vec4<f32>) -> vec4<f32> {
// find where ray intersects with group // find where ray intersects with group
let pos_min = (vec3<f32>(1.0) - dir_uf) * dim_f; let pos_min = (vec3<f32>(1.0) - dir_uf) * dim_f;
let pos_max = dir_uf * dim_f;
var pos = pos_start;
// time of intersection; x = td + p, solve for t // time of intersection; x = td + p, solve for t
let t_min = (pos_min - pos) / dir; var t_min = (pos_min - pos_start) / dir;
let t_max = (pos_max - pos) / dir; if outside3f(pos_start, ZERO3F, dim_f) {
if outside3f(pos, ZERO3F, dim_f) {
// points of intersection // points of intersection
let px = pos + t_min.x * dir; let px = pos_start + t_min.x * dir;
let py = pos + t_min.y * dir; let py = pos_start + t_min.y * dir;
let pz = pos + t_min.z * dir; let pz = pos_start + t_min.z * dir;
// check if point is in bounds // check if point is in bounds
let hit = vec3<bool>( let hit = vec3<bool>(
@@ -111,29 +113,36 @@ fn trace_full(pos_view: vec4<f32>, dir_view: vec4<f32>) -> vec4<f32> {
if !any(hit) { if !any(hit) {
return vec4<f32>(0.0); return vec4<f32>(0.0);
} }
pos = select(select(pz, py, hit.y), px, hit.x);
axis = select(select(2u, 1u, hit.y), 0u, hit.x); axis = select(select(2u, 1u, hit.y), 0u, hit.x);
} }
let t_mult =f32(1u << (MAX_SCALE - group.scale));
t_min *= t_mult;
// time to move 1 unit in each direction // time to move 1 unit in each direction
let inc_t = abs(1.0 / dir); let full = f32(1u << MAX_SCALE);
let inc_t = abs(1.0 / dir) * full;
let t_offset = max(max(t_min.x, t_min.y), t_min.z); let t_offset = max(max(t_min.x, t_min.y), t_min.z);
var t = max(0.0, t_offset); var t = max(0.0, t_offset);
let dir_i = vec3<i32>(dir_if); let dir_i = vec3<i32>(dir_if);
let dir_u = vec3<u32>((dir_i + vec3<i32>(1)) / 2); let dir_u = vec3<u32>(dir_uf);
let dir_bits = vec_to_dir(dir_u); let dir_bits = vec_to_dir(dir_u);
let inv_dir_bits = 7 - dir_bits; let inv_dir_bits = 7 - dir_bits;
var node_start = 1u; var node_start = 1u;
var scale = group.scale - 1; var scale = MAX_SCALE - 1;
var half_t_span = f32(1u << scale) * inc_t; var scale_exp2 = 0.5;
var t_center = t_min + half_t_span;
var color = vec4<f32>(0.0); var color = vec4<f32>(0.0);
var parents = array<u32, MAX_SCALE>(); var parents = array<u32, MAX_SCALE>();
var prev = LEAF_BIT;
var old_t = t;
var child = (u32(t > t_center.x) << 2) + (u32(t > t_center.y) << 1) + u32(t > t_center.z); var child = 0u;
var child_pos = dir_to_vec(child); var vox_pos = vec3<f32>(1.0);
var vox_pos = child_pos * (1u << scale); let t_center = t_min + scale_exp2 * inc_t;
if t > t_center.x { vox_pos.x = 1.5; child |= 4u; }
if t > t_center.y { vox_pos.y = 1.5; child |= 2u; }
if t > t_center.z { vox_pos.z = 1.5; child |= 1u; }
let min_adj = t_min - inc_t;
var iters = 0; var iters = 0;
loop { loop {
@@ -141,10 +150,22 @@ fn trace_full(pos_view: vec4<f32>, dir_view: vec4<f32>) -> vec4<f32> {
return vec4<f32>(1.0, 0.0, 1.0, 1.0); return vec4<f32>(1.0, 0.0, 1.0, 1.0);
} }
iters += 1; iters += 1;
let t_corner = vox_pos * inc_t + min_adj;
let node = voxels[group.offset + node_start + (child ^ inv_dir_bits)]; let node = voxels[group.offset + node_start + (child ^ inv_dir_bits)];
if node >= LEAF_BIT { if node >= LEAF_BIT {
if node != prev {
if node != LEAF_BIT { if node != LEAF_BIT {
let vcolor = get_color(node & LEAF_MASK); let dist = (t - old_t) / t_mult;
old_t = t;
let filt = min(dist / 64.0, 1.0);
if prev == LEAF_BIT + 3 {
color.a += filt * (1.0 - color.a);
if color.a > FULL_ALPHA { break; }
}
let pos = (vox_pos - 1.5) * (dir_if) + 0.5 - scale_exp2 * (1.0 - dir_uf);
// let pos = t / t_mult;
// if true {return vec4<f32>(pos, 1.0);}
let vcolor = get_color(node & LEAF_MASK, pos);
let diffuse = max(dot(global_lights[0].dir, normals[axis]) + 0.1, 0.0); let diffuse = max(dot(global_lights[0].dir, normals[axis]) + 0.1, 0.0);
let ambient = 0.2; let ambient = 0.2;
let lighting = max(diffuse, ambient); let lighting = max(diffuse, ambient);
@@ -152,9 +173,11 @@ fn trace_full(pos_view: vec4<f32>, dir_view: vec4<f32>) -> vec4<f32> {
color += vec4<f32>(new_color.xyz * vcolor.a, vcolor.a) * (1.0 - color.a); color += vec4<f32>(new_color.xyz * vcolor.a, vcolor.a) * (1.0 - color.a);
if color.a > FULL_ALPHA { break; } if color.a > FULL_ALPHA { break; }
} }
prev = node;
}
// move to next time point and determine which axis to move along // move to next time point and determine which axis to move along
let t_next = t_center + half_t_span * vec3<f32>(child_pos); let t_next = t_corner + scale_exp2 * inc_t;
t = min(min(t_next.x, t_next.y), t_next.z); t = min(min(t_next.x, t_next.y), t_next.z);
axis = select(select(0u, 1u, t == t_next.y), 2u, t == t_next.z); axis = select(select(0u, 1u, t == t_next.y), 2u, t == t_next.z);
let move_dir = 4u >> axis; let move_dir = 4u >> axis;
@@ -163,45 +186,44 @@ fn trace_full(pos_view: vec4<f32>, dir_view: vec4<f32>) -> vec4<f32> {
if (child & move_dir) > 0 { if (child & move_dir) > 0 {
// calculate new scale; first differing bit after adding // calculate new scale; first differing bit after adding
let axis_pos = vox_pos[axis]; let axis_pos = vox_pos[axis];
let differing = axis_pos ^ (axis_pos + (1u << scale)); // AWARE
scale = firstLeadingBit(differing); let differing = bitcast<u32>(axis_pos) ^ bitcast<u32>(axis_pos + scale_exp2);
if scale == group.scale { break; } scale = (bitcast<u32>(f32(differing)) >> 23) - 127 - (23 - MAX_SCALE);
scale_exp2 = bitcast<f32>((scale + 127 - MAX_SCALE) << 23);
if scale >= MAX_SCALE { break; }
// restore & recalculate parent // restore & recalculate parent
let parent_info = parents[scale]; let parent_info = parents[scale];
node_start = parent_info >> 3; node_start = parent_info >> 3;
child = parent_info & 7; child = parent_info & 7;
let scale_vec = vec3<u32>(scale + 1); let scale_vec = vec3<u32>(scale + 23 - MAX_SCALE);
vox_pos = (vox_pos >> scale_vec) << scale_vec; // remove lower scale bits // remove bits lower than current scale
half_t_span = f32(1u << scale) * inc_t; vox_pos = bitcast<vec3<f32>>((bitcast<vec3<u32>>(vox_pos) >> scale_vec) << scale_vec);
t_center = vec3<f32>(vox_pos) * inc_t + t_min + half_t_span;
} }
// move to next child and voxel position // move to next child and voxel position
child ^= move_dir; child += move_dir;
child_pos = dir_to_vec(child); vox_pos[axis] += scale_exp2;
vox_pos |= child_pos << vec3<u32>(scale);
// vox_pos[axis] += (1u << scale);
} else { } else {
// push current node to stack // push current node to stack
parents[scale] = (node_start << 3) + child; parents[scale] = (node_start << 3) + child;
scale -= 1u; scale -= 1u;
// calculate child node vars // calculate child node vars
half_t_span /= 2.0; scale_exp2 *= 0.5;
t_center += half_t_span * (vec3<f32>(child_pos * 2) - 1.0); child = 0u;
child_pos = vec3<u32>(vec3<f32>(t) > t_center); let t_center = t_corner + scale_exp2 * inc_t;
child = (child_pos.x << 2) + (child_pos.y << 1) + child_pos.z; if t > t_center.x { vox_pos.x += scale_exp2; child |= 4u; }
vox_pos += child_pos * (1u << scale); if t > t_center.y { vox_pos.y += scale_exp2; child |= 2u; }
if t > t_center.z { vox_pos.z += scale_exp2; child |= 1u; }
node_start += 8 + node; node_start += 8 + node;
} }
} }
// let fog = min(t / t_mult / 1000.0, 1.0);
// return vec4<f32>(color.xyz * (1.0 - fog) + vec3<f32>(fog), color.a * (1.0 - fog) + fog);
// return vec4<f32>(f32(iters) / f32(MAX_ITERS), 0.0, 0.0, 1.0); // return vec4<f32>(f32(iters) / f32(MAX_ITERS), 0.0, 0.0, 1.0);
return color; return color;
} }
const LEAF_BIT = 1u << 31u;
const LEAF_MASK = ~LEAF_BIT;
fn dir_to_vec(bits: u32) -> vec3<u32> { fn dir_to_vec(bits: u32) -> vec3<u32> {
return vec3<u32>(bits >> 2, (bits & 2) >> 1, bits & 1); return vec3<u32>(bits >> 2, (bits & 2) >> 1, bits & 1);
} }
@@ -210,19 +232,24 @@ fn vec_to_dir(vec: vec3<u32>) -> u32 {
return vec.x * 4 + vec.y * 2 + vec.z * 1; return vec.x * 4 + vec.y * 2 + vec.z * 1;
} }
fn get_color(id: u32) -> vec4<f32> { fn get_color(id: u32, pos: vec3<f32>) -> vec4<f32> {
let random = random(pos);
let random2 = random(pos + vec3<f32>(0.0001));
switch id { switch id {
case 0u: { case 0u: {
return vec4<f32>(0.0); return vec4<f32>(0.0);
} }
case 1u: { case 1u: {
return vec4<f32>(0.5, 0.5, 0.5, 1.0); let color = vec3<f32>(0.5, 0.5, 0.5 + random * 0.2) * (random2 * 0.4 + 0.8);
return vec4<f32>(color, 1.0);
} }
case 2u: { case 2u: {
return vec4<f32>(0.5, 1.0, 0.5, 1.0); let color = vec3<f32>(0.4 + random * 0.2, 0.9, 0.4 + random * 0.2) * (random2 * 0.2 + 0.9);
return vec4<f32>(color, 1.0);
} }
case 3u: { case 3u: {
return vec4<f32>(0.5, 0.5, 1.0, 0.5); let color = vec3<f32>(0.5, 0.5, 1.0) * (random2 * 0.2 + 0.8);
return vec4<f32>(color, 0.5);
} }
default: { default: {
return vec4<f32>(1.0, 0.0, 0.0, 1.0); return vec4<f32>(1.0, 0.0, 0.0, 1.0);
@@ -230,6 +257,10 @@ fn get_color(id: u32) -> vec4<f32> {
} }
} }
fn random(pos: vec3<f32>) -> f32 {
return fract(sin(dot(pos,vec3<f32>(12.9898,78.233,25.1279)))*43758.5453123);
}
fn outside3f(v: vec3<f32>, low: vec3<f32>, high: vec3<f32>) -> bool { fn outside3f(v: vec3<f32>, low: vec3<f32>, high: vec3<f32>) -> bool {
return any(v < low) || any(v > high); return any(v < low) || any(v > high);
} }

View File

@@ -8,7 +8,7 @@ use bevy_derive::{Deref, DerefMut};
use bevy_ecs::{bundle::Bundle, component::Component, entity::Entity, system::Resource}; use bevy_ecs::{bundle::Bundle, component::Component, entity::Entity, system::Resource};
use nalgebra::Vector3; use nalgebra::Vector3;
pub const SCALE: u32 = 8; pub const SCALE: u32 = 10;
pub const SIDE_LENGTH: usize = 2usize.pow(SCALE); pub const SIDE_LENGTH: usize = 2usize.pow(SCALE);
pub const SHAPE: (usize, usize, usize) = (SIDE_LENGTH, SIDE_LENGTH, SIDE_LENGTH); pub const SHAPE: (usize, usize, usize) = (SIDE_LENGTH, SIDE_LENGTH, SIDE_LENGTH);
pub const DIMENSIONS: Vector3<usize> = Vector3::new(SIDE_LENGTH, SIDE_LENGTH, SIDE_LENGTH); pub const DIMENSIONS: Vector3<usize> = Vector3::new(SIDE_LENGTH, SIDE_LENGTH, SIDE_LENGTH);

View File

@@ -1,17 +1,10 @@
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use bevy_ecs::{entity::Entity, system::Commands}; use bevy_ecs::{entity::Entity, system::Commands};
use nalgebra::Vector3;
use ndarray::{s, Array3, Axis};
use simdnoise::NoiseBuilder;
use crate::{ use crate::{
client::render::voxel::VoxelColor, common::component::{ChunkBundle, ChunkData, ChunkMesh, ChunkPos}, server::generation::generate_tree, util::
common::component::{chunk, ChunkBundle, ChunkData, ChunkMesh, ChunkPos}, thread::{ExitType, ThreadChannel, ThreadHandle}
util::{
oct_tree::OctTree,
thread::{ExitType, ThreadChannel, ThreadHandle},
},
}; };
pub struct ChunkManager { pub struct ChunkManager {
@@ -114,32 +107,7 @@ fn chunk_loader_main(channel: ThreadChannel<ServerChunkMsg, ChunkLoaderMsg>) {
let tree = ChunkData::from_tree(generate_tree(pos)); let tree = ChunkData::from_tree(generate_tree(pos));
let tree_time = std::time::Instant::now() - start; let tree_time = std::time::Instant::now() - start;
// let start = std::time::Instant::now(); println!("gen time: {:<5?}; size: {}", tree_time, tree.raw().len());
// let mut data = generate(pos);
// let data_time = std::time::Instant::now() - start;
//
// let start = std::time::Instant::now();
// let shape = s![
// 1..data.len_of(Axis(0)) - 1,
// 1..data.len_of(Axis(1)) - 1,
// 1..data.len_of(Axis(2)) - 1
// ];
// let mut slice = data.slice_mut(shape);
// let mut iter = tree.into_iter();
// slice.assign(&Array3::from_shape_fn(chunk::SHAPE, |_| {
// iter.next().unwrap()
// }));
// let convert_time = std::time::Instant::now() - start;
//
// let start = std::time::Instant::now();
// let mesh = ChunkMesh::from_data(data.map(|i| COLOR_MAP[*i as usize]).view());
// let mesh_time = std::time::Instant::now() - start;
//
// println!(
// "data: {:<5?} mesh: {:<5?} convert: {:<5?} tree: {:<5?}",
// data_time, mesh_time, convert_time, tree_time
// );
println!("gen time: {:<5?}", tree_time);
channel.send(ServerChunkMsg::ChunkGenerated(GeneratedChunk { channel.send(ServerChunkMsg::ChunkGenerated(GeneratedChunk {
pos, pos,
@@ -154,83 +122,3 @@ fn chunk_loader_main(channel: ThreadChannel<ServerChunkMsg, ChunkLoaderMsg>) {
} }
} }
fn generate(pos: ChunkPos) -> Array3<u32> {
let shape = [chunk::SIDE_LENGTH + 2; 3];
if pos.y > 0 || pos.y < -1 {
return Array3::from_elem(shape, 0);
}
let posf: Vector3<f32> = (pos.cast() * chunk::SIDE_LENGTH as f32) - Vector3::from_element(1.0);
let (noise, min, max) = NoiseBuilder::gradient_2d_offset(
posf.x,
chunk::SIDE_LENGTH + 2,
posf.z,
chunk::SIDE_LENGTH + 2,
)
.with_seed(0)
.with_freq(0.005)
.generate();
Array3::from_shape_fn(shape, |(x, y, z)| {
generate_at(Vector3::new(x, y, z), posf, &noise, min, max)
})
}
fn generate_tree(pos: ChunkPos) -> OctTree {
if pos.y > 0 || pos.y < -1 {
return OctTree::from_leaf(0, 8);
}
let posf: Vector3<f32> = pos.cast() * chunk::SIDE_LENGTH as f32;
let (noise, min, max) =
NoiseBuilder::gradient_2d_offset(posf.x, chunk::SIDE_LENGTH, posf.z, chunk::SIDE_LENGTH)
.with_seed(0)
.with_freq(1.0 / (chunk::SIDE_LENGTH as f32))
.generate();
OctTree::from_fn_rec(&mut |p| generate_at(p, posf, &noise, min, max), chunk::SCALE)
}
fn generate_at(p: Vector3<usize>, posf: Vector3<f32>, noise: &[f32], min: f32, max: f32) -> u32 {
// 0 air 1 stone 2 "sand" 3 water
let y = p.y as f32 + posf.y;
// highest heights, 0.0 .. 1.0 relative to chunk size
let [water, grass, top] = [0.18, 0.35, 0.5].map(|f| chunk::SIDE_LENGTH as f32 * f);
let n = ((noise[p.x + p.z * chunk::SIDE_LENGTH] - min) / (max - min) * 2.0).exp2() * top * 0.25;
if y < n {
if y < water {
1
} else if y < grass {
2
} else {
1
}
} else if y <= water {
3
} else {
0
}
}
const COLOR_MAP: [VoxelColor; 4] = [
VoxelColor {
r: 0,
g: 0,
b: 0,
a: 0,
},
VoxelColor {
r: 150,
g: 150,
b: 150,
a: 255,
},
VoxelColor {
r: 100,
g: 255,
b: 100,
a: 255,
},
VoxelColor {
r: 100,
g: 100,
b: 255,
a: 200,
},
];

View File

@@ -0,0 +1,145 @@
use nalgebra::Vector3;
use simdnoise::NoiseBuilder;
use crate::{
common::component::{chunk, ChunkPos},
util::oct_tree::OctTree,
};
pub fn generate_tree(pos: ChunkPos) -> OctTree {
if pos.y > 0 || pos.y < -1 {
return OctTree::from_leaf(0, 8);
}
let posf: Vector3<f32> = pos.cast() * chunk::SIDE_LENGTH as f32;
let noise1 = generate_noise_map(0, 1.0, posf, chunk::SCALE, &mut |v: f32| {
(v * 2.0).exp2() * TOP * 0.25
});
let noise2 = generate_noise_map(1, 50.0, posf, chunk::SCALE, &mut |v: f32| v * 20.0 + GRASS);
OctTree::from_fn_rec(
&mut |p| generate_leaf(p, posf, (&noise1.base, &noise2.base)),
&mut |p, lvl| generate_node(p, lvl, posf, (&noise1, &noise2)),
chunk::SCALE,
)
}
const WATER: f32 = 0.18 * chunk::SIDE_LENGTH as f32;
const GRASS: f32 = 0.35 * chunk::SIDE_LENGTH as f32;
const TOP: f32 = 0.5 * chunk::SIDE_LENGTH as f32;
// 0 air 1 stone 2 grass 3 water
fn generate_leaf(p: Vector3<usize>, posf: Vector3<f32>, noise: (&[f32], &[f32])) -> u32 {
let y = p.y as f32 + posf.y;
let n = noise.0[p.x + p.z * chunk::SIDE_LENGTH];
let n2 = noise.1[p.x + p.z * chunk::SIDE_LENGTH];
if y < n {
if y < WATER {
1
} else if y < n2 {
2
} else {
1
}
} else if y <= WATER {
3
} else {
0
}
}
// 0 air 1 stone 2 grass 3 water
fn generate_node(
p: Vector3<usize>,
scale: u32,
posf: Vector3<f32>,
noise: (&NoiseMap, &NoiseMap),
) -> Option<u32> {
let side_len = 2usize.pow(scale);
let y = NumRange {
min: p.y as f32 + posf.y,
max: (p.y + side_len - 1) as f32 + posf.y,
};
let l = scale as usize - 1;
let i = (p.x >> scale) + (p.z >> scale) * (chunk::SIDE_LENGTH / side_len);
let n = &noise.0.levels[l][i];
let n2 = &noise.1.levels[l][i];
Some(if y.max < n.min {
if y.max < WATER {
1
} else if y.max < n2.min && y.min >= WATER {
2
} else if y.min > n2.max {
1
} else {
return None;
}
} else if y.max <= WATER && y.min > n.max {
3
} else if y.min > WATER && y.min > n.max {
0
} else {
return None;
})
}
fn generate_noise_map(
seed: i32,
freq: f32,
posf: Vector3<f32>,
levels: u32,
adjust: &mut impl FnMut(f32) -> f32,
) -> NoiseMap {
let mut size = 2usize.pow(levels);
let (mut base, min, max) = NoiseBuilder::gradient_2d_offset(posf.x, size, posf.z, size)
.with_seed(seed)
.with_freq(freq / (size as f32))
.generate();
for v in &mut base {
*v = adjust((*v - min) / (max - min));
}
let first_len = base.len() / 4;
let mut first = Vec::with_capacity(first_len);
for y in (0..size).step_by(2) {
for x in (0..size).step_by(2) {
let a = base[x + y * size];
let b = base[x + 1 + y * size];
let c = base[x + (y + 1) * size];
let d = base[x + 1 + (y + 1) * size];
first.push(NumRange {
min: a.min(b).min(c).min(d),
max: a.max(b).max(c).max(d),
})
}
}
let mut arr = vec![first];
for l in 1..levels as usize {
size /= 2;
let prev = &arr[l - 1];
let mut new = Vec::with_capacity(prev.len() / 4);
for y in (0..size).step_by(2) {
for x in (0..size).step_by(2) {
let a = &prev[x + y * size];
let b = &prev[x + 1 + y * size];
let c = &prev[x + (y + 1) * size];
let d = &prev[x + 1 + (y + 1) * size];
new.push(NumRange {
min: a.min.min(b.min).min(c.min).min(d.min),
max: a.max.max(b.max).max(c.max).max(d.max),
})
}
}
arr.push(new);
}
NoiseMap { base, levels: arr }
}
#[derive(Debug)]
pub struct NoiseMap {
levels: Vec<Vec<NumRange>>,
base: Vec<f32>,
}
#[derive(Debug)]
pub struct NumRange {
min: f32,
max: f32,
}

View File

@@ -3,6 +3,7 @@ mod client;
mod rsc; mod rsc;
mod system; mod system;
mod test; mod test;
mod generation;
pub use client::*; pub use client::*;

View File

@@ -1,2 +1,3 @@
pub mod thread; pub mod thread;
pub mod oct_tree; pub mod oct_tree;
pub mod timer;

View File

@@ -1,6 +1,5 @@
use std::fmt::Debug; use std::fmt::Debug;
use bevy_ecs::system::IntoSystem;
use nalgebra::Vector3; use nalgebra::Vector3;
use ndarray::ArrayView3; use ndarray::ArrayView3;
@@ -20,13 +19,13 @@ impl OctNode {
pub const fn is_leaf(&self) -> bool { pub const fn is_leaf(&self) -> bool {
self.0 >= LEAF_BIT self.0 >= LEAF_BIT
} }
pub fn is_node(&self) -> bool { pub const fn is_node(&self) -> bool {
self.0 < LEAF_BIT self.0 < LEAF_BIT
} }
pub fn node_data(&self) -> u32 { pub const fn node_data(&self) -> u32 {
self.0 self.0
} }
pub fn leaf_data(&self) -> u32 { pub const fn leaf_data(&self) -> u32 {
self.0 & !LEAF_BIT self.0 & !LEAF_BIT
} }
} }
@@ -57,17 +56,22 @@ impl OctTree {
levels, levels,
} }
} }
pub fn from_fn_rec(f: &mut impl FnMut(Vector3<usize>) -> u32, levels: u32) -> OctTree { pub fn from_fn_rec(
Self::from_fn_offset(f, levels, Vector3::from_element(0)) f_leaf: &mut impl FnMut(Vector3<usize>) -> u32,
f_node: &mut impl FnMut(Vector3<usize>, u32) -> Option<u32>,
levels: u32,
) -> OctTree {
Self::from_fn_offset(f_leaf, f_node, levels, Vector3::from_element(0))
} }
pub fn from_fn_offset( pub fn from_fn_offset(
f: &mut impl FnMut(Vector3<usize>) -> u32, f_leaf: &mut impl FnMut(Vector3<usize>) -> u32,
f_node: &mut impl FnMut(Vector3<usize>, u32) -> Option<u32>,
levels: u32, levels: u32,
offset: Vector3<usize>, offset: Vector3<usize>,
) -> Self { ) -> Self {
let mut data = Vec::new(); let mut data = Vec::new();
data.push(OctNode::new_node(0)); data.push(OctNode::new_node(0));
Self::from_fn_offset_inner(f, &mut data, levels, offset); Self::from_fn_offset_inner(f_leaf, f_node, &mut data, levels, offset);
if data.len() == 2 { if data.len() == 2 {
data.remove(0); data.remove(0);
} }
@@ -78,17 +82,18 @@ impl OctTree {
} }
} }
fn from_fn_offset_inner( fn from_fn_offset_inner(
f: &mut impl FnMut(Vector3<usize>) -> u32, f_leaf: &mut impl FnMut(Vector3<usize>) -> u32,
f_node: &mut impl FnMut(Vector3<usize>, u32) -> Option<u32>,
accumulator: &mut Vec<OctNode>, accumulator: &mut Vec<OctNode>,
level: u32, level: u32,
offset: Vector3<usize>, offset: Vector3<usize>,
) { ) {
if level == 0 { if level == 0 {
accumulator.push(OctNode::new_leaf(f(offset))); accumulator.push(OctNode::new_leaf(f_leaf(offset)));
return; return;
} else if level == 1 { } else if level == 1 {
let leaves: [OctNode; 8] = let leaves: [OctNode; 8] =
core::array::from_fn(|i| OctNode::new_leaf(f(offset + CORNERS[i]))); core::array::from_fn(|i| OctNode::new_leaf(f_leaf(offset + CORNERS[i])));
if leaves[1..].iter().all(|l| *l == leaves[0]) { if leaves[1..].iter().all(|l| *l == leaves[0]) {
accumulator.push(leaves[0]); accumulator.push(leaves[0]);
} else { } else {
@@ -100,13 +105,13 @@ impl OctTree {
accumulator.resize(i + 8, OctNode::new_node(0)); accumulator.resize(i + 8, OctNode::new_node(0));
let mut data_start = 0; let mut data_start = 0;
for (j, corner_offset) in CORNERS.iter().enumerate() { for (j, corner_offset) in CORNERS.iter().enumerate() {
let lvl = level - 1;
let pos = offset + corner_offset * 2usize.pow(lvl);
if let Some(node) = f_node(pos, lvl) {
accumulator[i + j] = OctNode::new_leaf(node);
} else {
let sub_start = accumulator.len(); let sub_start = accumulator.len();
Self::from_fn_offset_inner( Self::from_fn_offset_inner(f_leaf, f_node, accumulator, lvl, pos);
f,
accumulator,
level - 1,
offset + corner_offset * 2usize.pow(level - 1),
);
let len = accumulator.len() - sub_start; let len = accumulator.len() - sub_start;
if len == 1 { if len == 1 {
accumulator[i + j] = accumulator[sub_start]; accumulator[i + j] = accumulator[sub_start];
@@ -116,6 +121,7 @@ impl OctTree {
data_start += len; data_start += len;
} }
} }
}
if data_start == 0 { if data_start == 0 {
let first = accumulator[i]; let first = accumulator[i];
if accumulator[i + 1..i + 8].iter().all(|l| *l == first) { if accumulator[i + 1..i + 8].iter().all(|l| *l == first) {
@@ -125,10 +131,7 @@ impl OctTree {
} }
} }
pub fn from_fn_iter( pub fn from_fn_iter(f: &mut impl FnMut(Vector3<usize>) -> u32, levels: u32) -> Self {
f: &mut impl FnMut(Vector3<usize>) -> u32,
levels: u32,
) -> Self {
let mut data = vec![OctNode::new_node(0)]; let mut data = vec![OctNode::new_node(0)];
let mut level: usize = 1; let mut level: usize = 1;
let mut children = Vec::new(); let mut children = Vec::new();
@@ -173,7 +176,7 @@ impl OctTree {
} }
pub fn from_arr(arr: ArrayView3<u32>, levels: u32) -> Self { pub fn from_arr(arr: ArrayView3<u32>, levels: u32) -> Self {
Self::from_fn_rec(&mut |p| arr[(p.x, p.y, p.z)], levels) Self::from_fn_rec(&mut |p| arr[(p.x, p.y, p.z)], &mut |_, _| None, levels)
} }
pub fn get(&self, mut pos: Vector3<usize>) -> u32 { pub fn get(&self, mut pos: Vector3<usize>) -> u32 {
let mut data_start = 1; let mut data_start = 1;
@@ -199,7 +202,6 @@ impl OctTree {
pub struct OctTreeIter<'a> { pub struct OctTreeIter<'a> {
queue: Vec<OctNode>, queue: Vec<OctNode>,
levels: Vec<u32>, levels: Vec<u32>,
pos: usize,
cur: u32, cur: u32,
run: usize, run: usize,
data: &'a [OctNode], data: &'a [OctNode],
@@ -218,9 +220,8 @@ impl<'a> Iterator for OctTreeIter<'a> {
self.run = 8usize.pow(level); self.run = 8usize.pow(level);
self.cur = node.leaf_data(); self.cur = node.leaf_data();
} else { } else {
let pos = 0; let add = &self.data[..8];
let add = &self.data[pos..pos + 8]; self.data = &self.data[DATA_OFFSET..];
self.data = &self.data[pos + DATA_OFFSET..];
self.queue.extend(add.iter().rev()); self.queue.extend(add.iter().rev());
self.levels.resize(self.levels.len() + 8, level - 1); self.levels.resize(self.levels.len() + 8, level - 1);
} }
@@ -234,7 +235,6 @@ impl<'a> IntoIterator for &'a OctTree {
fn into_iter(self) -> Self::IntoIter { fn into_iter(self) -> Self::IntoIter {
OctTreeIter { OctTreeIter {
data: &self.data[1..], data: &self.data[1..],
pos: 0,
cur: 0, cur: 0,
levels: vec![self.levels], levels: vec![self.levels],
run: 0, run: 0,

59
src/util/timer.rs Normal file
View File

@@ -0,0 +1,59 @@
use std::time::{Duration, Instant};
pub struct Timer {
start: Instant,
pos: usize,
times: Vec<Option<Instant>>,
durs: Vec<Option<Duration>>,
}
impl Timer {
pub fn new(len: usize) -> Self {
Self {
start: Instant::now(),
pos: 0,
durs: vec![None; len],
times: vec![None; len],
}
}
pub fn start(&mut self) {
self.start = Instant::now();
}
pub fn stop(&mut self) {
let duration = Instant::now() - self.start;
self.durs[self.pos] = Some(duration);
self.times[self.pos] = Some(self.start);
self.pos = (self.pos + 1) % self.times.len();
}
pub fn avg(&self) -> Duration {
let filtered: Vec<_> = self.durs.iter().filter_map(|d| *d).collect();
let len = filtered.len();
if len != 0 {
let total: Duration = filtered.into_iter().sum();
total / len as u32
} else {
Duration::ZERO
}
}
pub fn max(&self) -> Duration {
self.durs
.iter()
.filter_map(|d| *d)
.max()
.unwrap_or(Duration::ZERO)
}
pub fn per_sec(&self) -> usize {
let now = Instant::now();
let mut count = 0;
while count < self.times.len() {
let i = (self.pos + count + 1) % self.times.len();
let Some(t) = self.times[i] else { break };
if now - t <= Duration::from_secs(1) {
count += 1;
} else {
break;
}
}
count
}
}