From 41399d83a96bf7b984fd88413afcdafd65b80a72 Mon Sep 17 00:00:00 2001 From: awtterpip Date: Sun, 2 Jun 2024 18:16:22 -0500 Subject: [PATCH 1/4] move wait frame to after render app finishes --- crates/bevy_openxr/src/openxr/init.rs | 6 ++--- crates/bevy_openxr/src/openxr/mod.rs | 2 +- crates/bevy_openxr/src/openxr/render.rs | 35 +++++++++++++++++++++++-- 3 files changed, 37 insertions(+), 6 deletions(-) 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( From 32fa13b4fda9a46216a22e9b63a5f209f623897c Mon Sep 17 00:00:00 2001 From: awtterpip Date: Sun, 2 Jun 2024 19:30:12 -0500 Subject: [PATCH 2/4] add compatibility for both pipelined and unpipelined --- crates/bevy_openxr/src/openxr/init.rs | 6 +++--- crates/bevy_openxr/src/openxr/render.rs | 12 +++++++++++- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/crates/bevy_openxr/src/openxr/init.rs b/crates/bevy_openxr/src/openxr/init.rs index f77165f..d1da5bd 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/render.rs b/crates/bevy_openxr/src/openxr/render.rs index 2923078..f4baea6 100644 --- a/crates/bevy_openxr/src/openxr/render.rs +++ b/crates/bevy_openxr/src/openxr/render.rs @@ -1,5 +1,5 @@ use bevy::{ - app::SubApp, + app::{MainScheduleOrder, SubApp}, ecs::{query::QuerySingleError, schedule::MainThreadExecutor}, prelude::*, render::{ @@ -33,6 +33,16 @@ impl Plugin for OxrRenderPlugin { if app.is_plugin_added::() { app.init_resource::(); + let mut schedule_order = app.world.resource_mut::(); + + if let Some(pos) = schedule_order + .labels + .iter() + .position(|label| (**label).eq(&OxrLast)) + { + schedule_order.labels.remove(pos); + } + if let Some(sub_app) = app.remove_sub_app(RenderExtractApp) { app.insert_sub_app(RenderExtractApp, SubApp::new(sub_app.app, update_rendering)); } From be23326859bdaa83d981e67402c7dbab3af1cc8b Mon Sep 17 00:00:00 2001 From: awtterpip Date: Sun, 2 Jun 2024 22:31:38 -0500 Subject: [PATCH 3/4] make materials unlit --- crates/bevy_openxr/examples/android/src/lib.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/crates/bevy_openxr/examples/android/src/lib.rs b/crates/bevy_openxr/examples/android/src/lib.rs index ad5ac98..9866566 100644 --- a/crates/bevy_openxr/examples/android/src/lib.rs +++ b/crates/bevy_openxr/examples/android/src/lib.rs @@ -37,17 +37,21 @@ fn setup( mut meshes: ResMut>, mut materials: ResMut>, ) { + let mut white: StandardMaterial = Color::WHITE.into(); + white.unlit = true; // circular base commands.spawn(PbrBundle { mesh: meshes.add(Circle::new(4.0)), - material: materials.add(Color::WHITE), + material: materials.add(white), transform: Transform::from_rotation(Quat::from_rotation_x(-std::f32::consts::FRAC_PI_2)), ..default() }); + let mut cube_mat: StandardMaterial = Color::rgb_u8(124, 144, 255).into(); + cube_mat.unlit = true; // cube commands.spawn(PbrBundle { mesh: meshes.add(Cuboid::new(1.0, 1.0, 1.0)), - material: materials.add(Color::rgb_u8(124, 144, 255)), + material: materials.add(cube_mat), transform: Transform::from_xyz(0.0, 0.5, 0.0), ..default() }); From 810ed1eeed0a7099f9647fbac824906aa572cb00 Mon Sep 17 00:00:00 2001 From: awtterpip Date: Sun, 2 Jun 2024 22:31:58 -0500 Subject: [PATCH 4/4] make pipelined rendering disabled by default --- crates/bevy_openxr/src/openxr/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_openxr/src/openxr/mod.rs b/crates/bevy_openxr/src/openxr/mod.rs index f7e1c0d..b4428e2 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())