moved renderer to separate thread, add evenio and start structure, make it so you can add stuff
This commit is contained in:
@@ -1,7 +1,189 @@
|
||||
mod grid;
|
||||
mod view;
|
||||
mod pipeline;
|
||||
mod color;
|
||||
mod group;
|
||||
|
||||
pub use pipeline::*;
|
||||
pub use color::*;
|
||||
|
||||
use nalgebra::{Projective3, Transform3, Translation3, Vector2};
|
||||
|
||||
use {group::VoxelGroup, view::View};
|
||||
use crate::client::{
|
||||
camera::Camera,
|
||||
render::{
|
||||
util::{ArrBufUpdate, Storage, Uniform},
|
||||
CreateVoxelGrid,
|
||||
},
|
||||
};
|
||||
|
||||
pub struct VoxelPipeline {
|
||||
pipeline: wgpu::RenderPipeline,
|
||||
view: Uniform<View>,
|
||||
bind_group_layout: wgpu::BindGroupLayout,
|
||||
bind_group: wgpu::BindGroup,
|
||||
voxel_groups: Storage<VoxelGroup>,
|
||||
voxels: Storage<VoxelColor>,
|
||||
}
|
||||
|
||||
impl VoxelPipeline {
|
||||
pub fn new(device: &wgpu::Device, format: &wgpu::TextureFormat) -> Self {
|
||||
// shaders
|
||||
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
|
||||
label: Some("Tile Shader"),
|
||||
source: wgpu::ShaderSource::Wgsl(include_str!("shader.wgsl").into()),
|
||||
});
|
||||
|
||||
let view = Uniform::<View>::init(device, "view", 0);
|
||||
let voxels = Storage::init(device, "voxels", 1);
|
||||
let voxel_groups = Storage::init(device, "voxel groups", 2);
|
||||
|
||||
// bind groups
|
||||
let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
entries: &[
|
||||
view.bind_group_layout_entry(),
|
||||
voxels.bind_group_layout_entry(),
|
||||
voxel_groups.bind_group_layout_entry(),
|
||||
],
|
||||
label: Some("tile_bind_group_layout"),
|
||||
});
|
||||
|
||||
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
layout: &bind_group_layout,
|
||||
entries: &[
|
||||
view.bind_group_entry(),
|
||||
voxels.bind_group_entry(),
|
||||
voxel_groups.bind_group_entry(),
|
||||
],
|
||||
label: Some("tile_bind_group"),
|
||||
});
|
||||
|
||||
// pipeline
|
||||
let render_pipeline_layout =
|
||||
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||
label: Some("Tile Pipeline Layout"),
|
||||
bind_group_layouts: &[&bind_group_layout],
|
||||
push_constant_ranges: &[],
|
||||
});
|
||||
|
||||
let render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
||||
label: Some("Voxel Pipeline"),
|
||||
layout: Some(&render_pipeline_layout),
|
||||
vertex: wgpu::VertexState {
|
||||
module: &shader,
|
||||
entry_point: "vs_main",
|
||||
buffers: &[],
|
||||
compilation_options: wgpu::PipelineCompilationOptions::default(),
|
||||
},
|
||||
fragment: Some(wgpu::FragmentState {
|
||||
module: &shader,
|
||||
entry_point: "fs_main",
|
||||
targets: &[Some(wgpu::ColorTargetState {
|
||||
format: *format,
|
||||
blend: Some(wgpu::BlendState::REPLACE),
|
||||
write_mask: wgpu::ColorWrites::ALL,
|
||||
})],
|
||||
compilation_options: wgpu::PipelineCompilationOptions::default(),
|
||||
}),
|
||||
primitive: wgpu::PrimitiveState {
|
||||
topology: wgpu::PrimitiveTopology::TriangleStrip,
|
||||
strip_index_format: None,
|
||||
front_face: wgpu::FrontFace::Ccw,
|
||||
cull_mode: None,
|
||||
polygon_mode: wgpu::PolygonMode::Fill,
|
||||
unclipped_depth: false,
|
||||
conservative: false,
|
||||
},
|
||||
depth_stencil: None,
|
||||
multisample: wgpu::MultisampleState {
|
||||
count: 1,
|
||||
mask: !0,
|
||||
alpha_to_coverage_enabled: true,
|
||||
},
|
||||
multiview: None,
|
||||
});
|
||||
|
||||
Self {
|
||||
pipeline: render_pipeline,
|
||||
view,
|
||||
bind_group,
|
||||
bind_group_layout,
|
||||
voxels,
|
||||
voxel_groups,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_group(
|
||||
&mut self,
|
||||
device: &wgpu::Device,
|
||||
encoder: &mut wgpu::CommandEncoder,
|
||||
belt: &mut wgpu::util::StagingBelt,
|
||||
CreateVoxelGrid {
|
||||
pos,
|
||||
orientation,
|
||||
dimensions,
|
||||
grid,
|
||||
}: CreateVoxelGrid,
|
||||
) {
|
||||
let offset = self.voxels.len();
|
||||
let updates = [ArrBufUpdate {
|
||||
offset,
|
||||
data: &grid,
|
||||
}];
|
||||
let size = offset + grid.len();
|
||||
self.voxels.update(device, encoder, belt, size, &updates);
|
||||
|
||||
let proj = Projective3::identity()
|
||||
* Translation3::from(pos)
|
||||
* orientation
|
||||
* Translation3::from(-dimensions.cast() / 2.0);
|
||||
let group = VoxelGroup {
|
||||
transform: proj,
|
||||
transform_inv: proj.inverse(),
|
||||
dimensions: dimensions.cast(),
|
||||
offset: offset as u32,
|
||||
};
|
||||
let updates = [ArrBufUpdate {
|
||||
offset: self.voxel_groups.len(),
|
||||
data: &[group],
|
||||
}];
|
||||
let size = self.voxel_groups.len() + 1;
|
||||
self.voxel_groups
|
||||
.update(device, encoder, belt, size, &updates);
|
||||
|
||||
self.bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
layout: &self.bind_group_layout,
|
||||
entries: &[
|
||||
self.view.bind_group_entry(),
|
||||
self.voxels.bind_group_entry(),
|
||||
self.voxel_groups.bind_group_entry(),
|
||||
],
|
||||
label: Some("tile_bind_group"),
|
||||
});
|
||||
}
|
||||
|
||||
pub fn update_view(
|
||||
&mut self,
|
||||
device: &wgpu::Device,
|
||||
encoder: &mut wgpu::CommandEncoder,
|
||||
belt: &mut wgpu::util::StagingBelt,
|
||||
size: Vector2<u32>,
|
||||
camera: &Camera,
|
||||
) {
|
||||
let transform =
|
||||
Transform3::identity() * Translation3::from(camera.pos) * camera.orientation;
|
||||
let data = View {
|
||||
width: size.x,
|
||||
height: size.y,
|
||||
zoom: camera.scale,
|
||||
padding: 0,
|
||||
transform,
|
||||
};
|
||||
self.view.update(device, encoder, belt, data)
|
||||
}
|
||||
|
||||
pub fn draw<'a>(&'a self, render_pass: &mut wgpu::RenderPass<'a>) {
|
||||
render_pass.set_pipeline(&self.pipeline);
|
||||
render_pass.set_bind_group(0, &self.bind_group, &[]);
|
||||
render_pass.draw(0..4, 0..1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,262 +0,0 @@
|
||||
use nalgebra::{Projective3, Rotation3, Transform3, Translation3, UnitVector3, Vector3};
|
||||
|
||||
use super::{color::VoxelColor, group::VoxelGroup, view::View};
|
||||
use crate::client::render::{
|
||||
buf::ArrBufUpdate, storage::Storage, uniform::Uniform, RenderUpdateData,
|
||||
};
|
||||
|
||||
pub struct VoxelPipeline {
|
||||
pipeline: wgpu::RenderPipeline,
|
||||
view: Uniform<View>,
|
||||
bind_group_layout: wgpu::BindGroupLayout,
|
||||
bind_group: wgpu::BindGroup,
|
||||
voxel_groups: Storage<VoxelGroup>,
|
||||
voxels: Storage<VoxelColor>,
|
||||
arst: bool,
|
||||
}
|
||||
|
||||
const WIDTH: u32 = 300;
|
||||
const HEIGHT: u32 = 300;
|
||||
|
||||
impl VoxelPipeline {
|
||||
pub fn new(device: &wgpu::Device, format: &wgpu::TextureFormat) -> Self {
|
||||
// shaders
|
||||
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
|
||||
label: Some("Tile Shader"),
|
||||
source: wgpu::ShaderSource::Wgsl(include_str!("shader.wgsl").into()),
|
||||
});
|
||||
|
||||
let view = Uniform::<View>::init(device, "view", 0);
|
||||
let voxels = Storage::init(device, "voxels", 1);
|
||||
let voxel_groups = Storage::init(device, "voxel groups", 2);
|
||||
|
||||
// bind groups
|
||||
let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
entries: &[
|
||||
view.bind_group_layout_entry(),
|
||||
voxels.bind_group_layout_entry(),
|
||||
voxel_groups.bind_group_layout_entry(),
|
||||
],
|
||||
label: Some("tile_bind_group_layout"),
|
||||
});
|
||||
|
||||
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
layout: &bind_group_layout,
|
||||
entries: &[
|
||||
view.bind_group_entry(),
|
||||
voxels.bind_group_entry(),
|
||||
voxel_groups.bind_group_entry(),
|
||||
],
|
||||
label: Some("tile_bind_group"),
|
||||
});
|
||||
|
||||
// pipeline
|
||||
let render_pipeline_layout =
|
||||
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||
label: Some("Tile Pipeline Layout"),
|
||||
bind_group_layouts: &[&bind_group_layout],
|
||||
push_constant_ranges: &[],
|
||||
});
|
||||
|
||||
let render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
||||
label: Some("Voxel Pipeline"),
|
||||
layout: Some(&render_pipeline_layout),
|
||||
vertex: wgpu::VertexState {
|
||||
module: &shader,
|
||||
entry_point: "vs_main",
|
||||
buffers: &[],
|
||||
compilation_options: wgpu::PipelineCompilationOptions::default(),
|
||||
},
|
||||
fragment: Some(wgpu::FragmentState {
|
||||
module: &shader,
|
||||
entry_point: "fs_main",
|
||||
targets: &[Some(wgpu::ColorTargetState {
|
||||
format: *format,
|
||||
blend: Some(wgpu::BlendState::REPLACE),
|
||||
write_mask: wgpu::ColorWrites::ALL,
|
||||
})],
|
||||
compilation_options: wgpu::PipelineCompilationOptions::default(),
|
||||
}),
|
||||
primitive: wgpu::PrimitiveState {
|
||||
topology: wgpu::PrimitiveTopology::TriangleStrip,
|
||||
strip_index_format: None,
|
||||
front_face: wgpu::FrontFace::Ccw,
|
||||
cull_mode: None,
|
||||
polygon_mode: wgpu::PolygonMode::Fill,
|
||||
unclipped_depth: false,
|
||||
conservative: false,
|
||||
},
|
||||
depth_stencil: None,
|
||||
multisample: wgpu::MultisampleState {
|
||||
count: 1,
|
||||
mask: !0,
|
||||
alpha_to_coverage_enabled: true,
|
||||
},
|
||||
multiview: None,
|
||||
});
|
||||
|
||||
Self {
|
||||
pipeline: render_pipeline,
|
||||
view,
|
||||
bind_group,
|
||||
bind_group_layout,
|
||||
voxels,
|
||||
voxel_groups,
|
||||
arst: false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update(
|
||||
&mut self,
|
||||
device: &wgpu::Device,
|
||||
encoder: &mut wgpu::CommandEncoder,
|
||||
belt: &mut wgpu::util::StagingBelt,
|
||||
update_data: &RenderUpdateData,
|
||||
) {
|
||||
if !self.arst {
|
||||
let lx = 15;
|
||||
let ly = 10;
|
||||
let lz = 10;
|
||||
let mut data = vec![VoxelColor::none(); lx * ly * lz];
|
||||
for x in 0..lx {
|
||||
for y in 0..ly {
|
||||
data[x + y * lx] = VoxelColor {
|
||||
r: (x as f32 / lx as f32 * 255.0) as u8,
|
||||
g: (y as f32 / ly as f32 * 255.0) as u8,
|
||||
b: 0,
|
||||
a: 100,
|
||||
};
|
||||
}
|
||||
}
|
||||
for x in 0..lx {
|
||||
for y in 0..ly {
|
||||
data[x + y * lx + 3 * lx * ly] = VoxelColor {
|
||||
r: (x as f32 / lx as f32 * 255.0) as u8,
|
||||
g: (y as f32 / ly as f32 * 255.0) as u8,
|
||||
b: 100,
|
||||
a: 255,
|
||||
};
|
||||
}
|
||||
}
|
||||
for i in 0..lx.min(ly.min(lz)) {
|
||||
data[i + i * lx + i * lx * ly] = VoxelColor::white();
|
||||
}
|
||||
|
||||
let lx2 = 1000;
|
||||
let ly2 = 2;
|
||||
let lz2 = 1000;
|
||||
let offset2 = data.len();
|
||||
let mut data2 = vec![VoxelColor::none(); lx2 * ly2 * lz2];
|
||||
let paint = VoxelColor {
|
||||
r: 255,
|
||||
g: 0,
|
||||
b: 255,
|
||||
a: 255,
|
||||
};
|
||||
for x in 0..lx2 {
|
||||
data2[x + (ly2 - 1) * lx2] = paint;
|
||||
data2[x + (ly2 - 1) * lx2 + (lz2 - 1) * lx2 * ly2] = paint;
|
||||
}
|
||||
for z in 0..lz2 {
|
||||
data2[(ly2 - 1) * lx2 + z * lx2 * ly2] = paint;
|
||||
data2[lx2 - 1 + (ly2 - 1) * lx2 + z * lx2 * ly2] = paint;
|
||||
}
|
||||
for x in 0..lx2 {
|
||||
for z in 0..lz2 {
|
||||
data2[x + z * lx2 * ly2] = rand::random();
|
||||
}
|
||||
}
|
||||
data.append(&mut data2);
|
||||
let lx3 = 3;
|
||||
let ly3 = 3;
|
||||
let lz3 = 3;
|
||||
let offset3 = data.len();
|
||||
data.append(&mut vec![
|
||||
VoxelColor {
|
||||
r: 255,
|
||||
g: 0,
|
||||
b: 255,
|
||||
a: 255,
|
||||
};
|
||||
lx3 * ly3 * lz3
|
||||
]);
|
||||
self.voxels.update(
|
||||
device,
|
||||
encoder,
|
||||
belt,
|
||||
data.len(),
|
||||
&[ArrBufUpdate { offset: 0, data }],
|
||||
);
|
||||
let proj = Projective3::identity()
|
||||
* Translation3::new(0.0, 0.0, 20.0)
|
||||
* Rotation3::from_axis_angle(&Vector3::y_axis(), 0.5)
|
||||
* Translation3::new(-(lx as f32 / 2.0), -(ly as f32 / 2.0), -(lz as f32 / 2.0));
|
||||
let group = VoxelGroup {
|
||||
transform: proj,
|
||||
transform_inv: proj.inverse(),
|
||||
dimensions: Vector3::new(lx as u32, ly as u32, lz as u32),
|
||||
offset: 0,
|
||||
};
|
||||
let proj2 = Projective3::identity()
|
||||
* Translation3::new(0.0, -2.1, 20.0)
|
||||
* Translation3::new(
|
||||
-(lx2 as f32 / 2.0),
|
||||
-(ly2 as f32 / 2.0),
|
||||
-(lz2 as f32 / 2.0),
|
||||
);
|
||||
let group2 = VoxelGroup {
|
||||
transform: proj2,
|
||||
transform_inv: proj2.inverse(),
|
||||
dimensions: Vector3::new(lx2 as u32, ly2 as u32, lz2 as u32),
|
||||
offset: offset2 as u32,
|
||||
};
|
||||
let proj3 = Projective3::identity()
|
||||
* Translation3::new(0.0, 0.0, 16.5)
|
||||
* Rotation3::from_axis_angle(&Vector3::y_axis(), std::f32::consts::PI / 4.0)
|
||||
* Rotation3::from_axis_angle(
|
||||
&UnitVector3::new_normalize(Vector3::new(1.0, 0.0, 1.0)),
|
||||
std::f32::consts::PI / 4.0,
|
||||
)
|
||||
* Translation3::new(
|
||||
-(lx3 as f32 / 2.0),
|
||||
-(ly3 as f32 / 2.0),
|
||||
-(lz3 as f32 / 2.0),
|
||||
);
|
||||
let group3 = VoxelGroup {
|
||||
transform: proj3,
|
||||
transform_inv: proj3.inverse(),
|
||||
dimensions: Vector3::new(lx3 as u32, ly3 as u32, lz3 as u32),
|
||||
offset: offset3 as u32,
|
||||
};
|
||||
let groups = vec![group, group2, group3];
|
||||
self.voxel_groups.update(
|
||||
device,
|
||||
encoder,
|
||||
belt,
|
||||
groups.len(),
|
||||
&[ArrBufUpdate {
|
||||
offset: 0,
|
||||
data: groups,
|
||||
}],
|
||||
);
|
||||
self.bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
layout: &self.bind_group_layout,
|
||||
entries: &[
|
||||
self.view.bind_group_entry(),
|
||||
self.voxels.bind_group_entry(),
|
||||
self.voxel_groups.bind_group_entry(),
|
||||
],
|
||||
label: Some("tile_bind_group"),
|
||||
});
|
||||
|
||||
self.arst = true;
|
||||
}
|
||||
self.view.update(device, encoder, belt, update_data);
|
||||
}
|
||||
|
||||
pub fn draw<'a>(&'a self, render_pass: &mut wgpu::RenderPass<'a>) {
|
||||
render_pass.set_pipeline(&self.pipeline);
|
||||
render_pass.set_bind_group(0, &self.bind_group, &[]);
|
||||
render_pass.draw(0..4, 0..1);
|
||||
}
|
||||
}
|
||||
@@ -167,7 +167,7 @@ fn apply_group(
|
||||
var depth = 0u;
|
||||
var prev_a = 0.0;
|
||||
loop {
|
||||
let i = u32(vox_pos.x + vox_pos.y * dim_i.x + vox_pos.z * dim_i.x * dim_i.y) + group.offset;
|
||||
let i = u32(vox_pos.x * dim_i.y * dim_i.z + vox_pos.y * dim_i.z + vox_pos.z) + group.offset;
|
||||
var vcolor = unpack4x8unorm(voxels[i]);
|
||||
let normal = next_normal;
|
||||
|
||||
@@ -204,7 +204,7 @@ fn apply_group(
|
||||
|
||||
// lighting
|
||||
let light = trace_light(full_pos);
|
||||
let diffuse = max(dot(norm_light, normal) * 1.3 + 0.1, 0.0);
|
||||
let diffuse = max(dot(norm_light, normal) * ((dot(dir_view.xyz, normal) + 1.0) / 2.0 * .7 + .3) * 1.3 + 0.1, 0.0);
|
||||
let ambient = 0.2;
|
||||
let specular = (exp(max(
|
||||
-(dot(reflect(dir_view.xyz, normal), norm_light) + 0.90) * 4.0, 0.0
|
||||
@@ -297,7 +297,7 @@ fn trace_one(gi: u32, pos_view: vec4<f32>, dir_view: vec4<f32>) -> vec4<f32> {
|
||||
var next_t = inc_t * abs(pos - corner);
|
||||
var color = vec4<f32>(0.0);
|
||||
loop {
|
||||
let i = u32(vox_pos.x + vox_pos.y * dim_i.x + vox_pos.z * dim_i.x * dim_i.y) + group.offset;
|
||||
let i = u32(vox_pos.x * dim_i.y * dim_i.z + vox_pos.y * dim_i.z + vox_pos.z) + group.offset;
|
||||
var vcolor = unpack4x8unorm(voxels[i]);
|
||||
|
||||
// select next voxel to move to next based on least time
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
use nalgebra::{Transform3, Translation3};
|
||||
|
||||
use crate::client::render::uniform::UniformData;
|
||||
use nalgebra::Transform3;
|
||||
|
||||
#[repr(C, align(16))]
|
||||
#[derive(Clone, Copy, PartialEq, bytemuck::Zeroable)]
|
||||
@@ -25,26 +23,3 @@ impl Default for View {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl UniformData for View {
|
||||
fn update(&mut self, data: &crate::client::render::RenderUpdateData) -> bool {
|
||||
let camera = data.state.camera;
|
||||
let new = Transform3::identity() * Translation3::from(camera.pos) * camera.orientation;
|
||||
if new == self.transform
|
||||
&& data.size.width == self.width
|
||||
&& data.size.height == self.height
|
||||
&& camera.scale == self.zoom
|
||||
{
|
||||
false
|
||||
} else {
|
||||
*self = Self {
|
||||
width: data.size.width,
|
||||
height: data.size.height,
|
||||
zoom: camera.scale,
|
||||
padding: 0,
|
||||
transform: new,
|
||||
};
|
||||
true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user