| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#pragma once |
| 5 |
|
| 6 |
#include <memory> |
| 7 |
#include <optional> |
| 8 |
#include <string> |
| 9 |
#include <vector> |
| 10 |
|
| 11 |
#include "Common/CommonTypes.h" |
| 12 |
#include "Common/WindowSystemInfo.h" |
| 13 |
#include "VideoBackends/Vulkan/Constants.h" |
| 14 |
#include "VideoCommon/VideoConfig.h" |
| 15 |
|
| 16 |
namespace Vulkan |
| 17 |
{ |
| 18 |
class VulkanContext |
| 19 |
{ |
| 20 |
public: |
| 21 |
struct PhysicalDeviceInfo |
| 22 |
{ |
| 23 |
PhysicalDeviceInfo(const PhysicalDeviceInfo&) = default; |
| 24 |
explicit PhysicalDeviceInfo(VkPhysicalDevice device); |
| 25 |
VkPhysicalDeviceFeatures features() const; |
| 26 |
|
| 27 |
char deviceName[VK_MAX_PHYSICAL_DEVICE_NAME_SIZE]; |
| 28 |
u8 pipelineCacheUUID[VK_UUID_SIZE]; |
| 29 |
u32 apiVersion; |
| 30 |
u32 driverVersion; |
| 31 |
u32 vendorID; |
| 32 |
u32 deviceID; |
| 33 |
VkDeviceSize minUniformBufferOffsetAlignment; |
| 34 |
VkDeviceSize bufferImageGranularity; |
| 35 |
u32 maxTexelBufferElements; |
| 36 |
u32 maxImageDimension2D; |
| 37 |
VkSampleCountFlags framebufferColorSampleCounts; |
| 38 |
VkSampleCountFlags framebufferDepthSampleCounts; |
| 39 |
float pointSizeRange[2]; |
| 40 |
float maxSamplerAnisotropy; |
| 41 |
u32 subgroupSize = 1; |
| 42 |
VkDriverId driverID = static_cast<VkDriverId>(0); |
| 43 |
bool dualSrcBlend; |
| 44 |
bool geometryShader; |
| 45 |
bool samplerAnisotropy; |
| 46 |
bool logicOp; |
| 47 |
bool fragmentStoresAndAtomics; |
| 48 |
bool sampleRateShading; |
| 49 |
bool largePoints; |
| 50 |
bool shaderStorageImageMultisample; |
| 51 |
bool shaderTessellationAndGeometryPointSize; |
| 52 |
bool occlusionQueryPrecise; |
| 53 |
bool shaderClipDistance; |
| 54 |
bool depthClamp; |
| 55 |
bool textureCompressionBC; |
| 56 |
bool shaderSubgroupOperations = false; |
| 57 |
}; |
| 58 |
|
| 59 |
VulkanContext(VkInstance instance, VkPhysicalDevice physical_device); |
| 60 |
~VulkanContext(); |
| 61 |
|
| 62 |
|
| 63 |
static bool CheckValidationLayerAvailablility(); |
| 64 |
|
| 65 |
|
| 66 |
static VkInstance CreateVulkanInstance(WindowSystemType wstype, bool enable_debug_utils, |
| 67 |
bool enable_validation_layer, u32* out_vk_api_version); |
| 68 |
|
| 69 |
|
| 70 |
using GPUList = std::vector<VkPhysicalDevice>; |
| 71 |
static GPUList EnumerateGPUs(VkInstance instance); |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
static void PopulateBackendInfo(BackendInfo* backend_info); |
| 76 |
static void PopulateBackendInfoAdapters(BackendInfo* backend_info, const GPUList& gpu_list); |
| 77 |
static void PopulateBackendInfoFeatures(BackendInfo* backend_info, VkPhysicalDevice gpu, |
| 78 |
const PhysicalDeviceInfo& info); |
| 79 |
static void PopulateBackendInfoMultisampleModes(BackendInfo* backend_info, VkPhysicalDevice gpu, |
| 80 |
const PhysicalDeviceInfo& info); |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
static std::unique_ptr<VulkanContext> Create(VkInstance instance, VkPhysicalDevice gpu, |
| 86 |
VkSurfaceKHR surface, bool enable_debug_utils, |
| 87 |
bool enable_validation_layer, u32 api_version); |
| 88 |
|
| 89 |
|
| 90 |
bool EnableDebugUtils(); |
| 91 |
void DisableDebugUtils(); |
| 92 |
|
| 93 |
|
| 94 |
VkInstance GetVulkanInstance() const { return m_instance; } |
| 95 |
VkPhysicalDevice GetPhysicalDevice() const { return m_physical_device; } |
| 96 |
VkDevice GetDevice() const { return m_device; } |
| 97 |
VkQueue GetGraphicsQueue() const { return m_graphics_queue; } |
| 98 |
u32 GetGraphicsQueueFamilyIndex() const { return m_graphics_queue_family_index; } |
| 99 |
VkQueue GetPresentQueue() const { return m_present_queue; } |
| 100 |
u32 GetPresentQueueFamilyIndex() const { return m_present_queue_family_index; } |
| 101 |
const VkQueueFamilyProperties& GetGraphicsQueueProperties() const |
| 102 |
{ |
| 103 |
return m_graphics_queue_properties; |
| 104 |
} |
| 105 |
const PhysicalDeviceInfo& GetDeviceInfo() const { return m_device_info; } |
| 106 |
|
| 107 |
bool SupportsAnisotropicFiltering() const { return m_device_info.samplerAnisotropy; } |
| 108 |
bool SupportsPreciseOcclusionQueries() const { return m_device_info.occlusionQueryPrecise; } |
| 109 |
u32 GetShaderSubgroupSize() const { return m_device_info.subgroupSize; } |
| 110 |
bool SupportsShaderSubgroupOperations() const { return m_device_info.shaderSubgroupOperations; } |
| 111 |
|
| 112 |
|
| 113 |
VkDeviceSize GetUniformBufferAlignment() const |
| 114 |
{ |
| 115 |
return m_device_info.minUniformBufferOffsetAlignment; |
| 116 |
} |
| 117 |
VkDeviceSize GetBufferImageGranularity() const { return m_device_info.bufferImageGranularity; } |
| 118 |
float GetMaxSamplerAnisotropy() const { return m_device_info.maxSamplerAnisotropy; } |
| 119 |
|
| 120 |
|
| 121 |
bool SupportsDeviceExtension(const char* name) const; |
| 122 |
|
| 123 |
|
| 124 |
bool SupportsExclusiveFullscreen(const WindowSystemInfo& wsi, VkSurfaceKHR surface); |
| 125 |
|
| 126 |
VmaAllocator GetMemoryAllocator() const { return m_allocator; } |
| 127 |
|
| 128 |
#ifdef WIN32 |
| 129 |
|
| 130 |
VkSurfaceFullScreenExclusiveWin32InfoEXT |
| 131 |
GetPlatformExclusiveFullscreenInfo(const WindowSystemInfo& wsi); |
| 132 |
#endif |
| 133 |
|
| 134 |
private: |
| 135 |
static bool SelectInstanceExtensions(std::vector<const char*>* extension_list, |
| 136 |
WindowSystemType wstype, bool enable_debug_utils, |
| 137 |
bool validation_layer_enabled); |
| 138 |
bool SelectDeviceExtensions(bool enable_surface); |
| 139 |
void WarnMissingDeviceFeatures(); |
| 140 |
bool CreateDevice(VkSurfaceKHR surface, bool enable_validation_layer); |
| 141 |
void InitDriverDetails(); |
| 142 |
bool CreateAllocator(u32 vk_api_version); |
| 143 |
|
| 144 |
VkInstance m_instance = VK_NULL_HANDLE; |
| 145 |
VkPhysicalDevice m_physical_device = VK_NULL_HANDLE; |
| 146 |
VkDevice m_device = VK_NULL_HANDLE; |
| 147 |
VmaAllocator m_allocator = VK_NULL_HANDLE; |
| 148 |
|
| 149 |
VkQueue m_graphics_queue = VK_NULL_HANDLE; |
| 150 |
u32 m_graphics_queue_family_index = 0; |
| 151 |
VkQueue m_present_queue = VK_NULL_HANDLE; |
| 152 |
u32 m_present_queue_family_index = 0; |
| 153 |
VkQueueFamilyProperties m_graphics_queue_properties = {}; |
| 154 |
|
| 155 |
VkDebugUtilsMessengerEXT m_debug_utils_messenger = VK_NULL_HANDLE; |
| 156 |
|
| 157 |
PhysicalDeviceInfo m_device_info; |
| 158 |
|
| 159 |
std::vector<std::string> m_device_extensions; |
| 160 |
}; |
| 161 |
|
| 162 |
extern std::unique_ptr<VulkanContext> g_vulkan_context; |
| 163 |
|
| 164 |
} |