Skip to content

Commit b8a6caf

Browse files
Fix clippy, rewrite the render pass abstraction
1 parent 4e6eb03 commit b8a6caf

9 files changed

Lines changed: 183 additions & 242 deletions

File tree

src/png/grammar.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ impl ImageHeader {
3939
pub(crate) const fn num_bytes_per_pixel(&self) -> usize {
4040
let bits_per_pixel = self.color_type.num_channels() * self.bit_depth;
4141

42-
((bits_per_pixel + 7) / 8) as usize
42+
bits_per_pixel.div_ceil(8) as usize
4343
}
4444
}
4545

src/renderer/app_state.rs

Lines changed: 61 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,21 @@ use crate::{
22
png::grammar::Png,
33
renderer::{
44
draw_uniform::DrawUniform,
5-
feature_uniform::{
6-
FeatureUniform,
7-
TransformAction,
8-
},
9-
gpu_state::{
10-
GpuResourceAllocator,
11-
Shader,
12-
},
5+
feature_uniform::{FeatureUniform, TransformAction},
6+
gpu_state::GpuResourceAllocator,
137
mouse_state::MouseState,
14-
shape::{
15-
compute_radius,
16-
Shape,
17-
ShapeStack,
18-
},
8+
shader::Shader,
9+
shape::{compute_radius, Shape, ShapeStack},
1910
},
2011
};
2112
use anyhow::Result;
22-
use wgpu::SurfaceError;
13+
2314
use winit::{
2415
dpi::PhysicalSize,
25-
event::{
26-
ElementState,
27-
Event,
28-
KeyEvent,
29-
MouseButton,
30-
WindowEvent,
31-
},
16+
event::{ElementState, Event, KeyEvent, MouseButton, WindowEvent},
3217
event_loop::EventLoop,
33-
keyboard::{
34-
KeyCode,
35-
PhysicalKey,
36-
},
37-
window::{
38-
CursorIcon,
39-
Window,
40-
WindowBuilder,
41-
},
18+
keyboard::{KeyCode, PhysicalKey},
19+
window::{CursorIcon, Window, WindowBuilder},
4220
};
4321

4422
/// AppState is the state that is created by user input.
@@ -245,8 +223,56 @@ impl<'a> AppState<'a> {
245223
.write_uniform_buffer(&uniform_resources[1].resource, self.draw_uniform);
246224
}
247225

248-
pub(crate) fn render(&self) -> Result<(), SurfaceError> {
249-
self.gpu_allocator.render(&self.image_shader)?;
226+
pub(crate) fn render(&self) -> Result<(), wgpu::SurfaceError> {
227+
let (output, view, mut encoder) = self.gpu_allocator.begin_frame()?;
228+
229+
// Image shader render pass
230+
{
231+
let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
232+
label: Some("content render pass"),
233+
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
234+
view: &view,
235+
resolve_target: None,
236+
ops: wgpu::Operations {
237+
load: wgpu::LoadOp::Clear(wgpu::Color {
238+
r: 0.1,
239+
g: 0.2,
240+
b: 0.3,
241+
a: 1.0,
242+
}),
243+
store: wgpu::StoreOp::Store,
244+
},
245+
})],
246+
depth_stencil_attachment: None,
247+
occlusion_query_set: None,
248+
timestamp_writes: None,
249+
});
250+
251+
render_pass.set_pipeline(&self.image_shader.render_pipeline);
252+
253+
let mut i = 0;
254+
255+
self.image_shader.texture_resources.iter().for_each(|r| {
256+
render_pass.set_bind_group(i, &r.bind_group, &[]);
257+
i += 1;
258+
});
259+
260+
self.image_shader.uniform_resources.iter().for_each(|r| {
261+
render_pass.set_bind_group(i, &r.bind_group, &[]);
262+
i += 1;
263+
});
264+
265+
render_pass.set_vertex_buffer(0, self.gpu_allocator.vertex_buffer.slice(..));
266+
render_pass.set_index_buffer(
267+
self.gpu_allocator.index_buffer.slice(..),
268+
wgpu::IndexFormat::Uint16,
269+
);
270+
271+
render_pass.draw_indexed(0..self.gpu_allocator.num_indices(), 0, 0..1);
272+
}
273+
274+
self.gpu_allocator.end_frame(encoder);
275+
output.present();
250276

