This commit is contained in:
2026-03-10 07:01:33 -07:00
parent 1c6461464f
commit 839c59ea54
4 changed files with 54 additions and 22 deletions

View File

@@ -35,22 +35,25 @@ struct Playback{
}
pub struct PlayerWorker<'a>{
surface:wgpu::Surface<'a>,
graphics_thread:Graphics,
surface:wgpu::Surface<'a>,
config:wgpu::SurfaceConfiguration,
playback:Option<Playback>,
}
impl<'a> PlayerWorker<'a>{
pub fn new(
surface:wgpu::Surface<'a>,
graphics_thread:Graphics,
surface:wgpu::Surface<'a>,
config:wgpu::SurfaceConfiguration,
)->Self{
Self{
surface,
graphics_thread,
surface,
config,
playback:None,
}
}
pub fn send(&mut self,device: &wgpu::Device,queue:&wgpu::Queue,ins:TimedInstruction<Instruction,SessionTime>){
pub fn send(&mut self,device:&wgpu::Device,queue:&wgpu::Queue,ins:TimedInstruction<Instruction,SessionTime>){
match ins.instruction{
Instruction::SessionControl(SessionControlInstruction::SetPaused(paused))=>if let Some(playback)=&mut self.playback{
playback.playback_head.set_paused(ins.time,paused);

View File

@@ -13,15 +13,20 @@ pub struct WindowContext<'a>{
simulation_paused:bool,
window:&'a winit::window::Window,
physics_thread:PlayerWorker<'a>,
device:wgpu::Device,
queue:wgpu::Queue,
}
impl WindowContext<'_>{
fn phys(&mut self,ins:TimedInstruction<crate::player::Instruction,strafesnet_common::session::Time>){
self.physics_thread.send(&self.device,&self.queue,ins);
}
fn window_event(&mut self,time:SessionTime,event:winit::event::WindowEvent){
match event{
winit::event::WindowEvent::DroppedFile(path)=>{
match crate::file::load(path.as_path()){
Ok(LoadFormat::Map(map))=>self.physics_thread.send(TimedInstruction{time,instruction:PhysicsWorkerInstruction::ChangeMap(map)}),
Ok(LoadFormat::Bot(bot))=>self.physics_thread.send(TimedInstruction{time,instruction:PhysicsWorkerInstruction::LoadReplay(bot)}),
Ok(LoadFormat::Map(map))=>self.phys(TimedInstruction{time,instruction:PhysicsWorkerInstruction::ChangeMap(map)}),
Ok(LoadFormat::Bot(bot))=>self.phys(TimedInstruction{time,instruction:PhysicsWorkerInstruction::LoadReplay(bot)}),
Err(e)=>println!("Failed to load file: {e}"),
}
},
@@ -31,7 +36,7 @@ impl WindowContext<'_>{
return;
}
//pause unpause
self.physics_thread.send(TimedInstruction{
self.phys(TimedInstruction{
time,
instruction:PhysicsWorkerInstruction::SessionControl(SessionControlInstruction::SetPaused(!state)),
});
@@ -74,7 +79,7 @@ impl WindowContext<'_>{
},
_=>None,
}{
self.physics_thread.send(TimedInstruction{
self.phys(TimedInstruction{
time,
instruction,
});
@@ -83,7 +88,7 @@ impl WindowContext<'_>{
}
},
winit::event::WindowEvent::Resized(size)=>{
self.physics_thread.send(
self.phys(
TimedInstruction{
time,
instruction:PhysicsWorkerInstruction::Resize(size)
@@ -92,7 +97,7 @@ impl WindowContext<'_>{
},
winit::event::WindowEvent::RedrawRequested=>{
self.window.request_redraw();
self.physics_thread.send(
self.phys(
TimedInstruction{
time,
instruction:PhysicsWorkerInstruction::Render
@@ -130,9 +135,12 @@ impl WindowContext<'_>{
simulation_paused:false,
window,
physics_thread:crate::player::PlayerWorker::new(
surface,
graphics,
surface,
config,
),
device,
queue,
}
}
}

View File

@@ -62,8 +62,7 @@ pub fn setup_and_start(){
let time=SessionTime::raw(i*SessionTime::ONE_SECOND.get()/target_framerate as i64);
playback_head.advance_time(&bot,time);
let (pos,angles)=playback_head.get_position_angles(&bot,time);
let camera=strafesnet_graphics::graphics::view_inv(pos,angles);
wgpu_state.render(camera);
wgpu_state.render(pos,angles);
let res = unsafe {
encoder
@@ -233,14 +232,14 @@ impl WgpuState {
self.graphics.change_map(&self.device,&self.queue,map);
}
fn render(&mut self,camera:glam::Mat4) {
fn render(&mut self,pos:glam::Vec3,angles:glam::Vec2) {
let mut encoder = self
.device
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("wgpu encoder"),
});
self.graphics.encode_commands(&mut encoder,&self.graphics_texture_view,camera);
self.graphics.encode_commands(&mut encoder,&self.graphics_texture_view,pos,angles);
self.y_renderer.render(&mut encoder);
self.uv_renderer.render(&mut encoder);

View File

@@ -23,10 +23,13 @@ impl From<ToSurfaceTarget> for wgpu::SurfaceTarget<'static>{
pub struct Graphics{
graphics:graphics::Graphics,
surface:wgpu::Surface<'static>,
config:wgpu::SurfaceConfiguration,
device:wgpu::Device,
queue:wgpu::Queue,
}
#[wasm_bindgen]
pub async fn setup_graphics(canvas:web_sys::HtmlCanvasElement)->Result<Graphics,JsError>{
let size=(canvas.width(),canvas.height());
let size=glam::uvec2(canvas.width(),canvas.height());
let instance_desc=wgpu::InstanceDescriptor::from_env_or_default();
let instance=wgpu::util::new_instance_with_webgpu_detection(&instance_desc).await;
@@ -37,10 +40,13 @@ pub async fn setup_graphics(canvas:web_sys::HtmlCanvasElement)->Result<Graphics,
compatible_surface:Some(&surface),
}).await.map_err(|e|JsError::new(&e.to_string()))?;
let (device,queue)=setup::step4::request_device(&adapter).await.map_err(|e|JsError::new(&e.to_string()))?;
let config=setup::step5::configure_surface(&adapter,&device,&surface,size).map_err(|e|JsError::new(&e.to_string()))?;
let config=setup::step5::configure_surface(&adapter,&device,&surface,(size.x,size.y)).map_err(|e|JsError::new(&e.to_string()))?;
Ok(Graphics{
graphics:graphics::Graphics::new(device,queue,config),
surface:surface,
graphics:graphics::Graphics::new(&device,&queue,size,config.view_formats[0]),
surface,
config,
device,
queue,
})
}
#[wasm_bindgen]
@@ -49,15 +55,31 @@ impl Graphics{
pub fn render(&mut self,bot:&CompleteBot,head:&PlaybackHead,time:f64){
let time=time::from_float(time).unwrap();
let (pos,angles)=head.head.get_position_angles(&bot.bot,time);
self.graphics.render(&self.surface,pos,angles);
let frame=match self.surface.get_current_texture(){
Ok(frame)=>frame,
Err(_)=>{
self.surface.configure(&self.device,&self.config);
self.surface
.get_current_texture()
.expect("Failed to acquire next surface texture!")
}
};
let view=frame.texture.create_view(&wgpu::TextureViewDescriptor{
format:Some(self.config.view_formats[0]),
..wgpu::TextureViewDescriptor::default()
});
let mut encoder=self.device.create_command_encoder(&wgpu::CommandEncoderDescriptor{label:None});
self.graphics.encode_commands(&mut encoder,&view,pos,angles);
self.queue.submit([encoder.finish()]);
frame.present();
}
#[wasm_bindgen]
pub fn resize(&mut self,width:u32,height:u32,fov_slope_x:f32,fov_slope_y:f32){
self.graphics.resize(&self.surface,[width,height].into(),[fov_slope_x as f32,fov_slope_y as f32].into());
self.graphics.resize(&self.device,[width,height].into(),[fov_slope_x as f32,fov_slope_y as f32].into());
}
#[wasm_bindgen]
pub fn change_map(&mut self,map:&CompleteMap){
self.graphics.change_map(&map.map);
self.graphics.change_map(&self.device,&self.queue,&map.map);
}
}