Implement main compositor

Replace the patches to Monado's main compositor with a local
implementation, reusing code when relevant.

The layer squasher is very close to comp_render_cs, while distortion is
completely replaced by foveation.
This commit is contained in:
Patrick Nicolas
2026-03-31 09:06:27 +02:00
parent b2f95938e6
commit 22a819b8c8
60 changed files with 3147 additions and 3680 deletions
+12 -25
View File
@@ -19,12 +19,12 @@
#include "encoder_settings.h"
#include "driver/configuration.h"
#include "driver/wivrn_comp_target.h"
#include "driver/wivrn_session.h"
#include "util/u_logging.h"
#include "utils/wivrn_vk_bundle.h"
#include "video_encoder.h"
#include "wivrn_packets.h"
#include <magic_enum.hpp>
#include <string>
#include <vulkan/vulkan.hpp>
@@ -46,6 +46,7 @@ static const double passthrough_bitrate_factor = 0.05;
static void split_bitrate(std::array<wivrn::encoder_settings, 3> & encoders, uint64_t bitrate)
{
assert(bitrate > 0);
double total_weight = 0;
for (auto [i, encoder]: std::ranges::enumerate_view(encoders))
{
@@ -75,22 +76,13 @@ static void split_bitrate(std::array<wivrn::encoder_settings, 3> & encoders, uin
void print_encoders(const std::array<wivrn::encoder_settings, 3> & encoders)
{
int group = -1;
std::stringstream str;
str << "Encoder configuration:";
for (auto & encoder: encoders)
{
if (encoder.group != group)
{
group = encoder.group;
str << "\n\t* Group " << group << ":";
}
else
str << "\n";
str << "\n\t\t" << encoder.encoder_name << " (" << magic_enum::enum_name(encoder.codec) << " " << encoder.bit_depth << "-bit)"
<< "\n\t\tsize: " << encoder.width << "x" << encoder.height
<< "\n\t\tbitrate: " << int(encoder.bitrate / 100'000) / 10. << "Mbit/s";
str << "\n\t* " << encoder.encoder_name << " (" << magic_enum::enum_name(encoder.codec) << " " << encoder.bit_depth << "-bit)"
<< "\n\t size: " << encoder.width << "x" << encoder.height
<< "\n\t bitrate: " << int(encoder.bitrate / 100'000) / 10. << "Mbit/s";
}
U_LOG_I("%s", str.str().c_str());
}
@@ -111,7 +103,7 @@ namespace
{
class prober
{
wivrn_vk_bundle & vk;
wivrn::vk_bundle & vk;
const from_headset::headset_info_packet & info;
const bool nvidia;
@@ -211,7 +203,7 @@ class prober
#endif
public:
prober(wivrn_vk_bundle & vk, const from_headset::headset_info_packet & info) :
prober(wivrn::vk_bundle & vk, const from_headset::headset_info_packet & info) :
vk(vk), info(info), nvidia(is_nvidia(vk.physical_device)) {}
std::pair<std::string, video_codec> select_encoder(const configuration::encoder & config)
@@ -283,28 +275,23 @@ static uint16_t align(uint16_t value, uint16_t alignment)
return ((value + alignment - 1) / alignment) * alignment;
}
std::array<encoder_settings, 3> get_encoder_settings(wivrn_vk_bundle & bundle, const from_headset::headset_info_packet & info, const from_headset::settings_changed & settings)
std::array<encoder_settings, 3> get_encoder_settings(wivrn::vk_bundle & bundle, wivrn_session & session)
{
configuration config;
std::array<wivrn::encoder_settings, 3> res;
const auto & info = session.get_info();
const auto settings = *session.get_settings();
prober prober{bundle, info};
std::unordered_map<std::string, int> groups;
int next_group = 0;
for (auto [src, dst]: std::ranges::zip_view(config.encoders, res))
{
dst.fps = get_default_rate(info, settings);
dst.fps = session.default_rate();
dst.options = src.options;
dst.device = src.device;
std::tie(dst.encoder_name, dst.codec) = prober.select_encoder(src);
auto [it, inserted] = groups.emplace(dst.encoder_name, next_group);
dst.group = it->second;
if (inserted)
++next_group;
}
auto width = align(info.stream_eye_width, 64);
+3 -4
View File
@@ -27,7 +27,8 @@
namespace wivrn
{
struct wivrn_vk_bundle;
struct vk_bundle;
class wivrn_session;
struct encoder_settings
{
@@ -40,13 +41,11 @@ struct encoder_settings
uint64_t bitrate; // bit/s
double bitrate_multiplier; // encoder bitrate / global bitrate
std::map<std::string, std::string> options; // additional encoder-specific configuration
// encoders in the same group are executed in sequence
int group = 0;
int bit_depth;
std::optional<std::string> device;
};
std::array<encoder_settings, 3> get_encoder_settings(wivrn_vk_bundle &, const from_headset::headset_info_packet & info, const from_headset::settings_changed & settings);
std::array<encoder_settings, 3> get_encoder_settings(wivrn::vk_bundle &, wivrn_session &);
void print_encoders(const std::array<wivrn::encoder_settings, 3> & encoders);
+2 -3
View File
@@ -18,7 +18,6 @@
*/
// must be included before vulkan_raii
#include "vk/vk_helpers.h"
#include "video_encoder_va.h"
@@ -156,11 +155,11 @@ vk::Format drm_to_vulkan_fmt(uint32_t drm_fourcc, int bit_depth)
} // namespace
video_encoder_va::video_encoder_va(wivrn_vk_bundle & vk,
video_encoder_va::video_encoder_va(wivrn::vk_bundle & vk,
const wivrn::encoder_settings & settings,
uint8_t stream_idx) :
video_encoder_ffmpeg(stream_idx, settings),
synchronization2(vk.vk.features.synchronization_2)
synchronization2(std::get<vk::PhysicalDeviceVulkan13Features>(vk.feat).synchronization2)
{
auto drm_hw_ctx = make_drm_hw_ctx(vk.physical_device, settings.device);
AVBufferRef * tmp;
+1 -2
View File
@@ -29,7 +29,6 @@
namespace wivrn
{
struct encoder_settings;
struct wivrn_vk_bundle;
class video_encoder_va : public video_encoder_ffmpeg
{
@@ -46,7 +45,7 @@ class video_encoder_va : public video_encoder_ffmpeg
bool synchronization2 = false;
public:
video_encoder_va(wivrn_vk_bundle &, const wivrn::encoder_settings & settings, uint8_t stream_index);
video_encoder_va(wivrn::vk_bundle &, const wivrn::encoder_settings & settings, uint8_t stream_index);
std::pair<bool, vk::Semaphore> present_image(vk::Image y_cbcr, bool transferred, vk::raii::CommandBuffer & cmd_buf, uint8_t slot, uint64_t frame_index) override;
+1 -1
View File
@@ -101,7 +101,7 @@ std::shared_ptr<video_encoder::sender> video_encoder::sender::get()
}
std::unique_ptr<video_encoder> video_encoder::create(
wivrn_vk_bundle & wivrn_vk,
wivrn::vk_bundle & wivrn_vk,
const encoder_settings & settings,
uint8_t stream_idx)
{
+2 -2
View File
@@ -37,7 +37,7 @@ namespace wivrn
{
struct encoder_settings;
struct wivrn_vk_bundle;
struct vk_bundle;
class wivrn_session;
inline const char * encoder_nvenc = "nvenc";
@@ -111,7 +111,7 @@ protected:
public:
static std::unique_ptr<video_encoder> create(
wivrn_vk_bundle &,
wivrn::vk_bundle &,
const encoder_settings & settings,
uint8_t stream_idx);
+1 -1
View File
@@ -149,7 +149,7 @@ void video_encoder_nvenc::set_init_params_fps(float framerate)
}
video_encoder_nvenc::video_encoder_nvenc(
wivrn_vk_bundle & vk,
wivrn::vk_bundle & vk,
const encoder_settings & settings,
uint8_t stream_idx) :
video_encoder(stream_idx, settings, std::make_unique<default_idr_handler>(), true),
+2 -2
View File
@@ -33,7 +33,7 @@ namespace wivrn
class video_encoder_nvenc : public video_encoder
{
private:
wivrn_vk_bundle & vk;
wivrn::vk_bundle & vk;
std::shared_ptr<video_encoder_nvenc_shared_state> shared_state;
@@ -58,7 +58,7 @@ private:
void set_init_params_fps(float framerate);
public:
video_encoder_nvenc(wivrn_vk_bundle & vk, const encoder_settings & settings, uint8_t stream_idx);
video_encoder_nvenc(wivrn::vk_bundle & vk, const encoder_settings & settings, uint8_t stream_idx);
~video_encoder_nvenc();
std::pair<bool, vk::Semaphore> present_image(vk::Image y_cbcr, bool transferred, vk::raii::CommandBuffer & cmd_buf, uint8_t slot, uint64_t frame_index) override;
+1 -1
View File
@@ -36,7 +36,7 @@ public:
} // namespace
wivrn::video_encoder_raw::video_encoder_raw(
wivrn_vk_bundle & vk,
wivrn::vk_bundle & vk,
const encoder_settings & settings,
uint8_t stream_idx) :
video_encoder(stream_idx, settings, std::make_unique<dummy_idr_handler>(), true)
+1 -1
View File
@@ -31,7 +31,7 @@ class video_encoder_raw : public video_encoder
std::array<buffer_allocation, num_slots> buffers;
public:
video_encoder_raw(wivrn_vk_bundle & vk, const encoder_settings & settings, uint8_t stream_idx);
video_encoder_raw(wivrn::vk_bundle & vk, const encoder_settings & settings, uint8_t stream_idx);
std::pair<bool, vk::Semaphore> present_image(vk::Image y_cbcr, bool transferred, vk::raii::CommandBuffer & cmd_buf, uint8_t slot, uint64_t frame_index) override;
+4 -6
View File
@@ -15,13 +15,11 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "vk/vk_helpers.h"
#include "video_encoder_vulkan.h"
#include "encoder/encoder_settings.h"
#include "util/u_logging.h"
#include "utils/scoped_lock.h"
#include "utils/wivrn_vk_bundle.h"
#include <iostream>
#include <stdexcept>
@@ -157,7 +155,7 @@ vk::VideoFormatPropertiesKHR wivrn::video_encoder_vulkan::select_video_format(
}
wivrn::video_encoder_vulkan::video_encoder_vulkan(
wivrn_vk_bundle & vk,
wivrn::vk_bundle & vk,
const vk::VideoCapabilitiesKHR & video_caps,
const vk::VideoEncodeCapabilitiesKHR & in_encode_caps,
uint8_t stream_idx,
@@ -378,7 +376,7 @@ void wivrn::video_encoder_vulkan::init(const vk::VideoCapabilitiesKHR & video_ca
.layerCount = 1},
};
if (not vk.vk.features.video_maintenance_1)
if (not std::get<vk::PhysicalDeviceVideoMaintenance1FeaturesKHR>(vk.feat).videoMaintenance1)
{
image_view_template.subresourceRange.baseArrayLayer = 0;
for (size_t i = 0; i < num_slots; ++i)
@@ -591,7 +589,7 @@ std::optional<wivrn::video_encoder::data> wivrn::video_encoder_vulkan::encode(ui
vk.device.resetFences(*slot_item.fence);
{
wivrn::scoped_lock lock(main_queue ? vk.vk.main_queue->mutex : vk.vk.encode_queue->mutex);
std::unique_lock lock(main_queue ? vk.queue_mutex : vk.encode_queue_mutex);
auto & queue = main_queue ? vk.queue : vk.encode_queue;
queue.submit(vk::SubmitInfo{
.commandBufferCount = 1,
@@ -961,7 +959,7 @@ void wivrn::video_encoder_vulkan::post_submit(uint8_t slot)
};
{
scoped_lock lock(vk.vk.encode_queue->mutex);
std::unique_lock lock(vk.encode_queue_mutex);
vk.encode_queue.submit2(vk::SubmitInfo2{
.waitSemaphoreInfoCount = 1,
.pWaitSemaphoreInfos = &wait_sem_info,
+2 -2
View File
@@ -29,7 +29,7 @@ namespace wivrn
{
class video_encoder_vulkan : public video_encoder
{
wivrn_vk_bundle & vk;
wivrn::vk_bundle & vk;
const vk::VideoEncodeCapabilitiesKHR encode_caps;
vk::raii::VideoSessionKHR video_session = nullptr;
@@ -74,7 +74,7 @@ protected:
vk::VideoEncodeRateControlLayerInfoKHR rate_control_layer;
std::optional<vk::VideoEncodeRateControlInfoKHR> rate_control;
video_encoder_vulkan(wivrn_vk_bundle & vk,
video_encoder_vulkan(wivrn::vk_bundle & vk,
const vk::VideoCapabilitiesKHR & video_caps,
const vk::VideoEncodeCapabilitiesKHR & encode_caps,
uint8_t stream_idx,
+2 -2
View File
@@ -70,7 +70,7 @@ static StdVideoH264LevelIdc compute_level(const StdVideoH264SequenceParameterSet
}
wivrn::video_encoder_vulkan_h264::video_encoder_vulkan_h264(
wivrn_vk_bundle & vk,
wivrn::vk_bundle & vk,
const vk::VideoCapabilitiesKHR & video_caps,
const vk::VideoEncodeCapabilitiesKHR & encode_caps,
uint8_t stream_idx,
@@ -210,7 +210,7 @@ auto get_video_caps(vk::raii::PhysicalDevice & phys_dev)
}
std::unique_ptr<wivrn::video_encoder_vulkan_h264> wivrn::video_encoder_vulkan_h264::create(
wivrn_vk_bundle & vk,
wivrn::vk_bundle & vk,
const encoder_settings & settings,
uint8_t stream_idx)
{
+2 -2
View File
@@ -48,7 +48,7 @@ class video_encoder_vulkan_h264 : public video_encoder_vulkan
vk::VideoEncodeH264RateControlInfoKHR rate_control_h264;
vk::VideoEncodeH264RateControlLayerInfoKHR rate_control_layer_h264;
video_encoder_vulkan_h264(wivrn_vk_bundle & vk,
video_encoder_vulkan_h264(wivrn::vk_bundle & vk,
const vk::VideoCapabilitiesKHR & video_caps,
const vk::VideoEncodeCapabilitiesKHR & encode_caps,
uint8_t stream_idx,
@@ -63,7 +63,7 @@ protected:
void send_idr_data() override;
public:
static std::unique_ptr<video_encoder_vulkan_h264> create(wivrn_vk_bundle & vk,
static std::unique_ptr<video_encoder_vulkan_h264> create(wivrn::vk_bundle & vk,
const encoder_settings & settings,
uint8_t stream_idx);
+2 -2
View File
@@ -60,7 +60,7 @@ static uint32_t find_msb(uint32_t v)
}
wivrn::video_encoder_vulkan_h265::video_encoder_vulkan_h265(
wivrn_vk_bundle & vk,
wivrn::vk_bundle & vk,
const vk::VideoCapabilitiesKHR & video_caps,
const vk::VideoEncodeCapabilitiesKHR & encode_caps,
uint8_t stream_idx,
@@ -269,7 +269,7 @@ static auto get_video_caps(vk::raii::PhysicalDevice & phys_dev, int bit_depth)
}
std::unique_ptr<wivrn::video_encoder_vulkan_h265> wivrn::video_encoder_vulkan_h265::create(
wivrn_vk_bundle & vk,
wivrn::vk_bundle & vk,
const encoder_settings & settings,
uint8_t stream_idx)
{
+2 -2
View File
@@ -59,7 +59,7 @@ class video_encoder_vulkan_h265 : public video_encoder_vulkan
vk::VideoEncodeH265RateControlInfoKHR rc_h265{};
vk::VideoEncodeH265RateControlLayerInfoKHR rc_layer_h265{};
video_encoder_vulkan_h265(wivrn_vk_bundle & vk,
video_encoder_vulkan_h265(wivrn::vk_bundle & vk,
const vk::VideoCapabilitiesKHR & video_caps,
const vk::VideoEncodeCapabilitiesKHR & encode_caps,
uint8_t stream_idx,
@@ -74,7 +74,7 @@ protected:
void send_idr_data() override;
public:
static std::unique_ptr<video_encoder_vulkan_h265> create(wivrn_vk_bundle & vk,
static std::unique_ptr<video_encoder_vulkan_h265> create(wivrn::vk_bundle & vk,
const encoder_settings & settings,
uint8_t stream_idx);
+1 -1
View File
@@ -87,7 +87,7 @@ void video_encoder_x264::InsertInPendingNal(pending_nal && nal)
}
video_encoder_x264::video_encoder_x264(
wivrn_vk_bundle & vk,
wivrn::vk_bundle & vk,
const encoder_settings & settings,
uint8_t stream_idx) :
video_encoder(stream_idx, settings, std::make_unique<default_idr_handler>(), false)
+1 -1
View File
@@ -60,7 +60,7 @@ class video_encoder_x264 : public video_encoder
std::list<pending_nal> pending_nals;
public:
video_encoder_x264(wivrn_vk_bundle & vk, const encoder_settings & settings, uint8_t stream_idx);
video_encoder_x264(wivrn::vk_bundle & vk, const encoder_settings & settings, uint8_t stream_idx);
std::pair<bool, vk::Semaphore> present_image(vk::Image y_cbcr, bool transferred, vk::raii::CommandBuffer & cmd_buf, uint8_t slot, uint64_t frame_index) override;