VERY experimental Nintendo Switch 2 emulator
C++
vulkan_common: Expose interop and headless devices
ReinUsesLisp5c156184952c
diff --git a/src/video_core/vulkan_common/vulkan_device.cpp b/src/video_core/vulkan_common/vulkan_device.cppindex 42a19d6fa3..4032573754 100644--- a/src/video_core/vulkan_common/vulkan_device.cpp+++ b/src/video_core/vulkan_common/vulkan_device.cpp@@ -34,7 +34,6 @@ constexpr std::array DEPTH16_UNORM_STENCIL8_UINT{} // namespace Alternativesconstexpr std::array REQUIRED_EXTENSIONS{- VK_KHR_SWAPCHAIN_EXTENSION_NAME,VK_KHR_MAINTENANCE1_EXTENSION_NAME,VK_KHR_STORAGE_BUFFER_STORAGE_CLASS_EXTENSION_NAME,VK_KHR_SHADER_DRAW_PARAMETERS_EXTENSION_NAME,@@ -536,16 +535,18 @@ bool Device::IsFormatSupported(VkFormat wanted_format, VkFormatFeatureFlags wantreturn (supported_usage & wanted_usage) == wanted_usage;}-void Device::CheckSuitability() const {+void Device::CheckSuitability(bool requires_swapchain) const {std::bitset<REQUIRED_EXTENSIONS.size()> available_extensions;+ bool has_swapchain = false;for (const VkExtensionProperties& property : physical.EnumerateDeviceExtensionProperties()) {- for (std::size_t i = 0; i < REQUIRED_EXTENSIONS.size(); ++i) {+ const std::string_view name{property.extensionName};+ for (size_t i = 0; i < REQUIRED_EXTENSIONS.size(); ++i) {if (available_extensions[i]) {continue;}- const std::string_view name{property.extensionName};available_extensions[i] = name == REQUIRED_EXTENSIONS[i];}+ has_swapchain = has_swapchain || name == VK_KHR_SWAPCHAIN_EXTENSION_NAME;}for (size_t i = 0; i < REQUIRED_EXTENSIONS.size(); ++i) {if (available_extensions[i]) {@@ -554,6 +555,11 @@ void Device::CheckSuitability() const {LOG_ERROR(Render_Vulkan, "Missing required extension: {}", REQUIRED_EXTENSIONS[i]);throw vk::Exception(VK_ERROR_EXTENSION_NOT_PRESENT);}+ if (requires_swapchain && !has_swapchain) {+ LOG_ERROR(Render_Vulkan, "Missing required extension: VK_KHR_swapchain");+ throw vk::Exception(VK_ERROR_EXTENSION_NOT_PRESENT);+ }+struct LimitTuple {u32 minimum;u32 value;@@ -601,10 +607,13 @@ void Device::CheckSuitability() const {}}-std::vector<const char*> Device::LoadExtensions() {+std::vector<const char*> Device::LoadExtensions(bool requires_surface) {std::vector<const char*> extensions;- extensions.reserve(7 + REQUIRED_EXTENSIONS.size());+ extensions.reserve(8 + REQUIRED_EXTENSIONS.size());extensions.insert(extensions.begin(), REQUIRED_EXTENSIONS.begin(), REQUIRED_EXTENSIONS.end());+ if (requires_surface) {+ extensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);+ }bool has_khr_shader_float16_int8{};bool has_ext_subgroup_size_control{};diff --git a/src/video_core/vulkan_common/vulkan_device.h b/src/video_core/vulkan_common/vulkan_device.hindex f1697b79a3..e0711f7335 100644--- a/src/video_core/vulkan_common/vulkan_device.h+++ b/src/video_core/vulkan_common/vulkan_device.h@@ -227,10 +227,10 @@ public:private:/// Checks if the physical device is suitable.- void CheckSuitability() const;+ void CheckSuitability(bool requires_swapchain) const;/// Loads extensions into a vector and stores available ones in this object.- std::vector<const char*> LoadExtensions();+ std::vector<const char*> LoadExtensions(bool requires_surface);/// Sets up queue families.void SetupFamilies(VkSurfaceKHR surface);diff --git a/src/video_core/vulkan_common/vulkan_memory_allocator.cpp b/src/video_core/vulkan_common/vulkan_memory_allocator.cppindex d6eb3af317..2a8b7a907e 100644--- a/src/video_core/vulkan_common/vulkan_memory_allocator.cpp+++ b/src/video_core/vulkan_common/vulkan_memory_allocator.cpp@@ -7,6 +7,8 @@#include <optional>#include <vector>+#include <glad/glad.h>+#include "common/alignment.h"#include "common/assert.h"#include "common/common_types.h"@@ -55,10 +57,24 @@ struct Range {class MemoryAllocation {public:- explicit MemoryAllocation(const Device& device_, vk::DeviceMemory memory_,- VkMemoryPropertyFlags properties, u64 allocation_size_, u32 type)- : device{device_}, memory{std::move(memory_)}, allocation_size{allocation_size_},- property_flags{properties}, shifted_memory_type{1U << type} {}+ explicit MemoryAllocation(vk::DeviceMemory memory_, VkMemoryPropertyFlags properties,+ u64 allocation_size_, u32 type)+ : memory{std::move(memory_)}, allocation_size{allocation_size_}, property_flags{properties},+ shifted_memory_type{1U << type} {}++#if defined(_WIN32) || defined(__linux__)+ ~MemoryAllocation() {+ if (owning_opengl_handle != 0) {+ glDeleteMemoryObjectsEXT(1, &owning_opengl_handle);+ }+ }+#endif++ MemoryAllocation& operator=(const MemoryAllocation&) = delete;+ MemoryAllocation(const MemoryAllocation&) = delete;++ MemoryAllocation& operator=(MemoryAllocation&&) = delete;+ MemoryAllocation(MemoryAllocation&&) = delete;[[nodiscard]] std::optional<MemoryCommit> Commit(VkDeviceSize size, VkDeviceSize alignment) {const std::optional<u64> alloc = FindFreeRegion(size, alignment);@@ -88,6 +104,31 @@ public:return memory_mapped_span;}+#ifdef _WIN32+ [[nodiscard]] u32 ExportOpenGLHandle() {+ if (!owning_opengl_handle) {+ glCreateMemoryObjectsEXT(1, &owning_opengl_handle);+ glImportMemoryWin32HandleEXT(owning_opengl_handle, allocation_size,+ GL_HANDLE_TYPE_OPAQUE_WIN32_EXT,+ memory.GetMemoryWin32HandleKHR());+ }+ return owning_opengl_handle;+ }+#elif __linux__+ [[nodiscard]] u32 ExportOpenGLHandle() {+ if (!owning_opengl_handle) {+ glCreateMemoryObjectsEXT(1, &owning_opengl_handle);+ glImportMemoryFdEXT(owning_opengl_handle, allocation_size, GL_HANDLE_TYPE_OPAQUE_FD_EXT,+ memory.GetMemoryFdKHR());+ }+ return owning_opengl_handle;+ }+#else+ [[nodiscard]] u32 ExportOpenGLHandle() {+ return 0;+ }+#endif+/// Returns whether this allocation is compatible with the arguments.[[nodiscard]] bool IsCompatible(VkMemoryPropertyFlags flags, u32 type_mask) const {return (flags & property_flags) && (type_mask & shifted_memory_type) != 0;@@ -118,13 +159,15 @@ private:return candidate;}- const Device& device; ///< Vulkan device.const vk::DeviceMemory memory; ///< Vulkan memory allocation handler.const u64 allocation_size; ///< Size of this allocation.const VkMemoryPropertyFlags property_flags; ///< Vulkan memory property flags.const u32 shifted_memory_type; ///< Shifted Vulkan memory type.std::vector<Range> commits; ///< All commit ranges done from this allocation.std::span<u8> memory_mapped_span; ///< Memory mapped span. Empty if not queried before.+#if defined(_WIN32) || defined(__linux__)+ u32 owning_opengl_handle{}; ///< Owning OpenGL memory object handle.+#endif};MemoryCommit::MemoryCommit(MemoryAllocation* allocation_, VkDeviceMemory memory_, u64 begin_,@@ -156,14 +199,19 @@ std::span<u8> MemoryCommit::Map() {return span;}+u32 MemoryCommit::ExportOpenGLHandle() const {+ return allocation->ExportOpenGLHandle();+}+void MemoryCommit::Release() {if (allocation) {allocation->Free(begin);}}-MemoryAllocator::MemoryAllocator(const Device& device_)- : device{device_}, properties{device_.GetPhysical().GetMemoryProperties()} {}+MemoryAllocator::MemoryAllocator(const Device& device_, bool export_allocations_)+ : device{device_}, properties{device_.GetPhysical().GetMemoryProperties()},+ export_allocations{export_allocations_} {}MemoryAllocator::~MemoryAllocator() = default;@@ -196,14 +244,24 @@ MemoryCommit MemoryAllocator::Commit(const vk::Image& image, MemoryUsage usage)void MemoryAllocator::AllocMemory(VkMemoryPropertyFlags flags, u32 type_mask, u64 size) {const u32 type = FindType(flags, type_mask).value();+ const VkExportMemoryAllocateInfo export_allocate_info{+ .sType = VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO,+ .pNext = nullptr,+#ifdef _WIN32+ .handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_BIT,+#elif __linux__+ .handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT,+#else+ .handleTypes = 0,+#endif+ };vk::DeviceMemory memory = device.GetLogical().AllocateMemory({.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,- .pNext = nullptr,+ .pNext = export_allocations ? &export_allocate_info : nullptr,.allocationSize = size,.memoryTypeIndex = type,});- allocations.push_back(- std::make_unique<MemoryAllocation>(device, std::move(memory), flags, size, type));+ allocations.push_back(std::make_unique<MemoryAllocation>(std::move(memory), flags, size, type));}std::optional<MemoryCommit> MemoryAllocator::TryCommit(const VkMemoryRequirements& requirements,diff --git a/src/video_core/vulkan_common/vulkan_memory_allocator.h b/src/video_core/vulkan_common/vulkan_memory_allocator.hindex 9e6cfabf96..86393310ab 100644--- a/src/video_core/vulkan_common/vulkan_memory_allocator.h+++ b/src/video_core/vulkan_common/vulkan_memory_allocator.h@@ -43,6 +43,9 @@ public:/// It will map the backing allocation if it hasn't been mapped before.std::span<u8> Map();+ /// Returns an non-owning OpenGL handle, creating one if it doesn't exist.+ u32 ExportOpenGLHandle() const;+/// Returns the Vulkan memory handler.VkDeviceMemory Memory() const {return memory;@@ -67,7 +70,15 @@ private:/// Allocates and releases memory allocations on demand.class MemoryAllocator {public:- explicit MemoryAllocator(const Device& device_);+ /**+ * Construct memory allocator+ *+ * @param device_ Device to allocate from+ * @param export_allocations_ True when allocations have to be exported+ *+ * @throw vk::Exception on failure+ */+ explicit MemoryAllocator(const Device& device_, bool export_allocations_ = false);~MemoryAllocator();MemoryAllocator& operator=(const MemoryAllocator&) = delete;@@ -106,8 +117,9 @@ private:/// Returns index to the fastest memory type compatible with the passed requirements.std::optional<u32> FindType(VkMemoryPropertyFlags flags, u32 type_mask) const;- const Device& device; ///< Device handle.- const VkPhysicalDeviceMemoryProperties properties; ///< Physical device properties.+ const Device& device; ///< Device handle.+ const VkPhysicalDeviceMemoryProperties properties; ///< Physical device properties.+ const bool export_allocations; ///< True when memory allocations have to be exported.std::vector<std::unique_ptr<MemoryAllocation>> allocations; ///< Current allocations.};