summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMicha White <botahamec@outlook.com>2022-09-18 15:30:56 -0400
committerMicha White <botahamec@outlook.com>2022-09-18 15:30:56 -0400
commitd613b20803f41eeb4f6ae27003d5bfa371502930 (patch)
tree233f1906d0150b4fce80af3fb63dceeb70f308da
parentffee09ada476b3a350f331718d60fa806b564bad (diff)
Created a vertex buffer
-rw-r--r--Cargo.toml1
-rw-r--r--examples/black.rs4
-rw-r--r--shaders/sprite.wgsl10
-rw-r--r--src/lib.rs2
-rw-r--r--src/renderer.rs28
-rw-r--r--src/vertex.rs34
6 files changed, 68 insertions, 11 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 281d897..b91f529 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -11,6 +11,7 @@ log = "0.4"
wgpu = "0.13"
thiserror = "1"
pollster = "0.2"
+bytemuck = { version = "1.4", features = ["derive"] }
[[example]]
name = "black" \ No newline at end of file
diff --git a/examples/black.rs b/examples/black.rs
index ac2f5e5..f0bf5d3 100644
--- a/examples/black.rs
+++ b/examples/black.rs
@@ -6,8 +6,8 @@ use winit::event_loop::EventLoop;
fn main() {
// configure the render window
let config = RenderWindowConfig {
- //vsync: false,
- //mode: alligator_render::config::WindowMode::BorderlessFullscreen,
+ vsync: false,
+ mode: alligator_render::config::WindowMode::BorderlessFullscreen,
title: "Black Screen.exe".into(),
..Default::default()
};
diff --git a/shaders/sprite.wgsl b/shaders/sprite.wgsl
index 7d2c5ce..d126cae 100644
--- a/shaders/sprite.wgsl
+++ b/shaders/sprite.wgsl
@@ -1,14 +1,16 @@
+struct VertexInput {
+ @location(0) position: vec2<f32>
+}
+
struct VertexOutput {
@builtin(position) clip_position: vec4<f32>
}
@vertex
-fn vs_main(@builtin(vertex_index) in_vertex_index: u32) -> VertexOutput {
+fn vs_main(model: VertexInput) -> VertexOutput {
var out: VertexOutput;
- let x = (1.0 * f32(in_vertex_index % u32(2))) + -0.5;
- let y = (1.0 * f32(in_vertex_index / u32(2))) + -0.5;
- out.clip_position = vec4<f32>(x, y, 0.0, 1.0);
+ out.clip_position = vec4<f32>(model.position, 0.0, 1.0);
return out;
}
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,
+ }
+ }
+}