Skip to content

Commit 7dfbe08

Browse files
andrew-r-thomasprokopyl
authored andcommitted
used uniform buffer instead of making new vertex buffer every time
1 parent 2174d23 commit 7dfbe08

File tree

2 files changed

+70
-31
lines changed

2 files changed

+70
-31
lines changed

examples/render_wgpu/src/main.rs

Lines changed: 67 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
use baseview::{MouseEvent, Size, Window, WindowHandler, WindowOpenOptions};
2-
use wgpu::{util::DeviceExt, Buffer, Device, Queue, RenderPipeline, Surface, SurfaceCapabilities};
2+
use wgpu::{
3+
util::DeviceExt, BindGroup, Buffer, Device, Queue, RenderPipeline, Surface, SurfaceCapabilities,
4+
};
5+
6+
const WINDOW_SIZE: u32 = 512;
37

48
struct WgpuExample {
59
pipeline: RenderPipeline,
610
device: Device,
711
surface: Surface,
812
queue: Queue,
913
vertex_buffer: Buffer,
14+
mouse_pos_buffer: Buffer,
1015
surface_caps: SurfaceCapabilities,
16+
bind_group: BindGroup,
17+
size: (u32, u32),
1118
}
1219

1320
impl<'a> WgpuExample {
@@ -40,6 +47,33 @@ impl<'a> WgpuExample {
4047
usage: wgpu::BufferUsages::VERTEX,
4148
});
4249

50+
let mouse_pos_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
51+
label: Some("mouse position buffer"),
52+
contents: bytemuck::cast_slice(&[0.0, 0.0]),
53+
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
54+
});
55+
let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
56+
label: None,
57+
entries: &[wgpu::BindGroupLayoutEntry {
58+
binding: 0,
59+
visibility: wgpu::ShaderStages::VERTEX,
60+
ty: wgpu::BindingType::Buffer {
61+
ty: wgpu::BufferBindingType::Uniform,
62+
has_dynamic_offset: false,
63+
min_binding_size: None,
64+
},
65+
count: None,
66+
}],
67+
});
68+
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
69+
label: None,
70+
layout: &bind_group_layout,
71+
entries: &[wgpu::BindGroupEntry {
72+
binding: 0,
73+
resource: mouse_pos_buffer.as_entire_binding(),
74+
}],
75+
});
76+
4377
let surface_caps = surface.get_capabilities(&adapter);
4478
let surface_format = surface_caps
4579
.formats
@@ -50,8 +84,8 @@ impl<'a> WgpuExample {
5084
let config = wgpu::SurfaceConfiguration {
5185
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
5286
format: surface_format,
53-
width: 512,
54-
height: 512,
87+
width: WINDOW_SIZE,
88+
height: WINDOW_SIZE,
5589
present_mode: surface_caps.present_modes[0],
5690
alpha_mode: surface_caps.alpha_modes[0],
5791
view_formats: vec![],
@@ -62,7 +96,7 @@ impl<'a> WgpuExample {
6296
let render_pipeline_layout =
6397
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
6498
label: Some("Render Pipeline Layout"),
65-
bind_group_layouts: &[],
99+
bind_group_layouts: &[&bind_group_layout],
66100
push_constant_ranges: &[],
67101
});
68102

@@ -103,7 +137,17 @@ impl<'a> WgpuExample {
103137

104138
surface.configure(&device, &config);
105139

106-
Self { pipeline, device, surface, queue, vertex_buffer, surface_caps }
140+
Self {
141+
pipeline,
142+
device,
143+
surface,
144+
queue,
145+
vertex_buffer,
146+
surface_caps,
147+
bind_group,
148+
mouse_pos_buffer,
149+
size: (WINDOW_SIZE, WINDOW_SIZE),
150+
}
107151
}
108152
}
109153

@@ -133,6 +177,7 @@ impl WindowHandler for WgpuExample {
133177
});
134178

