Compare commits

...

8 commits

19 changed files with 319 additions and 51 deletions

1
Cargo.lock generated
View file

@ -1033,6 +1033,7 @@ dependencies = [
"glam",
"glium",
"hashbrown",
"log",
"nohash-hasher",
]

View file

@ -16,6 +16,7 @@ pub fn init() {
level_style.set_color(match record.level() {
Level::Error => Color::Red,
Level::Warn => Color::Yellow,
Level::Debug | Level::Trace => Color::Cyan,
_ => Color::Blue
}).set_bold(true);

View file

@ -9,6 +9,7 @@ hashbrown = "0.14"
nohash-hasher = "0.2"
glam = { version = "0.24", features = ["approx"] }
glium = { git = "https://github.com/glium/glium", rev = "968fc92378caf", optional = true }
log = "0.4"
[features]
default = ["backend_glium", "builtin_elements"]

View file

@ -0,0 +1,10 @@
#version 300 es
precision highp float;
out vec4 out_color;
uniform vec4 color;
void main() {
if (color.w <= 0.) discard;
out_color = color;
}

View file

@ -0,0 +1,9 @@
#version 300 es
precision highp float;
in vec2 position;
uniform vec2 resolution;
void main() {
gl_Position = vec4(vec2(1., -1.) * (position / resolution), 0., 1.);
}

View file

@ -0,0 +1,101 @@
use glam::Vec2;
use glium::{
Surface, DrawParameters, Blend,
Program, VertexBuffer, IndexBuffer,
backend::Facade,
index::PrimitiveType,
implement_vertex, uniform,
};
use crate::draw::{UiDrawPlan, UiVertex};
const VERTEX_SHADER: &str = include_str!("../../shaders/vertex.vert");
const FRAGMENT_SHADER: &str = include_str!("../../shaders/fragment.frag");
#[derive(Clone, Copy)]
struct Vertex {
position: [f32; 2],
color: [f32; 4],
}
impl From<UiVertex> for Vertex {
fn from(v: UiVertex) -> Self {
Self {
position: v.position.to_array(),
color: v.color.to_array(),
}
}
}
implement_vertex!(Vertex, position, color);
pub struct GliumUiRenderer {
pub program: glium::Program,
pub vertex_buffer: glium::VertexBuffer<Vertex>,
pub index_buffer: glium::IndexBuffer<u32>,
}
impl GliumUiRenderer {
pub fn new<F: Facade>(facade: &F) -> Self {
log::info!("init glium backend for ui");
log::debug!("init program");
let program = Program::from_source(facade, VERTEX_SHADER, FRAGMENT_SHADER, None).unwrap();
log::debug!("init buffers");
let vertex_buffer = VertexBuffer::empty_persistent(facade, 1024).unwrap();
let index_buffer = IndexBuffer::empty_persistent(facade, PrimitiveType::TrianglesList, 1024).unwrap();
Self {
program,
vertex_buffer,
index_buffer,
}
}
fn ensure_buffer_size(&mut self, need_vtx: usize, need_idx: usize) {
let current_vtx_size = self.vertex_buffer.get_size();
let current_idx_size = self.index_buffer.get_size();
if current_vtx_size >= need_vtx && current_idx_size >= need_idx {
return
}
let new_vtx_size = (need_vtx + 1).next_power_of_two();
let new_idx_size = (need_idx + 1).next_power_of_two();
log::debug!("resizing buffers: vtx {} -> {}, idx {} -> {}", current_vtx_size, new_vtx_size, current_idx_size, new_idx_size);
if current_vtx_size != new_vtx_size {
self.vertex_buffer = VertexBuffer::empty_persistent(self.vertex_buffer.get_context(), new_vtx_size).unwrap();
}
if current_idx_size != new_idx_size {
self.index_buffer = IndexBuffer::empty_persistent(self.index_buffer.get_context(), PrimitiveType::TrianglesList, new_idx_size).unwrap();
}
}
fn write_buffer_data(&mut self, vtx: &[Vertex], idx: &[u32]) {
log::info!("uploading {} vertices and {} indices", vtx.len(), idx.len());
self.ensure_buffer_size(vtx.len(), idx.len());
self.vertex_buffer.invalidate();
self.vertex_buffer.slice_mut(0..vtx.len()).unwrap().write(vtx);
self.index_buffer.invalidate();
self.index_buffer.slice_mut(0..idx.len()).unwrap().write(idx);
}
pub fn update(&mut self, plan: &UiDrawPlan) {
let data_vtx = &plan.vertices.iter().copied().map(Vertex::from).collect::<Vec<_>>();
let data_idx = &plan.indices;
self.write_buffer_data(data_vtx, data_idx);
}
pub fn draw(&self, frame: &mut glium::Frame, resolution: Vec2) {
let params = DrawParameters {
blend: Blend::alpha_blending(),
..Default::default()
};
frame.draw(
&self.vertex_buffer,
&self.index_buffer,
&self.program,
&uniform! {
resolution: resolution.to_array(),
},
&params,
).unwrap();
}
}

View file

@ -1,7 +1,8 @@
use glam::{Vec2, vec2, Vec4};
use std::num::NonZeroU16;
use glam::{Vec2, Vec4};
#[derive(Clone, Copy, Debug)]
pub enum UiDrawCall {
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum UiDrawCommand {
///Filled, colored rectangle
Rectangle {
///Position in pixels
@ -13,10 +14,60 @@ pub enum UiDrawCall {
}
}
pub struct UiDrawCalls {
pub calls: Vec<UiDrawCall>,
#[derive(Default)]
pub struct UiDrawCommands {
pub commands: Vec<UiDrawCommand>,
}
// impl UiDrawCommands {
// pub fn compare(&self, other: &Self) -> bool {
// // if self.commands.len() != other.commands.len() { return false }
// // self.commands.iter().zip(other.commands.iter()).all(|(a, b)| a == b)
// }
// }
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct UiVertex {
pub position: Vec2,
pub color: Vec4,
//pub texture: Option<NonZeroU16>,
}
#[derive(Default)]
pub struct UiDrawPlan {
pub vertices: Vec<UiVertex>,
pub indices: Vec<u32>,
}
impl UiDrawPlan {
pub fn build(calls: &UiDrawCommands) -> Self {
let mut plan = Self::default();
for call in &calls.commands {
match call {
UiDrawCommand::Rectangle { position, size, color } => {
let idx = plan.vertices.len() as u32;
plan.indices.extend([idx, idx + 1, idx + 2, idx, idx + 2, idx + 3]);
plan.vertices.extend([
UiVertex {
position: *position,
color: *color,
},
UiVertex {
position: *position + Vec2::new(size.x, 0.0),
color: *color,
},
UiVertex {
position: *position + *size,
color: *color,
},
UiVertex {
position: *position + Vec2::new(0.0, size.y),
color: *color,
},
]);
}
}
}
plan
}
}

View file

@ -1,7 +1,7 @@
use std::any::Any;
use crate::{
LayoutInfo,
draw::UiDrawCall,
draw::UiDrawCommand,
measure::Response,
state::StateRepo
};
@ -17,5 +17,5 @@ pub trait UiElement {
fn is_stateless(&self) -> bool { self.state_id().is_none() }
fn init_state(&self) -> Option<Box<dyn Any>> { None }
fn measure(&self, state: &StateRepo, layout: &LayoutInfo) -> Response;
fn draw(&self, measure: &Response, state: &mut StateRepo, layout: &LayoutInfo, draw: &mut Vec<UiDrawCall>);
fn process(&self, measure: &Response, state: &mut StateRepo, layout: &LayoutInfo, draw: &mut Vec<UiDrawCommand>);
}

View file

@ -1,5 +1,5 @@
use glam::{Vec2, Vec4};
use crate::{UiDirection, LayoutInfo, draw::UiDrawCall, measure::{IsMeasurable, Response}, state::StateRepo, UiSize};
use crate::{UiDirection, LayoutInfo, draw::UiDrawCommand, measure::{IsMeasurable, Response}, state::StateRepo, UiSize};
use super::UiElement;
#[derive(Default, Clone, Copy, Debug)]
@ -73,9 +73,9 @@ impl UiElement for Container {
Response { desired_size: size }
}
fn draw(&self, measure: &Response, state: &mut StateRepo, layout: &LayoutInfo, draw: &mut Vec<UiDrawCall>) {
fn process(&self, measure: &Response, state: &mut StateRepo, layout: &LayoutInfo, draw: &mut Vec<UiDrawCommand>) {
if let Some(color) = self.background {
draw.push(UiDrawCall::Rectangle {
draw.push(UiDrawCommand::Rectangle {
position: layout.position,
size: measure.desired_size,
color

View file

@ -1,17 +1,28 @@
use glam::{vec2, Vec4};
use glam::{vec2, Vec4, vec4};
use crate::{
UiSize, LayoutInfo,
draw::UiDrawCall,
draw::UiDrawCommand,
measure::Response,
state::StateRepo
};
use super::UiElement;
struct ProgressBar {
size: (UiSize, UiSize),
value: f32,
color_foreground: Vec4,
color_background: Vec4,
pub struct ProgressBar {
pub size: (UiSize, UiSize),
pub value: f32,
pub color_foreground: Vec4,
pub color_background: Vec4,
}
impl Default for ProgressBar {
fn default() -> Self {
Self {
size: (UiSize::Auto, UiSize::Auto),
value: 0.,
color_foreground: vec4(0.0, 0.0, 1.0, 1.0),
color_background: vec4(0.0, 0.0, 0.0, 1.0),
}
}
}
const BAR_HEIGHT: f32 = 20.0;
@ -36,14 +47,14 @@ impl UiElement for ProgressBar {
}
}
fn draw(&self, measure: &Response, state: &mut StateRepo, layout: &LayoutInfo, draw: &mut Vec<UiDrawCall>) {
draw.push(UiDrawCall::Rectangle {
fn process(&self, measure: &Response, state: &mut StateRepo, layout: &LayoutInfo, draw: &mut Vec<UiDrawCommand>) {
draw.push(UiDrawCommand::Rectangle {
position: layout.position,
size: measure.desired_size,
color: self.color_background
});
draw.push(UiDrawCall::Rectangle {
draw.push(UiDrawCommand::Rectangle {
position: layout.position,
size: measure.desired_size * vec2(self.value, 1.0),
color: self.color_foreground

View file

@ -1,5 +1,5 @@
use glam::vec2;
use crate::{state::StateRepo, LayoutInfo, measure::Response, draw::UiDrawCall, UiDirection};
use crate::{state::StateRepo, LayoutInfo, measure::Response, draw::UiDrawCommand, UiDirection};
use super::UiElement;
pub struct Spacer(f32);
@ -14,5 +14,5 @@ impl UiElement for Spacer {
}
}
fn draw(&self, _measure: &Response, _state: &mut StateRepo, _layout: &LayoutInfo, _draw: &mut Vec<UiDrawCall>) {}
fn process(&self, _measure: &Response, _state: &mut StateRepo, _layout: &LayoutInfo, _draw: &mut Vec<UiDrawCommand>) {}
}

View file

@ -8,12 +8,19 @@ pub mod backend;
pub mod measure;
pub mod state;
use element::UiElement;
use state::StateRepo;
use event::UiEvent;
use draw::{UiDrawCommands, UiDrawPlan};
pub struct KubiUi {
mouse_position: Vec2,
stateful_state: StateRepo,
event_queue: VecDeque<event::UiEvent>,
event_queue: VecDeque<UiEvent>,
prev_draw_commands: UiDrawCommands,
draw_commands: UiDrawCommands,
draw_plan: UiDrawPlan,
draw_plan_modified: bool,
}
impl KubiUi {
@ -22,8 +29,31 @@ impl KubiUi {
mouse_position: Vec2::ZERO,
stateful_state: StateRepo::default(),
event_queue: VecDeque::new(),
// root_elements: Vec::new(),
prev_draw_commands: UiDrawCommands::default(),
draw_commands: UiDrawCommands::default(),
draw_plan: UiDrawPlan::default(),
draw_plan_modified: false,
}
}
pub fn begin(&mut self) {
std::mem::swap(&mut self.prev_draw_commands, &mut self.draw_commands);
self.draw_plan_modified = false;
self.draw_commands.commands.clear();
}
pub fn end(&mut self) {
if self.draw_commands.commands == self.prev_draw_commands.commands {
return
}
self.draw_plan = UiDrawPlan::build(&self.draw_commands);
self.draw_plan_modified = true;
}
pub fn draw_plan(&self) -> (bool, &UiDrawPlan) {
(self.draw_plan_modified, &self.draw_plan)
}
}
impl Default for KubiUi {

View file

@ -0,0 +1,45 @@
use kubi_ui::{KubiUi, backend::glium::GliumUiRenderer};
use shipyard::{AllStoragesView, Unique, UniqueView, NonSendSync, UniqueViewMut};
use crate::rendering::{Renderer, RenderTarget, WindowSize};
#[derive(Unique)]
pub struct UiState {
pub ui: KubiUi,
pub renderer: GliumUiRenderer,
}
pub fn kubi_ui_init(
storages: AllStoragesView
) {
let renderer = storages.borrow::<NonSendSync<UniqueView<Renderer>>>().unwrap();
storages.add_unique_non_send_sync(UiState {
ui: KubiUi::new(),
renderer: GliumUiRenderer::new(&renderer.display)
});
}
pub fn kubi_ui_begin(
mut ui: NonSendSync<UniqueViewMut<UiState>>
) {
ui.ui.begin();
}
pub fn kubi_ui_end(
mut ui: NonSendSync<UniqueViewMut<UiState>>
) {
let ui: &mut UiState = &mut ui;
let UiState { ui, renderer } = ui;
ui.end();
let (upload_needed, plan) = ui.draw_plan();
if upload_needed {
renderer.update(plan);
}
}
pub fn kubi_ui_draw(
ui: NonSendSync<UniqueView<UiState>>,
mut target: NonSendSync<UniqueViewMut<RenderTarget>>,
size: UniqueView<WindowSize>
) {
ui.renderer.draw(&mut target.0, size.0.as_vec2());
}

View file

@ -9,29 +9,29 @@ use progressbar::render_progressbars;
//TODO compute gui scale on window resize
#[derive(Unique, Clone, Copy, Debug, Default)]
pub struct GuiView(pub Mat4);
pub struct LegacyGuiView(pub Mat4);
#[derive(Component, Clone, Copy, Debug, Default)]
pub struct GuiComponent;
pub struct LegacyGuiComponent;
#[derive(Component, Clone, Copy, Debug)]
pub struct PrimaryColor(pub Vec4);
impl Default for PrimaryColor {
pub struct LegacyPrimaryColor(pub Vec4);
impl Default for LegacyPrimaryColor {
fn default() -> Self {
Self(color_hex(0x156cddff))
}
}
#[derive(Component, Clone, Copy, Debug)]
pub struct SecondaryColor(pub Vec4);
impl Default for SecondaryColor {
pub struct LegacySecondaryColor(pub Vec4);
impl Default for LegacySecondaryColor {
fn default() -> Self {
Self(color_hex(0xc9d5e4ff))
}
}
fn update_gui_view(
mut view: UniqueViewMut<GuiView>,
fn update_legacy_gui_view(
mut view: UniqueViewMut<LegacyGuiView>,
resize: View<WindowResizedEvent>,
) {
let Some(&size) = resize.iter().next() else {
@ -41,19 +41,19 @@ fn update_gui_view(
view.0 = Mat4::orthographic_rh_gl(0.0, w as f32, h as f32, 0.0, -1.0, 1.0);
}
pub fn init_gui(
pub fn legacy_ui_init(
storages: AllStoragesView
) {
storages.add_unique(GuiView::default());
storages.add_unique(LegacyGuiView::default());
}
pub fn update_gui() -> Workload {
pub fn legacy_ui_update() -> Workload {
(
update_gui_view
update_legacy_gui_view
).into_sequential_workload()
}
pub fn render_gui() -> Workload {
pub fn legacy_ui_render() -> Workload {
(
render_progressbars
).into_sequential_workload()

View file

@ -8,7 +8,7 @@ use crate::{
},
transform::Transform2d,
};
use super::{GuiComponent, PrimaryColor, SecondaryColor, GuiView};
use super::{LegacyGuiComponent, LegacyPrimaryColor, LegacySecondaryColor, LegacyGuiView};
#[derive(Component, Debug, Clone, Copy, Default)]
pub struct ProgressbarComponent {
@ -19,12 +19,12 @@ pub fn render_progressbars(
mut target: NonSendSync<UniqueViewMut<RenderTarget>>,
rect: NonSendSync<UniqueView<RectPrimitive>>,
program: NonSendSync<UniqueView<ProgressbarShaderPrefab>>,
view: UniqueView<GuiView>,
components: View<GuiComponent>,
view: UniqueView<LegacyGuiView>,
components: View<LegacyGuiComponent>,
transforms: View<Transform2d, track::All>,
progressbars: View<ProgressbarComponent>,
primary: View<PrimaryColor>,
secondary: View<SecondaryColor>,
primary: View<LegacyPrimaryColor>,
secondary: View<LegacySecondaryColor>,
) {
for (eid, (_, transform, progress)) in (&components, &transforms, &progressbars).iter().with_id() {
let primary_color = primary.get(eid).copied().unwrap_or_default();

View file

@ -30,6 +30,7 @@ pub(crate) mod cursor_lock;
pub(crate) mod control_flow;
pub(crate) mod state;
pub(crate) mod legacy_gui;
pub(crate) mod guiv2_integration;
pub(crate) mod networking;
pub(crate) mod init;
pub(crate) mod color;
@ -43,7 +44,7 @@ use world::{
loading::update_loaded_world_around_player,
raycast::update_raycasts,
queue::apply_queued_blocks,
tasks::{ChunkTaskManager},
tasks::ChunkTaskManager,
};
use player::{spawn_player, MainPlayer};
use prefabs::load_prefabs;
@ -76,7 +77,8 @@ use control_flow::{exit_on_esc, insert_control_flow_unique, RequestExit};
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 legacy_gui::{render_gui, init_gui, update_gui};
use legacy_gui::{legacy_ui_render, legacy_ui_init, legacy_ui_update};
use guiv2_integration::{kubi_ui_init, kubi_ui_begin, kubi_ui_end, kubi_ui_draw};
use loading_screen::update_loading_screen;
use connecting_screen::switch_to_loading_if_connected;
use fixed_timestamp::init_fixed_timestamp_storage;
@ -94,6 +96,7 @@ fn startup() -> Workload {
init_fixed_timestamp_storage,
initial_resize_event,
init_window_size,
kubi_ui_init,
load_prefabs,
init_primitives,
insert_lock_state,
@ -101,7 +104,7 @@ fn startup() -> Workload {
initialize_from_args,
lock_cursor_now,
init_input,
init_gui,
legacy_ui_init,
insert_control_flow_unique,
init_delta_time,
).into_sequential_workload()
@ -112,6 +115,7 @@ fn update() -> Workload {
update_window_size,
update_cursor_lock_state,
process_inputs,
kubi_ui_begin,
(
init_game_world.run_if_missing_unique::<ChunkTaskManager>(),
(
@ -137,7 +141,8 @@ fn update() -> Workload {
).into_sequential_workload().run_if(is_ingame),
update_networking_late.run_if(is_multiplayer),
compute_cameras,
update_gui,
legacy_ui_update,
kubi_ui_end,
update_state,
exit_on_esc,
disconnect_on_exit.run_if(is_multiplayer),
@ -153,7 +158,7 @@ fn render() -> Workload {
render_selection_box,
render_entities,
).into_sequential_workload().run_if(is_ingame),
render_gui,
legacy_ui_render,
).into_sequential_workload()
}

View file

@ -6,7 +6,7 @@ use crate::{
state::{GameState, NextState, is_changing_state},
transform::Transform2d,
legacy_gui::{
GuiComponent,
LegacyGuiComponent,
progressbar::ProgressbarComponent
},
rendering::{WindowSize, if_resized},
@ -21,7 +21,7 @@ fn spawn_loading_screen(
) {
let size = *storages.borrow::<UniqueView<WindowSize>>().unwrap();
let entity = storages.add_entity((
GuiComponent,
LegacyGuiComponent,
Transform2d(Mat3::from_scale_angle_translation(
vec2(size.0.x as f32, 16.),
0.,

View file

@ -92,5 +92,8 @@ pub fn load_prefabs(
&renderer.display
)
));
log::info!("releasing shader compiler");
renderer.display.release_shader_compiler();
}

View file

@ -234,8 +234,8 @@ fn process_completed_tasks(
}
//apply the mesh
let vertex_buffer = VertexBuffer::new(&renderer.display, &vertices).unwrap();
let index_buffer = IndexBuffer::new(&renderer.display, PrimitiveType::TrianglesList, &indexes).unwrap();
let vertex_buffer = VertexBuffer::immutable(&renderer.display, &vertices).unwrap();
let index_buffer = IndexBuffer::immutable(&renderer.display, PrimitiveType::TrianglesList, &indexes).unwrap();
let mesh = ChunkMesh {
vertex_buffer,
index_buffer,