Enforce synchronization across encoders

This commit is contained in:
Patrick Nicolas
2024-08-02 17:47:56 +02:00
parent ac4127759a
commit 19b67d22de
4 changed files with 120 additions and 143 deletions
+66 -85
View File
@@ -29,9 +29,6 @@
using namespace xrt::drivers::wivrn;
static const pseudo_swapchain::status_type::value_type image_free = 0;
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_device_extensions = {
// For FFMPEG
@@ -57,10 +54,12 @@ static void destroy_images(struct wivrn_comp_target * cn)
if (cn->images == nullptr)
return;
cn->psc.status = 1;
cn->psc.status.notify_all();
cn->encoder_threads.clear();
cn->encoders.clear();
cn->psc.images.reset();
cn->psc.images.clear();
free(cn->images);
cn->images = NULL;
@@ -68,7 +67,7 @@ static void destroy_images(struct wivrn_comp_target * cn)
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<VideoEncoder>> encoders);
static void comp_wivrn_present_thread(std::stop_token stop_token, wivrn_comp_target * cn, int index, std::vector<std::shared_ptr<VideoEncoder>> encoders);
static void create_encoders(wivrn_comp_target * cn)
{
@@ -76,6 +75,7 @@ static void create_encoders(wivrn_comp_target * cn)
assert(cn->encoders.empty());
assert(cn->encoder_threads.empty());
assert(cn->wivrn_bundle);
cn->psc.status = 0;
auto & desc = cn->desc;
desc.width = cn->width;
@@ -97,10 +97,9 @@ static void create_encoders(wivrn_comp_target * cn)
for (auto & [group, params]: thread_params)
{
auto & thread = cn->encoder_threads.emplace_back(
std::jthread(comp_wivrn_present_thread, cn, cn->encoder_threads.size(), std::move(params)),
cn->psc.present_gen);
comp_wivrn_present_thread, cn, cn->encoder_threads.size(), std::move(params));
std::string name = "encoder " + std::to_string(group);
pthread_setname_np(thread.thread.native_handle(), name.c_str());
pthread_setname_np(thread.native_handle(), name.c_str());
}
cn->pacer.set_stream_count(cn->encoders.size());
cn->cnx->send_control(desc);
@@ -119,7 +118,8 @@ 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->psc.images.reset(new pseudo_swapchain::item[cn->image_count]);
cn->psc.images.resize(cn->image_count);
std::vector<vk::Image> rgb;
for (uint32_t i = 0; i < cn->image_count; i++)
{
auto & image = cn->psc.images[i].image;
@@ -144,6 +144,7 @@ static VkResult create_images(struct wivrn_comp_target * cn, vk::ImageUsageFlags
.usage = VMA_MEMORY_USAGE_AUTO,
});
cn->images[i].handle = image;
rgb.push_back(image);
}
for (uint32_t i = 0; i < cn->image_count; i++)
@@ -165,15 +166,16 @@ static VkResult create_images(struct wivrn_comp_target * cn, vk::ImageUsageFlags
},
});
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.fence = vk::raii::Fence(device, vk::FenceCreateInfo{.flags = vk::FenceCreateFlagBits::eSignaled});
item.command_buffer = std::move(device.allocateCommandBuffers(
{.commandPool = *cn->command_pool,
.commandBufferCount = 1})[0]);
}
cn->psc.yuv = yuv_converter(vk->physical_device, device, rgb, format, vk::Extent2D{cn->width, cn->height});
cn->psc.fence = vk::raii::Fence(device, vk::FenceCreateInfo{.flags = vk::FenceCreateFlagBits::eSignaled});
cn->psc.command_buffer = std::move(device.allocateCommandBuffers(
{.commandPool = *cn->command_pool,
.commandBufferCount = 1})[0]);
return VK_SUCCESS;
}
@@ -339,9 +341,9 @@ static VkResult comp_wivrn_acquire(struct comp_target * ct, uint32_t * out_index
{
for (uint32_t i = 0; i < ct->image_count; i++)
{
auto expected = image_free;
if (cn->psc.images[i].status.compare_exchange_weak(expected, image_acquired))
if (cn->psc.images[i].status == pseudo_swapchain::status_t::free)
{
cn->psc.images[i].status = pseudo_swapchain::status_t::acquired;
*out_index = i;
return VK_SUCCESS;
}
@@ -349,61 +351,35 @@ 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<VideoEncoder>> encoders)
static void comp_wivrn_present_thread(std::stop_token stop_token, wivrn_comp_target * cn, int index, std::vector<std::shared_ptr<VideoEncoder>> encoders)
{
struct vk_bundle * vk = get_vk(cn);
auto & vk = *cn->wivrn_bundle;
U_LOG_I("Starting encoder thread %d", index);
uint8_t status_bit = 1 << (index + 1);
const uint8_t status_bit = 1 << (index + 1);
std::vector<VkFence> fences(cn->image_count);
std::vector<int> indices(cn->image_count);
while (not stop_token.stop_requested())
{
int presenting_index = -1;
int nb_fences = 0;
{
uint64_t timestamp = 0;
auto gen = cn->psc.present_gen.load();
auto status = cn->psc.status.load();
// Bit 0 to request exit
if (status & 1)
return;
for (uint32_t i = 0; i < cn->image_count; i++)
if (not(status & status_bit))
{
if (cn->psc.images[i].status & status_bit)
{
if (timestamp < cn->psc.images[i].view_info.display_time)
{
presenting_index = i;
}
indices[nb_fences] = i;
fences[nb_fences++] = *cn->psc.images[i].fence;
}
}
if (presenting_index < 0)
{
cn->psc.present_gen.wait(gen);
cn->psc.status.wait(status);
continue;
}
}
VkResult res = vk->vkWaitForFences(vk->device, nb_fences, fences.data(), VK_TRUE, UINT64_MAX);
if (nb_fences > 1)
{
U_LOG_I("Encoder group %d dropped %d frames", index, nb_fences - 1);
}
VK_CHK_WITH_RET(res, "vkWaitForFences", NULL);
auto res = vk.device.waitForFences(*cn->psc.fence, true, UINT64_MAX);
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];
try
{
for (auto & encoder: encoders)
{
encoder->Encode(*cn->cnx, psc_image.view_info, psc_image.frame_index);
encoder->Encode(*cn->cnx, cn->psc.view_info, cn->psc.frame_index);
}
}
catch (std::exception & e)
@@ -414,9 +390,20 @@ static void * comp_wivrn_present_thread(std::stop_token stop_token, wivrn_comp_t
{
// Ignore errors
}
}
return NULL;
// Update encoder status, release image
if ((cn->psc.status &= ~status_bit) == 0)
{
for (auto & img: cn->psc.images)
{
if (img.status == pseudo_swapchain::status_t::encoding)
{
img.status = pseudo_swapchain::status_t::free;
break;
}
}
}
}
}
static VkResult comp_wivrn_present(struct comp_target * ct,
@@ -429,10 +416,11 @@ static VkResult comp_wivrn_present(struct comp_target * ct,
struct wivrn_comp_target * cn = (struct wivrn_comp_target *)ct;
assert(index < cn->image_count);
assert(cn->psc.images[index].status == pseudo_swapchain::status_t::acquired);
struct vk_bundle * vk = get_vk(cn);
auto & command_buffer = cn->psc.images[index].command_buffer;
auto & command_buffer = cn->psc.command_buffer;
command_buffer.reset();
command_buffer.begin(vk::CommandBufferBeginInfo{});
@@ -446,50 +434,41 @@ static VkResult comp_wivrn_present(struct comp_target * ct,
.pCommandBuffers = &*command_buffer,
};
if (cn->c->base.slot.layer_count == 0 or not cn->cnx->get_offset())
bool skip = cn->c->base.slot.layer_count == 0 or not cn->cnx->get_offset();
if (cn->psc.status != 0)
{
U_LOG_W("Encoders not done with previous image, skip one");
skip = true;
}
if (skip)
{
// TODO: Tell the headset that there is no image to display
assert(cn->psc.images[index].status == image_acquired);
command_buffer.end();
scoped_lock lock(vk->queue_mutex);
cn->wivrn_bundle->queue.submit(submit_info);
cn->psc.images[index].status = image_free;
cn->psc.images[index].status = pseudo_swapchain::status_t::free;
return VK_SUCCESS;
}
assert(index < ct->image_count);
assert(ct->images != NULL);
cn->psc.images[index].status = pseudo_swapchain::status_t::encoding;
auto & yuv = cn->psc.images[index].yuv;
yuv.record_draw_commands(command_buffer);
auto & yuv = cn->psc.yuv;
yuv.record_draw_commands(command_buffer, index);
for (auto & encoder: cn->encoders)
{
encoder->PresentImage(yuv, command_buffer);
}
command_buffer.end();
{
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);
}
auto res = cn->wivrn_bundle->device.waitForFences(*cn->psc.fence, true, UINT64_MAX);
cn->wivrn_bundle->device.resetFences(*cn->psc.images[index].fence);
cn->wivrn_bundle->device.resetFences(*cn->psc.fence);
{
scoped_lock lock(vk->queue_mutex);
cn->wivrn_bundle->queue.submit(submit_info, *cn->psc.images[index].fence);
cn->wivrn_bundle->queue.submit(submit_info, *cn->psc.fence);
}
assert(cn->psc.images[index].status == image_acquired);
// set bits to 1 for index 1..num encoder threads + 1
cn->psc.images[index].status = (1 << (cn->encoder_threads.size() + 1)) - 2;
cn->psc.images[index].frame_index = cn->current_frame_id;
auto & view_info = cn->psc.images[index].view_info;
auto & view_info = cn->psc.view_info;
view_info.display_time = cn->cnx->get_offset().to_headset(desired_present_time_ns);
for (int eye = 0; eye < 2; ++eye)
{
@@ -510,8 +489,10 @@ static VkResult comp_wivrn_present(struct comp_target * ct,
view_info.pose[eye] = xrt_cast(result.pose);
}
}
cn->psc.present_gen += 1;
cn->psc.present_gen.notify_all();
// set bits to 1 for index 1..num encoder threads + 1
cn->psc.status = (1 << (cn->encoder_threads.size() + 1)) - 2;
cn->psc.frame_index = cn->current_frame_id;
cn->psc.status.notify_all();
return VK_SUCCESS;
}
+21 -31
View File
@@ -48,20 +48,31 @@ struct pseudo_swapchain
#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 = nullptr;
vk::raii::Fence fence = nullptr;
vk::raii::CommandBuffer command_buffer = nullptr;
yuv_converter yuv;
status_type status; // bitmask of consumer status, index 0 for acquired, the rest for each encoder
int64_t frame_index;
to_headset::video_stream_data_shard::view_info_t view_info{};
status_t status;
};
std::unique_ptr<item[]> images;
// Incremented and notified when a new image is available for encoders
status_type present_gen;
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;
yuv_converter yuv;
int64_t frame_index;
to_headset::video_stream_data_shard::view_info_t view_info{};
};
struct wivrn_comp_target : public comp_target
@@ -82,30 +93,9 @@ struct wivrn_comp_target : public comp_target
static std::vector<const char *> wanted_instance_extensions;
static std::vector<const char *> wanted_device_extensions;
struct encoder_thread
{
std::jthread thread;
pseudo_swapchain::status_type & gen;
std::jthread stopper;
encoder_thread(std::jthread && thread, pseudo_swapchain::status_type & gen) :
thread(std::move(thread)), gen(gen) {}
~encoder_thread()
{
thread.request_stop();
// Encoder will be in a loop on the gen variable, wake them up
stopper = std::jthread([&]() {
while (not stopper.get_stop_token().stop_requested())
{
gen += 1;
gen.notify_all();
std::this_thread::sleep_for(std::chrono::milliseconds(10));
} });
}
};
std::vector<encoder_settings> settings;
to_headset::video_stream_description desc{};
std::list<encoder_thread> encoder_threads;
std::list<std::jthread> encoder_threads;
std::vector<std::shared_ptr<VideoEncoder>> encoders;
std::shared_ptr<xrt::drivers::wivrn::wivrn_session> cnx;
+27 -18
View File
@@ -59,8 +59,13 @@ static vk::Format view_format(vk::Format image_format)
}
yuv_converter::yuv_converter() {}
yuv_converter::yuv_converter(vk::PhysicalDevice physical_device, vk::raii::Device & device, vk::Image rgb, vk::Format fmt, vk::Extent2D extent) :
extent(extent), rgb(rgb)
yuv_converter::yuv_converter(
vk::PhysicalDevice physical_device,
vk::raii::Device & device,
std::span<vk::Image> rgb,
vk::Format fmt,
vk::Extent2D extent) :
device(*device), extent(extent), rgb(rgb.begin(), rgb.end())
{
auto view_fmt = view_format(fmt);
@@ -89,14 +94,15 @@ yuv_converter::yuv_converter(vk::PhysicalDevice physical_device, vk::raii::Devic
};
// Input image view
for(vk::Image rgb_image: rgb)
{
vk::ImageViewUsageCreateInfo usage{
.usage = vk::ImageUsageFlagBits::eStorage,
};
view_rgb = device.createImageView(
views_rgb.push_back(device.createImageView(
{
.pNext = &usage,
.image = rgb,
.image = rgb_image,
.viewType = vk::ImageViewType::e2D,
.format = view_fmt,
.subresourceRange = {
@@ -104,7 +110,7 @@ yuv_converter::yuv_converter(vk::PhysicalDevice physical_device, vk::raii::Devic
.levelCount = 1,
.layerCount = 1,
},
});
}));
}
// Output images
@@ -232,10 +238,6 @@ yuv_converter::yuv_converter(vk::PhysicalDevice physical_device, vk::raii::Devic
.pSetLayouts = &*ds_layout,
})[0].release();
vk::DescriptorImageInfo rgb_desc_image_info{
.imageView = *view_rgb,
.imageLayout = vk::ImageLayout::eGeneral,
};
vk::DescriptorImageInfo luma_desc_img_info_{
.imageView = *view_luma,
.imageLayout = vk::ImageLayout::eGeneral,
@@ -247,13 +249,6 @@ yuv_converter::yuv_converter(vk::PhysicalDevice physical_device, vk::raii::Devic
device.updateDescriptorSets(
{
vk::WriteDescriptorSet{
.dstSet = ds,
.dstBinding = 0,
.descriptorCount = 1,
.descriptorType = vk::DescriptorType::eStorageImage,
.pImageInfo = &rgb_desc_image_info,
},
vk::WriteDescriptorSet{
.dstSet = ds,
.dstBinding = 1,
@@ -273,8 +268,22 @@ yuv_converter::yuv_converter(vk::PhysicalDevice physical_device, vk::raii::Devic
}
void yuv_converter::record_draw_commands(
vk::raii::CommandBuffer & cmd_buf)
vk::raii::CommandBuffer & cmd_buf, size_t index)
{
assert(index < rgb.size());
vk::DescriptorImageInfo rgb_desc_image_info{
.imageView = *views_rgb[index],
.imageLayout = vk::ImageLayout::eGeneral,
};
device.updateDescriptorSets(
vk::WriteDescriptorSet{
.dstSet = ds,
.dstBinding = 0,
.descriptorCount = 1,
.descriptorType = vk::DescriptorType::eStorageImage,
.pImageInfo = &rgb_desc_image_info,
}, nullptr);
std::array im_barriers = {
vk::ImageMemoryBarrier{
.srcAccessMask = vk::AccessFlagBits::eNone,
@@ -305,7 +314,7 @@ void yuv_converter::record_draw_commands(
.dstAccessMask = vk::AccessFlagBits::eMemoryRead,
.oldLayout = vk::ImageLayout::ePresentSrcKHR,
.newLayout = vk::ImageLayout::eGeneral,
.image = rgb,
.image = rgb[index],
.subresourceRange = {.aspectMask = vk::ImageAspectFlagBits::eColor,
.baseMipLevel = 0,
.levelCount = 1,
+6 -9
View File
@@ -25,16 +25,15 @@
class yuv_converter
{
vk::Extent2D extent;
vk::Image rgb;
public:
image_allocation luma;
image_allocation chroma;
private:
vk::raii::ImageView view_rgb = nullptr;
vk::Device device;
vk::Extent2D extent;
std::vector<vk::Image> rgb;
std::vector<vk::raii::ImageView> views_rgb;
vk::raii::ImageView view_luma = nullptr;
vk::raii::ImageView view_chroma = nullptr;
@@ -44,15 +43,13 @@ private:
vk::raii::DescriptorPool dp = nullptr;
vk::DescriptorSet ds = nullptr;
std::vector<vk::raii::DeviceMemory> mem;
public:
yuv_converter();
yuv_converter(vk::PhysicalDevice, vk::raii::Device & device, vk::Image rgb, vk::Format format, vk::Extent2D extent);
yuv_converter(vk::PhysicalDevice, vk::raii::Device & device, std::span<vk::Image> rgb, vk::Format format, vk::Extent2D extent);
// Converts the given image to yuv, stored in luma and chroma images.
// The output images will be in transfer src optimal layout
void record_draw_commands(vk::raii::CommandBuffer & cmd_buf);
void record_draw_commands(vk::raii::CommandBuffer & cmd_buf, size_t index);
void assemble_planes(vk::Rect2D, vk::raii::CommandBuffer &, vk::Image target);
};