5 Commits

8 changed files with 160 additions and 68 deletions

2
.gitignore vendored
View File

@@ -1 +1,3 @@
/target
Cargo.lock
.idea/*

8
.idea/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
# Editor-based HTTP Client requests
/httpRequests/

View File

@@ -14,7 +14,7 @@ use imgui::Ui;
pub struct ImguiWindow<'a, T> {
pub window: fn() -> imgui::Window<'a>,
pub func: fn(&Ui, &T),
pub func: fn(&Ui, Vec<&T>),
}
#[derive(Clone, Copy, Debug, PartialEq)]

View File

@@ -45,7 +45,6 @@ use wgpu_subscriber;
use winit_24::event::DeviceEvent::MouseMotion;
use winit_24::event::{ElementState, VirtualKeyCode};
use winit_24::event_loop::EventLoopProxy;
use winit_24::platform::unix::x11::ffi::Time;
use winit_24::window::Window;
use winit_24::{
event::{self, WindowEvent},
@@ -61,6 +60,7 @@ use crate::owned_event::{OwnedEvent, OwnedEventExtension};
use crate::physics::state::PhysicsState;
use crate::render::system::ImguiPerformanceProfilerLine;
use crate::runtime::state::RuntimeState;
use winit_24::dpi::PhysicalSize;
mod camera;
mod components;
@@ -134,11 +134,13 @@ fn main() {
let mut load_schedule = Schedule::builder()
.add_system(runtime::system::runtime_load_system())
.add_system(runtime::system::runtime_spawn_system())
.flush()
.build();
let mut render_schedule = Schedule::builder()
.add_system(render::system::render_imgui_system())
.add_system(render::system::render_test_system())
.add_system(render::system::render_performance_flag_system())
.build();
let mut update_schedule = Schedule::builder()
@@ -155,6 +157,7 @@ fn main() {
let event_loop = EventLoop::<OwnedEventExtension>::with_user_event();
let mut builder = winit_24::window::WindowBuilder::new();
builder = builder.with_title("MVGE");
builder = builder.with_inner_size(PhysicalSize::new(1200,900));
let window = builder.build(&event_loop).unwrap();

View File

@@ -14,6 +14,7 @@ use crate::components::{Collider, LoopState, Mesh, Physics, Position, ImguiWindo
use imgui::{FontSource, Condition};
use imgui::*;
use crate::physics::state::PhysicsState;
use crate::render::system::ImguiGenericOutputLine;
#[system]
@@ -78,12 +79,18 @@ pub fn run_physics(
#[system]
#[write_component(Camera)]
#[write_component(CameraController)]
#[write_component(ImguiGenericOutputLine)]
pub fn update_camera(world: &mut SubWorld, #[resource] loop_state: &mut LoopState) {
let mut query = <(&mut Camera, &mut CameraController)>::query();
for (mut camera, controller) in query.iter_mut(world) {
controller.update_camera(&mut camera, loop_state.step_size)
}
let mut query = <(&mut Camera, &mut CameraController, &mut ImguiGenericOutputLine)>::query();
for (mut camera, controller, ui_entry) in query.iter_mut(world) {
ui_entry.label = format!("Camera: {:.2}, {:.2}, {:.2}", camera.position.x, camera.position.y, camera.position.z);
}
}
#[system]
@@ -96,6 +103,7 @@ pub fn update_models(
#[resource] physics_state: &mut PhysicsState,
#[resource] physics_pipeline: &mut PhysicsPipeline,
) {
// Make sure all the entities we care about are added to the system
let mut query = <(&mut Collider, &mut Physics, &mut Mesh, &mut Position)>::query();
for (collider, physics, mesh, position) in query.iter_mut(world) {

View File

@@ -20,7 +20,6 @@ use rapier3d::parry::motion::RigidMotionComposition;
use wgpu::util::DeviceExt;
use wgpu::{BackendBit, BindGroup, BindGroupLayout, Buffer, BufferBindingType, Device, FragmentState, Instance, Queue, Surface, SwapChain, SwapChainDescriptor, SwapChainFrame, TextureView, VertexState, CommandEncoder};
use winit_24::dpi::PhysicalSize;
use winit_24::platform::unix::x11::ffi::Time;
use winit_24::window::Window;
use crate::camera::{Camera, CameraController};

View File

@@ -1,4 +1,6 @@
use std::cell::RefCell;
use std::iter::Chain;
use std::slice::Iter;
use std::sync::{Arc, Mutex};
use std::thread::current;
use std::time::Duration;
@@ -18,22 +20,24 @@ use legion::world::SubWorld;
use legion::*;
use rapier3d::parry::motion::RigidMotionComposition;
use wgpu::util::DeviceExt;
use wgpu::{BackendBit, BindGroup, BindGroupLayout, Buffer, BufferBindingType, Device, FragmentState, Instance, Queue, Surface, SwapChain, SwapChainDescriptor, SwapChainFrame, TextureView, VertexState, CommandEncoder};
use wgpu::{
BackendBit, BindGroup, BindGroupLayout, Buffer, BufferBindingType, CommandEncoder, Device,
FragmentState, Instance, Queue, Surface, SwapChain, SwapChainDescriptor, SwapChainFrame,
TextureView, VertexState,
};
use winit_24::dpi::PhysicalSize;
use winit_24::platform::unix::x11::ffi::Time;
use winit_24::window::Window;
use crate::camera::{Camera, CameraController};
use crate::components::{Mesh, Position, RangeCopy, LoopState, ImguiWindow};
use crate::components::{ImguiWindow, LoopState, Mesh, Position, RangeCopy};
use crate::current_ui;
use crate::geometry::{load_obj, Vertex};
use crate::imgui_supp::imgui_support::{ImguiContext, ImguiPlatform};
use crate::light::{DirectionalLight, LightRaw};
use crate::render::state::{RenderState};
use crate::render::{push_debug_group_checked, insert_debug_marker_checked, pop_debug_group_checked, EntityUniforms};
use std::iter::Chain;
use std::slice::Iter;
use crate::render::state::RenderState;
use crate::render::{
insert_debug_marker_checked, pop_debug_group_checked, push_debug_group_checked, EntityUniforms,
};
#[system]
#[write_component(Camera)]
@@ -55,22 +59,51 @@ pub fn imgui_prepare(
unsafe { crate::CURRENT_UI = Some(std::mem::transmute(imgui_context.frame())) }
}
fn run_imgui_render_step<G: 'static + Sized + Send + Sync>(world: &mut SubWorld, ui: &Ui) {
let mut component_query = <(&G)>::query();
let mut window_query = <(&ImguiWindow<G>)>::query();
let mut window_data = None;
let mut window_func = None;
for (window) in window_query.iter(world) {
window_data = Some((window.window)());
window_func = Some(window.func);
}
if window_data.is_some() {
let mut v = Vec::new();
for (component_state) in component_query.iter(world) {
v.push(component_state)
}
window_data
.unwrap()
.build(&ui, || (window_func.unwrap())(ui, v));
}
}
/// Go through each "global" window-data component and render it's data
#[system]
#[write_component(ImguiWindow<ImguiPerformanceProfilerLine>)]
#[write_component(ImguiPerformanceProfilerLine)]
#[write_component(ImguiWindow<ImguiGenericOutputLine>)]
#[write_component(ImguiGenericOutputLine)]
pub fn render_imgui(world: &mut SubWorld, #[resource] loop_state: &mut LoopState) {
let ui = unsafe { crate::current_ui().unwrap() };
let mut query = <(&ImguiPerformanceProfilerLine, &mut ImguiWindow<ImguiPerformanceProfilerLine>)>::query();
for (state, mut window) in query.iter_mut(world) {
let new_window = (window.window)();
new_window.build(&ui, || { (window.func)(ui, state) });
}
// Pull out the window associated with this type, and render each of the components in the sytem
run_imgui_render_step::<ImguiGenericOutputLine>(world, &ui);
run_imgui_render_step::<ImguiPerformanceProfilerLine>(world, &ui);
}
// This would be the shared state for all imgui generic output things
pub struct ImguiGenericOutputLine {
pub label: String,
}
impl ImguiGenericOutputLine {
pub fn new(label: String) -> ImguiGenericOutputLine {
ImguiGenericOutputLine { label }
}
}
// This would be the shared state for all imgui performance window things
pub struct ImguiPerformanceProfilerLine {
@@ -83,7 +116,6 @@ pub struct ImguiPerformanceProfilerLine {
impl ImguiPerformanceProfilerLine {
fn add_sample(&mut self, sample: f32) {
self.list_of_fps[self.index] = sample;
if self.index >= 399 {
@@ -105,7 +137,16 @@ impl ImguiPerformanceProfilerLine {
pub fn current_average_label(&self) -> (f32, String) {
let (left, right) = self.list_of_fps.split_at(self.index);
((left.iter().rev().chain(right.iter().rev()).take(50).sum::<f32>() / 50.0), "FPS".to_string())
(
(left
.iter()
.rev()
.chain(right.iter().rev())
.take(50)
.sum::<f32>()
/ 50.0),
"FPS".to_string(),
)
}
pub fn new(label: String) -> ImguiPerformanceProfilerLine {
@@ -114,11 +155,22 @@ impl ImguiPerformanceProfilerLine {
list_of_fps: [0.0; 400],
index: 0,
scale_min: 0.0,
scale_max: 0.0
scale_max: 0.0,
}
}
}
#[system]
#[write_component(ImguiPerformanceProfilerLine)]
pub fn render_performance_flag(world: &mut SubWorld, #[resource] loop_state: &mut LoopState) {
let delta_time = loop_state.delta_time.as_secs_f32();
let mut query = <(&mut ImguiPerformanceProfilerLine)>::query();
for (mut profiler) in query.iter_mut(world) {
profiler.add_sample(delta_time);
}
}
#[system]
#[write_component(Camera)]
#[write_component(Position)]
@@ -134,20 +186,12 @@ pub fn render_test(
#[resource] imgui_context: &mut Arc<Mutex<ImguiContext>>,
#[resource] imgui_platform: &mut Arc<Mutex<ImguiPlatform>>,
) {
let mut query = <(&mut ImguiPerformanceProfilerLine)>::query();
for (mut profiler) in query.iter_mut(world) {
let delta_time = loop_state.delta_time.as_secs_f32();
profiler.add_sample(delta_time);
}
let mut encoder = renderer
.device
.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
push_debug_group_checked("start render function", &mut encoder);
let frame = renderer.get_current_frame();
// Update the camera uniform buffers, need to make it support selection of
@@ -207,8 +251,10 @@ pub fn render_test(
let mut query = <(&mut DirectionalLight, &mut Position)>::query();
for (i, (light, pos)) in query.iter_mut(world).enumerate() {
insert_debug_marker_checked(&format!("shadow pass {} (light at position {:?})", i, pos), &mut encoder);
insert_debug_marker_checked(
&format!("shadow pass {} (light at position {:?})", i, pos),
&mut encoder,
);
// The light uniform buffer already has the projection,
// let's just copy it over to the shadow uniform buffer.
@@ -222,7 +268,6 @@ pub fn render_test(
insert_debug_marker_checked("render entities", &mut encoder);
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("render pass"),
color_attachments: &[],
@@ -298,7 +343,6 @@ pub fn render_test(
let ui = unsafe { crate::current_ui().unwrap() };
// ui.show_demo_window(&mut true);
imgui_platform.prepare_render(&ui, &winit_window);

View File

@@ -22,7 +22,7 @@ use crate::components::{Collider, ImguiWindow, LoopState, Mesh, Physics, Positio
use crate::geometry::RawMesh;
use crate::physics::state::PhysicsState;
use crate::render::state::RenderState;
use crate::render::system::ImguiPerformanceProfilerLine;
use crate::render::system::{ImguiGenericOutputLine, ImguiPerformanceProfilerLine};
use crate::runtime::state::RuntimeState;
pub fn quad_color(color: [f32; 4]) -> [[f32; 4]; 4] {
@@ -38,35 +38,38 @@ pub fn runtime_load(
) {
runtime_state.preload_meshes(PathBuf::from("./resources"));
let entity: Entity = cmd.push((
ImguiWindow {
let entity: Entity = cmd.push((ImguiWindow {
// a window that does everything for the performance profiler
window: || {
imgui::Window::new(im_str!("Performance Profiler"))
.size([400.0, 500.0], Condition::FirstUseEver)
.position([50.0, 50.0], Condition::FirstUseEver)
.size([400.0, 200.0], Condition::FirstUseEver)
.position([10.0, 10.0], Condition::FirstUseEver)
},
func: |ui: &Ui, a: &ImguiPerformanceProfilerLine| {
ui.text(im_str!("Performance Graph"));
func: |ui: &Ui, a: Vec<&ImguiPerformanceProfilerLine>| {
// ui.text(im_str!("Performance Graph"));
let draw_list = ui.get_window_draw_list();
let top_left = ui.cursor_screen_pos();
let region_size = ui.content_region_avail();
let region_size = [region_size[0] * 0.80, region_size[1]];
// Fill rect
let qcolor = quad_color([0.5, 0.5, 1.0, 0.1]);
draw_list.add_rect_filled_multicolor(
top_left,
[top_left[0] + (region_size[0] - region_size[0]/5.0), top_left[1] + region_size[1]],
[top_left[0] + (region_size[0]), top_left[1] + region_size[1]],
qcolor[0],
qcolor[1],
qcolor[2],
qcolor[3],
);
let x_scale = (region_size[0] - region_size[0]/5.0) / 400.0;
let y_scale = region_size[1] / a.scale_max;
a.iter_data_from_head()
for profiler_line in a {
let x_scale = (region_size[0]) / 400.0;
let y_scale = region_size[1] / profiler_line.scale_max;
profiler_line
.iter_data_from_head()
.fold((0, 0.0f32), |accum, &fps_val| {
let x1 = accum.0 as f32 * x_scale + top_left[0];
let y1 = top_left[1] + region_size[1] - accum.1 * y_scale;
@@ -75,19 +78,42 @@ pub fn runtime_load(
let p1 = [x1, y1];
let p2 = [x2, y2];
draw_list
.add_line(p1, p2, [1.0, 1.0, 0.0])
.add_line(p1, p2, [1.0, 1.0, 0.0, 0.8])
.thickness(1.0)
.build();
(accum.0 + 1, fps_val)
});
let text_x = (region_size[0] - region_size[0]/5.0 + 75.0);
let text_y = top_left[1] + region_size[1] - a.current_average_label().0 * y_scale;
draw_list.add_text([text_x, text_y], [1.0,1.0,0.0,1.0], format!("{} {:.0}",a.current_average_label().1, 1.0/a.current_average_label().0));
let text_x = (region_size[0] + top_left[0]);
let text_y = top_left[1] + region_size[1]
- profiler_line.current_average_label().0 * y_scale;
draw_list.add_text(
[text_x, text_y],
[1.0, 1.0, 0.0, 1.0],
format!(
"{} {:.0}",
profiler_line.current_average_label().1,
1.0 / profiler_line.current_average_label().0
),
);
}
},
},));
let entity: Entity = cmd.push((ImguiPerformanceProfilerLine::new("RenderFPS".to_string()),));
let entity: Entity = cmd.push((ImguiWindow {
// a window that does everything for the performance profiler
window: || {
imgui::Window::new(im_str!("Generic Output"))
.size([400.0, 500.0], Condition::FirstUseEver)
.position([50.0, 250.0], Condition::FirstUseEver)
},
ImguiPerformanceProfilerLine::new("RenderFFS".to_string()),
));
func: |ui: &Ui, a: Vec<&ImguiGenericOutputLine>| {
for label in a {
ui.text(im_str!("{}", label.label));
}
},
},));
}
#[system]
@@ -149,6 +175,7 @@ pub fn runtime_spawn(
collider: collider,
collider_handle: None,
},
ImguiGenericOutputLine::new("wahoo! from a physics entity".to_string()),
));
}
"Terrain" => {
@@ -214,6 +241,7 @@ pub fn runtime_spawn(
pitch: Rad(PI / 2.0 + 25.0),
},
CameraController::new(5.0, 1.0),
ImguiGenericOutputLine::new("wahoo! from a camera".to_string()),
));
}
"Light" => {