Merge remote-tracking branch 'upstream/master'

This commit is contained in:
Vindicus999
2026-03-20 02:28:35 +00:00
3 changed files with 326 additions and 300 deletions
+268 -291
View File
@@ -2,6 +2,7 @@
* WiVRn VR streaming * WiVRn VR streaming
* Copyright (C) 2022 Guillaume Meunier <guillaume.meunier@centraliens.net> * Copyright (C) 2022 Guillaume Meunier <guillaume.meunier@centraliens.net>
* Copyright (C) 2022 Patrick Nicolas <patricknicolas@laposte.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 * 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 * it under the terms of the GNU General Public License as published by
@@ -22,6 +23,7 @@
#include "driver/wivrn_session.h" #include "driver/wivrn_session.h"
#include "encoder/video_encoder.h" #include "encoder/video_encoder.h"
#include "util/u_logging.h" #include "util/u_logging.h"
#include "utils/method.h"
#include "utils/scoped_lock.h" #include "utils/scoped_lock.h"
#include "wivrn_config.h" #include "wivrn_config.h"
#include "wivrn_foveation.h" #include "wivrn_foveation.h"
@@ -72,15 +74,6 @@ std::vector<const char *> wivrn_comp_target::wanted_device_extensions = {
#endif #endif
}; };
static void target_init_semaphores(struct wivrn_comp_target * cn);
static void target_fini_semaphores(struct wivrn_comp_target * cn);
static inline struct vk_bundle * get_vk(struct wivrn_comp_target * cn)
{
return &cn->c->base.vk;
}
#ifdef XRT_FEATURE_RENDERDOC #ifdef XRT_FEATURE_RENDERDOC
static auto renderdoc() static auto renderdoc()
{ {
@@ -103,72 +96,57 @@ static auto renderdoc()
} }
#endif #endif
static void destroy_images(struct wivrn_comp_target * cn) void wivrn_comp_target::init_semaphores()
{ {
if (cn->images == nullptr) auto vk = get_vk();
return; VkResult ret;
if (cn->wivrn_bundle) fini_semaphores();
cn->wivrn_bundle->device.waitIdle();
cn->psc.status = 1; VkSemaphoreCreateInfo info = {
cn->psc.status.notify_all(); .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
cn->encoder_threads.clear(); };
cn->encoders.clear();
cn->psc.images.clear(); ret = vk->vkCreateSemaphore(vk->device, &info, nullptr, &semaphores.present_complete);
if (ret != VK_SUCCESS)
free(cn->images);
cn->images = NULL;
target_fini_semaphores(cn);
}
static void comp_wivrn_present_thread(std::stop_token stop_token, wivrn_comp_target * cn, int index, std::vector<std::shared_ptr<video_encoder>> encoders);
static void create_encoders(wivrn_comp_target * cn)
{
auto vk = get_vk(cn);
assert(cn->encoders.empty());
assert(cn->encoder_threads.empty());
assert(cn->wivrn_bundle);
cn->psc.status = 0;
to_headset::video_stream_description & desc = cn->desc;
desc.width = cn->width;
desc.height = cn->height;
std::map<int, std::vector<std::shared_ptr<video_encoder>>> thread_params;
for (auto [i, settings]: std::ranges::enumerate_view(cn->settings))
{ {
auto & encoder = cn->encoders.emplace_back( COMP_ERROR(c, "vkCreateSemaphore: %s", vk_result_string(ret));
video_encoder::create(*cn->wivrn_bundle, settings, i));
desc.codec[i] = settings.codec;
thread_params[settings.group].emplace_back(encoder);
} }
for (auto & [group, params]: thread_params) semaphores.render_complete_is_timeline = false;
ret = vk->vkCreateSemaphore(vk->device, &info, nullptr, &semaphores.render_complete);
if (ret != VK_SUCCESS)
{ {
auto & thread = cn->encoder_threads.emplace_back( COMP_ERROR(c, "vkCreateSemaphore: %s", vk_result_string(ret));
comp_wivrn_present_thread, cn, cn->encoder_threads.size(), std::move(params)); }
std::string name = "encoder " + std::to_string(group); }
pthread_setname_np(thread.native_handle(), name.c_str()); 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;
} }
cn->cnx.send_control(to_headset::video_stream_description{desc});
} }
static VkResult create_images(struct wivrn_comp_target * cn, vk::ImageUsageFlags flags) VkResult wivrn_comp_target::create_images_impl(vk::ImageUsageFlags flags)
{ {
auto vk = get_vk(cn); auto vk = get_vk();
assert(cn->image_count > 0); assert(image_count > 0);
COMP_DEBUG(cn->c, "Creating %d images.", cn->image_count); COMP_DEBUG(c, "Creating %d images.", image_count);
auto & device = cn->wivrn_bundle->device; auto & device = wivrn_bundle->device;
destroy_images(cn); destroy_images();
auto format = vk::Format(cn->format); auto format = vk::Format(this->format);
bool is_10bit = format == vk::Format::eG10X6B10X6R10X62Plane420Unorm3Pack16; bool is_10bit = format == vk::Format::eG10X6B10X6R10X62Plane420Unorm3Pack16;
@@ -177,7 +155,7 @@ static VkResult create_images(struct wivrn_comp_target * cn, vk::ImageUsageFlags
is_10bit ? vk::Format::eR16G16Unorm : vk::Format::eR8G8Unorm, is_10bit ? vk::Format::eR16G16Unorm : vk::Format::eR8G8Unorm,
format}; format};
cn->images = U_TYPED_ARRAY_CALLOC(struct comp_target_image, cn->image_count); images = U_TYPED_ARRAY_CALLOC(comp_target_image, image_count);
vk::StructureChain image_info{ vk::StructureChain image_info{
vk::ImageCreateInfo{ vk::ImageCreateInfo{
@@ -185,8 +163,8 @@ static VkResult create_images(struct wivrn_comp_target * cn, vk::ImageUsageFlags
.imageType = vk::ImageType::e2D, .imageType = vk::ImageType::e2D,
.format = format, .format = format,
.extent = { .extent = {
.width = cn->width, .width = width,
.height = cn->height, .height = height,
.depth = 1, .depth = 1,
}, },
.mipLevels = 1, .mipLevels = 1,
@@ -205,7 +183,7 @@ static VkResult create_images(struct wivrn_comp_target * cn, vk::ImageUsageFlags
if ( if (
vk->features.video_maintenance_1 and vk->features.video_maintenance_1 and
std::ranges::contains( std::ranges::contains(
cn->settings, settings,
encoder_vulkan, encoder_vulkan,
&encoder_settings::encoder_name)) &encoder_settings::encoder_name))
{ {
@@ -214,21 +192,21 @@ static VkResult create_images(struct wivrn_comp_target * cn, vk::ImageUsageFlags
} }
#endif #endif
cn->psc.images.resize(cn->image_count); psc.images.resize(image_count);
for (uint32_t i = 0; i < cn->image_count; i++) for (uint32_t i = 0; i < image_count; i++)
{ {
auto & image = cn->psc.images[i].image; auto & image = psc.images[i].image;
image = image_allocation( image = image_allocation(
device, device,
image_info.get(), image_info.get(),
{.usage = VMA_MEMORY_USAGE_AUTO}, {.usage = VMA_MEMORY_USAGE_AUTO},
std::format("comp target image {}", i)); std::format("comp target image {}", i));
cn->images[i].handle = image; images[i].handle = image;
} }
for (uint32_t i = 0; i < cn->image_count; i++) for (uint32_t i = 0; i < image_count; i++)
{ {
auto & item = cn->psc.images[i]; auto & item = psc.images[i];
vk::ImageViewUsageCreateInfo usage{ vk::ImageViewUsageCreateInfo usage{
.usage = flags, .usage = flags,
}; };
@@ -256,44 +234,93 @@ static VkResult create_images(struct wivrn_comp_target * cn, vk::ImageUsageFlags
.layerCount = vk::RemainingArrayLayers, .layerCount = vk::RemainingArrayLayers,
}, },
}); });
cn->images[i].view = VkImageView(*item.image_view_y); images[i].view = VkImageView(*item.image_view_y);
cn->images[i].view_cbcr = VkImageView(*item.image_view_cbcr); images[i].view_cbcr = VkImageView(*item.image_view_cbcr);
cn->wivrn_bundle->name(item.image_view_y, "comp target image view (y)"); wivrn_bundle->name(item.image_view_y, "comp target image view (y)");
cn->wivrn_bundle->name(item.image_view_cbcr, "comp target image view (CbCr)"); wivrn_bundle->name(item.image_view_cbcr, "comp target image view (CbCr)");
} }
cn->psc.fence = vk::raii::Fence(device, vk::FenceCreateInfo{.flags = vk::FenceCreateFlagBits::eSignaled}); psc.fence = vk::raii::Fence(device, vk::FenceCreateInfo{.flags = vk::FenceCreateFlagBits::eSignaled});
cn->wivrn_bundle->name(cn->psc.fence, "comp target fence"); wivrn_bundle->name(psc.fence, "comp target fence");
cn->psc.command_buffer = std::move(device.allocateCommandBuffers( psc.command_buffer = std::move(device.allocateCommandBuffers(
{.commandPool = *cn->command_pool, {.commandPool = *command_pool,
.commandBufferCount = 1})[0]); .commandBufferCount = 1})[0]);
cn->wivrn_bundle->name(cn->psc.command_buffer, "comp target command buffer"); wivrn_bundle->name(psc.command_buffer, "comp target command buffer");
return VK_SUCCESS; return VK_SUCCESS;
} }
void wivrn_comp_target::destroy_images()
{
if (images == nullptr)
return;
static bool comp_wivrn_init_pre_vulkan(struct comp_target * ct) 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; return true;
} }
bool wivrn_comp_target::init_post_vulkan(uint32_t preferred_width, uint32_t preferred_height)
static bool comp_wivrn_init_post_vulkan(struct comp_target * ct, uint32_t preferred_width, uint32_t preferred_height)
{ {
struct wivrn_comp_target * cn = (struct wivrn_comp_target *)ct; auto vk = get_vk();
auto vk = get_vk(cn);
try try
{ {
cn->wivrn_bundle.emplace(*vk, wivrn_bundle.emplace(*vk,
wivrn_comp_target::wanted_instance_extensions, wivrn_comp_target::wanted_instance_extensions,
wivrn_comp_target::wanted_device_extensions); wivrn_comp_target::wanted_device_extensions);
cn->command_pool = vk::raii::CommandPool( command_pool = vk::raii::CommandPool(
cn->wivrn_bundle->device, wivrn_bundle->device,
{ {
.flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer, .flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer,
.queueFamilyIndex = vk->main_queue->family_index, .queueFamilyIndex = vk->main_queue->family_index,
}); });
cn->wivrn_bundle->name(cn->command_pool, "comp target command pool"); wivrn_bundle->name(command_pool, "comp target command pool");
} }
catch (std::exception & e) catch (std::exception & e)
{ {
@@ -303,14 +330,14 @@ static bool comp_wivrn_init_post_vulkan(struct comp_target * ct, uint32_t prefer
try try
{ {
cn->settings = get_encoder_settings( settings = get_encoder_settings(
*cn->wivrn_bundle, *wivrn_bundle,
cn->cnx.get_info(), cnx.get_info(),
*cn->cnx.get_settings()); *cnx.get_settings());
print_encoders(cn->settings); print_encoders(settings);
cn->c->settings.preferred.width = cn->settings[0].width; c->settings.preferred.width = settings[0].width;
cn->c->settings.preferred.height = cn->settings[0].height; c->settings.preferred.height = settings[0].height;
} }
catch (const std::exception & e) catch (const std::exception & e)
{ {
@@ -321,114 +348,67 @@ static bool comp_wivrn_init_post_vulkan(struct comp_target * ct, uint32_t prefer
U_LOG_E("Failed to create video encoder: %s", e.what()); U_LOG_E("Failed to create video encoder: %s", e.what());
return false; return false;
} }
cn->cnx.set_foveated_size(cn->c->settings.preferred.width, cn->c->settings.preferred.height); cnx.set_foveated_size(c->settings.preferred.width, c->settings.preferred.height);
cn->foveation.emplace(*cn->wivrn_bundle, *cn->cnx.get_hmd().hmd); foveation.emplace(*wivrn_bundle, *cnx.get_hmd().hmd);
return true; return true;
} }
static bool comp_wivrn_check_ready(struct comp_target * ct) bool wivrn_comp_target::check_ready()
{ {
struct wivrn_comp_target * cn = (struct wivrn_comp_target *)ct; if (not cnx.connected())
if (not cn->cnx.connected())
return false; return false;
return true; return true;
} }
static void target_fini_semaphores(struct wivrn_comp_target * cn) void wivrn_comp_target::create_images(const comp_target_create_images_info * create_info, vk_bundle_queue * present_queue)
{ {
struct vk_bundle * vk = get_vk(cn); assert(present_queue != nullptr);
if (cn->semaphores.present_complete != VK_NULL_HANDLE)
{
vk->vkDestroySemaphore(vk->device, cn->semaphores.present_complete, NULL);
cn->semaphores.present_complete = VK_NULL_HANDLE;
}
if (cn->semaphores.render_complete != VK_NULL_HANDLE)
{
vk->vkDestroySemaphore(vk->device, cn->semaphores.render_complete, NULL);
cn->semaphores.render_complete = VK_NULL_HANDLE;
}
}
static void target_init_semaphores(struct wivrn_comp_target * cn)
{
struct vk_bundle * vk = get_vk(cn);
VkResult ret;
target_fini_semaphores(cn);
VkSemaphoreCreateInfo info = {
.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
};
ret = vk->vkCreateSemaphore(vk->device, &info, NULL, &cn->semaphores.present_complete);
if (ret != VK_SUCCESS)
{
COMP_ERROR(cn->c, "vkCreateSemaphore: %s", vk_result_string(ret));
}
cn->semaphores.render_complete_is_timeline = false;
ret = vk->vkCreateSemaphore(vk->device, &info, NULL, &cn->semaphores.render_complete);
if (ret != VK_SUCCESS)
{
COMP_ERROR(cn->c, "vkCreateSemaphore: %s", vk_result_string(ret));
}
}
static void comp_wivrn_create_images(struct comp_target * ct, const struct comp_target_create_images_info * create_info, struct vk_bundle_queue * present_queue)
{
assert(present_queue != NULL);
(void)present_queue; (void)present_queue;
struct wivrn_comp_target * cn = (struct wivrn_comp_target *)ct;
// Free old images. // Free old images.
destroy_images(cn); destroy_images();
target_init_semaphores(cn); init_semaphores();
// will fail on encoder init if bit_depth is arbitrary garbage // will fail on encoder init if bit_depth is arbitrary garbage
if (cn->settings[0].bit_depth == 10) if (settings[0].bit_depth == 10)
ct->format = VK_FORMAT_G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16; format = VK_FORMAT_G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16;
else else
ct->format = VK_FORMAT_G8_B8R8_2PLANE_420_UNORM; format = VK_FORMAT_G8_B8R8_2PLANE_420_UNORM;
ct->width = create_info->extent.width; width = create_info->extent.width;
ct->height = create_info->extent.height; height = create_info->extent.height;
ct->surface_transform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR; surface_transform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
cn->image_count = 3; image_count = 3;
cn->color_space = create_info->color_space; color_space = create_info->color_space;
VkResult res = create_images(cn, vk::ImageUsageFlags(create_info->image_usage)); VkResult res = create_images_impl(vk::ImageUsageFlags(create_info->image_usage));
if (res != VK_SUCCESS) if (res != VK_SUCCESS)
{ {
vk_print_result(get_vk(cn), __FILE__, __LINE__, __func__, res, "create_images"); vk_print_result(get_vk(), __FILE__, __LINE__, __func__, res, "create_images");
// TODO // TODO
abort(); abort();
} }
create_encoders(cn); create_encoders();
} }
static bool comp_wivrn_has_images(struct comp_target * ct) bool wivrn_comp_target::has_images()
{ {
return ct->images; return images;
} }
static VkResult comp_wivrn_acquire(struct comp_target * ct, uint32_t * out_index) VkResult wivrn_comp_target::acquire(uint32_t * out_index)
{ {
struct wivrn_comp_target * cn = (struct wivrn_comp_target *)ct;
while (true) while (true)
{ {
for (uint32_t i = 0; i < ct->image_count; i++) for (uint32_t i = 0; i < image_count; i++)
{ {
if (cn->psc.images[i].status == pseudo_swapchain::status_t::free) if (psc.images[i].status == pseudo_swapchain::status_t::free)
{ {
cn->psc.images[i].status = pseudo_swapchain::status_t::acquired; psc.images[i].status = pseudo_swapchain::status_t::acquired;
*out_index = i; *out_index = i;
return VK_SUCCESS; return VK_SUCCESS;
} }
@@ -436,9 +416,9 @@ static VkResult comp_wivrn_acquire(struct comp_target * ct, uint32_t * out_index
}; };
} }
static void comp_wivrn_present_thread(std::stop_token stop_token, wivrn_comp_target * cn, int index, std::vector<std::shared_ptr<video_encoder>> encoders) void wivrn_comp_target::run_present(std::stop_token stop_token, int index, std::vector<std::shared_ptr<video_encoder>> encoders)
{ {
auto & vk = *cn->wivrn_bundle; auto & vk = *wivrn_bundle;
U_LOG_I("Starting encoder thread %d", index); U_LOG_I("Starting encoder thread %d", index);
const uint8_t status_bit = 1 << (index + 1); const uint8_t status_bit = 1 << (index + 1);
@@ -446,30 +426,30 @@ static void comp_wivrn_present_thread(std::stop_token stop_token, wivrn_comp_tar
while (not stop_token.stop_requested()) while (not stop_token.stop_requested())
{ {
{ {
auto status = cn->psc.status.load(); auto status = psc.status.load();
// Bit 0 to request exit // Bit 0 to request exit
if (status & 1) if (status & 1)
return; return;
if (not(status & status_bit)) if (not(status & status_bit))
{ {
cn->psc.status.wait(status); psc.status.wait(status);
continue; continue;
} }
} }
// Get local copies before releasing the image // Get local copies before releasing the image
auto view_info = cn->psc.view_info; auto view_info = psc.view_info;
auto frame_index = cn->psc.frame_index; auto frame_index = psc.frame_index;
auto res = vk.device.waitForFences(*cn->psc.fence, true, UINT64_MAX); auto res = vk.device.waitForFences(*psc.fence, true, UINT64_MAX);
try try
{ {
for (auto & encoder: encoders) for (auto & encoder: encoders)
{ {
if (encoder->stream_idx < 2 or view_info.alpha) if (encoder->stream_idx < 2 or view_info.alpha)
encoder->encode(cn->cnx, view_info, frame_index); encoder->encode(cnx, view_info, frame_index);
} }
} }
catch (std::exception & e) catch (std::exception & e)
@@ -482,10 +462,10 @@ static void comp_wivrn_present_thread(std::stop_token stop_token, wivrn_comp_tar
} }
// Update encoder status, release image // Update encoder status, release image
if ((cn->psc.status &= ~status_bit) == 0) if ((psc.status &= ~status_bit) == 0)
{ {
cn->psc.status.notify_all(); psc.status.notify_all();
for (auto & img: cn->psc.images) for (auto & img: psc.images)
{ {
if (img.status == pseudo_swapchain::status_t::encoding) if (img.status == pseudo_swapchain::status_t::encoding)
{ {
@@ -497,25 +477,23 @@ static void comp_wivrn_present_thread(std::stop_token stop_token, wivrn_comp_tar
} }
} }
static VkResult comp_wivrn_present(struct comp_target * ct, VkResult wivrn_comp_target::present(
struct vk_bundle_queue * present_queue, vk_bundle_queue * present_queue,
uint32_t index, uint32_t index,
uint64_t timeline_semaphore_value, uint64_t timeline_semaphore_value,
int64_t desired_present_time_ns, int64_t desired_present_time_ns,
int64_t present_slop_ns) int64_t present_slop_ns)
{ {
assert(present_queue != NULL); assert(present_queue != nullptr);
(void)present_queue; (void)present_queue;
struct wivrn_comp_target * cn = (struct wivrn_comp_target *)ct;
assert(index < cn->image_count); assert(index < cn->image_count);
assert(cn->psc.images[index].status == pseudo_swapchain::status_t::acquired); assert(cn->psc.images[index].status == pseudo_swapchain::status_t::acquired);
struct vk_bundle * vk = get_vk(cn); auto vk = get_vk();
auto res = cn->wivrn_bundle->device.waitForFences(*cn->psc.fence, true, UINT64_MAX); auto res = wivrn_bundle->device.waitForFences(*psc.fence, true, UINT64_MAX);
vk::Semaphore wait_semaphore(cn->semaphores.render_complete); vk::Semaphore wait_semaphore(semaphores.render_complete);
vk::PipelineStageFlags wait_stage = vk::PipelineStageFlagBits::eTransfer; vk::PipelineStageFlags wait_stage = vk::PipelineStageFlagBits::eTransfer;
vk::SubmitInfo submit_info{ vk::SubmitInfo submit_info{
.waitSemaphoreCount = 1, .waitSemaphoreCount = 1,
@@ -523,34 +501,34 @@ static VkResult comp_wivrn_present(struct comp_target * ct,
.pWaitDstStageMask = &wait_stage, .pWaitDstStageMask = &wait_stage,
}; };
if (cn->c->base.layer_accum.layer_count == 0 or not cn->cnx.get_offset() or cn->skip_encoding) if (c->base.layer_accum.layer_count == 0 or not cnx.get_offset() or skip_encoding)
{ {
scoped_lock lock(vk->main_queue->mutex); scoped_lock lock(vk->main_queue->mutex);
cn->wivrn_bundle->queue.submit(submit_info); wivrn_bundle->queue.submit(submit_info);
cn->psc.images[index].status = pseudo_swapchain::status_t::free; psc.images[index].status = pseudo_swapchain::status_t::free;
return VK_SUCCESS; return VK_SUCCESS;
} }
auto & command_buffer = cn->psc.command_buffer; auto & command_buffer = psc.command_buffer;
auto & psc_image = cn->psc.images[index]; auto & psc_image = psc.images[index];
// Wait for encoders to be done with previous frame // Wait for encoders to be done with previous frame
for (auto status = cn->psc.status.load(); status != 0; status = cn->psc.status) for (auto status = psc.status.load(); status != 0; status = psc.status)
cn->psc.status.wait(status); psc.status.wait(status);
command_buffer.reset(); command_buffer.reset();
command_buffer.begin(vk::CommandBufferBeginInfo{ command_buffer.begin(vk::CommandBufferBeginInfo{
.flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit, .flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit,
}); });
cn->wivrn_bundle->device.resetFences(*cn->psc.fence); wivrn_bundle->device.resetFences(*psc.fence);
psc_image.status = pseudo_swapchain::status_t::encoding; psc_image.status = pseudo_swapchain::status_t::encoding;
auto info = cn->pacer.present_to_info(desired_present_time_ns); auto info = pacer.present_to_info(desired_present_time_ns);
const bool do_alpha = cn->c->base.layer_accum.data.env_blend_mode == XRT_BLEND_MODE_ALPHA_BLEND; const bool do_alpha = c->base.layer_accum.data.env_blend_mode == XRT_BLEND_MODE_ALPHA_BLEND;
bool need_queue_transfer = false; bool need_queue_transfer = false;
std::vector<vk::Semaphore> present_done_sem; std::vector<vk::Semaphore> present_done_sem;
for (auto & encoder: cn->encoders) for (auto & encoder: encoders)
{ {
if (encoder->stream_idx == 2 and not do_alpha) if (encoder->stream_idx == 2 and not do_alpha)
continue; continue;
@@ -594,8 +572,8 @@ static VkResult comp_wivrn_present(struct comp_target * ct,
{ {
scoped_lock lock(vk->main_queue->mutex); scoped_lock lock(vk->main_queue->mutex);
cn->wivrn_bundle->queue.submit(submit_info, *cn->psc.fence); wivrn_bundle->queue.submit(submit_info, *psc.fence);
for (auto & encoder: cn->encoders) for (auto & encoder: encoders)
{ {
if (encoder->stream_idx == 2 and not do_alpha) if (encoder->stream_idx == 2 and not do_alpha)
continue; continue;
@@ -605,23 +583,23 @@ static VkResult comp_wivrn_present(struct comp_target * ct,
#ifdef XRT_FEATURE_RENDERDOC #ifdef XRT_FEATURE_RENDERDOC
if (auto r = renderdoc()) if (auto r = renderdoc())
r->EndFrameCapture(NULL, NULL); r->EndFrameCapture(nullptr, nullptr);
#endif #endif
auto & view_info = cn->psc.view_info; auto & view_info = psc.view_info;
view_info.foveation = cn->foveation->get_parameters(); view_info.foveation = foveation->get_parameters();
view_info.display_time = cn->cnx.get_offset().to_headset(info.predicted_display_time); view_info.display_time = cnx.get_offset().to_headset(info.predicted_display_time);
if (view_info.alpha != do_alpha) if (view_info.alpha != do_alpha)
cn->pacer.reset(); pacer.reset();
view_info.alpha = do_alpha; view_info.alpha = do_alpha;
for (int eye = 0; eye < 2; ++eye) for (int eye = 0; eye < 2; ++eye)
{ {
const auto & frame_params = cn->c->base.frame_params; const auto & frame_params = c->base.frame_params;
view_info.fov[eye] = xrt_cast(frame_params.fovs[eye]); view_info.fov[eye] = xrt_cast(frame_params.fovs[eye]);
view_info.pose[eye] = xrt_cast(frame_params.poses[eye]); view_info.pose[eye] = xrt_cast(frame_params.poses[eye]);
if (cn->c->base.frame_params.one_projection_layer_fast_path) if (c->base.frame_params.one_projection_layer_fast_path)
{ {
const auto & proj = cn->c->base.layer_accum.layers[0].data.proj; const auto & proj = c->base.layer_accum.layers[0].data.proj;
view_info.pose[eye] = xrt_cast(proj.v[eye].pose); view_info.pose[eye] = xrt_cast(proj.v[eye].pose);
view_info.fov[eye] = xrt_cast(proj.v[eye].fov); view_info.fov[eye] = xrt_cast(proj.v[eye].fov);
} }
@@ -635,46 +613,44 @@ static VkResult comp_wivrn_present(struct comp_target * ct,
} }
} }
// set bits to 1 for index 1..num encoder threads + 1 // set bits to 1 for index 1..num encoder threads + 1
cn->psc.status = (1 << (cn->encoder_threads.size() + 1)) - 2; psc.status = (1 << (encoder_threads.size() + 1)) - 2;
cn->psc.frame_index = info.frame_id; psc.frame_index = info.frame_id;
cn->psc.status.notify_all(); psc.status.notify_all();
return VK_SUCCESS; return VK_SUCCESS;
} }
static void comp_wivrn_flush(struct comp_target * ct) void wivrn_comp_target::flush()
{ {
struct wivrn_comp_target * cn = (struct wivrn_comp_target *)ct;
#ifdef XRT_FEATURE_RENDERDOC #ifdef XRT_FEATURE_RENDERDOC
if (auto r = renderdoc()) if (auto r = renderdoc())
r->StartFrameCapture(NULL, NULL); r->StartFrameCapture(nullptr, nullptr);
#endif #endif
if (cn->cnx.get_info().eye_gaze) if (cnx.get_info().eye_gaze)
{ {
// FIXME: actually get the gaze data here // FIXME: actually get the gaze data here
auto now = os_monotonic_get_ns(); auto now = os_monotonic_get_ns();
cn->cnx.add_tracking_request(device_id::EYE_GAZE, cn->c->base.layer_accum.data.display_time_ns, now, now); cnx.add_tracking_request(device_id::EYE_GAZE, c->base.layer_accum.data.display_time_ns, now, now);
} }
vk::CommandBuffer cmd; vk::CommandBuffer cmd;
// apply foveation for current frame // apply foveation for current frame
if (cn->c->base.frame_params.one_projection_layer_fast_path) if (c->base.frame_params.one_projection_layer_fast_path)
{ {
const auto & data = cn->c->base.layer_accum.layers[0].data; const auto & data = c->base.layer_accum.layers[0].data;
const auto & hmd = cn->cnx.get_hmd().hmd; const auto & hmd = cnx.get_hmd().hmd;
xrt_rect rect[] = {data.proj.v[0].sub.rect, data.proj.v[1].sub.rect}; 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}; xrt_fov fov[] = {data.proj.v[0].fov, data.proj.v[1].fov};
cmd = cn->foveation->update_foveation_buffer( cmd = foveation->update_foveation_buffer(
ct->c->nr.distortion.buffer, c->nr.distortion.buffer,
data.flip_y, data.flip_y,
rect, rect,
fov); fov);
} }
else else
{ {
const auto & hmd = cn->cnx.get_hmd().hmd; const auto & hmd = cnx.get_hmd().hmd;
xrt_rect rect[] = { xrt_rect rect[] = {
{.extent = { {.extent = {
.w = int(hmd->views[0].display.w_pixels), .w = int(hmd->views[0].display.w_pixels),
@@ -685,16 +661,16 @@ static void comp_wivrn_flush(struct comp_target * ct)
.h = int(hmd->views[1].display.h_pixels), .h = int(hmd->views[1].display.h_pixels),
}}, }},
}; };
cmd = cn->foveation->update_foveation_buffer( cmd = foveation->update_foveation_buffer(
ct->c->nr.distortion.buffer, c->nr.distortion.buffer,
false, false,
rect, rect,
hmd->distortion.fov); hmd->distortion.fov);
} }
struct vk_bundle * vk = get_vk(cn); auto vk = get_vk();
vk::Semaphore sem(cn->semaphores.present_complete); vk::Semaphore sem(semaphores.present_complete);
vk::SubmitInfo submit_info{ vk::SubmitInfo submit_info{
.commandBufferCount = cmd ? 1u : 0, .commandBufferCount = cmd ? 1u : 0,
@@ -705,20 +681,18 @@ static void comp_wivrn_flush(struct comp_target * ct)
{ {
scoped_lock lock(vk->main_queue->mutex); scoped_lock lock(vk->main_queue->mutex);
cn->wivrn_bundle->queue.submit(submit_info); wivrn_bundle->queue.submit(submit_info);
} }
} }
static void comp_wivrn_calc_frame_pacing(struct comp_target * ct, void wivrn_comp_target::calc_frame_pacing(
int64_t * out_frame_id, int64_t * out_frame_id,
int64_t * out_wake_up_time_ns, int64_t * out_wake_up_time_ns,
int64_t * out_desired_present_time_ns, int64_t * out_desired_present_time_ns,
int64_t * out_present_slop_ns, int64_t * out_present_slop_ns,
int64_t * out_predicted_display_time_ns) int64_t * out_predicted_display_time_ns)
{ {
struct wivrn_comp_target * cn = (struct wivrn_comp_target *)ct; pacer.predict(
cn->pacer.predict(
*out_frame_id, *out_frame_id,
*out_wake_up_time_ns, *out_wake_up_time_ns,
*out_desired_present_time_ns, *out_desired_present_time_ns,
@@ -726,68 +700,63 @@ static void comp_wivrn_calc_frame_pacing(struct comp_target * ct,
*out_predicted_display_time_ns); *out_predicted_display_time_ns);
} }
static void comp_wivrn_mark_timing_point(struct comp_target * ct, void wivrn_comp_target::mark_timing_point(
enum comp_target_timing_point point, enum comp_target_timing_point point,
int64_t frame_id, int64_t frame_id,
int64_t when_ns) int64_t when_ns)
{ {
struct wivrn_comp_target * cn = (struct wivrn_comp_target *)ct; pacer.mark_timing_point(point, frame_id, when_ns);
cn->pacer.mark_timing_point(point, frame_id, when_ns);
switch (point) switch (point)
{ {
case COMP_TARGET_TIMING_POINT_WAKE_UP: case COMP_TARGET_TIMING_POINT_WAKE_UP:
cn->cnx.dump_time("wake_up", frame_id, when_ns); cnx.dump_time("wake_up", frame_id, when_ns);
break; break;
case COMP_TARGET_TIMING_POINT_BEGIN: case COMP_TARGET_TIMING_POINT_BEGIN:
cn->cnx.dump_time("begin", frame_id, when_ns); cnx.dump_time("begin", frame_id, when_ns);
break; break;
case COMP_TARGET_TIMING_POINT_SUBMIT_BEGIN: case COMP_TARGET_TIMING_POINT_SUBMIT_BEGIN:
break; break;
case COMP_TARGET_TIMING_POINT_SUBMIT_END: case COMP_TARGET_TIMING_POINT_SUBMIT_END:
cn->cnx.dump_time("submit", frame_id, when_ns); cnx.dump_time("submit", frame_id, when_ns);
break; break;
default: default:
assert(false); assert(false);
} }
} }
static VkResult comp_wivrn_update_timings(struct comp_target * ct) VkResult wivrn_comp_target::update_timings()
{ {
// TODO // TODO
return VK_SUCCESS; return VK_SUCCESS;
} }
static void comp_wivrn_set_title(struct comp_target * ct, const char * title) void wivrn_comp_target::set_title(const char * title)
{} {}
static xrt_result_t comp_wivrn_get_refresh_rates(struct comp_target * ct, uint32_t * count, float * refresh_rates_hz) xrt_result_t wivrn_comp_target::get_refresh_rates(uint32_t * count, float * refresh_rates_hz)
{ {
struct wivrn_comp_target * cn = (struct wivrn_comp_target *)ct; const auto & rates = cnx.get_info().available_refresh_rates;
const auto & rates = cn->cnx.get_info().available_refresh_rates;
*count = std::min<uint32_t>(rates.size(), XRT_MAX_SUPPORTED_REFRESH_RATES); *count = std::min<uint32_t>(rates.size(), XRT_MAX_SUPPORTED_REFRESH_RATES);
std::copy_n(rates.begin(), *count, refresh_rates_hz); std::copy_n(rates.begin(), *count, refresh_rates_hz);
return XRT_SUCCESS; return XRT_SUCCESS;
} }
static xrt_result_t comp_wivrn_get_current_refresh_rate(struct comp_target * ct, float * refresh_rate_hz) xrt_result_t wivrn_comp_target::get_current_refresh_rate(float * refresh_rate_hz)
{ {
struct wivrn_comp_target * cn = (struct wivrn_comp_target *)ct; *refresh_rate_hz = desc.fps;
*refresh_rate_hz = cn->desc.fps;
return XRT_SUCCESS; return XRT_SUCCESS;
} }
static xrt_result_t comp_wivrn_request_refresh_rate(struct comp_target * ct, float refresh_rate_hz) xrt_result_t wivrn_comp_target::request_refresh_rate(float refresh_rate_hz)
{ {
struct wivrn_comp_target * cn = (struct wivrn_comp_target *)ct; requested_refresh_rate = refresh_rate_hz;
cn->requested_refresh_rate = refresh_rate_hz;
if (refresh_rate_hz == 0.0f) if (refresh_rate_hz == 0.0f)
refresh_rate_hz = get_default_rate(cn->cnx.get_info(), *cn->cnx.get_settings()); refresh_rate_hz = get_default_rate(cnx.get_info(), *cnx.get_settings());
try try
{ {
cn->cnx.send_control(to_headset::refresh_rate_change{.fps = refresh_rate_hz}); cnx.send_control(to_headset::refresh_rate_change{.fps = refresh_rate_hz});
} }
catch (...) catch (...)
{ {
@@ -799,17 +768,16 @@ static xrt_result_t comp_wivrn_request_refresh_rate(struct comp_target * ct, flo
wivrn_comp_target::~wivrn_comp_target() wivrn_comp_target::~wivrn_comp_target()
{ {
destroy_images(this); destroy_images();
} }
static void comp_wivrn_destroy(struct comp_target * ct) void wivrn_comp_target::destroy()
{ {
auto cn = (struct wivrn_comp_target *)ct; cnx.unset_comp_target();
cn->cnx.unset_comp_target(); delete this;
delete cn;
} }
static void comp_wivrn_info_gpu(struct comp_target * ct, int64_t frame_id, int64_t gpu_start_ns, int64_t gpu_end_ns, int64_t when_ns) 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(); COMP_TRACE_MARKER();
} }
@@ -835,6 +803,15 @@ void wivrn_comp_target::reset_encoders()
cnx.send_control(to_headset::video_stream_description{desc}); 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() void wivrn_comp_target::pause()
{ {
skip_encoding = true; skip_encoding = true;
@@ -877,27 +854,27 @@ float wivrn_comp_target::get_refresh_rate()
wivrn_comp_target::wivrn_comp_target(wivrn::wivrn_session & cnx, struct comp_compositor * c) : wivrn_comp_target::wivrn_comp_target(wivrn::wivrn_session & cnx, struct comp_compositor * c) :
comp_target{ comp_target{
.c = c, .c = c,
.init_pre_vulkan = comp_wivrn_init_pre_vulkan, .init_pre_vulkan = method_pointer<&wivrn_comp_target::init_pre_vulkan>,
.init_post_vulkan = comp_wivrn_init_post_vulkan, .init_post_vulkan = method_pointer<&wivrn_comp_target::init_post_vulkan>,
.check_ready = comp_wivrn_check_ready, .check_ready = method_pointer<&wivrn_comp_target::check_ready>,
.create_images = comp_wivrn_create_images, .create_images = method_pointer<&wivrn_comp_target::create_images>,
.has_images = comp_wivrn_has_images, .has_images = method_pointer<&wivrn_comp_target::has_images>,
.acquire = comp_wivrn_acquire, .acquire = method_pointer<&wivrn_comp_target::acquire>,
.present = comp_wivrn_present, .present = method_pointer<&wivrn_comp_target::present>,
.flush = comp_wivrn_flush, .flush = method_pointer<&wivrn_comp_target::flush>,
.calc_frame_pacing = comp_wivrn_calc_frame_pacing, .calc_frame_pacing = method_pointer<&wivrn_comp_target::calc_frame_pacing>,
.mark_timing_point = comp_wivrn_mark_timing_point, .mark_timing_point = method_pointer<&wivrn_comp_target::mark_timing_point>,
.update_timings = comp_wivrn_update_timings, .update_timings = method_pointer<&wivrn_comp_target::update_timings>,
.info_gpu = comp_wivrn_info_gpu, .info_gpu = method_pointer<&wivrn_comp_target::info_gpu>,
.set_title = comp_wivrn_set_title, .set_title = method_pointer<&wivrn_comp_target::set_title>,
.get_refresh_rates = comp_wivrn_get_refresh_rates, .get_refresh_rates = method_pointer<&wivrn_comp_target::get_refresh_rates>,
.get_current_refresh_rate = comp_wivrn_get_current_refresh_rate, .get_current_refresh_rate = method_pointer<&wivrn_comp_target::get_current_refresh_rate>,
.request_refresh_rate = comp_wivrn_request_refresh_rate, .request_refresh_rate = method_pointer<&wivrn_comp_target::request_refresh_rate>,
.destroy = comp_wivrn_destroy, .destroy = method_pointer<&wivrn_comp_target::destroy>,
}, },
desc{.fps = get_default_rate(cnx.get_info(), *cnx.get_settings())}, desc{.fps = get_default_rate(cnx.get_info(), *cnx.get_settings())},
pacer(U_TIME_1S_IN_NS / desc.fps), cnx(cnx),
cnx(cnx) pacer(U_TIME_1S_IN_NS / desc.fps)
{ {
c->frame_interval_ns = U_TIME_1S_IN_NS / desc.fps; c->frame_interval_ns = U_TIME_1S_IN_NS / desc.fps;
} }
+56 -7
View File
@@ -2,6 +2,7 @@
* WiVRn VR streaming * WiVRn VR streaming
* Copyright (C) 2022 Guillaume Meunier <guillaume.meunier@centraliens.net> * Copyright (C) 2022 Guillaume Meunier <guillaume.meunier@centraliens.net>
* Copyright (C) 2022 Patrick Nicolas <patricknicolas@laposte.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 * 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 * it under the terms of the GNU General Public License as published by
@@ -19,6 +20,7 @@
#pragma once #pragma once
#include "main/comp_compositor.h"
#include "main/comp_target.h" #include "main/comp_target.h"
#include "encoder/encoder_settings.h" #include "encoder/encoder_settings.h"
@@ -28,11 +30,11 @@
#include "wivrn_pacer.h" #include "wivrn_pacer.h"
#include "wivrn_packets.h" #include "wivrn_packets.h"
#include <array>
#include <list> #include <list>
#include <memory> #include <memory>
#include <optional> #include <optional>
#include <thread> #include <thread>
#include <vector>
#include <vulkan/vulkan_raii.hpp> #include <vulkan/vulkan_raii.hpp>
namespace wivrn namespace wivrn
@@ -76,10 +78,9 @@ struct pseudo_swapchain
to_headset::video_stream_data_shard::view_info_t view_info{}; to_headset::video_stream_data_shard::view_info_t view_info{};
}; };
struct wivrn_comp_target : public comp_target class wivrn_comp_target : public comp_target
{ {
to_headset::video_stream_description desc{}; to_headset::video_stream_description desc{};
wivrn_pacer pacer;
std::optional<wivrn_vk_bundle> wivrn_bundle; std::optional<wivrn_vk_bundle> wivrn_bundle;
vk::raii::CommandPool command_pool = nullptr; vk::raii::CommandPool command_pool = nullptr;
@@ -90,25 +91,73 @@ struct wivrn_comp_target : public comp_target
VkColorSpaceKHR color_space; VkColorSpaceKHR color_space;
static std::vector<const char *> wanted_instance_extensions;
static std::vector<const char *> wanted_device_extensions;
std::array<encoder_settings, 3> settings; std::array<encoder_settings, 3> settings;
std::list<std::jthread> encoder_threads; std::list<std::jthread> encoder_threads;
std::vector<std::shared_ptr<video_encoder>> encoders; std::vector<std::shared_ptr<video_encoder>> encoders;
wivrn::wivrn_session & cnx; wivrn::wivrn_session & cnx;
std::optional<wivrn_foveation> foveation;
std::atomic<float> requested_refresh_rate; std::atomic<float> requested_refresh_rate;
std::atomic<bool> skip_encoding; 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(wivrn::wivrn_session & cnx, struct comp_compositor * c);
~wivrn_comp_target(); ~wivrn_comp_target();
void on_feedback(const from_headset::feedback &, const clock_offset &); void on_feedback(const from_headset::feedback &, const clock_offset &);
void reset_encoders(); void reset_encoders();
float get_requested_refresh_rate() const;
void reset_requested_refresh_rate();
void pause(); void pause();
void resume(); void resume();
+2 -2
View File
@@ -1095,9 +1095,9 @@ void wivrn_session::run_worker(std::stop_token stop)
{ {
scoped_lock lock(xrt_system.sessions.mutex); scoped_lock lock(xrt_system.sessions.mutex);
if (xrt_system.sessions.count == 0) if (xrt_system.sessions.count == 0)
comp_target->requested_refresh_rate = 0; comp_target->reset_requested_refresh_rate();
} }
if (comp_target->requested_refresh_rate == 0) if (comp_target->get_requested_refresh_rate() == 0)
refresh.adjust(*connection); refresh.adjust(*connection);
} }
if (do_control) if (do_control)