diff --git a/examples/xr.rs b/examples/xr.rs index faefc7a..fb47651 100644 --- a/examples/xr.rs +++ b/examples/xr.rs @@ -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::() .run(); diff --git a/src/graphics/vulkan.rs b/src/graphics/vulkan.rs index 914dc29..b53ed02 100644 --- a/src/graphics/vulkan.rs +++ b/src/graphics/vulkan.rs @@ -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 { @@ -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), diff --git a/src/lib.rs b/src/lib.rs index 2066452..675553b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; @@ -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; @@ -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| *status != XrStatus::NoInstance), @@ -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::) + .after(RenderSet::ExtractCommands), + ); } } +fn clean_resources_render(mut cmds: &mut World) { + let session = cmds.remove_resource::().unwrap(); + cmds.remove_resource::(); + cmds.remove_resource::(); + // cmds.remove_resource::(); + cmds.remove_resource::(); + cmds.remove_resource::(); + cmds.remove_resource::(); + cmds.remove_resource::(); + cmds.remove_resource::(); + cmds.remove_resource::(); + // 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::().unwrap(); + cmds.remove_resource::(); + cmds.remove_resource::(); + // cmds.remove_resource::(); + cmds.remove_resource::(); + cmds.remove_resource::(); + cmds.remove_resource::(); + cmds.remove_resource::(); + cmds.remove_resource::(); + // cmds.remove_resource::(); + // unsafe { + // (session.instance().fp().destroy_session)(session.as_raw()); + // } + warn!("Cleanup Resources"); +} + fn xr_skip_frame( xr_swapchain: Res, xr_frame_state: Res, @@ -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::(XrInitPlugin) + // .add(XrInput) + // .add(XrActionsPlugin) .add(XrCameraPlugin) .add_before::(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"))] diff --git a/src/resources.rs b/src/resources.rs index 305c76a..ba780d7 100644 --- a/src/resources.rs +++ b/src/resources.rs @@ -66,7 +66,7 @@ impl Plugin for XrResourcePlugin { app.add_plugins(ExtractResourcePlugin::::default()); app.add_plugins(ExtractResourcePlugin::::default()); app.add_plugins(ExtractResourcePlugin::::default()); - app.add_plugins(ExtractResourcePlugin::::default()); + // app.add_plugins(ExtractResourcePlugin::::default()); app.add_plugins(ExtractResourcePlugin::::default()); } } diff --git a/src/xr_init/mod.rs b/src/xr_init/mod.rs index 70ac9c9..f04f5f0 100644 --- a/src/xr_init/mod.rs +++ b/src/xr_init/mod.rs @@ -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, }; @@ -52,6 +50,9 @@ pub fn xr_after_wait_only() -> impl FnMut(Res) -> 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); @@ -67,6 +68,7 @@ impl Plugin for XrInitPlugin { app.add_plugins(ExtractResourcePlugin::::default()); app.add_plugins(ExtractResourcePlugin::::default()); app.add_plugins(ExtractResourcePlugin::::default()); + app.add_plugins(ExtractResourcePlugin::::default()); app.init_resource::(); app.init_resource::(); app.add_systems(PreUpdate, setup_xr.run_if(on_event::())) @@ -80,9 +82,25 @@ impl Plugin for XrInitPlugin { stop_xr_session.run_if(on_event::()), ); 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::(); +} + fn setup_manual_texture_views( mut manual_texture_views: ResMut, swapchain: Res, @@ -135,7 +153,6 @@ pub(crate) struct CleanupXrData; #[allow(clippy::too_many_arguments)] fn start_xr_session( mut commands: Commands, - mut setup_xr: EventWriter, mut status: ResMut, instance: Res, primary_window: Query<&RawHandleWrapper, With>, diff --git a/src/xr_input/actions.rs b/src/xr_input/actions.rs index 940f40a..84b3273 100644 --- a/src/xr_input/actions.rs +++ b/src/xr_input/actions.rs @@ -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( diff --git a/src/xr_input/controllers.rs b/src/xr_input/controllers.rs index dd3c743..e13294f 100644 --- a/src/xr_input/controllers.rs +++ b/src/xr_input/controllers.rs @@ -8,7 +8,3 @@ pub struct Handed { pub left: T, pub right: T, } -#[derive(Copy, Clone)] -pub enum XrControllerType { - OculusTouch, -} diff --git a/src/xr_input/hand_poses.rs b/src/xr_input/hand_poses.rs index 70544eb..be5598b 100644 --- a/src/xr_input/hand_poses.rs +++ b/src/xr_input/hand_poses.rs @@ -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] { diff --git a/src/xr_input/mod.rs b/src/xr_input/mod.rs index 700df77..2d6cb6a 100644 --- a/src/xr_input/mod.rs +++ b/src/xr_input/mod.rs @@ -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}; @@ -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, @@ -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()));