This commit is contained in:
griffi-gh 2023-07-15 22:14:08 +02:00
parent 12cb29ca9d
commit 8b16081a68
14 changed files with 174 additions and 303 deletions

View file

@ -3,6 +3,7 @@
## ***wgpu branch*** ## ***wgpu branch***
<b>Highly experimental very early work-in-progress wgpu version of Kubi!</b><br> <b>Highly experimental very early work-in-progress wgpu version of Kubi!</b><br>
(will also include the new gui system)
<hr> <hr>

1
kubi/shaders/world.wgsl Normal file
View file

@ -0,0 +1 @@
//TODO migrate

View file

@ -37,19 +37,7 @@ impl AssetPaths for BlockTexture {
#[derive(Unique)] #[derive(Unique)]
#[repr(transparent)] #[repr(transparent)]
pub struct BlockTexturesPrefab(pub SrgbTexture2dArray); pub struct BlockTexturesPrefab(pub wgpu::Texture);
#[derive(Unique)]
#[repr(transparent)]
pub struct ChunkShaderPrefab(pub Program);
#[derive(Unique)]
#[repr(transparent)]
pub struct ColoredShaderPrefab(pub Program);
#[derive(Unique)]
#[repr(transparent)]
pub struct ProgressbarShaderPrefab(pub Program);
pub fn load_prefabs( pub fn load_prefabs(
storages: AllStoragesView, storages: AllStoragesView,
@ -59,23 +47,4 @@ pub fn load_prefabs(
storages.add_unique_non_send_sync(BlockTexturesPrefab( storages.add_unique_non_send_sync(BlockTexturesPrefab(
load_asset_texture_array::<BlockTexture>("blocks".into(), &renderer) load_asset_texture_array::<BlockTexture>("blocks".into(), &renderer)
)); ));
log::info!("Compiling shaders...");
storages.add_unique_non_send_sync(ChunkShaderPrefab(
include_shader_prefab!(
"world",
"../shaders/world.vert",
"../shaders/world.frag",
&renderer.display
)
));
storages.add_unique_non_send_sync(ColoredShaderPrefab(
include_shader_prefab!(
"colored",
"../shaders/colored.vert",
"../shaders/colored.frag",
&renderer.display
)
));
renderer.display.release_shader_compiler();
} }

View file

@ -1,83 +0,0 @@
use shipyard::{Component, Unique, Workload, IntoWorkload, AllStoragesView, View, UniqueViewMut, IntoIter};
use glam::{Vec4, Mat4};
use crate::{color::color_hex, events::WindowResizedEvent};
pub mod progressbar;
use progressbar::render_progressbars;
//TODO compute gui scale on window resize
#[derive(Unique, Clone, Copy, Debug, Default)]
pub struct GuiView(pub Mat4);
#[derive(Component, Clone, Copy, Debug, Default)]
pub struct GuiComponent;
#[derive(Component, Clone, Copy, Debug)]
pub struct PrimaryColor(pub Vec4);
impl Default for PrimaryColor {
fn default() -> Self {
Self(color_hex(0x156cddff))
}
}
#[derive(Component, Clone, Copy, Debug)]
pub struct SecondaryColor(pub Vec4);
impl Default for SecondaryColor {
fn default() -> Self {
Self(color_hex(0xc9d5e4ff))
}
}
fn update_gui_view(
mut view: UniqueViewMut<GuiView>,
resize: View<WindowResizedEvent>,
) {
let Some(&size) = resize.iter().next() else {
return
};
let [w, h] = size.0.to_array();
view.0 = Mat4::orthographic_rh_gl(0.0, w as f32, h as f32, 0.0, -1.0, 1.0);
}
#[deprecated="will be replaced by an immediate-mode ui soon, currently a no-op"]
#[allow(deprecated)]
pub fn init_gui(
storages: AllStoragesView
) {
storages.add_unique(GuiView::default());
}
#[deprecated="will be replaced by an immediate-mode ui soon, currently a no-op"]
#[allow(deprecated)]
pub fn update_gui() -> Workload {
(
update_gui_view
).into_sequential_workload()
}
#[deprecated="will be replaced by an immediate-mode ui soon, currently a no-op"]
#[allow(deprecated)]
pub fn render_gui() -> Workload {
(
render_progressbars
).into_sequential_workload()
}
// pub fn gui_testing(
// mut storages: AllStoragesViewMut,
// ) {
// storages.add_entity((
// GuiComponent,
// Transform2d(Mat3::from_scale_angle_translation(
// vec2(1920., 16.),
// 0.,
// vec2(0., 0.)
// )),
// ProgressbarComponent {
// progress: 0.33
// },
// PrimaryColor::default(),
// SecondaryColor::default(),
// ));
// }

View file

@ -1,45 +0,0 @@
use shipyard::{UniqueView, UniqueViewMut, NonSendSync, View, Component, IntoIter, IntoWithId, Get, track};
use crate::{
assets::ProgressbarShaderPrefab,
rendering::{
RenderTarget,
primitives::rect::RectPrimitive
},
transform::Transform2d,
};
use super::{GuiComponent, PrimaryColor, SecondaryColor, GuiView};
#[derive(Component, Debug, Clone, Copy, Default)]
pub struct ProgressbarComponent {
pub progress: f32
}
pub fn render_progressbars(
mut target: NonSendSync<UniqueViewMut<RenderTarget>>,
rect: NonSendSync<UniqueView<RectPrimitive>>,
program: NonSendSync<UniqueView<ProgressbarShaderPrefab>>,
view: UniqueView<GuiView>,
components: View<GuiComponent>,
transforms: View<Transform2d, track::All>,
progressbars: View<ProgressbarComponent>,
primary: View<PrimaryColor>,
secondary: View<SecondaryColor>,
) {
for (eid, (_, transform, progress)) in (&components, &transforms, &progressbars).iter().with_id() {
let primary_color = primary.get(eid).copied().unwrap_or_default();
let secondary_color = secondary.get(eid).copied().unwrap_or_default();
// target.0.draw(
// &rect.0,
// &rect.1,
// &program.0,
// &uniform! {
// transform: transform.0.to_cols_array_2d(),
// ui_view: view.0.to_cols_array_2d(),
// progress: progress.progress,
// color: primary_color.0.to_array(),
// bg_color: secondary_color.0.to_array(),
// },
// &DrawParameters::default()
// ).unwrap();
}
}

View file

@ -13,8 +13,7 @@ use winit::{
use glam::vec3; use glam::vec3;
use std::time::Instant; use std::time::Instant;
pub use kubi_shared::transform; pub(crate) use kubi_shared::transform;
pub(crate) mod rendering; pub(crate) mod rendering;
pub(crate) mod world; pub(crate) mod world;
pub(crate) mod player; pub(crate) mod player;
@ -29,9 +28,6 @@ pub(crate) mod delta_time;
pub(crate) mod cursor_lock; pub(crate) mod cursor_lock;
pub(crate) mod control_flow; pub(crate) mod control_flow;
pub(crate) mod state; pub(crate) mod state;
#[deprecated="will be replaced by an immediate-mode ui soon, currently a no-op"]
#[allow(deprecated)]
pub(crate) mod gui;
pub(crate) mod networking; pub(crate) mod networking;
pub(crate) mod init; pub(crate) mod init;
pub(crate) mod color; pub(crate) mod color;
@ -76,7 +72,6 @@ use control_flow::{exit_on_esc, insert_control_flow_unique, SetControlFlow};
use state::{is_ingame, is_ingame_or_loading, is_loading, init_state, update_state, is_connecting}; use state::{is_ingame, is_ingame_or_loading, is_loading, init_state, update_state, is_connecting};
use networking::{update_networking, update_networking_late, is_multiplayer, disconnect_on_exit, is_singleplayer}; use networking::{update_networking, update_networking_late, is_multiplayer, disconnect_on_exit, is_singleplayer};
use init::initialize_from_args; use init::initialize_from_args;
use gui::{render_gui, init_gui, update_gui};
use loading_screen::update_loading_screen; use loading_screen::update_loading_screen;
use connecting_screen::switch_to_loading_if_connected; use connecting_screen::switch_to_loading_if_connected;
use fixed_timestamp::init_fixed_timestamp_storage; use fixed_timestamp::init_fixed_timestamp_storage;
@ -99,7 +94,6 @@ fn startup() -> Workload {
initialize_from_args, initialize_from_args,
lock_cursor_now, lock_cursor_now,
init_input, init_input,
init_gui,
insert_control_flow_unique, insert_control_flow_unique,
init_delta_time, init_delta_time,
).into_sequential_workload() ).into_sequential_workload()
@ -134,7 +128,6 @@ fn update() -> Workload {
).into_sequential_workload().run_if(is_ingame), ).into_sequential_workload().run_if(is_ingame),
update_networking_late.run_if(is_multiplayer), update_networking_late.run_if(is_multiplayer),
compute_cameras, compute_cameras,
update_gui,
update_state, update_state,
exit_on_esc, exit_on_esc,
disconnect_on_exit.run_if(is_multiplayer), disconnect_on_exit.run_if(is_multiplayer),
@ -150,7 +143,6 @@ fn render() -> Workload {
render_selection_box, render_selection_box,
render_entities, render_entities,
).into_sequential_workload().run_if(is_ingame), ).into_sequential_workload().run_if(is_ingame),
render_gui,
).into_sequential_workload() ).into_sequential_workload()
} }

