nothing works

This commit is contained in:
galister
2023-11-09 22:27:05 +09:00
commit f193f33f4e
42 changed files with 9992 additions and 0 deletions

152
src/backend/common.rs Normal file
View File

@@ -0,0 +1,152 @@
use std::{collections::VecDeque, time::Instant};
use glam::{Affine3A, Vec2, Vec3, Vec3A};
use ovr_overlay::TrackedDeviceIndex;
pub struct InputState<TState, THand> {
pub hmd: Affine3A,
pub pointers: [Pointer<THand>; 2],
pub devices: Vec<TrackedDevice>,
pub(super) data: TState,
}
impl<TState, THand> InputState<TState, THand> {
pub fn pre_update(&mut self) {
self.pointers[0].before = self.pointers[0].now;
self.pointers[1].before = self.pointers[1].now;
}
pub fn post_update(&mut self) {
for hand in &mut self.pointers {
if hand.now.click_modifier_right {
hand.interaction.mode = PointerMode::Right;
continue;
}
if hand.now.click_modifier_middle {
hand.interaction.mode = PointerMode::Middle;
continue;
}
let hmd_up = self.hmd.transform_vector3a(Vec3A::Y);
let dot =
hmd_up.dot(hand.pose.transform_vector3a(Vec3A::X)) * (1.0 - 2.0 * hand.hand as f32);
hand.interaction.mode = if dot < -0.85 {
PointerMode::Right
} else if dot > 0.7 {
PointerMode::Middle
} else {
PointerMode::Left
};
let middle_click_orientation = false;
let right_click_orientation = false;
match hand.interaction.mode {
PointerMode::Middle => {
if !middle_click_orientation {
hand.interaction.mode = PointerMode::Left;
}
}
PointerMode::Right => {
if !right_click_orientation {
hand.interaction.mode = PointerMode::Left;
}
}
_ => {}
};
}
}
}
pub struct Pointer<THand> {
pub hand: usize,
pub pose: Affine3A,
pub now: PointerState,
pub before: PointerState,
pub(super) interaction: InteractionState,
pub(super) data: THand,
}
#[derive(Debug, Clone, Copy, Default)]
pub struct PointerState {
pub scroll: f32,
pub click: bool,
pub grab: bool,
pub alt_click: bool,
pub show_hide: bool,
pub space_drag: bool,
pub click_modifier_right: bool,
pub click_modifier_middle: bool,
}
pub struct InteractionState {
pub mode: PointerMode,
pub grabbed: Option<GrabData>,
pub clicked_id: Option<usize>,
pub hovered_id: Option<usize>,
pub release_actions: VecDeque<Box<dyn Fn()>>,
pub next_push: Instant,
}
impl Default for InteractionState {
fn default() -> Self {
Self {
mode: PointerMode::Left,
grabbed: None,
clicked_id: None,
hovered_id: None,
release_actions: VecDeque::new(),
next_push: Instant::now(),
}
}
}
pub struct PointerHit {
pub hand: usize,
pub mode: PointerMode,
pub primary: bool,
pub uv: Vec2,
pub dist: f32,
}
struct RayHit {
idx: usize,
ray_pos: Vec3,
hit_pos: Vec3,
uv: Vec2,
dist: f32,
}
#[derive(Debug, Clone, Copy, Default)]
pub struct GrabData {
pub offset: Vec3,
pub grabbed_id: usize,
}
#[repr(u8)]
#[derive(Debug, Clone, Copy, Default)]
pub enum PointerMode {
#[default]
Left,
Right,
Middle,
}
pub struct TrackedDevice {
pub index: TrackedDeviceIndex,
pub valid: bool,
pub soc: Option<f32>,
pub charging: bool,
pub role: TrackedDeviceRole,
}
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TrackedDeviceRole {
None,
Hmd,
LeftHand,
RightHand,
Tracker,
}

3
src/backend/mod.rs Normal file
View File

@@ -0,0 +1,3 @@
pub mod common;
pub mod openvr;
pub mod openxr;

288
src/backend/openvr/input.rs Normal file
View File

