upload data and orient thing

This commit is contained in:
2024-06-04 15:36:04 -04:00
parent 7ae6a01949
commit f455837183
5 changed files with 100 additions and 103 deletions

View File

@@ -144,7 +144,6 @@ impl<'a> Renderer<'a> {
&self.device, &self.device,
&mut encoder, &mut encoder,
&mut self.staging_belt, &mut self.staging_belt,
&mut self.queue,
&RenderUpdateData { &RenderUpdateData {
state, state,
size: &self.size, size: &self.size,

View File

@@ -0,0 +1,10 @@
use nalgebra::{Transform3, Vector3};
#[repr(C, align(16))]
#[derive(Debug, Clone, Copy, PartialEq, bytemuck::Zeroable)]
pub struct VoxelGroup {
pub transform: Transform3<f32>,
pub dimensions: Vector3<u32>,
}
unsafe impl bytemuck::Pod for VoxelGroup {}

View File

@@ -2,5 +2,6 @@ mod grid;
mod view; mod view;
mod pipeline; mod pipeline;
mod color; mod color;
mod group;
pub use pipeline::*; pub use pipeline::*;

View File

@@ -1,4 +1,6 @@
use super::{color::VoxelColor, view::View}; use nalgebra::{Rotation3, Transform3, Translation3, Vector3};
use super::{color::VoxelColor, group::VoxelGroup, view::View};
use crate::client::render::{ use crate::client::render::{
buf::ArrBufUpdate, storage::Storage, uniform::Uniform, RenderUpdateData, buf::ArrBufUpdate, storage::Storage, uniform::Uniform, RenderUpdateData,
}; };
@@ -8,7 +10,7 @@ pub struct VoxelPipeline {
view: Uniform<View>, view: Uniform<View>,
bind_group_layout: wgpu::BindGroupLayout, bind_group_layout: wgpu::BindGroupLayout,
bind_group: wgpu::BindGroup, bind_group: wgpu::BindGroup,
texture: wgpu::Texture, voxel_groups: Storage<VoxelGroup>,
voxels: Storage<VoxelColor>, voxels: Storage<VoxelColor>,
arst: bool, arst: bool,
} }
@@ -24,56 +26,16 @@ impl VoxelPipeline {
source: wgpu::ShaderSource::Wgsl(include_str!("shader.wgsl").into()), source: wgpu::ShaderSource::Wgsl(include_str!("shader.wgsl").into()),
}); });
let view = Uniform::<View>::init(device, "View", 0); let view = Uniform::<View>::init(device, "view", 0);
let texture_size = wgpu::Extent3d { let voxels = Storage::init(device, "voxels", 1);
width: WIDTH, let voxel_groups = Storage::init(device, "voxel groups", 2);
height: HEIGHT,
depth_or_array_layers: 1,
};
let texture = device.create_texture(&wgpu::TextureDescriptor {
size: texture_size,
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::Rgba8UnormSrgb,
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
label: Some("diffuse_texture"),
view_formats: &[],
});
let voxels = Storage::init(device, "voxels", 3);
let diffuse_texture_view = texture.create_view(&wgpu::TextureViewDescriptor::default());
let diffuse_sampler = device.create_sampler(&wgpu::SamplerDescriptor {
address_mode_u: wgpu::AddressMode::ClampToEdge,
address_mode_v: wgpu::AddressMode::ClampToEdge,
address_mode_w: wgpu::AddressMode::ClampToEdge,
mag_filter: wgpu::FilterMode::Linear,
min_filter: wgpu::FilterMode::Nearest,
mipmap_filter: wgpu::FilterMode::Nearest,
..Default::default()
});
// bind groups // bind groups
let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
entries: &[ entries: &[
view.bind_group_layout_entry(), view.bind_group_layout_entry(),
wgpu::BindGroupLayoutEntry {
binding: 1,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Texture {
multisampled: false,
view_dimension: wgpu::TextureViewDimension::D2,
sample_type: wgpu::TextureSampleType::Float { filterable: true },
},
count: None,
},
wgpu::BindGroupLayoutEntry {
binding: 2,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
count: None,
},
voxels.bind_group_layout_entry(), voxels.bind_group_layout_entry(),
voxel_groups.bind_group_layout_entry(),
], ],
label: Some("tile_bind_group_layout"), label: Some("tile_bind_group_layout"),
}); });
@@ -82,15 +44,8 @@ impl VoxelPipeline {
layout: &bind_group_layout, layout: &bind_group_layout,
entries: &[ entries: &[
view.bind_group_entry(), view.bind_group_entry(),
wgpu::BindGroupEntry {
binding: 1,
resource: wgpu::BindingResource::TextureView(&diffuse_texture_view),
},
wgpu::BindGroupEntry {
binding: 2,
resource: wgpu::BindingResource::Sampler(&diffuse_sampler),
},
voxels.bind_group_entry(), voxels.bind_group_entry(),
voxel_groups.bind_group_entry(),
], ],
label: Some("tile_bind_group"), label: Some("tile_bind_group"),
}); });
@@ -145,8 +100,8 @@ impl VoxelPipeline {
view, view,
bind_group, bind_group,
bind_group_layout, bind_group_layout,
texture,
voxels, voxels,
voxel_groups,
arst: false, arst: false,
} }
} }
@@ -156,38 +111,37 @@ impl VoxelPipeline {
device: &wgpu::Device, device: &wgpu::Device,
encoder: &mut wgpu::CommandEncoder, encoder: &mut wgpu::CommandEncoder,
belt: &mut wgpu::util::StagingBelt, belt: &mut wgpu::util::StagingBelt,
queue: &mut wgpu::Queue,
update_data: &RenderUpdateData, update_data: &RenderUpdateData,
) { ) {
let texture_size = wgpu::Extent3d {
width: WIDTH,
height: HEIGHT,
depth_or_array_layers: 1,
};
if !self.arst { if !self.arst {
queue.write_texture( let lx = 15;
// Tells wgpu where to copy the pixel data let ly = 10;
wgpu::ImageCopyTexture { let lz = 10;
texture: &self.texture, let size = lx * ly * lz;
mip_level: 0, let mut data = vec![VoxelColor::none(); size];
origin: wgpu::Origin3d::ZERO, for x in 0..lx {
aspect: wgpu::TextureAspect::All, for y in 0..ly {
}, data[x + y * lx] = VoxelColor {
// The actual pixel data r: (x as f32 / lx as f32 * 255.0) as u8,
&[0xff, 0x00, 0xff, 0xff].repeat((WIDTH * HEIGHT) as usize), g: (y as f32 / ly as f32 * 255.0) as u8,
// The layout of the texture b: 0,
wgpu::ImageDataLayout { a: 20,
offset: 0, };
bytes_per_row: Some(4 * WIDTH), }
rows_per_image: Some(HEIGHT), }
}, for x in 0..lx {
texture_size, for y in 0..ly {
); data[x + y * lx + 3 * lx * ly] = VoxelColor {
let l = 10; r: (x as f32 / lx as f32 * 255.0) as u8,
let size = l * l * l; g: (y as f32 / ly as f32 * 255.0) as u8,
let mut data: Vec<_> = vec![VoxelColor::none(); size]; b: 100,
data[0] = VoxelColor::white(); a: 255,
data[size - 1] = VoxelColor::white(); };
}
}
for i in 0..lx.min(ly.min(lz)) {
data[i + i * lx + i * lx * ly] = VoxelColor::white();
}
self.voxels.update( self.voxels.update(
device, device,
encoder, encoder,
@@ -195,6 +149,32 @@ impl VoxelPipeline {
data.len(), data.len(),
&[ArrBufUpdate { offset: 0, data }], &[ArrBufUpdate { offset: 0, data }],
); );
let group = VoxelGroup {
transform: Transform3::identity()
* (Translation3::new(-5.0, -5.0, 20.0)
* Rotation3::from_axis_angle(&Vector3::y_axis(), 0.5)).inverse(),
dimensions: Vector3::new(lx as u32, ly as u32, lz as u32),
};
self.voxel_groups.update(
device,
encoder,
belt,
1,
&[ArrBufUpdate {
offset: 0,
data: vec![group],
}],
);
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.arst = true;
} }
self.view.update(device, encoder, belt, update_data); self.view.update(device, encoder, belt, update_data);

View File

@@ -13,14 +13,17 @@ struct View {
transform: mat4x4<f32>, transform: mat4x4<f32>,
}; };
struct VoxelGroup {
transform: mat4x4<f32>,
dimensions: vec3<u32>,
};
@group(0) @binding(0) @group(0) @binding(0)
var<uniform> view: View; var<uniform> view: View;
@group(0) @binding(1) @group(0) @binding(1)
var t_diffuse: texture_2d<f32>;
@group(0) @binding(2)
var s_diffuse: sampler;
@group(0) @binding(3)
var<storage, read> voxels: array<u32>; var<storage, read> voxels: array<u32>;
@group(0) @binding(2)
var<storage, read> voxel_groups: array<VoxelGroup>;
@vertex @vertex
fn vs_main( fn vs_main(
@@ -52,30 +55,34 @@ fn fs_main(
pixel_pos.y *= 2.0; pixel_pos.y *= 2.0;
pixel_pos.y *= aspect; pixel_pos.y *= aspect;
pixel_pos = (view.transform * vec4<f32>(pixel_pos, 1.0)).xyz; let group = voxel_groups[0];
let origin = (view.transform * vec4<f32>(0.0, 0.0, 0.0, 1.0)).xyz; let dim_f = vec3<f32>(group.dimensions);
// this should definitely be done per pixel trust me guys
let transform = group.transform * view.transform;
pixel_pos = (transform * vec4<f32>(pixel_pos, 1.0)).xyz;
let origin = (transform * vec4<f32>(0.0, 0.0, 0.0, 1.0)).xyz;
let dir = normalize(pixel_pos - origin); let dir = normalize(pixel_pos - origin);
let voxel_pos = vec3<f32>(-5.0, -5.0, 30.0);
var t = 0; var t = 0;
var color = vec4<f32>(0.0, 0.0, 0.0, 0.0);
for(t = 0; t < 1000; t += 1) { for(t = 0; t < 1000; t += 1) {
let pos = pixel_pos + f32(t) * 0.1 * dir - voxel_pos; let pos = pixel_pos + f32(t) * 0.1 * dir;
let rel_coords = vec3<i32>(pos.xyz); if pos.x < 0.0 || pos.y < 0.0 || pos.z < 0.0 || pos.x > dim_f.x || pos.y > dim_f.y || pos.z > dim_f.z {
if rel_coords.x < 0 || rel_coords.y < 0 || rel_coords.z < 0 || rel_coords.x > 10 || rel_coords.y > 10 || rel_coords.z > 10 {
continue; continue;
} else { } else {
let i = rel_coords.x + rel_coords.y * 10 + rel_coords.z * 100; let rel_coords = vec3<u32>(pos.xyz);
let color = unpack4x8unorm(voxels[i]); let i = u32(rel_coords.x + rel_coords.y * group.dimensions.x + rel_coords.z * group.dimensions.x * group.dimensions.y);
if voxels[i] != 0 { let vcolor = unpack4x8unorm(voxels[i]);
return vec4<f32>(1.0); // now I understand premultiplied alpha lmao
} else { color += vec4<f32>(vcolor.xyz * vcolor.a * (1.0 - color.a), (1.0 - color.a) * vcolor.a);
let pos = vec3<f32>(rel_coords); if color.a == 1.0 {
return vec4<f32>(pos.x / 10.0, pos.y / 10.0, pos.z / 10.0, 1.0); break;
} }
} }
} }
return vec4<f32>(0.0, 0.0, 0.0, 1.0); return color;
} }