small changes

This commit is contained in:
awtterpip
2024-02-01 17:45:17 -06:00
parent dafb59ff3d
commit a078d4baa9
6 changed files with 394 additions and 54 deletions

View File

@@ -1,4 +1,4 @@
use glam::{UVec2, Vec2};
use glam::{UVec2, Vec2, Vec3A};
use wgpu::{Adapter, AdapterInfo, Device, Queue, TextureView};
use crate::prelude::*;
@@ -27,10 +27,14 @@ pub trait SessionTrait {
/// Get render resources compatible with this session.
fn get_render_resources(&self)
-> Option<(Device, Queue, AdapterInfo, Adapter, wgpu::Instance)>;
/// Returns the position of the headset.
fn headset_location(&self) -> Result<Pose>;
/// Request input modules with the specified bindings.
fn create_input(&self, bindings: Bindings) -> Result<Input>;
/// Blocks until a rendering frame is available, then returns the views for the left and right eyes.
fn begin_frame(&self) -> Result<(View, View)>;
fn begin_frame(&self) -> Result<()>;
/// Locate the views of each eye.
fn locate_views(&self) -> Result<(View, View)>;
/// Submits rendering work for this frame.
fn end_frame(&self) -> Result<()>;
/// Gets the resolution of a single eye.
@@ -45,7 +49,9 @@ pub trait ViewTrait {
/// Returns the [Pose] representing the current position of this view.
fn pose(&self) -> Pose;
/// Returns the projection matrix for the current view.
fn projection_matrix(&self) -> glam::Mat4;
fn projection_matrix(&self, near: f32, far: f32) -> glam::Mat4;
/// Gets the fov of the camera.
fn fov(&self) -> Fov;
/// Gets the resolution for this view.
fn resolution(&self) -> UVec2;
/// Gets the texture format for the view.

View File

@@ -1,9 +1,11 @@
mod graphics;
mod utils;
use utils::UntypedOXrAction;
use std::{marker::PhantomData, rc::Rc, sync::Mutex};
use glam::{Mat4, UVec2, Vec2};
use glam::{Mat4, UVec2, Vec2, Vec3A};
use hashbrown::{hash_map, HashMap};
use openxr::{EnvironmentBlendMode, Vector2f};
use tracing::{info, info_span, warn};
@@ -71,14 +73,6 @@ impl InstanceTrait for OXrInstance {
}
}
pub(crate) enum UntypedOXrAction {
Haptics(openxr::Action<openxr::Haptic>),
Pose(openxr::Action<openxr::Posef>),
Float(openxr::Action<f32>),
Bool(openxr::Action<bool>),
Vec2(openxr::Action<openxr::Vector2f>),
}
#[derive(Default)]
pub(crate) struct BindingState {
sessions_attached: bool,
@@ -151,7 +145,7 @@ impl SessionTrait for OXrSession {
.into())
}
fn begin_frame(&self) -> Result<(View, View)> {
fn begin_frame(&self) -> Result<()> {
{
let mut bindings = self.bindings.lock().unwrap();
if !bindings.sessions_attached {
@@ -263,6 +257,10 @@ impl SessionTrait for OXrSession {
self.session
.sync_actions(&action_sets.iter().map(Into::into).collect::<Vec<_>>())?;
}
Ok(())
}
fn locate_views(&self) -> Result<(View, View)> {
let views = {
let _span = info_span!("xr_locate_views").entered();
self.session
@@ -326,6 +324,14 @@ impl SessionTrait for OXrSession {
fn format(&self) -> wgpu::TextureFormat {
self.format
}
fn headset_location(&self) -> Result<Pose> {
let location = self.head.locate(
&self.stage,
self.frame_state.lock().unwrap().predicted_display_time,
)?;
Ok(location.pose.into())
}
}
pub struct OXrInput {
@@ -498,7 +504,7 @@ impl ViewTrait for OXrView {
self.view.pose.clone().into()
}
fn projection_matrix(&self) -> glam::Mat4 {
fn projection_matrix(&self, near: f32, far: f32) -> glam::Mat4 {
// symmetric perspective for debugging
// let x_fov = (self.fov.angle_left.abs() + self.fov.angle_right.abs());
// let y_fov = (self.fov.angle_up.abs() + self.fov.angle_down.abs());
@@ -506,9 +512,6 @@ impl ViewTrait for OXrView {
let fov = self.view.fov;
let is_vulkan_api = false; // FIXME wgpu probably abstracts this
let near_z = 0.1;
let far_z = -1.; // use infinite proj
// let far_z = self.far;
let tan_angle_left = fov.angle_left.tan();
let tan_angle_right = fov.angle_right.tan();
@@ -538,7 +541,7 @@ impl ViewTrait for OXrView {
let mut cols: [f32; 16] = [0.0; 16];
if far_z <= near_z {
if far <= near {
// place the far plane at infinity
cols[0] = 2. / tan_angle_width;
cols[4] = 0.;
@@ -553,7 +556,7 @@ impl ViewTrait for OXrView {
cols[2] = 0.;
cols[6] = 0.;
cols[10] = -1.;
cols[14] = -(near_z + offset_z);
cols[14] = -(near + offset_z);
cols[3] = 0.;
cols[7] = 0.;
@@ -584,8 +587,8 @@ impl ViewTrait for OXrView {
cols[2] = 0.;
cols[6] = 0.;
cols[10] = -(far_z + offset_z) / (far_z - near_z);
cols[14] = -(far_z * (near_z + offset_z)) / (far_z - near_z);
cols[10] = -(far + offset_z) / (far - near);
cols[14] = -(far * (near + offset_z)) / (far - near);
cols[3] = 0.;
cols[7] = 0.;
@@ -603,4 +606,8 @@ impl ViewTrait for OXrView {
fn format(&self) -> wgpu::TextureFormat {
self.format
}
fn fov(&self) -> Fov {
self.view.fov.into()
}
}

View File

@@ -1,5 +1,5 @@
use glam::Quat;
use openxr::Posef;
use openxr::{Action, Fovf, Posef};
use crate::{
error::XrError,
@@ -7,7 +7,7 @@ use crate::{
prelude::Pose,
};
use super::Bindings;
use super::{Bindings, Fov};
impl From<openxr::sys::Result> for XrError {
fn from(_: openxr::sys::Result) -> Self {
@@ -20,9 +20,9 @@ impl From<Posef> for Pose {
// with enough sign errors anything is possible
let rotation = {
let o = pose.orientation;
Quat::from_rotation_x(180.0f32.to_radians()) * glam::quat(o.w, o.z, o.y, o.x)
Quat::from_xyzw(o.x, o.y, o.z, o.w)
};
let translation = glam::vec3(-pose.position.x, pose.position.y, -pose.position.z);
let translation = glam::vec3(pose.position.x, pose.position.y, pose.position.z);
Pose {
translation,
@@ -31,6 +31,69 @@ impl From<Posef> for Pose {
}
}
impl From<Fovf> for Fov {
fn from(fov: Fovf) -> Self {
let Fovf {
angle_left,
angle_right,
angle_down,
angle_up,
} = fov;
Self {
angle_down,
angle_left,
angle_right,
angle_up,
}
}
}
macro_rules! untyped_oxr_actions {
(
$id:ident {
$(
$inner:ident($inner_ty:ty)
),*
$(,)?
}
) => {
pub(crate) enum $id {
$(
$inner($inner_ty),
)*
}
$(
impl TryInto<$inner_ty> for $id {
type Error = ();
fn try_into(self) -> std::prelude::v1::Result<$inner_ty, Self::Error> {
match self {
Self::$inner(action) => Ok(action),
_ => Err(()),
}
}
}
impl From<$inner_ty> for $id {
fn from(value: $inner_ty) -> Self {
Self::$inner(value)
}
}
)*
};
}
untyped_oxr_actions! {
UntypedOXrAction {
Haptics(Action<openxr::Haptic>),
Pose(Action<openxr::Posef>),
Float(Action<f32>),
Bool(Action<bool>),
Vec2(Action<openxr::Vector2f>),
}
}
impl UntypedActionPath {
pub(crate) fn into_xr_path(self) -> String {
let dev_path;

View File

@@ -22,11 +22,20 @@ pub enum Bindings {
pub struct Haptic;
#[derive(Clone, Copy, Debug, Default)]
pub struct Pose {
pub translation: Vec3,
pub rotation: Quat,
}
#[derive(Clone, Copy, Debug, Default)]
pub struct Fov {
pub angle_left: f32,
pub angle_right: f32,
pub angle_down: f32,
pub angle_up: f32,
}
pub trait ActionType: Sized {
type Inner: ?Sized;