135179
render_pass.set_pipeline(&self.pipeline);
180+
render_pass.set_bind_group(0, &self.bind_group, &[]);
136181
render_pass.set_vertex_buffer(0, self.vertex_buffer.slice(..));
137182
render_pass.draw(0..3, 0..1);
138183
}
@@ -145,29 +190,21 @@ impl WindowHandler for WgpuExample {
145190
) -> baseview::EventStatus {
146191
match event {
147192
baseview::Event::Mouse(MouseEvent::CursorMoved { position, modifiers: _ }) => {
148-
let center_x: f32 = (position.x as f32 - 256.0) / 256.0;
149-
let center_y: f32 = (256.0 - position.y as f32) / 256.0;
150-
let vertices = &[
151-
Vertex { position: [center_x, center_y + 0.25, 0.0], color: [1.0, 0.0, 0.0] },
152-
Vertex {
153-
position: [center_x - 0.25, center_y - 0.25, 0.0],
154-
color: [0.0, 1.0, 0.0],
155-
},
156-
Vertex {
157-
position: [center_x + 0.25, center_y - 0.25, 0.0],
158-
color: [0.0, 0.0, 1.0],
159-
},
160-
];
161-
let vertex_buffer =
162-
self.device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
163-
label: Some("Vertex Buffer"),
164-
contents: bytemuck::cast_slice(vertices),
165-
usage: wgpu::BufferUsages::VERTEX,
166-
});
167-
168-
self.vertex_buffer = vertex_buffer;
193+
let center_x: f32 =
194+
(position.x as f32 - (self.size.0 as f32 / 2.0)) / (self.size.0 as f32 / 2.0);
195+
let center_y: f32 =
196+
((self.size.1 as f32 / 2.0) - position.y as f32) / (self.size.1 as f32 / 2.0);
197+
198+
self.queue.write_buffer(
199+
&self.mouse_pos_buffer,
200+
0,
201+
bytemuck::cast_slice(&[center_x, center_y]),
202+
)
169203
}
170204
baseview::Event::Window(baseview::WindowEvent::Resized(size)) => {
205+
let width = size.physical_size().width;
206+
let height = size.physical_size().height;
207+
171208
let surface_format = self
172209
.surface_caps
173210
.formats
@@ -181,13 +218,13 @@ impl WindowHandler for WgpuExample {
181218
&wgpu::SurfaceConfiguration {
182219
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
183220
format: surface_format,
184-
width: size.physical_size().width,
185-
height: size.physical_size().height,
221+
width,
222+
height,
186223
present_mode: self.surface_caps.present_modes[0],
187224
alpha_mode: self.surface_caps.alpha_modes[0],
188225
view_formats: vec![],
189226
},
190-
)
227+
);
191228
}
192229
_ => {}
193230
}
@@ -226,7 +263,7 @@ impl Vertex {
226263
fn main() {
227264
let window_open_options = WindowOpenOptions {
228265
title: "wgpu on baseview".into(),
229-
size: Size::new(512.0, 512.0),
266+
size: Size::new(WINDOW_SIZE as f64, WINDOW_SIZE as f64),
230267
scale: baseview::WindowScalePolicy::SystemScaleFactor,
231268
gl_config: None,
232269
};

examples/render_wgpu/src/shader.wgsl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
@group(0) @binding(0) var<uniform> mouse_pos: vec2<f32>;
2+
13
struct VertexInput {
24
@location(0) position: vec3<f32>,
35
@location(1) color: vec3<f32>,
@@ -14,7 +16,7 @@ fn vs_main(
1416
) -> VertexOutput {
1517
var out: VertexOutput;
1618
out.color = model.color;
17-
out.clip_position = vec4<f32>(model.position, 1.0);
19+
out.clip_position = vec4<f32>(model.position.x + mouse_pos.x, model.position.y + mouse_pos.y, model.position.z, 1.0);
1820
return out;
1921
}
2022

0 commit comments

Comments
 (0)