Skip to content

Commit

Permalink
bevy_render: Avoid creating multiple surfaces on Android
Browse files Browse the repository at this point in the history
When looking for an adapter Bevy currently creates a one-shot
surface that can be used to find a compatible adapter instead
of referencing the surface that's associated with the
primary window for rendering. This results in creating multiple
render surfaces for a single window which may not be portable
and may also result in large, redundant allocations (e.g.
for a full swap chain)

On Android VkAndroidSurfaceCreateInfoKHR
(which is used by wgpu to create a surface) will throw a
`VK_ERROR_NATIVE_WINDOW_IN_USE_KHR` error if we try to create
multiple surfaces for a single window.

As a workaround for now we don't request an adapter based on a
surface on Android.
  • Loading branch information
rib committed Jun 5, 2022
1 parent 1fb429b commit 1ea04f0
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions crates/bevy_render/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,26 @@ impl Plugin for RenderPlugin {
};

let instance = wgpu::Instance::new(backends);
let surface = {

// FIXME: instead of creating a new surface here we should be aiming to
// reference the surface that's created for the primary window to avoid
// creating multiple render surfaces. (Or the surface created here should
// somehow become the surface for the primary window.)
//
// This is especially important on Android where VkAndroidSurfaceCreateInfoKHR
// (which is used by wgpu to reate a surface) will throw a
// `VK_ERROR_NATIVE_WINDOW_IN_USE_KHR` error if we try to create multiple
// surfaces for a single window. As a workaround for now we don't
// request an adapter based on a surface on Android.
let surface = if cfg!(not(target_os = "android")) {
let windows = app.world.resource_mut::<bevy_window::Windows>();
let raw_handle = windows.get_primary().map(|window| unsafe {
let handle = window.raw_window_handle().get_handle();
instance.create_surface(&handle)
});
raw_handle
};
} else { None };

let request_adapter_options = wgpu::RequestAdapterOptions {
power_preference: options.power_preference,
compatible_surface: surface.as_ref(),
Expand Down

0 comments on commit 1ea04f0

Please sign in to comment.