i see... something?

This commit is contained in:
griffi-gh 2024-05-06 13:32:44 +02:00
parent 0440f207cc
commit c071986f2a
13 changed files with 439 additions and 229 deletions

View file

@ -9,7 +9,7 @@ pub fn init() {
use env_logger::{fmt::Color, Builder, Env};
let env = Env::default()
.filter_or("RUST_LOG", "trace,gilrs=warn,rusty_xinput=warn,wgpu=warn,wgpu_core=warn,wgpu_hal=warn,hui=info,hui-winit=info,hui-glium=info,hui-wgpu=info");
.filter_or("RUST_LOG", "trace,gilrs=warn,rusty_xinput=warn,wgpu=warn,wgpu_core=warn,wgpu_hal=warn,hui=info,hui-winit=info,hui-glium=info,hui-wgpu=info,naga=warn");
Builder::from_env(env)
.format(|buf, record| {
let mut level_style = buf.style();

View file

@ -1,9 +1,9 @@
// struct Uniforms {
// transform: mat4x4<f32>;
// };
struct CameraUniform {
view_proj: mat4x4<f32>,
};
// @group(1) @binding(0)
// var<uniform> uniforms: Uniforms;
@group(1) @binding(0)
var<uniform> camera: CameraUniform;
struct VertexInput {
@location(0) position: vec3<f32>,
@ -26,7 +26,7 @@ fn vs_main(
) -> VertexOutput {
var out: VertexOutput;
out.uv = in.uv;
out.clip_position = vec4<f32>(in.position, 1.0);
out.clip_position = camera.view_proj * vec4<f32>(in.position, 1.0);
return out;
}

View file

@ -79,8 +79,11 @@ use player_controller::{debug_switch_ctl_type, update_player_controllers};
// clear_background, entities::render_entities, init_window_size, primitives::init_primitives, resize_renderer, selection_box::render_selection_box, sumberge::render_submerged_view, update_window_size, world::{draw_current_chunk_border, draw_world, draw_world_trans, init_trans_chunk_queue}, BackgroundColor, RenderTarget, Renderer
// };
use rendering::{
init_window_size, render_master, resize_renderer, update_window_size,
world::{init_trans_chunk_queue, TransChunkQueue},
init_render_states,
init_window_size,
render_master,
resize_renderer,
update_window_size,
BackgroundColor, Renderer,
};
use block_placement::update_block_placement;
@ -116,7 +119,7 @@ fn startup() -> Workload {
init_window_size,
kubi_ui_init,
load_prefabs,
//init_primitives,
init_render_states,
insert_lock_state,
init_state,
initialize_from_args,
@ -139,7 +142,6 @@ fn update() -> Workload {
process_inputs,
kubi_ui_begin,
(
init_trans_chunk_queue.run_if_missing_unique::<TransChunkQueue>(),
init_game_world.run_if_missing_unique::<ChunkTaskManager>(),
(
spawn_player.run_if_storage_empty::<MainPlayer>(),

View file

@ -38,6 +38,7 @@ impl AssetPaths for BlockTexture {
#[derive(Unique)]
pub struct TexturePrefabs {
pub block_diffuse_texture: wgpu::Texture,
pub block_diffuse_bind_group_layout: wgpu::BindGroupLayout,
pub block_diffuse_bind_group: wgpu::BindGroup,
}
@ -107,6 +108,7 @@ pub fn load_prefabs(
});
storages.add_unique_non_send_sync(TexturePrefabs {
block_diffuse_texture,
block_diffuse_bind_group_layout,
block_diffuse_bind_group,
});

View file

@ -1,16 +1,50 @@
use shipyard::{AllStoragesView, AllStoragesViewMut, IntoIter, Unique, UniqueView, UniqueViewMut, View};
use shipyard::{AllStoragesView, AllStoragesViewMut, IntoIter, IntoWorkload, Unique, UniqueView, UniqueViewMut, View, Workload};
use winit::dpi::PhysicalSize;
use glam::{Vec3, UVec2};
use glam::{mat4, vec4, Mat4, UVec2, Vec3};
use crate::{events::WindowResizedEvent, state::is_ingame};
mod renderer;
pub use renderer::Renderer;
pub mod primitives;
use self::camera::update_camera_unform_buffer;
pub mod world;
pub mod selection_box;
pub mod entities;
pub mod sumberge;
pub mod camera;
//pub mod primitives;
//pub mod selection_box;
//pub mod entities;
//pub mod sumberge;
pub const WGPU_COORDINATE_SYSTEM: Mat4 = mat4(
vec4(1.0, 0.0, 0.0, 0.0),
vec4(0.0, 1.0, 0.0, 0.0),
vec4(0.0, 0.0, 0.5, 0.5),
vec4(0.0, 0.0, 0.0, 1.0),
);
// #[repr(C)]
// #[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
// struct TrasnformUniformData {
// pub transform: [[f32; 4]; 4],
// }
// impl TrasnformUniformData {
// pub const LAYOUT: &wgpu::Layou
// }
// impl From<Mat4> for TrasnformUniformData {
// fn from(mat: Mat4) -> Self {
// Self {
// transform: mat.to_cols_array_2d(),
// }
// }
// }
// impl From<Transform> for TrasnformUniformData {
// fn from(value: Transform) -> Self {
// value.0.into()
// }
// }
pub struct BufferPair {
pub index: wgpu::Buffer,
@ -28,6 +62,13 @@ pub struct RenderCtx<'a> {
pub surface_view: &'a wgpu::TextureView,
}
pub fn init_render_states() -> Workload {
(
camera::init_camera_uniform_buffer,
world::init_world_render_state,
).into_workload()
}
pub fn render_master(storages: AllStoragesViewMut) {
let renderer = storages.borrow::<UniqueView<Renderer>>().unwrap();
@ -66,6 +107,10 @@ pub fn render_master(storages: AllStoragesViewMut) {
};
if storages.run(is_ingame) {
//XXX: probably should be in pre_update or sth
storages.run(update_camera_unform_buffer);
//TODO init world render state on demand
storages.run_with_data(world::draw_world, &mut data);
}

View file

@ -0,0 +1,84 @@
use bytemuck::{Pod, Zeroable};
use kubi_shared::transform::Transform;
use shipyard::{AllStoragesView, IntoIter, Unique, UniqueView, View};
use wgpu::util::DeviceExt;
use crate::camera::{self, Camera};
use super::{Renderer, WGPU_COORDINATE_SYSTEM};
#[derive(Debug, Clone, Copy, Default, Pod, Zeroable)]
#[repr(C, packed)]
pub struct CameraUniformData {
pub view_proj: [[f32; 4]; 4],
}
//TODO if multiple cameras, buffer per camera
#[derive(Unique)]
pub struct CameraUniformBuffer {
pub camera_uniform_buffer: wgpu::Buffer,
pub camera_bind_group_layout: wgpu::BindGroupLayout,
pub camera_bind_group: wgpu::BindGroup,
}
impl CameraUniformBuffer {
pub fn init(renderer: &Renderer, data: CameraUniformData) -> Self {
let camera_uniform_buffer = renderer.device().create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("camera_uniform_buffer"),
contents: bytemuck::cast_slice(&[data]),
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
});
let camera_bind_group_layout = renderer.device().create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("camera_bind_group_layout"),
entries: &[
wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::VERTEX,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
},
],
});
let camera_bind_group = renderer.device().create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("camera_bind_group"),
layout: &camera_bind_group_layout,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: camera_uniform_buffer.as_entire_binding(),
},
],
});
Self { camera_uniform_buffer, camera_bind_group_layout, camera_bind_group }
}
pub fn init_default(renderer: &Renderer) -> Self {
Self::init(renderer, CameraUniformData::default())
}
pub fn update(&self, renderer: &Renderer, data: CameraUniformData) {
renderer.queue().write_buffer(&self.camera_uniform_buffer, 0, bytemuck::cast_slice(&[data]));
}
}
pub fn init_camera_uniform_buffer(storages: AllStoragesView) {
let renderer = storages.borrow::<UniqueView<Renderer>>().unwrap();
storages.add_unique(CameraUniformBuffer::init_default(&renderer));
}
pub fn update_camera_unform_buffer(
renderer: UniqueView<Renderer>,
camera_uniform_buffer: UniqueView<CameraUniformBuffer>,
camera: View<Camera>,
) {
let Some(camera) = camera.iter().next() else { return };
let proj = camera.view_matrix * camera.perspective_matrix * WGPU_COORDINATE_SYSTEM;
camera_uniform_buffer.update(&renderer, CameraUniformData { view_proj: proj.to_cols_array_2d() });
}

