client: support XR_META_foveation_eye_tracked (#1123)

This commit is contained in:
galister
2026-10-22 18:52:55 +02:00
committed by GitHub
parent 2e60fa75e4
commit 76da2e1cb7
20 changed files with 896 additions and 266 deletions
+25 -1
View File
@@ -899,6 +899,8 @@ void application::initialize_vulkan()
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);
optional_device_extensions.emplace(VK_EXT_FRAGMENT_DENSITY_MAP_2_EXTENSION_NAME);
optional_device_extensions.emplace(VK_QCOM_FRAGMENT_DENSITY_MAP_OFFSET_EXTENSION_NAME);
#if WIVRN_USE_V4L2
// V4L2 decoders expose their output through DMA-buf
@@ -1054,7 +1056,9 @@ void application::initialize_vulkan()
vk::PhysicalDeviceMultiviewFeaturesKHR{
.multiview = true,
},
vk::PhysicalDeviceIndexTypeUint8FeaturesEXT{}};
vk::PhysicalDeviceIndexTypeUint8FeaturesEXT{},
vk::PhysicalDeviceFragmentDensityMapFeaturesEXT{},
vk::PhysicalDeviceFragmentDensityMapOffsetFeaturesQCOM{}};
auto check_feature_flag = [&](auto feature_flag, const char * extension_name) -> bool {
using FeatureStruct = class_from_member_t<decltype(feature_flag)>;
@@ -1074,6 +1078,14 @@ void application::initialize_vulkan()
check_feature_flag(&vk::PhysicalDeviceTimelineSemaphoreFeaturesKHR::timelineSemaphore, VK_KHR_TIMELINE_SEMAPHORE_EXTENSION_NAME);
check_feature_flag(&vk::PhysicalDeviceIndexTypeUint8FeaturesEXT::indexTypeUint8, VK_EXT_INDEX_TYPE_UINT8_EXTENSION_NAME);
const bool fragment_density_map_supported =
check_feature_flag(&vk::PhysicalDeviceFragmentDensityMapFeaturesEXT::fragmentDensityMap, VK_EXT_FRAGMENT_DENSITY_MAP_EXTENSION_NAME);
const bool fragment_density_map2_supported =
utils::contains(vk_device_extensions, VK_EXT_FRAGMENT_DENSITY_MAP_2_EXTENSION_NAME);
const bool fragment_density_map_offset_supported =
check_feature_flag(&vk::PhysicalDeviceFragmentDensityMapOffsetFeaturesQCOM::fragmentDensityMapOffset, VK_QCOM_FRAGMENT_DENSITY_MAP_OFFSET_EXTENSION_NAME);
foveation_vulkan_supported = fragment_density_map_supported and fragment_density_map2_supported and fragment_density_map_offset_supported;
spdlog::info(" Vulkan eye-tracked foveation support: {}", foveation_vulkan_supported);
vk_device = xr_system_id.create_device(vk_physical_device, device_create_info.get());
*vk_queue.lock() = vk_device.getQueue(vk_queue_family_index, 0);
@@ -1382,6 +1394,9 @@ void application::initialize()
XR_FB_COMPOSITION_LAYER_SETTINGS_EXTENSION_NAME,
XR_FB_DISPLAY_REFRESH_RATE_EXTENSION_NAME,
XR_FB_FACE_TRACKING2_EXTENSION_NAME,
XR_FB_FOVEATION_EXTENSION_NAME,
XR_FB_FOVEATION_CONFIGURATION_EXTENSION_NAME,
XR_FB_FOVEATION_VULKAN_EXTENSION_NAME,
XR_FB_PASSTHROUGH_EXTENSION_NAME,
XR_FB_SWAPCHAIN_UPDATE_STATE_EXTENSION_NAME,
@@ -1392,8 +1407,10 @@ void application::initialize()
XR_META_BODY_TRACKING_FIDELITY_EXTENSION_NAME,
XR_META_BODY_TRACKING_FULL_BODY_EXTENSION_NAME,
XR_META_FOVEATION_EYE_TRACKED_EXTENSION_NAME,
XR_META_LOCAL_DIMMING_EXTENSION_NAME,
XR_META_BOUNDARY_VISIBILITY_EXTENSION_NAME,
XR_META_VULKAN_SWAPCHAIN_CREATE_INFO_EXTENSION_NAME,
};
for (const auto & i: interaction_profiles)
@@ -1444,6 +1461,13 @@ void application::initialize()
eye_gaze_supported = eye_gaze_properties.supportsEyeGazeInteraction;
}
if (xr_instance.has_extension(XR_META_FOVEATION_EYE_TRACKED_EXTENSION_NAME))
{
XrSystemFoveationEyeTrackedPropertiesMETA foveation_properties = xr_system_id.foveation_eye_tracked_properties();
spdlog::info(" Eye tracked foveation center support: {}", (bool)foveation_properties.supportsFoveationEyeTracked);
foveation_center_supported = foveation_properties.supportsFoveationEyeTracked;
}
if (xr_instance.has_extension(XR_FB_COMPOSITION_LAYER_SETTINGS_EXTENSION_NAME))
{
spdlog::info(" OpenXR post-processing extension support: true");
+12
View File
@@ -131,6 +131,8 @@ private:
std::vector<std::pair<XrPath, xr::space>> generic_trackers;
bool eye_gaze_supported = false;
bool foveation_center_supported = false;
bool foveation_vulkan_supported = false;
bool openxr_post_processing_supported = false;
@@ -407,6 +409,16 @@ public:
return instance().eye_gaze_supported;
}
static bool get_foveation_center_supported()
{
return instance().foveation_center_supported;
}
static bool get_foveation_vulkan_supported()
{
return instance().foveation_vulkan_supported;
}
static bool get_openxr_post_processing_supported()
{
return instance().openxr_post_processing_supported;
+487
View File
@@ -0,0 +1,487 @@
/*
* WiVRn VR streaming
* Copyright (C) 2026 galister <galister-dev@pm.me>
*
* 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 <https://www.gnu.org/licenses/>.
*/
#include "foveation.h"
#include "application.h"
#include "xr/instance.h"
#include "xr/session.h"
#include "xr/system.h"
#include <algorithm>
#include <cassert>
#include <cmath>
#include <spdlog/spdlog.h>
#include <stdexcept>
namespace
{
float convergence_angle(float distance, float eye_x, float yaw)
{
const float target_x = distance * std::sin(yaw);
const float target_z = distance * std::cos(yaw);
return std::atan2(target_x - eye_x, target_z);
}
XrVector2f yaw_pitch(const XrQuaternionf & q)
{
const float sine_theta = std::clamp(-2.0f * (q.y * q.z - q.w * q.x), -1.0f, 1.0f);
const float pitch = std::asin(sine_theta);
if (std::abs(sine_theta) > 0.99999f)
{
const float scale = std::copysign(2.0f, sine_theta);
return {scale * std::atan2(-q.z, q.w), pitch};
}
return {
std::atan2(2.0f * (q.x * q.z + q.w * q.y),
q.w * q.w - q.x * q.x - q.y * q.y + q.z * q.z),
pitch};
}
float ndc_to_angle(float center, float a0, float a1)
{
const float u = std::clamp(center * 0.5f + 0.5f, 0.0f, 1.0f);
const float tangent = std::lerp(std::tan(a0), std::tan(a1), u);
return std::atan(tangent);
}
client_foveation::angles center_to_angles(const std::array<XrVector2f, 2> & center, std::span<const XrFovf> fovs)
{
assert(fovs.size() == 2);
client_foveation::angles result{};
for (size_t i = 0; i < result.size(); ++i)
{
result[i].x = ndc_to_angle(center[i].x, fovs[i].angleLeft, fovs[i].angleRight);
result[i].y = ndc_to_angle(center[i].y, fovs[i].angleUp, fovs[i].angleDown);
}
return result;
}
bool fov_equal(const XrFovf & a, const XrFovf & b, const float epsilon)
{
return std::abs(a.angleLeft - b.angleLeft) <= epsilon and
std::abs(a.angleRight - b.angleRight) <= epsilon and
std::abs(a.angleUp - b.angleUp) <= epsilon and
std::abs(a.angleDown - b.angleDown) <= epsilon;
}
client_foveation::angles target_angles(
float yaw,
float pitch,
float convergence_distance,
std::span<const XrView> views)
{
assert(views.size() == 2);
client_foveation::angles result{};
for (size_t i = 0; i < result.size(); ++i)
{
result[i].x = convergence_angle(convergence_distance, views[i].pose.position.x, yaw);
result[i].y = pitch;
}
return result;
}
std::optional<client_foveation::angles> eye_gaze_angles(
XrSpace eye_gaze_space,
XrSpace view_space,
XrTime time,
XrViewStateFlags view_flags,
std::span<const XrView> views)
{
if (not eye_gaze_space or not view_space or views.size() != 2)
return {};
XrSpaceLocation location{
.type = XR_TYPE_SPACE_LOCATION,
};
XrResult res = xrLocateSpace(eye_gaze_space, view_space, time, &location);
if (XR_FAILED(res) or
not(location.locationFlags & XR_SPACE_LOCATION_ORIENTATION_VALID_BIT) or
not(location.locationFlags & XR_SPACE_LOCATION_ORIENTATION_TRACKED_BIT))
return {};
if (not(view_flags & XR_VIEW_STATE_POSITION_VALID_BIT))
return {};
const XrVector2f gaze = yaw_pitch(location.pose.orientation);
return target_angles(-gaze.x, gaze.y, client_foveation::default_convergence_distance, views);
}
} // namespace
namespace client_foveation
{
center::center(xr::instance & inst, xr::system & sys, xr::session & xr_session, vk::raii::Device & vk_device, vk::raii::CommandPool & vk_commandpool, thread_safe<vk::raii::Queue> & vk_queue, vk::Format format) :
instance(inst),
session(xr_session),
device(vk_device),
commandpool(vk_commandpool),
queue(vk_queue),
swapchain_format(format)
{
if (application::get_foveation_center_supported() and
application::get_foveation_vulkan_supported() and
inst.has_extension(XR_META_FOVEATION_EYE_TRACKED_EXTENSION_NAME) and
inst.has_extension(XR_FB_FOVEATION_EXTENSION_NAME) and
inst.has_extension(XR_FB_FOVEATION_CONFIGURATION_EXTENSION_NAME) and
inst.has_extension(XR_FB_FOVEATION_VULKAN_EXTENSION_NAME) and
inst.has_extension(XR_FB_SWAPCHAIN_UPDATE_STATE_EXTENSION_NAME) and
inst.has_extension(XR_META_VULKAN_SWAPCHAIN_CREATE_INFO_EXTENSION_NAME) and
sys.foveation_eye_tracked_properties().supportsFoveationEyeTracked)
{
xrCreateFoveationProfileFB = inst.get_proc<PFN_xrCreateFoveationProfileFB>("xrCreateFoveationProfileFB");
xrDestroyFoveationProfileFB = inst.get_proc<PFN_xrDestroyFoveationProfileFB>("xrDestroyFoveationProfileFB");
xrUpdateSwapchainFB = inst.get_proc<PFN_xrUpdateSwapchainFB>("xrUpdateSwapchainFB");
xrGetFoveationEyeTrackedStateMETA = inst.get_proc<PFN_xrGetFoveationEyeTrackedStateMETA>("xrGetFoveationEyeTrackedStateMETA");
if (xrCreateFoveationProfileFB and xrDestroyFoveationProfileFB and
xrUpdateSwapchainFB and xrGetFoveationEyeTrackedStateMETA)
{
XrFoveationEyeTrackedProfileCreateInfoMETA eye_tracked_info{
.type = XR_TYPE_FOVEATION_EYE_TRACKED_PROFILE_CREATE_INFO_META,
.flags = 0,
};
XrFoveationLevelProfileCreateInfoFB level_info{
.type = XR_TYPE_FOVEATION_LEVEL_PROFILE_CREATE_INFO_FB,
.next = &eye_tracked_info,
.level = XR_FOVEATION_LEVEL_LOW_FB,
.verticalOffset = 0,
.dynamic = XR_FOVEATION_DYNAMIC_DISABLED_FB,
};
XrFoveationProfileCreateInfoFB profile_info{
.type = XR_TYPE_FOVEATION_PROFILE_CREATE_INFO_FB,
.next = &level_info,
};
XrFoveationProfileFB raw_profile = XR_NULL_HANDLE;
XrResult profile_result = xrCreateFoveationProfileFB(session, &profile_info, &raw_profile);
if (XR_SUCCEEDED(profile_result))
foveation_profile.emplace(raw_profile, xrDestroyFoveationProfileFB);
}
}
if (foveation_profile.has_value())
spdlog::info("XR_META_foveation_eye_tracked will be used for this stream");
else if (application::get_eye_gaze_supported())
spdlog::info("Eye gaze tracking will be used for this stream");
else
spdlog::info("Static foveation will be used for this stream");
auto [flags, views] = session.locate_views(
XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO,
instance.now(),
application::space(xr::spaces::view));
(void)flags;
assert(views.size() == headset_fov.size());
for (size_t i = 0; i < headset_fov.size(); ++i)
headset_fov[i] = views[i].fov;
const auto & config = application::get_config();
set_manual_mode({
.enabled = config.override_foveation_enable,
.pitch = config.override_foveation_pitch,
.distance = config.override_foveation_distance,
});
}
// member order ensures swapchain is dropped before foveation profile
center::~center() = default;
bool center::supports_meta() const
{
return foveation_profile.has_value();
}
manual_override center::get_manual_mode() const
{
return *manual_mode_.lock();
}
void center::set_manual_mode(manual_override mode)
{
*manual_mode_.lock() = mode;
}
bool center::is_cropped(std::span<const XrFovf> submitted_fov, std::span<const bool> valid) const
{
assert(submitted_fov.size() == view_count and valid.size() == view_count);
for (size_t i = 0; i < view_count; ++i)
{
// generous epsilon of ~6° per-side to filter out false positives
if (valid[i] and not fov_equal(submitted_fov[i], headset_fov[i], 0.1f))
return true;
}
return false;
}
bool center::needs_swapchain_recreation(bool cropped) const
{
return supports_meta() and (not cropped_mode or *cropped_mode != cropped);
}
bool center::use_meta_swapchain(bool cropped) const
{
return supports_meta() and not cropped;
}
void center::set_cropped(bool cropped)
{
if (not cropped_mode or *cropped_mode != cropped)
spdlog::info("Stream FOV mode: {}", cropped ? "cropped" : "full");
cropped_mode = cropped;
}
void center::on_swapchain_recreated(bool cropped)
{
dummy_swapchain = {};
if (supports_meta() and cropped)
create_dummy_swapchain();
set_cropped(cropped);
}
bool center::update_meta_swapchain(XrSwapchain swapchain) const
{
if (not supports_meta() or not swapchain)
return false;
XrSwapchainStateFoveationFB swapchain_state{
.type = XR_TYPE_SWAPCHAIN_STATE_FOVEATION_FB,
.flags = 0,
.profile = *foveation_profile,
};
XrResult update_result = xrUpdateSwapchainFB(
swapchain,
reinterpret_cast<const XrSwapchainStateBaseHeaderFB *>(&swapchain_state));
return XR_SUCCEEDED(update_result);
}
std::optional<angles> center::meta_angles(XrSwapchain swapchain, std::span<const XrFovf> fov) const
{
if (not update_meta_swapchain(swapchain))
return {};
XrFoveationEyeTrackedStateMETA state{
.type = XR_TYPE_FOVEATION_EYE_TRACKED_STATE_META,
};
XrResult result = xrGetFoveationEyeTrackedStateMETA(session, &state);
if (XR_FAILED(result) or not(state.flags & XR_FOVEATION_EYE_TRACKED_STATE_VALID_BIT_META))
return {};
std::array<XrVector2f, view_count> ndc_center{state.foveationCenter[0], state.foveationCenter[1]};
return center_to_angles(ndc_center, fov);
}
void center::update(std::span<const XrFovf> submitted_fov, std::span<const bool> valid, XrSwapchain stream_swapchain, XrTime display_time)
{
assert(submitted_fov.size() == view_count and valid.size() == view_count);
layer_.reset();
packet_.reset();
XrSpace view_space = application::space(xr::spaces::view);
auto [view_flags, views] = session.locate_views(
XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO, display_time, view_space);
constexpr XrViewStateFlags layer_flags =
XR_VIEW_STATE_POSITION_VALID_BIT | XR_VIEW_STATE_ORIENTATION_VALID_BIT;
if (supports_meta() and cropped_mode.value_or(false) and dummy_swapchain and
views.size() == view_count and (view_flags & layer_flags) == layer_flags)
{
projection_layer layer{
.flags = XR_COMPOSITION_LAYER_BLEND_TEXTURE_SOURCE_ALPHA_BIT,
.space = view_space,
};
for (size_t i = 0; i < view_count; ++i)
{
layer.views[i] = {
.type = XR_TYPE_COMPOSITION_LAYER_PROJECTION_VIEW,
.pose = views[i].pose,
.fov = headset_fov[i],
.subImage = {
.swapchain = dummy_swapchain,
.imageRect = {
.offset = {0, 0},
.extent = {dummy_swapchain_size, dummy_swapchain_size},
},
.imageArrayIndex = uint32_t(i),
},
};
}
layer_ = layer;
}
if (views.size() != view_count or not(view_flags & XR_VIEW_STATE_POSITION_VALID_BIT))
return;
// dummy layer is always full FOV
std::array<XrFovf, view_count> effective_fov{};
for (size_t i = 0; i < view_count; ++i)
effective_fov[i] = valid[i] ? submitted_fov[i] : headset_fov[i];
const XrSwapchain meta_swapchain = cropped_mode.value_or(false) ? static_cast<XrSwapchain>(dummy_swapchain) : stream_swapchain;
const std::span<const XrFovf> meta_fov = cropped_mode.value_or(false) ? std::span<const XrFovf>(headset_fov) : std::span<const XrFovf>(effective_fov);
const auto manual = get_manual_mode();
std::optional<angles> angles;
if (manual.enabled)
{
if (supports_meta())
update_meta_swapchain(meta_swapchain);
angles = target_angles(0, manual.pitch, manual.distance, views);
}
else
{
if (supports_meta())
{
if (auto a = meta_angles(meta_swapchain, meta_fov))
angles = *a;
}
if (not angles and application::get_eye_gaze_supported())
{
if (auto a = eye_gaze_angles(application::space(xr::spaces::eye_gaze),
view_space,
display_time,
view_flags,
views))
angles = *a;
}
if (not angles)
angles = target_angles(0, default_foveation_pitch, default_convergence_distance, views);
}
// angles guaranteed to have a value at this point
packet_ = wivrn::from_headset::foveation{.angles = *angles};
}
std::optional<center::projection_layer> center::get_layer() const
{
return layer_;
}
std::optional<wivrn::from_headset::foveation> center::take_packet()
{
// A skipped render must not resend the previous frame's center.
auto result = packet_;
packet_.reset();
return result;
}
void center::create_dummy_swapchain()
{
device.waitIdle();
dummy_swapchain = xr::swapchain(
instance,
session,
device,
swapchain_format,
dummy_swapchain_size,
dummy_swapchain_size,
1,
view_count,
true,
true);
if (not update_meta_swapchain(dummy_swapchain))
throw std::runtime_error("Failed to apply eye-tracked foveation profile to dummy swapchain");
const int image_index = dummy_swapchain.acquire();
dummy_swapchain.wait();
auto clear_command_buffer = std::move(device.allocateCommandBuffers({
.commandPool = *commandpool,
.level = vk::CommandBufferLevel::ePrimary,
.commandBufferCount = 1,
})[0]);
auto clear_fence = device.createFence({});
clear_command_buffer.begin(vk::CommandBufferBeginInfo{
.flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit,
});
vk::ImageMemoryBarrier to_transfer{
.srcAccessMask = vk::AccessFlagBits::eNone,
.dstAccessMask = vk::AccessFlagBits::eTransferWrite,
.oldLayout = vk::ImageLayout::eUndefined,
.newLayout = vk::ImageLayout::eTransferDstOptimal,
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.image = dummy_swapchain.image(image_index),
.subresourceRange = {
.aspectMask = vk::ImageAspectFlagBits::eColor,
.baseMipLevel = 0,
.levelCount = 1,
.baseArrayLayer = 0,
.layerCount = view_count,
},
};
clear_command_buffer.pipelineBarrier(
vk::PipelineStageFlagBits::eTopOfPipe,
vk::PipelineStageFlagBits::eTransfer,
{},
{},
{},
to_transfer);
clear_command_buffer.clearColorImage(
dummy_swapchain.image(image_index),
vk::ImageLayout::eTransferDstOptimal,
vk::ClearColorValue{std::array<float, 4>{0, 0, 0, 0}},
to_transfer.subresourceRange);
vk::ImageMemoryBarrier to_runtime{
.srcAccessMask = vk::AccessFlagBits::eTransferWrite,
.dstAccessMask = vk::AccessFlagBits::eMemoryRead,
.oldLayout = vk::ImageLayout::eTransferDstOptimal,
.newLayout = vk::ImageLayout::eColorAttachmentOptimal,
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.image = dummy_swapchain.image(image_index),
.subresourceRange = to_transfer.subresourceRange,
};
clear_command_buffer.pipelineBarrier(
vk::PipelineStageFlagBits::eTransfer,
vk::PipelineStageFlagBits::eAllCommands,
{},
{},
{},
to_runtime);
clear_command_buffer.end();
vk::SubmitInfo submit_info;
submit_info.setCommandBuffers(*clear_command_buffer);
queue.lock()->submit(submit_info, *clear_fence);
if (device.waitForFences(*clear_fence, VK_TRUE, UINT64_MAX) == vk::Result::eTimeout)
throw std::runtime_error("Vulkan fence timeout while clearing foveation dummy swapchain");
dummy_swapchain.release();
spdlog::info(
"Created {}x{} eye-tracked foveation dummy swapchain",
dummy_swapchain_size,
dummy_swapchain_size);
}
} // namespace client_foveation
+112
View File
@@ -0,0 +1,112 @@
/*
* WiVRn VR streaming
* Copyright (C) 2026 galister <galister-dev@pm.me>
*
* 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 <https://www.gnu.org/licenses/>.
*/
#pragma once
#include "utils/handle.h"
#include "utils/thread_safe.h"
#include "wivrn_packets.h"
#include "xr/swapchain.h"
#include <array>
#include <optional>
#include <span>
#include <openxr/openxr.h>
namespace xr
{
class instance;
class session;
class system;
} // namespace xr
namespace client_foveation
{
using angles = std::array<XrVector2f, 2>;
inline constexpr float default_convergence_distance = 1.0f;
// Default foveation target is 1 m forward and 10 degrees below horizontal.
inline constexpr float default_foveation_pitch = -0.17453292519943295f; // -10 degrees
struct manual_override
{
bool enabled = false;
float pitch = default_foveation_pitch;
float distance = default_convergence_distance;
};
// Unified class for static, meta and eye-gaze based foveation.
class center
{
public:
static constexpr size_t view_count = 2;
static constexpr int32_t dummy_swapchain_size = 512;
struct projection_layer
{
XrCompositionLayerFlags flags;
XrSpace space;
std::array<XrCompositionLayerProjectionView, view_count> views;
};
center(xr::instance &, xr::system &, xr::session &, vk::raii::Device &, vk::raii::CommandPool &, thread_safe<vk::raii::Queue> &, vk::Format swapchain_format);
~center();
bool supports_meta() const;
manual_override get_manual_mode() const;
void set_manual_mode(manual_override mode);
bool is_cropped(std::span<const XrFovf> submitted_fov, std::span<const bool> valid) const;
bool needs_swapchain_recreation(bool cropped) const;
bool use_meta_swapchain(bool cropped) const;
void on_swapchain_recreated(bool cropped);
void set_cropped(bool cropped);
void update(std::span<const XrFovf> submitted_fov, std::span<const bool> valid, XrSwapchain stream_swapchain, XrTime display_time);
std::optional<projection_layer> get_layer() const;
std::optional<wivrn::from_headset::foveation> take_packet();
private:
mutable thread_safe<manual_override> manual_mode_{};
xr::instance & instance;
xr::session & session;
vk::raii::Device & device;
vk::raii::CommandPool & commandpool;
thread_safe<vk::raii::Queue> & queue;
vk::Format swapchain_format;
PFN_xrCreateFoveationProfileFB xrCreateFoveationProfileFB = nullptr;
PFN_xrDestroyFoveationProfileFB xrDestroyFoveationProfileFB = nullptr;
PFN_xrUpdateSwapchainFB xrUpdateSwapchainFB = nullptr;
PFN_xrGetFoveationEyeTrackedStateMETA xrGetFoveationEyeTrackedStateMETA = nullptr;
std::optional<utils::handle<XrFoveationProfileFB>> foveation_profile;
std::array<XrFovf, view_count> headset_fov{};
std::optional<bool> cropped_mode;
xr::swapchain dummy_swapchain;
std::optional<projection_layer> layer_;
std::optional<wivrn::from_headset::foveation> packet_;
bool update_meta_swapchain(XrSwapchain swapchain) const;
std::optional<angles> meta_angles(XrSwapchain swapchain, std::span<const XrFovf> fov) const;
void create_dummy_swapchain();
};
} // namespace client_foveation
+6
View File
@@ -38,6 +38,7 @@
#include <unordered_map>
#include <spdlog/spdlog.h>
#include <openxr/openxr.h>
#ifdef __ANDROID__
#include "utils/wrap_lambda.h"
@@ -229,11 +230,13 @@ void hmd_traits::init()
else if (manufacturer == "Play For Dream" and model == "PFDM MR")
{
blacklisted_extensions.insert(XR_META_FOVEATION_EYE_TRACKED_EXTENSION_NAME);
controller_profile = "yvr-touch-v2";
}
else if (manufacturer == "Pico")
{
blacklisted_extensions.insert(XR_META_FOVEATION_EYE_TRACKED_EXTENSION_NAME);
permissions[feature::eye_gaze] = "com.picovr.permission.EYE_TRACKING";
permissions[feature::face_tracking] = "com.picovr.permission.FACE_TRACKING";
view_locate = false;
@@ -315,6 +318,7 @@ void hmd_traits::init()
}
if (manufacturer == "samsung")
{
blacklisted_extensions.insert(XR_META_FOVEATION_EYE_TRACKED_EXTENSION_NAME);
if (model == "SM-I610") // Galaxy XR
{
panel_width_override = 3552;
@@ -359,6 +363,8 @@ void hmd_traits::init()
break;
}
// frame: always returns 0,0
blacklisted_extensions.insert(XR_META_FOVEATION_EYE_TRACKED_EXTENSION_NAME);
#endif
spdlog::info("HMD traits initialized");
+81 -60
View File
@@ -40,6 +40,7 @@
#include "utils/ranges.h"
#include "wivrn_packets.h"
#include <algorithm>
#include <cmath>
#include <mutex>
#include <ranges>
#include <thread>
@@ -200,7 +201,8 @@ static const std::array supported_depth_formats{
scenes::stream::stream(std::string server_name, scene & parent_scene) :
scene_impl<stream>(supported_color_formats, supported_depth_formats, parent_scene),
apps{*this, std::move(server_name)}
apps{*this, std::move(server_name)},
foveation_center{instance, system, session, device, commandpool, queue, swapchain_format}
{
auto views = system.view_configuration_views(viewconfig);
width = views[0].recommendedImageRectWidth;
@@ -387,20 +389,6 @@ std::shared_ptr<scenes::stream> scenes::stream::create(std::unique_ptr<wivrn_ses
}
}
{
const auto & config = application::get_config();
self->override_foveation_enable = config.override_foveation_enable;
self->override_foveation_pitch = config.override_foveation_pitch;
self->override_foveation_distance = config.override_foveation_distance;
if (self->override_foveation_enable)
self->network_session->send_control(from_headset::override_foveation_center{
.enabled = self->override_foveation_enable,
.pitch = self->override_foveation_pitch,
.distance = self->override_foveation_distance,
});
}
self->network_thread = utils::named_thread("network_thread", &stream::process_packets, self.get());
self->command_buffer = std::move(self->device.allocateCommandBuffers({
@@ -892,6 +880,33 @@ void scenes::stream::render(const XrFrameState & frame_state)
}
}
// Search for frame with desired display time on all decoders
// If no such frame exists, use the latest frame for each decoder
current_blit_handles = common_frame(frame_state.predictedDisplayTime);
std::array<XrPosef, view_count> pose;
std::array<XrFovf, view_count> fov{};
std::array<bool, view_count> valid_fov{};
std::array<wivrn::to_headset::foveation_parameter, view_count> foveation;
std::array<XrExtent2Di, view_count> extents;
int32_t max_width = 0;
int32_t max_height = 0;
for (size_t i = 0; i < view_count; ++i)
{
auto & blit_handle = current_blit_handles[i];
if (!blit_handle)
continue;
fov[i] = blit_handle->view_info.fov[i];
valid_fov[i] = true;
foveation[i] = blit_handle->view_info.foveation[i];
extents[i] = stream_defoveator::defoveated_size(foveation[i]);
max_width = std::max(max_width, extents[i].width);
max_height = std::max(max_height, extents[i].height);
}
if (max_width > 0 and max_height > 0)
ensure_foveation_mode(foveation_center.is_cropped(fov, valid_fov), max_width, max_height);
session.begin_frame();
#if WIVRN_FEATURE_RENDERDOC
@@ -907,12 +922,6 @@ void scenes::stream::render(const XrFrameState & frame_state)
command_buffer.resetQueryPool(*query_pool, 0, size_gpu_timestamps);
command_buffer.writeTimestamp(vk::PipelineStageFlagBits::eTopOfPipe, *query_pool, 0);
// Search for frame with desired display time on all decoders
// If no such frame exists, use the latest frame for each decoder
current_blit_handles = common_frame(frame_state.predictedDisplayTime);
std::array<XrPosef, view_count> pose;
std::array<XrFovf, view_count> fov;
std::array<wivrn::to_headset::foveation_parameter, view_count> foveation;
bool use_alpha = false;
std::array<stream_defoveator::input, view_count> images;
@@ -1025,42 +1034,18 @@ void scenes::stream::render(const XrFrameState & frame_state)
std::ranges::any_of(current_blit_handles, [](const auto & h) { return h and h->feedback.times_displayed < 2; }) or
is_gui_interactable())
{
XrExtent2Di extents[view_count];
{
int32_t max_width = 0;
int32_t max_height = 0;
for (size_t i = 0; i < view_count; ++i)
{
extents[i] = stream_defoveator::defoveated_size(foveation[i]);
max_width = std::max(max_width, extents[i].width);
max_height = std::max(max_height, extents[i].height);
}
if (not swapchain)
setup_reprojection_swapchain(max_width, max_height);
else if (swapchain.width() < max_width or swapchain.height() < max_height)
{
// If the defoveated image is larger than the swapchain, try to reallocate one
try
{
spdlog::info("Recreating swapchain, from {}x{} to {}x{}",
swapchain.width(),
swapchain.height(),
max_width,
max_height);
setup_reprojection_swapchain(max_width, max_height);
}
catch (std::exception & e)
{
spdlog::warn("failed to increase swapchain size");
for (size_t i = 0; i < view_count; ++i)
{
extents[i].width = std::min(extents[i].width, swapchain.width());
extents[i].height = std::min(extents[i].height, swapchain.height());
}
}
}
}
assert(swapchain);
// Swapchains only recreated when switching between cropped/full
// Otherwise we clamp and resize the existing one
for (size_t i = 0; i < view_count; ++i)
{
extents[i].width = std::min(extents[i].width, swapchain.width());
extents[i].height = std::min(extents[i].height, swapchain.height());
}
foveation_center.update(fov, valid_fov, swapchain, frame_state.predictedDisplayTime);
// defoveate the image, apply scale/bias
int image_index = swapchain.acquire();
swapchain.wait();
@@ -1170,6 +1155,10 @@ void scenes::stream::render(const XrFrameState & frame_state)
render_start(use_alpha, frame_state.predictedDisplayTime);
// full-FOV dummy layer for Meta foveation, used when cropping FOV
if (auto layer = foveation_center.get_layer())
add_projection_layer(layer->flags, layer->space, layer->views);
// Add the layer with the streamed content
std::array<XrCompositionLayerProjectionView, view_count> layer_view;
for (uint32_t view = 0; view < view_count; view++)
@@ -1220,7 +1209,8 @@ void scenes::stream::render(const XrFrameState & frame_state)
{
// Keep a copy of the feedback packets as they can be modified if they're encrypted
inplace_vector<from_headset::feedback, decoder_count> feedbacks;
inplace_vector<serialization_packet, decoder_count> packets;
// +1 for optional foveation packet
inplace_vector<serialization_packet, decoder_count + 1> packets;
feedbacks.reserve(current_blit_handles.size());
packets.reserve(current_blit_handles.size());
@@ -1233,6 +1223,11 @@ void scenes::stream::render(const XrFrameState & frame_state)
wivrn_session::control_socket_t::serialize(packet, feedbacks.emplace_back(handle->feedback));
}
}
if (auto foveation_packet = foveation_center.take_packet())
{
auto & packet = packets.emplace_back();
wivrn_session::control_socket_t::serialize(packet, *foveation_packet);
}
if (not packets.empty())
{
try
@@ -1304,7 +1299,7 @@ void scenes::stream::setup(const to_headset::video_stream_description & descript
defoveator->reset_pipelines();
}
void scenes::stream::setup_reprojection_swapchain(uint32_t swapchain_width, uint32_t swapchain_height)
void scenes::stream::setup_reprojection_swapchain(uint32_t swapchain_width, uint32_t swapchain_height, bool foveation)
{
assert(swapchain_width);
assert(swapchain_height);
@@ -1314,8 +1309,21 @@ void scenes::stream::setup_reprojection_swapchain(uint32_t swapchain_width, uint
auto views = system.view_configuration_views(viewconfig);
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());
swapchain = xr::swapchain(
instance,
session,
device,
swapchain_format,
swapchain_width,
swapchain_height,
1,
views.size(),
foveation);
spdlog::info(
"Created stream swapchain: {}x{}{}",
swapchain.width(),
swapchain.height(),
foveation ? " with eye-tracked foveation" : "");
for (auto view: views)
{
if (swapchain.width() > view.maxImageRectWidth or swapchain.height() > view.maxImageRectHeight)
@@ -1333,6 +1341,19 @@ void scenes::stream::setup_reprojection_swapchain(uint32_t swapchain_width, uint
swapchain.format());
}
void scenes::stream::ensure_foveation_mode(bool cropped, uint32_t width, uint32_t height)
{
if (!swapchain or foveation_center.needs_swapchain_recreation(cropped))
{
setup_reprojection_swapchain(width, height, foveation_center.use_meta_swapchain(cropped));
foveation_center.on_swapchain_recreated(cropped);
}
else
{
foveation_center.set_cropped(cropped);
}
}
scene::meta & scenes::stream::get_meta_scene()
{
static meta m{
+4 -5
View File
@@ -24,6 +24,7 @@
#include "audio/audio.h"
#include "configuration.h"
#include "decoder/shard_accumulator.h"
#include "foveation.h"
#include "render/imgui_impl.h"
#include "scene.h"
#include "scenes/input_profile.h"
@@ -119,6 +120,7 @@ private:
state_.compare_exchange_strong(prev, new_state);
}
client_foveation::center foveation_center;
xr::swapchain swapchain;
std::optional<audio> audio_handle;
@@ -190,10 +192,6 @@ private:
glm::vec3 world_gui_position;
glm::quat world_gui_orientation;
bool override_foveation_enable;
float override_foveation_pitch; // The pitch is the opposite as the height displayed in the GUI
float override_foveation_distance;
// Which controller is used for recentering and position of the GUI relative to the controller, in controller axes, during recentering
std::optional<std::tuple<xr::spaces, glm::vec3, glm::quat>> recentering_context;
void update_gui_position(xr::spaces controller, float predicted_display_period);
@@ -279,7 +277,8 @@ private:
void send_derived_pose();
void setup(const to_headset::video_stream_description &);
void setup_reprojection_swapchain(uint32_t width, uint32_t height);
void setup_reprojection_swapchain(uint32_t width, uint32_t height, bool foveation);
void ensure_foveation_mode(bool cropped, uint32_t width, uint32_t height);
vk::raii::QueryPool query_pool = nullptr;
bool query_pool_filled = false;
+25 -24
View File
@@ -415,13 +415,10 @@ void scenes::stream::gui_settings(float)
.enter_foveation_adjust = [this] { next_gui_status = stream_tab::foveation_settings; },
.on_foveation_override_changed = [this] {
const auto & config = application::get_config();
override_foveation_enable = config.override_foveation_enable;
override_foveation_pitch = config.override_foveation_pitch;
override_foveation_distance = config.override_foveation_distance;
network_session->send_control(from_headset::override_foveation_center{
.enabled = override_foveation_enable,
.pitch = override_foveation_pitch,
.distance = override_foveation_distance,
foveation_center.set_manual_mode({
.enabled = config.override_foveation_enable,
.pitch = config.override_foveation_pitch,
.distance = config.override_foveation_distance,
}); },
};
@@ -482,11 +479,13 @@ void scenes::stream::gui_bitrate_settings(float predicted_display_period)
void scenes::stream::gui_foveation_settings(float predicted_display_period)
{
auto override = foveation_center.get_manual_mode();
ImGui::PushFont(nullptr, constants::gui::font_size_large);
ImGui::Text("%s", _S("Use the thumbsticks to move the foveation center"));
ImGui::Text("%s", _S("Press A to save or B to cancel"));
ImGui::Text("%s", fmt::format(_F("Height {:.1f} °"), -override_foveation_pitch * 180 / M_PI).c_str());
ImGui::Text("%s", fmt::format(_F("Distance {:.2f} m"), override_foveation_distance).c_str());
ImGui::Text("%s", fmt::format(_F("Height {:.1f} °"), -override.pitch * 180 / M_PI).c_str());
ImGui::Text("%s", fmt::format(_F("Distance {:.2f} m"), override.distance).c_str());
ImGui::PopFont();
// Maximum speed 1 rad/s
@@ -495,8 +494,8 @@ void scenes::stream::gui_foveation_settings(float predicted_display_period)
// Maximum speed 2m/s @ 1m
float delta_distance = std::pow(constants::stream::gui_max_foveation_speed, application::read_action_float(foveation_distance).value_or(std::pair{0, 0}).second * predicted_display_period);
override_foveation_pitch = std::clamp<float>(override_foveation_pitch + delta_pitch, constants::stream::gui_min_foveation_pitch, constants::stream::gui_max_foveation_pitch);
override_foveation_distance = std::clamp<float>(override_foveation_distance * delta_distance, constants::stream::gui_min_foveation_distance, constants::stream::gui_max_foveation_distance);
override.pitch = std::clamp<float>(override.pitch + delta_pitch, constants::stream::gui_min_foveation_pitch, constants::stream::gui_max_foveation_pitch);
override.distance = std::clamp<float>(override.distance * delta_distance, constants::stream::gui_min_foveation_distance, constants::stream::gui_max_foveation_distance);
bool ok = application::read_action_bool(foveation_ok).value_or(std::pair{0, false}).second;
bool cancel = application::read_action_bool(foveation_cancel).value_or(std::pair{0, false}).second;
@@ -504,12 +503,13 @@ void scenes::stream::gui_foveation_settings(float predicted_display_period)
if (ok)
{
next_gui_status = stream_tab::settings;
override.enabled = true;
// Save settings
auto & config = application::get_config();
config.override_foveation_enable = true;
config.override_foveation_pitch = override_foveation_pitch;
config.override_foveation_distance = override_foveation_distance;
config.override_foveation_enable = override.enabled;
config.override_foveation_pitch = override.pitch;
config.override_foveation_distance = override.distance;
config.save();
}
else if (cancel)
@@ -518,16 +518,14 @@ void scenes::stream::gui_foveation_settings(float predicted_display_period)
// Restore settings
const auto & config = application::get_config();
override_foveation_enable = config.override_foveation_enable;
override_foveation_pitch = config.override_foveation_pitch;
override_foveation_distance = config.override_foveation_distance;
override = {
.enabled = config.override_foveation_enable,
.pitch = config.override_foveation_pitch,
.distance = config.override_foveation_distance,
};
}
network_session->send_control(from_headset::override_foveation_center{
.enabled = override_foveation_enable,
.pitch = override_foveation_pitch,
.distance = override_foveation_distance,
});
foveation_center.set_manual_mode(override);
}
void scenes::stream::gui_applications()
@@ -727,10 +725,13 @@ void scenes::stream::draw_gui(XrTime predicted_display_time, XrDuration predicte
glm::mat3 M = glm::mat3_cast(head_position->second);
switch (gui_status)
{
case stream_tab::foveation_settings:
case stream_tab::foveation_settings: {
auto override = foveation_center.get_manual_mode();
imgui_ctx->layers()[0].orientation = head_position->second;
imgui_ctx->layers()[0].position = head_position->first + M * glm::vec3{0, override_foveation_distance * sin(override_foveation_pitch), -override_foveation_distance};
imgui_ctx->layers()[0].position = head_position->first + M * glm::vec3{0, override.distance * sin(override.pitch), -override.distance};
break;
}
case stream_tab::hidden:
// Always use the same position for the GUI shortcut tip
+48 -28
View File
@@ -26,6 +26,7 @@
#include "xr/face_tracker.h"
#include "xr/fb_body_tracker.h"
#include "xr/to_string.h"
#include <glm/gtc/quaternion.hpp>
#include <magic_enum.hpp>
#include <magic_enum_containers.hpp>
#include <ranges>
@@ -60,6 +61,37 @@ from_headset::tracking::pose locate_space(device_id device, XrSpace space, XrSpa
return {.device = device};
}
from_headset::tracking::pose make_eye_gaze_view_relative(
const from_headset::tracking::pose & gaze_pose,
const from_headset::tracking::pose & view_pose)
{
glm::quat gaze_quat(
gaze_pose.pose.orientation.w,
gaze_pose.pose.orientation.x,
gaze_pose.pose.orientation.y,
gaze_pose.pose.orientation.z);
const glm::quat view_quat(
view_pose.pose.orientation.w,
view_pose.pose.orientation.x,
view_pose.pose.orientation.y,
view_pose.pose.orientation.z);
gaze_quat = glm::conjugate(view_quat) * gaze_quat;
using flags = from_headset::pose_flags;
return {
.pose = {
.orientation = {
.x = gaze_quat.x,
.y = gaze_quat.y,
.z = gaze_quat.z,
.w = gaze_quat.w,
},
},
.device = gaze_pose.device,
.flags = uint8_t(gaze_pose.flags & view_pose.flags & ~(flags::linear_velocity_valid | flags::angular_velocity_valid)),
};
}
class locate_spaces_functor
{
std::vector<XrSpaceLocationData> locations;
@@ -253,6 +285,7 @@ void scenes::stream::tracking()
magic_enum::containers::array<device_id, XrSpace> spaces{};
const auto & config = application::get_config();
const bool eye_gaze_enabled = config.check_feature(feature::eye_gaze);
{
std::vector ids{
@@ -270,7 +303,7 @@ void scenes::stream::tracking()
ids.insert(ids.end(), {device_id::LEFT_PINCH_POSE, device_id::LEFT_POKE, device_id::RIGHT_PINCH_POSE, device_id::RIGHT_POKE});
}
if (config.check_feature(feature::eye_gaze))
if (eye_gaze_enabled)
ids.emplace_back(device_id::EYE_GAZE);
for (auto id: ids)
@@ -294,6 +327,17 @@ void scenes::stream::tracking()
};
float applied_height_offset = config.get_height_offset();
xr::space height_offset_space = make_height_offset_space(applied_height_offset);
XrSpace world_space = application::space(xr::spaces::world);
auto locate_eye_gaze = [&](XrTime time) {
// Eye gaze uses view pose as the origin.
if (application::get_hmd_traits().view_locate)
return locate_space(device_id::EYE_GAZE, spaces[device_id::EYE_GAZE], view_space, time);
// Pico headsets fail to locate gaze relative to view.
return make_eye_gaze_view_relative(
locate_space(device_id::EYE_GAZE, spaces[device_id::EYE_GAZE], world_space, time),
locate_space(device_id::EYE_GAZE, view_space, world_space, time));
};
from_headset::tracking tracking;
std::vector<from_headset::hand_tracking> hands;
@@ -464,6 +508,7 @@ void scenes::stream::tracking()
j.pose = i.pose;
j.fov = i.fov;
}
locate_spaces.add_space(item.device, view_space, tracking.timestamp, tracking.device_poses);
break;
case wivrn::device_id::LEFT_GRIP:
@@ -479,33 +524,8 @@ void scenes::stream::tracking()
locate_spaces.add_space(item.device, spaces[item.device], tracking.timestamp, tracking.device_poses);
break;
case wivrn::device_id::EYE_GAZE:
// Eye gaze uses view pose as the origin
if (application::get_hmd_traits().view_locate)
tracking.device_poses.push_back(locate_space(item.device, spaces[item.device], spaces[wivrn::device_id::HEAD], tracking.timestamp));
else
{
// Pico headsets fail to locate gaze relative to view
auto gaze = locate_space(item.device, spaces[item.device], height_offset_space, tracking.timestamp);
auto view_pose = locate_space(item.device, view_space, height_offset_space, tracking.timestamp);
glm::quat gaze_quat(gaze.pose.orientation.w, gaze.pose.orientation.x, gaze.pose.orientation.y, gaze.pose.orientation.z);
glm::quat view_quat(view_pose.pose.orientation.w, view_pose.pose.orientation.x, view_pose.pose.orientation.y, view_pose.pose.orientation.z);
gaze_quat = glm::conjugate(view_quat) * gaze_quat;
using flags = from_headset::pose_flags;
tracking.device_poses.push_back(
from_headset::tracking::pose{
// Zero position and velocities
.pose = {
.orientation = {
.x = gaze_quat.x,
.y = gaze_quat.y,
.z = gaze_quat.z,
.w = gaze_quat.w,
},
},
.device = item.device,
.flags = uint8_t(gaze.flags & view_pose.flags & ~(flags::linear_velocity_valid | flags::angular_velocity_valid)),
});
}
if (eye_gaze_enabled)
tracking.device_poses.push_back(locate_eye_gaze(tracking.timestamp));
break;
case wivrn::device_id::FACE:
std::visit(utils::overloaded{
+48 -5
View File
@@ -21,6 +21,7 @@
#include "application.h"
#include "details/enumerate.h"
#include "session.h"
#include <spdlog/spdlog.h>
xr::swapchain::swapchain(
xr::instance & inst,
@@ -30,7 +31,9 @@ xr::swapchain::swapchain(
int32_t width,
int32_t height,
int sample_count,
uint32_t array_size) :
uint32_t array_size,
bool foveation,
bool transfer_dst) :
width_(width),
height_(height),
sample_count_(sample_count),
@@ -55,8 +58,24 @@ xr::swapchain::swapchain(
break;
}
if (transfer_dst)
usage_flags |= XR_SWAPCHAIN_USAGE_TRANSFER_DST_BIT;
XrVulkanSwapchainCreateInfoMETA vulkan_create_info{
.type = XR_TYPE_VULKAN_SWAPCHAIN_CREATE_INFO_META,
.additionalCreateFlags = VK_IMAGE_CREATE_FRAGMENT_DENSITY_MAP_OFFSET_BIT_QCOM,
.additionalUsageFlags = 0,
};
XrSwapchainCreateInfoFoveationFB foveation_create_info{
.type = XR_TYPE_SWAPCHAIN_CREATE_INFO_FOVEATION_FB,
.next = &vulkan_create_info,
.flags = XR_SWAPCHAIN_CREATE_FOVEATION_FRAGMENT_DENSITY_MAP_BIT_FB,
};
XrSwapchainCreateInfo create_info{
.type = XR_TYPE_SWAPCHAIN_CREATE_INFO,
.next = foveation ? &foveation_create_info : nullptr,
.createFlags = 0,
.usageFlags = usage_flags,
.format = static_cast<VkFormat>(format),
@@ -70,11 +89,35 @@ xr::swapchain::swapchain(
CHECK_XR(xrCreateSwapchain(s, &create_info, &id));
auto images = details::enumerate<XrSwapchainImageVulkanKHR>(xrEnumerateSwapchainImages, id);
if (foveation)
{
auto [images, foveation_images] =
details::enumerate2<XrSwapchainImageVulkanKHR, XrSwapchainImageFoveationVulkanFB>(
xrEnumerateSwapchainImages, id);
images_.reserve(images.size());
for (auto & image: images)
images_.push_back(image.image);
images_.reserve(images.size());
for (auto & image: images)
images_.push_back(image.image);
spdlog::debug("XR_FB_foveation: enumerated {} color images and {} FDM images", images.size(), foveation_images.size());
for (size_t i = 0; i < foveation_images.size(); ++i)
{
const auto & fdm = foveation_images[i];
spdlog::debug(
" {}. FDM image @ {}x{}",
i,
fdm.width,
fdm.height);
}
}
else
{
auto images = details::enumerate<XrSwapchainImageVulkanKHR>(xrEnumerateSwapchainImages, id);
images_.reserve(images.size());
for (auto & image: images)
images_.push_back(image.image);
}
}
int xr::swapchain::acquire()
+3 -1
View File
@@ -53,7 +53,9 @@ public:
int32_t width,
int32_t height,
int sample_count = 1,
uint32_t array_size = 1);
uint32_t array_size = 1,
bool foveation = false,
bool transfer_dst = false);
int32_t width() const
{
+18
View File
@@ -117,6 +117,24 @@ XrSystemEyeGazeInteractionPropertiesEXT xr::system::eye_gaze_interaction_propert
return eye_gaze_prop;
}
XrSystemFoveationEyeTrackedPropertiesMETA xr::system::foveation_eye_tracked_properties() const
{
if (!id)
throw std::invalid_argument("this");
XrSystemFoveationEyeTrackedPropertiesMETA foveation_prop{
.type = XR_TYPE_SYSTEM_FOVEATION_EYE_TRACKED_PROPERTIES_META,
};
XrSystemProperties prop{
.type = XR_TYPE_SYSTEM_PROPERTIES,
.next = &foveation_prop,
};
CHECK_XR(xrGetSystemProperties(*inst, id, &prop));
return foveation_prop;
}
XrSystemUserPresencePropertiesEXT xr::system::user_presence_properties() const
{
if (!id)
+1
View File
@@ -85,6 +85,7 @@ public:
XrSystemProperties properties() const;
XrSystemHandTrackingPropertiesEXT hand_tracking_properties() const;
XrSystemEyeGazeInteractionPropertiesEXT eye_gaze_interaction_properties() const;
XrSystemFoveationEyeTrackedPropertiesMETA foveation_eye_tracked_properties() const;
XrSystemUserPresencePropertiesEXT user_presence_properties() const;
XrSystemFaceTrackingPropertiesANDROID android_face_tracking_properties() const;
XrSystemFaceTrackingProperties2FB fb_face_tracking2_properties() const;
+7 -9
View File
@@ -399,6 +399,11 @@ inline uint8_t to_pose_flags(XrSpaceLocationFlags location_flags, XrSpaceVelocit
return flags;
}
struct foveation
{
std::array<XrVector2f, 2> angles; // Per-eye x/y view angles in radians
};
struct tracking
{
enum state_flags : uint8_t
@@ -690,13 +695,6 @@ struct stream_tab_changed
stream_tab tab;
};
struct override_foveation_center
{
bool enabled;
float pitch;
float distance;
};
struct get_application_list
{
std::string language;
@@ -747,13 +745,13 @@ using packets = std::variant<
session_state_changed,
user_presence_changed,
stream_tab_changed,
override_foveation_center,
get_application_list,
start_app,
get_running_applications,
set_active_application,
hid::input,
stop_application>;
stop_application,
foveation>;
} // namespace from_headset
namespace to_headset
+2 -12
View File
@@ -404,11 +404,6 @@ xrt_result_t compositor::layer_commit(xrt_graphics_sync_handle_t sync_handle)
cmd.writeTimestamp(vk::PipelineStageFlagBits::eBottomOfPipe, *query_pool, 1);
if (session.get_info().eye_gaze)
{
auto now = os_monotonic_get_ns();
session.add_tracking_request(device_id::EYE_GAZE, frame.rendering.desired_present_time_ns, now, now);
}
view_info.foveation = foveation.foveate(
vk.device,
cmd,
@@ -815,14 +810,9 @@ void compositor::set_framerate(float hz)
encoder->set_framerate(hz);
}
void compositor::update_tracking(const from_headset::tracking & tracking)
void compositor::update_foveation(const from_headset::foveation & packet)
{
foveation.update_tracking(tracking);
}
void compositor::update_foveation_center_override(const from_headset::override_foveation_center & center)
{
foveation.update_foveation_center_override(center);
foveation.update_foveation(packet);
}
void compositor::resume()
+1 -2
View File
@@ -188,8 +188,7 @@ public:
void set_framerate(float hz);
void set_bitrate(uint32_t);
void update_tracking(const from_headset::tracking &);
void update_foveation_center_override(const from_headset::override_foveation_center &);
void update_foveation(const from_headset::foveation &);
void resume();
+9 -98
View File
@@ -19,7 +19,6 @@
#include "foveation.h"
#include "driver/xrt_cast.h"
#include "utils/wivrn_vk_bundle.h"
#include "vk/specialization_constants.h"
#include "wivrn_packets.h"
@@ -261,32 +260,6 @@ static std::tuple<float, float> solve_foveation(float λ, float c)
return {a, b(a)};
}
static bool is_zero_quat(xrt_quat q)
{
return q.x == 0 and q.y == 0 and q.z == 0 and q.w == 0;
}
static xrt_vec2 yaw_pitch(xrt_quat q)
{
if (is_zero_quat(q))
return xrt_vec2{};
float sine_theta = std::clamp(-2.0f * (q.y * q.z - q.w * q.x), -1.0f, 1.0f);
float pitch = std::asin(sine_theta);
if (std::abs(sine_theta) > 0.99999f)
{
float scale = std::copysign(2.0, sine_theta);
return {scale * std::atan2(-q.z, q.w), pitch};
}
return {
std::atan2(2.0f * (q.x * q.z + q.w * q.y),
q.w * q.w - q.x * q.x - q.y * q.y + q.z * q.z),
pitch};
}
static float angles_to_center(float e, float l, float r)
{
e = tan(e);
@@ -299,17 +272,6 @@ static float angles_to_center(float e, float l, float r)
return res;
}
static float convergence_angle(float distance, float eye_x, float gaze_yaw)
{
float target_x = distance * std::sin(gaze_yaw);
float target_z = distance * std::cos(gaze_yaw);
float dx = target_x - eye_x;
float dz = target_z;
return std::atan2(dx, dz);
}
static void fill_param_2d(
float c,
size_t foveated_dim,
@@ -357,11 +319,6 @@ namespace wivrn
void foveation::compute_params()
{
auto e = yaw_pitch(gaze);
if (manual_foveation.enabled)
e.y = manual_foveation.pitch;
for (size_t i = 0; i < 2; ++i)
{
const auto & fov = last.fovs[i];
@@ -369,9 +326,7 @@ void foveation::compute_params()
size_t extent_w = std::abs(last.src[i].extent.w);
if (foveated_size.width < extent_w)
{
auto distance = manual_foveation.enabled ? manual_foveation.distance : convergence_distance;
auto angle_x = convergence_angle(distance, eye_x[i], -e.x);
auto center = angles_to_center(angle_x, fov.angle_left, fov.angle_right);
const float center = angles_to_center(last.foveation_angles[i].x, fov.angle_left, fov.angle_right);
fill_param_2d(center, foveated_size.width, extent_w, params[i].x);
}
else
@@ -380,13 +335,7 @@ void foveation::compute_params()
size_t extent_h = std::abs(last.src[i].extent.h);
if (foveated_size.height < extent_h)
{
auto angle_y = -e.y;
if (is_zero_quat(gaze) and not manual_foveation.enabled)
{
// Natural gaze is not straight forward, adjust the angle
angle_y += angle_offset;
}
auto center = angles_to_center(-angle_y, fov.angle_up, fov.angle_down);
const float center = angles_to_center(last.foveation_angles[i].y, fov.angle_up, fov.angle_down);
fill_param_2d(center, foveated_size.height, extent_h, params[i].y);
}
else
@@ -396,11 +345,6 @@ void foveation::compute_params()
foveation::foveation(wivrn::vk_bundle & bundle, vk::Extent3D foveated_size) :
foveated_size(foveated_size),
// normal sight line is between 10° and 15° below horizontal
// https://apps.dtic.mil/sti/tr/pdf/AD0758339.pdf pages 393-394
// testing shows 10° looks better
angle_offset(10 * M_PI / 180),
convergence_distance(1 /* meter*/),
gpu_buffer(
bundle.device,
{
@@ -427,35 +371,10 @@ foveation::foveation(wivrn::vk_bundle & bundle, vk::Extent3D foveated_size) :
bundle.name(descriptor_set, "foveation descriptor set");
}
void foveation::update_tracking(const from_headset::tracking & tracking)
void foveation::update_foveation(const from_headset::foveation & packet)
{
std::lock_guard lock(mutex);
const uint8_t orientation_ok = from_headset::pose_flags::orientation_valid | from_headset::pose_flags::orientation_tracked;
if (tracking.view_flags & XR_VIEW_STATE_POSITION_VALID_BIT)
{
eye_x[0] = tracking.views[0].pose.position.x;
eye_x[1] = tracking.views[1].pose.position.x;
}
for (const auto & pose: tracking.device_poses)
{
if (pose.device != device_id::EYE_GAZE)
continue;
if ((pose.flags & orientation_ok) != orientation_ok)
return;
gaze = xrt_cast(pose.pose.orientation);
return;
}
}
void foveation::update_foveation_center_override(const from_headset::override_foveation_center & center)
{
std::lock_guard lock(mutex);
manual_foveation = center;
foveation_angles = packet.angles;
}
static void fill_ubo(
@@ -495,10 +414,6 @@ static bool operator==(const T & a, const T & b)
static_assert(std::has_unique_object_representations_v<T>);
return std::memcmp(&a, &b, sizeof(T)) == 0;
}
static bool operator==(const xrt_quat & a, const xrt_quat & b)
{
return a.x == b.x and a.y == b.y and a.z == b.z and a.w == b.w;
}
static bool operator==(const xrt_fov & a, const xrt_fov & b)
{
return a.angle_left == b.angle_left and a.angle_right == b.angle_right and a.angle_up == b.angle_up and a.angle_down == b.angle_down;
@@ -516,21 +431,17 @@ void foveation::update_ubo(
last.src[1] == src_rect[1] and
last.fovs[0] == src_fov[0] and
last.fovs[1] == src_fov[1] and
(last.gaze == gaze or manual_foveation.enabled) and // Ignore the gaze if foveation center is overridden
std::abs(last.eye_x[0] - eye_x[0]) < 0.0005 and
std::abs(last.eye_x[1] - eye_x[1]) < 0.0005 and
last.manual_foveation.enabled == manual_foveation.enabled and
std::abs(last.manual_foveation.pitch - manual_foveation.pitch) < 0.0005 and
std::abs(last.manual_foveation.distance - manual_foveation.distance) < 0.0005)
last.foveation_angles[0].x == foveation_angles[0].x and
last.foveation_angles[0].y == foveation_angles[0].y and
last.foveation_angles[1].x == foveation_angles[1].x and
last.foveation_angles[1].y == foveation_angles[1].y)
return;
last = {
.gaze = gaze,
.flip_y = flip_y,
.src = {src_rect[0], src_rect[1]},
.fovs = {src_fov[0], src_fov[1]},
.eye_x = {eye_x[0], eye_x[1]},
.manual_foveation = manual_foveation,
.foveation_angles = foveation_angles,
};
compute_params();
+4 -17
View File
@@ -39,15 +39,7 @@ class foveation
std::mutex mutex;
const vk::Extent3D foveated_size; // per eye
// Natural vertical gaze angle
const float angle_offset;
// Optionally defined from environment variables
const float convergence_distance;
float eye_x[2] = {}; // eye x position
xrt_quat gaze = {};
from_headset::override_foveation_center manual_foveation = {};
std::array<XrVector2f, 2> foveation_angles = {};
std::array<to_headset::foveation_parameter, 2> params;
buffer_allocation gpu_buffer;
@@ -62,13 +54,10 @@ class foveation
// parameters used for last computation
struct P
{
xrt_quat gaze = {};
bool flip_y = false;
xrt_rect src[2] = {};
xrt_fov fovs[2] = {};
float eye_x[2] = {};
from_headset::override_foveation_center manual_foveation = {};
std::array<XrVector2f, 2> foveation_angles = {};
};
P last;
@@ -82,11 +71,9 @@ class foveation
std::array<xrt_fov, 2> src_fov);
public:
foveation(wivrn::vk_bundle &,
vk::Extent3D foveated_size);
foveation(wivrn::vk_bundle &, vk::Extent3D foveated_size);
void update_tracking(const from_headset::tracking &);
void update_foveation_center_override(const from_headset::override_foveation_center &);
void update_foveation(const from_headset::foveation &);
std::array<to_headset::foveation_parameter, 2> foveate(
vk::raii::Device &,
+2 -3
View File
@@ -675,7 +675,6 @@ void wivrn_session::operator()(const from_headset::tracking & tracking)
if (eye_tracker)
eye_tracker->update_tracking(tracking, offset);
compositor.update_tracking(tracking);
if (android_face_tracker)
android_face_tracker->update_tracking(tracking, offset);
@@ -685,9 +684,9 @@ void wivrn_session::operator()(const from_headset::tracking & tracking)
htc_face_tracker->update_tracking(tracking, offset);
}
void wivrn_session::operator()(from_headset::override_foveation_center && foveation_center)
void wivrn_session::operator()(const from_headset::foveation & packet)
{
compositor.update_foveation_center_override(foveation_center);
compositor.update_foveation(packet);
}
void wivrn_session::operator()(from_headset::derived_pose && derived)
+1 -1
View File
@@ -166,6 +166,7 @@ public:
void operator()(const from_headset::settings_changed &);
void operator()(from_headset::handshake &&) {}
void operator()(const from_headset::tracking &);
void operator()(const from_headset::foveation &);
void operator()(from_headset::derived_pose &&);
void operator()(from_headset::hand_tracking &&);
void operator()(from_headset::meta_body &&);
@@ -182,7 +183,6 @@ public:
void operator()(from_headset::user_presence_changed &&);
void operator()(from_headset::refresh_rate_changed &&);
void operator()(from_headset::stream_tab_changed &&);
void operator()(from_headset::override_foveation_center &&);
void operator()(from_headset::get_application_list &&);
void operator()(const from_headset::start_app &);
void operator()(const from_headset::get_running_applications &);