From 7792b29aa40785a37f49d51f0241e6329c91a185 Mon Sep 17 00:00:00 2001 From: TheRawMeatball Date: Wed, 30 Jun 2021 00:43:56 +0300 Subject: [PATCH] Force main thread for prepare_windows system (#11) Force main thread for prepare_windows system --- pipelined/bevy_render2/src/view/window.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pipelined/bevy_render2/src/view/window.rs b/pipelined/bevy_render2/src/view/window.rs index 760b8e1a69..3173da919c 100644 --- a/pipelined/bevy_render2/src/view/window.rs +++ b/pipelined/bevy_render2/src/view/window.rs @@ -11,6 +11,10 @@ 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 { @@ -18,6 +22,7 @@ impl Plugin for WindowRenderPlugin { let render_app = app.sub_app_mut(0); render_app .init_resource::() + .init_resource::() .add_system_to_stage(RenderStage::Extract, extract_windows.system()) .add_system_to_stage(RenderStage::Prepare, prepare_windows.system()); } @@ -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, mut windows: ResMut, mut window_surfaces: ResMut, render_device: Res, @@ -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()) });