Skip to content

Commit

Permalink
Update for the new wgpu-rs passes
Browse files Browse the repository at this point in the history
  • Loading branch information
kvark committed Jan 13, 2020
1 parent 53abc69 commit 86d5f5a
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 30 deletions.
12 changes: 6 additions & 6 deletions src/render/collision.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ pub struct GpuCollider {
ranges: Vec<GpuRange>,
}

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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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],
};
Expand Down
43 changes: 24 additions & 19 deletions src/render/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<wgpu::Buffer>,
color_buf: Option<wgpu::Buffer>,
}

impl Context {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
) {
Expand All @@ -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 {
Expand Down Expand Up @@ -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(),
);
Expand Down
14 changes: 10 additions & 4 deletions src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,8 @@ struct InstanceArray {
data: Vec<object::Instance>,
// holding the mesh alive, while the key is just a raw pointer
mesh: Arc<model::Mesh>,
// actual hardware buffer for this data
buffer: Option<wgpu::Buffer>,
}

pub struct Batcher {
Expand Down Expand Up @@ -344,6 +346,7 @@ impl Batcher {
.or_insert_with(|| InstanceArray {
data: Vec::new(),
mesh: Arc::clone(mesh),
buffer: None,
})
.data.push(instance);
}
Expand Down Expand Up @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion src/render/terrain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions src/space.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ pub struct Camera {
pub proj: Projection,
}

#[derive(Debug)]
pub struct Follow {
pub transform: Transform,
pub speed: f32,
Expand Down

0 comments on commit 86d5f5a

Please sign in to comment.