Skip to content

Commit de0ff78

Browse files
[render] test vertex/index buffer
1 parent 3e8bfb4 commit de0ff78

File tree

5 files changed

+80
-9
lines changed

5 files changed

+80
-9
lines changed

crates/eqx_window/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ edition = "2021"
55

66
[dependencies]
77
winit = { version = "0.29", features = ["rwh_05"] }
8+
bytemuck = { version = "1.22", features = ["derive"] }
89
cgmath = "0.18"
910
log = "0.4"
1011
wgpu = "24.0"

crates/eqx_window/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mod primitives;
12
mod window;
23

34
pub mod prelude {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#[repr(C)]
2+
#[derive(Copy, Clone, Debug, bytemuck::Zeroable, bytemuck::Pod)]
3+
pub struct Vertex {
4+
position: [f32; 3],
5+
color: [f32; 3],
6+
}
7+
8+
impl Vertex {
9+
const ATTRIBUTE: [wgpu::VertexAttribute; 2] =
10+
wgpu::vertex_attr_array![0 => Float32x3, 1 => Float32x3];
11+
12+
pub fn descriptor() -> wgpu::VertexBufferLayout<'static> {
13+
wgpu::VertexBufferLayout {
14+
array_stride: size_of::<Self>() as wgpu::BufferAddress,
15+
step_mode: wgpu::VertexStepMode::Vertex,
16+
attributes: &Self::ATTRIBUTE,
17+
}
18+
}
19+
}
20+
21+
pub const TEST_VERTICES: &[Vertex] = &[
22+
Vertex {
23+
position: [-0.0868241, 0.49240386, 0.0],
24+
color: [0.5, 0.0, 0.5],
25+
}, // A
26+
Vertex {
27+
position: [-0.49513406, 0.06958647, 0.0],
28+
color: [0.5, 0.0, 0.5],
29+
}, // B
30+
Vertex {
31+
position: [-0.21918549, -0.44939706, 0.0],
32+
color: [0.5, 0.0, 0.5],
33+
}, // C
34+
Vertex {
35+
position: [0.35966998, -0.3473291, 0.0],
36+
color: [0.5, 0.0, 0.5],
37+
}, // D
38+
Vertex {
39+
position: [0.44147372, 0.2347359, 0.0],
40+
color: [0.5, 0.0, 0.5],
41+
}, // E
42+
];
43+
44+
pub const TEST_INDICES: &[u16] = &[0, 1, 4, 1, 2, 4, 2, 3, 4];

crates/eqx_window/src/window.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
use wgpu::util::{BufferInitDescriptor, DeviceExt};
2+
3+
use crate::primitives::{Vertex, TEST_INDICES, TEST_VERTICES};
4+
15
use eqx_app::prelude::Module;
26

37
use winit::{
@@ -20,6 +24,8 @@ struct State<'a> {
2024
size: winit::dpi::PhysicalSize<u32>,
2125
window: &'a WInitWindow,
2226
render_pipeline: wgpu::RenderPipeline,
27+
vertex_buffer: wgpu::Buffer,
28+
index_buffer: wgpu::Buffer,
2329
}
2430

2531
impl<'a> State<'a> {
@@ -96,7 +102,7 @@ impl<'a> State<'a> {
96102
vertex: wgpu::VertexState {
97103
module: &shader,
98104
entry_point: Some(VS_MAIN),
99-
buffers: &[],
105+
buffers: &[Vertex::descriptor()],
100106
compilation_options: wgpu::PipelineCompilationOptions::default(),
101107
},
102108
fragment: Some(wgpu::FragmentState {
@@ -128,6 +134,18 @@ impl<'a> State<'a> {
128134
cache: None,
129135
});
130136

137+
let vertex_buffer = device.create_buffer_init(&BufferInitDescriptor {
138+
label: Some("VertexBuffer"),
139+
contents: bytemuck::cast_slice(TEST_VERTICES),
140+
usage: wgpu::BufferUsages::VERTEX,
141+
});
142+
143+
let index_buffer = device.create_buffer_init(&BufferInitDescriptor {
144+
label: Some("IndexBuffer"),
145+
contents: bytemuck::cast_slice(TEST_INDICES),
146+
usage: wgpu::BufferUsages::INDEX,
147+
});
148+
131149
Self {
132150
surface,
133151
device,
@@ -136,6 +154,8 @@ impl<'a> State<'a> {
136154
size,
137155
window,
138156
render_pipeline,
157+
vertex_buffer,
158+
index_buffer,
139159
}
140160
}
141161

@@ -193,7 +213,9 @@ impl<'a> State<'a> {
193213
});
194214

195215
render_pass.set_pipeline(&self.render_pipeline);
196-
render_pass.draw(0..3, 0..1)
216+
render_pass.set_vertex_buffer(0, self.vertex_buffer.slice(..));
217+
render_pass.set_index_buffer(self.index_buffer.slice(..), wgpu::IndexFormat::Uint16);
218+
render_pass.draw_indexed(0..TEST_INDICES.len() as u32, 0, 0..1);
197219
}
198220

199221
self.queue.submit(std::iter::once(encoder.finish()));

shaders/trig/shader.wgsl

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
1+
struct VertexInput {
2+
@location(0) vert_pos: vec3<f32>,
3+
@location(1) vert_color: vec3<f32>,
4+
}
5+
16
struct VertexOutput {
27
@builtin(position) clip_position: vec4<f32>,
3-
@location(0) vert_pos: vec3<f32>,
8+
@location(0) color: vec3<f32>,
49
}
510

611
@vertex
712
fn vs_main(
8-
@builtin(vertex_index) in_vertex_index: u32
13+
model: VertexInput
914
) -> VertexOutput {
1015
var out: VertexOutput;
11-
let x = f32(1 - i32(in_vertex_index)) * 0.5;
12-
let y = f32(i32(in_vertex_index & 1u) * 2 - 1) * 0.5;
13-
out.clip_position = vec4<f32>(x, y, 0.0, 1.0);
14-
out.vert_pos = out.clip_position.xyz;
16+
out.clip_position = vec4<f32>(model.vert_pos, 1.0);
17+
out.color = model.vert_color;
1518
return out;
1619
}
1720

1821
@fragment
1922
fn fs_main(
2023
in: VertexOutput
2124
) -> @location(0) vec4<f32> {
22-
return vec4<f32>(0.3, 0.2, 0.1, 1.0);
25+
return vec4<f32>(in.color, 1.0);
2326
}
2427

0 commit comments

Comments
 (0)