improved performance

This commit is contained in:
awtterpip
2023-09-09 23:27:12 -05:00
parent a4065d279d
commit 086c964e82
5 changed files with 210 additions and 156 deletions

View File

@@ -8,11 +8,12 @@ use openxr as xr;
xr_resource_wrapper!(XrInstance, xr::Instance);
xr_resource_wrapper!(XrSession, xr::Session<xr::AnyGraphics>);
xr_resource_wrapper!(XrEnvironmentBlendMode, xr::EnvironmentBlendMode);
xr_resource_wrapper!(XrViewConfigurationViews, Vec<xr::ViewConfigurationView>);
xr_resource_wrapper!(XrResolution, UVec2);
xr_resource_wrapper!(XrFormat, wgpu::TextureFormat);
xr_arc_resource_wrapper!(XrSessionRunning, AtomicBool);
xr_arc_resource_wrapper!(XrFrameWaiter, Mutex<xr::FrameWaiter>);
xr_arc_resource_wrapper!(XrSwapchain, Mutex<Swapchain>);
xr_arc_resource_wrapper!(XrFrameState, Mutex<Option<xr::FrameState>>);
xr_arc_resource_wrapper!(XrSwapchain, Swapchain);
xr_arc_resource_wrapper!(XrFrameState, Mutex<xr::FrameState>);
xr_arc_resource_wrapper!(XrViews, Mutex<Vec<xr::View>>);
pub enum Swapchain {
@@ -20,67 +21,70 @@ pub enum Swapchain {
}
impl Swapchain {
pub(crate) fn begin(&mut self) -> xr::Result<()> {
pub(crate) fn begin(&self) -> xr::Result<()> {
match self {
Swapchain::Vulkan(swap) => swap.begin(),
}
}
pub(crate) fn update_render_views(&mut self) {
match self {
Swapchain::Vulkan(swap) => swap.update_render_views(),
Swapchain::Vulkan(swapchain) => swapchain.begin(),
}
}
pub(crate) fn get_render_views(&self) -> (wgpu::TextureView, wgpu::TextureView) {
match self {
Swapchain::Vulkan(swap) => swap.get_render_views(),
Swapchain::Vulkan(swapchain) => swapchain.get_render_views(),
}
}
pub(crate) fn format(&self) -> wgpu::TextureFormat {
pub(crate) fn acquire_image(&self) -> xr::Result<()> {
match self {
Swapchain::Vulkan(swap) => swap.format,
Swapchain::Vulkan(swapchain) => swapchain.acquire_image(),
}
}
pub(crate) fn resolution(&self) -> UVec2 {
pub(crate) fn wait_image(&self) -> xr::Result<()> {
match self {
Swapchain::Vulkan(swap) => swap.resolution,
Swapchain::Vulkan(swapchain) => swapchain.wait_image(),
}
}
pub(crate) fn post_queue_submit(
&mut self,
xr_frame_state: xr::FrameState,
pub(crate) fn release_image(&self) -> xr::Result<()> {
match self {
Swapchain::Vulkan(swapchain) => swapchain.release_image(),
}
}
pub(crate) fn end(
&self,
predicted_display_time: xr::Time,
views: &[openxr::View],
stage: &xr::Space,
resolution: UVec2,
environment_blend_mode: xr::EnvironmentBlendMode,
) -> xr::Result<()> {
match self {
Swapchain::Vulkan(swap) => {
swap.post_queue_submit(xr_frame_state, views, stage, environment_blend_mode)
}
Swapchain::Vulkan(swapchain) => swapchain.end(
predicted_display_time,
views,
stage,
resolution,
environment_blend_mode,
),
}
}
}
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) stream: Mutex<xr::FrameStream<G>>,
pub(crate) handle: Mutex<xr::Swapchain<G>>,
pub(crate) buffers: Vec<wgpu::Texture>,
pub(crate) image_index: usize,
pub(crate) image_index: Mutex<usize>,
}
impl<G: xr::Graphics> SwapchainInner<G> {
fn begin(&mut self) -> xr::Result<()> {
self.stream.begin()
fn begin(&self) -> xr::Result<()> {
self.stream.lock().unwrap().begin()
}
fn get_render_views(&self) -> (wgpu::TextureView, wgpu::TextureView) {
let texture = &self.buffers[self.image_index];
let texture = &self.buffers[*self.image_index.lock().unwrap()];
(
texture.create_view(&wgpu::TextureViewDescriptor {
@@ -97,30 +101,41 @@ 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 acquire_image(&self) -> xr::Result<()> {
let image_index = self.handle.lock().unwrap().acquire_image()?;
*self.image_index.lock().unwrap() = image_index as _;
Ok(())
}
fn post_queue_submit(
&mut self,
xr_frame_state: xr::FrameState,
fn wait_image(&self) -> xr::Result<()> {
self.handle
.lock()
.unwrap()
.wait_image(xr::Duration::INFINITE)
}
fn release_image(&self) -> xr::Result<()> {
self.handle.lock().unwrap().release_image()
}
fn end(
&self,
predicted_display_time: xr::Time,
views: &[openxr::View],
stage: &xr::Space,
resolution: UVec2,
environment_blend_mode: xr::EnvironmentBlendMode,
) -> xr::Result<()> {
self.handle.release_image().unwrap();
let rect = xr::Rect2Di {
offset: xr::Offset2Di { x: 0, y: 0 },
extent: xr::Extent2Di {
width: self.resolution.x as _,
height: self.resolution.y as _,
width: resolution.x as _,
height: resolution.y as _,
},
};
self.stream.end(
xr_frame_state.predicted_display_time,
let swapchain = self.handle.lock().unwrap();
self.stream.lock().unwrap().end(
predicted_display_time,
environment_blend_mode,
&[&xr::CompositionLayerProjection::new().space(stage).views(&[
xr::CompositionLayerProjectionView::new()
@@ -128,7 +143,7 @@ impl<G: xr::Graphics> SwapchainInner<G> {
.fov(views[0].fov)
.sub_image(
xr::SwapchainSubImage::new()
.swapchain(&self.handle)
.swapchain(&swapchain)
.image_array_index(0)
.image_rect(rect),
),
@@ -137,7 +152,7 @@ impl<G: xr::Graphics> SwapchainInner<G> {
.fov(views[1].fov)
.sub_image(
xr::SwapchainSubImage::new()
.swapchain(&self.handle)
.swapchain(&swapchain)
.image_array_index(1)
.image_rect(rect),
),