Skip to content

Commit

Permalink
core dumps on session end
Browse files Browse the repository at this point in the history
  • Loading branch information
Schmarni-Dev committed Feb 22, 2024
1 parent 4779993 commit 3f27c8d
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 75 deletions.
56 changes: 28 additions & 28 deletions examples/xr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,35 +37,35 @@ fn main() {
.add_plugins(LogDiagnosticsPlugin::default())
.add_plugins(FrameTimeDiagnosticsPlugin)
.add_systems(Startup, setup)
.add_systems(Update, proto_locomotion.run_if(xr_only()))
.insert_resource(PrototypeLocomotionConfig::default())
// .add_systems(Update, proto_locomotion.run_if(xr_only()))
// .insert_resource(PrototypeLocomotionConfig::default())
.add_systems(Startup, spawn_controllers_example)
.add_plugins(HandInputDebugRenderer)
.add_systems(
Update,
draw_interaction_gizmos
.after(update_interactable_states)
.run_if(xr_only()),
)
.add_systems(
Update,
draw_socket_gizmos
.after(update_interactable_states)
.run_if(xr_only()),
)
.add_systems(
Update,
interactions
.before(update_interactable_states)
.run_if(xr_only()),
)
.add_systems(
Update,
socket_interactions.before(update_interactable_states),
)
.add_systems(Update, prototype_interaction_input.run_if(xr_only()))
.add_systems(Update, update_interactable_states)
.add_systems(Update, update_grabbables.after(update_interactable_states))
// .add_plugins(HandInputDebugRenderer)
// .add_systems(
// Update,
// draw_interaction_gizmos
// .after(update_interactable_states)
// .run_if(xr_only()),
// )
// .add_systems(
// Update,
// draw_socket_gizmos
// .after(update_interactable_states)
// .run_if(xr_only()),
// )
// .add_systems(
// Update,
// interactions
// .before(update_interactable_states)
// .run_if(xr_only()),
// )
// .add_systems(
// Update,
// socket_interactions.before(update_interactable_states),
// )
// .add_systems(Update, prototype_interaction_input.run_if(xr_only()))
// .add_systems(Update, update_interactable_states)
// .add_systems(Update, update_grabbables.after(update_interactable_states))
.add_systems(Update, start_stop_session)
.add_event::<InteractionEvent>()
.run();
Expand Down
3 changes: 2 additions & 1 deletion src/graphics/vulkan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ pub fn start_xr_session(
XrSession::Vulkan(session.clone()),
resolution.into(),
swapchain_format.into(),
// TODO: this shouldn't be in here
AtomicBool::new(false).into(),
frame_wait.into(),
Swapchain::Vulkan(SwapchainInner {
Expand All @@ -458,7 +459,7 @@ pub fn start_xr_session(
.into(),
XrInput::new(xr_instance, &session.into_any_graphics())?,
Vec::default().into(),
// Feels wrong to return a FrameState here, we probably should just wait for the next frame
// TODO: Feels wrong to return a FrameState here, we probably should just wait for the next frame
xr::FrameState {
predicted_display_time: xr::Time::from_nanos(1),
predicted_display_period: xr::Duration::from_nanos(1),
Expand Down
65 changes: 52 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
pub mod graphics;
pub mod input;
pub mod passthrough;
pub mod prelude;
pub mod resource_macros;
pub mod resources;
pub mod xr_init;
pub mod xr_input;
pub mod prelude;

use std::sync::atomic::AtomicBool;

Expand All @@ -28,16 +28,14 @@ use openxr as xr;
use passthrough::{PassthroughPlugin, XrPassthroughLayer, XrPassthroughState};
use resources::*;
use xr_init::{
xr_after_wait_only, xr_only, xr_render_only, CleanupXrData, SetupXrData, XrEarlyInitPlugin,
XrHasWaited, XrShouldRender, XrStatus,
xr_after_wait_only, xr_only, xr_render_only, CleanupRenderWorld, CleanupXrData, SetupXrData,
XrCleanup, XrEarlyInitPlugin, XrHasWaited, XrPostCleanup, XrShouldRender, XrStatus,
};
use xr_input::actions::OpenXrActionsPlugin;
use xr_input::controllers::XrControllerType;
use xr_input::actions::XrActionsPlugin;
use xr_input::hands::emulated::HandEmulationPlugin;
use xr_input::hands::hand_tracking::HandTrackingPlugin;
use xr_input::hands::HandPlugin;
use xr_input::xr_camera::XrCameraPlugin;
use xr_input::OpenXrInput;

const VIEW_TYPE: xr::ViewConfigurationType = xr::ViewConfigurationType::PRIMARY_STEREO;

Expand Down Expand Up @@ -112,6 +110,8 @@ impl Plugin for OpenXrPlugin {
app.add_plugins(RenderPlugin::default());
app.insert_resource(XrStatus::Disabled);
}
app.add_systems(XrPostCleanup, clean_resources);
app.add_systems(XrPostCleanup, || info!("Main World Post Cleanup!"));
app.add_systems(
PreUpdate,
xr_poll_events.run_if(|status: Res<XrStatus>| *status != XrStatus::NoInstance),
Expand Down Expand Up @@ -164,9 +164,48 @@ impl Plugin for OpenXrPlugin {
.run_if(not(xr_render_only()))
.in_set(RenderSet::Cleanup),
);
render_app.add_systems(
Render,
clean_resources_render
.run_if(resource_exists::<CleanupRenderWorld>)
.after(RenderSet::ExtractCommands),
);
}
}

fn clean_resources_render(mut cmds: &mut World) {
let session = cmds.remove_resource::<XrSession>().unwrap();
cmds.remove_resource::<XrResolution>();
cmds.remove_resource::<XrFormat>();
// cmds.remove_resource::<XrSessionRunning>();
cmds.remove_resource::<XrFrameWaiter>();
cmds.remove_resource::<XrSwapchain>();
cmds.remove_resource::<XrInput>();
cmds.remove_resource::<XrViews>();
cmds.remove_resource::<XrFrameState>();
cmds.remove_resource::<CleanupRenderWorld>();
// unsafe {
// (session.instance().fp().destroy_session)(session.as_raw());
// }
warn!("Cleanup Resources Render");
}
fn clean_resources(mut cmds: &mut World) {
let session = cmds.remove_resource::<XrSession>().unwrap();
cmds.remove_resource::<XrResolution>();
cmds.remove_resource::<XrFormat>();
// cmds.remove_resource::<XrSessionRunning>();
cmds.remove_resource::<XrFrameWaiter>();
cmds.remove_resource::<XrSwapchain>();
cmds.remove_resource::<XrInput>();
cmds.remove_resource::<XrViews>();
cmds.remove_resource::<XrFrameState>();
// cmds.remove_resource::<CleanupRenderWorld>();
// unsafe {
// (session.instance().fp().destroy_session)(session.as_raw());
// }
warn!("Cleanup Resources");
}

fn xr_skip_frame(
xr_swapchain: Res<XrSwapchain>,
xr_frame_state: Res<XrFrameState>,
Expand Down Expand Up @@ -217,15 +256,15 @@ impl PluginGroup for DefaultXrPlugins {
reqeusted_extensions: self.reqeusted_extensions,
app_info: self.app_info.clone(),
})
.add(XrInitPlugin)
.add(OpenXrInput::new(XrControllerType::OculusTouch))
.add(OpenXrActionsPlugin)
.add_after::<OpenXrPlugin, _>(XrInitPlugin)
// .add(XrInput)
// .add(XrActionsPlugin)
.add(XrCameraPlugin)
.add_before::<OpenXrPlugin, _>(XrEarlyInitPlugin)
.add(HandPlugin)
.add(HandTrackingPlugin)
.add(HandEmulationPlugin)
.add(PassthroughPlugin)
// .add(HandPlugin)
// .add(HandTrackingPlugin)
// .add(HandEmulationPlugin)
// .add(PassthroughPlugin)
.add(XrResourcePlugin)
.set(WindowPlugin {
#[cfg(not(target_os = "android"))]
Expand Down
2 changes: 1 addition & 1 deletion src/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl Plugin for XrResourcePlugin {
app.add_plugins(ExtractResourcePlugin::<XrViews>::default());
app.add_plugins(ExtractResourcePlugin::<XrInput>::default());
app.add_plugins(ExtractResourcePlugin::<XrEnvironmentBlendMode>::default());
app.add_plugins(ExtractResourcePlugin::<XrSessionRunning>::default());
// app.add_plugins(ExtractResourcePlugin::<XrSessionRunning>::default());
app.add_plugins(ExtractResourcePlugin::<XrSession>::default());
}
}
Expand Down
29 changes: 23 additions & 6 deletions src/xr_init/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@ use bevy::{
camera::{ManualTextureView, ManualTextureViews},
extract_resource::{ExtractResource, ExtractResourcePlugin},
renderer::{RenderAdapter, RenderDevice, RenderInstance},
Render, RenderApp, RenderSet,
},
window::{PrimaryWindow, RawHandleWrapper},
};

use crate::{
graphics,
resources::{
OXrSessionSetupInfo, XrFormat, XrInstance, XrResolution, XrSession, XrSessionRunning,
XrSwapchain,
},
clean_resources, graphics,
resources::{OXrSessionSetupInfo, XrFormat, XrInstance, XrResolution, XrSession, XrSwapchain},
LEFT_XR_TEXTURE_HANDLE, RIGHT_XR_TEXTURE_HANDLE,
};

Expand Down Expand Up @@ -52,6 +50,9 @@ pub fn xr_after_wait_only() -> impl FnMut(Res<XrHasWaited>) -> bool {
resource_equals(XrHasWaited(true))
}

#[derive(Resource, Clone, Copy, ExtractResource)]
pub struct CleanupRenderWorld;

impl Plugin for XrEarlyInitPlugin {
fn build(&self, app: &mut App) {
add_schedules(app);
Expand All @@ -67,6 +68,7 @@ impl Plugin for XrInitPlugin {
app.add_plugins(ExtractResourcePlugin::<XrStatus>::default());
app.add_plugins(ExtractResourcePlugin::<XrShouldRender>::default());
app.add_plugins(ExtractResourcePlugin::<XrHasWaited>::default());
app.add_plugins(ExtractResourcePlugin::<CleanupRenderWorld>::default());
app.init_resource::<XrShouldRender>();
app.init_resource::<XrHasWaited>();
app.add_systems(PreUpdate, setup_xr.run_if(on_event::<SetupXrData>()))
Expand All @@ -80,9 +82,25 @@ impl Plugin for XrInitPlugin {
stop_xr_session.run_if(on_event::<EndXrSession>()),
);
app.add_systems(XrSetup, setup_manual_texture_views);
app.add_systems(XrCleanup, set_cleanup_res);
let render_app = app.sub_app_mut(RenderApp);
render_app.add_systems(
Render,
remove_cleanup_res
.in_set(RenderSet::Cleanup)
.after(clean_resources),
);
}
}

fn set_cleanup_res(mut commands: Commands) {
info!("Set Cleanup Res");
commands.insert_resource(CleanupRenderWorld);
}
fn remove_cleanup_res(mut commands: Commands) {
commands.remove_resource::<CleanupRenderWorld>();
}

fn setup_manual_texture_views(
mut manual_texture_views: ResMut<ManualTextureViews>,
swapchain: Res<XrSwapchain>,
Expand Down Expand Up @@ -135,7 +153,6 @@ pub(crate) struct CleanupXrData;
#[allow(clippy::too_many_arguments)]
fn start_xr_session(
mut commands: Commands,
mut setup_xr: EventWriter<SetupXrData>,
mut status: ResMut<XrStatus>,
instance: Res<XrInstance>,
primary_window: Query<&RawHandleWrapper, With<PrimaryWindow>>,
Expand Down
4 changes: 2 additions & 2 deletions src/xr_input/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use super::oculus_touch::ActionSets;

pub use xr::sys::NULL_PATH;

pub struct OpenXrActionsPlugin;
impl Plugin for OpenXrActionsPlugin {
pub struct XrActionsPlugin;
impl Plugin for XrActionsPlugin {
fn build(&self, app: &mut App) {
app.add_systems(PreUpdate, sync_actions.run_if(xr_only()));
app.add_systems(
Expand Down
4 changes: 0 additions & 4 deletions src/xr_input/controllers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,3 @@ pub struct Handed<T> {
pub left: T,
pub right: T,
}
#[derive(Copy, Clone)]
pub enum XrControllerType {
OculusTouch,
}
3 changes: 1 addition & 2 deletions src/xr_input/hand_poses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,7 @@ pub fn get_simulated_open_hand_transforms(hand: Hand) -> [Transform; 26] {
z: 0.01,
},
];
let result = bones_to_transforms(test_hand_bones, hand);
return result;
bones_to_transforms(test_hand_bones, hand)
}

fn bones_to_transforms(hand_bones: [Vec3; 26], hand: Hand) -> [Transform; 26] {
Expand Down
22 changes: 4 additions & 18 deletions src/xr_input/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ pub mod xr_camera;

use crate::resources::{XrInstance, XrSession};
use crate::xr_init::{xr_only, XrCleanup, XrPostSetup, XrPreSetup, XrSetup};
use crate::xr_input::controllers::XrControllerType;
use crate::xr_input::oculus_touch::setup_oculus_controller;
use crate::xr_input::xr_camera::{xr_camera_head_sync, Eye, XRProjection, XrCameraBundle};
use crate::{locate_views, xr_wait_frame};
Expand All @@ -31,7 +30,7 @@ use bevy::transform::TransformSystem;
use bevy::utils::HashMap;
use openxr::Binding;

use self::actions::{setup_oxr_actions, OpenXrActionsPlugin};
use self::actions::{setup_oxr_actions, XrActionsPlugin};
use self::oculus_touch::{init_subaction_path, post_action_setup_oculus_controller, ActionSets};
use self::trackers::{
adopt_open_xr_trackers, update_open_xr_controllers, OpenXRLeftEye, OpenXRRightEye,
Expand All @@ -40,30 +39,17 @@ use self::trackers::{
use self::xr_camera::{/* GlobalTransformExtract, TransformExtract, */ XrCamera};

#[derive(Copy, Clone)]
pub struct OpenXrInput {
pub controller_type: XrControllerType,
}
pub struct XrInput;
#[derive(Clone, Copy, Debug, Ord, PartialOrd, Eq, PartialEq, Component)]
pub enum Hand {
Left,
Right,
}

impl OpenXrInput {
pub fn new(controller_type: XrControllerType) -> Self {
Self { controller_type }
}
}

impl Plugin for OpenXrInput {
impl Plugin for XrInput {
fn build(&self, app: &mut App) {
app.add_systems(XrPostSetup, post_action_setup_oculus_controller);
// why only when the controller is oculus? that is still backed by generic actions
match self.controller_type {
XrControllerType::OculusTouch => {
app.add_systems(XrSetup, setup_oculus_controller);
}
}
app.add_systems(XrSetup, setup_oculus_controller);
//adopt any new trackers
app.add_systems(PreUpdate, adopt_open_xr_trackers.run_if(xr_only()));
// app.add_systems(PreUpdate, action_set_system.run_if(xr_only()));
Expand Down

0 comments on commit 3f27c8d

Please sign in to comment.