begin_render_pass works fine without putting the whole thing in nested scope.

pull/539/head
zahash 3 months ago
parent 2bb2f8373b
commit 14d7bd1a9c

@ -435,38 +435,34 @@ We also need to create a `CommandEncoder` to create the actual commands to send
Now we can get to clearing the screen (a long time coming). We need to use the `encoder` to create a `RenderPass`. The `RenderPass` has all the methods for the actual drawing. The code for creating a `RenderPass` is a bit nested, so I'll copy it all here before talking about its pieces. Now we can get to clearing the screen (a long time coming). We need to use the `encoder` to create a `RenderPass`. The `RenderPass` has all the methods for the actual drawing. The code for creating a `RenderPass` is a bit nested, so I'll copy it all here before talking about its pieces.
```rust ```rust
{ encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
let _render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { label: Some("Render Pass"),
label: Some("Render Pass"), color_attachments: &[Some(wgpu::RenderPassColorAttachment {
color_attachments: &[Some(wgpu::RenderPassColorAttachment { view: &view,
view: &view, resolve_target: None,
resolve_target: None, ops: wgpu::Operations {
ops: wgpu::Operations { load: wgpu::LoadOp::Clear(wgpu::Color {
load: wgpu::LoadOp::Clear(wgpu::Color { r: 0.1,
r: 0.1, g: 0.2,
g: 0.2, b: 0.3,
b: 0.3, a: 1.0,
a: 1.0, }),
}), store: wgpu::StoreOp::Store,
store: wgpu::StoreOp::Store, },
}, })],
})], depth_stencil_attachment: None,
depth_stencil_attachment: None, occlusion_query_set: None,
occlusion_query_set: None, timestamp_writes: None,
timestamp_writes: None, });
});
}
// submit will accept anything that implements IntoIter // submit will accept anything that implements IntoIter
self.queue.submit(std::iter::once(encoder.finish())); self.queue.submit([encoder.finish()]);
output.present(); output.present();
Ok(()) Ok(())
} }
``` ```
First things first, let's talk about the extra block (`{}`) around `encoder.begin_render_pass(...)`. `begin_render_pass()` borrows `encoder` mutably (aka `&mut self`). We can't call `encoder.finish()` until we release that mutable borrow. The block tells Rust to drop any variables within it when the code leaves that scope, thus releasing the mutable borrow on `encoder` and allowing us to `finish()` it. If you don't like the `{}`, you can also use `drop(render_pass)` to achieve the same effect.
The last lines of the code tell `wgpu` to finish the command buffer and submit it to the GPU's render queue. The last lines of the code tell `wgpu` to finish the command buffer and submit it to the GPU's render queue.
We need to update the event loop again to call this method. We'll also call `update()` before it, too. We need to update the event loop again to call this method. We'll also call `update()` before it, too.

Loading…
Cancel
Save