aspiojsanfopinaogpinhaeogfl
This commit is contained in:
@@ -0,0 +1,84 @@
|
|||||||
|
|
||||||
|
const BRICK_SIZE : usize = 32; // 32x32x32 brick size
|
||||||
|
const BRICK_NUM : usize = 64; // 64 bricks in texture
|
||||||
|
|
||||||
|
|
||||||
|
pub struct BrickBuffer {
|
||||||
|
//bricks: [[u8; BRICK_SIZE*BRICK_SIZE*BRICK_SIZE];BRICK_NUM],
|
||||||
|
texture: wgpu::Texture,
|
||||||
|
size: wgpu::Extent3d,
|
||||||
|
pub bind_layout: wgpu::BindGroupLayout,
|
||||||
|
pub bind_group: wgpu::BindGroup
|
||||||
|
}
|
||||||
|
|
||||||
|
impl BrickBuffer {
|
||||||
|
pub fn new(device: &wgpu::Device) -> Self {
|
||||||
|
|
||||||
|
// Create texture
|
||||||
|
let size = wgpu::Extent3d { width: (BRICK_SIZE*BRICK_NUM) as u32, height: BRICK_SIZE as u32, depth: BRICK_SIZE as u32 };
|
||||||
|
let texture = device.create_texture(
|
||||||
|
&wgpu::TextureDescriptor {
|
||||||
|
size: size,
|
||||||
|
mip_level_count: 1,
|
||||||
|
sample_count: 1,
|
||||||
|
dimension: wgpu::TextureDimension::D3,
|
||||||
|
format: wgpu::TextureFormat::R32Uint,
|
||||||
|
usage: wgpu::TextureUsage::SAMPLED | wgpu::TextureUsage::COPY_DST, // STORAGE?
|
||||||
|
label: Some("Brick texture"),
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// Bind layout
|
||||||
|
let bind_layout = device.create_bind_group_layout(
|
||||||
|
&wgpu::BindGroupLayoutDescriptor {
|
||||||
|
entries: &[
|
||||||
|
wgpu::BindGroupLayoutEntry {
|
||||||
|
binding: 0,
|
||||||
|
visibility: wgpu::ShaderStage::FRAGMENT,
|
||||||
|
ty: wgpu::BindingType::Texture { multisampled: false, view_dimension: wgpu::TextureViewDimension::D3, sample_type: wgpu::TextureSampleType::Uint },
|
||||||
|
count: None,
|
||||||
|
},
|
||||||
|
wgpu::BindGroupLayoutEntry {
|
||||||
|
binding: 1,
|
||||||
|
visibility: wgpu::ShaderStage::FRAGMENT,
|
||||||
|
ty: wgpu::BindingType::Sampler { comparison: false, filtering: false },
|
||||||
|
count: None,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
label: Some("Node texture layout"),
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// Bind group
|
||||||
|
let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
|
||||||
|
let 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::Nearest, min_filter: wgpu::FilterMode::Nearest, mipmap_filter: wgpu::FilterMode::Nearest,
|
||||||
|
..Default::default()
|
||||||
|
});
|
||||||
|
let bind_group = device.create_bind_group(
|
||||||
|
&wgpu::BindGroupDescriptor {
|
||||||
|
layout: &bind_layout,
|
||||||
|
entries: &[
|
||||||
|
wgpu::BindGroupEntry { binding: 0, resource: wgpu::BindingResource::TextureView(&view) },
|
||||||
|
wgpu::BindGroupEntry { binding: 1, resource: wgpu::BindingResource::Sampler(&sampler) }
|
||||||
|
],
|
||||||
|
label: Some("Brick texture group"),
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// Data
|
||||||
|
// let mut bricks = [[0; BRICK_SIZE*BRICK_SIZE*BRICK_SIZE]; BRICK_NUM];
|
||||||
|
// for x in 0..32 {
|
||||||
|
// for y in 0..16 {
|
||||||
|
// for z in 0..32 {
|
||||||
|
// let idx = x + BRICK_SIZE * (y + BRICK_SIZE * z);
|
||||||
|
// bricks[0][idx] = 1;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// Done
|
||||||
|
Self { texture, size, bind_layout, bind_group }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,6 +2,9 @@
|
|||||||
mod node_buffer;
|
mod node_buffer;
|
||||||
pub use node_buffer::NodeBuffer;
|
pub use node_buffer::NodeBuffer;
|
||||||
|
|
||||||
|
mod brick_buffer;
|
||||||
|
pub use brick_buffer::BrickBuffer;
|
||||||
|
|
||||||
mod uniform_buffer;
|
mod uniform_buffer;
|
||||||
pub use uniform_buffer::UniformBuffer;
|
pub use uniform_buffer::UniformBuffer;
|
||||||
pub use uniform_buffer::UniformValues;
|
pub use uniform_buffer::UniformValues;
|
||||||
|
|||||||
@@ -1,31 +1,10 @@
|
|||||||
|
|
||||||
// pub const DATA: &[u32] = &[
|
// 4x4x4 nodes
|
||||||
// // z0
|
const WORLD_SIZE: u32 = 4;
|
||||||
// 1,0,
|
const NUM_NODES: usize = (WORLD_SIZE*WORLD_SIZE*WORLD_SIZE) as usize;
|
||||||
// 0,1,
|
|
||||||
// // z1
|
|
||||||
// 0,0,
|
|
||||||
// 0,0,
|
|
||||||
// ];
|
|
||||||
|
|
||||||
const MAX_NODE_NUM : usize = 16;
|
|
||||||
const TEXTURE_SIZE : u32 = 4;
|
|
||||||
|
|
||||||
#[repr(C)]
|
|
||||||
#[derive(Default, Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
|
|
||||||
struct Node {
|
|
||||||
data: [u32; 8]
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Node {
|
|
||||||
pub fn set(&mut self, x: usize, y: usize, z: usize, value: u32) {
|
|
||||||
let idx = x + 2 * (y + 2 * z);
|
|
||||||
self.data[idx] = value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct NodeBuffer {
|
pub struct NodeBuffer {
|
||||||
nodes: [Node;MAX_NODE_NUM],
|
nodes: [u32;NUM_NODES],
|
||||||
texture: wgpu::Texture,
|
texture: wgpu::Texture,
|
||||||
size: wgpu::Extent3d,
|
size: wgpu::Extent3d,
|
||||||
pub bind_layout: wgpu::BindGroupLayout,
|
pub bind_layout: wgpu::BindGroupLayout,
|
||||||
@@ -36,10 +15,8 @@ impl NodeBuffer {
|
|||||||
|
|
||||||
pub fn new(device: &wgpu::Device) -> Self {
|
pub fn new(device: &wgpu::Device) -> Self {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Create texture
|
// Create texture
|
||||||
let size = wgpu::Extent3d { width: TEXTURE_SIZE, height: TEXTURE_SIZE, depth: TEXTURE_SIZE };
|
let size = wgpu::Extent3d { width: WORLD_SIZE, height: WORLD_SIZE, depth: WORLD_SIZE };
|
||||||
let texture = device.create_texture(
|
let texture = device.create_texture(
|
||||||
&wgpu::TextureDescriptor {
|
&wgpu::TextureDescriptor {
|
||||||
size: size,
|
size: size,
|
||||||
@@ -92,10 +69,8 @@ impl NodeBuffer {
|
|||||||
);
|
);
|
||||||
|
|
||||||
// Data
|
// Data
|
||||||
let mut nodes : [Node; MAX_NODE_NUM] = [Default::default(); MAX_NODE_NUM];
|
let mut nodes : [u32; NUM_NODES] = [Default::default(); NUM_NODES];
|
||||||
nodes[0].set(0, 0, 0, 1);
|
nodes[0] = 1;
|
||||||
nodes[1].set(0, 0, 0, 1);
|
|
||||||
nodes[1].set(1, 1, 1, 1);
|
|
||||||
|
|
||||||
// Done
|
// Done
|
||||||
Self { nodes, texture, size, bind_layout, bind_group }
|
Self { nodes, texture, size, bind_layout, bind_group }
|
||||||
|
|||||||
+2
-1
@@ -1,6 +1,6 @@
|
|||||||
use winit::{window::Window, dpi::PhysicalSize};
|
use winit::{window::Window, dpi::PhysicalSize};
|
||||||
|
|
||||||
mod buffers; use buffers::{NodeBuffer, UniformBuffer, RasterBuffer, Vertex};
|
mod buffers; use buffers::{NodeBuffer, BrickBuffer, UniformBuffer, RasterBuffer, Vertex};
|
||||||
pub mod camera; use camera::Camera;
|
pub mod camera; use camera::Camera;
|
||||||
|
|
||||||
|
|
||||||
@@ -107,6 +107,7 @@ impl Renderer {
|
|||||||
let raster_buffer = RasterBuffer::new(&device);
|
let raster_buffer = RasterBuffer::new(&device);
|
||||||
let mut node_buffer = NodeBuffer::new(&device);
|
let mut node_buffer = NodeBuffer::new(&device);
|
||||||
node_buffer.write(&queue);
|
node_buffer.write(&queue);
|
||||||
|
let mut brick_buffer = BrickBuffer::new(&device);
|
||||||
|
|
||||||
// Pipeline
|
// Pipeline
|
||||||
let pipeline = Self::create_pipeline(&device, &swapchain_desc, &[&uniform_buffer.bind_layout, &node_buffer.bind_layout]);
|
let pipeline = Self::create_pipeline(&device, &swapchain_desc, &[&uniform_buffer.bind_layout, &node_buffer.bind_layout]);
|
||||||
|
|||||||
Binary file not shown.
@@ -19,7 +19,7 @@ vec3 solve(vec3 point, vec3 view, vec3 normal)
|
|||||||
return (ambient + diffuse + specular) * object_color;
|
return (ambient + diffuse + specular) * object_color;
|
||||||
}
|
}
|
||||||
|
|
||||||
vec3 raycast(vec3 origin, vec3 dir)
|
vec3 castNodes(vec3 origin, vec3 dir)
|
||||||
{
|
{
|
||||||
// Round input pos to nearest voxel
|
// Round input pos to nearest voxel
|
||||||
vec3 pos = floor(origin);
|
vec3 pos = floor(origin);
|
||||||
@@ -48,9 +48,9 @@ vec3 raycast(vec3 origin, vec3 dir)
|
|||||||
vec3 normal = -incAxis * raySign;
|
vec3 normal = -incAxis * raySign;
|
||||||
|
|
||||||
// Get subnode
|
// Get subnode
|
||||||
return step(vec3(0.5, 0.5, 0.5), point - pos);
|
//return step(vec3(0.5, 0.5, 0.5), point - pos);
|
||||||
|
|
||||||
//return solve(point, dir, normal);
|
return solve(point, dir, normal);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get new closest axis to increment
|
// Get new closest axis to increment
|
||||||
|
|||||||
@@ -33,5 +33,5 @@ void main()
|
|||||||
vec3 ray_dir = normalize(view_matrix * vec4(view_dir,0)).xyz;
|
vec3 ray_dir = normalize(view_matrix * vec4(view_dir,0)).xyz;
|
||||||
|
|
||||||
// Raycast
|
// Raycast
|
||||||
out_color = vec4(raycast(cam_pos, ray_dir), 1.0);
|
out_color = vec4(castNodes(cam_pos, ray_dir), 1.0);
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user