View file

@ -1,57 +1,44 @@
use bytemuck::{Pod, Zeroable};
use glam::{IVec3, Vec3};
use shipyard::{AllStoragesView, IntoIter, NonSendSync, Unique, UniqueView, UniqueViewMut, View};
use kubi_shared::{chunk::CHUNK_SIZE, transform::Transform};
use wgpu::util::RenderEncoder;
use crate::{
camera::Camera,
prefabs::TexturePrefabs,
settings::GameSettings,
world::{ChunkMeshStorage, ChunkStorage},
};
use super::{RenderCtx, Renderer};
use super::{camera::CameraUniformBuffer, RenderCtx, WGPU_COORDINATE_SYSTEM};
#[derive(Clone, Copy, Pod, Zeroable)]
#[repr(C, packed)]
pub struct ChunkVertex {
pub position: [f32; 3],
pub normal: [f32; 3],
pub uv: [f32; 2],
pub tex_index: u8,
}
impl ChunkVertex {
pub const LAYOUT: wgpu::VertexBufferLayout<'static> = wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<ChunkVertex>() as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &wgpu::vertex_attr_array![
0 => Float32x3,
1 => Float32x3,
2 => Float32x2,
3 => Uint32,
],
};
}
mod pipeline;
mod vertex;
pub use vertex::ChunkVertex;
#[derive(Unique)]
pub struct TransChunkQueue(pub Vec<IVec3>);
pub struct WorldRenderState {
pub pipeline: wgpu::RenderPipeline,
pub trans_chunk_queue: Vec<IVec3>,
}
pub fn init_trans_chunk_queue(storages: AllStoragesView) {
storages.add_unique(TransChunkQueue(Vec::with_capacity(512)));
pub fn init_world_render_state(storages: AllStoragesView) {
storages.add_unique(WorldRenderState {
pipeline: storages.run(pipeline::init_world_pipeline),
trans_chunk_queue: Vec::with_capacity(512),
})
}
pub fn draw_world(
ctx: &mut RenderCtx,
mut state: UniqueViewMut<WorldRenderState>,
textures: UniqueView<TexturePrefabs>,
chunks: UniqueView<ChunkStorage>,
meshes: NonSendSync<UniqueView<ChunkMeshStorage>>,
transform: View<Transform>,
//transform: View<Transform>,
camera: View<Camera>,
camera_ubo: UniqueView<CameraUniformBuffer>,
settings: UniqueView<GameSettings>,
mut trans_queue: UniqueViewMut<TransChunkQueue>,
) {
let camera = camera.iter().next().expect("No cameras in the scene");
let camera_matrix = camera.view_matrix * camera.perspective_matrix;
let matrix = WGPU_COORDINATE_SYSTEM * camera.view_matrix * camera.perspective_matrix;
let mut render_pass = ctx.encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("draw_world"),
@ -66,6 +53,10 @@ pub fn draw_world(
..Default::default()
});
render_pass.set_pipeline(&state.pipeline);
render_pass.set_bind_group(0, &textures.block_diffuse_bind_group, &[]);
render_pass.set_bind_group(1, &camera_ubo.camera_bind_group, &[]);
for (&position, chunk) in &chunks.chunks {
if let Some(key) = chunk.mesh_index {
let mesh = meshes.get(key).expect("Mesh index pointing to nothing");
@ -85,10 +76,8 @@ pub fn draw_world(
//Draw chunk mesh
if mesh.main.index.size() > 0 {
//TODO
render_pass.set_index_buffer(mesh.main.index.slice(..), wgpu::IndexFormat::Uint32);
render_pass.set_vertex_buffer(0, mesh.main.vertex.slice(..));
render_pass.set_bind_group(0, &textures.block_diffuse_bind_group, &[]);
render_pass.draw_indexed(0..mesh.main.index_len, 0, 0..1);
}

View file

@ -0,0 +1,59 @@
use shipyard::{Unique, UniqueView};
use crate::{
prefabs::TexturePrefabs,
rendering::{camera::CameraUniformBuffer, world::ChunkVertex, Renderer}
};
pub fn init_world_pipeline(
ren: UniqueView<Renderer>,
textures: UniqueView<TexturePrefabs>,
camera_ubo: UniqueView<CameraUniformBuffer>,
) -> wgpu::RenderPipeline {
let shader = ren.device().create_shader_module(
wgpu::include_wgsl!("../../../shaders/world.wgsl")
);
let world_pipeline_layout = ren.device().create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("world_pipeline_layout"),
bind_group_layouts: &[
&textures.block_diffuse_bind_group_layout,
&camera_ubo.camera_bind_group_layout,
],
push_constant_ranges: &[],
});
ren.device().create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("world_pipeline"),
layout: Some(&world_pipeline_layout),
fragment: Some(wgpu::FragmentState {
module: &shader,
entry_point: "fs_main",
compilation_options: wgpu::PipelineCompilationOptions::default(),
targets: &[Some(wgpu::ColorTargetState {
format: ren.surface_config().format,
blend: Some(wgpu::BlendState::REPLACE),
write_mask: wgpu::ColorWrites::ALL,
})],
}),
vertex: wgpu::VertexState {
module: &shader,
entry_point: "vs_main",
compilation_options: wgpu::PipelineCompilationOptions::default(),
buffers: &[
ChunkVertex::LAYOUT,
],
},
primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::TriangleList,
strip_index_format: None,
cull_mode: Some(wgpu::Face::Back),
front_face: wgpu::FrontFace::Cw,
unclipped_depth: false,
polygon_mode: wgpu::PolygonMode::Fill,
conservative: false,
},
depth_stencil: None,
multisample: wgpu::MultisampleState::default(),
multiview: None,
})
}

