Skip to content

Commit

Permalink
Avoid creating SurfaceConfiguration in prepare_windows (bevyengin…
Browse files Browse the repository at this point in the history
…e#6255)

# Objective

- Avoids creating a `SurfaceConfiguration` for every window in every frame for the `prepare_windows` system
- As such also avoid calling `get_supported_formats` for every window in every frame

## Solution

- Construct `SurfaceConfiguration` lazyly in `prepare_windows`

---

This also changes the error message for failed initial surface configuration from "Failed to acquire next swapchain texture" to "Error configuring surface".
  • Loading branch information
MDeiml authored and ItsDoot committed Feb 1, 2023
1 parent beee699 commit 699a6d1
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions crates/bevy_render/src/view/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ pub fn prepare_windows(
render_instance.create_surface(&window.raw_handle.as_ref().unwrap().get_handle())
});

let swap_chain_descriptor = wgpu::SurfaceConfiguration {
// Creates a closure to avoid calling this logic unnecessarily
let create_swap_chain_descriptor = || wgpu::SurfaceConfiguration {
format: *surface
.get_supported_formats(&render_adapter)
.get(0)
Expand Down Expand Up @@ -211,22 +212,25 @@ pub fn prepare_windows(

// Do the initial surface configuration if it hasn't been configured yet. Or if size or
// present mode changed.
if window_surfaces.configured_windows.insert(window.id)
let frame = if window_surfaces.configured_windows.insert(window.id)
|| window.size_changed
|| window.present_mode_changed
{
render_device.configure_surface(surface, &swap_chain_descriptor);
}

let frame = match surface.get_current_texture() {
Ok(swap_chain_frame) => swap_chain_frame,
Err(wgpu::SurfaceError::Outdated) => {
render_device.configure_surface(surface, &swap_chain_descriptor);
surface
.get_current_texture()
.expect("Error reconfiguring surface")
render_device.configure_surface(surface, &create_swap_chain_descriptor());
surface
.get_current_texture()
.expect("Error configuring surface")
} else {
match surface.get_current_texture() {
Ok(swap_chain_frame) => swap_chain_frame,
Err(wgpu::SurfaceError::Outdated) => {
render_device.configure_surface(surface, &create_swap_chain_descriptor());
surface
.get_current_texture()
.expect("Error reconfiguring surface")
}
err => err.expect("Failed to acquire next swap chain texture!"),
}
err => err.expect("Failed to acquire next swap chain texture!"),
};

window.swap_chain_texture = Some(TextureView::from(frame));
Expand Down

0 comments on commit 699a6d1

Please sign in to comment.