View file

@ -1,104 +1,111 @@
use shipyard::{UniqueView, UniqueViewMut, Workload, IntoWorkload, EntityId, Unique, AllStoragesViewMut, ViewMut, Get, SystemModificator, track}; // use shipyard::{UniqueView, UniqueViewMut, Workload, IntoWorkload, EntityId, Unique, AllStoragesViewMut, ViewMut, Get, SystemModificator, track};
use winit::event::VirtualKeyCode; // use winit::event::VirtualKeyCode;
use glam::{Mat3, vec2}; // use glam::{Mat3, vec2};
use crate::{ // use crate::{
world::ChunkStorage, // world::ChunkStorage,
state::{GameState, NextState, is_changing_state}, // state::{GameState, NextState, is_changing_state},
transform::Transform2d, // transform::Transform2d,
gui::{ // gui::{
GuiComponent, // GuiComponent,
progressbar::ProgressbarComponent // progressbar::ProgressbarComponent
}, // },
rendering::{Renderer, if_resized}, // rendering::{Renderer, if_resized},
input::RawKbmInputState, // input::RawKbmInputState,
}; // };
#[derive(Unique, Clone, Copy)] // #[derive(Unique, Clone, Copy)]
struct ProgressbarId(EntityId); // struct ProgressbarId(EntityId);
fn spawn_loading_screen( // fn spawn_loading_screen(
mut storages: AllStoragesViewMut, // mut storages: AllStoragesViewMut,
) { // ) {
let size = storages.borrow::<UniqueView<Renderer>>().unwrap().size; // let size = storages.borrow::<UniqueView<Renderer>>().unwrap().size;
let entity = storages.add_entity(( // let entity = storages.add_entity((
GuiComponent, // GuiComponent,
Transform2d(Mat3::from_scale_angle_translation( // Transform2d(Mat3::from_scale_angle_translation(
vec2(size.width as f32, 16.), // vec2(size.width as f32, 16.),
0., // 0.,
vec2(0., 0.) // vec2(0., 0.)
)), // )),
ProgressbarComponent { // ProgressbarComponent {
progress: 0.33 // progress: 0.33
}, // },
)); // ));
storages.add_unique(ProgressbarId(entity)); // storages.add_unique(ProgressbarId(entity));
} // }
fn resize_progress_bar( // fn resize_progress_bar(
renderer: UniqueView<Renderer>, // renderer: UniqueView<Renderer>,
bar: UniqueView<ProgressbarId>, // bar: UniqueView<ProgressbarId>,
mut transforms: ViewMut<Transform2d, track::All> // mut transforms: ViewMut<Transform2d, track::All>
) { // ) {
let mut trans = (&mut transforms).get(bar.0).unwrap(); // let mut trans = (&mut transforms).get(bar.0).unwrap();
trans.0.x_axis.x = renderer.size.width as f32; // trans.0.x_axis.x = renderer.size.width as f32;
} // }
fn update_progress_bar_progress ( // fn update_progress_bar_progress (
world: UniqueView<ChunkStorage>, // world: UniqueView<ChunkStorage>,
mut bar: ViewMut<ProgressbarComponent>, // mut bar: ViewMut<ProgressbarComponent>,
eid: UniqueView<ProgressbarId>, // eid: UniqueView<ProgressbarId>,
) { // ) {
let mut bar = (&mut bar).get(eid.0).unwrap(); // let mut bar = (&mut bar).get(eid.0).unwrap();
let loaded = world.chunks.iter().fold(0, |acc, (&_, chunk)| { // let loaded = world.chunks.iter().fold(0, |acc, (&_, chunk)| {
acc + chunk.desired_state.matches_current(chunk.current_state) as usize // acc + chunk.desired_state.matches_current(chunk.current_state) as usize
}); // });
let total = world.chunks.len(); // let total = world.chunks.len();
let progress = loaded as f32 / total as f32; // let progress = loaded as f32 / total as f32;
bar.progress = progress; // bar.progress = progress;
} // }
fn switch_to_ingame_if_loaded( // fn switch_to_ingame_if_loaded(
world: UniqueView<ChunkStorage>, // world: UniqueView<ChunkStorage>,
mut state: UniqueViewMut<NextState> // mut state: UniqueViewMut<NextState>
) { // ) {
if world.chunks.is_empty() { // if world.chunks.is_empty() {
return // return
} // }
if world.chunks.iter().all(|(_, chunk)| { // if world.chunks.iter().all(|(_, chunk)| {
chunk.desired_state.matches_current(chunk.current_state) // chunk.desired_state.matches_current(chunk.current_state)
}) { // }) {
log::info!("Finished loading chunks"); // log::info!("Finished loading chunks");
state.0 = Some(GameState::InGame); // state.0 = Some(GameState::InGame);
} // }
} // }
fn override_loading( // fn override_loading(
kbm_state: UniqueView<RawKbmInputState>, // kbm_state: UniqueView<RawKbmInputState>,
mut state: UniqueViewMut<NextState> // mut state: UniqueViewMut<NextState>
) { // ) {
if kbm_state.keyboard_state.contains(VirtualKeyCode::F as u32) { // if kbm_state.keyboard_state.contains(VirtualKeyCode::F as u32) {
state.0 = Some(GameState::InGame); // state.0 = Some(GameState::InGame);
} // }
} // }
// fn despawn_loading_screen_if_switching_state(
// mut storages: AllStoragesViewMut,
// ) {
// let state = storages.borrow::<UniqueView<NextState>>().unwrap().0.unwrap();
// if state != GameState::LoadingWorld {
// let progress_bar = storages.borrow::<UniqueView<ProgressbarId>>().unwrap().0;
// storages.delete_entity(progress_bar);
// storages.remove_unique::<ProgressbarId>().unwrap();
// }
// }
// pub fn update_loading_screen() -> Workload {
// (
// spawn_loading_screen.run_if_missing_unique::<ProgressbarId>(),
// resize_progress_bar.run_if(if_resized),
// update_progress_bar_progress,
// override_loading,
// switch_to_ingame_if_loaded,
// despawn_loading_screen_if_switching_state.run_if(is_changing_state),
// ).into_sequential_workload()
// }
use shipyard::Workload;
fn despawn_loading_screen_if_switching_state(
mut storages: AllStoragesViewMut,
) {
let state = storages.borrow::<UniqueView<NextState>>().unwrap().0.unwrap();
if state != GameState::LoadingWorld {
let progress_bar = storages.borrow::<UniqueView<ProgressbarId>>().unwrap().0;
storages.delete_entity(progress_bar);
storages.remove_unique::<ProgressbarId>().unwrap();
}
}
pub fn update_loading_screen() -> Workload { pub fn update_loading_screen() -> Workload {
( Workload::new("todo")
spawn_loading_screen.run_if_missing_unique::<ProgressbarId>(),
resize_progress_bar.run_if(if_resized),
update_progress_bar_progress,
override_loading,
switch_to_ingame_if_loaded,
despawn_loading_screen_if_switching_state.run_if(is_changing_state),
).into_sequential_workload()
} }

