Skip to content

Commit 43012ea

Browse files
committed
It Works
1 parent a583f1a commit 43012ea

18 files changed

Lines changed: 341 additions & 505 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "wgpu-bootstrap"
3-
version = "0.3.2"
3+
version = "0.4.0"
44
edition = "2021"
55

66
[dependencies]

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ Add the library to your `cargo.toml`. Use the `tag` key to specify the version.
99

1010
```toml
1111
[dependencies]
12-
wgpu-bootstrap = { git = "https://github.com/qlurkin/wgpu-bootstrap", tag = "v0.3.2" }
12+
wgpu-bootstrap = { git = "https://github.com/qlurkin/wgpu-bootstrap", tag = "v0.4.0" }
1313
bytemuck = { version = "1.18", features = ["derive"] }
14-
pollster = "0.3"
1514
```
1615

1716
## Example

examples/cube/cube_app.rs

Lines changed: 14 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
use wgpu_bootstrap::{
22
cgmath,
3-
context::Context,
4-
runner::App,
53
util::orbit_camera::{CameraUniform, OrbitCamera},
6-
wgpu::{self, util::DeviceExt, TextureView},
4+
wgpu::{self, util::DeviceExt},
5+
App, Context,
76
};
8-
use winit::event::{DeviceEvent, WindowEvent};
97

108
#[repr(C)]
119
#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
@@ -158,9 +156,7 @@ pub struct CubeApp {
158156
}
159157

