Skip to content

Commit

Permalink
Force main thread for prepare_windows system (#11)
Browse files Browse the repository at this point in the history
Force main thread for prepare_windows system
  • Loading branch information
TheRawMeatball authored and cart committed Jul 24, 2021
1 parent d7230a8 commit 8d7b0e6
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pipelined/bevy_render2/src/view/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@ use bevy_window::{RawWindowHandleWrapper, WindowId, Windows};
use std::ops::{Deref, DerefMut};
use wgpu::TextureFormat;

// Token to ensure a system runs on the main thread.
#[derive(Default)]
pub struct NonSendMarker;

pub struct WindowRenderPlugin;

impl Plugin for WindowRenderPlugin {
fn build(&self, app: &mut App) {
let render_app = app.sub_app_mut(0);
render_app
.init_resource::<WindowSurfaces>()
.init_resource::<NonSendMarker>()
.add_system_to_stage(RenderStage::Extract, extract_windows.system())
.add_system_to_stage(RenderStage::Prepare, prepare_windows.system());
}
Expand Down Expand Up @@ -77,6 +82,9 @@ pub struct WindowSurfaces {
}

pub fn prepare_windows(
// By accessing a NonSend resource, we tell the scheduler to put this system on the main thread,
// which is necessary for some OS s
_marker: NonSend<NonSendMarker>,
mut windows: ResMut<ExtractedWindows>,
mut window_surfaces: ResMut<WindowSurfaces>,
render_device: Res<RenderDevice>,
Expand All @@ -88,6 +96,7 @@ pub fn prepare_windows(
.surfaces
.entry(window.id)
.or_insert_with(|| unsafe {
// NOTE: On some OSes this MUST be called from the main thread.
render_instance.create_surface(&window.handle.get_handle())
});

Expand Down

0 comments on commit 8d7b0e6

Please sign in to comment.