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

View File

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

View File

@@ -34,7 +34,7 @@ pub struct VoxelPipeline {
}
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 {
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) {
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 {
println!("Failed to reload shader!");
return;
@@ -198,7 +198,7 @@ impl VoxelPipeline {
zoom: camera.scale,
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>) {

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 dir = view.transform * vec4<f32>(normalize(pixel_pos), 0.0);
let start = start_ray(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);
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 sun_color = light_mult * vec3<f32>(1.0, 1.0, 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;
// NOTE: CANNOT GO HIGHER THAN 23 due to how floating point
// numbers are stored and the bit manipulation used
const MAX_SCALE: u32 = 12;
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
);
}
const MAX_SCALE: u32 = 10;
fn trace_full(pos_view: vec4<f32>, dir_view: vec4<f32>) -> vec4<f32> {
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
let pos_min = (vec3<f32>(1.0) - dir_uf) * dim_f;
var pos = pos_start;
// time of intersection; x = td + p, solve for t
var t_min = (pos_min - pos) / dir;
if outside3f(pos, ZERO3F, dim_f) {
var t_min = (pos_min - pos_start) / dir;
if outside3f(pos_start, 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;
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>(
@@ -344,10 +113,10 @@ fn trace_full(pos_view: vec4<f32>, dir_view: vec4<f32>) -> vec4<f32> {
if !any(hit) {
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);
}
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
let full = f32(1u << MAX_SCALE);
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 scale = MAX_SCALE - 1;
var scale_exp2 = 0.5;
var skip = LEAF_BIT;
var color = vec4<f32>(0.0);
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 node = voxels[group.offset + node_start + (child ^ inv_dir_bits)];
if node >= LEAF_BIT {
if node != LEAF_BIT {
let vcolor = get_color(node & LEAF_MASK);
let diffuse = max(dot(global_lights[0].dir, normals[axis]) + 0.1, 0.0);
if node != skip {
skip = node;
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 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);
let specular = (exp(max(
-(dot(reflect(dir_view.xyz, normal), sun_dir) + 0.90) * 4.0, 0.0
)) - 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; }
}
@@ -438,6 +220,164 @@ fn trace_full(pos_view: vec4<f32>, dir_view: vec4<f32>) -> vec4<f32> {
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> {
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 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 sun_color = light_mult * vec3<f32>(1.0, 1.0, 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);
}
const LEAF_BIT = 1u << 31u;
const LEAF_MASK = ~LEAF_BIT;
const ZERO3F = vec3<f32>(0.0);
const ZERO2F = vec2<f32>(0.0);
const FULL_ALPHA = 0.999;
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;
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 group = voxel_groups[gi];
if group.scale == 0 {
return vec4<f32>(0.0);
return RayStart();
}
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;
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;}
@@ -91,7 +171,6 @@ fn trace_full(pos_view: vec4<f32>, dir_view: vec4<f32>) -> vec4<f32> {
// find where ray intersects with group
let pos_min = (vec3<f32>(1.0) - dir_uf) * dim_f;
var pos = pos_start;
// time of intersection; x = td + p, solve for t
var t_min = (pos_min - pos) / dir;
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),
) && (t_min > ZERO3F);
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);
}
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
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);
var t = max(0.0, t_offset);
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;
var node_start = 1u;
var scale = MAX_SCALE - 1;
var scale_exp2 = 0.5;
var color = vec4<f32>(0.0);
var parents = array<u32, MAX_SCALE>();
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>(0.0);
let t_center = t_min + scale_exp2 * inc_t;
if t > t_center.x { vox_pos.x = 0.5; child |= 4u; }
if t > t_center.y { vox_pos.y = 0.5; child |= 2u; }
if t > t_center.z { vox_pos.z = 0.5; child |= 1u; }
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;
var iters = 0;
return RayStart(
true,
Ray(
t,
vox_pos,
t_inc,
scale,
min_adj,
child,
axis,
node_start,
group.offset,
inv_dir_bits,
parents,
),
normals,
t_mult,
);
}
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 {
if iters == MAX_ITERS {
return vec4<f32>(1.0, 0.0, 1.0, 1.0);
}
iters += 1;
let t_corner = vox_pos * inc_t + t_min;
let node = voxels[group.offset + node_start + (child ^ inv_dir_bits)];
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 {
let vcolor = get_color(node & LEAF_MASK);
let diffuse = max(dot(global_lights[0].dir, normals[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; }
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 * inc_t;
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;
@@ -165,22 +268,23 @@ fn trace_full(pos_view: vec4<f32>, dir_view: vec4<f32>) -> vec4<f32> {
// check if need to pop stack
if (child & move_dir) > 0 {
// calculate new scale; first differing bit after adding
let axis_pos = u32(vox_pos[axis] * full);
let differing = axis_pos ^ (axis_pos + u32(scale_exp2 * full));
scale = u32(firstLeadingBit(differing));
if scale == MAX_SCALE { break; }
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);
// remove lower scale bits
vox_pos = vec3<f32>((vec3<i32>(vox_pos * full) >> scale_vec) << scale_vec) / full;
scale_exp2 = 1.0 / f32(1u << (MAX_SCALE - scale));
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;
child += move_dir;
vox_pos[axis] += scale_exp2;
} else {
// 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
scale_exp2 *= 0.5;
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.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 vec4<f32>(f32(iters) / f32(MAX_ITERS), 0.0, 0.0, 1.0);
return color;
return RayResult(
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> {
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);
}
const LEAF_BIT = 1u << 31u;
const LEAF_MASK = ~LEAF_BIT;
const ZERO3F = vec3<f32>(0.0);
const ZERO2F = vec2<f32>(0.0);
const FULL_ALPHA = 0.999;
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;
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
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
let t_min = (pos_min - pos) / dir;
let t_max = (pos_max - pos) / dir;
if outside3f(pos, ZERO3F, dim_f) {
var t_min = (pos_min - pos_start) / dir;
if outside3f(pos_start, 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;
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>(
@@ -111,29 +113,36 @@ fn trace_full(pos_view: vec4<f32>, dir_view: vec4<f32>) -> vec4<f32> {
if !any(hit) {
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);
}
let t_mult =f32(1u << (MAX_SCALE - group.scale));
t_min *= t_mult;
// 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);
var 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_u = vec3<u32>(dir_uf);
let dir_bits = vec_to_dir(dir_u);
let inv_dir_bits = 7 - dir_bits;
var node_start = 1u;
var scale = group.scale - 1;
var half_t_span = f32(1u << scale) * inc_t;
var t_center = t_min + half_t_span;
var scale = MAX_SCALE - 1;
var scale_exp2 = 0.5;
var color = vec4<f32>(0.0);
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_pos = dir_to_vec(child);
var vox_pos = child_pos * (1u << 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 iters = 0;
loop {
@@ -141,20 +150,34 @@ fn trace_full(pos_view: vec4<f32>, dir_view: vec4<f32>) -> vec4<f32> {
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 != LEAF_BIT {
let vcolor = get_color(node & LEAF_MASK);
let diffuse = max(dot(global_lights[0].dir, normals[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; }
if node != prev {
if node != LEAF_BIT {
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 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; }
}
prev = node;
}
// 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);
axis = select(select(0u, 1u, t == t_next.y), 2u, t == t_next.z);
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 {
// calculate new scale; first differing bit after adding
let axis_pos = vox_pos[axis];
let differing = axis_pos ^ (axis_pos + (1u << scale));
scale = firstLeadingBit(differing);
if scale == group.scale { break; }
// 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 + 1);
vox_pos = (vox_pos >> scale_vec) << scale_vec; // remove lower scale bits
half_t_span = f32(1u << scale) * inc_t;
t_center = vec3<f32>(vox_pos) * inc_t + t_min + half_t_span;
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;
child_pos = dir_to_vec(child);
vox_pos |= child_pos << vec3<u32>(scale);
// vox_pos[axis] += (1u << scale);
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
half_t_span /= 2.0;
t_center += half_t_span * (vec3<f32>(child_pos * 2) - 1.0);
child_pos = vec3<u32>(vec3<f32>(t) > t_center);
child = (child_pos.x << 2) + (child_pos.y << 1) + child_pos.z;
vox_pos += child_pos * (1u << scale);
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;
}
}
// 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 color;
}
const LEAF_BIT = 1u << 31u;
const LEAF_MASK = ~LEAF_BIT;
fn dir_to_vec(bits: u32) -> vec3<u32> {
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;
}
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 {
case 0u: {
return vec4<f32>(0.0);
}
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: {
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: {
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: {
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 {
return any(v < low) || any(v > high);
}