From 9678bf1e2f220165564704cb910cc22be6d83905 Mon Sep 17 00:00:00 2001 From: Stenzek Date: Thu, 13 Jul 2023 21:37:46 +1000 Subject: [PATCH] GS/Vulkan: Combine GSDeviceVK and VKContext Less indirection. --- pcsx2/CMakeLists.txt | 4 - pcsx2/GS/Renderers/Vulkan/GSDeviceVK.cpp | 2539 ++++++++++++++++-- pcsx2/GS/Renderers/Vulkan/GSDeviceVK.h | 315 ++- pcsx2/GS/Renderers/Vulkan/GSTextureVK.cpp | 117 +- pcsx2/GS/Renderers/Vulkan/GSTextureVK.h | 3 +- pcsx2/GS/Renderers/Vulkan/VKBuilders.cpp | 107 +- pcsx2/GS/Renderers/Vulkan/VKBuilders.h | 84 + pcsx2/GS/Renderers/Vulkan/VKContext.cpp | 2165 --------------- pcsx2/GS/Renderers/Vulkan/VKContext.h | 405 --- pcsx2/GS/Renderers/Vulkan/VKLoader.cpp | 83 +- pcsx2/GS/Renderers/Vulkan/VKLoader.h | 1 + pcsx2/GS/Renderers/Vulkan/VKShaderCache.cpp | 35 +- pcsx2/GS/Renderers/Vulkan/VKShaderCache.h | 2 +- pcsx2/GS/Renderers/Vulkan/VKStreamBuffer.cpp | 20 +- pcsx2/GS/Renderers/Vulkan/VKSwapChain.cpp | 54 +- pcsx2/GS/Renderers/Vulkan/VKUtil.cpp | 239 -- pcsx2/GS/Renderers/Vulkan/VKUtil.h | 131 - pcsx2/pcsx2.vcxproj | 4 - pcsx2/pcsx2.vcxproj.filters | 12 - 19 files changed, 2919 insertions(+), 3401 deletions(-) delete mode 100644 pcsx2/GS/Renderers/Vulkan/VKContext.cpp delete mode 100644 pcsx2/GS/Renderers/Vulkan/VKContext.h delete mode 100644 pcsx2/GS/Renderers/Vulkan/VKUtil.cpp delete mode 100644 pcsx2/GS/Renderers/Vulkan/VKUtil.h diff --git a/pcsx2/CMakeLists.txt b/pcsx2/CMakeLists.txt index 8f58291fcf..8f71179584 100644 --- a/pcsx2/CMakeLists.txt +++ b/pcsx2/CMakeLists.txt @@ -654,25 +654,21 @@ if(USE_VULKAN) GS/Renderers/Vulkan/GSTextureVK.cpp GS/Renderers/Vulkan/vk_mem_alloc.cpp GS/Renderers/Vulkan/VKBuilders.cpp - GS/Renderers/Vulkan/VKContext.cpp GS/Renderers/Vulkan/VKLoader.cpp GS/Renderers/Vulkan/VKShaderCache.cpp GS/Renderers/Vulkan/VKStreamBuffer.cpp GS/Renderers/Vulkan/VKSwapChain.cpp - GS/Renderers/Vulkan/VKUtil.cpp ) list(APPEND pcsx2GSHeaders GS/Renderers/Vulkan/GSDeviceVK.h GS/Renderers/Vulkan/GSTextureVK.h GS/Renderers/Vulkan/VKBuilders.h - GS/Renderers/Vulkan/VKContext.h GS/Renderers/Vulkan/VKEntryPoints.h GS/Renderers/Vulkan/VKEntryPoints.inl GS/Renderers/Vulkan/VKLoader.h GS/Renderers/Vulkan/VKShaderCache.h GS/Renderers/Vulkan/VKStreamBuffer.h GS/Renderers/Vulkan/VKSwapChain.h - GS/Renderers/Vulkan/VKUtil.h ) target_link_libraries(PCSX2_FLAGS INTERFACE Vulkan-Headers glslang) endif() diff --git a/pcsx2/GS/Renderers/Vulkan/GSDeviceVK.cpp b/pcsx2/GS/Renderers/Vulkan/GSDeviceVK.cpp index 4df33e2328..49a1e30add 100644 --- a/pcsx2/GS/Renderers/Vulkan/GSDeviceVK.cpp +++ b/pcsx2/GS/Renderers/Vulkan/GSDeviceVK.cpp @@ -19,7 +19,6 @@ #include "GS/Renderers/Vulkan/VKBuilders.h" #include "GS/Renderers/Vulkan/VKShaderCache.h" #include "GS/Renderers/Vulkan/VKSwapChain.h" -#include "GS/Renderers/Vulkan/VKUtil.h" #include "GS/GS.h" #include "GS/GSGL.h" #include "GS/GSPerfMon.h" @@ -33,9 +32,29 @@ #include "imgui.h" +#include #include #include +// Tweakables +enum : u32 +{ + MAX_DRAW_CALLS_PER_FRAME = 8192, + MAX_COMBINED_IMAGE_SAMPLER_DESCRIPTORS_PER_FRAME = 2 * MAX_DRAW_CALLS_PER_FRAME, + MAX_SAMPLED_IMAGE_DESCRIPTORS_PER_FRAME = + MAX_DRAW_CALLS_PER_FRAME, // assume at least half our draws aren't going to be shuffle/blending + MAX_STORAGE_IMAGE_DESCRIPTORS_PER_FRAME = 4, // Currently used by CAS only + MAX_INPUT_ATTACHMENT_IMAGE_DESCRIPTORS_PER_FRAME = MAX_DRAW_CALLS_PER_FRAME, + MAX_DESCRIPTOR_SETS_PER_FRAME = MAX_DRAW_CALLS_PER_FRAME * 2, + + VERTEX_BUFFER_SIZE = 32 * 1024 * 1024, + INDEX_BUFFER_SIZE = 16 * 1024 * 1024, + VERTEX_UNIFORM_BUFFER_SIZE = 8 * 1024 * 1024, + FRAGMENT_UNIFORM_BUFFER_SIZE = 8 * 1024 * 1024, + TEXTURE_BUFFER_SIZE = 64 * 1024 * 1024, +}; + + #ifdef ENABLE_OGL_DEBUG static u32 s_debug_scope_depth = 0; #endif @@ -61,6 +80,9 @@ static VkAttachmentLoadOp GetLoadOpForTexture(GSTextureVK* tex) static constexpr VkClearValue s_present_clear_color = {{{0.0f, 0.0f, 0.0f, 1.0f}}}; +// We need to synchronize instance creation because of adapter enumeration from the UI thread. +static std::mutex s_instance_mutex; + GSDeviceVK::GSDeviceVK() { #ifdef ENABLE_OGL_DEBUG @@ -70,14 +92,1895 @@ GSDeviceVK::GSDeviceVK() std::memset(&m_pipeline_selector, 0, sizeof(m_pipeline_selector)); } -GSDeviceVK::~GSDeviceVK() +GSDeviceVK::~GSDeviceVK() = default; + +VkInstance GSDeviceVK::CreateVulkanInstance(const WindowInfo& wi, bool enable_debug_utils, bool enable_validation_layer) { - pxAssert(!g_vulkan_context); + ExtensionList enabled_extensions; + if (!SelectInstanceExtensions(&enabled_extensions, wi, enable_debug_utils)) + return VK_NULL_HANDLE; + + // Remember to manually update this every release. We don't pull in svnrev.h here, because + // it's only the major/minor version, and rebuilding the file every time something else changes + // is unnecessary. + VkApplicationInfo app_info = {}; + app_info.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO; + app_info.pNext = nullptr; + app_info.pApplicationName = "PCSX2"; + app_info.applicationVersion = VK_MAKE_VERSION(1, 7, 0); + app_info.pEngineName = "PCSX2"; + app_info.engineVersion = VK_MAKE_VERSION(1, 7, 0); + app_info.apiVersion = VK_API_VERSION_1_1; + + VkInstanceCreateInfo instance_create_info = {}; + instance_create_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO; + instance_create_info.pNext = nullptr; + instance_create_info.flags = 0; + instance_create_info.pApplicationInfo = &app_info; + instance_create_info.enabledExtensionCount = static_cast(enabled_extensions.size()); + instance_create_info.ppEnabledExtensionNames = enabled_extensions.data(); + instance_create_info.enabledLayerCount = 0; + instance_create_info.ppEnabledLayerNames = nullptr; + + // Enable debug layer on debug builds + if (enable_validation_layer) + { + static const char* layer_names[] = {"VK_LAYER_KHRONOS_validation"}; + instance_create_info.enabledLayerCount = 1; + instance_create_info.ppEnabledLayerNames = layer_names; + } + + VkInstance instance; + VkResult res = vkCreateInstance(&instance_create_info, nullptr, &instance); + if (res != VK_SUCCESS) + { + LOG_VULKAN_ERROR(res, "vkCreateInstance failed: "); + return nullptr; + } + + return instance; } -static void GPUListToAdapterNames(std::vector* dest, VkInstance instance) +bool GSDeviceVK::SelectInstanceExtensions(ExtensionList* extension_list, const WindowInfo& wi, bool enable_debug_utils) { - VKContext::GPUList gpus = VKContext::EnumerateGPUs(instance); + u32 extension_count = 0; + VkResult res = vkEnumerateInstanceExtensionProperties(nullptr, &extension_count, nullptr); + if (res != VK_SUCCESS) + { + LOG_VULKAN_ERROR(res, "vkEnumerateInstanceExtensionProperties failed: "); + return false; + } + + if (extension_count == 0) + { + Console.Error("Vulkan: No extensions supported by instance."); + return false; + } + + std::vector available_extension_list(extension_count); + res = vkEnumerateInstanceExtensionProperties(nullptr, &extension_count, available_extension_list.data()); + pxAssert(res == VK_SUCCESS); + + auto SupportsExtension = [&](const char* name, bool required) { + if (std::find_if(available_extension_list.begin(), available_extension_list.end(), + [&](const VkExtensionProperties& properties) { return !strcmp(name, properties.extensionName); }) != + available_extension_list.end()) + { + DevCon.WriteLn("Enabling extension: %s", name); + extension_list->push_back(name); + return true; + } + + if (required) + Console.Error("Vulkan: Missing required extension %s.", name); + + return false; + }; + + // Common extensions + if (wi.type != WindowInfo::Type::Surfaceless && !SupportsExtension(VK_KHR_SURFACE_EXTENSION_NAME, true)) + return false; + +#if defined(VK_USE_PLATFORM_WIN32_KHR) + if (wi.type == WindowInfo::Type::Win32 && !SupportsExtension(VK_KHR_WIN32_SURFACE_EXTENSION_NAME, true)) + return false; +#endif +#if defined(VK_USE_PLATFORM_XLIB_KHR) + if (wi.type == WindowInfo::Type::X11 && !SupportsExtension(VK_KHR_XLIB_SURFACE_EXTENSION_NAME, true)) + return false; +#endif +#if defined(VK_USE_PLATFORM_WAYLAND_KHR) + if (wi.type == WindowInfo::Type::Wayland && !SupportsExtension(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME, true)) + return false; +#endif +#if defined(VK_USE_PLATFORM_METAL_EXT) + if (wi.type == WindowInfo::Type::MacOS && !SupportsExtension(VK_EXT_METAL_SURFACE_EXTENSION_NAME, true)) + return false; +#endif + + // VK_EXT_debug_utils + if (enable_debug_utils && !SupportsExtension(VK_EXT_DEBUG_UTILS_EXTENSION_NAME, false)) + Console.Warning("Vulkan: Debug report requested, but extension is not available."); + + // Needed for exclusive fullscreen control. + SupportsExtension(VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME, false); + + return true; +} + +GSDeviceVK::GPUList GSDeviceVK::EnumerateGPUs(VkInstance instance) +{ + GPUList gpus; + + u32 gpu_count = 0; + VkResult res = vkEnumeratePhysicalDevices(instance, &gpu_count, nullptr); + if ((res != VK_SUCCESS && res != VK_INCOMPLETE) || gpu_count == 0) + { + LOG_VULKAN_ERROR(res, "vkEnumeratePhysicalDevices (1) failed: "); + return gpus; + } + + std::vector physical_devices(gpu_count); + res = vkEnumeratePhysicalDevices(instance, &gpu_count, physical_devices.data()); + if (res == VK_INCOMPLETE) + { + Console.Warning("First vkEnumeratePhysicalDevices() call returned %zu devices, but second returned %u", + physical_devices.size(), gpu_count); + } + else if (res != VK_SUCCESS) + { + LOG_VULKAN_ERROR(res, "vkEnumeratePhysicalDevices (2) failed: "); + return gpus; + } + + // Maybe we lost a GPU? + if (gpu_count < physical_devices.size()) + physical_devices.resize(gpu_count); + + gpus.reserve(physical_devices.size()); + for (VkPhysicalDevice device : physical_devices) + { + VkPhysicalDeviceProperties props = {}; + vkGetPhysicalDeviceProperties(device, &props); + + std::string gpu_name = props.deviceName; + + // handle duplicate adapter names + if (std::any_of( + gpus.begin(), gpus.end(), [&gpu_name](const auto& other) { return (gpu_name == other.second); })) + { + std::string original_adapter_name = std::move(gpu_name); + + u32 current_extra = 2; + do + { + gpu_name = fmt::format("{} ({})", original_adapter_name, current_extra); + current_extra++; + } while (std::any_of( + gpus.begin(), gpus.end(), [&gpu_name](const auto& other) { return (gpu_name == other.second); })); + } + + gpus.emplace_back(device, std::move(gpu_name)); + } + + return gpus; +} + +bool GSDeviceVK::SelectDeviceExtensions(ExtensionList* extension_list, bool enable_surface) +{ + u32 extension_count = 0; + VkResult res = vkEnumerateDeviceExtensionProperties(m_physical_device, nullptr, &extension_count, nullptr); + if (res != VK_SUCCESS) + { + LOG_VULKAN_ERROR(res, "vkEnumerateDeviceExtensionProperties failed: "); + return false; + } + + if (extension_count == 0) + { + Console.Error("Vulkan: No extensions supported by device."); + return false; + } + + std::vector available_extension_list(extension_count); + res = vkEnumerateDeviceExtensionProperties( + m_physical_device, nullptr, &extension_count, available_extension_list.data()); + pxAssert(res == VK_SUCCESS); + + auto SupportsExtension = [&](const char* name, bool required) { + if (std::find_if(available_extension_list.begin(), available_extension_list.end(), + [&](const VkExtensionProperties& properties) { return !strcmp(name, properties.extensionName); }) != + available_extension_list.end()) + { + if (std::none_of(extension_list->begin(), extension_list->end(), + [&](const char* existing_name) { return (std::strcmp(existing_name, name) == 0); })) + { + DevCon.WriteLn("Enabling extension: %s", name); + extension_list->push_back(name); + } + + return true; + } + + if (required) + Console.Error("Vulkan: Missing required extension %s.", name); + + return false; + }; + + if (enable_surface && !SupportsExtension(VK_KHR_SWAPCHAIN_EXTENSION_NAME, true)) + return false; + + m_optional_extensions.vk_ext_provoking_vertex = SupportsExtension(VK_EXT_PROVOKING_VERTEX_EXTENSION_NAME, false); + m_optional_extensions.vk_ext_memory_budget = SupportsExtension(VK_EXT_MEMORY_BUDGET_EXTENSION_NAME, false); + m_optional_extensions.vk_ext_calibrated_timestamps = + SupportsExtension(VK_EXT_CALIBRATED_TIMESTAMPS_EXTENSION_NAME, false); + m_optional_extensions.vk_ext_line_rasterization = + SupportsExtension(VK_EXT_LINE_RASTERIZATION_EXTENSION_NAME, false); + m_optional_extensions.vk_ext_rasterization_order_attachment_access = + SupportsExtension(VK_EXT_RASTERIZATION_ORDER_ATTACHMENT_ACCESS_EXTENSION_NAME, false) || + SupportsExtension(VK_ARM_RASTERIZATION_ORDER_ATTACHMENT_ACCESS_EXTENSION_NAME, false); + m_optional_extensions.vk_khr_driver_properties = SupportsExtension(VK_KHR_DRIVER_PROPERTIES_EXTENSION_NAME, false); + m_optional_extensions.vk_khr_fragment_shader_barycentric = + SupportsExtension(VK_KHR_FRAGMENT_SHADER_BARYCENTRIC_EXTENSION_NAME, false); + m_optional_extensions.vk_khr_shader_draw_parameters = + SupportsExtension(VK_KHR_SHADER_DRAW_PARAMETERS_EXTENSION_NAME, false); + m_optional_extensions.vk_ext_attachment_feedback_loop_layout = + SupportsExtension(VK_EXT_ATTACHMENT_FEEDBACK_LOOP_LAYOUT_EXTENSION_NAME, false); + +#ifdef _WIN32 + m_optional_extensions.vk_ext_full_screen_exclusive = + enable_surface && SupportsExtension(VK_EXT_FULL_SCREEN_EXCLUSIVE_EXTENSION_NAME, false); +#endif + + return true; +} + +bool GSDeviceVK::SelectDeviceFeatures() +{ + VkPhysicalDeviceFeatures available_features; + vkGetPhysicalDeviceFeatures(m_physical_device, &available_features); + + // Enable the features we use. + m_device_features.dualSrcBlend = available_features.dualSrcBlend; + m_device_features.largePoints = available_features.largePoints; + m_device_features.wideLines = available_features.wideLines; + m_device_features.fragmentStoresAndAtomics = available_features.fragmentStoresAndAtomics; + m_device_features.textureCompressionBC = available_features.textureCompressionBC; + m_device_features.samplerAnisotropy = available_features.samplerAnisotropy; + m_device_features.geometryShader = available_features.geometryShader; + + return true; +} + +bool GSDeviceVK::CreateDevice(VkSurfaceKHR surface, bool enable_validation_layer) +{ + u32 queue_family_count; + vkGetPhysicalDeviceQueueFamilyProperties(m_physical_device, &queue_family_count, nullptr); + if (queue_family_count == 0) + { + Console.Error("No queue families found on specified vulkan physical device."); + return false; + } + + std::vector queue_family_properties(queue_family_count); + vkGetPhysicalDeviceQueueFamilyProperties(m_physical_device, &queue_family_count, queue_family_properties.data()); + DevCon.WriteLn("%u vulkan queue families", queue_family_count); + + // Find graphics and present queues. + m_graphics_queue_family_index = queue_family_count; + m_present_queue_family_index = queue_family_count; + m_spin_queue_family_index = queue_family_count; + u32 spin_queue_index = 0; + for (uint32_t i = 0; i < queue_family_count; i++) + { + VkBool32 graphics_supported = queue_family_properties[i].queueFlags & VK_QUEUE_GRAPHICS_BIT; + if (graphics_supported) + { + m_graphics_queue_family_index = i; + // Quit now, no need for a present queue. + if (!surface) + { + break; + } + } + + if (surface) + { + VkBool32 present_supported; + VkResult res = vkGetPhysicalDeviceSurfaceSupportKHR(m_physical_device, i, surface, &present_supported); + if (res != VK_SUCCESS) + { + LOG_VULKAN_ERROR(res, "vkGetPhysicalDeviceSurfaceSupportKHR failed: "); + return false; + } + + if (present_supported) + { + m_present_queue_family_index = i; + } + + // Prefer one queue family index that does both graphics and present. + if (graphics_supported && present_supported) + { + break; + } + } + } + for (uint32_t i = 0; i < queue_family_count; i++) + { + // Pick a queue for spinning + if (!(queue_family_properties[i].queueFlags & VK_QUEUE_COMPUTE_BIT)) + continue; // We need compute + if (queue_family_properties[i].timestampValidBits == 0) + continue; // We need timing + const bool queue_is_used = i == m_graphics_queue_family_index || i == m_present_queue_family_index; + if (queue_is_used && m_spin_queue_family_index != queue_family_count) + continue; // Found a non-graphics queue to use + spin_queue_index = 0; + m_spin_queue_family_index = i; + if (queue_is_used && queue_family_properties[i].queueCount > 1) + spin_queue_index = 1; + if (!(queue_family_properties[i].queueFlags & VK_QUEUE_GRAPHICS_BIT)) + break; // Async compute queue, definitely pick this one + } + if (m_graphics_queue_family_index == queue_family_count) + { + Console.Error("Vulkan: Failed to find an acceptable graphics queue."); + return false; + } + if (surface != VK_NULL_HANDLE && m_present_queue_family_index == queue_family_count) + { + Console.Error("Vulkan: Failed to find an acceptable present queue."); + return false; + } + + VkDeviceCreateInfo device_info = {}; + device_info.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO; + device_info.pNext = nullptr; + device_info.flags = 0; + device_info.queueCreateInfoCount = 0; + + static constexpr float queue_priorities[] = {1.0f, 0.0f}; // Low priority for the spin queue + std::array queue_infos; + VkDeviceQueueCreateInfo& graphics_queue_info = queue_infos[device_info.queueCreateInfoCount++]; + graphics_queue_info.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO; + graphics_queue_info.pNext = nullptr; + graphics_queue_info.flags = 0; + graphics_queue_info.queueFamilyIndex = m_graphics_queue_family_index; + graphics_queue_info.queueCount = 1; + graphics_queue_info.pQueuePriorities = queue_priorities; + + if (surface != VK_NULL_HANDLE && m_graphics_queue_family_index != m_present_queue_family_index) + { + VkDeviceQueueCreateInfo& present_queue_info = queue_infos[device_info.queueCreateInfoCount++]; + present_queue_info.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO; + present_queue_info.pNext = nullptr; + present_queue_info.flags = 0; + present_queue_info.queueFamilyIndex = m_present_queue_family_index; + present_queue_info.queueCount = 1; + present_queue_info.pQueuePriorities = queue_priorities; + } + + if (m_spin_queue_family_index == m_graphics_queue_family_index) + { + if (spin_queue_index != 0) + graphics_queue_info.queueCount = 2; + } + else if (m_spin_queue_family_index == m_present_queue_family_index) + { + if (spin_queue_index != 0) + queue_infos[1].queueCount = 2; // present queue + } + else + { + VkDeviceQueueCreateInfo& spin_queue_info = queue_infos[device_info.queueCreateInfoCount++]; + spin_queue_info.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO; + spin_queue_info.pNext = nullptr; + spin_queue_info.flags = 0; + spin_queue_info.queueFamilyIndex = m_spin_queue_family_index; + spin_queue_info.queueCount = 1; + spin_queue_info.pQueuePriorities = queue_priorities + 1; + } + + device_info.pQueueCreateInfos = queue_infos.data(); + + ExtensionList enabled_extensions; + if (!SelectDeviceExtensions(&enabled_extensions, surface != VK_NULL_HANDLE)) + return false; + + device_info.enabledExtensionCount = static_cast(enabled_extensions.size()); + device_info.ppEnabledExtensionNames = enabled_extensions.data(); + + // Check for required features before creating. + if (!SelectDeviceFeatures()) + return false; + + device_info.pEnabledFeatures = &m_device_features; + + // Enable debug layer on debug builds + if (enable_validation_layer) + { + static const char* layer_names[] = {"VK_LAYER_LUNARG_standard_validation"}; + device_info.enabledLayerCount = 1; + device_info.ppEnabledLayerNames = layer_names; + } + + // provoking vertex + VkPhysicalDeviceProvokingVertexFeaturesEXT provoking_vertex_feature = { + VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROVOKING_VERTEX_FEATURES_EXT}; + VkPhysicalDeviceRasterizationOrderAttachmentAccessFeaturesEXT rasterization_order_access_feature = { + VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_RASTERIZATION_ORDER_ATTACHMENT_ACCESS_FEATURES_EXT}; + VkPhysicalDeviceLineRasterizationFeaturesEXT line_rasterization_feature = { + VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_LINE_RASTERIZATION_FEATURES_EXT}; + VkPhysicalDeviceAttachmentFeedbackLoopLayoutFeaturesEXT attachment_feedback_loop_feature = { + VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ATTACHMENT_FEEDBACK_LOOP_LAYOUT_FEATURES_EXT}; + + if (m_optional_extensions.vk_ext_provoking_vertex) + { + provoking_vertex_feature.provokingVertexLast = VK_TRUE; + Vulkan::AddPointerToChain(&device_info, &provoking_vertex_feature); + } + if (m_optional_extensions.vk_ext_line_rasterization) + { + line_rasterization_feature.bresenhamLines = VK_TRUE; + Vulkan::AddPointerToChain(&device_info, &line_rasterization_feature); + } + if (m_optional_extensions.vk_ext_rasterization_order_attachment_access) + { + rasterization_order_access_feature.rasterizationOrderColorAttachmentAccess = VK_TRUE; + Vulkan::AddPointerToChain(&device_info, &rasterization_order_access_feature); + } + if (m_optional_extensions.vk_ext_attachment_feedback_loop_layout) + { + attachment_feedback_loop_feature.attachmentFeedbackLoopLayout = VK_TRUE; + Vulkan::AddPointerToChain(&device_info, &attachment_feedback_loop_feature); + } + + VkResult res = vkCreateDevice(m_physical_device, &device_info, nullptr, &m_device); + if (res != VK_SUCCESS) + { + LOG_VULKAN_ERROR(res, "vkCreateDevice failed: "); + return false; + } + + // With the device created, we can fill the remaining entry points. + if (!Vulkan::LoadVulkanDeviceFunctions(m_device)) + return false; + + // Grab the graphics and present queues. + vkGetDeviceQueue(m_device, m_graphics_queue_family_index, 0, &m_graphics_queue); + if (surface) + { + vkGetDeviceQueue(m_device, m_present_queue_family_index, 0, &m_present_queue); + } + m_spinning_supported = m_spin_queue_family_index != queue_family_count && + queue_family_properties[m_graphics_queue_family_index].timestampValidBits > 0 && + m_device_properties.limits.timestampPeriod > 0; + m_spin_queue_is_graphics_queue = + m_spin_queue_family_index == m_graphics_queue_family_index && spin_queue_index == 0; + + m_gpu_timing_supported = (m_device_properties.limits.timestampComputeAndGraphics != 0 && + queue_family_properties[m_graphics_queue_family_index].timestampValidBits > 0 && + m_device_properties.limits.timestampPeriod > 0); + DevCon.WriteLn("GPU timing is %s (TS=%u TS valid bits=%u, TS period=%f)", + m_gpu_timing_supported ? "supported" : "not supported", + static_cast(m_device_properties.limits.timestampComputeAndGraphics), + queue_family_properties[m_graphics_queue_family_index].timestampValidBits, + m_device_properties.limits.timestampPeriod); + + ProcessDeviceExtensions(); + + if (m_spinning_supported) + { + vkGetDeviceQueue(m_device, m_spin_queue_family_index, spin_queue_index, &m_spin_queue); + + m_spin_timestamp_scale = m_device_properties.limits.timestampPeriod; + if (m_optional_extensions.vk_ext_calibrated_timestamps) + { +#ifdef _WIN32 + LARGE_INTEGER Freq; + QueryPerformanceFrequency(&Freq); + m_queryperfcounter_to_ns = 1000000000.0 / static_cast(Freq.QuadPart); +#endif + CalibrateSpinTimestamp(); + } + } + + return true; +} + +void GSDeviceVK::ProcessDeviceExtensions() +{ + // advanced feature checks + VkPhysicalDeviceFeatures2 features2 = {VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2}; + VkPhysicalDeviceProvokingVertexFeaturesEXT provoking_vertex_features = { + VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROVOKING_VERTEX_FEATURES_EXT}; + VkPhysicalDeviceLineRasterizationFeaturesEXT line_rasterization_feature = { + VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_LINE_RASTERIZATION_FEATURES_EXT}; + VkPhysicalDeviceRasterizationOrderAttachmentAccessFeaturesEXT rasterization_order_access_feature = { + VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_RASTERIZATION_ORDER_ATTACHMENT_ACCESS_FEATURES_EXT}; + VkPhysicalDeviceAttachmentFeedbackLoopLayoutFeaturesEXT attachment_feedback_loop_feature = { + VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ATTACHMENT_FEEDBACK_LOOP_LAYOUT_FEATURES_EXT}; + + // add in optional feature structs + if (m_optional_extensions.vk_ext_provoking_vertex) + Vulkan::AddPointerToChain(&features2, &provoking_vertex_features); + if (m_optional_extensions.vk_ext_line_rasterization) + Vulkan::AddPointerToChain(&features2, &line_rasterization_feature); + if (m_optional_extensions.vk_ext_rasterization_order_attachment_access) + Vulkan::AddPointerToChain(&features2, &rasterization_order_access_feature); + if (m_optional_extensions.vk_ext_attachment_feedback_loop_layout) + Vulkan::AddPointerToChain(&features2, &attachment_feedback_loop_feature); + + // query + vkGetPhysicalDeviceFeatures2(m_physical_device, &features2); + + // confirm we actually support it + m_optional_extensions.vk_ext_provoking_vertex &= (provoking_vertex_features.provokingVertexLast == VK_TRUE); + m_optional_extensions.vk_ext_rasterization_order_attachment_access &= + (rasterization_order_access_feature.rasterizationOrderColorAttachmentAccess == VK_TRUE); + m_optional_extensions.vk_ext_line_rasterization &= (line_rasterization_feature.bresenhamLines == VK_TRUE); + m_optional_extensions.vk_ext_attachment_feedback_loop_layout &= + (attachment_feedback_loop_feature.attachmentFeedbackLoopLayout == VK_TRUE); + + VkPhysicalDeviceProperties2 properties2 = {VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2}; + void** pNext = &properties2.pNext; + + if (m_optional_extensions.vk_khr_driver_properties) + { + m_device_driver_properties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRIVER_PROPERTIES; + *pNext = &m_device_driver_properties; + pNext = &m_device_driver_properties.pNext; + } + + // query + vkGetPhysicalDeviceProperties2(m_physical_device, &properties2); + + // VK_EXT_calibrated_timestamps checking + if (m_optional_extensions.vk_ext_calibrated_timestamps) + { + u32 count = 0; + vkGetPhysicalDeviceCalibrateableTimeDomainsEXT(m_physical_device, &count, nullptr); + std::unique_ptr time_domains = std::make_unique(count); + vkGetPhysicalDeviceCalibrateableTimeDomainsEXT(m_physical_device, &count, time_domains.get()); + const VkTimeDomainEXT* begin = &time_domains[0]; + const VkTimeDomainEXT* end = &time_domains[count]; + if (std::find(begin, end, VK_TIME_DOMAIN_DEVICE_EXT) == end) + m_optional_extensions.vk_ext_calibrated_timestamps = false; + VkTimeDomainEXT preferred_types[] = { +#ifdef _WIN32 + VK_TIME_DOMAIN_QUERY_PERFORMANCE_COUNTER_EXT, +#else +#ifdef CLOCK_MONOTONIC_RAW + VK_TIME_DOMAIN_CLOCK_MONOTONIC_RAW_EXT, +#endif + VK_TIME_DOMAIN_CLOCK_MONOTONIC_EXT, +#endif + }; + m_calibrated_timestamp_type = VK_TIME_DOMAIN_DEVICE_EXT; + for (VkTimeDomainEXT type : preferred_types) + { + if (std::find(begin, end, type) != end) + { + m_calibrated_timestamp_type = type; + break; + } + } + if (m_calibrated_timestamp_type == VK_TIME_DOMAIN_DEVICE_EXT) + m_optional_extensions.vk_ext_calibrated_timestamps = false; + } + + Console.WriteLn( + "VK_EXT_provoking_vertex is %s", m_optional_extensions.vk_ext_provoking_vertex ? "supported" : "NOT supported"); + Console.WriteLn("VK_EXT_line_rasterization is %s", + m_optional_extensions.vk_ext_line_rasterization ? "supported" : "NOT supported"); + Console.WriteLn("VK_EXT_calibrated_timestamps is %s", + m_optional_extensions.vk_ext_calibrated_timestamps ? "supported" : "NOT supported"); + Console.WriteLn("VK_EXT_rasterization_order_attachment_access is %s", + m_optional_extensions.vk_ext_rasterization_order_attachment_access ? "supported" : "NOT supported"); + Console.WriteLn("VK_EXT_attachment_feedback_loop_layout is %s", + m_optional_extensions.vk_ext_attachment_feedback_loop_layout ? "supported" : "NOT supported"); +} + +bool GSDeviceVK::CreateAllocator() +{ + VmaAllocatorCreateInfo ci = {}; + ci.vulkanApiVersion = VK_API_VERSION_1_1; + ci.flags = VMA_ALLOCATOR_CREATE_EXTERNALLY_SYNCHRONIZED_BIT; + ci.physicalDevice = m_physical_device; + ci.device = m_device; + ci.instance = m_instance; + + if (m_optional_extensions.vk_ext_memory_budget) + ci.flags |= VMA_ALLOCATOR_CREATE_EXT_MEMORY_BUDGET_BIT; + + // Limit usage of the DEVICE_LOCAL upload heap when we're using a debug device. + // On NVIDIA drivers, it results in frequently running out of device memory when trying to + // play back captures in RenderDoc, making life very painful. Re-BAR GPUs should be fine. + constexpr VkDeviceSize UPLOAD_HEAP_SIZE_THRESHOLD = 512 * 1024 * 1024; + constexpr VkMemoryPropertyFlags UPLOAD_HEAP_PROPERTIES = + VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT; + std::array heap_size_limits; + if (GSConfig.UseDebugDevice) + { + VkPhysicalDeviceMemoryProperties memory_properties; + vkGetPhysicalDeviceMemoryProperties(m_physical_device, &memory_properties); + + bool has_upload_heap = false; + heap_size_limits.fill(VK_WHOLE_SIZE); + for (u32 i = 0; i < memory_properties.memoryTypeCount; i++) + { + // Look for any memory types which are upload-like. + const VkMemoryType& type = memory_properties.memoryTypes[i]; + if ((type.propertyFlags & UPLOAD_HEAP_PROPERTIES) != UPLOAD_HEAP_PROPERTIES) + continue; + + const VkMemoryHeap& heap = memory_properties.memoryHeaps[type.heapIndex]; + if (heap.size >= UPLOAD_HEAP_SIZE_THRESHOLD) + continue; + + if (heap_size_limits[type.heapIndex] == VK_WHOLE_SIZE) + { + Console.Warning("Disabling allocation from upload heap #%u (%.2f MB) due to debug device.", + type.heapIndex, static_cast(heap.size) / 1048576.0f); + heap_size_limits[type.heapIndex] = 0; + has_upload_heap = true; + } + } + + if (has_upload_heap) + ci.pHeapSizeLimit = heap_size_limits.data(); + } + + VkResult res = vmaCreateAllocator(&ci, &m_allocator); + if (res != VK_SUCCESS) + { + LOG_VULKAN_ERROR(res, "vmaCreateAllocator failed: "); + return false; + } + + return true; +} + +bool GSDeviceVK::CreateCommandBuffers() +{ + VkResult res; + + uint32_t frame_index = 0; + for (FrameResources& resources : m_frame_resources) + { + resources.needs_fence_wait = false; + + VkCommandPoolCreateInfo pool_info = { + VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, nullptr, 0, m_graphics_queue_family_index}; + res = vkCreateCommandPool(m_device, &pool_info, nullptr, &resources.command_pool); + if (res != VK_SUCCESS) + { + LOG_VULKAN_ERROR(res, "vkCreateCommandPool failed: "); + return false; + } + Vulkan::SetObjectName(m_device, resources.command_pool, "Frame Command Pool %u", frame_index); + + VkCommandBufferAllocateInfo buffer_info = {VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, nullptr, + resources.command_pool, VK_COMMAND_BUFFER_LEVEL_PRIMARY, + static_cast(resources.command_buffers.size())}; + + res = vkAllocateCommandBuffers(m_device, &buffer_info, resources.command_buffers.data()); + if (res != VK_SUCCESS) + { + LOG_VULKAN_ERROR(res, "vkAllocateCommandBuffers failed: "); + return false; + } + for (u32 i = 0; i < resources.command_buffers.size(); i++) + { + Vulkan::SetObjectName(m_device, resources.command_buffers[i], + "Frame %u %sCommand Buffer", frame_index, (i == 0) ? "Init" : ""); + } + + VkFenceCreateInfo fence_info = {VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, nullptr, VK_FENCE_CREATE_SIGNALED_BIT}; + + res = vkCreateFence(m_device, &fence_info, nullptr, &resources.fence); + if (res != VK_SUCCESS) + { + LOG_VULKAN_ERROR(res, "vkCreateFence failed: "); + return false; + } + Vulkan::SetObjectName(m_device, resources.fence, "Frame Fence %u", frame_index); + // TODO: A better way to choose the number of descriptors. + VkDescriptorPoolSize pool_sizes[] = { + {VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, MAX_COMBINED_IMAGE_SAMPLER_DESCRIPTORS_PER_FRAME}, + {VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, MAX_SAMPLED_IMAGE_DESCRIPTORS_PER_FRAME}, + {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, MAX_STORAGE_IMAGE_DESCRIPTORS_PER_FRAME}, + {VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT, MAX_INPUT_ATTACHMENT_IMAGE_DESCRIPTORS_PER_FRAME}, + }; + + VkDescriptorPoolCreateInfo pool_create_info = {VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, nullptr, 0, + MAX_DESCRIPTOR_SETS_PER_FRAME, static_cast(std::size(pool_sizes)), pool_sizes}; + + res = vkCreateDescriptorPool(m_device, &pool_create_info, nullptr, &resources.descriptor_pool); + if (res != VK_SUCCESS) + { + LOG_VULKAN_ERROR(res, "vkCreateDescriptorPool failed: "); + return false; + } + Vulkan::SetObjectName( + m_device, resources.descriptor_pool, "Frame Descriptor Pool %u", frame_index); + + ++frame_index; + } + + ActivateCommandBuffer(0); + return true; +} + +bool GSDeviceVK::CreateGlobalDescriptorPool() +{ + static constexpr const VkDescriptorPoolSize pool_sizes[] = { + {VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, 2}, + {VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 2}, + }; + + VkDescriptorPoolCreateInfo pool_create_info = {VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, nullptr, + VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT, + 1024, // TODO: tweak this + static_cast(std::size(pool_sizes)), pool_sizes}; + + VkResult res = vkCreateDescriptorPool(m_device, &pool_create_info, nullptr, &m_global_descriptor_pool); + if (res != VK_SUCCESS) + { + LOG_VULKAN_ERROR(res, "vkCreateDescriptorPool failed: "); + return false; + } + Vulkan::SetObjectName(m_device, m_global_descriptor_pool, "Global Descriptor Pool"); + + if (m_gpu_timing_supported) + { + const VkQueryPoolCreateInfo query_create_info = { + VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO, nullptr, 0, VK_QUERY_TYPE_TIMESTAMP, NUM_COMMAND_BUFFERS * 4, 0}; + res = vkCreateQueryPool(m_device, &query_create_info, nullptr, &m_timestamp_query_pool); + if (res != VK_SUCCESS) + { + LOG_VULKAN_ERROR(res, "vkCreateQueryPool failed: "); + m_gpu_timing_supported = false; + return false; + } + } + + return true; +} + +VkRenderPass GSDeviceVK::GetRenderPass(VkFormat color_format, VkFormat depth_format, VkAttachmentLoadOp color_load_op, + VkAttachmentStoreOp color_store_op, VkAttachmentLoadOp depth_load_op, VkAttachmentStoreOp depth_store_op, + VkAttachmentLoadOp stencil_load_op, VkAttachmentStoreOp stencil_store_op, bool color_feedback_loop, + bool depth_sampling) +{ + RenderPassCacheKey key = {}; + key.color_format = color_format; + key.depth_format = depth_format; + key.color_load_op = color_load_op; + key.color_store_op = color_store_op; + key.depth_load_op = depth_load_op; + key.depth_store_op = depth_store_op; + key.stencil_load_op = stencil_load_op; + key.stencil_store_op = stencil_store_op; + key.color_feedback_loop = color_feedback_loop; + key.depth_sampling = depth_sampling; + + auto it = m_render_pass_cache.find(key.key); + if (it != m_render_pass_cache.end()) + return it->second; + + return CreateCachedRenderPass(key); +} + +VkRenderPass GSDeviceVK::GetRenderPassForRestarting(VkRenderPass pass) +{ + for (const auto& it : m_render_pass_cache) + { + if (it.second != pass) + continue; + + RenderPassCacheKey modified_key; + modified_key.key = it.first; + if (modified_key.color_load_op == VK_ATTACHMENT_LOAD_OP_CLEAR) + modified_key.color_load_op = VK_ATTACHMENT_LOAD_OP_LOAD; + if (modified_key.depth_load_op == VK_ATTACHMENT_LOAD_OP_CLEAR) + modified_key.depth_load_op = VK_ATTACHMENT_LOAD_OP_LOAD; + if (modified_key.stencil_load_op == VK_ATTACHMENT_LOAD_OP_CLEAR) + modified_key.stencil_load_op = VK_ATTACHMENT_LOAD_OP_LOAD; + + if (modified_key.key == it.first) + return pass; + + auto fit = m_render_pass_cache.find(modified_key.key); + if (fit != m_render_pass_cache.end()) + return fit->second; + + return CreateCachedRenderPass(modified_key); + } + + return pass; +} + +VkCommandBuffer GSDeviceVK::GetCurrentInitCommandBuffer() +{ + FrameResources& res = m_frame_resources[m_current_frame]; + VkCommandBuffer buf = res.command_buffers[0]; + if (res.init_buffer_used) + return buf; + + VkCommandBufferBeginInfo bi{ + VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, nullptr, VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT, nullptr}; + vkBeginCommandBuffer(buf, &bi); + res.init_buffer_used = true; + return buf; +} + +VkDescriptorSet GSDeviceVK::AllocateDescriptorSet(VkDescriptorSetLayout set_layout) +{ + VkDescriptorSetAllocateInfo allocate_info = {VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO, nullptr, + m_frame_resources[m_current_frame].descriptor_pool, 1, &set_layout}; + + VkDescriptorSet descriptor_set; + VkResult res = vkAllocateDescriptorSets(m_device, &allocate_info, &descriptor_set); + if (res != VK_SUCCESS) + { + // Failing to allocate a descriptor set is not a fatal error, we can + // recover by moving to the next command buffer. + return VK_NULL_HANDLE; + } + + return descriptor_set; +} + +VkDescriptorSet GSDeviceVK::AllocatePersistentDescriptorSet(VkDescriptorSetLayout set_layout) +{ + VkDescriptorSetAllocateInfo allocate_info = { + VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO, nullptr, m_global_descriptor_pool, 1, &set_layout}; + + VkDescriptorSet descriptor_set; + VkResult res = vkAllocateDescriptorSets(m_device, &allocate_info, &descriptor_set); + if (res != VK_SUCCESS) + return VK_NULL_HANDLE; + + return descriptor_set; +} + +void GSDeviceVK::FreePersistentDescriptorSet(VkDescriptorSet set) +{ + vkFreeDescriptorSets(m_device, m_global_descriptor_pool, 1, &set); +} + +void GSDeviceVK::WaitForFenceCounter(u64 fence_counter) +{ + if (m_completed_fence_counter >= fence_counter) + return; + + // Find the first command buffer which covers this counter value. + u32 index = (m_current_frame + 1) % NUM_COMMAND_BUFFERS; + while (index != m_current_frame) + { + if (m_frame_resources[index].fence_counter >= fence_counter) + break; + + index = (index + 1) % NUM_COMMAND_BUFFERS; + } + + pxAssert(index != m_current_frame); + WaitForCommandBufferCompletion(index); +} + +void GSDeviceVK::WaitForGPUIdle() +{ + WaitForPresentComplete(); + vkDeviceWaitIdle(m_device); +} + +float GSDeviceVK::GetAndResetAccumulatedGPUTime() +{ + const float time = m_accumulated_gpu_time; + m_accumulated_gpu_time = 0.0f; + return time; +} + +bool GSDeviceVK::SetGPUTimingEnabled(bool enabled) +{ + m_gpu_timing_enabled = enabled && m_gpu_timing_supported; + return (enabled == m_gpu_timing_enabled); +} + +void GSDeviceVK::ScanForCommandBufferCompletion() +{ + for (u32 check_index = (m_current_frame + 1) % NUM_COMMAND_BUFFERS; check_index != m_current_frame; + check_index = (check_index + 1) % NUM_COMMAND_BUFFERS) + { + FrameResources& resources = m_frame_resources[check_index]; + if (resources.fence_counter <= m_completed_fence_counter) + continue; // Already completed + if (vkGetFenceStatus(m_device, resources.fence) != VK_SUCCESS) + break; // Fence not signaled, later fences won't be either + CommandBufferCompleted(check_index); + m_completed_fence_counter = resources.fence_counter; + } + for (SpinResources& resources : m_spin_resources) + { + if (!resources.in_progress) + continue; + if (vkGetFenceStatus(m_device, resources.fence) != VK_SUCCESS) + continue; + SpinCommandCompleted(&resources - &m_spin_resources[0]); + } +} + +void GSDeviceVK::WaitForCommandBufferCompletion(u32 index) +{ + // Wait for this command buffer to be completed. + const VkResult res = vkWaitForFences(m_device, 1, &m_frame_resources[index].fence, VK_TRUE, UINT64_MAX); + if (res != VK_SUCCESS) + { + LOG_VULKAN_ERROR(res, "vkWaitForFences failed: "); + m_last_submit_failed.store(true, std::memory_order_release); + return; + } + + // Clean up any resources for command buffers between the last known completed buffer and this + // now-completed command buffer. If we use >2 buffers, this may be more than one buffer. + const u64 now_completed_counter = m_frame_resources[index].fence_counter; + u32 cleanup_index = (m_current_frame + 1) % NUM_COMMAND_BUFFERS; + while (cleanup_index != m_current_frame) + { + FrameResources& resources = m_frame_resources[cleanup_index]; + if (resources.fence_counter > now_completed_counter) + break; + + if (resources.fence_counter > m_completed_fence_counter) + CommandBufferCompleted(cleanup_index); + + cleanup_index = (cleanup_index + 1) % NUM_COMMAND_BUFFERS; + } + + m_completed_fence_counter = now_completed_counter; +} + +void GSDeviceVK::SubmitCommandBuffer( + VKSwapChain* present_swap_chain /* = nullptr */, bool submit_on_thread /* = false */) +{ + FrameResources& resources = m_frame_resources[m_current_frame]; + + // End the current command buffer. + VkResult res; + if (resources.init_buffer_used) + { + res = vkEndCommandBuffer(resources.command_buffers[0]); + if (res != VK_SUCCESS) + { + LOG_VULKAN_ERROR(res, "vkEndCommandBuffer failed: "); + pxFailRel("Failed to end command buffer"); + } + } + + bool wants_timestamp = m_gpu_timing_enabled || m_spin_timer; + if (wants_timestamp && resources.timestamp_written) + { + vkCmdWriteTimestamp(m_current_command_buffer, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, m_timestamp_query_pool, + m_current_frame * 2 + 1); + } + + res = vkEndCommandBuffer(resources.command_buffers[1]); + if (res != VK_SUCCESS) + { + LOG_VULKAN_ERROR(res, "vkEndCommandBuffer failed: "); + pxFailRel("Failed to end command buffer"); + } + + // This command buffer now has commands, so can't be re-used without waiting. + resources.needs_fence_wait = true; + + u32 spin_cycles = 0; + const bool spin_enabled = m_spin_timer; + if (spin_enabled) + { + ScanForCommandBufferCompletion(); + auto draw = m_spin_manager.DrawSubmitted(m_command_buffer_render_passes); + u32 constant_offset = + 400000 * m_spin_manager.SpinsPerUnitTime(); // 400us, just to be safe since going over gets really bad + if (m_optional_extensions.vk_ext_calibrated_timestamps) + constant_offset /= + 2; // Safety factor isn't as important here, going over just hurts this one submission a bit + u32 minimum_spin = 200000 * m_spin_manager.SpinsPerUnitTime(); + u32 maximum_spin = std::max(1024, 16000000 * m_spin_manager.SpinsPerUnitTime()); // 16ms + if (draw.recommended_spin > minimum_spin + constant_offset) + spin_cycles = std::min(draw.recommended_spin - constant_offset, maximum_spin); + resources.spin_id = draw.id; + } + else + { + resources.spin_id = -1; + } + m_command_buffer_render_passes = 0; + + if (present_swap_chain != VK_NULL_HANDLE && m_spinning_supported) + { + m_spin_manager.NextFrame(); + if (m_spin_timer) + m_spin_timer--; + // Calibrate a max of once per frame + m_wants_new_timestamp_calibration = m_optional_extensions.vk_ext_calibrated_timestamps; + } + + if (spin_cycles != 0) + WaitForSpinCompletion(m_current_frame); + + std::unique_lock lock(m_present_mutex); + WaitForPresentComplete(lock); + + if (spin_enabled && m_optional_extensions.vk_ext_calibrated_timestamps) + resources.submit_timestamp = GetCPUTimestamp(); + + if (!submit_on_thread || !m_present_thread.joinable()) + { + DoSubmitCommandBuffer(m_current_frame, present_swap_chain, spin_cycles); + if (present_swap_chain) + DoPresent(present_swap_chain); + return; + } + + m_queued_present.command_buffer_index = m_current_frame; + m_queued_present.swap_chain = present_swap_chain; + m_queued_present.spin_cycles = spin_cycles; + m_present_done.store(false); + m_present_queued_cv.notify_one(); +} + +void GSDeviceVK::DoSubmitCommandBuffer(u32 index, VKSwapChain* present_swap_chain, u32 spin_cycles) +{ + FrameResources& resources = m_frame_resources[index]; + + uint32_t wait_bits = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; + VkSemaphore semas[2]; + VkSubmitInfo submit_info = {VK_STRUCTURE_TYPE_SUBMIT_INFO}; + submit_info.commandBufferCount = resources.init_buffer_used ? 2u : 1u; + submit_info.pCommandBuffers = + resources.init_buffer_used ? resources.command_buffers.data() : &resources.command_buffers[1]; + + if (present_swap_chain) + { + submit_info.pWaitSemaphores = present_swap_chain->GetImageAvailableSemaphorePtr(); + submit_info.waitSemaphoreCount = 1; + submit_info.pWaitDstStageMask = &wait_bits; + + if (spin_cycles != 0) + { + semas[0] = present_swap_chain->GetRenderingFinishedSemaphore(); + semas[1] = m_spin_resources[index].semaphore; + submit_info.signalSemaphoreCount = 2; + submit_info.pSignalSemaphores = semas; + } + else + { + submit_info.pSignalSemaphores = present_swap_chain->GetRenderingFinishedSemaphorePtr(); + submit_info.signalSemaphoreCount = 1; + } + } + else if (spin_cycles != 0) + { + submit_info.signalSemaphoreCount = 1; + submit_info.pSignalSemaphores = &m_spin_resources[index].semaphore; + } + + const VkResult res = vkQueueSubmit(m_graphics_queue, 1, &submit_info, resources.fence); + if (res != VK_SUCCESS) + { + LOG_VULKAN_ERROR(res, "vkQueueSubmit failed: "); + m_last_submit_failed.store(true, std::memory_order_release); + return; + } + + if (spin_cycles != 0) + SubmitSpinCommand(index, spin_cycles); +} + +void GSDeviceVK::DoPresent(VKSwapChain* present_swap_chain) +{ + const VkPresentInfoKHR present_info = {VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, nullptr, 1, + present_swap_chain->GetRenderingFinishedSemaphorePtr(), 1, present_swap_chain->GetSwapChainPtr(), + present_swap_chain->GetCurrentImageIndexPtr(), nullptr}; + + present_swap_chain->ReleaseCurrentImage(); + + const VkResult res = vkQueuePresentKHR(m_present_queue, &present_info); + if (res != VK_SUCCESS) + { + // VK_ERROR_OUT_OF_DATE_KHR is not fatal, just means we need to recreate our swap chain. + if (res != VK_ERROR_OUT_OF_DATE_KHR && res != VK_SUBOPTIMAL_KHR) + LOG_VULKAN_ERROR(res, "vkQueuePresentKHR failed: "); + + m_last_present_failed.store(true, std::memory_order_release); + return; + } + + // Grab the next image as soon as possible, that way we spend less time blocked on the next + // submission. Don't care if it fails, we'll deal with that at the presentation call site. + // Credit to dxvk for the idea. + present_swap_chain->AcquireNextImage(); +} + +void GSDeviceVK::WaitForPresentComplete() +{ + if (m_present_done.load()) + return; + + std::unique_lock lock(m_present_mutex); + WaitForPresentComplete(lock); +} + +void GSDeviceVK::WaitForPresentComplete(std::unique_lock& lock) +{ + if (m_present_done.load()) + return; + + m_present_done_cv.wait(lock, [this]() { return m_present_done.load(); }); +} + +void GSDeviceVK::PresentThread() +{ + std::unique_lock lock(m_present_mutex); + while (!m_present_thread_done.load()) + { + m_present_queued_cv.wait(lock, [this]() { return !m_present_done.load() || m_present_thread_done.load(); }); + + if (m_present_done.load()) + continue; + + DoSubmitCommandBuffer( + m_queued_present.command_buffer_index, m_queued_present.swap_chain, m_queued_present.spin_cycles); + if (m_queued_present.swap_chain) + DoPresent(m_queued_present.swap_chain); + m_present_done.store(true); + m_present_done_cv.notify_one(); + } +} + +void GSDeviceVK::StartPresentThread() +{ + pxAssert(!m_present_thread.joinable()); + m_present_thread_done.store(false); + m_present_thread = std::thread(&GSDeviceVK::PresentThread, this); +} + +void GSDeviceVK::StopPresentThread() +{ + if (!m_present_thread.joinable()) + return; + + { + std::unique_lock lock(m_present_mutex); + WaitForPresentComplete(lock); + m_present_thread_done.store(true); + m_present_queued_cv.notify_one(); + } + + m_present_thread.join(); +} + +void GSDeviceVK::CommandBufferCompleted(u32 index) +{ + FrameResources& resources = m_frame_resources[index]; + + for (auto& it : resources.cleanup_resources) + it(); + resources.cleanup_resources.clear(); + + bool wants_timestamps = m_gpu_timing_enabled || resources.spin_id >= 0; + + if (wants_timestamps && resources.timestamp_written) + { + std::array timestamps; + VkResult res = + vkGetQueryPoolResults(m_device, m_timestamp_query_pool, index * 2, static_cast(timestamps.size()), + sizeof(u64) * timestamps.size(), timestamps.data(), sizeof(u64), VK_QUERY_RESULT_64_BIT); + if (res == VK_SUCCESS) + { + // if we didn't write the timestamp at the start of the cmdbuffer (just enabled timing), the first TS will be zero + if (timestamps[0] > 0 && m_gpu_timing_enabled) + { + const double ns_diff = + (timestamps[1] - timestamps[0]) * static_cast(m_device_properties.limits.timestampPeriod); + m_accumulated_gpu_time += ns_diff / 1000000.0; + } + if (resources.spin_id >= 0) + { + if (m_optional_extensions.vk_ext_calibrated_timestamps && timestamps[1] > 0) + { + u64 end = timestamps[1] * m_spin_timestamp_scale + m_spin_timestamp_offset; + m_spin_manager.DrawCompleted(resources.spin_id, resources.submit_timestamp, end); + } + else if (!m_optional_extensions.vk_ext_calibrated_timestamps && timestamps[0] > 0) + { + u64 begin = timestamps[0] * m_spin_timestamp_scale; + u64 end = timestamps[1] * m_spin_timestamp_scale; + m_spin_manager.DrawCompleted(resources.spin_id, begin, end); + } + } + } + else + { + LOG_VULKAN_ERROR(res, "vkGetQueryPoolResults failed: "); + } + } +} + +void GSDeviceVK::MoveToNextCommandBuffer() +{ + ActivateCommandBuffer((m_current_frame + 1) % NUM_COMMAND_BUFFERS); +} + +void GSDeviceVK::ActivateCommandBuffer(u32 index) +{ + FrameResources& resources = m_frame_resources[index]; + + if (!m_present_done.load() && m_queued_present.command_buffer_index == index) + WaitForPresentComplete(); + + // Wait for the GPU to finish with all resources for this command buffer. + if (resources.fence_counter > m_completed_fence_counter) + WaitForCommandBufferCompletion(index); + + // Reset fence to unsignaled before starting. + VkResult res = vkResetFences(m_device, 1, &resources.fence); + if (res != VK_SUCCESS) + LOG_VULKAN_ERROR(res, "vkResetFences failed: "); + + // Reset command pools to beginning since we can re-use the memory now + res = vkResetCommandPool(m_device, resources.command_pool, 0); + if (res != VK_SUCCESS) + LOG_VULKAN_ERROR(res, "vkResetCommandPool failed: "); + + // Enable commands to be recorded to the two buffers again. + VkCommandBufferBeginInfo begin_info = { + VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, nullptr, VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT, nullptr}; + res = vkBeginCommandBuffer(resources.command_buffers[1], &begin_info); + if (res != VK_SUCCESS) + LOG_VULKAN_ERROR(res, "vkBeginCommandBuffer failed: "); + + // Also can do the same for the descriptor pools + res = vkResetDescriptorPool(m_device, resources.descriptor_pool, 0); + if (res != VK_SUCCESS) + LOG_VULKAN_ERROR(res, "vkResetDescriptorPool failed: "); + + bool wants_timestamp = m_gpu_timing_enabled || m_spin_timer; + if (wants_timestamp) + { + vkCmdResetQueryPool(resources.command_buffers[1], m_timestamp_query_pool, index * 2, 2); + vkCmdWriteTimestamp( + resources.command_buffers[1], VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, m_timestamp_query_pool, index * 2); + } + + resources.fence_counter = m_next_fence_counter++; + resources.init_buffer_used = false; + resources.timestamp_written = wants_timestamp; + + m_current_frame = index; + m_current_command_buffer = resources.command_buffers[1]; + + // using the lower 32 bits of the fence index should be sufficient here, I hope... + vmaSetCurrentFrameIndex(m_allocator, static_cast(m_next_fence_counter)); +} + +void GSDeviceVK::ExecuteCommandBuffer(WaitType wait_for_completion) +{ + if (m_last_submit_failed.load(std::memory_order_acquire)) + return; + + // If we're waiting for completion, don't bother waking the worker thread. + const u32 current_frame = m_current_frame; + SubmitCommandBuffer(); + MoveToNextCommandBuffer(); + + if (wait_for_completion != WaitType::None) + { + // Calibrate while we wait + if (m_wants_new_timestamp_calibration) + CalibrateSpinTimestamp(); + if (wait_for_completion == WaitType::Spin) + { + while (vkGetFenceStatus(m_device, m_frame_resources[current_frame].fence) == VK_NOT_READY) + ShortSpin(); + } + WaitForCommandBufferCompletion(current_frame); + } +} + +bool GSDeviceVK::CheckLastPresentFail() +{ + return m_last_present_failed.exchange(false, std::memory_order_acq_rel); +} + +bool GSDeviceVK::CheckLastSubmitFail() +{ + return m_last_submit_failed.load(std::memory_order_acquire); +} + +void GSDeviceVK::DeferBufferDestruction(VkBuffer object, VmaAllocation allocation) +{ + FrameResources& resources = m_frame_resources[m_current_frame]; + resources.cleanup_resources.push_back( + [this, object, allocation]() { vmaDestroyBuffer(m_allocator, object, allocation); }); +} + +void GSDeviceVK::DeferFramebufferDestruction(VkFramebuffer object) +{ + FrameResources& resources = m_frame_resources[m_current_frame]; + resources.cleanup_resources.push_back([this, object]() { vkDestroyFramebuffer(m_device, object, nullptr); }); +} + +void GSDeviceVK::DeferImageDestruction(VkImage object, VmaAllocation allocation) +{ + FrameResources& resources = m_frame_resources[m_current_frame]; + resources.cleanup_resources.push_back( + [this, object, allocation]() { vmaDestroyImage(m_allocator, object, allocation); }); +} + +void GSDeviceVK::DeferImageViewDestruction(VkImageView object) +{ + FrameResources& resources = m_frame_resources[m_current_frame]; + resources.cleanup_resources.push_back([this, object]() { vkDestroyImageView(m_device, object, nullptr); }); +} + +VKAPI_ATTR VkBool32 VKAPI_CALL DebugMessengerCallback(VkDebugUtilsMessageSeverityFlagBitsEXT severity, + VkDebugUtilsMessageTypeFlagsEXT messageType, const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData, + void* pUserData) +{ + if (severity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) + { + Console.Error("Vulkan debug report: (%s) %s", + pCallbackData->pMessageIdName ? pCallbackData->pMessageIdName : "", pCallbackData->pMessage); + } + else if (severity & (VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT)) + { + Console.Warning("Vulkan debug report: (%s) %s", + pCallbackData->pMessageIdName ? pCallbackData->pMessageIdName : "", pCallbackData->pMessage); + } + else if (severity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT) + { + Console.WriteLn("Vulkan debug report: (%s) %s", + pCallbackData->pMessageIdName ? pCallbackData->pMessageIdName : "", pCallbackData->pMessage); + } + else + { + DevCon.WriteLn("Vulkan debug report: (%s) %s", + pCallbackData->pMessageIdName ? pCallbackData->pMessageIdName : "", pCallbackData->pMessage); + } + + return VK_FALSE; +} + +bool GSDeviceVK::EnableDebugUtils() +{ + // Already enabled? + if (m_debug_messenger_callback != VK_NULL_HANDLE) + return true; + + // Check for presence of the functions before calling + if (!vkCreateDebugUtilsMessengerEXT || !vkDestroyDebugUtilsMessengerEXT || !vkSubmitDebugUtilsMessageEXT) + { + return false; + } + + VkDebugUtilsMessengerCreateInfoEXT messenger_info = {VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT, + nullptr, 0, + VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | + VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT, + VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT | + VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT, + DebugMessengerCallback, nullptr}; + + const VkResult res = + vkCreateDebugUtilsMessengerEXT(m_instance, &messenger_info, nullptr, &m_debug_messenger_callback); + if (res != VK_SUCCESS) + { + LOG_VULKAN_ERROR(res, "vkCreateDebugUtilsMessengerEXT failed: "); + return false; + } + + return true; +} + +void GSDeviceVK::DisableDebugUtils() +{ + if (m_debug_messenger_callback != VK_NULL_HANDLE) + { + vkDestroyDebugUtilsMessengerEXT(m_instance, m_debug_messenger_callback, nullptr); + m_debug_messenger_callback = VK_NULL_HANDLE; + } +} + +VkRenderPass GSDeviceVK::CreateCachedRenderPass(RenderPassCacheKey key) +{ + VkAttachmentReference color_reference; + VkAttachmentReference* color_reference_ptr = nullptr; + VkAttachmentReference depth_reference; + VkAttachmentReference* depth_reference_ptr = nullptr; + VkAttachmentReference input_reference; + VkAttachmentReference* input_reference_ptr = nullptr; + VkSubpassDependency subpass_dependency; + VkSubpassDependency* subpass_dependency_ptr = nullptr; + std::array attachments; + u32 num_attachments = 0; + if (key.color_format != VK_FORMAT_UNDEFINED) + { + const VkImageLayout layout = + key.color_feedback_loop ? (UseFeedbackLoopLayout() ? VK_IMAGE_LAYOUT_ATTACHMENT_FEEDBACK_LOOP_OPTIMAL_EXT : + VK_IMAGE_LAYOUT_GENERAL) : + VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; + attachments[num_attachments] = {0, static_cast(key.color_format), VK_SAMPLE_COUNT_1_BIT, + static_cast(key.color_load_op), static_cast(key.color_store_op), + VK_ATTACHMENT_LOAD_OP_DONT_CARE, VK_ATTACHMENT_STORE_OP_DONT_CARE, layout, layout}; + color_reference.attachment = num_attachments; + color_reference.layout = layout; + color_reference_ptr = &color_reference; + + if (key.color_feedback_loop) + { + if (!UseFeedbackLoopLayout()) + { + input_reference.attachment = num_attachments; + input_reference.layout = layout; + input_reference_ptr = &input_reference; + } + + if (!m_optional_extensions.vk_ext_rasterization_order_attachment_access) + { + // don't need the framebuffer-local dependency when we have rasterization order attachment access + subpass_dependency.srcSubpass = 0; + subpass_dependency.dstSubpass = 0; + subpass_dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; + subpass_dependency.dstStageMask = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT; + subpass_dependency.srcAccessMask = + VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; + subpass_dependency.dstAccessMask = + UseFeedbackLoopLayout() ? VK_ACCESS_SHADER_READ_BIT : VK_ACCESS_INPUT_ATTACHMENT_READ_BIT; + subpass_dependency.dependencyFlags = + UseFeedbackLoopLayout() ? (VK_DEPENDENCY_BY_REGION_BIT | VK_DEPENDENCY_FEEDBACK_LOOP_BIT_EXT) : + VK_DEPENDENCY_BY_REGION_BIT; + subpass_dependency_ptr = &subpass_dependency; + } + } + + num_attachments++; + } + if (key.depth_format != VK_FORMAT_UNDEFINED) + { + const VkImageLayout layout = key.depth_sampling ? (UseFeedbackLoopLayout() ? VK_IMAGE_LAYOUT_ATTACHMENT_FEEDBACK_LOOP_OPTIMAL_EXT : + VK_IMAGE_LAYOUT_GENERAL) : + VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; + attachments[num_attachments] = {0, static_cast(key.depth_format), VK_SAMPLE_COUNT_1_BIT, + static_cast(key.depth_load_op), static_cast(key.depth_store_op), + static_cast(key.stencil_load_op), + static_cast(key.stencil_store_op), layout, layout}; + depth_reference.attachment = num_attachments; + depth_reference.layout = layout; + depth_reference_ptr = &depth_reference; + num_attachments++; + } + + const VkSubpassDescriptionFlags subpass_flags = + (key.color_feedback_loop && m_optional_extensions.vk_ext_rasterization_order_attachment_access) ? + VK_SUBPASS_DESCRIPTION_RASTERIZATION_ORDER_ATTACHMENT_COLOR_ACCESS_BIT_EXT : + 0; + const VkSubpassDescription subpass = {subpass_flags, VK_PIPELINE_BIND_POINT_GRAPHICS, input_reference_ptr ? 1u : 0u, + input_reference_ptr ? input_reference_ptr : nullptr, color_reference_ptr ? 1u : 0u, + color_reference_ptr ? color_reference_ptr : nullptr, nullptr, depth_reference_ptr, 0, nullptr}; + const VkRenderPassCreateInfo pass_info = {VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO, nullptr, 0u, num_attachments, + attachments.data(), 1u, &subpass, subpass_dependency_ptr ? 1u : 0u, subpass_dependency_ptr}; + + VkRenderPass pass; + const VkResult res = vkCreateRenderPass(m_device, &pass_info, nullptr, &pass); + if (res != VK_SUCCESS) + { + LOG_VULKAN_ERROR(res, "vkCreateRenderPass failed: "); + return VK_NULL_HANDLE; + } + + m_render_pass_cache.emplace(key.key, pass); + return pass; +} + +static constexpr std::string_view SPIN_SHADER = R"( +#version 460 core + +layout(std430, set=0, binding=0) buffer SpinBuffer { uint spin[]; }; +layout(push_constant) uniform constants { uint cycles; }; +layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in; + +void main() +{ + uint value = spin[0]; + // The compiler doesn't know, but spin[0] == 0, so this loop won't actually go anywhere + for (uint i = 0; i < cycles; i++) + value = spin[value]; + // Store the result back to the buffer so the compiler can't optimize it away + spin[0] = value; +} +)"; + +bool GSDeviceVK::InitSpinResources() +{ + if (!m_spinning_supported) + return true; + + // TODO: Move to safe destroy functions, use scoped guard. + + VkResult res; +#define CHECKED_CREATE(create_fn, create_struct, output_struct) \ + do \ + { \ + if ((res = create_fn(m_device, create_struct, nullptr, output_struct)) != VK_SUCCESS) \ + { \ + LOG_VULKAN_ERROR(res, #create_fn " failed: "); \ + return false; \ + } \ + } while (0) + + VkDescriptorSetLayoutBinding set_layout_binding = {}; + set_layout_binding.binding = 0; + set_layout_binding.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; + set_layout_binding.descriptorCount = 1; + set_layout_binding.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT; + VkDescriptorSetLayoutCreateInfo desc_set_layout_create = {VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO}; + desc_set_layout_create.bindingCount = 1; + desc_set_layout_create.pBindings = &set_layout_binding; + CHECKED_CREATE(vkCreateDescriptorSetLayout, &desc_set_layout_create, &m_spin_descriptor_set_layout); + + const VkPushConstantRange push_constant_range = {VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(u32)}; + VkPipelineLayoutCreateInfo pl_layout_create = {VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO}; + pl_layout_create.setLayoutCount = 1; + pl_layout_create.pSetLayouts = &m_spin_descriptor_set_layout; + pl_layout_create.pushConstantRangeCount = 1; + pl_layout_create.pPushConstantRanges = &push_constant_range; + CHECKED_CREATE(vkCreatePipelineLayout, &pl_layout_create, &m_spin_pipeline_layout); + + VkShaderModule shader_module = g_vulkan_shader_cache->GetComputeShader(SPIN_SHADER); + if (shader_module == VK_NULL_HANDLE) + return false; + + Vulkan::SetObjectName(m_device, shader_module, "Spin Shader"); + + VkComputePipelineCreateInfo pl_create = {VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO}; + pl_create.layout = m_spin_pipeline_layout; + pl_create.stage.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; + pl_create.stage.stage = VK_SHADER_STAGE_COMPUTE_BIT; + pl_create.stage.pName = "main"; + pl_create.stage.module = shader_module; + res = vkCreateComputePipelines(m_device, VK_NULL_HANDLE, 1, &pl_create, nullptr, &m_spin_pipeline); + vkDestroyShaderModule(m_device, shader_module, nullptr); + if (res != VK_SUCCESS) + { + LOG_VULKAN_ERROR(res, "vkCreateComputePipelines failed: "); + return false; + } + Vulkan::SetObjectName(m_device, m_spin_pipeline, "Spin Pipeline"); + + VmaAllocationCreateInfo buf_vma_create = {}; + buf_vma_create.usage = VMA_MEMORY_USAGE_GPU_ONLY; + VkBufferCreateInfo buf_create = {VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO}; + buf_create.size = 4; + buf_create.usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT; + if ((res = vmaCreateBuffer(m_allocator, &buf_create, &buf_vma_create, &m_spin_buffer, &m_spin_buffer_allocation, + nullptr)) != VK_SUCCESS) + { + LOG_VULKAN_ERROR(res, "vmaCreateBuffer failed: "); + return false; + } + Vulkan::SetObjectName(m_device, m_spin_buffer, "Spin Buffer"); + + VkDescriptorSetAllocateInfo desc_set_allocate = {VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO}; + desc_set_allocate.descriptorPool = m_global_descriptor_pool; + desc_set_allocate.descriptorSetCount = 1; + desc_set_allocate.pSetLayouts = &m_spin_descriptor_set_layout; + if ((res = vkAllocateDescriptorSets(m_device, &desc_set_allocate, &m_spin_descriptor_set)) != VK_SUCCESS) + { + LOG_VULKAN_ERROR(res, "vkAllocateDescriptorSets failed: "); + return false; + } + const VkDescriptorBufferInfo desc_buffer_info = {m_spin_buffer, 0, VK_WHOLE_SIZE}; + VkWriteDescriptorSet desc_set_write = {VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET}; + desc_set_write.dstSet = m_spin_descriptor_set; + desc_set_write.dstBinding = 0; + desc_set_write.descriptorCount = 1; + desc_set_write.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; + desc_set_write.pBufferInfo = &desc_buffer_info; + vkUpdateDescriptorSets(m_device, 1, &desc_set_write, 0, nullptr); + + for (SpinResources& resources : m_spin_resources) + { + u32 index = &resources - &m_spin_resources[0]; + VkCommandPoolCreateInfo pool_info = {VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO}; + pool_info.queueFamilyIndex = m_spin_queue_family_index; + CHECKED_CREATE(vkCreateCommandPool, &pool_info, &resources.command_pool); + Vulkan::SetObjectName(m_device, resources.command_pool, "Spin Command Pool %u", index); + + VkCommandBufferAllocateInfo buffer_info = {VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO}; + buffer_info.commandPool = resources.command_pool; + buffer_info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; + buffer_info.commandBufferCount = 1; + res = vkAllocateCommandBuffers(m_device, &buffer_info, &resources.command_buffer); + if (res != VK_SUCCESS) + { + LOG_VULKAN_ERROR(res, "vkAllocateCommandBuffers failed: "); + return false; + } + Vulkan::SetObjectName(m_device, resources.command_buffer, "Spin Command Buffer %u", index); + + VkFenceCreateInfo fence_info = {VK_STRUCTURE_TYPE_FENCE_CREATE_INFO}; + fence_info.flags = VK_FENCE_CREATE_SIGNALED_BIT; + CHECKED_CREATE(vkCreateFence, &fence_info, &resources.fence); + Vulkan::SetObjectName(m_device, resources.fence, "Spin Fence %u", index); + + if (!m_spin_queue_is_graphics_queue) + { + VkSemaphoreCreateInfo sem_info = {VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO}; + CHECKED_CREATE(vkCreateSemaphore, &sem_info, &resources.semaphore); + Vulkan::SetObjectName(m_device, resources.semaphore, "Draw to Spin Semaphore %u", index); + } + } + +#undef CHECKED_CREATE + return true; +} + +void GSDeviceVK::DestroySpinResources() +{ +#define CHECKED_DESTROY(destructor, obj) \ + do \ + { \ + if (obj != VK_NULL_HANDLE) \ + { \ + destructor(m_device, obj, nullptr); \ + obj = VK_NULL_HANDLE; \ + } \ + } while (0) + + if (m_spin_buffer) + { + vmaDestroyBuffer(m_allocator, m_spin_buffer, m_spin_buffer_allocation); + m_spin_buffer = VK_NULL_HANDLE; + m_spin_buffer_allocation = VK_NULL_HANDLE; + } + CHECKED_DESTROY(vkDestroyPipeline, m_spin_pipeline); + CHECKED_DESTROY(vkDestroyPipelineLayout, m_spin_pipeline_layout); + CHECKED_DESTROY(vkDestroyDescriptorSetLayout, m_spin_descriptor_set_layout); + if (m_spin_descriptor_set != VK_NULL_HANDLE) + { + vkFreeDescriptorSets(m_device, m_global_descriptor_pool, 1, &m_spin_descriptor_set); + m_spin_descriptor_set = VK_NULL_HANDLE; + } + for (SpinResources& resources : m_spin_resources) + { + CHECKED_DESTROY(vkDestroySemaphore, resources.semaphore); + CHECKED_DESTROY(vkDestroyFence, resources.fence); + if (resources.command_buffer != VK_NULL_HANDLE) + { + vkFreeCommandBuffers(m_device, resources.command_pool, 1, &resources.command_buffer); + resources.command_buffer = VK_NULL_HANDLE; + } + CHECKED_DESTROY(vkDestroyCommandPool, resources.command_pool); + } +#undef CHECKED_DESTROY +} + +void GSDeviceVK::WaitForSpinCompletion(u32 index) +{ + SpinResources& resources = m_spin_resources[index]; + if (!resources.in_progress) + return; + + const VkResult res = vkWaitForFences(m_device, 1, &resources.fence, VK_TRUE, UINT64_MAX); + if (res != VK_SUCCESS) + { + LOG_VULKAN_ERROR(res, "vkWaitForFences failed: "); + m_last_submit_failed.store(true, std::memory_order_release); + return; + } + SpinCommandCompleted(index); +} + +void GSDeviceVK::SpinCommandCompleted(u32 index) +{ + SpinResources& resources = m_spin_resources[index]; + resources.in_progress = false; + const u32 timestamp_base = (index + NUM_COMMAND_BUFFERS) * 2; + std::array timestamps; + const VkResult res = + vkGetQueryPoolResults(m_device, m_timestamp_query_pool, timestamp_base, static_cast(timestamps.size()), + sizeof(timestamps), timestamps.data(), sizeof(u64), VK_QUERY_RESULT_64_BIT); + if (res == VK_SUCCESS) + { + u64 begin, end; + if (m_optional_extensions.vk_ext_calibrated_timestamps) + { + begin = timestamps[0] * m_spin_timestamp_scale + m_spin_timestamp_offset; + end = timestamps[1] * m_spin_timestamp_scale + m_spin_timestamp_offset; + } + else + { + begin = timestamps[0] * m_spin_timestamp_scale; + end = timestamps[1] * m_spin_timestamp_scale; + } + m_spin_manager.SpinCompleted(resources.cycles, begin, end); + } + else + { + LOG_VULKAN_ERROR(res, "vkGetQueryPoolResults failed: "); + } +} + +void GSDeviceVK::SubmitSpinCommand(u32 index, u32 cycles) +{ + SpinResources& resources = m_spin_resources[index]; + VkResult res; + + // Reset fence to unsignaled before starting. + if ((res = vkResetFences(m_device, 1, &resources.fence)) != VK_SUCCESS) + LOG_VULKAN_ERROR(res, "vkResetFences failed: "); + + // Reset command pools to beginning since we can re-use the memory now + if ((res = vkResetCommandPool(m_device, resources.command_pool, 0)) != VK_SUCCESS) + LOG_VULKAN_ERROR(res, "vkResetCommandPool failed: "); + + // Enable commands to be recorded to the two buffers again. + VkCommandBufferBeginInfo begin_info = {VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO}; + begin_info.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; + if ((res = vkBeginCommandBuffer(resources.command_buffer, &begin_info)) != VK_SUCCESS) + LOG_VULKAN_ERROR(res, "vkBeginCommandBuffer failed: "); + + if (!m_spin_buffer_initialized) + { + m_spin_buffer_initialized = true; + vkCmdFillBuffer(resources.command_buffer, m_spin_buffer, 0, VK_WHOLE_SIZE, 0); + VkBufferMemoryBarrier barrier = {VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER}; + barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; + barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT; + barrier.srcQueueFamilyIndex = m_spin_queue_family_index; + barrier.dstQueueFamilyIndex = m_spin_queue_family_index; + barrier.buffer = m_spin_buffer; + barrier.offset = 0; + barrier.size = VK_WHOLE_SIZE; + vkCmdPipelineBarrier(resources.command_buffer, VK_PIPELINE_STAGE_TRANSFER_BIT, + VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, nullptr, 1, &barrier, 0, nullptr); + } + + if (m_spin_queue_is_graphics_queue) + vkCmdPipelineBarrier(resources.command_buffer, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, + VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, nullptr, 0, nullptr, 0, nullptr); + + const u32 timestamp_base = (index + NUM_COMMAND_BUFFERS) * 2; + vkCmdResetQueryPool(resources.command_buffer, m_timestamp_query_pool, timestamp_base, 2); + vkCmdWriteTimestamp( + resources.command_buffer, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, m_timestamp_query_pool, timestamp_base); + vkCmdPushConstants( + resources.command_buffer, m_spin_pipeline_layout, VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(u32), &cycles); + vkCmdBindPipeline(resources.command_buffer, VK_PIPELINE_BIND_POINT_COMPUTE, m_spin_pipeline); + vkCmdBindDescriptorSets(resources.command_buffer, VK_PIPELINE_BIND_POINT_COMPUTE, m_spin_pipeline_layout, 0, 1, + &m_spin_descriptor_set, 0, nullptr); + vkCmdDispatch(resources.command_buffer, 1, 1, 1); + vkCmdWriteTimestamp( + resources.command_buffer, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, m_timestamp_query_pool, timestamp_base + 1); + + if ((res = vkEndCommandBuffer(resources.command_buffer)) != VK_SUCCESS) + LOG_VULKAN_ERROR(res, "vkEndCommandBuffer failed: "); + + VkSubmitInfo submit_info = {VK_STRUCTURE_TYPE_SUBMIT_INFO}; + submit_info.commandBufferCount = 1; + submit_info.pCommandBuffers = &resources.command_buffer; + VkPipelineStageFlags sema_waits[] = {VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT}; + if (!m_spin_queue_is_graphics_queue) + { + submit_info.waitSemaphoreCount = 1; + submit_info.pWaitSemaphores = &resources.semaphore; + submit_info.pWaitDstStageMask = sema_waits; + } + vkQueueSubmit(m_spin_queue, 1, &submit_info, resources.fence); + resources.in_progress = true; + resources.cycles = cycles; +} + +void GSDeviceVK::CalibrateSpinTimestamp() +{ + if (!m_optional_extensions.vk_ext_calibrated_timestamps) + return; + VkCalibratedTimestampInfoEXT infos[2] = { + {VK_STRUCTURE_TYPE_CALIBRATED_TIMESTAMP_INFO_EXT, nullptr, VK_TIME_DOMAIN_DEVICE_EXT}, + {VK_STRUCTURE_TYPE_CALIBRATED_TIMESTAMP_INFO_EXT, nullptr, m_calibrated_timestamp_type}, + }; + u64 timestamps[2]; + u64 maxDeviation; + constexpr u64 MAX_MAX_DEVIATION = 100000; // 100us + for (int i = 0; i < 4; i++) // 4 tries to get under MAX_MAX_DEVIATION + { + const VkResult res = vkGetCalibratedTimestampsEXT(m_device, std::size(infos), infos, timestamps, &maxDeviation); + if (res != VK_SUCCESS) + { + LOG_VULKAN_ERROR(res, "vkGetCalibratedTimestampsEXT failed: "); + return; + } + if (maxDeviation < MAX_MAX_DEVIATION) + break; + } + if (maxDeviation >= MAX_MAX_DEVIATION) + Console.Warning("vkGetCalibratedTimestampsEXT returned high max deviation of %lluus", maxDeviation / 1000); + const double gpu_time = timestamps[0] * m_spin_timestamp_scale; +#ifdef _WIN32 + const double cpu_time = timestamps[1] * m_queryperfcounter_to_ns; +#else + const double cpu_time = timestamps[1]; +#endif + m_spin_timestamp_offset = cpu_time - gpu_time; +} + +u64 GSDeviceVK::GetCPUTimestamp() +{ +#ifdef _WIN32 + LARGE_INTEGER value = {}; + QueryPerformanceCounter(&value); + return static_cast(static_cast(value.QuadPart) * m_queryperfcounter_to_ns); +#else +#ifdef CLOCK_MONOTONIC_RAW + const bool use_raw = m_calibrated_timestamp_type == VK_TIME_DOMAIN_CLOCK_MONOTONIC_RAW_EXT; + const clockid_t clock = use_raw ? CLOCK_MONOTONIC_RAW : CLOCK_MONOTONIC; +#else + const clockid_t clock = CLOCK_MONOTONIC; +#endif + timespec ts = {}; + clock_gettime(clock, &ts); + return static_cast(ts.tv_sec) * 1000000000 + ts.tv_nsec; +#endif +} + +bool GSDeviceVK::AllocatePreinitializedGPUBuffer(u32 size, VkBuffer* gpu_buffer, VmaAllocation* gpu_allocation, + VkBufferUsageFlags gpu_usage, const std::function& fill_callback) +{ + // Try to place the fixed index buffer in GPU local memory. + // Use the staging buffer to copy into it. + + const VkBufferCreateInfo cpu_bci = {VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, nullptr, 0, size, + VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_SHARING_MODE_EXCLUSIVE}; + const VmaAllocationCreateInfo cpu_aci = {VMA_ALLOCATION_CREATE_MAPPED_BIT, VMA_MEMORY_USAGE_CPU_ONLY, 0, 0}; + VkBuffer cpu_buffer; + VmaAllocation cpu_allocation; + VmaAllocationInfo cpu_ai; + VkResult res = vmaCreateBuffer(m_allocator, &cpu_bci, &cpu_aci, &cpu_buffer, &cpu_allocation, &cpu_ai); + if (res != VK_SUCCESS) + { + LOG_VULKAN_ERROR(res, "vmaCreateBuffer() for CPU expand buffer failed: "); + return false; + } + + const VkBufferCreateInfo gpu_bci = {VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, nullptr, 0, size, + VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT, VK_SHARING_MODE_EXCLUSIVE}; + const VmaAllocationCreateInfo gpu_aci = {0, VMA_MEMORY_USAGE_GPU_ONLY, 0, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT}; + VmaAllocationInfo ai; + res = vmaCreateBuffer(m_allocator, &gpu_bci, &gpu_aci, gpu_buffer, gpu_allocation, &ai); + if (res != VK_SUCCESS) + { + LOG_VULKAN_ERROR(res, "vmaCreateBuffer() for expand buffer failed: "); + vmaDestroyBuffer(m_allocator, cpu_buffer, cpu_allocation); + return false; + } + + const VkBufferCopy buf_copy = {0u, 0u, size}; + fill_callback(cpu_ai.pMappedData); + vmaFlushAllocation(m_allocator, cpu_allocation, 0, size); + vkCmdCopyBuffer(GetCurrentInitCommandBuffer(), cpu_buffer, *gpu_buffer, 1, &buf_copy); + DeferBufferDestruction(cpu_buffer, cpu_allocation); + return true; +} + + +void GSDeviceVK::GPUListToAdapterNames(std::vector* dest, VkInstance instance) +{ + GPUList gpus = EnumerateGPUs(instance); dest->clear(); dest->reserve(gpus.size()); for (auto& [gpu, name] : gpus) @@ -87,17 +1990,20 @@ static void GPUListToAdapterNames(std::vector* dest, VkInstance ins void GSDeviceVK::GetAdaptersAndFullscreenModes( std::vector* adapters, std::vector* fullscreen_modes) { - if (g_vulkan_context) + std::unique_lock lock(s_instance_mutex); + + // Device shouldn't be torn down since we have the lock. + if (g_gs_device && Vulkan::IsVulkanLibraryLoaded()) { if (adapters) - GPUListToAdapterNames(adapters, g_vulkan_context->GetVulkanInstance()); + GPUListToAdapterNames(adapters, GSDeviceVK::GetInstance()->GetVulkanInstance()); } else { if (Vulkan::LoadVulkanLibrary()) { ScopedGuard lib_guard([]() { Vulkan::UnloadVulkanLibrary(); }); - const VkInstance instance = VKContext::CreateVulkanInstance(WindowInfo(), false, false); + const VkInstance instance = CreateVulkanInstance(WindowInfo(), false, false); if (instance != VK_NULL_HANDLE) { if (Vulkan::LoadVulkanInstanceFunctions(instance)) @@ -163,7 +2069,7 @@ bool GSDeviceVK::Create() if (!CheckFeatures()) { - Console.Error("Your GPU does not support the required Vulkan features."); + Host::ReportErrorAsync("GS", "Your GPU does not support the required Vulkan features."); return false; } @@ -201,7 +2107,7 @@ bool GSDeviceVK::Create() if (!CompileConvertPipelines() || !CompilePresentPipelines() || !CompileInterlacePipelines() || !CompileMergePipelines() || - !CompilePostProcessingPipelines()) + !CompilePostProcessingPipelines() || !InitSpinResources()) { Host::ReportErrorAsync("GS", "Failed to compile utility pipelines"); return false; @@ -224,19 +2130,35 @@ bool GSDeviceVK::Create() void GSDeviceVK::Destroy() { + std::unique_lock lock(s_instance_mutex); + GSDevice::Destroy(); - if (g_vulkan_context) + EndRenderPass(); + if (GetCurrentCommandBuffer() != VK_NULL_HANDLE) { - EndRenderPass(); - ExecuteCommandBuffer(true); - DestroyResources(); - - g_vulkan_context->WaitForGPUIdle(); - m_swap_chain.reset(); - - VKContext::Destroy(); + ExecuteCommandBuffer(false); + WaitForGPUIdle(); } + + StopPresentThread(); + m_swap_chain.reset(); + + DestroySpinResources(); + DestroyResources(); + + VKShaderCache::Destroy(); + + if (m_device != VK_NULL_HANDLE) + vkDestroyDevice(m_device, nullptr); + + if (m_debug_messenger_callback != VK_NULL_HANDLE) + DisableDebugUtils(); + + if (m_instance != VK_NULL_HANDLE) + vkDestroyInstance(m_instance, nullptr); + + Vulkan::UnloadVulkanLibrary(); } bool GSDeviceVK::UpdateWindow() @@ -251,7 +2173,7 @@ bool GSDeviceVK::UpdateWindow() // make sure previous frames are presented ExecuteCommandBuffer(false); - g_vulkan_context->WaitForGPUIdle(); + WaitForGPUIdle(); // recreate surface in existing swap chain if it already exists if (m_swap_chain) @@ -265,8 +2187,7 @@ bool GSDeviceVK::UpdateWindow() m_swap_chain.reset(); } - VkSurfaceKHR surface = VKSwapChain::CreateVulkanSurface( - g_vulkan_context->GetVulkanInstance(), g_vulkan_context->GetPhysicalDevice(), &m_window_info); + VkSurfaceKHR surface = VKSwapChain::CreateVulkanSurface(m_instance, m_physical_device, &m_window_info); if (surface == VK_NULL_HANDLE) { Console.Error("Failed to create new surface for swap chain"); @@ -278,7 +2199,7 @@ bool GSDeviceVK::UpdateWindow() if (!m_swap_chain) { Console.Error("Failed to create swap chain"); - VKSwapChain::DestroyVulkanSurface(g_vulkan_context->GetVulkanInstance(), &m_window_info, surface); + VKSwapChain::DestroyVulkanSurface(m_instance, &m_window_info, surface); return false; } @@ -298,7 +2219,7 @@ void GSDeviceVK::ResizeWindow(s32 new_window_width, s32 new_window_height, float } // make sure previous frames are presented - g_vulkan_context->WaitForGPUIdle(); + WaitForGPUIdle(); if (!m_swap_chain->ResizeSwapChain(new_window_width, new_window_height, new_window_scale)) { @@ -317,32 +2238,32 @@ bool GSDeviceVK::SupportsExclusiveFullscreen() const void GSDeviceVK::DestroySurface() { - g_vulkan_context->WaitForGPUIdle(); + WaitForGPUIdle(); m_swap_chain.reset(); } std::string GSDeviceVK::GetDriverInfo() const { std::string ret; - const u32 api_version = g_vulkan_context->GetDeviceProperties().apiVersion; - const u32 driver_version = g_vulkan_context->GetDeviceProperties().driverVersion; - if (g_vulkan_context->GetOptionalExtensions().vk_khr_driver_properties) + const u32 api_version = m_device_properties.apiVersion; + const u32 driver_version = m_device_properties.driverVersion; + if (m_optional_extensions.vk_khr_driver_properties) { - const VkPhysicalDeviceDriverProperties& props = g_vulkan_context->GetDeviceDriverProperties(); + const VkPhysicalDeviceDriverProperties& props = m_device_driver_properties; ret = StringUtil::StdStringFromFormat( "Driver %u.%u.%u\nVulkan %u.%u.%u\nConformance Version %u.%u.%u.%u\n%s\n%s\n%s", VK_VERSION_MAJOR(driver_version), VK_VERSION_MINOR(driver_version), VK_VERSION_PATCH(driver_version), VK_API_VERSION_MAJOR(api_version), VK_API_VERSION_MINOR(api_version), VK_API_VERSION_PATCH(api_version), props.conformanceVersion.major, props.conformanceVersion.minor, props.conformanceVersion.subminor, props.conformanceVersion.patch, props.driverInfo, props.driverName, - g_vulkan_context->GetDeviceProperties().deviceName); + m_device_properties.deviceName); } else { ret = StringUtil::StdStringFromFormat("Driver %u.%u.%u\nVulkan %u.%u.%u\n%s", VK_VERSION_MAJOR(driver_version), VK_VERSION_MINOR(driver_version), VK_VERSION_PATCH(driver_version), VK_API_VERSION_MAJOR(api_version), VK_API_VERSION_MINOR(api_version), VK_API_VERSION_PATCH(api_version), - g_vulkan_context->GetDeviceProperties().deviceName); + m_device_properties.deviceName); } return ret; @@ -354,7 +2275,7 @@ void GSDeviceVK::SetVSync(VsyncMode mode) return; // This swap chain should not be used by the current buffer, thus safe to destroy. - g_vulkan_context->WaitForGPUIdle(); + WaitForGPUIdle(); if (!m_swap_chain->SetVSync(mode)) { // Try switching back to the old mode.. @@ -383,10 +2304,10 @@ GSDevice::PresentResult GSDeviceVK::BeginPresent(bool frame_skip) } // Previous frame needs to be presented before we can acquire the swap chain. - g_vulkan_context->WaitForPresentComplete(); + WaitForPresentComplete(); // Check if the device was lost. - if (g_vulkan_context->CheckLastSubmitFail()) + if (CheckLastSubmitFail()) return PresentResult::DeviceLost; VkResult res = m_swap_chain->AcquireNextImage(); @@ -423,7 +2344,7 @@ GSDevice::PresentResult GSDeviceVK::BeginPresent(bool frame_skip) } } - VkCommandBuffer cmdbuffer = g_vulkan_context->GetCurrentCommandBuffer(); + VkCommandBuffer cmdbuffer = GetCurrentCommandBuffer(); // Swap chain images start in undefined GSTextureVK* swap_chain_texture = m_swap_chain->GetCurrentTexture(); @@ -435,19 +2356,19 @@ GSDevice::PresentResult GSDeviceVK::BeginPresent(bool frame_skip) return GSDevice::PresentResult::FrameSkipped; const VkRenderPassBeginInfo rp = {VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO, nullptr, - g_vulkan_context->GetRenderPass(swap_chain_texture->GetVkFormat(), VK_FORMAT_UNDEFINED, + GetRenderPass(swap_chain_texture->GetVkFormat(), VK_FORMAT_UNDEFINED, VK_ATTACHMENT_LOAD_OP_CLEAR, VK_ATTACHMENT_STORE_OP_STORE), fb, {{0, 0}, {static_cast(swap_chain_texture->GetWidth()), static_cast(swap_chain_texture->GetHeight())}}, 1u, &s_present_clear_color}; - vkCmdBeginRenderPass(g_vulkan_context->GetCurrentCommandBuffer(), &rp, VK_SUBPASS_CONTENTS_INLINE); + vkCmdBeginRenderPass(GetCurrentCommandBuffer(), &rp, VK_SUBPASS_CONTENTS_INLINE); const VkViewport vp{0.0f, 0.0f, static_cast(swap_chain_texture->GetWidth()), static_cast(swap_chain_texture->GetHeight()), 0.0f, 1.0f}; const VkRect2D scissor{ {0, 0}, {static_cast(swap_chain_texture->GetWidth()), static_cast(swap_chain_texture->GetHeight())}}; - vkCmdSetViewport(g_vulkan_context->GetCurrentCommandBuffer(), 0, 1, &vp); - vkCmdSetScissor(g_vulkan_context->GetCurrentCommandBuffer(), 0, 1, &scissor); + vkCmdSetViewport(GetCurrentCommandBuffer(), 0, 1, &vp); + vkCmdSetScissor(GetCurrentCommandBuffer(), 0, 1, &scissor); return PresentResult::OK; } @@ -455,27 +2376,17 @@ void GSDeviceVK::EndPresent() { RenderImGui(); - VkCommandBuffer cmdbuffer = g_vulkan_context->GetCurrentCommandBuffer(); - vkCmdEndRenderPass(g_vulkan_context->GetCurrentCommandBuffer()); + VkCommandBuffer cmdbuffer = GetCurrentCommandBuffer(); + vkCmdEndRenderPass(cmdbuffer); m_swap_chain->GetCurrentTexture()->TransitionToLayout(cmdbuffer, GSTextureVK::Layout::PresentSrc); g_perfmon.Put(GSPerfMon::RenderPasses, 1); - g_vulkan_context->SubmitCommandBuffer(m_swap_chain.get(), !m_swap_chain->IsPresentModeSynchronizing()); - g_vulkan_context->MoveToNextCommandBuffer(); + SubmitCommandBuffer(m_swap_chain.get(), !m_swap_chain->IsPresentModeSynchronizing()); + MoveToNextCommandBuffer(); InvalidateCachedState(); } -bool GSDeviceVK::SetGPUTimingEnabled(bool enabled) -{ - return g_vulkan_context->SetEnableGPUTiming(enabled); -} - -float GSDeviceVK::GetAndResetAccumulatedGPUTime() -{ - return g_vulkan_context->GetAndResetAccumulatedGPUTime(); -} - #ifdef ENABLE_OGL_DEBUG static std::array Palette(float phase, const std::array& a, const std::array& b, const std::array& c, const std::array& d) @@ -508,7 +2419,7 @@ void GSDeviceVK::PushDebugGroup(const char* fmt, ...) buf.c_str(), {color[0], color[1], color[2], 1.0f}, }; - vkCmdBeginDebugUtilsLabelEXT(g_vulkan_context->GetCurrentCommandBuffer(), &label); + vkCmdBeginDebugUtilsLabelEXT(GetCurrentCommandBuffer(), &label); #endif } @@ -520,7 +2431,7 @@ void GSDeviceVK::PopDebugGroup() s_debug_scope_depth = (s_debug_scope_depth == 0) ? 0 : (s_debug_scope_depth - 1u); - vkCmdEndDebugUtilsLabelEXT(g_vulkan_context->GetCurrentCommandBuffer()); + vkCmdEndDebugUtilsLabelEXT(GetCurrentCommandBuffer()); #endif } @@ -549,12 +2460,13 @@ void GSDeviceVK::InsertDebugMessage(DebugMessageCategory category, const char* f const VkDebugUtilsLabelEXT label = {VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT, nullptr, buf.c_str(), {colors[static_cast(category)][0], colors[static_cast(category)][1], colors[static_cast(category)][2], 1.0f}}; - vkCmdInsertDebugUtilsLabelEXT(g_vulkan_context->GetCurrentCommandBuffer(), &label); + vkCmdInsertDebugUtilsLabelEXT(GetCurrentCommandBuffer(), &label); #endif } bool GSDeviceVK::CreateDeviceAndSwapChain() { + std::unique_lock lock(s_instance_mutex); bool enable_debug_utils = GSConfig.UseDebugDevice; bool enable_validation_layer = GSConfig.UseDebugDevice; @@ -564,21 +2476,19 @@ bool GSDeviceVK::CreateDeviceAndSwapChain() return false; } - ScopedGuard library_cleanup(&Vulkan::UnloadVulkanLibrary); - if (!AcquireWindow(true)) return false; - VkInstance instance = VKContext::CreateVulkanInstance(m_window_info, enable_debug_utils, enable_validation_layer); - if (instance == VK_NULL_HANDLE) + m_instance = CreateVulkanInstance(m_window_info, enable_debug_utils, enable_validation_layer); + if (m_instance == VK_NULL_HANDLE) { if (enable_debug_utils || enable_validation_layer) { // Try again without the validation layer. enable_debug_utils = false; enable_validation_layer = false; - instance = VKContext::CreateVulkanInstance(m_window_info, enable_debug_utils, enable_validation_layer); - if (instance == VK_NULL_HANDLE) + m_instance = CreateVulkanInstance(m_window_info, enable_debug_utils, enable_validation_layer); + if (m_instance == VK_NULL_HANDLE) { Host::ReportErrorAsync( "Error", "Failed to create Vulkan instance. Does your GPU and/or driver support Vulkan?"); @@ -589,14 +2499,13 @@ bool GSDeviceVK::CreateDeviceAndSwapChain() } } - ScopedGuard instance_cleanup = [&instance]() { vkDestroyInstance(instance, nullptr); }; - if (!Vulkan::LoadVulkanInstanceFunctions(instance)) + if (!Vulkan::LoadVulkanInstanceFunctions(m_instance)) { Console.Error("Failed to load Vulkan instance functions"); return false; } - VKContext::GPUList gpus = VKContext::EnumerateGPUs(instance); + GPUList gpus = EnumerateGPUs(m_instance); if (gpus.empty()) { Host::ReportErrorAsync("Error", "No physical devices found. Does your GPU and/or driver support Vulkan?"); @@ -606,45 +2515,73 @@ bool GSDeviceVK::CreateDeviceAndSwapChain() u32 gpu_index = 0; if (!GSConfig.Adapter.empty()) { + u32 gpu_index = 0; for (; gpu_index < static_cast(gpus.size()); gpu_index++) { Console.WriteLn(fmt::format("GPU {}: {}", gpu_index, gpus[gpu_index].second)); if (gpus[gpu_index].second == GSConfig.Adapter) + { + m_physical_device = gpus[gpu_index].first; break; + } } if (gpu_index == static_cast(gpus.size())) { Console.Warning( fmt::format("Requested GPU '{}' not found, using first ({})", GSConfig.Adapter, gpus[0].second)); - gpu_index = 0; + m_physical_device = gpus[0].first; } } else { Console.WriteLn(fmt::format("No GPU requested, using first ({})", gpus[0].second)); + m_physical_device = gpus[0].first; } + // Read device physical memory properties, we need it for allocating buffers + vkGetPhysicalDeviceProperties(m_physical_device, &m_device_properties); + + // We need this to be at least 32 byte aligned for AVX2 stores. + m_device_properties.limits.minUniformBufferOffsetAlignment = + std::max(m_device_properties.limits.minUniformBufferOffsetAlignment, static_cast(32)); + m_device_properties.limits.minTexelBufferOffsetAlignment = + std::max(m_device_properties.limits.minTexelBufferOffsetAlignment, static_cast(32)); + m_device_properties.limits.optimalBufferCopyOffsetAlignment = + std::max(m_device_properties.limits.optimalBufferCopyOffsetAlignment, static_cast(32)); + m_device_properties.limits.optimalBufferCopyRowPitchAlignment = Common::NextPow2( + std::max(m_device_properties.limits.optimalBufferCopyRowPitchAlignment, static_cast(32))); + m_device_properties.limits.bufferImageGranularity = + std::max(m_device_properties.limits.bufferImageGranularity, static_cast(32)); + + if (enable_debug_utils) + EnableDebugUtils(); + VkSurfaceKHR surface = VK_NULL_HANDLE; - ScopedGuard surface_cleanup = [&instance, &surface]() { + ScopedGuard surface_cleanup = [this, &surface]() { if (surface != VK_NULL_HANDLE) - vkDestroySurfaceKHR(instance, surface, nullptr); + vkDestroySurfaceKHR(m_instance, surface, nullptr); }; if (m_window_info.type != WindowInfo::Type::Surfaceless) { - surface = VKSwapChain::CreateVulkanSurface(instance, gpus[gpu_index].first, &m_window_info); + surface = VKSwapChain::CreateVulkanSurface(m_instance, m_physical_device, &m_window_info); if (surface == VK_NULL_HANDLE) return false; } - if (!VKContext::Create(instance, surface, gpus[gpu_index].first, !GSConfig.DisableThreadedPresentation, - enable_debug_utils, enable_validation_layer)) - { - Console.Error("Failed to create Vulkan context"); + // Attempt to create the device. + if (!CreateDevice(surface, enable_validation_layer)) return false; - } - // NOTE: This is assigned afterwards, because some platforms can modify the window info (e.g. Metal). + // And critical resources. + if (!CreateAllocator() || !CreateGlobalDescriptorPool() || !CreateCommandBuffers()) + return false; + + VKShaderCache::Create(); + + if (!GSConfig.DisableThreadedPresentation) + StartPresentThread(); + if (surface != VK_NULL_HANDLE) { m_swap_chain = VKSwapChain::Create(m_window_info, surface, m_vsync_mode, @@ -655,14 +2592,11 @@ bool GSDeviceVK::CreateDeviceAndSwapChain() return false; } + // NOTE: This is assigned afterwards, because some platforms can modify the window info (e.g. Metal). m_window_info = m_swap_chain->GetWindowInfo(); } surface_cleanup.Cancel(); - instance_cleanup.Cancel(); - library_cleanup.Cancel(); - - m_has_feedback_loop_layout = g_vulkan_context->UseFeedbackLoopLayout(); // Render a frame as soon as possible to clear out whatever was previously being displayed. if (m_window_info.type != WindowInfo::Type::Surfaceless) @@ -673,33 +2607,31 @@ bool GSDeviceVK::CreateDeviceAndSwapChain() bool GSDeviceVK::CheckFeatures() { - const VkPhysicalDeviceProperties& properties = g_vulkan_context->GetDeviceProperties(); - const VkPhysicalDeviceFeatures& features = g_vulkan_context->GetDeviceFeatures(); - const VkPhysicalDeviceLimits& limits = g_vulkan_context->GetDeviceLimits(); - const u32 vendorID = properties.vendorID; + const VkPhysicalDeviceLimits& limits = m_device_properties.limits; + const u32 vendorID = m_device_properties.vendorID; const bool isAMD = (vendorID == 0x1002 || vendorID == 0x1022); // const bool isNVIDIA = (vendorID == 0x10DE); - m_features.framebuffer_fetch = g_vulkan_context->GetOptionalExtensions().vk_ext_rasterization_order_attachment_access && !GSConfig.DisableFramebufferFetch; + m_features.framebuffer_fetch = m_optional_extensions.vk_ext_rasterization_order_attachment_access && !GSConfig.DisableFramebufferFetch; m_features.texture_barrier = GSConfig.OverrideTextureBarriers != 0; m_features.broken_point_sampler = isAMD; // geometryShader is needed because gl_PrimitiveID is part of the Geometry SPIR-V Execution Model. - m_features.primitive_id = g_vulkan_context->GetDeviceFeatures().geometryShader; + m_features.primitive_id = m_device_features.geometryShader; #ifdef __APPLE__ // On Metal (MoltenVK), primid is sometimes available, but broken on some older GPUs and MacOS versions. // Officially, it's available on GPUs that support barycentric coordinates (Newer AMD and Apple) // Unofficially, it seems to work on older Intel GPUs (but breaks other things on newer Intel GPUs, see GSMTLDeviceInfo.mm for details) - m_features.primitive_id &= g_vulkan_context->GetOptionalExtensions().vk_khr_fragment_shader_barycentric; + m_features.primitive_id &= m_optional_extensions.vk_khr_fragment_shader_barycentric; #endif m_features.prefer_new_textures = true; - m_features.provoking_vertex_last = g_vulkan_context->GetOptionalExtensions().vk_ext_provoking_vertex; - m_features.dual_source_blend = features.dualSrcBlend && !GSConfig.DisableDualSourceBlend; + m_features.provoking_vertex_last = m_optional_extensions.vk_ext_provoking_vertex; + m_features.dual_source_blend = m_device_features.dualSrcBlend && !GSConfig.DisableDualSourceBlend; m_features.clip_control = true; m_features.vs_expand = - !GSConfig.DisableVertexShaderExpand && g_vulkan_context->GetOptionalExtensions().vk_khr_shader_draw_parameters; + !GSConfig.DisableVertexShaderExpand && m_optional_extensions.vk_khr_shader_draw_parameters; if (!m_features.dual_source_blend) Console.Warning("Vulkan driver is missing dual-source blending. This will have an impact on performance."); @@ -707,13 +2639,13 @@ bool GSDeviceVK::CheckFeatures() if (!m_features.texture_barrier) Console.Warning("Texture buffers are disabled. This may break some graphical effects."); - if (!g_vulkan_context->GetOptionalExtensions().vk_ext_line_rasterization) + if (!m_optional_extensions.vk_ext_line_rasterization) Console.WriteLn("VK_EXT_line_rasterization or the BRESENHAM mode is not supported, this may cause rendering inaccuracies."); // Test for D32S8 support. { VkFormatProperties props = {}; - vkGetPhysicalDeviceFormatProperties(g_vulkan_context->GetPhysicalDevice(), VK_FORMAT_D32_SFLOAT_S8_UINT, &props); + vkGetPhysicalDeviceFormatProperties(m_physical_device, VK_FORMAT_D32_SFLOAT_S8_UINT, &props); m_features.stencil_buffer = ((props.optimalTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) != 0); } @@ -729,9 +2661,9 @@ bool GSDeviceVK::CheckFeatures() // whether we can do point/line expand depends on the range of the device const float f_upscale = static_cast(GSConfig.UpscaleMultiplier); m_features.point_expand = - (features.largePoints && limits.pointSizeRange[0] <= f_upscale && limits.pointSizeRange[1] >= f_upscale); + (m_device_features.largePoints && limits.pointSizeRange[0] <= f_upscale && limits.pointSizeRange[1] >= f_upscale); m_features.line_expand = - (features.wideLines && limits.lineWidthRange[0] <= f_upscale && limits.lineWidthRange[1] >= f_upscale); + (m_device_features.wideLines && limits.lineWidthRange[0] <= f_upscale && limits.lineWidthRange[1] >= f_upscale); DevCon.WriteLn("Optional features:%s%s%s%s%s%s", m_features.primitive_id ? " primitive_id" : "", m_features.texture_barrier ? " texture_barrier" : "", m_features.framebuffer_fetch ? " framebuffer_fetch" : "", @@ -750,7 +2682,7 @@ bool GSDeviceVK::CheckFeatures() (VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT | VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT); VkFormatProperties props = {}; - vkGetPhysicalDeviceFormatProperties(g_vulkan_context->GetPhysicalDevice(), vkfmt, &props); + vkGetPhysicalDeviceFormatProperties(m_physical_device, vkfmt, &props); if ((props.optimalTilingFeatures & bits) != bits) { Host::ReportFormattedErrorAsync("Vulkan Renderer Unavailable", @@ -760,8 +2692,8 @@ bool GSDeviceVK::CheckFeatures() } } - m_features.dxt_textures = g_vulkan_context->GetDeviceFeatures().textureCompressionBC; - m_features.bptc_textures = g_vulkan_context->GetDeviceFeatures().textureCompressionBC; + m_features.dxt_textures = m_device_features.textureCompressionBC; + m_features.bptc_textures = m_device_features.textureCompressionBC; if (!m_features.texture_barrier && !m_features.stencil_buffer) { @@ -776,20 +2708,20 @@ bool GSDeviceVK::CheckFeatures() void GSDeviceVK::DrawPrimitive() { g_perfmon.Put(GSPerfMon::DrawCalls, 1); - vkCmdDraw(g_vulkan_context->GetCurrentCommandBuffer(), m_vertex.count, 1, m_vertex.start, 0); + vkCmdDraw(GetCurrentCommandBuffer(), m_vertex.count, 1, m_vertex.start, 0); } void GSDeviceVK::DrawIndexedPrimitive() { g_perfmon.Put(GSPerfMon::DrawCalls, 1); - vkCmdDrawIndexed(g_vulkan_context->GetCurrentCommandBuffer(), m_index.count, 1, m_index.start, m_vertex.start, 0); + vkCmdDrawIndexed(GetCurrentCommandBuffer(), m_index.count, 1, m_index.start, m_vertex.start, 0); } void GSDeviceVK::DrawIndexedPrimitive(int offset, int count) { ASSERT(offset + count <= (int)m_index.count); g_perfmon.Put(GSPerfMon::DrawCalls, 1); - vkCmdDrawIndexed(g_vulkan_context->GetCurrentCommandBuffer(), count, 1, m_index.start + offset, m_vertex.start, 0); + vkCmdDrawIndexed(GetCurrentCommandBuffer(), count, 1, m_index.start + offset, m_vertex.start, 0); } VkFormat GSDeviceVK::LookupNativeFormat(GSTexture::Format format) const @@ -816,8 +2748,8 @@ VkFormat GSDeviceVK::LookupNativeFormat(GSTexture::Format format) const GSTexture* GSDeviceVK::CreateSurface(GSTexture::Type type, int width, int height, int levels, GSTexture::Format format) { - const u32 clamped_width = static_cast(std::clamp(width, 1, g_vulkan_context->GetMaxImageDimension2D())); - const u32 clamped_height = static_cast(std::clamp(height, 1, g_vulkan_context->GetMaxImageDimension2D())); + const u32 clamped_width = static_cast(std::clamp(width, 1, m_device_properties.limits.maxImageDimension2D)); + const u32 clamped_height = static_cast(std::clamp(height, 1, m_device_properties.limits.maxImageDimension2D)); std::unique_ptr tex(GSTextureVK::Create(type, format, clamped_width, clamped_height, levels)); if (!tex) @@ -889,7 +2821,7 @@ void GSDeviceVK::CopyRect(GSTexture* sTex, GSTexture* dTex, const GSVector4i& r, ca.colorAttachment = 0; const VkClearRect cr = { {{0, 0}, {static_cast(r.width()), static_cast(r.height())}}, 0u, 1u }; - vkCmdClearAttachments(g_vulkan_context->GetCurrentCommandBuffer(), 1, &ca, 1, &cr); + vkCmdClearAttachments(GetCurrentCommandBuffer(), 1, &ca, 1, &cr); return; } @@ -911,14 +2843,14 @@ void GSDeviceVK::CopyRect(GSTexture* sTex, GSTexture* dTex, const GSVector4i& r, EndRenderPass(); - sTexVK->SetUsedThisCommandBuffer(); - dTexVK->SetUsedThisCommandBuffer(); + sTexVK->SetUseFenceCounter(GetCurrentFenceCounter()); + dTexVK->SetUseFenceCounter(GetCurrentFenceCounter()); sTexVK->TransitionToLayout( (dTexVK == sTexVK) ? GSTextureVK::Layout::TransferSelf : GSTextureVK::Layout::TransferSrc); dTexVK->TransitionToLayout( (dTexVK == sTexVK) ? GSTextureVK::Layout::TransferSelf : GSTextureVK::Layout::TransferDst); - vkCmdCopyImage(g_vulkan_context->GetCurrentCommandBuffer(), sTexVK->GetImage(), + vkCmdCopyImage(GetCurrentCommandBuffer(), sTexVK->GetImage(), sTexVK->GetVkLayout(), dTexVK->GetImage(), dTexVK->GetVkLayout(), 1, &ic); dTexVK->SetState(GSTexture::State::Dirty); @@ -1105,7 +3037,7 @@ void GSDeviceVK::BeginRenderPassForStretchRect( else { // integer formats, etc - const VkRenderPass rp = g_vulkan_context->GetRenderPass(dTex->GetVkFormat(), VK_FORMAT_UNDEFINED, + const VkRenderPass rp = GetRenderPass(dTex->GetVkFormat(), VK_FORMAT_UNDEFINED, load_op, VK_ATTACHMENT_STORE_OP_STORE, VK_ATTACHMENT_LOAD_OP_DONT_CARE, VK_ATTACHMENT_STORE_OP_DONT_CARE); if (load_op == VK_ATTACHMENT_LOAD_OP_CLEAR) { @@ -1199,7 +3131,7 @@ void GSDeviceVK::BlitRect(GSTexture* sTex, const GSVector4i& sRect, u32 sLevel, const VkImageBlit ib{{aspect, sLevel, 0u, 1u}, {{sRect.left, sRect.top, 0}, {sRect.right, sRect.bottom, 1}}, {aspect, dLevel, 0u, 1u}, {{dRect.left, dRect.top, 0}, {dRect.right, dRect.bottom, 1}}}; - vkCmdBlitImage(g_vulkan_context->GetCurrentCommandBuffer(), sTexVK->GetImage(), + vkCmdBlitImage(GetCurrentCommandBuffer(), sTexVK->GetImage(), VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, dTexVK->GetImage(), VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &ib, linear ? VK_FILTER_LINEAR : VK_FILTER_NEAREST); } @@ -1511,7 +3443,7 @@ void GSDeviceVK::OMSetRenderTargets(GSTexture* rt, GSTexture* ds, const GSVector { const GSVector2i size = vkRt ? vkRt->GetSize() : vkDs->GetSize(); const VkClearRect cr = {{{0, 0}, {static_cast(size.x), static_cast(size.y)}}, 0u, 1u}; - vkCmdClearAttachments(g_vulkan_context->GetCurrentCommandBuffer(), num_ca, cas.data(), 1, &cr); + vkCmdClearAttachments(GetCurrentCommandBuffer(), num_ca, cas.data(), 1, &cr); } } @@ -1526,7 +3458,7 @@ void GSDeviceVK::OMSetRenderTargets(GSTexture* rt, GSTexture* ds, const GSVector // NVIDIA drivers appear to return random garbage when sampling the RT via a feedback loop, if the load op for // the render pass is CLEAR. Using vkCmdClearAttachments() doesn't work, so we have to clear the image instead. if (feedback_loop & FeedbackLoopFlag_ReadAndWriteRT && vkRt->GetState() == GSTexture::State::Cleared && - g_vulkan_context->IsDeviceNVIDIA()) + IsDeviceNVIDIA()) { vkRt->CommitClear(); } @@ -1561,7 +3493,7 @@ VkSampler GSDeviceVK::GetSampler(GSHWDrawConfig::SamplerSelector ss) if (it != m_samplers.end()) return it->second; - const bool aniso = (ss.aniso && GSConfig.MaxAnisotropy > 1 && g_vulkan_context->GetDeviceFeatures().samplerAnisotropy); + const bool aniso = (ss.aniso && GSConfig.MaxAnisotropy > 1 && m_device_features.samplerAnisotropy); // See https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/VkSamplerCreateInfo.html#_description // for the reasoning behind 0.25f here. @@ -1586,7 +3518,7 @@ VkSampler GSDeviceVK::GetSampler(GSHWDrawConfig::SamplerSelector ss) VK_FALSE // unnormalized coordinates }; VkSampler sampler = VK_NULL_HANDLE; - VkResult res = vkCreateSampler(g_vulkan_context->GetDevice(), &ci, nullptr, &sampler); + VkResult res = vkCreateSampler(m_device, &ci, nullptr, &sampler); if (res != VK_SUCCESS) LOG_VULKAN_ERROR(res, "vkCreateSampler() failed: "); @@ -1596,8 +3528,12 @@ VkSampler GSDeviceVK::GetSampler(GSHWDrawConfig::SamplerSelector ss) void GSDeviceVK::ClearSamplerCache() { + ExecuteCommandBuffer(true); for (const auto& it : m_samplers) - g_vulkan_context->DeferSamplerDestruction(it.second); + { + if (it.second != VK_NULL_HANDLE) + vkDestroySampler(m_device, it.second, nullptr); + } m_samplers.clear(); m_point_sampler = GetSampler(GSHWDrawConfig::SamplerSelector::Point()); m_linear_sampler = GetSampler(GSHWDrawConfig::SamplerSelector::Linear()); @@ -1624,7 +3560,7 @@ static void AddShaderHeader(std::stringstream& ss) ss << "#define DISABLE_TEXTURE_BARRIER 1\n"; if (!features.dual_source_blend) ss << "#define DISABLE_DUAL_SOURCE 1\n"; - if (features.texture_barrier && g_vulkan_context->UseFeedbackLoopLayout()) + if (features.texture_barrier && GSDeviceVK::GetInstance()->UseFeedbackLoopLayout()) ss << "#define HAS_FEEDBACK_LOOP_LAYOUT 1\n"; } @@ -1685,14 +3621,14 @@ bool GSDeviceVK::CreateNullTexture() if (!m_null_texture) return false; - const VkCommandBuffer cmdbuf = g_vulkan_context->GetCurrentCommandBuffer(); + const VkCommandBuffer cmdbuf = GetCurrentCommandBuffer(); const VkImageSubresourceRange srr{VK_IMAGE_ASPECT_COLOR_BIT, 0u, 1u, 0u, 1u}; const VkClearColorValue ccv{}; m_null_texture->TransitionToLayout(cmdbuf, GSTextureVK::Layout::ClearDst); vkCmdClearColorImage(cmdbuf, m_null_texture->GetImage(), VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &ccv, 1, &srr); m_null_texture->TransitionToLayout(cmdbuf, GSTextureVK::Layout::General); - Vulkan::SetObjectName(g_vulkan_context->GetDevice(), m_null_texture->GetImage(), "Null texture"); - Vulkan::SetObjectName(g_vulkan_context->GetDevice(), m_null_texture->GetView(), "Null texture view"); + Vulkan::SetObjectName(m_device, m_null_texture->GetImage(), "Null texture"); + Vulkan::SetObjectName(m_device, m_null_texture->GetView(), "Null texture view"); return true; } @@ -1725,9 +3661,15 @@ bool GSDeviceVK::CreateBuffers() return false; } + if (!m_texture_stream_buffer.Create(VK_BUFFER_USAGE_TRANSFER_SRC_BIT, TEXTURE_BUFFER_SIZE)) + { + Host::ReportErrorAsync("GS", "Failed to allocate texture upload buffer"); + return false; + } + SetVertexBuffer(m_vertex_stream_buffer.GetBuffer(), 0); - if (!g_vulkan_context->AllocatePreinitializedGPUBuffer(EXPAND_BUFFER_SIZE, &m_expand_index_buffer, + if (!AllocatePreinitializedGPUBuffer(EXPAND_BUFFER_SIZE, &m_expand_index_buffer, &m_expand_index_buffer_allocation, VK_BUFFER_USAGE_INDEX_BUFFER_BIT, &GSDevice::GenerateExpansionIndexBuffer)) { @@ -1740,7 +3682,7 @@ bool GSDeviceVK::CreateBuffers() bool GSDeviceVK::CreatePipelineLayouts() { - VkDevice dev = g_vulkan_context->GetDevice(); + VkDevice dev = m_device; Vulkan::DescriptorSetLayoutBuilder dslb; Vulkan::PipelineLayoutBuilder plb; @@ -1776,7 +3718,7 @@ bool GSDeviceVK::CreatePipelineLayouts() return false; Vulkan::SetObjectName(dev, m_tfx_sampler_ds_layout, "TFX sampler descriptor layout"); dslb.AddBinding(0, - (m_features.texture_barrier && !m_has_feedback_loop_layout) ? VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT : + (m_features.texture_barrier && !UseFeedbackLoopLayout()) ? VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT : VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 1, VK_SHADER_STAGE_FRAGMENT_BIT); dslb.AddBinding(1, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 1, VK_SHADER_STAGE_FRAGMENT_BIT); @@ -1798,7 +3740,7 @@ bool GSDeviceVK::CreateRenderPasses() #define GET(dest, rt, depth, fbl, dsp, opa, opb, opc) \ do \ { \ - dest = g_vulkan_context->GetRenderPass( \ + dest = GetRenderPass( \ (rt), (depth), ((rt) != VK_FORMAT_UNDEFINED) ? (opa) : VK_ATTACHMENT_LOAD_OP_DONT_CARE, /* color load */ \ ((rt) != VK_FORMAT_UNDEFINED) ? VK_ATTACHMENT_STORE_OP_STORE : \ VK_ATTACHMENT_STORE_OP_DONT_CARE, /* color store */ \ @@ -1868,7 +3810,7 @@ bool GSDeviceVK::CreateRenderPasses() GET(m_utility_depth_render_pass_discard, VK_FORMAT_UNDEFINED, depth_format, false, false, VK_ATTACHMENT_LOAD_OP_DONT_CARE, VK_ATTACHMENT_LOAD_OP_DONT_CARE, VK_ATTACHMENT_LOAD_OP_DONT_CARE); - m_date_setup_render_pass = g_vulkan_context->GetRenderPass(VK_FORMAT_UNDEFINED, depth_format, + m_date_setup_render_pass = GetRenderPass(VK_FORMAT_UNDEFINED, depth_format, VK_ATTACHMENT_LOAD_OP_LOAD, VK_ATTACHMENT_STORE_OP_STORE, VK_ATTACHMENT_LOAD_OP_LOAD, VK_ATTACHMENT_STORE_OP_STORE, m_features.stencil_buffer ? VK_ATTACHMENT_LOAD_OP_CLEAR : VK_ATTACHMENT_LOAD_OP_DONT_CARE, m_features.stencil_buffer ? VK_ATTACHMENT_STORE_OP_STORE : VK_ATTACHMENT_STORE_OP_DONT_CARE); @@ -1892,7 +3834,7 @@ bool GSDeviceVK::CompileConvertPipelines() VkShaderModule vs = GetUtilityVertexShader(*shader); if (vs == VK_NULL_HANDLE) return false; - ScopedGuard vs_guard([&vs]() { Vulkan::SafeDestroyShaderModule(vs); }); + ScopedGuard vs_guard([this, &vs]() { vkDestroyShaderModule(m_device, vs, nullptr); }); Vulkan::GraphicsPipelineBuilder gpb; SetPipelineProvokingVertex(m_features, gpb); @@ -1917,13 +3859,13 @@ bool GSDeviceVK::CompileConvertPipelines() case ShaderConvert::RGBA8_TO_16_BITS: case ShaderConvert::FLOAT32_TO_16_BITS: { - rp = g_vulkan_context->GetRenderPass(LookupNativeFormat(GSTexture::Format::UInt16), + rp = GetRenderPass(LookupNativeFormat(GSTexture::Format::UInt16), VK_FORMAT_UNDEFINED, VK_ATTACHMENT_LOAD_OP_DONT_CARE); } break; case ShaderConvert::FLOAT32_TO_32_BITS: { - rp = g_vulkan_context->GetRenderPass(LookupNativeFormat(GSTexture::Format::UInt32), + rp = GetRenderPass(LookupNativeFormat(GSTexture::Format::UInt32), VK_FORMAT_UNDEFINED, VK_ATTACHMENT_LOAD_OP_DONT_CARE); } break; @@ -1935,7 +3877,7 @@ bool GSDeviceVK::CompileConvertPipelines() break; default: { - rp = g_vulkan_context->GetRenderPass( + rp = GetRenderPass( LookupNativeFormat(depth ? GSTexture::Format::Invalid : GSTexture::Format::Color), LookupNativeFormat( depth ? GSTexture::Format::DepthStencil : GSTexture::Format::Invalid), @@ -1965,15 +3907,15 @@ bool GSDeviceVK::CompileConvertPipelines() if (ps == VK_NULL_HANDLE) return false; - ScopedGuard ps_guard([&ps]() { Vulkan::SafeDestroyShaderModule(ps); }); + ScopedGuard ps_guard([this, &ps]() { vkDestroyShaderModule(m_device, ps, nullptr); }); gpb.SetFragmentShader(ps); m_convert[index] = - gpb.Create(g_vulkan_context->GetDevice(), g_vulkan_shader_cache->GetPipelineCache(true), false); + gpb.Create(m_device, g_vulkan_shader_cache->GetPipelineCache(true), false); if (!m_convert[index]) return false; - Vulkan::SetObjectName(g_vulkan_context->GetDevice(), m_convert[index], "Convert pipeline %d", i); + Vulkan::SetObjectName(m_device, m_convert[index], "Convert pipeline %d", i); if (i == ShaderConvert::COPY) { @@ -1986,11 +3928,11 @@ bool GSDeviceVK::CompileConvertPipelines() gpb.SetBlendAttachment(0, false, VK_BLEND_FACTOR_ONE, VK_BLEND_FACTOR_ZERO, VK_BLEND_OP_ADD, VK_BLEND_FACTOR_ONE, VK_BLEND_FACTOR_ZERO, VK_BLEND_OP_ADD, static_cast(i)); m_color_copy[i] = - gpb.Create(g_vulkan_context->GetDevice(), g_vulkan_shader_cache->GetPipelineCache(true), false); + gpb.Create(m_device, g_vulkan_shader_cache->GetPipelineCache(true), false); if (!m_color_copy[i]) return false; - Vulkan::SetObjectName(g_vulkan_context->GetDevice(), m_color_copy[i], + Vulkan::SetObjectName(m_device, m_color_copy[i], "Color copy pipeline (r=%u, g=%u, b=%u, a=%u)", i & 1u, (i >> 1) & 1u, (i >> 2) & 1u, (i >> 3) & 1u); } @@ -2009,11 +3951,11 @@ bool GSDeviceVK::CompileConvertPipelines() GetTFXRenderPass(true, ds != 0, is_setup, DATE_RENDER_PASS_NONE, fbl != 0, false, VK_ATTACHMENT_LOAD_OP_DONT_CARE, VK_ATTACHMENT_LOAD_OP_DONT_CARE), 0); - arr[ds][fbl] = gpb.Create(g_vulkan_context->GetDevice(), g_vulkan_shader_cache->GetPipelineCache(true), false); + arr[ds][fbl] = gpb.Create(m_device, g_vulkan_shader_cache->GetPipelineCache(true), false); if (!arr[ds][fbl]) return false; - Vulkan::SetObjectName(g_vulkan_context->GetDevice(), arr[ds][fbl], + Vulkan::SetObjectName(m_device, arr[ds][fbl], "HDR %s/copy pipeline (ds=%u, fbl=%u)", is_setup ? "setup" : "finish", i, ds, fbl); } } @@ -2026,7 +3968,7 @@ bool GSDeviceVK::CompileConvertPipelines() for (u32 clear = 0; clear < 2; clear++) { m_date_image_setup_render_passes[ds][clear] = - g_vulkan_context->GetRenderPass(LookupNativeFormat(GSTexture::Format::PrimID), + GetRenderPass(LookupNativeFormat(GSTexture::Format::PrimID), ds ? LookupNativeFormat(GSTexture::Format::DepthStencil) : VK_FORMAT_UNDEFINED, VK_ATTACHMENT_LOAD_OP_DONT_CARE, VK_ATTACHMENT_STORE_OP_STORE, ds ? (clear ? VK_ATTACHMENT_LOAD_OP_CLEAR : VK_ATTACHMENT_LOAD_OP_LOAD) : VK_ATTACHMENT_LOAD_OP_DONT_CARE, @@ -2041,7 +3983,7 @@ bool GSDeviceVK::CompileConvertPipelines() if (ps == VK_NULL_HANDLE) return false; - ScopedGuard ps_guard([&ps]() { Vulkan::SafeDestroyShaderModule(ps); }); + ScopedGuard ps_guard([this, &ps]() { vkDestroyShaderModule(m_device, ps, nullptr); }); gpb.SetPipelineLayout(m_utility_pipeline_layout); gpb.SetFragmentShader(ps); gpb.SetNoDepthTestState(); @@ -2054,11 +3996,11 @@ bool GSDeviceVK::CompileConvertPipelines() { gpb.SetRenderPass(m_date_image_setup_render_passes[ds][0], 0); m_date_image_setup_pipelines[ds][datm] = - gpb.Create(g_vulkan_context->GetDevice(), g_vulkan_shader_cache->GetPipelineCache(true), false); + gpb.Create(m_device, g_vulkan_shader_cache->GetPipelineCache(true), false); if (!m_date_image_setup_pipelines[ds][datm]) return false; - Vulkan::SetObjectName(g_vulkan_context->GetDevice(), m_date_image_setup_pipelines[ds][datm], + Vulkan::SetObjectName(m_device, m_date_image_setup_pipelines[ds][datm], "DATE image clear pipeline (ds=%u, datm=%u)", ds, datm); } } @@ -2069,7 +4011,7 @@ bool GSDeviceVK::CompileConvertPipelines() bool GSDeviceVK::CompilePresentPipelines() { // we may not have a swap chain if running in headless mode. - m_swap_chain_render_pass = g_vulkan_context->GetRenderPass( + m_swap_chain_render_pass = GetRenderPass( m_swap_chain ? m_swap_chain->GetTextureFormat() : VK_FORMAT_R8G8B8A8_UNORM, VK_FORMAT_UNDEFINED); if (m_swap_chain_render_pass == VK_NULL_HANDLE) return false; @@ -2084,7 +4026,7 @@ bool GSDeviceVK::CompilePresentPipelines() VkShaderModule vs = GetUtilityVertexShader(*shader); if (vs == VK_NULL_HANDLE) return false; - ScopedGuard vs_guard([&vs]() { Vulkan::SafeDestroyShaderModule(vs); }); + ScopedGuard vs_guard([this, &vs]() { vkDestroyShaderModule(m_device, vs, nullptr); }); Vulkan::GraphicsPipelineBuilder gpb; SetPipelineProvokingVertex(m_features, gpb); @@ -2109,15 +4051,15 @@ bool GSDeviceVK::CompilePresentPipelines() if (ps == VK_NULL_HANDLE) return false; - ScopedGuard ps_guard([&ps]() { Vulkan::SafeDestroyShaderModule(ps); }); + ScopedGuard ps_guard([this, &ps]() { vkDestroyShaderModule(m_device, ps, nullptr); }); gpb.SetFragmentShader(ps); m_present[index] = - gpb.Create(g_vulkan_context->GetDevice(), g_vulkan_shader_cache->GetPipelineCache(true), false); + gpb.Create(m_device, g_vulkan_shader_cache->GetPipelineCache(true), false); if (!m_present[index]) return false; - Vulkan::SetObjectName(g_vulkan_context->GetDevice(), m_present[index], "Present pipeline %d", i); + Vulkan::SetObjectName(m_device, m_present[index], "Present pipeline %d", i); } return true; @@ -2133,7 +4075,7 @@ bool GSDeviceVK::CompileInterlacePipelines() return false; } - VkRenderPass rp = g_vulkan_context->GetRenderPass( + VkRenderPass rp = GetRenderPass( LookupNativeFormat(GSTexture::Format::Color), VK_FORMAT_UNDEFINED, VK_ATTACHMENT_LOAD_OP_LOAD); if (!rp) return false; @@ -2141,7 +4083,7 @@ bool GSDeviceVK::CompileInterlacePipelines() VkShaderModule vs = GetUtilityVertexShader(*shader); if (vs == VK_NULL_HANDLE) return false; - ScopedGuard vs_guard([&vs]() { Vulkan::SafeDestroyShaderModule(vs); }); + ScopedGuard vs_guard([this, &vs]() { vkDestroyShaderModule(m_device, vs, nullptr); }); Vulkan::GraphicsPipelineBuilder gpb; SetPipelineProvokingVertex(m_features, gpb); @@ -2165,12 +4107,12 @@ bool GSDeviceVK::CompileInterlacePipelines() gpb.SetFragmentShader(ps); m_interlace[i] = - gpb.Create(g_vulkan_context->GetDevice(), g_vulkan_shader_cache->GetPipelineCache(true), false); - Vulkan::SafeDestroyShaderModule(ps); + gpb.Create(m_device, g_vulkan_shader_cache->GetPipelineCache(true), false); + vkDestroyShaderModule(m_device, ps, nullptr); if (!m_interlace[i]) return false; - Vulkan::SetObjectName(g_vulkan_context->GetDevice(), m_convert[i], "Interlace pipeline %d", i); + Vulkan::SetObjectName(m_device, m_convert[i], "Interlace pipeline %d", i); } return true; @@ -2185,7 +4127,7 @@ bool GSDeviceVK::CompileMergePipelines() return false; } - VkRenderPass rp = g_vulkan_context->GetRenderPass( + VkRenderPass rp = GetRenderPass( LookupNativeFormat(GSTexture::Format::Color), VK_FORMAT_UNDEFINED, VK_ATTACHMENT_LOAD_OP_LOAD); if (!rp) return false; @@ -2193,7 +4135,7 @@ bool GSDeviceVK::CompileMergePipelines() VkShaderModule vs = GetUtilityVertexShader(*shader); if (vs == VK_NULL_HANDLE) return false; - ScopedGuard vs_guard([&vs]() { Vulkan::SafeDestroyShaderModule(vs); }); + ScopedGuard vs_guard([this, &vs]() { vkDestroyShaderModule(m_device, vs, nullptr); }); Vulkan::GraphicsPipelineBuilder gpb; SetPipelineProvokingVertex(m_features, gpb); @@ -2217,12 +4159,12 @@ bool GSDeviceVK::CompileMergePipelines() gpb.SetBlendAttachment(0, true, VK_BLEND_FACTOR_SRC_ALPHA, VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA, VK_BLEND_OP_ADD, VK_BLEND_FACTOR_ONE, VK_BLEND_FACTOR_ZERO, VK_BLEND_OP_ADD); - m_merge[i] = gpb.Create(g_vulkan_context->GetDevice(), g_vulkan_shader_cache->GetPipelineCache(true), false); - Vulkan::SafeDestroyShaderModule(ps); + m_merge[i] = gpb.Create(m_device, g_vulkan_shader_cache->GetPipelineCache(true), false); + vkDestroyShaderModule(m_device, ps, nullptr); if (!m_merge[i]) return false; - Vulkan::SetObjectName(g_vulkan_context->GetDevice(), m_convert[i], "Merge pipeline %d", i); + Vulkan::SetObjectName(m_device, m_convert[i], "Merge pipeline %d", i); } return true; @@ -2230,7 +4172,7 @@ bool GSDeviceVK::CompileMergePipelines() bool GSDeviceVK::CompilePostProcessingPipelines() { - VkRenderPass rp = g_vulkan_context->GetRenderPass( + VkRenderPass rp = GetRenderPass( LookupNativeFormat(GSTexture::Format::Color), VK_FORMAT_UNDEFINED, VK_ATTACHMENT_LOAD_OP_LOAD); if (!rp) return false; @@ -2265,22 +4207,21 @@ bool GSDeviceVK::CompilePostProcessingPipelines() const std::string psource = "#define FXAA_GLSL_VK 1\n" + *pshader; VkShaderModule vs = GetUtilityVertexShader(*vshader); - VkShaderModule ps = GetUtilityFragmentShader(psource, "ps_main"); - ScopedGuard shader_guard([&vs, &ps]() { - Vulkan::SafeDestroyShaderModule(vs); - Vulkan::SafeDestroyShaderModule(ps); - }); - if (vs == VK_NULL_HANDLE || ps == VK_NULL_HANDLE) + if (vs == VK_NULL_HANDLE) return false; - + ScopedGuard vs_guard([this, &vs]() { vkDestroyShaderModule(m_device, vs, nullptr); }); + VkShaderModule ps = GetUtilityFragmentShader(psource, "ps_main"); + if (ps == VK_NULL_HANDLE) + return false; + ScopedGuard ps_guard([this, &ps]() { vkDestroyShaderModule(m_device, ps, nullptr); }); gpb.SetVertexShader(vs); gpb.SetFragmentShader(ps); - m_fxaa_pipeline = gpb.Create(g_vulkan_context->GetDevice(), g_vulkan_shader_cache->GetPipelineCache(true), false); + m_fxaa_pipeline = gpb.Create(m_device, g_vulkan_shader_cache->GetPipelineCache(true), false); if (!m_fxaa_pipeline) return false; - Vulkan::SetObjectName(g_vulkan_context->GetDevice(), m_fxaa_pipeline, "FXAA pipeline"); + Vulkan::SetObjectName(m_device, m_fxaa_pipeline, "FXAA pipeline"); } { @@ -2292,22 +4233,23 @@ bool GSDeviceVK::CompilePostProcessingPipelines() } VkShaderModule vs = GetUtilityVertexShader(*shader); + ScopedGuard vs_guard([this, &vs]() { vkDestroyShaderModule(m_device, vs, nullptr); }); + if (vs == VK_NULL_HANDLE) + return false; + VkShaderModule ps = GetUtilityFragmentShader(*shader); - ScopedGuard shader_guard([&vs, &ps]() { - Vulkan::SafeDestroyShaderModule(vs); - Vulkan::SafeDestroyShaderModule(ps); - }); - if (vs == VK_NULL_HANDLE || ps == VK_NULL_HANDLE) + ScopedGuard ps_guard([this, &ps]() { vkDestroyShaderModule(m_device, ps, nullptr); }); + if (ps == VK_NULL_HANDLE) return false; gpb.SetVertexShader(vs); gpb.SetFragmentShader(ps); - m_shadeboost_pipeline = gpb.Create(g_vulkan_context->GetDevice(), g_vulkan_shader_cache->GetPipelineCache(true), false); + m_shadeboost_pipeline = gpb.Create(m_device, g_vulkan_shader_cache->GetPipelineCache(true), false); if (!m_shadeboost_pipeline) return false; - Vulkan::SetObjectName(g_vulkan_context->GetDevice(), m_shadeboost_pipeline, "Shadeboost pipeline"); + Vulkan::SetObjectName(m_device, m_shadeboost_pipeline, "Shadeboost pipeline"); } return true; @@ -2315,7 +4257,7 @@ bool GSDeviceVK::CompilePostProcessingPipelines() bool GSDeviceVK::CompileCASPipelines() { - VkDevice dev = g_vulkan_context->GetDevice(); + VkDevice dev = m_device; Vulkan::DescriptorSetLayoutBuilder dslb; Vulkan::PipelineLayoutBuilder plb; @@ -2337,7 +4279,7 @@ bool GSDeviceVK::CompileCASPipelines() return false; VkShaderModule mod = g_vulkan_shader_cache->GetComputeShader(cas_source->c_str()); - ScopedGuard mod_guard = [&mod]() { Vulkan::SafeDestroyShaderModule(mod); }; + ScopedGuard mod_guard = [this, &mod]() { vkDestroyShaderModule(m_device, mod, nullptr); }; if (mod == VK_NULL_HANDLE) return false; @@ -2371,7 +4313,7 @@ bool GSDeviceVK::CompileImGuiPipeline() Console.Error("Failed to compile ImGui vertex shader"); return false; } - ScopedGuard vs_guard([&vs]() { Vulkan::SafeDestroyShaderModule(vs); }); + ScopedGuard vs_guard([this, &vs]() { vkDestroyShaderModule(m_device, vs, nullptr); }); VkShaderModule ps = GetUtilityFragmentShader(glsl.value(), "ps_main"); if (ps == VK_NULL_HANDLE) @@ -2379,7 +4321,7 @@ bool GSDeviceVK::CompileImGuiPipeline() Console.Error("Failed to compile ImGui pixel shader"); return false; } - ScopedGuard ps_guard([&ps]() { Vulkan::SafeDestroyShaderModule(ps); }); + ScopedGuard ps_guard([this, &ps]() { vkDestroyShaderModule(m_device, ps, nullptr); }); Vulkan::GraphicsPipelineBuilder gpb; SetPipelineProvokingVertex(m_features, gpb); @@ -2400,14 +4342,14 @@ bool GSDeviceVK::CompileImGuiPipeline() gpb.AddDynamicState(VK_DYNAMIC_STATE_BLEND_CONSTANTS); gpb.AddDynamicState(VK_DYNAMIC_STATE_LINE_WIDTH); - m_imgui_pipeline = gpb.Create(g_vulkan_context->GetDevice(), g_vulkan_shader_cache->GetPipelineCache(), false); + m_imgui_pipeline = gpb.Create(m_device, g_vulkan_shader_cache->GetPipelineCache(), false); if (!m_imgui_pipeline) { Console.Error("Failed to compile ImGui pipeline"); return false; } - Vulkan::SetObjectName(g_vulkan_context->GetDevice(), m_imgui_pipeline, "ImGui pipeline"); + Vulkan::SetObjectName(m_device, m_imgui_pipeline, "ImGui pipeline"); return true; } @@ -2478,7 +4420,7 @@ void GSDeviceVK::RenderImGui() if (ApplyUtilityState()) { - vkCmdDrawIndexed(g_vulkan_context->GetCurrentCommandBuffer(), pcmd->ElemCount, 1, + vkCmdDrawIndexed(GetCurrentCommandBuffer(), pcmd->ElemCount, 1, m_index.start + pcmd->IdxOffset, vertex_offset + pcmd->VtxOffset, 0); } } @@ -2496,7 +4438,7 @@ void GSDeviceVK::RenderBlankFrame() return; } - VkCommandBuffer cmdbuffer = g_vulkan_context->GetCurrentCommandBuffer(); + VkCommandBuffer cmdbuffer = GetCurrentCommandBuffer(); GSTextureVK* sctex = m_swap_chain->GetCurrentTexture(); sctex->TransitionToLayout(cmdbuffer, GSTextureVK::Layout::TransferDst); @@ -2504,8 +4446,8 @@ void GSDeviceVK::RenderBlankFrame() vkCmdClearColorImage(cmdbuffer, sctex->GetImage(), VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &s_present_clear_color.color, 1, &srr); m_swap_chain->GetCurrentTexture()->TransitionToLayout(cmdbuffer, GSTextureVK::Layout::PresentSrc); - g_vulkan_context->SubmitCommandBuffer(m_swap_chain.get(), !m_swap_chain->IsPresentModeSynchronizing()); - g_vulkan_context->MoveToNextCommandBuffer(); + SubmitCommandBuffer(m_swap_chain.get(), !m_swap_chain->IsPresentModeSynchronizing()); + MoveToNextCommandBuffer(); InvalidateCachedState(); } @@ -2514,13 +4456,13 @@ bool GSDeviceVK::DoCAS(GSTexture* sTex, GSTexture* dTex, bool sharpen_only, cons { EndRenderPass(); - VkDescriptorSet ds = g_vulkan_context->AllocateDescriptorSet(m_cas_ds_layout); + VkDescriptorSet ds = AllocateDescriptorSet(m_cas_ds_layout); if (ds == VK_NULL_HANDLE) return false; GSTextureVK* const sTexVK = static_cast(sTex); GSTextureVK* const dTexVK = static_cast(dTex); - VkCommandBuffer cmdbuf = g_vulkan_context->GetCurrentCommandBuffer(); + VkCommandBuffer cmdbuf = GetCurrentCommandBuffer(); sTexVK->TransitionToLayout(cmdbuf, GSTextureVK::Layout::ShaderReadOnly); dTexVK->TransitionToLayout(cmdbuf, GSTextureVK::Layout::ComputeReadWriteImage); @@ -2529,7 +4471,7 @@ bool GSDeviceVK::DoCAS(GSTexture* sTex, GSTexture* dTex, bool sharpen_only, cons Vulkan::DescriptorSetUpdateBuilder dsub; dsub.AddImageDescriptorWrite(ds, 0, sTexVK->GetView(), sTexVK->GetVkLayout()); dsub.AddStorageImageDescriptorWrite(ds, 1, dTexVK->GetView(), dTexVK->GetVkLayout()); - dsub.Update(g_vulkan_context->GetDevice(), false); + dsub.Update(m_device, false); // the actual meat and potatoes! only four commands. static const int threadGroupWorkRegionDim = 16; @@ -2549,88 +4491,137 @@ bool GSDeviceVK::DoCAS(GSTexture* sTex, GSTexture* dTex, bool sharpen_only, cons void GSDeviceVK::DestroyResources() { - g_vulkan_context->ExecuteCommandBuffer(VKContext::WaitType::Sleep); if (m_tfx_ubo_descriptor_set != VK_NULL_HANDLE) - g_vulkan_context->FreeGlobalDescriptorSet(m_tfx_ubo_descriptor_set); + FreePersistentDescriptorSet(m_tfx_ubo_descriptor_set); for (auto& it : m_tfx_pipelines) - Vulkan::SafeDestroyPipeline(it.second); + vkDestroyPipeline(m_device, it.second, nullptr); for (auto& it : m_tfx_fragment_shaders) - Vulkan::SafeDestroyShaderModule(it.second); + vkDestroyShaderModule(m_device, it.second, nullptr); for (auto& it : m_tfx_vertex_shaders) - Vulkan::SafeDestroyShaderModule(it.second); - for (VkPipeline& it : m_interlace) - Vulkan::SafeDestroyPipeline(it); - for (VkPipeline& it : m_merge) - Vulkan::SafeDestroyPipeline(it); - for (VkPipeline& it : m_color_copy) - Vulkan::SafeDestroyPipeline(it); - for (VkPipeline& it : m_present) - Vulkan::SafeDestroyPipeline(it); - for (VkPipeline& it : m_convert) - Vulkan::SafeDestroyPipeline(it); + vkDestroyShaderModule(m_device, it.second, nullptr); + for (VkPipeline it : m_interlace) + { + if (it != VK_NULL_HANDLE) + vkDestroyPipeline(m_device, it, nullptr); + } + for (VkPipeline it : m_merge) + { + if (it != VK_NULL_HANDLE) + vkDestroyPipeline(m_device, it, nullptr); + } + for (VkPipeline it : m_color_copy) + { + if (it != VK_NULL_HANDLE) + vkDestroyPipeline(m_device, it, nullptr); + } + for (VkPipeline it : m_present) + { + if (it != VK_NULL_HANDLE) + vkDestroyPipeline(m_device, it, nullptr); + } + for (VkPipeline it : m_convert) + { + if (it != VK_NULL_HANDLE) + vkDestroyPipeline(m_device, it, nullptr); + } for (u32 ds = 0; ds < 2; ds++) { for (u32 fbl = 0; fbl < 2; fbl++) { - Vulkan::SafeDestroyPipeline(m_hdr_setup_pipelines[ds][fbl]); - Vulkan::SafeDestroyPipeline(m_hdr_finish_pipelines[ds][fbl]); + if (m_hdr_setup_pipelines[ds][fbl] != VK_NULL_HANDLE) + vkDestroyPipeline(m_device, m_hdr_setup_pipelines[ds][fbl], nullptr); + if (m_hdr_finish_pipelines[ds][fbl] != VK_NULL_HANDLE) + vkDestroyPipeline(m_device, m_hdr_finish_pipelines[ds][fbl], nullptr); } } for (u32 ds = 0; ds < 2; ds++) { for (u32 datm = 0; datm < 2; datm++) { - Vulkan::SafeDestroyPipeline(m_date_image_setup_pipelines[ds][datm]); + if (m_date_image_setup_pipelines[ds][datm] != VK_NULL_HANDLE) + vkDestroyPipeline(m_device, m_date_image_setup_pipelines[ds][datm], nullptr); } } - Vulkan::SafeDestroyPipeline(m_fxaa_pipeline); - Vulkan::SafeDestroyPipeline(m_shadeboost_pipeline); + if (m_fxaa_pipeline != VK_NULL_HANDLE) + vkDestroyPipeline(m_device, m_fxaa_pipeline, nullptr); + if (m_shadeboost_pipeline != VK_NULL_HANDLE) + vkDestroyPipeline(m_device, m_shadeboost_pipeline, nullptr); - for (VkPipeline& it : m_cas_pipelines) - Vulkan::SafeDestroyPipeline(it); - Vulkan::SafeDestroyPipelineLayout(m_cas_pipeline_layout); - Vulkan::SafeDestroyDescriptorSetLayout(m_cas_ds_layout); - Vulkan::SafeDestroyPipeline(m_imgui_pipeline); + for (VkPipeline it : m_cas_pipelines) + { + if (it != VK_NULL_HANDLE) + vkDestroyPipeline(m_device, it, nullptr); + } + vkDestroyPipelineLayout(m_device, m_cas_pipeline_layout, nullptr); + vkDestroyDescriptorSetLayout(m_device, m_cas_ds_layout, nullptr); + vkDestroyPipeline(m_device, m_imgui_pipeline, nullptr); - for (auto& it : m_samplers) - Vulkan::SafeDestroySampler(it.second); - - m_linear_sampler = VK_NULL_HANDLE; - m_point_sampler = VK_NULL_HANDLE; - - m_utility_color_render_pass_load = VK_NULL_HANDLE; - m_utility_color_render_pass_clear = VK_NULL_HANDLE; - m_utility_color_render_pass_discard = VK_NULL_HANDLE; - m_utility_depth_render_pass_load = VK_NULL_HANDLE; - m_utility_depth_render_pass_clear = VK_NULL_HANDLE; - m_utility_depth_render_pass_discard = VK_NULL_HANDLE; - m_date_setup_render_pass = VK_NULL_HANDLE; - m_swap_chain_render_pass = VK_NULL_HANDLE; + for (const auto& it : m_samplers) + { + if (it.second != VK_NULL_HANDLE) + vkDestroySampler(m_device, it.second, nullptr); + } + m_samplers.clear(); + m_texture_stream_buffer.Destroy(false); m_fragment_uniform_stream_buffer.Destroy(false); m_vertex_uniform_stream_buffer.Destroy(false); m_index_stream_buffer.Destroy(false); m_vertex_stream_buffer.Destroy(false); if (m_expand_index_buffer != VK_NULL_HANDLE) - { - vmaDestroyBuffer(g_vulkan_context->GetAllocator(), m_expand_index_buffer, m_expand_index_buffer_allocation); - m_expand_index_buffer = VK_NULL_HANDLE; - m_expand_index_buffer_allocation = VK_NULL_HANDLE; - } + vmaDestroyBuffer(m_allocator, m_expand_index_buffer, m_expand_index_buffer_allocation); - Vulkan::SafeDestroyPipelineLayout(m_tfx_pipeline_layout); - Vulkan::SafeDestroyDescriptorSetLayout(m_tfx_rt_texture_ds_layout); - Vulkan::SafeDestroyDescriptorSetLayout(m_tfx_sampler_ds_layout); - Vulkan::SafeDestroyDescriptorSetLayout(m_tfx_ubo_ds_layout); - Vulkan::SafeDestroyPipelineLayout(m_utility_pipeline_layout); - Vulkan::SafeDestroyDescriptorSetLayout(m_utility_ds_layout); + if (m_tfx_pipeline_layout != VK_NULL_HANDLE) + vkDestroyPipelineLayout(m_device, m_tfx_pipeline_layout, nullptr); + if (m_tfx_rt_texture_ds_layout != VK_NULL_HANDLE) + vkDestroyDescriptorSetLayout(m_device, m_tfx_rt_texture_ds_layout, nullptr); + if (m_tfx_sampler_ds_layout != VK_NULL_HANDLE) + vkDestroyDescriptorSetLayout(m_device, m_tfx_sampler_ds_layout, nullptr); + if (m_tfx_ubo_ds_layout != VK_NULL_HANDLE) + vkDestroyDescriptorSetLayout(m_device, m_tfx_ubo_ds_layout, nullptr); + if (m_utility_pipeline_layout != VK_NULL_HANDLE) + vkDestroyPipelineLayout(m_device, m_utility_pipeline_layout, nullptr); + if (m_utility_ds_layout != VK_NULL_HANDLE) + vkDestroyDescriptorSetLayout(m_device, m_utility_ds_layout, nullptr); if (m_null_texture) { m_null_texture->Destroy(false); m_null_texture.reset(); } + + for (FrameResources& resources : m_frame_resources) + { + for (auto& it : resources.cleanup_resources) + it(); + resources.cleanup_resources.clear(); + + if (resources.fence != VK_NULL_HANDLE) + vkDestroyFence(m_device, resources.fence, nullptr); + if (resources.descriptor_pool != VK_NULL_HANDLE) + vkDestroyDescriptorPool(m_device, resources.descriptor_pool, nullptr); + if (resources.command_buffers[0] != VK_NULL_HANDLE) + { + vkFreeCommandBuffers(m_device, resources.command_pool, static_cast(resources.command_buffers.size()), + resources.command_buffers.data()); + } + if (resources.command_pool != VK_NULL_HANDLE) + vkDestroyCommandPool(m_device, resources.command_pool, nullptr); + } + + if (m_timestamp_query_pool != VK_NULL_HANDLE) + vkDestroyQueryPool(m_device, m_timestamp_query_pool, nullptr); + + if (m_global_descriptor_pool != VK_NULL_HANDLE) + vkDestroyDescriptorPool(m_device, m_global_descriptor_pool, nullptr); + + for (auto& it : m_render_pass_cache) + vkDestroyRenderPass(m_device, it.second, nullptr); + m_render_pass_cache.clear(); + + if (m_allocator != VK_NULL_HANDLE) + vmaDestroyAllocator(m_allocator); } VkShaderModule GSDeviceVK::GetTFXVertexShader(GSHWDrawConfig::VSSelector sel) @@ -2652,7 +4643,7 @@ VkShaderModule GSDeviceVK::GetTFXVertexShader(GSHWDrawConfig::VSSelector sel) VkShaderModule mod = g_vulkan_shader_cache->GetVertexShader(ss.str()); if (mod) - Vulkan::SetObjectName(g_vulkan_context->GetDevice(), mod, "TFX Vertex %08X", sel.key); + Vulkan::SetObjectName(m_device, mod, "TFX Vertex %08X", sel.key); m_tfx_vertex_shaders.emplace(sel.key, mod); return mod; @@ -2722,7 +4713,7 @@ VkShaderModule GSDeviceVK::GetTFXFragmentShader(const GSHWDrawConfig::PSSelector VkShaderModule mod = g_vulkan_shader_cache->GetFragmentShader(ss.str()); if (mod) - Vulkan::SetObjectName(g_vulkan_context->GetDevice(), mod, "TFX Fragment %" PRIX64 "%08X", sel.key_hi, sel.key_lo); + Vulkan::SetObjectName(m_device, mod, "TFX Fragment %" PRIX64 "%08X", sel.key_hi, sel.key_lo); m_tfx_fragment_shaders.emplace(sel, mod); return mod; @@ -2771,7 +4762,7 @@ VkPipeline GSDeviceVK::CreateTFXPipeline(const PipelineSelector& p) } gpb.SetPrimitiveTopology(topology_lookup[p.topology]); gpb.SetRasterizationState(VK_POLYGON_MODE_FILL, VK_CULL_MODE_NONE, VK_FRONT_FACE_CLOCKWISE); - if (p.topology == static_cast(GSHWDrawConfig::Topology::Line) && g_vulkan_context->GetOptionalExtensions().vk_ext_line_rasterization) + if (p.topology == static_cast(GSHWDrawConfig::Topology::Line) && m_optional_extensions.vk_ext_line_rasterization) gpb.SetLineRasterizationMode(VK_LINE_RASTERIZATION_MODE_BRESENHAM_EXT); gpb.SetDynamicViewportAndScissorState(); gpb.AddDynamicState(VK_DYNAMIC_STATE_BLEND_CONSTANTS); @@ -2841,11 +4832,11 @@ VkPipeline GSDeviceVK::CreateTFXPipeline(const PipelineSelector& p) if (m_features.framebuffer_fetch && p.IsRTFeedbackLoop()) gpb.AddBlendFlags(VK_PIPELINE_COLOR_BLEND_STATE_CREATE_RASTERIZATION_ORDER_ATTACHMENT_ACCESS_BIT_EXT); - VkPipeline pipeline = gpb.Create(g_vulkan_context->GetDevice(), g_vulkan_shader_cache->GetPipelineCache(true)); + VkPipeline pipeline = gpb.Create(m_device, g_vulkan_shader_cache->GetPipelineCache(true)); if (pipeline) { Vulkan::SetObjectName( - g_vulkan_context->GetDevice(), pipeline, "TFX Pipeline %08X/%" PRIX64 "%08X", p.vs.key, p.ps.key_hi, p.ps.key_lo); + m_device, pipeline, "TFX Pipeline %08X/%" PRIX64 "%08X", p.vs.key, p.ps.key_hi, p.ps.key_lo); } return pipeline; @@ -2890,10 +4881,10 @@ void GSDeviceVK::InitializeState() m_point_sampler = GetSampler(GSHWDrawConfig::SamplerSelector::Point()); if (m_point_sampler) - Vulkan::SetObjectName(g_vulkan_context->GetDevice(), m_point_sampler, "Point sampler"); + Vulkan::SetObjectName(m_device, m_point_sampler, "Point sampler"); m_linear_sampler = GetSampler(GSHWDrawConfig::SamplerSelector::Linear()); if (m_linear_sampler) - Vulkan::SetObjectName(g_vulkan_context->GetDevice(), m_point_sampler, "Linear sampler"); + Vulkan::SetObjectName(m_device, m_point_sampler, "Linear sampler"); m_tfx_sampler_sel = GSHWDrawConfig::SamplerSelector::Point().key; m_tfx_sampler = m_point_sampler; @@ -2903,11 +4894,11 @@ void GSDeviceVK::InitializeState() bool GSDeviceVK::CreatePersistentDescriptorSets() { - const VkDevice dev = g_vulkan_context->GetDevice(); + const VkDevice dev = m_device; Vulkan::DescriptorSetUpdateBuilder dsub; // Allocate UBO descriptor sets for TFX. - m_tfx_ubo_descriptor_set = g_vulkan_context->AllocatePersistentDescriptorSet(m_tfx_ubo_ds_layout); + m_tfx_ubo_descriptor_set = AllocatePersistentDescriptorSet(m_tfx_ubo_ds_layout); if (m_tfx_ubo_descriptor_set == VK_NULL_HANDLE) return false; dsub.AddBufferDescriptorWrite(m_tfx_ubo_descriptor_set, 0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, @@ -2924,20 +4915,20 @@ bool GSDeviceVK::CreatePersistentDescriptorSets() return true; } -static VKContext::WaitType GetWaitType(bool wait, bool spin) +GSDeviceVK::WaitType GSDeviceVK::GetWaitType(bool wait, bool spin) { if (!wait) - return VKContext::WaitType::None; + return WaitType::None; if (spin) - return VKContext::WaitType::Spin; + return WaitType::Spin; else - return VKContext::WaitType::Sleep; + return WaitType::Sleep; } void GSDeviceVK::ExecuteCommandBuffer(bool wait_for_completion) { EndRenderPass(); - g_vulkan_context->ExecuteCommandBuffer(GetWaitType(wait_for_completion, GSConfig.HWSpinCPUForReadbacks)); + ExecuteCommandBuffer(GetWaitType(wait_for_completion, GSConfig.HWSpinCPUForReadbacks)); InvalidateCachedState(); } @@ -2964,7 +4955,7 @@ void GSDeviceVK::ExecuteCommandBufferAndRestartRenderPass(bool wait_for_completi const FeedbackLoopFlag current_feedback_loop = m_current_framebuffer_feedback_loop; EndRenderPass(); - g_vulkan_context->ExecuteCommandBuffer(GetWaitType(wait_for_completion, GSConfig.HWSpinCPUForReadbacks)); + ExecuteCommandBuffer(GetWaitType(wait_for_completion, GSConfig.HWSpinCPUForReadbacks)); InvalidateCachedState(); if (render_pass != VK_NULL_HANDLE) @@ -2973,17 +4964,19 @@ void GSDeviceVK::ExecuteCommandBufferAndRestartRenderPass(bool wait_for_completi OMSetRenderTargets(current_rt, current_ds, scissor, current_feedback_loop); // restart render pass - BeginRenderPass(g_vulkan_context->GetRenderPassForRestarting(render_pass), render_pass_area); + BeginRenderPass(GetRenderPassForRestarting(render_pass), render_pass_area); } } void GSDeviceVK::ExecuteCommandBufferForReadback() { ExecuteCommandBuffer(true); - if (GSConfig.HWSpinGPUForReadbacks) + if (m_spinning_supported && GSConfig.HWSpinGPUForReadbacks) { - g_vulkan_context->NotifyOfReadback(); - if (!g_vulkan_context->GetOptionalExtensions().vk_ext_calibrated_timestamps && !m_warned_slow_spin) + m_spin_timer = 30; + m_spin_manager.ReadbackRequested(); + + if (!m_optional_extensions.vk_ext_calibrated_timestamps && !m_warned_slow_spin) { m_warned_slow_spin = true; Host::AddKeyedOSDMessage("GSDeviceVK_NoCalibratedTimestamps", @@ -3074,7 +5067,7 @@ void GSDeviceVK::PSSetShaderResource(int i, GSTexture* sr, bool check_state) vkTex->CommitClear(); vkTex->TransitionToLayout(GSTextureVK::Layout::ShaderReadOnly); } - vkTex->SetUsedThisCommandBuffer(); + vkTex->SetUseFenceCounter(GetCurrentFenceCounter()); } else { @@ -3106,7 +5099,7 @@ void GSDeviceVK::SetUtilityTexture(GSTexture* tex, VkSampler sampler) { vkTex->CommitClear(); vkTex->TransitionToLayout(GSTextureVK::Layout::ShaderReadOnly); - vkTex->SetUsedThisCommandBuffer(); + vkTex->SetUseFenceCounter(GetCurrentFenceCounter()); } else { @@ -3123,7 +5116,7 @@ void GSDeviceVK::SetUtilityTexture(GSTexture* tex, VkSampler sampler) void GSDeviceVK::SetUtilityPushConstants(const void* data, u32 size) { - vkCmdPushConstants(g_vulkan_context->GetCurrentCommandBuffer(), m_utility_pipeline_layout, + vkCmdPushConstants(GetCurrentCommandBuffer(), m_utility_pipeline_layout, VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, 0, size, data); } @@ -3165,8 +5158,8 @@ void GSDeviceVK::BeginRenderPass(VkRenderPass rp, const GSVector4i& rect) m_current_framebuffer, {{rect.x, rect.y}, {static_cast(rect.width()), static_cast(rect.height())}}, 0, nullptr}; - g_vulkan_context->CountRenderPass(); - vkCmdBeginRenderPass(g_vulkan_context->GetCurrentCommandBuffer(), &begin_info, VK_SUBPASS_CONTENTS_INLINE); + m_command_buffer_render_passes++; + vkCmdBeginRenderPass(GetCurrentCommandBuffer(), &begin_info, VK_SUBPASS_CONTENTS_INLINE); } void GSDeviceVK::BeginClearRenderPass(VkRenderPass rp, const GSVector4i& rect, const VkClearValue* cv, u32 cv_count) @@ -3181,7 +5174,7 @@ void GSDeviceVK::BeginClearRenderPass(VkRenderPass rp, const GSVector4i& rect, c m_current_framebuffer, {{rect.x, rect.y}, {static_cast(rect.width()), static_cast(rect.height())}}, cv_count, cv}; - vkCmdBeginRenderPass(g_vulkan_context->GetCurrentCommandBuffer(), &begin_info, VK_SUBPASS_CONTENTS_INLINE); + vkCmdBeginRenderPass(GetCurrentCommandBuffer(), &begin_info, VK_SUBPASS_CONTENTS_INLINE); } void GSDeviceVK::BeginClearRenderPass(VkRenderPass rp, const GSVector4i& rect, u32 clear_color) @@ -3207,7 +5200,7 @@ void GSDeviceVK::EndRenderPass() m_current_render_pass = VK_NULL_HANDLE; g_perfmon.Put(GSPerfMon::RenderPasses, 1); - vkCmdEndRenderPass(g_vulkan_context->GetCurrentCommandBuffer()); + vkCmdEndRenderPass(GetCurrentCommandBuffer()); } void GSDeviceVK::SetViewport(const VkViewport& viewport) @@ -3273,8 +5266,8 @@ bool GSDeviceVK::ApplyTFXState(bool already_execed) if (m_current_pipeline_layout == PipelineLayout::TFX && m_dirty_flags == 0) return true; - const VkDevice dev = g_vulkan_context->GetDevice(); - const VkCommandBuffer cmdbuf = g_vulkan_context->GetCurrentCommandBuffer(); + const VkDevice dev = m_device; + const VkCommandBuffer cmdbuf = GetCurrentCommandBuffer(); u32 flags = m_dirty_flags; m_dirty_flags &= ~(DIRTY_TFX_STATE | DIRTY_CONSTANT_BUFFER_STATE | DIRTY_FLAG_TFX_DYNAMIC_OFFSETS); @@ -3282,7 +5275,7 @@ bool GSDeviceVK::ApplyTFXState(bool already_execed) if (flags & DIRTY_FLAG_VS_CONSTANT_BUFFER) { if (!m_vertex_uniform_stream_buffer.ReserveMemory( - sizeof(m_vs_cb_cache), g_vulkan_context->GetUniformBufferAlignment())) + sizeof(m_vs_cb_cache), static_cast(m_device_properties.limits.minUniformBufferOffsetAlignment))) { if (already_execed) { @@ -3303,7 +5296,7 @@ bool GSDeviceVK::ApplyTFXState(bool already_execed) if (flags & DIRTY_FLAG_PS_CONSTANT_BUFFER) { if (!m_fragment_uniform_stream_buffer.ReserveMemory( - sizeof(m_ps_cb_cache), g_vulkan_context->GetUniformBufferAlignment())) + sizeof(m_ps_cb_cache), static_cast(m_device_properties.limits.minUniformBufferOffsetAlignment))) { if (already_execed) { @@ -3333,7 +5326,7 @@ bool GSDeviceVK::ApplyTFXState(bool already_execed) if ((flags & DIRTY_FLAG_TFX_SAMPLERS_DS) || m_tfx_texture_descriptor_set == VK_NULL_HANDLE) { - m_tfx_texture_descriptor_set = g_vulkan_context->AllocateDescriptorSet(m_tfx_sampler_ds_layout); + m_tfx_texture_descriptor_set = AllocateDescriptorSet(m_tfx_sampler_ds_layout); if (m_tfx_texture_descriptor_set == VK_NULL_HANDLE) { if (already_execed) @@ -3361,7 +5354,7 @@ bool GSDeviceVK::ApplyTFXState(bool already_execed) if ((flags & DIRTY_FLAG_TFX_RT_TEXTURE_DS) || m_tfx_rt_descriptor_set == VK_NULL_HANDLE) { - m_tfx_rt_descriptor_set = g_vulkan_context->AllocateDescriptorSet(m_tfx_rt_texture_ds_layout); + m_tfx_rt_descriptor_set = AllocateDescriptorSet(m_tfx_rt_texture_ds_layout); if (m_tfx_rt_descriptor_set == VK_NULL_HANDLE) { if (already_execed) @@ -3374,7 +5367,7 @@ bool GSDeviceVK::ApplyTFXState(bool already_execed) return ApplyTFXState(true); } - if (m_features.texture_barrier && !m_has_feedback_loop_layout) + if (m_features.texture_barrier && !UseFeedbackLoopLayout()) { dsub.AddInputAttachmentDescriptorWrite( m_tfx_rt_descriptor_set, 0, m_tfx_textures[NUM_TFX_DRAW_TEXTURES]->GetView(), VK_IMAGE_LAYOUT_GENERAL); @@ -3439,8 +5432,8 @@ bool GSDeviceVK::ApplyUtilityState(bool already_execed) if (m_current_pipeline_layout == PipelineLayout::Utility && m_dirty_flags == 0) return true; - const VkDevice dev = g_vulkan_context->GetDevice(); - const VkCommandBuffer cmdbuf = g_vulkan_context->GetCurrentCommandBuffer(); + const VkDevice dev = m_device; + const VkCommandBuffer cmdbuf = GetCurrentCommandBuffer(); u32 flags = m_dirty_flags; m_dirty_flags &= ~DIRTY_UTILITY_STATE; @@ -3448,7 +5441,7 @@ bool GSDeviceVK::ApplyUtilityState(bool already_execed) if ((flags & DIRTY_FLAG_UTILITY_TEXTURE) || m_utility_descriptor_set == VK_NULL_HANDLE) { - m_utility_descriptor_set = g_vulkan_context->AllocateDescriptorSet(m_utility_ds_layout); + m_utility_descriptor_set = AllocateDescriptorSet(m_utility_ds_layout); if (m_utility_descriptor_set == VK_NULL_HANDLE) { if (already_execed) @@ -3951,9 +5944,9 @@ void GSDeviceVK::UploadHWDrawVerticesAndIndices(const GSHWDrawConfig& config) VkImageMemoryBarrier GSDeviceVK::GetColorBufferBarrier(GSTextureVK* rt) const { const VkImageLayout layout = - m_has_feedback_loop_layout ? VK_IMAGE_LAYOUT_ATTACHMENT_FEEDBACK_LOOP_OPTIMAL_EXT : VK_IMAGE_LAYOUT_GENERAL; + UseFeedbackLoopLayout() ? VK_IMAGE_LAYOUT_ATTACHMENT_FEEDBACK_LOOP_OPTIMAL_EXT : VK_IMAGE_LAYOUT_GENERAL; const VkAccessFlags dst_access = - m_has_feedback_loop_layout ? VK_ACCESS_SHADER_READ_BIT : VK_ACCESS_INPUT_ATTACHMENT_READ_BIT; + UseFeedbackLoopLayout() ? VK_ACCESS_SHADER_READ_BIT : VK_ACCESS_INPUT_ATTACHMENT_READ_BIT; return {VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, nullptr, VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, dst_access, layout, layout, VK_QUEUE_FAMILY_IGNORED, VK_QUEUE_FAMILY_IGNORED, rt->GetImage(), {VK_IMAGE_ASPECT_COLOR_BIT, 0u, 1u, 0u, 1u}}; @@ -3961,7 +5954,7 @@ VkImageMemoryBarrier GSDeviceVK::GetColorBufferBarrier(GSTextureVK* rt) const VkDependencyFlags GSDeviceVK::GetColorBufferBarrierFlags() const { - return m_has_feedback_loop_layout ? (VK_DEPENDENCY_BY_REGION_BIT | VK_DEPENDENCY_FEEDBACK_LOOP_BIT_EXT) : + return UseFeedbackLoopLayout() ? (VK_DEPENDENCY_BY_REGION_BIT | VK_DEPENDENCY_FEEDBACK_LOOP_BIT_EXT) : VK_DEPENDENCY_BY_REGION_BIT; } @@ -3990,7 +5983,7 @@ void GSDeviceVK::SendHWDraw(const GSHWDrawConfig& config, GSTextureVK* draw_rt, for (; n < draw_list_size; n++) { - vkCmdPipelineBarrier(g_vulkan_context->GetCurrentCommandBuffer(), + vkCmdPipelineBarrier(GetCurrentCommandBuffer(), VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, barrier_flags, 0, nullptr, 0, nullptr, 1, &barrier); @@ -4024,7 +6017,7 @@ void GSDeviceVK::SendHWDraw(const GSHWDrawConfig& config, GSTextureVK* draw_rt, for (; p < config.nindices; p += indices_per_prim) { - vkCmdPipelineBarrier(g_vulkan_context->GetCurrentCommandBuffer(), + vkCmdPipelineBarrier(GetCurrentCommandBuffer(), VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, barrier_flags, 0, nullptr, 0, nullptr, 1, &barrier); @@ -4037,7 +6030,7 @@ void GSDeviceVK::SendHWDraw(const GSHWDrawConfig& config, GSTextureVK* draw_rt, if (config.require_one_barrier && !skip_first_barrier) { g_perfmon.Put(GSPerfMon::Barriers, 1); - vkCmdPipelineBarrier(g_vulkan_context->GetCurrentCommandBuffer(), + vkCmdPipelineBarrier(GetCurrentCommandBuffer(), VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, barrier_flags, 0, nullptr, 0, nullptr, 1, &barrier); } diff --git a/pcsx2/GS/Renderers/Vulkan/GSDeviceVK.h b/pcsx2/GS/Renderers/Vulkan/GSDeviceVK.h index 7bc416bf20..bd35c84eeb 100644 --- a/pcsx2/GS/Renderers/Vulkan/GSDeviceVK.h +++ b/pcsx2/GS/Renderers/Vulkan/GSDeviceVK.h @@ -22,17 +22,317 @@ #include "GS/GSVector.h" #include "common/HashCombine.h" +#include "common/ReadbackSpinManager.h" #include -#include +#include +#include +#include +#include +#include +#include +#include +#include +#include -namespace Vulkan -{ class VKSwapChain; -} class GSDeviceVK final : public GSDevice { +public: + enum : u32 + { + NUM_COMMAND_BUFFERS = 3, + }; + + struct OptionalExtensions + { + bool vk_ext_provoking_vertex : 1; + bool vk_ext_memory_budget : 1; + bool vk_ext_calibrated_timestamps : 1; + bool vk_ext_line_rasterization : 1; + bool vk_ext_rasterization_order_attachment_access : 1; + bool vk_ext_attachment_feedback_loop_layout : 1; + bool vk_ext_full_screen_exclusive : 1; + bool vk_khr_driver_properties : 1; + bool vk_khr_fragment_shader_barycentric : 1; + bool vk_khr_shader_draw_parameters : 1; + }; + + // Global state accessors + __fi VkInstance GetVulkanInstance() const { return m_instance; } + __fi VkPhysicalDevice GetPhysicalDevice() const { return m_physical_device; } + __fi VkDevice GetDevice() const { return m_device; } + __fi VmaAllocator GetAllocator() const { return m_allocator; } + __fi u32 GetGraphicsQueueFamilyIndex() const { return m_graphics_queue_family_index; } + __fi u32 GetPresentQueueFamilyIndex() const { return m_present_queue_family_index; } + __fi const VkPhysicalDeviceProperties& GetDeviceProperties() const { return m_device_properties; } + __fi const OptionalExtensions& GetOptionalExtensions() const { return m_optional_extensions; } + + // The interaction between raster order attachment access and fbfetch is unclear. + __fi bool UseFeedbackLoopLayout() const + { + return (m_optional_extensions.vk_ext_attachment_feedback_loop_layout && + !m_optional_extensions.vk_ext_rasterization_order_attachment_access); + } + + // Helpers for getting constants + __fi u32 GetBufferCopyOffsetAlignment() const + { + return static_cast(m_device_properties.limits.optimalBufferCopyOffsetAlignment); + } + __fi u32 GetBufferCopyRowPitchAlignment() const + { + return static_cast(m_device_properties.limits.optimalBufferCopyRowPitchAlignment); + } + + /// Returns true if running on an NVIDIA GPU. + __fi bool IsDeviceNVIDIA() const { return (m_device_properties.vendorID == 0x10DE); } + + // Creates a simple render pass. + VkRenderPass GetRenderPass(VkFormat color_format, VkFormat depth_format, + VkAttachmentLoadOp color_load_op = VK_ATTACHMENT_LOAD_OP_LOAD, + VkAttachmentStoreOp color_store_op = VK_ATTACHMENT_STORE_OP_STORE, + VkAttachmentLoadOp depth_load_op = VK_ATTACHMENT_LOAD_OP_LOAD, + VkAttachmentStoreOp depth_store_op = VK_ATTACHMENT_STORE_OP_STORE, + VkAttachmentLoadOp stencil_load_op = VK_ATTACHMENT_LOAD_OP_DONT_CARE, + VkAttachmentStoreOp stencil_store_op = VK_ATTACHMENT_STORE_OP_DONT_CARE, bool color_feedback_loop = false, + bool depth_sampling = false); + + // Gets a non-clearing version of the specified render pass. Slow, don't call in hot path. + VkRenderPass GetRenderPassForRestarting(VkRenderPass pass); + + // These command buffers are allocated per-frame. They are valid until the command buffer + // is submitted, after that you should call these functions again. + __fi VkCommandBuffer GetCurrentCommandBuffer() const { return m_current_command_buffer; } + __fi VKStreamBuffer& GetTextureUploadBuffer() { return m_texture_stream_buffer; } + VkCommandBuffer GetCurrentInitCommandBuffer(); + + /// Allocates a descriptor set from the pool reserved for the current frame. + VkDescriptorSet AllocateDescriptorSet(VkDescriptorSetLayout set_layout); + + /// Allocates a descriptor set from the pool reserved for the current frame. + VkDescriptorSet AllocatePersistentDescriptorSet(VkDescriptorSetLayout set_layout); + + /// Frees a descriptor set allocated from the global pool. + void FreePersistentDescriptorSet(VkDescriptorSet set); + + // Gets the fence that will be signaled when the currently executing command buffer is + // queued and executed. Do not wait for this fence before the buffer is executed. + __fi VkFence GetCurrentCommandBufferFence() const { return m_frame_resources[m_current_frame].fence; } + + // Fence "counters" are used to track which commands have been completed by the GPU. + // If the last completed fence counter is greater or equal to N, it means that the work + // associated counter N has been completed by the GPU. The value of N to associate with + // commands can be retreived by calling GetCurrentFenceCounter(). + u64 GetCompletedFenceCounter() const { return m_completed_fence_counter; } + + // Gets the fence that will be signaled when the currently executing command buffer is + // queued and executed. Do not wait for this fence before the buffer is executed. + u64 GetCurrentFenceCounter() const { return m_frame_resources[m_current_frame].fence_counter; } + + // Schedule a vulkan resource for destruction later on. This will occur when the command buffer + // is next re-used, and the GPU has finished working with the specified resource. + void DeferBufferDestruction(VkBuffer object, VmaAllocation allocation); + void DeferFramebufferDestruction(VkFramebuffer object); + void DeferImageDestruction(VkImage object, VmaAllocation allocation); + void DeferImageViewDestruction(VkImageView object); + + // Wait for a fence to be completed. + // Also invokes callbacks for completion. + void WaitForFenceCounter(u64 fence_counter); + + void WaitForGPUIdle(); + +private: + // Helper method to create a Vulkan instance. + static VkInstance CreateVulkanInstance(const WindowInfo& wi, bool enable_debug_utils, bool enable_validation_layer); + + // Returns a list of Vulkan-compatible GPUs. + using GPUList = std::vector>; + static GPUList EnumerateGPUs(VkInstance instance); + static void GPUListToAdapterNames(std::vector* dest, VkInstance instance); + + // Enable/disable debug message runtime. + bool EnableDebugUtils(); + void DisableDebugUtils(); + + void SubmitCommandBuffer(VKSwapChain* present_swap_chain = nullptr, bool submit_on_thread = false); + void MoveToNextCommandBuffer(); + + enum class WaitType + { + None, + Sleep, + Spin, + }; + + static WaitType GetWaitType(bool wait, bool spin); + void ExecuteCommandBuffer(WaitType wait_for_completion); + void WaitForPresentComplete(); + + // Was the last present submitted to the queue a failure? If so, we must recreate our swapchain. + bool CheckLastPresentFail(); + bool CheckLastSubmitFail(); + + // Allocates a temporary CPU staging buffer, fires the callback with it to populate, then copies to a GPU buffer. + bool AllocatePreinitializedGPUBuffer(u32 size, VkBuffer* gpu_buffer, VmaAllocation* gpu_allocation, + VkBufferUsageFlags gpu_usage, const std::function& fill_callback); + + union RenderPassCacheKey + { + struct + { + u32 color_format : 8; + u32 depth_format : 8; + u32 color_load_op : 2; + u32 color_store_op : 1; + u32 depth_load_op : 2; + u32 depth_store_op : 1; + u32 stencil_load_op : 2; + u32 stencil_store_op : 1; + u32 color_feedback_loop : 1; + u32 depth_sampling : 1; + }; + + u32 key; + }; + + using ExtensionList = std::vector; + static bool SelectInstanceExtensions( + ExtensionList* extension_list, const WindowInfo& wi, bool enable_debug_utils); + bool SelectDeviceExtensions(ExtensionList* extension_list, bool enable_surface); + bool SelectDeviceFeatures(); + bool CreateDevice(VkSurfaceKHR surface, bool enable_validation_layer); + void ProcessDeviceExtensions(); + + bool CreateAllocator(); + bool CreateCommandBuffers(); + bool CreateGlobalDescriptorPool(); + + VkRenderPass CreateCachedRenderPass(RenderPassCacheKey key); + + void CommandBufferCompleted(u32 index); + void ActivateCommandBuffer(u32 index); + void ScanForCommandBufferCompletion(); + void WaitForCommandBufferCompletion(u32 index); + + void DoSubmitCommandBuffer(u32 index, VKSwapChain* present_swap_chain, u32 spin_cycles); + void DoPresent(VKSwapChain* present_swap_chain); + void WaitForPresentComplete(std::unique_lock& lock); + void PresentThread(); + void StartPresentThread(); + void StopPresentThread(); + + bool InitSpinResources(); + void DestroySpinResources(); + void WaitForSpinCompletion(u32 index); + void SpinCommandCompleted(u32 index); + void SubmitSpinCommand(u32 index, u32 cycles); + void CalibrateSpinTimestamp(); + u64 GetCPUTimestamp(); + + struct FrameResources + { + // [0] - Init (upload) command buffer, [1] - draw command buffer + VkCommandPool command_pool = VK_NULL_HANDLE; + std::array command_buffers{VK_NULL_HANDLE, VK_NULL_HANDLE}; + VkDescriptorPool descriptor_pool = VK_NULL_HANDLE; + VkFence fence = VK_NULL_HANDLE; + u64 fence_counter = 0; + s32 spin_id = -1; + u32 submit_timestamp = 0; + bool init_buffer_used = false; + bool needs_fence_wait = false; + bool timestamp_written = false; + + std::vector> cleanup_resources; + }; + + struct SpinResources + { + VkCommandPool command_pool = VK_NULL_HANDLE; + VkCommandBuffer command_buffer = VK_NULL_HANDLE; + VkSemaphore semaphore = VK_NULL_HANDLE; + VkFence fence = VK_NULL_HANDLE; + u32 cycles = 0; + bool in_progress = false; + }; + + VkInstance m_instance = VK_NULL_HANDLE; + VkPhysicalDevice m_physical_device = VK_NULL_HANDLE; + VkDevice m_device = VK_NULL_HANDLE; + VmaAllocator m_allocator = VK_NULL_HANDLE; + + VkCommandBuffer m_current_command_buffer = VK_NULL_HANDLE; + + VkDescriptorPool m_global_descriptor_pool = VK_NULL_HANDLE; + + VkQueue m_graphics_queue = VK_NULL_HANDLE; + VkQueue m_present_queue = VK_NULL_HANDLE; + u32 m_graphics_queue_family_index = 0; + u32 m_present_queue_family_index = 0; + + ReadbackSpinManager m_spin_manager; + VkQueue m_spin_queue = VK_NULL_HANDLE; + VkDescriptorSetLayout m_spin_descriptor_set_layout = VK_NULL_HANDLE; + VkPipelineLayout m_spin_pipeline_layout = VK_NULL_HANDLE; + VkPipeline m_spin_pipeline = VK_NULL_HANDLE; + VkBuffer m_spin_buffer = VK_NULL_HANDLE; + VmaAllocation m_spin_buffer_allocation = VK_NULL_HANDLE; + VkDescriptorSet m_spin_descriptor_set = VK_NULL_HANDLE; + std::array m_spin_resources; +#ifdef _WIN32 + double m_queryperfcounter_to_ns = 0; +#endif + double m_spin_timestamp_scale = 0; + double m_spin_timestamp_offset = 0; + u32 m_spin_queue_family_index = 0; + u32 m_command_buffer_render_passes = 0; + u32 m_spin_timer = 0; + bool m_spinning_supported = false; + bool m_spin_queue_is_graphics_queue = false; + bool m_spin_buffer_initialized = false; + + VkQueryPool m_timestamp_query_pool = VK_NULL_HANDLE; + float m_accumulated_gpu_time = 0.0f; + bool m_gpu_timing_enabled = false; + bool m_gpu_timing_supported = false; + bool m_wants_new_timestamp_calibration = false; + VkTimeDomainEXT m_calibrated_timestamp_type = VK_TIME_DOMAIN_DEVICE_EXT; + + std::array m_frame_resources; + u64 m_next_fence_counter = 1; + u64 m_completed_fence_counter = 0; + u32 m_current_frame = 0; + + std::atomic_bool m_last_submit_failed{false}; + std::atomic_bool m_last_present_failed{false}; + std::atomic_bool m_present_done{true}; + std::mutex m_present_mutex; + std::condition_variable m_present_queued_cv; + std::condition_variable m_present_done_cv; + std::thread m_present_thread; + std::atomic_bool m_present_thread_done{false}; + + struct QueuedPresent + { + VKSwapChain* swap_chain; + u32 command_buffer_index; + u32 spin_cycles; + }; + + QueuedPresent m_queued_present = {}; + + std::map m_render_pass_cache; + + VkDebugUtilsMessengerEXT m_debug_messenger_callback = VK_NULL_HANDLE; + + VkPhysicalDeviceFeatures m_device_features = {}; + VkPhysicalDeviceProperties m_device_properties = {}; + VkPhysicalDeviceDriverPropertiesKHR m_device_driver_properties = {}; + OptionalExtensions m_optional_extensions = {}; + public: enum FeedbackLoopFlag : u8 { @@ -95,11 +395,6 @@ public: NUM_CONVERT_SAMPLERS = 1, CONVERT_PUSH_CONSTANTS_SIZE = 96, - VERTEX_BUFFER_SIZE = 32 * 1024 * 1024, - INDEX_BUFFER_SIZE = 16 * 1024 * 1024, - VERTEX_UNIFORM_BUFFER_SIZE = 8 * 1024 * 1024, - FRAGMENT_UNIFORM_BUFFER_SIZE = 8 * 1024 * 1024, - NUM_CAS_PIPELINES = 2, }; enum TFX_DESCRIPTOR_SET : u32 @@ -132,6 +427,7 @@ private: VKStreamBuffer m_index_stream_buffer; VKStreamBuffer m_vertex_uniform_stream_buffer; VKStreamBuffer m_fragment_uniform_stream_buffer; + VKStreamBuffer m_texture_stream_buffer; VkBuffer m_expand_index_buffer = VK_NULL_HANDLE; VmaAllocation m_expand_index_buffer_allocation = VK_NULL_HANDLE; @@ -397,7 +693,6 @@ private: // Which bindings/state has to be updated before the next draw. u32 m_dirty_flags = 0; FeedbackLoopFlag m_current_framebuffer_feedback_loop = FeedbackLoopFlag_None; - bool m_has_feedback_loop_layout = false; bool m_warned_slow_spin = false; // input assembly diff --git a/pcsx2/GS/Renderers/Vulkan/GSTextureVK.cpp b/pcsx2/GS/Renderers/Vulkan/GSTextureVK.cpp index 070e5542f7..27e5421503 100644 --- a/pcsx2/GS/Renderers/Vulkan/GSTextureVK.cpp +++ b/pcsx2/GS/Renderers/Vulkan/GSTextureVK.cpp @@ -18,8 +18,6 @@ #include "GS/Renderers/Vulkan/GSDeviceVK.h" #include "GS/Renderers/Vulkan/GSTextureVK.h" #include "GS/Renderers/Vulkan/VKBuilders.h" -#include "GS/Renderers/Vulkan/VKContext.h" -#include "GS/Renderers/Vulkan/VKUtil.h" #include "GS/GSPerfMon.h" #include "GS/GSGL.h" @@ -47,14 +45,14 @@ static VkImageLayout GetVkImageLayout(GSTextureVK::Layout layout) VK_IMAGE_LAYOUT_GENERAL, // ComputeReadWriteImage VK_IMAGE_LAYOUT_GENERAL, // General }}; - return (layout == GSTextureVK::Layout::FeedbackLoop && g_vulkan_context->UseFeedbackLoopLayout()) ? + return (layout == GSTextureVK::Layout::FeedbackLoop && GSDeviceVK::GetInstance()->UseFeedbackLoopLayout()) ? VK_IMAGE_LAYOUT_ATTACHMENT_FEEDBACK_LOOP_OPTIMAL_EXT : s_vk_layout_mapping[static_cast(layout)]; } static VkAccessFlagBits GetFeedbackLoopInputAccessBits() { - return g_vulkan_context->UseFeedbackLoopLayout() ? VK_ACCESS_SHADER_READ_BIT : VK_ACCESS_INPUT_ATTACHMENT_READ_BIT; + return GSDeviceVK::GetInstance()->UseFeedbackLoopLayout() ? VK_ACCESS_SHADER_READ_BIT : VK_ACCESS_INPUT_ATTACHMENT_READ_BIT; } GSTextureVK::GSTextureVK(Type type, Format format, int width, int height, int levels, VkImage image, @@ -115,7 +113,7 @@ std::unique_ptr GSTextureVK::Create(Type type, Format format, int w pxAssert(levels == 1); ici.usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT | - (g_vulkan_context->UseFeedbackLoopLayout() ? VK_IMAGE_USAGE_ATTACHMENT_FEEDBACK_LOOP_BIT_EXT : + (GSDeviceVK::GetInstance()->UseFeedbackLoopLayout() ? VK_IMAGE_USAGE_ATTACHMENT_FEEDBACK_LOOP_BIT_EXT : VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT); } break; @@ -126,7 +124,7 @@ std::unique_ptr GSTextureVK::Create(Type type, Format format, int w ici.usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT | - (g_vulkan_context->UseFeedbackLoopLayout() ? VK_IMAGE_USAGE_ATTACHMENT_FEEDBACK_LOOP_BIT_EXT : 0); + (GSDeviceVK::GetInstance()->UseFeedbackLoopLayout() ? VK_IMAGE_USAGE_ATTACHMENT_FEEDBACK_LOOP_BIT_EXT : 0); vci.subresourceRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT; } break; @@ -149,12 +147,12 @@ std::unique_ptr GSTextureVK::Create(Type type, Format format, int w VkImage image = VK_NULL_HANDLE; VmaAllocation allocation = VK_NULL_HANDLE; - VkResult res = vmaCreateImage(g_vulkan_context->GetAllocator(), &ici, &aci, &image, &allocation, nullptr); + VkResult res = vmaCreateImage(GSDeviceVK::GetInstance()->GetAllocator(), &ici, &aci, &image, &allocation, nullptr); if (aci.flags & VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT && res != VK_SUCCESS) { // try without dedicated allocation aci.flags &= ~VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT; - res = vmaCreateImage(g_vulkan_context->GetAllocator(), &ici, &aci, &image, &allocation, nullptr); + res = vmaCreateImage(GSDeviceVK::GetInstance()->GetAllocator(), &ici, &aci, &image, &allocation, nullptr); } if (res == VK_ERROR_OUT_OF_DEVICE_MEMORY) { @@ -169,30 +167,30 @@ std::unique_ptr GSTextureVK::Create(Type type, Format format, int w VkImageView view = VK_NULL_HANDLE; vci.image = image; - res = vkCreateImageView(g_vulkan_context->GetDevice(), &vci, nullptr, &view); + res = vkCreateImageView(GSDeviceVK::GetInstance()->GetDevice(), &vci, nullptr, &view); if (res != VK_SUCCESS) { LOG_VULKAN_ERROR(res, "vkCreateImageView failed: "); - vmaDestroyImage(g_vulkan_context->GetAllocator(), image, allocation); + vmaDestroyImage(GSDeviceVK::GetInstance()->GetAllocator(), image, allocation); return {}; } switch (type) { case Type::Texture: - Vulkan::SetObjectName(g_vulkan_context->GetDevice(), image, "%dx%d texture", width, height); + Vulkan::SetObjectName(GSDeviceVK::GetInstance()->GetDevice(), image, "%dx%d texture", width, height); break; case Type::RenderTarget: - Vulkan::SetObjectName(g_vulkan_context->GetDevice(), image, "%dx%d render target", width, height); + Vulkan::SetObjectName(GSDeviceVK::GetInstance()->GetDevice(), image, "%dx%d render target", width, height); break; case Type::DepthStencil: - Vulkan::SetObjectName(g_vulkan_context->GetDevice(), image, "%dx%d depth stencil", width, height); + Vulkan::SetObjectName(GSDeviceVK::GetInstance()->GetDevice(), image, "%dx%d depth stencil", width, height); break; case Type::RWTexture: - Vulkan::SetObjectName(g_vulkan_context->GetDevice(), image, "%dx%d RW texture", width, height); + Vulkan::SetObjectName(GSDeviceVK::GetInstance()->GetDevice(), image, "%dx%d RW texture", width, height); break; default: @@ -215,7 +213,7 @@ std::unique_ptr GSTextureVK::Adopt( // Memory is managed by the owner of the image. VkImageView view = VK_NULL_HANDLE; - VkResult res = vkCreateImageView(g_vulkan_context->GetDevice(), &view_info, nullptr, &view); + VkResult res = vkCreateImageView(GSDeviceVK::GetInstance()->GetDevice(), &view_info, nullptr, &view); if (res != VK_SUCCESS) { LOG_VULKAN_ERROR(res, "vkCreateImageView failed: "); @@ -248,9 +246,9 @@ void GSTextureVK::Destroy(bool defer) } if (defer) - g_vulkan_context->DeferFramebufferDestruction(fb); + GSDeviceVK::GetInstance()->DeferFramebufferDestruction(fb); else - vkDestroyFramebuffer(g_vulkan_context->GetDevice(), fb, nullptr); + vkDestroyFramebuffer(GSDeviceVK::GetInstance()->GetDevice(), fb, nullptr); } m_framebuffers.clear(); } @@ -258,9 +256,9 @@ void GSTextureVK::Destroy(bool defer) if (m_view != VK_NULL_HANDLE) { if (defer) - g_vulkan_context->DeferImageViewDestruction(m_view); + GSDeviceVK::GetInstance()->DeferImageViewDestruction(m_view); else - vkDestroyImageView(g_vulkan_context->GetDevice(), m_view, nullptr); + vkDestroyImageView(GSDeviceVK::GetInstance()->GetDevice(), m_view, nullptr); m_view = VK_NULL_HANDLE; } @@ -268,9 +266,9 @@ void GSTextureVK::Destroy(bool defer) if (m_allocation != VK_NULL_HANDLE) { if (defer) - g_vulkan_context->DeferImageDestruction(m_image, m_allocation); + GSDeviceVK::GetInstance()->DeferImageDestruction(m_image, m_allocation); else - vmaDestroyImage(g_vulkan_context->GetAllocator(), m_image, m_allocation); + vmaDestroyImage(GSDeviceVK::GetInstance()->GetAllocator(), m_image, m_allocation); m_image = VK_NULL_HANDLE; m_allocation = VK_NULL_HANDLE; } @@ -288,14 +286,14 @@ void* GSTextureVK::GetNativeHandle() const VkCommandBuffer GSTextureVK::GetCommandBufferForUpdate() { - if (m_type != Type::Texture || m_use_fence_counter == g_vulkan_context->GetCurrentFenceCounter()) + if (m_type != Type::Texture || m_use_fence_counter == GSDeviceVK::GetInstance()->GetCurrentFenceCounter()) { // Console.WriteLn("Texture update within frame, can't use do beforehand"); GSDeviceVK::GetInstance()->EndRenderPass(); - return g_vulkan_context->GetCurrentCommandBuffer(); + return GSDeviceVK::GetInstance()->GetCurrentCommandBuffer(); } - return g_vulkan_context->GetCurrentInitCommandBuffer(); + return GSDeviceVK::GetInstance()->GetCurrentInitCommandBuffer(); } void GSTextureVK::CopyTextureDataForUpload(void* dst, const void* src, u32 pitch, u32 upload_pitch, u32 height) const @@ -321,7 +319,7 @@ VkBuffer GSTextureVK::AllocateUploadStagingBuffer(const void* data, u32 pitch, u VmaAllocationInfo ai; VkBuffer buffer; VmaAllocation allocation; - VkResult res = vmaCreateBuffer(g_vulkan_context->GetAllocator(), &bci, &aci, &buffer, &allocation, &ai); + VkResult res = vmaCreateBuffer(GSDeviceVK::GetInstance()->GetAllocator(), &bci, &aci, &buffer, &allocation, &ai); if (res != VK_SUCCESS) { LOG_VULKAN_ERROR(res, "(AllocateUploadStagingBuffer) vmaCreateBuffer() failed: "); @@ -329,11 +327,11 @@ VkBuffer GSTextureVK::AllocateUploadStagingBuffer(const void* data, u32 pitch, u } // Immediately queue it for freeing after the command buffer finishes, since it's only needed for the copy. - g_vulkan_context->DeferBufferDestruction(buffer, allocation); + GSDeviceVK::GetInstance()->DeferBufferDestruction(buffer, allocation); // And write the data. CopyTextureDataForUpload(ai.pMappedData, data, pitch, upload_pitch, height); - vmaFlushAllocation(g_vulkan_context->GetAllocator(), allocation, 0, size); + vmaFlushAllocation(GSDeviceVK::GetInstance()->GetAllocator(), allocation, 0, size); return buffer; } @@ -363,14 +361,14 @@ bool GSTextureVK::Update(const GSVector4i& r, const void* data, int pitch, int l const u32 width = r.width(); const u32 height = r.height(); - const u32 upload_pitch = Common::AlignUpPow2(pitch, g_vulkan_context->GetBufferCopyRowPitchAlignment()); + const u32 upload_pitch = Common::AlignUpPow2(pitch, GSDeviceVK::GetInstance()->GetBufferCopyRowPitchAlignment()); const u32 required_size = CalcUploadSize(height, upload_pitch); // If the texture is larger than half our streaming buffer size, use a separate buffer. // Otherwise allocation will either fail, or require lots of cmdbuffer submissions. VkBuffer buffer; u32 buffer_offset; - if (required_size > (g_vulkan_context->GetTextureUploadBuffer().GetCurrentSize() / 2)) + if (required_size > (GSDeviceVK::GetInstance()->GetTextureUploadBuffer().GetCurrentSize() / 2)) { buffer_offset = 0; buffer = AllocateUploadStagingBuffer(data, pitch, upload_pitch, height); @@ -379,12 +377,12 @@ bool GSTextureVK::Update(const GSVector4i& r, const void* data, int pitch, int l } else { - VKStreamBuffer& sbuffer = g_vulkan_context->GetTextureUploadBuffer(); - if (!sbuffer.ReserveMemory(required_size, g_vulkan_context->GetBufferCopyOffsetAlignment())) + VKStreamBuffer& sbuffer = GSDeviceVK::GetInstance()->GetTextureUploadBuffer(); + if (!sbuffer.ReserveMemory(required_size, GSDeviceVK::GetInstance()->GetBufferCopyOffsetAlignment())) { GSDeviceVK::GetInstance()->ExecuteCommandBuffer( false, "While waiting for %u bytes in texture upload buffer", required_size); - if (!sbuffer.ReserveMemory(required_size, g_vulkan_context->GetBufferCopyOffsetAlignment())) + if (!sbuffer.ReserveMemory(required_size, GSDeviceVK::GetInstance()->GetBufferCopyOffsetAlignment())) { Console.Error("Failed to reserve texture upload memory (%u bytes).", required_size); return false; @@ -433,19 +431,19 @@ bool GSTextureVK::Map(GSMap& m, const GSVector4i* r, int layer) m_map_level = layer; m.pitch = - Common::AlignUpPow2(CalcUploadPitch(m_map_area.width()), g_vulkan_context->GetBufferCopyRowPitchAlignment()); + Common::AlignUpPow2(CalcUploadPitch(m_map_area.width()), GSDeviceVK::GetInstance()->GetBufferCopyRowPitchAlignment()); // see note in Update() for the reason why. const u32 required_size = CalcUploadSize(m_map_area.height(), m.pitch); - VKStreamBuffer& buffer = g_vulkan_context->GetTextureUploadBuffer(); + VKStreamBuffer& buffer = GSDeviceVK::GetInstance()->GetTextureUploadBuffer(); if (required_size >= (buffer.GetCurrentSize() / 2)) return false; - if (!buffer.ReserveMemory(required_size, g_vulkan_context->GetBufferCopyOffsetAlignment())) + if (!buffer.ReserveMemory(required_size, GSDeviceVK::GetInstance()->GetBufferCopyOffsetAlignment())) { GSDeviceVK::GetInstance()->ExecuteCommandBuffer( false, "While waiting for %u bytes in texture upload buffer", required_size); - if (!buffer.ReserveMemory(required_size, g_vulkan_context->GetBufferCopyOffsetAlignment())) + if (!buffer.ReserveMemory(required_size, GSDeviceVK::GetInstance()->GetBufferCopyOffsetAlignment())) pxFailRel("Failed to reserve texture upload memory"); } @@ -461,9 +459,9 @@ void GSTextureVK::Unmap() const u32 width = m_map_area.width(); const u32 height = m_map_area.height(); - const u32 pitch = Common::AlignUpPow2(CalcUploadPitch(width), g_vulkan_context->GetBufferCopyRowPitchAlignment()); + const u32 pitch = Common::AlignUpPow2(CalcUploadPitch(width), GSDeviceVK::GetInstance()->GetBufferCopyRowPitchAlignment()); const u32 required_size = CalcUploadSize(height, pitch); - VKStreamBuffer& buffer = g_vulkan_context->GetTextureUploadBuffer(); + VKStreamBuffer& buffer = GSDeviceVK::GetInstance()->GetTextureUploadBuffer(); const u32 buffer_offset = buffer.GetCurrentOffset(); buffer.CommitMemory(required_size); @@ -545,7 +543,7 @@ void GSTextureVK::CommitClear() GSDeviceVK::GetInstance()->EndRenderPass(); - CommitClear(g_vulkan_context->GetCurrentCommandBuffer()); + CommitClear(GSDeviceVK::GetInstance()->GetCurrentCommandBuffer()); } void GSTextureVK::CommitClear(VkCommandBuffer cmdbuf) @@ -576,7 +574,7 @@ void GSTextureVK::OverrideImageLayout(Layout new_layout) void GSTextureVK::TransitionToLayout(Layout layout) { - TransitionToLayout(g_vulkan_context->GetCurrentCommandBuffer(), layout); + TransitionToLayout(GSDeviceVK::GetInstance()->GetCurrentCommandBuffer(), layout); } void GSTextureVK::TransitionToLayout(VkCommandBuffer command_buffer, Layout new_layout) @@ -788,7 +786,7 @@ VkFramebuffer GSTextureVK::GetLinkedFramebuffer(GSTextureVK* depth_texture, bool return fb; } - const VkRenderPass rp = g_vulkan_context->GetRenderPass( + const VkRenderPass rp = GSDeviceVK::GetInstance()->GetRenderPass( (m_type != GSTexture::Type::DepthStencil) ? m_vk_format : VK_FORMAT_UNDEFINED, (m_type != GSTexture::Type::DepthStencil) ? (depth_texture ? depth_texture->m_vk_format : VK_FORMAT_UNDEFINED) : m_vk_format, @@ -804,7 +802,7 @@ VkFramebuffer GSTextureVK::GetLinkedFramebuffer(GSTextureVK* depth_texture, bool fbb.SetSize(m_size.x, m_size.y, 1); fbb.SetRenderPass(rp); - VkFramebuffer fb = fbb.Create(g_vulkan_context->GetDevice()); + VkFramebuffer fb = fbb.Create(GSDeviceVK::GetInstance()->GetDevice()); if (!fb) return VK_NULL_HANDLE; @@ -823,12 +821,12 @@ GSDownloadTextureVK::~GSDownloadTextureVK() { // Buffer was created mapped, no need to manually unmap. if (m_buffer != VK_NULL_HANDLE) - g_vulkan_context->DeferBufferDestruction(m_buffer, m_allocation); + GSDeviceVK::GetInstance()->DeferBufferDestruction(m_buffer, m_allocation); } std::unique_ptr GSDownloadTextureVK::Create(u32 width, u32 height, GSTexture::Format format) { - const u32 buffer_size = GetBufferSize(width, height, format, g_vulkan_context->GetBufferCopyRowPitchAlignment()); + const u32 buffer_size = GetBufferSize(width, height, format, GSDeviceVK::GetInstance()->GetBufferCopyRowPitchAlignment()); const VkBufferCreateInfo bci = {VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, nullptr, 0u, buffer_size, VK_BUFFER_USAGE_TRANSFER_DST_BIT, VK_SHARING_MODE_EXCLUSIVE, 0u, nullptr}; @@ -841,7 +839,7 @@ std::unique_ptr GSDownloadTextureVK::Create(u32 width, u32 VmaAllocationInfo ai = {}; VmaAllocation allocation; VkBuffer buffer; - VkResult res = vmaCreateBuffer(g_vulkan_context->GetAllocator(), &bci, &aci, &buffer, &allocation, &ai); + VkResult res = vmaCreateBuffer(GSDeviceVK::GetInstance()->GetAllocator(), &bci, &aci, &buffer, &allocation, &ai); if (res != VK_SUCCESS) { LOG_VULKAN_ERROR(res, "vmaCreateBuffer() failed: "); @@ -871,14 +869,14 @@ void GSDownloadTextureVK::CopyFromTexture( u32 copy_offset, copy_size, copy_rows; m_current_pitch = GetTransferPitch(use_transfer_pitch ? static_cast(drc.width()) : m_width, - g_vulkan_context->GetBufferCopyRowPitchAlignment()); + GSDeviceVK::GetInstance()->GetBufferCopyRowPitchAlignment()); GetTransferSize(drc, ©_offset, ©_size, ©_rows); g_perfmon.Put(GSPerfMon::Readbacks, 1); GSDeviceVK::GetInstance()->EndRenderPass(); vkTex->CommitClear(); - const VkCommandBuffer cmdbuf = g_vulkan_context->GetCurrentCommandBuffer(); + const VkCommandBuffer cmdbuf = GSDeviceVK::GetInstance()->GetCurrentCommandBuffer(); GL_INS("GSDownloadTextureVK::CopyFromTexture: {%d,%d} %ux%u", src.left, src.top, src.width(), src.height()); GSTextureVK::Layout old_layout = vkTex->GetLayout(); @@ -898,13 +896,24 @@ void GSDownloadTextureVK::CopyFromTexture( vkCmdCopyImageToBuffer(cmdbuf, vkTex->GetImage(), VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, m_buffer, 1, &image_copy); // flush gpu cache - Vulkan::BufferMemoryBarrier(cmdbuf, m_buffer, VK_ACCESS_TRANSFER_WRITE_BIT, VK_ACCESS_HOST_READ_BIT, 0, copy_size, - VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_HOST_BIT); + const VkBufferMemoryBarrier buffer_info = { + VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER, // VkStructureType sType + nullptr, // const void* pNext + VK_ACCESS_TRANSFER_WRITE_BIT, // VkAccessFlags srcAccessMask + VK_ACCESS_HOST_READ_BIT, // VkAccessFlags dstAccessMask + VK_QUEUE_FAMILY_IGNORED, // uint32_t srcQueueFamilyIndex + VK_QUEUE_FAMILY_IGNORED, // uint32_t dstQueueFamilyIndex + m_buffer, // VkBuffer buffer + 0, // VkDeviceSize offset + copy_size // VkDeviceSize size + }; + vkCmdPipelineBarrier( + cmdbuf, VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_HOST_BIT, 0, 0, nullptr, 1, &buffer_info, 0, nullptr); if (old_layout != GSTextureVK::Layout::TransferSrc) vkTex->TransitionSubresourcesToLayout(cmdbuf, src_level, 1, GSTextureVK::Layout::TransferSrc, old_layout); - m_copy_fence_counter = g_vulkan_context->GetCurrentFenceCounter(); + m_copy_fence_counter = GSDeviceVK::GetInstance()->GetCurrentFenceCounter(); m_needs_cache_invalidate = true; m_needs_flush = true; } @@ -916,7 +925,7 @@ bool GSDownloadTextureVK::Map(const GSVector4i& read_rc) { u32 copy_offset, copy_size, copy_rows; GetTransferSize(read_rc, ©_offset, ©_size, ©_rows); - vmaInvalidateAllocation(g_vulkan_context->GetAllocator(), m_allocation, copy_offset, copy_size); + vmaInvalidateAllocation(GSDeviceVK::GetInstance()->GetAllocator(), m_allocation, copy_offset, copy_size); m_needs_cache_invalidate = false; } @@ -935,12 +944,12 @@ void GSDownloadTextureVK::Flush() m_needs_flush = false; - if (g_vulkan_context->GetCompletedFenceCounter() >= m_copy_fence_counter) + if (GSDeviceVK::GetInstance()->GetCompletedFenceCounter() >= m_copy_fence_counter) return; // Need to execute command buffer. - if (g_vulkan_context->GetCurrentFenceCounter() == m_copy_fence_counter) + if (GSDeviceVK::GetInstance()->GetCurrentFenceCounter() == m_copy_fence_counter) GSDeviceVK::GetInstance()->ExecuteCommandBufferForReadback(); else - g_vulkan_context->WaitForFenceCounter(m_copy_fence_counter); + GSDeviceVK::GetInstance()->WaitForFenceCounter(m_copy_fence_counter); } diff --git a/pcsx2/GS/Renderers/Vulkan/GSTextureVK.h b/pcsx2/GS/Renderers/Vulkan/GSTextureVK.h index 7bdbb3dd66..c42ae7b1f0 100644 --- a/pcsx2/GS/Renderers/Vulkan/GSTextureVK.h +++ b/pcsx2/GS/Renderers/Vulkan/GSTextureVK.h @@ -17,7 +17,6 @@ #include "GS/GS.h" #include "GS/Renderers/Common/GSTexture.h" -#include "GS/Renderers/Vulkan/VKContext.h" #include "GS/Renderers/Vulkan/VKLoader.h" #include @@ -86,7 +85,7 @@ public: VkFramebuffer GetLinkedFramebuffer(GSTextureVK* depth_texture, bool feedback_loop); // Call when the texture is bound to the pipeline, or read from in a copy. - __fi void SetUsedThisCommandBuffer() { m_use_fence_counter = g_vulkan_context->GetCurrentFenceCounter(); } + __fi void SetUseFenceCounter(u64 counter) { m_use_fence_counter = counter; } private: GSTextureVK(Type type, Format format, int width, int height, int levels, VkImage image, VmaAllocation allocation, diff --git a/pcsx2/GS/Renderers/Vulkan/VKBuilders.cpp b/pcsx2/GS/Renderers/Vulkan/VKBuilders.cpp index 808a7bb7d1..fd8aa8e1ac 100644 --- a/pcsx2/GS/Renderers/Vulkan/VKBuilders.cpp +++ b/pcsx2/GS/Renderers/Vulkan/VKBuilders.cpp @@ -16,12 +16,117 @@ #include "PrecompiledHeader.h" #include "GS/Renderers/Vulkan/VKBuilders.h" -#include "GS/Renderers/Vulkan/VKUtil.h" #include "common/Assertions.h" #include +void Vulkan::AddPointerToChain(void* head, const void* ptr) +{ + VkBaseInStructure* last_st = static_cast(head); + while (last_st->pNext) + { + if (last_st->pNext == ptr) + return; + + last_st = const_cast(last_st->pNext); + } + + last_st->pNext = static_cast(ptr); +} + + +const char* Vulkan::VkResultToString(VkResult res) +{ + switch (res) + { + case VK_SUCCESS: + return "VK_SUCCESS"; + + case VK_NOT_READY: + return "VK_NOT_READY"; + + case VK_TIMEOUT: + return "VK_TIMEOUT"; + + case VK_EVENT_SET: + return "VK_EVENT_SET"; + + case VK_EVENT_RESET: + return "VK_EVENT_RESET"; + + case VK_INCOMPLETE: + return "VK_INCOMPLETE"; + + case VK_ERROR_OUT_OF_HOST_MEMORY: + return "VK_ERROR_OUT_OF_HOST_MEMORY"; + + case VK_ERROR_OUT_OF_DEVICE_MEMORY: + return "VK_ERROR_OUT_OF_DEVICE_MEMORY"; + + case VK_ERROR_INITIALIZATION_FAILED: + return "VK_ERROR_INITIALIZATION_FAILED"; + + case VK_ERROR_DEVICE_LOST: + return "VK_ERROR_DEVICE_LOST"; + + case VK_ERROR_MEMORY_MAP_FAILED: + return "VK_ERROR_MEMORY_MAP_FAILED"; + + case VK_ERROR_LAYER_NOT_PRESENT: + return "VK_ERROR_LAYER_NOT_PRESENT"; + + case VK_ERROR_EXTENSION_NOT_PRESENT: + return "VK_ERROR_EXTENSION_NOT_PRESENT"; + + case VK_ERROR_FEATURE_NOT_PRESENT: + return "VK_ERROR_FEATURE_NOT_PRESENT"; + + case VK_ERROR_INCOMPATIBLE_DRIVER: + return "VK_ERROR_INCOMPATIBLE_DRIVER"; + + case VK_ERROR_TOO_MANY_OBJECTS: + return "VK_ERROR_TOO_MANY_OBJECTS"; + + case VK_ERROR_FORMAT_NOT_SUPPORTED: + return "VK_ERROR_FORMAT_NOT_SUPPORTED"; + + case VK_ERROR_SURFACE_LOST_KHR: + return "VK_ERROR_SURFACE_LOST_KHR"; + + case VK_ERROR_NATIVE_WINDOW_IN_USE_KHR: + return "VK_ERROR_NATIVE_WINDOW_IN_USE_KHR"; + + case VK_SUBOPTIMAL_KHR: + return "VK_SUBOPTIMAL_KHR"; + + case VK_ERROR_OUT_OF_DATE_KHR: + return "VK_ERROR_OUT_OF_DATE_KHR"; + + case VK_ERROR_INCOMPATIBLE_DISPLAY_KHR: + return "VK_ERROR_INCOMPATIBLE_DISPLAY_KHR"; + + case VK_ERROR_VALIDATION_FAILED_EXT: + return "VK_ERROR_VALIDATION_FAILED_EXT"; + + case VK_ERROR_INVALID_SHADER_NV: + return "VK_ERROR_INVALID_SHADER_NV"; + + default: + return "UNKNOWN_VK_RESULT"; + } +} + +void Vulkan::LogVulkanResult(const char* func_name, VkResult res, const char* msg, ...) +{ + std::va_list ap; + va_start(ap, msg); + std::string real_msg = StringUtil::StdStringFromFormatV(msg, ap); + va_end(ap); + + Console.Error("(%s) %s (%d: %s)", func_name, real_msg.c_str(), static_cast(res), VkResultToString(res)); +} + Vulkan::DescriptorSetLayoutBuilder::DescriptorSetLayoutBuilder() { Clear(); diff --git a/pcsx2/GS/Renderers/Vulkan/VKBuilders.h b/pcsx2/GS/Renderers/Vulkan/VKBuilders.h index 206b4fe4a7..3fee015292 100644 --- a/pcsx2/GS/Renderers/Vulkan/VKBuilders.h +++ b/pcsx2/GS/Renderers/Vulkan/VKBuilders.h @@ -17,10 +17,26 @@ #include "GS/Renderers/Vulkan/VKLoader.h" +#include "common/StringUtil.h" + #include +#include +#include + +#ifdef _DEBUG +#define ENABLE_VULKAN_DEBUG_OBJECTS 1 +#endif + +#define LOG_VULKAN_ERROR(res, ...) ::Vulkan::LogVulkanResult(__func__, res, __VA_ARGS__) namespace Vulkan { + // Adds a structure to a chain. + void AddPointerToChain(void* head, const void* ptr); + + const char* VkResultToString(VkResult res); + void LogVulkanResult(const char* func_name, VkResult res, const char* msg, ...); + class DescriptorSetLayoutBuilder { public: @@ -329,4 +345,72 @@ namespace Vulkan VkBufferViewCreateInfo m_ci; }; + +#ifdef ENABLE_VULKAN_DEBUG_OBJECTS + + // Provides a compile-time mapping between a Vulkan-type into its matching VkObjectType + template + struct VkObjectTypeMap; + + // clang-format off + template<> struct VkObjectTypeMap { using type = VkInstance; static constexpr VkObjectType value = VK_OBJECT_TYPE_INSTANCE; }; + template<> struct VkObjectTypeMap { using type = VkPhysicalDevice; static constexpr VkObjectType value = VK_OBJECT_TYPE_PHYSICAL_DEVICE; }; + template<> struct VkObjectTypeMap { using type = VkDevice; static constexpr VkObjectType value = VK_OBJECT_TYPE_DEVICE; }; + template<> struct VkObjectTypeMap { using type = VkQueue; static constexpr VkObjectType value = VK_OBJECT_TYPE_QUEUE; }; + template<> struct VkObjectTypeMap { using type = VkSemaphore; static constexpr VkObjectType value = VK_OBJECT_TYPE_SEMAPHORE; }; + template<> struct VkObjectTypeMap { using type = VkCommandBuffer; static constexpr VkObjectType value = VK_OBJECT_TYPE_COMMAND_BUFFER; }; + template<> struct VkObjectTypeMap { using type = VkFence; static constexpr VkObjectType value = VK_OBJECT_TYPE_FENCE; }; + template<> struct VkObjectTypeMap { using type = VkDeviceMemory; static constexpr VkObjectType value = VK_OBJECT_TYPE_DEVICE_MEMORY; }; + template<> struct VkObjectTypeMap { using type = VkBuffer; static constexpr VkObjectType value = VK_OBJECT_TYPE_BUFFER; }; + template<> struct VkObjectTypeMap { using type = VkImage; static constexpr VkObjectType value = VK_OBJECT_TYPE_IMAGE; }; + template<> struct VkObjectTypeMap { using type = VkEvent; static constexpr VkObjectType value = VK_OBJECT_TYPE_EVENT; }; + template<> struct VkObjectTypeMap { using type = VkQueryPool; static constexpr VkObjectType value = VK_OBJECT_TYPE_QUERY_POOL; }; + template<> struct VkObjectTypeMap { using type = VkBufferView; static constexpr VkObjectType value = VK_OBJECT_TYPE_BUFFER_VIEW; }; + template<> struct VkObjectTypeMap { using type = VkImageView; static constexpr VkObjectType value = VK_OBJECT_TYPE_IMAGE_VIEW; }; + template<> struct VkObjectTypeMap { using type = VkShaderModule; static constexpr VkObjectType value = VK_OBJECT_TYPE_SHADER_MODULE; }; + template<> struct VkObjectTypeMap { using type = VkPipelineCache; static constexpr VkObjectType value = VK_OBJECT_TYPE_PIPELINE_CACHE; }; + template<> struct VkObjectTypeMap { using type = VkPipelineLayout; static constexpr VkObjectType value = VK_OBJECT_TYPE_PIPELINE_LAYOUT; }; + template<> struct VkObjectTypeMap { using type = VkRenderPass; static constexpr VkObjectType value = VK_OBJECT_TYPE_RENDER_PASS; }; + template<> struct VkObjectTypeMap { using type = VkPipeline; static constexpr VkObjectType value = VK_OBJECT_TYPE_PIPELINE; }; + template<> struct VkObjectTypeMap { using type = VkDescriptorSetLayout; static constexpr VkObjectType value = VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT; }; + template<> struct VkObjectTypeMap { using type = VkSampler; static constexpr VkObjectType value = VK_OBJECT_TYPE_SAMPLER; }; + template<> struct VkObjectTypeMap { using type = VkDescriptorPool; static constexpr VkObjectType value = VK_OBJECT_TYPE_DESCRIPTOR_POOL; }; + template<> struct VkObjectTypeMap { using type = VkDescriptorSet; static constexpr VkObjectType value = VK_OBJECT_TYPE_DESCRIPTOR_SET; }; + template<> struct VkObjectTypeMap { using type = VkFramebuffer; static constexpr VkObjectType value = VK_OBJECT_TYPE_FRAMEBUFFER; }; + template<> struct VkObjectTypeMap { using type = VkCommandPool; static constexpr VkObjectType value = VK_OBJECT_TYPE_COMMAND_POOL; }; + template<> struct VkObjectTypeMap { using type = VkDescriptorUpdateTemplate; static constexpr VkObjectType value = VK_OBJECT_TYPE_DESCRIPTOR_UPDATE_TEMPLATE; }; + template<> struct VkObjectTypeMap { using type = VkSurfaceKHR; static constexpr VkObjectType value = VK_OBJECT_TYPE_SURFACE_KHR; }; + template<> struct VkObjectTypeMap { using type = VkSwapchainKHR; static constexpr VkObjectType value = VK_OBJECT_TYPE_SWAPCHAIN_KHR; }; + template<> struct VkObjectTypeMap { using type = VkDebugUtilsMessengerEXT; static constexpr VkObjectType value = VK_OBJECT_TYPE_DEBUG_UTILS_MESSENGER_EXT; }; + // clang-format on + +#endif + + static inline void SetObjectName( + VkDevice device, void* object_handle, VkObjectType object_type, const char* format, va_list ap) + { +#ifdef ENABLE_VULKAN_DEBUG_OBJECTS + if (!vkSetDebugUtilsObjectNameEXT) + { + return; + } + + const std::string str(StringUtil::StdStringFromFormatV(format, ap)); + const VkDebugUtilsObjectNameInfoEXT nameInfo{VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT, nullptr, + object_type, reinterpret_cast(object_handle), str.c_str()}; + vkSetDebugUtilsObjectNameEXT(device, &nameInfo); +#endif + } + + template + static inline void SetObjectName(VkDevice device, T object_handle, const char* format, ...) + { +#ifdef ENABLE_VULKAN_DEBUG_OBJECTS + std::va_list ap; + va_start(ap, format); + SetObjectName(device, reinterpret_cast((typename VkObjectTypeMap::type)object_handle), + VkObjectTypeMap::value, format, ap); + va_end(ap); +#endif + } } // namespace Vulkan \ No newline at end of file diff --git a/pcsx2/GS/Renderers/Vulkan/VKContext.cpp b/pcsx2/GS/Renderers/Vulkan/VKContext.cpp deleted file mode 100644 index 6dfe37561f..0000000000 --- a/pcsx2/GS/Renderers/Vulkan/VKContext.cpp +++ /dev/null @@ -1,2165 +0,0 @@ -/* PCSX2 - PS2 Emulator for PCs - * Copyright (C) 2002-2023 PCSX2 Dev Team - * - * PCSX2 is free software: you can redistribute it and/or modify it under the terms - * of the GNU Lesser General Public License as published by the Free Software Found- - * ation, either version 3 of the License, or (at your option) any later version. - * - * PCSX2 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 PCSX2. - * If not, see . - */ - -#include "PrecompiledHeader.h" - -#include "GS/Renderers/Vulkan/VKContext.h" -#include "GS/Renderers/Vulkan/VKShaderCache.h" -#include "GS/Renderers/Vulkan/VKSwapChain.h" -#include "GS/Renderers/Vulkan/VKUtil.h" - -#include "common/Align.h" -#include "common/Assertions.h" -#include "common/Console.h" -#include "common/General.h" -#include "common/StringUtil.h" - -#include -#include -#include - -#include "fmt/format.h" - -#ifdef _WIN32 -#include "common/RedtapeWindows.h" -#else -#include -#endif - -std::unique_ptr g_vulkan_context; - -// Tweakables -enum : u32 -{ - MAX_DRAW_CALLS_PER_FRAME = 8192, - MAX_COMBINED_IMAGE_SAMPLER_DESCRIPTORS_PER_FRAME = 2 * MAX_DRAW_CALLS_PER_FRAME, - MAX_SAMPLED_IMAGE_DESCRIPTORS_PER_FRAME = - MAX_DRAW_CALLS_PER_FRAME, // assume at least half our draws aren't going to be shuffle/blending - MAX_STORAGE_IMAGE_DESCRIPTORS_PER_FRAME = 4, // Currently used by CAS only - MAX_INPUT_ATTACHMENT_IMAGE_DESCRIPTORS_PER_FRAME = MAX_DRAW_CALLS_PER_FRAME, - MAX_DESCRIPTOR_SETS_PER_FRAME = MAX_DRAW_CALLS_PER_FRAME * 2 -}; - -VKContext::VKContext(VkInstance instance, VkPhysicalDevice physical_device) - : m_instance(instance) - , m_physical_device(physical_device) -{ - // Read device physical memory properties, we need it for allocating buffers - vkGetPhysicalDeviceProperties(physical_device, &m_device_properties); - vkGetPhysicalDeviceMemoryProperties(physical_device, &m_device_memory_properties); - - // We need this to be at least 32 byte aligned for AVX2 stores. - m_device_properties.limits.minUniformBufferOffsetAlignment = - std::max(m_device_properties.limits.minUniformBufferOffsetAlignment, static_cast(32)); - m_device_properties.limits.minTexelBufferOffsetAlignment = - std::max(m_device_properties.limits.minTexelBufferOffsetAlignment, static_cast(32)); - m_device_properties.limits.optimalBufferCopyOffsetAlignment = - std::max(m_device_properties.limits.optimalBufferCopyOffsetAlignment, static_cast(32)); - m_device_properties.limits.optimalBufferCopyRowPitchAlignment = Common::NextPow2( - std::max(m_device_properties.limits.optimalBufferCopyRowPitchAlignment, static_cast(32))); - m_device_properties.limits.bufferImageGranularity = - std::max(m_device_properties.limits.bufferImageGranularity, static_cast(32)); -} - -VKContext::~VKContext() = default; - -VkInstance VKContext::CreateVulkanInstance(const WindowInfo& wi, bool enable_debug_utils, bool enable_validation_layer) -{ - ExtensionList enabled_extensions; - if (!SelectInstanceExtensions(&enabled_extensions, wi, enable_debug_utils)) - return VK_NULL_HANDLE; - - // Remember to manually update this every release. We don't pull in svnrev.h here, because - // it's only the major/minor version, and rebuilding the file every time something else changes - // is unnecessary. - VkApplicationInfo app_info = {}; - app_info.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO; - app_info.pNext = nullptr; - app_info.pApplicationName = "PCSX2"; - app_info.applicationVersion = VK_MAKE_VERSION(1, 7, 0); - app_info.pEngineName = "PCSX2"; - app_info.engineVersion = VK_MAKE_VERSION(1, 7, 0); - app_info.apiVersion = VK_API_VERSION_1_1; - - VkInstanceCreateInfo instance_create_info = {}; - instance_create_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO; - instance_create_info.pNext = nullptr; - instance_create_info.flags = 0; - instance_create_info.pApplicationInfo = &app_info; - instance_create_info.enabledExtensionCount = static_cast(enabled_extensions.size()); - instance_create_info.ppEnabledExtensionNames = enabled_extensions.data(); - instance_create_info.enabledLayerCount = 0; - instance_create_info.ppEnabledLayerNames = nullptr; - - // Enable debug layer on debug builds - if (enable_validation_layer) - { - static const char* layer_names[] = {"VK_LAYER_KHRONOS_validation"}; - instance_create_info.enabledLayerCount = 1; - instance_create_info.ppEnabledLayerNames = layer_names; - } - - VkInstance instance; - VkResult res = vkCreateInstance(&instance_create_info, nullptr, &instance); - if (res != VK_SUCCESS) - { - LOG_VULKAN_ERROR(res, "vkCreateInstance failed: "); - return nullptr; - } - - return instance; -} - -bool VKContext::SelectInstanceExtensions(ExtensionList* extension_list, const WindowInfo& wi, bool enable_debug_utils) -{ - u32 extension_count = 0; - VkResult res = vkEnumerateInstanceExtensionProperties(nullptr, &extension_count, nullptr); - if (res != VK_SUCCESS) - { - LOG_VULKAN_ERROR(res, "vkEnumerateInstanceExtensionProperties failed: "); - return false; - } - - if (extension_count == 0) - { - Console.Error("Vulkan: No extensions supported by instance."); - return false; - } - - std::vector available_extension_list(extension_count); - res = vkEnumerateInstanceExtensionProperties(nullptr, &extension_count, available_extension_list.data()); - pxAssert(res == VK_SUCCESS); - - auto SupportsExtension = [&](const char* name, bool required) { - if (std::find_if(available_extension_list.begin(), available_extension_list.end(), - [&](const VkExtensionProperties& properties) { return !strcmp(name, properties.extensionName); }) != - available_extension_list.end()) - { - DevCon.WriteLn("Enabling extension: %s", name); - extension_list->push_back(name); - return true; - } - - if (required) - Console.Error("Vulkan: Missing required extension %s.", name); - - return false; - }; - - // Common extensions - if (wi.type != WindowInfo::Type::Surfaceless && !SupportsExtension(VK_KHR_SURFACE_EXTENSION_NAME, true)) - return false; - -#if defined(VK_USE_PLATFORM_WIN32_KHR) - if (wi.type == WindowInfo::Type::Win32 && !SupportsExtension(VK_KHR_WIN32_SURFACE_EXTENSION_NAME, true)) - return false; -#endif -#if defined(VK_USE_PLATFORM_XLIB_KHR) - if (wi.type == WindowInfo::Type::X11 && !SupportsExtension(VK_KHR_XLIB_SURFACE_EXTENSION_NAME, true)) - return false; -#endif -#if defined(VK_USE_PLATFORM_WAYLAND_KHR) - if (wi.type == WindowInfo::Type::Wayland && !SupportsExtension(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME, true)) - return false; -#endif -#if defined(VK_USE_PLATFORM_METAL_EXT) - if (wi.type == WindowInfo::Type::MacOS && !SupportsExtension(VK_EXT_METAL_SURFACE_EXTENSION_NAME, true)) - return false; -#endif - - // VK_EXT_debug_utils - if (enable_debug_utils && !SupportsExtension(VK_EXT_DEBUG_UTILS_EXTENSION_NAME, false)) - Console.Warning("Vulkan: Debug report requested, but extension is not available."); - - // Needed for exclusive fullscreen control. - SupportsExtension(VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME, false); - - return true; -} - -VKContext::GPUList VKContext::EnumerateGPUs(VkInstance instance) -{ - GPUList gpus; - - u32 gpu_count = 0; - VkResult res = vkEnumeratePhysicalDevices(instance, &gpu_count, nullptr); - if ((res != VK_SUCCESS && res != VK_INCOMPLETE) || gpu_count == 0) - { - LOG_VULKAN_ERROR(res, "vkEnumeratePhysicalDevices (1) failed: "); - return gpus; - } - - std::vector physical_devices(gpu_count); - res = vkEnumeratePhysicalDevices(instance, &gpu_count, physical_devices.data()); - if (res == VK_INCOMPLETE) - { - Console.Warning("First vkEnumeratePhysicalDevices() call returned %zu devices, but second returned %u", - physical_devices.size(), gpu_count); - } - else if (res != VK_SUCCESS) - { - LOG_VULKAN_ERROR(res, "vkEnumeratePhysicalDevices (2) failed: "); - return gpus; - } - - // Maybe we lost a GPU? - if (gpu_count < physical_devices.size()) - physical_devices.resize(gpu_count); - - gpus.reserve(physical_devices.size()); - for (VkPhysicalDevice device : physical_devices) - { - VkPhysicalDeviceProperties props = {}; - vkGetPhysicalDeviceProperties(device, &props); - - std::string gpu_name = props.deviceName; - - // handle duplicate adapter names - if (std::any_of( - gpus.begin(), gpus.end(), [&gpu_name](const auto& other) { return (gpu_name == other.second); })) - { - std::string original_adapter_name = std::move(gpu_name); - - u32 current_extra = 2; - do - { - gpu_name = fmt::format("{} ({})", original_adapter_name, current_extra); - current_extra++; - } while (std::any_of( - gpus.begin(), gpus.end(), [&gpu_name](const auto& other) { return (gpu_name == other.second); })); - } - - gpus.emplace_back(device, std::move(gpu_name)); - } - - return gpus; -} - -bool VKContext::Create(VkInstance instance, VkSurfaceKHR surface, VkPhysicalDevice physical_device, - bool threaded_presentation, bool enable_debug_utils, bool enable_validation_layer) -{ - pxAssertMsg(!g_vulkan_context, "Has no current context"); - g_vulkan_context.reset(new VKContext(instance, physical_device)); - - if (enable_debug_utils) - g_vulkan_context->EnableDebugUtils(); - - // Attempt to create the device. - if (!g_vulkan_context->CreateDevice(surface, enable_validation_layer, nullptr, 0, nullptr, 0, nullptr) || - !VKShaderCache::Create() || !g_vulkan_context->CreateAllocator() || - !g_vulkan_context->CreateGlobalDescriptorPool() || !g_vulkan_context->CreateCommandBuffers() || - !g_vulkan_context->CreateTextureStreamBuffer() || !g_vulkan_context->InitSpinResources()) - { - // Since we are destroying the instance, we're also responsible for destroying the surface. - if (surface != VK_NULL_HANDLE) - vkDestroySurfaceKHR(instance, surface, nullptr); - - // TODO: Move this to the destructor instead.. - g_vulkan_context->m_texture_upload_buffer.Destroy(false); - g_vulkan_context->DestroySpinResources(); - g_vulkan_context->DestroyRenderPassCache(); - g_vulkan_context->DestroyGlobalDescriptorPool(); - g_vulkan_context->DestroyCommandBuffers(); - g_vulkan_context->DestroyAllocator(); - VKShaderCache::Destroy(); - - if (g_vulkan_context->m_device != VK_NULL_HANDLE) - vkDestroyDevice(g_vulkan_context->m_device, nullptr); - - if (g_vulkan_context->m_debug_messenger_callback != VK_NULL_HANDLE) - g_vulkan_context->DisableDebugUtils(); - - if (g_vulkan_context->m_instance != VK_NULL_HANDLE) - vkDestroyInstance(g_vulkan_context->m_instance, nullptr); - - g_vulkan_context.reset(); - return false; - } - - if (threaded_presentation) - g_vulkan_context->StartPresentThread(); - - return true; -} - -void VKContext::Destroy() -{ - pxAssertMsg(g_vulkan_context, "Has context"); - - g_vulkan_context->StopPresentThread(); - - if (g_vulkan_context->m_device != VK_NULL_HANDLE) - g_vulkan_context->WaitForGPUIdle(); - - g_vulkan_context->m_texture_upload_buffer.Destroy(false); - - g_vulkan_context->DestroySpinResources(); - g_vulkan_context->DestroyRenderPassCache(); - g_vulkan_context->DestroyGlobalDescriptorPool(); - g_vulkan_context->DestroyCommandBuffers(); - g_vulkan_context->DestroyAllocator(); - - VKShaderCache::Destroy(); - - if (g_vulkan_context->m_device != VK_NULL_HANDLE) - vkDestroyDevice(g_vulkan_context->m_device, nullptr); - - if (g_vulkan_context->m_debug_messenger_callback != VK_NULL_HANDLE) - g_vulkan_context->DisableDebugUtils(); - - if (g_vulkan_context->m_instance != VK_NULL_HANDLE) - vkDestroyInstance(g_vulkan_context->m_instance, nullptr); - - Vulkan::UnloadVulkanLibrary(); - - g_vulkan_context.reset(); -} - -bool VKContext::SelectDeviceExtensions(ExtensionList* extension_list, bool enable_surface) -{ - u32 extension_count = 0; - VkResult res = vkEnumerateDeviceExtensionProperties(m_physical_device, nullptr, &extension_count, nullptr); - if (res != VK_SUCCESS) - { - LOG_VULKAN_ERROR(res, "vkEnumerateDeviceExtensionProperties failed: "); - return false; - } - - if (extension_count == 0) - { - Console.Error("Vulkan: No extensions supported by device."); - return false; - } - - std::vector available_extension_list(extension_count); - res = vkEnumerateDeviceExtensionProperties( - m_physical_device, nullptr, &extension_count, available_extension_list.data()); - pxAssert(res == VK_SUCCESS); - - auto SupportsExtension = [&](const char* name, bool required) { - if (std::find_if(available_extension_list.begin(), available_extension_list.end(), - [&](const VkExtensionProperties& properties) { return !strcmp(name, properties.extensionName); }) != - available_extension_list.end()) - { - if (std::none_of(extension_list->begin(), extension_list->end(), - [&](const char* existing_name) { return (std::strcmp(existing_name, name) == 0); })) - { - DevCon.WriteLn("Enabling extension: %s", name); - extension_list->push_back(name); - } - - return true; - } - - if (required) - Console.Error("Vulkan: Missing required extension %s.", name); - - return false; - }; - - if (enable_surface && !SupportsExtension(VK_KHR_SWAPCHAIN_EXTENSION_NAME, true)) - return false; - - m_optional_extensions.vk_ext_provoking_vertex = SupportsExtension(VK_EXT_PROVOKING_VERTEX_EXTENSION_NAME, false); - m_optional_extensions.vk_ext_memory_budget = SupportsExtension(VK_EXT_MEMORY_BUDGET_EXTENSION_NAME, false); - m_optional_extensions.vk_ext_calibrated_timestamps = - SupportsExtension(VK_EXT_CALIBRATED_TIMESTAMPS_EXTENSION_NAME, false); - m_optional_extensions.vk_ext_line_rasterization = - SupportsExtension(VK_EXT_LINE_RASTERIZATION_EXTENSION_NAME, false); - m_optional_extensions.vk_ext_rasterization_order_attachment_access = - SupportsExtension(VK_EXT_RASTERIZATION_ORDER_ATTACHMENT_ACCESS_EXTENSION_NAME, false) || - SupportsExtension(VK_ARM_RASTERIZATION_ORDER_ATTACHMENT_ACCESS_EXTENSION_NAME, false); - m_optional_extensions.vk_khr_driver_properties = SupportsExtension(VK_KHR_DRIVER_PROPERTIES_EXTENSION_NAME, false); - m_optional_extensions.vk_khr_fragment_shader_barycentric = - SupportsExtension(VK_KHR_FRAGMENT_SHADER_BARYCENTRIC_EXTENSION_NAME, false); - m_optional_extensions.vk_khr_shader_draw_parameters = - SupportsExtension(VK_KHR_SHADER_DRAW_PARAMETERS_EXTENSION_NAME, false); - m_optional_extensions.vk_ext_attachment_feedback_loop_layout = - SupportsExtension(VK_EXT_ATTACHMENT_FEEDBACK_LOOP_LAYOUT_EXTENSION_NAME, false); - -#ifdef _WIN32 - m_optional_extensions.vk_ext_full_screen_exclusive = - enable_surface && SupportsExtension(VK_EXT_FULL_SCREEN_EXCLUSIVE_EXTENSION_NAME, false); -#endif - - return true; -} - -bool VKContext::SelectDeviceFeatures(const VkPhysicalDeviceFeatures* required_features) -{ - VkPhysicalDeviceFeatures available_features; - vkGetPhysicalDeviceFeatures(m_physical_device, &available_features); - - if (required_features) - std::memcpy(&m_device_features, required_features, sizeof(m_device_features)); - - // Enable the features we use. - m_device_features.dualSrcBlend = available_features.dualSrcBlend; - m_device_features.largePoints = available_features.largePoints; - m_device_features.wideLines = available_features.wideLines; - m_device_features.fragmentStoresAndAtomics = available_features.fragmentStoresAndAtomics; - m_device_features.textureCompressionBC = available_features.textureCompressionBC; - m_device_features.samplerAnisotropy = available_features.samplerAnisotropy; - m_device_features.geometryShader = available_features.geometryShader; - - return true; -} - -bool VKContext::CreateDevice(VkSurfaceKHR surface, bool enable_validation_layer, - const char** required_device_extensions, u32 num_required_device_extensions, const char** required_device_layers, - u32 num_required_device_layers, const VkPhysicalDeviceFeatures* required_features) -{ - u32 queue_family_count; - vkGetPhysicalDeviceQueueFamilyProperties(m_physical_device, &queue_family_count, nullptr); - if (queue_family_count == 0) - { - Console.Error("No queue families found on specified vulkan physical device."); - return false; - } - - std::vector queue_family_properties(queue_family_count); - vkGetPhysicalDeviceQueueFamilyProperties(m_physical_device, &queue_family_count, queue_family_properties.data()); - Console.WriteLn("%u vulkan queue families", queue_family_count); - - // Find graphics and present queues. - m_graphics_queue_family_index = queue_family_count; - m_present_queue_family_index = queue_family_count; - m_spin_queue_family_index = queue_family_count; - u32 spin_queue_index = 0; - for (uint32_t i = 0; i < queue_family_count; i++) - { - VkBool32 graphics_supported = queue_family_properties[i].queueFlags & VK_QUEUE_GRAPHICS_BIT; - if (graphics_supported) - { - m_graphics_queue_family_index = i; - // Quit now, no need for a present queue. - if (!surface) - { - break; - } - } - - if (surface) - { - VkBool32 present_supported; - VkResult res = vkGetPhysicalDeviceSurfaceSupportKHR(m_physical_device, i, surface, &present_supported); - if (res != VK_SUCCESS) - { - LOG_VULKAN_ERROR(res, "vkGetPhysicalDeviceSurfaceSupportKHR failed: "); - return false; - } - - if (present_supported) - { - m_present_queue_family_index = i; - } - - // Prefer one queue family index that does both graphics and present. - if (graphics_supported && present_supported) - { - break; - } - } - } - for (uint32_t i = 0; i < queue_family_count; i++) - { - // Pick a queue for spinning - if (!(queue_family_properties[i].queueFlags & VK_QUEUE_COMPUTE_BIT)) - continue; // We need compute - if (queue_family_properties[i].timestampValidBits == 0) - continue; // We need timing - const bool queue_is_used = i == m_graphics_queue_family_index || i == m_present_queue_family_index; - if (queue_is_used && m_spin_queue_family_index != queue_family_count) - continue; // Found a non-graphics queue to use - spin_queue_index = 0; - m_spin_queue_family_index = i; - if (queue_is_used && queue_family_properties[i].queueCount > 1) - spin_queue_index = 1; - if (!(queue_family_properties[i].queueFlags & VK_QUEUE_GRAPHICS_BIT)) - break; // Async compute queue, definitely pick this one - } - if (m_graphics_queue_family_index == queue_family_count) - { - Console.Error("Vulkan: Failed to find an acceptable graphics queue."); - return false; - } - if (surface != VK_NULL_HANDLE && m_present_queue_family_index == queue_family_count) - { - Console.Error("Vulkan: Failed to find an acceptable present queue."); - return false; - } - - VkDeviceCreateInfo device_info = {}; - device_info.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO; - device_info.pNext = nullptr; - device_info.flags = 0; - device_info.queueCreateInfoCount = 0; - - static constexpr float queue_priorities[] = {1.0f, 0.0f}; // Low priority for the spin queue - std::array queue_infos; - VkDeviceQueueCreateInfo& graphics_queue_info = queue_infos[device_info.queueCreateInfoCount++]; - graphics_queue_info.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO; - graphics_queue_info.pNext = nullptr; - graphics_queue_info.flags = 0; - graphics_queue_info.queueFamilyIndex = m_graphics_queue_family_index; - graphics_queue_info.queueCount = 1; - graphics_queue_info.pQueuePriorities = queue_priorities; - - if (surface != VK_NULL_HANDLE && m_graphics_queue_family_index != m_present_queue_family_index) - { - VkDeviceQueueCreateInfo& present_queue_info = queue_infos[device_info.queueCreateInfoCount++]; - present_queue_info.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO; - present_queue_info.pNext = nullptr; - present_queue_info.flags = 0; - present_queue_info.queueFamilyIndex = m_present_queue_family_index; - present_queue_info.queueCount = 1; - present_queue_info.pQueuePriorities = queue_priorities; - } - - if (m_spin_queue_family_index == m_graphics_queue_family_index) - { - if (spin_queue_index != 0) - graphics_queue_info.queueCount = 2; - } - else if (m_spin_queue_family_index == m_present_queue_family_index) - { - if (spin_queue_index != 0) - queue_infos[1].queueCount = 2; // present queue - } - else - { - VkDeviceQueueCreateInfo& spin_queue_info = queue_infos[device_info.queueCreateInfoCount++]; - spin_queue_info.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO; - spin_queue_info.pNext = nullptr; - spin_queue_info.flags = 0; - spin_queue_info.queueFamilyIndex = m_spin_queue_family_index; - spin_queue_info.queueCount = 1; - spin_queue_info.pQueuePriorities = queue_priorities + 1; - } - - device_info.pQueueCreateInfos = queue_infos.data(); - - ExtensionList enabled_extensions; - for (u32 i = 0; i < num_required_device_extensions; i++) - enabled_extensions.emplace_back(required_device_extensions[i]); - if (!SelectDeviceExtensions(&enabled_extensions, surface != VK_NULL_HANDLE)) - return false; - - device_info.enabledLayerCount = num_required_device_layers; - device_info.ppEnabledLayerNames = required_device_layers; - device_info.enabledExtensionCount = static_cast(enabled_extensions.size()); - device_info.ppEnabledExtensionNames = enabled_extensions.data(); - - // Check for required features before creating. - if (!SelectDeviceFeatures(required_features)) - return false; - - device_info.pEnabledFeatures = &m_device_features; - - // Enable debug layer on debug builds - if (enable_validation_layer) - { - static const char* layer_names[] = {"VK_LAYER_LUNARG_standard_validation"}; - device_info.enabledLayerCount = 1; - device_info.ppEnabledLayerNames = layer_names; - } - - // provoking vertex - VkPhysicalDeviceProvokingVertexFeaturesEXT provoking_vertex_feature = { - VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROVOKING_VERTEX_FEATURES_EXT}; - VkPhysicalDeviceRasterizationOrderAttachmentAccessFeaturesEXT rasterization_order_access_feature = { - VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_RASTERIZATION_ORDER_ATTACHMENT_ACCESS_FEATURES_EXT}; - VkPhysicalDeviceLineRasterizationFeaturesEXT line_rasterization_feature = { - VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_LINE_RASTERIZATION_FEATURES_EXT}; - VkPhysicalDeviceAttachmentFeedbackLoopLayoutFeaturesEXT attachment_feedback_loop_feature = { - VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ATTACHMENT_FEEDBACK_LOOP_LAYOUT_FEATURES_EXT}; - - if (m_optional_extensions.vk_ext_provoking_vertex) - { - provoking_vertex_feature.provokingVertexLast = VK_TRUE; - Vulkan::AddPointerToChain(&device_info, &provoking_vertex_feature); - } - if (m_optional_extensions.vk_ext_line_rasterization) - { - line_rasterization_feature.bresenhamLines = VK_TRUE; - Vulkan::AddPointerToChain(&device_info, &line_rasterization_feature); - } - if (m_optional_extensions.vk_ext_rasterization_order_attachment_access) - { - rasterization_order_access_feature.rasterizationOrderColorAttachmentAccess = VK_TRUE; - Vulkan::AddPointerToChain(&device_info, &rasterization_order_access_feature); - } - if (m_optional_extensions.vk_ext_attachment_feedback_loop_layout) - { - attachment_feedback_loop_feature.attachmentFeedbackLoopLayout = VK_TRUE; - Vulkan::AddPointerToChain(&device_info, &attachment_feedback_loop_feature); - } - - VkResult res = vkCreateDevice(m_physical_device, &device_info, nullptr, &m_device); - if (res != VK_SUCCESS) - { - LOG_VULKAN_ERROR(res, "vkCreateDevice failed: "); - return false; - } - - // With the device created, we can fill the remaining entry points. - if (!Vulkan::LoadVulkanDeviceFunctions(m_device)) - return false; - - // Grab the graphics and present queues. - vkGetDeviceQueue(m_device, m_graphics_queue_family_index, 0, &m_graphics_queue); - if (surface) - { - vkGetDeviceQueue(m_device, m_present_queue_family_index, 0, &m_present_queue); - } - m_spinning_supported = m_spin_queue_family_index != queue_family_count && - queue_family_properties[m_graphics_queue_family_index].timestampValidBits > 0 && - m_device_properties.limits.timestampPeriod > 0; - m_spin_queue_is_graphics_queue = - m_spin_queue_family_index == m_graphics_queue_family_index && spin_queue_index == 0; - - m_gpu_timing_supported = (m_device_properties.limits.timestampComputeAndGraphics != 0 && - queue_family_properties[m_graphics_queue_family_index].timestampValidBits > 0 && - m_device_properties.limits.timestampPeriod > 0); - DevCon.WriteLn("GPU timing is %s (TS=%u TS valid bits=%u, TS period=%f)", - m_gpu_timing_supported ? "supported" : "not supported", - static_cast(m_device_properties.limits.timestampComputeAndGraphics), - queue_family_properties[m_graphics_queue_family_index].timestampValidBits, - m_device_properties.limits.timestampPeriod); - - ProcessDeviceExtensions(); - - if (m_spinning_supported) - { - vkGetDeviceQueue(m_device, m_spin_queue_family_index, spin_queue_index, &m_spin_queue); - - m_spin_timestamp_scale = m_device_properties.limits.timestampPeriod; - if (m_optional_extensions.vk_ext_calibrated_timestamps) - { -#ifdef _WIN32 - LARGE_INTEGER Freq; - QueryPerformanceFrequency(&Freq); - m_queryperfcounter_to_ns = 1000000000.0 / static_cast(Freq.QuadPart); -#endif - CalibrateSpinTimestamp(); - } - } - - return true; -} - -void VKContext::ProcessDeviceExtensions() -{ - // advanced feature checks - VkPhysicalDeviceFeatures2 features2 = {VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2}; - VkPhysicalDeviceProvokingVertexFeaturesEXT provoking_vertex_features = { - VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROVOKING_VERTEX_FEATURES_EXT}; - VkPhysicalDeviceLineRasterizationFeaturesEXT line_rasterization_feature = { - VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_LINE_RASTERIZATION_FEATURES_EXT}; - VkPhysicalDeviceRasterizationOrderAttachmentAccessFeaturesEXT rasterization_order_access_feature = { - VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_RASTERIZATION_ORDER_ATTACHMENT_ACCESS_FEATURES_EXT}; - VkPhysicalDeviceAttachmentFeedbackLoopLayoutFeaturesEXT attachment_feedback_loop_feature = { - VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ATTACHMENT_FEEDBACK_LOOP_LAYOUT_FEATURES_EXT}; - - // add in optional feature structs - if (m_optional_extensions.vk_ext_provoking_vertex) - Vulkan::AddPointerToChain(&features2, &provoking_vertex_features); - if (m_optional_extensions.vk_ext_line_rasterization) - Vulkan::AddPointerToChain(&features2, &line_rasterization_feature); - if (m_optional_extensions.vk_ext_rasterization_order_attachment_access) - Vulkan::AddPointerToChain(&features2, &rasterization_order_access_feature); - if (m_optional_extensions.vk_ext_attachment_feedback_loop_layout) - Vulkan::AddPointerToChain(&features2, &attachment_feedback_loop_feature); - - // query - vkGetPhysicalDeviceFeatures2(m_physical_device, &features2); - - // confirm we actually support it - m_optional_extensions.vk_ext_provoking_vertex &= (provoking_vertex_features.provokingVertexLast == VK_TRUE); - m_optional_extensions.vk_ext_rasterization_order_attachment_access &= - (rasterization_order_access_feature.rasterizationOrderColorAttachmentAccess == VK_TRUE); - m_optional_extensions.vk_ext_line_rasterization &= (line_rasterization_feature.bresenhamLines == VK_TRUE); - m_optional_extensions.vk_ext_attachment_feedback_loop_layout &= - (attachment_feedback_loop_feature.attachmentFeedbackLoopLayout == VK_TRUE); - - VkPhysicalDeviceProperties2 properties2 = {VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2}; - void** pNext = &properties2.pNext; - - if (m_optional_extensions.vk_khr_driver_properties) - { - m_device_driver_properties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRIVER_PROPERTIES; - *pNext = &m_device_driver_properties; - pNext = &m_device_driver_properties.pNext; - } - - // query - vkGetPhysicalDeviceProperties2(m_physical_device, &properties2); - - // VK_EXT_calibrated_timestamps checking - if (m_optional_extensions.vk_ext_calibrated_timestamps) - { - u32 count = 0; - vkGetPhysicalDeviceCalibrateableTimeDomainsEXT(m_physical_device, &count, nullptr); - std::unique_ptr time_domains = std::make_unique(count); - vkGetPhysicalDeviceCalibrateableTimeDomainsEXT(m_physical_device, &count, time_domains.get()); - const VkTimeDomainEXT* begin = &time_domains[0]; - const VkTimeDomainEXT* end = &time_domains[count]; - if (std::find(begin, end, VK_TIME_DOMAIN_DEVICE_EXT) == end) - m_optional_extensions.vk_ext_calibrated_timestamps = false; - VkTimeDomainEXT preferred_types[] = { -#ifdef _WIN32 - VK_TIME_DOMAIN_QUERY_PERFORMANCE_COUNTER_EXT, -#else -#ifdef CLOCK_MONOTONIC_RAW - VK_TIME_DOMAIN_CLOCK_MONOTONIC_RAW_EXT, -#endif - VK_TIME_DOMAIN_CLOCK_MONOTONIC_EXT, -#endif - }; - m_calibrated_timestamp_type = VK_TIME_DOMAIN_DEVICE_EXT; - for (VkTimeDomainEXT type : preferred_types) - { - if (std::find(begin, end, type) != end) - { - m_calibrated_timestamp_type = type; - break; - } - } - if (m_calibrated_timestamp_type == VK_TIME_DOMAIN_DEVICE_EXT) - m_optional_extensions.vk_ext_calibrated_timestamps = false; - } - - Console.WriteLn( - "VK_EXT_provoking_vertex is %s", m_optional_extensions.vk_ext_provoking_vertex ? "supported" : "NOT supported"); - Console.WriteLn("VK_EXT_line_rasterization is %s", - m_optional_extensions.vk_ext_line_rasterization ? "supported" : "NOT supported"); - Console.WriteLn("VK_EXT_calibrated_timestamps is %s", - m_optional_extensions.vk_ext_calibrated_timestamps ? "supported" : "NOT supported"); - Console.WriteLn("VK_EXT_rasterization_order_attachment_access is %s", - m_optional_extensions.vk_ext_rasterization_order_attachment_access ? "supported" : "NOT supported"); - Console.WriteLn("VK_EXT_attachment_feedback_loop_layout is %s", - m_optional_extensions.vk_ext_attachment_feedback_loop_layout ? "supported" : "NOT supported"); -} - -bool VKContext::CreateAllocator() -{ - VmaAllocatorCreateInfo ci = {}; - ci.vulkanApiVersion = VK_API_VERSION_1_1; - ci.flags = VMA_ALLOCATOR_CREATE_EXTERNALLY_SYNCHRONIZED_BIT; - ci.physicalDevice = m_physical_device; - ci.device = m_device; - ci.instance = m_instance; - - if (m_optional_extensions.vk_ext_memory_budget) - ci.flags |= VMA_ALLOCATOR_CREATE_EXT_MEMORY_BUDGET_BIT; - - // Limit usage of the DEVICE_LOCAL upload heap when we're using a debug device. - // On NVIDIA drivers, it results in frequently running out of device memory when trying to - // play back captures in RenderDoc, making life very painful. Re-BAR GPUs should be fine. - constexpr VkDeviceSize UPLOAD_HEAP_SIZE_THRESHOLD = 512 * 1024 * 1024; - constexpr VkMemoryPropertyFlags UPLOAD_HEAP_PROPERTIES = - VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT; - std::array heap_size_limits; - if (GSConfig.UseDebugDevice) - { - bool has_upload_heap = false; - heap_size_limits.fill(VK_WHOLE_SIZE); - for (u32 i = 0; i < m_device_memory_properties.memoryTypeCount; i++) - { - // Look for any memory types which are upload-like. - const VkMemoryType& type = m_device_memory_properties.memoryTypes[i]; - if ((type.propertyFlags & UPLOAD_HEAP_PROPERTIES) != UPLOAD_HEAP_PROPERTIES) - continue; - - const VkMemoryHeap& heap = m_device_memory_properties.memoryHeaps[type.heapIndex]; - if (heap.size >= UPLOAD_HEAP_SIZE_THRESHOLD) - continue; - - if (heap_size_limits[type.heapIndex] == VK_WHOLE_SIZE) - { - Console.Warning("Disabling allocation from upload heap #%u (%.2f MB) due to debug device.", - type.heapIndex, static_cast(heap.size) / 1048576.0f); - heap_size_limits[type.heapIndex] = 0; - has_upload_heap = true; - } - } - - if (has_upload_heap) - ci.pHeapSizeLimit = heap_size_limits.data(); - } - - VkResult res = vmaCreateAllocator(&ci, &m_allocator); - if (res != VK_SUCCESS) - { - LOG_VULKAN_ERROR(res, "vmaCreateAllocator failed: "); - return false; - } - - return true; -} - -void VKContext::DestroyAllocator() -{ - if (m_allocator == VK_NULL_HANDLE) - return; - - vmaDestroyAllocator(m_allocator); - m_allocator = VK_NULL_HANDLE; -} - -bool VKContext::CreateCommandBuffers() -{ - VkResult res; - - uint32_t frame_index = 0; - for (FrameResources& resources : m_frame_resources) - { - resources.needs_fence_wait = false; - - VkCommandPoolCreateInfo pool_info = { - VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, nullptr, 0, m_graphics_queue_family_index}; - res = vkCreateCommandPool(m_device, &pool_info, nullptr, &resources.command_pool); - if (res != VK_SUCCESS) - { - LOG_VULKAN_ERROR(res, "vkCreateCommandPool failed: "); - return false; - } - Vulkan::SetObjectName( - g_vulkan_context->GetDevice(), resources.command_pool, "Frame Command Pool %u", frame_index); - - VkCommandBufferAllocateInfo buffer_info = {VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, nullptr, - resources.command_pool, VK_COMMAND_BUFFER_LEVEL_PRIMARY, - static_cast(resources.command_buffers.size())}; - - res = vkAllocateCommandBuffers(m_device, &buffer_info, resources.command_buffers.data()); - if (res != VK_SUCCESS) - { - LOG_VULKAN_ERROR(res, "vkAllocateCommandBuffers failed: "); - return false; - } - for (u32 i = 0; i < resources.command_buffers.size(); i++) - { - Vulkan::SetObjectName(g_vulkan_context->GetDevice(), resources.command_buffers[i], - "Frame %u %sCommand Buffer", frame_index, (i == 0) ? "Init" : ""); - } - - VkFenceCreateInfo fence_info = {VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, nullptr, VK_FENCE_CREATE_SIGNALED_BIT}; - - res = vkCreateFence(m_device, &fence_info, nullptr, &resources.fence); - if (res != VK_SUCCESS) - { - LOG_VULKAN_ERROR(res, "vkCreateFence failed: "); - return false; - } - Vulkan::SetObjectName(g_vulkan_context->GetDevice(), resources.fence, "Frame Fence %u", frame_index); - // TODO: A better way to choose the number of descriptors. - VkDescriptorPoolSize pool_sizes[] = { - {VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, MAX_COMBINED_IMAGE_SAMPLER_DESCRIPTORS_PER_FRAME}, - {VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, MAX_SAMPLED_IMAGE_DESCRIPTORS_PER_FRAME}, - {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, MAX_STORAGE_IMAGE_DESCRIPTORS_PER_FRAME}, - {VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT, MAX_INPUT_ATTACHMENT_IMAGE_DESCRIPTORS_PER_FRAME}, - }; - - VkDescriptorPoolCreateInfo pool_create_info = {VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, nullptr, 0, - MAX_DESCRIPTOR_SETS_PER_FRAME, static_cast(std::size(pool_sizes)), pool_sizes}; - - res = vkCreateDescriptorPool(m_device, &pool_create_info, nullptr, &resources.descriptor_pool); - if (res != VK_SUCCESS) - { - LOG_VULKAN_ERROR(res, "vkCreateDescriptorPool failed: "); - return false; - } - Vulkan::SetObjectName( - g_vulkan_context->GetDevice(), resources.descriptor_pool, "Frame Descriptor Pool %u", frame_index); - - ++frame_index; - } - - ActivateCommandBuffer(0); - return true; -} - -void VKContext::DestroyCommandBuffers() -{ - for (FrameResources& resources : m_frame_resources) - { - for (auto& it : resources.cleanup_resources) - it(); - resources.cleanup_resources.clear(); - - if (resources.fence != VK_NULL_HANDLE) - { - vkDestroyFence(m_device, resources.fence, nullptr); - resources.fence = VK_NULL_HANDLE; - } - if (resources.descriptor_pool != VK_NULL_HANDLE) - { - vkDestroyDescriptorPool(m_device, resources.descriptor_pool, nullptr); - resources.descriptor_pool = VK_NULL_HANDLE; - } - if (resources.command_buffers[0] != VK_NULL_HANDLE) - { - vkFreeCommandBuffers(m_device, resources.command_pool, static_cast(resources.command_buffers.size()), - resources.command_buffers.data()); - resources.command_buffers.fill(VK_NULL_HANDLE); - } - if (resources.command_pool != VK_NULL_HANDLE) - { - vkDestroyCommandPool(m_device, resources.command_pool, nullptr); - resources.command_pool = VK_NULL_HANDLE; - } - } -} - -bool VKContext::CreateGlobalDescriptorPool() -{ - static constexpr const VkDescriptorPoolSize pool_sizes[] = { - {VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, 2}, - {VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 2}, - }; - - VkDescriptorPoolCreateInfo pool_create_info = {VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, nullptr, - VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT, - 1024, // TODO: tweak this - static_cast(std::size(pool_sizes)), pool_sizes}; - - VkResult res = vkCreateDescriptorPool(m_device, &pool_create_info, nullptr, &m_global_descriptor_pool); - if (res != VK_SUCCESS) - { - LOG_VULKAN_ERROR(res, "vkCreateDescriptorPool failed: "); - return false; - } - Vulkan::SetObjectName(g_vulkan_context->GetDevice(), m_global_descriptor_pool, "Global Descriptor Pool"); - - if (m_gpu_timing_supported) - { - const VkQueryPoolCreateInfo query_create_info = { - VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO, nullptr, 0, VK_QUERY_TYPE_TIMESTAMP, NUM_COMMAND_BUFFERS * 4, 0}; - res = vkCreateQueryPool(m_device, &query_create_info, nullptr, &m_timestamp_query_pool); - if (res != VK_SUCCESS) - { - LOG_VULKAN_ERROR(res, "vkCreateQueryPool failed: "); - m_gpu_timing_supported = false; - return false; - } - } - - return true; -} - -void VKContext::DestroyGlobalDescriptorPool() -{ - if (m_timestamp_query_pool != VK_NULL_HANDLE) - { - vkDestroyQueryPool(m_device, m_timestamp_query_pool, nullptr); - m_timestamp_query_pool = VK_NULL_HANDLE; - } - - if (m_global_descriptor_pool != VK_NULL_HANDLE) - { - vkDestroyDescriptorPool(m_device, m_global_descriptor_pool, nullptr); - m_global_descriptor_pool = VK_NULL_HANDLE; - } -} - -bool VKContext::CreateTextureStreamBuffer() -{ - if (!m_texture_upload_buffer.Create(VK_BUFFER_USAGE_TRANSFER_SRC_BIT, TEXTURE_BUFFER_SIZE)) - { - Console.Error("Failed to allocate texture upload buffer"); - return false; - } - - return true; -} - -VkRenderPass VKContext::GetRenderPass(VkFormat color_format, VkFormat depth_format, VkAttachmentLoadOp color_load_op, - VkAttachmentStoreOp color_store_op, VkAttachmentLoadOp depth_load_op, VkAttachmentStoreOp depth_store_op, - VkAttachmentLoadOp stencil_load_op, VkAttachmentStoreOp stencil_store_op, bool color_feedback_loop, - bool depth_sampling) -{ - RenderPassCacheKey key = {}; - key.color_format = color_format; - key.depth_format = depth_format; - key.color_load_op = color_load_op; - key.color_store_op = color_store_op; - key.depth_load_op = depth_load_op; - key.depth_store_op = depth_store_op; - key.stencil_load_op = stencil_load_op; - key.stencil_store_op = stencil_store_op; - key.color_feedback_loop = color_feedback_loop; - key.depth_sampling = depth_sampling; - - auto it = m_render_pass_cache.find(key.key); - if (it != m_render_pass_cache.end()) - return it->second; - - return CreateCachedRenderPass(key); -} - -VkRenderPass VKContext::GetRenderPassForRestarting(VkRenderPass pass) -{ - for (const auto& it : m_render_pass_cache) - { - if (it.second != pass) - continue; - - RenderPassCacheKey modified_key; - modified_key.key = it.first; - if (modified_key.color_load_op == VK_ATTACHMENT_LOAD_OP_CLEAR) - modified_key.color_load_op = VK_ATTACHMENT_LOAD_OP_LOAD; - if (modified_key.depth_load_op == VK_ATTACHMENT_LOAD_OP_CLEAR) - modified_key.depth_load_op = VK_ATTACHMENT_LOAD_OP_LOAD; - if (modified_key.stencil_load_op == VK_ATTACHMENT_LOAD_OP_CLEAR) - modified_key.stencil_load_op = VK_ATTACHMENT_LOAD_OP_LOAD; - - if (modified_key.key == it.first) - return pass; - - auto fit = m_render_pass_cache.find(modified_key.key); - if (fit != m_render_pass_cache.end()) - return fit->second; - - return CreateCachedRenderPass(modified_key); - } - - return pass; -} - -VkCommandBuffer VKContext::GetCurrentInitCommandBuffer() -{ - FrameResources& res = m_frame_resources[m_current_frame]; - VkCommandBuffer buf = res.command_buffers[0]; - if (res.init_buffer_used) - return buf; - - VkCommandBufferBeginInfo bi{ - VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, nullptr, VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT, nullptr}; - vkBeginCommandBuffer(buf, &bi); - res.init_buffer_used = true; - return buf; -} - -VkDescriptorSet VKContext::AllocateDescriptorSet(VkDescriptorSetLayout set_layout) -{ - VkDescriptorSetAllocateInfo allocate_info = {VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO, nullptr, - m_frame_resources[m_current_frame].descriptor_pool, 1, &set_layout}; - - VkDescriptorSet descriptor_set; - VkResult res = vkAllocateDescriptorSets(m_device, &allocate_info, &descriptor_set); - if (res != VK_SUCCESS) - { - // Failing to allocate a descriptor set is not a fatal error, we can - // recover by moving to the next command buffer. - return VK_NULL_HANDLE; - } - - return descriptor_set; -} - -VkDescriptorSet VKContext::AllocatePersistentDescriptorSet(VkDescriptorSetLayout set_layout) -{ - VkDescriptorSetAllocateInfo allocate_info = { - VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO, nullptr, m_global_descriptor_pool, 1, &set_layout}; - - VkDescriptorSet descriptor_set; - VkResult res = vkAllocateDescriptorSets(m_device, &allocate_info, &descriptor_set); - if (res != VK_SUCCESS) - return VK_NULL_HANDLE; - - return descriptor_set; -} - -void VKContext::FreeGlobalDescriptorSet(VkDescriptorSet set) -{ - vkFreeDescriptorSets(m_device, m_global_descriptor_pool, 1, &set); -} - -void VKContext::WaitForFenceCounter(u64 fence_counter) -{ - if (m_completed_fence_counter >= fence_counter) - return; - - // Find the first command buffer which covers this counter value. - u32 index = (m_current_frame + 1) % NUM_COMMAND_BUFFERS; - while (index != m_current_frame) - { - if (m_frame_resources[index].fence_counter >= fence_counter) - break; - - index = (index + 1) % NUM_COMMAND_BUFFERS; - } - - pxAssert(index != m_current_frame); - WaitForCommandBufferCompletion(index); -} - -void VKContext::WaitForGPUIdle() -{ - WaitForPresentComplete(); - vkDeviceWaitIdle(m_device); -} - -float VKContext::GetAndResetAccumulatedGPUTime() -{ - const float time = m_accumulated_gpu_time; - m_accumulated_gpu_time = 0.0f; - return time; -} - -bool VKContext::SetEnableGPUTiming(bool enabled) -{ - m_gpu_timing_enabled = enabled && m_gpu_timing_supported; - return (enabled == m_gpu_timing_enabled); -} - -void VKContext::ScanForCommandBufferCompletion() -{ - for (u32 check_index = (m_current_frame + 1) % NUM_COMMAND_BUFFERS; check_index != m_current_frame; - check_index = (check_index + 1) % NUM_COMMAND_BUFFERS) - { - FrameResources& resources = m_frame_resources[check_index]; - if (resources.fence_counter <= m_completed_fence_counter) - continue; // Already completed - if (vkGetFenceStatus(m_device, resources.fence) != VK_SUCCESS) - break; // Fence not signaled, later fences won't be either - CommandBufferCompleted(check_index); - m_completed_fence_counter = resources.fence_counter; - } - for (SpinResources& resources : m_spin_resources) - { - if (!resources.in_progress) - continue; - if (vkGetFenceStatus(m_device, resources.fence) != VK_SUCCESS) - continue; - SpinCommandCompleted(&resources - &m_spin_resources[0]); - } -} - -void VKContext::WaitForCommandBufferCompletion(u32 index) -{ - // Wait for this command buffer to be completed. - const VkResult res = vkWaitForFences(m_device, 1, &m_frame_resources[index].fence, VK_TRUE, UINT64_MAX); - if (res != VK_SUCCESS) - { - LOG_VULKAN_ERROR(res, "vkWaitForFences failed: "); - m_last_submit_failed.store(true, std::memory_order_release); - return; - } - - // Clean up any resources for command buffers between the last known completed buffer and this - // now-completed command buffer. If we use >2 buffers, this may be more than one buffer. - const u64 now_completed_counter = m_frame_resources[index].fence_counter; - u32 cleanup_index = (m_current_frame + 1) % NUM_COMMAND_BUFFERS; - while (cleanup_index != m_current_frame) - { - FrameResources& resources = m_frame_resources[cleanup_index]; - if (resources.fence_counter > now_completed_counter) - break; - - if (resources.fence_counter > m_completed_fence_counter) - CommandBufferCompleted(cleanup_index); - - cleanup_index = (cleanup_index + 1) % NUM_COMMAND_BUFFERS; - } - - m_completed_fence_counter = now_completed_counter; -} - -void VKContext::SubmitCommandBuffer( - VKSwapChain* present_swap_chain /* = nullptr */, bool submit_on_thread /* = false */) -{ - FrameResources& resources = m_frame_resources[m_current_frame]; - - // End the current command buffer. - VkResult res; - if (resources.init_buffer_used) - { - res = vkEndCommandBuffer(resources.command_buffers[0]); - if (res != VK_SUCCESS) - { - LOG_VULKAN_ERROR(res, "vkEndCommandBuffer failed: "); - pxFailRel("Failed to end command buffer"); - } - } - - bool wants_timestamp = m_gpu_timing_enabled || m_spin_timer; - if (wants_timestamp && resources.timestamp_written) - { - vkCmdWriteTimestamp(m_current_command_buffer, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, m_timestamp_query_pool, - m_current_frame * 2 + 1); - } - - res = vkEndCommandBuffer(resources.command_buffers[1]); - if (res != VK_SUCCESS) - { - LOG_VULKAN_ERROR(res, "vkEndCommandBuffer failed: "); - pxFailRel("Failed to end command buffer"); - } - - // This command buffer now has commands, so can't be re-used without waiting. - resources.needs_fence_wait = true; - - u32 spin_cycles = 0; - const bool spin_enabled = m_spin_timer; - if (spin_enabled) - { - ScanForCommandBufferCompletion(); - auto draw = m_spin_manager.DrawSubmitted(m_command_buffer_render_passes); - u32 constant_offset = - 400000 * m_spin_manager.SpinsPerUnitTime(); // 400µs, just to be safe since going over gets really bad - if (m_optional_extensions.vk_ext_calibrated_timestamps) - constant_offset /= - 2; // Safety factor isn't as important here, going over just hurts this one submission a bit - u32 minimum_spin = 200000 * m_spin_manager.SpinsPerUnitTime(); - u32 maximum_spin = std::max(1024, 16000000 * m_spin_manager.SpinsPerUnitTime()); // 16ms - if (draw.recommended_spin > minimum_spin + constant_offset) - spin_cycles = std::min(draw.recommended_spin - constant_offset, maximum_spin); - resources.spin_id = draw.id; - } - else - { - resources.spin_id = -1; - } - m_command_buffer_render_passes = 0; - - if (present_swap_chain != VK_NULL_HANDLE && m_spinning_supported) - { - m_spin_manager.NextFrame(); - if (m_spin_timer) - m_spin_timer--; - // Calibrate a max of once per frame - m_wants_new_timestamp_calibration = m_optional_extensions.vk_ext_calibrated_timestamps; - } - - if (spin_cycles != 0) - WaitForSpinCompletion(m_current_frame); - - std::unique_lock lock(m_present_mutex); - WaitForPresentComplete(lock); - - if (spin_enabled && m_optional_extensions.vk_ext_calibrated_timestamps) - resources.submit_timestamp = GetCPUTimestamp(); - - if (!submit_on_thread || !m_present_thread.joinable()) - { - DoSubmitCommandBuffer(m_current_frame, present_swap_chain, spin_cycles); - if (present_swap_chain) - DoPresent(present_swap_chain); - return; - } - - m_queued_present.command_buffer_index = m_current_frame; - m_queued_present.swap_chain = present_swap_chain; - m_queued_present.spin_cycles = spin_cycles; - m_present_done.store(false); - m_present_queued_cv.notify_one(); -} - -void VKContext::DoSubmitCommandBuffer(u32 index, VKSwapChain* present_swap_chain, u32 spin_cycles) -{ - FrameResources& resources = m_frame_resources[index]; - - uint32_t wait_bits = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; - VkSemaphore semas[2]; - VkSubmitInfo submit_info = {VK_STRUCTURE_TYPE_SUBMIT_INFO}; - submit_info.commandBufferCount = resources.init_buffer_used ? 2u : 1u; - submit_info.pCommandBuffers = - resources.init_buffer_used ? resources.command_buffers.data() : &resources.command_buffers[1]; - - if (present_swap_chain) - { - submit_info.pWaitSemaphores = present_swap_chain->GetImageAvailableSemaphorePtr(); - submit_info.waitSemaphoreCount = 1; - submit_info.pWaitDstStageMask = &wait_bits; - - if (spin_cycles != 0) - { - semas[0] = present_swap_chain->GetRenderingFinishedSemaphore(); - semas[1] = m_spin_resources[index].semaphore; - submit_info.signalSemaphoreCount = 2; - submit_info.pSignalSemaphores = semas; - } - else - { - submit_info.pSignalSemaphores = present_swap_chain->GetRenderingFinishedSemaphorePtr(); - submit_info.signalSemaphoreCount = 1; - } - } - else if (spin_cycles != 0) - { - submit_info.signalSemaphoreCount = 1; - submit_info.pSignalSemaphores = &m_spin_resources[index].semaphore; - } - - const VkResult res = vkQueueSubmit(m_graphics_queue, 1, &submit_info, resources.fence); - if (res != VK_SUCCESS) - { - LOG_VULKAN_ERROR(res, "vkQueueSubmit failed: "); - m_last_submit_failed.store(true, std::memory_order_release); - return; - } - - if (spin_cycles != 0) - SubmitSpinCommand(index, spin_cycles); -} - -void VKContext::DoPresent(VKSwapChain* present_swap_chain) -{ - const VkPresentInfoKHR present_info = {VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, nullptr, 1, - present_swap_chain->GetRenderingFinishedSemaphorePtr(), 1, present_swap_chain->GetSwapChainPtr(), - present_swap_chain->GetCurrentImageIndexPtr(), nullptr}; - - present_swap_chain->ReleaseCurrentImage(); - - const VkResult res = vkQueuePresentKHR(m_present_queue, &present_info); - if (res != VK_SUCCESS) - { - // VK_ERROR_OUT_OF_DATE_KHR is not fatal, just means we need to recreate our swap chain. - if (res != VK_ERROR_OUT_OF_DATE_KHR && res != VK_SUBOPTIMAL_KHR) - LOG_VULKAN_ERROR(res, "vkQueuePresentKHR failed: "); - - m_last_present_failed.store(true, std::memory_order_release); - return; - } - - // Grab the next image as soon as possible, that way we spend less time blocked on the next - // submission. Don't care if it fails, we'll deal with that at the presentation call site. - // Credit to dxvk for the idea. - present_swap_chain->AcquireNextImage(); -} - -void VKContext::WaitForPresentComplete() -{ - if (m_present_done.load()) - return; - - std::unique_lock lock(m_present_mutex); - WaitForPresentComplete(lock); -} - -void VKContext::WaitForPresentComplete(std::unique_lock& lock) -{ - if (m_present_done.load()) - return; - - m_present_done_cv.wait(lock, [this]() { return m_present_done.load(); }); -} - -void VKContext::PresentThread() -{ - std::unique_lock lock(m_present_mutex); - while (!m_present_thread_done.load()) - { - m_present_queued_cv.wait(lock, [this]() { return !m_present_done.load() || m_present_thread_done.load(); }); - - if (m_present_done.load()) - continue; - - DoSubmitCommandBuffer( - m_queued_present.command_buffer_index, m_queued_present.swap_chain, m_queued_present.spin_cycles); - if (m_queued_present.swap_chain) - DoPresent(m_queued_present.swap_chain); - m_present_done.store(true); - m_present_done_cv.notify_one(); - } -} - -void VKContext::StartPresentThread() -{ - pxAssert(!m_present_thread.joinable()); - m_present_thread_done.store(false); - m_present_thread = std::thread(&VKContext::PresentThread, this); -} - -void VKContext::StopPresentThread() -{ - if (!m_present_thread.joinable()) - return; - - { - std::unique_lock lock(m_present_mutex); - WaitForPresentComplete(lock); - m_present_thread_done.store(true); - m_present_queued_cv.notify_one(); - } - - m_present_thread.join(); -} - -void VKContext::CommandBufferCompleted(u32 index) -{ - FrameResources& resources = m_frame_resources[index]; - - for (auto& it : resources.cleanup_resources) - it(); - resources.cleanup_resources.clear(); - - bool wants_timestamps = m_gpu_timing_enabled || resources.spin_id >= 0; - - if (wants_timestamps && resources.timestamp_written) - { - std::array timestamps; - VkResult res = - vkGetQueryPoolResults(m_device, m_timestamp_query_pool, index * 2, static_cast(timestamps.size()), - sizeof(u64) * timestamps.size(), timestamps.data(), sizeof(u64), VK_QUERY_RESULT_64_BIT); - if (res == VK_SUCCESS) - { - // if we didn't write the timestamp at the start of the cmdbuffer (just enabled timing), the first TS will be zero - if (timestamps[0] > 0 && m_gpu_timing_enabled) - { - const double ns_diff = - (timestamps[1] - timestamps[0]) * static_cast(m_device_properties.limits.timestampPeriod); - m_accumulated_gpu_time += ns_diff / 1000000.0; - } - if (resources.spin_id >= 0) - { - if (m_optional_extensions.vk_ext_calibrated_timestamps && timestamps[1] > 0) - { - u64 end = timestamps[1] * m_spin_timestamp_scale + m_spin_timestamp_offset; - m_spin_manager.DrawCompleted(resources.spin_id, resources.submit_timestamp, end); - } - else if (!m_optional_extensions.vk_ext_calibrated_timestamps && timestamps[0] > 0) - { - u64 begin = timestamps[0] * m_spin_timestamp_scale; - u64 end = timestamps[1] * m_spin_timestamp_scale; - m_spin_manager.DrawCompleted(resources.spin_id, begin, end); - } - } - } - else - { - LOG_VULKAN_ERROR(res, "vkGetQueryPoolResults failed: "); - } - } -} - -void VKContext::MoveToNextCommandBuffer() -{ - ActivateCommandBuffer((m_current_frame + 1) % NUM_COMMAND_BUFFERS); -} - -void VKContext::ActivateCommandBuffer(u32 index) -{ - FrameResources& resources = m_frame_resources[index]; - - if (!m_present_done.load() && m_queued_present.command_buffer_index == index) - WaitForPresentComplete(); - - // Wait for the GPU to finish with all resources for this command buffer. - if (resources.fence_counter > m_completed_fence_counter) - WaitForCommandBufferCompletion(index); - - // Reset fence to unsignaled before starting. - VkResult res = vkResetFences(m_device, 1, &resources.fence); - if (res != VK_SUCCESS) - LOG_VULKAN_ERROR(res, "vkResetFences failed: "); - - // Reset command pools to beginning since we can re-use the memory now - res = vkResetCommandPool(m_device, resources.command_pool, 0); - if (res != VK_SUCCESS) - LOG_VULKAN_ERROR(res, "vkResetCommandPool failed: "); - - // Enable commands to be recorded to the two buffers again. - VkCommandBufferBeginInfo begin_info = { - VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, nullptr, VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT, nullptr}; - res = vkBeginCommandBuffer(resources.command_buffers[1], &begin_info); - if (res != VK_SUCCESS) - LOG_VULKAN_ERROR(res, "vkBeginCommandBuffer failed: "); - - // Also can do the same for the descriptor pools - res = vkResetDescriptorPool(m_device, resources.descriptor_pool, 0); - if (res != VK_SUCCESS) - LOG_VULKAN_ERROR(res, "vkResetDescriptorPool failed: "); - - bool wants_timestamp = m_gpu_timing_enabled || m_spin_timer; - if (wants_timestamp) - { - vkCmdResetQueryPool(resources.command_buffers[1], m_timestamp_query_pool, index * 2, 2); - vkCmdWriteTimestamp( - resources.command_buffers[1], VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, m_timestamp_query_pool, index * 2); - } - - resources.fence_counter = m_next_fence_counter++; - resources.init_buffer_used = false; - resources.timestamp_written = wants_timestamp; - - m_current_frame = index; - m_current_command_buffer = resources.command_buffers[1]; - - // using the lower 32 bits of the fence index should be sufficient here, I hope... - vmaSetCurrentFrameIndex(m_allocator, static_cast(m_next_fence_counter)); -} - -void VKContext::ExecuteCommandBuffer(WaitType wait_for_completion) -{ - if (m_last_submit_failed.load(std::memory_order_acquire)) - return; - - // If we're waiting for completion, don't bother waking the worker thread. - const u32 current_frame = m_current_frame; - SubmitCommandBuffer(); - MoveToNextCommandBuffer(); - - if (wait_for_completion != WaitType::None) - { - // Calibrate while we wait - if (m_wants_new_timestamp_calibration) - CalibrateSpinTimestamp(); - if (wait_for_completion == WaitType::Spin) - { - while (vkGetFenceStatus(m_device, m_frame_resources[current_frame].fence) == VK_NOT_READY) - ShortSpin(); - } - WaitForCommandBufferCompletion(current_frame); - } -} - -bool VKContext::CheckLastPresentFail() -{ - return m_last_present_failed.exchange(false, std::memory_order_acq_rel); -} - -bool VKContext::CheckLastSubmitFail() -{ - return m_last_submit_failed.load(std::memory_order_acquire); -} - -void VKContext::DeferBufferDestruction(VkBuffer object) -{ - FrameResources& resources = m_frame_resources[m_current_frame]; - resources.cleanup_resources.push_back([this, object]() { vkDestroyBuffer(m_device, object, nullptr); }); -} - -void VKContext::DeferBufferDestruction(VkBuffer object, VmaAllocation allocation) -{ - FrameResources& resources = m_frame_resources[m_current_frame]; - resources.cleanup_resources.push_back( - [this, object, allocation]() { vmaDestroyBuffer(m_allocator, object, allocation); }); -} - -void VKContext::DeferBufferViewDestruction(VkBufferView object) -{ - FrameResources& resources = m_frame_resources[m_current_frame]; - resources.cleanup_resources.push_back([this, object]() { vkDestroyBufferView(m_device, object, nullptr); }); -} - -void VKContext::DeferDeviceMemoryDestruction(VkDeviceMemory object) -{ - FrameResources& resources = m_frame_resources[m_current_frame]; - resources.cleanup_resources.push_back([this, object]() { vkFreeMemory(m_device, object, nullptr); }); -} - -void VKContext::DeferFramebufferDestruction(VkFramebuffer object) -{ - FrameResources& resources = m_frame_resources[m_current_frame]; - resources.cleanup_resources.push_back([this, object]() { vkDestroyFramebuffer(m_device, object, nullptr); }); -} - -void VKContext::DeferImageDestruction(VkImage object) -{ - FrameResources& resources = m_frame_resources[m_current_frame]; - resources.cleanup_resources.push_back([this, object]() { vkDestroyImage(m_device, object, nullptr); }); -} - -void VKContext::DeferImageDestruction(VkImage object, VmaAllocation allocation) -{ - FrameResources& resources = m_frame_resources[m_current_frame]; - resources.cleanup_resources.push_back( - [this, object, allocation]() { vmaDestroyImage(m_allocator, object, allocation); }); -} - -void VKContext::DeferImageViewDestruction(VkImageView object) -{ - FrameResources& resources = m_frame_resources[m_current_frame]; - resources.cleanup_resources.push_back([this, object]() { vkDestroyImageView(m_device, object, nullptr); }); -} - -void VKContext::DeferPipelineDestruction(VkPipeline pipeline) -{ - FrameResources& resources = m_frame_resources[m_current_frame]; - resources.cleanup_resources.push_back([this, pipeline]() { vkDestroyPipeline(m_device, pipeline, nullptr); }); -} - -void VKContext::DeferSamplerDestruction(VkSampler sampler) -{ - FrameResources& resources = m_frame_resources[m_current_frame]; - resources.cleanup_resources.push_back([this, sampler]() { vkDestroySampler(m_device, sampler, nullptr); }); -} - -VKAPI_ATTR VkBool32 VKAPI_CALL DebugMessengerCallback(VkDebugUtilsMessageSeverityFlagBitsEXT severity, - VkDebugUtilsMessageTypeFlagsEXT messageType, const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData, - void* pUserData) -{ - if (severity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) - { - Console.Error("Vulkan debug report: (%s) %s", - pCallbackData->pMessageIdName ? pCallbackData->pMessageIdName : "", pCallbackData->pMessage); - } - else if (severity & (VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT)) - { - Console.Warning("Vulkan debug report: (%s) %s", - pCallbackData->pMessageIdName ? pCallbackData->pMessageIdName : "", pCallbackData->pMessage); - } - else if (severity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT) - { - Console.WriteLn("Vulkan debug report: (%s) %s", - pCallbackData->pMessageIdName ? pCallbackData->pMessageIdName : "", pCallbackData->pMessage); - } - else - { - DevCon.WriteLn("Vulkan debug report: (%s) %s", - pCallbackData->pMessageIdName ? pCallbackData->pMessageIdName : "", pCallbackData->pMessage); - } - - return VK_FALSE; -} - -bool VKContext::EnableDebugUtils() -{ - // Already enabled? - if (m_debug_messenger_callback != VK_NULL_HANDLE) - return true; - - // Check for presence of the functions before calling - if (!vkCreateDebugUtilsMessengerEXT || !vkDestroyDebugUtilsMessengerEXT || !vkSubmitDebugUtilsMessageEXT) - { - return false; - } - - VkDebugUtilsMessengerCreateInfoEXT messenger_info = {VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT, - nullptr, 0, - VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | - VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT, - VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT | - VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT, - DebugMessengerCallback, nullptr}; - - const VkResult res = - vkCreateDebugUtilsMessengerEXT(m_instance, &messenger_info, nullptr, &m_debug_messenger_callback); - if (res != VK_SUCCESS) - { - LOG_VULKAN_ERROR(res, "vkCreateDebugUtilsMessengerEXT failed: "); - return false; - } - - return true; -} - -void VKContext::DisableDebugUtils() -{ - if (m_debug_messenger_callback != VK_NULL_HANDLE) - { - vkDestroyDebugUtilsMessengerEXT(m_instance, m_debug_messenger_callback, nullptr); - m_debug_messenger_callback = VK_NULL_HANDLE; - } -} - -VkRenderPass VKContext::CreateCachedRenderPass(RenderPassCacheKey key) -{ - VkAttachmentReference color_reference; - VkAttachmentReference* color_reference_ptr = nullptr; - VkAttachmentReference depth_reference; - VkAttachmentReference* depth_reference_ptr = nullptr; - VkAttachmentReference input_reference; - VkAttachmentReference* input_reference_ptr = nullptr; - VkSubpassDependency subpass_dependency; - VkSubpassDependency* subpass_dependency_ptr = nullptr; - std::array attachments; - u32 num_attachments = 0; - if (key.color_format != VK_FORMAT_UNDEFINED) - { - const VkImageLayout layout = - key.color_feedback_loop ? (UseFeedbackLoopLayout() ? VK_IMAGE_LAYOUT_ATTACHMENT_FEEDBACK_LOOP_OPTIMAL_EXT : - VK_IMAGE_LAYOUT_GENERAL) : - VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; - attachments[num_attachments] = {0, static_cast(key.color_format), VK_SAMPLE_COUNT_1_BIT, - static_cast(key.color_load_op), static_cast(key.color_store_op), - VK_ATTACHMENT_LOAD_OP_DONT_CARE, VK_ATTACHMENT_STORE_OP_DONT_CARE, layout, layout}; - color_reference.attachment = num_attachments; - color_reference.layout = layout; - color_reference_ptr = &color_reference; - - if (key.color_feedback_loop) - { - if (!UseFeedbackLoopLayout()) - { - input_reference.attachment = num_attachments; - input_reference.layout = layout; - input_reference_ptr = &input_reference; - } - - if (!m_optional_extensions.vk_ext_rasterization_order_attachment_access) - { - // don't need the framebuffer-local dependency when we have rasterization order attachment access - subpass_dependency.srcSubpass = 0; - subpass_dependency.dstSubpass = 0; - subpass_dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; - subpass_dependency.dstStageMask = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT; - subpass_dependency.srcAccessMask = - VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; - subpass_dependency.dstAccessMask = - UseFeedbackLoopLayout() ? VK_ACCESS_SHADER_READ_BIT : VK_ACCESS_INPUT_ATTACHMENT_READ_BIT; - subpass_dependency.dependencyFlags = - UseFeedbackLoopLayout() ? (VK_DEPENDENCY_BY_REGION_BIT | VK_DEPENDENCY_FEEDBACK_LOOP_BIT_EXT) : - VK_DEPENDENCY_BY_REGION_BIT; - subpass_dependency_ptr = &subpass_dependency; - } - } - - num_attachments++; - } - if (key.depth_format != VK_FORMAT_UNDEFINED) - { - const VkImageLayout layout = key.depth_sampling ? (UseFeedbackLoopLayout() ? VK_IMAGE_LAYOUT_ATTACHMENT_FEEDBACK_LOOP_OPTIMAL_EXT : - VK_IMAGE_LAYOUT_GENERAL) : - VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; - attachments[num_attachments] = {0, static_cast(key.depth_format), VK_SAMPLE_COUNT_1_BIT, - static_cast(key.depth_load_op), static_cast(key.depth_store_op), - static_cast(key.stencil_load_op), - static_cast(key.stencil_store_op), layout, layout}; - depth_reference.attachment = num_attachments; - depth_reference.layout = layout; - depth_reference_ptr = &depth_reference; - num_attachments++; - } - - const VkSubpassDescriptionFlags subpass_flags = - (key.color_feedback_loop && m_optional_extensions.vk_ext_rasterization_order_attachment_access) ? - VK_SUBPASS_DESCRIPTION_RASTERIZATION_ORDER_ATTACHMENT_COLOR_ACCESS_BIT_EXT : - 0; - const VkSubpassDescription subpass = {subpass_flags, VK_PIPELINE_BIND_POINT_GRAPHICS, input_reference_ptr ? 1u : 0u, - input_reference_ptr ? input_reference_ptr : nullptr, color_reference_ptr ? 1u : 0u, - color_reference_ptr ? color_reference_ptr : nullptr, nullptr, depth_reference_ptr, 0, nullptr}; - const VkRenderPassCreateInfo pass_info = {VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO, nullptr, 0u, num_attachments, - attachments.data(), 1u, &subpass, subpass_dependency_ptr ? 1u : 0u, subpass_dependency_ptr}; - - VkRenderPass pass; - const VkResult res = vkCreateRenderPass(m_device, &pass_info, nullptr, &pass); - if (res != VK_SUCCESS) - { - LOG_VULKAN_ERROR(res, "vkCreateRenderPass failed: "); - return VK_NULL_HANDLE; - } - - m_render_pass_cache.emplace(key.key, pass); - return pass; -} - -void VKContext::DestroyRenderPassCache() -{ - for (auto& it : m_render_pass_cache) - vkDestroyRenderPass(m_device, it.second, nullptr); - - m_render_pass_cache.clear(); -} - -static constexpr std::string_view SPIN_SHADER = R"( -#version 460 core - -layout(std430, set=0, binding=0) buffer SpinBuffer { uint spin[]; }; -layout(push_constant) uniform constants { uint cycles; }; -layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in; - -void main() -{ - uint value = spin[0]; - // The compiler doesn't know, but spin[0] == 0, so this loop won't actually go anywhere - for (uint i = 0; i < cycles; i++) - value = spin[value]; - // Store the result back to the buffer so the compiler can't optimize it away - spin[0] = value; -} -)"; - -bool VKContext::InitSpinResources() -{ - if (!m_spinning_supported) - return true; - - // TODO: Move to safe destroy functions, use scoped guard. - - VkResult res; -#define CHECKED_CREATE(create_fn, create_struct, output_struct) \ - do \ - { \ - if ((res = create_fn(m_device, create_struct, nullptr, output_struct)) != VK_SUCCESS) \ - { \ - LOG_VULKAN_ERROR(res, #create_fn " failed: "); \ - return false; \ - } \ - } while (0) - - VkDescriptorSetLayoutBinding set_layout_binding = {}; - set_layout_binding.binding = 0; - set_layout_binding.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; - set_layout_binding.descriptorCount = 1; - set_layout_binding.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT; - VkDescriptorSetLayoutCreateInfo desc_set_layout_create = {VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO}; - desc_set_layout_create.bindingCount = 1; - desc_set_layout_create.pBindings = &set_layout_binding; - CHECKED_CREATE(vkCreateDescriptorSetLayout, &desc_set_layout_create, &m_spin_descriptor_set_layout); - - const VkPushConstantRange push_constant_range = {VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(u32)}; - VkPipelineLayoutCreateInfo pl_layout_create = {VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO}; - pl_layout_create.setLayoutCount = 1; - pl_layout_create.pSetLayouts = &m_spin_descriptor_set_layout; - pl_layout_create.pushConstantRangeCount = 1; - pl_layout_create.pPushConstantRanges = &push_constant_range; - CHECKED_CREATE(vkCreatePipelineLayout, &pl_layout_create, &m_spin_pipeline_layout); - - VkShaderModule shader_module = g_vulkan_shader_cache->GetComputeShader(SPIN_SHADER); - if (shader_module == VK_NULL_HANDLE) - return false; - - Vulkan::SetObjectName(m_device, shader_module, "Spin Shader"); - - VkComputePipelineCreateInfo pl_create = {VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO}; - pl_create.layout = m_spin_pipeline_layout; - pl_create.stage.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; - pl_create.stage.stage = VK_SHADER_STAGE_COMPUTE_BIT; - pl_create.stage.pName = "main"; - pl_create.stage.module = shader_module; - res = vkCreateComputePipelines(m_device, VK_NULL_HANDLE, 1, &pl_create, nullptr, &m_spin_pipeline); - vkDestroyShaderModule(m_device, shader_module, nullptr); - if (res != VK_SUCCESS) - { - LOG_VULKAN_ERROR(res, "vkCreateComputePipelines failed: "); - return false; - } - Vulkan::SetObjectName(m_device, m_spin_pipeline, "Spin Pipeline"); - - VmaAllocationCreateInfo buf_vma_create = {}; - buf_vma_create.usage = VMA_MEMORY_USAGE_GPU_ONLY; - VkBufferCreateInfo buf_create = {VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO}; - buf_create.size = 4; - buf_create.usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT; - if ((res = vmaCreateBuffer(m_allocator, &buf_create, &buf_vma_create, &m_spin_buffer, &m_spin_buffer_allocation, - nullptr)) != VK_SUCCESS) - { - LOG_VULKAN_ERROR(res, "vmaCreateBuffer failed: "); - return false; - } - Vulkan::SetObjectName(m_device, m_spin_buffer, "Spin Buffer"); - - VkDescriptorSetAllocateInfo desc_set_allocate = {VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO}; - desc_set_allocate.descriptorPool = m_global_descriptor_pool; - desc_set_allocate.descriptorSetCount = 1; - desc_set_allocate.pSetLayouts = &m_spin_descriptor_set_layout; - if ((res = vkAllocateDescriptorSets(m_device, &desc_set_allocate, &m_spin_descriptor_set)) != VK_SUCCESS) - { - LOG_VULKAN_ERROR(res, "vkAllocateDescriptorSets failed: "); - return false; - } - const VkDescriptorBufferInfo desc_buffer_info = {m_spin_buffer, 0, VK_WHOLE_SIZE}; - VkWriteDescriptorSet desc_set_write = {VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET}; - desc_set_write.dstSet = m_spin_descriptor_set; - desc_set_write.dstBinding = 0; - desc_set_write.descriptorCount = 1; - desc_set_write.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; - desc_set_write.pBufferInfo = &desc_buffer_info; - vkUpdateDescriptorSets(m_device, 1, &desc_set_write, 0, nullptr); - - for (SpinResources& resources : m_spin_resources) - { - u32 index = &resources - &m_spin_resources[0]; - VkCommandPoolCreateInfo pool_info = {VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO}; - pool_info.queueFamilyIndex = m_spin_queue_family_index; - CHECKED_CREATE(vkCreateCommandPool, &pool_info, &resources.command_pool); - Vulkan::SetObjectName(m_device, resources.command_pool, "Spin Command Pool %u", index); - - VkCommandBufferAllocateInfo buffer_info = {VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO}; - buffer_info.commandPool = resources.command_pool; - buffer_info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; - buffer_info.commandBufferCount = 1; - res = vkAllocateCommandBuffers(m_device, &buffer_info, &resources.command_buffer); - if (res != VK_SUCCESS) - { - LOG_VULKAN_ERROR(res, "vkAllocateCommandBuffers failed: "); - return false; - } - Vulkan::SetObjectName(m_device, resources.command_buffer, "Spin Command Buffer %u", index); - - VkFenceCreateInfo fence_info = {VK_STRUCTURE_TYPE_FENCE_CREATE_INFO}; - fence_info.flags = VK_FENCE_CREATE_SIGNALED_BIT; - CHECKED_CREATE(vkCreateFence, &fence_info, &resources.fence); - Vulkan::SetObjectName(m_device, resources.fence, "Spin Fence %u", index); - - if (!m_spin_queue_is_graphics_queue) - { - VkSemaphoreCreateInfo sem_info = {VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO}; - CHECKED_CREATE(vkCreateSemaphore, &sem_info, &resources.semaphore); - Vulkan::SetObjectName(m_device, resources.semaphore, "Draw to Spin Semaphore %u", index); - } - } - -#undef CHECKED_CREATE - return true; -} - -void VKContext::DestroySpinResources() -{ -#define CHECKED_DESTROY(destructor, obj) \ - do \ - { \ - if (obj != VK_NULL_HANDLE) \ - { \ - destructor(m_device, obj, nullptr); \ - obj = VK_NULL_HANDLE; \ - } \ - } while (0) - - if (m_spin_buffer) - { - vmaDestroyBuffer(m_allocator, m_spin_buffer, m_spin_buffer_allocation); - m_spin_buffer = VK_NULL_HANDLE; - m_spin_buffer_allocation = VK_NULL_HANDLE; - } - CHECKED_DESTROY(vkDestroyPipeline, m_spin_pipeline); - CHECKED_DESTROY(vkDestroyPipelineLayout, m_spin_pipeline_layout); - CHECKED_DESTROY(vkDestroyDescriptorSetLayout, m_spin_descriptor_set_layout); - if (m_spin_descriptor_set != VK_NULL_HANDLE) - { - vkFreeDescriptorSets(m_device, m_global_descriptor_pool, 1, &m_spin_descriptor_set); - m_spin_descriptor_set = VK_NULL_HANDLE; - } - for (SpinResources& resources : m_spin_resources) - { - CHECKED_DESTROY(vkDestroySemaphore, resources.semaphore); - CHECKED_DESTROY(vkDestroyFence, resources.fence); - if (resources.command_buffer != VK_NULL_HANDLE) - { - vkFreeCommandBuffers(m_device, resources.command_pool, 1, &resources.command_buffer); - resources.command_buffer = VK_NULL_HANDLE; - } - CHECKED_DESTROY(vkDestroyCommandPool, resources.command_pool); - } -#undef CHECKED_DESTROY -} - -void VKContext::WaitForSpinCompletion(u32 index) -{ - SpinResources& resources = m_spin_resources[index]; - if (!resources.in_progress) - return; - - const VkResult res = vkWaitForFences(m_device, 1, &resources.fence, VK_TRUE, UINT64_MAX); - if (res != VK_SUCCESS) - { - LOG_VULKAN_ERROR(res, "vkWaitForFences failed: "); - m_last_submit_failed.store(true, std::memory_order_release); - return; - } - SpinCommandCompleted(index); -} - -void VKContext::SpinCommandCompleted(u32 index) -{ - SpinResources& resources = m_spin_resources[index]; - resources.in_progress = false; - const u32 timestamp_base = (index + NUM_COMMAND_BUFFERS) * 2; - std::array timestamps; - const VkResult res = - vkGetQueryPoolResults(m_device, m_timestamp_query_pool, timestamp_base, static_cast(timestamps.size()), - sizeof(timestamps), timestamps.data(), sizeof(u64), VK_QUERY_RESULT_64_BIT); - if (res == VK_SUCCESS) - { - u64 begin, end; - if (m_optional_extensions.vk_ext_calibrated_timestamps) - { - begin = timestamps[0] * m_spin_timestamp_scale + m_spin_timestamp_offset; - end = timestamps[1] * m_spin_timestamp_scale + m_spin_timestamp_offset; - } - else - { - begin = timestamps[0] * m_spin_timestamp_scale; - end = timestamps[1] * m_spin_timestamp_scale; - } - m_spin_manager.SpinCompleted(resources.cycles, begin, end); - } - else - { - LOG_VULKAN_ERROR(res, "vkGetQueryPoolResults failed: "); - } -} - -void VKContext::SubmitSpinCommand(u32 index, u32 cycles) -{ - SpinResources& resources = m_spin_resources[index]; - VkResult res; - - // Reset fence to unsignaled before starting. - if ((res = vkResetFences(m_device, 1, &resources.fence)) != VK_SUCCESS) - LOG_VULKAN_ERROR(res, "vkResetFences failed: "); - - // Reset command pools to beginning since we can re-use the memory now - if ((res = vkResetCommandPool(m_device, resources.command_pool, 0)) != VK_SUCCESS) - LOG_VULKAN_ERROR(res, "vkResetCommandPool failed: "); - - // Enable commands to be recorded to the two buffers again. - VkCommandBufferBeginInfo begin_info = {VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO}; - begin_info.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; - if ((res = vkBeginCommandBuffer(resources.command_buffer, &begin_info)) != VK_SUCCESS) - LOG_VULKAN_ERROR(res, "vkBeginCommandBuffer failed: "); - - if (!m_spin_buffer_initialized) - { - m_spin_buffer_initialized = true; - vkCmdFillBuffer(resources.command_buffer, m_spin_buffer, 0, VK_WHOLE_SIZE, 0); - VkBufferMemoryBarrier barrier = {VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER}; - barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; - barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT; - barrier.srcQueueFamilyIndex = m_spin_queue_family_index; - barrier.dstQueueFamilyIndex = m_spin_queue_family_index; - barrier.buffer = m_spin_buffer; - barrier.offset = 0; - barrier.size = VK_WHOLE_SIZE; - vkCmdPipelineBarrier(resources.command_buffer, VK_PIPELINE_STAGE_TRANSFER_BIT, - VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, nullptr, 1, &barrier, 0, nullptr); - } - - if (m_spin_queue_is_graphics_queue) - vkCmdPipelineBarrier(resources.command_buffer, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, - VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, nullptr, 0, nullptr, 0, nullptr); - - const u32 timestamp_base = (index + NUM_COMMAND_BUFFERS) * 2; - vkCmdResetQueryPool(resources.command_buffer, m_timestamp_query_pool, timestamp_base, 2); - vkCmdWriteTimestamp( - resources.command_buffer, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, m_timestamp_query_pool, timestamp_base); - vkCmdPushConstants( - resources.command_buffer, m_spin_pipeline_layout, VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(u32), &cycles); - vkCmdBindPipeline(resources.command_buffer, VK_PIPELINE_BIND_POINT_COMPUTE, m_spin_pipeline); - vkCmdBindDescriptorSets(resources.command_buffer, VK_PIPELINE_BIND_POINT_COMPUTE, m_spin_pipeline_layout, 0, 1, - &m_spin_descriptor_set, 0, nullptr); - vkCmdDispatch(resources.command_buffer, 1, 1, 1); - vkCmdWriteTimestamp( - resources.command_buffer, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, m_timestamp_query_pool, timestamp_base + 1); - - if ((res = vkEndCommandBuffer(resources.command_buffer)) != VK_SUCCESS) - LOG_VULKAN_ERROR(res, "vkEndCommandBuffer failed: "); - - VkSubmitInfo submit_info = {VK_STRUCTURE_TYPE_SUBMIT_INFO}; - submit_info.commandBufferCount = 1; - submit_info.pCommandBuffers = &resources.command_buffer; - VkPipelineStageFlags sema_waits[] = {VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT}; - if (!m_spin_queue_is_graphics_queue) - { - submit_info.waitSemaphoreCount = 1; - submit_info.pWaitSemaphores = &resources.semaphore; - submit_info.pWaitDstStageMask = sema_waits; - } - vkQueueSubmit(m_spin_queue, 1, &submit_info, resources.fence); - resources.in_progress = true; - resources.cycles = cycles; -} - -void VKContext::NotifyOfReadback() -{ - if (!m_spinning_supported) - return; - m_spin_timer = 30; - m_spin_manager.ReadbackRequested(); -} - -void VKContext::CalibrateSpinTimestamp() -{ - if (!m_optional_extensions.vk_ext_calibrated_timestamps) - return; - VkCalibratedTimestampInfoEXT infos[2] = { - {VK_STRUCTURE_TYPE_CALIBRATED_TIMESTAMP_INFO_EXT, nullptr, VK_TIME_DOMAIN_DEVICE_EXT}, - {VK_STRUCTURE_TYPE_CALIBRATED_TIMESTAMP_INFO_EXT, nullptr, m_calibrated_timestamp_type}, - }; - u64 timestamps[2]; - u64 maxDeviation; - constexpr u64 MAX_MAX_DEVIATION = 100000; // 100µs - for (int i = 0; i < 4; i++) // 4 tries to get under MAX_MAX_DEVIATION - { - const VkResult res = vkGetCalibratedTimestampsEXT(m_device, std::size(infos), infos, timestamps, &maxDeviation); - if (res != VK_SUCCESS) - { - LOG_VULKAN_ERROR(res, "vkGetCalibratedTimestampsEXT failed: "); - return; - } - if (maxDeviation < MAX_MAX_DEVIATION) - break; - } - if (maxDeviation >= MAX_MAX_DEVIATION) - Console.Warning("vkGetCalibratedTimestampsEXT returned high max deviation of %lluµs", maxDeviation / 1000); - const double gpu_time = timestamps[0] * m_spin_timestamp_scale; -#ifdef _WIN32 - const double cpu_time = timestamps[1] * m_queryperfcounter_to_ns; -#else - const double cpu_time = timestamps[1]; -#endif - m_spin_timestamp_offset = cpu_time - gpu_time; -} - -u64 VKContext::GetCPUTimestamp() -{ -#ifdef _WIN32 - LARGE_INTEGER value = {}; - QueryPerformanceCounter(&value); - return static_cast(static_cast(value.QuadPart) * m_queryperfcounter_to_ns); -#else -#ifdef CLOCK_MONOTONIC_RAW - const bool use_raw = m_calibrated_timestamp_type == VK_TIME_DOMAIN_CLOCK_MONOTONIC_RAW_EXT; - const clockid_t clock = use_raw ? CLOCK_MONOTONIC_RAW : CLOCK_MONOTONIC; -#else - const clockid_t clock = CLOCK_MONOTONIC; -#endif - timespec ts = {}; - clock_gettime(clock, &ts); - return static_cast(ts.tv_sec) * 1000000000 + ts.tv_nsec; -#endif -} - -bool VKContext::AllocatePreinitializedGPUBuffer(u32 size, VkBuffer* gpu_buffer, VmaAllocation* gpu_allocation, - VkBufferUsageFlags gpu_usage, const std::function& fill_callback) -{ - // Try to place the fixed index buffer in GPU local memory. - // Use the staging buffer to copy into it. - - const VkBufferCreateInfo cpu_bci = {VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, nullptr, 0, size, - VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_SHARING_MODE_EXCLUSIVE}; - const VmaAllocationCreateInfo cpu_aci = {VMA_ALLOCATION_CREATE_MAPPED_BIT, VMA_MEMORY_USAGE_CPU_ONLY, 0, 0}; - VkBuffer cpu_buffer; - VmaAllocation cpu_allocation; - VmaAllocationInfo cpu_ai; - VkResult res = vmaCreateBuffer(m_allocator, &cpu_bci, &cpu_aci, &cpu_buffer, &cpu_allocation, &cpu_ai); - if (res != VK_SUCCESS) - { - LOG_VULKAN_ERROR(res, "vmaCreateBuffer() for CPU expand buffer failed: "); - return false; - } - - const VkBufferCreateInfo gpu_bci = {VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, nullptr, 0, size, - VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT, VK_SHARING_MODE_EXCLUSIVE}; - const VmaAllocationCreateInfo gpu_aci = {0, VMA_MEMORY_USAGE_GPU_ONLY, 0, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT}; - VmaAllocationInfo ai; - res = vmaCreateBuffer(m_allocator, &gpu_bci, &gpu_aci, gpu_buffer, gpu_allocation, &ai); - if (res != VK_SUCCESS) - { - LOG_VULKAN_ERROR(res, "vmaCreateBuffer() for expand buffer failed: "); - vmaDestroyBuffer(m_allocator, cpu_buffer, cpu_allocation); - return false; - } - - const VkBufferCopy buf_copy = {0u, 0u, size}; - fill_callback(cpu_ai.pMappedData); - vmaFlushAllocation(m_allocator, cpu_allocation, 0, size); - vkCmdCopyBuffer(GetCurrentInitCommandBuffer(), cpu_buffer, *gpu_buffer, 1, &buf_copy); - DeferBufferDestruction(cpu_buffer, cpu_allocation); - return true; -} diff --git a/pcsx2/GS/Renderers/Vulkan/VKContext.h b/pcsx2/GS/Renderers/Vulkan/VKContext.h deleted file mode 100644 index 383666428d..0000000000 --- a/pcsx2/GS/Renderers/Vulkan/VKContext.h +++ /dev/null @@ -1,405 +0,0 @@ -/* PCSX2 - PS2 Emulator for PCs - * Copyright (C) 2002-2023 PCSX2 Dev Team - * - * PCSX2 is free software: you can redistribute it and/or modify it under the terms - * of the GNU Lesser General Public License as published by the Free Software Found- - * ation, either version 3 of the License, or (at your option) any later version. - * - * PCSX2 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 PCSX2. - * If not, see . - */ - -#pragma once - -#include "GS/Renderers/Vulkan/VKLoader.h" -#include "GS/Renderers/Vulkan/VKStreamBuffer.h" - -#include "common/ReadbackSpinManager.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -struct WindowInfo; - -class VKSwapChain; - -class VKContext -{ -public: - enum : u32 - { - NUM_COMMAND_BUFFERS = 3, - TEXTURE_BUFFER_SIZE = 64 * 1024 * 1024, - }; - - struct OptionalExtensions - { - bool vk_ext_provoking_vertex : 1; - bool vk_ext_memory_budget : 1; - bool vk_ext_calibrated_timestamps : 1; - bool vk_ext_line_rasterization : 1; - bool vk_ext_rasterization_order_attachment_access : 1; - bool vk_ext_attachment_feedback_loop_layout : 1; - bool vk_ext_full_screen_exclusive : 1; - bool vk_khr_driver_properties : 1; - bool vk_khr_fragment_shader_barycentric : 1; - bool vk_khr_shader_draw_parameters : 1; - }; - - ~VKContext(); - - // Helper method to create a Vulkan instance. - static VkInstance CreateVulkanInstance( - const WindowInfo& wi, bool enable_debug_utils, bool enable_validation_layer); - - // Returns a list of Vulkan-compatible GPUs. - using GPUList = std::vector>; - static GPUList EnumerateGPUs(VkInstance instance); - - // Creates a new context and sets it up as global. - static bool Create(VkInstance instance, VkSurfaceKHR surface, VkPhysicalDevice physical_device, - bool threaded_presentation, bool enable_debug_utils, bool enable_validation_layer); - - // Destroys context. - static void Destroy(); - - // Enable/disable debug message runtime. - bool EnableDebugUtils(); - void DisableDebugUtils(); - - // Global state accessors - __fi VkInstance GetVulkanInstance() const { return m_instance; } - __fi VkPhysicalDevice GetPhysicalDevice() const { return m_physical_device; } - __fi VkDevice GetDevice() const { return m_device; } - __fi VmaAllocator GetAllocator() const { return m_allocator; } - __fi VkQueue GetGraphicsQueue() const { return m_graphics_queue; } - __fi u32 GetGraphicsQueueFamilyIndex() const { return m_graphics_queue_family_index; } - __fi VkQueue GetPresentQueue() const { return m_present_queue; } - __fi u32 GetPresentQueueFamilyIndex() const { return m_present_queue_family_index; } - __fi const VkQueueFamilyProperties& GetGraphicsQueueProperties() const { return m_graphics_queue_properties; } - __fi const VkPhysicalDeviceMemoryProperties& GetDeviceMemoryProperties() const - { - return m_device_memory_properties; - } - __fi const VkPhysicalDeviceProperties& GetDeviceProperties() const { return m_device_properties; } - __fi const VkPhysicalDeviceFeatures& GetDeviceFeatures() const { return m_device_features; } - __fi const VkPhysicalDeviceLimits& GetDeviceLimits() const { return m_device_properties.limits; } - __fi const VkPhysicalDeviceDriverProperties& GetDeviceDriverProperties() const { return m_device_driver_properties; } - __fi const OptionalExtensions& GetOptionalExtensions() const { return m_optional_extensions; } - - // The interaction between raster order attachment access and fbfetch is unclear. - __fi bool UseFeedbackLoopLayout() const - { - return (m_optional_extensions.vk_ext_attachment_feedback_loop_layout && - !m_optional_extensions.vk_ext_rasterization_order_attachment_access); - } - - // Helpers for getting constants - __fi u32 GetUniformBufferAlignment() const - { - return static_cast(m_device_properties.limits.minUniformBufferOffsetAlignment); - } - __fi u32 GetTexelBufferAlignment() const - { - return static_cast(m_device_properties.limits.minTexelBufferOffsetAlignment); - } - __fi u32 GetStorageBufferAlignment() const - { - return static_cast(m_device_properties.limits.minStorageBufferOffsetAlignment); - } - __fi u32 GetBufferImageGranularity() const - { - return static_cast(m_device_properties.limits.bufferImageGranularity); - } - __fi u32 GetBufferCopyOffsetAlignment() const - { - return static_cast(m_device_properties.limits.optimalBufferCopyOffsetAlignment); - } - __fi u32 GetBufferCopyRowPitchAlignment() const - { - return static_cast(m_device_properties.limits.optimalBufferCopyRowPitchAlignment); - } - __fi u32 GetMaxImageDimension2D() const - { - return m_device_properties.limits.maxImageDimension2D; - } - - /// Returns true if running on an NVIDIA GPU. - __fi bool IsDeviceNVIDIA() const { return (m_device_properties.vendorID == 0x10DE); } - - // Creates a simple render pass. - VkRenderPass GetRenderPass(VkFormat color_format, VkFormat depth_format, - VkAttachmentLoadOp color_load_op = VK_ATTACHMENT_LOAD_OP_LOAD, - VkAttachmentStoreOp color_store_op = VK_ATTACHMENT_STORE_OP_STORE, - VkAttachmentLoadOp depth_load_op = VK_ATTACHMENT_LOAD_OP_LOAD, - VkAttachmentStoreOp depth_store_op = VK_ATTACHMENT_STORE_OP_STORE, - VkAttachmentLoadOp stencil_load_op = VK_ATTACHMENT_LOAD_OP_DONT_CARE, - VkAttachmentStoreOp stencil_store_op = VK_ATTACHMENT_STORE_OP_DONT_CARE, - bool color_feedback_loop = false, bool depth_sampling = false); - - // Gets a non-clearing version of the specified render pass. Slow, don't call in hot path. - VkRenderPass GetRenderPassForRestarting(VkRenderPass pass); - - // These command buffers are allocated per-frame. They are valid until the command buffer - // is submitted, after that you should call these functions again. - __fi u32 GetCurrentCommandBufferIndex() const { return m_current_frame; } - __fi VkDescriptorPool GetGlobalDescriptorPool() const { return m_global_descriptor_pool; } - __fi VkCommandBuffer GetCurrentCommandBuffer() const { return m_current_command_buffer; } - __fi VKStreamBuffer& GetTextureUploadBuffer() { return m_texture_upload_buffer; } - __fi VkDescriptorPool GetCurrentDescriptorPool() const - { - return m_frame_resources[m_current_frame].descriptor_pool; - } - VkCommandBuffer GetCurrentInitCommandBuffer(); - - /// Allocates a descriptor set from the pool reserved for the current frame. - VkDescriptorSet AllocateDescriptorSet(VkDescriptorSetLayout set_layout); - - /// Allocates a descriptor set from the pool reserved for the current frame. - VkDescriptorSet AllocatePersistentDescriptorSet(VkDescriptorSetLayout set_layout); - - /// Frees a descriptor set allocated from the global pool. - void FreeGlobalDescriptorSet(VkDescriptorSet set); - - // Gets the fence that will be signaled when the currently executing command buffer is - // queued and executed. Do not wait for this fence before the buffer is executed. - __fi VkFence GetCurrentCommandBufferFence() const { return m_frame_resources[m_current_frame].fence; } - - // Fence "counters" are used to track which commands have been completed by the GPU. - // If the last completed fence counter is greater or equal to N, it means that the work - // associated counter N has been completed by the GPU. The value of N to associate with - // commands can be retreived by calling GetCurrentFenceCounter(). - u64 GetCompletedFenceCounter() const { return m_completed_fence_counter; } - - // Gets the fence that will be signaled when the currently executing command buffer is - // queued and executed. Do not wait for this fence before the buffer is executed. - u64 GetCurrentFenceCounter() const { return m_frame_resources[m_current_frame].fence_counter; } - - void SubmitCommandBuffer(VKSwapChain* present_swap_chain = nullptr, bool submit_on_thread = false); - void MoveToNextCommandBuffer(); - - enum class WaitType - { - None, - Sleep, - Spin, - }; - - void ExecuteCommandBuffer(WaitType wait_for_completion); - void WaitForPresentComplete(); - - // Was the last present submitted to the queue a failure? If so, we must recreate our swapchain. - bool CheckLastPresentFail(); - bool CheckLastSubmitFail(); - - // Schedule a vulkan resource for destruction later on. This will occur when the command buffer - // is next re-used, and the GPU has finished working with the specified resource. - void DeferBufferDestruction(VkBuffer object); - void DeferBufferDestruction(VkBuffer object, VmaAllocation allocation); - void DeferBufferViewDestruction(VkBufferView object); - void DeferDeviceMemoryDestruction(VkDeviceMemory object); - void DeferFramebufferDestruction(VkFramebuffer object); - void DeferImageDestruction(VkImage object); - void DeferImageDestruction(VkImage object, VmaAllocation allocation); - void DeferImageViewDestruction(VkImageView object); - void DeferPipelineDestruction(VkPipeline pipeline); - void DeferSamplerDestruction(VkSampler sampler); - - // Wait for a fence to be completed. - // Also invokes callbacks for completion. - void WaitForFenceCounter(u64 fence_counter); - - void WaitForGPUIdle(); - - float GetAndResetAccumulatedGPUTime(); - bool SetEnableGPUTiming(bool enabled); - - void CountRenderPass() { m_command_buffer_render_passes++; } - void NotifyOfReadback(); - - // Allocates a temporary CPU staging buffer, fires the callback with it to populate, then copies to a GPU buffer. - bool AllocatePreinitializedGPUBuffer(u32 size, VkBuffer* gpu_buffer, VmaAllocation* gpu_allocation, - VkBufferUsageFlags gpu_usage, const std::function& fill_callback); - -private: - VKContext(VkInstance instance, VkPhysicalDevice physical_device); - - union RenderPassCacheKey - { - struct - { - u32 color_format : 8; - u32 depth_format : 8; - u32 color_load_op : 2; - u32 color_store_op : 1; - u32 depth_load_op : 2; - u32 depth_store_op : 1; - u32 stencil_load_op : 2; - u32 stencil_store_op : 1; - u32 color_feedback_loop : 1; - u32 depth_sampling : 1; - }; - - u32 key; - }; - - using ExtensionList = std::vector; - static bool SelectInstanceExtensions( - ExtensionList* extension_list, const WindowInfo& wi, bool enable_debug_utils); - bool SelectDeviceExtensions(ExtensionList* extension_list, bool enable_surface); - bool SelectDeviceFeatures(const VkPhysicalDeviceFeatures* required_features); - bool CreateDevice(VkSurfaceKHR surface, bool enable_validation_layer, const char** required_device_extensions, - u32 num_required_device_extensions, const char** required_device_layers, u32 num_required_device_layers, - const VkPhysicalDeviceFeatures* required_features); - void ProcessDeviceExtensions(); - - bool CreateAllocator(); - void DestroyAllocator(); - bool CreateCommandBuffers(); - void DestroyCommandBuffers(); - bool CreateGlobalDescriptorPool(); - void DestroyGlobalDescriptorPool(); - bool CreateTextureStreamBuffer(); - - VkRenderPass CreateCachedRenderPass(RenderPassCacheKey key); - void DestroyRenderPassCache(); - - void CommandBufferCompleted(u32 index); - void ActivateCommandBuffer(u32 index); - void ScanForCommandBufferCompletion(); - void WaitForCommandBufferCompletion(u32 index); - - void DoSubmitCommandBuffer(u32 index, VKSwapChain* present_swap_chain, u32 spin_cycles); - void DoPresent(VKSwapChain* present_swap_chain); - void WaitForPresentComplete(std::unique_lock& lock); - void PresentThread(); - void StartPresentThread(); - void StopPresentThread(); - - bool InitSpinResources(); - void DestroySpinResources(); - void WaitForSpinCompletion(u32 index); - void SpinCommandCompleted(u32 index); - void SubmitSpinCommand(u32 index, u32 cycles); - void CalibrateSpinTimestamp(); - u64 GetCPUTimestamp(); - - struct FrameResources - { - // [0] - Init (upload) command buffer, [1] - draw command buffer - VkCommandPool command_pool = VK_NULL_HANDLE; - std::array command_buffers{VK_NULL_HANDLE, VK_NULL_HANDLE}; - VkDescriptorPool descriptor_pool = VK_NULL_HANDLE; - VkFence fence = VK_NULL_HANDLE; - u64 fence_counter = 0; - s32 spin_id = -1; - u32 submit_timestamp = 0; - bool init_buffer_used = false; - bool needs_fence_wait = false; - bool timestamp_written = false; - - std::vector> cleanup_resources; - }; - - struct SpinResources - { - VkCommandPool command_pool = VK_NULL_HANDLE; - VkCommandBuffer command_buffer = VK_NULL_HANDLE; - VkSemaphore semaphore = VK_NULL_HANDLE; - VkFence fence = VK_NULL_HANDLE; - u32 cycles = 0; - bool in_progress = false; - }; - - VkInstance m_instance = VK_NULL_HANDLE; - VkPhysicalDevice m_physical_device = VK_NULL_HANDLE; - VkDevice m_device = VK_NULL_HANDLE; - VmaAllocator m_allocator = VK_NULL_HANDLE; - - VkCommandBuffer m_current_command_buffer = VK_NULL_HANDLE; - - VkDescriptorPool m_global_descriptor_pool = VK_NULL_HANDLE; - - VkQueue m_graphics_queue = VK_NULL_HANDLE; - VkQueue m_present_queue = VK_NULL_HANDLE; - u32 m_graphics_queue_family_index = 0; - u32 m_present_queue_family_index = 0; - - ReadbackSpinManager m_spin_manager; - VkQueue m_spin_queue = VK_NULL_HANDLE; - VkDescriptorSetLayout m_spin_descriptor_set_layout = VK_NULL_HANDLE; - VkPipelineLayout m_spin_pipeline_layout = VK_NULL_HANDLE; - VkPipeline m_spin_pipeline = VK_NULL_HANDLE; - VkBuffer m_spin_buffer = VK_NULL_HANDLE; - VmaAllocation m_spin_buffer_allocation = VK_NULL_HANDLE; - VkDescriptorSet m_spin_descriptor_set = VK_NULL_HANDLE; - std::array m_spin_resources; -#ifdef _WIN32 - double m_queryperfcounter_to_ns = 0; -#endif - double m_spin_timestamp_scale = 0; - double m_spin_timestamp_offset = 0; - u32 m_spin_queue_family_index = 0; - u32 m_command_buffer_render_passes = 0; - u32 m_spin_timer = 0; - bool m_spinning_supported = false; - bool m_spin_queue_is_graphics_queue = false; - bool m_spin_buffer_initialized = false; - - VkQueryPool m_timestamp_query_pool = VK_NULL_HANDLE; - float m_accumulated_gpu_time = 0.0f; - bool m_gpu_timing_enabled = false; - bool m_gpu_timing_supported = false; - bool m_wants_new_timestamp_calibration = false; - VkTimeDomainEXT m_calibrated_timestamp_type = VK_TIME_DOMAIN_DEVICE_EXT; - - std::array m_frame_resources; - u64 m_next_fence_counter = 1; - u64 m_completed_fence_counter = 0; - u32 m_current_frame = 0; - - VKStreamBuffer m_texture_upload_buffer; - - std::atomic_bool m_last_submit_failed{false}; - std::atomic_bool m_last_present_failed{false}; - std::atomic_bool m_present_done{true}; - std::mutex m_present_mutex; - std::condition_variable m_present_queued_cv; - std::condition_variable m_present_done_cv; - std::thread m_present_thread; - std::atomic_bool m_present_thread_done{false}; - - struct QueuedPresent - { - VKSwapChain* swap_chain; - u32 command_buffer_index; - u32 spin_cycles; - }; - - QueuedPresent m_queued_present = {}; - - std::map m_render_pass_cache; - - VkDebugUtilsMessengerEXT m_debug_messenger_callback = VK_NULL_HANDLE; - - VkQueueFamilyProperties m_graphics_queue_properties = {}; - VkPhysicalDeviceFeatures m_device_features = {}; - VkPhysicalDeviceProperties m_device_properties = {}; - VkPhysicalDeviceMemoryProperties m_device_memory_properties = {}; - VkPhysicalDeviceDriverPropertiesKHR m_device_driver_properties = {}; - OptionalExtensions m_optional_extensions = {}; -}; - -extern std::unique_ptr g_vulkan_context; diff --git a/pcsx2/GS/Renderers/Vulkan/VKLoader.cpp b/pcsx2/GS/Renderers/Vulkan/VKLoader.cpp index 1803d833c7..05cfd202e5 100644 --- a/pcsx2/GS/Renderers/Vulkan/VKLoader.cpp +++ b/pcsx2/GS/Renderers/Vulkan/VKLoader.cpp @@ -15,7 +15,8 @@ #include "GS/Renderers/Vulkan/VKLoader.h" -#include +#include "common/Assertions.h" + #include #include #include @@ -54,20 +55,19 @@ void Vulkan::ResetVulkanLibraryFunctionPointers() #if defined(_WIN32) -static HMODULE vulkan_module; -static std::atomic_int vulkan_module_ref_count = {0}; +static HMODULE s_vulkan_module; + +bool Vulkan::IsVulkanLibraryLoaded() +{ + return s_vulkan_module != NULL; +} bool Vulkan::LoadVulkanLibrary() { - // Not thread safe if a second thread calls the loader whilst the first is still in-progress. - if (vulkan_module) - { - vulkan_module_ref_count++; - return true; - } + pxAssertRel(!s_vulkan_module, "Vulkan module is not loaded."); - vulkan_module = LoadLibraryA("vulkan-1.dll"); - if (!vulkan_module) + s_vulkan_module = LoadLibraryA("vulkan-1.dll"); + if (!s_vulkan_module) { std::fprintf(stderr, "Failed to load vulkan-1.dll\n"); return false; @@ -75,7 +75,7 @@ bool Vulkan::LoadVulkanLibrary() bool required_functions_missing = false; auto LoadFunction = [&](FARPROC* func_ptr, const char* name, bool is_required) { - *func_ptr = GetProcAddress(vulkan_module, name); + *func_ptr = GetProcAddress(s_vulkan_module, name); if (!(*func_ptr) && is_required) { std::fprintf(stderr, "Vulkan: Failed to load required module function %s\n", name); @@ -90,45 +90,41 @@ bool Vulkan::LoadVulkanLibrary() if (required_functions_missing) { ResetVulkanLibraryFunctionPointers(); - FreeLibrary(vulkan_module); - vulkan_module = nullptr; + FreeLibrary(s_vulkan_module); + s_vulkan_module = nullptr; return false; } - vulkan_module_ref_count++; return true; } void Vulkan::UnloadVulkanLibrary() { - if ((--vulkan_module_ref_count) > 0) - return; - ResetVulkanLibraryFunctionPointers(); - FreeLibrary(vulkan_module); - vulkan_module = nullptr; + if (s_vulkan_module) + FreeLibrary(s_vulkan_module); + s_vulkan_module = nullptr; } #else -static void* vulkan_module; -static std::atomic_int vulkan_module_ref_count = {0}; +static void* s_vulkan_module; + +bool Vulkan::IsVulkanLibraryLoaded() +{ + return s_vulkan_module != nullptr; +} bool Vulkan::LoadVulkanLibrary() { - // Not thread safe if a second thread calls the loader whilst the first is still in-progress. - if (vulkan_module) - { - vulkan_module_ref_count++; - return true; - } + pxAssertRel(!s_vulkan_module, "Vulkan module is not loaded."); #if defined(__APPLE__) // Check if a path to a specific Vulkan library has been specified. char* libvulkan_env = getenv("LIBVULKAN_PATH"); if (libvulkan_env) - vulkan_module = dlopen(libvulkan_env, RTLD_NOW); - if (!vulkan_module) + s_vulkan_module = dlopen(libvulkan_env, RTLD_NOW); + if (!s_vulkan_module) { unsigned path_size = 0; _NSGetExecutablePath(nullptr, &path_size); @@ -143,24 +139,24 @@ bool Vulkan::LoadVulkanLibrary() { path.erase(pos); path += "/../Frameworks/libMoltenVK.dylib"; - vulkan_module = dlopen(path.c_str(), RTLD_NOW); + s_vulkan_module = dlopen(path.c_str(), RTLD_NOW); } } } - if (!vulkan_module) - vulkan_module = dlopen("libvulkan.dylib", RTLD_NOW); + if (!s_vulkan_module) + s_vulkan_module = dlopen("libvulkan.dylib", RTLD_NOW); #else // Names of libraries to search. Desktop should use libvulkan.so.1 or libvulkan.so. static const char* search_lib_names[] = {"libvulkan.so.1", "libvulkan.so"}; for (size_t i = 0; i < sizeof(search_lib_names) / sizeof(search_lib_names[0]); i++) { - vulkan_module = dlopen(search_lib_names[i], RTLD_NOW); - if (vulkan_module) + s_vulkan_module = dlopen(search_lib_names[i], RTLD_NOW); + if (s_vulkan_module) break; } #endif - if (!vulkan_module) + if (!s_vulkan_module) { std::fprintf(stderr, "Failed to load or locate libvulkan.so\n"); return false; @@ -168,7 +164,7 @@ bool Vulkan::LoadVulkanLibrary() bool required_functions_missing = false; auto LoadFunction = [&](void** func_ptr, const char* name, bool is_required) { - *func_ptr = dlsym(vulkan_module, name); + *func_ptr = dlsym(s_vulkan_module, name); if (!(*func_ptr) && is_required) { std::fprintf(stderr, "Vulkan: Failed to load required module function %s\n", name); @@ -183,23 +179,20 @@ bool Vulkan::LoadVulkanLibrary() if (required_functions_missing) { ResetVulkanLibraryFunctionPointers(); - dlclose(vulkan_module); - vulkan_module = nullptr; + dlclose(s_vulkan_module); + s_vulkan_module = nullptr; return false; } - vulkan_module_ref_count++; return true; } void Vulkan::UnloadVulkanLibrary() { - if ((--vulkan_module_ref_count) > 0) - return; - ResetVulkanLibraryFunctionPointers(); - dlclose(vulkan_module); - vulkan_module = nullptr; + if (s_vulkan_module) + dlclose(s_vulkan_module); + s_vulkan_module = nullptr; } #endif diff --git a/pcsx2/GS/Renderers/Vulkan/VKLoader.h b/pcsx2/GS/Renderers/Vulkan/VKLoader.h index c835ab1d9c..9cb3f06681 100644 --- a/pcsx2/GS/Renderers/Vulkan/VKLoader.h +++ b/pcsx2/GS/Renderers/Vulkan/VKLoader.h @@ -97,6 +97,7 @@ namespace Vulkan { + bool IsVulkanLibraryLoaded(); bool LoadVulkanLibrary(); bool LoadVulkanInstanceFunctions(VkInstance instance); bool LoadVulkanDeviceFunctions(VkDevice device); diff --git a/pcsx2/GS/Renderers/Vulkan/VKShaderCache.cpp b/pcsx2/GS/Renderers/Vulkan/VKShaderCache.cpp index 34b02e547a..f9a3a4ac14 100644 --- a/pcsx2/GS/Renderers/Vulkan/VKShaderCache.cpp +++ b/pcsx2/GS/Renderers/Vulkan/VKShaderCache.cpp @@ -16,8 +16,8 @@ #include "PrecompiledHeader.h" #include "GS/Renderers/Vulkan/VKShaderCache.h" -#include "GS/Renderers/Vulkan/VKContext.h" -#include "GS/Renderers/Vulkan/VKUtil.h" +#include "GS/Renderers/Vulkan/GSDeviceVK.h" +#include "GS/Renderers/Vulkan/VKBuilders.h" #include "GS/GS.h" #include "Config.h" @@ -81,21 +81,21 @@ static bool ValidatePipelineCacheHeader(const VK_PIPELINE_CACHE_HEADER& header) return false; } - if (header.vendor_id != g_vulkan_context->GetDeviceProperties().vendorID) + if (header.vendor_id != GSDeviceVK::GetInstance()->GetDeviceProperties().vendorID) { Console.Error("Pipeline cache failed validation: Incorrect vendor ID (file: 0x%X, device: 0x%X)", - header.vendor_id, g_vulkan_context->GetDeviceProperties().vendorID); + header.vendor_id, GSDeviceVK::GetInstance()->GetDeviceProperties().vendorID); return false; } - if (header.device_id != g_vulkan_context->GetDeviceProperties().deviceID) + if (header.device_id != GSDeviceVK::GetInstance()->GetDeviceProperties().deviceID) { Console.Error("Pipeline cache failed validation: Incorrect device ID (file: 0x%X, device: 0x%X)", - header.device_id, g_vulkan_context->GetDeviceProperties().deviceID); + header.device_id, GSDeviceVK::GetInstance()->GetDeviceProperties().deviceID); return false; } - if (std::memcmp(header.uuid, g_vulkan_context->GetDeviceProperties().pipelineCacheUUID, VK_UUID_SIZE) != 0) + if (std::memcmp(header.uuid, GSDeviceVK::GetInstance()->GetDeviceProperties().pipelineCacheUUID, VK_UUID_SIZE) != 0) { Console.Error("Pipeline cache failed validation: Incorrect UUID"); return false; @@ -108,9 +108,9 @@ static void FillPipelineCacheHeader(VK_PIPELINE_CACHE_HEADER* header) { header->header_length = sizeof(VK_PIPELINE_CACHE_HEADER); header->header_version = VK_PIPELINE_CACHE_HEADER_VERSION_ONE; - header->vendor_id = g_vulkan_context->GetDeviceProperties().vendorID; - header->device_id = g_vulkan_context->GetDeviceProperties().deviceID; - std::memcpy(header->uuid, g_vulkan_context->GetDeviceProperties().pipelineCacheUUID, VK_UUID_SIZE); + header->vendor_id = GSDeviceVK::GetInstance()->GetDeviceProperties().vendorID; + header->device_id = GSDeviceVK::GetInstance()->GetDeviceProperties().deviceID; + std::memcpy(header->uuid, GSDeviceVK::GetInstance()->GetDeviceProperties().pipelineCacheUUID, VK_UUID_SIZE); } static unsigned s_next_bad_shader_id = 1; @@ -243,12 +243,11 @@ bool VKShaderCache::CacheIndexKey::operator!=(const CacheIndexKey& key) const source_length != key.source_length || shader_type != key.shader_type); } -bool VKShaderCache::Create() +void VKShaderCache::Create() { pxAssert(!g_vulkan_shader_cache); g_vulkan_shader_cache.reset(new VKShaderCache()); g_vulkan_shader_cache->Open(); - return true; } void VKShaderCache::Destroy() @@ -433,7 +432,7 @@ bool VKShaderCache::CreateNewPipelineCache() } const VkPipelineCacheCreateInfo ci{VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO, nullptr, 0, 0, nullptr}; - VkResult res = vkCreatePipelineCache(g_vulkan_context->GetDevice(), &ci, nullptr, &m_pipeline_cache); + VkResult res = vkCreatePipelineCache(GSDeviceVK::GetInstance()->GetDevice(), &ci, nullptr, &m_pipeline_cache); if (res != VK_SUCCESS) { LOG_VULKAN_ERROR(res, "vkCreatePipelineCache() failed: "); @@ -463,7 +462,7 @@ bool VKShaderCache::ReadExistingPipelineCache() const VkPipelineCacheCreateInfo ci{ VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO, nullptr, 0, data->size(), data->data()}; - VkResult res = vkCreatePipelineCache(g_vulkan_context->GetDevice(), &ci, nullptr, &m_pipeline_cache); + VkResult res = vkCreatePipelineCache(GSDeviceVK::GetInstance()->GetDevice(), &ci, nullptr, &m_pipeline_cache); if (res != VK_SUCCESS) { LOG_VULKAN_ERROR(res, "vkCreatePipelineCache() failed: "); @@ -479,7 +478,7 @@ bool VKShaderCache::FlushPipelineCache() return false; size_t data_size; - VkResult res = vkGetPipelineCacheData(g_vulkan_context->GetDevice(), m_pipeline_cache, &data_size, nullptr); + VkResult res = vkGetPipelineCacheData(GSDeviceVK::GetInstance()->GetDevice(), m_pipeline_cache, &data_size, nullptr); if (res != VK_SUCCESS) { LOG_VULKAN_ERROR(res, "vkGetPipelineCacheData() failed: "); @@ -487,7 +486,7 @@ bool VKShaderCache::FlushPipelineCache() } std::vector data(data_size); - res = vkGetPipelineCacheData(g_vulkan_context->GetDevice(), m_pipeline_cache, &data_size, data.data()); + res = vkGetPipelineCacheData(GSDeviceVK::GetInstance()->GetDevice(), m_pipeline_cache, &data_size, data.data()); if (res != VK_SUCCESS) { LOG_VULKAN_ERROR(res, "vkGetPipelineCacheData() (2) failed: "); @@ -521,7 +520,7 @@ void VKShaderCache::ClosePipelineCache() if (m_pipeline_cache == VK_NULL_HANDLE) return; - vkDestroyPipelineCache(g_vulkan_context->GetDevice(), m_pipeline_cache, nullptr); + vkDestroyPipelineCache(GSDeviceVK::GetInstance()->GetDevice(), m_pipeline_cache, nullptr); m_pipeline_cache = VK_NULL_HANDLE; } @@ -596,7 +595,7 @@ VkShaderModule VKShaderCache::GetShaderModule(u32 type, std::string_view shader_ VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, nullptr, 0, spv->size() * sizeof(SPIRVCodeType), spv->data()}; VkShaderModule mod; - VkResult res = vkCreateShaderModule(g_vulkan_context->GetDevice(), &ci, nullptr, &mod); + VkResult res = vkCreateShaderModule(GSDeviceVK::GetInstance()->GetDevice(), &ci, nullptr, &mod); if (res != VK_SUCCESS) { LOG_VULKAN_ERROR(res, "vkCreateShaderModule() failed: "); diff --git a/pcsx2/GS/Renderers/Vulkan/VKShaderCache.h b/pcsx2/GS/Renderers/Vulkan/VKShaderCache.h index 1927c30188..b79de972f8 100644 --- a/pcsx2/GS/Renderers/Vulkan/VKShaderCache.h +++ b/pcsx2/GS/Renderers/Vulkan/VKShaderCache.h @@ -32,7 +32,7 @@ class VKShaderCache public: ~VKShaderCache(); - static bool Create(); + static void Create(); static void Destroy(); /// Returns a handle to the pipeline cache. Set set_dirty to true if you are planning on writing to it externally. diff --git a/pcsx2/GS/Renderers/Vulkan/VKStreamBuffer.cpp b/pcsx2/GS/Renderers/Vulkan/VKStreamBuffer.cpp index a69beb96bd..c1a259369d 100644 --- a/pcsx2/GS/Renderers/Vulkan/VKStreamBuffer.cpp +++ b/pcsx2/GS/Renderers/Vulkan/VKStreamBuffer.cpp @@ -15,9 +15,9 @@ #include "PrecompiledHeader.h" +#include "GS/Renderers/Vulkan/GSDeviceVK.h" #include "GS/Renderers/Vulkan/VKStreamBuffer.h" -#include "GS/Renderers/Vulkan/VKContext.h" -#include "GS/Renderers/Vulkan/VKUtil.h" +#include "GS/Renderers/Vulkan/VKBuilders.h" #include "common/Align.h" #include "common/Assertions.h" @@ -79,7 +79,7 @@ bool VKStreamBuffer::Create(VkBufferUsageFlags usage, u32 size) VmaAllocationInfo ai = {}; VkBuffer new_buffer = VK_NULL_HANDLE; VmaAllocation new_allocation = VK_NULL_HANDLE; - VkResult res = vmaCreateBuffer(g_vulkan_context->GetAllocator(), &bci, &aci, &new_buffer, &new_allocation, &ai); + VkResult res = vmaCreateBuffer(GSDeviceVK::GetInstance()->GetAllocator(), &bci, &aci, &new_buffer, &new_allocation, &ai); if (res != VK_SUCCESS) { LOG_VULKAN_ERROR(res, "vkCreateBuffer failed: "); @@ -105,9 +105,9 @@ void VKStreamBuffer::Destroy(bool defer) if (m_buffer != VK_NULL_HANDLE) { if (defer) - g_vulkan_context->DeferBufferDestruction(m_buffer, m_allocation); + GSDeviceVK::GetInstance()->DeferBufferDestruction(m_buffer, m_allocation); else - vmaDestroyBuffer(g_vulkan_context->GetAllocator(), m_buffer, m_allocation); + vmaDestroyBuffer(GSDeviceVK::GetInstance()->GetAllocator(), m_buffer, m_allocation); } m_size = 0; @@ -194,7 +194,7 @@ void VKStreamBuffer::CommitMemory(u32 final_num_bytes) pxAssert(final_num_bytes <= m_current_space); // For non-coherent mappings, flush the memory range - vmaFlushAllocation(g_vulkan_context->GetAllocator(), m_allocation, m_current_offset, final_num_bytes); + vmaFlushAllocation(GSDeviceVK::GetInstance()->GetAllocator(), m_allocation, m_current_offset, final_num_bytes); m_current_offset += final_num_bytes; m_current_space -= final_num_bytes; @@ -204,7 +204,7 @@ void VKStreamBuffer::CommitMemory(u32 final_num_bytes) void VKStreamBuffer::UpdateCurrentFencePosition() { // Has the offset changed since the last fence? - const u64 counter = g_vulkan_context->GetCurrentFenceCounter(); + const u64 counter = GSDeviceVK::GetInstance()->GetCurrentFenceCounter(); if (!m_tracked_fences.empty() && m_tracked_fences.back().first == counter) { // Still haven't executed a command buffer, so just update the offset. @@ -221,7 +221,7 @@ void VKStreamBuffer::UpdateGPUPosition() auto start = m_tracked_fences.begin(); auto end = start; - const u64 completed_counter = g_vulkan_context->GetCompletedFenceCounter(); + const u64 completed_counter = GSDeviceVK::GetInstance()->GetCompletedFenceCounter(); while (end != m_tracked_fences.end() && completed_counter >= end->first) { m_current_gpu_position = end->second; @@ -308,11 +308,11 @@ bool VKStreamBuffer::WaitForClearSpace(u32 num_bytes) // Did any fences satisfy this condition? // Has the command buffer been executed yet? If not, the caller should execute it. - if (iter == m_tracked_fences.end() || iter->first == g_vulkan_context->GetCurrentFenceCounter()) + if (iter == m_tracked_fences.end() || iter->first == GSDeviceVK::GetInstance()->GetCurrentFenceCounter()) return false; // Wait until this fence is signaled. This will fire the callback, updating the GPU position. - g_vulkan_context->WaitForFenceCounter(iter->first); + GSDeviceVK::GetInstance()->WaitForFenceCounter(iter->first); m_tracked_fences.erase( m_tracked_fences.begin(), m_current_offset == iter->second ? m_tracked_fences.end() : ++iter); m_current_offset = new_offset; diff --git a/pcsx2/GS/Renderers/Vulkan/VKSwapChain.cpp b/pcsx2/GS/Renderers/Vulkan/VKSwapChain.cpp index 74608cf485..fe7474e665 100644 --- a/pcsx2/GS/Renderers/Vulkan/VKSwapChain.cpp +++ b/pcsx2/GS/Renderers/Vulkan/VKSwapChain.cpp @@ -15,9 +15,9 @@ #include "PrecompiledHeader.h" +#include "GS/Renderers/Vulkan/GSDeviceVK.h" #include "GS/Renderers/Vulkan/VKSwapChain.h" -#include "GS/Renderers/Vulkan/VKContext.h" -#include "GS/Renderers/Vulkan/VKUtil.h" +#include "GS/Renderers/Vulkan/VKBuilders.h" #include "common/Assertions.h" #include "common/CocoaTools.h" @@ -140,7 +140,7 @@ VkSurfaceKHR VKSwapChain::CreateVulkanSurface(VkInstance instance, VkPhysicalDev void VKSwapChain::DestroyVulkanSurface(VkInstance instance, WindowInfo* wi, VkSurfaceKHR surface) { - vkDestroySurfaceKHR(g_vulkan_context->GetVulkanInstance(), surface, nullptr); + vkDestroySurfaceKHR(GSDeviceVK::GetInstance()->GetVulkanInstance(), surface, nullptr); #if defined(__APPLE__) if (wi->type == WindowInfo::Type::MacOS && wi->surface_handle) @@ -184,7 +184,7 @@ std::optional VKSwapChain::SelectSurfaceFormat(VkSurfaceKHR { u32 format_count; VkResult res = - vkGetPhysicalDeviceSurfaceFormatsKHR(g_vulkan_context->GetPhysicalDevice(), surface, &format_count, nullptr); + vkGetPhysicalDeviceSurfaceFormatsKHR(GSDeviceVK::GetInstance()->GetPhysicalDevice(), surface, &format_count, nullptr); if (res != VK_SUCCESS || format_count == 0) { LOG_VULKAN_ERROR(res, "vkGetPhysicalDeviceSurfaceFormatsKHR failed: "); @@ -193,7 +193,7 @@ std::optional VKSwapChain::SelectSurfaceFormat(VkSurfaceKHR std::vector surface_formats(format_count); res = vkGetPhysicalDeviceSurfaceFormatsKHR( - g_vulkan_context->GetPhysicalDevice(), surface, &format_count, surface_formats.data()); + GSDeviceVK::GetInstance()->GetPhysicalDevice(), surface, &format_count, surface_formats.data()); pxAssert(res == VK_SUCCESS); // If there is a single undefined surface format, the device doesn't care, so we'll just use RGBA @@ -255,7 +255,7 @@ std::optional VKSwapChain::SelectPresentMode(VkSurfaceKHR surf VkResult res; u32 mode_count; res = - vkGetPhysicalDeviceSurfacePresentModesKHR(g_vulkan_context->GetPhysicalDevice(), surface, &mode_count, nullptr); + vkGetPhysicalDeviceSurfacePresentModesKHR(GSDeviceVK::GetInstance()->GetPhysicalDevice(), surface, &mode_count, nullptr); if (res != VK_SUCCESS || mode_count == 0) { LOG_VULKAN_ERROR(res, "vkGetPhysicalDeviceSurfaceFormatsKHR failed: "); @@ -264,7 +264,7 @@ std::optional VKSwapChain::SelectPresentMode(VkSurfaceKHR surf std::vector present_modes(mode_count); res = vkGetPhysicalDeviceSurfacePresentModesKHR( - g_vulkan_context->GetPhysicalDevice(), surface, &mode_count, present_modes.data()); + GSDeviceVK::GetInstance()->GetPhysicalDevice(), surface, &mode_count, present_modes.data()); pxAssert(res == VK_SUCCESS); // Checks if a particular mode is supported, if it is, returns that mode. @@ -315,7 +315,7 @@ bool VKSwapChain::CreateSwapChain() // Look up surface properties to determine image count and dimensions VkSurfaceCapabilitiesKHR surface_capabilities; VkResult res = vkGetPhysicalDeviceSurfaceCapabilitiesKHR( - g_vulkan_context->GetPhysicalDevice(), m_surface, &surface_capabilities); + GSDeviceVK::GetInstance()->GetPhysicalDevice(), m_surface, &surface_capabilities); if (res != VK_SUCCESS) { LOG_VULKAN_ERROR(res, "vkGetPhysicalDeviceSurfaceCapabilitiesKHR failed: "); @@ -373,10 +373,10 @@ bool VKSwapChain::CreateSwapChain() image_count, surface_format->format, surface_format->colorSpace, size, 1u, image_usage, VK_SHARING_MODE_EXCLUSIVE, 0, nullptr, transform, alpha, present_mode.value(), VK_TRUE, old_swap_chain}; std::array indices = {{ - g_vulkan_context->GetGraphicsQueueFamilyIndex(), - g_vulkan_context->GetPresentQueueFamilyIndex(), + GSDeviceVK::GetInstance()->GetGraphicsQueueFamilyIndex(), + GSDeviceVK::GetInstance()->GetPresentQueueFamilyIndex(), }}; - if (g_vulkan_context->GetGraphicsQueueFamilyIndex() != g_vulkan_context->GetPresentQueueFamilyIndex()) + if (GSDeviceVK::GetInstance()->GetGraphicsQueueFamilyIndex() != GSDeviceVK::GetInstance()->GetPresentQueueFamilyIndex()) { swap_chain_info.imageSharingMode = VK_SHARING_MODE_CONCURRENT; swap_chain_info.queueFamilyIndexCount = 2; @@ -389,7 +389,7 @@ bool VKSwapChain::CreateSwapChain() VK_STRUCTURE_TYPE_SURFACE_FULL_SCREEN_EXCLUSIVE_WIN32_INFO_EXT}; if (m_exclusive_fullscreen_control.has_value()) { - if (g_vulkan_context->GetOptionalExtensions().vk_ext_full_screen_exclusive) + if (GSDeviceVK::GetInstance()->GetOptionalExtensions().vk_ext_full_screen_exclusive) { exclusive_info.fullScreenExclusive = (m_exclusive_fullscreen_control.value() ? VK_FULL_SCREEN_EXCLUSIVE_ALLOWED_EXT : @@ -413,7 +413,7 @@ bool VKSwapChain::CreateSwapChain() Console.Error("Exclusive fullscreen control requested, but is not supported on this platform."); #endif - res = vkCreateSwapchainKHR(g_vulkan_context->GetDevice(), &swap_chain_info, nullptr, &m_swap_chain); + res = vkCreateSwapchainKHR(GSDeviceVK::GetInstance()->GetDevice(), &swap_chain_info, nullptr, &m_swap_chain); if (res != VK_SUCCESS) { LOG_VULKAN_ERROR(res, "vkCreateSwapchainKHR failed: "); @@ -423,7 +423,7 @@ bool VKSwapChain::CreateSwapChain() // Now destroy the old swap chain, since it's been recreated. // We can do this immediately since all work should have been completed before calling resize. if (old_swap_chain != VK_NULL_HANDLE) - vkDestroySwapchainKHR(g_vulkan_context->GetDevice(), old_swap_chain, nullptr); + vkDestroySwapchainKHR(GSDeviceVK::GetInstance()->GetDevice(), old_swap_chain, nullptr); m_window_info.surface_width = std::max(1u, size.width); m_window_info.surface_height = std::max(1u, size.height); @@ -431,7 +431,7 @@ bool VKSwapChain::CreateSwapChain() // Get and create images. pxAssert(m_images.empty()); - res = vkGetSwapchainImagesKHR(g_vulkan_context->GetDevice(), m_swap_chain, &image_count, nullptr); + res = vkGetSwapchainImagesKHR(GSDeviceVK::GetInstance()->GetDevice(), m_swap_chain, &image_count, nullptr); if (res != VK_SUCCESS) { LOG_VULKAN_ERROR(res, "vkGetSwapchainImagesKHR failed: "); @@ -439,7 +439,7 @@ bool VKSwapChain::CreateSwapChain() } std::vector images(image_count); - res = vkGetSwapchainImagesKHR(g_vulkan_context->GetDevice(), m_swap_chain, &image_count, images.data()); + res = vkGetSwapchainImagesKHR(GSDeviceVK::GetInstance()->GetDevice(), m_swap_chain, &image_count, images.data()); pxAssert(res == VK_SUCCESS); m_images.reserve(image_count); @@ -462,7 +462,7 @@ bool VKSwapChain::CreateSwapChain() ImageSemaphores sema; const VkSemaphoreCreateInfo semaphore_info = {VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO, nullptr, 0}; - res = vkCreateSemaphore(g_vulkan_context->GetDevice(), &semaphore_info, nullptr, &sema.available_semaphore); + res = vkCreateSemaphore(GSDeviceVK::GetInstance()->GetDevice(), &semaphore_info, nullptr, &sema.available_semaphore); if (res != VK_SUCCESS) { LOG_VULKAN_ERROR(res, "vkCreateSemaphore failed: "); @@ -470,11 +470,11 @@ bool VKSwapChain::CreateSwapChain() } res = vkCreateSemaphore( - g_vulkan_context->GetDevice(), &semaphore_info, nullptr, &sema.rendering_finished_semaphore); + GSDeviceVK::GetInstance()->GetDevice(), &semaphore_info, nullptr, &sema.rendering_finished_semaphore); if (res != VK_SUCCESS) { LOG_VULKAN_ERROR(res, "vkCreateSemaphore failed: "); - vkDestroySemaphore(g_vulkan_context->GetDevice(), sema.available_semaphore, nullptr); + vkDestroySemaphore(GSDeviceVK::GetInstance()->GetDevice(), sema.available_semaphore, nullptr); return false; } @@ -494,8 +494,8 @@ void VKSwapChain::DestroySwapChainImages() m_images.clear(); for (auto& it : m_semaphores) { - vkDestroySemaphore(g_vulkan_context->GetDevice(), it.rendering_finished_semaphore, nullptr); - vkDestroySemaphore(g_vulkan_context->GetDevice(), it.available_semaphore, nullptr); + vkDestroySemaphore(GSDeviceVK::GetInstance()->GetDevice(), it.rendering_finished_semaphore, nullptr); + vkDestroySemaphore(GSDeviceVK::GetInstance()->GetDevice(), it.available_semaphore, nullptr); } m_semaphores.clear(); @@ -509,7 +509,7 @@ void VKSwapChain::DestroySwapChain() if (m_swap_chain == VK_NULL_HANDLE) return; - vkDestroySwapchainKHR(g_vulkan_context->GetDevice(), m_swap_chain, nullptr); + vkDestroySwapchainKHR(GSDeviceVK::GetInstance()->GetDevice(), m_swap_chain, nullptr); m_swap_chain = VK_NULL_HANDLE; m_window_info.surface_width = 0; m_window_info.surface_height = 0; @@ -534,7 +534,7 @@ VkResult VKSwapChain::AcquireNextImage() // Use a different semaphore for each image. m_current_semaphore = (m_current_semaphore + 1) % static_cast(m_semaphores.size()); - const VkResult res = vkAcquireNextImageKHR(g_vulkan_context->GetDevice(), m_swap_chain, UINT64_MAX, + const VkResult res = vkAcquireNextImageKHR(GSDeviceVK::GetInstance()->GetDevice(), m_swap_chain, UINT64_MAX, m_semaphores[m_current_semaphore].available_semaphore, VK_NULL_HANDLE, &m_current_image); m_image_acquire_result = res; return res; @@ -594,14 +594,14 @@ bool VKSwapChain::RecreateSurface(const WindowInfo& new_wi) // Re-create the surface with the new native handle m_window_info = new_wi; m_surface = CreateVulkanSurface( - g_vulkan_context->GetVulkanInstance(), g_vulkan_context->GetPhysicalDevice(), &m_window_info); + GSDeviceVK::GetInstance()->GetVulkanInstance(), GSDeviceVK::GetInstance()->GetPhysicalDevice(), &m_window_info); if (m_surface == VK_NULL_HANDLE) return false; // The validation layers get angry at us if we don't call this before creating the swapchain. VkBool32 present_supported = VK_TRUE; - VkResult res = vkGetPhysicalDeviceSurfaceSupportKHR(g_vulkan_context->GetPhysicalDevice(), - g_vulkan_context->GetPresentQueueFamilyIndex(), m_surface, &present_supported); + VkResult res = vkGetPhysicalDeviceSurfaceSupportKHR(GSDeviceVK::GetInstance()->GetPhysicalDevice(), + GSDeviceVK::GetInstance()->GetPresentQueueFamilyIndex(), m_surface, &present_supported); if (res != VK_SUCCESS) { LOG_VULKAN_ERROR(res, "vkGetPhysicalDeviceSurfaceSupportKHR failed: "); @@ -628,6 +628,6 @@ void VKSwapChain::DestroySurface() if (m_surface == VK_NULL_HANDLE) return; - DestroyVulkanSurface(g_vulkan_context->GetVulkanInstance(), &m_window_info, m_surface); + DestroyVulkanSurface(GSDeviceVK::GetInstance()->GetVulkanInstance(), &m_window_info, m_surface); m_surface = VK_NULL_HANDLE; } diff --git a/pcsx2/GS/Renderers/Vulkan/VKUtil.cpp b/pcsx2/GS/Renderers/Vulkan/VKUtil.cpp deleted file mode 100644 index ad6e7398a6..0000000000 --- a/pcsx2/GS/Renderers/Vulkan/VKUtil.cpp +++ /dev/null @@ -1,239 +0,0 @@ -/* PCSX2 - PS2 Emulator for PCs - * Copyright (C) 2002-2023 PCSX2 Dev Team - * - * PCSX2 is free software: you can redistribute it and/or modify it under the terms - * of the GNU Lesser General Public License as published by the Free Software Found- - * ation, either version 3 of the License, or (at your option) any later version. - * - * PCSX2 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 PCSX2. - * If not, see . - */ - -#include "PrecompiledHeader.h" - -#include "GS/Renderers/Vulkan/VKUtil.h" -#include "GS/Renderers/Vulkan/VKContext.h" - -#include "common/Assertions.h" -#include "common/Console.h" -#include "common/StringUtil.h" - -#include - -void Vulkan::SafeDestroyFramebuffer(VkFramebuffer& fb) -{ - if (fb != VK_NULL_HANDLE) - { - vkDestroyFramebuffer(g_vulkan_context->GetDevice(), fb, nullptr); - fb = VK_NULL_HANDLE; - } -} - -void Vulkan::SafeDestroyShaderModule(VkShaderModule& sm) -{ - if (sm != VK_NULL_HANDLE) - { - vkDestroyShaderModule(g_vulkan_context->GetDevice(), sm, nullptr); - sm = VK_NULL_HANDLE; - } -} - -void Vulkan::SafeDestroyPipeline(VkPipeline& p) -{ - if (p != VK_NULL_HANDLE) - { - vkDestroyPipeline(g_vulkan_context->GetDevice(), p, nullptr); - p = VK_NULL_HANDLE; - } -} - -void Vulkan::SafeDestroyPipelineLayout(VkPipelineLayout& pl) -{ - if (pl != VK_NULL_HANDLE) - { - vkDestroyPipelineLayout(g_vulkan_context->GetDevice(), pl, nullptr); - pl = VK_NULL_HANDLE; - } -} - -void Vulkan::SafeDestroyDescriptorSetLayout(VkDescriptorSetLayout& dsl) -{ - if (dsl != VK_NULL_HANDLE) - { - vkDestroyDescriptorSetLayout(g_vulkan_context->GetDevice(), dsl, nullptr); - dsl = VK_NULL_HANDLE; - } -} - -void Vulkan::SafeDestroyBufferView(VkBufferView& bv) -{ - if (bv != VK_NULL_HANDLE) - { - vkDestroyBufferView(g_vulkan_context->GetDevice(), bv, nullptr); - bv = VK_NULL_HANDLE; - } -} - -void Vulkan::SafeDestroyImageView(VkImageView& iv) -{ - if (iv != VK_NULL_HANDLE) - { - vkDestroyImageView(g_vulkan_context->GetDevice(), iv, nullptr); - iv = VK_NULL_HANDLE; - } -} - -void Vulkan::SafeDestroySampler(VkSampler& samp) -{ - if (samp != VK_NULL_HANDLE) - { - vkDestroySampler(g_vulkan_context->GetDevice(), samp, nullptr); - samp = VK_NULL_HANDLE; - } -} - -void Vulkan::SafeDestroySemaphore(VkSemaphore& sem) -{ - if (sem != VK_NULL_HANDLE) - { - vkDestroySemaphore(g_vulkan_context->GetDevice(), sem, nullptr); - sem = VK_NULL_HANDLE; - } -} - -void Vulkan::SafeFreeGlobalDescriptorSet(VkDescriptorSet& ds) -{ - if (ds != VK_NULL_HANDLE) - { - g_vulkan_context->FreeGlobalDescriptorSet(ds); - ds = VK_NULL_HANDLE; - } -} - -void Vulkan::BufferMemoryBarrier(VkCommandBuffer command_buffer, VkBuffer buffer, VkAccessFlags src_access_mask, - VkAccessFlags dst_access_mask, VkDeviceSize offset, VkDeviceSize size, VkPipelineStageFlags src_stage_mask, - VkPipelineStageFlags dst_stage_mask) -{ - VkBufferMemoryBarrier buffer_info = { - VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER, // VkStructureType sType - nullptr, // const void* pNext - src_access_mask, // VkAccessFlags srcAccessMask - dst_access_mask, // VkAccessFlags dstAccessMask - VK_QUEUE_FAMILY_IGNORED, // uint32_t srcQueueFamilyIndex - VK_QUEUE_FAMILY_IGNORED, // uint32_t dstQueueFamilyIndex - buffer, // VkBuffer buffer - offset, // VkDeviceSize offset - size // VkDeviceSize size - }; - - vkCmdPipelineBarrier(command_buffer, src_stage_mask, dst_stage_mask, 0, 0, nullptr, 1, &buffer_info, 0, nullptr); -} - -void Vulkan::AddPointerToChain(void* head, const void* ptr) -{ - VkBaseInStructure* last_st = static_cast(head); - while (last_st->pNext) - { - if (last_st->pNext == ptr) - return; - - last_st = const_cast(last_st->pNext); - } - - last_st->pNext = static_cast(ptr); -} - -const char* Vulkan::VkResultToString(VkResult res) -{ - switch (res) - { - case VK_SUCCESS: - return "VK_SUCCESS"; - - case VK_NOT_READY: - return "VK_NOT_READY"; - - case VK_TIMEOUT: - return "VK_TIMEOUT"; - - case VK_EVENT_SET: - return "VK_EVENT_SET"; - - case VK_EVENT_RESET: - return "VK_EVENT_RESET"; - - case VK_INCOMPLETE: - return "VK_INCOMPLETE"; - - case VK_ERROR_OUT_OF_HOST_MEMORY: - return "VK_ERROR_OUT_OF_HOST_MEMORY"; - - case VK_ERROR_OUT_OF_DEVICE_MEMORY: - return "VK_ERROR_OUT_OF_DEVICE_MEMORY"; - - case VK_ERROR_INITIALIZATION_FAILED: - return "VK_ERROR_INITIALIZATION_FAILED"; - - case VK_ERROR_DEVICE_LOST: - return "VK_ERROR_DEVICE_LOST"; - - case VK_ERROR_MEMORY_MAP_FAILED: - return "VK_ERROR_MEMORY_MAP_FAILED"; - - case VK_ERROR_LAYER_NOT_PRESENT: - return "VK_ERROR_LAYER_NOT_PRESENT"; - - case VK_ERROR_EXTENSION_NOT_PRESENT: - return "VK_ERROR_EXTENSION_NOT_PRESENT"; - - case VK_ERROR_FEATURE_NOT_PRESENT: - return "VK_ERROR_FEATURE_NOT_PRESENT"; - - case VK_ERROR_INCOMPATIBLE_DRIVER: - return "VK_ERROR_INCOMPATIBLE_DRIVER"; - - case VK_ERROR_TOO_MANY_OBJECTS: - return "VK_ERROR_TOO_MANY_OBJECTS"; - - case VK_ERROR_FORMAT_NOT_SUPPORTED: - return "VK_ERROR_FORMAT_NOT_SUPPORTED"; - - case VK_ERROR_SURFACE_LOST_KHR: - return "VK_ERROR_SURFACE_LOST_KHR"; - - case VK_ERROR_NATIVE_WINDOW_IN_USE_KHR: - return "VK_ERROR_NATIVE_WINDOW_IN_USE_KHR"; - - case VK_SUBOPTIMAL_KHR: - return "VK_SUBOPTIMAL_KHR"; - - case VK_ERROR_OUT_OF_DATE_KHR: - return "VK_ERROR_OUT_OF_DATE_KHR"; - - case VK_ERROR_INCOMPATIBLE_DISPLAY_KHR: - return "VK_ERROR_INCOMPATIBLE_DISPLAY_KHR"; - - case VK_ERROR_VALIDATION_FAILED_EXT: - return "VK_ERROR_VALIDATION_FAILED_EXT"; - - case VK_ERROR_INVALID_SHADER_NV: - return "VK_ERROR_INVALID_SHADER_NV"; - - default: - return "UNKNOWN_VK_RESULT"; - } -} - -void Vulkan::LogVulkanResult(const char* func_name, VkResult res, const char* msg, ...) -{ - std::va_list ap; - va_start(ap, msg); - std::string real_msg = StringUtil::StdStringFromFormatV(msg, ap); - va_end(ap); - - Console.Error("(%s) %s (%d: %s)", func_name, real_msg.c_str(), static_cast(res), VkResultToString(res)); -} diff --git a/pcsx2/GS/Renderers/Vulkan/VKUtil.h b/pcsx2/GS/Renderers/Vulkan/VKUtil.h deleted file mode 100644 index 998531fb88..0000000000 --- a/pcsx2/GS/Renderers/Vulkan/VKUtil.h +++ /dev/null @@ -1,131 +0,0 @@ -/* PCSX2 - PS2 Emulator for PCs - * Copyright (C) 2002-2023 PCSX2 Dev Team - * - * PCSX2 is free software: you can redistribute it and/or modify it under the terms - * of the GNU Lesser General Public License as published by the Free Software Found- - * ation, either version 3 of the License, or (at your option) any later version. - * - * PCSX2 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 PCSX2. - * If not, see . - */ - -#pragma once - -#include "GS/Renderers/Vulkan/VKLoader.h" - -#include "common/StringUtil.h" - -#include -#include -#include -#include - -namespace Vulkan -{ - // Safe destroy helpers - void SafeDestroyFramebuffer(VkFramebuffer& fb); - void SafeDestroyShaderModule(VkShaderModule& sm); - void SafeDestroyPipeline(VkPipeline& p); - void SafeDestroyPipelineLayout(VkPipelineLayout& pl); - void SafeDestroyDescriptorSetLayout(VkDescriptorSetLayout& dsl); - void SafeDestroyBufferView(VkBufferView& bv); - void SafeDestroyImageView(VkImageView& iv); - void SafeDestroySampler(VkSampler& samp); - void SafeDestroySemaphore(VkSemaphore& sem); - void SafeFreeGlobalDescriptorSet(VkDescriptorSet& ds); - - // Wrapper for creating an barrier on a buffer - void BufferMemoryBarrier(VkCommandBuffer command_buffer, VkBuffer buffer, VkAccessFlags src_access_mask, - VkAccessFlags dst_access_mask, VkDeviceSize offset, VkDeviceSize size, VkPipelineStageFlags src_stage_mask, - VkPipelineStageFlags dst_stage_mask); - - // Adds a structure to a chain. - void AddPointerToChain(void* head, const void* ptr); - - const char* VkResultToString(VkResult res); - void LogVulkanResult(const char* func_name, VkResult res, const char* msg, ...) /*printflike(4, 5)*/; - -#define LOG_VULKAN_ERROR(res, ...) ::Vulkan::LogVulkanResult(__func__, res, __VA_ARGS__) - -#if defined(_DEBUG) - -// We can't use the templates below because they're all the same type on 32-bit. -#if defined(__LP64__) || defined(_WIN64) || (defined(__x86_64__) && !defined(__ILP32__)) || defined(_M_X64) || \ - defined(__ia64) || defined(_M_IA64) || defined(__aarch64__) || defined(__powerpc64__) -#define ENABLE_VULKAN_DEBUG_OBJECTS 1 -#endif - -#endif - -#ifdef ENABLE_VULKAN_DEBUG_OBJECTS - - // Provides a compile-time mapping between a Vulkan-type into its matching VkObjectType - template - struct VkObjectTypeMap; - - // clang-format off -template<> struct VkObjectTypeMap { using type = VkInstance ; static constexpr VkObjectType value = VK_OBJECT_TYPE_INSTANCE; }; -template<> struct VkObjectTypeMap { using type = VkPhysicalDevice ; static constexpr VkObjectType value = VK_OBJECT_TYPE_PHYSICAL_DEVICE; }; -template<> struct VkObjectTypeMap { using type = VkDevice ; static constexpr VkObjectType value = VK_OBJECT_TYPE_DEVICE; }; -template<> struct VkObjectTypeMap { using type = VkQueue ; static constexpr VkObjectType value = VK_OBJECT_TYPE_QUEUE; }; -template<> struct VkObjectTypeMap { using type = VkSemaphore ; static constexpr VkObjectType value = VK_OBJECT_TYPE_SEMAPHORE; }; -template<> struct VkObjectTypeMap { using type = VkCommandBuffer ; static constexpr VkObjectType value = VK_OBJECT_TYPE_COMMAND_BUFFER; }; -template<> struct VkObjectTypeMap { using type = VkFence ; static constexpr VkObjectType value = VK_OBJECT_TYPE_FENCE; }; -template<> struct VkObjectTypeMap { using type = VkDeviceMemory ; static constexpr VkObjectType value = VK_OBJECT_TYPE_DEVICE_MEMORY; }; -template<> struct VkObjectTypeMap { using type = VkBuffer ; static constexpr VkObjectType value = VK_OBJECT_TYPE_BUFFER; }; -template<> struct VkObjectTypeMap { using type = VkImage ; static constexpr VkObjectType value = VK_OBJECT_TYPE_IMAGE; }; -template<> struct VkObjectTypeMap { using type = VkEvent ; static constexpr VkObjectType value = VK_OBJECT_TYPE_EVENT; }; -template<> struct VkObjectTypeMap { using type = VkQueryPool ; static constexpr VkObjectType value = VK_OBJECT_TYPE_QUERY_POOL; }; -template<> struct VkObjectTypeMap { using type = VkBufferView ; static constexpr VkObjectType value = VK_OBJECT_TYPE_BUFFER_VIEW; }; -template<> struct VkObjectTypeMap { using type = VkImageView ; static constexpr VkObjectType value = VK_OBJECT_TYPE_IMAGE_VIEW; }; -template<> struct VkObjectTypeMap { using type = VkShaderModule ; static constexpr VkObjectType value = VK_OBJECT_TYPE_SHADER_MODULE; }; -template<> struct VkObjectTypeMap { using type = VkPipelineCache ; static constexpr VkObjectType value = VK_OBJECT_TYPE_PIPELINE_CACHE; }; -template<> struct VkObjectTypeMap { using type = VkPipelineLayout ; static constexpr VkObjectType value = VK_OBJECT_TYPE_PIPELINE_LAYOUT; }; -template<> struct VkObjectTypeMap { using type = VkRenderPass ; static constexpr VkObjectType value = VK_OBJECT_TYPE_RENDER_PASS; }; -template<> struct VkObjectTypeMap { using type = VkPipeline ; static constexpr VkObjectType value = VK_OBJECT_TYPE_PIPELINE; }; -template<> struct VkObjectTypeMap { using type = VkDescriptorSetLayout ; static constexpr VkObjectType value = VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT; }; -template<> struct VkObjectTypeMap { using type = VkSampler ; static constexpr VkObjectType value = VK_OBJECT_TYPE_SAMPLER; }; -template<> struct VkObjectTypeMap { using type = VkDescriptorPool ; static constexpr VkObjectType value = VK_OBJECT_TYPE_DESCRIPTOR_POOL; }; -template<> struct VkObjectTypeMap { using type = VkDescriptorSet ; static constexpr VkObjectType value = VK_OBJECT_TYPE_DESCRIPTOR_SET; }; -template<> struct VkObjectTypeMap { using type = VkFramebuffer ; static constexpr VkObjectType value = VK_OBJECT_TYPE_FRAMEBUFFER; }; -template<> struct VkObjectTypeMap { using type = VkCommandPool ; static constexpr VkObjectType value = VK_OBJECT_TYPE_COMMAND_POOL; }; -template<> struct VkObjectTypeMap { using type = VkDescriptorUpdateTemplate; static constexpr VkObjectType value = VK_OBJECT_TYPE_DESCRIPTOR_UPDATE_TEMPLATE; }; -template<> struct VkObjectTypeMap { using type = VkSurfaceKHR ; static constexpr VkObjectType value = VK_OBJECT_TYPE_SURFACE_KHR; }; -template<> struct VkObjectTypeMap { using type = VkSwapchainKHR ; static constexpr VkObjectType value = VK_OBJECT_TYPE_SWAPCHAIN_KHR; }; -template<> struct VkObjectTypeMap { using type = VkDebugUtilsMessengerEXT ; static constexpr VkObjectType value = VK_OBJECT_TYPE_DEBUG_UTILS_MESSENGER_EXT; }; - // clang-format on - -#endif - - static inline void SetObjectName( - VkDevice device, void* object_handle, VkObjectType object_type, const char* format, va_list ap) - { -#ifdef ENABLE_VULKAN_DEBUG_OBJECTS - if (!vkSetDebugUtilsObjectNameEXT) - { - return; - } - - const std::string str(StringUtil::StdStringFromFormatV(format, ap)); - const VkDebugUtilsObjectNameInfoEXT nameInfo{VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT, nullptr, - object_type, reinterpret_cast(object_handle), str.c_str()}; - vkSetDebugUtilsObjectNameEXT(device, &nameInfo); -#endif - } - - template - static inline void SetObjectName(VkDevice device, T object_handle, const char* format, ...) - { -#ifdef ENABLE_VULKAN_DEBUG_OBJECTS - std::va_list ap; - va_start(ap, format); - SetObjectName(device, reinterpret_cast((typename VkObjectTypeMap::type)object_handle), - VkObjectTypeMap::value, format, ap); - va_end(ap); -#endif - } -} // namespace Vulkan diff --git a/pcsx2/pcsx2.vcxproj b/pcsx2/pcsx2.vcxproj index 9e0eb0c1ee..416f429941 100644 --- a/pcsx2/pcsx2.vcxproj +++ b/pcsx2/pcsx2.vcxproj @@ -192,12 +192,10 @@ - - @@ -546,13 +544,11 @@ - - diff --git a/pcsx2/pcsx2.vcxproj.filters b/pcsx2/pcsx2.vcxproj.filters index c4628011e1..fb0dc4a58d 100644 --- a/pcsx2/pcsx2.vcxproj.filters +++ b/pcsx2/pcsx2.vcxproj.filters @@ -1310,9 +1310,6 @@ System\Ps2\GS\Renderers\Vulkan - - System\Ps2\GS\Renderers\Vulkan - System\Ps2\GS\Renderers\Vulkan @@ -1325,9 +1322,6 @@ System\Ps2\GS\Renderers\Vulkan - - System\Ps2\GS\Renderers\Vulkan - System\Ps2\EmotionEngine\DMAC @@ -2256,9 +2250,6 @@ System\Ps2\GS\Renderers\Vulkan - - System\Ps2\GS\Renderers\Vulkan - System\Ps2\GS\Renderers\Vulkan @@ -2274,9 +2265,6 @@ System\Ps2\GS\Renderers\Vulkan - - System\Ps2\GS\Renderers\Vulkan - System\Ps2\Iop