Use multiple vulkan encode queues if possible

This commit is contained in:
Patrick Nicolas committed 2026-05-19 06:59:03 +02:00
1 parent 8d6245330a
commit 9313ccbc1d
12 files changed
+119 -109

No files matched your search

+24 -23
View File
@@ -164,8 +164,9 @@ wivrn::video_encoder_vulkan::video_encoder_vulkan(
const vk::VideoEncodeCapabilitiesKHR & in_encode_caps,
uint8_t stream_idx,
const encoder_settings & settings) :
video_encoder(vk, stream_idx, vk.encode_queue_family_index, settings, std::make_unique<dpb_state>(), true),
video_encoder(vk, stream_idx, vk.encode_queues[stream_idx % vk.encode_queues.size()].family_index, settings, std::make_unique<dpb_state>(), true),
vk(vk),
encode_queue(vk.encode_queues[stream_idx % vk.encode_queues.size()]),
encode_caps(patch_capabilities(in_encode_caps)),
num_dpb_slots(std::min(video_caps.maxDpbSlots, 16u))
{
@@ -270,7 +271,7 @@ void wivrn::video_encoder_vulkan::init(const vk::VideoCapabilitiesKHR & video_ca
// NVIDIA has this flag while AMD has mutable flag on images, we don't have to
// care for transfer on main queue
if (not(vk.physical_device.getQueueFamilyProperties().at(vk.encode_queue_family_index).queueFlags & vk::QueueFlagBits::eTransfer))
if (not(vk.physical_device.getQueueFamilyProperties().at(encode_queue.family_index).queueFlags & vk::QueueFlagBits::eTransfer))
throw std::runtime_error("Vulkan encode queue is missing transfer capability");
tmp_image = image_allocation(
@@ -428,7 +429,7 @@ void wivrn::video_encoder_vulkan::init(const vk::VideoCapabilitiesKHR & video_ca
video_session =
vk.device.createVideoSessionKHR(vk::VideoSessionCreateInfoKHR{
.pNext = video_session_create_next,
.queueFamilyIndex = vk.encode_queue_family_index,
.queueFamilyIndex = encode_queue.family_index,
//.flags = vk::VideoSessionCreateFlagBitsKHR::eAllowEncodeParameterOptimizations,
.pVideoProfile = &video_profile,
.pictureFormat = picture_format.format,
@@ -569,7 +570,7 @@ void wivrn::video_encoder_vulkan::init(const vk::VideoCapabilitiesKHR & video_ca
video_command_pool = vk.device.createCommandPool(
vk::CommandPoolCreateInfo{
.flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer | vk::CommandPoolCreateFlagBits::eTransient,
.queueFamilyIndex = vk.encode_queue_family_index,
.queueFamilyIndex = encode_queue.family_index,
});
vk.name(video_command_pool, std::format("vulkan encoder {} video command pool", stream_idx));
@@ -585,12 +586,12 @@ void wivrn::video_encoder_vulkan::init(const vk::VideoCapabilitiesKHR & video_ca
if (slot_data[0].host_buffer)
{
if (*vk.transfer_queue)
if (vk.transfer_queue)
{
transfer_command_pool = vk.device.createCommandPool(
vk::CommandPoolCreateInfo{
.flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer | vk::CommandPoolCreateFlagBits::eTransient,
.queueFamilyIndex = vk.transfer_queue_family_index,
.queueFamilyIndex = vk.transfer_queue.family_index,
});
vk.name(transfer_command_pool, std::format("vulkan encoder {} transfer command pool", stream_idx));
@@ -605,7 +606,7 @@ void wivrn::video_encoder_vulkan::init(const vk::VideoCapabilitiesKHR & video_ca
}
else
{
auto properties = vk.physical_device.getQueueFamilyProperties().at(vk.encode_queue_family_index);
auto properties = vk.physical_device.getQueueFamilyProperties().at(encode_queue.family_index);
if (not(properties.queueFlags & vk::QueueFlagBits::eTransfer))
throw std::runtime_error("Incorrect vulkan queues for video encode support");
}
@@ -656,7 +657,7 @@ void wivrn::video_encoder_vulkan::present_image(vk::Image y_cbcr, vk::SemaphoreS
.dstAccessMask = vk::AccessFlagBits2::eTransferRead,
.oldLayout = vk::ImageLayout::eGeneral,
.newLayout = target_layout,
.srcQueueFamilyIndex = vk.queue_family_index,
.srcQueueFamilyIndex = vk.queue.family_index,
.dstQueueFamilyIndex = target_queue,
.image = y_cbcr,
.subresourceRange = {.aspectMask = vk::ImageAspectFlagBits::eColor,
@@ -759,7 +760,7 @@ void wivrn::video_encoder_vulkan::present_image(vk::Image y_cbcr, vk::SemaphoreS
.dstAccessMask = vk::AccessFlagBits2::eVideoEncodeReadKHR,
.oldLayout = vk::ImageLayout::eGeneral,
.newLayout = target_layout,
.srcQueueFamilyIndex = need_transfer ? vk.queue_family_index : vk::QueueFamilyIgnored,
.srcQueueFamilyIndex = need_transfer ? vk.queue.family_index : vk::QueueFamilyIgnored,
.dstQueueFamilyIndex = need_transfer ? target_queue : vk::QueueFamilyIgnored,
.image = y_cbcr,
.subresourceRange = {.aspectMask = vk::ImageAspectFlagBits::eColor,
@@ -929,8 +930,8 @@ void wivrn::video_encoder_vulkan::present_image(vk::Image y_cbcr, vk::SemaphoreS
.srcAccessMask = vk::AccessFlagBits2::eMemoryWrite,
.dstStageMask = vk::PipelineStageFlagBits2::eTransfer,
.dstAccessMask = vk::AccessFlagBits2::eMemoryRead,
.srcQueueFamilyIndex = vk.encode_queue_family_index,
.dstQueueFamilyIndex = vk.transfer_queue_family_index,
.srcQueueFamilyIndex = encode_queue.family_index,
.dstQueueFamilyIndex = vk.transfer_queue.family_index,
.buffer = slot_item.output_buffer,
.size = slot_item.copy_size,
};
@@ -988,8 +989,8 @@ void wivrn::video_encoder_vulkan::present_image(vk::Image y_cbcr, vk::SemaphoreS
.stageMask = vk::PipelineStageFlagBits2::eVideoEncodeKHR,
};
std::unique_lock lock(vk.encode_queue_mutex);
vk.encode_queue.submit2(
std::unique_lock lock(encode_queue.mutex);
encode_queue.queue.submit2(
vk::SubmitInfo2{
.waitSemaphoreInfoCount = 1,
.pWaitSemaphoreInfos = &compositor_sem,
@@ -1013,14 +1014,14 @@ void wivrn::video_encoder_vulkan::present_image(vk::Image y_cbcr, vk::SemaphoreS
.stageMask = vk::PipelineStageFlagBits2::eTransfer,
};
std::unique_lock lock(vk.transfer_queue_mutex);
vk.transfer_queue.submit2(vk::SubmitInfo2{
.waitSemaphoreInfoCount = 1,
.pWaitSemaphoreInfos = &wait_info,
.commandBufferInfoCount = 1,
.pCommandBufferInfos = &cmd_info,
},
*slot_item.fence);
std::unique_lock lock(vk.transfer_queue.mutex);
vk.transfer_queue.queue.submit2(vk::SubmitInfo2{
.waitSemaphoreInfoCount = 1,
.pWaitSemaphoreInfos = &wait_info,
.commandBufferInfoCount = 1,
.pCommandBufferInfos = &cmd_info,
},
*slot_item.fence);
}
}
@@ -1065,8 +1066,8 @@ std::optional<wivrn::video_encoder::data> wivrn::video_encoder_vulkan::encode(ui
cmd.end();
{
std::unique_lock lock(transfer_queue ? vk.transfer_queue_mutex : vk.encode_queue_mutex);
auto & queue = transfer_queue ? vk.transfer_queue : vk.encode_queue;
std::unique_lock lock(transfer_queue ? vk.transfer_queue.mutex : encode_queue.mutex);
auto & queue = (transfer_queue ? vk.transfer_queue : encode_queue).queue;
vk::CommandBufferSubmitInfo cmd_info{
.commandBuffer = *cmd,