Remove encoders lock

This commit is contained in:
Patrick Nicolas
2024-06-18 23:39:13 +02:00
parent 03c12a9aaf
commit b5159dc030
2 changed files with 31 additions and 22 deletions
+24 -19
View File
@@ -29,8 +29,8 @@
using namespace xrt::drivers::wivrn; using namespace xrt::drivers::wivrn;
static const uint8_t image_free = 0; static const pseudo_swapchain::status_type::value_type image_free = 0;
static const uint8_t image_acquired = 1; static const pseudo_swapchain::status_type::value_type image_acquired = 1;
std::vector<const char *> wivrn_comp_target::wanted_instance_extensions = {}; std::vector<const char *> wivrn_comp_target::wanted_instance_extensions = {};
std::vector<const char *> wivrn_comp_target::wanted_device_extensions = { std::vector<const char *> wivrn_comp_target::wanted_device_extensions = {
@@ -79,11 +79,10 @@ static void destroy_images(struct wivrn_comp_target * cn)
cn->encoder_threads.clear(); cn->encoder_threads.clear();
cn->encoders.clear(); cn->encoders.clear();
cn->psc.images.clear(); cn->psc.images.reset();
free(cn->images); free(cn->images);
cn->images = NULL; cn->images = NULL;
cn->psc.images.clear();
target_fini_semaphores(cn); target_fini_semaphores(cn);
} }
@@ -149,7 +148,7 @@ static VkResult create_images(struct wivrn_comp_target * cn, vk::ImageUsageFlags
cn->images = U_TYPED_ARRAY_CALLOC(struct comp_target_image, cn->image_count); cn->images = U_TYPED_ARRAY_CALLOC(struct comp_target_image, cn->image_count);
cn->psc.images.resize(cn->image_count); cn->psc.images.reset(new pseudo_swapchain::item[cn->image_count]);
for (uint32_t i = 0; i < cn->image_count; i++) for (uint32_t i = 0; i < cn->image_count; i++)
{ {
auto & image = cn->psc.images[i].image; auto & image = cn->psc.images[i].image;
@@ -197,7 +196,7 @@ static VkResult create_images(struct wivrn_comp_target * cn, vk::ImageUsageFlags
cn->images[i].view = *item.image_view; cn->images[i].view = *item.image_view;
item.yuv = yuv_converter(vk->physical_device, device, item.image, format, vk::Extent2D{cn->width, cn->height}); item.yuv = yuv_converter(vk->physical_device, device, item.image, format, vk::Extent2D{cn->width, cn->height});
item.fence = vk::raii::Fence(device, vk::FenceCreateInfo()); item.fence = vk::raii::Fence(device, vk::FenceCreateInfo{.flags = vk::FenceCreateFlagBits::eSignaled});
item.command_buffer = std::move(device.allocateCommandBuffers( item.command_buffer = std::move(device.allocateCommandBuffers(
{.commandPool = *cn->command_pool, {.commandPool = *cn->command_pool,
@@ -367,13 +366,11 @@ static VkResult comp_wivrn_acquire(struct comp_target * ct, uint32_t * out_index
while (true) while (true)
{ {
std::lock_guard lock(cn->psc.mutex);
for (uint32_t i = 0; i < ct->image_count; i++) for (uint32_t i = 0; i < ct->image_count; i++)
{ {
if (cn->psc.images[i].status == image_free) auto expected = image_free;
if (cn->psc.images[i].status.compare_exchange_weak(expected, image_acquired))
{ {
cn->psc.images[i].status = image_acquired;
*out_index = i; *out_index = i;
return VK_SUCCESS; return VK_SUCCESS;
} }
@@ -389,6 +386,7 @@ static void * comp_wivrn_present_thread(void * void_param)
U_LOG_I("Starting encoder thread %d", param->thread->index); U_LOG_I("Starting encoder thread %d", param->thread->index);
uint8_t status_bit = 1 << (param->thread->index + 1); uint8_t status_bit = 1 << (param->thread->index + 1);
std::mutex dummy_mutex;
std::vector<VkFence> fences(cn->image_count); std::vector<VkFence> fences(cn->image_count);
std::vector<int> indices(cn->image_count); std::vector<int> indices(cn->image_count);
@@ -397,8 +395,6 @@ static void * comp_wivrn_present_thread(void * void_param)
int presenting_index = -1; int presenting_index = -1;
int nb_fences = 0; int nb_fences = 0;
{ {
std::unique_lock lock(cn->psc.mutex);
uint64_t timestamp = 0; uint64_t timestamp = 0;
for (uint32_t i = 0; i < cn->image_count; i++) for (uint32_t i = 0; i < cn->image_count; i++)
@@ -416,6 +412,7 @@ static void * comp_wivrn_present_thread(void * void_param)
if (presenting_index < 0) if (presenting_index < 0)
{ {
std::unique_lock lock(dummy_mutex);
// condition variable is not notified when we want to stop thread, // condition variable is not notified when we want to stop thread,
// use a timeout, but longer than a typical frame // use a timeout, but longer than a typical frame
cn->psc.cv.wait_for(lock, std::chrono::milliseconds(50)); cn->psc.cv.wait_for(lock, std::chrono::milliseconds(50));
@@ -430,6 +427,11 @@ static void * comp_wivrn_present_thread(void * void_param)
} }
VK_CHK_WITH_RET(res, "vkWaitForFences", NULL); VK_CHK_WITH_RET(res, "vkWaitForFences", NULL);
for (int i = 0; i < nb_fences; i++)
{
cn->psc.images[indices[i]].status &= ~status_bit;
}
const auto & psc_image = cn->psc.images[presenting_index]; const auto & psc_image = cn->psc.images[presenting_index];
try try
{ {
@@ -446,12 +448,6 @@ static void * comp_wivrn_present_thread(void * void_param)
{ {
// Ignore errors // Ignore errors
} }
std::lock_guard lock(cn->psc.mutex);
for (int i = 0; i < nb_fences; i++)
{
cn->psc.images[indices[i]].status &= ~status_bit;
}
} }
return NULL; return NULL;
@@ -506,7 +502,16 @@ static VkResult comp_wivrn_present(struct comp_target * ct,
} }
command_buffer.end(); command_buffer.end();
std::lock_guard lock(cn->psc.mutex); {
std::vector<vk::Fence> fences;
fences.reserve(ct->image_count);
for (size_t i = 0; i < ct->image_count; ++i)
{
fences.push_back(*cn->psc.images[i].fence);
}
auto res = cn->wivrn_bundle->device.waitForFences(fences, VK_TRUE, UINT64_MAX);
}
cn->wivrn_bundle->device.resetFences(*cn->psc.images[index].fence); cn->wivrn_bundle->device.resetFences(*cn->psc.images[index].fence);
{ {
scoped_lock lock(vk->queue_mutex); scoped_lock lock(vk->queue_mutex);
+7 -3
View File
@@ -44,6 +44,11 @@ class VideoEncoder;
struct pseudo_swapchain 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
struct item struct item
{ {
image_allocation image; image_allocation image;
@@ -51,12 +56,11 @@ struct pseudo_swapchain
vk::raii::Fence fence = nullptr; vk::raii::Fence fence = nullptr;
vk::raii::CommandBuffer command_buffer = nullptr; vk::raii::CommandBuffer command_buffer = nullptr;
yuv_converter yuv; yuv_converter yuv;
uint8_t status; // bitmask of consumer status, index 0 for acquired, the rest for each encoder status_type status; // bitmask of consumer status, index 0 for acquired, the rest for each encoder
int64_t frame_index; int64_t frame_index;
to_headset::video_stream_data_shard::view_info_t view_info{}; to_headset::video_stream_data_shard::view_info_t view_info{};
}; };
std::vector<item> images; std::unique_ptr<item[]> images;
std::mutex mutex;
std::condition_variable cv; std::condition_variable cv;
}; };