/* * 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 #ifdef XR_USE_PLATFORM_ANDROID #include #endif #include "spdlog/spdlog.h" #include "vk/vk.h" #include "xr/xr.h" #include #include #include #include #include #include #include class scene; struct application_info { std::string name = "Unnamed application"; int version = VK_MAKE_VERSION(1, 0, 0); XrFormFactor formfactor = XR_FORM_FACTOR_HEAD_MOUNTED_DISPLAY; XrViewConfigurationType viewconfig = XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO; XrVersion min_vulkan_version = XR_MAKE_VERSION(1, 1, 0); #ifdef XR_USE_PLATFORM_ANDROID android_app * native_app; #endif }; class application; #ifdef XR_USE_PLATFORM_ANDROID class jni_thread { JNIEnv * env = nullptr; JavaVM * vm = nullptr; public: explicit jni_thread(application & app); jni_thread(); ~jni_thread(); JNIEnv * get_JNIEnv() { return env; } }; #else class [[maybe_unused]] jni_thread { public: explicit jni_thread(application & app) {} jni_thread() = default; ~jni_thread() = default; }; #endif class application { friend class scene; friend class jni_thread; application_info app_info; #ifdef XR_USE_PLATFORM_ANDROID ANativeWindow * native_window = nullptr; bool resumed = false; #endif static inline const char engine_name[] = "No engine"; static inline const int engine_version = VK_MAKE_VERSION(1, 0, 0); static inline const std::array supported_formats = {VK_FORMAT_R8G8B8A8_SRGB, VK_FORMAT_B8G8R8A8_SRGB}; void initialize_vulkan(); void log_views(); void initialize(); void cleanup(); void poll_events(); void session_state_changed(XrSessionState new_state, XrTime timestamp); void interaction_profile_changed(); static application * instance_; jni_thread jni; // OpenXR stuff xr::instance xr_instance; xr::system xr_system_id; xr::session xr_session; std::vector xr_swapchains; XrSessionState session_state = XR_SESSION_STATE_UNKNOWN; xr::space world_space; xr::space view_space; xr::actionset xr_actionset; std::vector> actions; std::vector action_spaces; // Vulkan stuff VkInstance vk_instance{}; VkPhysicalDevice vk_physical_device{}; VkDevice vk_device{}; uint32_t vk_queue_family_index; VkQueue vk_queue{}; vk::renderpass vk_renderpass; vk::pipeline vk_pipeline; vk::command_pool vk_cmdpool; VkFormat swapchain_format; static inline const std::pair oculus_touch[] = { {"/user/hand/left/output/haptic", XR_ACTION_TYPE_VIBRATION_OUTPUT}, {"/user/hand/right/output/haptic", XR_ACTION_TYPE_VIBRATION_OUTPUT}, {"/user/hand/left/input/grip/pose", XR_ACTION_TYPE_POSE_INPUT}, {"/user/hand/left/input/aim/pose", XR_ACTION_TYPE_POSE_INPUT}, {"/user/hand/right/input/grip/pose", XR_ACTION_TYPE_POSE_INPUT}, {"/user/hand/right/input/aim/pose", XR_ACTION_TYPE_POSE_INPUT}, {"/user/hand/left/input/x/click", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/left/input/x/touch", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/left/input/y/click", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/left/input/y/touch", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/left/input/menu/click", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/left/input/squeeze/value", XR_ACTION_TYPE_FLOAT_INPUT}, {"/user/hand/left/input/trigger/value", XR_ACTION_TYPE_FLOAT_INPUT}, {"/user/hand/left/input/trigger/touch", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/left/input/thumbstick", XR_ACTION_TYPE_VECTOR2F_INPUT}, {"/user/hand/left/input/thumbstick/click", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/left/input/thumbstick/touch", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/left/input/thumbrest/touch", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/right/input/a/click", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/right/input/a/touch", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/right/input/b/click", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/right/input/b/touch", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/right/input/system/click", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/right/input/squeeze/value", XR_ACTION_TYPE_FLOAT_INPUT}, {"/user/hand/right/input/trigger/value", XR_ACTION_TYPE_FLOAT_INPUT}, {"/user/hand/right/input/trigger/touch", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/right/input/thumbstick", XR_ACTION_TYPE_VECTOR2F_INPUT}, {"/user/hand/right/input/thumbstick/click", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/right/input/thumbstick/touch", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/right/input/thumbrest/touch", XR_ACTION_TYPE_BOOLEAN_INPUT}, }; static inline const std::pair pico_neo_3[] = { {"/user/hand/left/output/haptic", XR_ACTION_TYPE_VIBRATION_OUTPUT}, {"/user/hand/right/output/haptic", XR_ACTION_TYPE_VIBRATION_OUTPUT}, {"/user/hand/left/input/grip/pose", XR_ACTION_TYPE_POSE_INPUT}, {"/user/hand/left/input/aim/pose", XR_ACTION_TYPE_POSE_INPUT}, {"/user/hand/right/input/grip/pose", XR_ACTION_TYPE_POSE_INPUT}, {"/user/hand/right/input/aim/pose", XR_ACTION_TYPE_POSE_INPUT}, {"/user/hand/left/input/x/click", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/left/input/x/touch", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/left/input/y/click", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/left/input/y/touch", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/left/input/squeeze/value", XR_ACTION_TYPE_FLOAT_INPUT}, {"/user/hand/left/input/trigger/value", XR_ACTION_TYPE_FLOAT_INPUT}, {"/user/hand/left/input/trigger/touch", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/left/input/thumbstick", XR_ACTION_TYPE_VECTOR2F_INPUT}, {"/user/hand/left/input/thumbstick/click", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/left/input/thumbstick/touch", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/left/input/thumbrest/touch", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/right/input/a/click", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/right/input/a/touch", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/right/input/b/click", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/right/input/b/touch", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/right/input/squeeze/value", XR_ACTION_TYPE_FLOAT_INPUT}, {"/user/hand/right/input/trigger/value", XR_ACTION_TYPE_FLOAT_INPUT}, {"/user/hand/right/input/trigger/touch", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/right/input/thumbstick", XR_ACTION_TYPE_VECTOR2F_INPUT}, {"/user/hand/right/input/thumbstick/click", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/right/input/thumbstick/touch", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/right/input/thumbrest/touch", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/left/input/menu/click", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/left/input/system/click", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/right/input/menu/click", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/right/input/system/click", XR_ACTION_TYPE_BOOLEAN_INPUT}, }; static inline const std::pair pico_4[] = { {"/user/hand/left/output/haptic", XR_ACTION_TYPE_VIBRATION_OUTPUT}, {"/user/hand/right/output/haptic", XR_ACTION_TYPE_VIBRATION_OUTPUT}, {"/user/hand/left/input/grip/pose", XR_ACTION_TYPE_POSE_INPUT}, {"/user/hand/left/input/aim/pose", XR_ACTION_TYPE_POSE_INPUT}, {"/user/hand/right/input/grip/pose", XR_ACTION_TYPE_POSE_INPUT}, {"/user/hand/right/input/aim/pose", XR_ACTION_TYPE_POSE_INPUT}, {"/user/hand/left/input/x/click", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/left/input/x/touch", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/left/input/y/click", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/left/input/y/touch", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/left/input/squeeze/click", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/left/input/squeeze/value", XR_ACTION_TYPE_FLOAT_INPUT}, {"/user/hand/left/input/trigger/value", XR_ACTION_TYPE_FLOAT_INPUT}, {"/user/hand/left/input/trigger/touch", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/left/input/thumbstick", XR_ACTION_TYPE_VECTOR2F_INPUT}, {"/user/hand/left/input/thumbstick/click", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/left/input/thumbstick/touch", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/left/input/thumbrest/touch", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/right/input/a/click", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/right/input/a/touch", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/right/input/b/click", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/right/input/b/touch", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/right/input/squeeze/click", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/right/input/squeeze/value", XR_ACTION_TYPE_FLOAT_INPUT}, {"/user/hand/right/input/trigger/value", XR_ACTION_TYPE_FLOAT_INPUT}, {"/user/hand/right/input/trigger/touch", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/right/input/thumbstick", XR_ACTION_TYPE_VECTOR2F_INPUT}, {"/user/hand/right/input/thumbstick/click", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/right/input/thumbstick/touch", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/right/input/thumbrest/touch", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/left/input/menu/click", XR_ACTION_TYPE_BOOLEAN_INPUT}, }; static inline const std::pair simple_controller[] = { {"/user/hand/left/output/haptic", XR_ACTION_TYPE_VIBRATION_OUTPUT}, {"/user/hand/right/output/haptic", XR_ACTION_TYPE_VIBRATION_OUTPUT}, {"/user/hand/left/input/menu/click", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/right/input/menu/click", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/left/input/select/click", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/right/input/select/click", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"/user/hand/left/input/grip/pose", XR_ACTION_TYPE_POSE_INPUT}, {"/user/hand/right/input/grip/pose", XR_ACTION_TYPE_POSE_INPUT}, {"/user/hand/left/input/aim/pose", XR_ACTION_TYPE_POSE_INPUT}, {"/user/hand/right/input/aim/pose", XR_ACTION_TYPE_POSE_INPUT}, {"/user/hand/left/input/palm_ext/pose", XR_ACTION_TYPE_POSE_INPUT}, {"/user/hand/right/input/palm_ext/pose", XR_ACTION_TYPE_POSE_INPUT}, {"/user/hand/left/input/pinch_ext/pose", XR_ACTION_TYPE_POSE_INPUT}, {"/user/hand/right/input/pinch_ext/pose", XR_ACTION_TYPE_POSE_INPUT}, {"/user/hand/left/input/poke_ext_ext/pose", XR_ACTION_TYPE_POSE_INPUT}, {"/user/hand/right/input/poke_ext/pose", XR_ACTION_TYPE_POSE_INPUT}, }; bool session_running = false; bool session_focused = false; bool session_visible = false; bool debug_extensions_found = false; std::atomic exit_requested = false; std::string server_address; std::mutex scene_stack_lock; std::vector> scene_stack; std::shared_ptr last_scene; void loop(); static VkBool32 vulkan_debug_report_callback(VkDebugReportFlagsEXT flags, VkDebugReportObjectTypeEXT objectType, uint64_t object, size_t location, int32_t messageCode, const char * pLayerPrefix, const char * pMessage, void * pUserData); std::unordered_set debug_report_ignored_objects; std::unordered_map debug_report_object_name; public: application(application_info info); application(const application &) = delete; ~application(); static bool is_session_running() { return instance_->session_running; } static bool is_focused() { return instance_->session_focused; } static bool is_visible() { return instance_->session_visible; } static bool is_exit_requested() { return instance_->exit_requested; }; static void poll_actions(); static bool read_action(XrAction action, float & value, XrTime& last_change_time); static bool read_action(XrAction action, bool & value, XrTime& last_change_time); static bool read_action(XrAction action, XrVector2f & value, XrTime& last_change_time); static void haptic_start(XrAction action, XrPath subpath, int64_t duration, float frequency, float amplitude); static void haptic_stop(XrAction action, XrPath subpath); static const std::vector> & inputs() { return instance_->actions; } static std::pair get_action(const std::string & name); static XrPath string_to_path(const std::string & s) { return instance_->xr_instance.string_to_path(s); } static std::string path_to_string(XrPath p) { return instance_->xr_instance.path_to_string(p); } void run(); void process_binding_action(std::vector & bindings, const char * name, const XrActionType & type); static void push_scene(std::shared_ptr); template static void push_scene(Args &&... args) { std::unique_lock _{instance_->scene_stack_lock}; instance_->scene_stack.push_back(std::make_shared(std::forward(args)...)); } static void pop_scene(); static std::shared_ptr current_scene(); static application & instance() { assert(instance_ != nullptr); return *instance_; } template static T get_vulkan_proc(const char * proc_name) { auto proc = vkGetInstanceProcAddr(instance_->vk_instance, proc_name); if (!proc) throw std::runtime_error(std::string("Cannot find Vulkan function ") + proc_name); return (T)proc; } static XrSpace view() { return instance_->view_space; }; static XrSpace left_grip() { return instance_->action_spaces[0]; }; static XrSpace left_aim() { return instance_->action_spaces[1]; }; static XrSpace right_grip() { return instance_->action_spaces[2]; }; static XrSpace right_aim() { return instance_->action_spaces[3]; }; static void ignore_debug_reports_for(void * object) { #ifndef NDEBUG instance_->debug_report_ignored_objects.emplace((uint64_t)object); #endif } static void unignore_debug_reports_for(void * object) { #ifndef NDEBUG instance_->debug_report_ignored_objects.erase((uint64_t)object); #endif } static void set_debug_reports_name(void * object, std::string name) { #ifndef NDEBUG instance_->debug_report_object_name[(uint64_t)object] = std::move(name); #endif } static XrTime now() { return instance_->xr_instance.now(); } static uint32_t queue_family_index() { return instance_->vk_queue_family_index; } const std::string& get_server_address() const { return server_address; } };