Skip to content

Commit a908c96

Browse files
committed
Refactored winit/window/render to support all wgpu surface targets
1 parent 12f71a8 commit a908c96

File tree

12 files changed

+565
-337
lines changed

12 files changed

+565
-337
lines changed

crates/bevy_internal/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ bevy_render = [
199199
"bevy_image",
200200
"bevy_color/wgpu-types",
201201
"bevy_color/encase",
202+
"bevy_winit?/bevy_render",
202203
]
203204

204205
# Enable assertions to check the validity of parameters passed to glam

crates/bevy_render/src/lib.rs

+31-21
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ use bevy_ecs::schedule::ScheduleBuildSettings;
7676
use bevy_utils::prelude::default;
7777
pub use extract_param::Extract;
7878

79-
use bevy_window::{PrimaryWindow, RawHandleWrapperHolder};
79+
use bevy_window::PrimaryWindow;
8080
use experimental::occlusion_culling::OcclusionCullingPlugin;
8181
use globals::GlobalsPlugin;
8282
use render_asset::{
@@ -88,6 +88,7 @@ use settings::RenderResources;
8888
use sync_world::{
8989
despawn_temporary_render_entities, entity_sync_system, SyncToRenderWorld, SyncWorldPlugin,
9090
};
91+
use view::surface_target::SurfaceTargetSource;
9192

9293
use crate::gpu_readback::GpuReadbackPlugin;
9394
use crate::{
@@ -107,7 +108,7 @@ use bevy_ecs::{prelude::*, schedule::ScheduleLabel};
107108
use bitflags::bitflags;
108109
use core::ops::{Deref, DerefMut};
109110
use std::sync::Mutex;
110-
use tracing::debug;
111+
use tracing::{debug, warn};
111112

112113
/// Contains the default Bevy rendering backend based on wgpu.
113114
///
@@ -314,12 +315,17 @@ impl Plugin for RenderPlugin {
314315
future_render_resources_wrapper.clone(),
315316
));
316317

317-
let primary_window = app
318+
let primary_window_surface_target_source = app
318319
.world_mut()
319-
.query_filtered::<&RawHandleWrapperHolder, With<PrimaryWindow>>()
320+
.query_filtered::<&SurfaceTargetSource, With<PrimaryWindow>>()
320321
.single(app.world())
321322
.ok()
322323
.cloned();
324+
325+
if primary_window_surface_target_source.is_none() {
326+
warn!("No PrimaryWindow found with a SurfaceTargetSource to use for render initialization");
327+
}
328+
323329
let settings = render_creation.clone();
324330
let async_renderer = async move {
325331
let instance = wgpu::Instance::new(&wgpu::InstanceDescriptor {
@@ -335,26 +341,30 @@ impl Plugin for RenderPlugin {
335341
},
336342
});
337343

338-
let surface = primary_window.and_then(|wrapper| {
339-
let maybe_handle = wrapper.0.lock().expect(
340-
"Couldn't get the window handle in time for renderer initialization",
341-
);
342-
if let Some(wrapper) = maybe_handle.as_ref() {
343-
// SAFETY: Plugins should be set up on the main thread.
344-
let handle = unsafe { wrapper.get_handle() };
345-
Some(
346-
instance
347-
.create_surface(handle)
348-
.expect("Failed to create wgpu surface"),
349-
)
350-
} else {
351-
None
352-
}
353-
});
344+
let is_main_thread = true; // Plugins are set up on the main thread.
345+
let compatible_surface = primary_window_surface_target_source
346+
.and_then(|source| {
347+
match source.create_surface(&instance, is_main_thread) {
348+
Ok(surface) => Some(surface),
349+
Err(err) => {
350+
warn!(
351+
"Unable to create surface for adapter init: {:?}",
352+
err
353+
);
354+
None
355+
}
356+
}
357+
})
358+
.map(|render_surface| {
359+
// SAFETY:
360+
// - This surface is just used for compatibility checks when creating the adapter. It's not configured.
361+
// - The surface is dropped at the end of this function; guaranteeing it does not outlive the window.
362+
unsafe { render_surface.into_inner() }
363+
});
354364

355365
let request_adapter_options = wgpu::RequestAdapterOptions {
356366
power_preference: settings.power_preference,
357-
compatible_surface: surface.as_ref(),
367+
compatible_surface: compatible_surface.as_ref(),
358368
..Default::default()
359369
};
360370

crates/bevy_render/src/view/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ impl Plugin for ViewPlugin {
127127
// `TextureView`s need to be dropped before reconfiguring window surfaces.
128128
clear_view_attachments
129129
.in_set(RenderSet::ManageViews)
130-
.before(create_surfaces),
130+
.before(create_send_surfaces)
131+
.before(create_non_send_surfaces),
131132
prepare_view_attachments
132133
.in_set(RenderSet::ManageViews)
133134
.before(prepare_view_targets)

0 commit comments

Comments
 (0)