separate graphics state from global state

This commit is contained in:
Quaternions 2023-10-03 19:43:41 -07:00
parent 7a8de938af
commit 50e9152ee2
2 changed files with 60 additions and 42 deletions

View File

@ -553,6 +553,10 @@ impl Body {
} }
impl PhysicsState { impl PhysicsState {
pub fn clear(&mut self){
self.models.clear();
self.modes.clear();
}
//tickless gaming //tickless gaming
pub fn run(&mut self, time_limit:TIME){ pub fn run(&mut self, time_limit:TIME){
//prepare is ommitted - everything is done via instructions. //prepare is ommitted - everything is done via instructions.

View File

@ -38,16 +38,13 @@ pub struct GraphicsBindGroups {
skybox_texture: wgpu::BindGroup, skybox_texture: wgpu::BindGroup,
} }
pub struct GraphicsPipelines { pub struct GraphicsPipelines{
skybox: wgpu::RenderPipeline, skybox: wgpu::RenderPipeline,
model: wgpu::RenderPipeline, model: wgpu::RenderPipeline,
} }
pub struct GraphicsData { pub struct GraphicsState{
start_time: std::time::Instant,
screen_size: (u32, u32), screen_size: (u32, u32),
manual_mouse_lock:bool,
physics: body::PhysicsState,
pipelines: GraphicsPipelines, pipelines: GraphicsPipelines,
bind_groups: GraphicsBindGroups, bind_groups: GraphicsBindGroups,
bind_group_layouts: GraphicsBindGroupLayouts, bind_group_layouts: GraphicsBindGroupLayouts,
@ -59,7 +56,20 @@ pub struct GraphicsData {
staging_belt: wgpu::util::StagingBelt, staging_belt: wgpu::util::StagingBelt,
} }
impl GraphicsData { impl GraphicsState{
pub fn clear(&mut self){
self.models.clear();
}
}
pub struct GlobalState{
start_time: std::time::Instant,
manual_mouse_lock:bool,
graphics:GraphicsState,
physics:body::PhysicsState,
}
impl GlobalState{
const DEPTH_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Depth24Plus; const DEPTH_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Depth24Plus;
fn create_depth_texture( fn create_depth_texture(
@ -247,9 +257,9 @@ impl GraphicsData {
//.into_iter() the modeldata vec so entities can be /moved/ to models.entities //.into_iter() the modeldata vec so entities can be /moved/ to models.entities
let mut model_count=0; let mut model_count=0;
let mut instance_count=0; let mut instance_count=0;
let uniform_buffer_binding_size=<GraphicsData as framework::Example>::required_limits().max_uniform_buffer_binding_size as usize; let uniform_buffer_binding_size=<GlobalState as framework::Example>::required_limits().max_uniform_buffer_binding_size as usize;
let chunk_size=uniform_buffer_binding_size/MODEL_BUFFER_SIZE_BYTES; let chunk_size=uniform_buffer_binding_size/MODEL_BUFFER_SIZE_BYTES;
self.models.reserve(models.len()); self.graphics.models.reserve(models.len());
for model in models.into_iter() { for model in models.into_iter() {
instance_count+=model.instances.len(); instance_count+=model.instances.len();
for instances_chunk in model.instances.rchunks(chunk_size){ for instances_chunk in model.instances.rchunks(chunk_size){
@ -264,13 +274,13 @@ impl GraphicsData {
Some(texture_id)=>{ Some(texture_id)=>{
match double_map.get(&texture_id){ match double_map.get(&texture_id){
Some(&mapped_texture_id)=>&texture_views[mapped_texture_id as usize], Some(&mapped_texture_id)=>&texture_views[mapped_texture_id as usize],
None=>&self.temp_squid_texture_view, None=>&self.graphics.temp_squid_texture_view,
} }
}, },
None=>&self.temp_squid_texture_view, None=>&self.graphics.temp_squid_texture_view,
}; };
let model_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor { let model_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &self.bind_group_layouts.model, layout: &self.graphics.bind_group_layouts.model,
entries: &[ entries: &[
wgpu::BindGroupEntry { wgpu::BindGroupEntry {
binding: 0, binding: 0,
@ -282,7 +292,7 @@ impl GraphicsData {
}, },
wgpu::BindGroupEntry { wgpu::BindGroupEntry {
binding: 2, binding: 2,
resource: wgpu::BindingResource::Sampler(&self.samplers.repeat), resource: wgpu::BindingResource::Sampler(&self.graphics.samplers.repeat),
}, },
], ],
label: Some(format!("Model{} Bind Group",model_count).as_str()), label: Some(format!("Model{} Bind Group",model_count).as_str()),
@ -293,7 +303,7 @@ impl GraphicsData {
usage: wgpu::BufferUsages::VERTEX, usage: wgpu::BufferUsages::VERTEX,
}); });
//all of these are being moved here //all of these are being moved here
self.models.push(ModelGraphics{ self.graphics.models.push(ModelGraphics{
instances:instances_chunk.to_vec(), instances:instances_chunk.to_vec(),
vertex_buf, vertex_buf,
entities: model.entities.iter().map(|indices|{ entities: model.entities.iter().map(|indices|{
@ -315,7 +325,7 @@ impl GraphicsData {
println!("Texture References={}",num_textures); println!("Texture References={}",num_textures);
println!("Textures Loaded={}",texture_views.len()); println!("Textures Loaded={}",texture_views.len());
println!("Indexed Models={}",indexed_models_len); println!("Indexed Models={}",indexed_models_len);
println!("Graphics Objects: {}",self.models.len()); println!("Graphics Objects: {}",self.graphics.models.len());
println!("Graphics Instances: {}",instance_count); println!("Graphics Instances: {}",instance_count);
} }
} }
@ -351,7 +361,7 @@ fn to_uniform_data(camera: &body::Camera, pos: glam::Vec3) -> [f32; 16 * 3 + 4]
raw raw
} }
impl framework::Example for GraphicsData { impl framework::Example for GlobalState {
fn optional_features() -> wgpu::Features { fn optional_features() -> wgpu::Features {
wgpu::Features::TEXTURE_COMPRESSION_ASTC wgpu::Features::TEXTURE_COMPRESSION_ASTC
| wgpu::Features::TEXTURE_COMPRESSION_ETC2 | wgpu::Features::TEXTURE_COMPRESSION_ETC2
@ -758,11 +768,8 @@ impl framework::Example for GraphicsData {
let depth_view = Self::create_depth_texture(config, device); let depth_view = Self::create_depth_texture(config, device);
let mut graphics=GraphicsData { let graphics=GraphicsState {
manual_mouse_lock:false,
start_time: Instant::now(),
screen_size: (config.width,config.height), screen_size: (config.width,config.height),
physics,
pipelines:GraphicsPipelines{ pipelines:GraphicsPipelines{
skybox:sky_pipeline, skybox:sky_pipeline,
model:model_pipeline model:model_pipeline
@ -780,21 +787,28 @@ impl framework::Example for GraphicsData {
temp_squid_texture_view: squid_texture_view, temp_squid_texture_view: squid_texture_view,
}; };
let mut state=GlobalState{
start_time:Instant::now(),
manual_mouse_lock:false,
graphics,
physics,
};
let indexed_model_instances=model::IndexedModelInstances{ let indexed_model_instances=model::IndexedModelInstances{
textures:Vec::new(), textures:Vec::new(),
models:indexed_models, models:indexed_models,
spawn_point:glam::Vec3::Y*50.0, spawn_point:glam::Vec3::Y*50.0,
modes:Vec::new(), modes:Vec::new(),
}; };
graphics.generate_model_physics(&indexed_model_instances); state.generate_model_physics(&indexed_model_instances);
graphics.generate_model_graphics(&device,&queue,indexed_model_instances); state.generate_model_graphics(&device,&queue,indexed_model_instances);
let args:Vec<String>=std::env::args().collect(); let args:Vec<String>=std::env::args().collect();
if args.len()==2{ if args.len()==2{
graphics.load_file(std::path::PathBuf::from(&args[1]), device, queue); state.load_file(std::path::PathBuf::from(&args[1]), device, queue);
} }
return graphics; return state;
} }
fn load_file(&mut self,path: std::path::PathBuf, device: &wgpu::Device, queue: &wgpu::Queue){ fn load_file(&mut self,path: std::path::PathBuf, device: &wgpu::Device, queue: &wgpu::Queue){
@ -837,8 +851,8 @@ impl framework::Example for GraphicsData {
}{ }{
let spawn_point=indexed_model_instances.spawn_point; let spawn_point=indexed_model_instances.spawn_point;
//if generate_indexed_models succeeds, clear the previous ones //if generate_indexed_models succeeds, clear the previous ones
self.models.clear(); self.physics.clear();
self.physics.models.clear(); self.graphics.clear();
self.generate_model_physics(&indexed_model_instances); self.generate_model_physics(&indexed_model_instances);
self.generate_model_graphics(device,queue,indexed_model_instances); self.generate_model_graphics(device,queue,indexed_model_instances);
//manual reset //manual reset
@ -911,7 +925,7 @@ impl framework::Example for GraphicsData {
15=>{//Tab 15=>{//Tab
if s{ if s{
self.manual_mouse_lock=false; self.manual_mouse_lock=false;
match window.set_cursor_position(winit::dpi::PhysicalPosition::new(self.screen_size.0 as f32/2.0, self.screen_size.1 as f32/2.0)){ match window.set_cursor_position(winit::dpi::PhysicalPosition::new(self.graphics.screen_size.0 as f32/2.0, self.graphics.screen_size.1 as f32/2.0)){
Ok(())=>(), Ok(())=>(),
Err(e)=>println!("Could not set cursor position: {:?}",e), Err(e)=>println!("Could not set cursor position: {:?}",e),
} }
@ -951,7 +965,7 @@ impl framework::Example for GraphicsData {
delta,//these (f64,f64) are integers on my machine delta,//these (f64,f64) are integers on my machine
} => { } => {
if self.manual_mouse_lock{ if self.manual_mouse_lock{
match window.set_cursor_position(winit::dpi::PhysicalPosition::new(self.screen_size.0 as f32/2.0, self.screen_size.1 as f32/2.0)){ match window.set_cursor_position(winit::dpi::PhysicalPosition::new(self.graphics.screen_size.0 as f32/2.0, self.graphics.screen_size.1 as f32/2.0)){
Ok(())=>(), Ok(())=>(),
Err(e)=>println!("Could not set cursor position: {:?}",e), Err(e)=>println!("Could not set cursor position: {:?}",e),
} }
@ -986,8 +1000,8 @@ impl framework::Example for GraphicsData {
device: &wgpu::Device, device: &wgpu::Device,
_queue: &wgpu::Queue, _queue: &wgpu::Queue,
) { ) {
self.depth_view = Self::create_depth_texture(config, device); self.graphics.depth_view = Self::create_depth_texture(config, device);
self.screen_size = (config.width, config.height); self.graphics.screen_size = (config.width, config.height);
self.physics.camera.set_fov_aspect(1.0,(config.width as f32)/(config.height as f32)); self.physics.camera.set_fov_aspect(1.0,(config.width as f32)/(config.height as f32));
} }
@ -1007,19 +1021,19 @@ impl framework::Example for GraphicsData {
// update rotation // update rotation
let camera_uniforms = to_uniform_data(&self.physics.camera,self.physics.body.extrapolated_position(time)); let camera_uniforms = to_uniform_data(&self.physics.camera,self.physics.body.extrapolated_position(time));
self.staging_belt self.graphics.staging_belt
.write_buffer( .write_buffer(
&mut encoder, &mut encoder,
&self.camera_buf, &self.graphics.camera_buf,
0, 0,
wgpu::BufferSize::new((camera_uniforms.len() * 4) as wgpu::BufferAddress).unwrap(), wgpu::BufferSize::new((camera_uniforms.len() * 4) as wgpu::BufferAddress).unwrap(),
device, device,
) )
.copy_from_slice(bytemuck::cast_slice(&camera_uniforms)); .copy_from_slice(bytemuck::cast_slice(&camera_uniforms));
//This code only needs to run when the uniforms change //This code only needs to run when the uniforms change
for model in self.models.iter() { for model in self.graphics.models.iter() {
let model_uniforms = get_instances_buffer_data(&model.instances); let model_uniforms = get_instances_buffer_data(&model.instances);
self.staging_belt self.graphics.staging_belt
.write_buffer( .write_buffer(
&mut encoder, &mut encoder,
&model.model_buf,//description of where data will be written when command is executed &model.model_buf,//description of where data will be written when command is executed
@ -1029,7 +1043,7 @@ impl framework::Example for GraphicsData {
) )
.copy_from_slice(bytemuck::cast_slice(&model_uniforms)); .copy_from_slice(bytemuck::cast_slice(&model_uniforms));
} }
self.staging_belt.finish(); self.graphics.staging_belt.finish();
{ {
let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
@ -1048,7 +1062,7 @@ impl framework::Example for GraphicsData {
}, },
})], })],
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment { depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
view: &self.depth_view, view: &self.graphics.depth_view,
depth_ops: Some(wgpu::Operations { depth_ops: Some(wgpu::Operations {
load: wgpu::LoadOp::Clear(1.0), load: wgpu::LoadOp::Clear(1.0),
store: false, store: false,
@ -1057,11 +1071,11 @@ impl framework::Example for GraphicsData {
}), }),
}); });
rpass.set_bind_group(0, &self.bind_groups.camera, &[]); rpass.set_bind_group(0, &self.graphics.bind_groups.camera, &[]);
rpass.set_bind_group(1, &self.bind_groups.skybox_texture, &[]); rpass.set_bind_group(1, &self.graphics.bind_groups.skybox_texture, &[]);
rpass.set_pipeline(&self.pipelines.model); rpass.set_pipeline(&self.graphics.pipelines.model);
for model in self.models.iter() { for model in self.graphics.models.iter() {
rpass.set_bind_group(2, &model.bind_group, &[]); rpass.set_bind_group(2, &model.bind_group, &[]);
rpass.set_vertex_buffer(0, model.vertex_buf.slice(..)); rpass.set_vertex_buffer(0, model.vertex_buf.slice(..));
@ -1071,18 +1085,18 @@ impl framework::Example for GraphicsData {
} }
} }
rpass.set_pipeline(&self.pipelines.skybox); rpass.set_pipeline(&self.graphics.pipelines.skybox);
rpass.draw(0..3, 0..1); rpass.draw(0..3, 0..1);
} }
queue.submit(std::iter::once(encoder.finish())); queue.submit(std::iter::once(encoder.finish()));
self.staging_belt.recall(); self.graphics.staging_belt.recall();
} }
} }
fn main() { fn main() {
framework::run::<GraphicsData>( framework::run::<GlobalState>(
format!("Strafe Client v{}", format!("Strafe Client v{}",
env!("CARGO_PKG_VERSION") env!("CARGO_PKG_VERSION")
).as_str() ).as_str()