Implement main compositor
Replace the patches to Monado's main compositor with a local implementation, reusing code when relevant. The layer squasher is very close to comp_render_cs, while distortion is completely replaced by foveation.
This commit is contained in:
@@ -56,7 +56,7 @@ class app_pacer : public u_pacing_app
|
||||
}
|
||||
|
||||
public:
|
||||
using base = u_pacing_app;
|
||||
using base_t = u_pacing_app;
|
||||
|
||||
app_pacer(pacing_app_factory & parent) :
|
||||
u_pacing_app{
|
||||
|
||||
@@ -37,7 +37,7 @@ class pacing_app_factory : public u_pacing_app_factory
|
||||
void remove_app(app_pacer *);
|
||||
|
||||
public:
|
||||
using base = u_pacing_app_factory;
|
||||
using base_t = u_pacing_app_factory;
|
||||
|
||||
pacing_app_factory();
|
||||
xrt_result_t create(struct u_pacing_app ** out_upa);
|
||||
|
||||
@@ -89,7 +89,7 @@ class wivrn_android_face_tracker : public xrt_device
|
||||
wivrn::wivrn_session & cnx;
|
||||
|
||||
public:
|
||||
using base = xrt_device;
|
||||
using base_t = xrt_device;
|
||||
wivrn_android_face_tracker(xrt_device * hmd, wivrn::wivrn_session & cnx);
|
||||
|
||||
xrt_result_t update_inputs();
|
||||
|
||||
@@ -1,881 +0,0 @@
|
||||
/*
|
||||
* WiVRn VR streaming
|
||||
* Copyright (C) 2022 Guillaume Meunier <guillaume.meunier@centraliens.net>
|
||||
* Copyright (C) 2022 Patrick Nicolas <patricknicolas@laposte.net>
|
||||
* Copyright (C) 2026 Sapphire <imsapphire0@gmail.com>
|
||||
*
|
||||
* 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 "wivrn_comp_target.h"
|
||||
|
||||
#include "driver/wivrn_session.h"
|
||||
#include "encoder/video_encoder.h"
|
||||
#include "util/u_logging.h"
|
||||
#include "utils/method.h"
|
||||
#include "utils/scoped_lock.h"
|
||||
#include "wivrn_config.h"
|
||||
#include "wivrn_foveation.h"
|
||||
|
||||
#include "main/comp_compositor.h"
|
||||
#include "math/m_space.h"
|
||||
#include "xrt_cast.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <ranges>
|
||||
#include <vector>
|
||||
#include <vulkan/vulkan.hpp>
|
||||
#include <vulkan/vulkan_core.h>
|
||||
#include <vulkan/vulkan_handles.hpp>
|
||||
|
||||
#include "xrt/xrt_config_build.h" // IWYU pragma: keep
|
||||
#ifdef XRT_FEATURE_RENDERDOC
|
||||
#include "renderdoc_app.h"
|
||||
#endif
|
||||
|
||||
namespace wivrn
|
||||
{
|
||||
std::vector<const char *> wivrn_comp_target::wanted_instance_extensions = {};
|
||||
std::vector<const char *> wivrn_comp_target::wanted_device_extensions = {
|
||||
// For FFMPEG
|
||||
#ifdef VK_EXT_external_memory_dma_buf
|
||||
VK_EXT_EXTERNAL_MEMORY_DMA_BUF_EXTENSION_NAME,
|
||||
#endif
|
||||
#ifdef VK_EXT_image_drm_format_modifier
|
||||
VK_EXT_IMAGE_DRM_FORMAT_MODIFIER_EXTENSION_NAME,
|
||||
#endif
|
||||
|
||||
// For vulkan video encode
|
||||
#ifdef VK_KHR_video_queue
|
||||
VK_KHR_VIDEO_QUEUE_EXTENSION_NAME,
|
||||
#endif
|
||||
#ifdef VK_KHR_video_encode_queue
|
||||
VK_KHR_VIDEO_ENCODE_QUEUE_EXTENSION_NAME,
|
||||
#endif
|
||||
#ifdef VK_KHR_video_maintenance1
|
||||
VK_KHR_VIDEO_MAINTENANCE_1_EXTENSION_NAME,
|
||||
#endif
|
||||
#ifdef VK_KHR_video_encode_h264
|
||||
VK_KHR_VIDEO_ENCODE_H264_EXTENSION_NAME,
|
||||
#endif
|
||||
#ifdef VK_KHR_video_encode_h265
|
||||
VK_KHR_VIDEO_ENCODE_H265_EXTENSION_NAME,
|
||||
#endif
|
||||
};
|
||||
|
||||
#ifdef XRT_FEATURE_RENDERDOC
|
||||
static auto renderdoc()
|
||||
{
|
||||
auto x = []() {
|
||||
RENDERDOC_API_1_5_0 * rdoc_api = nullptr;
|
||||
const char * env = std::getenv("ENABLE_VULKAN_RENDERDOC_CAPTURE");
|
||||
if (not env or env != std::string_view("1"))
|
||||
return rdoc_api;
|
||||
void * mod = dlopen("librenderdoc.so", RTLD_NOW | RTLD_NOLOAD);
|
||||
if (mod)
|
||||
{
|
||||
pRENDERDOC_GetAPI RENDERDOC_GetAPI = (pRENDERDOC_GetAPI)dlsym(mod, "RENDERDOC_GetAPI");
|
||||
XRT_MAYBE_UNUSED int ret = RENDERDOC_GetAPI(eRENDERDOC_API_Version_1_5_0, (void **)&rdoc_api);
|
||||
assert(ret == 1);
|
||||
}
|
||||
return rdoc_api;
|
||||
};
|
||||
static auto res = x();
|
||||
return res;
|
||||
}
|
||||
#endif
|
||||
|
||||
void wivrn_comp_target::init_semaphores()
|
||||
{
|
||||
auto vk = get_vk();
|
||||
VkResult ret;
|
||||
|
||||
fini_semaphores();
|
||||
|
||||
VkSemaphoreCreateInfo info = {
|
||||
.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
|
||||
};
|
||||
|
||||
ret = vk->vkCreateSemaphore(vk->device, &info, nullptr, &semaphores.present_complete);
|
||||
if (ret != VK_SUCCESS)
|
||||
{
|
||||
COMP_ERROR(c, "vkCreateSemaphore: %s", vk_result_string(ret));
|
||||
}
|
||||
|
||||
semaphores.render_complete_is_timeline = false;
|
||||
ret = vk->vkCreateSemaphore(vk->device, &info, nullptr, &semaphores.render_complete);
|
||||
if (ret != VK_SUCCESS)
|
||||
{
|
||||
COMP_ERROR(c, "vkCreateSemaphore: %s", vk_result_string(ret));
|
||||
}
|
||||
}
|
||||
void wivrn_comp_target::fini_semaphores()
|
||||
{
|
||||
auto vk = get_vk();
|
||||
|
||||
if (semaphores.present_complete != VK_NULL_HANDLE)
|
||||
{
|
||||
vk->vkDestroySemaphore(vk->device, semaphores.present_complete, nullptr);
|
||||
semaphores.present_complete = VK_NULL_HANDLE;
|
||||
}
|
||||
|
||||
if (semaphores.render_complete != VK_NULL_HANDLE)
|
||||
{
|
||||
vk->vkDestroySemaphore(vk->device, semaphores.render_complete, nullptr);
|
||||
semaphores.render_complete = VK_NULL_HANDLE;
|
||||
}
|
||||
}
|
||||
|
||||
VkResult wivrn_comp_target::create_images_impl(vk::ImageUsageFlags flags)
|
||||
{
|
||||
auto vk = get_vk();
|
||||
assert(image_count > 0);
|
||||
COMP_DEBUG(c, "Creating %d images.", image_count);
|
||||
auto & device = wivrn_bundle->device;
|
||||
|
||||
destroy_images();
|
||||
|
||||
auto format = vk::Format(this->format);
|
||||
|
||||
bool is_10bit = format == vk::Format::eG10X6B10X6R10X62Plane420Unorm3Pack16;
|
||||
|
||||
std::array formats = {
|
||||
is_10bit ? vk::Format::eR16Unorm : vk::Format::eR8Unorm,
|
||||
is_10bit ? vk::Format::eR16G16Unorm : vk::Format::eR8G8Unorm,
|
||||
format};
|
||||
|
||||
images = U_TYPED_ARRAY_CALLOC(comp_target_image, image_count);
|
||||
|
||||
vk::StructureChain image_info{
|
||||
vk::ImageCreateInfo{
|
||||
.flags = vk::ImageCreateFlagBits::eExtendedUsage | vk::ImageCreateFlagBits::eMutableFormat,
|
||||
.imageType = vk::ImageType::e2D,
|
||||
.format = format,
|
||||
.extent = {
|
||||
.width = width,
|
||||
.height = height,
|
||||
.depth = 1,
|
||||
},
|
||||
.mipLevels = 1,
|
||||
.arrayLayers = 3, // left, right then alpha
|
||||
.samples = vk::SampleCountFlagBits::e1,
|
||||
.tiling = vk::ImageTiling::eOptimal,
|
||||
.usage = flags | vk::ImageUsageFlagBits::eStorage | vk::ImageUsageFlagBits::eTransferSrc,
|
||||
.sharingMode = vk::SharingMode::eExclusive,
|
||||
},
|
||||
vk::ImageFormatListCreateInfo{
|
||||
.viewFormatCount = formats.size(),
|
||||
.pViewFormats = formats.data(),
|
||||
},
|
||||
};
|
||||
#if WIVRN_USE_VULKAN_ENCODE
|
||||
if (
|
||||
vk->features.video_maintenance_1 and
|
||||
std::ranges::contains(
|
||||
settings,
|
||||
encoder_vulkan,
|
||||
&encoder_settings::encoder_name))
|
||||
{
|
||||
image_info.get().flags |= vk::ImageCreateFlagBits::eVideoProfileIndependentKHR;
|
||||
image_info.get().usage |= vk::ImageUsageFlagBits::eVideoEncodeSrcKHR;
|
||||
}
|
||||
#endif
|
||||
|
||||
psc.images.resize(image_count);
|
||||
for (uint32_t i = 0; i < image_count; i++)
|
||||
{
|
||||
auto & image = psc.images[i].image;
|
||||
image = image_allocation(
|
||||
device,
|
||||
image_info.get(),
|
||||
{.usage = VMA_MEMORY_USAGE_AUTO},
|
||||
std::format("comp target image {}", i));
|
||||
images[i].handle = image;
|
||||
}
|
||||
|
||||
for (uint32_t i = 0; i < image_count; i++)
|
||||
{
|
||||
auto & item = psc.images[i];
|
||||
vk::ImageViewUsageCreateInfo usage{
|
||||
.usage = flags,
|
||||
};
|
||||
item.image_view_y = vk::raii::ImageView(device,
|
||||
{
|
||||
.pNext = &usage,
|
||||
.image = item.image,
|
||||
.viewType = vk::ImageViewType::e2DArray,
|
||||
.format = formats[0],
|
||||
.subresourceRange = {
|
||||
.aspectMask = vk::ImageAspectFlagBits::ePlane0,
|
||||
.levelCount = 1,
|
||||
.layerCount = vk::RemainingArrayLayers,
|
||||
},
|
||||
});
|
||||
item.image_view_cbcr = vk::raii::ImageView(device,
|
||||
{
|
||||
.pNext = &usage,
|
||||
.image = item.image,
|
||||
.viewType = vk::ImageViewType::e2DArray,
|
||||
.format = formats[1],
|
||||
.subresourceRange = {
|
||||
.aspectMask = vk::ImageAspectFlagBits::ePlane1,
|
||||
.levelCount = 1,
|
||||
.layerCount = vk::RemainingArrayLayers,
|
||||
},
|
||||
});
|
||||
images[i].view = VkImageView(*item.image_view_y);
|
||||
images[i].view_cbcr = VkImageView(*item.image_view_cbcr);
|
||||
wivrn_bundle->name(item.image_view_y, "comp target image view (y)");
|
||||
wivrn_bundle->name(item.image_view_cbcr, "comp target image view (CbCr)");
|
||||
}
|
||||
|
||||
psc.fence = vk::raii::Fence(device, vk::FenceCreateInfo{.flags = vk::FenceCreateFlagBits::eSignaled});
|
||||
wivrn_bundle->name(psc.fence, "comp target fence");
|
||||
|
||||
psc.command_buffer = std::move(device.allocateCommandBuffers(
|
||||
{.commandPool = *command_pool,
|
||||
.commandBufferCount = 1})[0]);
|
||||
wivrn_bundle->name(psc.command_buffer, "comp target command buffer");
|
||||
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
void wivrn_comp_target::destroy_images()
|
||||
{
|
||||
if (images == nullptr)
|
||||
return;
|
||||
|
||||
if (wivrn_bundle)
|
||||
wivrn_bundle->device.waitIdle();
|
||||
|
||||
psc.status = 1;
|
||||
psc.status.notify_all();
|
||||
encoder_threads.clear();
|
||||
encoders.clear();
|
||||
|
||||
psc.images.clear();
|
||||
|
||||
free(images);
|
||||
images = nullptr;
|
||||
|
||||
fini_semaphores();
|
||||
}
|
||||
|
||||
void wivrn_comp_target::create_encoders()
|
||||
{
|
||||
auto vk = get_vk();
|
||||
assert(encoders.empty());
|
||||
assert(encoder_threads.empty());
|
||||
assert(wivrn_bundle);
|
||||
psc.status = 0;
|
||||
|
||||
desc.width = width;
|
||||
desc.height = height;
|
||||
|
||||
std::map<int, std::vector<std::shared_ptr<video_encoder>>> thread_params;
|
||||
|
||||
for (auto [i, settings]: std::ranges::enumerate_view(settings))
|
||||
{
|
||||
auto & encoder = encoders.emplace_back(
|
||||
video_encoder::create(*wivrn_bundle, settings, i));
|
||||
desc.codec[i] = settings.codec;
|
||||
|
||||
thread_params[settings.group].emplace_back(encoder);
|
||||
}
|
||||
|
||||
for (auto & [group, params]: thread_params)
|
||||
{
|
||||
auto & thread = encoder_threads.emplace_back([this](auto stop_token, auto... args) { return run_present(stop_token, args...); }, encoder_threads.size(), std::move(params));
|
||||
std::string name = "encoder " + std::to_string(group);
|
||||
pthread_setname_np(thread.native_handle(), name.c_str());
|
||||
}
|
||||
cnx.send_control(to_headset::video_stream_description{desc});
|
||||
}
|
||||
|
||||
bool wivrn_comp_target::init_pre_vulkan()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
bool wivrn_comp_target::init_post_vulkan(uint32_t preferred_width, uint32_t preferred_height)
|
||||
{
|
||||
auto vk = get_vk();
|
||||
try
|
||||
{
|
||||
wivrn_bundle.emplace(*vk,
|
||||
wivrn_comp_target::wanted_instance_extensions,
|
||||
wivrn_comp_target::wanted_device_extensions);
|
||||
command_pool = vk::raii::CommandPool(
|
||||
wivrn_bundle->device,
|
||||
{
|
||||
.flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer,
|
||||
.queueFamilyIndex = vk->main_queue->family_index,
|
||||
});
|
||||
wivrn_bundle->name(command_pool, "comp target command pool");
|
||||
}
|
||||
catch (std::exception & e)
|
||||
{
|
||||
U_LOG_E("Compositor target init failed: %s", e.what());
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
settings = get_encoder_settings(
|
||||
*wivrn_bundle,
|
||||
cnx.get_info(),
|
||||
*cnx.get_settings());
|
||||
print_encoders(settings);
|
||||
|
||||
c->settings.preferred.width = settings[0].width;
|
||||
c->settings.preferred.height = settings[0].height;
|
||||
}
|
||||
catch (const std::exception & e)
|
||||
{
|
||||
wivrn_ipc_socket_monado->send(from_monado::server_error{
|
||||
.where = "Error creating encoder",
|
||||
.message = e.what(),
|
||||
});
|
||||
U_LOG_E("Failed to create video encoder: %s", e.what());
|
||||
return false;
|
||||
}
|
||||
cnx.set_foveated_size(c->settings.preferred.width, c->settings.preferred.height);
|
||||
foveation.emplace(*wivrn_bundle, *cnx.get_hmd().hmd);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool wivrn_comp_target::check_ready()
|
||||
{
|
||||
if (not cnx.connected())
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void wivrn_comp_target::create_images(const comp_target_create_images_info * create_info, vk_bundle_queue * present_queue)
|
||||
{
|
||||
assert(present_queue != nullptr);
|
||||
(void)present_queue;
|
||||
|
||||
// Free old images.
|
||||
destroy_images();
|
||||
|
||||
init_semaphores();
|
||||
|
||||
// will fail on encoder init if bit_depth is arbitrary garbage
|
||||
if (settings[0].bit_depth == 10)
|
||||
format = VK_FORMAT_G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16;
|
||||
else
|
||||
format = VK_FORMAT_G8_B8R8_2PLANE_420_UNORM;
|
||||
|
||||
width = create_info->extent.width;
|
||||
height = create_info->extent.height;
|
||||
surface_transform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
|
||||
|
||||
image_count = 3;
|
||||
color_space = create_info->color_space;
|
||||
|
||||
VkResult res = create_images_impl(vk::ImageUsageFlags(create_info->image_usage));
|
||||
if (res != VK_SUCCESS)
|
||||
{
|
||||
vk_print_result(get_vk(), __FILE__, __LINE__, __func__, res, "create_images");
|
||||
// TODO
|
||||
abort();
|
||||
}
|
||||
create_encoders();
|
||||
}
|
||||
|
||||
bool wivrn_comp_target::has_images()
|
||||
{
|
||||
return images;
|
||||
}
|
||||
|
||||
VkResult wivrn_comp_target::acquire(uint32_t * out_index)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
for (uint32_t i = 0; i < image_count; i++)
|
||||
{
|
||||
if (psc.images[i].status == pseudo_swapchain::status_t::free)
|
||||
{
|
||||
psc.images[i].status = pseudo_swapchain::status_t::acquired;
|
||||
*out_index = i;
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
void wivrn_comp_target::run_present(std::stop_token stop_token, int index, std::vector<std::shared_ptr<video_encoder>> encoders)
|
||||
{
|
||||
auto & vk = *wivrn_bundle;
|
||||
U_LOG_I("Starting encoder thread %d", index);
|
||||
|
||||
const uint8_t status_bit = 1 << (index + 1);
|
||||
|
||||
while (not stop_token.stop_requested())
|
||||
{
|
||||
{
|
||||
auto status = psc.status.load();
|
||||
// Bit 0 to request exit
|
||||
if (status & 1)
|
||||
return;
|
||||
|
||||
if (not(status & status_bit))
|
||||
{
|
||||
psc.status.wait(status);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Get local copies before releasing the image
|
||||
auto view_info = psc.view_info;
|
||||
auto frame_index = psc.frame_index;
|
||||
|
||||
auto res = vk.device.waitForFences(*psc.fence, true, UINT64_MAX);
|
||||
|
||||
try
|
||||
{
|
||||
for (auto & encoder: encoders)
|
||||
{
|
||||
if (encoder->stream_idx < 2 or view_info.alpha)
|
||||
encoder->encode(cnx, view_info, frame_index);
|
||||
}
|
||||
}
|
||||
catch (std::exception & e)
|
||||
{
|
||||
U_LOG_W("encode error: %s", e.what());
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
// Ignore errors
|
||||
}
|
||||
|
||||
// Update encoder status, release image
|
||||
if ((psc.status &= ~status_bit) == 0)
|
||||
{
|
||||
psc.status.notify_all();
|
||||
for (auto & img: psc.images)
|
||||
{
|
||||
if (img.status == pseudo_swapchain::status_t::encoding)
|
||||
{
|
||||
img.status = pseudo_swapchain::status_t::free;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
VkResult wivrn_comp_target::present(
|
||||
vk_bundle_queue * present_queue,
|
||||
uint32_t index,
|
||||
uint64_t timeline_semaphore_value,
|
||||
int64_t desired_present_time_ns,
|
||||
int64_t present_slop_ns)
|
||||
{
|
||||
assert(present_queue != nullptr);
|
||||
(void)present_queue;
|
||||
|
||||
assert(index < image_count);
|
||||
assert(psc.images[index].status == pseudo_swapchain::status_t::acquired);
|
||||
|
||||
auto vk = get_vk();
|
||||
auto res = wivrn_bundle->device.waitForFences(*psc.fence, true, UINT64_MAX);
|
||||
|
||||
vk::Semaphore wait_semaphore(semaphores.render_complete);
|
||||
vk::PipelineStageFlags wait_stage = vk::PipelineStageFlagBits::eTransfer;
|
||||
vk::SubmitInfo submit_info{
|
||||
.waitSemaphoreCount = 1,
|
||||
.pWaitSemaphores = &wait_semaphore,
|
||||
.pWaitDstStageMask = &wait_stage,
|
||||
};
|
||||
|
||||
if (c->base.layer_accum.layer_count == 0 or not cnx.get_offset() or skip_encoding)
|
||||
{
|
||||
scoped_lock lock(vk->main_queue->mutex);
|
||||
wivrn_bundle->queue.submit(submit_info);
|
||||
psc.images[index].status = pseudo_swapchain::status_t::free;
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
auto & command_buffer = psc.command_buffer;
|
||||
auto & psc_image = psc.images[index];
|
||||
|
||||
// Wait for encoders to be done with previous frame
|
||||
for (auto status = psc.status.load(); status != 0; status = psc.status)
|
||||
psc.status.wait(status);
|
||||
|
||||
command_buffer.reset();
|
||||
command_buffer.begin(vk::CommandBufferBeginInfo{
|
||||
.flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit,
|
||||
});
|
||||
|
||||
wivrn_bundle->device.resetFences(*psc.fence);
|
||||
psc_image.status = pseudo_swapchain::status_t::encoding;
|
||||
auto info = pacer.present_to_info(desired_present_time_ns);
|
||||
const bool do_alpha = c->base.layer_accum.data.env_blend_mode == XRT_BLEND_MODE_ALPHA_BLEND;
|
||||
|
||||
bool need_queue_transfer = false;
|
||||
std::vector<vk::Semaphore> present_done_sem;
|
||||
for (auto & encoder: encoders)
|
||||
{
|
||||
if (encoder->stream_idx == 2 and not do_alpha)
|
||||
continue;
|
||||
auto [transfer, sem] = encoder->present_image(
|
||||
psc_image.image,
|
||||
need_queue_transfer,
|
||||
command_buffer,
|
||||
info.frame_id);
|
||||
need_queue_transfer |= transfer;
|
||||
if (sem)
|
||||
present_done_sem.push_back(sem);
|
||||
}
|
||||
|
||||
#if WIVRN_USE_VULKAN_ENCODE
|
||||
if (need_queue_transfer)
|
||||
{
|
||||
vk::ImageMemoryBarrier2 video_barrier{
|
||||
.srcStageMask = vk::PipelineStageFlagBits2KHR::eTransfer,
|
||||
.srcAccessMask = vk::AccessFlagBits2::eMemoryRead,
|
||||
.dstStageMask = vk::PipelineStageFlagBits2KHR::eVideoEncodeKHR,
|
||||
.oldLayout = vk::ImageLayout::eTransferSrcOptimal,
|
||||
.newLayout = vk::ImageLayout::eVideoEncodeSrcKHR,
|
||||
.srcQueueFamilyIndex = vk->main_queue->family_index,
|
||||
.dstQueueFamilyIndex = vk->encode_queue->family_index,
|
||||
.image = psc_image.image,
|
||||
.subresourceRange = {.aspectMask = vk::ImageAspectFlagBits::eColor,
|
||||
.baseMipLevel = 0,
|
||||
.levelCount = 1,
|
||||
.baseArrayLayer = 0,
|
||||
.layerCount = vk::RemainingArrayLayers},
|
||||
};
|
||||
command_buffer.pipelineBarrier2({
|
||||
.imageMemoryBarrierCount = 1,
|
||||
.pImageMemoryBarriers = &video_barrier,
|
||||
});
|
||||
}
|
||||
submit_info.setSignalSemaphores(present_done_sem);
|
||||
#endif
|
||||
command_buffer.end();
|
||||
submit_info.setCommandBuffers(*command_buffer);
|
||||
|
||||
{
|
||||
scoped_lock lock(vk->main_queue->mutex);
|
||||
wivrn_bundle->queue.submit(submit_info, *psc.fence);
|
||||
for (auto & encoder: encoders)
|
||||
{
|
||||
if (encoder->stream_idx == 2 and not do_alpha)
|
||||
continue;
|
||||
encoder->post_submit();
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef XRT_FEATURE_RENDERDOC
|
||||
if (auto r = renderdoc())
|
||||
r->EndFrameCapture(nullptr, nullptr);
|
||||
#endif
|
||||
|
||||
auto & view_info = psc.view_info;
|
||||
view_info.foveation = foveation->get_parameters();
|
||||
view_info.display_time = cnx.get_offset().to_headset(info.predicted_display_time);
|
||||
if (view_info.alpha != do_alpha)
|
||||
pacer.reset();
|
||||
view_info.alpha = do_alpha;
|
||||
for (int eye = 0; eye < 2; ++eye)
|
||||
{
|
||||
const auto & frame_params = c->base.frame_params;
|
||||
view_info.fov[eye] = xrt_cast(frame_params.fovs[eye]);
|
||||
view_info.pose[eye] = xrt_cast(frame_params.poses[eye]);
|
||||
if (c->base.frame_params.one_projection_layer_fast_path)
|
||||
{
|
||||
const auto & proj = c->base.layer_accum.layers[0].data.proj;
|
||||
view_info.pose[eye] = xrt_cast(proj.v[eye].pose);
|
||||
view_info.fov[eye] = xrt_cast(proj.v[eye].fov);
|
||||
}
|
||||
else
|
||||
{
|
||||
xrt_relation_chain xrc{};
|
||||
xrt_space_relation result{};
|
||||
m_relation_chain_push_pose_if_not_identity(&xrc, &frame_params.poses[eye]);
|
||||
m_relation_chain_resolve(&xrc, &result);
|
||||
view_info.pose[eye] = xrt_cast(result.pose);
|
||||
}
|
||||
}
|
||||
// set bits to 1 for index 1..num encoder threads + 1
|
||||
psc.status = (1 << (encoder_threads.size() + 1)) - 2;
|
||||
psc.frame_index = info.frame_id;
|
||||
psc.status.notify_all();
|
||||
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
void wivrn_comp_target::flush()
|
||||
{
|
||||
#ifdef XRT_FEATURE_RENDERDOC
|
||||
if (auto r = renderdoc())
|
||||
r->StartFrameCapture(nullptr, nullptr);
|
||||
#endif
|
||||
|
||||
if (cnx.get_info().eye_gaze)
|
||||
{
|
||||
// FIXME: actually get the gaze data here
|
||||
auto now = os_monotonic_get_ns();
|
||||
cnx.add_tracking_request(device_id::EYE_GAZE, c->base.layer_accum.data.display_time_ns, now, now);
|
||||
}
|
||||
|
||||
vk::CommandBuffer cmd;
|
||||
// apply foveation for current frame
|
||||
if (c->base.frame_params.one_projection_layer_fast_path)
|
||||
{
|
||||
const auto & data = c->base.layer_accum.layers[0].data;
|
||||
const auto & hmd = cnx.get_hmd().hmd;
|
||||
xrt_rect rect[] = {data.proj.v[0].sub.rect, data.proj.v[1].sub.rect};
|
||||
xrt_fov fov[] = {data.proj.v[0].fov, data.proj.v[1].fov};
|
||||
cmd = foveation->update_foveation_buffer(
|
||||
c->nr.distortion.buffer,
|
||||
data.flip_y,
|
||||
rect,
|
||||
fov);
|
||||
}
|
||||
else
|
||||
{
|
||||
const auto & hmd = cnx.get_hmd().hmd;
|
||||
xrt_rect rect[] = {
|
||||
{.extent = {
|
||||
.w = int(hmd->views[0].display.w_pixels),
|
||||
.h = int(hmd->views[0].display.h_pixels),
|
||||
}},
|
||||
{.extent = {
|
||||
.w = int(hmd->views[1].display.w_pixels),
|
||||
.h = int(hmd->views[1].display.h_pixels),
|
||||
}},
|
||||
};
|
||||
cmd = foveation->update_foveation_buffer(
|
||||
c->nr.distortion.buffer,
|
||||
false,
|
||||
rect,
|
||||
hmd->distortion.fov);
|
||||
}
|
||||
|
||||
auto vk = get_vk();
|
||||
|
||||
vk::Semaphore sem(semaphores.present_complete);
|
||||
|
||||
vk::SubmitInfo submit_info{
|
||||
.commandBufferCount = cmd ? 1u : 0,
|
||||
.pCommandBuffers = &cmd,
|
||||
.signalSemaphoreCount = 1,
|
||||
.pSignalSemaphores = &sem,
|
||||
};
|
||||
|
||||
{
|
||||
scoped_lock lock(vk->main_queue->mutex);
|
||||
wivrn_bundle->queue.submit(submit_info);
|
||||
}
|
||||
}
|
||||
|
||||
void wivrn_comp_target::calc_frame_pacing(
|
||||
int64_t * out_frame_id,
|
||||
int64_t * out_wake_up_time_ns,
|
||||
int64_t * out_desired_present_time_ns,
|
||||
int64_t * out_present_slop_ns,
|
||||
int64_t * out_predicted_display_time_ns)
|
||||
{
|
||||
pacer.predict(
|
||||
*out_frame_id,
|
||||
*out_wake_up_time_ns,
|
||||
*out_desired_present_time_ns,
|
||||
*out_present_slop_ns,
|
||||
*out_predicted_display_time_ns);
|
||||
}
|
||||
|
||||
void wivrn_comp_target::mark_timing_point(
|
||||
enum comp_target_timing_point point,
|
||||
int64_t frame_id,
|
||||
int64_t when_ns)
|
||||
{
|
||||
pacer.mark_timing_point(point, frame_id, when_ns);
|
||||
|
||||
switch (point)
|
||||
{
|
||||
case COMP_TARGET_TIMING_POINT_WAKE_UP:
|
||||
cnx.dump_time("wake_up", frame_id, when_ns);
|
||||
break;
|
||||
case COMP_TARGET_TIMING_POINT_BEGIN:
|
||||
cnx.dump_time("begin", frame_id, when_ns);
|
||||
break;
|
||||
case COMP_TARGET_TIMING_POINT_SUBMIT_BEGIN:
|
||||
break;
|
||||
case COMP_TARGET_TIMING_POINT_SUBMIT_END:
|
||||
cnx.dump_time("submit", frame_id, when_ns);
|
||||
break;
|
||||
default:
|
||||
assert(false);
|
||||
}
|
||||
}
|
||||
|
||||
VkResult wivrn_comp_target::update_timings()
|
||||
{
|
||||
// TODO
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
void wivrn_comp_target::set_title(const char * title)
|
||||
{}
|
||||
|
||||
xrt_result_t wivrn_comp_target::get_refresh_rates(uint32_t * count, float * refresh_rates_hz)
|
||||
{
|
||||
const auto & rates = cnx.get_info().available_refresh_rates;
|
||||
*count = std::min<uint32_t>(rates.size(), XRT_MAX_SUPPORTED_REFRESH_RATES);
|
||||
std::copy_n(rates.begin(), *count, refresh_rates_hz);
|
||||
return XRT_SUCCESS;
|
||||
}
|
||||
|
||||
xrt_result_t wivrn_comp_target::get_current_refresh_rate(float * refresh_rate_hz)
|
||||
{
|
||||
*refresh_rate_hz = desc.fps;
|
||||
return XRT_SUCCESS;
|
||||
}
|
||||
|
||||
xrt_result_t wivrn_comp_target::request_refresh_rate(float refresh_rate_hz)
|
||||
{
|
||||
requested_refresh_rate = refresh_rate_hz;
|
||||
if (refresh_rate_hz == 0.0f)
|
||||
refresh_rate_hz = get_default_rate(cnx.get_info(), *cnx.get_settings());
|
||||
|
||||
try
|
||||
{
|
||||
cnx.send_control(to_headset::refresh_rate_change{.fps = refresh_rate_hz});
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
// ignore network errors
|
||||
}
|
||||
|
||||
return XRT_SUCCESS;
|
||||
}
|
||||
|
||||
wivrn_comp_target::~wivrn_comp_target()
|
||||
{
|
||||
destroy_images();
|
||||
}
|
||||
|
||||
void wivrn_comp_target::destroy()
|
||||
{
|
||||
cnx.unset_comp_target();
|
||||
delete this;
|
||||
}
|
||||
|
||||
void wivrn_comp_target::info_gpu(int64_t frame_id, int64_t gpu_start_ns, int64_t gpu_end_ns, int64_t when_ns)
|
||||
{
|
||||
COMP_TRACE_MARKER();
|
||||
}
|
||||
|
||||
void wivrn_comp_target::on_feedback(const from_headset::feedback & feedback, const clock_offset & o)
|
||||
{
|
||||
uint8_t stream = feedback.stream_index;
|
||||
if (psc.status & 1)
|
||||
return;
|
||||
if (encoders.size() <= stream)
|
||||
return;
|
||||
encoders[stream]->on_feedback(feedback);
|
||||
if (not o)
|
||||
return;
|
||||
pacer.on_feedback(feedback, o);
|
||||
}
|
||||
|
||||
void wivrn_comp_target::reset_encoders()
|
||||
{
|
||||
pacer.reset();
|
||||
for (auto & encoder: encoders)
|
||||
encoder->reset();
|
||||
cnx.send_control(to_headset::video_stream_description{desc});
|
||||
}
|
||||
|
||||
float wivrn_comp_target::get_requested_refresh_rate() const
|
||||
{
|
||||
return requested_refresh_rate;
|
||||
}
|
||||
void wivrn_comp_target::reset_requested_refresh_rate()
|
||||
{
|
||||
requested_refresh_rate = 0;
|
||||
}
|
||||
|
||||
void wivrn_comp_target::pause()
|
||||
{
|
||||
skip_encoding = true;
|
||||
}
|
||||
|
||||
void wivrn_comp_target::resume()
|
||||
{
|
||||
if (!skip_encoding)
|
||||
return;
|
||||
|
||||
reset_encoders();
|
||||
skip_encoding = false;
|
||||
}
|
||||
|
||||
void wivrn_comp_target::set_bitrate(uint32_t bitrate_bps)
|
||||
{
|
||||
for (auto & encoder: encoders)
|
||||
{
|
||||
auto encoder_bps = (uint32_t)(bitrate_bps * encoder->bitrate_multiplier);
|
||||
U_LOG_D("Encoder %d bitrate: %d", encoder->stream_idx, encoder_bps);
|
||||
encoder->set_bitrate(encoder_bps);
|
||||
}
|
||||
}
|
||||
|
||||
void wivrn_comp_target::set_refresh_rate(float refresh_rate_hz)
|
||||
{
|
||||
U_LOG_I("Refresh rate change from %.0f to %.0f", desc.fps, refresh_rate_hz);
|
||||
desc.fps = refresh_rate_hz;
|
||||
c->frame_interval_ns = U_TIME_1S_IN_NS / refresh_rate_hz;
|
||||
pacer.set_frame_duration(c->frame_interval_ns);
|
||||
for (auto & encoder: encoders)
|
||||
encoder->set_framerate(refresh_rate_hz);
|
||||
}
|
||||
|
||||
float wivrn_comp_target::get_refresh_rate()
|
||||
{
|
||||
return desc.fps;
|
||||
}
|
||||
|
||||
wivrn_comp_target::wivrn_comp_target(wivrn::wivrn_session & cnx, struct comp_compositor * c) :
|
||||
comp_target{
|
||||
.c = c,
|
||||
.init_pre_vulkan = method_pointer<&wivrn_comp_target::init_pre_vulkan>,
|
||||
.init_post_vulkan = method_pointer<&wivrn_comp_target::init_post_vulkan>,
|
||||
.check_ready = method_pointer<&wivrn_comp_target::check_ready>,
|
||||
.create_images = method_pointer<&wivrn_comp_target::create_images>,
|
||||
.has_images = method_pointer<&wivrn_comp_target::has_images>,
|
||||
.acquire = method_pointer<&wivrn_comp_target::acquire>,
|
||||
.present = method_pointer<&wivrn_comp_target::present>,
|
||||
.flush = method_pointer<&wivrn_comp_target::flush>,
|
||||
.calc_frame_pacing = method_pointer<&wivrn_comp_target::calc_frame_pacing>,
|
||||
.mark_timing_point = method_pointer<&wivrn_comp_target::mark_timing_point>,
|
||||
.update_timings = method_pointer<&wivrn_comp_target::update_timings>,
|
||||
.info_gpu = method_pointer<&wivrn_comp_target::info_gpu>,
|
||||
.set_title = method_pointer<&wivrn_comp_target::set_title>,
|
||||
.get_refresh_rates = method_pointer<&wivrn_comp_target::get_refresh_rates>,
|
||||
.get_current_refresh_rate = method_pointer<&wivrn_comp_target::get_current_refresh_rate>,
|
||||
.request_refresh_rate = method_pointer<&wivrn_comp_target::request_refresh_rate>,
|
||||
.destroy = method_pointer<&wivrn_comp_target::destroy>,
|
||||
},
|
||||
desc{.fps = get_default_rate(cnx.get_info(), *cnx.get_settings())},
|
||||
cnx(cnx),
|
||||
pacer(U_TIME_1S_IN_NS / desc.fps)
|
||||
{
|
||||
c->frame_interval_ns = U_TIME_1S_IN_NS / desc.fps;
|
||||
}
|
||||
} // namespace wivrn
|
||||
@@ -1,176 +0,0 @@
|
||||
/*
|
||||
* WiVRn VR streaming
|
||||
* Copyright (C) 2022 Guillaume Meunier <guillaume.meunier@centraliens.net>
|
||||
* Copyright (C) 2022 Patrick Nicolas <patricknicolas@laposte.net>
|
||||
* Copyright (C) 2026 Sapphire <imsapphire0@gmail.com>
|
||||
*
|
||||
* 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 "main/comp_compositor.h"
|
||||
#include "main/comp_target.h"
|
||||
|
||||
#include "encoder/encoder_settings.h"
|
||||
#include "utils/wivrn_vk_bundle.h"
|
||||
#include "vk/allocation.h"
|
||||
#include "wivrn_foveation.h"
|
||||
#include "wivrn_pacer.h"
|
||||
#include "wivrn_packets.h"
|
||||
|
||||
#include <array>
|
||||
#include <list>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <thread>
|
||||
#include <vulkan/vulkan_raii.hpp>
|
||||
|
||||
namespace wivrn
|
||||
{
|
||||
|
||||
class wivrn_session;
|
||||
class video_encoder;
|
||||
|
||||
struct pseudo_swapchain
|
||||
{
|
||||
#ifdef __cpp_lib_atomic_lock_free_type_aliases
|
||||
using status_type = std::atomic_unsigned_lock_free;
|
||||
#else
|
||||
using status_type = std::atomic_uint64_t;
|
||||
#endif
|
||||
enum class status_t
|
||||
{
|
||||
free,
|
||||
acquired,
|
||||
encoding,
|
||||
};
|
||||
struct item
|
||||
{
|
||||
image_allocation image;
|
||||
vk::raii::ImageView image_view_y = nullptr;
|
||||
vk::raii::ImageView image_view_cbcr = nullptr;
|
||||
status_t status;
|
||||
};
|
||||
std::vector<item> images;
|
||||
|
||||
// bitmask of encoder status, first bit to request exit, then one bit per thread:
|
||||
// 0 when encoder is done
|
||||
// 1 when busy/image to be encoded
|
||||
status_type status;
|
||||
|
||||
// Data to be encoded
|
||||
vk::raii::Fence fence = nullptr;
|
||||
vk::raii::CommandBuffer command_buffer = nullptr;
|
||||
|
||||
int64_t frame_index;
|
||||
to_headset::video_stream_data_shard::view_info_t view_info{};
|
||||
};
|
||||
|
||||
class wivrn_comp_target : public comp_target
|
||||
{
|
||||
to_headset::video_stream_description desc{};
|
||||
|
||||
std::optional<wivrn_vk_bundle> wivrn_bundle;
|
||||
vk::raii::CommandPool command_pool = nullptr;
|
||||
|
||||
int64_t current_frame_id = 0;
|
||||
|
||||
pseudo_swapchain psc;
|
||||
|
||||
VkColorSpaceKHR color_space;
|
||||
|
||||
std::array<encoder_settings, 3> settings;
|
||||
std::list<std::jthread> encoder_threads;
|
||||
std::vector<std::shared_ptr<video_encoder>> encoders;
|
||||
|
||||
wivrn::wivrn_session & cnx;
|
||||
|
||||
std::atomic<float> requested_refresh_rate;
|
||||
std::atomic<bool> skip_encoding;
|
||||
|
||||
inline vk_bundle * get_vk()
|
||||
{
|
||||
return &c->base.vk;
|
||||
}
|
||||
|
||||
void init_semaphores();
|
||||
void fini_semaphores();
|
||||
VkResult create_images_impl(vk::ImageUsageFlags flags);
|
||||
void destroy_images();
|
||||
void create_encoders();
|
||||
|
||||
void run_present(std::stop_token stop_token, int index, std::vector<std::shared_ptr<video_encoder>> encoders);
|
||||
|
||||
bool init_pre_vulkan();
|
||||
bool init_post_vulkan(uint32_t preferred_width, uint32_t preferred_height);
|
||||
bool check_ready();
|
||||
void create_images(const struct comp_target_create_images_info * create_info, struct vk_bundle_queue * present_queue);
|
||||
bool has_images();
|
||||
VkResult acquire(uint32_t * out_index);
|
||||
VkResult present(struct vk_bundle_queue * present_queue,
|
||||
uint32_t index,
|
||||
uint64_t timeline_semaphore_value,
|
||||
int64_t desired_present_time_ns,
|
||||
int64_t present_slop_ns);
|
||||
void flush();
|
||||
void calc_frame_pacing(int64_t * out_frame_id,
|
||||
int64_t * out_wake_up_time_ns,
|
||||
int64_t * out_desired_present_time_ns,
|
||||
int64_t * out_present_slop_ns,
|
||||
int64_t * out_predicted_display_time_ns);
|
||||
void mark_timing_point(enum comp_target_timing_point point,
|
||||
int64_t frame_id,
|
||||
int64_t when_ns);
|
||||
VkResult update_timings();
|
||||
void info_gpu(int64_t frame_id, int64_t gpu_start_ns, int64_t gpu_end_ns, int64_t when_ns);
|
||||
void set_title(const char * title);
|
||||
xrt_result_t get_refresh_rates(uint32_t * count, float * refresh_rates_hz);
|
||||
xrt_result_t get_current_refresh_rate(float * refresh_rate_hz);
|
||||
xrt_result_t request_refresh_rate(float refresh_rate_hz);
|
||||
void destroy();
|
||||
|
||||
public:
|
||||
static std::vector<const char *> wanted_instance_extensions;
|
||||
static std::vector<const char *> wanted_device_extensions;
|
||||
|
||||
wivrn_pacer pacer;
|
||||
std::optional<wivrn_foveation> foveation;
|
||||
|
||||
using base = comp_target;
|
||||
wivrn_comp_target(wivrn::wivrn_session & cnx, struct comp_compositor * c);
|
||||
~wivrn_comp_target();
|
||||
|
||||
void on_feedback(const from_headset::feedback &, const clock_offset &);
|
||||
void reset_encoders();
|
||||
|
||||
float get_requested_refresh_rate() const;
|
||||
void reset_requested_refresh_rate();
|
||||
|
||||
void pause();
|
||||
void resume();
|
||||
|
||||
void set_bitrate(uint32_t bitrate_bps);
|
||||
void set_refresh_rate(float);
|
||||
float get_refresh_rate();
|
||||
};
|
||||
|
||||
inline float get_default_rate(const from_headset::headset_info_packet & info, const from_headset::settings_changed & settings)
|
||||
{
|
||||
if (settings.preferred_refresh_rate)
|
||||
return settings.preferred_refresh_rate;
|
||||
return info.available_refresh_rates.back();
|
||||
}
|
||||
|
||||
} // namespace wivrn
|
||||
@@ -50,7 +50,7 @@ class wivrn_controller : public xrt_device
|
||||
wivrn::wivrn_session * cnx;
|
||||
|
||||
public:
|
||||
using base = xrt_device;
|
||||
using base_t = xrt_device;
|
||||
wivrn_controller(xrt_device_name name, int hand_id, xrt_device * hmd, wivrn::wivrn_session * cnx);
|
||||
|
||||
xrt_result_t update_inputs();
|
||||
|
||||
@@ -42,7 +42,7 @@ class wivrn_eye_tracker : public xrt_device
|
||||
wivrn_session & cnx;
|
||||
|
||||
public:
|
||||
using base = xrt_device;
|
||||
using base_t = xrt_device;
|
||||
wivrn_eye_tracker(wivrn_session &);
|
||||
|
||||
xrt_result_t update_inputs();
|
||||
|
||||
@@ -94,7 +94,7 @@ class wivrn_fb_face2_tracker : public xrt_device
|
||||
wivrn::wivrn_session & cnx;
|
||||
|
||||
public:
|
||||
using base = xrt_device;
|
||||
using base_t = xrt_device;
|
||||
wivrn_fb_face2_tracker(xrt_device * hmd, wivrn::wivrn_session & cnx);
|
||||
|
||||
xrt_result_t update_inputs();
|
||||
|
||||
@@ -1,477 +0,0 @@
|
||||
/*
|
||||
* WiVRn VR streaming
|
||||
* Copyright (C) 2024 galister <galister@librevr.org>
|
||||
* Copyright (C) 2025 Patrick Nicolas <patricknicolas@laposte.net>
|
||||
*
|
||||
* 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 "render/render_interface.h"
|
||||
|
||||
#include "wivrn_foveation.h"
|
||||
|
||||
#include "driver/xrt_cast.h"
|
||||
#include "utils/wivrn_vk_bundle.h"
|
||||
#include "wivrn_packets.h"
|
||||
#include "xrt/xrt_defines.h"
|
||||
|
||||
#include <array>
|
||||
#include <cmath>
|
||||
#include <map>
|
||||
#include <ranges>
|
||||
#include <vulkan/vulkan_raii.hpp>
|
||||
#include <vulkan/vulkan_structs.hpp>
|
||||
#include <openxr/openxr.h>
|
||||
|
||||
extern const std::map<std::string, std::vector<uint32_t>> shaders;
|
||||
|
||||
bool render_distortion_images_ensure(struct render_resources * r,
|
||||
struct vk_bundle * vk,
|
||||
struct xrt_device * xdev,
|
||||
bool pre_rotate)
|
||||
{
|
||||
if (r->distortion.buffer == VK_NULL_HANDLE)
|
||||
r->distortion.buffer = wivrn::wivrn_foveation::instance().get_gpu_buffer();
|
||||
return true;
|
||||
}
|
||||
|
||||
void render_distortion_images_fini(struct render_resources * r)
|
||||
{
|
||||
r->distortion.buffer = VK_NULL_HANDLE;
|
||||
}
|
||||
|
||||
// a, b: parameters computed by solve_foveation
|
||||
// λ: pixel ratio between full size and foveated image (in range ]0,1[)
|
||||
// c: coordinates in -1,1 range of the full size image where pixel ratio must be 1:1
|
||||
// x: coordinates in -1,1 range of the foveated image
|
||||
// result: full size image coordinates in -1,1 range
|
||||
static double defoveate(double a, double b, double λ, double c, double x)
|
||||
{
|
||||
// In order to save encoding, transmit and decoding time, only a portion of the image is encoded in full resolution.
|
||||
// on each axis, foveated coordinates are defined by the following formula.
|
||||
return λ / a * tan(a * x + b) + c;
|
||||
// a and b are defined such as:
|
||||
// edges of the image are not moved
|
||||
// f(-1) = -1
|
||||
// f( 1) = 1
|
||||
// the function also enforces pixel ratio 1:1 at fovea
|
||||
// df⁻¹(x)/dx = 1/scale for x = c
|
||||
|
||||
// We then ensure that source and destination pixel grids match by
|
||||
// rounding to integer pixel ratios: 1:1, 1:2 etc.
|
||||
// Finally, pixel spans are sorted so that we only have increasing
|
||||
// ratios going out from the center.
|
||||
}
|
||||
|
||||
static std::tuple<float, float> solve_foveation(float λ, float c)
|
||||
{
|
||||
// Compute a and b for the foveation function such that:
|
||||
// foveate(a, b, scale, c, -1) = -1 (eq. 1)
|
||||
// foveate(a, b, scale, c, 1) = 1 (eq. 2)
|
||||
//
|
||||
// Use eq. 2 to express a as function of b, then replace in eq. 1
|
||||
// equation that needs to be null is:
|
||||
auto b = [λ, c](double a) { return atan(a * (1 - c) / λ) - a; };
|
||||
auto eq = [λ, c](double a) { return atan(a * (1 - c) / λ) + atan(a * (1 + c) / λ) - 2 * a; }; // (eq. 3)
|
||||
|
||||
// function starts positive, reaches a maximum then decreases to -∞
|
||||
double a0 = 0;
|
||||
// Find a negative value by computing eq(2^n)
|
||||
double a1 = 1;
|
||||
while (eq(a1) > 0)
|
||||
a1 *= 2;
|
||||
|
||||
// last computed values for f(a0) and f(a1)
|
||||
std::optional<double> f_a0;
|
||||
double f_a1 = eq(a1);
|
||||
|
||||
int n = 0;
|
||||
double a = 0;
|
||||
while (std::abs(a1 - a0) > 0.0000001 && n++ < 100)
|
||||
{
|
||||
if (not f_a0)
|
||||
{
|
||||
// use binary search
|
||||
a = 0.5 * (a0 + a1);
|
||||
double val = eq(a);
|
||||
if (val > 0)
|
||||
{
|
||||
a0 = a;
|
||||
f_a0 = val;
|
||||
}
|
||||
else
|
||||
{
|
||||
a1 = a;
|
||||
f_a1 = val;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// f(a1) is always defined
|
||||
// when f(a0) is defined, use secant method
|
||||
a = a1 - f_a1 * (a1 - a0) / (f_a1 - *f_a0);
|
||||
a0 = a1;
|
||||
a1 = a;
|
||||
f_a0 = f_a1;
|
||||
f_a1 = eq(a);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
l = tan(l);
|
||||
r = tan(r);
|
||||
float res = std::clamp((e - l) / (r - l) * 2 - 1, -1.f, 1.f);
|
||||
// If the center isn't in the FoV, fallback to middle of image
|
||||
if (std::isnan(res))
|
||||
return 0;
|
||||
return res;
|
||||
}
|
||||
|
||||
static float convergence_angle(float distance, float eye_x, float gaze_yaw)
|
||||
{
|
||||
auto b = distance * std::sin(gaze_yaw) - eye_x;
|
||||
return std::asin(b / distance);
|
||||
}
|
||||
|
||||
static void fill_param_2d(
|
||||
float c,
|
||||
size_t foveated_dim,
|
||||
size_t source_dim,
|
||||
std::vector<uint16_t> & out)
|
||||
{
|
||||
float scale = float(foveated_dim) / source_dim;
|
||||
auto [a, b] = solve_foveation(scale, c);
|
||||
|
||||
uint16_t last = 0;
|
||||
std::vector<uint16_t> left; // index 0: 1:1 ratio, then 2:1 etc.
|
||||
std::vector<uint16_t> right;
|
||||
for (size_t i = 1; i < foveated_dim; ++i)
|
||||
{
|
||||
double u = (i * 2.) / foveated_dim - 1;
|
||||
auto f = defoveate(a, b, scale, c, u);
|
||||
uint16_t n = std::clamp<uint16_t>((f * 0.5 + 0.5) * source_dim + 0.5, 0, source_dim);
|
||||
assert(n > last);
|
||||
size_t count = n - last;
|
||||
auto & vec = u < c ? left : right;
|
||||
if (count > vec.size())
|
||||
vec.resize(count);
|
||||
vec[count - 1]++;
|
||||
last = n;
|
||||
}
|
||||
assert(last < source_dim);
|
||||
size_t count = source_dim - last;
|
||||
if (count > right.size())
|
||||
right.resize(count);
|
||||
right[count - 1]++;
|
||||
|
||||
count = std::max(left.size(), right.size());
|
||||
out.clear();
|
||||
out.resize(count - left.size());
|
||||
out.insert(out.end(), left.rbegin(), left.rend());
|
||||
if (not right.empty())
|
||||
out.back() += right.front();
|
||||
if (right.size() > 1)
|
||||
out.insert(out.end(), right.begin() + 1, right.end());
|
||||
out.resize(count * 2 - 1);
|
||||
}
|
||||
|
||||
namespace wivrn
|
||||
{
|
||||
|
||||
void wivrn_foveation::compute_params(
|
||||
xrt_rect src[2],
|
||||
const xrt_fov fovs[2])
|
||||
{
|
||||
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 = fovs[i];
|
||||
|
||||
size_t extent_w = std::abs(src[i].extent.w);
|
||||
if (foveated_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);
|
||||
fill_param_2d(center, foveated_width, extent_w, params[i].x);
|
||||
}
|
||||
else
|
||||
params[i].x = {uint16_t(src[i].extent.w)};
|
||||
|
||||
size_t extent_h = std::abs(src[i].extent.h);
|
||||
if (foveated_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);
|
||||
fill_param_2d(center, foveated_height, extent_h, params[i].y);
|
||||
}
|
||||
else
|
||||
params[i].y = {uint16_t(src[i].extent.h)};
|
||||
}
|
||||
}
|
||||
|
||||
wivrn_foveation::wivrn_foveation(wivrn_vk_bundle & bundle, const xrt_hmd_parts & hmd) :
|
||||
foveated_width(hmd.screens[0].w_pixels),
|
||||
foveated_height(hmd.screens[0].h_pixels),
|
||||
// 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*/),
|
||||
command_pool(bundle.device, vk::CommandPoolCreateInfo{.queueFamilyIndex = bundle.queue_family_index}),
|
||||
cmd(std::move(bundle.device.allocateCommandBuffers({
|
||||
.commandPool = *command_pool,
|
||||
.commandBufferCount = 1,
|
||||
})[0])),
|
||||
gpu_buffer(
|
||||
bundle.device,
|
||||
{
|
||||
.size = sizeof(render_compute_distortion_foveation_data),
|
||||
.usage = vk::BufferUsageFlagBits::eTransferDst | vk::BufferUsageFlagBits::eStorageBuffer,
|
||||
},
|
||||
VmaAllocationCreateInfo{
|
||||
.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_RANDOM_BIT | VMA_ALLOCATION_CREATE_HOST_ACCESS_ALLOW_TRANSFER_INSTEAD_BIT,
|
||||
.usage = VMA_MEMORY_USAGE_AUTO,
|
||||
},
|
||||
"foveation storage buffer")
|
||||
{
|
||||
if (not(gpu_buffer.properties() & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT))
|
||||
{
|
||||
host_buffer = buffer_allocation(
|
||||
bundle.device,
|
||||
{
|
||||
.size = gpu_buffer.info().size,
|
||||
.usage = vk::BufferUsageFlagBits::eTransferSrc,
|
||||
},
|
||||
{
|
||||
.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_RANDOM_BIT,
|
||||
.usage = VMA_MEMORY_USAGE_AUTO_PREFER_HOST,
|
||||
},
|
||||
"foveation staging buffer");
|
||||
}
|
||||
bundle.name(command_pool, "foveation command pool");
|
||||
bundle.name(cmd, "foveation command buffer");
|
||||
}
|
||||
|
||||
void wivrn_foveation::update_tracking(const from_headset::tracking & tracking, const clock_offset & offset)
|
||||
{
|
||||
std::lock_guard lock(mutex);
|
||||
|
||||
const uint8_t orientation_ok = from_headset::tracking::orientation_valid | from_headset::tracking::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 wivrn_foveation::update_foveation_center_override(const from_headset::override_foveation_center & center)
|
||||
{
|
||||
std::lock_guard lock(mutex);
|
||||
manual_foveation = center;
|
||||
}
|
||||
|
||||
std::array<to_headset::foveation_parameter, 2> wivrn_foveation::get_parameters()
|
||||
{
|
||||
std::lock_guard lock(mutex);
|
||||
return params;
|
||||
}
|
||||
|
||||
static void fill_ubo(
|
||||
std::span<uint32_t> ubo,
|
||||
const std::vector<uint16_t> & params,
|
||||
bool flip,
|
||||
size_t offset,
|
||||
size_t size,
|
||||
[[maybe_unused]] int count)
|
||||
{
|
||||
assert(params.size() % 2 == 1);
|
||||
const int n_ratio = (params.size() - 1) / 2;
|
||||
ubo[0] = offset;
|
||||
if (flip)
|
||||
ubo[0] += size;
|
||||
for (auto [i, n]: std::ranges::enumerate_view(params))
|
||||
{
|
||||
const int n_source = std::abs(n_ratio - int(i)) + 1;
|
||||
for (size_t j = 0; j < n; ++j)
|
||||
{
|
||||
assert(count > 0);
|
||||
if (flip)
|
||||
ubo[1] = ubo[0] - n_source;
|
||||
else
|
||||
ubo[1] = ubo[0] + n_source;
|
||||
ubo = ubo.subspan(1);
|
||||
--count;
|
||||
}
|
||||
}
|
||||
if (not ubo.empty())
|
||||
std::ranges::fill(ubo, ubo[0]);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
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;
|
||||
}
|
||||
|
||||
vk::Buffer wivrn_foveation::get_gpu_buffer()
|
||||
{
|
||||
return gpu_buffer;
|
||||
}
|
||||
|
||||
vk::CommandBuffer wivrn_foveation::update_foveation_buffer(
|
||||
vk::Buffer target,
|
||||
bool flip_y,
|
||||
xrt_rect source[2],
|
||||
xrt_fov fovs[2])
|
||||
{
|
||||
if (not target)
|
||||
return nullptr;
|
||||
|
||||
// Check if the last value is still valid
|
||||
std::lock_guard lock(mutex);
|
||||
if (last.flip_y == flip_y //
|
||||
and last.src[0] == source[0] //
|
||||
and last.src[1] == source[1] //
|
||||
and last.fovs[0] == fovs[0] //
|
||||
and last.fovs[1] == fovs[1] //
|
||||
and (last.gaze == gaze or manual_foveation.enabled) // Ignore the gaze if foveation center is overridden
|
||||
and 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 //
|
||||
)
|
||||
return nullptr;
|
||||
|
||||
assert(target == gpu_buffer);
|
||||
if (host_buffer)
|
||||
{
|
||||
command_pool.reset();
|
||||
cmd.begin({});
|
||||
cmd.copyBuffer(host_buffer, target, vk::BufferCopy{.size = sizeof(render_compute_distortion_foveation_data)});
|
||||
cmd.end();
|
||||
}
|
||||
|
||||
last = {
|
||||
.gaze = gaze,
|
||||
.flip_y = flip_y,
|
||||
.src = {source[0], source[1]},
|
||||
.fovs = {fovs[0], fovs[1]},
|
||||
.eye_x = {eye_x[0], eye_x[1]},
|
||||
.manual_foveation = manual_foveation,
|
||||
};
|
||||
|
||||
compute_params(source, fovs);
|
||||
|
||||
auto ubo = host_buffer ? host_buffer.data<render_compute_distortion_foveation_data>() : gpu_buffer.data<render_compute_distortion_foveation_data>();
|
||||
for (size_t view = 0; view < 2; ++view)
|
||||
{
|
||||
bool flip = false;
|
||||
size_t offset, extent;
|
||||
|
||||
if (source[view].extent.w < 0)
|
||||
{
|
||||
flip = true;
|
||||
offset = source[view].offset.w + source[view].extent.w;
|
||||
extent = -source[view].extent.w;
|
||||
}
|
||||
else
|
||||
{
|
||||
offset = source[view].offset.w;
|
||||
extent = source[view].extent.w;
|
||||
}
|
||||
fill_ubo(std::span(ubo->x + view * RENDER_FOVEATION_BUFFER_DIMENSIONS, RENDER_FOVEATION_BUFFER_DIMENSIONS), params[view].x, flip, offset, extent, foveated_width);
|
||||
|
||||
if (source[view].extent.h < 0)
|
||||
{
|
||||
flip = not flip_y;
|
||||
offset = source[view].offset.h + source[view].extent.h;
|
||||
extent = -source[view].extent.h;
|
||||
}
|
||||
else
|
||||
{
|
||||
flip = flip_y;
|
||||
offset = source[view].offset.h;
|
||||
extent = source[view].extent.h;
|
||||
}
|
||||
fill_ubo(std::span(ubo->y + view * RENDER_FOVEATION_BUFFER_DIMENSIONS, RENDER_FOVEATION_BUFFER_DIMENSIONS), params[view].y, flip, offset, extent, foveated_height);
|
||||
}
|
||||
if (host_buffer)
|
||||
return *cmd;
|
||||
return nullptr;
|
||||
}
|
||||
} // namespace wivrn
|
||||
@@ -1,94 +0,0 @@
|
||||
/*
|
||||
* WiVRn VR streaming
|
||||
* Copyright (C) 2024 galister <galister@librevr.org>
|
||||
* Copyright (C) 2025 Patrick Nicolas <patricknicolas@laposte.net>
|
||||
*
|
||||
* 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 "vk/allocation.h"
|
||||
#include "wivrn_packets.h"
|
||||
#include "xrt/xrt_defines.h"
|
||||
#include "xrt/xrt_device.h"
|
||||
|
||||
#include "utils/singleton.h"
|
||||
#include <mutex>
|
||||
#include <vulkan/vulkan_raii.hpp>
|
||||
|
||||
struct render_resources;
|
||||
|
||||
namespace wivrn
|
||||
{
|
||||
|
||||
struct clock_offset;
|
||||
struct wivrn_vk_bundle;
|
||||
|
||||
class wivrn_foveation : public singleton<wivrn_foveation>
|
||||
{
|
||||
std::mutex mutex;
|
||||
|
||||
const size_t foveated_width; // per eye
|
||||
const size_t foveated_height;
|
||||
|
||||
// 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<to_headset::foveation_parameter, 2> params;
|
||||
|
||||
vk::raii::CommandPool command_pool;
|
||||
vk::raii::CommandBuffer cmd;
|
||||
buffer_allocation gpu_buffer;
|
||||
buffer_allocation host_buffer;
|
||||
|
||||
// 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 = {};
|
||||
};
|
||||
P last;
|
||||
|
||||
// must hold lock on mutex to call it
|
||||
void compute_params(
|
||||
xrt_rect src[2],
|
||||
const xrt_fov fovs[2]);
|
||||
|
||||
public:
|
||||
wivrn_foveation(wivrn_vk_bundle &, const xrt_hmd_parts &);
|
||||
|
||||
void update_tracking(const from_headset::tracking &, const clock_offset &);
|
||||
void update_foveation_center_override(const from_headset::override_foveation_center &);
|
||||
std::array<to_headset::foveation_parameter, 2> get_parameters();
|
||||
|
||||
vk::Buffer get_gpu_buffer();
|
||||
|
||||
vk::CommandBuffer update_foveation_buffer(
|
||||
vk::Buffer target,
|
||||
bool flip_y,
|
||||
xrt_rect src[2],
|
||||
xrt_fov fovs[2]);
|
||||
};
|
||||
} // namespace wivrn
|
||||
@@ -47,7 +47,7 @@ class wivrn_generic_tracker : public xrt_device
|
||||
wivrn_session & cnx;
|
||||
|
||||
public:
|
||||
using base = xrt_device;
|
||||
using base_t = xrt_device;
|
||||
wivrn_generic_tracker(int index, xrt_device * hmd, wivrn_session & cnx);
|
||||
|
||||
xrt_result_t update_inputs();
|
||||
|
||||
@@ -59,7 +59,7 @@ class wivrn_hmd : public xrt_device
|
||||
xrt_result_t get_visibility_mask(xrt_visibility_mask_type, uint32_t view_index, xrt_visibility_mask **);
|
||||
|
||||
public:
|
||||
using base = xrt_device;
|
||||
using base_t = xrt_device;
|
||||
wivrn_hmd(wivrn::wivrn_session * cnx,
|
||||
const from_headset::headset_info_packet & info);
|
||||
|
||||
|
||||
@@ -107,7 +107,7 @@ class wivrn_htc_face_tracker : public xrt_device
|
||||
wivrn::wivrn_session & cnx;
|
||||
|
||||
public:
|
||||
using base = xrt_device;
|
||||
using base_t = xrt_device;
|
||||
wivrn_htc_face_tracker(xrt_device * hmd, wivrn::wivrn_session & cnx);
|
||||
|
||||
xrt_result_t update_inputs();
|
||||
|
||||
@@ -1,205 +0,0 @@
|
||||
/*
|
||||
* WiVRn VR streaming
|
||||
* Copyright (C) 2024 Guillaume Meunier <guillaume.meunier@centraliens.net>
|
||||
* Copyright (C) 2024 Patrick Nicolas <patricknicolas@laposte.net>
|
||||
*
|
||||
* 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 "wivrn_pacer.h"
|
||||
#include "driver/clock_offset.h"
|
||||
#include "os/os_time.h"
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
|
||||
namespace wivrn
|
||||
{
|
||||
|
||||
static const int64_t margin_ns = 3'000'000;
|
||||
static const int64_t slop_ns = 500'000;
|
||||
|
||||
template <typename T>
|
||||
static T lerp_mod(T a, T b, double t, T mod)
|
||||
{
|
||||
if (2 * std::abs(a - b) < mod)
|
||||
return std::lerp(a, b, t);
|
||||
if (a < b)
|
||||
a += mod;
|
||||
else
|
||||
b += mod;
|
||||
return T(std::lerp(a, b, t)) % mod;
|
||||
}
|
||||
|
||||
wivrn_pacer::wivrn_pacer(uint64_t frame_duration) :
|
||||
frame_duration_ns(frame_duration),
|
||||
frame_times(5000),
|
||||
frame_times_compute(frame_times),
|
||||
worker([this](std::stop_token t) {
|
||||
std::vector<XrDuration> samples;
|
||||
while (not t.stop_requested())
|
||||
{
|
||||
samples.clear();
|
||||
{
|
||||
std::unique_lock lock(compute_mutex);
|
||||
compute_cv.wait(lock);
|
||||
for (const auto & time: frame_times_compute)
|
||||
{
|
||||
if (time.decoded > time.present)
|
||||
samples.push_back(time.decoded - time.present);
|
||||
}
|
||||
}
|
||||
if (samples.empty())
|
||||
continue;
|
||||
auto it = samples.begin() + (samples.size() * 995) / 1000;
|
||||
std::ranges::nth_element(samples, it);
|
||||
|
||||
std::unique_lock lock(mutex);
|
||||
safe_present_to_decoded_ns = *it + 1'000'000;
|
||||
}
|
||||
})
|
||||
{}
|
||||
|
||||
wivrn_pacer::~wivrn_pacer()
|
||||
{
|
||||
worker.request_stop();
|
||||
compute_cv.notify_all();
|
||||
}
|
||||
|
||||
uint64_t wivrn_pacer::get_frame_duration()
|
||||
{
|
||||
std::lock_guard lock(mutex);
|
||||
return frame_duration_ns;
|
||||
}
|
||||
|
||||
void wivrn_pacer::set_frame_duration(uint64_t frame_duration_ns)
|
||||
{
|
||||
std::lock_guard lock(mutex);
|
||||
this->frame_duration_ns = frame_duration_ns;
|
||||
}
|
||||
|
||||
void wivrn_pacer::predict(
|
||||
int64_t & frame_id,
|
||||
int64_t & out_wake_up_time_ns,
|
||||
int64_t & out_desired_present_time_ns,
|
||||
int64_t & out_present_slop_ns,
|
||||
int64_t & out_predicted_display_time_ns)
|
||||
{
|
||||
std::lock_guard lock(mutex);
|
||||
frame_id = this->frame_id++;
|
||||
auto now = os_monotonic_get_ns();
|
||||
|
||||
int64_t predicted_client_render = last_ns + frame_duration_ns;
|
||||
// snap to phase
|
||||
predicted_client_render = (predicted_client_render / frame_duration_ns) * frame_duration_ns + client_render_phase_ns;
|
||||
|
||||
if (now + mean_wake_up_to_present_ns + safe_present_to_decoded_ns > predicted_client_render)
|
||||
predicted_client_render += frame_duration_ns * ((now + mean_wake_up_to_present_ns + safe_present_to_decoded_ns - predicted_client_render) / frame_duration_ns);
|
||||
|
||||
out_predicted_display_time_ns = predicted_client_render + mean_render_to_display_ns;
|
||||
out_desired_present_time_ns = predicted_client_render - safe_present_to_decoded_ns;
|
||||
out_wake_up_time_ns = out_desired_present_time_ns - mean_wake_up_to_present_ns + margin_ns; // we should be awoken early by the application
|
||||
last_wake_up_ns = out_wake_up_time_ns;
|
||||
|
||||
last_ns = predicted_client_render;
|
||||
|
||||
in_flight_frames[frame_id % in_flight_frames.size()] = {
|
||||
.frame_id = frame_id,
|
||||
.present_ns = out_desired_present_time_ns,
|
||||
.predicted_display_time = out_predicted_display_time_ns,
|
||||
};
|
||||
|
||||
out_present_slop_ns = slop_ns;
|
||||
}
|
||||
|
||||
void wivrn_pacer::on_feedback(const wivrn::from_headset::feedback & feedback, const clock_offset & offset)
|
||||
{
|
||||
if (feedback.times_displayed > 1 or not feedback.blitted)
|
||||
return;
|
||||
|
||||
std::lock_guard lock(mutex);
|
||||
auto & when = in_flight_frames[feedback.frame_index % in_flight_frames.size()];
|
||||
if (when.frame_id != feedback.frame_index)
|
||||
return;
|
||||
|
||||
auto & times = frame_times[feedback.frame_index % frame_times.size()];
|
||||
if (times.frame_id != feedback.frame_index)
|
||||
{
|
||||
times.frame_id = feedback.frame_index;
|
||||
times.present = when.present_ns;
|
||||
times.decoded = 0;
|
||||
}
|
||||
times.decoded = std::max(times.decoded, offset.from_headset(feedback.received_from_decoder));
|
||||
|
||||
if (feedback.stream_index == 0)
|
||||
{
|
||||
if (feedback.frame_index % 100 == 0)
|
||||
{
|
||||
std::unique_lock lock(compute_mutex);
|
||||
std::swap(frame_times, frame_times_compute);
|
||||
compute_cv.notify_all();
|
||||
}
|
||||
|
||||
client_render_phase_ns = lerp_mod<int64_t>(client_render_phase_ns, offset.from_headset(feedback.blitted) % frame_duration_ns, 0.1, frame_duration_ns);
|
||||
}
|
||||
|
||||
if (feedback.displayed and feedback.displayed > feedback.blitted and feedback.displayed < feedback.blitted + 100'000'000)
|
||||
mean_render_to_display_ns = std::lerp(mean_render_to_display_ns, feedback.displayed - feedback.blitted, 0.1);
|
||||
}
|
||||
void wivrn_pacer::mark_timing_point(
|
||||
comp_target_timing_point point,
|
||||
int64_t frame_id,
|
||||
int64_t when_ns)
|
||||
{
|
||||
switch (point)
|
||||
{
|
||||
//! Woke up after sleeping in wait frame.
|
||||
case COMP_TARGET_TIMING_POINT_WAKE_UP:
|
||||
return;
|
||||
|
||||
//! Began CPU side work for GPU.
|
||||
case COMP_TARGET_TIMING_POINT_BEGIN:
|
||||
return;
|
||||
|
||||
//! Just before submitting work to the GPU.
|
||||
case COMP_TARGET_TIMING_POINT_SUBMIT_BEGIN:
|
||||
return;
|
||||
|
||||
//! Just after submitting work to the GPU.
|
||||
case COMP_TARGET_TIMING_POINT_SUBMIT_END:
|
||||
if (when_ns > last_wake_up_ns and when_ns < last_wake_up_ns + 100'000'000)
|
||||
{
|
||||
std::lock_guard lock(mutex);
|
||||
mean_wake_up_to_present_ns = std::lerp(mean_wake_up_to_present_ns, when_ns - last_wake_up_ns, 0.1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
wivrn_pacer::frame_info wivrn_pacer::present_to_info(int64_t present)
|
||||
{
|
||||
std::lock_guard lock(mutex);
|
||||
for (const auto & info: in_flight_frames)
|
||||
{
|
||||
if (info.present_ns == present)
|
||||
return info;
|
||||
}
|
||||
assert(false);
|
||||
return {};
|
||||
}
|
||||
|
||||
void wivrn_pacer::reset()
|
||||
{
|
||||
std::lock_guard lock(mutex);
|
||||
std::ranges::fill(frame_times, frame_time{});
|
||||
}
|
||||
} // namespace wivrn
|
||||
@@ -1,100 +0,0 @@
|
||||
/*
|
||||
* WiVRn VR streaming
|
||||
* Copyright (C) 2024 Guillaume Meunier <guillaume.meunier@centraliens.net>
|
||||
* Copyright (C) 2024 Patrick Nicolas <patricknicolas@laposte.net>
|
||||
*
|
||||
* 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 "wivrn_packets.h"
|
||||
|
||||
#include <condition_variable>
|
||||
#include <cstdint>
|
||||
#include <main/comp_target.h>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
namespace wivrn
|
||||
{
|
||||
|
||||
struct clock_offset;
|
||||
|
||||
class wivrn_pacer
|
||||
{
|
||||
public:
|
||||
struct frame_info
|
||||
{
|
||||
int64_t frame_id;
|
||||
int64_t present_ns;
|
||||
int64_t predicted_display_time;
|
||||
};
|
||||
|
||||
private:
|
||||
std::mutex mutex;
|
||||
uint64_t frame_duration_ns;
|
||||
int64_t last_ns = 0;
|
||||
int64_t frame_id = 0;
|
||||
|
||||
int64_t client_render_phase_ns = 0;
|
||||
|
||||
int64_t mean_wake_up_to_present_ns = 1'000'000;
|
||||
int64_t safe_present_to_decoded_ns = 0;
|
||||
int64_t mean_render_to_display_ns = 0;
|
||||
|
||||
int64_t last_wake_up_ns = 0;
|
||||
|
||||
struct frame_time
|
||||
{
|
||||
int64_t frame_id = -1;
|
||||
XrTime present = 0;
|
||||
XrTime decoded = 0;
|
||||
};
|
||||
std::vector<frame_time> frame_times;
|
||||
|
||||
std::mutex compute_mutex;
|
||||
std::condition_variable compute_cv;
|
||||
std::vector<frame_time> frame_times_compute;
|
||||
std::jthread worker;
|
||||
|
||||
std::array<frame_info, 8> in_flight_frames;
|
||||
|
||||
public:
|
||||
wivrn_pacer(uint64_t frame_duration);
|
||||
~wivrn_pacer();
|
||||
|
||||
uint64_t get_frame_duration();
|
||||
void set_frame_duration(uint64_t frame_duration);
|
||||
|
||||
void predict(
|
||||
int64_t & out_frame_id,
|
||||
int64_t & out_wake_up_time_ns,
|
||||
int64_t & out_desired_present_time_ns,
|
||||
int64_t & out_present_slop_ns,
|
||||
int64_t & out_predicted_display_time_ns);
|
||||
|
||||
void on_feedback(const wivrn::from_headset::feedback &, const clock_offset &);
|
||||
|
||||
void mark_timing_point(
|
||||
comp_target_timing_point point,
|
||||
int64_t frame_id,
|
||||
int64_t when_ns);
|
||||
|
||||
frame_info present_to_info(int64_t present);
|
||||
|
||||
void reset();
|
||||
};
|
||||
} // namespace wivrn
|
||||
+38
-104
@@ -24,9 +24,6 @@
|
||||
#include "application.h"
|
||||
#include "configuration.h"
|
||||
#include "driver/app_pacer.h"
|
||||
#include "main/comp_compositor.h"
|
||||
#include "main/comp_main_interface.h"
|
||||
#include "main/comp_target.h"
|
||||
#include "server/ipc_server.h"
|
||||
#include "util/u_builders.h"
|
||||
#include "util/u_logging.h"
|
||||
@@ -37,11 +34,9 @@
|
||||
|
||||
#include "audio/audio_setup.h"
|
||||
#include "wivrn_android_face_tracker.h"
|
||||
#include "wivrn_comp_target.h"
|
||||
#include "wivrn_config.h"
|
||||
#include "wivrn_eye_tracker.h"
|
||||
#include "wivrn_fb_face2_tracker.h"
|
||||
#include "wivrn_foveation.h"
|
||||
#include "wivrn_generic_tracker.h"
|
||||
#include "wivrn_htc_face_tracker.h"
|
||||
#include "wivrn_ipc.h"
|
||||
@@ -54,6 +49,7 @@
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <magic_enum.hpp>
|
||||
#include <multi/comp_multi_interface.h>
|
||||
#include <stdexcept>
|
||||
#include <string.h>
|
||||
#include <utility>
|
||||
@@ -70,41 +66,6 @@
|
||||
namespace wivrn
|
||||
{
|
||||
|
||||
struct wivrn_comp_target_factory : public comp_target_factory
|
||||
{
|
||||
wivrn_session & session;
|
||||
|
||||
wivrn_comp_target_factory(wivrn_session & session) :
|
||||
comp_target_factory{
|
||||
.name = "WiVRn",
|
||||
.identifier = "wivrn",
|
||||
.requires_vulkan_for_create = false,
|
||||
.is_deferred = false,
|
||||
.required_instance_version = VK_MAKE_VERSION(1, 3, 0),
|
||||
.required_instance_extensions = wivrn_comp_target::wanted_instance_extensions.data(),
|
||||
.required_instance_extension_count = wivrn_comp_target::wanted_instance_extensions.size(),
|
||||
.optional_device_extensions = wivrn_comp_target::wanted_device_extensions.data(),
|
||||
.optional_device_extension_count = wivrn_comp_target::wanted_device_extensions.size(),
|
||||
.detect = wivrn_comp_target_factory::detect,
|
||||
.create_target = wivrn_comp_target_factory::create_target},
|
||||
session(session)
|
||||
{
|
||||
}
|
||||
|
||||
static bool detect(const struct comp_target_factory * ctf, struct comp_compositor * c)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool create_target(const struct comp_target_factory * ctf, struct comp_compositor * c, struct comp_target ** out_ct)
|
||||
{
|
||||
auto self = (wivrn_comp_target_factory *)ctf;
|
||||
self->session.comp_target = new wivrn_comp_target(self->session, c);
|
||||
*out_ct = self->session.comp_target;
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
bool is_forced_extension(const char * ext_name)
|
||||
{
|
||||
const char * val = std::getenv("WIVRN_FORCE_EXTENSIONS");
|
||||
@@ -122,14 +83,15 @@ wivrn::wivrn_session::wivrn_session(std::unique_ptr<wivrn_connection> connection
|
||||
},
|
||||
connection(std::move(connection)),
|
||||
headset_info(this->connection->info()),
|
||||
settings(get_info().settings),
|
||||
compositor(*this),
|
||||
xrt_system(system),
|
||||
control(*this->connection),
|
||||
hmd(this, get_info()),
|
||||
left_controller(XRT_DEVICE_TOUCH_CONTROLLER, 0, &hmd, this),
|
||||
right_controller(XRT_DEVICE_TOUCH_CONTROLLER, 1, &hmd, this),
|
||||
left_hand_interaction(XRT_DEVICE_EXT_HAND_INTERACTION, 0, &hmd, this),
|
||||
right_hand_interaction(XRT_DEVICE_EXT_HAND_INTERACTION, 1, &hmd, this),
|
||||
settings(get_info().settings)
|
||||
right_hand_interaction(XRT_DEVICE_EXT_HAND_INTERACTION, 1, &hmd, this)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -318,10 +280,11 @@ xrt_result_t wivrn::wivrn_session::create_session(std::unique_ptr<wivrn_connecti
|
||||
return XRT_ERROR_DEVICE_CREATION_FAILED;
|
||||
}
|
||||
|
||||
send_to_main(self->get_info());
|
||||
const auto & info = self->get_info();
|
||||
send_to_main(info);
|
||||
|
||||
wivrn_comp_target_factory ctf(*self);
|
||||
auto xret = comp_main_create_system_compositor(&self->hmd, &ctf, &self->app_pacers, out_xsysc);
|
||||
auto sys_info{self->compositor.sys_info()};
|
||||
auto xret = comp_multi_create_system_compositor(&self->compositor.base, &self->app_pacers, &sys_info, false, out_xsysc);
|
||||
if (xret != XRT_SUCCESS)
|
||||
{
|
||||
U_LOG_E("Failed to create system compositor: %s", u_str_xrt_result(xret));
|
||||
@@ -416,11 +379,7 @@ void wivrn_session::pause_session()
|
||||
if (audio_handle)
|
||||
audio_handle->pause();
|
||||
|
||||
{
|
||||
std::shared_lock lock(comp_target_mutex);
|
||||
if (comp_target)
|
||||
comp_target->pause();
|
||||
}
|
||||
// FIXME: pause compositor
|
||||
}
|
||||
|
||||
void wivrn_session::resume_session()
|
||||
@@ -436,21 +395,18 @@ void wivrn_session::resume_session()
|
||||
send_control(to_headset::feature_control{to_headset::feature_control::hid_input, true});
|
||||
|
||||
{
|
||||
std::shared_lock lock(comp_target_mutex);
|
||||
if (comp_target)
|
||||
float target_fps = default_rate();
|
||||
auto current_fps = compositor.get_refresh_rate();
|
||||
|
||||
if (target_fps != current_fps)
|
||||
{
|
||||
float target_fps = get_default_rate(get_info(), *get_settings());
|
||||
|
||||
if (target_fps != comp_target->get_refresh_rate())
|
||||
{
|
||||
(*this)(from_headset::refresh_rate_changed{
|
||||
.from = comp_target->get_refresh_rate(),
|
||||
.to = target_fps,
|
||||
});
|
||||
}
|
||||
|
||||
comp_target->resume();
|
||||
(*this)(from_headset::refresh_rate_changed{
|
||||
.from = current_fps,
|
||||
.to = target_fps,
|
||||
});
|
||||
}
|
||||
|
||||
// FIXME: resume compositor
|
||||
}
|
||||
|
||||
if (get_info().user_presence)
|
||||
@@ -482,10 +438,12 @@ bool wivrn_session::connected()
|
||||
return connection->is_active();
|
||||
}
|
||||
|
||||
void wivrn_session::unset_comp_target()
|
||||
float wivrn_session::default_rate()
|
||||
{
|
||||
std::lock_guard lock(comp_target_mutex);
|
||||
comp_target = nullptr;
|
||||
auto s = settings.lock();
|
||||
if (s->preferred_refresh_rate)
|
||||
return s->preferred_refresh_rate;
|
||||
return headset_info.available_refresh_rates.back();
|
||||
}
|
||||
|
||||
void wivrn_session::add_tracking_request(device_id device, int64_t at_ns, int64_t produced_ns, int64_t now)
|
||||
@@ -508,11 +466,7 @@ void wivrn_session::operator()(const from_headset::settings_changed & settings)
|
||||
*this->settings.lock() = settings;
|
||||
|
||||
if (settings.bitrate_bps != 0)
|
||||
{
|
||||
std::lock_guard lock(comp_target_mutex);
|
||||
if (comp_target)
|
||||
comp_target->set_bitrate(settings.bitrate_bps);
|
||||
}
|
||||
compositor.set_bitrate(settings.bitrate_bps);
|
||||
|
||||
wivrn_ipc_socket_monado->send(std::move(settings));
|
||||
}
|
||||
@@ -654,11 +608,7 @@ void wivrn_session::operator()(const from_headset::tracking & tracking)
|
||||
|
||||
if (eye_tracker)
|
||||
eye_tracker->update_tracking(tracking, offset);
|
||||
{
|
||||
std::shared_lock lock(comp_target_mutex);
|
||||
if (comp_target)
|
||||
comp_target->foveation->update_tracking(tracking, offset);
|
||||
}
|
||||
compositor.update_tracking(tracking);
|
||||
|
||||
if (android_face_tracker)
|
||||
android_face_tracker->update_tracking(tracking, offset);
|
||||
@@ -670,9 +620,7 @@ void wivrn_session::operator()(const from_headset::tracking & tracking)
|
||||
|
||||
void wivrn_session::operator()(from_headset::override_foveation_center && foveation_center)
|
||||
{
|
||||
std::shared_lock lock(comp_target_mutex);
|
||||
if (comp_target)
|
||||
comp_target->foveation->update_foveation_center_override(foveation_center);
|
||||
compositor.update_foveation_center_override(foveation_center);
|
||||
}
|
||||
|
||||
void wivrn_session::operator()(from_headset::derived_pose && derived)
|
||||
@@ -738,11 +686,7 @@ void wivrn_session::operator()(from_headset::feedback && feedback)
|
||||
clock_offset o = offset_est.get_offset();
|
||||
if (not o)
|
||||
return;
|
||||
{
|
||||
std::shared_lock lock(comp_target_mutex);
|
||||
if (comp_target)
|
||||
comp_target->on_feedback(feedback, o);
|
||||
}
|
||||
compositor.on_feedback(feedback, o);
|
||||
|
||||
if (feedback.received_first_packet)
|
||||
dump_time("receive_begin", feedback.frame_index, o.from_headset(feedback.received_first_packet), feedback.stream_index);
|
||||
@@ -811,11 +755,7 @@ void wivrn_session::operator()(from_headset::user_presence_changed && event)
|
||||
|
||||
void wivrn_session::operator()(from_headset::refresh_rate_changed && event)
|
||||
{
|
||||
{
|
||||
std::shared_lock lock(comp_target_mutex);
|
||||
if (comp_target)
|
||||
comp_target->set_refresh_rate(event.to);
|
||||
}
|
||||
compositor.set_refresh_rate(event.to);
|
||||
push_event(
|
||||
{
|
||||
.display = {
|
||||
@@ -968,9 +908,7 @@ void wivrn_session::operator()(to_monado::disconnect &&)
|
||||
|
||||
void wivrn_session::operator()(to_monado::set_bitrate && data)
|
||||
{
|
||||
std::shared_lock lock(comp_target_mutex);
|
||||
if (comp_target)
|
||||
comp_target->set_bitrate(data.bitrate_bps);
|
||||
compositor.set_bitrate(data.bitrate_bps);
|
||||
}
|
||||
|
||||
void wivrn_session::operator()(to_headset::stream_tab_change && data)
|
||||
@@ -1080,22 +1018,18 @@ void wivrn_session::run_worker(std::stop_token stop)
|
||||
const bool do_control = control.advance(now);
|
||||
if (do_refresh or do_control)
|
||||
{
|
||||
std::shared_lock lock(comp_target_mutex);
|
||||
if (comp_target)
|
||||
if (do_refresh)
|
||||
{
|
||||
if (do_refresh)
|
||||
{
|
||||
{
|
||||
scoped_lock lock(xrt_system.sessions.mutex);
|
||||
if (xrt_system.sessions.count == 0)
|
||||
comp_target->reset_requested_refresh_rate();
|
||||
}
|
||||
if (comp_target->get_requested_refresh_rate() == 0)
|
||||
refresh.adjust(*connection);
|
||||
scoped_lock lock(xrt_system.sessions.mutex);
|
||||
if (xrt_system.sessions.count == 0)
|
||||
compositor.request_display_refresh_rate(0);
|
||||
}
|
||||
if (do_control)
|
||||
control.resolve(comp_target->pacer.get_frame_duration(), tracking_latency);
|
||||
if (compositor.get_requested_refresh_rate() == 0)
|
||||
refresh.adjust(*connection);
|
||||
}
|
||||
if (do_control)
|
||||
control.resolve(compositor.get_frame_duration(), tracking_latency);
|
||||
}
|
||||
poll_session_loss();
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
|
||||
#include "app_pacer.h"
|
||||
#include "clock_offset.h"
|
||||
#include "compositor/compositor.h"
|
||||
#include "inplace_vector.hpp"
|
||||
#include "tracking_control.h"
|
||||
#include "utils/thread_safe.h"
|
||||
@@ -42,7 +43,6 @@
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <optional>
|
||||
#include <shared_mutex>
|
||||
#include <thread>
|
||||
|
||||
struct ipc_server;
|
||||
@@ -59,14 +59,15 @@ class wivrn_fb_face2_tracker;
|
||||
class wivrn_htc_face_tracker;
|
||||
class wivrn_generic_tracker;
|
||||
struct audio_device;
|
||||
struct wivrn_comp_target;
|
||||
struct wivrn_comp_target_factory;
|
||||
|
||||
class wivrn_session : public xrt_system_devices
|
||||
{
|
||||
friend wivrn_comp_target_factory;
|
||||
std::unique_ptr<wivrn_connection> connection;
|
||||
from_headset::headset_info_packet headset_info;
|
||||
// run-time editable settings
|
||||
thread_safe<from_headset::settings_changed> settings;
|
||||
|
||||
wivrn::compositor compositor;
|
||||
pacing_app_factory app_pacers;
|
||||
|
||||
u_system & xrt_system;
|
||||
@@ -100,9 +101,6 @@ class wivrn_session : public xrt_system_devices
|
||||
beman::inplace_vector::inplace_vector<wivrn_generic_tracker, from_headset::body_tracking::max_tracked_poses> generic_trackers;
|
||||
std::optional<wivrn_uinput> uinput_handler;
|
||||
|
||||
std::shared_mutex comp_target_mutex;
|
||||
wivrn_comp_target * comp_target;
|
||||
|
||||
clock_offset_estimator offset_est;
|
||||
std::atomic<XrDuration> tracking_latency; // production to reception time
|
||||
|
||||
@@ -111,9 +109,6 @@ class wivrn_session : public xrt_system_devices
|
||||
|
||||
std::unique_ptr<audio_device> audio_handle;
|
||||
|
||||
// run-time editable settings
|
||||
thread_safe<from_headset::settings_changed> settings;
|
||||
|
||||
// when sessions shall be destroyed, key is client id, value is timestamp
|
||||
thread_safe<std::map<uint32_t, int64_t>> session_loss;
|
||||
|
||||
@@ -123,7 +118,7 @@ class wivrn_session : public xrt_system_devices
|
||||
wivrn_session(std::unique_ptr<wivrn_connection> connection, u_system &);
|
||||
|
||||
public:
|
||||
using base = xrt_system_devices;
|
||||
using base_t = xrt_system_devices;
|
||||
~wivrn_session();
|
||||
|
||||
static xrt_result_t create_session(std::unique_ptr<wivrn_connection> connection,
|
||||
@@ -145,13 +140,13 @@ public:
|
||||
return headset_info;
|
||||
};
|
||||
|
||||
float default_rate();
|
||||
|
||||
locked<from_headset::settings_changed> get_settings()
|
||||
{
|
||||
return settings.lock();
|
||||
}
|
||||
|
||||
void unset_comp_target();
|
||||
|
||||
wivrn_hmd & get_hmd()
|
||||
{
|
||||
return hmd;
|
||||
|
||||
Reference in New Issue
Block a user