Fix vulkan encode QFOT layouts (NVIDIA green frame)

Compositor release left layouts as eUndefined while encoder acquire
transitioned to eVideoEncodeSrcKHR (or eTransferSrcOptimal on the NVIDIA
tmp_image path); the spec forbids that layout mismatch for exclusive-mode
QFOT. NVIDIA honoured it by treating encode-queue contents as undefined
(solid green frame).

Encoders now publish target_layout. The compositor's release and the
encoder's acquire both transition eGeneral -> target_layout in a single
barrier; per VkImageMemoryBarrier2 the layout transition is performed
exactly once between the queues, so no second non-QFOT barrier is needed.
target_layout is eVideoEncodeSrcKHR on the direct vulkan encode path and
eGeneral elsewhere.

Also fix optimal_transfer(): the inner `auto to = ...` shadowed the
parameter, so the maintenance9 check was effectively always false.
This commit is contained in:
Jonathan Steffan
2026-05-19 06:59:03 +02:00
committed by Patrick Nicolas
parent f54be85c26
commit e79621d377
4 changed files with 49 additions and 28 deletions
+10
View File
@@ -442,6 +442,16 @@ xrt_result_t compositor::layer_commit(xrt_graphics_sync_handle_t sync_handle)
.srcAccessMask = vk::AccessFlagBits2::eShaderStorageWrite,
.dstStageMask = vk::PipelineStageFlagBits2::eAllCommands,
.dstAccessMask = vk::AccessFlagBits2::eMemoryRead,
// For a queue-family ownership transfer in EXCLUSIVE
// sharing mode, the release barrier's old/new layout
// must match the encoder-side acquire. newLayout is
// the encoder's target_layout; per the VkImageMemoryBarrier2
// spec the layout transition is executed exactly once
// between the queues, so this single QFOT barrier covers
// both the queue-family transfer and the layout
// transition the encoder needs.
.oldLayout = vk::ImageLayout::eGeneral,
.newLayout = encoder->target_layout,
.srcQueueFamilyIndex = vk.queue_family_index,
.dstQueueFamilyIndex = encoder->target_queue,
.image = images[i].image,
+4
View File
@@ -78,6 +78,10 @@ public:
const uint8_t stream_idx;
const uint32_t target_queue;
const bool need_transfer;
// Layout the encoder needs y_cbcr to arrive in. Set during construction
// or init() and read by the compositor on every layer_commit — must not
// change after the first frame.
vk::ImageLayout target_layout = vk::ImageLayout::eGeneral;
static const uint8_t num_slots = 2;
const double bitrate_multiplier;
+33 -26
View File
@@ -317,6 +317,13 @@ void wivrn::video_encoder_vulkan::init(const vk::VideoCapabilitiesKHR & video_ca
}
}
// tmp_image path: copyImage source accepts eGeneral, so no transition needed.
// Direct path: vkCmdEncodeVideoKHR requires eVideoEncodeSrcKHR — folding it
// into target_layout lets the QFOT barrier perform the transition in one step.
target_layout = tmp_image
? vk::ImageLayout::eGeneral
: vk::ImageLayout::eVideoEncodeSrcKHR;
// Decode picture buffer (DPB) images
vk::VideoFormatPropertiesKHR reference_picture_format = select_video_format(
vk.physical_device,
@@ -638,14 +645,13 @@ void wivrn::video_encoder_vulkan::present_image(vk::Image y_cbcr, vk::SemaphoreS
compositor_sem.stageMask = vk::PipelineStageFlagBits2::eCopy;
beman::inplace_vector::inplace_vector<vk::ImageMemoryBarrier2, 2> im_barriers;
vk::ImageLayout y_cbcr_layout{vk::ImageLayout::eGeneral};
if (need_transfer)
{
im_barriers.push_back(vk::ImageMemoryBarrier2{
.dstStageMask = vk::PipelineStageFlagBits2KHR::eCopy,
.dstAccessMask = vk::AccessFlagBits2::eTransferRead,
.oldLayout = y_cbcr_layout,
.newLayout = vk::ImageLayout::eTransferSrcOptimal,
.oldLayout = vk::ImageLayout::eGeneral,
.newLayout = target_layout,
.srcQueueFamilyIndex = vk.queue_family_index,
.dstQueueFamilyIndex = target_queue,
.image = y_cbcr,
@@ -655,7 +661,6 @@ void wivrn::video_encoder_vulkan::present_image(vk::Image y_cbcr, vk::SemaphoreS
.baseArrayLayer = stream_idx,
.layerCount = 1},
});
y_cbcr_layout = vk::ImageLayout::eTransferSrcOptimal;
}
im_barriers.push_back(vk::ImageMemoryBarrier2{
.dstStageMask = vk::PipelineStageFlagBits2KHR::eCopy,
@@ -677,7 +682,7 @@ void wivrn::video_encoder_vulkan::present_image(vk::Image y_cbcr, vk::SemaphoreS
video_cmd_buf.copyImage(
y_cbcr,
y_cbcr_layout,
target_layout,
tmp_image,
vk::ImageLayout::eTransferDstOptimal,
std::array{
@@ -739,27 +744,29 @@ void wivrn::video_encoder_vulkan::present_image(vk::Image y_cbcr, vk::SemaphoreS
else
{
compositor_sem.stageMask = vk::PipelineStageFlagBits2::eVideoEncodeKHR;
if (need_transfer)
{
vk::ImageMemoryBarrier2 video_barrier{
.dstStageMask = vk::PipelineStageFlagBits2KHR::eVideoEncodeKHR,
.dstAccessMask = vk::AccessFlagBits2::eVideoEncodeReadKHR,
.oldLayout = vk::ImageLayout::eGeneral,
.newLayout = vk::ImageLayout::eVideoEncodeSrcKHR,
.srcQueueFamilyIndex = vk.queue_family_index,
.dstQueueFamilyIndex = target_queue,
.image = y_cbcr,
.subresourceRange = {.aspectMask = vk::ImageAspectFlagBits::eColor,
.baseMipLevel = 0,
.levelCount = 1,
.baseArrayLayer = stream_idx,
.layerCount = 1},
};
video_cmd_buf.pipelineBarrier2({
.imageMemoryBarrierCount = 1,
.pImageMemoryBarriers = &video_barrier,
});
}
// Single barrier: when need_transfer, this is the QFOT acquire and
// carries the layout transition (eGeneral → target_layout) per the
// VkImageMemoryBarrier2 spec (executed once between the queues).
// When need_transfer is false (optimal cross-queue transfer, no QFOT)
// the same barrier degenerates to a plain layout transition.
vk::ImageMemoryBarrier2 barrier{
.dstStageMask = vk::PipelineStageFlagBits2KHR::eVideoEncodeKHR,
.dstAccessMask = vk::AccessFlagBits2::eVideoEncodeReadKHR,
.oldLayout = vk::ImageLayout::eGeneral,
.newLayout = target_layout,
.srcQueueFamilyIndex = need_transfer ? vk.queue_family_index : vk::QueueFamilyIgnored,
.dstQueueFamilyIndex = need_transfer ? target_queue : vk::QueueFamilyIgnored,
.image = y_cbcr,
.subresourceRange = {.aspectMask = vk::ImageAspectFlagBits::eColor,
.baseMipLevel = 0,
.levelCount = 1,
.baseArrayLayer = stream_idx,
.layerCount = 1},
};
video_cmd_buf.pipelineBarrier2({
.imageMemoryBarrierCount = 1,
.pImageMemoryBarriers = &barrier,
});
auto it = image_views.find(VkImage(y_cbcr));
if (it != image_views.end())
+2 -2
View File
@@ -419,8 +419,8 @@ bool wivrn::vk_bundle::optimal_transfer(uint32_t from, uint32_t to) const
{
auto props = physical_device.getQueueFamilyProperties2<vk::StructureChain<vk::QueueFamilyProperties2, vk::QueueFamilyOwnershipTransferPropertiesKHR>>();
auto to = std::get<1>(props[from]).optimalImageTransferToQueueFamilies;
return to & (1 << to);
auto mask = std::get<1>(props[from]).optimalImageTransferToQueueFamilies;
return (mask & (1u << to)) != 0;
}
#endif
return true;