renamed brick to block, world gen stuff in separate file
This commit is contained in:
@@ -1,18 +1,18 @@
|
||||
use std::convert::TryInto;
|
||||
|
||||
use crate::renderer::buffers::{BRICK_SIZE, BRICK_NUM, BRICK_LEN};
|
||||
const DATA_LEN : usize = BRICK_LEN * BRICK_NUM;
|
||||
use crate::renderer::buffers::{BLOCK_SIZE, BLOCK_NUM, BLOCK_LEN};
|
||||
const DATA_LEN : usize = BLOCK_LEN * BLOCK_NUM;
|
||||
|
||||
|
||||
pub struct BrickBuffer {
|
||||
pub bricks: Box<[u8]>,
|
||||
pub free_brick: usize,
|
||||
pub struct Blocks {
|
||||
pub blocks: Box<[u8]>,
|
||||
pub free_block: usize,
|
||||
texture: wgpu::Texture,
|
||||
pub bind_layout: wgpu::BindGroupLayout,
|
||||
pub bind_group: wgpu::BindGroup
|
||||
}
|
||||
|
||||
impl BrickBuffer {
|
||||
impl Blocks {
|
||||
|
||||
/*
|
||||
* Create buffer in memory and gpu
|
||||
@@ -20,7 +20,7 @@ impl BrickBuffer {
|
||||
pub fn new(device: &wgpu::Device) -> Self {
|
||||
|
||||
// Create texture
|
||||
let size = wgpu::Extent3d { width: BRICK_SIZE as u32, height: BRICK_SIZE as u32, depth: (BRICK_SIZE*BRICK_NUM) as u32 };
|
||||
let size = wgpu::Extent3d { width: BLOCK_SIZE as u32, height: BLOCK_SIZE as u32, depth: (BLOCK_SIZE*BLOCK_NUM) as u32 };
|
||||
let texture = device.create_texture(
|
||||
&wgpu::TextureDescriptor {
|
||||
size: size,
|
||||
@@ -29,7 +29,7 @@ impl BrickBuffer {
|
||||
dimension: wgpu::TextureDimension::D3,
|
||||
format: wgpu::TextureFormat::R8Uint,
|
||||
usage: wgpu::TextureUsage::SAMPLED | wgpu::TextureUsage::COPY_DST, // STORAGE?
|
||||
label: Some("Brick texture"),
|
||||
label: Some("block texture"),
|
||||
}
|
||||
);
|
||||
|
||||
@@ -68,49 +68,50 @@ impl BrickBuffer {
|
||||
wgpu::BindGroupEntry { binding: 0, resource: wgpu::BindingResource::TextureView(&view) },
|
||||
wgpu::BindGroupEntry { binding: 1, resource: wgpu::BindingResource::Sampler(&sampler) }
|
||||
],
|
||||
label: Some("Brick texture group"),
|
||||
label: Some("block texture group"),
|
||||
}
|
||||
);
|
||||
|
||||
// Data
|
||||
let mut bricks = vec![0_u8; DATA_LEN].into_boxed_slice();
|
||||
bricks[0] = 1;
|
||||
println!("Brick buffer size: {} MB", DATA_LEN as f32 / 1024.0 / 1024.0);
|
||||
let mut blocks = vec![0_u8; DATA_LEN].into_boxed_slice();
|
||||
blocks[0] = 1;
|
||||
println!("block buffer size: {} MB", DATA_LEN as f32 / 1024.0 / 1024.0);
|
||||
|
||||
// Done
|
||||
Self { bricks, texture, bind_layout, bind_group, free_brick: 0 }
|
||||
Self { blocks, texture, bind_layout, bind_group, free_block: 0 }
|
||||
}
|
||||
|
||||
/*
|
||||
Write brick data to gpu, should be called to apply changes
|
||||
offset: Brick index to copy
|
||||
Write block data to gpu, should be called to apply changes
|
||||
offset: block index to copy
|
||||
*/
|
||||
pub fn write(&mut self, queue: &wgpu::Queue, brick: usize) {
|
||||
// Get first brick
|
||||
let brick_offset = brick*BRICK_LEN;
|
||||
let brick_bytes : [u8;BRICK_LEN] = self.bricks[brick_offset..brick_offset+BRICK_LEN].try_into().unwrap();
|
||||
let brick_size = wgpu::Extent3d{ width: BRICK_SIZE as u32, height: BRICK_SIZE as u32, depth: BRICK_SIZE as u32 };
|
||||
let brick_origin = wgpu::Origin3d{ x:0, y:0, z: (brick*BRICK_SIZE) as u32 };
|
||||
pub fn write(&mut self, queue: &wgpu::Queue, block: usize) {
|
||||
// Get first block
|
||||
let block_offset = block*BLOCK_LEN;
|
||||
let block_bytes : [u8;BLOCK_LEN] = self.blocks[block_offset..block_offset+BLOCK_LEN].try_into().unwrap();
|
||||
let block_size = wgpu::Extent3d{ width: BLOCK_SIZE as u32, height: BLOCK_SIZE as u32, depth: BLOCK_SIZE as u32 };
|
||||
let block_origin = wgpu::Origin3d{ x:0, y:0, z: (block*BLOCK_SIZE) as u32 };
|
||||
|
||||
// Write
|
||||
queue.write_texture(
|
||||
wgpu::TextureCopyView { texture: &self.texture, mip_level: 0, origin: brick_origin },
|
||||
&brick_bytes,
|
||||
wgpu::TextureDataLayout { offset: 0, bytes_per_row: brick_size.width, rows_per_image: brick_size.height },
|
||||
brick_size
|
||||
wgpu::TextureCopyView { texture: &self.texture, mip_level: 0, origin: block_origin },
|
||||
&block_bytes,
|
||||
wgpu::TextureDataLayout { offset: 0, bytes_per_row: block_size.width, rows_per_image: block_size.height },
|
||||
block_size
|
||||
);
|
||||
}
|
||||
|
||||
/*
|
||||
* Get id of next free brick
|
||||
* Later: reusing old bricks
|
||||
* Get id of next free block
|
||||
* Later: reusing old blocks
|
||||
*/
|
||||
pub fn next_brick(&mut self) -> usize {
|
||||
if self.free_brick >= self.bricks.len() {
|
||||
panic!("No more free bricks! we should consider reusing bricks...");
|
||||
pub fn next_block(&mut self, consume: bool) -> usize {
|
||||
if self.free_block >= self.blocks.len() {
|
||||
panic!("No more free blocks! we should consider reusing blocks...");
|
||||
}
|
||||
|
||||
self.free_brick += 1;
|
||||
self.free_brick
|
||||
let next = self.free_block + 1;
|
||||
if consume { self.free_block = next; }
|
||||
next
|
||||
}
|
||||
}
|
||||
@@ -1,37 +1,38 @@
|
||||
use wgpu::util::DeviceExt;
|
||||
|
||||
use cgmath::Point3;
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Default, Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
|
||||
struct Material {
|
||||
pub albedo: [f32; 4],
|
||||
pub albedo: [f32; 3],
|
||||
pub specular: f32,
|
||||
}
|
||||
|
||||
impl Material {
|
||||
pub fn set_albedo(&mut self, r: f32, g: f32, b:f32)
|
||||
pub fn set(&mut self, color: Point3<f32>, specular: f32)
|
||||
{
|
||||
self.albedo[0] = r;
|
||||
self.albedo[1] = g;
|
||||
self.albedo[2] = b;
|
||||
self.albedo = color.into();
|
||||
self.specular = specular;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub struct MaterialBuffer {
|
||||
pub struct Materials {
|
||||
#[allow(dead_code)]
|
||||
materials: [Material; 256],
|
||||
pub buffer: wgpu::Buffer,
|
||||
pub bind_layout: wgpu::BindGroupLayout,
|
||||
pub bind_group: wgpu::BindGroup
|
||||
}
|
||||
|
||||
impl MaterialBuffer {
|
||||
impl Materials {
|
||||
|
||||
pub fn new(device: &wgpu::Device) -> Self {
|
||||
// Create values
|
||||
let mut materials: [Material; 256] = [Default::default(); 256];
|
||||
materials[0].set_albedo(0.41,0.33,0.20);
|
||||
materials[1].set_albedo(0.15,0.5,0.05);
|
||||
materials[2].set_albedo(0.8,0.8,0.8);
|
||||
materials[0].set(Point3{x:0.41, y:0.33, z:0.20}, 0.0); // Dirt
|
||||
materials[1].set(Point3{x:0.15, y:0.50, z:0.05}, 0.3); // Grass
|
||||
materials[2].set(Point3{x:0.90, y:0.90, z:0.90}, 1.0); // White
|
||||
|
||||
// Create buffer
|
||||
let buffer = device.create_buffer_init(
|
||||
@@ -70,6 +71,6 @@ impl MaterialBuffer {
|
||||
});
|
||||
|
||||
// Done
|
||||
MaterialBuffer { buffer, materials, bind_layout, bind_group }
|
||||
Materials { buffer, materials, bind_layout, bind_group }
|
||||
}
|
||||
}
|
||||
+15
-15
@@ -4,25 +4,25 @@
|
||||
pub const WORLD_SIZE: usize = 32;
|
||||
pub const NUM_NODES: usize = (WORLD_SIZE*WORLD_SIZE*WORLD_SIZE) as usize;
|
||||
|
||||
pub const BRICK_SIZE : usize = 32; // 32x32x32 brick size
|
||||
pub const BRICK_NUM : usize = 32768; // max bricks in texture
|
||||
pub const BRICK_LEN : usize = BRICK_SIZE*BRICK_SIZE*BRICK_SIZE;
|
||||
pub const BLOCK_SIZE : usize = 32; // 32x32x32 brick size
|
||||
pub const BLOCK_NUM : usize = 32768; // max bricks in texture
|
||||
pub const BLOCK_LEN : usize = BLOCK_SIZE*BLOCK_SIZE*BLOCK_SIZE;
|
||||
|
||||
// Import names
|
||||
|
||||
mod node_buffer;
|
||||
pub use node_buffer::NodeBuffer;
|
||||
mod nodes;
|
||||
pub use nodes::Nodes;
|
||||
|
||||
mod brick_buffer;
|
||||
pub use brick_buffer::{BrickBuffer};
|
||||
mod blocks;
|
||||
pub use blocks::Blocks;
|
||||
|
||||
mod uniform_buffer;
|
||||
pub use uniform_buffer::UniformBuffer;
|
||||
pub use uniform_buffer::UniformValues;
|
||||
mod uniform;
|
||||
pub use uniform::Uniform;
|
||||
pub use uniform::UniformValues;
|
||||
|
||||
mod raster_buffer;
|
||||
pub use raster_buffer::RasterBuffer;
|
||||
pub use raster_buffer::Vertex;
|
||||
mod raster;
|
||||
pub use raster::Raster;
|
||||
pub use raster::Vertex;
|
||||
|
||||
mod material_buffer;
|
||||
pub use material_buffer::MaterialBuffer;
|
||||
mod materials;
|
||||
pub use materials::Materials;
|
||||
@@ -3,7 +3,7 @@ use rand::prelude::*;
|
||||
|
||||
use crate::renderer::buffers::{WORLD_SIZE, NUM_NODES};
|
||||
|
||||
pub struct NodeBuffer {
|
||||
pub struct Nodes {
|
||||
pub nodes: Box<[u32]>,
|
||||
texture: wgpu::Texture,
|
||||
size: wgpu::Extent3d,
|
||||
@@ -11,7 +11,7 @@ pub struct NodeBuffer {
|
||||
pub bind_group: wgpu::BindGroup
|
||||
}
|
||||
|
||||
impl NodeBuffer {
|
||||
impl Nodes {
|
||||
|
||||
pub fn new(device: &wgpu::Device) -> Self {
|
||||
|
||||
@@ -40,13 +40,13 @@ const VERTICES: &[Vertex] = &[
|
||||
pub const INDICES: &[u16] = &[ 0, 1, 3, 3, 1, 2 ];
|
||||
|
||||
|
||||
pub struct RasterBuffer {
|
||||
pub struct Raster {
|
||||
pub vertex_buffer: wgpu::Buffer,
|
||||
pub index_buffer: wgpu::Buffer,
|
||||
pub index_len: u32
|
||||
}
|
||||
|
||||
impl RasterBuffer {
|
||||
impl Raster {
|
||||
pub fn new(device: &wgpu::Device) -> Self {
|
||||
|
||||
// Create vertex buffer
|
||||
@@ -32,14 +32,14 @@ impl UniformValues {
|
||||
}
|
||||
|
||||
|
||||
pub struct UniformBuffer {
|
||||
pub struct Uniform {
|
||||
pub values: UniformValues,
|
||||
pub buffer: wgpu::Buffer,
|
||||
pub bind_layout: wgpu::BindGroupLayout,
|
||||
pub bind_group: wgpu::BindGroup,
|
||||
}
|
||||
|
||||
impl UniformBuffer {
|
||||
impl Uniform {
|
||||
pub fn new(device: &wgpu::Device) -> Self {
|
||||
// Create values
|
||||
let values = UniformValues::new();
|
||||
@@ -81,7 +81,7 @@ impl UniformBuffer {
|
||||
});
|
||||
|
||||
// Done
|
||||
UniformBuffer { buffer, values, bind_layout, bind_group }
|
||||
Uniform { buffer, values, bind_layout, bind_group }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user