Files

401 lines
11 KiB
C++

/*
* WiVRn VR streaming
* Copyright (C) 2022 Guillaume Meunier <[email protected]>
* Copyright (C) 2022 Patrick Nicolas <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "system.h"
#include "body_tracker.h"
#include "details/enumerate.h"
#include "face_tracker.h"
#include "openxr/openxr.h"
#include "utils/contains.h"
#include "vk/check.h"
#include "xr/check.h"
#include "xr/instance.h"
#include <cassert>
#include <stdexcept>
#include <openxr/openxr_platform.h>
xr::system::system(xr::instance & inst, XrFormFactor formfactor)
{
if (!inst)
throw std::invalid_argument("instance");
this->inst = &inst;
XrSystemGetInfo system_info{};
system_info.type = XR_TYPE_SYSTEM_GET_INFO;
system_info.formFactor = formfactor;
CHECK_XR(xrGetSystem(inst, &system_info, &id));
assert(id != XR_NULL_SYSTEM_ID);
if (inst.has_extension(XR_EXT_HAND_TRACKING_EXTENSION_NAME))
hand_tracking_supported_ = hand_tracking_properties().supportsHandTracking;
if (hand_tracking_supported_ and inst.has_extension(XR_FB_HAND_TRACKING_MESH_EXTENSION_NAME))
hand_mesh_fb_supported_ = true;
body_tracker = xr::body_tracker_supported(inst, *this);
face_tracker = xr::face_tracker_supported(inst, *this);
}
XrGraphicsRequirementsVulkan2KHR xr::system::graphics_requirements() const
{
auto xrGetVulkanGraphicsRequirements2KHR =
inst->get_proc<PFN_xrGetVulkanGraphicsRequirements2KHR>("xrGetVulkanGraphicsRequirements2KHR");
XrGraphicsRequirementsVulkan2KHR requirements{
.type = XR_TYPE_GRAPHICS_REQUIREMENTS_VULKAN2_KHR,
};
CHECK_XR(xrGetVulkanGraphicsRequirements2KHR(*inst, id, &requirements));
return requirements;
}
XrSystemProperties xr::system::properties() const
{
if (!id)
throw std::invalid_argument("this");
XrSystemProperties prop{
.type = XR_TYPE_SYSTEM_PROPERTIES,
};
CHECK_XR(xrGetSystemProperties(*inst, id, &prop));
return prop;
}
XrSystemHandTrackingPropertiesEXT xr::system::hand_tracking_properties() const
{
if (!id)
throw std::invalid_argument("this");
XrSystemHandTrackingPropertiesEXT hand_tracking_prop{
.type = XR_TYPE_SYSTEM_HAND_TRACKING_PROPERTIES_EXT,
};
XrSystemProperties prop{
.type = XR_TYPE_SYSTEM_PROPERTIES,
.next = &hand_tracking_prop,
};
CHECK_XR(xrGetSystemProperties(*inst, id, &prop));
return hand_tracking_prop;
}
XrSystemEyeGazeInteractionPropertiesEXT xr::system::eye_gaze_interaction_properties() const
{
if (!id)
throw std::invalid_argument("this");
XrSystemEyeGazeInteractionPropertiesEXT eye_gaze_prop{
.type = XR_TYPE_SYSTEM_EYE_GAZE_INTERACTION_PROPERTIES_EXT,
};
XrSystemProperties prop{
.type = XR_TYPE_SYSTEM_PROPERTIES,
.next = &eye_gaze_prop,
};
CHECK_XR(xrGetSystemProperties(*inst, id, &prop));
return eye_gaze_prop;
}
XrSystemFoveationEyeTrackedPropertiesMETA xr::system::foveation_eye_tracked_properties() const
{
if (!id)
throw std::invalid_argument("this");
XrSystemFoveationEyeTrackedPropertiesMETA foveation_prop{
.type = XR_TYPE_SYSTEM_FOVEATION_EYE_TRACKED_PROPERTIES_META,
};
XrSystemProperties prop{
.type = XR_TYPE_SYSTEM_PROPERTIES,
.next = &foveation_prop,
};
CHECK_XR(xrGetSystemProperties(*inst, id, &prop));
return foveation_prop;
}
XrSystemUserPresencePropertiesEXT xr::system::user_presence_properties() const
{
if (!id)
throw std::invalid_argument("this");
XrSystemUserPresencePropertiesEXT presence_prop{
.type = XR_TYPE_SYSTEM_USER_PRESENCE_PROPERTIES_EXT,
};
XrSystemProperties prop{
.type = XR_TYPE_SYSTEM_PROPERTIES,
.next = &presence_prop,
};
CHECK_XR(xrGetSystemProperties(*inst, id, &prop));
return presence_prop;
}
XrSystemFaceTrackingPropertiesANDROID xr::system::android_face_tracking_properties() const
{
if (!id)
throw std::invalid_argument("this");
XrSystemFaceTrackingPropertiesANDROID face_tracking_prop{
.type = XR_TYPE_SYSTEM_FACE_TRACKING_PROPERTIES_ANDROID,
};
XrSystemProperties prop{
.type = XR_TYPE_SYSTEM_PROPERTIES,
.next = &face_tracking_prop,
};
CHECK_XR(xrGetSystemProperties(*inst, id, &prop));
return face_tracking_prop;
}
XrSystemFaceTrackingProperties2FB xr::system::fb_face_tracking2_properties() const
{
if (!id)
throw std::invalid_argument("this");
XrSystemFaceTrackingProperties2FB face_tracking_prop{
.type = XR_TYPE_SYSTEM_FACE_TRACKING_PROPERTIES2_FB,
};
XrSystemProperties prop{
.type = XR_TYPE_SYSTEM_PROPERTIES,
.next = &face_tracking_prop,
};
CHECK_XR(xrGetSystemProperties(*inst, id, &prop));
return face_tracking_prop;
}
XrSystemFacialTrackingPropertiesHTC xr::system::htc_face_tracking_properties() const
{
if (!id)
throw std::invalid_argument("this");
XrSystemFacialTrackingPropertiesHTC face_tracking_prop{
.type = XR_TYPE_SYSTEM_FACIAL_TRACKING_PROPERTIES_HTC,
};
XrSystemProperties prop{
.type = XR_TYPE_SYSTEM_PROPERTIES,
.next = &face_tracking_prop,
};
CHECK_XR(xrGetSystemProperties(*inst, id, &prop));
return face_tracking_prop;
}
XrSystemBodyTrackingPropertiesFB xr::system::fb_body_tracking_properties() const
{
if (!id)
throw std::invalid_argument("this");
XrSystemBodyTrackingPropertiesFB body_tracking_prop{
.type = XR_TYPE_SYSTEM_BODY_TRACKING_PROPERTIES_FB,
};
XrSystemProperties prop{
.type = XR_TYPE_SYSTEM_PROPERTIES,
.next = &body_tracking_prop,
};
CHECK_XR(xrGetSystemProperties(*inst, id, &prop));
return body_tracking_prop;
}
XrSystemPropertiesBodyTrackingFullBodyMETA xr::system::meta_body_tracking_properties() const
{
if (!id)
throw std::invalid_argument("this");
XrSystemPropertiesBodyTrackingFullBodyMETA body_tracking_prop{
.type = XR_TYPE_SYSTEM_PROPERTIES_BODY_TRACKING_FULL_BODY_META,
};
XrSystemProperties prop{
.type = XR_TYPE_SYSTEM_PROPERTIES,
.next = &body_tracking_prop,
};
CHECK_XR(xrGetSystemProperties(*inst, id, &prop));
return body_tracking_prop;
}
XrSystemBodyTrackingPropertiesBD xr::system::bd_body_tracking_properties() const
{
if (!id)
throw std::invalid_argument("this");
XrSystemBodyTrackingPropertiesBD body_tracking_prop{
.type = XR_TYPE_SYSTEM_BODY_TRACKING_PROPERTIES_BD,
};
XrSystemProperties prop{
.type = XR_TYPE_SYSTEM_PROPERTIES,
.next = &body_tracking_prop,
};
CHECK_XR(xrGetSystemProperties(*inst, id, &prop));
return body_tracking_prop;
}
XrSystemBoundaryVisibilityPropertiesMETA xr::system::boundary_visibility_properties() const
{
if (!id)
throw std::invalid_argument("this");
XrSystemBoundaryVisibilityPropertiesMETA boundary_prop{
.type = XR_TYPE_SYSTEM_BOUNDARY_VISIBILITY_PROPERTIES_META,
};
XrSystemProperties prop{
.type = XR_TYPE_SYSTEM_PROPERTIES,
.next = &boundary_prop,
};
CHECK_XR(xrGetSystemProperties(*inst, id, &prop));
return boundary_prop;
}
xr::passthrough_type xr::system::passthrough_supported() const
{
if (utils::contains(environment_blend_modes(XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO), XR_ENVIRONMENT_BLEND_MODE_ALPHA_BLEND))
return passthrough_type::color;
if (inst->has_extension(XR_HTC_PASSTHROUGH_EXTENSION_NAME))
return passthrough_type::color;
if (inst->has_extension(XR_FB_PASSTHROUGH_EXTENSION_NAME))
{
XrSystemPassthroughProperties2FB passthrough_prop2{
.type = XR_TYPE_SYSTEM_PASSTHROUGH_PROPERTIES2_FB,
};
XrSystemPassthroughPropertiesFB passthrough_prop{
.type = XR_TYPE_SYSTEM_PASSTHROUGH_PROPERTIES_FB,
.next = &passthrough_prop2,
};
XrSystemProperties prop{
.type = XR_TYPE_SYSTEM_PROPERTIES,
.next = &passthrough_prop,
};
CHECK_XR(xrGetSystemProperties(*inst, id, &prop));
if (passthrough_prop.supportsPassthrough)
{
if (!(passthrough_prop2.capabilities & XR_PASSTHROUGH_CAPABILITY_BIT_FB))
return passthrough_type::none;
if (passthrough_prop2.capabilities & XR_PASSTHROUGH_CAPABILITY_COLOR_BIT_FB)
return passthrough_type::color;
return passthrough_type::bw;
}
}
return passthrough_type::none;
}
xr::face_tracker_type xr::system::face_tracker_supported() const
{
return face_tracker;
}
xr::body_tracker_type xr::system::body_tracker_supported() const
{
return body_tracker;
}
bool xr::system::hand_tracking_supported() const
{
return hand_tracking_supported_;
}
bool xr::system::hand_mesh_fb_supported() const
{
return hand_mesh_fb_supported_;
}
vk::raii::PhysicalDevice xr::system::physical_device(vk::raii::Instance & vulkan) const
{
auto xrGetVulkanGraphicsDevice2KHR =
inst->get_proc<PFN_xrGetVulkanGraphicsDevice2KHR>("xrGetVulkanGraphicsDevice2KHR");
XrVulkanGraphicsDeviceGetInfoKHR get_info{
.type = XR_TYPE_VULKAN_GRAPHICS_DEVICE_GET_INFO_KHR,
.systemId = id,
.vulkanInstance = *vulkan,
};
VkPhysicalDevice dev;
CHECK_XR(xrGetVulkanGraphicsDevice2KHR(*inst, &get_info, &dev));
return vk::raii::PhysicalDevice{vulkan, dev};
}
vk::raii::Device xr::system::create_device(vk::raii::PhysicalDevice & pdev, vk::DeviceCreateInfo & create_info) const
{
XrVulkanDeviceCreateInfoKHR xr_create_info{
.type = XR_TYPE_VULKAN_DEVICE_CREATE_INFO_KHR,
.systemId = id,
.pfnGetInstanceProcAddr = vkGetInstanceProcAddr,
.vulkanPhysicalDevice = *pdev,
.vulkanCreateInfo = &(VkDeviceCreateInfo &)create_info,
};
VkDevice dev;
VkResult vresult;
auto xrCreateVulkanDeviceKHR = inst->get_proc<PFN_xrCreateVulkanDeviceKHR>("xrCreateVulkanDeviceKHR");
CHECK_XR(xrCreateVulkanDeviceKHR(*inst, &xr_create_info, &dev, &vresult));
CHECK_VK(vresult, "xrCreateVulkanDeviceKHR");
return vk::raii::Device{pdev, dev};
}
std::vector<XrViewConfigurationType> xr::system::view_configurations() const
{
return details::enumerate<XrViewConfigurationType>(xrEnumerateViewConfigurations, *inst, id);
}
XrViewConfigurationProperties xr::system::view_configuration_properties(XrViewConfigurationType type) const
{
XrViewConfigurationProperties prop{
.type = XR_TYPE_VIEW_CONFIGURATION_PROPERTIES,
};
CHECK_XR(xrGetViewConfigurationProperties(*inst, id, type, &prop));
return prop;
}
std::vector<XrViewConfigurationView> xr::system::view_configuration_views(XrViewConfigurationType type) const
{
return details::enumerate<XrViewConfigurationView>(xrEnumerateViewConfigurationViews, *inst, id, type);
}
std::vector<XrEnvironmentBlendMode> xr::system::environment_blend_modes(XrViewConfigurationType type) const
{
return details::enumerate<XrEnvironmentBlendMode>(xrEnumerateEnvironmentBlendModes, *inst, id, type);
}