idk stuff, need to try on another machine

This commit is contained in:
2024-09-12 19:12:44 -04:00
parent b6db483189
commit 244440c38a
9 changed files with 305 additions and 232 deletions

View File

@@ -162,7 +162,7 @@ impl Client<'_> {
match event {
WindowEvent::CloseRequested => self.exit = true,
WindowEvent::Resized(size) => self.renderer.resize(size),
WindowEvent::RedrawRequested => self.renderer.draw(),
// WindowEvent::RedrawRequested => self.renderer.draw(),
WindowEvent::CursorLeft { .. } => {
self.input.clear();
}

View File

@@ -110,7 +110,7 @@ impl<'a> Renderer<'a> {
label: None,
timestamp_writes: None,
});
self.voxel_pipeline.compute(&mut compute_pass, self.config.width, self.config.height);
self.voxel_pipeline.compute(&mut compute_pass);
drop(compute_pass);
let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
@@ -123,14 +123,15 @@ impl<'a> Renderer<'a> {
store: wgpu::StoreOp::Store,
},
})],
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
view: &self.depth_texture.view,
depth_ops: Some(wgpu::Operations {
load: wgpu::LoadOp::Clear(1.0),
store: wgpu::StoreOp::Store,
}),
stencil_ops: None,
}),
// depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
// view: &self.depth_texture.view,
// depth_ops: Some(wgpu::Operations {
// load: wgpu::LoadOp::Clear(1.0),
// store: wgpu::StoreOp::Store,
// }),
// stencil_ops: None,
// }),
depth_stencil_attachment: None,
timestamp_writes: None,
occlusion_query_set: None,
});

View File

