diff --git a/crates/bevy_openxr/src/openxr/features/handtracking.rs b/crates/bevy_openxr/src/openxr/features/handtracking.rs index f681cda..f784c3b 100644 --- a/crates/bevy_openxr/src/openxr/features/handtracking.rs +++ b/crates/bevy_openxr/src/openxr/features/handtracking.rs @@ -39,7 +39,7 @@ fn spawn_default_hands( session: Res, root: Query>, ) { - info!("spawning hands"); + dbg!("spawning default hands"); let Ok(root) = root.get_single() else { error!("unable to get tracking root, skipping hand creation"); return; @@ -71,6 +71,7 @@ fn spawn_default_hands( for bone in HandBone::get_all_bones() { let bone_left = cmds .spawn(( + DefaultHandBone, SpatialBundle::default(), bone, HandBoneRadius(0.0), @@ -79,6 +80,7 @@ fn spawn_default_hands( .id(); let bone_right = cmds .spawn(( + DefaultHandBone, SpatialBundle::default(), bone, HandBoneRadius(0.0), @@ -107,15 +109,15 @@ fn spawn_default_hands( #[derive(Component)] struct DefaultHandTracker; #[derive(Component)] -struct DefaultHandBones; +struct DefaultHandBone; #[allow(clippy::type_complexity)] fn clean_up_default_hands( mut cmds: Commands, - query: Query, With)>>, + query: Query, With)>>, ) { for e in &query { - info!("removing_hand_entity"); + dbg!("removing default hand entity"); cmds.entity(e).despawn_recursive(); } } diff --git a/crates/bevy_openxr/src/openxr/init.rs b/crates/bevy_openxr/src/openxr/init.rs index c9200ed..32d1c84 100644 --- a/crates/bevy_openxr/src/openxr/init.rs +++ b/crates/bevy_openxr/src/openxr/init.rs @@ -23,7 +23,6 @@ use bevy_xr::session::BeginXrSession; use bevy_xr::session::CreateXrSession; use bevy_xr::session::DestroyXrSession; use bevy_xr::session::EndXrSession; -use bevy_xr::session::XrRenderSessionEnding; use bevy_xr::session::XrSessionExiting; use bevy_xr::session::XrSharedStatus; use bevy_xr::session::XrStatus; @@ -167,7 +166,7 @@ impl Plugin for OxrInitPlugin { Render, destroy_xr_session_render .run_if(resource_equals(OxrCleanupSession(true))) - .after(RenderSet::ExtractCommands), + .after(RenderSet::Cleanup), ) .add_systems( ExtractSchedule, @@ -455,7 +454,6 @@ pub fn create_xr_session( system_id: Res, mut commands: Commands, ) { - info!("creating session!"); match init_xr_session( device.wgpu_device(), &instance, @@ -481,7 +479,6 @@ pub fn create_xr_session( pub fn begin_xr_session(session: Res, session_started: Res) { let _span = info_span!("xr_begin_session"); - info!("begining session!"); session .begin(openxr::ViewConfigurationType::PRIMARY_STEREO) .expect("Failed to begin session"); @@ -490,7 +487,6 @@ pub fn begin_xr_session(session: Res, session_started: Res, session_started: Res) { let _span = info_span!("xr_end_session"); - info!("ending session!"); session .request_exit() .expect("Failed to request session exit"); @@ -553,7 +549,7 @@ pub fn poll_events( SessionState::STOPPING => XrStatus::Stopping, SessionState::EXITING | SessionState::LOSS_PENDING => { session_status_events.send(OxrSessionStatusEvent::AboutToBeDestroyed); - info!("sending destroy info"); + info!("sending destroy info"); XrStatus::Exiting } _ => unreachable!(), @@ -583,17 +579,7 @@ pub fn destroy_xr_session_render(world: &mut World) { world.remove_resource::(); world.remove_resource::(); world.remove_resource::(); - world.run_schedule(XrRenderSessionEnding); + world.run_schedule(XrSessionExiting); world.run_system_once(apply_deferred); - if let Some(sess) = world.remove_resource::() { - // This is needed because there is one space that survives the session exit schedule and - // holds on to the session. this causes an error message but does not seem to cause any - // actuall issues. - unsafe { - (sess.instance().fp().destroy_session)(sess.as_raw()); - } - // leaking the session so that it does not call destroy_session at a later point. might not - // actually be needed. - Box::leak(Box::new(sess)); - } + world.remove_resource::(); } diff --git a/crates/bevy_openxr/src/openxr/reference_space.rs b/crates/bevy_openxr/src/openxr/reference_space.rs index 2516c03..96a4d61 100644 --- a/crates/bevy_openxr/src/openxr/reference_space.rs +++ b/crates/bevy_openxr/src/openxr/reference_space.rs @@ -2,11 +2,12 @@ use std::sync::Arc; use bevy::{ prelude::*, - render::extract_resource::{ExtractResource, ExtractResourcePlugin}, -}; -use bevy_xr::session::{ - status_changed_to, XrRenderSessionEnding, XrSessionCreated, XrSessionExiting, XrStatus, + render::{ + extract_resource::{ExtractResource, ExtractResourcePlugin}, + RenderApp, + }, }; +use bevy_xr::session::{status_changed_to, XrSessionCreated, XrSessionExiting, XrStatus}; use crate::{init::OxrPreUpdateSet, session::OxrSession}; @@ -33,11 +34,14 @@ pub struct OxrReferenceSpace(pub openxr::Space); impl Plugin for OxrReferenceSpacePlugin { fn build(&self, app: &mut App) { - app.insert_resource(OxrDefaultPrimaryReferenceSpaceType(self.default_primary_ref_space)); + app.insert_resource(OxrDefaultPrimaryReferenceSpaceType( + self.default_primary_ref_space, + )); app.add_plugins(ExtractResourcePlugin::::default()); app.add_systems(XrSessionCreated, set_primary_ref_space); app.add_systems(XrSessionExiting, cleanup); - app.add_systems(XrRenderSessionEnding, cleanup); + app.sub_app_mut(RenderApp) + .add_systems(XrSessionExiting, cleanup); } } diff --git a/crates/bevy_openxr/src/openxr/resources.rs b/crates/bevy_openxr/src/openxr/resources.rs index db8b07d..cc143d7 100644 --- a/crates/bevy_openxr/src/openxr/resources.rs +++ b/crates/bevy_openxr/src/openxr/resources.rs @@ -3,7 +3,6 @@ use std::sync::Arc; use bevy::prelude::*; use bevy::render::extract_resource::ExtractResource; -use openxr::AnyGraphics; use crate::error::OxrError; use crate::graphics::*; @@ -217,11 +216,6 @@ pub struct OxrFrameWaiter(pub openxr::FrameWaiter); /// Graphics agnostic wrapper around [openxr::Swapchain] #[derive(Resource)] pub struct OxrSwapchain(pub GraphicsWrap); -impl Drop for OxrSwapchain { - fn drop(&mut self) { - info!("Dropping Swapchain"); - } -} impl GraphicsType for OxrSwapchain { type Inner = openxr::Swapchain; diff --git a/crates/bevy_openxr/src/openxr/session.rs b/crates/bevy_openxr/src/openxr/session.rs index f770b91..d3125a5 100644 --- a/crates/bevy_openxr/src/openxr/session.rs +++ b/crates/bevy_openxr/src/openxr/session.rs @@ -3,13 +3,12 @@ use crate::resources::{ OxrCleanupSession, OxrPassthrough, OxrPassthroughLayer, OxrSessionStarted, OxrSwapchain, }; use crate::types::{Result, SwapchainCreateInfo}; -use bevy::ecs::system::{RunSystemOnce, SystemState}; +use bevy::ecs::event::ManualEventReader; +use bevy::ecs::system::RunSystemOnce; use bevy::prelude::*; -use bevy::render::extract_resource::ExtractResourcePlugin; use bevy::render::RenderApp; use bevy_xr::session::{ - status_changed_to, XrRenderSessionEnding, XrSessionCreated, XrSessionExiting, XrSharedStatus, - XrStatus, + status_changed_to, XrSessionCreated, XrSessionExiting, XrSharedStatus, XrStatus, }; use openxr::AnyGraphics; @@ -32,7 +31,7 @@ impl Plugin for OxrSessionPlugin { ); app.add_systems(XrSessionExiting, clean_session); app.sub_app_mut(RenderApp) - .add_systems(XrRenderSessionEnding, |mut cmds: Commands| { + .add_systems(XrSessionExiting, |mut cmds: Commands| { cmds.remove_resource::(); }); app.add_systems( @@ -56,10 +55,23 @@ fn clean_session(world: &mut World) { .set(XrStatus::Available); } +#[derive(Resource, Default)] +struct SessionStatusReader(ManualEventReader); + fn run_session_status_schedules(world: &mut World) { - let mut state = SystemState::>::new(world); - let mut e = state.get_mut(world); - let events = e.read().copied().collect::>(); + let mut reader = world + .remove_resource::() + .unwrap_or_default(); + + let events = reader + .0 + .read( + world + .get_resource::>() + .unwrap(), + ) + .copied() + .collect::>(); for e in events.iter() { match e { OxrSessionStatusEvent::Created => { @@ -73,6 +85,7 @@ fn run_session_status_schedules(world: &mut World) { } } } + world.insert_resource(reader); } /// Graphics agnostic wrapper around [openxr::Session]. @@ -88,11 +101,6 @@ pub struct OxrSession( /// This is so that we can still operate on functions that don't take [`AnyGraphics`] as the generic. pub(crate) GraphicsWrap, ); -impl Drop for OxrSession { - fn drop(&mut self) { - info!("dropping session"); - } -} impl GraphicsType for OxrSession { type Inner = openxr::Session; diff --git a/crates/bevy_xr/src/session.rs b/crates/bevy_xr/src/session.rs index 9da4b2a..e70b835 100644 --- a/crates/bevy_xr/src/session.rs +++ b/crates/bevy_xr/src/session.rs @@ -23,7 +23,7 @@ impl Plugin for XrSessionPlugin { // This is in finnish because we need the RenderPlugin to already be added. app.get_sub_app_mut(RenderApp) .unwrap() - .init_schedule(XrRenderSessionEnding); + .init_schedule(XrSessionExiting); } } @@ -44,9 +44,6 @@ pub struct XrSessionCreated; #[derive(ScheduleLabel, Clone, Copy, PartialEq, Eq, Debug, Hash)] pub struct XrSessionExiting; -#[derive(ScheduleLabel, Clone, Copy, PartialEq, Eq, Debug, Hash)] -pub struct XrRenderSessionEnding; - #[derive(Event, Clone, Copy, Deref)] pub struct XrStatusChanged(pub XrStatus);