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***
<b>Highly experimental very early work-in-progress wgpu version of Kubi!</b><br>
(will also include the new gui system)
<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)]
#[repr(transparent)]
pub struct BlockTexturesPrefab(pub SrgbTexture2dArray);
#[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 struct BlockTexturesPrefab(pub wgpu::Texture);
pub fn load_prefabs(
storages: AllStoragesView,
@ -59,23 +47,4 @@ pub fn load_prefabs(
storages.add_unique_non_send_sync(BlockTexturesPrefab(
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 std::time::Instant;
pub use kubi_shared::transform;
pub(crate) use kubi_shared::transform;
pub(crate) mod rendering;
pub(crate) mod world;
pub(crate) mod player;
@ -29,9 +28,6 @@ pub(crate) mod delta_time;
pub(crate) mod cursor_lock;
pub(crate) mod control_flow;
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 init;
pub(crate) mod color;
@ -42,9 +38,9 @@ pub(crate) mod filesystem;
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};
@ -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 networking::{update_networking, update_networking_late, is_multiplayer, disconnect_on_exit, is_singleplayer};
use init::initialize_from_args;
use gui::{render_gui, init_gui, update_gui};
use loading_screen::update_loading_screen;
use connecting_screen::switch_to_loading_if_connected;
use fixed_timestamp::init_fixed_timestamp_storage;
@ -99,7 +94,6 @@ fn startup() -> Workload {
initialize_from_args,
lock_cursor_now,
init_input,
init_gui,
insert_control_flow_unique,
init_delta_time,
).into_sequential_workload()
@ -134,7 +128,6 @@ fn update() -> Workload {
).into_sequential_workload().run_if(is_ingame),
update_networking_late.run_if(is_multiplayer),
compute_cameras,
update_gui,
update_state,
exit_on_esc,
disconnect_on_exit.run_if(is_multiplayer),
@ -150,7 +143,6 @@ fn render() -> Workload {
render_selection_box,
render_entities,
).into_sequential_workload().run_if(is_ingame),
render_gui,
).into_sequential_workload()
}

View file

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

View file

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

View file

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

View file

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

View file

@ -1,15 +1,15 @@
use shipyard::{Unique, AllStoragesView, NonSendSync, UniqueView};
use crate::rendering::Renderer;
use super::PositionOnlyVertex2d;
use super::PositionVertex2d;
#[derive(Unique)]
pub struct RectPrimitive(pub VertexBuffer<PositionOnlyVertex2d>, pub IndexBuffer<u16>);
pub struct RectPrimitive(pub VertexBuffer<PositionVertex2d>, pub IndexBuffer<u16>);
const RECT_VERTEX: &[PositionOnlyVertex2d] = &[
PositionOnlyVertex2d { position: [0., 0.] },
PositionOnlyVertex2d { position: [1., 0.] },
PositionOnlyVertex2d { position: [0., 1.] },
PositionOnlyVertex2d { position: [1., 1.] },
const RECT_VERTEX: &[PositionVertex2d] = &[
PositionVertex2d { position: [0., 0.] },
PositionVertex2d { position: [1., 0.] },
PositionVertex2d { position: [0., 1.] },
PositionVertex2d { position: [1., 1.] },
];
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};
#[repr(C)]
#[repr(C, packed)]
#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
pub struct ChunkVertex {
pub position: [f32; 3],

View file

@ -27,8 +27,8 @@ pub enum CurrentChunkState {
Loaded,
CalculatingMesh,
Rendered,
RecalculatingMesh,
Unloading,
RecalculatingMesh,
Unloading,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]