Files
WiVRn/server/utils/wivrn_vk_bundle.h
schlegp 04837ba215 Add Vulkan AV1 video encoder (#1087)
Experimental support, is never automatically selected.
2026-11-16 17:24:08 +01:00

174 lines
4.4 KiB
C++

/*
* WiVRn VR streaming
* Copyright (C) 2024 Guillaume Meunier <[email protected]>
* 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 "vk/vk_allocator.h"
#include <cstdint>
#include <inplace_vector.hpp>
#include <pthread.h>
#include <type_traits>
#include <vector>
#include <vulkan/vulkan_raii.hpp>
namespace wivrn
{
namespace details
{
template <typename T, typename U = void>
struct vk_handle
{
uint64_t operator()(const T & handle)
{
typename T::CType chandle = handle;
return uint64_t(chandle);
}
};
// vk::raii specialization
template <typename T>
struct vk_handle<T, std::void_t<typename T::CppType>>
{
uint64_t operator()(const T & handle)
{
typename T::CType chandle = *handle;
return uint64_t(chandle);
}
};
} // namespace details
template <typename T>
uint64_t vk_handle(const T & handle)
{
return details::vk_handle<T>{}(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<vk_allocator> allocator;
queue_data queue;
queue_data transfer_queue;
beman::inplace_vector::inplace_vector<queue_data, 3> 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_video_encode_av1
vk::PhysicalDeviceVideoEncodeAV1FeaturesKHR,
#endif
#ifdef VK_KHR_unified_image_layouts
vk::PhysicalDeviceUnifiedImageLayoutsFeaturesKHR,
#endif
vk::PhysicalDeviceVulkan12Features,
vk::PhysicalDeviceVulkan13Features>
feat{};
std::vector<const char *> instance_extensions;
std::vector<const char *> device_extensions;
vk_bundle();
uint32_t get_memory_type(uint32_t type_bits, vk::MemoryPropertyFlags memory_props);
template <typename T, typename U>
void name(const T & handle, U && value)
{
return name(T::objectType, vk_handle(handle), std::forward<U>(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