Remove CPU sync for vulkan video encode

This commit is contained in:
Patrick Nicolas
2024-10-27 11:31:29 +01:00
parent 6f434ec4cd
commit 1e0ba8b3ea
6 changed files with 161 additions and 73 deletions
+105 -4
View File
@@ -131,6 +131,31 @@ static void create_encoders(wivrn_comp_target * cn)
std::map<int, std::vector<std::shared_ptr<VideoEncoder>>> thread_params;
#if WIVRN_USE_VULKAN_ENCODE
if (std::ranges::any_of(cn->settings, [](const auto & item) { return item.encoder_name == encoder_vulkan; }))
{
auto & device = cn->wivrn_bundle->device;
cn->psc.video_command_pool = device.createCommandPool({
.flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer,
.queueFamilyIndex = vk->encode_queue_family_index,
});
auto command_buffers = device.allocateCommandBuffers({
.commandPool = *cn->psc.video_command_pool,
.commandBufferCount = uint32_t(cn->psc.images.size()),
});
for (size_t i = 0; i < command_buffers.size(); ++i)
{
cn->psc.images[i].video_command_buffer = std::move(command_buffers[i]);
cn->psc.images[i].video_fence = device.createFence({.flags = vk::FenceCreateFlagBits::eSignaled});
}
}
else
{
for (auto & i: cn->psc.images)
i.video_command_buffer = nullptr;
cn->psc.video_command_pool = nullptr;
}
#endif
for (auto & settings: cn->settings)
{
uint8_t stream_index = cn->encoders.size();
@@ -286,6 +311,7 @@ static bool comp_wivrn_init_post_vulkan(struct comp_target * ct, uint32_t prefer
.flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer,
.queueFamilyIndex = vk->queue_family_index,
});
cn->psc.video_sem = vk::raii::Semaphore(cn->wivrn_bundle->device, vk::SemaphoreCreateInfo{});
}
catch (std::exception & e)
{
@@ -524,20 +550,75 @@ static VkResult comp_wivrn_present(struct comp_target * ct,
}
auto & command_buffer = cn->psc.command_buffer;
command_buffer.reset();
command_buffer.begin(vk::CommandBufferBeginInfo{});
auto & psc_image = cn->psc.images[index];
// Wait for encoders to be done with previous frame
for (auto status = cn->psc.status.load(); status != 0; status = cn->psc.status)
cn->psc.status.wait(status);
#if WIVRN_USE_VULKAN_ENCODE
auto & video_command_buffer = psc_image.video_command_buffer;
vk::ImageMemoryBarrier2 video_barrier{
.srcStageMask = vk::PipelineStageFlagBits2KHR::eTransfer,
.srcAccessMask = vk::AccessFlagBits2::eMemoryRead,
.dstStageMask = vk::PipelineStageFlagBits2KHR::eVideoEncodeKHR,
.dstAccessMask = vk::AccessFlagBits2::eVideoEncodeReadKHR,
.oldLayout = vk::ImageLayout::eTransferSrcOptimal,
.newLayout = vk::ImageLayout::eVideoEncodeSrcKHR,
.srcQueueFamilyIndex = vk->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 = 1},
};
if (*video_command_buffer)
{
if (auto res = cn->wivrn_bundle->device.waitForFences(*psc_image.video_fence, true, 1'000'000'000);
res != vk::Result::eSuccess)
throw std::runtime_error("wait for fences: " + vk::to_string(res));
video_command_buffer.reset();
video_command_buffer.begin(vk::CommandBufferBeginInfo{
.flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit,
});
video_command_buffer.pipelineBarrier2({
.imageMemoryBarrierCount = 1,
.pImageMemoryBarriers = &video_barrier,
});
submit_info.signalSemaphoreCount = 1;
submit_info.pSignalSemaphores = &*cn->psc.video_sem;
}
#endif
command_buffer.reset();
command_buffer.begin(vk::CommandBufferBeginInfo{
.flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit,
});
cn->wivrn_bundle->device.resetFences(*cn->psc.fence);
cn->psc.images[index].status = pseudo_swapchain::status_t::encoding;
psc_image.status = pseudo_swapchain::status_t::encoding;
for (auto & encoder: cn->encoders)
{
encoder->PresentImage(cn->psc.images[index].image, command_buffer);
#if WIVRN_USE_VULKAN_ENCODE
encoder->present_image(psc_image.image, video_command_buffer, *cn->psc.images[index].video_fence);
#endif
encoder->present_image(psc_image.image, command_buffer);
}
#if WIVRN_USE_VULKAN_ENCODE
if (*video_command_buffer)
{
command_buffer.pipelineBarrier2({
.imageMemoryBarrierCount = 1,
.pImageMemoryBarriers = &video_barrier,
});
}
#endif
command_buffer.end();
submit_info.setCommandBuffers(*command_buffer);
@@ -545,6 +626,26 @@ static VkResult comp_wivrn_present(struct comp_target * ct,
scoped_lock lock(vk->queue_mutex);
cn->wivrn_bundle->queue.submit(submit_info, *cn->psc.fence);
}
#if WIVRN_USE_VULKAN_ENCODE
if (*video_command_buffer)
{
cn->wivrn_bundle->device.resetFences(*psc_image.video_fence);
vk::SemaphoreSubmitInfo sem{
.semaphore = *cn->psc.video_sem,
.stageMask = vk::PipelineStageFlagBits2KHR::eAllCommands,
};
vk::CommandBufferSubmitInfo cmd_info{
.commandBuffer = *video_command_buffer,
};
vk::SubmitInfo2 submit{
.waitSemaphoreInfoCount = 1,
.pWaitSemaphoreInfos = &sem,
.commandBufferInfoCount = 1,
.pCommandBufferInfos = &cmd_info,
};
cn->wivrn_bundle->encode_queue.submit2(submit, *psc_image.video_fence);
}
#endif
#ifdef XRT_FEATURE_RENDERDOC
if (auto r = renderdoc())