summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/lib.rs2
-rw-r--r--src/renderer.rs28
-rw-r--r--src/vertex.rs34
3 files changed, 59 insertions, 5 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 1bdc5f6..fbc517b 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -6,6 +6,8 @@
pub mod config;
pub mod renderer;
+mod vertex;
pub use config::RenderWindowConfig;
pub use renderer::Renderer;
+pub(crate) use vertex::Vertex;
diff --git a/src/renderer.rs b/src/renderer.rs
index 2264ab5..dc8ecb2 100644
--- a/src/renderer.rs
+++ b/src/renderer.rs
@@ -1,7 +1,9 @@
-use crate::config::RenderWindowConfig;
+use std::convert::TryInto;
+
+use crate::{vertex::SQUARE, RenderWindowConfig, Vertex};
use pollster::FutureExt;
use thiserror::Error;
-use wgpu::include_wgsl;
+use wgpu::{include_wgsl, util::DeviceExt};
use winit::{
dpi::PhysicalSize,
error::OsError,
@@ -40,6 +42,8 @@ pub struct Renderer {
queue: wgpu::Queue,
surface_config: wgpu::SurfaceConfiguration,
render_pipeline: wgpu::RenderPipeline,
+ square_vertex_buffer: wgpu::Buffer,
+ square_vertices: u32,
window: Window,
}
@@ -100,7 +104,7 @@ impl Renderer {
None,
)
.block_on()
- .unwrap();
+ .expect("there was no device with the selected features");
// configuration for the surface
let surface_config = config.to_surface_configuration(
@@ -124,7 +128,7 @@ impl Renderer {
vertex: wgpu::VertexState {
module: &shader,
entry_point: "vs_main",
- buffers: &[],
+ buffers: &[Vertex::desc()],
},
// information about the fragment shader
fragment: Some(wgpu::FragmentState {
@@ -156,12 +160,25 @@ impl Renderer {
multiview: None,
});
+ // the vertex buffer used for rendering squares
+ let square_vertices = SQUARE
+ .len()
+ .try_into()
+ .expect("expected fewer than 3 billion vertices in a square");
+ let square_vertex_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
+ label: Some("Square Vertex Buffer"),
+ contents: bytemuck::cast_slice(&SQUARE),
+ usage: wgpu::BufferUsages::VERTEX,
+ });
+
Ok(Self {
surface,
device,
queue,
surface_config,
render_pipeline,
+ square_vertex_buffer,
+ square_vertices,
window,
})
}
@@ -214,7 +231,8 @@ impl Renderer {
});
render_pass.set_pipeline(&self.render_pipeline);
- render_pass.draw(0..4, 0..1);
+ render_pass.set_vertex_buffer(0, self.square_vertex_buffer.slice(..));
+ render_pass.draw(0..self.square_vertices, 0..1);
}
// the encoder can't finish building the command buffer until the
// render pass is dropped
diff --git a/src/vertex.rs b/src/vertex.rs
new file mode 100644
index 0000000..7735801
--- /dev/null
+++ b/src/vertex.rs
@@ -0,0 +1,34 @@
+use std::mem::size_of;
+
+use bytemuck::{Pod, Zeroable};
+
+pub const SQUARE: [Vertex; 4] = [
+ Vertex::new(-0.5, -0.5),
+ Vertex::new(0.5, -0.5),
+ Vertex::new(-0.5, 0.5),
+ Vertex::new(0.5, 0.5),
+];
+
+#[repr(C)]
+#[derive(Copy, Clone, Debug, PartialEq, Pod, Zeroable)]
+pub struct Vertex {
+ position: [f32; 2],
+}
+
+impl Vertex {
+ const fn new(x: f32, y: f32) -> Self {
+ Self { position: [x, y] }
+ }
+}
+
+impl Vertex {
+ const ATTRIBUTES: [wgpu::VertexAttribute; 1] = wgpu::vertex_attr_array![0 => Float32x2];
+
+ pub(crate) const fn desc<'a>() -> wgpu::VertexBufferLayout<'a> {
+ wgpu::VertexBufferLayout {
+ array_stride: size_of::<Self>() as wgpu::BufferAddress,
+ step_mode: wgpu::VertexStepMode::Vertex,
+ attributes: &Self::ATTRIBUTES,
+ }
+ }
+}