diff --git a/client/scenes/lobby.cpp b/client/scenes/lobby.cpp index 418567ba..0487c1d1 100644 --- a/client/scenes/lobby.cpp +++ b/client/scenes/lobby.cpp @@ -628,6 +628,9 @@ void scenes::lobby::render(const XrFrameState & frame_state) { std::visit( overloaded{ + [&](std::monostate &) { + assert(false); + }, [&](xr::passthrough_alpha_blend & p) { blend_mode = XR_ENVIRONMENT_BLEND_MODE_ALPHA_BLEND; }, @@ -744,7 +747,7 @@ void scenes::lobby::setup_passthrough() auto & passthrough_enabled = application::get_config().passthrough_enabled; if (not passthrough_enabled) { - passthrough.emplace(); + passthrough.emplace(); return; } if (passthrough_supported != xr::system::passthrough_type::no_passthrough) @@ -761,8 +764,6 @@ void scenes::lobby::setup_passthrough() { passthrough.emplace(instance, session); } - - std::visit([](auto & p) { p.start(); }, passthrough); } } @@ -785,7 +786,7 @@ void scenes::lobby::on_unfocused() swapchains_lobby.clear(); swapchains_controllers.clear(); swapchain_imgui = xr::swapchain(); - passthrough.emplace(); // default constructor does no passthrough + passthrough.emplace(); wifi_lock::want_multicast(false); } diff --git a/client/scenes/lobby.h b/client/scenes/lobby.h index a73e8d10..1902ab62 100644 --- a/client/scenes/lobby.h +++ b/client/scenes/lobby.h @@ -28,6 +28,7 @@ #include #include "xr/passthrough.h" +#include "xr/system.h" #include #include diff --git a/client/scenes/stream.h b/client/scenes/stream.h index 58a68a79..9cedaf1d 100644 --- a/client/scenes/stream.h +++ b/client/scenes/stream.h @@ -76,7 +76,7 @@ private: std::atomic exiting = false; std::thread network_thread; std::mutex local_floor_mutex; - xr::space local_floor = nullptr; + xr::space local_floor; std::atomic tracking_prediction_offset; std::optional tracking_thread; diff --git a/client/utils/handle.h b/client/utils/handle.h index 685c3bac..80bba6ea 100644 --- a/client/utils/handle.h +++ b/client/utils/handle.h @@ -20,10 +20,11 @@ #pragma once #include +#include namespace utils { -template +template class handle { protected: @@ -31,10 +32,16 @@ protected: T id = null_value; - handle() {} - ~handle() {} - public: + handle() {} + handle(T id) : + id(id) {} + ~handle() + { + if (id != null_value) + deleter(id); + } + handle(handle && other) noexcept : id(other.id) { @@ -68,14 +75,14 @@ public: } }; -template -bool operator==(const handle & lhs, const T & rhs) +template +bool operator==(const handle & lhs, const T & rhs) { return (T)lhs == rhs; } -template -bool operator==(const T & lhs, const handle & rhs) +template +bool operator==(const T & lhs, const handle & rhs) { return lhs == (T)rhs; } diff --git a/client/xr/actionset.cpp b/client/xr/actionset.cpp index 9bfde75f..6bbde30f 100644 --- a/client/xr/actionset.cpp +++ b/client/xr/actionset.cpp @@ -18,7 +18,8 @@ */ #include "actionset.h" -#include "xr.h" +#include "xr/check.h" +#include "xr/instance.h" #include xr::actionset::actionset(xr::instance & inst, const std::string & name, const std::string & localized_name, uint32_t priority) @@ -55,9 +56,3 @@ XrAction xr::actionset::create_action(XrActionType type, const std::string & nam return action; } - -xr::actionset::~actionset() -{ - if (id != XR_NULL_HANDLE) - xrDestroyActionSet(id); -} diff --git a/client/xr/actionset.h b/client/xr/actionset.h index fcdfe185..3db9f596 100644 --- a/client/xr/actionset.h +++ b/client/xr/actionset.h @@ -29,7 +29,7 @@ namespace xr { class instance; -class actionset : public utils::handle +class actionset : public utils::handle { instance * inst; @@ -45,7 +45,5 @@ public: { return create_action(type, name, name, subactionpaths); } - - ~actionset(); }; } // namespace xr diff --git a/client/xr/hand_tracker.cpp b/client/xr/hand_tracker.cpp index 2e4e658b..f002f72c 100644 --- a/client/xr/hand_tracker.cpp +++ b/client/xr/hand_tracker.cpp @@ -19,6 +19,20 @@ #include "hand_tracker.h" +static PFN_xrDestroyHandTrackerEXT xrDestroyHandTrackerEXT{}; + +XrResult xr::destroy_hand_tracker(XrHandTrackerEXT id) +{ + return xrDestroyHandTrackerEXT(id); +} + +xr::hand_tracker::hand_tracker(instance & inst, XrHandTrackerEXT h) +{ + id = h; + xrLocateHandJointsEXT = inst.get_proc("xrLocateHandJointsEXT"); + xrDestroyHandTrackerEXT = inst.get_proc("xrDestroyHandTrackerEXT"); +} + std::optional> xr::hand_tracker::locate(XrSpace space, XrTime time) { if (!id || !xrLocateHandJointsEXT) diff --git a/client/xr/hand_tracker.h b/client/xr/hand_tracker.h index 9d74d92b..baaa7d86 100644 --- a/client/xr/hand_tracker.h +++ b/client/xr/hand_tracker.h @@ -29,19 +29,16 @@ namespace xr { -class hand_tracker : public utils::handle +XrResult destroy_hand_tracker(XrHandTrackerEXT); + +class hand_tracker : public utils::handle { PFN_xrLocateHandJointsEXT xrLocateHandJointsEXT{}; PFN_xrDestroyHandTrackerEXT xrDestroyHandTrackerEXT{}; public: hand_tracker() = default; - hand_tracker(instance & inst, XrHandTrackerEXT h) - { - id = h; - xrLocateHandJointsEXT = inst.get_proc("xrLocateHandJointsEXT"); - xrDestroyHandTrackerEXT = inst.get_proc("xrDestroyHandTrackerEXT"); - } + hand_tracker(instance & inst, XrHandTrackerEXT h); hand_tracker(hand_tracker &&) = default; hand_tracker & operator=(hand_tracker &&) = default; diff --git a/client/xr/instance.cpp b/client/xr/instance.cpp index d8a91a64..ab934b1f 100644 --- a/client/xr/instance.cpp +++ b/client/xr/instance.cpp @@ -214,12 +214,6 @@ void xr::instance::suggest_bindings(const std::string & interaction_profile, CHECK_XR(xrSuggestInteractionProfileBindings(id, &suggested_binding)); } -xr::instance::~instance() -{ - if (id != XR_NULL_HANDLE) - xrDestroyInstance(id); -} - XrTime xr::instance::now() { static PFN_xrConvertTimespecTimeToTimeKHR xrConvertTimespecTimeToTimeKHR = diff --git a/client/xr/instance.h b/client/xr/instance.h index 8434f7c0..94691c14 100644 --- a/client/xr/instance.h +++ b/client/xr/instance.h @@ -45,7 +45,7 @@ union event XrEventDataDisplayRefreshRateChangedFB refresh_rate_changed; XrEventDataPassthroughStateChangedFB passthrough_state_changed; }; -class instance : public utils::handle +class instance : public utils::handle { std::string runtime_version; std::string runtime_name; @@ -61,7 +61,6 @@ public: instance() = default; instance(instance &&) = default; instance & operator=(instance &&) = default; - ~instance(); const std::string & get_runtime_version() const { diff --git a/client/xr/passthrough.cpp b/client/xr/passthrough.cpp index b785bd28..0a481046 100644 --- a/client/xr/passthrough.cpp +++ b/client/xr/passthrough.cpp @@ -21,6 +21,23 @@ #include "openxr/openxr.h" #include "session.h" +static PFN_xrDestroyPassthroughLayerFB xrDestroyPassthroughLayerFB{}; +static PFN_xrDestroyPassthroughFB xrDestroyPassthroughFB{}; +static PFN_xrDestroyPassthroughHTC xrDestroyPassthroughHTC{}; + +XrResult xr::destroy_passthrough_layer_fb(XrPassthroughLayerFB id) +{ + return xrDestroyPassthroughLayerFB(id); +} +XrResult xr::destroy_passthrough_fb(XrPassthroughFB id) +{ + return xrDestroyPassthroughFB(id); +} +XrResult xr::destroy_passthrough_htc(XrPassthroughHTC id) +{ + return xrDestroyPassthroughHTC(id); +} + xr::passthrough_layer_fb::passthrough_layer_fb(instance & inst, session & s, const XrPassthroughLayerCreateInfoFB & info) { xrDestroyPassthroughLayerFB = inst.get_proc("xrDestroyPassthroughLayerFB"); @@ -29,62 +46,53 @@ xr::passthrough_layer_fb::passthrough_layer_fb(instance & inst, session & s, con 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) +static XrPassthroughFB create_passthrough_fb(xr::instance & inst, xr::session & s) { PFN_xrCreatePassthroughFB xrCreatePassthroughFB = inst.get_proc("xrCreatePassthroughFB"); - xrDestroyPassthroughFB = inst.get_proc("xrDestroyPassthroughFB"); - xrPassthroughStartFB = inst.get_proc("xrPassthroughStartFB"); - xrPassthroughPauseFB = inst.get_proc("xrPassthroughPauseFB"); - xrPassthroughLayerPauseFB = inst.get_proc("xrPassthroughLayerPauseFB"); - xrPassthroughLayerResumeFB = inst.get_proc("xrPassthroughLayerResumeFB"); - + XrPassthroughFB id; XrPassthroughCreateInfoFB info{ .type = XR_TYPE_PASSTHROUGH_CREATE_INFO_FB, .flags = 0, }; CHECK_XR(xrCreatePassthroughFB(s, &info, &id)); + return id; +} - XrPassthroughLayerCreateInfoFB layer_info{ - .type = XR_TYPE_PASSTHROUGH_LAYER_CREATE_INFO_FB, - .passthrough = id, - .flags = 0, - .purpose = XR_PASSTHROUGH_LAYER_PURPOSE_RECONSTRUCTION_FB, - }; - - passthrough_layer.emplace(inst, s, layer_info); +xr::passthrough_fb::passthrough_fb(instance & inst, session & s) : + utils::handle(create_passthrough_fb(inst, s)), + passthrough_layer(inst, s, XrPassthroughLayerCreateInfoFB{ + .type = XR_TYPE_PASSTHROUGH_LAYER_CREATE_INFO_FB, + .passthrough = id, + .flags = 0, + .purpose = XR_PASSTHROUGH_LAYER_PURPOSE_RECONSTRUCTION_FB, + }) +{ + xrDestroyPassthroughFB = inst.get_proc("xrDestroyPassthroughFB"); + xrPassthroughStartFB = inst.get_proc("xrPassthroughStartFB"); + xrPassthroughPauseFB = inst.get_proc("xrPassthroughPauseFB"); + xrPassthroughLayerPauseFB = inst.get_proc("xrPassthroughLayerPauseFB"); + xrPassthroughLayerResumeFB = inst.get_proc("xrPassthroughLayerResumeFB"); 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() -{ - passthrough_layer.reset(); - if (id != XR_NULL_HANDLE && xrDestroyPassthroughFB) - xrDestroyPassthroughFB(id); + start(); } 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)); } @@ -111,9 +119,3 @@ xr::passthrough_htc::passthrough_htc(instance & inst, session & s) }, }; } - -xr::passthrough_htc::~passthrough_htc() -{ - if (id != XR_NULL_HANDLE && xrDestroyPassthroughHTC) - xrDestroyPassthroughHTC(id); -} diff --git a/client/xr/passthrough.h b/client/xr/passthrough.h index f224917c..3b5d2259 100644 --- a/client/xr/passthrough.h +++ b/client/xr/passthrough.h @@ -20,45 +20,36 @@ #pragma once #include "openxr/openxr.h" -#include "xr.h" -#include +#include "utils/handle.h" #include namespace xr { +class instance; class session; -class passthrough_layer_fb : public utils::handle -{ - PFN_xrDestroyPassthroughLayerFB xrDestroyPassthroughLayerFB{}; +XrResult destroy_passthrough_layer_fb(XrPassthroughLayerFB); +XrResult destroy_passthrough_fb(XrPassthroughFB); +XrResult destroy_passthrough_htc(XrPassthroughHTC); +class passthrough_layer_fb : public utils::handle +{ 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 +class passthrough_fb : public utils::handle { - PFN_xrDestroyPassthroughFB xrDestroyPassthroughFB{}; PFN_xrPassthroughStartFB xrPassthroughStartFB{}; PFN_xrPassthroughPauseFB xrPassthroughPauseFB{}; PFN_xrPassthroughLayerPauseFB xrPassthroughLayerPauseFB{}; PFN_xrPassthroughLayerResumeFB xrPassthroughLayerResumeFB{}; - std::optional passthrough_layer; + passthrough_layer_fb passthrough_layer; XrCompositionLayerPassthroughFB composition_layer; public: - passthrough_fb() = default; passthrough_fb(instance &, session &); - passthrough_fb(passthrough_fb &&) = delete; - passthrough_fb(const passthrough_fb &) = delete; - passthrough_fb & operator=(passthrough_fb &&) = delete; - passthrough_fb & operator=(const passthrough_fb &) = delete; - ~passthrough_fb(); void start(); void pause(); @@ -68,23 +59,12 @@ public: } }; -class passthrough_htc : public utils::handle +class passthrough_htc : public utils::handle { - PFN_xrDestroyPassthroughHTC xrDestroyPassthroughHTC{}; - XrCompositionLayerPassthroughHTC composition_layer; public: - passthrough_htc() = default; passthrough_htc(instance &, session &); - passthrough_htc(passthrough_htc &&) = default; - passthrough_htc(const passthrough_htc &) = delete; - passthrough_htc & operator=(passthrough_htc &&) = default; - passthrough_htc & operator=(const passthrough_htc &) = delete; - ~passthrough_htc(); - - void start() {} - void pause() {} XrCompositionLayerBaseHeader * layer() { return (XrCompositionLayerBaseHeader *)&composition_layer; @@ -93,11 +73,8 @@ public: class passthrough_alpha_blend { -public: - void start() {} - void pause() {} }; -using passthrough = std::variant; +using passthrough = std::variant; } // namespace xr diff --git a/client/xr/session.cpp b/client/xr/session.cpp index 5da12a65..2f1c64d0 100644 --- a/client/xr/session.cpp +++ b/client/xr/session.cpp @@ -21,7 +21,7 @@ #include "details/enumerate.h" #include "utils/ranges.h" -#include "xr.h" +#include "xr/system.h" #include #include @@ -312,9 +312,3 @@ void xr::session::sync_actions(XrActionSet action_set, const std::string & subac { sync_actions(action_set, inst->string_to_path(subaction_path)); } - -xr::session::~session() -{ - if (id != XR_NULL_HANDLE) - xrDestroySession(id); -} diff --git a/client/xr/session.h b/client/xr/session.h index c7902833..af22da9f 100644 --- a/client/xr/session.h +++ b/client/xr/session.h @@ -34,17 +34,13 @@ namespace xr class instance; class system; -class session : public utils::handle +class session : public utils::handle { instance * inst = nullptr; public: session() = default; session(instance &, system &, vk::raii::Instance &, vk::raii::PhysicalDevice &, vk::raii::Device &, int queue_family_index); - session(session &&) = default; - session & operator=(session &&) = default; - - ~session(); std::vector get_reference_spaces() const; space create_reference_space(XrReferenceSpaceType ref, const XrPosef & pose = {{0, 0, 0, 1}, {0, 0, 0}}); diff --git a/client/xr/space.h b/client/xr/space.h index f22d136f..41bfe4f9 100644 --- a/client/xr/space.h +++ b/client/xr/space.h @@ -24,21 +24,5 @@ namespace xr { -class space : public utils::handle -{ -public: - space() = default; - space(XrSpace s) - { - id = s; - } - space(space &&) = default; - space & operator=(space &&) = default; - - ~space() - { - if (id != XR_NULL_HANDLE) - xrDestroySpace(id); - } -}; +using space = utils::handle; } // namespace xr diff --git a/client/xr/swapchain.cpp b/client/xr/swapchain.cpp index d36f0067..dc7b0653 100644 --- a/client/xr/swapchain.cpp +++ b/client/xr/swapchain.cpp @@ -70,12 +70,6 @@ xr::swapchain::swapchain(xr::session & s, vk::raii::Device & device, vk::Format } } -xr::swapchain::~swapchain() -{ - if (id != XR_NULL_HANDLE) - xrDestroySwapchain(id); -} - int xr::swapchain::acquire() { uint32_t index; diff --git a/client/xr/swapchain.h b/client/xr/swapchain.h index 3163d9c1..0e57b8a3 100644 --- a/client/xr/swapchain.h +++ b/client/xr/swapchain.h @@ -33,7 +33,7 @@ namespace xr { class session; -class swapchain : public utils::handle +class swapchain : public utils::handle { public: struct image @@ -53,11 +53,6 @@ private: public: swapchain() = default; swapchain(session &, vk::raii::Device & device, vk::Format format, int32_t width, int32_t height, int sample_count = 1); - swapchain(swapchain &&) = default; - swapchain(const swapchain &) = delete; - swapchain & operator=(swapchain &&) = default; - swapchain & operator=(const swapchain &) = delete; - ~swapchain(); int32_t width() const { diff --git a/client/xr/system.h b/client/xr/system.h index 5d4974bb..9b0bc524 100644 --- a/client/xr/system.h +++ b/client/xr/system.h @@ -23,7 +23,6 @@ #include #endif -#include "xr.h" #include #include #include