/* * WiVRn VR streaming * Copyright (C) 2024 Guillaume Meunier * 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 "vk/vk_allocator.h" #include #include #include #include #include #include namespace wivrn { namespace details { template struct vk_handle { uint64_t operator()(const T & handle) { typename T::CType chandle = handle; return uint64_t(chandle); } }; // vk::raii specialization template struct vk_handle> { uint64_t operator()(const T & handle) { typename T::CType chandle = *handle; return uint64_t(chandle); } }; } // namespace details template uint64_t vk_handle(const T & handle) { return details::vk_handle{}(handle); } struct vk_bundle { static const uint32_t api_version = VK_API_VERSION_1_3; // Lockable that can be redirected to a pthread_mutex_t owned by monado, // so that all VkQueue submitters take the same lock. class queue_mutex { pthread_mutex_t storage = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t * mutex = &storage; public: queue_mutex() = default; queue_mutex(const queue_mutex &) = delete; queue_mutex & operator=(const queue_mutex &) = delete; ~queue_mutex() { pthread_mutex_destroy(&storage); } void lock() { pthread_mutex_lock(mutex); } bool try_lock() { return pthread_mutex_trylock(mutex) == 0; } void unlock() { pthread_mutex_unlock(mutex); } // external must be initialized, not currently locked, and // outlive the last lock() void share(pthread_mutex_t & external) { mutex = &external; } }; struct queue_data { queue_mutex mutex; vk::raii::Queue queue = nullptr; uint32_t family_index = vk::QueueFamilyIgnored; operator bool() const { return *queue != VK_NULL_HANDLE; } }; vk::raii::Context vk_ctx; vk::raii::Instance instance; vk::raii::PhysicalDevice physical_device; vk::raii::Device device; std::optional allocator; queue_data queue; queue_data transfer_queue; beman::inplace_vector::inplace_vector encode_queues; vk::raii::DebugUtilsMessengerEXT debug; vk::StructureChain< vk::PhysicalDeviceFeatures2, #ifdef VK_KHR_video_maintenance1 vk::PhysicalDeviceVideoMaintenance1FeaturesKHR, #endif #ifdef VK_KHR_maintenance9 vk::PhysicalDeviceMaintenance9FeaturesKHR, #endif #ifdef VK_KHR_video_encode_intra_refresh vk::PhysicalDeviceVideoEncodeIntraRefreshFeaturesKHR, #endif #ifdef VK_KHR_unified_image_layouts vk::PhysicalDeviceUnifiedImageLayoutsFeaturesKHR, #endif vk::PhysicalDeviceVulkan12Features, vk::PhysicalDeviceVulkan13Features> feat{}; std::vector instance_extensions; std::vector device_extensions; vk_bundle(); uint32_t get_memory_type(uint32_t type_bits, vk::MemoryPropertyFlags memory_props); template void name(const T & handle, U && value) { return name(T::objectType, vk_handle(handle), std::forward(value)); } bool has_instance_ext(const char *) const; bool has_device_ext(const char *) const; // return true if optimal images do NOT require a transfer operation // between those queues bool optimal_transfer(uint32_t from_queue_family_index, uint32_t to_queue_family_index) const; vk::raii::ShaderModule load_shader(const char * name); private: void name(vk::ObjectType, uint64_t handle, const char * value); void name(vk::ObjectType t, uint64_t handle, const std::string & value) { return name(t, handle, value.c_str()); } }; } // namespace wivrn