/* * 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 "render/image_loader.h" #include "render/scene_loader.h" #include "render/scene_renderer.h" #include "utils/cache.h" #include "xr/actionset.h" #include "xr/instance.h" #include "xr/session.h" #include "xr/swapchain.h" #include "xr/system.h" #include #include #include #include #include #include #include #include #include #include // See http://www.nirfriedman.com/2018/04/29/unforgettable-factory/ // for automatic registration of scenes class scene { public: struct action_binding { std::string action_name; std::string input_source; }; struct suggested_binding { std::vector profile_names; std::vector paths; }; struct meta { // Filled by the scene class std::string name; std::vector> actions; std::vector bindings; // Filled by the application class xr::actionset actionset; std::unordered_map> actions_by_name; std::unordered_map spaces_by_name; }; private: // Force derived classes to inherit from scene_impl instead of scene class key {}; template friend class scene_impl; friend class application; protected: static std::vector scene_registry; xr::instance & instance; xr::system & system; xr::session & session; XrViewConfigurationType viewconfig; bool focused = false; public: vk::raii::Instance & vk_instance; vk::raii::Device & device; vk::raii::PhysicalDevice & physical_device; thread_safe & queue; uint32_t queue_family_index; protected: vk::raii::CommandPool & commandpool; const meta & current_meta; std::shared_ptr renderer; using gltf_cache_type = utils::cache; std::shared_ptr gltf_cache; using image_cache_type = utils::cache; std::shared_ptr image_cache; vk::Format swapchain_format; vk::Format depth_format; bool composition_layer_depth_test_supported; bool composition_layer_color_scale_bias_supported; std::pair get_action(const std::string & name) { return current_meta.actions_by_name.at(name); } XrSpace get_action_space(const std::string & name) { return current_meta.spaces_by_name.at(name); } // Helper functions static glm::mat4 projection_matrix(XrFovf fov, float zn = 0.02); static glm::mat4 view_matrix(XrPosef pose); // Layer rendering XrEnvironmentBlendMode blend_mode; XrTime predicted_display_time; struct layer { static const int max_views = 2; std::variant composition_layer; beman::inplace_vector::inplace_vector color_views; // Used by XrCompositionLayerProjection beman::inplace_vector::inplace_vector depth_views; // Used by XrCompositionLayerProjection std::optional color_scale_bias; std::optional depth_test; std::optional settings; }; std::vector layers; std::vector openxr_layers; struct swapchain_entry { vk::Format format; int32_t width; int32_t height; int sample_count; uint32_t array_size; bool used; xr::swapchain swapchain; }; std::vector swapchains; // The returned reference is valid until the next call to get_swapchain xr::swapchain & get_swapchain(vk::Format format, int32_t width, int32_t height, int sample_count, uint32_t array_size); void clear_swapchains(); void render_start(bool passthrough, XrTime predicted_display_time); void add_projection_layer( XrCompositionLayerFlags flags, XrSpace space, std::span color_views, std::span depth_views = {}); void add_quad_layer( XrCompositionLayerFlags flags, XrSpace space, XrEyeVisibility eyeVisibility, XrSwapchainSubImage subImage, XrPosef pose, XrExtent2Df size); void render_world( XrCompositionLayerFlags flags, XrSpace space, std::span views, uint32_t width, uint32_t height, bool keep_depth_buffer, uint32_t layer_mask, XrColor4f clear_color, bool render_debug_draws = false); void set_color_scale_bias(XrColor4f scale, XrColor4f bias); void set_depth_test(bool write, XrCompareOpFB op); void set_layer_settings(XrCompositionLayerSettingsFlagsFB flags); void render_end(); virtual void on_unfocused(); virtual void on_focused(); public: scene(key, const meta &, std::span supported_color_formats, std::span supported_depth_formats, scene * parent_scene); virtual ~scene(); void set_focused(bool status); virtual void render(const XrFrameState &) = 0; virtual void on_xr_event(const xr::event &); virtual bool on_input_key_down(uint8_t key_code); virtual bool on_input_key_up(uint8_t key_code); virtual bool on_input_mouse_move(float x, float y); virtual bool on_input_button_down(uint8_t button); virtual bool on_input_button_up(uint8_t button); virtual bool on_input_scroll(float h, float v); entt::registry world; std::shared_ptr create_material(std::function init = {}) const; std::shared_ptr load_gltf(const std::filesystem::path & path, std::function progress_cb = {}); void unload_gltf(const std::filesystem::path & path); std::pair add_gltf(std::shared_ptr gltf, uint32_t layer_mask = -1); std::pair add_gltf(const std::filesystem::path & path, uint32_t layer_mask = -1); void clear_texture_cache() { gltf_cache->loader().clear_texture_cache(); } void clear_gltf_cache() { gltf_cache->clear(); } void remove(entt::entity entity); }; template class scene_impl : public scene { friend T; friend scene; // Magically register all classes that derive from scene_impl static bool register_scene() { scene_registry.emplace_back(&T::get_meta_scene()); return true; } static inline bool registered = scene_impl::register_scene(); scene_impl(std::span supported_color_formats, std::span supported_depth_formats, scene & parent_scene) : scene(key{}, T::get_meta_scene(), supported_color_formats, supported_depth_formats, &parent_scene) { (void)registered; } scene_impl(std::span supported_color_formats, std::span supported_depth_formats) : scene(key{}, T::get_meta_scene(), supported_color_formats, supported_depth_formats, nullptr) { (void)registered; } };