@@ -0,0 +1,288 @@
use std::array;
use glam::Affine3A;
use ovr_overlay::{
input::{ActionHandle, ActionSetHandle, ActiveActionSet, InputManager, InputValueHandle},
sys::{
k_unMaxTrackedDeviceCount, ETrackedControllerRole, ETrackedDeviceClass,
ETrackedDeviceProperty, ETrackingUniverseOrigin, HmdMatrix34_t,
},
system::SystemManager,
TrackedDeviceIndex,
};
use crate::backend::common::{
InputState, InteractionState, Pointer, PointerState, TrackedDevice, TrackedDeviceRole,
};
macro_rules! result_str {
( $e:expr ) => {
match $e {
Ok(x) => Ok(x),
Err(y) => Err(y.description()),
}
};
}
const SET_DEFAULT: &str = "/actions/default";
const INPUT_SOURCES: [&str; 2] = ["/user/hand/left", "/user/hand/right"];
const PATH_POSES: [&str; 2] = [
"/actions/default/in/LeftHand",
"/actions/default/in/RightHand",
];
const PATH_HAPTICS: [&str; 2] = [
"/actions/default/out/HapticsLeft",
"/actions/default/out/HapticsRight",
];
const PATH_CLICK: &str = "/actions/default/in/Click";
const PATH_GRAB: &str = "/actions/default/in/Grab";
const PATH_SCROLL: &str = "/actions/default/in/Scroll";
const PATH_ALT_CLICK: &str = "/actions/default/in/AltClick";
const PATH_SHOW_HIDE: &str = "/actions/default/in/ShowHide";
const PATH_SPACE_DRAG: &str = "/actions/default/in/SpaceDrag";
const PATH_CLICK_MODIFIER_RIGHT: &str = "/actions/default/in/ClickModifierRight";
const PATH_CLICK_MODIFIER_MIDDLE: &str = "/actions/default/in/ClickModifierMiddle";
const INPUT_ANY: InputValueHandle = InputValueHandle(ovr_overlay::sys::k_ulInvalidInputValueHandle);
pub(super) struct OpenVrInputState {
set_hnd: ActionSetHandle,
click_hnd: ActionHandle,
grab_hnd: ActionHandle,
scroll_hnd: ActionHandle,
alt_click_hnd: ActionHandle,
show_hide_hnd: ActionHandle,
space_drag_hnd: ActionHandle,
click_modifier_right_hnd: ActionHandle,
click_modifier_middle_hnd: ActionHandle,
}
pub(super) struct OpenVrHandState {
has_pose: bool,
input_hnd: InputValueHandle,
pose_hnd: ActionHandle,
haptics_hnd: ActionHandle,
}
impl InputState<OpenVrInputState, OpenVrHandState> {
pub fn new(input: &mut InputManager) -> Result<Self, &'static str> {
let set_hnd = result_str!(input.get_action_set_handle(SET_DEFAULT))?;
let click_hnd = result_str!(input.get_action_handle(PATH_CLICK))?;
let grab_hnd = result_str!(input.get_action_handle(PATH_GRAB))?;
let scroll_hnd = result_str!(input.get_action_handle(PATH_SCROLL))?;
let alt_click_hnd = result_str!(input.get_action_handle(PATH_ALT_CLICK))?;
let show_hide_hnd = result_str!(input.get_action_handle(PATH_SHOW_HIDE))?;
let space_drag_hnd = result_str!(input.get_action_handle(PATH_SPACE_DRAG))?;
let click_modifier_right_hnd =
result_str!(input.get_action_handle(PATH_CLICK_MODIFIER_RIGHT))?;
let click_modifier_middle_hnd =
result_str!(input.get_action_handle(PATH_CLICK_MODIFIER_MIDDLE))?;
let input_hnd: Vec<InputValueHandle> = INPUT_SOURCES
.iter()
.map(|path| Ok(result_str!(input.get_input_source_handle(path))?))
.collect::<Result<_, &'static str>>()?;
let pose_hnd: Vec<ActionHandle> = PATH_POSES
.iter()
.map(|path| Ok(result_str!(input.get_action_handle(path))?))
.collect::<Result<_, &'static str>>()?;
let haptics_hnd: Vec<ActionHandle> = PATH_HAPTICS
.iter()
.map(|path| Ok(result_str!(input.get_action_handle(path))?))
.collect::<Result<_, &'static str>>()?;
let hands: [Pointer<OpenVrHandState>; 2] = array::from_fn(|i| Pointer::<OpenVrHandState> {
hand: i,
now: PointerState::default(),
before: PointerState::default(),
pose: Affine3A::IDENTITY,
interaction: InteractionState::default(),
data: OpenVrHandState {
has_pose: false,
input_hnd: input_hnd[i],
pose_hnd: pose_hnd[i],
haptics_hnd: haptics_hnd[i],
},
});
Ok(InputState {
hmd: Affine3A::IDENTITY,
pointers: hands,
devices: vec![],
data: OpenVrInputState {
set_hnd,
click_hnd,
grab_hnd,
scroll_hnd,
alt_click_hnd,
show_hide_hnd,
space_drag_hnd,
click_modifier_right_hnd,
click_modifier_middle_hnd,
},
})
}
pub fn update(&mut self, input: &mut InputManager, system: &mut SystemManager) {
let aas = ActiveActionSet {
0: ovr_overlay::sys::VRActiveActionSet_t {
ulActionSet: self.data.set_hnd.0,
ulRestrictedToDevice: 0,
ulSecondaryActionSet: 0,
unPadding: 0,
nPriority: 0,
},
};
let _ = input.update_actions(&mut [aas]);
let universe = ETrackingUniverseOrigin::TrackingUniverseStanding;
for i in 0..2 {
let hand = &mut self.pointers[i];
hand.data.has_pose = false;
let _ = input
.get_pose_action_data_relative_to_now(
hand.data.pose_hnd,
universe.clone(),
0.005,
INPUT_ANY,
)
.and_then(|pose| {
copy_from_hmd(&pose.0.pose.mDeviceToAbsoluteTracking, &mut hand.pose);
hand.data.has_pose = true;
Ok(())
});
hand.now.click = input
.get_digital_action_data(self.data.click_hnd, hand.data.input_hnd)
.map(|x| x.0.bState)
.unwrap_or(false);
hand.now.grab = input
.get_digital_action_data(self.data.grab_hnd, hand.data.input_hnd)
.map(|x| x.0.bState)
.unwrap_or(false);
hand.now.alt_click = input
.get_digital_action_data(self.data.alt_click_hnd, hand.data.input_hnd)
.map(|x| x.0.bState)
.unwrap_or(false);
hand.now.show_hide = input
.get_digital_action_data(self.data.show_hide_hnd, hand.data.input_hnd)
.map(|x| x.0.bState)
.unwrap_or(false);
hand.now.space_drag = input
.get_digital_action_data(self.data.space_drag_hnd, hand.data.input_hnd)
.map(|x| x.0.bState)
.unwrap_or(false);
hand.now.click_modifier_right = input
.get_digital_action_data(self.data.click_modifier_right_hnd, hand.data.input_hnd)
.map(|x| x.0.bState)
.unwrap_or(false);
hand.now.click_modifier_middle = input
.get_digital_action_data(self.data.click_modifier_middle_hnd, hand.data.input_hnd)
.map(|x| x.0.bState)
.unwrap_or(false);
hand.now.scroll = input
.get_analog_action_data(self.data.scroll_hnd, hand.data.input_hnd)
.map(|x| x.0.x)
.unwrap_or(0.0);
}
let devices = system.get_device_to_absolute_tracking_pose(universe, 0.005);
copy_from_hmd(&devices[0].mDeviceToAbsoluteTracking, &mut self.hmd);
}
pub fn update_devices(&mut self, system: &mut SystemManager) {
self.devices.clear();
for i in 0..k_unMaxTrackedDeviceCount {
let index = TrackedDeviceIndex(i);
let maybe_role = match system.get_tracked_device_class(index) {
ETrackedDeviceClass::TrackedDeviceClass_HMD => Some(TrackedDeviceRole::Hmd),
ETrackedDeviceClass::TrackedDeviceClass_Controller => {
let sys_role = system.get_controller_role_for_tracked_device_index(index);
match sys_role {
ETrackedControllerRole::TrackedControllerRole_LeftHand => {
Some(TrackedDeviceRole::LeftHand)
}
ETrackedControllerRole::TrackedControllerRole_RightHand => {
Some(TrackedDeviceRole::RightHand)
}
_ => None,
}
}
ETrackedDeviceClass::TrackedDeviceClass_GenericTracker => {
Some(TrackedDeviceRole::Tracker)
}
_ => None,
};
if let Some(role) = maybe_role {
if let Some(device) = get_tracked_device(system, index, role) {
self.devices.push(device);
}
}
}
self.devices.sort_by(|a, b| {
(a.role as u8)
.cmp(&(b.role as u8))
.then(a.index.0.cmp(&b.index.0))
});
}
}
fn get_tracked_device(
system: &mut SystemManager,
index: TrackedDeviceIndex,
role: TrackedDeviceRole,
) -> Option<TrackedDevice> {
let soc = system
.get_tracked_device_property(
index,
ETrackedDeviceProperty::Prop_DeviceBatteryPercentage_Float,
)
.ok();
let charging = if soc.is_some() {
system
.get_tracked_device_property(index, ETrackedDeviceProperty::Prop_DeviceIsCharging_Bool)
.unwrap_or(false)
} else {
false
};
Some(TrackedDevice {
valid: true,
index,
soc,
charging,
role,
})
}
fn copy_from_hmd(in_mat: &HmdMatrix34_t, out_mat: &mut Affine3A) {
out_mat.x_axis[0] = in_mat.m[0][0];
out_mat.x_axis[1] = in_mat.m[1][0];
out_mat.x_axis[2] = in_mat.m[2][0];
out_mat.y_axis[0] = in_mat.m[0][1];
out_mat.y_axis[1] = in_mat.m[1][1];
out_mat.y_axis[2] = in_mat.m[2][1];
out_mat.z_axis[0] = in_mat.m[0][2];
out_mat.z_axis[1] = in_mat.m[1][2];
out_mat.z_axis[2] = in_mat.m[2][2];
out_mat.w_axis[0] = in_mat.m[0][3];
out_mat.w_axis[1] = in_mat.m[1][3];
out_mat.w_axis[2] = in_mat.m[2][3];
}