View file

@ -9,6 +9,7 @@ use glam::{Vec3, UVec2};
use pollster::FutureExt as _; use pollster::FutureExt as _;
use crate::{events::WindowResizedEvent, settings::{GameSettings, FullscreenMode}}; use crate::{events::WindowResizedEvent, settings::{GameSettings, FullscreenMode}};
pub mod shaders;
pub mod primitives; pub mod primitives;
pub mod world; pub mod world;
pub mod selection_box; pub mod selection_box;
@ -17,9 +18,6 @@ pub mod entities;
#[derive(Unique)] #[derive(Unique)]
#[repr(transparent)] #[repr(transparent)]
pub struct RenderTarget(pub ()); pub struct RenderTarget(pub ());
impl Drop for RenderTarget {
}
#[derive(Unique)] #[derive(Unique)]
#[repr(transparent)] #[repr(transparent)]

View file

@ -6,15 +6,15 @@ pub mod rect;
use cube::init_cube_primitive; use cube::init_cube_primitive;
use rect::init_rect_primitive; use rect::init_rect_primitive;
#[repr(C)] #[repr(C, packed)]
#[derive(Clone, Copy, Default)] #[derive(Clone, Copy, Default, bytemuck::Pod, bytemuck::Zeroable)]
pub struct PositionOnlyVertex { pub struct PositionVertex {
pub position: [f32; 3], pub position: [f32; 3],
} }
#[repr(C)] #[repr(C, packed)]
#[derive(Clone, Copy, Default)] #[derive(Clone, Copy, Default, bytemuck::Pod, bytemuck::Zeroable)]
pub struct PositionOnlyVertex2d { pub struct PositionVertex2d {
pub position: [f32; 2], pub position: [f32; 2],
} }

