2023-10-19 00:17:21 +00:00
|
|
|
use std::borrow::Cow;
|
2024-01-30 06:37:05 +00:00
|
|
|
use std::collections::{HashSet,HashMap};
|
|
|
|
use strafesnet_common::map;
|
2024-01-30 00:19:41 +00:00
|
|
|
use strafesnet_common::integer;
|
2024-01-30 06:37:05 +00:00
|
|
|
use strafesnet_common::model::{self, ColorId, NormalId, PolygonIter, PositionId, RenderConfigId, TextureCoordinateId, VertexId};
|
2023-10-19 00:17:21 +00:00
|
|
|
use wgpu::{util::DeviceExt,AstcBlock,AstcChannel};
|
2024-01-30 06:37:05 +00:00
|
|
|
use crate::model_graphics::{self,IndexedGraphicsMeshOwnedRenderConfig,IndexedGraphicsMeshOwnedRenderConfigId,GraphicsMeshOwnedRenderConfig,GraphicsModelColor4,GraphicsModelOwned,GraphicsVertex};
|
2023-10-19 00:17:21 +00:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
2023-10-26 02:51:24 +00:00
|
|
|
pub struct GraphicsModelUpdate{
|
2023-10-19 00:17:21 +00:00
|
|
|
transform:Option<glam::Mat4>,
|
|
|
|
color:Option<glam::Vec4>,
|
|
|
|
}
|
|
|
|
|
2024-01-30 06:37:05 +00:00
|
|
|
struct Indices{
|
|
|
|
count:u32,
|
|
|
|
buf:wgpu::Buffer,
|
|
|
|
format:wgpu::IndexFormat,
|
2023-10-30 23:31:17 +00:00
|
|
|
}
|
2024-01-30 06:37:05 +00:00
|
|
|
impl Indices{
|
|
|
|
fn new<T:bytemuck::Pod>(device:&wgpu::Device,indices:&Vec<T>,format:wgpu::IndexFormat)->Self{
|
|
|
|
Self{
|
|
|
|
buf:device.create_buffer_init(&wgpu::util::BufferInitDescriptor{
|
|
|
|
label:Some("Index"),
|
|
|
|
contents:bytemuck::cast_slice(indices),
|
|
|
|
usage:wgpu::BufferUsages::INDEX,
|
|
|
|
}),
|
|
|
|
count:indices.len() as u32,
|
|
|
|
format,
|
2023-10-30 23:31:17 +00:00
|
|
|
}
|
2024-01-30 06:37:05 +00:00
|
|
|
}
|
2023-10-19 00:17:21 +00:00
|
|
|
}
|
2023-10-26 02:51:24 +00:00
|
|
|
struct GraphicsModel{
|
2024-01-30 06:37:05 +00:00
|
|
|
indices:Indices,
|
2023-10-30 23:31:17 +00:00
|
|
|
model_buf:wgpu::Buffer,
|
|
|
|
vertex_buf:wgpu::Buffer,
|
|
|
|
bind_group:wgpu::BindGroup,
|
2024-01-30 06:37:05 +00:00
|
|
|
instance_count:u32,
|
2023-10-19 00:17:21 +00:00
|
|
|
}
|
|
|
|
|
2024-01-30 06:37:05 +00:00
|
|
|
struct GraphicsSamplers{
|
|
|
|
repeat:wgpu::Sampler,
|
2023-10-19 00:17:21 +00:00
|
|
|
}
|
|
|
|
|
2024-01-30 06:37:05 +00:00
|
|
|
struct GraphicsBindGroupLayouts{
|
|
|
|
model:wgpu::BindGroupLayout,
|
2023-10-19 00:17:21 +00:00
|
|
|
}
|
|
|
|
|
2024-01-30 06:37:05 +00:00
|
|
|
struct GraphicsBindGroups{
|
|
|
|
camera:wgpu::BindGroup,
|
|
|
|
skybox_texture:wgpu::BindGroup,
|
2023-10-19 00:17:21 +00:00
|
|
|
}
|
|
|
|
|
2024-01-30 06:37:05 +00:00
|
|
|
struct GraphicsPipelines{
|
|
|
|
skybox:wgpu::RenderPipeline,
|
|
|
|
model:wgpu::RenderPipeline,
|
2023-10-19 00:17:21 +00:00
|
|
|
}
|
|
|
|
|
2024-01-30 06:37:05 +00:00
|
|
|
struct GraphicsCamera{
|
|
|
|
screen_size:glam::UVec2,
|
|
|
|
fov:glam::Vec2,//slope
|
2023-10-19 00:17:21 +00:00
|
|
|
//camera angles and such are extrapolated and passed in every time
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2024-01-30 06:37:05 +00:00
|
|
|
fn perspective_rh(fov_x_slope:f32,fov_y_slope:f32,z_near:f32,z_far:f32)->glam::Mat4{
|
2023-10-19 00:17:21 +00:00
|
|
|
//glam_assert!(z_near > 0.0 && z_far > 0.0);
|
2024-01-30 06:37:05 +00:00
|
|
|
let r=z_far / (z_near-z_far);
|
2023-10-19 00:17:21 +00:00
|
|
|
glam::Mat4::from_cols(
|
2024-01-30 06:37:05 +00:00
|
|
|
glam::Vec4::new(1.0/fov_x_slope,0.0,0.0,0.0),
|
|
|
|
glam::Vec4::new(0.0,1.0/fov_y_slope,0.0,0.0),
|
|
|
|
glam::Vec4::new(0.0,0.0,r,-1.0),
|
|
|
|
glam::Vec4::new(0.0,0.0,r * z_near,0.0),
|
2023-10-19 00:17:21 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
impl GraphicsCamera{
|
|
|
|
pub fn proj(&self)->glam::Mat4{
|
2024-02-18 08:23:27 +00:00
|
|
|
perspective_rh(self.fov.x,self.fov.y,0.4,4000.0)
|
2023-10-19 00:17:21 +00:00
|
|
|
}
|
|
|
|
pub fn world(&self,pos:glam::Vec3,angles:glam::Vec2)->glam::Mat4{
|
|
|
|
//f32 good enough for view matrix
|
2024-01-30 06:37:05 +00:00
|
|
|
glam::Mat4::from_translation(pos) * glam::Mat4::from_euler(glam::EulerRot::YXZ,angles.x,angles.y,0f32)
|
2023-10-19 00:17:21 +00:00
|
|
|
}
|
|
|
|
|
2024-01-30 06:37:05 +00:00
|
|
|
pub fn to_uniform_data(&self,(pos,angles):(glam::Vec3,glam::Vec2))->[f32; 16 * 4]{
|
2023-10-19 00:17:21 +00:00
|
|
|
let proj=self.proj();
|
2024-01-30 06:37:05 +00:00
|
|
|
let proj_inv=proj.inverse();
|
2023-10-19 00:17:21 +00:00
|
|
|
let view_inv=self.world(pos,angles);
|
|
|
|
let view=view_inv.inverse();
|
|
|
|
|
2024-01-30 06:37:05 +00:00
|
|
|
let mut raw=[0f32; 16 * 4];
|
2023-10-19 00:17:21 +00:00
|
|
|
raw[..16].copy_from_slice(&AsRef::<[f32; 16]>::as_ref(&proj)[..]);
|
|
|
|
raw[16..32].copy_from_slice(&AsRef::<[f32; 16]>::as_ref(&proj_inv)[..]);
|
|
|
|
raw[32..48].copy_from_slice(&AsRef::<[f32; 16]>::as_ref(&view)[..]);
|
|
|
|
raw[48..64].copy_from_slice(&AsRef::<[f32; 16]>::as_ref(&view_inv)[..]);
|
|
|
|
raw
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl std::default::Default for GraphicsCamera{
|
|
|
|
fn default()->Self{
|
|
|
|
Self{
|
|
|
|
screen_size:glam::UVec2::ONE,
|
|
|
|
fov:glam::Vec2::ONE,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct GraphicsState{
|
2024-01-30 06:37:05 +00:00
|
|
|
pipelines:GraphicsPipelines,
|
|
|
|
bind_groups:GraphicsBindGroups,
|
|
|
|
bind_group_layouts:GraphicsBindGroupLayouts,
|
|
|
|
samplers:GraphicsSamplers,
|
2023-10-19 00:17:21 +00:00
|
|
|
camera:GraphicsCamera,
|
2024-01-30 06:37:05 +00:00
|
|
|
camera_buf:wgpu::Buffer,
|
|
|
|
temp_squid_texture_view:wgpu::TextureView,
|
|
|
|
models:Vec<GraphicsModel>,
|
|
|
|
depth_view:wgpu::TextureView,
|
|
|
|
staging_belt:wgpu::util::StagingBelt,
|
2023-10-19 00:17:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl GraphicsState{
|
2024-01-30 06:37:05 +00:00
|
|
|
const DEPTH_FORMAT:wgpu::TextureFormat=wgpu::TextureFormat::Depth24Plus;
|
2023-10-19 00:17:21 +00:00
|
|
|
fn create_depth_texture(
|
2024-01-30 06:37:05 +00:00
|
|
|
config:&wgpu::SurfaceConfiguration,
|
|
|
|
device:&wgpu::Device,
|
|
|
|
)->wgpu::TextureView{
|
|
|
|
let depth_texture=device.create_texture(&wgpu::TextureDescriptor{
|
|
|
|
size:wgpu::Extent3d{
|
|
|
|
width:config.width,
|
|
|
|
height:config.height,
|
|
|
|
depth_or_array_layers:1,
|
2023-10-19 00:17:21 +00:00
|
|
|
},
|
2024-01-30 06:37:05 +00:00
|
|
|
mip_level_count:1,
|
|
|
|
sample_count:1,
|
|
|
|
dimension:wgpu::TextureDimension::D2,
|
|
|
|
format:Self::DEPTH_FORMAT,
|
|
|
|
usage:wgpu::TextureUsages::RENDER_ATTACHMENT,
|
|
|
|
label:None,
|
|
|
|
view_formats:&[],
|
2023-10-19 00:17:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
depth_texture.create_view(&wgpu::TextureViewDescriptor::default())
|
|
|
|
}
|
|
|
|
pub fn clear(&mut self){
|
|
|
|
self.models.clear();
|
|
|
|
}
|
|
|
|
pub fn load_user_settings(&mut self,user_settings:&crate::settings::UserSettings){
|
|
|
|
self.camera.fov=user_settings.calculate_fov(1.0,&self.camera.screen_size).as_vec2();
|
|
|
|
}
|
2024-02-15 07:19:15 +00:00
|
|
|
pub fn generate_models(&mut self,device:&wgpu::Device,queue:&wgpu::Queue,map:&map::CompleteMap){
|
2023-10-19 00:17:21 +00:00
|
|
|
//generate texture view per texture
|
2024-02-15 07:19:15 +00:00
|
|
|
let texture_views:HashMap<strafesnet_common::model::TextureId,wgpu::TextureView>=map.textures.iter().enumerate().filter_map(|(texture_id,texture_data)|{
|
|
|
|
let texture_id=model::TextureId::new(texture_id as u32);
|
2024-02-13 10:30:16 +00:00
|
|
|
let image=ddsfile::Dds::read(std::io::Cursor::new(texture_data)).ok()?;
|
2023-10-19 00:17:21 +00:00
|
|
|
|
|
|
|
let (mut width,mut height)=(image.get_width(),image.get_height());
|
|
|
|
|
|
|
|
let format=match image.header10.unwrap().dxgi_format{
|
2024-01-30 06:37:05 +00:00
|
|
|
ddsfile::DxgiFormat::R8G8B8A8_UNorm_sRGB=>wgpu::TextureFormat::Rgba8UnormSrgb,
|
|
|
|
ddsfile::DxgiFormat::BC7_UNorm_sRGB =>{
|
|
|
|
//floor(w,4),should be ceil(w,4)
|
2023-10-19 00:17:21 +00:00
|
|
|
width=width/4*4;
|
|
|
|
height=height/4*4;
|
|
|
|
wgpu::TextureFormat::Bc7RgbaUnormSrgb
|
|
|
|
},
|
2024-02-13 13:55:11 +00:00
|
|
|
other=>{
|
|
|
|
println!("unsupported texture format{:?}",other);
|
|
|
|
return None;
|
|
|
|
},
|
2023-10-19 00:17:21 +00:00
|
|
|
};
|
|
|
|
|
2024-01-30 06:37:05 +00:00
|
|
|
let size=wgpu::Extent3d{
|
2023-10-19 00:17:21 +00:00
|
|
|
width,
|
|
|
|
height,
|
2024-01-30 06:37:05 +00:00
|
|
|
depth_or_array_layers:1,
|
2023-10-19 00:17:21 +00:00
|
|
|
};
|
|
|
|
|
2024-01-30 06:37:05 +00:00
|
|
|
let layer_size=wgpu::Extent3d{
|
|
|
|
depth_or_array_layers:1,
|
2023-10-19 00:17:21 +00:00
|
|
|
..size
|
|
|
|
};
|
2024-01-30 06:37:05 +00:00
|
|
|
let max_mips=layer_size.max_mips(wgpu::TextureDimension::D2);
|
2023-10-19 00:17:21 +00:00
|
|
|
|
2024-01-30 06:37:05 +00:00
|
|
|
let texture=device.create_texture_with_data(
|
2023-10-19 00:17:21 +00:00
|
|
|
queue,
|
2024-01-30 06:37:05 +00:00
|
|
|
&wgpu::TextureDescriptor{
|
2023-10-19 00:17:21 +00:00
|
|
|
size,
|
2024-01-30 06:37:05 +00:00
|
|
|
mip_level_count:max_mips,
|
|
|
|
sample_count:1,
|
|
|
|
dimension:wgpu::TextureDimension::D2,
|
2023-10-19 00:17:21 +00:00
|
|
|
format,
|
2024-01-30 06:37:05 +00:00
|
|
|
usage:wgpu::TextureUsages::TEXTURE_BINDING|wgpu::TextureUsages::COPY_DST,
|
|
|
|
label:Some(format!("Texture{}",texture_id.get()).as_str()),
|
|
|
|
view_formats:&[],
|
2023-10-19 00:17:21 +00:00
|
|
|
},
|
2024-01-18 22:26:08 +00:00
|
|
|
wgpu::util::TextureDataOrder::LayerMajor,
|
2023-10-19 00:17:21 +00:00
|
|
|
&image.data,
|
|
|
|
);
|
2024-02-13 10:30:16 +00:00
|
|
|
Some((texture_id,texture.create_view(&wgpu::TextureViewDescriptor{
|
2024-01-30 06:37:05 +00:00
|
|
|
label:Some(format!("Texture{} View",texture_id.get()).as_str()),
|
|
|
|
dimension:Some(wgpu::TextureViewDimension::D2),
|
2023-10-19 00:17:21 +00:00
|
|
|
..wgpu::TextureViewDescriptor::default()
|
2024-02-13 10:30:16 +00:00
|
|
|
})))
|
2023-10-19 00:17:21 +00:00
|
|
|
}).collect();
|
2024-01-30 06:37:05 +00:00
|
|
|
let num_textures=texture_views.len();
|
2023-10-19 00:17:21 +00:00
|
|
|
|
|
|
|
//split groups with different textures into separate models
|
2024-01-30 06:37:05 +00:00
|
|
|
//the models received here are supposed to be tightly packed,i.e. no code needs to check if two models are using the same groups.
|
|
|
|
let indexed_models_len=map.models.len();
|
|
|
|
//models split into graphics_group.RenderConfigId
|
|
|
|
let mut owned_mesh_id_from_mesh_id_render_config_id:HashMap<model::MeshId,HashMap<RenderConfigId,IndexedGraphicsMeshOwnedRenderConfigId>>=HashMap::new();
|
|
|
|
let mut unique_render_config_models:Vec<IndexedGraphicsMeshOwnedRenderConfig>=Vec::with_capacity(indexed_models_len);
|
|
|
|
for model in &map.models{
|
|
|
|
//wow
|
|
|
|
let instance=GraphicsModelOwned{
|
|
|
|
transform:model.transform.into(),
|
|
|
|
normal_transform:Into::<glam::Mat3>::into(model.transform.matrix3).inverse().transpose(),
|
|
|
|
color:GraphicsModelColor4::new(model.color),
|
|
|
|
};
|
|
|
|
//get or create owned mesh map
|
2024-02-16 14:04:24 +00:00
|
|
|
let owned_mesh_map=owned_mesh_id_from_mesh_id_render_config_id
|
|
|
|
.entry(model.mesh).or_insert_with(||{
|
2024-01-30 06:37:05 +00:00
|
|
|
let mut owned_mesh_map=HashMap::new();
|
|
|
|
//add mesh if renderid never before seen for this model
|
|
|
|
//add instance
|
|
|
|
//convert Model into GraphicsModelOwned
|
|
|
|
//check each group, if it's using a new render config then make a new clone of the model
|
|
|
|
if let Some(mesh)=map.meshes.get(model.mesh.get() as usize){
|
|
|
|
for graphics_group in mesh.graphics_groups.iter(){
|
|
|
|
//get or create owned mesh
|
2024-02-16 14:04:24 +00:00
|
|
|
let owned_mesh_id=owned_mesh_map
|
|
|
|
.entry(graphics_group.render).or_insert_with(||{
|
2024-01-30 06:37:05 +00:00
|
|
|
//create
|
|
|
|
let owned_mesh_id=IndexedGraphicsMeshOwnedRenderConfigId::new(unique_render_config_models.len() as u32);
|
|
|
|
unique_render_config_models.push(IndexedGraphicsMeshOwnedRenderConfig{
|
|
|
|
unique_pos:mesh.unique_pos.iter().map(|&v|*Into::<glam::Vec3>::into(v).as_ref()).collect(),
|
|
|
|
unique_tex:mesh.unique_tex.iter().map(|v|*v.as_ref()).collect(),
|
|
|
|
unique_normal:mesh.unique_normal.iter().map(|&v|*Into::<glam::Vec3>::into(v).as_ref()).collect(),
|
|
|
|
unique_color:mesh.unique_color.iter().map(|v|*v.as_ref()).collect(),
|
|
|
|
unique_vertices:mesh.unique_vertices.clone(),
|
|
|
|
render_config:graphics_group.render,
|
|
|
|
polys:model::PolygonGroup::PolygonList(model::PolygonList::new(Vec::new())),
|
2024-02-16 12:11:42 +00:00
|
|
|
instances:Vec::new(),
|
2024-01-30 06:37:05 +00:00
|
|
|
});
|
|
|
|
owned_mesh_id
|
2024-02-16 14:04:24 +00:00
|
|
|
});
|
2024-01-30 06:37:05 +00:00
|
|
|
let owned_mesh=unique_render_config_models.get_mut(owned_mesh_id.get() as usize).unwrap();
|
|
|
|
match &mut owned_mesh.polys{
|
|
|
|
model::PolygonGroup::PolygonList(polygon_list)=>polygon_list.extend(
|
|
|
|
graphics_group.groups.iter().flat_map(|polygon_group_id|{
|
|
|
|
mesh.polygon_groups[polygon_group_id.get() as usize].polys()
|
|
|
|
})
|
|
|
|
.map(|vertex_id_slice|
|
|
|
|
vertex_id_slice.to_vec()
|
|
|
|
)
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-02-16 14:04:24 +00:00
|
|
|
owned_mesh_map
|
|
|
|
});
|
2024-02-16 12:11:42 +00:00
|
|
|
for owned_mesh_id in owned_mesh_map.values(){
|
|
|
|
let owned_mesh=unique_render_config_models.get_mut(owned_mesh_id.get() as usize).unwrap();
|
|
|
|
let render_config=&map.render_configs[owned_mesh.render_config.get() as usize];
|
|
|
|
if model.color.w==0.0&&render_config.texture.is_none(){
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
owned_mesh.instances.push(instance.clone());
|
|
|
|
}
|
2023-10-19 00:17:21 +00:00
|
|
|
}
|
2024-01-30 06:37:05 +00:00
|
|
|
//check every model to see if it's using the same (texture,color) but has few instances,if it is combine it into one model
|
|
|
|
//1. collect unique instances of texture and color,note model id
|
|
|
|
//2. for each model id,check if removing it from the pool decreases both the model count and instance count by more than one
|
2023-10-19 00:17:21 +00:00
|
|
|
//3. transpose all models that stay in the set
|
|
|
|
|
2024-01-30 06:37:05 +00:00
|
|
|
//best plan:benchmark set_bind_group,set_vertex_buffer,set_index_buffer and draw_indexed
|
2023-10-19 00:17:21 +00:00
|
|
|
//check if the estimated render performance is better by transposing multiple model instances into one model instance
|
|
|
|
|
2024-01-30 06:37:05 +00:00
|
|
|
//for now:just deduplicate single models...
|
2023-10-19 00:17:21 +00:00
|
|
|
let mut deduplicated_models=Vec::with_capacity(indexed_models_len);//use indexed_models_len because the list will likely get smaller instead of bigger
|
2024-01-30 06:37:05 +00:00
|
|
|
let mut unique_texture_color=HashMap::new();//texture->color->vec![(model_id,instance_id)]
|
|
|
|
for (model_id,model) in unique_render_config_models.iter().enumerate(){
|
|
|
|
//for now:filter out models with more than one instance
|
2023-10-19 00:17:21 +00:00
|
|
|
if 1<model.instances.len(){
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
//populate hashmap
|
2024-02-16 14:04:24 +00:00
|
|
|
let unique_color=unique_texture_color
|
|
|
|
.entry(model.render_config)
|
|
|
|
.or_insert_with(||HashMap::new());
|
2023-10-19 00:17:21 +00:00
|
|
|
//separate instances by color
|
|
|
|
for (instance_id,instance) in model.instances.iter().enumerate(){
|
2024-02-16 14:04:24 +00:00
|
|
|
let model_instance_list=unique_color
|
|
|
|
.entry(instance.color)
|
|
|
|
.or_insert_with(||Vec::new());
|
2023-10-19 00:17:21 +00:00
|
|
|
//add model instance to list
|
|
|
|
model_instance_list.push((model_id,instance_id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//populate a hashset of models selected for transposition
|
|
|
|
//construct transposed models
|
2024-01-30 06:37:05 +00:00
|
|
|
let mut selected_model_instances=HashSet::new();
|
|
|
|
for (render_config,unique_color) in unique_texture_color.into_iter(){
|
2023-10-19 00:17:21 +00:00
|
|
|
for (color,model_instance_list) in unique_color.into_iter(){
|
|
|
|
//world transforming one model does not meet the definition of deduplicaiton
|
|
|
|
if 1<model_instance_list.len(){
|
|
|
|
//create model
|
|
|
|
let mut unique_pos=Vec::new();
|
2024-01-30 06:37:05 +00:00
|
|
|
let mut pos_id_from=HashMap::new();
|
2023-10-19 00:17:21 +00:00
|
|
|
let mut unique_tex=Vec::new();
|
2024-01-30 06:37:05 +00:00
|
|
|
let mut tex_id_from=HashMap::new();
|
2023-10-19 00:17:21 +00:00
|
|
|
let mut unique_normal=Vec::new();
|
2024-01-30 06:37:05 +00:00
|
|
|
let mut normal_id_from=HashMap::new();
|
2023-10-19 00:17:21 +00:00
|
|
|
let mut unique_color=Vec::new();
|
2024-01-30 06:37:05 +00:00
|
|
|
let mut color_id_from=HashMap::new();
|
2023-10-19 00:17:21 +00:00
|
|
|
let mut unique_vertices=Vec::new();
|
2024-01-30 06:37:05 +00:00
|
|
|
let mut vertex_id_from=HashMap::new();
|
2023-10-19 00:17:21 +00:00
|
|
|
|
|
|
|
let mut polys=Vec::new();
|
|
|
|
//transform instance vertices
|
|
|
|
for (model_id,instance_id) in model_instance_list.into_iter(){
|
|
|
|
//populate hashset to prevent these models from being copied
|
|
|
|
selected_model_instances.insert(model_id);
|
|
|
|
//there is only one instance per model
|
2024-01-30 06:37:05 +00:00
|
|
|
let model=&unique_render_config_models[model_id];
|
2023-10-19 00:17:21 +00:00
|
|
|
let instance=&model.instances[instance_id];
|
|
|
|
//just hash word slices LOL
|
2024-01-30 06:37:05 +00:00
|
|
|
let map_pos_id:Vec<PositionId>=model.unique_pos.iter().map(|untransformed_pos|{
|
2023-10-19 00:17:21 +00:00
|
|
|
let pos=instance.transform.transform_point3(glam::Vec3::from_array(untransformed_pos.clone())).to_array();
|
2024-01-30 06:37:05 +00:00
|
|
|
let h=bytemuck::cast::<[f32;3],[u32;3]>(pos);
|
2024-02-16 14:04:24 +00:00
|
|
|
PositionId::new(*pos_id_from.entry(h).or_insert_with(||{
|
2023-10-19 00:17:21 +00:00
|
|
|
let pos_id=unique_pos.len();
|
2024-01-30 06:37:05 +00:00
|
|
|
unique_pos.push(pos);
|
2023-10-19 00:17:21 +00:00
|
|
|
pos_id
|
2024-01-30 06:37:05 +00:00
|
|
|
}) as u32)
|
2023-10-19 00:17:21 +00:00
|
|
|
}).collect();
|
2024-01-30 06:37:05 +00:00
|
|
|
let map_tex_id:Vec<TextureCoordinateId>=model.unique_tex.iter().map(|&tex|{
|
|
|
|
let h=bytemuck::cast::<[f32;2],[u32;2]>(tex);
|
2024-02-16 14:04:24 +00:00
|
|
|
TextureCoordinateId::new(*tex_id_from.entry(h).or_insert_with(||{
|
2023-10-19 00:17:21 +00:00
|
|
|
let tex_id=unique_tex.len();
|
2024-01-30 06:37:05 +00:00
|
|
|
unique_tex.push(tex);
|
2023-10-19 00:17:21 +00:00
|
|
|
tex_id
|
2024-01-30 06:37:05 +00:00
|
|
|
}) as u32)
|
2023-10-19 00:17:21 +00:00
|
|
|
}).collect();
|
2024-01-30 06:37:05 +00:00
|
|
|
let map_normal_id:Vec<NormalId>=model.unique_normal.iter().map(|untransformed_normal|{
|
2023-10-19 00:17:21 +00:00
|
|
|
let normal=(instance.normal_transform*glam::Vec3::from_array(untransformed_normal.clone())).to_array();
|
2024-01-30 06:37:05 +00:00
|
|
|
let h=bytemuck::cast::<[f32;3],[u32;3]>(normal);
|
2024-02-16 14:04:24 +00:00
|
|
|
NormalId::new(*normal_id_from.entry(h).or_insert_with(||{
|
2023-10-19 00:17:21 +00:00
|
|
|
let normal_id=unique_normal.len();
|
2024-01-30 06:37:05 +00:00
|
|
|
unique_normal.push(normal);
|
2023-10-19 00:17:21 +00:00
|
|
|
normal_id
|
2024-01-30 06:37:05 +00:00
|
|
|
}) as u32)
|
2023-10-19 00:17:21 +00:00
|
|
|
}).collect();
|
2024-01-30 06:37:05 +00:00
|
|
|
let map_color_id:Vec<ColorId>=model.unique_color.iter().map(|&color|{
|
|
|
|
let h=bytemuck::cast::<[f32;4],[u32;4]>(color);
|
2024-02-16 14:04:24 +00:00
|
|
|
ColorId::new(*color_id_from.entry(h).or_insert_with(||{
|
2023-10-19 00:17:21 +00:00
|
|
|
let color_id=unique_color.len();
|
2024-01-30 06:37:05 +00:00
|
|
|
unique_color.push(color);
|
2023-10-19 00:17:21 +00:00
|
|
|
color_id
|
2024-01-30 06:37:05 +00:00
|
|
|
}) as u32)
|
2023-10-19 00:17:21 +00:00
|
|
|
}).collect();
|
|
|
|
//map the indexed vertices onto new indices
|
|
|
|
//creating the vertex map is slightly different because the vertices are directly hashable
|
2024-01-30 06:37:05 +00:00
|
|
|
let map_vertex_id:Vec<VertexId>=model.unique_vertices.iter().map(|unmapped_vertex|{
|
|
|
|
let vertex=model::IndexedVertex{
|
|
|
|
pos:map_pos_id[unmapped_vertex.pos.get() as usize],
|
|
|
|
tex:map_tex_id[unmapped_vertex.tex.get() as usize],
|
|
|
|
normal:map_normal_id[unmapped_vertex.normal.get() as usize],
|
|
|
|
color:map_color_id[unmapped_vertex.color.get() as usize],
|
2023-10-19 00:17:21 +00:00
|
|
|
};
|
2024-02-16 14:04:24 +00:00
|
|
|
VertexId::new(*vertex_id_from.entry(vertex.clone()).or_insert_with(||{
|
2023-10-19 00:17:21 +00:00
|
|
|
let vertex_id=unique_vertices.len();
|
2024-02-16 14:04:24 +00:00
|
|
|
unique_vertices.push(vertex);
|
2023-10-19 00:17:21 +00:00
|
|
|
vertex_id
|
2024-01-30 06:37:05 +00:00
|
|
|
}) as u32)
|
2023-10-19 00:17:21 +00:00
|
|
|
}).collect();
|
2024-01-30 06:37:05 +00:00
|
|
|
polys.extend(model.polys.polys().map(|poly|
|
|
|
|
poly.iter().map(|vertex_id|
|
|
|
|
map_vertex_id[vertex_id.get() as usize]
|
|
|
|
).collect()
|
|
|
|
));
|
2023-10-19 00:17:21 +00:00
|
|
|
}
|
|
|
|
//push model into dedup
|
2024-01-30 06:37:05 +00:00
|
|
|
deduplicated_models.push(IndexedGraphicsMeshOwnedRenderConfig{
|
2023-10-19 00:17:21 +00:00
|
|
|
unique_pos,
|
|
|
|
unique_tex,
|
|
|
|
unique_normal,
|
|
|
|
unique_color,
|
|
|
|
unique_vertices,
|
2024-01-30 06:37:05 +00:00
|
|
|
render_config,
|
|
|
|
polys:model::PolygonGroup::PolygonList(model::PolygonList::new(polys)),
|
|
|
|
instances:vec![GraphicsModelOwned{
|
2023-10-19 00:17:21 +00:00
|
|
|
transform:glam::Mat4::IDENTITY,
|
|
|
|
normal_transform:glam::Mat3::IDENTITY,
|
|
|
|
color
|
|
|
|
}],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//fill untouched models
|
2024-01-30 06:37:05 +00:00
|
|
|
for (model_id,model) in unique_render_config_models.into_iter().enumerate(){
|
2023-10-19 00:17:21 +00:00
|
|
|
if !selected_model_instances.contains(&model_id){
|
|
|
|
deduplicated_models.push(model);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//de-index models
|
|
|
|
let deduplicated_models_len=deduplicated_models.len();
|
2024-01-30 06:37:05 +00:00
|
|
|
let models:Vec<GraphicsMeshOwnedRenderConfig>=deduplicated_models.into_iter().map(|model|{
|
|
|
|
let mut vertices=Vec::new();
|
|
|
|
let mut index_from_vertex=HashMap::new();//::<IndexedVertex,usize>
|
2023-10-19 00:17:21 +00:00
|
|
|
//this mut be combined in a more complex way if the models use different render patterns per group
|
2024-01-30 06:37:05 +00:00
|
|
|
let mut indices=Vec::new();
|
|
|
|
for poly in model.polys.polys(){
|
|
|
|
for end_index in 2..poly.len(){
|
|
|
|
for index in [0,end_index-1,end_index]{
|
|
|
|
let vertex_index=poly[index];
|
2024-02-16 14:04:24 +00:00
|
|
|
indices.push(*index_from_vertex.entry(vertex_index).or_insert_with(||{
|
2024-01-30 06:37:05 +00:00
|
|
|
let i=vertices.len();
|
|
|
|
let vertex=&model.unique_vertices[vertex_index.get() as usize];
|
|
|
|
vertices.push(GraphicsVertex{
|
|
|
|
pos:model.unique_pos[vertex.pos.get() as usize],
|
|
|
|
tex:model.unique_tex[vertex.tex.get() as usize],
|
|
|
|
normal:model.unique_normal[vertex.normal.get() as usize],
|
|
|
|
color:model.unique_color[vertex.color.get() as usize],
|
|
|
|
});
|
2024-02-16 14:04:24 +00:00
|
|
|
i
|
|
|
|
}));
|
2023-10-19 00:17:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-01-30 06:37:05 +00:00
|
|
|
GraphicsMeshOwnedRenderConfig{
|
2023-10-19 00:17:21 +00:00
|
|
|
instances:model.instances,
|
2024-01-30 06:37:05 +00:00
|
|
|
indices:if (u32::MAX as usize)<vertices.len(){
|
2023-10-31 05:12:17 +00:00
|
|
|
panic!("Model has too many vertices!")
|
|
|
|
}else if (u16::MAX as usize)<vertices.len(){
|
2024-01-30 06:37:05 +00:00
|
|
|
model_graphics::Indices::U32(indices.into_iter().map(|vertex_idx|vertex_idx as u32).collect())
|
2023-10-30 23:31:17 +00:00
|
|
|
}else{
|
2024-01-30 06:37:05 +00:00
|
|
|
model_graphics::Indices::U16(indices.into_iter().map(|vertex_idx|vertex_idx as u16).collect())
|
2023-10-30 23:31:17 +00:00
|
|
|
},
|
2023-10-19 00:17:21 +00:00
|
|
|
vertices,
|
2024-01-30 06:37:05 +00:00
|
|
|
render_config:model.render_config,
|
2023-10-19 00:17:21 +00:00
|
|
|
}
|
|
|
|
}).collect();
|
|
|
|
//.into_iter() the modeldata vec so entities can be /moved/ to models.entities
|
|
|
|
let mut model_count=0;
|
|
|
|
let mut instance_count=0;
|
|
|
|
let uniform_buffer_binding_size=crate::setup::required_limits().max_uniform_buffer_binding_size as usize;
|
|
|
|
let chunk_size=uniform_buffer_binding_size/MODEL_BUFFER_SIZE_BYTES;
|
|
|
|
self.models.reserve(models.len());
|
2024-01-30 06:37:05 +00:00
|
|
|
for model in models.into_iter(){
|
2023-10-19 00:17:21 +00:00
|
|
|
instance_count+=model.instances.len();
|
|
|
|
for instances_chunk in model.instances.rchunks(chunk_size){
|
|
|
|
model_count+=1;
|
2024-01-30 06:37:05 +00:00
|
|
|
let model_uniforms=get_instances_buffer_data(instances_chunk);
|
|
|
|
let model_buf=device.create_buffer_init(&wgpu::util::BufferInitDescriptor{
|
|
|
|
label:Some(format!("Model{} Buf",model_count).as_str()),
|
|
|
|
contents:bytemuck::cast_slice(&model_uniforms),
|
|
|
|
usage:wgpu::BufferUsages::UNIFORM|wgpu::BufferUsages::COPY_DST,
|
2023-10-19 00:17:21 +00:00
|
|
|
});
|
2024-01-30 06:37:05 +00:00
|
|
|
let render_config=&map.render_configs[model.render_config.get() as usize];
|
2024-02-13 10:30:16 +00:00
|
|
|
let texture_view=render_config.texture.and_then(|texture_id|
|
|
|
|
texture_views.get(&texture_id)
|
|
|
|
).unwrap_or(&self.temp_squid_texture_view);
|
2024-01-30 06:37:05 +00:00
|
|
|
let bind_group=device.create_bind_group(&wgpu::BindGroupDescriptor{
|
|
|
|
layout:&self.bind_group_layouts.model,
|
|
|
|
entries:&[
|
|
|
|
wgpu::BindGroupEntry{
|
|
|
|
binding:0,
|
|
|
|
resource:model_buf.as_entire_binding(),
|
2023-10-19 00:17:21 +00:00
|
|
|
},
|
2024-01-30 06:37:05 +00:00
|
|
|
wgpu::BindGroupEntry{
|
|
|
|
binding:1,
|
|
|
|
resource:wgpu::BindingResource::TextureView(texture_view),
|
2023-10-19 00:17:21 +00:00
|
|
|
},
|
2024-01-30 06:37:05 +00:00
|
|
|
wgpu::BindGroupEntry{
|
|
|
|
binding:2,
|
|
|
|
resource:wgpu::BindingResource::Sampler(&self.samplers.repeat),
|
2023-10-19 00:17:21 +00:00
|
|
|
},
|
|
|
|
],
|
2024-01-30 06:37:05 +00:00
|
|
|
label:Some(format!("Model{} Bind Group",model_count).as_str()),
|
2023-10-19 00:17:21 +00:00
|
|
|
});
|
2024-01-30 06:37:05 +00:00
|
|
|
let vertex_buf=device.create_buffer_init(&wgpu::util::BufferInitDescriptor{
|
|
|
|
label:Some("Vertex"),
|
|
|
|
contents:bytemuck::cast_slice(&model.vertices),
|
|
|
|
usage:wgpu::BufferUsages::VERTEX,
|
2023-10-19 00:17:21 +00:00
|
|
|
});
|
|
|
|
//all of these are being moved here
|
2023-10-26 02:51:24 +00:00
|
|
|
self.models.push(GraphicsModel{
|
2024-01-30 06:37:05 +00:00
|
|
|
instance_count:instances_chunk.len() as u32,
|
2023-10-19 00:17:21 +00:00
|
|
|
vertex_buf,
|
2024-01-30 06:37:05 +00:00
|
|
|
indices:match &model.indices{
|
|
|
|
model_graphics::Indices::U32(indices)=>Indices::new(device,indices,wgpu::IndexFormat::Uint32),
|
|
|
|
model_graphics::Indices::U16(indices)=>Indices::new(device,indices,wgpu::IndexFormat::Uint16),
|
2023-10-30 23:31:17 +00:00
|
|
|
},
|
2024-01-30 06:37:05 +00:00
|
|
|
bind_group,
|
2023-10-19 00:17:21 +00:00
|
|
|
model_buf,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
println!("Texture References={}",num_textures);
|
|
|
|
println!("Textures Loaded={}",texture_views.len());
|
|
|
|
println!("Indexed Models={}",indexed_models_len);
|
|
|
|
println!("Deduplicated Models={}",deduplicated_models_len);
|
2024-01-30 06:37:05 +00:00
|
|
|
println!("Graphics Objects:{}",self.models.len());
|
|
|
|
println!("Graphics Instances:{}",instance_count);
|
2023-10-19 00:17:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new(
|
|
|
|
device:&wgpu::Device,
|
|
|
|
queue:&wgpu::Queue,
|
|
|
|
config:&wgpu::SurfaceConfiguration,
|
|
|
|
)->Self{
|
2024-01-30 06:37:05 +00:00
|
|
|
let camera_bind_group_layout=device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor{
|
|
|
|
label:None,
|
|
|
|
entries:&[
|
|
|
|
wgpu::BindGroupLayoutEntry{
|
|
|
|
binding:0,
|
|
|
|
visibility:wgpu::ShaderStages::VERTEX,
|
|
|
|
ty:wgpu::BindingType::Buffer{
|
|
|
|
ty:wgpu::BufferBindingType::Uniform,
|
|
|
|
has_dynamic_offset:false,
|
|
|
|
min_binding_size:None,
|
2023-10-19 00:17:21 +00:00
|
|
|
},
|
2024-01-30 06:37:05 +00:00
|
|
|
count:None,
|
2023-10-19 00:17:21 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
2024-01-30 06:37:05 +00:00
|
|
|
let skybox_texture_bind_group_layout=device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor{
|
|
|
|
label:Some("Skybox Texture Bind Group Layout"),
|
|
|
|
entries:&[
|
|
|
|
wgpu::BindGroupLayoutEntry{
|
|
|
|
binding:0,
|
|
|
|
visibility:wgpu::ShaderStages::FRAGMENT,
|
|
|
|
ty:wgpu::BindingType::Texture{
|
|
|
|
sample_type:wgpu::TextureSampleType::Float{filterable:true},
|
|
|
|
multisampled:false,
|
|
|
|
view_dimension:wgpu::TextureViewDimension::Cube,
|
2023-10-19 00:17:21 +00:00
|
|
|
},
|
2024-01-30 06:37:05 +00:00
|
|
|
count:None,
|
2023-10-19 00:17:21 +00:00
|
|
|
},
|
2024-01-30 06:37:05 +00:00
|
|
|
wgpu::BindGroupLayoutEntry{
|
|
|
|
binding:1,
|
|
|
|
visibility:wgpu::ShaderStages::FRAGMENT,
|
|
|
|
ty:wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
|
|
|
|
count:None,
|
2023-10-19 00:17:21 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
2024-01-30 06:37:05 +00:00
|
|
|
let model_bind_group_layout=device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor{
|
|
|
|
label:Some("Model Bind Group Layout"),
|
|
|
|
entries:&[
|
|
|
|
wgpu::BindGroupLayoutEntry{
|
|
|
|
binding:0,
|
|
|
|
visibility:wgpu::ShaderStages::VERTEX,
|
|
|
|
ty:wgpu::BindingType::Buffer{
|
|
|
|
ty:wgpu::BufferBindingType::Uniform,
|
|
|
|
has_dynamic_offset:false,
|
|
|
|
min_binding_size:None,
|
2023-10-19 00:17:21 +00:00
|
|
|
},
|
2024-01-30 06:37:05 +00:00
|
|
|
count:None,
|
2023-10-19 00:17:21 +00:00
|
|
|
},
|
2024-01-30 06:37:05 +00:00
|
|
|
wgpu::BindGroupLayoutEntry{
|
|
|
|
binding:1,
|
|
|
|
visibility:wgpu::ShaderStages::FRAGMENT,
|
|
|
|
ty:wgpu::BindingType::Texture{
|
|
|
|
sample_type:wgpu::TextureSampleType::Float{filterable:true},
|
|
|
|
multisampled:false,
|
|
|
|
view_dimension:wgpu::TextureViewDimension::D2,
|
2023-10-19 00:17:21 +00:00
|
|
|
},
|
2024-01-30 06:37:05 +00:00
|
|
|
count:None,
|
2023-10-19 00:17:21 +00:00
|
|
|
},
|
2024-01-30 06:37:05 +00:00
|
|
|
wgpu::BindGroupLayoutEntry{
|
|
|
|
binding:2,
|
|
|
|
visibility:wgpu::ShaderStages::FRAGMENT,
|
|
|
|
ty:wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
|
|
|
|
count:None,
|
2023-10-19 00:17:21 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
2024-01-30 06:37:05 +00:00
|
|
|
let clamp_sampler=device.create_sampler(&wgpu::SamplerDescriptor{
|
|
|
|
label:Some("Clamp Sampler"),
|
|
|
|
address_mode_u:wgpu::AddressMode::ClampToEdge,
|
|
|
|
address_mode_v:wgpu::AddressMode::ClampToEdge,
|
|
|
|
address_mode_w:wgpu::AddressMode::ClampToEdge,
|
|
|
|
mag_filter:wgpu::FilterMode::Linear,
|
|
|
|
min_filter:wgpu::FilterMode::Linear,
|
|
|
|
mipmap_filter:wgpu::FilterMode::Linear,
|
2023-10-19 00:17:21 +00:00
|
|
|
..Default::default()
|
|
|
|
});
|
2024-01-30 06:37:05 +00:00
|
|
|
let repeat_sampler=device.create_sampler(&wgpu::SamplerDescriptor{
|
|
|
|
label:Some("Repeat Sampler"),
|
|
|
|
address_mode_u:wgpu::AddressMode::Repeat,
|
|
|
|
address_mode_v:wgpu::AddressMode::Repeat,
|
|
|
|
address_mode_w:wgpu::AddressMode::Repeat,
|
|
|
|
mag_filter:wgpu::FilterMode::Linear,
|
|
|
|
min_filter:wgpu::FilterMode::Linear,
|
|
|
|
mipmap_filter:wgpu::FilterMode::Linear,
|
2023-11-11 03:08:33 +00:00
|
|
|
anisotropy_clamp:16,
|
2023-10-19 00:17:21 +00:00
|
|
|
..Default::default()
|
|
|
|
});
|
|
|
|
|
|
|
|
// Create the render pipeline
|
2024-01-30 06:37:05 +00:00
|
|
|
let shader=device.create_shader_module(wgpu::ShaderModuleDescriptor{
|
|
|
|
label:None,
|
|
|
|
source:wgpu::ShaderSource::Wgsl(Cow::Borrowed(include_str!("shader.wgsl"))),
|
2023-10-19 00:17:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
//load textures
|
2024-01-30 06:37:05 +00:00
|
|
|
let device_features=device.features();
|
2023-10-19 00:17:21 +00:00
|
|
|
|
|
|
|
let skybox_texture_view={
|
2024-01-30 06:37:05 +00:00
|
|
|
let skybox_format=if device_features.contains(wgpu::Features::TEXTURE_COMPRESSION_ASTC){
|
2023-10-19 00:17:21 +00:00
|
|
|
println!("Using ASTC");
|
2024-01-30 06:37:05 +00:00
|
|
|
wgpu::TextureFormat::Astc{
|
|
|
|
block:AstcBlock::B4x4,
|
|
|
|
channel:AstcChannel::UnormSrgb,
|
2023-10-19 00:17:21 +00:00
|
|
|
}
|
2024-01-30 06:37:05 +00:00
|
|
|
}else if device_features.contains(wgpu::Features::TEXTURE_COMPRESSION_ETC2){
|
2023-10-19 00:17:21 +00:00
|
|
|
println!("Using ETC2");
|
|
|
|
wgpu::TextureFormat::Etc2Rgb8UnormSrgb
|
2024-01-30 06:37:05 +00:00
|
|
|
}else if device_features.contains(wgpu::Features::TEXTURE_COMPRESSION_BC){
|
2023-10-19 00:17:21 +00:00
|
|
|
println!("Using BC");
|
|
|
|
wgpu::TextureFormat::Bc1RgbaUnormSrgb
|
2024-01-30 06:37:05 +00:00
|
|
|
}else{
|
2023-10-19 00:17:21 +00:00
|
|
|
println!("Using plain");
|
|
|
|
wgpu::TextureFormat::Bgra8UnormSrgb
|
|
|
|
};
|
|
|
|
|
2024-01-30 06:37:05 +00:00
|
|
|
let bytes=match skybox_format{
|
|
|
|
wgpu::TextureFormat::Astc{
|
|
|
|
block:AstcBlock::B4x4,
|
|
|
|
channel:AstcChannel::UnormSrgb,
|
|
|
|
}=>&include_bytes!("../images/astc.dds")[..],
|
|
|
|
wgpu::TextureFormat::Etc2Rgb8UnormSrgb=>&include_bytes!("../images/etc2.dds")[..],
|
|
|
|
wgpu::TextureFormat::Bc1RgbaUnormSrgb=>&include_bytes!("../images/bc1.dds")[..],
|
|
|
|
wgpu::TextureFormat::Bgra8UnormSrgb=>&include_bytes!("../images/bgra.dds")[..],
|
|
|
|
_=>unreachable!(),
|
2023-10-19 00:17:21 +00:00
|
|
|
};
|
|
|
|
|
2024-01-30 06:37:05 +00:00
|
|
|
let skybox_image=ddsfile::Dds::read(&mut std::io::Cursor::new(bytes)).unwrap();
|
2023-10-19 00:17:21 +00:00
|
|
|
|
2024-01-30 06:37:05 +00:00
|
|
|
let size=wgpu::Extent3d{
|
|
|
|
width:skybox_image.get_width(),
|
|
|
|
height:skybox_image.get_height(),
|
|
|
|
depth_or_array_layers:6,
|
2023-10-19 00:17:21 +00:00
|
|
|
};
|
|
|
|
|
2024-01-30 06:37:05 +00:00
|
|
|
let layer_size=wgpu::Extent3d{
|
|
|
|
depth_or_array_layers:1,
|
2023-10-19 00:17:21 +00:00
|
|
|
..size
|
|
|
|
};
|
2024-01-30 06:37:05 +00:00
|
|
|
let max_mips=layer_size.max_mips(wgpu::TextureDimension::D2);
|
2023-10-19 00:17:21 +00:00
|
|
|
|
2024-01-30 06:37:05 +00:00
|
|
|
let skybox_texture=device.create_texture_with_data(
|
2023-10-19 00:17:21 +00:00
|
|
|
queue,
|
2024-01-30 06:37:05 +00:00
|
|
|
&wgpu::TextureDescriptor{
|
2023-10-19 00:17:21 +00:00
|
|
|
size,
|
2024-01-30 06:37:05 +00:00
|
|
|
mip_level_count:max_mips,
|
|
|
|
sample_count:1,
|
|
|
|
dimension:wgpu::TextureDimension::D2,
|
|
|
|
format:skybox_format,
|
|
|
|
usage:wgpu::TextureUsages::TEXTURE_BINDING|wgpu::TextureUsages::COPY_DST,
|
|
|
|
label:Some("Skybox Texture"),
|
|
|
|
view_formats:&[],
|
2023-10-19 00:17:21 +00:00
|
|
|
},
|
2024-01-18 22:26:08 +00:00
|
|
|
wgpu::util::TextureDataOrder::LayerMajor,
|
2023-10-19 00:17:21 +00:00
|
|
|
&skybox_image.data,
|
|
|
|
);
|
|
|
|
|
2024-01-30 06:37:05 +00:00
|
|
|
skybox_texture.create_view(&wgpu::TextureViewDescriptor{
|
|
|
|
label:Some("Skybox Texture View"),
|
|
|
|
dimension:Some(wgpu::TextureViewDimension::Cube),
|
2023-10-19 00:17:21 +00:00
|
|
|
..wgpu::TextureViewDescriptor::default()
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
//squid
|
|
|
|
let squid_texture_view={
|
2024-01-30 06:37:05 +00:00
|
|
|
let bytes=include_bytes!("../images/squid.dds");
|
2023-10-19 00:17:21 +00:00
|
|
|
|
2024-01-30 06:37:05 +00:00
|
|
|
let image=ddsfile::Dds::read(&mut std::io::Cursor::new(bytes)).unwrap();
|
2023-10-19 00:17:21 +00:00
|
|
|
|
2024-01-30 06:37:05 +00:00
|
|
|
let size=wgpu::Extent3d{
|
|
|
|
width:image.get_width(),
|
|
|
|
height:image.get_height(),
|
|
|
|
depth_or_array_layers:1,
|
2023-10-19 00:17:21 +00:00
|
|
|
};
|
|
|
|
|
2024-01-30 06:37:05 +00:00
|
|
|
let layer_size=wgpu::Extent3d{
|
|
|
|
depth_or_array_layers:1,
|
2023-10-19 00:17:21 +00:00
|
|
|
..size
|
|
|
|
};
|
2024-01-30 06:37:05 +00:00
|
|
|
let max_mips=layer_size.max_mips(wgpu::TextureDimension::D2);
|
2023-10-19 00:17:21 +00:00
|
|
|
|
2024-01-30 06:37:05 +00:00
|
|
|
let texture=device.create_texture_with_data(
|
2023-10-19 00:17:21 +00:00
|
|
|
queue,
|
2024-01-30 06:37:05 +00:00
|
|
|
&wgpu::TextureDescriptor{
|
2023-10-19 00:17:21 +00:00
|
|
|
size,
|
2024-01-30 06:37:05 +00:00
|
|
|
mip_level_count:max_mips,
|
|
|
|
sample_count:1,
|
|
|
|
dimension:wgpu::TextureDimension::D2,
|
|
|
|
format:wgpu::TextureFormat::Bc7RgbaUnorm,
|
|
|
|
usage:wgpu::TextureUsages::TEXTURE_BINDING|wgpu::TextureUsages::COPY_DST,
|
|
|
|
label:Some("Squid Texture"),
|
|
|
|
view_formats:&[],
|
2023-10-19 00:17:21 +00:00
|
|
|
},
|
2024-01-18 22:26:08 +00:00
|
|
|
wgpu::util::TextureDataOrder::LayerMajor,
|
2023-10-19 00:17:21 +00:00
|
|
|
&image.data,
|
|
|
|
);
|
|
|
|
|
2024-01-30 06:37:05 +00:00
|
|
|
texture.create_view(&wgpu::TextureViewDescriptor{
|
|
|
|
label:Some("Squid Texture View"),
|
|
|
|
dimension:Some(wgpu::TextureViewDimension::D2),
|
2023-10-19 00:17:21 +00:00
|
|
|
..wgpu::TextureViewDescriptor::default()
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2024-01-30 06:37:05 +00:00
|
|
|
let model_pipeline_layout=device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor{
|
|
|
|
label:None,
|
|
|
|
bind_group_layouts:&[
|
2023-10-19 00:17:21 +00:00
|
|
|
&camera_bind_group_layout,
|
|
|
|
&skybox_texture_bind_group_layout,
|
|
|
|
&model_bind_group_layout,
|
|
|
|
],
|
2024-01-30 06:37:05 +00:00
|
|
|
push_constant_ranges:&[],
|
2023-10-19 00:17:21 +00:00
|
|
|
});
|
2024-01-30 06:37:05 +00:00
|
|
|
let sky_pipeline_layout=device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor{
|
|
|
|
label:None,
|
|
|
|
bind_group_layouts:&[
|
2023-10-19 00:17:21 +00:00
|
|
|
&camera_bind_group_layout,
|
|
|
|
&skybox_texture_bind_group_layout,
|
|
|
|
],
|
2024-01-30 06:37:05 +00:00
|
|
|
push_constant_ranges:&[],
|
2023-10-19 00:17:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Create the render pipelines
|
2024-01-30 06:37:05 +00:00
|
|
|
let sky_pipeline=device.create_render_pipeline(&wgpu::RenderPipelineDescriptor{
|
|
|
|
label:Some("Sky Pipeline"),
|
|
|
|
layout:Some(&sky_pipeline_layout),
|
|
|
|
vertex:wgpu::VertexState{
|
|
|
|
module:&shader,
|
|
|
|
entry_point:"vs_sky",
|
|
|
|
buffers:&[],
|
2023-10-19 00:17:21 +00:00
|
|
|
},
|
2024-01-30 06:37:05 +00:00
|
|
|
fragment:Some(wgpu::FragmentState{
|
|
|
|
module:&shader,
|
|
|
|
entry_point:"fs_sky",
|
|
|
|
targets:&[Some(config.view_formats[0].into())],
|
2023-10-19 00:17:21 +00:00
|
|
|
}),
|
2024-01-30 06:37:05 +00:00
|
|
|
primitive:wgpu::PrimitiveState{
|
|
|
|
front_face:wgpu::FrontFace::Cw,
|
2023-10-19 00:17:21 +00:00
|
|
|
..Default::default()
|
|
|
|
},
|
2024-01-30 06:37:05 +00:00
|
|
|
depth_stencil:Some(wgpu::DepthStencilState{
|
|
|
|
format:Self::DEPTH_FORMAT,
|
|
|
|
depth_write_enabled:false,
|
|
|
|
depth_compare:wgpu::CompareFunction::LessEqual,
|
|
|
|
stencil:wgpu::StencilState::default(),
|
|
|
|
bias:wgpu::DepthBiasState::default(),
|
2023-10-19 00:17:21 +00:00
|
|
|
}),
|
2024-01-30 06:37:05 +00:00
|
|
|
multisample:wgpu::MultisampleState::default(),
|
|
|
|
multiview:None,
|
2023-10-19 00:17:21 +00:00
|
|
|
});
|
2024-01-30 06:37:05 +00:00
|
|
|
let model_pipeline=device.create_render_pipeline(&wgpu::RenderPipelineDescriptor{
|
|
|
|
label:Some("Model Pipeline"),
|
|
|
|
layout:Some(&model_pipeline_layout),
|
|
|
|
vertex:wgpu::VertexState{
|
|
|
|
module:&shader,
|
|
|
|
entry_point:"vs_entity_texture",
|
|
|
|
buffers:&[wgpu::VertexBufferLayout{
|
|
|
|
array_stride:std::mem::size_of::<GraphicsVertex>() as wgpu::BufferAddress,
|
|
|
|
step_mode:wgpu::VertexStepMode::Vertex,
|
|
|
|
attributes:&wgpu::vertex_attr_array![0=>Float32x3,1=>Float32x2,2=>Float32x3,3=>Float32x4],
|
2023-10-19 00:17:21 +00:00
|
|
|
}],
|
|
|
|
},
|
2024-01-30 06:37:05 +00:00
|
|
|
fragment:Some(wgpu::FragmentState{
|
|
|
|
module:&shader,
|
|
|
|
entry_point:"fs_entity_texture",
|
|
|
|
targets:&[Some(config.view_formats[0].into())],
|
2023-10-19 00:17:21 +00:00
|
|
|
}),
|
2024-01-30 06:37:05 +00:00
|
|
|
primitive:wgpu::PrimitiveState{
|
|
|
|
front_face:wgpu::FrontFace::Cw,
|
2023-10-27 02:07:44 +00:00
|
|
|
cull_mode:Some(wgpu::Face::Front),
|
2023-10-19 00:17:21 +00:00
|
|
|
..Default::default()
|
|
|
|
},
|
2024-01-30 06:37:05 +00:00
|
|
|
depth_stencil:Some(wgpu::DepthStencilState{
|
|
|
|
format:Self::DEPTH_FORMAT,
|
|
|
|
depth_write_enabled:true,
|
|
|
|
depth_compare:wgpu::CompareFunction::LessEqual,
|
|
|
|
stencil:wgpu::StencilState::default(),
|
|
|
|
bias:wgpu::DepthBiasState::default(),
|
2023-10-19 00:17:21 +00:00
|
|
|
}),
|
2024-01-30 06:37:05 +00:00
|
|
|
multisample:wgpu::MultisampleState::default(),
|
|
|
|
multiview:None,
|
2023-10-19 00:17:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
let camera=GraphicsCamera::default();
|
2024-02-16 08:19:44 +00:00
|
|
|
let camera_uniforms=camera.to_uniform_data(crate::physics::PhysicsOutputState::default().extrapolate(crate::physics::MouseState::default()));
|
2024-01-30 06:37:05 +00:00
|
|
|
let camera_buf=device.create_buffer_init(&wgpu::util::BufferInitDescriptor{
|
|
|
|
label:Some("Camera"),
|
|
|
|
contents:bytemuck::cast_slice(&camera_uniforms),
|
|
|
|
usage:wgpu::BufferUsages::UNIFORM|wgpu::BufferUsages::COPY_DST,
|
2023-10-19 00:17:21 +00:00
|
|
|
});
|
2024-01-30 06:37:05 +00:00
|
|
|
let camera_bind_group=device.create_bind_group(&wgpu::BindGroupDescriptor{
|
|
|
|
layout:&camera_bind_group_layout,
|
|
|
|
entries:&[
|
|
|
|
wgpu::BindGroupEntry{
|
|
|
|
binding:0,
|
|
|
|
resource:camera_buf.as_entire_binding(),
|
2023-10-19 00:17:21 +00:00
|
|
|
},
|
|
|
|
],
|
2024-01-30 06:37:05 +00:00
|
|
|
label:Some("Camera"),
|
2023-10-19 00:17:21 +00:00
|
|
|
});
|
|
|
|
|
2024-01-30 06:37:05 +00:00
|
|
|
let skybox_texture_bind_group=device.create_bind_group(&wgpu::BindGroupDescriptor{
|
|
|
|
layout:&skybox_texture_bind_group_layout,
|
|
|
|
entries:&[
|
|
|
|
wgpu::BindGroupEntry{
|
|
|
|
binding:0,
|
|
|
|
resource:wgpu::BindingResource::TextureView(&skybox_texture_view),
|
2023-10-19 00:17:21 +00:00
|
|
|
},
|
2024-01-30 06:37:05 +00:00
|
|
|
wgpu::BindGroupEntry{
|
|
|
|
binding:1,
|
|
|
|
resource:wgpu::BindingResource::Sampler(&clamp_sampler),
|
2023-10-19 00:17:21 +00:00
|
|
|
},
|
|
|
|
],
|
2024-01-30 06:37:05 +00:00
|
|
|
label:Some("Sky Texture"),
|
2023-10-19 00:17:21 +00:00
|
|
|
});
|
|
|
|
|
2024-01-30 06:37:05 +00:00
|
|
|
let depth_view=Self::create_depth_texture(config,device);
|
2023-10-19 00:17:21 +00:00
|
|
|
|
|
|
|
Self{
|
|
|
|
pipelines:GraphicsPipelines{
|
|
|
|
skybox:sky_pipeline,
|
|
|
|
model:model_pipeline
|
|
|
|
},
|
|
|
|
bind_groups:GraphicsBindGroups{
|
|
|
|
camera:camera_bind_group,
|
|
|
|
skybox_texture:skybox_texture_bind_group,
|
|
|
|
},
|
|
|
|
camera,
|
|
|
|
camera_buf,
|
2024-01-30 06:37:05 +00:00
|
|
|
models:Vec::new(),
|
2023-10-19 00:17:21 +00:00
|
|
|
depth_view,
|
2024-01-30 06:37:05 +00:00
|
|
|
staging_belt:wgpu::util::StagingBelt::new(0x100),
|
|
|
|
bind_group_layouts:GraphicsBindGroupLayouts{model:model_bind_group_layout},
|
|
|
|
samplers:GraphicsSamplers{repeat:repeat_sampler},
|
|
|
|
temp_squid_texture_view:squid_texture_view,
|
2023-10-19 00:17:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn resize(
|
|
|
|
&mut self,
|
|
|
|
device:&wgpu::Device,
|
|
|
|
config:&wgpu::SurfaceConfiguration,
|
|
|
|
user_settings:&crate::settings::UserSettings,
|
2024-01-30 06:37:05 +00:00
|
|
|
){
|
2023-10-25 22:56:36 +00:00
|
|
|
self.depth_view=Self::create_depth_texture(config,device);
|
|
|
|
self.camera.screen_size=glam::uvec2(config.width,config.height);
|
2023-10-19 00:17:21 +00:00
|
|
|
self.load_user_settings(user_settings);
|
|
|
|
}
|
|
|
|
pub fn render(
|
|
|
|
&mut self,
|
|
|
|
view:&wgpu::TextureView,
|
|
|
|
device:&wgpu::Device,
|
|
|
|
queue:&wgpu::Queue,
|
|
|
|
physics_output:crate::physics::PhysicsOutputState,
|
2024-01-30 00:19:41 +00:00
|
|
|
predicted_time:integer::Time,
|
2023-10-19 00:17:21 +00:00
|
|
|
mouse_pos:glam::IVec2,
|
2024-01-30 06:37:05 +00:00
|
|
|
){
|
|
|
|
//TODO:use scheduled frame times to create beautiful smoothing simulation physics extrapolation assuming no input
|
2023-10-19 00:17:21 +00:00
|
|
|
|
2024-01-30 06:37:05 +00:00
|
|
|
let mut encoder=device.create_command_encoder(&wgpu::CommandEncoderDescriptor{label:None});
|
2023-10-19 00:17:21 +00:00
|
|
|
|
|
|
|
// update rotation
|
2024-02-16 08:19:44 +00:00
|
|
|
let camera_uniforms=self.camera.to_uniform_data(physics_output.extrapolate(crate::physics::MouseState{pos:mouse_pos,time:predicted_time}));
|
2023-10-19 00:17:21 +00:00
|
|
|
self.staging_belt
|
|
|
|
.write_buffer(
|
|
|
|
&mut encoder,
|
|
|
|
&self.camera_buf,
|
|
|
|
0,
|
|
|
|
wgpu::BufferSize::new((camera_uniforms.len() * 4) as wgpu::BufferAddress).unwrap(),
|
|
|
|
device,
|
|
|
|
)
|
|
|
|
.copy_from_slice(bytemuck::cast_slice(&camera_uniforms));
|
|
|
|
//This code only needs to run when the uniforms change
|
|
|
|
/*
|
2024-01-30 06:37:05 +00:00
|
|
|
for model in self.models.iter(){
|
|
|
|
let model_uniforms=get_instances_buffer_data(&model.instances);
|
2023-10-19 00:17:21 +00:00
|
|
|
self.staging_belt
|
|
|
|
.write_buffer(
|
|
|
|
&mut encoder,
|
|
|
|
&model.model_buf,//description of where data will be written when command is executed
|
|
|
|
0,//offset in staging belt?
|
|
|
|
wgpu::BufferSize::new((model_uniforms.len() * 4) as wgpu::BufferAddress).unwrap(),
|
|
|
|
device,
|
|
|
|
)
|
|
|
|
.copy_from_slice(bytemuck::cast_slice(&model_uniforms));
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
self.staging_belt.finish();
|
|
|
|
|
|
|
|
{
|
2024-01-30 06:37:05 +00:00
|
|
|
let mut rpass=encoder.begin_render_pass(&wgpu::RenderPassDescriptor{
|
|
|
|
label:None,
|
|
|
|
color_attachments:&[Some(wgpu::RenderPassColorAttachment{
|
2023-10-19 00:17:21 +00:00
|
|
|
view,
|
2024-01-30 06:37:05 +00:00
|
|
|
resolve_target:None,
|
|
|
|
ops:wgpu::Operations{
|
|
|
|
load:wgpu::LoadOp::Clear(wgpu::Color{
|
|
|
|
r:0.1,
|
|
|
|
g:0.2,
|
|
|
|
b:0.3,
|
|
|
|
a:1.0,
|
2023-10-19 00:17:21 +00:00
|
|
|
}),
|
2023-10-27 06:58:27 +00:00
|
|
|
store:wgpu::StoreOp::Store,
|
2023-10-19 00:17:21 +00:00
|
|
|
},
|
|
|
|
})],
|
2024-01-30 06:37:05 +00:00
|
|
|
depth_stencil_attachment:Some(wgpu::RenderPassDepthStencilAttachment{
|
|
|
|
view:&self.depth_view,
|
|
|
|
depth_ops:Some(wgpu::Operations{
|
|
|
|
load:wgpu::LoadOp::Clear(1.0),
|
2023-10-27 06:58:27 +00:00
|
|
|
store:wgpu::StoreOp::Discard,
|
2023-10-19 00:17:21 +00:00
|
|
|
}),
|
2024-01-30 06:37:05 +00:00
|
|
|
stencil_ops:None,
|
2023-10-19 00:17:21 +00:00
|
|
|
}),
|
2023-10-27 06:58:27 +00:00
|
|
|
timestamp_writes:Default::default(),
|
|
|
|
occlusion_query_set:Default::default(),
|
2023-10-19 00:17:21 +00:00
|
|
|
});
|
|
|
|
|
2024-01-30 06:37:05 +00:00
|
|
|
rpass.set_bind_group(0,&self.bind_groups.camera,&[]);
|
|
|
|
rpass.set_bind_group(1,&self.bind_groups.skybox_texture,&[]);
|
2023-10-19 00:17:21 +00:00
|
|
|
|
|
|
|
rpass.set_pipeline(&self.pipelines.model);
|
2024-01-30 06:37:05 +00:00
|
|
|
for model in &self.models{
|
|
|
|
rpass.set_bind_group(2,&model.bind_group,&[]);
|
|
|
|
rpass.set_vertex_buffer(0,model.vertex_buf.slice(..));
|
|
|
|
rpass.set_index_buffer(model.indices.buf.slice(..),model.indices.format);
|
|
|
|
//TODO: loop over triangle strips
|
|
|
|
rpass.draw_indexed(0..model.indices.count,0,0..model.instance_count);
|
2023-10-19 00:17:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rpass.set_pipeline(&self.pipelines.skybox);
|
2024-01-30 06:37:05 +00:00
|
|
|
rpass.draw(0..3,0..1);
|
2023-10-19 00:17:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
queue.submit(std::iter::once(encoder.finish()));
|
|
|
|
|
|
|
|
self.staging_belt.recall();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const MODEL_BUFFER_SIZE:usize=4*4 + 12 + 4;//let size=std::mem::size_of::<ModelInstance>();
|
|
|
|
const MODEL_BUFFER_SIZE_BYTES:usize=MODEL_BUFFER_SIZE*4;
|
2024-01-30 06:37:05 +00:00
|
|
|
fn get_instances_buffer_data(instances:&[GraphicsModelOwned])->Vec<f32>{
|
|
|
|
let mut raw=Vec::with_capacity(MODEL_BUFFER_SIZE*instances.len());
|
|
|
|
for mi in instances{
|
2023-10-30 23:13:57 +00:00
|
|
|
//model transform
|
|
|
|
raw.extend_from_slice(&AsRef::<[f32; 4*4]>::as_ref(&mi.transform)[..]);
|
|
|
|
//normal transform
|
|
|
|
raw.extend_from_slice(AsRef::<[f32; 3]>::as_ref(&mi.normal_transform.x_axis));
|
|
|
|
raw.extend_from_slice(&[0.0]);
|
|
|
|
raw.extend_from_slice(AsRef::<[f32; 3]>::as_ref(&mi.normal_transform.y_axis));
|
|
|
|
raw.extend_from_slice(&[0.0]);
|
|
|
|
raw.extend_from_slice(AsRef::<[f32; 3]>::as_ref(&mi.normal_transform.z_axis));
|
|
|
|
raw.extend_from_slice(&[0.0]);
|
|
|
|
//color
|
|
|
|
raw.extend_from_slice(AsRef::<[f32; 4]>::as_ref(&mi.color.get()));
|
2023-10-19 00:17:21 +00:00
|
|
|
}
|
|
|
|
raw
|
2023-10-26 02:51:24 +00:00
|
|
|
}
|