forked from StrafesNET/strafe-client
binding group per model
This commit is contained in:
parent
1a1509f315
commit
69c7896011
88
src/main.rs
88
src/main.rs
@ -12,11 +12,23 @@ struct Vertex {
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct Entity {
|
struct Entity {
|
||||||
transform: glam::Affine3A,
|
|
||||||
vertex_count: u32,
|
vertex_count: u32,
|
||||||
vertex_buf: wgpu::Buffer,
|
vertex_buf: wgpu::Buffer,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//temp?
|
||||||
|
struct ModelData {
|
||||||
|
transform: glam::Affine3A,
|
||||||
|
entities: Vec<Entity>,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Model {
|
||||||
|
transform: glam::Affine3A,
|
||||||
|
entities: Vec<Entity>,
|
||||||
|
bind_group: wgpu::BindGroup,
|
||||||
|
model_buf: wgpu::Buffer,
|
||||||
|
}
|
||||||
|
|
||||||
// Note: we use the Y=up coordinate space in this example.
|
// Note: we use the Y=up coordinate space in this example.
|
||||||
struct Camera {
|
struct Camera {
|
||||||
time: Instant,
|
time: Instant,
|
||||||
@ -112,10 +124,8 @@ pub struct Skybox {
|
|||||||
entity_pipeline: wgpu::RenderPipeline,
|
entity_pipeline: wgpu::RenderPipeline,
|
||||||
ground_pipeline: wgpu::RenderPipeline,
|
ground_pipeline: wgpu::RenderPipeline,
|
||||||
main_bind_group: wgpu::BindGroup,
|
main_bind_group: wgpu::BindGroup,
|
||||||
model_bind_group: wgpu::BindGroup,
|
|
||||||
camera_buf: wgpu::Buffer,
|
camera_buf: wgpu::Buffer,
|
||||||
entity_buf: wgpu::Buffer,
|
models: Vec<Model>,
|
||||||
entities: Vec<Entity>,
|
|
||||||
depth_view: wgpu::TextureView,
|
depth_view: wgpu::TextureView,
|
||||||
staging_belt: wgpu::util::StagingBelt,
|
staging_belt: wgpu::util::StagingBelt,
|
||||||
}
|
}
|
||||||
@ -146,16 +156,17 @@ impl Skybox {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_entity_uniform_data(entity:&Entity) -> [f32; 4*4] {
|
fn get_transform_uniform_data(transform:&glam::Affine3A) -> [f32; 4*4] {
|
||||||
let mut raw = [0f32; 4*4];
|
let mut raw = [0f32; 4*4];
|
||||||
raw[0..16].copy_from_slice(&AsRef::<[f32; 4*4]>::as_ref(&glam::Mat4::from(entity.transform))[..]);
|
raw[0..16].copy_from_slice(&AsRef::<[f32; 4*4]>::as_ref(&glam::Mat4::from(*transform))[..]);
|
||||||
raw
|
raw
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_obj(device:&wgpu::Device,entities:& mut Vec<Entity>,source:&[u8]){
|
fn add_obj(device:&wgpu::Device,modeldatas:& mut Vec<ModelData>,source:&[u8]){
|
||||||
let data = obj::ObjData::load_buf(&source[..]).unwrap();
|
let data = obj::ObjData::load_buf(&source[..]).unwrap();
|
||||||
let mut vertices = Vec::new();
|
let mut vertices = Vec::new();
|
||||||
for object in data.objects {
|
for object in data.objects {
|
||||||
|
let mut entities = Vec::<Entity>::new();
|
||||||
for group in object.groups {
|
for group in object.groups {
|
||||||
vertices.clear();
|
vertices.clear();
|
||||||
for poly in group.polys {
|
for poly in group.polys {
|
||||||
@ -176,11 +187,14 @@ fn add_obj(device:&wgpu::Device,entities:& mut Vec<Entity>,source:&[u8]){
|
|||||||
usage: wgpu::BufferUsages::VERTEX,
|
usage: wgpu::BufferUsages::VERTEX,
|
||||||
});
|
});
|
||||||
entities.push(Entity {
|
entities.push(Entity {
|
||||||
transform: glam::Affine3A::default(),
|
|
||||||
vertex_count: vertices.len() as u32,
|
vertex_count: vertices.len() as u32,
|
||||||
vertex_buf,
|
vertex_buf,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
modeldatas.push(ModelData {
|
||||||
|
transform: glam::Affine3A::default(),
|
||||||
|
entities,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -197,11 +211,11 @@ impl strafe_client::framework::Example for Skybox {
|
|||||||
device: &wgpu::Device,
|
device: &wgpu::Device,
|
||||||
queue: &wgpu::Queue,
|
queue: &wgpu::Queue,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let mut entities = Vec::<Entity>::new();
|
let mut modeldatas = Vec::<ModelData>::new();
|
||||||
add_obj(device,& mut entities,include_bytes!("../models/teslacyberv3.0.obj"));
|
add_obj(device,& mut modeldatas,include_bytes!("../models/teslacyberv3.0.obj"));
|
||||||
add_obj(device,& mut entities,include_bytes!("../models/suzanne.obj"));
|
add_obj(device,& mut modeldatas,include_bytes!("../models/suzanne.obj"));
|
||||||
println!("entities.len = {:?}", entities.len());
|
println!("models.len = {:?}", modeldatas.len());
|
||||||
entities[6].transform=glam::Affine3A::from_translation(glam::vec3(10.,5.,10.));
|
modeldatas[1].transform=glam::Affine3A::from_translation(glam::vec3(10.,5.,10.));
|
||||||
|
|
||||||
let main_bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
let main_bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||||
label: None,
|
label: None,
|
||||||
@ -279,13 +293,6 @@ impl strafe_client::framework::Example for Skybox {
|
|||||||
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
|
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
|
||||||
});
|
});
|
||||||
|
|
||||||
let entity_uniforms = get_entity_uniform_data(&entities[6]);
|
|
||||||
let entity_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
|
||||||
label: Some("EntityData"),
|
|
||||||
contents: bytemuck::cast_slice(&entity_uniforms),
|
|
||||||
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
|
|
||||||
});
|
|
||||||
|
|
||||||
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||||
label: None,
|
label: None,
|
||||||
bind_group_layouts: &[&main_bind_group_layout, &model_bind_group_layout],
|
bind_group_layouts: &[&main_bind_group_layout, &model_bind_group_layout],
|
||||||
@ -480,16 +487,31 @@ impl strafe_client::framework::Example for Skybox {
|
|||||||
],
|
],
|
||||||
label: Some("Camera"),
|
label: Some("Camera"),
|
||||||
});
|
});
|
||||||
|
let mut models = Vec::<Model>::new();
|
||||||
|
for (i,modeldata) in modeldatas.drain(..).enumerate() {
|
||||||
|
let model_uniforms = get_transform_uniform_data(&modeldata.transform);
|
||||||
|
let model_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||||
|
label: Some(format!("Model{}",i).as_str()),
|
||||||
|
contents: bytemuck::cast_slice(&model_uniforms),
|
||||||
|
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
|
||||||
|
});
|
||||||
let model_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
let model_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||||
layout: &model_bind_group_layout,
|
layout: &model_bind_group_layout,
|
||||||
entries: &[
|
entries: &[
|
||||||
wgpu::BindGroupEntry {
|
wgpu::BindGroupEntry {
|
||||||
binding: 0,
|
binding: 0,
|
||||||
resource: entity_buf.as_entire_binding(),
|
resource: model_buf.as_entire_binding(),
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
label: Some("Model"),
|
label: Some(format!("Model{}",i).as_str()),
|
||||||
});
|
});
|
||||||
|
models.push(Model{
|
||||||
|
transform: modeldata.transform,
|
||||||
|
entities: modeldata.entities,
|
||||||
|
bind_group: model_bind_group,
|
||||||
|
model_buf: model_buf,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
let depth_view = Self::create_depth_texture(config, device);
|
let depth_view = Self::create_depth_texture(config, device);
|
||||||
|
|
||||||
@ -499,10 +521,8 @@ impl strafe_client::framework::Example for Skybox {
|
|||||||
entity_pipeline,
|
entity_pipeline,
|
||||||
ground_pipeline,
|
ground_pipeline,
|
||||||
main_bind_group,
|
main_bind_group,
|
||||||
model_bind_group,
|
|
||||||
camera_buf,
|
camera_buf,
|
||||||
entity_buf,
|
models,
|
||||||
entities,
|
|
||||||
depth_view,
|
depth_view,
|
||||||
staging_belt: wgpu::util::StagingBelt::new(0x100),
|
staging_belt: wgpu::util::StagingBelt::new(0x100),
|
||||||
}
|
}
|
||||||
@ -629,17 +649,18 @@ impl strafe_client::framework::Example for Skybox {
|
|||||||
device,
|
device,
|
||||||
)
|
)
|
||||||
.copy_from_slice(bytemuck::cast_slice(&camera_uniforms));
|
.copy_from_slice(bytemuck::cast_slice(&camera_uniforms));
|
||||||
let entity_uniforms = get_entity_uniform_data(&self.entities[6]);
|
for model in self.models.iter() {
|
||||||
|
let model_uniforms = get_transform_uniform_data(&model.transform);
|
||||||
self.staging_belt
|
self.staging_belt
|
||||||
.write_buffer(
|
.write_buffer(
|
||||||
&mut encoder,
|
&mut encoder,
|
||||||
&self.entity_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
|
||||||
0,//offset in staging belt?
|
0,//offset in staging belt?
|
||||||
wgpu::BufferSize::new((entity_uniforms.len() * 4) as wgpu::BufferAddress).unwrap(),
|
wgpu::BufferSize::new((model_uniforms.len() * 4) as wgpu::BufferAddress).unwrap(),
|
||||||
device,
|
device,
|
||||||
)
|
)
|
||||||
.copy_from_slice(bytemuck::cast_slice(&entity_uniforms));
|
.copy_from_slice(bytemuck::cast_slice(&model_uniforms));
|
||||||
|
}
|
||||||
self.staging_belt.finish();
|
self.staging_belt.finish();
|
||||||
|
|
||||||
{
|
{
|
||||||
@ -669,13 +690,16 @@ impl strafe_client::framework::Example for Skybox {
|
|||||||
});
|
});
|
||||||
|
|
||||||
rpass.set_bind_group(0, &self.main_bind_group, &[]);
|
rpass.set_bind_group(0, &self.main_bind_group, &[]);
|
||||||
rpass.set_bind_group(1, &self.model_bind_group, &[]);
|
|
||||||
|
|
||||||
rpass.set_pipeline(&self.entity_pipeline);
|
rpass.set_pipeline(&self.entity_pipeline);
|
||||||
for entity in self.entities.iter() {
|
for model in self.models.iter() {
|
||||||
|
rpass.set_bind_group(1, &model.bind_group, &[]);
|
||||||
|
|
||||||
|
for entity in model.entities.iter() {
|
||||||
rpass.set_vertex_buffer(0, entity.vertex_buf.slice(..));
|
rpass.set_vertex_buffer(0, entity.vertex_buf.slice(..));
|
||||||
rpass.draw(0..entity.vertex_count, 0..1);
|
rpass.draw(0..entity.vertex_count, 0..1);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
rpass.set_pipeline(&self.ground_pipeline);
|
rpass.set_pipeline(&self.ground_pipeline);
|
||||||
//rpass.set_index_buffer(&[0u16,1,2,1,2,3][..] as wgpu::BufferSlice, wgpu::IndexFormat::Uint16);
|
//rpass.set_index_buffer(&[0u16,1,2,1,2,3][..] as wgpu::BufferSlice, wgpu::IndexFormat::Uint16);
|
||||||
|
Loading…
Reference in New Issue
Block a user