Skip to content

Commit

Permalink
refactor uniform creation
Browse files Browse the repository at this point in the history
  • Loading branch information
BreakingLead committed May 31, 2024
1 parent 3553643 commit 0cb994f
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 63 deletions.
8 changes: 5 additions & 3 deletions blockworld-client/game/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ impl Chunk {
}

impl Default for Chunk {
// THIS IS NOT IDEAL
// JUST FOR TEST
// REMEMBER TO DELETE THOSE CODE
// ! THIS IS NOT IDEAL
// ! JUST FOR TEST
// ! REMEMBER TO DELETE THOSE CODE
fn default() -> Self {
let mut blocks = Box::new([Block::default(); CHUNK_BLOCK_NUM]);
for x in 0..CHUNK_SIZE as i32 {
Expand Down Expand Up @@ -143,3 +143,5 @@ impl Default for Chunk {
}
}
}

pub struct ChunkProvider;
1 change: 0 additions & 1 deletion blockworld-client/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ mod debug;
mod game;
mod io;
mod render;
mod util;

#[derive(Parser, Clone, Default)]
#[command(author, version, about)]
Expand Down
4 changes: 2 additions & 2 deletions blockworld-client/render/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ impl Camera {
#[repr(C)]
// This is so we can store this in a buffer
#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
pub struct MatrixUniform {
pub struct MatrixData {
// We can't use cgmath with bytemuck directly, so we'll have
// to convert the Matrix4 into a 4x4 f32 array
matrix: [[f32; 4]; 4],
}

impl MatrixUniform {
impl MatrixData {
pub fn new() -> Self {
Self {
matrix: Mat4::IDENTITY.to_cols_array_2d(),
Expand Down
78 changes: 22 additions & 56 deletions blockworld-client/render/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,12 @@ use crate::{
Game,
},
io::{atlas_helper::AtlasMeta, input_helper::InputState},
render::{
camera::{Camera, MatrixUniform},
texture,
vertex::Vertex,
},
BootArgs,
};

use super::camera::{Camera, MatrixData};
use super::texture::Texture;
use super::uniform::*;
use super::{
pipeline::{RegularPipeline, WireframePipeline},
render_chunk::RenderChunk,
Expand All @@ -48,22 +46,19 @@ pub struct State<'a> {
pub queue: wgpu::Queue,
pub config: wgpu::SurfaceConfiguration,
pub size: winit::dpi::PhysicalSize<u32>,

pub main_pipeline: RegularPipeline,
pub wireframe_pipeline: WireframePipeline,

pub render_chunk: RenderChunk,

pub texture: texture::Texture,
pub texture: Texture,
pub texture_bind_group: wgpu::BindGroup,

pub depth_texture: texture::Texture,
pub depth_texture: Texture,

pub camera: Camera,
pub matrix_uniform: MatrixUniform,

/// matrix_buffer represents a gpu buffer
pub matrix_buffer: wgpu::Buffer,
pub matrix_bind_group: wgpu::BindGroup,
pub matrix_uniform: Uniform<MatrixData>,

// IO
pub input_state: InputState,
Expand Down Expand Up @@ -169,38 +164,14 @@ impl<'a> State<'a> {
// Camera thingy
let camera = Camera::new(size.width as f32 / size.height as f32);

let mut matrix_uniform = MatrixUniform::new();
matrix_uniform.update_matrix(&camera);

let matrix_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Martix Buffer"),
contents: bytemuck::cast_slice(&[matrix_uniform]),
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
});

let matrix_bind_group_layout =
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
entries: &[wgpu::BindGroupLayoutEntry {
binding: 30,
visibility: wgpu::ShaderStages::VERTEX,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
}],
label: Some("camera_bind_group_layout"),
});
let mut matrix_uniform = Uniform::new(
&device,
Box::new(MatrixData::new()),
30,
Some("Matrix Uniform"),
);
matrix_uniform.uniform.as_mut().update_matrix(&camera);

let matrix_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &matrix_bind_group_layout,
entries: &[wgpu::BindGroupEntry {
binding: 30,
resource: matrix_buffer.as_entire_binding(),
}],
label: Some("Blockworld Prespective Matrix Bind Group"),
});
// \-------------------

// /-------------------
Expand Down Expand Up @@ -252,7 +223,7 @@ impl<'a> State<'a> {
label: Some("diffuse_bind_group"),
});

let depth_texture = texture::Texture::create_depth_texture(&device, &config);
let depth_texture = Texture::create_depth_texture(&device, &config);
// \-------------------

let shader = device.create_shader_module(include_wgsl!("shaders/default_shader.wgsl"));
Expand All @@ -261,14 +232,14 @@ impl<'a> State<'a> {

let main_pipeline = RegularPipeline::new(
&device,
&[&texture_bind_group_layout, &matrix_bind_group_layout],
&[&texture_bind_group_layout, &matrix_uniform.layout],
&shader,
&config,
);

let wireframe_pipeline = WireframePipeline::new(
&device,
&[&texture_bind_group_layout, &matrix_bind_group_layout],
&[&texture_bind_group_layout, &matrix_uniform.layout],
&wireframe_shader,
&config,
);
Expand Down Expand Up @@ -356,9 +327,7 @@ impl<'a> State<'a> {
render_chunk,

camera,
matrix_buffer,
matrix_uniform,
matrix_bind_group,

brush,
settings,
Expand Down Expand Up @@ -388,7 +357,7 @@ impl<'a> State<'a> {
self.config.height = new_size.height;

self.surface.configure(&self.device, &self.config);
self.depth_texture = texture::Texture::create_depth_texture(&self.device, &self.config);
self.depth_texture = Texture::create_depth_texture(&self.device, &self.config);
}
}

Expand Down Expand Up @@ -425,11 +394,11 @@ impl<'a> State<'a> {
// Camera Update
self.camera.update(&self.game.player_state);

self.matrix_uniform.update_matrix(&self.camera);
self.matrix_uniform.uniform.update_matrix(&self.camera);
self.queue.write_buffer(
&self.matrix_buffer,
&self.matrix_uniform.buffer,
0,
bytemuck::cast_slice(&[self.matrix_uniform]),
bytemuck::cast_slice(&[*self.matrix_uniform.uniform]),
);
}

Expand Down Expand Up @@ -487,7 +456,7 @@ impl<'a> State<'a> {
}

render_pass.set_bind_group(0, &self.texture_bind_group, &[]);
render_pass.set_bind_group(1, &self.matrix_bind_group, &[]);
render_pass.set_bind_group(1, &self.matrix_uniform.bind_group, &[]);

render_pass.set_vertex_buffer(0, self.render_chunk.vertex_buffer.slice(..));
render_pass.draw(0..self.render_chunk.vertex_count, 0..1);
Expand Down Expand Up @@ -527,9 +496,6 @@ impl<'a> Debug for State<'a> {
.field("texture_bind_group", &self.texture_bind_group)
.field("depth_texture", &self.depth_texture)
.field("camera", &self.camera)
.field("matrix_uniform", &self.matrix_uniform)
.field("matrix_buffer", &self.matrix_buffer)
.field("matrix_bind_group", &self.matrix_bind_group)
.field("input_state", &self.input_state)
.field("game", &self.game)
.field("fps", &self.fps)
Expand Down
3 changes: 2 additions & 1 deletion blockworld-client/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub mod render_block;
pub mod render_chunk;
pub mod render_chunk_pool;
pub mod texture;
pub mod uniform_utils;
pub mod uniform;
pub mod utils;
pub mod vertex;
pub mod window_init;
Empty file.

0 comments on commit 0cb994f

Please sign in to comment.