diff --git a/server/driver/wivrn_comp_target.cpp b/server/driver/wivrn_comp_target.cpp index 4011c2c3..65914315 100644 --- a/server/driver/wivrn_comp_target.cpp +++ b/server/driver/wivrn_comp_target.cpp @@ -131,6 +131,31 @@ static void create_encoders(wivrn_comp_target * cn) std::map>> 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()) diff --git a/server/driver/wivrn_comp_target.h b/server/driver/wivrn_comp_target.h index 4c41761b..2c4408fd 100644 --- a/server/driver/wivrn_comp_target.h +++ b/server/driver/wivrn_comp_target.h @@ -60,7 +60,11 @@ struct pseudo_swapchain vk::raii::ImageView image_view_y = nullptr; vk::raii::ImageView image_view_cbcr = nullptr; status_t status; + vk::raii::CommandBuffer video_command_buffer = nullptr; + vk::raii::Fence video_fence = nullptr; }; + vk::raii::Semaphore video_sem = nullptr; + vk::raii::CommandPool video_command_pool = nullptr; std::vector images; // bitmask of encoder status, first bit to request exit, then one bit per thread: @@ -71,6 +75,7 @@ struct pseudo_swapchain // Data to be encoded vk::raii::Fence fence = nullptr; vk::raii::CommandBuffer command_buffer = nullptr; + int64_t frame_index; to_headset::video_stream_data_shard::view_info_t view_info{}; }; diff --git a/server/encoder/video_encoder.cpp b/server/encoder/video_encoder.cpp index 7b6f240c..9cae88d8 100644 --- a/server/encoder/video_encoder.cpp +++ b/server/encoder/video_encoder.cpp @@ -223,7 +223,7 @@ void VideoEncoder::SyncNeeded() sync_needed = true; } -void VideoEncoder::PresentImage(vk::Image y_cbcr, vk::raii::CommandBuffer & cmd_buf) +void VideoEncoder::present_image(vk::Image y_cbcr, vk::raii::CommandBuffer & cmd_buf) { // Wait for encoder to be done busy[next_present].wait(true); @@ -233,6 +233,11 @@ void VideoEncoder::PresentImage(vk::Image y_cbcr, vk::raii::CommandBuffer & cmd_ next_present = (next_present + 1) % num_slots; } +void VideoEncoder::present_image(vk::Image y_cbcr, vk::raii::CommandBuffer & video_cmd_buf, vk::Fence fence) +{ + present_image(sync_needed.load(), y_cbcr, video_cmd_buf, fence, next_present); +} + void VideoEncoder::Encode(wivrn_session & cnx, const to_headset::video_stream_data_shard::view_info_t & view_info, uint64_t frame_index) diff --git a/server/encoder/video_encoder.h b/server/encoder/video_encoder.h index 71b7901a..777b8b36 100644 --- a/server/encoder/video_encoder.h +++ b/server/encoder/video_encoder.h @@ -113,7 +113,9 @@ public: VideoEncoder(bool async_send = false); virtual ~VideoEncoder(); - void PresentImage(vk::Image y_cbcr, vk::raii::CommandBuffer & cmd_buf); + void present_image(vk::Image y_cbcr, vk::raii::CommandBuffer & cmd_buf); + // for vulkan video (command buffer is on a video queue) + void present_image(vk::Image y_cbcr, vk::raii::CommandBuffer & cmd_buf, vk::Fence fence); // The other end lost a frame and needs to resynchronize void SyncNeeded(); @@ -124,7 +126,9 @@ public: protected: // called on present to submit command buffers for the image. - virtual void present_image(vk::Image y_cbcr, vk::raii::CommandBuffer & cmd_buf, uint8_t slot) = 0; + virtual void present_image(vk::Image y_cbcr, vk::raii::CommandBuffer & cmd_buf, uint8_t slot) {}; + // for vulkan video (command buffer is on a video queue) + virtual void present_image(bool idr, vk::Image y_cbcr, vk::raii::CommandBuffer & cmd_buf, vk::Fence, uint8_t slot) {}; // called when command buffer finished executing virtual std::optional encode(bool idr, std::chrono::steady_clock::time_point target_timestamp, uint8_t slot) = 0; diff --git a/server/encoder/video_encoder_vulkan.cpp b/server/encoder/video_encoder_vulkan.cpp index 097561d3..b8d3e130 100644 --- a/server/encoder/video_encoder_vulkan.cpp +++ b/server/encoder/video_encoder_vulkan.cpp @@ -102,8 +102,6 @@ void wivrn::video_encoder_vulkan::init(const vk::VideoCapabilitiesKHR & video_ca void * video_session_create_next, void * session_params_next) { - fence = vk.device.createFence({}); - vk::VideoProfileListInfoKHR video_profile_list{ .profileCount = 1, .pProfiles = &video_profile, @@ -222,6 +220,7 @@ void wivrn::video_encoder_vulkan::init(const vk::VideoCapabilitiesKHR & video_ca .usage = picture_format.imageUsageFlags, }; image_view_template = { + .pNext = &image_view_template_next, .viewType = vk::ImageViewType::e2D, .format = picture_format.format, .components = picture_format.componentMapping, @@ -303,17 +302,6 @@ void wivrn::video_encoder_vulkan::init(const vk::VideoCapabilitiesKHR & video_ca query_pool = vk.device.createQueryPool(query_pool_create.get()); } - - // command pool and buffer - { - command_pool = vk.device.createCommandPool({ - .flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer, - .queueFamilyIndex = vk.encode_queue_family_index, - }); - - command_buffer = std::move(vk.device.allocateCommandBuffers({.commandPool = *command_pool, - .commandBufferCount = 1})[0]); - } } wivrn::video_encoder_vulkan::~video_encoder_vulkan() @@ -331,10 +319,41 @@ std::vector wivrn::video_encoder_vulkan::get_encoded_parameters(void * std::optional wivrn::video_encoder_vulkan::encode(bool idr, std::chrono::steady_clock::time_point target_timestamp, uint8_t encode_slot) { - command_buffer.reset(); - command_buffer.begin(vk::CommandBufferBeginInfo{ - .flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit, - }); + if (idr) + send_idr_data(); + + if (auto res = vk.device.waitForFences(fences[encode_slot], true, 1'000'000'000); + res != vk::Result::eSuccess) + { + throw std::runtime_error("wait for fences: " + vk::to_string(res)); + } + + // Feedback = offset / size / has overrides + auto [res, feedback] = query_pool.getResults(0, 1, 3 * sizeof(uint32_t), 0, vk::QueryResultFlagBits::eWait); + if (res != vk::Result::eSuccess) + { + std::cerr << "device.getQueryPoolResults: " << vk::to_string(res) << std::endl; + } + + return data{ + .encoder = this, + .span = std::span(((uint8_t *)output_buffer.map()) + feedback[0], feedback[1]), + }; +} + +void wivrn::video_encoder_vulkan::present_image(bool idr, vk::Image src_yuv, vk::raii::CommandBuffer & command_buffer, vk::Fence fence, uint8_t encode_slot) +{ + fences[encode_slot] = fence; + auto it = image_views.find(src_yuv); + vk::ImageView image_view; + if (it != image_views.end()) + image_view = *it->second; + else + { + image_view_template.image = src_yuv; + image_view = *image_views.emplace(src_yuv, vk.device.createImageView(image_view_template)).first->second; + } + command_buffer.resetQueryPool(*query_pool, 0, 1); if (idr) @@ -412,7 +431,7 @@ std::optional wivrn::video_encoder_vulkan::encode(boo .dstBufferRange = output_buffer_size, .srcPictureResource = {.codedExtent = rect.extent, .baseArrayLayer = 0, - .imageViewBinding = image_views_slots[encode_slot]}, + .imageViewBinding = image_view}, .pSetupReferenceSlot = &dpb_slots[slot], .referenceSlotCount = uint32_t(ref_slots.size()), .pReferenceSlots = ref_slots.data(), @@ -424,46 +443,5 @@ std::optional wivrn::video_encoder_vulkan::encode(boo command_buffer.endVideoCodingKHR(vk::VideoEndCodingInfoKHR{}); command_buffer.end(); - vk::SubmitInfo2 submit{}; - vk::CommandBufferSubmitInfo cmd_info{ - .commandBuffer = *command_buffer, - }; - submit.setCommandBufferInfos(cmd_info); - vk.encode_queue.submit2(submit, *fence); ++frame_num; - - if (idr) - send_idr_data(); - - if (auto res = vk.device.waitForFences(*fence, true, 1'000'000'000); - res != vk::Result::eSuccess) - { - throw std::runtime_error("wait for fences: " + vk::to_string(res)); - } - - // Feedback = offset / size / has overrides - auto [res, feedback] = query_pool.getResults(0, 1, 3 * sizeof(uint32_t), 0, vk::QueryResultFlagBits::eWait); - if (res != vk::Result::eSuccess) - { - std::cerr << "device.getQueryPoolResults: " << vk::to_string(res) << std::endl; - } - - vk.device.resetFences(*fence); - - return data{ - .encoder = this, - .span = std::span(((uint8_t *)output_buffer.map()) + feedback[0], feedback[1]), - }; -} - -void wivrn::video_encoder_vulkan::present_image(vk::Image src_yuv, vk::raii::CommandBuffer &, uint8_t slot) -{ - auto it = image_views.find(src_yuv); - if (it != image_views.end()) - image_views_slots[slot] = *it->second; - else - { - image_view_template.image = src_yuv; - image_views_slots[slot] = *image_views.emplace(src_yuv, vk.device.createImageView(image_view_template)).first->second; - } } diff --git a/server/encoder/video_encoder_vulkan.h b/server/encoder/video_encoder_vulkan.h index ea582a26..d9eb8635 100644 --- a/server/encoder/video_encoder_vulkan.h +++ b/server/encoder/video_encoder_vulkan.h @@ -31,15 +31,10 @@ class video_encoder_vulkan : public VideoEncoder wivrn_vk_bundle & vk; const vk::VideoEncodeCapabilitiesKHR encode_caps; - vk::raii::Fence fence = nullptr; - vk::raii::VideoSessionKHR video_session = nullptr; vk::raii::VideoSessionParametersKHR video_session_parameters = nullptr; vk::raii::QueryPool query_pool = nullptr; - vk::raii::CommandPool command_pool = nullptr; - - vk::raii::CommandBuffer command_buffer = nullptr; buffer_allocation output_buffer; size_t output_buffer_size; @@ -47,7 +42,7 @@ class video_encoder_vulkan : public VideoEncoder vk::ImageViewUsageCreateInfo image_view_template_next; vk::ImageViewCreateInfo image_view_template; std::unordered_map image_views; // for input images - std::array image_views_slots; + std::array fences; image_allocation dpb_image; std::vector dpb_image_views; @@ -90,6 +85,6 @@ protected: public: std::optional encode(bool idr, std::chrono::steady_clock::time_point target_timestamp, uint8_t slot) override; - void present_image(vk::Image src_yuv, vk::raii::CommandBuffer & cmd_buf, uint8_t slot) override; + void present_image(bool idr, vk::Image y_cbcr, vk::raii::CommandBuffer & cmd_buf, vk::Fence, uint8_t slot) override; }; } // namespace wivrn