use std::ffi::c_void;
use crate::math::Matrix4;
#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub struct SimdFloat4x4 {
pub columns: [[f32; 4]; 4],
}
impl From<SimdFloat4x4> for Matrix4 {
fn from(m: SimdFloat4x4) -> Self {
let mut elements = [0.0f32; 16];
for (c, column) in m.columns.iter().enumerate() {
elements[c * 4..c * 4 + 4].copy_from_slice(column);
}
Matrix4 { elements }
}
}
impl From<Matrix4> for SimdFloat4x4 {
fn from(m: Matrix4) -> Self {
let mut columns = [[0.0f32; 4]; 4];
for (c, column) in columns.iter_mut().enumerate() {
column.copy_from_slice(&m.elements[c * 4..c * 4 + 4]);
}
Self { columns }
}
}
#[derive(Clone, Copy, Debug, Default)]
#[repr(C)]
pub struct SimdFloat2 {
pub x: f32,
pub y: f32,
}
#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub struct CpTime {
pub mach_abs_time: u64,
}
pub type CpLayerRenderer = *mut c_void;
pub type CpFrame = *mut c_void;
pub type CpDrawable = *mut c_void;
pub type CpView = *mut c_void;
pub type CpViewTextureMap = *mut c_void;
pub type CpFrameTiming = *mut c_void;
pub type CpLayerRendererProperties = *mut c_void;
pub type CpLayerRendererConfiguration = *mut c_void;
pub type ArSession = *mut c_void;
pub type ArWorldTrackingProvider = *mut c_void;
pub type ArWorldTrackingConfiguration = *mut c_void;
pub type ArDataProviders = *mut c_void;
pub type ArDeviceAnchor = *mut c_void;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(u32)]
pub enum LayerRendererState {
Paused = 1,
Running = 2,
Invalidated = 3,
}
impl LayerRendererState {
pub fn from_raw(raw: u32) -> Self {
match raw {
2 => Self::Running,
3 => Self::Invalidated,
_ => Self::Paused,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(u8)]
pub enum AxisConvention {
RightUpBack = 0,
RightUpForward = 1,
RightDownBack = 2,
RightDownForward = 3,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DeviceAnchorStatus {
Success,
Error(isize),
}
#[cfg(target_os = "visionos")]
mod frame_loop;
#[cfg(target_os = "visionos")]
pub use frame_loop::{ImmersiveRenderer, WorldTracking};
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn simd_matrices_round_trip() {
let m = Matrix4::perspective(1.0, 1.5, 0.1, 100.0);
let simd: SimdFloat4x4 = m.into();
assert_eq!(
simd.columns[0],
[m.elements[0], m.elements[1], m.elements[2], m.elements[3]]
);
let back: Matrix4 = simd.into();
assert_eq!(back.elements, m.elements);
}
#[test]
fn layer_states_map_from_the_c_enum() {
assert_eq!(LayerRendererState::from_raw(1), LayerRendererState::Paused);
assert_eq!(LayerRendererState::from_raw(2), LayerRendererState::Running);
assert_eq!(
LayerRendererState::from_raw(3),
LayerRendererState::Invalidated
);
assert_eq!(LayerRendererState::from_raw(99), LayerRendererState::Paused);
}
#[test]
fn axis_conventions_match_the_header_values() {
assert_eq!(AxisConvention::RightUpBack as u8, 0);
assert_eq!(AxisConvention::RightUpForward as u8, 1);
assert_eq!(AxisConvention::RightDownBack as u8, 2);
assert_eq!(AxisConvention::RightDownForward as u8, 3);
}
#[test]
fn simd_structs_have_the_c_layout() {
assert_eq!(std::mem::size_of::<SimdFloat4x4>(), 64);
assert_eq!(std::mem::size_of::<SimdFloat2>(), 8);
assert_eq!(std::mem::size_of::<CpTime>(), 8);
}
}