This commit is contained in:
Quaternions 2023-10-24 16:53:40 -07:00
parent 4655f43f5f
commit 83cdb0413c
5 changed files with 65 additions and 61 deletions

View File

@ -1,6 +1,6 @@
struct Context{ struct Context<'a>{
device:wgpu::Device, device:&'a wgpu::Device,
queue:wgpu::Queue, queue:&'a wgpu::Queue,
} }
impl Context{ impl Context{

View File

@ -61,7 +61,7 @@ fn load_file(path: std::path::PathBuf)->Option<model::IndexedModelInstances>{
} }
} }
fn default_models()->model::IndexedModelInstances{ pub fn default_models()->model::IndexedModelInstances{
let mut indexed_models = Vec::new(); let mut indexed_models = Vec::new();
indexed_models.append(&mut model::generate_indexed_model_list_from_obj(obj::ObjData::load_buf(&include_bytes!("../models/teslacyberv3.0.obj")[..]).unwrap(),glam::Vec4::ONE)); indexed_models.append(&mut model::generate_indexed_model_list_from_obj(obj::ObjData::load_buf(&include_bytes!("../models/teslacyberv3.0.obj")[..]).unwrap(),glam::Vec4::ONE));
indexed_models.push(primitives::unit_sphere()); indexed_models.push(primitives::unit_sphere());
@ -119,8 +119,6 @@ fn default_models()->model::IndexedModelInstances{
fn main(){ fn main(){
let title=format!("Strafe Client v{}",env!("CARGO_PKG_VERSION")).as_str(); let title=format!("Strafe Client v{}",env!("CARGO_PKG_VERSION")).as_str();
let context=setup_context::setup(title); let context=setup::setup(title);
let run=run::RunState::init();//new context.start();//creates and runs a run context
run.replace_models(&context,default_models());
context.start(run);
} }

View File

@ -21,6 +21,15 @@ pub enum InputInstruction {
} }
pub struct Context{ pub struct Context{
//Ideally the graphics thread worker description is:
/*
WorkerDescription{
input:Immediate,
output:Realtime(PoolOrdering::Ordered(3)),
}
*/
//up to three frames in flight, dropping new frame requests when all three are busy, and dropping output frames when one renders out of order
graphics_thread:crate::worker::INWorker<crate::graphics::GraphicsInstruction>,
} }
impl Context{ impl Context{
pub fn new(user_settings:&crate::settings::UserSettings,indexed_model_instances:&crate::model::IndexedModelInstances){ pub fn new(user_settings:&crate::settings::UserSettings,indexed_model_instances:&crate::model::IndexedModelInstances){

View File

@ -1,85 +1,78 @@
use crate::physics::PhysicsInstruction; use crate::physics::PhysicsInstruction;
use crate::render_thread::InputInstruction; use crate::physics_context::InputInstruction;
use crate::instruction::{TimedInstruction, InstructionConsumer}; use crate::instruction::TimedInstruction;
pub enum RunInstruction{ pub enum RunInstruction{
Resize(winit::dpi::PhysicalSize<u32>), Resize(winit::dpi::PhysicalSize<u32>),
WindowEvent(winit::event::WindowEvent), WindowEvent(winit::event::WindowEvent),
DeviceEvent(winit::event::DeviceEvent), DeviceEvent(winit::event::DeviceEvent),
RequestRedraw,
Render, Render,
} }
pub struct RunState{ //holds thread handles to dispatch to
manual_mouse_lock:bool, struct RunContext{
mouse:std::sync::Arc<std::sync::Mutex<crate::physics::MouseState>>,
user_settings:crate::settings::UserSettings,
//Ideally the graphics thread worker description is:
/*
WorkerDescription{
input:Immediate,
output:Realtime(PoolOrdering::Ordered(3)),
}
*/
//up to three frames in flight, dropping new frame requests when all three are busy, and dropping output frames when one renders out of order
graphics_thread:crate::worker::INWorker<crate::graphics::GraphicsInstruction>,
physics_thread:crate::worker::QNWorker<TimedInstruction<InputInstruction>>, physics_thread:crate::worker::QNWorker<TimedInstruction<InputInstruction>>,
} }
impl RunState { pub struct RunContextSetup{
fn init() -> Self { manual_mouse_lock:bool,
mouse:crate::physics::MouseState,//std::sync::Arc<std::sync::Mutex<>>
user_settings:crate::settings::UserSettings,
window:winit::window::Window,
physics:crate::physics::PhysicsState,
graphics:crate::graphics::GraphicsState,
}
impl RunContextSetup {
pub fn new(context:&crate::setup::SetupContext,window:winit::window::Window)->Self{
//wee //wee
let user_settings=crate::settings::read_user_settings(); let user_settings=crate::settings::read_user_settings();
let mut graphics=GraphicsState::new(); let args:Vec<String>=std::env::args().collect();
let indexed_model_instances=if args.len()==2{
crate::load_file(std::path::PathBuf::from(&args[1]))
}else{
None
}.unwrap_or(crate::default_models());
let mut graphics=crate::graphics::GraphicsState::new(&context.device,&context.queue);
graphics.load_user_settings(&user_settings); graphics.load_user_settings(&user_settings);
graphics.generate_models(&context.device,&context.queue,indexed_model_instances);
//how to multithread let mut physics=crate::physics::PhysicsState::default();
physics.load_user_settings(&user_settings);
//1. build
physics.generate_models(&indexed_model_instances); physics.generate_models(&indexed_model_instances);
//2. move Self{
let physics_thread=physics.into_worker();
//3. forget
let mut state=Self{
manual_mouse_lock:false, manual_mouse_lock:false,
mouse:physics::MouseState::default(), mouse:crate::physics::MouseState::default(),
user_settings, user_settings,
window,
graphics, graphics,
physics_thread, physics,
};
state.generate_model_graphics(&device,&queue,indexed_model_instances);
let args:Vec<String>=std::env::args().collect();
if args.len()==2{
let indexed_model_instances=load_file(std::path::PathBuf::from(&args[1]));
state.render_thread=RenderThread::new(user_settings,indexed_model_instances);
} }
return state;
} }
fn window_event(&mut self, time:crate::integer::Time, event: winit::event::WindowEvent) { fn window_event(&mut self, time:crate::integer::Time, event: winit::event::WindowEvent) {
match event { match event {
winit::event::WindowEvent::DroppedFile(path)=>{ winit::event::WindowEvent::DroppedFile(path)=>{
let sender=self.sender.clone();//mpsc
std::thread::spawn(move ||{ std::thread::spawn(move ||{
let indexed_model_instances=load_file(path); let indexed_model_instances=crate::load_file(path);
self.render_thread.send(Instruction::Die(indexed_model_instances)); sender.send(Instruction::Die(indexed_model_instances));
}); });
}, },
winit::event::WindowEvent::Focused(state)=>{ winit::event::WindowEvent::Focused(state)=>{
//pause unpause //pause unpause
//recalculate pressed keys on focus //recalculate pressed keys on focus
}, },
winit::event::WindowEvent::KeyboardInput { winit::event::WindowEvent::KeyboardInput{
input:winit::event::KeyboardInput{state, virtual_keycode,..}, input:winit::event::KeyboardInput{state,virtual_keycode,..},
.. ..
}=>{ }=>{
let s=match state { let s=match state{
winit::event::ElementState::Pressed => true, winit::event::ElementState::Pressed=>true,
winit::event::ElementState::Released => false, winit::event::ElementState::Released=>false,
}; };
match virtual_keycode{ match virtual_keycode{
Some(winit::event::VirtualKeyCode::Tab)=>{ Some(winit::event::VirtualKeyCode::Tab)=>{
@ -192,13 +185,16 @@ impl RunState {
} }
} }
pub fn into_worker(self,mut setup_context:crate::setup_context::SetupContext)->crate::worker::QNWorker<TimedInstruction<RunInstruction>>{ pub fn into_worker(self,mut setup_context:crate::setup_context::SetupContext,)->crate::worker::QNWorker<TimedInstruction<RunInstruction>>{
//create child context //create child context
let physics_context=crate::physics_context::Context::new(indexed_models,&setup_context);//this needs all the context for graphics_context too let physics_context=crate::physics_context::Context::new(indexed_models,&setup_context);//this needs all the context for graphics_context too
let physics_thread=physics_context.into_worker(); let physics_thread=physics_context.into_worker();
// //
crate::worker::QNWorker::new(move |ins:TimedInstruction<RunInstruction>|{ crate::worker::QNWorker::new(move |ins:TimedInstruction<RunInstruction>|{
match ins.instruction{ match ins.instruction{
RunInstruction::RequestRedraw=>{
self.window.request_redraw();
}
RunInstruction::WindowEvent(window_event)=>{ RunInstruction::WindowEvent(window_event)=>{
self.window_event(ins.time,window_event); self.window_event(ins.time,window_event);
}, },

View File

@ -208,7 +208,7 @@ pub fn setup(title:&str)->SetupContextSetup{
} }
} }
struct SetupContextSetup{ pub struct SetupContextSetup{
window:winit::window::Window, window:winit::window::Window,
event_loop:winit::event_loop::EventLoop<()>, event_loop:winit::event_loop::EventLoop<()>,
partial_context:SetupContextPartial4, partial_context:SetupContextPartial4,
@ -224,12 +224,13 @@ impl SetupContextSetup{
self.partial_context.configure_surface(&size), self.partial_context.configure_surface(&size),
) )
} }
pub fn start(self,mut run:crate::run::RunState){ pub fn start(self){
let (window,event_loop,setup_context)=self.into_split(); let (window,event_loop,setup_context)=self.into_split();
//dedicated thread to ping request redraw back and resize the window doesn't seem logical //dedicated thread to ping request redraw back and resize the window doesn't seem logical
//physics and graphics render thread let run=crate::run::RunContextSetup::new(&setup_context,window);
//the thread that spawns the physics thread
let run_thread=run.into_worker(setup_context); let run_thread=run.into_worker(setup_context);
println!("Entering render loop..."); println!("Entering render loop...");
@ -243,7 +244,7 @@ impl SetupContextSetup{
// }; // };
match event{ match event{
winit::event::Event::AboutToWait=>{ winit::event::Event::AboutToWait=>{
window.request_redraw(); run_thread.send(TimedInstruction{time,instruction:RunInstruction::RequestRedraw});
} }
winit::event::Event::WindowEvent { winit::event::Event::WindowEvent {
event: event:
@ -271,12 +272,12 @@ impl SetupContextSetup{
|winit::event::WindowEvent::CloseRequested=>{ |winit::event::WindowEvent::CloseRequested=>{
elwt.exit(); elwt.exit();
} }
_=>{
run_thread.send(TimedInstruction{time,instruction:RunInstruction::WindowEvent(event)});
}
winit::event::WindowEvent::RedrawRequested=>{ winit::event::WindowEvent::RedrawRequested=>{
run_thread.send(TimedInstruction{time,instruction:RunInstruction::Render}); run_thread.send(TimedInstruction{time,instruction:RunInstruction::Render});
} }
_=>{
run_thread.send(TimedInstruction{time,instruction:RunInstruction::WindowEvent(event)});
}
}, },
winit::event::Event::DeviceEvent{ winit::event::Event::DeviceEvent{
event, event,