Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

openxr: Support proper floor spaces #138

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
57 changes: 46 additions & 11 deletions webxr/openxr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use openxr::{
self, ActionSet, ActiveActionSet, ApplicationInfo, CompositionLayerFlags,
CompositionLayerProjection, Entry, EnvironmentBlendMode, ExtensionSet, Extent2Di, FormFactor,
Fovf, FrameState, FrameStream, FrameWaiter, Instance, Posef, Quaternionf, ReferenceSpaceType,
Session, Space, Swapchain, SwapchainCreateFlags, SwapchainCreateInfo, SwapchainUsageFlags,
Vector3f, ViewConfigurationType,
Session, Space, SpaceLocationFlags, Swapchain, SwapchainCreateFlags, SwapchainCreateInfo,
SwapchainUsageFlags, Time, Vector3f, ViewConfigurationType,
};
use std::sync::{Arc, Mutex};
use surfman::platform::generic::universal::device::Device as SurfmanDevice;
Expand Down Expand Up @@ -51,8 +51,6 @@ use winapi::shared::dxgiformat;
mod input;
use input::OpenXRInput;

const HEIGHT: f32 = 1.4;

pub trait GlThread: Send {
fn execute(&self, runnable: Box<dyn FnOnce() + Send>);
fn clone(&self) -> Box<dyn GlThread>;
Expand Down Expand Up @@ -156,6 +154,8 @@ struct OpenXrDevice {
frame_waiter: FrameWaiter,
shared_data: Arc<Mutex<SharedData>>,
viewer_space: Space,
floor_space: Space,
floor_dirty: bool,
blend_mode: EnvironmentBlendMode,
clip_planes: ClipPlanes,
view_configurations: Vec<openxr::ViewConfigurationView>,
Expand Down Expand Up @@ -435,6 +435,10 @@ impl OpenXrDevice {
.create_reference_space(ReferenceSpaceType::VIEW, pose)
.map_err(|e| Error::BackendSpecific(format!("{:?}", e)))?;

let floor_space = session
.create_reference_space(ReferenceSpaceType::STAGE, pose)
.map_err(|e| Error::BackendSpecific(format!("{:?}", e)))?;

let view_configuration_type = ViewConfigurationType::PRIMARY_STEREO;
let view_configurations = instance
.enumerate_view_configuration_views(system, view_configuration_type)
Expand Down Expand Up @@ -538,6 +542,8 @@ impl OpenXrDevice {
session,
frame_waiter,
viewer_space,
floor_space,
floor_dirty: true,
clip_planes: Default::default(),
blend_mode,
view_configurations,
Expand Down Expand Up @@ -567,6 +573,12 @@ impl OpenXrDevice {
Some(InstanceLossPending(_)) => {
break;
}
Some(ReferenceSpaceChangePending(r)) => {
if r.reference_space_type() == ReferenceSpaceType::STAGE {
self.floor_dirty = true;
}
// FIXME: Trigger reset events for all other cases
}
Some(_) => {
// FIXME: Handle other events
}
Expand All @@ -578,12 +590,29 @@ impl OpenXrDevice {
}
true
}

fn floor_transform_inner(
&self,
space: &Space,
time: Time,
) -> Option<RigidTransform3D<f32, Native, Floor>> {
let pose = space.locate(&self.floor_space, time).unwrap();

let pose_valid = pose
.location_flags
.intersects(SpaceLocationFlags::POSITION_VALID | SpaceLocationFlags::ORIENTATION_VALID);

if pose_valid {
Some(transform(&pose.pose))
} else {
None
}
}
}

impl DeviceAPI<Surface> for OpenXrDevice {
fn floor_transform(&self) -> Option<RigidTransform3D<f32, Native, Floor>> {
let translation = Vector3D::new(0.0, HEIGHT, 0.0);
Some(RigidTransform3D::from_translation(translation))
None
}

fn views(&self) -> Views {
Expand Down Expand Up @@ -672,15 +701,21 @@ impl DeviceAPI<Surface> for OpenXrDevice {
self.left_hand
.frame(&self.session, &data.frame_state, &data.space);

// views() needs to reacquire the lock.
drop(data);

let events = if self.clip_planes.recently_updated() {
vec![FrameUpdateEvent::UpdateViews(self.views())]
let mut events = if self.floor_dirty {
vec![FrameUpdateEvent::UpdateFloorTransform(
self.floor_transform_inner(&data.space, data.frame_state.predicted_display_time),
)]
} else {
vec![]
};

// views() needs to reacquire the lock.
drop(data);

if self.clip_planes.recently_updated() {
events.push(FrameUpdateEvent::UpdateViews(self.views()));
}

let frame = Frame {
transform,
inputs: vec![right_input_frame, left_input_frame],
Expand Down