From 94e6014861a10aaced177b5d83ac6c34bc15ee3b Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Sun, 24 Mar 2024 15:20:38 +0100 Subject: [PATCH] Release passthrough resources when lobby is not focused --- client/scenes/lobby.cpp | 31 ++++++++++++++++--------------- client/scenes/stream.cpp | 2 +- client/utils/handle.h | 5 ----- client/xr/passthrough.cpp | 28 ++++++++++++++++++++-------- client/xr/passthrough.h | 19 +++++++++++++++---- 5 files changed, 52 insertions(+), 33 deletions(-) diff --git a/client/scenes/lobby.cpp b/client/scenes/lobby.cpp index 7c164e77..e3db34e9 100644 --- a/client/scenes/lobby.cpp +++ b/client/scenes/lobby.cpp @@ -267,20 +267,6 @@ scenes::lobby::lobby() throw std::runtime_error("No supported swapchain format"); spdlog::info("Using format {}", vk::to_string(swapchain_format)); - - if (passthrough_supported != xr::system::passthrough_type::no_passthrough) - { - if (utils::contains(application::get_xr_extensions(), XR_FB_PASSTHROUGH_EXTENSION_NAME)) - { - passthrough = xr::passthrough_fb(instance, session); - } - else if (utils::contains(application::get_xr_extensions(), XR_HTC_PASSTHROUGH_EXTENSION_NAME)) - { - passthrough = xr::passthrough_htc(instance, session); - } - - std::visit([](auto& p) {p.start();}, passthrough); - } } void scenes::lobby::save_config() @@ -723,6 +709,20 @@ void scenes::lobby::on_focused() { about_picture = imgui_ctx->load_texture("wivrn.png"); } + + if (passthrough_supported != xr::system::passthrough_type::no_passthrough) + { + if (utils::contains(application::get_xr_extensions(), XR_FB_PASSTHROUGH_EXTENSION_NAME)) + { + passthrough.emplace(instance, session); + } + else if (utils::contains(application::get_xr_extensions(), XR_HTC_PASSTHROUGH_EXTENSION_NAME)) + { + passthrough.emplace(instance, session); + } + + std::visit([](auto& p) {p.start();}, passthrough); + } } void scenes::lobby::on_unfocused() @@ -738,7 +738,8 @@ void scenes::lobby::on_unfocused() renderer.reset(); swapchains_lobby.clear(); swapchains_controllers.clear(); - swapchain_imgui.reset(); + swapchain_imgui = xr::swapchain(); + passthrough.emplace(); // default constructor does no passthrough } void scenes::lobby::on_session_state_changed(XrSessionState state) diff --git a/client/scenes/stream.cpp b/client/scenes/stream.cpp index fbc9dbcd..2f5176e0 100644 --- a/client/scenes/stream.cpp +++ b/client/scenes/stream.cpp @@ -197,7 +197,7 @@ void scenes::stream::on_focused() void scenes::stream::on_unfocused() { imgui_ctx.reset(); - swapchain_imgui.reset(); + swapchain_imgui = xr::swapchain(); } scenes::stream::~stream() diff --git a/client/utils/handle.h b/client/utils/handle.h index 3f3cc1cc..685c3bac 100644 --- a/client/utils/handle.h +++ b/client/utils/handle.h @@ -66,11 +66,6 @@ public: id = null_value; return r; } - - void reset() - { - *this = handle{}; - } }; template diff --git a/client/xr/passthrough.cpp b/client/xr/passthrough.cpp index 17613134..ea854394 100644 --- a/client/xr/passthrough.cpp +++ b/client/xr/passthrough.cpp @@ -21,10 +21,24 @@ #include "session.h" #include "openxr/openxr.h" +xr::passthrough_layer_fb::passthrough_layer_fb(instance& inst, session& s, const XrPassthroughLayerCreateInfoFB& info) +{ + xrDestroyPassthroughLayerFB = inst.get_proc("xrDestroyPassthroughLayerFB"); + + PFN_xrCreatePassthroughLayerFB xrCreatePassthroughLayerFB = inst.get_proc("xrCreatePassthroughLayerFB"); + CHECK_XR(xrCreatePassthroughLayerFB(s, &info, &id)); +} + +xr::passthrough_layer_fb::~passthrough_layer_fb() +{ + if (id != XR_NULL_HANDLE and xrDestroyPassthroughLayerFB) + xrDestroyPassthroughLayerFB(id); +} + + xr::passthrough_fb::passthrough_fb(instance & inst, session & s) { PFN_xrCreatePassthroughFB xrCreatePassthroughFB = inst.get_proc("xrCreatePassthroughFB"); - PFN_xrCreatePassthroughLayerFB xrCreatePassthroughLayerFB = inst.get_proc("xrCreatePassthroughLayerFB"); xrDestroyPassthroughFB = inst.get_proc("xrDestroyPassthroughFB"); xrPassthroughStartFB = inst.get_proc("xrPassthroughStartFB"); @@ -46,21 +60,19 @@ xr::passthrough_fb::passthrough_fb(instance & inst, session & s) .purpose = XR_PASSTHROUGH_LAYER_PURPOSE_RECONSTRUCTION_FB }; - CHECK_XR(xrCreatePassthroughLayerFB(s, &layer_info, &passthrough_layer)); + passthrough_layer.emplace(inst, s, layer_info); composition_layer = XrCompositionLayerPassthroughFB{ .type = XR_TYPE_COMPOSITION_LAYER_PASSTHROUGH_FB, .flags = 0, .space = XR_NULL_HANDLE, - .layerHandle = passthrough_layer + .layerHandle = *passthrough_layer }; } xr::passthrough_fb::~passthrough_fb() { - if (passthrough_layer && xrDestroyPassthroughLayerFB) - xrDestroyPassthroughLayerFB(passthrough_layer); - + passthrough_layer.reset(); if (id != XR_NULL_HANDLE && xrDestroyPassthroughFB) xrDestroyPassthroughFB(id); } @@ -68,12 +80,12 @@ xr::passthrough_fb::~passthrough_fb() void xr::passthrough_fb::start() { CHECK_XR(xrPassthroughStartFB(id)); - CHECK_XR(xrPassthroughLayerResumeFB(passthrough_layer)); + CHECK_XR(xrPassthroughLayerResumeFB(*passthrough_layer)); } void xr::passthrough_fb::pause() { - CHECK_XR(xrPassthroughLayerPauseFB(passthrough_layer)); + CHECK_XR(xrPassthroughLayerPauseFB(*passthrough_layer)); CHECK_XR(xrPassthroughPauseFB(id)); } diff --git a/client/xr/passthrough.h b/client/xr/passthrough.h index e869c6d5..5a435ef8 100644 --- a/client/xr/passthrough.h +++ b/client/xr/passthrough.h @@ -21,30 +21,41 @@ #include "openxr/openxr.h" #include "xr.h" +#include #include namespace xr { class session; +class passthrough_layer_fb : public utils::handle +{ + PFN_xrDestroyPassthroughLayerFB xrDestroyPassthroughLayerFB{}; +public: + passthrough_layer_fb() = default; + passthrough_layer_fb(instance&, session&, const XrPassthroughLayerCreateInfoFB&); + passthrough_layer_fb(passthrough_layer_fb&&) = delete; + passthrough_layer_fb& operator=(passthrough_layer_fb&&) = delete; + ~passthrough_layer_fb(); +}; + class passthrough_fb : public utils::handle { PFN_xrDestroyPassthroughFB xrDestroyPassthroughFB{}; - PFN_xrDestroyPassthroughLayerFB xrDestroyPassthroughLayerFB{}; PFN_xrPassthroughStartFB xrPassthroughStartFB{}; PFN_xrPassthroughPauseFB xrPassthroughPauseFB{}; PFN_xrPassthroughLayerPauseFB xrPassthroughLayerPauseFB{}; PFN_xrPassthroughLayerResumeFB xrPassthroughLayerResumeFB{}; - XrPassthroughLayerFB passthrough_layer{}; + std::optional passthrough_layer; XrCompositionLayerPassthroughFB composition_layer; public: passthrough_fb() = default; passthrough_fb(instance &, session &); - passthrough_fb(passthrough_fb &&) = default; + passthrough_fb(passthrough_fb &&) = delete; passthrough_fb(const passthrough_fb &) = delete; - passthrough_fb & operator=(passthrough_fb &&) = default; + passthrough_fb & operator=(passthrough_fb &&) = delete; passthrough_fb & operator=(const passthrough_fb &) = delete; ~passthrough_fb();