add model_bind_group

This commit is contained in:
Quaternions 2023-09-05 12:22:38 -07:00
parent cce9800045
commit 87f674238d
2 changed files with 29 additions and 16 deletions

View File

@ -113,7 +113,8 @@ pub struct Skybox {
sky_pipeline: wgpu::RenderPipeline, sky_pipeline: wgpu::RenderPipeline,
entity_pipeline: wgpu::RenderPipeline, entity_pipeline: wgpu::RenderPipeline,
ground_pipeline: wgpu::RenderPipeline, ground_pipeline: wgpu::RenderPipeline,
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, entity_buf: wgpu::Buffer,
entities: Vec<Entity>, entities: Vec<Entity>,
@ -207,7 +208,7 @@ impl strafe_client::framework::Example for Skybox {
println!("entities.len = {:?}", entities.len()); println!("entities.len = {:?}", entities.len());
entities[6].transform=glam::Affine3A::from_translation(glam::vec3(10.,5.,10.)); entities[6].transform=glam::Affine3A::from_translation(glam::vec3(10.,5.,10.));
let 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,
entries: &[ entries: &[
wgpu::BindGroupLayoutEntry { wgpu::BindGroupLayoutEntry {
@ -236,8 +237,13 @@ impl strafe_client::framework::Example for Skybox {
ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering), ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
count: None, count: None,
}, },
],
});
let model_bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: None,
entries: &[
wgpu::BindGroupLayoutEntry { wgpu::BindGroupLayoutEntry {
binding: 3, binding: 0,
visibility: wgpu::ShaderStages::VERTEX, visibility: wgpu::ShaderStages::VERTEX,
ty: wgpu::BindingType::Buffer { ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform, ty: wgpu::BufferBindingType::Uniform,
@ -287,7 +293,7 @@ impl strafe_client::framework::Example for Skybox {
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: &[&bind_group_layout], bind_group_layouts: &[&main_bind_group_layout, &model_bind_group_layout],
push_constant_ranges: &[], push_constant_ranges: &[],
}); });
@ -461,8 +467,8 @@ impl strafe_client::framework::Example for Skybox {
dimension: Some(wgpu::TextureViewDimension::Cube), dimension: Some(wgpu::TextureViewDimension::Cube),
..wgpu::TextureViewDescriptor::default() ..wgpu::TextureViewDescriptor::default()
}); });
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor { let main_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &bind_group_layout, layout: &main_bind_group_layout,
entries: &[ entries: &[
wgpu::BindGroupEntry { wgpu::BindGroupEntry {
binding: 0, binding: 0,
@ -476,12 +482,18 @@ impl strafe_client::framework::Example for Skybox {
binding: 2, binding: 2,
resource: wgpu::BindingResource::Sampler(&sampler), resource: wgpu::BindingResource::Sampler(&sampler),
}, },
],
label: Some("Camera"),
});
let model_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &model_bind_group_layout,
entries: &[
wgpu::BindGroupEntry { wgpu::BindGroupEntry {
binding: 3, binding: 0,
resource: entity_buf.as_entire_binding(), resource: entity_buf.as_entire_binding(),
}, },
], ],
label: None, label: Some("Model"),
}); });
let depth_view = Self::create_depth_texture(config, device); let depth_view = Self::create_depth_texture(config, device);
@ -491,7 +503,8 @@ impl strafe_client::framework::Example for Skybox {
sky_pipeline, sky_pipeline,
entity_pipeline, entity_pipeline,
ground_pipeline, ground_pipeline,
bind_group, main_bind_group,
model_bind_group,
camera_buf, camera_buf,
entity_buf, entity_buf,
entities, entities,
@ -660,7 +673,8 @@ impl strafe_client::framework::Example for Skybox {
}), }),
}); });
rpass.set_bind_group(0, &self.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 entity in self.entities.iter() {

View File

@ -72,19 +72,18 @@ struct EntityTransform {
matrix3: mat3x3<f32>, matrix3: mat3x3<f32>,
translation: vec3<f32>, translation: vec3<f32>,
}; };
@group(0) @group(1)
@binding(3) @binding(0)
var<uniform> r_EntityTransforms: array<EntityTransform, 7>; var<uniform> r_EntityTransform: EntityTransform;
@vertex @vertex
fn vs_entity( fn vs_entity(
@builtin(instance_index) instance_index: u32,
@location(0) pos: vec3<f32>, @location(0) pos: vec3<f32>,
@location(1) normal: vec3<f32>, @location(1) normal: vec3<f32>,
) -> EntityOutput { ) -> EntityOutput {
var position: vec3<f32> = r_EntityTransforms[instance_index].matrix3 * pos+r_EntityTransforms[instance_index].translation; var position: vec3<f32> = r_EntityTransform.matrix3 * pos+r_EntityTransform.translation;
var result: EntityOutput; var result: EntityOutput;
result.normal = r_EntityTransforms[instance_index].matrix3 * normal; result.normal = r_EntityTransform.matrix3 * normal;
result.view = position - r_data.cam_pos.xyz; result.view = position - r_data.cam_pos.xyz;
result.position = r_data.proj * r_data.view * vec4<f32>(position, 1.0); result.position = r_data.proj * r_data.view * vec4<f32>(position, 1.0);
return result; return result;