From db3539d97563a3bdff096360879cde5d5dbe60a8 Mon Sep 17 00:00:00 2001 From: awtterpip Date: Sun, 10 Mar 2024 20:14:46 -0500 Subject: [PATCH] add documentation and satisfy clippy --- crates/bevy_openxr/examples/3d_scene.rs | 8 ++---- crates/bevy_openxr/src/init.rs | 22 +++++++++------- crates/bevy_openxr/src/render.rs | 35 +++++-------------------- crates/bevy_xr/src/session.rs | 6 ++--- 4 files changed, 25 insertions(+), 46 deletions(-) diff --git a/crates/bevy_openxr/examples/3d_scene.rs b/crates/bevy_openxr/examples/3d_scene.rs index 391a9ab..bbc2873 100644 --- a/crates/bevy_openxr/examples/3d_scene.rs +++ b/crates/bevy_openxr/examples/3d_scene.rs @@ -1,11 +1,7 @@ //! A simple 3D scene with light shining over a cube sitting on a plane. -use bevy::{ - prelude::*, - render::camera::{ManualTextureViewHandle, RenderTarget}, -}; -use bevy_openxr::{add_xr_plugins, render::XR_TEXTURE_INDEX}; -use bevy_xr::camera::XrCameraBundle; +use bevy::prelude::*; +use bevy_openxr::add_xr_plugins; fn main() { App::new() diff --git a/crates/bevy_openxr/src/init.rs b/crates/bevy_openxr/src/init.rs index 3076281..1a10170 100644 --- a/crates/bevy_openxr/src/init.rs +++ b/crates/bevy_openxr/src/init.rs @@ -1,4 +1,4 @@ -use bevy::app::{App, First, Plugin, PreUpdate}; +use bevy::app::{App, First, Plugin}; use bevy::ecs::event::EventWriter; use bevy::ecs::schedule::common_conditions::{not, on_event}; use bevy::ecs::schedule::IntoSystemConfigs; @@ -6,7 +6,6 @@ use bevy::ecs::system::{Commands, Res, ResMut, Resource}; use bevy::ecs::world::World; use bevy::log::{error, info}; use bevy::math::{uvec2, UVec2}; -use bevy::render::camera::ManualTextureViews; use bevy::render::extract_resource::ExtractResourcePlugin; use bevy::render::renderer::{ RenderAdapter, RenderAdapterInfo, RenderDevice, RenderInstance, RenderQueue, @@ -48,14 +47,14 @@ pub fn session_created(status: Option>) -> bool { status.is_some_and(|status| status.session_created) } -pub fn session_running(status: Option>) -> bool { - status.is_some_and(|status| status.session_running) -} - pub fn session_ready(status: Option>) -> bool { status.is_some_and(|status| status.session_ready) } +pub fn session_running(status: Option>) -> bool { + status.is_some_and(|status| status.session_running) +} + impl Plugin for XrInitPlugin { fn build(&self, app: &mut App) { if let Err(e) = init_xr(&self, app) { @@ -74,6 +73,7 @@ fn xr_entry() -> Result { Ok(XrEntry(entry)) } +/// This is called from [`XrInitPlugin::build()`]. Its a separate function so that we can return a [Result] and control flow is cleaner. fn init_xr(config: &XrInitPlugin, app: &mut App) -> Result<()> { let entry = xr_entry()?; @@ -165,7 +165,8 @@ fn init_xr(config: &XrInitPlugin, app: &mut App) -> Result<()> { begin_xr_session .run_if(session_ready) .run_if(on_event::()), - ), + ) + .chain(), ) .sub_app_mut(RenderApp) .insert_resource(instance) @@ -208,12 +209,13 @@ pub fn create_xr_session(world: &mut World) { return; }; - if let Err(e) = init_xr_session(world, instance, *system_id, create_info) { + if let Err(e) = create_xr_session_inner(world, instance, *system_id, create_info) { error!("Failed to initialize XrSession: {e}"); } } -fn init_xr_session( +/// This is called from [create_xr_session]. It is a separate function to allow us to return a [Result] and make control flow cleaner. +fn create_xr_session_inner( world: &mut World, instance: XrInstance, system_id: openxr::SystemId, @@ -378,6 +380,7 @@ struct XrRenderResources { stage: XrStage, } +/// This system transfers important render resources from the main world to the render world when a session is created. pub fn transfer_xr_resources(mut commands: Commands, mut world: ResMut) { let Some(XrRenderResources { session, @@ -399,6 +402,7 @@ pub fn transfer_xr_resources(mut commands: Commands, mut world: ResMut, session: Option>, diff --git a/crates/bevy_openxr/src/render.rs b/crates/bevy_openxr/src/render.rs index 044fd46..41741e8 100644 --- a/crates/bevy_openxr/src/render.rs +++ b/crates/bevy_openxr/src/render.rs @@ -9,8 +9,8 @@ use bevy::{ use bevy_xr::camera::{XrCameraBundle, XrProjection, XrView}; use openxr::CompositionLayerFlags; -use crate::layer_builder::*; use crate::resources::*; +use crate::{init::begin_xr_session, layer_builder::*}; use crate::init::session_running; @@ -19,11 +19,12 @@ pub struct XrRenderPlugin; impl Plugin for XrRenderPlugin { fn build(&self, app: &mut App) { app.add_systems( - PreUpdate, + First, ( init_views.run_if(resource_added::), wait_frame.run_if(session_running), - ), + ) + .after(begin_xr_session), ); // .add_systems(Startup, init_views); app.sub_app_mut(RenderApp).add_systems( @@ -42,6 +43,7 @@ impl Plugin for XrRenderPlugin { 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, @@ -73,6 +75,8 @@ pub fn init_views( pub fn wait_frame(mut frame_waiter: ResMut, mut commands: Commands) { 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(openxr::Time::from_nanos( state.predicted_display_time.as_nanos() + state.predicted_display_period.as_nanos(), ))); @@ -227,31 +231,6 @@ pub fn insert_texture_views( for i in 0..2 { add_texture_view(&mut manual_texture_views, image, &graphics_info, i); } - // let left = image.create_view(&wgpu::TextureViewDescriptor { - // dimension: Some(wgpu::TextureViewDimension::D2), - // array_layer_count: Some(1), - // ..Default::default() - // }); - // let right = image.create_view(&wgpu::TextureViewDescriptor { - // dimension: Some(wgpu::TextureViewDimension::D2), - // array_layer_count: Some(1), - // base_array_layer: 1, - // ..Default::default() - // }); - // let resolution = graphics_info.resolution; - // let format = graphics_info.format; - // let left = ManualTextureView { - // texture_view: left.into(), - // size: resolution, - // format: format, - // }; - // let right = ManualTextureView { - // texture_view: right.into(), - // size: resolution, - // format: format, - // }; - // manual_texture_views.insert(LEFT_XR_TEXTURE_HANDLE, left); - // manual_texture_views.insert(RIGHT_XR_TEXTURE_HANDLE, right); } pub fn add_texture_view( diff --git a/crates/bevy_xr/src/session.rs b/crates/bevy_xr/src/session.rs index eeff78b..adfe357 100644 --- a/crates/bevy_xr/src/session.rs +++ b/crates/bevy_xr/src/session.rs @@ -49,15 +49,15 @@ pub fn handle_xr_events( } } -/// Event sent to backends to create an XR session +/// Event sent to backends to create an XR session. Should only be called after [`XrInstanceCreated`] is recieved. #[derive(Event, Clone, Copy, Default)] pub struct CreateXrSession; -/// Event sent to backends to begin an XR session +/// Event sent to backends to begin an XR session. Should only be called after [`XrSessionState::Ready`] is recieved. #[derive(Event, Clone, Copy, Default)] pub struct BeginXrSession; -/// Event sent to backends to end an XR session. +/// Event sent to backends to end an XR session. Should only be called after [`XrSessionState::Running`] is recieved. #[derive(Event, Clone, Copy, Default)] pub struct EndXrSession;