cleaned up code and created webxr crate

This commit is contained in:
awtterpip
2024-04-18 17:06:56 -05:00
parent d98b9a4455
commit f585c9e2dc
17 changed files with 531 additions and 634 deletions

View File

@@ -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]

View File

@@ -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::<Jump>()
.add_systems(Startup, setup)
.run();
}
@@ -28,15 +15,7 @@ fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
actions: Res<XrActions>,
instance: Res<XrInstance>,
) {
let TypedAction::Bool(action) = actions.get(&TypeId::of::<Jump>()).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<ActionState<Jump>>
) {
info!("{}", state.pressed())
}

View File

@@ -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::<XrSession>),
);
}
}
pub fn create_action_sets(
instance: Res<XrInstance>,
action_list: Res<ActionList>,
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<XrActionSet>, session: Res<XrSession>) {
session
.attach_action_sets(&[&action_set])
.expect("Failed to attach action sets");
action_set.attach();
}
pub fn sync_actions(session: Res<XrSession>, action_set: Res<XrActionSet>) {
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<A: Action>(PhantomData<A>);
impl<A> Plugin for XrActionUpdatePlugin<A>
where
A: Action,
A::ActionType: XrActionTy,
{
fn build(&self, app: &mut App) {
app.add_systems(PreUpdate, update_action_state::<A>.in_set(InputSystem).run_if(session_running));
}
}
impl<A: Action> Default for XrActionUpdatePlugin<A> {
fn default() -> Self {
Self(Default::default())
}
}
pub trait XrActionTy: Sized {
fn get_action_state(
action: &TypedAction,
session: &XrSession,
subaction_path: Option<openxr::Path>,
) -> Option<Self>;
}
impl XrActionTy for bool {
fn get_action_state(
action: &TypedAction,
session: &XrSession,
subaction_path: Option<openxr::Path>,
) -> Option<Self> {
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<openxr::Path>,
) -> Option<Self> {
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<openxr::Path>,
) -> Option<Self> {
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<A>(
mut action_state: ResMut<ActionState<A>>,
session: Res<XrSession>,
actions: Res<XrActions>,
) where
A: Action,
A::ActionType: XrActionTy,
{
if let Some(action) = actions.get(&TypeId::of::<A>()) {
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::<A>()
);
}
}
}
pub trait ActionApp {
fn register_action<A>(&mut self) -> &mut Self
where
A: Action,
A::ActionType: XrActionTy;
}
impl ActionApp for App {
fn register_action<A>(&mut self) -> &mut Self
where
A: Action,
A::ActionType: XrActionTy,
{
self.add_plugins((
ActionPlugin::<A>::default(),
XrActionUpdatePlugin::<A>::default(),
))
}
}

View File

@@ -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<G: PluginGroup>(plugins: G) -> PluginGroupBuilder {
plugins
.build()
.disable::<RenderPlugin>()
.disable::<PipelinedRenderingPlugin>()
.add_before::<RenderPlugin, _>(bevy_xr::session::XrSessionPlugin)
.add_before::<RenderPlugin, _>(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::*;

View File

@@ -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<Vec<Cow<'static, str>>> 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<ash::vk::Result> for OXrError {
fn from(value: ash::vk::Result) -> Self {
Self::InitError(InitError::VulkanError(value))
}
}
#[cfg(feature = "vulkan")]
impl From<ash::LoadingError> for OXrError {
fn from(value: ash::LoadingError) -> Self {
Self::InitError(InitError::VulkanLoadingError(value))
}
}
}
impl From<Vec<Cow<'static, str>>> for OXrError {
fn from(value: Vec<Cow<'static, str>>) -> Self {
Self::UnavailableExtensions(UnavailableExts(value))
}

View File

@@ -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<ExtensionSet> for XrExtensions {
impl From<ExtensionSet> for OXrExtensions {
fn from(value: ExtensionSet) -> Self {
Self(value)
}
}
impl From<XrExtensions> for ExtensionSet {
fn from(val: XrExtensions) -> Self {
impl From<OXrExtensions> 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,

View File

@@ -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<T: GraphicsType>(item: T::Inner<Self>) -> GraphicsWrap<T>;
/// 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<Self::Format>;
/// Convert from the graphics format to wgpu format
fn to_wgpu_format(format: Self::Format) -> Option<wgpu::TextureFormat>;
/// 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<wgpu::TextureFormat>;
/// 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<wgpu::Texture>;
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<XrSession>);
///
/// impl GraphicsType for XrSession {
/// type Inner<G: GraphicsExt> = openxr::Session<G>;
/// }
///
/// ```
///
/// In this example, `GraphicsWrap<XrSession>` 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<G: GraphicsExt>;
}
@@ -39,12 +58,13 @@ impl GraphicsType for () {
type Inner<G: GraphicsExt> = ();
}
/// 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<Self> {
pub fn available_backends(exts: &OXrExtensions) -> Vec<Self> {
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<T: GraphicsType> {
#[cfg(feature = "vulkan")]
@@ -97,6 +118,33 @@ impl<T: GraphicsType> GraphicsWrap<T> {
}
}
/// 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<XrFrameStream>);
///
/// impl GraphicsType for OXrFrameStream {
/// // Here is the inner type
/// type Inner<G: GraphicsExt> = openxr::FrameStream<G>;
/// }
///
/// 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)
};
(

View File

@@ -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<T: GraphicsType>(item: T::Inner<Self>) -> GraphicsWrap<T> {
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<Self::Format> {
wgpu_to_vulkan(format).map(|f| f.as_raw() as _)
}
fn to_wgpu_format(format: Self::Format) -> Option<wgpu::TextureFormat> {
fn into_wgpu_format(format: Self::Format) -> Option<wgpu::TextureFormat> {
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<wgpu::Texture> {
let color_image = ash::vk::Image::from_raw(color_image);
let wgpu_hal_texture = unsafe {
<wgpu_hal::vulkan::Api as wgpu_hal::Api>::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::vulkan::Api>(
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<wgpu::Texture> {
let color_image = ash::vk::Image::from_raw(color_image);
let wgpu_hal_texture = unsafe {
<wgpu_hal::vulkan::Api as wgpu_hal::Api>::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::vulkan::Api>(
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<T: super::GraphicsType>(item: T::Inner<Self>) -> super::GraphicsWrap<T> {
super::GraphicsWrap::Vulkan(item)
}
}
fn vulkan_to_wgpu(format: ash::vk::Format) -> Option<wgpu::TextureFormat> {

View File

@@ -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<Res<XrSessionStarted>>) -> bool {
pub fn session_started(started: Option<Res<OXrSessionStarted>>) -> 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<Vec<EnvironmentBlendMode>>,
/// 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::<XrCleanupSession>::default(),
ExtractResourcePlugin::<XrTime>::default(),
ExtractResourcePlugin::<XrRootTransform>::default(),
ExtractResourcePlugin::<OXrCleanupSession>::default(),
ExtractResourcePlugin::<OXrTime>::default(),
ExtractResourcePlugin::<OXrRootTransform>::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::<DestroyXrSession>())
.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::<XrCleanupSession>()
.init_resource::<XrRootTransform>()
.init_resource::<OXrCleanupSession>()
.init_resource::<OXrRootTransform>()
.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::<XrRootTransform>()
.init_resource::<XrCleanupSession>()
.init_resource::<OXrRootTransform>()
.init_resource::<OXrCleanupSession>()
.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<XrRootTransform>,
root: Query<&GlobalTransform, With<XrTrackingRoot>>,
mut root_transform: ResMut<OXrRootTransform>,
root: Query<&GlobalTransform, With<OXrTrackingRoot>>,
) {
let transform = root.single();
root_transform.0 = *transform;
}
fn xr_entry() -> Result<XrEntry> {
fn xr_entry() -> Result<OXrEntry> {
#[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<RenderDevice>,
instance: Res<XrInstance>,
create_info: NonSend<XrSessionCreateInfo>,
system_id: Res<XrSystemId>,
instance: Res<OXrInstance>,
create_info: NonSend<SessionConfigInfo>,
system_id: Res<OXrSystemId>,
mut commands: Commands,
) {
match init_xr_session(
@@ -446,7 +459,7 @@ pub fn create_xr_session(
}
}
pub fn begin_xr_session(session: Res<XrSession>, session_started: Res<XrSessionStarted>) {
pub fn begin_xr_session(session: Res<OXrSession>, session_started: Res<OXrSessionStarted>) {
let _span = info_span!("xr_begin_session");
session
.begin(openxr::ViewConfigurationType::PRIMARY_STEREO)
@@ -454,7 +467,7 @@ pub fn begin_xr_session(session: Res<XrSession>, session_started: Res<XrSessionS
session_started.set(true);
}
pub fn end_xr_session(session: Res<XrSession>, session_started: Res<XrSessionStarted>) {
pub fn end_xr_session(session: Res<OXrSession>, session_started: Res<OXrSessionStarted>) {
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<MainWorld
}
/// Polls any OpenXR events and handles them accordingly
pub fn poll_events(instance: Res<XrInstance>, status: Res<XrSharedStatus>) {
pub fn poll_events(instance: Res<OXrInstance>, status: Res<XrSharedStatus>) {
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<XrInstance>, status: Res<XrSharedStatus>) {
}
}
pub fn reset_per_frame_resources(mut cleanup: ResMut<XrCleanupSession>) {
pub fn reset_per_frame_resources(mut cleanup: ResMut<OXrCleanupSession>) {
**cleanup = false;
}
pub fn destroy_xr_session(mut commands: Commands) {
commands.remove_resource::<XrSession>();
commands.remove_resource::<XrFrameWaiter>();
commands.remove_resource::<XrSwapchainImages>();
commands.remove_resource::<XrGraphicsInfo>();
commands.remove_resource::<XrStage>();
commands.insert_resource(XrCleanupSession(true));
commands.remove_resource::<OXrSession>();
commands.remove_resource::<OXrFrameWaiter>();
commands.remove_resource::<OXrSwapchainImages>();
commands.remove_resource::<OXrGraphicsInfo>();
commands.remove_resource::<OXrStage>();
commands.insert_resource(OXrCleanupSession(true));
}
pub fn destroy_xr_session_render(world: &mut World) {
world.remove_resource::<XrSwapchain>();
world.remove_resource::<XrFrameStream>();
world.remove_resource::<XrStage>();
world.remove_resource::<XrSwapchainImages>();
world.remove_resource::<XrGraphicsInfo>();
world.remove_resource::<XrSession>();
world.remove_resource::<OXrSwapchain>();
world.remove_resource::<OXrFrameStream>();
world.remove_resource::<OXrStage>();
world.remove_resource::<OXrSwapchainImages>();
world.remove_resource::<OXrGraphicsInfo>();
world.remove_resource::<OXrSession>();
}

View File

@@ -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<sys::CompositionLayerProjectionView>,
}
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
}

View File

@@ -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<G: PluginGroup>(plugins: G) -> PluginGroupBuilder {
plugins
.build()
.disable::<RenderPlugin>()
.disable::<PipelinedRenderingPlugin>()
.add_before::<RenderPlugin, _>(XrSessionPlugin)
.add_before::<RenderPlugin, _>(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()
})
}

View File

@@ -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::<XrViews>::default(),))
app.add_plugins((ExtractResourcePlugin::<OXrViews>::default(),))
.add_systems(
PreUpdate,
(
init_views.run_if(resource_added::<XrGraphicsInfo>),
init_views.run_if(resource_added::<OXrGraphicsInfo>),
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<XrGraphicsInfo>,
graphics_info: Res<OXrGraphicsInfo>,
mut manual_texture_views: ResMut<ManualTextureViews>,
swapchain_images: Res<XrSwapchainImages>,
swapchain_images: Res<OXrSwapchainImages>,
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<XrFrameWaiter>, mut commands: Commands) {
pub fn wait_frame(mut frame_waiter: ResMut<OXrFrameWaiter>, 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<XrSession>,
stage: Res<XrStage>,
time: Res<XrTime>,
mut openxr_views: ResMut<XrViews>,
session: Res<OXrSession>,
stage: Res<OXrStage>,
time: Res<OXrTime>,
mut openxr_views: ResMut<OXrViews>,
) {
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<XrViews>,
views: ResMut<OXrViews>,
) {
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<XrViews>,
root: Res<XrRootTransform>,
views: Res<OXrViews>,
root: Res<OXrRootTransform>,
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<XrSwapchainImages>,
mut swapchain: ResMut<XrSwapchain>,
swapchain_images: Res<OXrSwapchainImages>,
mut swapchain: ResMut<OXrSwapchain>,
mut manual_texture_views: ResMut<ManualTextureViews>,
graphics_info: Res<XrGraphicsInfo>,
graphics_info: Res<OXrGraphicsInfo>,
) {
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<XrSwapchain>) {
pub fn wait_image(mut swapchain: ResMut<OXrSwapchain>) {
swapchain
.wait_image(openxr::Duration::INFINITE)
.expect("Failed to wait image");
@@ -303,7 +303,7 @@ pub fn wait_image(mut swapchain: ResMut<XrSwapchain>) {
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<XrFrameStream>) {
pub fn begin_frame(mut frame_stream: ResMut<OXrFrameStream>) {
frame_stream.begin().expect("Failed to begin frame")
}
pub fn end_frame(
mut frame_stream: ResMut<XrFrameStream>,
mut swapchain: ResMut<XrSwapchain>,
stage: Res<XrStage>,
display_time: Res<XrTime>,
graphics_info: Res<XrGraphicsInfo>,
openxr_views: Res<XrViews>,
mut frame_stream: ResMut<OXrFrameStream>,
mut swapchain: ResMut<OXrSwapchain>,
stage: Res<OXrStage>,
display_time: Res<OXrTime>,
graphics_info: Res<OXrGraphicsInfo>,
openxr_views: Res<OXrViews>,
) {
let _span = info_span!("xr_end_frame");
swapchain.release_image().unwrap();

View File

@@ -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<XrExtensions> {
impl OXrEntry {
/// Enumerate available extensions for this OpenXR runtime.
pub fn enumerate_extensions(&self) -> Result<OXrExtensions> {
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<XrInstance> {
) -> Result<OXrInstance> {
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<Vec<GraphicsBackend>> {
@@ -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::<XrSessionGraphicsInfo>(),
return Err(OXrError::GraphicsBackendMismatch {
item: std::any::type_name::<SessionCreateInfo>(),
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::<Api>(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<Self>);
impl GraphicsType for XrSessionGraphicsInfo {
type Inner<G: GraphicsExt> = G::SessionCreateInfo;
}
/// Graphics agnostic wrapper around [openxr::Session]
#[derive(Resource, Deref, Clone)]
pub struct XrSession(
#[deref] pub(crate) openxr::Session<AnyGraphics>,
pub(crate) GraphicsWrap<Self>,
pub struct OXrSession(
#[deref] pub openxr::Session<AnyGraphics>,
pub GraphicsWrap<Self>,
);
impl GraphicsType for XrSession {
impl GraphicsType for OXrSession {
type Inner<G: GraphicsExt> = openxr::Session<G>;
}
impl<G: GraphicsExt> From<openxr::Session<G>> for XrSession {
fn from(value: openxr::Session<G>) -> Self {
Self(value.clone().into_any_graphics(), G::wrap(value))
impl<G: GraphicsExt> From<openxr::Session<G>> for OXrSession {
fn from(session: openxr::Session<G>) -> Self {
Self::new(session)
}
}
impl XrSession {
impl OXrSession {
pub fn new<G: GraphicsExt>(session: openxr::Session<G>) -> Self {
Self(session.clone().into_any_graphics(), G::wrap(session))
}
/// Enumerate all available swapchain formats.
pub fn enumerate_swapchain_formats(&self) -> Result<Vec<wgpu::TextureFormat>> {
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<XrSwapchain> {
Ok(XrSwapchain(graphics_match!(
/// Creates an [OXrSwapchain].
pub fn create_swapchain(&self, info: SwapchainCreateInfo) -> Result<OXrSwapchain> {
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<Self>);
pub struct OXrFrameStream(pub GraphicsWrap<Self>);
impl GraphicsType for XrFrameStream {
impl GraphicsType for OXrFrameStream {
type Inner<G: GraphicsExt> = openxr::FrameStream<G>;
}
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<Self>);
pub struct OXrSwapchain(pub GraphicsWrap<Self>);
impl GraphicsType for XrSwapchain {
impl GraphicsType for OXrSwapchain {
type Inner<G: GraphicsExt> = openxr::Swapchain<G>;
}
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<u32> {
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<XrSwapchainImages> {
) -> Result<OXrSwapchainImages> {
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<openxr::Space>);
/// Stores the generated swapchain images.
#[derive(Debug, Deref, Resource, Clone)]
pub struct XrSwapchainImages(pub Arc<Vec<wgpu::Texture>>);
pub struct OXrSwapchainImages(pub Arc<Vec<wgpu::Texture>>);
#[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<openxr::Space>);
#[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<openxr::View>);
/// 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<openxr::View>);
#[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<Vec<EnvironmentBlendMode>>,
/// 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<Vec<UVec2>>,
/// Graphics info used to create a session.
pub graphics_info: XrSessionGraphicsInfo,
pub graphics_info: SessionCreateInfo,
}
#[derive(Resource, Clone, Default)]
pub struct XrSessionStarted(Arc<AtomicBool>);
pub struct OXrSessionStarted(Arc<AtomicBool>);
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<bool>),
Float(openxr::Action<f32>),
Vector(openxr::Action<openxr::Vector2f>),
}
#[derive(Resource, Clone, Deref)]
pub struct XrActions(pub HashMap<TypeId, TypedAction>);
pub struct OXrCleanupSession(pub bool);

View File

@@ -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<T> = std::result::Result<T, XrError>;
pub use openxr::{EnvironmentBlendMode, SwapchainCreateFlags, SwapchainUsageFlags};
pub type Result<T> = std::result::Result<T, OXrError>;
/// 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<G: GraphicsExt> TryFrom<SwapchainCreateInfo> for openxr::SwapchainCreateInfo<G> {
type Error = XrError;
type Error = OXrError;
fn try_from(value: SwapchainCreateInfo) -> Result<Self> {
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<G: GraphicsExt> TryFrom<SwapchainCreateInfo> 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<Self>);
impl GraphicsType for SessionCreateInfo {
type Inner<G: GraphicsExt> = G::SessionCreateInfo;
}