View file

@ -1,6 +1,6 @@
use shipyard::{AllStoragesView, NonSendSync, UniqueView, Unique}; use shipyard::{AllStoragesView, NonSendSync, UniqueView, Unique};
use crate::rendering::Renderer; use crate::rendering::Renderer;
use super::PositionOnlyVertex; use super::PositionVertex;
#[derive(Unique)] #[derive(Unique)]
pub struct CubePrimitive { pub struct CubePrimitive {
@ -14,29 +14,29 @@ pub struct CenteredCubePrimitive {
pub idx: wgpu::Buffer pub idx: wgpu::Buffer
} }
const CENTERED_CUBE_VERTICES: &[PositionOnlyVertex] = &[ const CENTERED_CUBE_VERTICES: &[PositionVertex] = &[
// front // front
PositionOnlyVertex { position: [-0.5, -0.5, 0.5] }, PositionVertex { position: [-0.5, -0.5, 0.5] },
PositionOnlyVertex { position: [ 0.5, -0.5, 0.5] }, PositionVertex { position: [ 0.5, -0.5, 0.5] },
PositionOnlyVertex { position: [ 0.5, 0.5, 0.5] }, PositionVertex { position: [ 0.5, 0.5, 0.5] },
PositionOnlyVertex { position: [-0.5, 0.5, 0.5] }, PositionVertex { position: [-0.5, 0.5, 0.5] },
// back // back
PositionOnlyVertex { position: [-0.5, -0.5, -0.5] }, PositionVertex { position: [-0.5, -0.5, -0.5] },
PositionOnlyVertex { position: [ 0.5, -0.5, -0.5] }, PositionVertex { position: [ 0.5, -0.5, -0.5] },
PositionOnlyVertex { position: [ 0.5, 0.5, -0.5] }, PositionVertex { position: [ 0.5, 0.5, -0.5] },
PositionOnlyVertex { position: [-0.5, 0.5, -0.5] }, PositionVertex { position: [-0.5, 0.5, -0.5] },
]; ];
const CUBE_VERTICES: &[PositionOnlyVertex] = &[ const CUBE_VERTICES: &[PositionVertex] = &[
// front // front
PositionOnlyVertex { position: [0.0, 0.0, 1.0] }, PositionVertex { position: [0.0, 0.0, 1.0] },
PositionOnlyVertex { position: [1.0, 0.0, 1.0] }, PositionVertex { position: [1.0, 0.0, 1.0] },
PositionOnlyVertex { position: [1.0, 1.0, 1.0] }, PositionVertex { position: [1.0, 1.0, 1.0] },
PositionOnlyVertex { position: [0.0, 1.0, 1.0] }, PositionVertex { position: [0.0, 1.0, 1.0] },
// back // back
PositionOnlyVertex { position: [0.0, 0.0, 0.0] }, PositionVertex { position: [0.0, 0.0, 0.0] },
PositionOnlyVertex { position: [1.0, 0.0, 0.0] }, PositionVertex { position: [1.0, 0.0, 0.0] },
PositionOnlyVertex { position: [1.0, 1.0, 0.0] }, PositionVertex { position: [1.0, 1.0, 0.0] },
PositionOnlyVertex { position: [0.0, 1.0, 0.0] }, PositionVertex { position: [0.0, 1.0, 0.0] },
]; ];
const CUBE_INDICES: &[u16] = &[ const CUBE_INDICES: &[u16] = &[
// front // front

View file

@ -1,15 +1,15 @@
use shipyard::{Unique, AllStoragesView, NonSendSync, UniqueView}; use shipyard::{Unique, AllStoragesView, NonSendSync, UniqueView};
use crate::rendering::Renderer; use crate::rendering::Renderer;
use super::PositionOnlyVertex2d; use super::PositionVertex2d;
#[derive(Unique)] #[derive(Unique)]
pub struct RectPrimitive(pub VertexBuffer<PositionOnlyVertex2d>, pub IndexBuffer<u16>); pub struct RectPrimitive(pub VertexBuffer<PositionVertex2d>, pub IndexBuffer<u16>);
const RECT_VERTEX: &[PositionOnlyVertex2d] = &[ const RECT_VERTEX: &[PositionVertex2d] = &[
PositionOnlyVertex2d { position: [0., 0.] }, PositionVertex2d { position: [0., 0.] },
PositionOnlyVertex2d { position: [1., 0.] }, PositionVertex2d { position: [1., 0.] },
PositionOnlyVertex2d { position: [0., 1.] }, PositionVertex2d { position: [0., 1.] },
PositionOnlyVertex2d { position: [1., 1.] }, PositionVertex2d { position: [1., 1.] },
]; ];
const RECT_INDEX: &[u16] = &[0, 1, 2, 1, 3, 2]; const RECT_INDEX: &[u16] = &[0, 1, 2, 1, 3, 2];

View file

@ -0,0 +1,31 @@
use shipyard::{Unique, NonSendSync, UniqueView, AllStoragesView};
use super::Renderer;
#[derive(Unique)]
pub struct Shaders {
pub world: wgpu::ShaderModule
}
macro_rules! shaders {
{$renderer: expr, $dir: literal, $($name: ident -> $path: literal),*} => {
{
use super::Renderer;
let renderer: &Renderer = $renderer;
$({
let _is_string_literal: &str = $path;
renderer.device.create_shader_module(wgpu::include_wgsl!(concat!($dir, "/", $path)));
})*
}
};
}
pub fn compile_shaders(
storages: AllStoragesView,
) {
let renderer = &storages.borrow::<NonSendSync<UniqueView<Renderer>>>().unwrap();
shaders! {
renderer, "../../shaders",
world -> "world.wgsl"
};
}

View file

@ -17,7 +17,7 @@ use crate::{
}; };
use super::{RenderTarget, primitives::cube::CubePrimitive}; use super::{RenderTarget, primitives::cube::CubePrimitive};
#[repr(C)] #[repr(C, packed)]
#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)] #[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
pub struct ChunkVertex { pub struct ChunkVertex {
pub position: [f32; 3], pub position: [f32; 3],