Skip to content

Commit 0f60759

Browse files
committed
Handle 0 sized swapchains correctly
By never creating them This allows minimise to work as expected
1 parent d7a31bc commit 0f60759

File tree

1 file changed

+53
-48
lines changed

1 file changed

+53
-48
lines changed

rust-gpu-toy/src/main.rs

Lines changed: 53 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ struct State {
5757
queue: wgpu::Queue,
5858

5959
sc_desc: wgpu::SwapChainDescriptor,
60-
swap_chain: wgpu::SwapChain,
60+
swap_chain: Option<wgpu::SwapChain>,
6161
size: winit::dpi::PhysicalSize<u32>,
6262

6363
sampler: wgpu::Sampler,
@@ -127,7 +127,7 @@ impl State {
127127
device,
128128
queue,
129129
sc_desc,
130-
swap_chain,
130+
swap_chain: Some(swap_chain),
131131
size,
132132
sampler,
133133
compute_bind_group: compute_group,
@@ -137,15 +137,19 @@ impl State {
137137
}
138138

139139
fn resize(&mut self, new_size: PhysicalSize<u32>) {
140-
self.sc_desc.width = new_size.width;
141-
self.sc_desc.height = new_size.height;
142-
self.size = new_size;
143-
let new_swap_chain = self.device.create_swap_chain(&self.surface, &self.sc_desc);
144-
self.swap_chain = new_swap_chain;
145-
let (compute_bind_group, copy_bind_group) =
146-
State::bind_for_size(&self.device, &self.sampler, new_size, &self.layouts);
147-
self.compute_bind_group = compute_bind_group;
148-
self.copy_bind_group = copy_bind_group;
140+
if new_size.width != 0 && new_size.height != 0 {
141+
self.sc_desc.width = new_size.width;
142+
self.sc_desc.height = new_size.height;
143+
self.size = new_size;
144+
let new_swap_chain = self.device.create_swap_chain(&self.surface, &self.sc_desc);
145+
self.swap_chain = Some(new_swap_chain);
146+
let (compute_bind_group, copy_bind_group) =
147+
State::bind_for_size(&self.device, &self.sampler, new_size, &self.layouts);
148+
self.compute_bind_group = compute_bind_group;
149+
self.copy_bind_group = copy_bind_group;
150+
} else {
151+
self.swap_chain = None;
152+
}
149153
}
150154

151155
fn bind_for_size(
@@ -310,46 +314,47 @@ impl State {
310314
}
311315

312316
fn render_frame(&self) {
313-
let frame = self
314-
.swap_chain
315-
.get_current_frame()
316-
.expect("error getting frame from swap chain")
317-
.output;
317+
if let Some(chain) = &self.swap_chain {
318+
let frame = chain
319+
.get_current_frame()
320+
.expect("error getting frame from swap chain")
321+
.output;
318322

319-
let i_time: f32 = 0.5 + self.start_time.elapsed().as_micros() as f32 * 1e-6;
320-
let size = self.size;
321-
let config = Config {
322-
width: size.width,
323-
height: size.height,
324-
time: i_time,
325-
};
323+
let i_time: f32 = 0.5 + self.start_time.elapsed().as_micros() as f32 * 1e-6;
324+
let size = self.size;
325+
let config = Config {
326+
width: size.width,
327+
height: size.height,
328+
time: i_time,
329+
};
326330

327-
let mut encoder = self.device.create_command_encoder(&Default::default());
328-
{
329-
let mut cpass = encoder.begin_compute_pass(&Default::default());
330-
cpass.set_pipeline(&self.pipelines.compute_pipeline);
331-
cpass.set_push_constants(0, bytemuck::bytes_of(&config));
332-
cpass.set_bind_group(0, &self.compute_bind_group, &[]);
333-
cpass.dispatch(size.width / 16, size.height / 16, 1);
334-
}
335-
{
336-
let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
337-
label: None,
338-
color_attachments: &[wgpu::RenderPassColorAttachment {
339-
view: &frame.view,
340-
resolve_target: None,
341-
ops: wgpu::Operations {
342-
load: wgpu::LoadOp::Clear(wgpu::Color::GREEN),
343-
store: true,
344-
},
345-
}],
346-
depth_stencil_attachment: None,
347-
});
348-
rpass.set_pipeline(&self.pipelines.copy_pipeline);
349-
rpass.set_bind_group(0, &self.copy_bind_group, &[]);
350-
rpass.draw(0..3, 0..2);
331+
let mut encoder = self.device.create_command_encoder(&Default::default());
332+
{
333+
let mut cpass = encoder.begin_compute_pass(&Default::default());
334+
cpass.set_pipeline(&self.pipelines.compute_pipeline);
335+
cpass.set_push_constants(0, bytemuck::bytes_of(&config));
336+
cpass.set_bind_group(0, &self.compute_bind_group, &[]);
337+
cpass.dispatch(size.width / 16, size.height / 16, 1);
338+
}
339+
{
340+
let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
341+
label: None,
342+
color_attachments: &[wgpu::RenderPassColorAttachment {
343+
view: &frame.view,
344+
resolve_target: None,
345+
ops: wgpu::Operations {
346+
load: wgpu::LoadOp::Clear(wgpu::Color::GREEN),
347+
store: true,
348+
},
349+
}],
350+
depth_stencil_attachment: None,
351+
});
352+
rpass.set_pipeline(&self.pipelines.copy_pipeline);
353+
rpass.set_bind_group(0, &self.copy_bind_group, &[]);
354+
rpass.draw(0..3, 0..2);
355+
}
356+
self.queue.submit(Some(encoder.finish()));
351357
}
352-
self.queue.submit(Some(encoder.finish()));
353358
}
354359
}
355360

0 commit comments

Comments
 (0)