251277
Ok(())
252278
}
@@ -330,17 +356,17 @@ pub async fn run(png: Png) -> anyhow::Result<()> {
330356
match state.render() {
331357
Ok(_) => {}
332358
// Reconfigure the surface if it's lost or outdated
333-
Err(SurfaceError::Lost | SurfaceError::Outdated) => {
359+
Err(wgpu::SurfaceError::Lost | wgpu::SurfaceError::Outdated) => {
334360
state.resize(state.size)
335361
}
336362
// The system is out of memory, we should probably quit
337-
Err(SurfaceError::OutOfMemory) => {
363+
Err(wgpu::SurfaceError::OutOfMemory) => {
338364
log::error!("OutOfMemory");
339365
control_flow.exit();
340366
}
341367

342368
// This happens when a frame takes too long to present
343-
Err(SurfaceError::Timeout) => {
369+
Err(wgpu::SurfaceError::Timeout) => {
344370
log::warn!("Surface timeout")
345371
}
346372
}

src/renderer/draw_uniform.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ impl DrawUniform {
2121
self.crosshair == 1
2222
}
2323

24-
pub(crate) fn toggle_crosshair(&mut self) {
24+
pub(crate) const fn toggle_crosshair(&mut self) {
2525
self.crosshair = !self.crosshair() as u32;
2626
}
2727

28-
pub(crate) fn set_circle_center(&mut self, x: f32, y: f32) {
28+
pub(crate) const fn set_circle_center(&mut self, x: f32, y: f32) {
2929
self.circle_center_x = x;
3030
self.circle_center_y = y;
3131
}
3232

33-
pub(crate) fn set_circle_radius(&mut self, radius: f32) {
33+
pub(crate) const fn set_circle_radius(&mut self, radius: f32) {
3434
self.circle_radius = radius;
3535
}
3636
}

src/renderer/feature_uniform.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl FeatureUniform {
3535
}
3636
}
3737

38-
pub(crate) fn reset_features(&mut self) {
38+
pub(crate) const fn reset_features(&mut self) {
3939
self.grayscale = 0;
4040
// self.sepia = 0;
4141
self.invert = 0;
@@ -59,15 +59,15 @@ impl FeatureUniform {
5959
self.invert == 1
6060
}
6161

62-
pub(crate) fn toggle_grayscale(&mut self) {
62+
pub(crate) const fn toggle_grayscale(&mut self) {
6363
self.grayscale = !self.grayscale() as u32;
6464
}
6565

6666
// pub(crate) fn toggle_sepia(&mut self) {
6767
// self.sepia = !self.sepia() as u32;
6868
// }
6969

70-
pub(crate) fn toggle_invert(&mut self) {
70+
pub(crate) const fn toggle_invert(&mut self) {
7171
self.invert = !self.invert() as u32;
7272
}
7373
}
@@ -81,7 +81,7 @@ impl FeatureUniform {
8181
self.blur == 1
8282
}
8383

84-
pub(crate) fn toggle_blur(&mut self) {
84+
pub(crate) const fn toggle_blur(&mut self) {
8585
self.blur = !self.blur() as u32;
8686
}
8787

@@ -103,7 +103,7 @@ impl FeatureUniform {
103103
self.sharpen == 1
104104
}
105105

106-
pub(crate) fn toggle_sharpen(&mut self) {
106+
pub(crate) const fn toggle_sharpen(&mut self) {
107107
self.sharpen = !self.sharpen() as u32;
108108
}
109109

@@ -117,7 +117,7 @@ impl FeatureUniform {
117117
}
118118

119119
impl FeatureUniform {
120-
pub(crate) fn update_window_dimensions(&mut self, width: u32, height: u32) {
120+
pub(crate) const fn update_window_dimensions(&mut self, width: u32, height: u32) {
121121
self.width = width;
122122
self.height = height;
123123
}
@@ -128,7 +128,7 @@ impl FeatureUniform {
128128
self.edge_detect == 1
129129
}
130130

131-
pub(crate) fn toggle_edge_detect(&mut self) {
131+
pub(crate) const fn toggle_edge_detect(&mut self) {
132132
self.edge_detect = !self.edge_detect() as u32;
133133
}
134134
}

0 commit comments

Comments
 (0)