/* * WiVRn VR streaming * Copyright (C) 2022 Guillaume Meunier * Copyright (C) 2022 Patrick Nicolas * * 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 . */ #include "session.h" #include "details/enumerate.h" #include "openxr/openxr.h" #include "utils/contains.h" #include "xr/instance.h" #include "xr/system.h" #include "xr/to_string.h" #include #include #include xr::session::session(xr::instance & inst, xr::system & sys, vk::raii::Instance & vk_inst, vk::raii::PhysicalDevice & pdev, vk::raii::Device & dev, thread_safe & queue, int queue_family_index) : inst(&inst), queue(&queue) { XrGraphicsBindingVulkan2KHR vulkan_binding{ .type = XR_TYPE_GRAPHICS_BINDING_VULKAN2_KHR, .instance = *vk_inst, .physicalDevice = *pdev, .device = *dev, .queueFamilyIndex = (uint32_t)queue_family_index, .queueIndex = 0, }; XrSessionCreateInfo session_info{ .type = XR_TYPE_SESSION_CREATE_INFO, .next = &vulkan_binding, .systemId = sys, }; CHECK_XR(xrCreateSession(inst, &session_info, &id)); if (inst.has_extension(XR_FB_DISPLAY_REFRESH_RATE_EXTENSION_NAME)) { xrGetDisplayRefreshRateFB = inst.get_proc("xrGetDisplayRefreshRateFB"); xrEnumerateDisplayRefreshRatesFB = inst.get_proc("xrEnumerateDisplayRefreshRatesFB"); xrRequestDisplayRefreshRateFB = inst.get_proc("xrRequestDisplayRefreshRateFB"); } if (inst.has_extension(XR_EXT_PERFORMANCE_SETTINGS_EXTENSION_NAME)) xrPerfSettingsSetPerformanceLevelEXT = inst.get_proc("xrPerfSettingsSetPerformanceLevelEXT"); if (inst.has_extension(XR_META_BOUNDARY_VISIBILITY_EXTENSION_NAME) and sys.boundary_visibility_properties().supportsBoundaryVisibility) xrRequestBoundaryVisibilityMETA = inst.get_proc("xrRequestBoundaryVisibilityMETA"); } std::vector xr::session::get_reference_spaces() const { return details::enumerate(xrEnumerateReferenceSpaces, id); } xr::space xr::session::create_reference_space(XrReferenceSpaceType ref, const XrPosef & pose) { XrReferenceSpaceCreateInfo create_info{ .type = XR_TYPE_REFERENCE_SPACE_CREATE_INFO, .referenceSpaceType = ref, .poseInReferenceSpace = pose, }; XrSpace s; CHECK_XR(xrCreateReferenceSpace(id, &create_info, &s)); return s; } xr::space xr::session::create_action_space(XrAction action, const XrPosef & pose) { XrActionSpaceCreateInfo create_info{ .type = XR_TYPE_ACTION_SPACE_CREATE_INFO, .action = action, .subactionPath = XR_NULL_PATH, .poseInActionSpace = pose, }; XrSpace s; CHECK_XR(xrCreateActionSpace(id, &create_info, &s)); return s; } xr::hand_tracker xr::session::create_hand_tracker(XrHandEXT hand, std::span sources, XrHandJointSetEXT hand_joint_set) { return { *inst, *this, XrHandTrackerCreateInfoEXT{ .type = XR_TYPE_HAND_TRACKER_CREATE_INFO_EXT, .next = nullptr, .hand = hand, .handJointSet = hand_joint_set, }, sources, }; } std::vector xr::session::get_swapchain_formats() const { std::vector formats; for (auto i: details::enumerate(xrEnumerateSwapchainFormats, id)) formats.push_back(static_cast(i)); return formats; } XrFrameState xr::session::wait_frame() { XrFrameWaitInfo wait_info{ .type = XR_TYPE_FRAME_WAIT_INFO, }; XrFrameState state{ .type = XR_TYPE_FRAME_STATE, }; CHECK_XR(xrWaitFrame(id, &wait_info, &state)); return state; } void xr::session::begin_frame() { XrFrameBeginInfo begin_info{ .type = XR_TYPE_FRAME_BEGIN_INFO, }; auto lock = queue->lock(); CHECK_XR(xrBeginFrame(id, &begin_info)); } void xr::session::end_frame(XrTime display_time, const std::vector & layers, XrEnvironmentBlendMode blend_mode) { const XrLocalDimmingFrameEndInfoMETA local_dimming_info{ .type = XR_TYPE_LOCAL_DIMMING_FRAME_END_INFO_META, .localDimmingMode = XR_LOCAL_DIMMING_MODE_ON_META, }; const XrFrameEndInfo end_info{ .type = XR_TYPE_FRAME_END_INFO, .next = inst->has_extension(XR_META_LOCAL_DIMMING_EXTENSION_NAME) ? &local_dimming_info : nullptr, .displayTime = display_time, .environmentBlendMode = blend_mode, .layerCount = (uint32_t)layers.size(), .layers = layers.data(), }; { auto lock = queue->lock(); CHECK_XR(xrEndFrame(id, &end_info)); } update_boundary_visibility(); } void xr::session::begin_session(XrViewConfigurationType view_config) { XrSessionBeginInfo begin_info{ .type = XR_TYPE_SESSION_BEGIN_INFO, .primaryViewConfigurationType = view_config, }; CHECK_XR(xrBeginSession(id, &begin_info)); } void xr::session::end_session() { CHECK_XR(xrEndSession(id)); } XrViewStateFlags xr::session::locate_views(XrViewConfigurationType view_config_type, XrTime display_time, XrSpace space, std::vector & views) { XrViewLocateInfo view_locate_info{ .type = XR_TYPE_VIEW_LOCATE_INFO, .viewConfigurationType = view_config_type, .displayTime = display_time, .space = space, }; XrViewState view_state{ .type = XR_TYPE_VIEW_STATE, }; details::enumerate(xrLocateViews, views, id, &view_locate_info, &view_state); return view_state.viewStateFlags; } std::pair> xr::session::locate_views(XrViewConfigurationType view_config_type, XrTime display_time, XrSpace space) { std::vector views(2, XrView{.type = XR_TYPE_VIEW}); return {locate_views(view_config_type, display_time, space, views), std::move(views)}; } std::string xr::session::get_current_interaction_profile(const std::string & path) { XrInteractionProfileState state{ .type = XR_TYPE_INTERACTION_PROFILE_STATE, }; CHECK_XR(xrGetCurrentInteractionProfile(id, inst->string_to_path(path), &state)); return inst->path_to_string(state.interactionProfile); } void xr::session::attach_actionsets(const std::vector & actionsets) { XrSessionActionSetsAttachInfo attach_info{ .type = XR_TYPE_SESSION_ACTION_SETS_ATTACH_INFO, .countActionSets = (uint32_t)actionsets.size(), .actionSets = actionsets.data(), }; CHECK_XR(xrAttachSessionActionSets(id, &attach_info)); } std::vector xr::session::sources_for_action(XrAction action) { XrBoundSourcesForActionEnumerateInfo action_info{ .type = XR_TYPE_BOUND_SOURCES_FOR_ACTION_ENUMERATE_INFO, .action = action, }; auto sources = xr::details::enumerate(xrEnumerateBoundSourcesForAction, id, &action_info); std::vector sources_name; sources_name.reserve(sources.size()); for (XrPath path: sources) { sources_name.push_back(inst->path_to_string(path)); } return sources_name; } std::vector xr::session::localized_sources_for_action(XrAction action, XrInputSourceLocalizedNameFlags components) { XrBoundSourcesForActionEnumerateInfo action_info{ .type = XR_TYPE_BOUND_SOURCES_FOR_ACTION_ENUMERATE_INFO, .action = action, }; auto sources = xr::details::enumerate(xrEnumerateBoundSourcesForAction, id, &action_info); std::vector sources_name; sources_name.reserve(sources.size()); for (XrPath path: sources) { XrInputSourceLocalizedNameGetInfo name_info{ .type = XR_TYPE_INPUT_SOURCE_LOCALIZED_NAME_GET_INFO, .sourcePath = path, .whichComponents = components, }; sources_name.push_back(details::enumerate(xrGetInputSourceLocalizedName, id, &name_info)); } return sources_name; } float xr::session::get_current_refresh_rate() { assert(inst->has_extension(XR_FB_DISPLAY_REFRESH_RATE_EXTENSION_NAME)); float refresh_rate = 0; if (xrGetDisplayRefreshRateFB) CHECK_XR(xrGetDisplayRefreshRateFB(id, &refresh_rate)); return refresh_rate; } std::vector xr::session::get_refresh_rates() { if (xrEnumerateDisplayRefreshRatesFB) { try { return xr::details::enumerate(xrEnumerateDisplayRefreshRatesFB, id); } catch (...) { // Return no available refresh rate in case of error } } return {}; } void xr::session::set_refresh_rate(float refresh_rate) { if (xrRequestDisplayRefreshRateFB) { spdlog::info("set refresh rate to {}Hz", refresh_rate); if (auto res = xrRequestDisplayRefreshRateFB(id, refresh_rate); res != XR_SUCCESS) spdlog::warn("Refresh rate change failed: {}", xr::to_string(res)); } } void xr::session::sync_actions(std::span action_sets) { std::vector active_action_sets(action_sets.size()); for (auto && [i, j]: std::views::zip(active_action_sets, action_sets)) { i.actionSet = j; i.subactionPath = XR_NULL_PATH; } XrActionsSyncInfo sync_info{ .type = XR_TYPE_ACTIONS_SYNC_INFO, .countActiveActionSets = (uint32_t)active_action_sets.size(), .activeActionSets = active_action_sets.data(), }; CHECK_XR(xrSyncActions(id, &sync_info)); } void xr::session::sync_actions(XrActionSet action_set, XrPath subaction_path) { XrActiveActionSet active_action_set{ .actionSet = action_set, .subactionPath = subaction_path}; XrActionsSyncInfo sync_info{ .type = XR_TYPE_ACTIONS_SYNC_INFO, .countActiveActionSets = 1, .activeActionSets = &active_action_set, }; CHECK_XR(xrSyncActions(id, &sync_info)); } void xr::session::sync_actions(XrActionSet action_set, const std::string & subaction_path) { sync_actions(action_set, inst->string_to_path(subaction_path)); } void xr::session::enable_passthrough(xr::system & system) { if (not std::holds_alternative(passthrough)) return; if (system.passthrough_supported() == xr::passthrough_type::none) return; if (inst->has_extension(XR_FB_PASSTHROUGH_EXTENSION_NAME)) { passthrough.emplace(*inst, *this); } else if (inst->has_extension(XR_HTC_PASSTHROUGH_EXTENSION_NAME)) { passthrough.emplace(*inst, *this); } else if (utils::contains(system.environment_blend_modes(XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO), XR_ENVIRONMENT_BLEND_MODE_ALPHA_BLEND)) { passthrough.emplace(); } } void xr::session::disable_passthrough() { if (std::holds_alternative(passthrough)) return; passthrough.emplace(); } void xr::session::set_passthrough_boundary_enabled(bool enabled) { passthrough_boundary_enabled = enabled; boundary_request_failed = false; } void xr::session::on_boundary_visibility_changed(XrBoundaryVisibilityMETA v) { runtime_boundary_visibility = v; } // the runtime only allows suppression while passthrough is shown and restores // the boundary when it stops, so this re-requests every time passthrough comes back void xr::session::update_boundary_visibility() { if (not xrRequestBoundaryVisibilityMETA) return; auto v = not passthrough_boundary_enabled and not std::holds_alternative(passthrough) ? XR_BOUNDARY_VISIBILITY_SUPPRESSED_META : XR_BOUNDARY_VISIBILITY_NOT_SUPPRESSED_META; if (v == runtime_boundary_visibility) return; if (auto res = xrRequestBoundaryVisibilityMETA(id, v); res == XR_SUCCESS) { runtime_boundary_visibility = v; boundary_request_failed = false; } else if (not boundary_request_failed) { spdlog::warn("Boundary visibility change failed: {}", xr::to_string(res)); boundary_request_failed = true; } } void xr::session::set_performance_level(XrPerfSettingsDomainEXT domain, XrPerfSettingsLevelEXT level) { if (xrPerfSettingsSetPerformanceLevelEXT) xrPerfSettingsSetPerformanceLevelEXT(*this, domain, level); }