Files
WiVRn/server/encoder/video_encoder_vulkan.h
T
Jonathan Steffan b9c8a54d92 Add WiVRn-native Perfetto via Perfetto SDK
Optional server profiling built on the Perfetto SDK. Adds CPU spans
(compositor, encoder, network), GPU encode/copy slices from Vulkan timestamp
queries (Vulkan/NVENC/VAAPI), and headset feedback markers from dump_time.

WIVRN_TRACING enables tracing and selects the backend (system, inprocess, or
both); WIVRN_USE_PERFETTO gates it at build time and compiles it out otherwise.
WIVRN_TRACE_MONADO additionally drives Monado's percetto tracing from the same
switch so WiVRn and Monado land on one timeline.

Tooling: server-tracing[-monado] presets and a CI build, two Perfetto configs
(WiVRn-only and full-system), a wivrn_capture.py capture wrapper, a pftrace
summary script, and docs/profiling.md.
2026-07-01 21:04:08 +02:00

119 lines
4.0 KiB
C++

/*
* WiVRn VR streaming
* Copyright (C) 2024 Patrick Nicolas <[email protected]>
*
* 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 <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <unordered_map>
#include <vector>
#include <vulkan/vulkan_raii.hpp>
#include <vulkan/vulkan_structs.hpp>
#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<VkImage, vk::raii::ImageView> 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<bool> busy = false;
};
std::array<slot_item, num_slots> slot_data;
image_allocation dpb_image;
std::vector<vk::raii::DeviceMemory> 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<vk::VideoEncodeRateControlInfoKHR> 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<uint8_t> get_encoded_parameters(void * next);
virtual void send_idr_data() = 0;
virtual std::vector<void *> setup_slot_info(size_t dpb_size) = 0;
virtual void * encode_info_next(uint32_t frame_num, size_t slot, std::optional<int32_t> reference_slot) = 0;
virtual vk::ExtensionProperties std_header_version() = 0;
public:
void present_image(vk::Image y_cbcr, vk::SemaphoreSubmitInfo, uint8_t slot, uint64_t frame_index) override;
std::optional<data> encode(uint8_t slot, uint64_t frame_index) override;
};
} // namespace wivrn