From b8c08f1febfd0c4a63c9867fba81371a2f9ed497 Mon Sep 17 00:00:00 2001 From: Guillaume Meunier Date: Wed, 10 Sep 2025 21:57:32 +0200 Subject: [PATCH] Add support for fragment density map --- client/application.cpp | 5 +++ client/scene.cpp | 2 +- client/scenes/lobby.cpp | 2 +- client/scenes/stream.cpp | 3 +- client/xr/foveation_profile.cpp | 38 ++++++++++++++++++++ client/xr/foveation_profile.h | 33 +++++++++++++++++ client/xr/swapchain.cpp | 64 +++++++++++++++++++++++++++------ client/xr/swapchain.h | 16 ++++++++- 8 files changed, 148 insertions(+), 15 deletions(-) create mode 100644 client/xr/foveation_profile.cpp create mode 100644 client/xr/foveation_profile.h diff --git a/client/application.cpp b/client/application.cpp index da994655..6f899697 100644 --- a/client/application.cpp +++ b/client/application.cpp @@ -761,6 +761,7 @@ void application::initialize_vulkan() vk_device_extensions.push_back(VK_KHR_SAMPLER_YCBCR_CONVERSION_EXTENSION_NAME); optional_device_extensions.emplace(VK_IMG_FILTER_CUBIC_EXTENSION_NAME); optional_device_extensions.emplace(VK_KHR_TIMELINE_SEMAPHORE_EXTENSION_NAME); + optional_device_extensions.emplace(VK_EXT_FRAGMENT_DENSITY_MAP_EXTENSION_NAME); #ifdef __ANDROID__ vk_device_extensions.push_back(VK_ANDROID_EXTERNAL_MEMORY_ANDROID_HARDWARE_BUFFER_EXTENSION_NAME); @@ -1215,6 +1216,10 @@ void application::initialize() opt_extensions.push_back(XR_EXT_USER_PRESENCE_EXTENSION_NAME); opt_extensions.push_back(XR_KHR_VISIBILITY_MASK_EXTENSION_NAME); opt_extensions.push_back(XR_FB_COMPOSITION_LAYER_SETTINGS_EXTENSION_NAME); + opt_extensions.push_back(XR_FB_SWAPCHAIN_UPDATE_STATE_EXTENSION_NAME); + opt_extensions.push_back(XR_FB_FOVEATION_EXTENSION_NAME); + opt_extensions.push_back(XR_FB_FOVEATION_CONFIGURATION_EXTENSION_NAME); + opt_extensions.push_back(XR_FB_FOVEATION_VULKAN_EXTENSION_NAME); for (const auto & i: interaction_profiles) opt_extensions.insert(opt_extensions.end(), i.required_extensions.begin(), i.required_extensions.end()); diff --git a/client/scene.cpp b/client/scene.cpp index 8da5e1aa..887f98ad 100644 --- a/client/scene.cpp +++ b/client/scene.cpp @@ -277,7 +277,7 @@ xr::swapchain & scene::get_swapchain(vk::Format format, int32_t width, int32_t h .sample_count = sample_count, .array_size = array_size, .used = true, - .swapchain = xr::swapchain(session, device, format, width, height, sample_count, array_size), + .swapchain = xr::swapchain(instance, session, device, format, width, height, sample_count, array_size), }; spdlog::info("Created swapchain"); diff --git a/client/scenes/lobby.cpp b/client/scenes/lobby.cpp index f73ba678..4f36ec37 100644 --- a/client/scenes/lobby.cpp +++ b/client/scenes/lobby.cpp @@ -906,7 +906,7 @@ void scenes::lobby::on_focused() .z_index = constants::lobby::zindex_recenter_tip, }}; - xr::swapchain swapchain_imgui(session, device, swapchain_format, 3000, 1300); + xr::swapchain swapchain_imgui(instance, session, device, swapchain_format, 3000, 1300); imgui_ctx.emplace( physical_device, diff --git a/client/scenes/stream.cpp b/client/scenes/stream.cpp index 74077e39..70c8610d 100644 --- a/client/scenes/stream.cpp +++ b/client/scenes/stream.cpp @@ -447,6 +447,7 @@ void scenes::stream::on_focused() }; xr::swapchain swapchain_imgui( + instance, session, device, swapchain_format, @@ -1146,7 +1147,7 @@ void scenes::stream::setup_reprojection_swapchain(uint32_t swapchain_width, uint auto views = system.view_configuration_views(viewconfig); - swapchain = xr::swapchain(session, device, swapchain_format, swapchain_width, swapchain_height, 1, views.size()); + swapchain = xr::swapchain(instance, session, device, swapchain_format, swapchain_width, swapchain_height, 1, views.size()); spdlog::info("Created stream swapchain: {}x{}", swapchain.width(), swapchain.height()); for (auto view: views) { diff --git a/client/xr/foveation_profile.cpp b/client/xr/foveation_profile.cpp new file mode 100644 index 00000000..cd75c855 --- /dev/null +++ b/client/xr/foveation_profile.cpp @@ -0,0 +1,38 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2025 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 "foveation_profile.h" +#include "xr/instance.h" +#include "xr/session.h" + +xr::foveation_profile::foveation_profile(instance & inst, session & s, XrFoveationLevelFB level, float verticalOffsetDegrees, bool dynamic) : + handle(inst.get_proc("xrDestroyFoveationProfileFB")) +{ + auto create = inst.get_proc("xrCreateFoveationProfileFB"); + XrFoveationLevelProfileCreateInfoFB level_info{ + .type = XR_TYPE_FOVEATION_LEVEL_PROFILE_CREATE_INFO_FB, + .level = level, + .verticalOffset = verticalOffsetDegrees, + .dynamic = dynamic ? XR_FOVEATION_DYNAMIC_LEVEL_ENABLED_FB : XR_FOVEATION_DYNAMIC_DISABLED_FB, + }; + XrFoveationProfileCreateInfoFB info{ + .type = XR_TYPE_FOVEATION_PROFILE_CREATE_INFO_FB, + .next = &level_info, + }; + create(s, &info, &id); +} diff --git a/client/xr/foveation_profile.h b/client/xr/foveation_profile.h new file mode 100644 index 00000000..2a3ba0c9 --- /dev/null +++ b/client/xr/foveation_profile.h @@ -0,0 +1,33 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2025 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 "utils/handle.h" +#include + +namespace xr +{ +class instance; +class session; +class foveation_profile : public utils::handle +{ +public: + foveation_profile(instance & inst, session & s, XrFoveationLevelFB, float verticalOffsetDegrees, bool dynamic); +}; +} // namespace xr diff --git a/client/xr/swapchain.cpp b/client/xr/swapchain.cpp index 667215f2..4c3dcf29 100644 --- a/client/xr/swapchain.cpp +++ b/client/xr/swapchain.cpp @@ -22,9 +22,24 @@ #include "details/enumerate.h" #include "session.h" -xr::swapchain::swapchain(xr::session & s, vk::raii::Device & device, vk::Format format, int32_t width, int32_t height, int sample_count, uint32_t array_size) +xr::swapchain::swapchain( + xr::instance & inst, + xr::session & s, + vk::raii::Device & device, + vk::Format format, + int32_t width, + int32_t height, + int sample_count, + uint32_t array_size, + XrFoveationProfileFB foveated) : + width_(width), + height_(height), + sample_count_(sample_count), + format_(format) { assert(sample_count == 1); + if (foveated) + update = inst.get_proc("xrUpdateSwapchainFB"); XrSwapchainUsageFlags usage_flags; @@ -43,8 +58,14 @@ xr::swapchain::swapchain(xr::session & s, vk::raii::Device & device, vk::Format break; } + XrSwapchainCreateInfoFoveationFB foveation_info{ + .type = XR_TYPE_SWAPCHAIN_CREATE_INFO_FOVEATION_FB, + .flags = XR_SWAPCHAIN_CREATE_FOVEATION_FRAGMENT_DENSITY_MAP_BIT_FB, + }; + XrSwapchainCreateInfo create_info{ .type = XR_TYPE_SWAPCHAIN_CREATE_INFO, + .next = foveated ? &foveation_info : nullptr, .createFlags = 0, .usageFlags = usage_flags, .format = static_cast(format), @@ -56,19 +77,31 @@ xr::swapchain::swapchain(xr::session & s, vk::raii::Device & device, vk::Format .mipCount = 1, }; - width_ = width; - height_ = height; - sample_count_ = sample_count; - format_ = format; - CHECK_XR(xrCreateSwapchain(s, &create_info, &id)); - std::vector array = - details::enumerate(xrEnumerateSwapchainImages, id); + std::tuple, std::vector> array; + auto & images = std::get<0>(array); + auto & foveation = std::get<1>(array); + if (foveated) + { + update_foveation(foveated); + details::enumerate2(xrEnumerateSwapchainImages, array, id); + assert(images.size() == foveation.size()); + } + else + { + details::enumerate(xrEnumerateSwapchainImages, images, id); + } - images_.resize(array.size()); - for (uint32_t i = 0; i < array.size(); i++) - images_[i].image = array[i].image; + images_.resize(images.size()); + for (uint32_t i = 0; i < images.size(); i++) + { + images_[i].image = images[i].image; + if (foveated) + { + images_[i].foveation = foveation[i].image; + } + } } int xr::swapchain::acquire() @@ -106,3 +139,12 @@ void xr::swapchain::release() auto lock = application::get_queue().lock(); CHECK_XR(xrReleaseSwapchainImage(id, &release_info)); } + +void xr::swapchain::update_foveation(XrFoveationProfileFB foveation) +{ + XrSwapchainStateFoveationFB update_info{ + .type = XR_TYPE_SWAPCHAIN_STATE_FOVEATION_FB, + .profile = foveation, + }; + CHECK_XR(update(id, (XrSwapchainStateBaseHeaderFB *)&update_info)); +} diff --git a/client/xr/swapchain.h b/client/xr/swapchain.h index 5a902f4d..d6f0446c 100644 --- a/client/xr/swapchain.h +++ b/client/xr/swapchain.h @@ -30,6 +30,7 @@ namespace xr { +class instance; class session; class swapchain : public utils::handle @@ -38,9 +39,11 @@ public: struct image { vk::Image image{}; + vk::Image foveation{}; }; private: + PFN_xrUpdateSwapchainFB update; int32_t width_; int32_t height_; int sample_count_; @@ -50,7 +53,16 @@ private: public: swapchain() = default; - swapchain(session &, vk::raii::Device & device, vk::Format format, int32_t width, int32_t height, int sample_count = 1, uint32_t array_size = 1); + swapchain( + instance &, + session &, + vk::raii::Device & device, + vk::Format format, + int32_t width, + int32_t height, + int sample_count = 1, + uint32_t array_size = 1, + XrFoveationProfileFB foveation = XR_NULL_HANDLE); int32_t width() const { @@ -84,5 +96,7 @@ public: int acquire(); bool wait(XrDuration timeout = XR_INFINITE_DURATION); void release(); + + void update_foveation(XrFoveationProfileFB foveation); }; } // namespace xr