diff --git a/src/main.rs b/src/main.rs index dbbd50c..d7d8975 100644 --- a/src/main.rs +++ b/src/main.rs @@ -113,7 +113,8 @@ pub struct Skybox { sky_pipeline: wgpu::RenderPipeline, entity_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, entity_buf: wgpu::Buffer, entities: Vec, @@ -207,7 +208,7 @@ impl strafe_client::framework::Example for Skybox { println!("entities.len = {:?}", entities.len()); 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, entries: &[ wgpu::BindGroupLayoutEntry { @@ -236,8 +237,13 @@ impl strafe_client::framework::Example for Skybox { ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering), count: None, }, + ], + }); + let model_bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + label: None, + entries: &[ wgpu::BindGroupLayoutEntry { - binding: 3, + binding: 0, visibility: wgpu::ShaderStages::VERTEX, ty: wgpu::BindingType::Buffer { ty: wgpu::BufferBindingType::Uniform, @@ -287,7 +293,7 @@ impl strafe_client::framework::Example for Skybox { let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { label: None, - bind_group_layouts: &[&bind_group_layout], + bind_group_layouts: &[&main_bind_group_layout, &model_bind_group_layout], push_constant_ranges: &[], }); @@ -461,8 +467,8 @@ impl strafe_client::framework::Example for Skybox { dimension: Some(wgpu::TextureViewDimension::Cube), ..wgpu::TextureViewDescriptor::default() }); - let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor { - layout: &bind_group_layout, + let main_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor { + layout: &main_bind_group_layout, entries: &[ wgpu::BindGroupEntry { binding: 0, @@ -476,12 +482,18 @@ impl strafe_client::framework::Example for Skybox { binding: 2, 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 { - binding: 3, + binding: 0, resource: entity_buf.as_entire_binding(), }, ], - label: None, + label: Some("Model"), }); let depth_view = Self::create_depth_texture(config, device); @@ -491,7 +503,8 @@ impl strafe_client::framework::Example for Skybox { sky_pipeline, entity_pipeline, ground_pipeline, - bind_group, + main_bind_group, + model_bind_group, camera_buf, entity_buf, 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); for entity in self.entities.iter() { diff --git a/src/shader.wgsl b/src/shader.wgsl index 2ef5ce8..e85e6d7 100644 --- a/src/shader.wgsl +++ b/src/shader.wgsl @@ -72,19 +72,18 @@ struct EntityTransform { matrix3: mat3x3, translation: vec3, }; -@group(0) -@binding(3) -var r_EntityTransforms: array; +@group(1) +@binding(0) +var r_EntityTransform: EntityTransform; @vertex fn vs_entity( - @builtin(instance_index) instance_index: u32, @location(0) pos: vec3, @location(1) normal: vec3, ) -> EntityOutput { - var position: vec3 = r_EntityTransforms[instance_index].matrix3 * pos+r_EntityTransforms[instance_index].translation; + var position: vec3 = r_EntityTransform.matrix3 * pos+r_EntityTransform.translation; 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.position = r_data.proj * r_data.view * vec4(position, 1.0); return result;