Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

partially revert scheduling changes #141

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 9 additions & 5 deletions crates/bevy_openxr/examples/raw_actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ use bevy_mod_openxr::{
action_set_attaching::OxrAttachActionSet,
action_set_syncing::{OxrActionSetSyncSet, OxrSyncActionSet},
add_xr_plugins,
init::create_xr_session,
resources::OxrInstance,
session::OxrSession,
spaces::OxrSpaceExt,
};
use bevy_mod_xr::{
session::{session_available, XrCreateSession, XrTrackingRoot},
session::{session_available, session_running, XrSessionCreated, XrTrackingRoot},
spaces::XrSpace,
types::XrPose,
};
Expand All @@ -21,9 +20,14 @@ use openxr::Posef;
fn main() {
let mut app = App::new();
app.add_plugins(add_xr_plugins(DefaultPlugins));
app.add_systems(XrCreateSession, spawn_hands.after(create_xr_session));
app.add_systems(XrCreateSession, attach_set.after(create_xr_session));
app.add_systems(PreUpdate, sync_actions.before(OxrActionSetSyncSet));
app.add_systems(XrSessionCreated, spawn_hands);
app.add_systems(XrSessionCreated, attach_set);
app.add_systems(
PreUpdate,
sync_actions
.before(OxrActionSetSyncSet)
.run_if(session_running),
);
app.add_systems(OxrSendActionBindings, suggest_action_bindings);
app.add_systems(Startup, create_actions.run_if(session_available));
app.add_systems(Startup, setup);
Expand Down
11 changes: 3 additions & 8 deletions crates/bevy_openxr/src/openxr/features/handtracking.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bevy::prelude::*;
use bevy_mod_xr::hands::{LeftHand, RightHand, XrHandBoneEntities, HAND_JOINT_COUNT};
use bevy_mod_xr::session::{XrCreateSession, XrDestroySession, XrTrackingRoot};
use bevy_mod_xr::session::{XrPreDestroySession, XrSessionCreated, XrTrackingRoot};
use bevy_mod_xr::spaces::{XrPrimaryReferenceSpace, XrReferenceSpace};
use bevy_mod_xr::{
hands::{HandBone, HandBoneRadius},
Expand Down Expand Up @@ -28,13 +28,8 @@ impl Plugin for HandTrackingPlugin {
fn build(&self, app: &mut App) {
app.add_systems(PreUpdate, locate_hands.run_if(session_running));
if self.default_hands {
app.add_systems(XrDestroySession, clean_up_default_hands)
.add_systems(
XrCreateSession,
(spawn_default_hands, apply_deferred)
.chain()
.after(create_xr_session),
);
app.add_systems(XrPreDestroySession, clean_up_default_hands)
.add_systems(XrSessionCreated, spawn_default_hands);
}
}
}
Expand Down
61 changes: 48 additions & 13 deletions crates/bevy_openxr/src/openxr/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,30 @@ impl Plugin for OxrInitPlugin {
.add_systems(
XrFirst,
poll_events
.before(XrHandleEvents)
.in_set(XrHandleEvents::Poll)
.run_if(not(state_equals(XrState::Unavailable))),
)
.add_systems(XrCreateSession, create_xr_session)
.add_systems(XrDestroySession, destroy_xr_session)
.add_systems(XrBeginSession, begin_xr_session)
.add_systems(XrEndSession, end_xr_session)
.add_systems(XrRequestExit, request_exit_xr_session)
.add_systems(
XrFirst,
(
create_xr_session
.run_if(state_equals(XrState::Available))
.run_if(on_event::<XrCreateSessionEvent>()),
destroy_xr_session
.run_if(state_matches!(XrState::Exiting { .. }))
.run_if(on_event::<XrDestroySessionEvent>()),
begin_xr_session
.run_if(state_equals(XrState::Ready))
.run_if(on_event::<XrBeginSessionEvent>()),
end_xr_session
.run_if(state_equals(XrState::Stopping))
.run_if(on_event::<XrEndSessionEvent>()),
request_exit_xr_session
.run_if(session_created)
.run_if(on_event::<XrRequestExitEvent>()),
)
.in_set(XrHandleEvents::SessionStateUpdateEvents),
)
.insert_resource(instance.clone())
.insert_resource(system_id)
.insert_resource(XrState::Available)
Expand Down Expand Up @@ -150,7 +166,7 @@ impl Plugin for OxrInitPlugin {

fn finish(&self, app: &mut App) {
app.sub_app_mut(RenderApp)
.add_systems(XrDestroySession, destroy_xr_session);
.add_systems(XrPreDestroySession, destroy_xr_session);
}
}

Expand Down Expand Up @@ -468,9 +484,13 @@ pub fn create_xr_session(world: &mut World) {
Err(e) => error!("Failed to initialize XrSession: {e}"),
}
world.insert_non_send_resource(chain);
world.run_schedule(XrSessionCreated);
world.send_event(XrSessionCreatedEvent);
}

pub fn destroy_xr_session(world: &mut World) {
world.run_schedule(XrPreDestroySession);
world.insert_resource(XrDestroySessionRender);
world.remove_resource::<OxrSession>();
world.remove_resource::<OxrFrameWaiter>();
world.remove_resource::<OxrFrameStream>();
Expand All @@ -480,18 +500,33 @@ pub fn destroy_xr_session(world: &mut World) {
world.insert_resource(XrState::Available);
}

pub fn begin_xr_session(session: Res<OxrSession>, mut session_started: ResMut<OxrSessionStarted>) {
pub fn begin_xr_session(
world: &mut World,
// session: Res<OxrSession>, mut session_started: ResMut<OxrSessionStarted>
) {
let _span = info_span!("xr_begin_session");
session
world
.get_resource::<OxrSession>()
.unwrap()
.begin(openxr::ViewConfigurationType::PRIMARY_STEREO)
.expect("Failed to begin session");
session_started.0 = true;
world.get_resource_mut::<OxrSessionStarted>().unwrap().0 = true;
world.run_schedule(XrPostSessionBegin);
}

pub fn end_xr_session(session: Res<OxrSession>, mut session_started: ResMut<OxrSessionStarted>) {
pub fn end_xr_session(
world: &mut World,
// session: Res<OxrSession>, mut session_started: ResMut<OxrSessionStarted>
) {
// Maybe this could be an event?
world.run_schedule(XrPreSessionEnd);
let _span = info_span!("xr_end_session");
session.end().expect("Failed to end session");
session_started.0 = false;
world
.get_resource::<OxrSession>()
.unwrap()
.end()
.expect("Failed to end session");
world.get_resource_mut::<OxrSessionStarted>().unwrap().0 = false;
}

pub fn request_exit_xr_session(session: Res<OxrSession>) {
Expand Down
11 changes: 4 additions & 7 deletions crates/bevy_openxr/src/openxr/reference_space.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use bevy::{
render::{extract_resource::ExtractResourcePlugin, RenderApp},
};
use bevy_mod_xr::{
session::{XrCreateSession, XrDestroySession},
session::{XrPreDestroySession, XrSessionCreated},
spaces::{XrPrimaryReferenceSpace, XrReferenceSpace},
};

Expand Down Expand Up @@ -38,15 +38,12 @@ impl Plugin for OxrReferenceSpacePlugin {
.insert_resource(OxrDefaultPrimaryReferenceSpaceType(
self.default_primary_ref_space,
))
.add_systems(
XrCreateSession,
set_primary_ref_space.after(create_xr_session),
)
.add_systems(XrDestroySession, cleanup);
.add_systems(XrSessionCreated, set_primary_ref_space)
.add_systems(XrPreDestroySession, cleanup);

let render_app = app.sub_app_mut(RenderApp);

render_app.add_systems(XrDestroySession, cleanup);
render_app.add_systems(XrPreDestroySession, cleanup);
}
}

Expand Down
8 changes: 4 additions & 4 deletions crates/bevy_openxr/src/openxr/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use bevy::{
use bevy_mod_xr::{
camera::{XrCamera, XrCameraBundle, XrProjection},
session::{
XrDestroySession, XrFirst, XrHandleEvents, XrRenderSet, XrRootTransform, XrTrackingRoot,
XrFirst, XrHandleEvents, XrPreDestroySession, XrRenderSet, XrRootTransform, XrTrackingRoot,
},
spaces::XrPrimaryReferenceSpace,
};
Expand Down Expand Up @@ -46,7 +46,7 @@ impl Plugin for OxrRenderPlugin {
ExtractResourcePlugin::<OxrSwapchainImages>::default(),
ExtractResourcePlugin::<OxrViews>::default(),
))
.add_systems(XrDestroySession, clean_views)
.add_systems(XrPreDestroySession, clean_views)
.add_systems(
XrFirst,
(
Expand All @@ -55,7 +55,7 @@ impl Plugin for OxrRenderPlugin {
init_views.run_if(resource_added::<OxrSession>),
)
.chain()
.after(XrHandleEvents),
.in_set(XrHandleEvents::FrameLoop),
)
.add_systems(
PostUpdate,
Expand All @@ -70,7 +70,7 @@ impl Plugin for OxrRenderPlugin {
let render_app = app.sub_app_mut(RenderApp);

render_app
.add_systems(XrDestroySession, clean_views)
.add_systems(XrPreDestroySession, clean_views)
.add_systems(
Render,
(
Expand Down
7 changes: 6 additions & 1 deletion crates/bevy_openxr/src/openxr/spaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ pub struct OxrSpatialPlugin;
impl Plugin for OxrSpatialPlugin {
fn build(&self, app: &mut App) {
app.add_event::<XrDestroySpace>()
.add_systems(XrFirst, destroy_space_event.before(XrHandleEvents))
.add_systems(
XrFirst,
destroy_space_event
.before(XrHandleEvents::Poll)
.run_if(session_available),
)
.add_systems(
PreUpdate,
update_space_transforms
Expand Down
Loading