From 66afd3057e787b2b2493177f4021cf81c94be509 Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Mon, 6 May 2024 16:53:40 +0200 Subject: [PATCH] restructure stuff and fix some warnings --- kubi-shared/src/worldgen/steps/_04_layers.rs | 1 - kubi/src/chat.rs | 2 +- kubi/src/client_physics.rs | 4 +- kubi/src/events.rs | 24 ++++---- kubi/src/lib.rs | 32 ++++------ kubi/src/rendering.rs | 61 ++++++-------------- kubi/src/rendering/camera.rs | 2 +- kubi/src/rendering/depth.rs | 15 +++-- 8 files changed, 56 insertions(+), 85 deletions(-) diff --git a/kubi-shared/src/worldgen/steps/_04_layers.rs b/kubi-shared/src/worldgen/steps/_04_layers.rs index f6d873d..52204b9 100644 --- a/kubi-shared/src/worldgen/steps/_04_layers.rs +++ b/kubi-shared/src/worldgen/steps/_04_layers.rs @@ -1,4 +1,3 @@ -use fastnoise_lite::FastNoiseLite; use glam::ivec3; use crate::{block::Block, chunk::CHUNK_SIZE, worldgen::SeedThingy}; use super::{ diff --git a/kubi/src/chat.rs b/kubi/src/chat.rs index 24b1949..90f4ee1 100644 --- a/kubi/src/chat.rs +++ b/kubi/src/chat.rs @@ -1,5 +1,5 @@ use kubi_shared::networking::client::ClientId; -use shipyard::{AllStoragesView, Unique, UniqueViewMut}; +use shipyard::{AllStoragesView, Unique}; pub enum ChatMessage { PlayerMessage { diff --git a/kubi/src/client_physics.rs b/kubi/src/client_physics.rs index ac79175..f6ef9e6 100644 --- a/kubi/src/client_physics.rs +++ b/kubi/src/client_physics.rs @@ -1,7 +1,7 @@ //TODO client-side physics //TODO move this to shared -use glam::{vec3, IVec3, Mat4, Vec3, Vec3Swizzles}; -use shipyard::{track, AllStoragesView, Component, IntoIter, Unique, UniqueView, View, ViewMut}; +use glam::{vec3, Mat4, Vec3, Vec3Swizzles}; +use shipyard::{track, AllStoragesView, Component, IntoIter, Unique, UniqueView, ViewMut}; use kubi_shared::{block::{Block, CollisionType}, transform::Transform}; use crate::{delta_time::DeltaTime, world::ChunkStorage}; diff --git a/kubi/src/events.rs b/kubi/src/events.rs index 84bad45..fcd4c83 100644 --- a/kubi/src/events.rs +++ b/kubi/src/events.rs @@ -86,18 +86,18 @@ pub fn process_winit_events(world: &mut World, event: &Event<()>) { } } -pub fn initial_resize_event( - mut storages: AllStoragesViewMut, -) { - let (w, h) = { - let renderer = storages.borrow::>>().unwrap(); - (renderer.size().width, renderer.size().height) - }; - storages.add_entity(( - EventComponent, - WindowResizedEvent(UVec2::new(w, h)) - )); -} +// pub fn initial_resize_event( +// mut storages: AllStoragesViewMut, +// ) { +// let (w, h) = { +// let renderer = storages.borrow::>>().unwrap(); +// (renderer.size().width, renderer.size().height) +// }; +// storages.add_entity(( +// EventComponent, +// WindowResizedEvent(UVec2::new(w, h)) +// )); +// } pub fn clear_events( mut all_storages: AllStoragesViewMut, diff --git a/kubi/src/lib.rs b/kubi/src/lib.rs index 2bb336d..b59a073 100644 --- a/kubi/src/lib.rs +++ b/kubi/src/lib.rs @@ -13,7 +13,7 @@ use shipyard::{ World, Workload, IntoWorkload, UniqueView, UniqueViewMut, - NonSendSync, WorkloadModificator, + WorkloadModificator, SystemModificator }; use winit::{ @@ -58,29 +58,19 @@ pub(crate) mod chat; use world::{ init_game_world, - loading::update_loaded_world_around_player, + loading::update_loaded_world_around_player, raycast::update_raycasts, - queue::apply_queued_blocks, + queue::apply_queued_blocks, tasks::ChunkTaskManager, }; use player::{spawn_player, MainPlayer}; use prefabs::load_prefabs; use settings::{load_settings, GameSettings}; use camera::compute_cameras; -use events::{ - clear_events, - process_winit_events, - initial_resize_event, - player_actions::generate_move_events, -}; +use events::{clear_events, process_winit_events, player_actions::generate_move_events}; use input::{init_input, process_inputs}; use player_controller::{debug_switch_ctl_type, update_player_controllers}; -use rendering::{ - init_render_states, - render_master, - resize_renderer, - BackgroundColor, Renderer, -}; +use rendering::{BackgroundColor, Renderer, init_rendering, render_master, update_rendering_early, update_rendering_late}; use block_placement::update_block_placement; use delta_time::{DeltaTime, init_delta_time}; use cursor_lock::{debug_toggle_lock, insert_lock_state, lock_cursor_now, update_cursor_lock_state}; @@ -110,10 +100,9 @@ fn pre_startup() -> Workload { fn startup() -> Workload { ( init_fixed_timestamp_storage, - initial_resize_event, kubi_ui_init, load_prefabs, - init_render_states, + init_rendering, insert_lock_state, init_state, initialize_from_args, @@ -129,8 +118,8 @@ fn startup() -> Workload { fn update() -> Workload { ( + update_rendering_early, debug_toggle_lock, - resize_renderer, update_cursor_lock_state, process_inputs, kubi_ui_begin, @@ -169,6 +158,7 @@ fn update() -> Workload { update_state, exit_on_esc, disconnect_on_exit.run_if(is_multiplayer), + update_rendering_late, ).into_sequential_workload() } @@ -187,7 +177,7 @@ fn update() -> Workload { // ).into_sequential_workload() // } -fn after_frame_end() -> Workload { +fn after_render() -> Workload { ( clear_events, ).into_sequential_workload() @@ -236,7 +226,7 @@ pub fn kubi_main( world.add_workload(startup); world.add_workload(update); //world.add_workload(render); - world.add_workload(after_frame_end); + world.add_workload(after_render); //Save _visualizer.json #[cfg(feature = "generate_visualizer_data")] @@ -333,7 +323,7 @@ pub fn kubi_main( // target.0.finish().unwrap(); //After frame end - world.run_workload(after_frame_end).unwrap(); + world.run_workload(after_render).unwrap(); //Process control flow changes if world.borrow::>().unwrap().0 { diff --git a/kubi/src/rendering.rs b/kubi/src/rendering.rs index e34f7b0..a7caa31 100644 --- a/kubi/src/rendering.rs +++ b/kubi/src/rendering.rs @@ -1,53 +1,17 @@ -use shipyard::{AllStoragesView, AllStoragesViewMut, IntoIter, IntoWorkload, Unique, UniqueView, UniqueViewMut, View, Workload}; +use shipyard::{AllStoragesViewMut, IntoIter, IntoWorkload, SystemModificator, Unique, UniqueView, UniqueViewMut, View, Workload, WorkloadModificator}; use winit::dpi::PhysicalSize; -use glam::{mat4, vec4, Mat4, UVec2, Vec3}; +use glam::Vec3; use crate::{events::WindowResizedEvent, state::is_ingame}; mod renderer; pub use renderer::Renderer; -use self::camera::update_camera_unform_buffer; +use self::{camera::CameraUniformBuffer, world::WorldRenderState}; pub mod world; pub mod camera; pub mod depth; -//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 for TrasnformUniformData { -// fn from(mat: Mat4) -> Self { -// Self { -// transform: mat.to_cols_array_2d(), -// } -// } -// } - -// impl From for TrasnformUniformData { -// fn from(value: Transform) -> Self { -// value.0.into() -// } -// } - pub struct BufferPair { pub index: wgpu::Buffer, pub index_len: u32, @@ -64,10 +28,24 @@ pub struct RenderCtx<'a> { pub surface_view: &'a wgpu::TextureView, } -pub fn init_render_states() -> Workload { +pub fn init_rendering() -> Workload { ( + depth::init_depth_texture, camera::init_camera_uniform_buffer, - world::init_world_render_state, + world::init_world_render_state, //TODO run only once ingame + ).into_sequential_workload() +} + +pub fn update_rendering_early() -> Workload { + ( + resize_renderer, + depth::resize_depth_texture, + ).into_sequential_workload() +} + +pub fn update_rendering_late() -> Workload { + ( + camera::update_camera_uniform_buffer, ).into_workload() } @@ -109,7 +87,6 @@ pub fn render_master(storages: AllStoragesViewMut) { }; if storages.run(is_ingame) { - //TODO init world render state on demand storages.run_with_data(world::draw_world, &mut data); } diff --git a/kubi/src/rendering/camera.rs b/kubi/src/rendering/camera.rs index 918f224..8cd815b 100644 --- a/kubi/src/rendering/camera.rs +++ b/kubi/src/rendering/camera.rs @@ -72,7 +72,7 @@ pub fn init_camera_uniform_buffer(storages: AllStoragesView) { storages.add_unique(CameraUniformBuffer::init_default(&renderer)); } -pub fn update_camera_unform_buffer( +pub fn update_camera_uniform_buffer( renderer: UniqueView, camera_uniform_buffer: UniqueView, camera: View, diff --git a/kubi/src/rendering/depth.rs b/kubi/src/rendering/depth.rs index 6c0f840..830b10c 100644 --- a/kubi/src/rendering/depth.rs +++ b/kubi/src/rendering/depth.rs @@ -1,7 +1,9 @@ use glam::{uvec2, UVec2}; +use shipyard::{AllStoragesView, Unique, UniqueView, UniqueViewMut}; use super::Renderer; +#[derive(Unique)] pub struct DepthTexture { pub depth_texture: wgpu::Texture, pub depth_view: wgpu::TextureView, @@ -55,13 +57,16 @@ impl DepthTexture { } } -pub fn init_depth_texture(renderer: &Renderer) -> DepthTexture { - DepthTexture::init(renderer) +pub fn init_depth_texture( + storages: AllStoragesView, +) { + let renderer = storages.borrow::>().unwrap(); + storages.add_unique(DepthTexture::init(&renderer)); } pub fn resize_depth_texture( - renderer: &Renderer, - depth_texture: &mut DepthTexture, + mut depth_texture: UniqueViewMut, + renderer: UniqueView, ) { - depth_texture.resize(renderer); + depth_texture.resize(&renderer); }