From 75a8c32a9dc6b9a667f067df31cb163f981cd77c Mon Sep 17 00:00:00 2001 From: Schmarni Date: Thu, 27 Jun 2024 02:10:53 +0200 Subject: [PATCH] add into_openxr_space and add a few comments Signed-off-by: Schmarni --- crates/bevy_openxr/examples/raw_actions.rs | 24 ++++++++-------- crates/bevy_openxr/src/openxr/spaces.rs | 33 ++++++++++++++++++---- 2 files changed, 40 insertions(+), 17 deletions(-) diff --git a/crates/bevy_openxr/examples/raw_actions.rs b/crates/bevy_openxr/examples/raw_actions.rs index e2f4576..6a22e97 100644 --- a/crates/bevy_openxr/examples/raw_actions.rs +++ b/crates/bevy_openxr/examples/raw_actions.rs @@ -113,18 +113,18 @@ fn spawn_hands( mut meshes: ResMut>, mut materials: ResMut>, ) { - let l = actions - .left - .create_space( - session.deref().deref().clone(), - openxr::Path::NULL, - Posef::IDENTITY, - ) - .unwrap(); - let left_space = XrSpace::from_openxr_space(l); - // let left_space = session - // .create_action_space(&actions.left, openxr::Path::NULL, XrPose::IDENTITY) - // .unwrap(); + // This is a demonstation of how to integrate with the openxr crate, the right space is the + // recommended way + let left_space = XrSpace::from_openxr_space( + actions + .left + .create_space( + session.deref().deref().clone(), + openxr::Path::NULL, + Posef::IDENTITY, + ) + .unwrap(), + ); let right_space = session .create_action_space(&actions.right, openxr::Path::NULL, XrPose::IDENTITY) .unwrap(); diff --git a/crates/bevy_openxr/src/openxr/spaces.rs b/crates/bevy_openxr/src/openxr/spaces.rs index 4b4eb7d..323e7f6 100644 --- a/crates/bevy_openxr/src/openxr/spaces.rs +++ b/crates/bevy_openxr/src/openxr/spaces.rs @@ -2,7 +2,7 @@ use std::{mem::MaybeUninit, ptr, sync::Mutex}; use bevy::{prelude::*, utils::hashbrown::HashSet}; use bevy_xr::{ - session::{session_available, session_running, XrSessionExiting}, + session::{session_available, XrSessionExiting}, spaces::{XrDestroySpace, XrPrimaryReferenceSpace, XrReferenceSpace, XrSpace, XrSpatialOffset}, types::XrPose, }; @@ -18,6 +18,9 @@ use crate::{ session::OxrSession, }; +#[derive(SystemSet, Hash, Debug, Clone, Copy, PartialEq, Eq)] +pub struct OxrSpaceSyncSet; + /// VERY IMPORTENT!! only disable when you know what you are doing pub struct OxrSpacePatchingPlugin; impl Plugin for OxrSpacePatchingPlugin { @@ -29,9 +32,9 @@ pub struct OxrSpatialPlugin; impl Plugin for OxrSpatialPlugin { fn build(&self, app: &mut App) { app.add_event::(); - app.add_systems(Startup, patch_destroy_space.run_if(session_available)); app.add_systems(OxrLast, destroy_space_event.before(OxrHandleEvents)); app.add_systems(XrSessionExiting, destroy_space_components); + app.add_systems(PreUpdate, update_space_transforms.in_set(OxrSpaceSyncSet)); } } @@ -96,7 +99,7 @@ unsafe extern "system" fn patched_destroy_space(space: openxr::sys::Space) -> op } } -fn update_spatial_transforms( +fn update_space_transforms( session: Res, default_ref_space: Res, pipelined: Option>, @@ -456,12 +459,23 @@ impl OxrInstance { /// # Safety /// This is an Extension trait. DO NOT IMPLEMENT IT! pub unsafe trait OxrSpaceExt { + /// get an openxr::sys::Space as a reference to the XrSpace + /// does not remove the space from the space managment system! fn as_raw_openxr_space(&self) -> sys::Space; + /// Adds the openxr::sys::Space into the the space managment system fn from_raw_openxr_space(space: sys::Space) -> Self; + /// Adds the openxr::Space into the the space manegment system fn from_openxr_space(space: openxr::Space) -> Self; + /// get an openxr::Space as a reference to the XrSpace + /// does not remove the space from the space managment system! /// # Safety /// Session has to be the session from which the space is from - unsafe fn to_openxr_space(self, session: &openxr::Session) -> openxr::Space; + unsafe fn as_openxr_space(&self, session: &openxr::Session) -> openxr::Space; + /// get an openxr::Space as an onwned version of the XrSpace + /// removes the space from the space managment system! + /// # Safety + /// Session has to be the session from which the space is from + unsafe fn into_openxr_space(self, session: &openxr::Session) -> openxr::Space; } unsafe impl OxrSpaceExt for XrSpace { @@ -484,7 +498,16 @@ unsafe impl OxrSpaceExt for XrSpace { Self::from_raw_openxr_space(space.as_raw()) } - unsafe fn to_openxr_space(self, session: &openxr::Session) -> openxr::Space { + unsafe fn as_openxr_space(&self, session: &openxr::Session) -> openxr::Space { + unsafe { openxr::Space::reference_from_raw(session.clone(), self.as_raw_openxr_space()) } + } + unsafe fn into_openxr_space(self, session: &openxr::Session) -> openxr::Space { + OXR_DO_NOT_CALL_DESTOY_SPACE_FOR_SPACES + .lock() + .unwrap() + .as_mut() + .unwrap() + .remove(&self.as_raw()); unsafe { openxr::Space::reference_from_raw(session.clone(), self.as_raw_openxr_space()) } } }