/* * WiVRn VR streaming * Copyright (C) 2024 Patrick Nicolas * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #pragma once #include #include #include #include #include "utils/gpu_timestamp_pool.h" #include "utils/wivrn_vk_bundle.h" #include "video_encoder.h" #include "vk/allocation.h" 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{}; #endif vk::raii::VideoSessionKHR video_session = nullptr; vk::raii::VideoSessionParametersKHR video_session_parameters = nullptr; vk::raii::Semaphore sem = nullptr; uint64_t sem_value = 0; vk::raii::QueryPool query_pool = nullptr; gpu_timestamp_pool ts_pool; // Optional per-op GPU timestamp pools; created only when their path runs. gpu_timestamp_pool ts_pool_image_copy; gpu_timestamp_pool ts_pool_host_copy; gpu_timestamp_pool ts_pool_host_copy_overflow; vk::raii::CommandPool transfer_command_pool = nullptr; vk::raii::CommandPool video_command_pool = nullptr; image_allocation tmp_image; vk::ImageViewUsageCreateInfo image_view_template_next; vk::ImageViewCreateInfo image_view_template; std::unordered_map image_views; // for input images struct slot_item { vk::raii::Fence fence = nullptr; vk::raii::CommandBuffer video_cmd_buf = nullptr; vk::raii::CommandBuffer transfer_cmd_buf = nullptr; vk::raii::ImageView view = nullptr; buffer_allocation output_buffer; buffer_allocation host_buffer; vk::DeviceSize copy_size; bool idr = false; std::atomic busy = false; }; std::array slot_data; image_allocation dpb_image; std::vector mem; vk::VideoFormatPropertiesKHR select_video_format( vk::raii::PhysicalDevice & physical_device, const vk::PhysicalDeviceVideoFormatInfoKHR &); bool session_initialized = false; protected: const uint8_t num_dpb_slots; vk::Extent3D aligned_extent; vk::VideoEncodeRateControlLayerInfoKHR rate_control_layer; std::optional rate_control; video_encoder_vulkan(wivrn::vk_bundle & vk, const vk::VideoCapabilitiesKHR & video_caps, const vk::VideoEncodeCapabilitiesKHR & encode_caps, uint8_t stream_idx, const encoder_settings & settings); void init(const vk::VideoCapabilitiesKHR & video_caps, #ifdef VK_KHR_video_encode_intra_refresh vk::VideoEncodeIntraRefreshCapabilitiesKHR intra_caps, #endif const vk::VideoProfileInfoKHR & video_profile, void * video_session_create_next, void * session_params_next); virtual ~video_encoder_vulkan(); std::vector get_encoded_parameters(void * next); virtual void send_idr_data() = 0; virtual std::vector setup_slot_info(size_t dpb_size) = 0; virtual void * encode_info_next(uint32_t frame_num, size_t slot, std::optional reference_slot) = 0; virtual vk::ExtensionProperties std_header_version() = 0; // Codec-specific structures to prepend to the vkCmdBeginVideoCodingKHR pNext chain. // Note that this chain accepts structures the vkCmdControlVideoCodingKHR one does not, // so it must not be reused for rate control updates. virtual const void * begin_coding_next(const void * next) { return next; } public: void present_image(vk::Image y_cbcr, vk::SemaphoreSubmitInfo, uint8_t slot, uint64_t frame_index) override; std::optional encode(uint8_t slot, uint64_t frame_index) override; }; } // namespace wivrn