View file

@ -0,0 +1,23 @@
use bytemuck::{Pod, Zeroable};
#[derive(Clone, Copy, Pod, Zeroable)]
#[repr(C, packed)]
pub struct ChunkVertex {
pub position: [f32; 3],
pub normal: [f32; 3],
pub uv: [f32; 2],
pub tex_index: u32,
}
impl ChunkVertex {
pub const LAYOUT: wgpu::VertexBufferLayout<'static> = wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<ChunkVertex>() as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &wgpu::vertex_attr_array![
0 => Float32x3,
1 => Float32x3,
2 => Float32x2,
3 => Uint32,
],
};
}

View file

@ -1,4 +1,4 @@
use glam::{IVec3, ivec3};
use glam::{ivec3, IVec3, Vec3};
use strum::IntoEnumIterator;
use kubi_shared::block::{Block, RenderType, Transparency};
use crate::world::chunk::CHUNK_SIZE;
@ -10,7 +10,7 @@ mod builder;
use data::MeshGenData;
use builder::{MeshBuilder, CubeFace, DiagonalFace};
pub fn generate_mesh(data: MeshGenData) -> (
pub fn generate_mesh(position: IVec3, data: MeshGenData) -> (
(Vec<ChunkVertex>, Vec<u32>),
(Vec<ChunkVertex>, Vec<u32>),
) {
@ -32,7 +32,7 @@ pub fn generate_mesh(data: MeshGenData) -> (
}
};
let mut builder = MeshBuilder::new();
let mut builder = MeshBuilder::new_with_offset((position * CHUNK_SIZE as i32).as_vec3());
let mut trans_builder = MeshBuilder::new();
for x in 0..CHUNK_SIZE as i32 {

View file

@ -1,5 +1,5 @@
use strum::EnumIter;
use glam::{Vec3, vec3, IVec3, ivec3};
use glam::{ivec3, vec3, IVec3, Vec3};
use std::f32::consts::FRAC_1_SQRT_2;
use crate::rendering::world::ChunkVertex;
@ -87,6 +87,7 @@ const UV_COORDS: [[f32; 2]; 4] = [
#[derive(Default)]
pub struct MeshBuilder {
offset: Vec3,
vertex_buffer: Vec<ChunkVertex>,
index_buffer: Vec<u32>,
idx_counter: u32,
@ -96,6 +97,10 @@ impl MeshBuilder {
Self::default()
}
pub fn new_with_offset(offset: Vec3) -> Self {
Self { offset, ..Self::new() }
}
pub fn add_face(&mut self, face: CubeFace, coord: IVec3, texture: u8) {
let coord = coord.as_vec3();
let face_index = face as usize;
@ -106,10 +111,10 @@ impl MeshBuilder {
self.vertex_buffer.reserve(4);
for i in 0..4 {
self.vertex_buffer.push(ChunkVertex {
position: (coord + vert[i]).to_array(),
position: (coord + vert[i] + self.offset).to_array(),
normal: norm.to_array(),
uv: UV_COORDS[i],
tex_index: texture
tex_index: texture as u32
});
}
@ -129,18 +134,18 @@ impl MeshBuilder {
self.vertex_buffer.reserve(8);
for i in 0..4 { //push front vertices
self.vertex_buffer.push(ChunkVertex {
position: (coord.as_vec3() + vertices[i]).to_array(),
position: (coord.as_vec3() + vertices[i] + self.offset).to_array(),
normal: normal_front,
uv: UV_COORDS[i],
tex_index: front_texture
tex_index: front_texture as u32
})
}
for i in 0..4 { //push back vertices
self.vertex_buffer.push(ChunkVertex {
position: (coord.as_vec3() + vertices[i]).to_array(),
position: (coord.as_vec3() + vertices[i] + self.offset).to_array(),
normal: normal_back,
uv: UV_COORDS[i],
tex_index: back_texture
tex_index: back_texture as u32
})
}
@ -151,24 +156,25 @@ impl MeshBuilder {
self.idx_counter += 8;
}
pub fn add_model(&mut self, position: Vec3, vertices: &[ChunkVertex], indices: Option<&[u32]>) {
//push vertices
self.vertex_buffer.extend(vertices.iter().map(|vertex| {
let mut vertex = *vertex;
vertex.position[0] += position.x;
vertex.position[0] += position.y;
vertex.position[0] += position.z;
vertex
}));
//push indices
if let Some(indices) = indices {
self.index_buffer.extend(indices.iter().map(|x| x + self.idx_counter));
} else {
self.index_buffer.extend(0..(self.vertex_buffer.len() as u32));
}
//increment idx counter
self.idx_counter += vertices.len() as u32;
}
//XXX: needs offset supprt
// pub fn add_model(&mut self, position: Vec3, vertices: &[ChunkVertex], indices: Option<&[u32]>) {
// //push vertices
// self.vertex_buffer.extend(vertices.iter().map(|vertex| {
// let mut vertex = *vertex;
// vertex.position[0] += position.x;
// vertex.position[0] += position.y;
// vertex.position[0] += position.z;
// vertex
// }));
// //push indices
// if let Some(indices) = indices {
// self.index_buffer.extend(indices.iter().map(|x| x + self.idx_counter));
// } else {
// self.index_buffer.extend(0..(self.vertex_buffer.len() as u32));
// }
// //increment idx counter
// self.idx_counter += vertices.len() as u32;
// }
pub fn finish(self) -> (Vec<ChunkVertex>, Vec<u32>) {
(self.vertex_buffer, self.index_buffer)

View file

@ -63,7 +63,7 @@ impl ChunkTaskManager {
let (
(vertices, indices),
(trans_vertices, trans_indices),
) = generate_mesh(data);
) = generate_mesh(position, data);
ChunkTaskResponse::GeneratedMesh {
position,
vertices, indices,