100
src/backend/openvr/mod.rs Normal file
View File

@@ -0,0 +1,100 @@
use std::{
path::Path,
time::{Duration, Instant},
};
use log::{error, info};
use ovr_overlay::{
sys::{ETrackedDeviceProperty, EVRApplicationType, EVREventType},
TrackedDeviceIndex,
};
use super::common::InputState;
pub mod input;
pub mod overlay;
fn openvr_run() {
let app_type = EVRApplicationType::VRApplication_Overlay;
let Ok(context) = ovr_overlay::Context::init(app_type) else {
error!("Failed to initialize OpenVR");
return;
};
let mut overlay = context.overlay_mngr();
let mut settings = context.settings_mngr();
let mut input = context.input_mngr();
let mut system = context.system_mngr();
let mut compositor = context.compositor_mngr();
let Ok(_) = input.set_action_manifest(Path::new("resources/actions.json")) else {
error!("Failed to set action manifest");
return;
};
let Ok(mut input_state) = InputState::new(&mut input) else {
error!("Failed to initialize input");
return;
};
let Ok(refresh_rate) = system.get_tracked_device_property::<f32>(TrackedDeviceIndex::HMD, ETrackedDeviceProperty::Prop_DisplayFrequency_Float) else {
error!("Failed to get display refresh rate");
return;
};
let frame_time = (1000.0 / refresh_rate).floor() * 0.001;
let mut next_device_update = Instant::now();
loop {
while let Some(event) = system.poll_next_event() {
match event.event_type {
EVREventType::VREvent_Quit => {
info!("Received quit event, shutting down.");
return;
}
EVREventType::VREvent_TrackedDeviceActivated
| EVREventType::VREvent_TrackedDeviceDeactivated
| EVREventType::VREvent_TrackedDeviceUpdated => {
next_device_update = Instant::now();
}
_ => {}
}
if next_device_update <= Instant::now() {
input_state.update_devices(&mut system);
next_device_update = Instant::now() + Duration::from_secs(30);
}
input_state.pre_update();
input_state.update(&mut input, &mut system);
input_state.post_update();
// task scheduler
// after input
// interactions
// show overlays
// chaperone
// render overlays
// hide overlays
// close font handles
// playspace moved end frame
let mut seconds_since_vsync = 0f32;
std::thread::sleep(Duration::from_secs_f32(
if system.get_time_since_last_vsync(&mut seconds_since_vsync, &mut 0u64) {
frame_time - seconds_since_vsync
} else {
0.011
},
));
}
}
}

View File

@@ -0,0 +1,15 @@
use ovr_overlay::sys::VRVulkanTextureData_t;
use crate::overlays::OverlayData;
pub(super) struct OpenVrOverlayManager {
pub(super) overlays: Vec<OpenVrOverlay>,
}
pub(super) struct OpenVrOverlay {
pub(super) visible: bool,
pub(super) color: [f32; 4],
overlay: OverlayData,
handle: u32,
ovr_texture: VRVulkanTextureData_t,
}

0
src/backend/openxr.rs Normal file
View File