diff --git a/crates/bevy_openxr/Cargo.toml b/crates/bevy_openxr/Cargo.toml index c9e8514..cc7534e 100644 --- a/crates/bevy_openxr/Cargo.toml +++ b/crates/bevy_openxr/Cargo.toml @@ -7,7 +7,8 @@ edition = "2021" default = ["vulkan"] vulkan = ["dep:ash"] -[dependencies] +# all dependencies are placed under this since on wasm, this crate is completely empty +[target.'cfg(not(target_family = "wasm"))'.dependencies] thiserror = "1.0.57" wgpu = "0.19.3" wgpu-hal = "0.19.3" @@ -15,7 +16,6 @@ wgpu-hal = "0.19.3" bevy_xr.path = "../bevy_xr" bevy.workspace = true - ash = { version = "0.37.3", optional = true } [target.'cfg(target_family = "unix")'.dependencies] diff --git a/crates/bevy_openxr/examples/3d_scene.rs b/crates/bevy_openxr/examples/3d_scene.rs index fe150d9..fa0c4a0 100644 --- a/crates/bevy_openxr/examples/3d_scene.rs +++ b/crates/bevy_openxr/examples/3d_scene.rs @@ -1,25 +1,12 @@ //! A simple 3D scene with light shining over a cube sitting on a plane. -use std::any::TypeId; - use bevy::prelude::*; -use bevy_openxr::{ - actions::{create_action_sets, ActionApp}, - add_xr_plugins, resources::{TypedAction, XrActions, XrInstance}, -}; -use bevy_xr::actions::{Action, ActionState}; -use openxr::Binding; - -#[derive(Action)] -#[action(action_type = bool, name = "jump")] -pub struct Jump; +use bevy_openxr::add_xr_plugins; fn main() { App::new() .add_plugins(add_xr_plugins(DefaultPlugins)) - .add_systems(Startup, setup.after(create_action_sets)) - .add_systems(Update, read_action_state) - .register_action::() + .add_systems(Startup, setup) .run(); } @@ -28,15 +15,7 @@ fn setup( mut commands: Commands, mut meshes: ResMut>, mut materials: ResMut>, - actions: Res, - instance: Res, ) { - let TypedAction::Bool(action) = actions.get(&TypeId::of::()).unwrap() else { - unreachable!() - }; - instance.suggest_interaction_profile_bindings(instance.string_to_path("/interaction_profiles/oculus/touch_controller").unwrap(), &[ - Binding::new(action, instance.string_to_path("/user/hand/right/input/a/click").unwrap()) - ]).unwrap(); // circular base commands.spawn(PbrBundle { mesh: meshes.add(Circle::new(4.0)), @@ -60,20 +39,4 @@ fn setup( transform: Transform::from_xyz(4.0, 8.0, 4.0), ..default() }); - // // camera - // commands.spawn(XrCameraBundle { - // transform: Transform::from_xyz(-2.5, 4.5, 9.0).looking_at(Vec3::ZERO, Vec3::Y), - // camera: Camera { - // target: RenderTarget::TextureView(ManualTextureViewHandle(XR_TEXTURE_INDEX + 1)), - // ..default() - // }, - // ..default() - // }); } - -fn read_action_state( - state: Res> -) { - info!("{}", state.pressed()) -} - diff --git a/crates/bevy_openxr/src/actions.rs b/crates/bevy_openxr/src/actions.rs deleted file mode 100644 index 12770f1..0000000 --- a/crates/bevy_openxr/src/actions.rs +++ /dev/null @@ -1,205 +0,0 @@ -use std::any::TypeId; -use std::marker::PhantomData; - -use crate::init::XrPreUpdateSet; -use crate::resources::*; -use crate::types::*; -use bevy::app::{App, Plugin, PreUpdate, Startup}; -use bevy::ecs::schedule::common_conditions::resource_added; -use bevy::ecs::schedule::IntoSystemConfigs; -use bevy::ecs::system::{Commands, Res, ResMut}; -use bevy::input::InputSystem; -use bevy::log::error; -use bevy::math::{vec2, Vec2}; -use bevy::utils::hashbrown::HashMap; -use bevy_xr::actions::ActionPlugin; -use bevy_xr::actions::{Action, ActionList, ActionState}; -use bevy_xr::session::session_available; -use bevy_xr::session::session_running; - -pub struct XrActionPlugin; - -impl Plugin for XrActionPlugin { - fn build(&self, app: &mut App) { - app.add_systems(Startup, create_action_sets.run_if(session_available)) - .add_systems( - PreUpdate, - sync_actions.run_if(session_running).before(InputSystem), - ) - .add_systems( - PreUpdate, - attach_action_sets - .after(XrPreUpdateSet::HandleEvents) - .run_if(resource_added::), - ); - } -} - -pub fn create_action_sets( - instance: Res, - action_list: Res, - mut commands: Commands, -) { - let (action_set, actions) = - initialize_action_sets(&instance, &action_list).expect("Failed to initialize action set"); - - commands.insert_resource(action_set); - commands.insert_resource(actions); -} - -pub fn attach_action_sets(mut action_set: ResMut, session: Res) { - session - .attach_action_sets(&[&action_set]) - .expect("Failed to attach action sets"); - action_set.attach(); -} - -pub fn sync_actions(session: Res, action_set: Res) { - session - .sync_actions(&[openxr::ActiveActionSet::new(&action_set)]) - .expect("Failed to sync actions"); -} - -fn initialize_action_sets( - instance: &XrInstance, - action_info: &ActionList, -) -> Result<(XrActionSet, XrActions)> { - let action_set = instance.create_action_set("actions", "actions", 0)?; - let mut actions = HashMap::new(); - for action_info in action_info.0.iter() { - use bevy_xr::actions::ActionType::*; - let action = match action_info.action_type { - Bool => TypedAction::Bool(action_set.create_action( - action_info.name, - action_info.pretty_name, - &[], - )?), - Float => TypedAction::Float(action_set.create_action( - action_info.name, - action_info.pretty_name, - &[], - )?), - Vector => TypedAction::Vector(action_set.create_action( - action_info.name, - action_info.pretty_name, - &[], - )?), - }; - actions.insert(action_info.type_id, action); - } - Ok((XrActionSet::new(action_set), XrActions(actions))) -} - -pub struct XrActionUpdatePlugin(PhantomData); - -impl Plugin for XrActionUpdatePlugin -where - A: Action, - A::ActionType: XrActionTy, -{ - fn build(&self, app: &mut App) { - app.add_systems(PreUpdate, update_action_state::.in_set(InputSystem).run_if(session_running)); - } -} - -impl Default for XrActionUpdatePlugin { - fn default() -> Self { - Self(Default::default()) - } -} - -pub trait XrActionTy: Sized { - fn get_action_state( - action: &TypedAction, - session: &XrSession, - subaction_path: Option, - ) -> Option; -} - -impl XrActionTy for bool { - fn get_action_state( - action: &TypedAction, - session: &XrSession, - subaction_path: Option, - ) -> Option { - match action { - TypedAction::Bool(action) => action - .state(session, subaction_path.unwrap_or(openxr::Path::NULL)) - .ok() - .map(|state| state.current_state), - _ => None, - } - } -} - -impl XrActionTy for f32 { - fn get_action_state( - action: &TypedAction, - session: &XrSession, - subaction_path: Option, - ) -> Option { - match action { - TypedAction::Float(action) => action - .state(session, subaction_path.unwrap_or(openxr::Path::NULL)) - .ok() - .map(|state| state.current_state), - _ => None, - } - } -} - -impl XrActionTy for Vec2 { - fn get_action_state( - action: &TypedAction, - session: &XrSession, - subaction_path: Option, - ) -> Option { - match action { - TypedAction::Vector(action) => action - .state(session, subaction_path.unwrap_or(openxr::Path::NULL)) - .ok() - .map(|state| vec2(state.current_state.x, state.current_state.y)), - _ => None, - } - } -} - -pub fn update_action_state( - mut action_state: ResMut>, - session: Res, - actions: Res, -) where - A: Action, - A::ActionType: XrActionTy, -{ - if let Some(action) = actions.get(&TypeId::of::()) { - if let Some(state) = A::ActionType::get_action_state(action, &session, None) { - action_state.set(state); - } else { - error!( - "Failed to update value for action '{}'", - std::any::type_name::() - ); - } - } -} - -pub trait ActionApp { - fn register_action(&mut self) -> &mut Self - where - A: Action, - A::ActionType: XrActionTy; -} - -impl ActionApp for App { - fn register_action(&mut self) -> &mut Self - where - A: Action, - A::ActionType: XrActionTy, - { - self.add_plugins(( - ActionPlugin::::default(), - XrActionUpdatePlugin::::default(), - )) - } -} diff --git a/crates/bevy_openxr/src/lib.rs b/crates/bevy_openxr/src/lib.rs index fb8289d..566b8ec 100644 --- a/crates/bevy_openxr/src/lib.rs +++ b/crates/bevy_openxr/src/lib.rs @@ -1,57 +1,4 @@ -use actions::XrActionPlugin; -use bevy::{ - app::{PluginGroup, PluginGroupBuilder}, - render::{pipelined_rendering::PipelinedRenderingPlugin, RenderPlugin}, - utils::default, - window::{PresentMode, Window, WindowPlugin}, -}; -use bevy_xr::camera::XrCameraPlugin; -use init::XrInitPlugin; -use render::XrRenderPlugin; - -pub mod actions; -pub mod camera; -pub mod error; -pub mod extensions; -pub mod graphics; -pub mod init; -pub mod layer_builder; -pub mod render; -pub mod resources; -pub mod types; - -pub fn add_xr_plugins(plugins: G) -> PluginGroupBuilder { - plugins - .build() - .disable::() - .disable::() - .add_before::(bevy_xr::session::XrSessionPlugin) - .add_before::(XrInitPlugin { - app_info: default(), - exts: default(), - blend_modes: default(), - backends: default(), - formats: Some(vec![wgpu::TextureFormat::Rgba8UnormSrgb]), - resolutions: default(), - synchronous_pipeline_compilation: default(), - }) - .add(XrRenderPlugin) - .add(XrCameraPlugin) - .add(XrActionPlugin) - .set(WindowPlugin { - #[cfg(not(target_os = "android"))] - primary_window: Some(Window { - transparent: true, - present_mode: PresentMode::AutoNoVsync, - // title: self.app_info.name.clone(), - ..default() - }), - #[cfg(target_os = "android")] - primary_window: None, // ? - #[cfg(target_os = "android")] - exit_condition: bevy::window::ExitCondition::DontExit, - #[cfg(target_os = "android")] - close_when_requested: true, - ..default() - }) -} +#[cfg(not(target_family = "wasm"))] +mod openxr; +#[cfg(not(target_family = "wasm"))] +pub use openxr::*; diff --git a/crates/bevy_openxr/src/error.rs b/crates/bevy_openxr/src/openxr/error.rs similarity index 56% rename from crates/bevy_openxr/src/error.rs rename to crates/bevy_openxr/src/openxr/error.rs index cf18d49..2bf1236 100644 --- a/crates/bevy_openxr/src/error.rs +++ b/crates/bevy_openxr/src/openxr/error.rs @@ -1,10 +1,12 @@ -use crate::graphics::GraphicsBackend; use std::borrow::Cow; use std::fmt; + +use super::graphics::GraphicsBackend; + use thiserror::Error; #[derive(Error, Debug)] -pub enum XrError { +pub enum OXrError { #[error("OpenXR error: {0}")] OpenXrError(#[from] openxr::sys::Result), #[error("OpenXR loading error: {0}")] @@ -17,10 +19,6 @@ pub enum XrError { WgpuRequestDeviceError(#[from] wgpu::RequestDeviceError), #[error("Unsupported texture format: {0:?}")] UnsupportedTextureFormat(wgpu::TextureFormat), - #[error("Vulkan error: {0}")] - VulkanError(#[from] ash::vk::Result), - #[error("Vulkan loading error: {0}")] - VulkanLoadingError(#[from] ash::LoadingError), #[error("Graphics backend '{0:?}' is not available")] UnavailableBackend(GraphicsBackend), #[error("No compatible backend available")] @@ -45,9 +43,55 @@ pub enum XrError { }, #[error("Failed to create CString: {0}")] NulError(#[from] std::ffi::NulError), + #[error("Graphics init error: {0}")] + InitError(InitError), } -impl From>> for XrError { +pub use init_error::InitError; + +/// This module is needed because thiserror does not allow conditional compilation within enums for some reason, +/// so graphics api specific errors are implemented here. +mod init_error { + use super::OXrError; + use std::fmt; + + #[derive(Debug)] + pub enum InitError { + #[cfg(feature = "vulkan")] + VulkanError(ash::vk::Result), + #[cfg(feature = "vulkan")] + VulkanLoadingError(ash::LoadingError), + } + + impl fmt::Display for InitError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + #[cfg(feature = "vulkan")] + InitError::VulkanError(error) => write!(f, "Vulkan error: {}", error), + #[cfg(feature = "vulkan")] + InitError::VulkanLoadingError(error) => { + write!(f, "Vulkan loading error: {}", error) + } + } + } + } + + #[cfg(feature = "vulkan")] + impl From for OXrError { + fn from(value: ash::vk::Result) -> Self { + Self::InitError(InitError::VulkanError(value)) + } + } + + #[cfg(feature = "vulkan")] + impl From for OXrError { + fn from(value: ash::LoadingError) -> Self { + Self::InitError(InitError::VulkanLoadingError(value)) + } + } +} + +impl From>> for OXrError { fn from(value: Vec>) -> Self { Self::UnavailableExtensions(UnavailableExts(value)) } diff --git a/crates/bevy_openxr/src/extensions.rs b/crates/bevy_openxr/src/openxr/exts.rs similarity index 96% rename from crates/bevy_openxr/src/extensions.rs rename to crates/bevy_openxr/src/openxr/exts.rs index 7c0ab27..509f498 100644 --- a/crates/bevy_openxr/src/extensions.rs +++ b/crates/bevy_openxr/src/openxr/exts.rs @@ -2,8 +2,8 @@ use bevy::prelude::{Deref, DerefMut}; use openxr::ExtensionSet; #[derive(Clone, Debug, Eq, PartialEq, Deref, DerefMut)] -pub struct XrExtensions(ExtensionSet); -impl XrExtensions { +pub struct OXrExtensions(ExtensionSet); +impl OXrExtensions { pub fn raw_mut(&mut self) -> &mut ExtensionSet { &mut self.0 } @@ -27,21 +27,21 @@ impl XrExtensions { self } /// returns true if all of the extensions enabled are also available in `available_exts` - pub fn is_available(&self, available_exts: &XrExtensions) -> bool { + pub fn is_available(&self, available_exts: &OXrExtensions) -> bool { self.clone() & available_exts.clone() == *self } } -impl From for XrExtensions { +impl From for OXrExtensions { fn from(value: ExtensionSet) -> Self { Self(value) } } -impl From for ExtensionSet { - fn from(val: XrExtensions) -> Self { +impl From for ExtensionSet { + fn from(val: OXrExtensions) -> Self { val.0 } } -impl Default for XrExtensions { +impl Default for OXrExtensions { fn default() -> Self { let exts = ExtensionSet::default(); //exts.ext_hand_tracking = true; @@ -165,7 +165,7 @@ macro_rules! impl_ext { ) => { $( $macro! { - XrExtensions; + OXrExtensions; almalence_digital_lens_control, bd_controller_interaction, epic_view_configuration_fov, diff --git a/crates/bevy_openxr/src/graphics.rs b/crates/bevy_openxr/src/openxr/graphics.rs similarity index 52% rename from crates/bevy_openxr/src/graphics.rs rename to crates/bevy_openxr/src/openxr/graphics.rs index 4d1e86b..c0e4462 100644 --- a/crates/bevy_openxr/src/graphics.rs +++ b/crates/bevy_openxr/src/openxr/graphics.rs @@ -5,32 +5,51 @@ use std::any::TypeId; use bevy::math::UVec2; -use crate::extensions::XrExtensions; -use crate::types::*; +use crate::types::{AppInfo, OXrExtensions, Result, WgpuGraphics}; +/// This is an extension trait to the [`Graphics`](openxr::Graphics) trait and is how the graphics API should be interacted with. pub unsafe trait GraphicsExt: openxr::Graphics { /// Wrap the graphics specific type into the [GraphicsWrap] enum fn wrap(item: T::Inner) -> GraphicsWrap; + /// Returns all of the required openxr extensions to use this graphics API. + fn required_exts() -> OXrExtensions; /// Convert from wgpu format to the graphics format fn from_wgpu_format(format: wgpu::TextureFormat) -> Option; /// Convert from the graphics format to wgpu format - fn to_wgpu_format(format: Self::Format) -> Option; - /// Initialize graphics for this backend - fn init_graphics( - app_info: &AppInfo, - instance: &openxr::Instance, - system_id: openxr::SystemId, - ) -> Result<(WgpuGraphics, Self::SessionCreateInfo)>; - /// Convert a swapchain function + fn into_wgpu_format(format: Self::Format) -> Option; + /// Convert an API specific swapchain image to a [`Texture`](wgpu::Texture). + /// + /// # Safety + /// + /// The `image` argument must be a valid handle. unsafe fn to_wgpu_img( image: Self::SwapchainImage, device: &wgpu::Device, format: wgpu::TextureFormat, resolution: UVec2, ) -> Result; - fn required_exts() -> XrExtensions; + /// Initialize graphics for this backend and return a [`WgpuGraphics`] for bevy and an API specific [Self::SessionCreateInfo] for openxr + fn init_graphics( + app_info: &AppInfo, + instance: &openxr::Instance, + system_id: openxr::SystemId, + ) -> Result<(WgpuGraphics, Self::SessionCreateInfo)>; } +/// A type that can be used in [`GraphicsWrap`]. +/// +/// # Example +/// +/// ``` +/// pub struct XrSession(GraphicsWrap); +/// +/// impl GraphicsType for XrSession { +/// type Inner = openxr::Session; +/// } +/// +/// ``` +/// +/// In this example, `GraphicsWrap` is now an enum with variants for each graphics API. The enum can be matched to get the graphics specific inner type. pub trait GraphicsType { type Inner; } @@ -39,12 +58,13 @@ impl GraphicsType for () { type Inner = (); } +/// This is a special variant of [GraphicsWrap] using the unit struct as the inner type. This is to simply represent a graphics backend without storing data. pub type GraphicsBackend = GraphicsWrap<()>; impl GraphicsBackend { const ALL: &'static [Self] = &[Self::Vulkan(())]; - pub fn available_backends(exts: &XrExtensions) -> Vec { + pub fn available_backends(exts: &OXrExtensions) -> Vec { Self::ALL .iter() .copied() @@ -52,11 +72,11 @@ impl GraphicsBackend { .collect() } - pub fn is_available(&self, exts: &XrExtensions) -> bool { + pub fn is_available(&self, exts: &OXrExtensions) -> bool { self.required_exts().is_available(exts) } - pub fn required_exts(&self) -> XrExtensions { + pub fn required_exts(&self) -> OXrExtensions { graphics_match!( self; _ => Api::required_exts() @@ -64,6 +84,7 @@ impl GraphicsBackend { } } +/// This struct is for creating agnostic objects for OpenXR graphics API specific structs. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum GraphicsWrap { #[cfg(feature = "vulkan")] @@ -97,6 +118,33 @@ impl GraphicsWrap { } } +/// This macro can be used to quickly run the same code for every variant of [GraphicsWrap]. +/// The first argument should be an expression that returns either a reference or owned value of [GraphicsWrap]. +/// The second argument should be a match arm with the pattern for the inner type. +/// +/// # Example +/// +/// ``` +/// pub struct OXrFrameStream(GraphicsWrap); +/// +/// impl GraphicsType for OXrFrameStream { +/// // Here is the inner type +/// type Inner = openxr::FrameStream; +/// } +/// +/// fn begin(frame_stream: &mut XrFrameStream) { +/// graphics_match! { +/// // get the inner 'GraphicsWrap' struct +/// &mut frame_stream.0; +/// // now we can handle every match arm with a single arm +/// // important: the first pattern here represents the inner type of `GraphicsWrap` +/// // it is already destructured for you. +/// stream => stream.begin(); +/// } +/// } +/// ``` +/// +/// Additionally, if you want the type that implements `GraphicsExt` in the scope of the match body, you can access that type through the type alias `Api`. macro_rules! graphics_match { ( $field:expr; @@ -117,7 +165,7 @@ macro_rules! graphics_match { $variant:ident; $expr:expr => $wrap_ty:ty ) => { - GraphicsWrap::<$wrap_ty>::$variant($expr) + $crate::graphics::GraphicsWrap::<$wrap_ty>::$variant($expr) }; ( diff --git a/crates/bevy_openxr/src/graphics/vulkan.rs b/crates/bevy_openxr/src/openxr/graphics/vulkan.rs similarity index 97% rename from crates/bevy_openxr/src/graphics/vulkan.rs rename to crates/bevy_openxr/src/openxr/graphics/vulkan.rs index 469815e..854dbc5 100644 --- a/crates/bevy_openxr/src/graphics/vulkan.rs +++ b/crates/bevy_openxr/src/openxr/graphics/vulkan.rs @@ -7,11 +7,9 @@ use openxr::Version; use wgpu_hal::api::Vulkan; use wgpu_hal::Api; -use crate::error::XrError; -use crate::extensions::XrExtensions; -use crate::types::*; - -use super::GraphicsExt; +use super::{GraphicsExt, GraphicsType, GraphicsWrap}; +use crate::error::OXrError; +use crate::types::{AppInfo, OXrExtensions, Result, WgpuGraphics}; #[cfg(not(target_os = "android"))] const VK_TARGET_VERSION: Version = Version::new(1, 2, 0); @@ -26,14 +24,74 @@ const VK_TARGET_VERSION_ASH: u32 = ash::vk::make_api_version( ); unsafe impl GraphicsExt for openxr::Vulkan { + fn wrap(item: T::Inner) -> GraphicsWrap { + GraphicsWrap::Vulkan(item) + } + + fn required_exts() -> OXrExtensions { + let mut extensions = openxr::ExtensionSet::default(); + extensions.khr_vulkan_enable2 = true; + extensions.into() + } + fn from_wgpu_format(format: wgpu::TextureFormat) -> Option { wgpu_to_vulkan(format).map(|f| f.as_raw() as _) } - fn to_wgpu_format(format: Self::Format) -> Option { + fn into_wgpu_format(format: Self::Format) -> Option { vulkan_to_wgpu(ash::vk::Format::from_raw(format as _)) } + unsafe fn to_wgpu_img( + color_image: Self::SwapchainImage, + device: &wgpu::Device, + format: wgpu::TextureFormat, + resolution: UVec2, + ) -> Result { + let color_image = ash::vk::Image::from_raw(color_image); + let wgpu_hal_texture = unsafe { + ::Device::texture_from_raw( + color_image, + &wgpu_hal::TextureDescriptor { + label: Some("VR Swapchain"), + size: wgpu::Extent3d { + width: resolution.x, + height: resolution.y, + depth_or_array_layers: 2, + }, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format: format, + usage: wgpu_hal::TextureUses::COLOR_TARGET | wgpu_hal::TextureUses::COPY_DST, + memory_flags: wgpu_hal::MemoryFlags::empty(), + view_formats: vec![], + }, + None, + ) + }; + let texture = unsafe { + device.create_texture_from_hal::( + wgpu_hal_texture, + &wgpu::TextureDescriptor { + label: Some("VR Swapchain"), + size: wgpu::Extent3d { + width: resolution.x, + height: resolution.y, + depth_or_array_layers: 2, + }, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format: format, + usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_DST, + view_formats: &[], + }, + ) + }; + Ok(texture) + } + fn init_graphics( app_info: &AppInfo, instance: &openxr::Instance, @@ -48,7 +106,7 @@ unsafe impl GraphicsExt for openxr::Vulkan { reqs.min_api_version_supported, reqs.max_api_version_supported.major() + 1 ); - return Err(XrError::FailedGraphicsRequirements); + return Err(OXrError::FailedGraphicsRequirements); }; let vk_entry = unsafe { ash::Entry::load() }?; let flags = wgpu::InstanceFlags::empty(); @@ -107,7 +165,7 @@ unsafe impl GraphicsExt for openxr::Vulkan { VK_TARGET_VERSION.minor(), VK_TARGET_VERSION.patch() ); - return Err(XrError::FailedGraphicsRequirements); + return Err(OXrError::FailedGraphicsRequirements); } let wgpu_vk_instance = unsafe { @@ -131,7 +189,7 @@ unsafe impl GraphicsExt for openxr::Vulkan { let Some(wgpu_exposed_adapter) = wgpu_vk_instance.expose_adapter(vk_physical_device) else { error!("WGPU failed to provide an adapter"); - return Err(XrError::FailedGraphicsRequirements); + return Err(OXrError::FailedGraphicsRequirements); }; let enabled_extensions = wgpu_exposed_adapter @@ -234,66 +292,6 @@ unsafe impl GraphicsExt for openxr::Vulkan { }, )) } - - unsafe fn to_wgpu_img( - color_image: Self::SwapchainImage, - device: &wgpu::Device, - format: wgpu::TextureFormat, - resolution: UVec2, - ) -> Result { - let color_image = ash::vk::Image::from_raw(color_image); - let wgpu_hal_texture = unsafe { - ::Device::texture_from_raw( - color_image, - &wgpu_hal::TextureDescriptor { - label: Some("VR Swapchain"), - size: wgpu::Extent3d { - width: resolution.x, - height: resolution.y, - depth_or_array_layers: 2, - }, - mip_level_count: 1, - sample_count: 1, - dimension: wgpu::TextureDimension::D2, - format: format, - usage: wgpu_hal::TextureUses::COLOR_TARGET | wgpu_hal::TextureUses::COPY_DST, - memory_flags: wgpu_hal::MemoryFlags::empty(), - view_formats: vec![], - }, - None, - ) - }; - let texture = unsafe { - device.create_texture_from_hal::( - wgpu_hal_texture, - &wgpu::TextureDescriptor { - label: Some("VR Swapchain"), - size: wgpu::Extent3d { - width: resolution.x, - height: resolution.y, - depth_or_array_layers: 2, - }, - mip_level_count: 1, - sample_count: 1, - dimension: wgpu::TextureDimension::D2, - format: format, - usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_DST, - view_formats: &[], - }, - ) - }; - Ok(texture) - } - - fn required_exts() -> XrExtensions { - let mut extensions = openxr::ExtensionSet::default(); - extensions.khr_vulkan_enable2 = true; - extensions.into() - } - - fn wrap(item: T::Inner) -> super::GraphicsWrap { - super::GraphicsWrap::Vulkan(item) - } } fn vulkan_to_wgpu(format: ash::vk::Format) -> Option { diff --git a/crates/bevy_openxr/src/init.rs b/crates/bevy_openxr/src/openxr/init.rs similarity index 78% rename from crates/bevy_openxr/src/init.rs rename to crates/bevy_openxr/src/openxr/init.rs index 2142197..8be56e5 100644 --- a/crates/bevy_openxr/src/init.rs +++ b/crates/bevy_openxr/src/openxr/init.rs @@ -1,41 +1,51 @@ -use bevy::math::uvec2; use bevy::prelude::*; use bevy::render::extract_resource::ExtractResourcePlugin; -use bevy::render::renderer::{ - RenderAdapter, RenderAdapterInfo, RenderDevice, RenderInstance, RenderQueue, -}; +use bevy::render::renderer::RenderAdapter; +use bevy::render::renderer::RenderAdapterInfo; +use bevy::render::renderer::RenderDevice; +use bevy::render::renderer::RenderInstance; +use bevy::render::renderer::RenderQueue; use bevy::render::settings::RenderCreation; -use bevy::render::{MainWorld, Render, RenderApp, RenderPlugin, RenderSet}; +use bevy::render::MainWorld; +use bevy::render::Render; +use bevy::render::RenderApp; +use bevy::render::RenderPlugin; +use bevy::render::RenderSet; use bevy::transform::TransformSystem; -use bevy::winit::{UpdateMode, WinitSettings}; -use bevy_xr::session::{ - handle_session, session_available, session_running, status_equals, BeginXrSession, - CreateXrSession, DestroyXrSession, EndXrSession, XrSharedStatus, XrStatus, -}; +use bevy::winit::UpdateMode; +use bevy::winit::WinitSettings; +use bevy_xr::session::handle_session; +use bevy_xr::session::session_available; +use bevy_xr::session::session_running; +use bevy_xr::session::status_equals; +use bevy_xr::session::BeginXrSession; +use bevy_xr::session::CreateXrSession; +use bevy_xr::session::DestroyXrSession; +use bevy_xr::session::EndXrSession; +use bevy_xr::session::XrSharedStatus; +use bevy_xr::session::XrStatus; +use crate::error::OXrError; use crate::graphics::*; use crate::resources::*; use crate::types::*; -pub fn session_started(started: Option>) -> bool { +pub fn session_started(started: Option>) -> bool { started.is_some_and(|started| started.get()) } #[derive(Debug, Hash, PartialEq, Eq, Clone, Copy, SystemSet)] -pub enum XrPreUpdateSet { +pub enum OXrPreUpdateSet { PollEvents, HandleEvents, } -#[derive(Component)] -pub struct XrTrackingRoot; - -pub struct XrInitPlugin { +pub struct OXrInitPlugin { /// Information about the app this is being used to build. pub app_info: AppInfo, /// Extensions wanted for this session. // TODO!() This should be changed to take a simpler list of features wanted that this crate supports. i.e. hand tracking - pub exts: XrExtensions, + pub exts: OXrExtensions, /// List of blend modes the openxr session can use. If [None], pick the first available blend mode. pub blend_modes: Option>, /// List of backends the openxr session can use. If [None], pick the first available backend. @@ -48,7 +58,10 @@ pub struct XrInitPlugin { pub synchronous_pipeline_compilation: bool, } -impl Plugin for XrInitPlugin { +#[derive(Component)] +pub struct OXrTrackingRoot; + +impl Plugin for OXrInitPlugin { fn build(&self, app: &mut App) { match self.init_xr() { Ok(( @@ -70,9 +83,9 @@ impl Plugin for XrInitPlugin { ), synchronous_pipeline_compilation: self.synchronous_pipeline_compilation, }, - ExtractResourcePlugin::::default(), - ExtractResourcePlugin::::default(), - ExtractResourcePlugin::::default(), + ExtractResourcePlugin::::default(), + ExtractResourcePlugin::::default(), + ExtractResourcePlugin::::default(), )) .add_systems(First, reset_per_frame_resources) .add_systems( @@ -80,7 +93,7 @@ impl Plugin for XrInitPlugin { ( poll_events .run_if(session_available) - .in_set(XrPreUpdateSet::PollEvents), + .in_set(OXrPreUpdateSet::PollEvents), ( (create_xr_session, apply_deferred) .chain() @@ -96,7 +109,7 @@ impl Plugin for XrInitPlugin { .run_if(on_event::()) .run_if(status_equals(XrStatus::Exiting)), ) - .in_set(XrPreUpdateSet::HandleEvents), + .in_set(OXrPreUpdateSet::HandleEvents), ), ) .add_systems( @@ -110,24 +123,24 @@ impl Plugin for XrInitPlugin { focused_mode: UpdateMode::Continuous, unfocused_mode: UpdateMode::Continuous, }) - .init_resource::() - .init_resource::() + .init_resource::() + .init_resource::() .insert_non_send_resource(session_create_info); app.world - .spawn((TransformBundle::default(), XrTrackingRoot)); + .spawn((TransformBundle::default(), OXrTrackingRoot)); let render_app = app.sub_app_mut(RenderApp); render_app .insert_resource(instance) .insert_resource(system_id) .insert_resource(status) - .init_resource::() - .init_resource::() + .init_resource::() + .init_resource::() .add_systems( Render, destroy_xr_session_render - .run_if(resource_equals(XrCleanupSession(true))) + .run_if(resource_equals(OXrCleanupSession(true))) .after(RenderSet::ExtractCommands), ) .add_systems( @@ -151,12 +164,12 @@ impl Plugin for XrInitPlugin { app.configure_sets( PreUpdate, ( - XrPreUpdateSet::PollEvents.before(handle_session), - XrPreUpdateSet::HandleEvents.after(handle_session), + OXrPreUpdateSet::PollEvents.before(handle_session), + OXrPreUpdateSet::HandleEvents.after(handle_session), ), ); - let session_started = XrSessionStarted::default(); + let session_started = OXrSessionStarted::default(); app.insert_resource(session_started.clone()); @@ -167,24 +180,24 @@ impl Plugin for XrInitPlugin { } pub fn update_root_transform( - mut root_transform: ResMut, - root: Query<&GlobalTransform, With>, + mut root_transform: ResMut, + root: Query<&GlobalTransform, With>, ) { let transform = root.single(); root_transform.0 = *transform; } -fn xr_entry() -> Result { +fn xr_entry() -> Result { #[cfg(windows)] let entry = openxr::Entry::linked(); #[cfg(not(windows))] let entry = unsafe { openxr::Entry::load()? }; - Ok(XrEntry(entry)) + Ok(OXrEntry(entry)) } -impl XrInitPlugin { - fn init_xr(&self) -> Result<(XrInstance, XrSystemId, WgpuGraphics, XrSessionCreateInfo)> { +impl OXrInitPlugin { + fn init_xr(&self) -> Result<(OXrInstance, OXrSystemId, WgpuGraphics, SessionConfigInfo)> { let entry = xr_entry()?; let available_exts = entry.enumerate_extensions()?; @@ -211,7 +224,7 @@ impl XrInitPlugin { } else { available_backends.first().copied() } - .ok_or(XrError::NoAvailableBackend)?; + .ok_or(OXrError::NoAvailableBackend)?; let exts = self.exts.clone() & available_exts; @@ -243,7 +256,7 @@ impl XrInitPlugin { let (graphics, graphics_info) = instance.init_graphics(system_id)?; - let session_create_info = XrSessionCreateInfo { + let session_create_info = SessionConfigInfo { blend_modes: self.blend_modes.clone(), formats: self.formats.clone(), resolutions: self.resolutions.clone(), @@ -252,7 +265,7 @@ impl XrInitPlugin { Ok(( instance, - XrSystemId(system_id), + OXrSystemId(system_id), graphics, session_create_info, )) @@ -261,22 +274,22 @@ impl XrInitPlugin { fn init_xr_session( device: &wgpu::Device, - instance: &XrInstance, + instance: &OXrInstance, system_id: openxr::SystemId, - XrSessionCreateInfo { + SessionConfigInfo { blend_modes, formats, resolutions, graphics_info, - }: XrSessionCreateInfo, + }: SessionConfigInfo, ) -> Result<( - XrSession, - XrFrameWaiter, - XrFrameStream, - XrSwapchain, - XrSwapchainImages, - XrGraphicsInfo, - XrStage, + OXrSession, + OXrFrameWaiter, + OXrFrameStream, + OXrSwapchain, + OXrSwapchainImages, + OXrGraphicsInfo, + OXrStage, )> { let (session, frame_waiter, frame_stream) = unsafe { instance.create_session(system_id, graphics_info)? }; @@ -284,7 +297,7 @@ fn init_xr_session( // TODO!() support other view configurations let available_view_configurations = instance.enumerate_view_configurations(system_id)?; if !available_view_configurations.contains(&openxr::ViewConfigurationType::PRIMARY_STEREO) { - return Err(XrError::NoAvailableViewConfiguration); + return Err(OXrError::NoAvailableViewConfiguration); } let view_configuration_type = openxr::ViewConfigurationType::PRIMARY_STEREO; @@ -320,7 +333,7 @@ fn init_xr_session( } else { if let Some(config) = view_configuration_views.first() { Some(( - uvec2( + UVec2::new( config.recommended_image_rect_width, config.recommended_image_rect_height, ), @@ -330,7 +343,7 @@ fn init_xr_session( None } } - .ok_or(XrError::NoAvailableViewConfiguration)?; + .ok_or(OXrError::NoAvailableViewConfiguration)?; let available_formats = session.enumerate_swapchain_formats()?; @@ -345,7 +358,7 @@ fn init_xr_session( } else { available_formats.first().copied() } - .ok_or(XrError::NoAvailableFormat)?; + .ok_or(OXrError::NoAvailableFormat)?; let swapchain = session.create_swapchain(SwapchainCreateInfo { create_flags: SwapchainCreateFlags::EMPTY, @@ -378,15 +391,15 @@ fn init_xr_session( } else { available_blend_modes.first().copied() } - .ok_or(XrError::NoAvailableBackend)?; + .ok_or(OXrError::NoAvailableBackend)?; - let stage = XrStage( + let stage = OXrStage( session .create_reference_space(openxr::ReferenceSpaceType::STAGE, openxr::Posef::IDENTITY)? .into(), ); - let graphics_info = XrGraphicsInfo { + let graphics_info = OXrGraphicsInfo { blend_mode, resolution, format, @@ -406,19 +419,19 @@ fn init_xr_session( /// This is used solely to transport resources from the main world to the render world. #[derive(Resource)] struct XrRenderResources { - session: XrSession, - frame_stream: XrFrameStream, - swapchain: XrSwapchain, - images: XrSwapchainImages, - graphics_info: XrGraphicsInfo, - stage: XrStage, + session: OXrSession, + frame_stream: OXrFrameStream, + swapchain: OXrSwapchain, + images: OXrSwapchainImages, + graphics_info: OXrGraphicsInfo, + stage: OXrStage, } pub fn create_xr_session( device: Res, - instance: Res, - create_info: NonSend, - system_id: Res, + instance: Res, + create_info: NonSend, + system_id: Res, mut commands: Commands, ) { match init_xr_session( @@ -446,7 +459,7 @@ pub fn create_xr_session( } } -pub fn begin_xr_session(session: Res, session_started: Res) { +pub fn begin_xr_session(session: Res, session_started: Res) { let _span = info_span!("xr_begin_session"); session .begin(openxr::ViewConfigurationType::PRIMARY_STEREO) @@ -454,7 +467,7 @@ pub fn begin_xr_session(session: Res, session_started: Res, session_started: Res) { +pub fn end_xr_session(session: Res, session_started: Res) { let _span = info_span!("xr_end_session"); session.end().expect("Failed to end session"); session_started.set(false); @@ -483,7 +496,7 @@ pub fn transfer_xr_resources(mut commands: Commands, mut world: ResMut, status: Res) { +pub fn poll_events(instance: Res, status: Res) { let _span = info_span!("xr_poll_events"); let mut buffer = Default::default(); while let Some(event) = instance @@ -519,24 +532,24 @@ pub fn poll_events(instance: Res, status: Res) { } } -pub fn reset_per_frame_resources(mut cleanup: ResMut) { +pub fn reset_per_frame_resources(mut cleanup: ResMut) { **cleanup = false; } pub fn destroy_xr_session(mut commands: Commands) { - commands.remove_resource::(); - commands.remove_resource::(); - commands.remove_resource::(); - commands.remove_resource::(); - commands.remove_resource::(); - commands.insert_resource(XrCleanupSession(true)); + commands.remove_resource::(); + commands.remove_resource::(); + commands.remove_resource::(); + commands.remove_resource::(); + commands.remove_resource::(); + commands.insert_resource(OXrCleanupSession(true)); } pub fn destroy_xr_session_render(world: &mut World) { - world.remove_resource::(); - world.remove_resource::(); - world.remove_resource::(); - world.remove_resource::(); - world.remove_resource::(); - world.remove_resource::(); + world.remove_resource::(); + world.remove_resource::(); + world.remove_resource::(); + world.remove_resource::(); + world.remove_resource::(); + world.remove_resource::(); } diff --git a/crates/bevy_openxr/src/layer_builder.rs b/crates/bevy_openxr/src/openxr/layer_builder.rs similarity index 92% rename from crates/bevy_openxr/src/layer_builder.rs rename to crates/bevy_openxr/src/openxr/layer_builder.rs index 08257bf..77ca85c 100644 --- a/crates/bevy_openxr/src/layer_builder.rs +++ b/crates/bevy_openxr/src/openxr/layer_builder.rs @@ -3,12 +3,12 @@ use std::mem; use openxr::{sys, CompositionLayerFlags, Fovf, Posef, Rect2Di, Space}; use crate::graphics::graphics_match; -use crate::resources::XrSwapchain; +use crate::resources::OXrSwapchain; #[derive(Copy, Clone)] pub struct SwapchainSubImage<'a> { inner: sys::SwapchainSubImage, - swapchain: Option<&'a XrSwapchain>, + swapchain: Option<&'a OXrSwapchain>, } impl<'a> SwapchainSubImage<'a> { @@ -30,7 +30,7 @@ impl<'a> SwapchainSubImage<'a> { &self.inner } #[inline] - pub fn swapchain(mut self, value: &'a XrSwapchain) -> Self { + pub fn swapchain(mut self, value: &'a OXrSwapchain) -> Self { graphics_match!( &value.0; swap => self.inner.swapchain = swap.as_raw() @@ -59,7 +59,7 @@ impl<'a> Default for SwapchainSubImage<'a> { #[derive(Copy, Clone)] pub struct CompositionLayerProjectionView<'a> { inner: sys::CompositionLayerProjectionView, - swapchain: Option<&'a XrSwapchain>, + swapchain: Option<&'a OXrSwapchain>, } impl<'a> CompositionLayerProjectionView<'a> { @@ -104,13 +104,13 @@ impl<'a> Default for CompositionLayerProjectionView<'a> { } } pub unsafe trait CompositionLayer<'a> { - fn swapchain(&self) -> Option<&'a XrSwapchain>; + fn swapchain(&self) -> Option<&'a OXrSwapchain>; fn header(&self) -> &'a sys::CompositionLayerBaseHeader; } #[derive(Clone)] pub struct CompositionLayerProjection<'a> { inner: sys::CompositionLayerProjection, - swapchain: Option<&'a XrSwapchain>, + swapchain: Option<&'a OXrSwapchain>, views: Vec, } impl<'a> CompositionLayerProjection<'a> { @@ -154,7 +154,7 @@ impl<'a> CompositionLayerProjection<'a> { } } unsafe impl<'a> CompositionLayer<'a> for CompositionLayerProjection<'a> { - fn swapchain(&self) -> Option<&'a XrSwapchain> { + fn swapchain(&self) -> Option<&'a OXrSwapchain> { self.swapchain } diff --git a/crates/bevy_openxr/src/openxr/mod.rs b/crates/bevy_openxr/src/openxr/mod.rs new file mode 100644 index 0000000..8f33995 --- /dev/null +++ b/crates/bevy_openxr/src/openxr/mod.rs @@ -0,0 +1,56 @@ +// use actions::XrActionPlugin; +use bevy::{ + app::{PluginGroup, PluginGroupBuilder}, + render::{pipelined_rendering::PipelinedRenderingPlugin, RenderPlugin}, + utils::default, + window::{PresentMode, Window, WindowPlugin}, +}; +use bevy_xr::camera::XrCameraPlugin; +use bevy_xr::session::XrSessionPlugin; +use init::OXrInitPlugin; +use render::XrRenderPlugin; + +pub mod error; +mod exts; +pub mod graphics; +pub mod init; +pub mod layer_builder; +pub mod render; +pub mod resources; +pub mod types; + +pub fn add_xr_plugins(plugins: G) -> PluginGroupBuilder { + plugins + .build() + .disable::() + .disable::() + .add_before::(XrSessionPlugin) + .add_before::(OXrInitPlugin { + app_info: default(), + exts: default(), + blend_modes: default(), + backends: default(), + formats: Some(vec![wgpu::TextureFormat::Rgba8UnormSrgb]), + resolutions: default(), + synchronous_pipeline_compilation: default(), + }) + .add(XrRenderPlugin) + .add(XrCameraPlugin) + // .add(XrActionPlugin) + .set(WindowPlugin { + #[cfg(not(target_os = "android"))] + primary_window: Some(Window { + transparent: true, + present_mode: PresentMode::AutoNoVsync, + // title: self.app_info.name.clone(), + ..default() + }), + #[cfg(target_os = "android")] + primary_window: None, // ? + #[cfg(target_os = "android")] + exit_condition: bevy::window::ExitCondition::DontExit, + #[cfg(target_os = "android")] + close_when_requested: true, + ..default() + }) +} diff --git a/crates/bevy_openxr/src/render.rs b/crates/bevy_openxr/src/openxr/render.rs similarity index 90% rename from crates/bevy_openxr/src/render.rs rename to crates/bevy_openxr/src/openxr/render.rs index 543f2d0..ec22c3b 100644 --- a/crates/bevy_openxr/src/render.rs +++ b/crates/bevy_openxr/src/openxr/render.rs @@ -12,7 +12,7 @@ use bevy::{ use bevy_xr::camera::{XrCamera, XrCameraBundle, XrProjection}; use openxr::{CompositionLayerFlags, ViewStateFlags}; -use crate::init::{session_started, XrPreUpdateSet}; +use crate::init::{session_started, OXrPreUpdateSet}; use crate::layer_builder::*; use crate::resources::*; @@ -20,17 +20,17 @@ pub struct XrRenderPlugin; impl Plugin for XrRenderPlugin { fn build(&self, app: &mut App) { - app.add_plugins((ExtractResourcePlugin::::default(),)) + app.add_plugins((ExtractResourcePlugin::::default(),)) .add_systems( PreUpdate, ( - init_views.run_if(resource_added::), + init_views.run_if(resource_added::), wait_frame.run_if(session_started), locate_views.run_if(session_started), update_views.run_if(session_started), ) .chain() - .after(XrPreUpdateSet::HandleEvents), + .after(OXrPreUpdateSet::HandleEvents), ) .add_systems( PostUpdate, @@ -65,9 +65,9 @@ pub const XR_TEXTURE_INDEX: u32 = 3383858418; // TODO: have cameras initialized externally and then recieved by this function. /// This is needed to properly initialize the texture views so that bevy will set them to the correct resolution despite them being updated in the render world. pub fn init_views( - graphics_info: Res, + graphics_info: Res, mut manual_texture_views: ResMut, - swapchain_images: Res, + swapchain_images: Res, mut commands: Commands, ) { let _span = info_span!("xr_init_views"); @@ -93,22 +93,22 @@ pub fn init_views( )); views.push(default()); } - commands.insert_resource(XrViews(views)); + commands.insert_resource(OXrViews(views)); } -pub fn wait_frame(mut frame_waiter: ResMut, mut commands: Commands) { +pub fn wait_frame(mut frame_waiter: ResMut, mut commands: Commands) { let _span = info_span!("xr_wait_frame"); let state = frame_waiter.wait().expect("Failed to wait frame"); // Here we insert the predicted display time for when this frame will be displayed. // TODO: don't add predicted_display_period if pipelined rendering plugin not enabled - commands.insert_resource(XrTime(state.predicted_display_time)); + commands.insert_resource(OXrTime(state.predicted_display_time)); } pub fn locate_views( - session: Res, - stage: Res, - time: Res, - mut openxr_views: ResMut, + session: Res, + stage: Res, + time: Res, + mut openxr_views: ResMut, ) { let _span = info_span!("xr_locate_views"); let (flags, xr_views) = session @@ -125,7 +125,7 @@ pub fn locate_views( flags & ViewStateFlags::ORIENTATION_VALID == ViewStateFlags::ORIENTATION_VALID, flags & ViewStateFlags::POSITION_VALID == ViewStateFlags::POSITION_VALID, ) { - (true, true) => *openxr_views = XrViews(xr_views), + (true, true) => *openxr_views = OXrViews(xr_views), (true, false) => { for (i, view) in openxr_views.iter_mut().enumerate() { view.pose.orientation = xr_views[i].pose.orientation; @@ -142,7 +142,7 @@ pub fn locate_views( pub fn update_views( mut query: Query<(&mut Transform, &mut XrProjection, &XrCamera)>, - views: ResMut, + views: ResMut, ) { for (mut transform, mut projection, camera) in query.iter_mut() { let Some(view) = views.get(camera.0 as usize) else { @@ -162,8 +162,8 @@ pub fn update_views( } pub fn update_views_render_world( - views: Res, - root: Res, + views: Res, + root: Res, mut query: Query<(&mut ExtractedView, &XrCamera)>, ) { for (mut extracted_view, camera) in query.iter_mut() { @@ -280,10 +280,10 @@ fn calculate_projection(near_z: f32, fov: openxr::Fovf) -> Mat4 { /// # Safety /// Images inserted into texture views here should not be written to until [`wait_image`] is ran pub fn insert_texture_views( - swapchain_images: Res, - mut swapchain: ResMut, + swapchain_images: Res, + mut swapchain: ResMut, mut manual_texture_views: ResMut, - graphics_info: Res, + graphics_info: Res, ) { let _span = info_span!("xr_insert_texture_views"); let index = swapchain.acquire_image().expect("Failed to acquire image"); @@ -294,7 +294,7 @@ pub fn insert_texture_views( } } -pub fn wait_image(mut swapchain: ResMut) { +pub fn wait_image(mut swapchain: ResMut) { swapchain .wait_image(openxr::Duration::INFINITE) .expect("Failed to wait image"); @@ -303,7 +303,7 @@ pub fn wait_image(mut swapchain: ResMut) { pub fn add_texture_view( manual_texture_views: &mut ManualTextureViews, texture: &wgpu::Texture, - info: &XrGraphicsInfo, + info: &OXrGraphicsInfo, index: u32, ) -> ManualTextureViewHandle { let view = texture.create_view(&wgpu::TextureViewDescriptor { @@ -322,17 +322,17 @@ pub fn add_texture_view( handle } -pub fn begin_frame(mut frame_stream: ResMut) { +pub fn begin_frame(mut frame_stream: ResMut) { frame_stream.begin().expect("Failed to begin frame") } pub fn end_frame( - mut frame_stream: ResMut, - mut swapchain: ResMut, - stage: Res, - display_time: Res, - graphics_info: Res, - openxr_views: Res, + mut frame_stream: ResMut, + mut swapchain: ResMut, + stage: Res, + display_time: Res, + graphics_info: Res, + openxr_views: Res, ) { let _span = info_span!("xr_end_frame"); swapchain.release_image().unwrap(); diff --git a/crates/bevy_openxr/src/resources.rs b/crates/bevy_openxr/src/openxr/resources.rs similarity index 62% rename from crates/bevy_openxr/src/resources.rs rename to crates/bevy_openxr/src/openxr/resources.rs index 882a799..d761584 100644 --- a/crates/bevy_openxr/src/resources.rs +++ b/crates/bevy_openxr/src/openxr/resources.rs @@ -1,35 +1,36 @@ -use std::any::TypeId; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; -use crate::error::XrError; +use bevy::prelude::*; +use bevy::render::extract_resource::ExtractResource; +use openxr::AnyGraphics; + +use crate::error::OXrError; use crate::graphics::*; use crate::layer_builder::CompositionLayer; use crate::types::*; -use bevy::prelude::*; -use bevy::render::extract_resource::ExtractResource; -use bevy::utils::HashMap; -use openxr::AnyGraphics; +/// Wrapper around the entry point to the OpenXR API #[derive(Deref, Clone)] -pub struct XrEntry(pub openxr::Entry); +pub struct OXrEntry(pub openxr::Entry); -impl XrEntry { - pub fn enumerate_extensions(&self) -> Result { +impl OXrEntry { + /// Enumerate available extensions for this OpenXR runtime. + pub fn enumerate_extensions(&self) -> Result { Ok(self.0.enumerate_extensions().map(Into::into)?) } pub fn create_instance( &self, app_info: AppInfo, - exts: XrExtensions, + exts: OXrExtensions, layers: &[&str], backend: GraphicsBackend, - ) -> Result { + ) -> Result { let available_exts = self.enumerate_extensions()?; if !backend.is_available(&available_exts) { - return Err(XrError::UnavailableBackend(backend)); + return Err(OXrError::UnavailableBackend(backend)); } let required_exts = exts | backend.required_exts(); @@ -45,7 +46,7 @@ impl XrEntry { layers, )?; - Ok(XrInstance(instance, backend, app_info)) + Ok(OXrInstance(instance, backend, app_info)) } pub fn available_backends(&self) -> Result> { @@ -55,39 +56,47 @@ impl XrEntry { } } +/// Wrapper around [openxr::Instance] with additional data for safety. #[derive(Resource, Deref, Clone)] -pub struct XrInstance( +pub struct OXrInstance( #[deref] pub openxr::Instance, pub(crate) GraphicsBackend, pub(crate) AppInfo, ); -impl XrInstance { +impl OXrInstance { + pub fn into_inner(self) -> openxr::Instance { + self.0 + } + + /// Initialize graphics. This is used to create [WgpuGraphics] for the bevy app and to get the [SessionCreateInfo] to make an XR session. pub fn init_graphics( &self, system_id: openxr::SystemId, - ) -> Result<(WgpuGraphics, XrSessionGraphicsInfo)> { + ) -> Result<(WgpuGraphics, SessionCreateInfo)> { graphics_match!( self.1; _ => { let (graphics, session_info) = Api::init_graphics(&self.2, &self, system_id)?; - Ok((graphics, XrSessionGraphicsInfo(Api::wrap(session_info)))) + Ok((graphics, SessionCreateInfo(Api::wrap(session_info)))) } ) } + /// Creates an [OXrSession] + /// /// # Safety /// /// `info` must contain valid handles for the graphics api pub unsafe fn create_session( &self, system_id: openxr::SystemId, - info: XrSessionGraphicsInfo, - ) -> Result<(XrSession, XrFrameWaiter, XrFrameStream)> { + info: SessionCreateInfo, + ) -> Result<(OXrSession, OXrFrameWaiter, OXrFrameStream)> { if !info.0.using_graphics_of_val(&self.1) { - return Err(XrError::GraphicsBackendMismatch { - item: std::any::type_name::(), + return Err(OXrError::GraphicsBackendMismatch { + item: std::any::type_name::(), backend: info.0.graphics_name(), expected_backend: self.1.graphics_name(), }); @@ -96,59 +105,61 @@ impl XrInstance { info.0; info => { let (session, frame_waiter, frame_stream) = self.0.create_session::(system_id, &info)?; - Ok((session.into(), XrFrameWaiter(frame_waiter), XrFrameStream(Api::wrap(frame_stream)))) + Ok((session.into(), OXrFrameWaiter(frame_waiter), OXrFrameStream(Api::wrap(frame_stream)))) } ) } } -#[derive(Clone)] -pub struct XrSessionGraphicsInfo(pub(crate) GraphicsWrap); - -impl GraphicsType for XrSessionGraphicsInfo { - type Inner = G::SessionCreateInfo; -} - +/// Graphics agnostic wrapper around [openxr::Session] #[derive(Resource, Deref, Clone)] -pub struct XrSession( - #[deref] pub(crate) openxr::Session, - pub(crate) GraphicsWrap, +pub struct OXrSession( + #[deref] pub openxr::Session, + pub GraphicsWrap, ); -impl GraphicsType for XrSession { +impl GraphicsType for OXrSession { type Inner = openxr::Session; } -impl From> for XrSession { - fn from(value: openxr::Session) -> Self { - Self(value.clone().into_any_graphics(), G::wrap(value)) +impl From> for OXrSession { + fn from(session: openxr::Session) -> Self { + Self::new(session) } } -impl XrSession { +impl OXrSession { + pub fn new(session: openxr::Session) -> Self { + Self(session.clone().into_any_graphics(), G::wrap(session)) + } + + /// Enumerate all available swapchain formats. pub fn enumerate_swapchain_formats(&self) -> Result> { graphics_match!( &self.1; - session => Ok(session.enumerate_swapchain_formats()?.into_iter().filter_map(Api::to_wgpu_format).collect()) + session => Ok(session.enumerate_swapchain_formats()?.into_iter().filter_map(Api::into_wgpu_format).collect()) ) } - pub fn create_swapchain(&self, info: SwapchainCreateInfo) -> Result { - Ok(XrSwapchain(graphics_match!( + /// Creates an [OXrSwapchain]. + pub fn create_swapchain(&self, info: SwapchainCreateInfo) -> Result { + Ok(OXrSwapchain(graphics_match!( &self.1; - session => session.create_swapchain(&info.try_into()?)? => XrSwapchain + session => session.create_swapchain(&info.try_into()?)? => OXrSwapchain ))) } } +/// Graphics agnostic wrapper around [openxr::FrameStream] #[derive(Resource)] -pub struct XrFrameStream(pub(crate) GraphicsWrap); +pub struct OXrFrameStream(pub GraphicsWrap); -impl GraphicsType for XrFrameStream { +impl GraphicsType for OXrFrameStream { type Inner = openxr::FrameStream; } -impl XrFrameStream { +impl OXrFrameStream { + /// Indicate that graphics device work is beginning. pub fn begin(&mut self) -> openxr::Result<()> { graphics_match!( &mut self.0; @@ -156,6 +167,10 @@ impl XrFrameStream { ) } + /// Indicate that all graphics work for the frame has been submitted + /// + /// `layers` is an array of references to any type of composition layer, + /// e.g. [`CompositionLayerProjection`](crate::oxr::layer_builder::CompositionLayerProjection) pub fn end( &mut self, display_time: openxr::Time, @@ -187,17 +202,20 @@ impl XrFrameStream { } } +/// Handle for waiting to render a frame. Check [`FrameWaiter`](openxr::FrameWaiter) for available methods. #[derive(Resource, Deref, DerefMut)] -pub struct XrFrameWaiter(pub openxr::FrameWaiter); +pub struct OXrFrameWaiter(pub openxr::FrameWaiter); +/// Graphics agnostic wrapper around [openxr::Swapchain] #[derive(Resource)] -pub struct XrSwapchain(pub(crate) GraphicsWrap); +pub struct OXrSwapchain(pub GraphicsWrap); -impl GraphicsType for XrSwapchain { +impl GraphicsType for OXrSwapchain { type Inner = openxr::Swapchain; } -impl XrSwapchain { +impl OXrSwapchain { + /// Determine the index of the next image to render to in the swapchain image array pub fn acquire_image(&mut self) -> Result { graphics_match!( &mut self.0; @@ -205,6 +223,7 @@ impl XrSwapchain { ) } + /// Wait for the compositor to finish reading from the oldest unwaited acquired image pub fn wait_image(&mut self, timeout: openxr::Duration) -> Result<()> { graphics_match!( &mut self.0; @@ -212,6 +231,7 @@ impl XrSwapchain { ) } + /// Release the oldest acquired image pub fn release_image(&mut self) -> Result<()> { graphics_match!( &mut self.0; @@ -219,12 +239,13 @@ impl XrSwapchain { ) } + /// Enumerates swapchain images and converts them to wgpu [`Texture`](wgpu::Texture)s. pub fn enumerate_images( &self, device: &wgpu::Device, format: wgpu::TextureFormat, resolution: UVec2, - ) -> Result { + ) -> Result { graphics_match!( &self.0; swap => { @@ -234,43 +255,39 @@ impl XrSwapchain { images.push(Api::to_wgpu_img(image, device, format, resolution)?); } } - Ok(XrSwapchainImages(images.into())) + Ok(OXrSwapchainImages(images.into())) } ) } } -#[derive(Deref, Clone, Resource)] -pub struct XrStage(pub Arc); - +/// Stores the generated swapchain images. #[derive(Debug, Deref, Resource, Clone)] -pub struct XrSwapchainImages(pub Arc>); +pub struct OXrSwapchainImages(pub Arc>); -#[derive(Copy, Clone, Eq, PartialEq, Deref, DerefMut, Resource, ExtractResource)] -pub struct XrTime(pub openxr::Time); +/// Thread safe wrapper around [openxr::Space] representing the stage. +#[derive(Deref, Clone, Resource)] +pub struct OXrStage(pub Arc); -#[derive(Copy, Clone, Eq, PartialEq, Resource)] -pub struct XrSwapchainInfo { - pub format: wgpu::TextureFormat, - pub resolution: UVec2, -} +/// Stores the latest generated [OXrViews] +#[derive(Clone, Resource, ExtractResource, Deref, DerefMut)] +pub struct OXrViews(pub Vec); +/// Wrapper around [openxr::SystemId] to allow it to be stored as a resource. #[derive(Debug, Copy, Clone, Deref, Default, Eq, PartialEq, Ord, PartialOrd, Hash, Resource)] -pub struct XrSystemId(pub openxr::SystemId); +pub struct OXrSystemId(pub openxr::SystemId); +/// Resource storing graphics info for the currently running session. #[derive(Clone, Copy, Resource)] -pub struct XrGraphicsInfo { +pub struct OXrGraphicsInfo { pub blend_mode: EnvironmentBlendMode, pub resolution: UVec2, pub format: wgpu::TextureFormat, } -#[derive(Clone, Resource, ExtractResource, Deref, DerefMut)] -pub struct XrViews(pub Vec); - #[derive(Clone)] /// This is used to store information from startup that is needed to create the session after the instance has been created. -pub struct XrSessionCreateInfo { +pub struct SessionConfigInfo { /// List of blend modes the openxr session can use. If [None], pick the first available blend mode. pub blend_modes: Option>, /// List of formats the openxr session can use. If [None], pick the first available format @@ -278,13 +295,13 @@ pub struct XrSessionCreateInfo { /// List of resolutions that the openxr swapchain can use. If [None] pick the first available resolution. pub resolutions: Option>, /// Graphics info used to create a session. - pub graphics_info: XrSessionGraphicsInfo, + pub graphics_info: SessionCreateInfo, } #[derive(Resource, Clone, Default)] -pub struct XrSessionStarted(Arc); +pub struct OXrSessionStarted(Arc); -impl XrSessionStarted { +impl OXrSessionStarted { pub fn set(&self, val: bool) { self.0.store(val, Ordering::SeqCst); } @@ -294,36 +311,14 @@ impl XrSessionStarted { } } +/// The calculated display time for the app. Passed through the pipeline. +#[derive(Copy, Clone, Eq, PartialEq, Deref, DerefMut, Resource, ExtractResource)] +pub struct OXrTime(pub openxr::Time); + +/// The root transform's global position for late latching in the render world. #[derive(ExtractResource, Resource, Clone, Copy, Default)] -pub struct XrRootTransform(pub GlobalTransform); +pub struct OXrRootTransform(pub GlobalTransform); #[derive(ExtractResource, Resource, Clone, Copy, Default, Deref, DerefMut, PartialEq)] /// This is inserted into the world to signify if the session should be cleaned up. -pub struct XrCleanupSession(pub bool); - -#[derive(Resource, Clone, Deref)] -pub struct XrActionSet(#[deref] pub openxr::ActionSet, bool); - -impl XrActionSet { - pub fn new(action_set: openxr::ActionSet) -> Self { - Self(action_set, false) - } - - pub fn attach(&mut self) { - self.1 = true; - } - - pub fn is_attached(&self) -> bool { - self.1 - } -} - -#[derive(Clone)] -pub enum TypedAction { - Bool(openxr::Action), - Float(openxr::Action), - Vector(openxr::Action), -} - -#[derive(Resource, Clone, Deref)] -pub struct XrActions(pub HashMap); \ No newline at end of file +pub struct OXrCleanupSession(pub bool); diff --git a/crates/bevy_openxr/src/types.rs b/crates/bevy_openxr/src/openxr/types.rs similarity index 54% rename from crates/bevy_openxr/src/types.rs rename to crates/bevy_openxr/src/openxr/types.rs index 305e0eb..7d2b1db 100644 --- a/crates/bevy_openxr/src/types.rs +++ b/crates/bevy_openxr/src/openxr/types.rs @@ -1,15 +1,15 @@ use std::borrow::Cow; -pub use crate::error::XrError; -pub use crate::extensions::XrExtensions; -use crate::graphics::GraphicsExt; +use crate::error::OXrError; +use crate::graphics::{GraphicsExt, GraphicsType, GraphicsWrap}; -pub use openxr::{ - ApiLayerProperties, EnvironmentBlendMode, SwapchainCreateFlags, SwapchainUsageFlags, -}; +pub use crate::openxr::exts::OXrExtensions; -pub type Result = std::result::Result; +pub use openxr::{EnvironmentBlendMode, SwapchainCreateFlags, SwapchainUsageFlags}; +pub type Result = std::result::Result; + +/// A container for all required graphics objects needed for a bevy app. pub struct WgpuGraphics( pub wgpu::Device, pub wgpu::Queue, @@ -18,11 +18,13 @@ pub struct WgpuGraphics( pub wgpu::Instance, ); +/// A version number that can be stored inside of a u32 #[derive(Clone, Copy, Debug, Default, PartialEq)] pub struct Version(pub u8, pub u8, pub u16); impl Version { - pub const BEVY: Self = Self(0, 12, 1); + /// Bevy's version number + pub const BEVY: Self = Self(0, 13, 0); pub const fn to_u32(self) -> u32 { let major = (self.0 as u32) << 24; @@ -31,21 +33,29 @@ impl Version { } } +/// Info needed about an app for OpenXR #[derive(Clone, Debug, PartialEq)] pub struct AppInfo { pub name: Cow<'static, str>, pub version: Version, } +impl AppInfo { + /// The default app info for a generic bevy app + pub const BEVY: Self = Self { + name: Cow::Borrowed("Bevy"), + version: Version::BEVY, + }; +} + impl Default for AppInfo { fn default() -> Self { - Self { - name: "Bevy".into(), - version: Version::BEVY, - } + Self::BEVY } } +/// Info needed to create a swapchain. +/// This is an API agnostic version of [openxr::SwapchainCreateInfo] used for some of this library's functions #[derive(Debug, Copy, Clone)] pub struct SwapchainCreateInfo { pub create_flags: SwapchainCreateFlags, @@ -60,14 +70,14 @@ pub struct SwapchainCreateInfo { } impl TryFrom for openxr::SwapchainCreateInfo { - type Error = XrError; + type Error = OXrError; fn try_from(value: SwapchainCreateInfo) -> Result { Ok(openxr::SwapchainCreateInfo { create_flags: value.create_flags, usage_flags: value.usage_flags, format: G::from_wgpu_format(value.format) - .ok_or(XrError::UnsupportedTextureFormat(value.format))?, + .ok_or(OXrError::UnsupportedTextureFormat(value.format))?, sample_count: value.sample_count, width: value.width, height: value.height, @@ -77,3 +87,12 @@ impl TryFrom for openxr::SwapchainCreateInf }) } } + +/// Info needed to create a session. Mostly contains graphics info. +/// This is an API agnostic version of [openxr::Graphics::SessionCreateInfo] used for some of this library's functions +#[derive(Clone)] +pub struct SessionCreateInfo(pub GraphicsWrap); + +impl GraphicsType for SessionCreateInfo { + type Inner = G::SessionCreateInfo; +} diff --git a/crates/bevy_webxr/Cargo.toml b/crates/bevy_webxr/Cargo.toml new file mode 100644 index 0000000..fd28dc1 --- /dev/null +++ b/crates/bevy_webxr/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "bevy_webxr" +version = "0.1.0" +edition = "2021" + + +# all dependencies are placed under this since on anything but wasm, this crate is completely empty +[target.'cfg(target_family = "wasm")'.dependencies] +thiserror = "1.0.57" +wgpu = "0.19.3" +wgpu-hal = "0.19.3" + +bevy_xr.path = "../bevy_xr" +bevy.workspace = true diff --git a/crates/bevy_webxr/src/lib.rs b/crates/bevy_webxr/src/lib.rs new file mode 100644 index 0000000..259e098 --- /dev/null +++ b/crates/bevy_webxr/src/lib.rs @@ -0,0 +1,5 @@ +#[cfg(target_family = "wasm")] +mod webxr; + +#[cfg(target_family = "wasm")] +pub use webxr::*; diff --git a/crates/bevy_openxr/src/camera.rs b/crates/bevy_webxr/src/webxr/mod.rs similarity index 100% rename from crates/bevy_openxr/src/camera.rs rename to crates/bevy_webxr/src/webxr/mod.rs