From 86d5f5a4f277185076c10cbc8ee60c8020fca9b1 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Mon, 13 Jan 2020 16:28:16 -0500 Subject: [PATCH] Update for the new wgpu-rs passes --- src/render/collision.rs | 12 ++++++------ src/render/debug.rs | 43 +++++++++++++++++++++++------------------ src/render/mod.rs | 14 ++++++++++---- src/render/terrain.rs | 2 +- src/space.rs | 1 + 5 files changed, 42 insertions(+), 30 deletions(-) diff --git a/src/render/collision.rs b/src/render/collision.rs index d3e246c3..566558bb 100644 --- a/src/render/collision.rs +++ b/src/render/collision.rs @@ -75,7 +75,7 @@ pub struct GpuCollider { ranges: Vec, } -pub struct GpuSession<'this, 'pass> { +pub struct GpuSession<'pass, 'this> { pass: wgpu::RenderPass<'pass>, uniform_buf: &'this wgpu::Buffer, dynamic_bind_group: &'this wgpu::BindGroup, @@ -340,12 +340,12 @@ impl GpuCollider { self.clear_pipeline = clear_pipeline; } - pub fn begin<'this, 'pass>( + pub fn begin<'pass, 'this: 'pass>( &'this mut self, encoder: &'pass mut wgpu::CommandEncoder, - terrain: &TerrainContext, + terrain: &'pass TerrainContext, _spawner: &LocalSpawner, - ) -> GpuSession<'this, 'pass> { + ) -> GpuSession<'pass, 'this> { if self.dirty_group_count != 0 { let mut pass = encoder.begin_compute_pass(); pass.set_pipeline(&self.clear_pipeline); @@ -396,8 +396,8 @@ impl GpuCollider { } } -impl<'this> GpuSession<'this, '_> { - pub fn add(&mut self, shape: &Shape, range_id: usize) -> usize { +impl<'pass, 'this: 'pass> GpuSession<'pass, 'this> { + pub fn add(&mut self, shape: &'pass Shape, range_id: usize) -> usize { let locals = Locals { indexes: [range_id as u32, self.polygon_id as u32], }; diff --git a/src/render/debug.rs b/src/render/debug.rs index bc0ef777..028816b2 100644 --- a/src/render/debug.rs +++ b/src/render/debug.rs @@ -112,6 +112,9 @@ pub struct Context { bind_group_line: wgpu::BindGroup, bind_group_face: wgpu::BindGroup, bind_group_edge: wgpu::BindGroup, + // hold the buffers alive + vertex_buf: Option, + color_buf: Option, } impl Context { @@ -200,6 +203,8 @@ impl Context { bind_group_line, bind_group_face, bind_group_edge, + vertex_buf: None, + color_buf: None, }; result.reload(device); result @@ -342,11 +347,11 @@ impl Context { } } - fn draw_liner( - &self, - pass: &mut wgpu::RenderPass, - vertex_buf: &wgpu::Buffer, - color_buf: &wgpu::Buffer, + fn draw_liner<'a>( + &'a self, + pass: &mut wgpu::RenderPass<'a>, + vertex_buf: &'a wgpu::Buffer, + color_buf: &'a wgpu::Buffer, color_rate: wgpu::InputStepMode, num_vert: usize, ) { @@ -363,11 +368,11 @@ impl Context { } } - pub fn draw_shape( - &self, - pass: &mut wgpu::RenderPass, - shape: &model::Shape, - instance_buf: &wgpu::Buffer, + pub fn draw_shape<'a>( + &'a self, + pass: &mut wgpu::RenderPass<'a>, + shape: &'a model::Shape, + instance_buf: &'a wgpu::Buffer, instance_id: usize, ) { if !self.settings.collision_shapes { @@ -409,26 +414,26 @@ impl Context { } } - pub fn draw_lines( - &self, - pass: &mut wgpu::RenderPass, + pub fn draw_lines<'a>( + &'a mut self, + pass: &mut wgpu::RenderPass<'a>, device: &wgpu::Device, linebuf: &LineBuffer, ){ - let vertex_buf = device.create_buffer_with_data( + self.vertex_buf = Some(device.create_buffer_with_data( linebuf.vertices.as_bytes(), wgpu::BufferUsage::VERTEX, - ); - let color_buf = device.create_buffer_with_data( + )); + self.color_buf = Some(device.create_buffer_with_data( linebuf.colors.as_bytes(), wgpu::BufferUsage::VERTEX, - ); + )); assert_eq!(linebuf.vertices.len(), linebuf.colors.len()); self.draw_liner( pass, - &vertex_buf, - &color_buf, + self.vertex_buf.as_ref().unwrap(), + self.color_buf.as_ref().unwrap(), wgpu::InputStepMode::Vertex, linebuf.vertices.len(), ); diff --git a/src/render/mod.rs b/src/render/mod.rs index a2a60a6d..0f33ca6b 100644 --- a/src/render/mod.rs +++ b/src/render/mod.rs @@ -317,6 +317,8 @@ struct InstanceArray { data: Vec, // holding the mesh alive, while the key is just a raw pointer mesh: Arc, + // actual hardware buffer for this data + buffer: Option, } pub struct Batcher { @@ -344,6 +346,7 @@ impl Batcher { .or_insert_with(|| InstanceArray { data: Vec::new(), mesh: Arc::clone(mesh), + buffer: None, }) .data.push(instance); } @@ -406,16 +409,19 @@ impl Batcher { } } - pub fn flush(&mut self, pass: &mut wgpu::RenderPass, device: &wgpu::Device) { + pub fn flush<'a>(&'a mut self, pass: &mut wgpu::RenderPass<'a>, device: &wgpu::Device) { for array in self.instances.values_mut() { if array.data.is_empty() { continue } - let instance_buf = device.create_buffer_with_data( + array.buffer = Some(device.create_buffer_with_data( array.data.as_bytes(), wgpu::BufferUsage::VERTEX, - ); - pass.set_vertex_buffers(0, &[(&array.mesh.vertex_buf, 0), (&instance_buf, 0)]); + )); + pass.set_vertex_buffers(0, &[ + (&array.mesh.vertex_buf, 0), + (array.buffer.as_ref().unwrap(), 0), + ]); pass.draw(0 .. array.mesh.num_vertices as u32, 0 .. array.data.len() as u32); array.data.clear(); } diff --git a/src/render/terrain.rs b/src/render/terrain.rs index b7a782a1..adc7a28d 100644 --- a/src/render/terrain.rs +++ b/src/render/terrain.rs @@ -1114,7 +1114,7 @@ impl Context { } } - pub fn draw(&self, pass: &mut wgpu::RenderPass) { + pub fn draw<'a>(&'a self, pass: &mut wgpu::RenderPass<'a>) { pass.set_bind_group(1, &self.bind_group, &[]); // draw terrain match self.kind { diff --git a/src/space.rs b/src/space.rs index b4c422d4..253578f4 100644 --- a/src/space.rs +++ b/src/space.rs @@ -65,6 +65,7 @@ pub struct Camera { pub proj: Projection, } +#[derive(Debug)] pub struct Follow { pub transform: Transform, pub speed: f32,