160158
impl CubeApp {
161-
pub fn new(context: &mut Context) -> Self {
162-
context.window().set_title("Cube App");
163-
159+
pub fn new(context: &Context) -> Self {
164160
let index_buffer = context
165161
.device()
166162
.create_buffer_init(&wgpu::util::BufferInitDescriptor {
@@ -216,7 +212,7 @@ impl CubeApp {
216212
module: &shader,
217213
entry_point: "fs_main",
218214
targets: &[Some(wgpu::ColorTargetState {
219-
format: context.config().format,
215+
format: context.format(),
220216
blend: Some(wgpu::BlendState::REPLACE),
221217
write_mask: wgpu::ColorWrites::ALL,
222218
})],
@@ -235,7 +231,7 @@ impl CubeApp {
235231
conservative: false,
236232
},
237233
depth_stencil: Some(wgpu::DepthStencilState {
238-
format: *context.depth_format(),
234+
format: context.depth_stencil_format(),
239235
depth_write_enabled: true,
240236
depth_compare: wgpu::CompareFunction::Less,
241237
stencil: wgpu::StencilState::default(),
@@ -253,7 +249,7 @@ impl CubeApp {
253249
let mut camera = OrbitCamera::new(
254250
context,
255251
45.0,
256-
(context.config().width as f32) / (context.config().height as f32),
252+
context.size().x / context.size().y,
257253
0.1,
258254
100.0,
259255
);
@@ -273,58 +269,15 @@ impl CubeApp {
273269
}
274270

275271
impl App for CubeApp {
276-
fn window_event(&mut self, context: &mut Context, event: &WindowEvent) -> bool {
277-
return self.camera.window_event(context, event);
278-
}
279-
280-
fn device_event(&mut self, context: &mut Context, event: &DeviceEvent) -> bool {
281-
return self.camera.device_event(context, event);
272+
fn input(&mut self, input: eframe::egui::InputState, context: &Context) {
273+
self.camera.input(input, context);
282274
}
283275

284-
fn render(&mut self, context: &mut Context, view: &TextureView) {
285-
let mut encoder =
286-
context
287-
.device()
288-
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
289-
label: Some("Render Encoder"),
290-
});
291-
292-
{
293-
let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
294-
label: Some("Render Pass"),
295-
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
296-
view,
297-
resolve_target: None,
298-
ops: wgpu::Operations {
299-
load: wgpu::LoadOp::Clear(wgpu::Color {
300-
r: 1.0,
301-
g: 1.0,
302-
b: 1.0,
303-
a: 1.0,
304-
}),
305-
store: wgpu::StoreOp::Store,
306-
},
307-
})],
308-
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
309-
view: context.depth_texture_view(),
310-
depth_ops: Some(wgpu::Operations {
311-
load: wgpu::LoadOp::Clear(1.0),
312-
store: wgpu::StoreOp::Store,
313-
}),
314-
stencil_ops: None,
315-
}),
316-
occlusion_query_set: None,
317-
timestamp_writes: None,
318-
});
319-
320-
render_pass.set_pipeline(&self.render_pipeline);
321-
render_pass.set_vertex_buffer(0, self.vertex_buffer.slice(..));
322-
render_pass.set_index_buffer(self.index_buffer.slice(..), wgpu::IndexFormat::Uint32);
323-
render_pass.set_bind_group(0, self.camera.bind_group(), &[]);
324-
render_pass.draw_indexed(0..self.num_indices, 0, 0..1);
325-
}
326-
327-
// submit will accept anything that implements IntoIter
328-
context.queue().submit(std::iter::once(encoder.finish()));
276+
fn render(&self, render_pass: &mut wgpu::RenderPass<'_>) {
277+
render_pass.set_pipeline(&self.render_pipeline);
278+
render_pass.set_vertex_buffer(0, self.vertex_buffer.slice(..));
279+
render_pass.set_index_buffer(self.index_buffer.slice(..), wgpu::IndexFormat::Uint32);
280+
render_pass.set_bind_group(0, self.camera.bind_group(), &[]);
281+
render_pass.draw_indexed(0..self.num_indices, 0, 0..1);
329282
}
330283
}

examples/cube/main.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
mod cube_app;
22

3+
use std::sync::Arc;
4+
35
use crate::cube_app::CubeApp;
4-
use wgpu_bootstrap::runner::Runner;
6+
use wgpu_bootstrap::Runner;
57

68
fn main() {
7-
let mut runner = Runner::new(Box::new(|context| Box::new(CubeApp::new(context))));
8-
pollster::block_on(runner.run());
9+
let mut runner = Runner::new(
10+
"Cube App",
11+
800,
12+
600,
13+
32,
14+
0,
15+
Box::new(|context| Arc::new(CubeApp::new(context))),
16+
);
17+
runner.run();
918
}

examples/gui/gui_app.rs

Lines changed: 18 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
use wgpu_bootstrap::{
2-
cgmath,
3-
context::Context,
4-
egui,
5-
runner::App,
2+
cgmath, egui,
63
util::orbit_camera::{CameraUniform, OrbitCamera},
7-
wgpu::{self, util::DeviceExt, TextureView},
4+
wgpu::{self, util::DeviceExt},
5+
App, Context,
86
};
9-
use winit::event::{DeviceEvent, WindowEvent};
107

118
#[repr(C)]
129
#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
@@ -160,9 +157,7 @@ pub struct GuiApp {
160157
}
161158

162159
impl GuiApp {
163-
pub fn new(context: &mut Context) -> Self {
164-
context.window().set_title("Gui App");
165-
160+
pub fn new(context: &Context) -> Self {
166161
let index_buffer = context
167162
.device()
168163
.create_buffer_init(&wgpu::util::BufferInitDescriptor {
@@ -218,7 +213,7 @@ impl GuiApp {
218213
module: &shader,
219214
entry_point: "fs_main",
220215
targets: &[Some(wgpu::ColorTargetState {
221-
format: context.config().format,
216+
format: context.format(),
222217
blend: Some(wgpu::BlendState::REPLACE),
223218
write_mask: wgpu::ColorWrites::ALL,
224219
})],
@@ -237,7 +232,7 @@ impl GuiApp {
237232
conservative: false,
238233
},
239234
depth_stencil: Some(wgpu::DepthStencilState {
240-
format: *context.depth_format(),
235+
format: context.depth_stencil_format(),
241236
depth_write_enabled: true,
242237
depth_compare: wgpu::CompareFunction::Less,
243238
stencil: wgpu::StencilState::default(),
@@ -255,7 +250,7 @@ impl GuiApp {
255250
let mut camera = OrbitCamera::new(
256251
context,
257252
45.0,
258-
(context.config().width as f32) / (context.config().height as f32),
253+
context.size().x / context.size().y,
259254
0.1,
260255
100.0,
261256
);
@@ -276,67 +271,24 @@ impl GuiApp {
276271
}
277272

278273
impl App for GuiApp {
279-
fn window_event(&mut self, context: &mut Context, event: &WindowEvent) -> bool {
280-
return self.camera.window_event(context, event);
281-
}
282-
283-
fn device_event(&mut self, context: &mut Context, event: &DeviceEvent) -> bool {
284-
return self.camera.device_event(context, event);
274+
fn input(&mut self, input: egui::InputState, context: &Context) {
275+
self.camera.input(input, context);
285276
}
286277

287-
fn render(&mut self, context: &mut Context, view: &TextureView) {
288-
let mut encoder =
289-
context
290-
.device()
291-
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
292-
label: Some("Render Encoder"),
293-
});
294-
295-
{
296-
let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
297-
label: Some("Render Pass"),
298-
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
299-
view,
300-
resolve_target: None,
301-
ops: wgpu::Operations {
302-
load: wgpu::LoadOp::Clear(wgpu::Color {
303-
r: 1.0,
304-
g: 1.0,
305-
b: 1.0,
306-
a: 1.0,
307-
}),
308-
store: wgpu::StoreOp::Store,
309-
},
310-
})],
311-
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
312-
view: context.depth_texture_view(),
313-
depth_ops: Some(wgpu::Operations {
314-
load: wgpu::LoadOp::Clear(1.0),
315-
store: wgpu::StoreOp::Store,
316-
}),
317-
stencil_ops: None,
318-
}),
319-
occlusion_query_set: None,
320-
timestamp_writes: None,
321-
});
322-
323-
render_pass.set_pipeline(&self.render_pipeline);
324-
render_pass.set_vertex_buffer(0, self.vertex_buffer.slice(..));
325-
render_pass.set_index_buffer(self.index_buffer.slice(..), wgpu::IndexFormat::Uint32);
326-
render_pass.set_bind_group(0, self.camera.bind_group(), &[]);
327-
render_pass.draw_indexed(0..self.num_indices, 0, 0..1);
328-
}
329-
330-
// submit will accept anything that implements IntoIter
331-
context.queue().submit(std::iter::once(encoder.finish()));
278+
fn render(&self, render_pass: &mut wgpu::RenderPass<'_>) {
279+
render_pass.set_pipeline(&self.render_pipeline);
280+
render_pass.set_vertex_buffer(0, self.vertex_buffer.slice(..));
281+
render_pass.set_index_buffer(self.index_buffer.slice(..), wgpu::IndexFormat::Uint32);
282+
render_pass.set_bind_group(0, self.camera.bind_group(), &[]);
283+
render_pass.draw_indexed(0..self.num_indices, 0, 0..1);
332284
}
333285

334-
fn update(&mut self, _context: &mut Context, delta_time: f32) {
286+
fn update(&mut self, delta_time: f32, _context: &Context) {
335287
self.fps = 1.0 / delta_time;
336288
}
337289

338-
fn render_gui(&mut self, context: &mut Context, egui_context: &egui::Context) {
339-
egui::Window::new("Params").show(egui_context, |ui| {
290+
fn render_gui(&mut self, egui_ctx: &egui::Context, context: &Context) {
291+
egui::Window::new("Params").show(egui_ctx, |ui| {
340292
let mut radius = self.camera.radius();
341293
ui.add(egui::Slider::new(&mut radius, 2.0..=10.0).text("radius"));
342294
self.camera.set_radius(radius).update(context);

examples/gui/main.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
mod gui_app;
22

3+
use std::sync::Arc;
4+
35
use crate::gui_app::GuiApp;
4-
use wgpu_bootstrap::runner::Runner;
6+
use wgpu_bootstrap::Runner;
57

68
fn main() {
7-
let mut runner = Runner::new(Box::new(|context| Box::new(GuiApp::new(context))));
8-
pollster::block_on(runner.run());
9+
let mut runner = Runner::new(
10+
"Gui App",
11+
800,
12+
600,
13+
32,
14+
0,
15+
Box::new(|context| Arc::new(GuiApp::new(context))),
16+
);
17+
runner.run();
918
}

0 commit comments

Comments
 (0)