diff --git a/crates/bevy_openxr/src/openxr/init.rs b/crates/bevy_openxr/src/openxr/init.rs index d1da5bd..f77165f 100644 --- a/crates/bevy_openxr/src/openxr/init.rs +++ b/crates/bevy_openxr/src/openxr/init.rs @@ -150,9 +150,9 @@ impl Plugin for OxrInitPlugin { .insert_non_send_resource(session_create_info) .configure_sets(OxrLast, OxrHandleEvents); - app.world - .resource_mut::() - .insert_after(Last, OxrLast); + // app.world + // .resource_mut::() + // .insert_after(Last, OxrLast); app.world .spawn((TransformBundle::default(), OxrTrackingRoot)); diff --git a/crates/bevy_openxr/src/openxr/mod.rs b/crates/bevy_openxr/src/openxr/mod.rs index b4428e2..f7e1c0d 100644 --- a/crates/bevy_openxr/src/openxr/mod.rs +++ b/crates/bevy_openxr/src/openxr/mod.rs @@ -34,7 +34,7 @@ pub fn add_xr_plugins(plugins: G) -> PluginGroupBuilder { plugins .build() .disable::() - .disable::() + // .disable::() .add_before::(XrSessionPlugin) .add_before::(OxrInitPlugin::default()) .add(OxrReferenceSpacePlugin::default()) diff --git a/crates/bevy_openxr/src/openxr/render.rs b/crates/bevy_openxr/src/openxr/render.rs index a4daa6e..2923078 100644 --- a/crates/bevy_openxr/src/openxr/render.rs +++ b/crates/bevy_openxr/src/openxr/render.rs @@ -1,13 +1,15 @@ use bevy::{ - ecs::query::QuerySingleError, + app::SubApp, + ecs::{query::QuerySingleError, schedule::MainThreadExecutor}, prelude::*, render::{ camera::{ManualTextureView, ManualTextureViewHandle, ManualTextureViews, RenderTarget}, extract_resource::ExtractResourcePlugin, - pipelined_rendering::PipelinedRenderingPlugin, + pipelined_rendering::{PipelinedRenderingPlugin, RenderAppChannels, RenderExtractApp}, view::ExtractedView, Render, RenderApp, RenderSet, }, + tasks::ComputeTaskPool, }; use bevy_xr::camera::{XrCamera, XrCameraBundle, XrProjection}; use openxr::ViewStateFlags; @@ -30,6 +32,10 @@ impl Plugin for OxrRenderPlugin { fn build(&self, app: &mut App) { if app.is_plugin_added::() { app.init_resource::(); + + if let Some(sub_app) = app.remove_sub_app(RenderExtractApp) { + app.insert_sub_app(RenderExtractApp, SubApp::new(sub_app.app, update_rendering)); + } } app.add_plugins(( @@ -138,6 +144,31 @@ impl Plugin for OxrRenderPlugin { } } +// This function waits for the rendering world to be received, +// runs extract, and then sends the rendering world back to the render thread. +// +// modified pipelined rendering extract function +fn update_rendering(app_world: &mut World, _sub_app: &mut App) { + app_world.resource_scope(|world, main_thread_executor: Mut| { + world.resource_scope(|world, mut render_channels: Mut| { + // we use a scope here to run any main thread tasks that the render world still needs to run + // while we wait for the render world to be received. + let mut render_app = ComputeTaskPool::get() + .scope_with_executor(true, Some(&*main_thread_executor.0), |s| { + s.spawn(async { render_channels.recv().await }); + }) + .pop() + .unwrap(); + + world.run_schedule(OxrLast); + + render_app.extract(world); + + render_channels.send_blocking(render_app); + }); + }); +} + pub const XR_TEXTURE_INDEX: u32 = 3383858418; pub fn init_views(