diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 5113db4a..2062c671 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -38,6 +38,10 @@ + + + + diff --git a/client/application.cpp b/client/application.cpp index eda4471b..2cd4d0d7 100644 --- a/client/application.cpp +++ b/client/application.cpp @@ -726,6 +726,8 @@ void application::initialize() opt_extensions.push_back(XR_KHR_COMPOSITION_LAYER_COLOR_SCALE_BIAS_EXTENSION_NAME); opt_extensions.push_back(XR_FB_DISPLAY_REFRESH_RATE_EXTENSION_NAME); opt_extensions.push_back(XR_EXT_HAND_TRACKING_EXTENSION_NAME); + opt_extensions.push_back(XR_FB_PASSTHROUGH_EXTENSION_NAME); + opt_extensions.push_back(XR_HTC_PASSTHROUGH_EXTENSION_NAME); for(const auto& i: interaction_profiles) { @@ -802,6 +804,8 @@ void application::initialize() } } + spdlog::info("Passthrough: {}", xr_system_id.passthrough_supported() ? "supported" : "not supported"); + vk::CommandPoolCreateInfo cmdpool_create_info; cmdpool_create_info.queueFamilyIndex = vk_queue_family_index; @@ -1343,6 +1347,18 @@ void application::poll_events() e.refresh_rate_changed.toDisplayRefreshRate); } break; + case XR_TYPE_EVENT_DATA_PASSTHROUGH_STATE_CHANGED_FB: { + spdlog::info("Passthrough state changed:"); + if (e.passthrough_state_changed.flags & XR_PASSTHROUGH_STATE_CHANGED_REINIT_REQUIRED_BIT_FB) + spdlog::info(" XR_PASSTHROUGH_STATE_CHANGED_REINIT_REQUIRED_BIT_FB"); + if (e.passthrough_state_changed.flags & XR_PASSTHROUGH_STATE_CHANGED_NON_RECOVERABLE_ERROR_BIT_FB) + spdlog::info(" XR_PASSTHROUGH_STATE_CHANGED_NON_RECOVERABLE_ERROR_BIT_FB"); + if (e.passthrough_state_changed.flags & XR_PASSTHROUGH_STATE_CHANGED_RECOVERABLE_ERROR_BIT_FB) + spdlog::info(" XR_PASSTHROUGH_STATE_CHANGED_RECOVERABLE_ERROR_BIT_FB"); + if (e.passthrough_state_changed.flags & XR_PASSTHROUGH_STATE_CHANGED_RESTORED_ERROR_BIT_FB) + spdlog::info(" XR_PASSTHROUGH_STATE_CHANGED_RESTORED_ERROR_BIT_FB"); + } + break; default: spdlog::info("Received event type {}", xr::to_string(e.header.type)); break; diff --git a/client/application.h b/client/application.h index 9d1aa0b6..ce0690d6 100644 --- a/client/application.h +++ b/client/application.h @@ -404,4 +404,9 @@ public: { return instance().right_hand; } + + static const std::vector& get_xr_extensions() + { + return instance().xr_extensions; + } }; diff --git a/client/scenes/lobby.cpp b/client/scenes/lobby.cpp index 6a76cf00..17b0ffcc 100644 --- a/client/scenes/lobby.cpp +++ b/client/scenes/lobby.cpp @@ -18,7 +18,7 @@ */ #include "lobby.h" -#include "../common/version.h" +#include "version.h" #include "application.h" #include "glm/geometric.hpp" #include "imgui.h" @@ -28,9 +28,10 @@ #include "render/scene_renderer.h" #include "stream.h" #include "hardware.h" +#include "utils/contains.h" #include "wivrn_client.h" +#include "xr/passthrough.h" #include -#include #include #include "wivrn_discover.h" @@ -45,7 +46,6 @@ #include #include #include -#include #include #include @@ -223,6 +223,10 @@ scenes::lobby::lobby() auto mic = root["microphone"]; if (mic.is_bool()) microphone = mic.get_bool(); + + auto passthrough = root["passthrough_enabled"]; + if (passthrough.is_bool()) + passthrough_enabled = passthrough.get_bool(); } catch(std::exception& e) { @@ -260,6 +264,22 @@ scenes::lobby::lobby() throw std::runtime_error("No supported swapchain format"); spdlog::info("Using format {}", vk::to_string(swapchain_format)); + + passthrough_supported = system.passthrough_supported(); + + if (passthrough_supported) + { + 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() @@ -292,6 +312,7 @@ void scenes::lobby::save_config() if (preferred_refresh_rate != 0.) json << ",\"preferred_refresh_rate\":" << preferred_refresh_rate ; json << ",\"microphone\":" << std::boolalpha << microphone; + json << ",\"passthrough_enabled\":" << std::boolalpha << passthrough_enabled; json << "}"; } @@ -585,13 +606,19 @@ void scenes::lobby::render(XrTime predicted_display_time, bool should_render) assert(renderer); renderer->start_frame(); - auto lobby_layer_views = render_layer(views, swapchains_lobby, *renderer, *lobby_scene, {0, 0.25, 0.5, 1}); + std::vector lobby_layer_views; + if (!passthrough_enabled) + lobby_layer_views = render_layer(views, swapchains_lobby, *renderer, *lobby_scene, {0, 0.25, 0.5, 1}); + auto controllers_layer_views = render_layer(views, swapchains_controllers, *renderer, *controllers_scene, {0, 0, 0, 0}); renderer->end_frame(); // After end_frame because the command buffers are submitted in end_frame - for (auto & swapchain: swapchains_lobby) - swapchain.release(); + if (!passthrough_enabled) + { + for (auto & swapchain: swapchains_lobby) + swapchain.release(); + } for (auto & swapchain: swapchains_controllers) swapchain.release(); @@ -613,7 +640,17 @@ void scenes::lobby::render(XrTime predicted_display_time, bool should_render) }; std::vector layers_base; - layers_base.push_back(reinterpret_cast(&lobby_layer)); + + if (passthrough_enabled) + { + std::visit([&](auto& p){ + layers_base.push_back(p.layer()); + }, passthrough); + } + else + { + layers_base.push_back(reinterpret_cast(&lobby_layer)); + } layers_base.push_back(reinterpret_cast(&imgui_layer)); layers_base.push_back(reinterpret_cast(&controllers_layer)); diff --git a/client/scenes/lobby.h b/client/scenes/lobby.h index 0e129bd4..fcd12ba2 100644 --- a/client/scenes/lobby.h +++ b/client/scenes/lobby.h @@ -24,6 +24,7 @@ #include #include "wivrn_discover.h" +#include "xr/passthrough.h" #include #include #include @@ -88,6 +89,8 @@ class lobby : public scene_impl std::vector swapchains_controllers; xr::swapchain swapchain_imgui; vk::Format swapchain_format; + bool passthrough_supported; + xr::passthrough passthrough; void save_config(); @@ -114,6 +117,7 @@ class lobby : public scene_impl float preferred_refresh_rate = 0; bool show_performance_metrics = false; bool microphone = false; + bool passthrough_enabled = false; void gui_connecting(); void gui_server_list(); diff --git a/client/scenes/lobby_gui.cpp b/client/scenes/lobby_gui.cpp index 8ec78ada..4bad1e7d 100644 --- a/client/scenes/lobby_gui.cpp +++ b/client/scenes/lobby_gui.cpp @@ -383,6 +383,14 @@ void scenes::lobby::gui_settings() } vibrate_on_hover(); + ImGui::BeginDisabled(!passthrough_supported); + if (ImGui::Checkbox("Enable video passthrough in lobby", &passthrough_enabled)) + { + save_config(); + } + vibrate_on_hover(); + ImGui::EndDisabled(); + if (ImGui::Checkbox("Show performance metrics", &show_performance_metrics)) save_config(); vibrate_on_hover(); diff --git a/client/xr/hand_tracker.h b/client/xr/hand_tracker.h index 52627085..07f01ba2 100644 --- a/client/xr/hand_tracker.h +++ b/client/xr/hand_tracker.h @@ -35,11 +35,11 @@ class hand_tracker : public utils::handle public: hand_tracker() = default; - hand_tracker(instance * inst, XrHandTrackerEXT h) + hand_tracker(instance & inst, XrHandTrackerEXT h) { id = h; - xrLocateHandJointsEXT = inst->get_proc("xrLocateHandJointsEXT"); - xrDestroyHandTrackerEXT = inst->get_proc("xrDestroyHandTrackerEXT"); + xrLocateHandJointsEXT = inst.get_proc("xrLocateHandJointsEXT"); + xrDestroyHandTrackerEXT = inst.get_proc("xrDestroyHandTrackerEXT"); } hand_tracker(hand_tracker &&) = default; hand_tracker & operator=(hand_tracker &&) = default; diff --git a/client/xr/instance.h b/client/xr/instance.h index 8cc1c51f..3ff1999a 100644 --- a/client/xr/instance.h +++ b/client/xr/instance.h @@ -45,6 +45,7 @@ union event XrEventDataReferenceSpaceChangePending space_changed_pending; XrEventDataSessionStateChanged state_changed; XrEventDataDisplayRefreshRateChangedFB refresh_rate_changed; + XrEventDataPassthroughStateChangedFB passthrough_state_changed; }; class instance : public utils::handle { diff --git a/client/xr/passthrough.cpp b/client/xr/passthrough.cpp new file mode 100644 index 00000000..17613134 --- /dev/null +++ b/client/xr/passthrough.cpp @@ -0,0 +1,109 @@ +/* + * 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 "passthrough.h" +#include "session.h" +#include "openxr/openxr.h" + +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"); + xrPassthroughPauseFB = inst.get_proc("xrPassthroughPauseFB"); + xrPassthroughLayerPauseFB = inst.get_proc("xrPassthroughLayerPauseFB"); + xrPassthroughLayerResumeFB = inst.get_proc("xrPassthroughLayerResumeFB"); + + XrPassthroughCreateInfoFB info{ + .type = XR_TYPE_PASSTHROUGH_CREATE_INFO_FB, + .flags = 0 + }; + + CHECK_XR(xrCreatePassthroughFB(s, &info, &id)); + + XrPassthroughLayerCreateInfoFB layer_info{ + .type = XR_TYPE_PASSTHROUGH_LAYER_CREATE_INFO_FB, + .passthrough = id, + .flags = 0, + .purpose = XR_PASSTHROUGH_LAYER_PURPOSE_RECONSTRUCTION_FB + }; + + CHECK_XR(xrCreatePassthroughLayerFB(s, &layer_info, &passthrough_layer)); + + composition_layer = XrCompositionLayerPassthroughFB{ + .type = XR_TYPE_COMPOSITION_LAYER_PASSTHROUGH_FB, + .flags = 0, + .space = XR_NULL_HANDLE, + .layerHandle = passthrough_layer + }; +} + +xr::passthrough_fb::~passthrough_fb() +{ + if (passthrough_layer && xrDestroyPassthroughLayerFB) + xrDestroyPassthroughLayerFB(passthrough_layer); + + if (id != XR_NULL_HANDLE && xrDestroyPassthroughFB) + xrDestroyPassthroughFB(id); +} + +void xr::passthrough_fb::start() +{ + CHECK_XR(xrPassthroughStartFB(id)); + CHECK_XR(xrPassthroughLayerResumeFB(passthrough_layer)); +} + +void xr::passthrough_fb::pause() +{ + CHECK_XR(xrPassthroughLayerPauseFB(passthrough_layer)); + CHECK_XR(xrPassthroughPauseFB(id)); +} + +xr::passthrough_htc::passthrough_htc(instance & inst, session & s) +{ + PFN_xrCreatePassthroughHTC xrCreatePassthroughHTC = inst.get_proc("xrCreatePassthroughHTC"); + xrDestroyPassthroughHTC = inst.get_proc("xrDestroyPassthroughHTC"); + + XrPassthroughCreateInfoHTC info{ + .type = XR_TYPE_PASSTHROUGH_CREATE_INFO_HTC, + .form = XR_PASSTHROUGH_FORM_PLANAR_HTC + }; + + CHECK_XR(xrCreatePassthroughHTC(s, &info, &id)); + + composition_layer = XrCompositionLayerPassthroughHTC{ + .type = XR_TYPE_COMPOSITION_LAYER_PASSTHROUGH_HTC, + .layerFlags = 0, + .space = XR_NULL_HANDLE, + .passthrough = id, + .color = { + .type = XR_TYPE_PASSTHROUGH_COLOR_HTC, + .alpha = 1 + } + }; +} + +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 new file mode 100644 index 00000000..e869c6d5 --- /dev/null +++ b/client/xr/passthrough.h @@ -0,0 +1,78 @@ +/* + * 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 . + */ + +#pragma once + +#include "openxr/openxr.h" +#include "xr.h" +#include + +namespace xr +{ +class session; + +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{}; + XrCompositionLayerPassthroughFB composition_layer; + +public: + passthrough_fb() = default; + passthrough_fb(instance &, session &); + passthrough_fb(passthrough_fb &&) = default; + passthrough_fb(const passthrough_fb &) = delete; + passthrough_fb & operator=(passthrough_fb &&) = default; + passthrough_fb & operator=(const passthrough_fb &) = delete; + ~passthrough_fb(); + + void start(); + void pause(); + XrCompositionLayerBaseHeader * layer() { return (XrCompositionLayerBaseHeader*)&composition_layer; } +}; + + +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; } +}; + +using passthrough = std::variant; + +} // namespace xr diff --git a/client/xr/session.cpp b/client/xr/session.cpp index e33ede02..b0150dfd 100644 --- a/client/xr/session.cpp +++ b/client/xr/session.cpp @@ -93,7 +93,7 @@ xr::hand_tracker xr::session::create_hand_tracker(XrHandEXT hand, XrHandJointSet assert(xrCreateHandTrackerEXT); CHECK_XR(xrCreateHandTrackerEXT(id, &create_info, &ht)); - return {inst, ht}; + return {*inst, ht}; } std::vector xr::session::get_swapchain_formats() const diff --git a/client/xr/system.cpp b/client/xr/system.cpp index 0250c27e..cd7b671f 100644 --- a/client/xr/system.cpp +++ b/client/xr/system.cpp @@ -19,7 +19,9 @@ #include "system.h" +#include "application.h" #include "details/enumerate.h" +#include "utils/contains.h" #include "vk/check.h" #include "xr/check.h" #include @@ -82,6 +84,31 @@ XrSystemHandTrackingPropertiesEXT xr::system::hand_tracking_properties() const return hand_tracking_prop; } +bool xr::system::passthrough_supported() const +{ + const std::vector& xr_extensions = application::get_xr_extensions(); + if (utils::contains(xr_extensions, XR_HTC_PASSTHROUGH_EXTENSION_NAME)) + return true; + + if (utils::contains(xr_extensions, XR_FB_PASSTHROUGH_EXTENSION_NAME)) + { + XrSystemPassthroughPropertiesFB passthrough_prop + { + .type = XR_TYPE_SYSTEM_PASSTHROUGH_PROPERTIES_FB, + }; + + XrSystemProperties prop{ + .type = XR_TYPE_SYSTEM_PROPERTIES, + .next = &passthrough_prop, + }; + CHECK_XR(xrGetSystemProperties(*inst, id, &prop)); + + return passthrough_prop.supportsPassthrough; + } + + return false; +} + vk::raii::PhysicalDevice xr::system::physical_device(vk::raii::Instance& vulkan) const { auto xrGetVulkanGraphicsDevice2KHR = diff --git a/client/xr/system.h b/client/xr/system.h index 97dad9fa..28593af6 100644 --- a/client/xr/system.h +++ b/client/xr/system.h @@ -55,6 +55,7 @@ public: XrSystemProperties properties() const; XrSystemHandTrackingPropertiesEXT hand_tracking_properties() const; + bool passthrough_supported() const; XrGraphicsRequirementsVulkan2KHR graphics_requirements() const; vk::raii::PhysicalDevice physical_device(vk::raii::Instance& vulkan) const;