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
+3 -3
View File
@@ -28,11 +28,11 @@ struct method_trait
template <auto Method, typename Result, typename Class, typename... Args>
struct method_trait<Method, Result (Class::*)(Args...)>
{
static Result magic(Class::base * arg, Args... args)
static Result magic(Class::base_t * arg, Args... args)
{
return std::invoke(Method, static_cast<Class *>(arg), args...);
}
static Result magic2(Args... args, Class::base * arg)
static Result magic2(Args... args, Class::base_t * arg)
{
return std::invoke(Method, static_cast<Class *>(arg), args...);
}
@@ -48,7 +48,7 @@ namespace wivrn
// Example:
// class derived: public base_class
// {
// using base = base_class;
// using base_t = base_class;
// void foo(int x);
// };
//
+272 -136
View File
@@ -16,21 +16,21 @@
* 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 "wivrn_vk_bundle.h"
#include "util/u_debug.h"
#include "util/u_logging.h"
#include <string>
#include "wivrn-server_shaders.h"
#include "wivrn_config.h"
#include <format>
#include <ranges>
#include <set>
DEBUG_GET_ONCE_NUM_OPTION(force_gpu_index, "XRT_COMPOSITOR_FORCE_GPU_INDEX", -1)
namespace
{
[[maybe_unused]] vk::raii::Queue make_queue(vk::raii::Device & device, vk_bundle_queue * queue)
{
if (queue == nullptr or queue->queue == VK_NULL_HANDLE)
return nullptr;
return vk::raii::Queue(device, queue->family_index, queue->index);
}
VkBool32 message_callback(
#if VK_HEADER_VERSION >= 304
@@ -63,154 +63,257 @@ VkBool32 message_callback(
U_LOG(level, "%s", pCallbackData->pMessage);
return false;
}
vk::raii::DebugUtilsMessengerEXT register_message_callback(vk::raii::Instance & instance, vk_bundle & vk)
struct strless
{
if (vk.has_EXT_debug_utils)
return vk::raii::DebugUtilsMessengerEXT(
instance, vk::DebugUtilsMessengerCreateInfoEXT{
.messageSeverity = vk::DebugUtilsMessageSeverityFlagBitsEXT::eWarning | vk::DebugUtilsMessageSeverityFlagBitsEXT::eError,
.messageType = vk::DebugUtilsMessageTypeFlagBitsEXT::eGeneral | vk::DebugUtilsMessageTypeFlagBitsEXT::eValidation | vk::DebugUtilsMessageTypeFlagBitsEXT::ePerformance,
.pfnUserCallback = message_callback,
});
return nullptr;
bool operator()(const char * a, const char * b) const
{
return strcmp(a, b) < 0;
}
};
int device_type_priority(vk::PhysicalDeviceType device_type)
{
switch (device_type)
{
case vk::PhysicalDeviceType::eDiscreteGpu:
return 4;
case vk::PhysicalDeviceType::eIntegratedGpu:
return 3;
case vk::PhysicalDeviceType::eVirtualGpu:
return 2;
case vk::PhysicalDeviceType::eCpu:
return 1;
case vk::PhysicalDeviceType::eOther:
return 0;
}
assert(false);
return 0;
}
uint32_t select_queue(const std::vector<vk::QueueFamilyProperties> & queues, vk::QueueFlags flags)
{
uint32_t res = vk::QueueFamilyIgnored;
auto queue_flag_cost = [](vk::QueueFlags flags) {
// number of bits set
return std::popcount(VkQueueFlags(flags));
};
for (auto [i, prop]: std::ranges::enumerate_view(queues))
{
if ((prop.queueFlags & flags) == flags)
{
if (res == vk::QueueFamilyIgnored or
queue_flag_cost(prop.queueFlags) < queue_flag_cost(queues[res].queueFlags))
res = i;
}
}
return res;
}
} // namespace
wivrn::wivrn_vk_bundle::wivrn_vk_bundle(vk_bundle & vk, std::span<const char *> requested_instance_extensions, std::span<const char *> requested_device_extensions) :
vk(vk),
instance(vk_ctx, vk.instance),
physical_device(instance, vk.physical_device),
device(physical_device, vk.device),
allocator({
.physicalDevice = vk.physical_device,
.device = vk.device,
.instance = vk.instance,
.vulkanApiVersion = VK_MAKE_VERSION(1, 3, 0), // FIXME: sync with wivrn_session.cpp
},
vk.has_EXT_debug_utils),
queue(device, vk.main_queue->family_index, vk.main_queue->index),
queue_family_index(vk.main_queue->family_index),
#ifdef VK_KHR_video_encode_queue
encode_queue(make_queue(device, vk.encode_queue)),
encode_queue_family_index(vk.encode_queue ? vk.encode_queue->family_index : vk::QueueFamilyIgnored),
#else
wivrn::vk_bundle::vk_bundle() :
instance(nullptr),
physical_device(nullptr),
device(nullptr),
queue(nullptr),
queue_family_index(vk::QueueFamilyIgnored),
encode_queue(nullptr),
encode_queue_family_index(vk::QueueFamilyIgnored),
#endif
debug(register_message_callback(instance, vk))
debug(nullptr)
{
// This is manually synced with monado code
// Instance extensions
#ifdef VK_EXT_display_surface_counter
if (vk.has_EXT_display_surface_counter)
instance_extensions.push_back(VK_EXT_DISPLAY_SURFACE_COUNTER_EXTENSION_NAME);
#endif
#ifdef VK_EXT_swapchain_colorspace
if (vk.has_EXT_swapchain_colorspace)
instance_extensions.push_back(VK_EXT_SWAPCHAIN_COLORSPACE_EXTENSION_NAME);
#endif
#ifdef VK_EXT_debug_utils
if (vk.has_EXT_debug_utils)
instance_extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
#endif
for (auto & ext: vk_ctx.enumerateInstanceExtensionProperties())
// Create instance
vk::ApplicationInfo app_info{
.pApplicationName = "WiVRn server",
.pEngineName = "WiVRn",
.apiVersion = VK_API_VERSION_1_3,
};
{
for (const char * requested: requested_instance_extensions)
// Required extensions
instance_extensions = {
// promoted to 1.1, but we need the KHR name for Monado
VK_KHR_EXTERNAL_FENCE_CAPABILITIES_EXTENSION_NAME,
VK_KHR_EXTERNAL_MEMORY_CAPABILITIES_EXTENSION_NAME,
VK_KHR_EXTERNAL_SEMAPHORE_CAPABILITIES_EXTENSION_NAME,
VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME,
};
// Optional extensions
std::set<const char *, strless> opt_instance_extensions = {
VK_EXT_DEBUG_UTILS_EXTENSION_NAME,
};
for (auto & ext: vk_ctx.enumerateInstanceExtensionProperties())
{
if (std::string(requested) == ext.extensionName)
{
instance_extensions.push_back(requested);
break;
}
if (auto it = opt_instance_extensions.find(ext.extensionName); it != opt_instance_extensions.end())
instance_extensions.push_back(*it);
}
instance = vk::raii::Instance(
vk_ctx,
vk::InstanceCreateInfo{
.pApplicationInfo = &app_info,
.enabledExtensionCount = uint32_t(instance_extensions.size()),
.ppEnabledExtensionNames = instance_extensions.data(),
});
}
// Device extensions
#ifdef VK_KHR_external_fence_fd
if (vk.has_KHR_external_fence_fd)
device_extensions.push_back(VK_KHR_EXTERNAL_FENCE_FD_EXTENSION_NAME);
#endif
#ifdef VK_KHR_external_semaphore_fd
if (vk.has_KHR_external_semaphore_fd)
device_extensions.push_back(VK_KHR_EXTERNAL_SEMAPHORE_FD_EXTENSION_NAME);
#endif
#ifdef VK_KHR_format_feature_flags2
if (vk.has_KHR_format_feature_flags2)
device_extensions.push_back(VK_KHR_FORMAT_FEATURE_FLAGS_2_EXTENSION_NAME);
#endif
#ifdef VK_KHR_global_priority
if (vk.has_KHR_global_priority)
device_extensions.push_back(VK_KHR_GLOBAL_PRIORITY_EXTENSION_NAME);
#endif
#ifdef VK_KHR_image_format_list
if (vk.has_KHR_image_format_list)
device_extensions.push_back(VK_KHR_IMAGE_FORMAT_LIST_EXTENSION_NAME);
#endif
#ifdef VK_KHR_maintenance1
if (vk.has_KHR_maintenance1)
device_extensions.push_back(VK_KHR_MAINTENANCE_1_EXTENSION_NAME);
#endif
#ifdef VK_KHR_maintenance2
if (vk.has_KHR_maintenance2)
device_extensions.push_back(VK_KHR_MAINTENANCE_2_EXTENSION_NAME);
#endif
#ifdef VK_KHR_maintenance3
if (vk.has_KHR_maintenance3)
device_extensions.push_back(VK_KHR_MAINTENANCE_3_EXTENSION_NAME);
#endif
#ifdef VK_KHR_maintenance4
if (vk.has_KHR_maintenance4)
device_extensions.push_back(VK_KHR_MAINTENANCE_4_EXTENSION_NAME);
#endif
#ifdef VK_KHR_timeline_semaphore
if (vk.has_KHR_timeline_semaphore)
device_extensions.push_back(VK_KHR_TIMELINE_SEMAPHORE_EXTENSION_NAME);
#endif
#ifdef VK_EXT_calibrated_timestamps
if (vk.has_EXT_calibrated_timestamps)
device_extensions.push_back(VK_EXT_CALIBRATED_TIMESTAMPS_EXTENSION_NAME);
#endif
#ifdef VK_EXT_display_control
if (vk.has_EXT_display_control)
device_extensions.push_back(VK_EXT_DISPLAY_CONTROL_EXTENSION_NAME);
if (has_instance_ext(VK_EXT_DEBUG_UTILS_EXTENSION_NAME))
debug = vk::raii::DebugUtilsMessengerEXT(
instance,
vk::DebugUtilsMessengerCreateInfoEXT{
.messageSeverity = vk::DebugUtilsMessageSeverityFlagBitsEXT::eWarning | vk::DebugUtilsMessageSeverityFlagBitsEXT::eError,
.messageType = vk::DebugUtilsMessageTypeFlagBitsEXT::eGeneral | vk::DebugUtilsMessageTypeFlagBitsEXT::eValidation | vk::DebugUtilsMessageTypeFlagBitsEXT::ePerformance,
.pfnUserCallback = message_callback,
});
// Select physical device
{
auto phys_devices = instance.enumeratePhysicalDevices();
if (phys_devices.empty())
throw std::runtime_error("No Vulkan device");
auto index = debug_get_num_option_force_gpu_index();
if (index < 0)
{
std::vector<vk::PhysicalDeviceType> types;
for (const auto & dev: phys_devices)
types.push_back(dev.getProperties().deviceType);
index = 0;
// Select the first device of the highest priority
for (auto [i, t]: std::ranges::enumerate_view(types))
{
if (device_type_priority(t) > device_type_priority(types[index]))
index = i;
}
}
else if (index > phys_devices.size())
throw std::runtime_error(std::format("Invalid GPU index {}, must be in range 0..{}", index, phys_devices.size() - 1));
physical_device = std::move(phys_devices[index]);
}
// Select queue families
{
auto queues = physical_device.getQueueFamilyProperties();
queue_family_index = select_queue(queues, vk::QueueFlagBits::eCompute);
#if WIVRN_USE_VULKAN_ENCODE
encode_queue_family_index = select_queue(queues, vk::QueueFlagBits::eVideoEncodeKHR);
#endif
// Technically allowed to have a device with only encode or decode capabilities
if (queue_family_index == vk::QueueFamilyIgnored)
throw std::runtime_error("GPU does not support vulkan compute");
}
// Create logical device
{
// Required extensions
device_extensions = {
VK_KHR_DEDICATED_ALLOCATION_EXTENSION_NAME,
VK_KHR_EXTERNAL_FENCE_EXTENSION_NAME,
VK_KHR_EXTERNAL_MEMORY_EXTENSION_NAME,
VK_KHR_EXTERNAL_SEMAPHORE_EXTENSION_NAME,
VK_KHR_GET_MEMORY_REQUIREMENTS_2_EXTENSION_NAME,
VK_KHR_EXTERNAL_MEMORY_FD_EXTENSION_NAME,
};
// Optional extensions
std::set<const char *, strless> opt_device_extensions = {
// For Monado
VK_KHR_EXTERNAL_FENCE_FD_EXTENSION_NAME,
VK_KHR_EXTERNAL_SEMAPHORE_FD_EXTENSION_NAME,
VK_KHR_EXTERNAL_MEMORY_FD_EXTENSION_NAME,
VK_KHR_IMAGE_FORMAT_LIST_EXTENSION_NAME,
VK_KHR_MAINTENANCE_1_EXTENSION_NAME,
VK_KHR_MAINTENANCE_2_EXTENSION_NAME,
VK_KHR_TIMELINE_SEMAPHORE_EXTENSION_NAME,
VK_EXT_ROBUSTNESS_2_EXTENSION_NAME,
VK_KHR_SYNCHRONIZATION_2_EXTENSION_NAME,
// For FFMPEG
#ifdef VK_EXT_external_memory_dma_buf
if (vk.has_EXT_external_memory_dma_buf)
device_extensions.push_back(VK_EXT_EXTERNAL_MEMORY_DMA_BUF_EXTENSION_NAME);
#endif
#ifdef VK_EXT_global_priority
if (vk.has_EXT_global_priority)
device_extensions.push_back(VK_EXT_GLOBAL_PRIORITY_EXTENSION_NAME);
VK_EXT_EXTERNAL_MEMORY_DMA_BUF_EXTENSION_NAME,
#endif
#ifdef VK_EXT_image_drm_format_modifier
if (vk.has_EXT_image_drm_format_modifier)
device_extensions.push_back(VK_EXT_IMAGE_DRM_FORMAT_MODIFIER_EXTENSION_NAME);
#endif
#ifdef VK_EXT_robustness2
if (vk.has_EXT_robustness2)
device_extensions.push_back(VK_EXT_ROBUSTNESS_2_EXTENSION_NAME);
#endif
#ifdef VK_GOOGLE_display_timing
if (vk.has_GOOGLE_display_timing)
device_extensions.push_back(VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME);
VK_EXT_IMAGE_DRM_FORMAT_MODIFIER_EXTENSION_NAME,
#endif
for (auto & ext: physical_device.enumerateDeviceExtensionProperties())
{
for (const char * requested: requested_device_extensions)
// For vulkan video encode
#ifdef VK_KHR_video_queue
VK_KHR_VIDEO_QUEUE_EXTENSION_NAME,
#endif
#ifdef VK_KHR_video_encode_queue
VK_KHR_VIDEO_ENCODE_QUEUE_EXTENSION_NAME,
#endif
#ifdef VK_KHR_video_maintenance1
VK_KHR_VIDEO_MAINTENANCE_1_EXTENSION_NAME,
#endif
#ifdef VK_KHR_video_encode_h264
VK_KHR_VIDEO_ENCODE_H264_EXTENSION_NAME,
#endif
#ifdef VK_KHR_video_encode_h265
VK_KHR_VIDEO_ENCODE_H265_EXTENSION_NAME,
#endif
};
for (auto & ext: physical_device.enumerateDeviceExtensionProperties())
{
if (std::string(requested) == ext.extensionName)
{
device_extensions.push_back(requested);
break;
}
if (auto it = opt_device_extensions.find(ext.extensionName); it != opt_device_extensions.end())
device_extensions.push_back(*it);
}
float prio = 1.0;
std::vector queues_info = {
vk::DeviceQueueCreateInfo{
.queueFamilyIndex = queue_family_index,
.queueCount = 1,
.pQueuePriorities = &prio,
},
};
#if WIVRN_USE_VULKAN_ENCODE
if (encode_queue_family_index != vk::QueueFamilyIgnored)
{
queues_info.push_back(
vk::DeviceQueueCreateInfo{
.queueFamilyIndex = encode_queue_family_index,
.queueCount = 1,
.pQueuePriorities = &prio,
});
#ifdef VK_KHR_video_maintenance1
auto [_, video_feat] = physical_device.getFeatures2<vk::PhysicalDeviceFeatures2, vk::PhysicalDeviceVideoMaintenance1FeaturesKHR>();
std::get<vk::PhysicalDeviceVideoMaintenance1FeaturesKHR>(feat).videoMaintenance1 = video_feat.videoMaintenance1;
#endif
}
#endif
// Enable features
auto [phys_feat, phys_feat12, phys_feat13] = physical_device.getFeatures2<vk::PhysicalDeviceFeatures2, vk::PhysicalDeviceVulkan12Features, vk::PhysicalDeviceVulkan13Features>();
std::get<vk::PhysicalDeviceVulkan12Features>(feat).descriptorBindingPartiallyBound = phys_feat12.descriptorBindingPartiallyBound;
std::get<vk::PhysicalDeviceVulkan12Features>(feat).timelineSemaphore = phys_feat12.timelineSemaphore;
std::get<vk::PhysicalDeviceVulkan13Features>(feat).synchronization2 = phys_feat13.synchronization2;
device = vk::raii::Device(
physical_device,
vk::DeviceCreateInfo{
.pNext = &feat.get(),
.queueCreateInfoCount = uint32_t(queues_info.size()),
.pQueueCreateInfos = queues_info.data(),
.enabledExtensionCount = uint32_t(device_extensions.size()),
.ppEnabledExtensionNames = device_extensions.data(),
});
queue = device.getQueue(queue_family_index, 0);
#if WIVRN_USE_VULKAN_ENCODE
if (encode_queue_family_index != vk::QueueFamilyIgnored)
encode_queue = device.getQueue(encode_queue_family_index, 0);
#endif
}
allocator.emplace(VmaAllocatorCreateInfo{
.physicalDevice = *physical_device,
.device = *device,
.instance = *instance,
.vulkanApiVersion = app_info.apiVersion,
},
*debug != VK_NULL_HANDLE);
}
uint32_t wivrn::wivrn_vk_bundle::get_memory_type(uint32_t type_bits, vk::MemoryPropertyFlags memory_props)
uint32_t wivrn::vk_bundle::get_memory_type(uint32_t type_bits, vk::MemoryPropertyFlags memory_props)
{
auto mem_prop = physical_device.getMemoryProperties();
@@ -226,9 +329,42 @@ uint32_t wivrn::wivrn_vk_bundle::get_memory_type(uint32_t type_bits, vk::MemoryP
throw std::runtime_error("Failed to get memory type");
}
void wivrn::wivrn_vk_bundle::name(vk::ObjectType type, uint64_t handle, const char * value)
bool wivrn::vk_bundle::has_instance_ext(const char * ext) const
{
if (not vk.has_EXT_debug_utils)
for (auto e: instance_extensions)
{
if (strcmp(e, ext) == 0)
return true;
}
return false;
}
bool wivrn::vk_bundle::has_device_ext(const char * ext) const
{
for (auto e: device_extensions)
{
if (strcmp(e, ext) == 0)
return true;
}
return false;
}
vk::raii::ShaderModule wivrn::vk_bundle::load_shader(const char * name)
{
std::span spirv{::shaders.at(name)};
return vk::raii::ShaderModule{
device,
vk::ShaderModuleCreateInfo{
.codeSize = spirv.size_bytes(),
.pCode = spirv.data(),
},
};
}
void wivrn::vk_bundle::name(vk::ObjectType type, uint64_t handle, const char * value)
{
if (not has_instance_ext(VK_EXT_DEBUG_UTILS_EXTENSION_NAME))
return;
device.setDebugUtilsObjectNameEXT(vk::DebugUtilsObjectNameInfoEXT{
.objectType = type,
+24 -32
View File
@@ -21,39 +21,14 @@
#include "vk/vk_allocator.h"
#include <cstdint>
#include <span>
#include <mutex>
#include <type_traits>
#include <vector>
#include <vulkan/vulkan_raii.hpp>
struct vk_bundle;
namespace wivrn
{
// to use vk::raii we need a vk::raii::{Instance,PhysicalDevice,Device}
// however we don't own it, so deactivate the destructor
struct raii_instance : public vk::raii::Instance
{
raii_instance(vk::raii::Context & ctx, VkInstance i) :
vk::raii::Instance(ctx, i) {}
raii_instance & operator=(raii_instance &&) noexcept = default;
~raii_instance()
{
release();
}
};
struct raii_device : public vk::raii::Device
{
raii_device(vk::raii::PhysicalDevice & p, VkDevice d) :
vk::raii::Device(p, d) {}
raii_device & operator=(raii_device &&) noexcept = default;
~raii_device()
{
release();
}
};
namespace details
{
template <typename T, typename U = void>
@@ -84,26 +59,38 @@ uint64_t vk_handle(const T & handle)
return details::vk_handle<T>{}(handle);
}
struct wivrn_vk_bundle
struct vk_bundle
{
vk_bundle & vk;
vk::raii::Context vk_ctx;
raii_instance instance;
vk::raii::Instance instance;
vk::raii::PhysicalDevice physical_device;
raii_device device;
vk_allocator allocator;
vk::raii::Device device;
std::optional<vk_allocator> allocator;
std::mutex queue_mutex;
vk::raii::Queue queue;
uint32_t queue_family_index;
std::mutex encode_queue_mutex;
vk::raii::Queue encode_queue;
uint32_t encode_queue_family_index;
vk::raii::DebugUtilsMessengerEXT debug;
vk::StructureChain<
vk::PhysicalDeviceFeatures2,
vk::PhysicalDeviceVulkan12Features,
vk::PhysicalDeviceVulkan13Features,
#ifdef VK_KHR_video_maintenance1
vk::PhysicalDeviceVideoMaintenance1FeaturesKHR
#endif
>
feat{};
std::vector<const char *> instance_extensions;
std::vector<const char *> device_extensions;
wivrn_vk_bundle(vk_bundle & vk, std::span<const char *> requested_instance_extensions, std::span<const char *> requested_device_extensions);
vk_bundle();
uint32_t get_memory_type(uint32_t type_bits, vk::MemoryPropertyFlags memory_props);
@@ -113,6 +100,11 @@ struct wivrn_vk_bundle
return name(T::objectType, vk_handle(handle), value);
}
bool has_instance_ext(const char *) const;
bool has_device_ext(const char *) const;
vk::raii::ShaderModule load_shader(const char * name);
private:
void name(vk::ObjectType, uint64_t handle, const char * value);
};