maybe fixed jittering?

This commit is contained in:
awtterpip
2024-03-16 20:19:15 -05:00
parent b77bb954d0
commit 78c6cc2192
4 changed files with 35 additions and 31 deletions

View File

@@ -87,7 +87,7 @@ impl Plugin for XrInitPlugin {
.run_if(status_equals(XrStatus::Ready)), .run_if(status_equals(XrStatus::Ready)),
end_xr_session end_xr_session
.run_if(on_event::<EndXrSession>()) .run_if(on_event::<EndXrSession>())
.run_if(status_equals(XrStatus::Exiting)), .run_if(status_equals(XrStatus::Stopping)),
) )
.in_set(XrPreUpdateSet::HandleEvents), .in_set(XrPreUpdateSet::HandleEvents),
), ),
@@ -188,8 +188,8 @@ impl XrInitPlugin {
let instance = entry.create_instance( let instance = entry.create_instance(
self.app_info.clone(), self.app_info.clone(),
exts, exts,
&["XR_APILAYER_LUNARG_api_dump"], // &["XR_APILAYER_LUNARG_api_dump"],
// &[], &[],
backend, backend,
)?; )?;
let instance_props = instance.properties()?; let instance_props = instance.properties()?;
@@ -317,7 +317,7 @@ fn init_xr_session(
} }
.ok_or(XrError::NoAvailableFormat)?; .ok_or(XrError::NoAvailableFormat)?;
let mut swapchain = session.create_swapchain(SwapchainCreateInfo { let swapchain = session.create_swapchain(SwapchainCreateInfo {
create_flags: SwapchainCreateFlags::EMPTY, create_flags: SwapchainCreateFlags::EMPTY,
usage_flags: SwapchainUsageFlags::COLOR_ATTACHMENT | SwapchainUsageFlags::SAMPLED, usage_flags: SwapchainUsageFlags::COLOR_ATTACHMENT | SwapchainUsageFlags::SAMPLED,
format, format,
@@ -404,6 +404,7 @@ pub fn create_xr_session(
commands.insert_resource(graphics_info.clone()); commands.insert_resource(graphics_info.clone());
commands.insert_resource(stage.clone()); commands.insert_resource(stage.clone());
commands.insert_resource(frame_stream.clone()); commands.insert_resource(frame_stream.clone());
commands.insert_resource(swapchain.clone());
commands.insert_resource(XrRenderResources { commands.insert_resource(XrRenderResources {
session, session,
frame_stream, frame_stream,
@@ -433,11 +434,15 @@ pub fn app_exit_xr(
mut app_exiting: ResMut<AppExiting>, mut app_exiting: ResMut<AppExiting>,
mut app_exit_events: ResMut<Events<AppExit>>, mut app_exit_events: ResMut<Events<AppExit>>,
session_started: Res<XrSessionStarted>, session_started: Res<XrSessionStarted>,
session: Option<Res<XrSession>>,
) { ) {
// we need to temporarily intercept the exit event to allow the session to exit. // we need to temporarily intercept the exit event to allow the session to exit.
app_exit_events.clear(); app_exit_events.clear();
*app_exiting = AppExiting(true); *app_exiting = AppExiting(true);
session_started.set(false); session_started.set(false);
if let Some(session) = &session {
session.request_exit().expect("Failed to request exit");
}
} }
/// This system transfers important render resources from the main world to the render world when a session is created. /// This system transfers important render resources from the main world to the render world when a session is created.

View File

@@ -33,7 +33,7 @@ impl<'a> SwapchainSubImage<'a> {
pub fn swapchain(mut self, value: &'a XrSwapchain) -> Self { pub fn swapchain(mut self, value: &'a XrSwapchain) -> Self {
graphics_match!( graphics_match!(
&value.0; &value.0;
swap => self.inner.swapchain = swap.as_raw() swap => self.inner.swapchain = swap.lock().unwrap().as_raw()
); );
self.swapchain = Some(value); self.swapchain = Some(value);
self self

View File

@@ -23,6 +23,7 @@ impl Plugin for XrRenderPlugin {
PreUpdate, PreUpdate,
( (
init_views.run_if(resource_added::<XrGraphicsInfo>), init_views.run_if(resource_added::<XrGraphicsInfo>),
insert_texture_views.run_if(session_started),
wait_frame.run_if(session_started), wait_frame.run_if(session_started),
) )
.chain() .chain()
@@ -38,13 +39,10 @@ impl Plugin for XrRenderPlugin {
app.sub_app_mut(RenderApp).add_systems( app.sub_app_mut(RenderApp).add_systems(
Render, Render,
( (
( // (insert_texture_views)
// begin_frame, // .chain()
insert_texture_views // .in_set(RenderSet::PrepareAssets),
) (end_frame).chain().in_set(RenderSet::Cleanup),
.chain()
.in_set(RenderSet::PrepareAssets),
(locate_views, end_frame).chain().in_set(RenderSet::Cleanup),
) )
.run_if(session_started), .run_if(session_started),
); );
@@ -255,7 +253,7 @@ fn calculate_projection(near_z: f32, fov: openxr::Fovf) -> Mat4 {
pub fn insert_texture_views( pub fn insert_texture_views(
swapchain_images: Res<XrSwapchainImages>, swapchain_images: Res<XrSwapchainImages>,
mut swapchain: ResMut<XrSwapchain>, swapchain: ResMut<XrSwapchain>,
mut manual_texture_views: ResMut<ManualTextureViews>, mut manual_texture_views: ResMut<ManualTextureViews>,
graphics_info: Res<XrGraphicsInfo>, graphics_info: Res<XrGraphicsInfo>,
) { ) {
@@ -298,8 +296,8 @@ pub fn begin_frame(frame_stream: ResMut<XrFrameStream>) {
} }
pub fn end_frame( pub fn end_frame(
mut frame_stream: ResMut<XrFrameStream>, frame_stream: ResMut<XrFrameStream>,
mut swapchain: ResMut<XrSwapchain>, swapchain: ResMut<XrSwapchain>,
stage: Res<XrStage>, stage: Res<XrStage>,
display_time: Res<XrTime>, display_time: Res<XrTime>,
graphics_info: Res<XrGraphicsInfo>, graphics_info: Res<XrGraphicsInfo>,

View File

@@ -134,7 +134,7 @@ impl XrSession {
pub fn create_swapchain(&self, info: SwapchainCreateInfo) -> Result<XrSwapchain> { pub fn create_swapchain(&self, info: SwapchainCreateInfo) -> Result<XrSwapchain> {
Ok(XrSwapchain(graphics_match!( Ok(XrSwapchain(graphics_match!(
&self.1; &self.1;
session => session.create_swapchain(&info.try_into()?)? => XrSwapchain session => Arc::new(Mutex::new(session.create_swapchain(&info.try_into()?)?)) => XrSwapchain
))) )))
} }
} }
@@ -155,13 +155,13 @@ impl XrFrameStream {
} }
pub fn end( pub fn end(
&mut self, &self,
display_time: openxr::Time, display_time: openxr::Time,
environment_blend_mode: openxr::EnvironmentBlendMode, environment_blend_mode: openxr::EnvironmentBlendMode,
layers: &[&dyn CompositionLayer], layers: &[&dyn CompositionLayer],
) -> Result<()> { ) -> Result<()> {
graphics_match!( graphics_match!(
&mut self.0; &self.0;
stream => { stream => {
let mut stream = stream.lock().unwrap(); let mut stream = stream.lock().unwrap();
let mut new_layers = vec![]; let mut new_layers = vec![];
@@ -189,44 +189,45 @@ impl XrFrameStream {
#[derive(Resource, Deref, DerefMut)] #[derive(Resource, Deref, DerefMut)]
pub struct XrFrameWaiter(pub openxr::FrameWaiter); pub struct XrFrameWaiter(pub openxr::FrameWaiter);
#[derive(Resource)] #[derive(Resource, Clone)]
pub struct XrSwapchain(pub(crate) GraphicsWrap<Self>); pub struct XrSwapchain(pub(crate) GraphicsWrap<Self>);
impl GraphicsType for XrSwapchain { impl GraphicsType for XrSwapchain {
type Inner<G: GraphicsExt> = openxr::Swapchain<G>; type Inner<G: GraphicsExt> = Arc<Mutex<openxr::Swapchain<G>>>;
} }
impl XrSwapchain { impl XrSwapchain {
pub fn acquire_image(&mut self) -> Result<u32> { pub fn acquire_image(&self) -> Result<u32> {
graphics_match!( graphics_match!(
&mut self.0; &self.0;
swap => Ok(swap.acquire_image()?) swap => Ok(swap.lock().unwrap().acquire_image()?)
) )
} }
pub fn wait_image(&mut self, timeout: openxr::Duration) -> Result<()> { pub fn wait_image(&self, timeout: openxr::Duration) -> Result<()> {
graphics_match!( graphics_match!(
&mut self.0; &self.0;
swap => Ok(swap.wait_image(timeout)?) swap => Ok(swap.lock().unwrap().wait_image(timeout)?)
) )
} }
pub fn release_image(&mut self) -> Result<()> { pub fn release_image(&self) -> Result<()> {
graphics_match!( graphics_match!(
&mut self.0; &self.0;
swap => Ok(swap.release_image()?) swap => Ok(swap.lock().unwrap().release_image()?)
) )
} }
pub fn enumerate_images( pub fn enumerate_images(
&mut self, &self,
device: &wgpu::Device, device: &wgpu::Device,
format: wgpu::TextureFormat, format: wgpu::TextureFormat,
resolution: UVec2, resolution: UVec2,
) -> Result<XrSwapchainImages> { ) -> Result<XrSwapchainImages> {
graphics_match!( graphics_match!(
&mut self.0; &self.0;
swap => { swap => {
let swap = swap.lock().unwrap();
let mut images = vec![]; let mut images = vec![];
for image in swap.enumerate_images()? { for image in swap.enumerate_images()? {
unsafe { unsafe {