learn-wgpu/docs/beginner/tutorial2-surface/README.md

509 lines
20 KiB
Markdown
Raw Normal View History

# The Surface
2019-10-19 20:32:52 +00:00
## First, some housekeeping: State
For convenience, we're going to pack all the fields into a struct and create some methods for it.
2019-10-19 20:32:52 +00:00
```rust
// lib.rs
2021-03-21 23:19:24 +00:00
use winit::window::Window;
2021-03-20 13:45:14 +00:00
2019-10-19 20:32:52 +00:00
struct State {
surface: wgpu::Surface,
device: wgpu::Device,
queue: wgpu::Queue,
2021-08-28 22:11:33 +00:00
config: wgpu::SurfaceConfiguration,
2020-01-11 22:55:08 +00:00
size: winit::dpi::PhysicalSize<u32>,
2019-10-19 20:32:52 +00:00
}
impl State {
2020-08-22 20:53:07 +00:00
// Creating some of the wgpu types requires async code
2020-04-18 23:57:16 +00:00
async fn new(window: &Window) -> Self {
todo!()
2019-10-19 20:32:52 +00:00
}
2020-04-26 00:36:50 +00:00
fn resize(&mut self, new_size: winit::dpi::PhysicalSize<u32>) {
todo!()
2019-10-19 20:32:52 +00:00
}
fn input(&mut self, event: &WindowEvent) -> bool {
todo!()
2019-10-19 20:32:52 +00:00
}
2020-04-26 00:36:50 +00:00
fn update(&mut self) {
todo!()
2019-10-19 20:32:52 +00:00
}
2021-08-28 22:11:33 +00:00
fn render(&mut self) -> Result<(), wgpu::SurfaceError> {
todo!()
2019-10-19 20:32:52 +00:00
}
}
```
I'm glossing over `State`s fields, but they'll make more sense as I explain the code behind these methods.
2019-10-19 20:32:52 +00:00
## State::new()
The code for this is pretty straightforward, but let's break it down a bit.
2019-10-19 20:32:52 +00:00
```rust
impl State {
// ...
2020-04-18 23:57:16 +00:00
async fn new(window: &Window) -> Self {
2019-10-19 20:32:52 +00:00
let size = window.inner_size();
2020-08-22 20:53:07 +00:00
// The instance is a handle to our GPU
// Backends::all => Vulkan + Metal + DX12 + Browser WebGPU
2021-08-28 22:11:33 +00:00
let instance = wgpu::Instance::new(wgpu::Backends::all());
2020-08-22 20:53:07 +00:00
let surface = unsafe { instance.create_surface(window) };
let adapter = instance.request_adapter(
2020-04-18 23:57:16 +00:00
&wgpu::RequestAdapterOptions {
2021-02-07 19:17:22 +00:00
power_preference: wgpu::PowerPreference::default(),
2020-04-18 23:57:16 +00:00
compatible_surface: Some(&surface),
2021-10-09 00:40:29 +00:00
force_fallback_adapter: false,
2020-04-18 23:57:16 +00:00
},
2020-08-22 20:53:07 +00:00
).await.unwrap();
2019-10-19 20:32:52 +00:00
```
### Instance and Adapter
2021-09-22 18:24:20 +00:00
The `instance` is the first thing you create when using wgpu. Its main purpose
is to create `Adapter`s and `Surface`s.
The `adapter` is a handle to our actual graphics card. You can use this to get information about the graphics card such as its name and what backend the adapter uses. We use this to create our `Device` and `Queue` later. Let's discuss the fields of `RequestAdapterOptions`.
2021-10-09 00:40:29 +00:00
* `power_preference` has two variants: `LowPower`, and `HighPerformance`. `LowPower` will pick an adapter that favors battery life, such as an integrated GPU. `HighPerformance` will pick an adapter for more power-hungry yet more performant GPU's such as a dedicated graphics card. WGPU will favor `LowPower` if there is no adapter for the `HighPerformance` option.
2021-10-09 00:40:29 +00:00
* The `compatible_surface` field tells wgpu to find an adapter that can present to the supplied surface.
2021-12-30 14:17:16 +00:00
* The `force_fallback_adapter` forces wgpu to pick an adapter that will work on all hardware. This usually means that the rendering backend will use a "software" system, instead of hardware such as a GPU.
2019-10-19 20:32:52 +00:00
2021-08-09 20:51:54 +00:00
<div class="note">
2021-10-09 00:40:29 +00:00
The options I've passed to `request_adapter` aren't guaranteed to work for all devices, but will work for most of them. If wgpu can't find an adapter with the required permissions, `request_adapter` will return `None`. If you want to get all adapters for a particular backend you can use `enumerate_adapters`. This will give you an iterator that you can loop over to check if one of the adapters works for your needs.
2021-08-09 20:51:54 +00:00
```rust
let adapter = instance
2021-08-28 22:11:33 +00:00
.enumerate_adapters(wgpu::Backends::all())
2021-08-09 20:51:54 +00:00
.filter(|adapter| {
// Check if this adapter supports our surface
!surface.get_supported_formats(&adapter).is_empty()
2021-08-09 20:51:54 +00:00
})
.next()
2021-08-09 20:51:54 +00:00
.unwrap()
```
Another thing to note is that `Adapter`s are locked to a specific backend. If you are on Windows and have 2 graphics cards you'll have at least 4 adapters available to use, 2 Vulkan and 2 DirectX.
For more fields you can use to refine your search, [check out the docs](https://docs.rs/wgpu/latest/wgpu/struct.Adapter.html).
2021-08-09 20:51:54 +00:00
</div>
2021-10-09 00:40:29 +00:00
### The Surface
The `surface` is the part of the window that we draw to. We need it to draw directly to the screen. Our `window` needs to implement [raw-window-handle](https://crates.io/crates/raw-window-handle)'s `HasRawWindowHandle` trait to create a surface. Fortunately, winit's `Window` fits the bill. We also need it to request our `adapter`.
### Device and Queue
Let's use the `adapter` to create the device and queue.
2019-10-19 20:32:52 +00:00
```rust
2020-08-22 20:53:07 +00:00
let (device, queue) = adapter.request_device(
&wgpu::DeviceDescriptor {
features: wgpu::Features::empty(),
// WebGL doesn't support all of wgpu's features, so if
2022-01-02 19:51:57 +00:00
// we're building for the web we'll have to disable some.
limits: if cfg!(target_arch = "wasm32") {
wgpu::Limits::downlevel_webgl2_defaults()
} else {
wgpu::Limits::default()
},
2021-03-08 22:28:27 +00:00
label: None,
2019-10-19 20:32:52 +00:00
},
2020-08-22 20:53:07 +00:00
None, // Trace path
).await.unwrap();
2019-10-19 20:32:52 +00:00
```
2020-08-22 20:53:07 +00:00
2021-06-06 04:09:10 +00:00
The `features` field on `DeviceDescriptor`, allows us to specify what extra features we want. For this simple example, I've decided not to use any extra features.
2020-08-22 20:53:07 +00:00
<div class="note">
The graphics card you have limits the features you can use. If you want to use certain features you may need to limit what devices you support or provide workarounds.
2020-08-22 20:53:07 +00:00
You can get a list of features supported by your device using `adapter.features()`, or `device.features()`.
2022-04-14 22:20:11 +00:00
You can view a full list of features [here](https://docs.rs/wgpu/latest/wgpu/struct.Features.html).
2020-08-22 20:53:07 +00:00
</div>
2022-04-14 22:20:11 +00:00
The `limits` field describes the limit of certain types of resources that we can create. We'll use the defaults for this tutorial, so we can support most devices. You can view a list of limits [here](https://docs.rs/wgpu/latest/wgpu/struct.Limits.html).
2019-10-19 20:32:52 +00:00
```rust
2021-08-28 22:11:33 +00:00
let config = wgpu::SurfaceConfiguration {
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
2022-07-01 23:00:19 +00:00
format: surface.get_supported_formats(&adapter)[0],
2020-01-11 22:55:08 +00:00
width: size.width,
height: size.height,
2020-04-18 23:57:16 +00:00
present_mode: wgpu::PresentMode::Fifo,
2022-10-20 16:07:03 +00:00
alpha_mode: wgpu::CompositeAlphaMode::Auto,
2019-10-19 20:32:52 +00:00
};
2021-08-28 22:11:33 +00:00
surface.configure(&device, &config);
2019-10-19 20:32:52 +00:00
```
Here we are defining a config for our surface. This will define how the surface creates its underlying `SurfaceTexture`s. We will talk about `SurfaceTexture` when we get to the `render` function. For now, let's talk about the config's fields.
2021-12-05 22:47:43 +00:00
The `usage` field describes how `SurfaceTexture`s will be used. `RENDER_ATTACHMENT` specifies that the textures will be used to write to the screen (we'll talk about more `TextureUsages`s later).
2021-12-05 22:47:43 +00:00
The `format` defines how `SurfaceTexture`s will be stored on the gpu. Different displays prefer different formats. We use `surface.get_preferred_format(&adapter)` to figure out the best format to use based on the display you're using.
2019-10-19 20:32:52 +00:00
2021-12-05 22:47:43 +00:00
`width` and `height` are the width and the height in pixels of a `SurfaceTexture`. This should usually be the width and the height of the window.
<div class="warning">
Make sure that the width and height of the `SurfaceTexture` are not 0, as that can cause your app to crash.
</div>
2019-10-19 20:32:52 +00:00
`present_mode` uses `wgpu::PresentMode` enum which determines how to sync the surface with the display. The option we picked, `PresentMode::Fifo`, will cap the display rate at the display's framerate. This is essentially VSync. This mode is guaranteed to be supported on all platforms. There are other options and you can see all of them [in the docs](https://docs.rs/wgpu/latest/wgpu/enum.PresentMode.html)
<div class="note">
If you want to let your users pick what `PresentMode` they use, you can use [Surface::get_supported_modes()](https://docs.rs/wgpu/latest/wgpu/struct.Surface.html#method.get_supported_modes) to get a list of all the `PresentMode`s the surface supports:
```rust
let modes = surface.get_supported_modes(&adapter);
```
Regardless, `PresentMode::Fifo` will always be supported, and `PresentMode::AutoVsync` and `PresentMode::AutoNoVsync` have fallback support and therefore will work on all platforms.
</div>
2019-10-19 20:32:52 +00:00
Now that we've configured our surface properly we can add these new fields at the end of the method.
2019-10-19 20:32:52 +00:00
```rust
Self {
surface,
device,
queue,
2021-08-28 22:11:33 +00:00
config,
2019-10-19 20:32:52 +00:00
size,
}
}
// ...
}
```
Since our `State::new()` method is async we need to change `run()` to be async as well so that we can await it.
2019-10-19 20:32:52 +00:00
```rust
2022-02-18 20:36:21 +00:00
pub async fn run() {
// Window setup...
let mut state = State::new(&window).await;
// Event loop...
}
2019-10-19 20:32:52 +00:00
```
2022-02-18 20:36:21 +00:00
Now that `run()` is async, `main()` will need some way to await the future. We could use a crate like [tokio](https://docs.rs/tokio), or [async-std](https://docs.rs/async-std), but I'm going to go with the much more lightweight [pollster](https://docs.rs/pollster). Add the following to your `Cargo.toml`:
2021-06-19 21:27:55 +00:00
2022-02-18 20:36:21 +00:00
```toml
[dependencies]
# other deps...
pollster = "0.2"
```
We then use the `block_on` function provided by pollster to await our future:
```rust
fn main() {
pollster::block_on(run());
}
```
<div class="warning">
Don't use `block_on` inside of an async function if you plan to support WASM. Futures have to be run using the browser's executor. If you try to bring your own your code will crash when you encounter a future that doesn't execute immediately.
2021-06-19 21:27:55 +00:00
</div>
If we try to build WASM now it will fail because `wasm-bindgen` doesn't support using async functions as `start` methods. You could switch to calling `run` manually in javascript, but for simplicity, we'll add the [wasm-bindgen-futures](https://docs.rs/wasm-bindgen-futures) crate to our WASM dependencies as that doesn't require us to change any code. Your dependencies should look something like this:
2022-02-18 20:36:21 +00:00
```toml
[dependencies]
cfg-if = "1"
2022-10-20 16:07:03 +00:00
winit = "0.27"
2022-02-18 20:36:21 +00:00
env_logger = "0.9"
log = "0.4"
2022-10-20 16:07:03 +00:00
wgpu = "0.14"
2022-02-18 20:36:21 +00:00
pollster = "0.2"
[target.'cfg(target_arch = "wasm32")'.dependencies]
console_error_panic_hook = "0.1.6"
console_log = "0.2.0"
2022-10-20 16:07:03 +00:00
wgpu = { version = "0.14", features = ["webgl"]}
2022-02-18 20:36:21 +00:00
wasm-bindgen = "0.2"
wasm-bindgen-futures = "0.4"
web-sys = { version = "0.3", features = [
"Document",
"Window",
"Element",
]}
```
2020-01-11 22:55:08 +00:00
## resize()
If we want to support resizing in our application, we're going to need to reconfigure the `surface` every time the window's size changes. That's the reason we stored the physical `size` and the `config` used to configure the `surface`. With all of these, the resize method is very simple.
2019-10-19 20:32:52 +00:00
```rust
// impl State
2021-08-08 22:24:18 +00:00
pub fn resize(&mut self, new_size: winit::dpi::PhysicalSize<u32>) {
if new_size.width > 0 && new_size.height > 0 {
self.size = new_size;
2021-08-28 22:11:33 +00:00
self.config.width = new_size.width;
self.config.height = new_size.height;
self.surface.configure(&self.device, &self.config);
2021-08-08 22:24:18 +00:00
}
2019-10-19 20:32:52 +00:00
}
```
There's nothing different here from the initial `surface` configuration, so I won't get into it.
2019-10-19 20:32:52 +00:00
We call this method in `run()` in the event loop for the following events.
2019-10-19 20:32:52 +00:00
```rust
match event {
// ...
2020-05-11 16:24:44 +00:00
} if window_id == window.id() => if !state.input(event) {
match event {
// ...
WindowEvent::Resized(physical_size) => {
state.resize(*physical_size);
}
WindowEvent::ScaleFactorChanged { new_inner_size, .. } => {
2020-08-22 20:53:07 +00:00
// new_inner_size is &&mut so we have to dereference it twice
state.resize(**new_inner_size);
}
// ...
2019-10-19 20:32:52 +00:00
}
```
2019-10-19 22:01:59 +00:00
## input()
2019-10-19 20:32:52 +00:00
`input()` returns a `bool` to indicate whether an event has been fully processed. If the method returns `true`, the main loop won't process the event any further.
We're just going to return false for now because we don't have any events we want to capture.
```rust
// impl State
fn input(&mut self, event: &WindowEvent) -> bool {
false
}
```
We need to do a little more work in the event loop. We want `State` to have priority over `run()`. Doing that (and previous changes) should have your loop looking like this.
2019-10-19 20:32:52 +00:00
```rust
// run()
2019-10-19 20:32:52 +00:00
event_loop.run(move |event, _, control_flow| {
2020-01-11 22:55:08 +00:00
match event {
Event::WindowEvent {
ref event,
window_id,
2020-08-22 20:53:07 +00:00
} if window_id == window.id() => if !state.input(event) { // UPDATED!
2020-01-11 22:55:08 +00:00
match event {
2021-08-04 12:17:21 +00:00
WindowEvent::CloseRequested
| WindowEvent::KeyboardInput {
input:
2020-01-11 22:55:08 +00:00
KeyboardInput {
state: ElementState::Pressed,
virtual_keycode: Some(VirtualKeyCode::Escape),
..
2021-08-04 12:17:21 +00:00
},
..
} => *control_flow = ControlFlow::Exit,
2020-01-11 22:55:08 +00:00
WindowEvent::Resized(physical_size) => {
state.resize(*physical_size);
2020-01-11 22:55:08 +00:00
}
WindowEvent::ScaleFactorChanged { new_inner_size, .. } => {
state.resize(**new_inner_size);
2020-01-11 22:55:08 +00:00
}
2020-04-18 23:57:16 +00:00
_ => {}
2020-08-22 20:53:07 +00:00
}
}
2020-04-18 23:57:16 +00:00
_ => {}
2020-01-11 22:55:08 +00:00
}
});
2019-10-19 20:32:52 +00:00
```
## update()
We don't have anything to update yet, so leave the method empty.
```rust
fn update(&mut self) {
// remove `todo!()`
2019-10-19 20:32:52 +00:00
}
```
We'll add some code here later on to move around objects.
2019-10-19 20:32:52 +00:00
## render()
Here's where the magic happens. First, we need to get a frame to render to.
2019-10-19 20:32:52 +00:00
```rust
// impl State
2021-08-28 22:11:33 +00:00
fn render(&mut self) -> Result<(), wgpu::SurfaceError> {
2021-10-09 00:40:29 +00:00
let output = self.surface.get_current_texture()?;
```
2021-10-09 00:40:29 +00:00
The `get_current_texture` function will wait for the `surface` to provide a new `SurfaceTexture` that we will render to. We'll store this in `output` for later.
```rust
let view = output.texture.create_view(&wgpu::TextureViewDescriptor::default());
2019-10-19 20:32:52 +00:00
```
This line creates a `TextureView` with default settings. We need to do this because we want to control how the render code interacts with the texture.
2019-10-19 20:32:52 +00:00
We also need to create a `CommandEncoder` to create the actual commands to send to the gpu. Most modern graphics frameworks expect commands to be stored in a command buffer before being sent to the gpu. The `encoder` builds a command buffer that we can then send to the gpu.
```rust
let mut encoder = self.device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
2020-04-18 23:57:16 +00:00
label: Some("Render Encoder"),
2019-10-19 20:32:52 +00:00
});
```
Now we can get to clearing the screen (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.
2019-10-19 20:32:52 +00:00
```rust
{
let _render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("Render Pass"),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: &view,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color {
r: 0.1,
g: 0.2,
b: 0.3,
a: 1.0,
}),
store: true,
},
})],
2019-10-19 20:32:52 +00:00
depth_stencil_attachment: None,
});
}
2020-05-11 16:24:44 +00:00
2021-10-15 18:05:03 +00:00
// submit will accept anything that implements IntoIter
self.queue.submit(std::iter::once(encoder.finish()));
2021-10-09 00:40:29 +00:00
output.present();
2020-11-11 22:41:06 +00:00
Ok(())
2019-10-19 20:32:52 +00:00
}
```
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.
2019-10-19 20:32:52 +00:00
The last lines of the code tell `wgpu` to finish the command buffer, and to 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.
2019-10-19 20:32:52 +00:00
```rust
// run()
2019-10-19 20:32:52 +00:00
event_loop.run(move |event, _, control_flow| {
match event {
// ...
2021-12-29 18:57:29 +00:00
Event::RedrawRequested(window_id) if window_id == window.id() => {
state.update();
2020-11-11 22:41:06 +00:00
match state.render() {
Ok(_) => {}
// Reconfigure the surface if lost
2021-08-28 22:11:33 +00:00
Err(wgpu::SurfaceError::Lost) => state.resize(state.size),
2020-11-11 22:41:06 +00:00
// The system is out of memory, we should probably quit
2021-08-28 22:11:33 +00:00
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,
2020-11-11 22:41:06 +00:00
// All other errors (Outdated, Timeout) should be resolved by the next frame
Err(e) => eprintln!("{:?}", e),
}
2020-04-18 23:57:16 +00:00
}
2020-01-11 22:55:08 +00:00
Event::MainEventsCleared => {
2020-04-18 23:57:16 +00:00
// RedrawRequested will only trigger once, unless we manually
// request it.
window.request_redraw();
2019-10-19 20:32:52 +00:00
}
// ...
}
});
```
With all that, you should be getting something that looks like this.
![Window with a blue background](./cleared-window.png)
2019-10-19 20:32:52 +00:00
## Wait, what's going on with RenderPassDescriptor?
Some of you may be able to tell what's going on just by looking at it, but I'd be remiss if I didn't go over it. Let's take a look at the code again.
```rust
&wgpu::RenderPassDescriptor {
label: Some("Render Pass"),
2019-10-19 20:32:52 +00:00
color_attachments: &[
// ...
],
depth_stencil_attachment: None,
}
```
2021-12-30 14:17:16 +00:00
A `RenderPassDescriptor` only has three fields: `label`, `color_attachments` and `depth_stencil_attachment`. The `color_attachments` describe where we are going to draw our color to. We use the `TextureView` we created earlier to make sure that we render to the screen.
<div class="note">
2022-07-15 20:06:36 +00:00
The `color_attachments` field is a "sparse" array. This allows you to use a pipeline that expects multiple render targets and only supply the ones you care about.
</div>
We'll use `depth_stencil_attachment` later, but we'll set it to `None` for now.
2019-10-19 20:32:52 +00:00
```rust
Some(wgpu::RenderPassColorAttachment {
view: &view,
2019-10-19 20:32:52 +00:00
resolve_target: None,
2020-08-22 20:53:07 +00:00
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color {
r: 0.1,
g: 0.2,
b: 0.3,
a: 1.0,
}),
store: true,
},
})
2019-10-19 20:32:52 +00:00
```
The `RenderPassColorAttachment` has the `view` field which informs `wgpu` what texture to save the colors to. In this case we specify the `view` that we created using `surface.get_current_texture()`. This means that any colors we draw to this attachment will get drawn to the screen.
2019-10-19 20:32:52 +00:00
The `resolve_target` is the texture that will receive the resolved output. This will be the same as `view` unless multisampling is enabled. We don't need to specify this, so we leave it as `None`.
2020-08-22 20:53:07 +00:00
2022-04-10 17:48:13 +00:00
The `ops` field takes a `wpgu::Operations` object. This tells wgpu what to do with the colors on the screen (specified by `view`). The `load` field tells wgpu how to handle colors stored from the previous frame. Currently, we are clearing the screen with a bluish color. The `store` field tells wgpu whether we want to store the rendered results to the `Texture` behind our `TextureView` (in this case it's the `SurfaceTexture`). We use `true` as we do want to store our render results.
2020-08-22 20:53:07 +00:00
<div class="note">
It's not uncommon to not clear the screen if the screen is going to be completely covered up with objects. If your scene doesn't cover the entire screen however you can end up with something like this.
2019-10-19 20:32:52 +00:00
2020-08-22 20:53:07 +00:00
![./no-clear.png](./no-clear.png)
2019-10-19 20:32:52 +00:00
2020-08-22 20:53:07 +00:00
</div>
2019-10-19 20:32:52 +00:00
2021-09-07 23:17:08 +00:00
## Validation Errors?
If wgpu is using Vulkan on your machine, you may run into validation errors if you are running an older version of the Vulkan SDK. You should be using at least version `1.2.182` as older versions can give out some false positives. If errors persist, you may have encountered a bug in wgpu. You can post an issue at [https://github.com/gfx-rs/wgpu](https://github.com/gfx-rs/wgpu)
2019-10-19 20:32:52 +00:00
## Challenge
Modify the `input()` method to capture mouse events, and update the clear color using that. *Hint: you'll probably need to use `WindowEvent::CursorMoved`*.
<WasmExample example="tutorial2_surface"></WasmExample>
2022-04-17 18:08:44 +00:00
<AutoGithubLink/>