Skip to content

Commit 3b7330d

Browse files
authored
Rects as App state (#2)
1 parent fc18815 commit 3b7330d

File tree

3 files changed

+76
-20
lines changed

3 files changed

+76
-20
lines changed

src/app.rs

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,63 @@ use winit::event::WindowEvent;
55
use winit::event_loop::ActiveEventLoop;
66
use winit::window::{Window, WindowId};
77

8+
use crate::gpu;
89
use crate::gpu::Gpu;
910

10-
#[derive(Default)]
11+
#[derive(Copy, Clone, Debug)]
12+
struct Rect {
13+
pos: [u32; 2],
14+
size: [u32; 2],
15+
}
16+
17+
impl Rect {
18+
pub fn new(x: u32, y: u32, w: u32, h: u32) -> Self {
19+
Self {
20+
pos: [x, y],
21+
size: [w, h],
22+
}
23+
}
24+
25+
pub fn to_instance(&self) -> gpu::Instance {
26+
gpu::Instance::new(
27+
self.pos[0] as f32,
28+
self.pos[1] as f32,
29+
self.size[0] as f32,
30+
self.size[1] as f32,
31+
)
32+
}
33+
}
34+
1135
pub struct App<'window> {
1236
window: Option<Arc<Window>>,
1337
gpu: Option<Gpu<'window>>,
38+
rects: Vec<Rect>,
39+
}
40+
41+
impl App<'_> {
42+
pub fn new() -> Self {
43+
Self {
44+
window: None,
45+
gpu: None,
46+
rects: Vec::new(),
47+
}
48+
}
49+
50+
pub fn add_rect(&mut self, x: u32, y: u32, w: u32, h: u32) {
51+
self.rects.push(Rect::new(x, y, w, h));
52+
self.sync_gpu_instance_buffer();
53+
}
54+
55+
fn rects_to_instances(&self) -> Vec<gpu::Instance> {
56+
self.rects.iter().map(|r| r.to_instance()).collect()
57+
}
58+
59+
fn sync_gpu_instance_buffer(&mut self) {
60+
let instances = self.rects_to_instances();
61+
if let Some(gpu) = self.gpu.as_mut() {
62+
gpu.update_instance_buffer(&instances);
63+
}
64+
}
1465
}
1566

1667
impl<'window> ApplicationHandler for App<'window> {
@@ -23,7 +74,7 @@ impl<'window> ApplicationHandler for App<'window> {
2374
.expect("create window err."),
2475
);
2576
self.window = Some(window.clone());
26-
let gpu = Gpu::new(window.clone());
77+
let gpu = Gpu::new(window.clone(), self.rects_to_instances());
2778
self.gpu = Some(gpu);
2879
}
2980
}

src/gpu.rs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ use winit::window::Window;
1111

1212
#[repr(C)]
1313
#[derive(Copy, Clone, Debug, Pod, Zeroable)]
14-
struct Instance {
14+
pub struct Instance {
1515
pos: [f32; 2],
1616
size: [f32; 2],
1717
}
1818

1919
impl Instance {
20-
fn new(x: f32, y: f32, width: f32, height: f32) -> Self {
20+
pub fn new(x: f32, y: f32, width: f32, height: f32) -> Self {
2121
Self {
2222
pos: [x, y],
2323
size: [width, height],
@@ -40,11 +40,11 @@ pub struct Gpu<'window> {
4040
}
4141

4242
impl<'window> Gpu<'window> {
43-
pub fn new(window: Arc<Window>) -> Gpu<'window> {
44-
pollster::block_on(Gpu::new_async(window))
43+
pub fn new(window: Arc<Window>, instances: Vec<Instance>) -> Gpu<'window> {
44+
pollster::block_on(Gpu::new_async(window, instances))
4545
}
4646

47-
pub async fn new_async(window: Arc<Window>) -> Gpu<'window> {
47+
pub async fn new_async(window: Arc<Window>, instances: Vec<Instance>) -> Gpu<'window> {
4848
/*
4949
* window
5050
*/
@@ -100,16 +100,6 @@ impl<'window> Gpu<'window> {
100100
range: 0..push_const_size,
101101
};
102102

103-
/*
104-
* rects
105-
*/
106-
107-
let rects: Vec<Instance> = vec![
108-
Instance::new(100.0, 100.0, 200.0, 200.0),
109-
Instance::new(400.0, 100.0, 200.0, 200.0),
110-
Instance::new(700.0, 100.0, 200.0, 200.0),
111-
];
112-
113103
/*
114104
* vertices
115105
*/
@@ -142,10 +132,10 @@ impl<'window> Gpu<'window> {
142132

143133
let instance_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
144134
label: Some("Instance Buffer"),
145-
contents: cast_slice(&rects),
135+
contents: cast_slice(&instances),
146136
usage: wgpu::BufferUsages::VERTEX,
147137
});
148-
let instance_count = rects.len() as u32;
138+
let instance_count = instances.len() as u32;
149139

150140
/*
151141
* shader
@@ -290,4 +280,15 @@ impl<'window> Gpu<'window> {
290280
self.queue.submit(Some(encoder.finish()));
291281
frame.present();
292282
}
283+
284+
pub fn update_instance_buffer(&mut self, instances: &[Instance]) {
285+
self.instance_buffer = self
286+
.device
287+
.create_buffer_init(&wgpu::util::BufferInitDescriptor {
288+
label: Some("Instance Buffer"),
289+
contents: cast_slice(instances),
290+
usage: wgpu::BufferUsages::VERTEX,
291+
});
292+
self.instance_count = instances.len() as u32;
293+
}
293294
}

src/main.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ mod app;
66
mod gpu;
77

88
fn main() -> Result<(), EventLoopError> {
9+
let mut app = App::new();
10+
app.add_rect(100, 100, 250, 250);
11+
app.add_rect(200, 150, 250, 250);
12+
app.add_rect(300, 200, 250, 250);
13+
914
let event_loop = EventLoop::new().unwrap();
1015
event_loop.set_control_flow(ControlFlow::Poll);
11-
let mut app = App::default();
1216
event_loop.run_app(&mut app)
1317
}

0 commit comments

Comments
 (0)