@@ -54,7 +54,7 @@ impl DepthTexture {
pub struct StorageTexture {
binding: u32,
visibility: wgpu::ShaderStages,
pub texture: wgpu::Texture,
pub buf: wgpu::Texture,
pub view: wgpu::TextureView,
pub sampler: wgpu::Sampler,
}
@@ -97,7 +97,7 @@ impl StorageTexture {
Self {
visibility,
binding,
texture,
buf: texture,
view,
sampler,
}

View File

@@ -28,9 +28,12 @@ struct VoxelGroup {
};
@compute
@workgroup_size(16, 16, 1)
@workgroup_size(8, 8, 1)
fn main(@builtin(global_invocation_id) cell: vec3<u32>) {
// get position of the pixel; eye at origin, pixel on plane z = 1
if cell.x >= view.width || cell.y >= view.height {
return;
}
let win_dim = vec2<f32>(f32(view.width), f32(view.height));
let aspect = win_dim.y / win_dim.x;
let pixel_pos = vec3<f32>(
@@ -41,8 +44,11 @@ fn main(@builtin(global_invocation_id) cell: vec3<u32>) {
let dir = view.transform * vec4<f32>(normalize(pixel_pos), 0.0);
var color = trace_full(pos, dir);
// var color = vec4<f32>(0.0);
let light_mult = clamp((-dot(dir.xyz, global_lights[0].dir) - 0.99) * 200.0, 0.0, 1.0);
let sky_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_color = sun_color + sky_bg * (1.0 - light_mult);
color += vec4<f32>(sky_color * (1.0 - color.a), 1.0 - color.a);
color.a = 1.0;
textureStore(output, cell.xy, color);
@@ -51,7 +57,7 @@ fn main(@builtin(global_invocation_id) cell: vec3<u32>) {
const ZERO3F = vec3<f32>(0.0);
const ZERO2F = vec2<f32>(0.0);
const DEPTH = 16u;
const FULL_ALPHA = 0.9999;
const FULL_ALPHA = 0.999;
fn trace_full(pos_view: vec4<f32>, dir_view: vec4<f32>) -> vec4<f32> {
let gi = 0;

View File

@@ -149,13 +149,7 @@ impl VoxelPipeline {
unclipped_depth: false,
conservative: false,
},
depth_stencil: Some(wgpu::DepthStencilState {
format: DepthTexture::DEPTH_FORMAT,
depth_write_enabled: true,
depth_compare: wgpu::CompareFunction::Less,
stencil: wgpu::StencilState::default(),
bias: wgpu::DepthBiasState::default(),
}),
depth_stencil: None,
multisample: wgpu::MultisampleState {
count: 1,
mask: !0,
@@ -309,10 +303,10 @@ impl VoxelPipeline {
.update(device, encoder, belt, size, &updates);
self.id_map.insert(id, (i, group));
self.update_bind_group(device);
self.update_cbind_group(device);
}
pub fn update_bind_group(&mut self, device: &wgpu::Device) {
pub fn update_cbind_group(&mut self, device: &wgpu::Device) {
self.cbind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &self.cbind_group_layout,
entries: &[
@@ -338,7 +332,7 @@ impl VoxelPipeline {
wgpu::ShaderStages::COMPUTE | wgpu::ShaderStages::FRAGMENT,
4,
);
self.update_bind_group(device);
self.update_cbind_group(device);
self.bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &self.bind_group_layout,
entries: &[
@@ -405,9 +399,14 @@ impl VoxelPipeline {
render_pass.draw(0..4, 0..1);
}
pub fn compute(&self, pass: &mut wgpu::ComputePass, w: u32, h: u32) {
pub const WORKGROUP_SIZE: u32 = 8;
pub fn compute(&self, pass: &mut wgpu::ComputePass) {
pass.set_pipeline(&self.compute_pipeline);
pass.set_bind_group(0, &self.cbind_group, &[]);
pass.dispatch_workgroups(w / 16, h / 16, 1);
let buf = &self.texture.buf;
let x = (buf.width() - 1) / Self::WORKGROUP_SIZE + 1;
let y = (buf.height() - 1) / Self::WORKGROUP_SIZE + 1;
pass.dispatch_workgroups(x, y, 1);
}
}

View File

@@ -111,16 +111,7 @@ fn chunk_loader_main(channel: ThreadChannel<ServerChunkMsg, ChunkLoaderMsg>) {
match channel.recv_wait() {
ChunkLoaderMsg::Generate(pos) => {
let start = std::time::Instant::now();
// let data = ChunkData::from_tree(OctTree::from_arr(
// data.slice(s![
// 1..data.len_of(Axis(0)) - 1,
// 1..data.len_of(Axis(1)) - 1,
// 1..data.len_of(Axis(2)) - 1
// ]),
// 8,
// ));
let tree = ChunkData::from_tree(generate_tree(pos));
// let data = ChunkData::empty();
let tree_time = std::time::Instant::now() - start;
let start = std::time::Instant::now();
@@ -168,7 +159,6 @@ fn generate(pos: ChunkPos) -> Array3<u32> {
return Array3::from_elem(shape, 0);
}
let posf: Vector3<f32> = (pos.cast() * chunk::SIDE_LENGTH as f32) - Vector3::from_element(1.0);
let (a, b, c, d) = (0.0, 50.0, 100.0, 127.0);
let (noise, ..) = NoiseBuilder::gradient_2d_offset(
posf.x,
chunk::SIDE_LENGTH + 2,
@@ -178,25 +168,7 @@ fn generate(pos: ChunkPos) -> Array3<u32> {
.with_seed(0)
.with_freq(0.005)
.generate();
Array3::from_shape_fn(shape, |(x, y, z)| {
let y = y as f32 + posf.y;
let n = (noise[x + z * (chunk::SIDE_LENGTH + 2)] + 0.022) * (1.0 / 0.044) * d;
if y < n.max(b) {
if y < b {
if y > n {
3
} else {
1
}
} else if y < c {
2
} else {
1
}
} else {
0
}
})
Array3::from_shape_fn(shape, |(x, y, z)| generate_at(Vector3::new(x, y, z), posf, &noise))
}
fn generate_tree(pos: ChunkPos) -> OctTree {
@@ -204,34 +176,34 @@ fn generate_tree(pos: ChunkPos) -> OctTree {
return OctTree::from_leaf(0, 8);
}
let posf: Vector3<f32> = pos.cast() * chunk::SIDE_LENGTH as f32;
let (a, b, c, d) = (0.0, 50.0, 100.0, 127.0);
let (noise, ..) =
NoiseBuilder::gradient_2d_offset(posf.x, chunk::SIDE_LENGTH, posf.z, chunk::SIDE_LENGTH)
.with_seed(0)
.with_freq(0.005)
.generate();
OctTree::from_fn(
&mut |p| {
let y = p.y as f32 + posf.y;
let n = (noise[p.x + p.z * chunk::SIDE_LENGTH] + 0.022) * (1.0 / 0.044) * d;
if y < n.max(b) {
if y < b {
if y > n {
3
} else {
1
}
} else if y < c {
2
} else {
1
}
OctTree::from_fn_rec(&mut |p| generate_at(p, posf, &noise), 8)
}
fn generate_at(p: Vector3<usize>, posf: Vector3<f32>, noise: &[f32]) -> u32 {
// 0 air 1 stone 2 "sand" 3 water
let y = p.y as f32 + posf.y;
let (_a, b, c, d) = (0.0, 50.0, 100.0, 127.0);
let n = (noise[p.x + p.z * chunk::SIDE_LENGTH] + 0.022) * (1.0 / 0.044) * d;
if y < n.max(b) {
if y < b {
if y > n {
3
} else {
0
1
}
},
8,
)
} else if y < c {
2
} else {
1
}
} else {
0
}
}
const COLOR_MAP: [VoxelColor; 4] = [

View File

@@ -1,5 +1,6 @@
use std::fmt::Debug;
use bevy_ecs::system::IntoSystem;
use nalgebra::Vector3;
use ndarray::ArrayView3;
@@ -56,7 +57,7 @@ impl OctTree {
levels,
}
}
pub fn from_fn(f: &mut impl FnMut(Vector3<usize>) -> u32, levels: u32) -> OctTree {
pub fn from_fn_rec(f: &mut impl FnMut(Vector3<usize>) -> u32, levels: u32) -> OctTree {
Self::from_fn_offset(f, levels, Vector3::from_element(0))
}
pub fn from_fn_offset(
@@ -124,8 +125,55 @@ impl OctTree {
}
}
pub fn from_fn_iter(
f: &mut impl FnMut(Vector3<usize>) -> u32,
levels: u32,
) -> Self {
let mut data = vec![OctNode::new_node(0)];
let mut level: usize = 1;
let mut children = Vec::new();
let mut child = vec![0; levels as usize + 1];
let pows: Vec<_> = (0..levels).map(|l| 2usize.pow(l)).collect();
while level < levels as usize {
if child[level] == 8 {
let i = children.len() - 8;
let first = children[i];
if children[i + 1..].iter().all(|l| *l == first) {
children.truncate(i);
children.push(first);
} else {
data.extend_from_slice(&children[i..]);
children.truncate(i);
children.push(OctNode::new_node(data.len() as u32 - 8));
}
child[level] = 0;
level += 1;
child[level] += 1;
} else if level == 1 {
let offset: Vector3<usize> = (level..8).map(|l| CORNERS[child[l]] * pows[l]).sum();
let leaves: [OctNode; 8] =
core::array::from_fn(|i| OctNode::new_leaf(f(offset + CORNERS[i])));
if leaves[1..].iter().all(|l| *l == leaves[0]) {
children.push(leaves[0]);
} else {
children.push(OctNode::new_node(data.len() as u32));
data.extend_from_slice(&leaves);
}
child[level] += 1;
} else {
level -= 1;
}
}
data[0] = children[0];
Self {
data,
side_length: 2usize.pow(levels),
levels,
}
}
pub fn from_arr(arr: ArrayView3<u32>, levels: u32) -> Self {
Self::from_fn(&mut |p| arr[(p.x, p.y, p.z)], levels)
Self::from_fn_rec(&mut |p| arr[(p.x, p.y, p.z)], levels)
}
pub fn get(&self, mut pos: Vector3<usize>) -> u32 {
let mut data_start = 1;