XR is kinda working now
This commit is contained in:
@@ -9,7 +9,7 @@ use bevy::prelude::*;
|
||||
use bevy::render::renderer::{RenderAdapter, RenderAdapterInfo, RenderDevice, RenderQueue};
|
||||
use bevy::window::RawHandleWrapper;
|
||||
use openxr as xr;
|
||||
use wgpu::Instance;
|
||||
use wgpu::{Instance, Texture};
|
||||
|
||||
use crate::input::XrInput;
|
||||
use crate::resources::{
|
||||
@@ -351,7 +351,6 @@ pub fn initialize_xr_graphics(window: Option<RawHandleWrapper>) -> anyhow::Resul
|
||||
texture
|
||||
})
|
||||
.collect();
|
||||
|
||||
|
||||
Ok((
|
||||
wgpu_device.into(),
|
||||
@@ -368,7 +367,9 @@ pub fn initialize_xr_graphics(window: Option<RawHandleWrapper>) -> anyhow::Resul
|
||||
stream: frame_stream,
|
||||
handle,
|
||||
resolution,
|
||||
format: swapchain_format,
|
||||
buffers,
|
||||
image_index: 0,
|
||||
})).into(),
|
||||
XrInput::new(xr_instance, session.into_any_graphics())?,
|
||||
Mutex::default().into(),
|
||||
|
||||
33
src/lib.rs
33
src/lib.rs
@@ -100,6 +100,24 @@ impl Plugin for OpenXrPlugin {
|
||||
.insert_resource(views.clone())
|
||||
.insert_resource(frame_state.clone());
|
||||
|
||||
let swapchain_mut = swapchain.lock().unwrap();
|
||||
let (left, right) = swapchain_mut.get_render_views();
|
||||
let format = swapchain_mut.format();
|
||||
let left = ManualTextureView {
|
||||
texture_view: left.into(),
|
||||
size: swapchain_mut.resolution(),
|
||||
format,
|
||||
};
|
||||
let right = ManualTextureView {
|
||||
texture_view: right.into(),
|
||||
size: swapchain_mut.resolution(),
|
||||
format,
|
||||
};
|
||||
let mut manual_texture_views = app.world.resource_mut::<ManualTextureViews>();
|
||||
manual_texture_views.insert(LEFT_XR_TEXTURE_HANDLE, left);
|
||||
manual_texture_views.insert(RIGHT_XR_TEXTURE_HANDLE, right);
|
||||
drop(manual_texture_views);
|
||||
drop(swapchain_mut);
|
||||
let render_app = app.sub_app_mut(RenderApp);
|
||||
|
||||
render_app.insert_resource(instance)
|
||||
@@ -111,6 +129,7 @@ impl Plugin for OpenXrPlugin {
|
||||
.insert_resource(input)
|
||||
.insert_resource(views)
|
||||
.insert_resource(frame_state);
|
||||
|
||||
render_app.add_systems(Render, (pre_frame.in_set(RenderSet::Prepare).before(post_frame), post_frame.in_set(RenderSet::Prepare), post_queue_submit.in_set(RenderSet::Cleanup)));
|
||||
}
|
||||
|
||||
@@ -178,9 +197,19 @@ pub fn pre_frame(
|
||||
let mut swapchain = swapchain.lock().unwrap();
|
||||
|
||||
swapchain.begin().unwrap();
|
||||
swapchain.update_render_views();
|
||||
let (left, right) = swapchain.get_render_views();
|
||||
let left = ManualTextureView::with_default_format(left.into(), swapchain.resolution());
|
||||
let right = ManualTextureView::with_default_format(right.into(), swapchain.resolution());
|
||||
let format = swapchain.format();
|
||||
let left = ManualTextureView {
|
||||
texture_view: left.into(),
|
||||
size: swapchain.resolution(),
|
||||
format,
|
||||
};
|
||||
let right = ManualTextureView {
|
||||
texture_view: right.into(),
|
||||
size: swapchain.resolution(),
|
||||
format,
|
||||
};
|
||||
manual_texture_views.insert(LEFT_XR_TEXTURE_HANDLE, left);
|
||||
manual_texture_views.insert(RIGHT_XR_TEXTURE_HANDLE, right);
|
||||
}
|
||||
|
||||
@@ -26,12 +26,24 @@ impl Swapchain {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn get_render_views(&mut self) -> (wgpu::TextureView, wgpu::TextureView) {
|
||||
pub(crate) fn update_render_views(&mut self) {
|
||||
match self {
|
||||
Swapchain::Vulkan(swap) => swap.update_render_views(),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn get_render_views(&self) -> (wgpu::TextureView, wgpu::TextureView) {
|
||||
match self {
|
||||
Swapchain::Vulkan(swap) => swap.get_render_views(),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn format(&self) -> wgpu::TextureFormat {
|
||||
match self {
|
||||
Swapchain::Vulkan(swap) => swap.format
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn resolution(&self) -> UVec2 {
|
||||
match self {
|
||||
Swapchain::Vulkan(swap) => swap.resolution,
|
||||
@@ -55,7 +67,9 @@ pub struct SwapchainInner<G: xr::Graphics> {
|
||||
pub(crate) stream: xr::FrameStream<G>,
|
||||
pub(crate) handle: xr::Swapchain<G>,
|
||||
pub(crate) resolution: UVec2,
|
||||
pub(crate) format: wgpu::TextureFormat,
|
||||
pub(crate) buffers: Vec<wgpu::Texture>,
|
||||
pub(crate) image_index: usize,
|
||||
}
|
||||
|
||||
impl<G: xr::Graphics> SwapchainInner<G> {
|
||||
@@ -63,11 +77,8 @@ impl<G: xr::Graphics> SwapchainInner<G> {
|
||||
self.stream.begin()
|
||||
}
|
||||
|
||||
fn get_render_views(&mut self) -> (wgpu::TextureView, wgpu::TextureView) {
|
||||
let image_index = self.handle.acquire_image().unwrap();
|
||||
self.handle.wait_image(xr::Duration::INFINITE).unwrap();
|
||||
|
||||
let texture = &self.buffers[image_index as usize];
|
||||
fn get_render_views(&self) -> (wgpu::TextureView, wgpu::TextureView) {
|
||||
let texture = &self.buffers[self.image_index];
|
||||
|
||||
(
|
||||
texture.create_view(&wgpu::TextureViewDescriptor {
|
||||
@@ -84,6 +95,13 @@ impl<G: xr::Graphics> SwapchainInner<G> {
|
||||
)
|
||||
}
|
||||
|
||||
fn update_render_views(&mut self) {
|
||||
let image_index = self.handle.acquire_image().unwrap();
|
||||
self.handle.wait_image(xr::Duration::INFINITE).unwrap();
|
||||
|
||||
self.image_index = image_index as _;
|
||||
}
|
||||
|
||||
fn post_queue_submit(
|
||||
&mut self,
|
||||
xr_frame_state: xr::FrameState,
|
||||
|
||||
Reference in New Issue
Block a user