Use multiple vulkan encode queues if possible

This commit is contained in:
Patrick Nicolas
2026-05-19 06:59:03 +02:00
parent 8d6245330a
commit 9313ccbc1d
12 changed files with 119 additions and 109 deletions
+7 -9
View File
@@ -434,7 +434,7 @@ xrt_result_t compositor::layer_commit(xrt_graphics_sync_handle_t sync_handle)
{
if (encoder->stream_idx == 2 and not view_info.alpha)
continue;
else if (encoder->need_transfer or encoder->target_queue == vk.queue_family_index)
else if (encoder->need_transfer or encoder->target_queue == vk.queue.family_index)
{
image_barriers.push_back(
vk::ImageMemoryBarrier2{
@@ -452,7 +452,7 @@ xrt_result_t compositor::layer_commit(xrt_graphics_sync_handle_t sync_handle)
// transition the encoder needs.
.oldLayout = vk::ImageLayout::eGeneral,
.newLayout = encoder->target_layout,
.srcQueueFamilyIndex = vk.queue_family_index,
.srcQueueFamilyIndex = vk.queue.family_index,
.dstQueueFamilyIndex = encoder->target_queue,
.image = images[i].image,
.subresourceRange = {.aspectMask = vk::ImageAspectFlagBits::eColor,
@@ -482,8 +482,8 @@ xrt_result_t compositor::layer_commit(xrt_graphics_sync_handle_t sync_handle)
vk::CommandBufferSubmitInfo cmd_info{
.commandBuffer = cmd,
};
std::unique_lock lock{vk.queue_mutex};
vk.queue.submit2(vk::SubmitInfo2{
std::unique_lock lock{vk.queue.mutex};
vk.queue.queue.submit2(vk::SubmitInfo2{
.commandBufferInfoCount = 1,
.pCommandBufferInfos = &cmd_info,
.signalSemaphoreInfoCount = 1,
@@ -682,7 +682,7 @@ compositor::compositor(wivrn_session & session) :
session(session),
cmd_pool(vk.device, vk::CommandPoolCreateInfo{
.flags = vk::CommandPoolCreateFlagBits::eTransient,
.queueFamilyIndex = vk.queue_family_index,
.queueFamilyIndex = vk.queue.family_index,
}),
query_pool(vk.device, vk::QueryPoolCreateInfo{
.queryType = vk::QueryType::eTimestamp,
@@ -706,7 +706,7 @@ compositor::compositor(wivrn_session & session) :
*vk.instance,
*vk.physical_device,
*vk.device,
vk.queue_family_index,
vk.queue.family_index,
0,
vk.has_device_ext(VK_KHR_EXTERNAL_FENCE_FD_EXTENSION_NAME),
vk.has_device_ext(VK_KHR_EXTERNAL_SEMAPHORE_FD_EXTENSION_NAME),
@@ -720,9 +720,7 @@ compositor::compositor(wivrn_session & session) :
c_base->vk.graphics_queue = nullptr;
if (c_base->vk.main_queue)
c_base->vk.main_queue->mutex = copy_mutex(vk.queue_mutex);
if (c_base->vk.encode_queue)
c_base->vk.encode_queue->mutex = copy_mutex(vk.encode_queue_mutex);
c_base->vk.main_queue->mutex = copy_mutex(vk.queue.mutex);
{
comp_vulkan_formats formats{};
+2 -2
View File
@@ -181,7 +181,7 @@ class prober
bool has_vk(video_codec codec)
{
if (*vk.encode_queue == VK_NULL_HANDLE)
if (vk.encode_queues.empty())
return false;
if (not std::get<vk::PhysicalDeviceVideoMaintenance1FeaturesKHR>(vk.feat).videoMaintenance1)
{
@@ -190,7 +190,7 @@ class prober
}
auto prop = vk.physical_device.getQueueFamilyProperties2<vk::StructureChain<vk::QueueFamilyProperties2, vk::QueueFamilyVideoPropertiesKHR>>();
assert(vk.encode_queue_family_index < prop.size());
const auto flags = prop.at(vk.encode_queue_family_index).get<vk::QueueFamilyVideoPropertiesKHR>().videoCodecOperations;
const auto flags = prop.at(vk.encode_queues[0].family_index).get<vk::QueueFamilyVideoPropertiesKHR>().videoCodecOperations;
switch (codec)
{
case h264: {
+1 -1
View File
@@ -39,7 +39,7 @@ public:
protected:
video_encoder_ffmpeg(vk_bundle & vk, uint8_t stream_idx, const encoder_settings & settings) :
wivrn::video_encoder(vk, stream_idx, vk.queue_family_index, settings, std::make_unique<default_idr_handler>(), true) {}
wivrn::video_encoder(vk, stream_idx, vk.queue.family_index, settings, std::make_unique<default_idr_handler>(), true) {}
virtual void push_frame(bool idr, uint8_t slot) = 0;
+7 -7
View File
@@ -159,7 +159,7 @@ vk::raii::CommandPool make_cmd_pool(wivrn::vk_bundle & vk, uint8_t stream_idx)
auto res = vk.device.createCommandPool(vk::CommandPoolCreateInfo{
.flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer | vk::CommandPoolCreateFlagBits::eTransient,
.queueFamilyIndex = vk.queue_family_index,
.queueFamilyIndex = vk.queue.family_index,
});
vk.name(res, std::format("vaapi encoder {} command pool", stream_idx));
return res;
@@ -536,17 +536,17 @@ void video_encoder_va::present_image(vk::Image y_cbcr, vk::SemaphoreSubmitInfo,
cmd.end();
std::unique_lock lock(vk.queue_mutex);
std::unique_lock lock(vk.queue.mutex);
vk::CommandBufferSubmitInfo cmd_info{
.commandBuffer = *cmd,
};
vk.device.resetFences(*in[slot].fence);
vk.queue.submit2(vk::SubmitInfo2{
.commandBufferInfoCount = 1,
.pCommandBufferInfos = &cmd_info,
},
*in[slot].fence);
vk.queue.queue.submit2(vk::SubmitInfo2{
.commandBufferInfoCount = 1,
.pCommandBufferInfos = &cmd_info,
},
*in[slot].fence);
}
void video_encoder_va::push_frame(bool idr, uint8_t slot)
+1 -1
View File
@@ -193,7 +193,7 @@ video_encoder::video_encoder(vk_bundle & vk,
bool async_send) :
stream_idx(stream_idx),
target_queue(target_queue),
need_transfer(not vk.optimal_transfer(vk.queue_family_index, target_queue)),
need_transfer(not vk.optimal_transfer(vk.queue.family_index, target_queue)),
bitrate_multiplier(settings.bitrate_multiplier),
shared_sender(async_send ? sender::get() : nullptr),
idr(std::move(idr)),
+8 -8
View File
@@ -153,7 +153,7 @@ static vk::raii::CommandPool make_cmd_pool(wivrn::vk_bundle & vk, uint8_t stream
auto res = vk.device.createCommandPool(vk::CommandPoolCreateInfo{
.flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer | vk::CommandPoolCreateFlagBits::eTransient,
.queueFamilyIndex = vk.queue_family_index,
.queueFamilyIndex = vk.queue.family_index,
});
vk.name(res, std::format("nvenc encoder {} command pool", stream_idx));
return res;
@@ -163,7 +163,7 @@ video_encoder_nvenc::video_encoder_nvenc(
wivrn::vk_bundle & vk,
const encoder_settings & settings,
uint8_t stream_idx) :
video_encoder(vk, stream_idx, vk.queue_family_index, settings, std::make_unique<default_idr_handler>(), true),
video_encoder(vk, stream_idx, vk.queue.family_index, settings, std::make_unique<default_idr_handler>(), true),
vk(vk),
cmd_pool{make_cmd_pool(vk, stream_idx)},
shared_state(video_encoder_nvenc_shared_state::get()),
@@ -461,17 +461,17 @@ void video_encoder_nvenc::present_image(vk::Image y_cbcr, vk::SemaphoreSubmitInf
}});
cmd.end();
std::unique_lock lock(vk.queue_mutex);
std::unique_lock lock(vk.queue.mutex);
vk::CommandBufferSubmitInfo cmd_info{
.commandBuffer = *cmd,
};
vk.device.resetFences(*in[slot].fence);
vk.queue.submit2(vk::SubmitInfo2{
.commandBufferInfoCount = 1,
.pCommandBufferInfos = &cmd_info,
},
*in[slot].fence);
vk.queue.queue.submit2(vk::SubmitInfo2{
.commandBufferInfoCount = 1,
.pCommandBufferInfos = &cmd_info,
},
*in[slot].fence);
}
std::optional<video_encoder::data> video_encoder_nvenc::encode(uint8_t slot, uint64_t frame_index)
+12 -12
View File
@@ -39,7 +39,7 @@ vk::raii::CommandPool make_cmd_pool(wivrn::vk_bundle & vk, uint8_t stream_idx)
auto res = vk.device.createCommandPool(vk::CommandPoolCreateInfo{
.flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer | vk::CommandPoolCreateFlagBits::eTransient,
.queueFamilyIndex = *vk.transfer_queue ? vk.transfer_queue_family_index : vk.queue_family_index,
.queueFamilyIndex = vk.transfer_queue ? vk.transfer_queue.family_index : vk.queue.family_index,
});
vk.name(res, std::format("raw encoder {} command pool", stream_idx));
return res;
@@ -52,7 +52,7 @@ wivrn::video_encoder_raw::video_encoder_raw(
uint8_t stream_idx) :
video_encoder(vk,
stream_idx,
*vk.transfer_queue ? vk.transfer_queue_family_index : vk.queue_family_index,
vk.transfer_queue ? vk.transfer_queue.family_index : vk.queue.family_index,
settings,
std::make_unique<dummy_idr_handler>(),
true),
@@ -105,7 +105,7 @@ void wivrn::video_encoder_raw::present_image(vk::Image y_cbcr, vk::SemaphoreSubm
vk::ImageMemoryBarrier2 barrier{
.dstStageMask = vk::PipelineStageFlagBits2KHR::eTransfer,
.dstAccessMask = vk::AccessFlagBits2::eTransferRead,
.srcQueueFamilyIndex = vk.queue_family_index,
.srcQueueFamilyIndex = vk.queue.family_index,
.dstQueueFamilyIndex = target_queue,
.image = y_cbcr,
.subresourceRange = {.aspectMask = vk::ImageAspectFlagBits::eColor,
@@ -156,21 +156,21 @@ void wivrn::video_encoder_raw::present_image(vk::Image y_cbcr, vk::SemaphoreSubm
cmd.end();
std::unique_lock lock(*vk.transfer_queue ? vk.transfer_queue_mutex : vk.queue_mutex);
std::unique_lock lock(vk.transfer_queue ? vk.transfer_queue.mutex : vk.queue.mutex);
vk::CommandBufferSubmitInfo cmd_info{
.commandBuffer = *cmd,
};
compositor_sem.stageMask = vk::PipelineStageFlagBits2::eTransfer;
vk.device.resetFences(*in[slot].fence);
(*vk.transfer_queue ? vk.transfer_queue : vk.queue)
.submit2(vk::SubmitInfo2{
.waitSemaphoreInfoCount = 1,
.pWaitSemaphoreInfos = &compositor_sem,
.commandBufferInfoCount = 1,
.pCommandBufferInfos = &cmd_info,
},
*in[slot].fence);
(vk.transfer_queue ? vk.transfer_queue : vk.queue)
.queue.submit2(vk::SubmitInfo2{
.waitSemaphoreInfoCount = 1,
.pWaitSemaphoreInfos = &compositor_sem,
.commandBufferInfoCount = 1,
.pCommandBufferInfos = &cmd_info,
},
*in[slot].fence);
}
std::optional<wivrn::video_encoder::data> wivrn::video_encoder_raw::encode(uint8_t slot, uint64_t frame_id)
+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,
+2
View File
@@ -22,6 +22,7 @@
#include <vulkan/vulkan_raii.hpp>
#include <vulkan/vulkan_structs.hpp>
#include "utils/wivrn_vk_bundle.h"
#include "video_encoder.h"
#include "vk/allocation.h"
@@ -30,6 +31,7 @@ namespace wivrn
class video_encoder_vulkan : public video_encoder
{
wivrn::vk_bundle & vk;
wivrn::vk_bundle::queue_data & encode_queue;
const vk::VideoEncodeCapabilitiesKHR encode_caps;
#ifdef VK_KHR_video_encode_intra_refresh
vk::VideoEncodeIntraRefreshInfoKHR intra_info{};
+11 -12
View File
@@ -93,7 +93,7 @@ vk::raii::CommandPool make_cmd_pool(wivrn::vk_bundle & vk, uint8_t stream_idx)
auto res = vk.device.createCommandPool(vk::CommandPoolCreateInfo{
.flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer | vk::CommandPoolCreateFlagBits::eTransient,
.queueFamilyIndex = *vk.transfer_queue ? vk.transfer_queue_family_index : vk.queue_family_index,
.queueFamilyIndex = vk.transfer_queue ? vk.transfer_queue.family_index : vk.queue.family_index,
});
vk.name(res, std::format("x264 encoder {} command pool", stream_idx));
return res;
@@ -106,7 +106,7 @@ video_encoder_x264::video_encoder_x264(
uint8_t stream_idx) :
video_encoder(vk,
stream_idx,
*vk.transfer_queue ? vk.transfer_queue_family_index : vk.queue_family_index,
vk.transfer_queue ? vk.transfer_queue.family_index : vk.queue.family_index,
settings,
std::make_unique<default_idr_handler>(),
false),
@@ -224,7 +224,7 @@ void video_encoder_x264::present_image(vk::Image y_cbcr, vk::SemaphoreSubmitInfo
vk::ImageMemoryBarrier2 barrier{
.dstStageMask = vk::PipelineStageFlagBits2KHR::eTransfer,
.dstAccessMask = vk::AccessFlagBits2::eTransferRead,
.srcQueueFamilyIndex = vk.queue_family_index,
.srcQueueFamilyIndex = vk.queue.family_index,
.dstQueueFamilyIndex = target_queue,
.image = y_cbcr,
.subresourceRange = {.aspectMask = vk::ImageAspectFlagBits::eColor,
@@ -274,21 +274,20 @@ void video_encoder_x264::present_image(vk::Image y_cbcr, vk::SemaphoreSubmitInfo
cmd.end();
std::unique_lock lock(*vk.transfer_queue ? vk.transfer_queue_mutex : vk.queue_mutex);
std::unique_lock lock(vk.transfer_queue ? vk.transfer_queue.mutex : vk.queue.mutex);
vk::CommandBufferSubmitInfo cmd_info{
.commandBuffer = *cmd,
};
compositor_sem.stageMask = vk::PipelineStageFlagBits2::eTransfer;
vk.device.resetFences(*in[slot].fence);
(*vk.transfer_queue ? vk.transfer_queue : vk.queue)
.submit2(vk::SubmitInfo2{
.waitSemaphoreInfoCount = 1,
.pWaitSemaphoreInfos = &compositor_sem,
.commandBufferInfoCount = 1,
.pCommandBufferInfos = &cmd_info,
},
*in[slot].fence);
(vk.transfer_queue ? vk.transfer_queue : vk.queue).queue.submit2(vk::SubmitInfo2{
.waitSemaphoreInfoCount = 1,
.pWaitSemaphoreInfos = &compositor_sem,
.commandBufferInfoCount = 1,
.pCommandBufferInfos = &cmd_info,
},
*in[slot].fence);
}
std::optional<video_encoder::data> video_encoder_x264::encode(uint8_t slot, uint64_t frame_index)
+30 -23
View File
@@ -126,7 +126,8 @@ int get_queue_index(const std::vector<vk::QueueFamilyProperties> & queues, std::
return info.queueCount++;
}
}
static std::array<float, 3> prios{1.0, 1.0, 1.0};
// worst case: compute, transfer and 3x encode are all of the same family
static std::array<float, 5> prios{1.0, 1.0, 1.0, 1.0, 1.0};
infos.push_back(vk::DeviceQueueCreateInfo{
.queueFamilyIndex = family_index,
.queueCount = 1,
@@ -140,12 +141,6 @@ wivrn::vk_bundle::vk_bundle() :
instance(nullptr),
physical_device(nullptr),
device(nullptr),
queue(nullptr),
queue_family_index(vk::QueueFamilyIgnored),
transfer_queue(nullptr),
transfer_queue_family_index(vk::QueueFamilyIgnored),
encode_queue(nullptr),
encode_queue_family_index(vk::QueueFamilyIgnored),
debug(nullptr)
{
// Create instance
@@ -218,14 +213,15 @@ wivrn::vk_bundle::vk_bundle() :
// Select queue families
auto queues = physical_device.getQueueFamilyProperties();
uint32_t encode_queue_family_index;
{
queue_family_index = select_queue(queues, vk::QueueFlagBits::eCompute);
transfer_queue_family_index = select_queue(queues, vk::QueueFlagBits::eTransfer);
queue.family_index = select_queue(queues, vk::QueueFlagBits::eCompute);
transfer_queue.family_index = select_queue(queues, vk::QueueFlagBits::eTransfer);
#if WIVRN_USE_VULKAN_ENCODE
encode_queue_family_index = select_queue(queues, vk::QueueFlagBits::eVideoEncodeKHR);
#endif
// Technically allowed to have a device with only encode or decode capabilities
if (queue_family_index == vk::QueueFamilyIgnored)
if (queue.family_index == vk::QueueFamilyIgnored)
throw std::runtime_error("GPU does not support vulkan compute");
}
@@ -295,13 +291,21 @@ wivrn::vk_bundle::vk_bundle() :
float prio = 1.0;
std::vector<vk::DeviceQueueCreateInfo> queues_info;
int queue_index = get_queue_index(queues, queues_info, queue_family_index);
int queue_index = get_queue_index(queues, queues_info, queue.family_index);
U_LOG_D("queue index: %d", queue_index);
int transfer_queue_index = get_queue_index(queues, queues_info, transfer_queue_family_index);
int transfer_queue_index = get_queue_index(queues, queues_info, transfer_queue.family_index);
U_LOG_D("transfer queue index: %d", transfer_queue_index);
#if WIVRN_USE_VULKAN_ENCODE
int encode_queue_index = get_queue_index(queues, queues_info, encode_queue_family_index);
U_LOG_D("encode queue index: %d", encode_queue_index);
std::vector<int> encode_queue_indices;
// Get up to 3 encode queues (left, right and alpha encoders)
for (int i = 0; i < 3; ++i)
{
int encode_queue_index = get_queue_index(queues, queues_info, encode_queue_family_index);
if (encode_queue_index < 0)
break;
U_LOG_D("encode queue index: %d", encode_queue_index);
encode_queue_indices.push_back(encode_queue_index);
}
#ifdef VK_KHR_video_maintenance1
if (has_device_ext(VK_KHR_VIDEO_MAINTENANCE_1_EXTENSION_NAME))
std::get<vk::PhysicalDeviceVideoMaintenance1FeaturesKHR>(feat).videoMaintenance1 =
@@ -353,19 +357,22 @@ wivrn::vk_bundle::vk_bundle() :
.ppEnabledExtensionNames = device_extensions.data(),
});
queue = device.getQueue(queue_family_index, queue_index);
name(queue, "compute queue");
queue.queue = device.getQueue(queue.family_index, queue_index);
name(queue.queue, "compute queue");
if (transfer_queue_index >= 0)
{
transfer_queue = device.getQueue(transfer_queue_family_index, transfer_queue_index);
name(transfer_queue, "transfer queue");
transfer_queue.queue = device.getQueue(transfer_queue.family_index, transfer_queue_index);
name(transfer_queue.queue, "transfer queue");
}
#if WIVRN_USE_VULKAN_ENCODE
if (encode_queue_index >= 0)
for (auto encode_queue_index: encode_queue_indices)
{
encode_queue = device.getQueue(encode_queue_family_index, encode_queue_index);
name(encode_queue, "encode queue");
auto & queue = encode_queues.emplace_back();
queue.queue = device.getQueue(encode_queue_family_index, encode_queue_index);
queue.family_index = encode_queue_family_index;
name(queue.queue, "encode queue");
}
U_LOG_D("Using %d vulkan encode queue(s)", int(encode_queues.size()));
#endif
}
@@ -382,9 +389,9 @@ wivrn::vk_bundle::vk_bundle() :
"\tGPU: %s\n"
"\tqueue families: %d %d %d (main, encode, transfer)\n",
prop.deviceName.data(),
int32_t(queue_family_index),
int32_t(queue.family_index),
int32_t(encode_queue_family_index),
int32_t(transfer_queue_family_index));
int32_t(transfer_queue.family_index));
}
uint32_t wivrn::vk_bundle::get_memory_type(uint32_t type_bits, vk::MemoryPropertyFlags memory_props)
+14 -11
View File
@@ -21,6 +21,7 @@
#include "vk/vk_allocator.h"
#include <cstdint>
#include <inplace_vector.hpp>
#include <mutex>
#include <type_traits>
#include <vector>
@@ -61,23 +62,25 @@ uint64_t vk_handle(const T & handle)
struct vk_bundle
{
struct queue_data
{
std::mutex mutex;
vk::raii::Queue queue = nullptr;
uint32_t family_index = vk::QueueFamilyIgnored;
operator bool() const
{
return *queue != VK_NULL_HANDLE;
}
};
vk::raii::Context vk_ctx;
vk::raii::Instance instance;
vk::raii::PhysicalDevice physical_device;
vk::raii::Device device;
std::optional<vk_allocator> allocator;
std::mutex queue_mutex;
vk::raii::Queue queue;
uint32_t queue_family_index;
std::mutex transfer_queue_mutex;
vk::raii::Queue transfer_queue;
uint32_t transfer_queue_family_index;
std::mutex encode_queue_mutex;
vk::raii::Queue encode_queue;
uint32_t encode_queue_family_index;
queue_data queue;
queue_data transfer_queue;
beman::inplace_vector::inplace_vector<queue_data, 3> encode_queues;
vk::raii::DebugUtilsMessengerEXT debug;