summaryrefslogtreecommitdiff
path: root/src/renderer.rs
diff options
context:
space:
mode:
authorMicha White <botahamec@outlook.com>2022-09-18 16:45:05 -0400
committerMicha White <botahamec@outlook.com>2022-09-18 16:45:05 -0400
commit83295e01008bdf25e03f6b5aa18b93b735a5e326 (patch)
tree7010e0f2fb8994a7168e3a11b4fa996804a49342 /src/renderer.rs
parent3e913d329ebdb36b66ccb8ac7ccea203db266d20 (diff)
instancing
Diffstat (limited to 'src/renderer.rs')
-rw-r--r--src/renderer.rs24
1 files changed, 21 insertions, 3 deletions
diff --git a/src/renderer.rs b/src/renderer.rs
index dc8ecb2..75e6d1e 100644
--- a/src/renderer.rs
+++ b/src/renderer.rs
@@ -1,6 +1,6 @@
use std::convert::TryInto;
-use crate::{vertex::SQUARE, RenderWindowConfig, Vertex};
+use crate::{vertex::SQUARE, Instance, RenderWindowConfig, Vertex};
use pollster::FutureExt;
use thiserror::Error;
use wgpu::{include_wgsl, util::DeviceExt};
@@ -44,6 +44,7 @@ pub struct Renderer {
render_pipeline: wgpu::RenderPipeline,
square_vertex_buffer: wgpu::Buffer,
square_vertices: u32,
+ instances: Vec<Instance>,
window: Window,
}
@@ -128,7 +129,7 @@ impl Renderer {
vertex: wgpu::VertexState {
module: &shader,
entry_point: "vs_main",
- buffers: &[Vertex::desc()],
+ buffers: &[Vertex::desc(), Instance::desc()],
},
// information about the fragment shader
fragment: Some(wgpu::FragmentState {
@@ -171,6 +172,8 @@ impl Renderer {
usage: wgpu::BufferUsages::VERTEX,
});
+ let instances = Vec::new();
+
Ok(Self {
surface,
device,
@@ -179,6 +182,7 @@ impl Renderer {
render_pipeline,
square_vertex_buffer,
square_vertices,
+ instances,
window,
})
}
@@ -216,6 +220,19 @@ impl Renderer {
label: Some("Render Encoder"),
});
+ let num_instances = self
+ .instances
+ .len()
+ .try_into()
+ .expect("expected less than 3 billion instances");
+ let instance_buffer = self
+ .device
+ .create_buffer_init(&wgpu::util::BufferInitDescriptor {
+ label: Some("Sprite Instance Buffer"),
+ contents: bytemuck::cast_slice(&self.instances),
+ usage: wgpu::BufferUsages::VERTEX,
+ });
+
{
let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("Render Pass"),
@@ -232,7 +249,8 @@ impl Renderer {
render_pass.set_pipeline(&self.render_pipeline);
render_pass.set_vertex_buffer(0, self.square_vertex_buffer.slice(..));
- render_pass.draw(0..self.square_vertices, 0..1);
+ render_pass.set_vertex_buffer(1, instance_buffer.slice(..));
+ render_pass.draw(0..self.square_vertices, 0..num_instances);
}
// the encoder can't finish building the command buffer until the
// render pass is dropped