-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Arc WinitWindow and extract to render world #12524
Closed
Closed
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
69d6040
Fix panic when closing a window and using pipelined rendering.
s-puig 60c8a79
Linting
s-puig 70e20f9
Add "multi-threaded" feature flag to bevy_winit
s-puig dba2a75
Add conditional import
s-puig 5af0b42
Arc winit windows and extract to render world
s-puig 49bea78
Revert all changes
s-puig 872644a
Merge branch 'arc-winit' into fix-close-panic
s-puig 407ce5b
Linting
s-puig 2ea5885
Merge branch 'main' into fix-close-panic
s-puig 51aa339
Disable for wasm
s-puig d644165
Linting
s-puig bb08055
CI fix
s-puig 61c5168
Docs CI fix
s-puig fd273fd
Merge branch 'main' into fix-close-panic
s-puig c095cf8
Fix doc CI
s-puig File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
//! Everything should be kept private since it's unlikely anyone needs to use this directly. | ||
//! Instead, abstractions should be used like [`ExtractedWindow`](crate::view::window::ExtractedWindow). | ||
|
||
use crate::{Extract, ExtractSchedule, RenderApp}; | ||
use bevy_app::{App, Plugin}; | ||
use bevy_ecs::entity::{Entity, EntityHashMap}; | ||
use bevy_ecs::system::{NonSend, NonSendMut, Resource}; | ||
use bevy_winit::winit; | ||
use std::sync::Arc; | ||
|
||
/// This [`Plugin`] extracts [`WinitWindows`](bevy_winit::WinitWindows) into render world. | ||
/// This is needed to avoid crashes *when using pipelined rendering* since the winit window can be | ||
/// modified or removed from app world. | ||
pub struct WinitWindowRenderPlugin; | ||
|
||
impl Plugin for WinitWindowRenderPlugin { | ||
fn build(&self, app: &mut App) { | ||
if let Ok(render_app) = app.get_sub_app_mut(RenderApp) { | ||
render_app | ||
.init_non_send_resource::<ExtractedWinitWindows>() | ||
.add_systems(ExtractSchedule, extract_winit_windows); | ||
} | ||
} | ||
} | ||
|
||
#[allow(dead_code)] | ||
struct ExtractedWinitWindow { | ||
entity: Entity, | ||
window: Arc<winit::window::Window>, | ||
window_id: winit::window::WindowId, | ||
} | ||
|
||
#[derive(Resource, Default)] | ||
struct ExtractedWinitWindows { | ||
windows: EntityHashMap<ExtractedWinitWindow>, | ||
} | ||
|
||
fn extract_winit_windows( | ||
mut extracted_windows: NonSendMut<ExtractedWinitWindows>, | ||
windows: Extract<NonSend<bevy_winit::WinitWindows>>, | ||
) { | ||
extracted_windows.windows.clear(); | ||
for (&window_id, window) in windows.windows.iter() { | ||
let entity = windows | ||
.winit_to_entity | ||
.get(&window_id) | ||
.expect("Window has no entity mapped. This should be impossible."); | ||
extracted_windows.windows.insert( | ||
*entity, | ||
ExtractedWinitWindow { | ||
window_id, | ||
window: window.clone(), | ||
entity: *entity, | ||
}, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This potentially hard-couples bevy_render and bevy_winit. I'm not sure if we want to do this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm open to alternatives, but i don't see a way around it if we want to properly fix it. There is always the option of using a hack (delaying the winit window removal in app world).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@s-puig Take a look at this: Friz64@0561754
It also works as a fix, and avoids the hard-coupling to
bevy_winit
. I'm not convinced that dynamic dispatch is the most elegant solution here.