Refactor xr::handle

This commit is contained in:
Patrick Nicolas
2024-06-16 12:07:38 +02:00
parent a7e8b281d3
commit 03c12a9aaf
18 changed files with 98 additions and 151 deletions
+5 -4
View File
@@ -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<xr::passthrough_fb>();
passthrough.emplace<std::monostate>();
return;
}
if (passthrough_supported != xr::system::passthrough_type::no_passthrough)
@@ -761,8 +764,6 @@ void scenes::lobby::setup_passthrough()
{
passthrough.emplace<xr::passthrough_htc>(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<xr::passthrough_fb>(); // default constructor does no passthrough
passthrough.emplace<std::monostate>();
wifi_lock::want_multicast(false);
}
+1
View File
@@ -28,6 +28,7 @@
#include <vulkan/vulkan_raii.hpp>
#include "xr/passthrough.h"
#include "xr/system.h"
#include <optional>
#include <vector>
+1 -1
View File
@@ -76,7 +76,7 @@ private:
std::atomic<bool> exiting = false;
std::thread network_thread;
std::mutex local_floor_mutex;
xr::space local_floor = nullptr;
xr::space local_floor;
std::atomic<std::chrono::nanoseconds::rep> tracking_prediction_offset;
std::optional<std::thread> tracking_thread;
+15 -8
View File
@@ -20,10 +20,11 @@
#pragma once
#include <utility>
#include <openxr/openxr.h>
namespace utils
{
template <typename T>
template <typename T, XrResult deleter(T)>
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 <typename T>
bool operator==(const handle<T> & lhs, const T & rhs)
template <typename T, XrResult deleter(T)>
bool operator==(const handle<T, deleter> & lhs, const T & rhs)
{
return (T)lhs == rhs;
}
template <typename T>
bool operator==(const T & lhs, const handle<T> & rhs)
template <typename T, XrResult deleter(T)>
bool operator==(const T & lhs, const handle<T, deleter> & rhs)
{
return lhs == (T)rhs;
}
+2 -7
View File
@@ -18,7 +18,8 @@
*/
#include "actionset.h"
#include "xr.h"
#include "xr/check.h"
#include "xr/instance.h"
#include <cstring>
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);
}
+1 -3
View File
@@ -29,7 +29,7 @@ namespace xr
{
class instance;
class actionset : public utils::handle<XrActionSet>
class actionset : public utils::handle<XrActionSet, xrDestroyActionSet>
{
instance * inst;
@@ -45,7 +45,5 @@ public:
{
return create_action(type, name, name, subactionpaths);
}
~actionset();
};
} // namespace xr
+14
View File
@@ -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<PFN_xrLocateHandJointsEXT>("xrLocateHandJointsEXT");
xrDestroyHandTrackerEXT = inst.get_proc<PFN_xrDestroyHandTrackerEXT>("xrDestroyHandTrackerEXT");
}
std::optional<std::array<xr::hand_tracker::joint, XR_HAND_JOINT_COUNT_EXT>> xr::hand_tracker::locate(XrSpace space, XrTime time)
{
if (!id || !xrLocateHandJointsEXT)
+4 -7
View File
@@ -29,19 +29,16 @@
namespace xr
{
class hand_tracker : public utils::handle<XrHandTrackerEXT>
XrResult destroy_hand_tracker(XrHandTrackerEXT);
class hand_tracker : public utils::handle<XrHandTrackerEXT, destroy_hand_tracker>
{
PFN_xrLocateHandJointsEXT xrLocateHandJointsEXT{};
PFN_xrDestroyHandTrackerEXT xrDestroyHandTrackerEXT{};
public:
hand_tracker() = default;
hand_tracker(instance & inst, XrHandTrackerEXT h)
{
id = h;
xrLocateHandJointsEXT = inst.get_proc<PFN_xrLocateHandJointsEXT>("xrLocateHandJointsEXT");
xrDestroyHandTrackerEXT = inst.get_proc<PFN_xrDestroyHandTrackerEXT>("xrDestroyHandTrackerEXT");
}
hand_tracker(instance & inst, XrHandTrackerEXT h);
hand_tracker(hand_tracker &&) = default;
hand_tracker & operator=(hand_tracker &&) = default;
-6
View File
@@ -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 =
+1 -2
View File
@@ -45,7 +45,7 @@ union event
XrEventDataDisplayRefreshRateChangedFB refresh_rate_changed;
XrEventDataPassthroughStateChangedFB passthrough_state_changed;
};
class instance : public utils::handle<XrInstance>
class instance : public utils::handle<XrInstance, xrDestroyInstance>
{
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
{
+39 -37
View File
@@ -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<PFN_xrDestroyPassthroughLayerFB>("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<PFN_xrCreatePassthroughFB>("xrCreatePassthroughFB");
xrDestroyPassthroughFB = inst.get_proc<PFN_xrDestroyPassthroughFB>("xrDestroyPassthroughFB");
xrPassthroughStartFB = inst.get_proc<PFN_xrPassthroughStartFB>("xrPassthroughStartFB");
xrPassthroughPauseFB = inst.get_proc<PFN_xrPassthroughPauseFB>("xrPassthroughPauseFB");
xrPassthroughLayerPauseFB = inst.get_proc<PFN_xrPassthroughLayerPauseFB>("xrPassthroughLayerPauseFB");
xrPassthroughLayerResumeFB = inst.get_proc<PFN_xrPassthroughLayerResumeFB>("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<XrPassthroughFB, destroy_passthrough_fb>(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<PFN_xrDestroyPassthroughFB>("xrDestroyPassthroughFB");
xrPassthroughStartFB = inst.get_proc<PFN_xrPassthroughStartFB>("xrPassthroughStartFB");
xrPassthroughPauseFB = inst.get_proc<PFN_xrPassthroughPauseFB>("xrPassthroughPauseFB");
xrPassthroughLayerPauseFB = inst.get_proc<PFN_xrPassthroughLayerPauseFB>("xrPassthroughLayerPauseFB");
xrPassthroughLayerResumeFB = inst.get_proc<PFN_xrPassthroughLayerResumeFB>("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);
}
+11 -34
View File
@@ -20,45 +20,36 @@
#pragma once
#include "openxr/openxr.h"
#include "xr.h"
#include <optional>
#include "utils/handle.h"
#include <variant>
namespace xr
{
class instance;
class session;
class passthrough_layer_fb : public utils::handle<XrPassthroughLayerFB>
{
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<XrPassthroughLayerFB, destroy_passthrough_layer_fb>
{
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<XrPassthroughFB>
class passthrough_fb : public utils::handle<XrPassthroughFB, destroy_passthrough_fb>
{
PFN_xrDestroyPassthroughFB xrDestroyPassthroughFB{};
PFN_xrPassthroughStartFB xrPassthroughStartFB{};
PFN_xrPassthroughPauseFB xrPassthroughPauseFB{};
PFN_xrPassthroughLayerPauseFB xrPassthroughLayerPauseFB{};
PFN_xrPassthroughLayerResumeFB xrPassthroughLayerResumeFB{};
std::optional<passthrough_layer_fb> 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<XrPassthroughHTC>
class passthrough_htc : public utils::handle<XrPassthroughHTC, destroy_passthrough_htc>
{
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<passthrough_fb, passthrough_htc, passthrough_alpha_blend>;
using passthrough = std::variant<std::monostate, passthrough_fb, passthrough_htc, passthrough_alpha_blend>;
} // namespace xr
+1 -7
View File
@@ -21,7 +21,7 @@
#include "details/enumerate.h"
#include "utils/ranges.h"
#include "xr.h"
#include "xr/system.h"
#include <vulkan/vulkan.h>
#include <openxr/openxr_platform.h>
@@ -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);
}
+1 -5
View File
@@ -34,17 +34,13 @@ namespace xr
class instance;
class system;
class session : public utils::handle<XrSession>
class session : public utils::handle<XrSession, xrDestroySession>
{
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<XrReferenceSpaceType> get_reference_spaces() const;
space create_reference_space(XrReferenceSpaceType ref, const XrPosef & pose = {{0, 0, 0, 1}, {0, 0, 0}});
+1 -17
View File
@@ -24,21 +24,5 @@
namespace xr
{
class space : public utils::handle<XrSpace>
{
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<XrSpace, xrDestroySpace>;
} // namespace xr
-6
View File
@@ -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;
+1 -6
View File
@@ -33,7 +33,7 @@ namespace xr
{
class session;
class swapchain : public utils::handle<XrSwapchain>
class swapchain : public utils::handle<XrSwapchain, xrDestroySwapchain>
{
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
{
-1
View File
@@ -23,7 +23,6 @@
#include <android_native_app_glue.h>
#endif
#include "xr.h"
#include <vector>
#include <vulkan/vulkan_raii.hpp>
#include <openxr/openxr.h>