vulkan_device.cpp 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247
  1. // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <algorithm>
  4. #include <bitset>
  5. #include <chrono>
  6. #include <optional>
  7. #include <thread>
  8. #include <unordered_set>
  9. #include <utility>
  10. #include <vector>
  11. #include "common/assert.h"
  12. #include "common/literals.h"
  13. #include "common/polyfill_ranges.h"
  14. #include "common/settings.h"
  15. #include "video_core/vulkan_common/nsight_aftermath_tracker.h"
  16. #include "video_core/vulkan_common/vulkan_device.h"
  17. #include "video_core/vulkan_common/vulkan_wrapper.h"
  18. #if defined(ANDROID) && defined(ARCHITECTURE_arm64)
  19. #include <adrenotools/bcenabler.h>
  20. #endif
  21. #include <vk_mem_alloc.h>
  22. namespace Vulkan {
  23. using namespace Common::Literals;
  24. namespace {
  25. namespace Alternatives {
  26. constexpr std::array STENCIL8_UINT{
  27. VK_FORMAT_D16_UNORM_S8_UINT,
  28. VK_FORMAT_D24_UNORM_S8_UINT,
  29. VK_FORMAT_D32_SFLOAT_S8_UINT,
  30. VK_FORMAT_UNDEFINED,
  31. };
  32. constexpr std::array DEPTH24_UNORM_STENCIL8_UINT{
  33. VK_FORMAT_D32_SFLOAT_S8_UINT,
  34. VK_FORMAT_D16_UNORM_S8_UINT,
  35. VK_FORMAT_UNDEFINED,
  36. };
  37. constexpr std::array DEPTH16_UNORM_STENCIL8_UINT{
  38. VK_FORMAT_D24_UNORM_S8_UINT,
  39. VK_FORMAT_D32_SFLOAT_S8_UINT,
  40. VK_FORMAT_UNDEFINED,
  41. };
  42. constexpr std::array B5G6R5_UNORM_PACK16{
  43. VK_FORMAT_R5G6B5_UNORM_PACK16,
  44. VK_FORMAT_UNDEFINED,
  45. };
  46. constexpr std::array R4G4_UNORM_PACK8{
  47. VK_FORMAT_R8_UNORM,
  48. VK_FORMAT_UNDEFINED,
  49. };
  50. constexpr std::array R16G16B16_SFLOAT{
  51. VK_FORMAT_R16G16B16A16_SFLOAT,
  52. VK_FORMAT_UNDEFINED,
  53. };
  54. constexpr std::array R16G16B16_SSCALED{
  55. VK_FORMAT_R16G16B16A16_SSCALED,
  56. VK_FORMAT_UNDEFINED,
  57. };
  58. constexpr std::array R8G8B8_SSCALED{
  59. VK_FORMAT_R8G8B8A8_SSCALED,
  60. VK_FORMAT_UNDEFINED,
  61. };
  62. } // namespace Alternatives
  63. enum class NvidiaArchitecture {
  64. AmpereOrNewer,
  65. Turing,
  66. VoltaOrOlder,
  67. };
  68. template <typename T>
  69. void SetNext(void**& next, T& data) {
  70. *next = &data;
  71. next = &data.pNext;
  72. }
  73. constexpr const VkFormat* GetFormatAlternatives(VkFormat format) {
  74. switch (format) {
  75. case VK_FORMAT_S8_UINT:
  76. return Alternatives::STENCIL8_UINT.data();
  77. case VK_FORMAT_D24_UNORM_S8_UINT:
  78. return Alternatives::DEPTH24_UNORM_STENCIL8_UINT.data();
  79. case VK_FORMAT_D16_UNORM_S8_UINT:
  80. return Alternatives::DEPTH16_UNORM_STENCIL8_UINT.data();
  81. case VK_FORMAT_B5G6R5_UNORM_PACK16:
  82. return Alternatives::B5G6R5_UNORM_PACK16.data();
  83. case VK_FORMAT_R4G4_UNORM_PACK8:
  84. return Alternatives::R4G4_UNORM_PACK8.data();
  85. case VK_FORMAT_R16G16B16_SFLOAT:
  86. return Alternatives::R16G16B16_SFLOAT.data();
  87. case VK_FORMAT_R16G16B16_SSCALED:
  88. return Alternatives::R16G16B16_SSCALED.data();
  89. case VK_FORMAT_R8G8B8_SSCALED:
  90. return Alternatives::R8G8B8_SSCALED.data();
  91. default:
  92. return nullptr;
  93. }
  94. }
  95. VkFormatFeatureFlags GetFormatFeatures(VkFormatProperties properties, FormatType format_type) {
  96. switch (format_type) {
  97. case FormatType::Linear:
  98. return properties.linearTilingFeatures;
  99. case FormatType::Optimal:
  100. return properties.optimalTilingFeatures;
  101. case FormatType::Buffer:
  102. return properties.bufferFeatures;
  103. default:
  104. return {};
  105. }
  106. }
  107. std::unordered_map<VkFormat, VkFormatProperties> GetFormatProperties(vk::PhysicalDevice physical) {
  108. static constexpr std::array formats{
  109. VK_FORMAT_A1R5G5B5_UNORM_PACK16,
  110. VK_FORMAT_A2B10G10R10_SINT_PACK32,
  111. VK_FORMAT_A2B10G10R10_SNORM_PACK32,
  112. VK_FORMAT_A2B10G10R10_SSCALED_PACK32,
  113. VK_FORMAT_A2B10G10R10_UINT_PACK32,
  114. VK_FORMAT_A2B10G10R10_UNORM_PACK32,
  115. VK_FORMAT_A2B10G10R10_USCALED_PACK32,
  116. VK_FORMAT_A8B8G8R8_SINT_PACK32,
  117. VK_FORMAT_A8B8G8R8_SNORM_PACK32,
  118. VK_FORMAT_A8B8G8R8_SRGB_PACK32,
  119. VK_FORMAT_A8B8G8R8_UINT_PACK32,
  120. VK_FORMAT_A8B8G8R8_UNORM_PACK32,
  121. VK_FORMAT_ASTC_10x10_SRGB_BLOCK,
  122. VK_FORMAT_ASTC_10x10_UNORM_BLOCK,
  123. VK_FORMAT_ASTC_10x5_SRGB_BLOCK,
  124. VK_FORMAT_ASTC_10x5_UNORM_BLOCK,
  125. VK_FORMAT_ASTC_10x6_SRGB_BLOCK,
  126. VK_FORMAT_ASTC_10x6_UNORM_BLOCK,
  127. VK_FORMAT_ASTC_10x8_SRGB_BLOCK,
  128. VK_FORMAT_ASTC_10x8_UNORM_BLOCK,
  129. VK_FORMAT_ASTC_12x10_SRGB_BLOCK,
  130. VK_FORMAT_ASTC_12x10_UNORM_BLOCK,
  131. VK_FORMAT_ASTC_12x12_SRGB_BLOCK,
  132. VK_FORMAT_ASTC_12x12_UNORM_BLOCK,
  133. VK_FORMAT_ASTC_4x4_SRGB_BLOCK,
  134. VK_FORMAT_ASTC_4x4_UNORM_BLOCK,
  135. VK_FORMAT_ASTC_5x4_SRGB_BLOCK,
  136. VK_FORMAT_ASTC_5x4_UNORM_BLOCK,
  137. VK_FORMAT_ASTC_5x5_SRGB_BLOCK,
  138. VK_FORMAT_ASTC_5x5_UNORM_BLOCK,
  139. VK_FORMAT_ASTC_6x5_SRGB_BLOCK,
  140. VK_FORMAT_ASTC_6x5_UNORM_BLOCK,
  141. VK_FORMAT_ASTC_6x6_SRGB_BLOCK,
  142. VK_FORMAT_ASTC_6x6_UNORM_BLOCK,
  143. VK_FORMAT_ASTC_8x5_SRGB_BLOCK,
  144. VK_FORMAT_ASTC_8x5_UNORM_BLOCK,
  145. VK_FORMAT_ASTC_8x6_SRGB_BLOCK,
  146. VK_FORMAT_ASTC_8x6_UNORM_BLOCK,
  147. VK_FORMAT_ASTC_8x8_SRGB_BLOCK,
  148. VK_FORMAT_ASTC_8x8_UNORM_BLOCK,
  149. VK_FORMAT_B10G11R11_UFLOAT_PACK32,
  150. VK_FORMAT_B4G4R4A4_UNORM_PACK16,
  151. VK_FORMAT_B5G5R5A1_UNORM_PACK16,
  152. VK_FORMAT_B5G6R5_UNORM_PACK16,
  153. VK_FORMAT_B8G8R8A8_SRGB,
  154. VK_FORMAT_B8G8R8A8_UNORM,
  155. VK_FORMAT_BC1_RGBA_SRGB_BLOCK,
  156. VK_FORMAT_BC1_RGBA_UNORM_BLOCK,
  157. VK_FORMAT_BC2_SRGB_BLOCK,
  158. VK_FORMAT_BC2_UNORM_BLOCK,
  159. VK_FORMAT_BC3_SRGB_BLOCK,
  160. VK_FORMAT_BC3_UNORM_BLOCK,
  161. VK_FORMAT_BC4_SNORM_BLOCK,
  162. VK_FORMAT_BC4_UNORM_BLOCK,
  163. VK_FORMAT_BC5_SNORM_BLOCK,
  164. VK_FORMAT_BC5_UNORM_BLOCK,
  165. VK_FORMAT_BC6H_SFLOAT_BLOCK,
  166. VK_FORMAT_BC6H_UFLOAT_BLOCK,
  167. VK_FORMAT_BC7_SRGB_BLOCK,
  168. VK_FORMAT_BC7_UNORM_BLOCK,
  169. VK_FORMAT_D16_UNORM,
  170. VK_FORMAT_D16_UNORM_S8_UINT,
  171. VK_FORMAT_D24_UNORM_S8_UINT,
  172. VK_FORMAT_D32_SFLOAT,
  173. VK_FORMAT_D32_SFLOAT_S8_UINT,
  174. VK_FORMAT_E5B9G9R9_UFLOAT_PACK32,
  175. VK_FORMAT_R16G16B16A16_SFLOAT,
  176. VK_FORMAT_R16G16B16A16_SINT,
  177. VK_FORMAT_R16G16B16A16_SNORM,
  178. VK_FORMAT_R16G16B16A16_SSCALED,
  179. VK_FORMAT_R16G16B16A16_UINT,
  180. VK_FORMAT_R16G16B16A16_UNORM,
  181. VK_FORMAT_R16G16B16A16_USCALED,
  182. VK_FORMAT_R16G16B16_SFLOAT,
  183. VK_FORMAT_R16G16B16_SINT,
  184. VK_FORMAT_R16G16B16_SNORM,
  185. VK_FORMAT_R16G16B16_SSCALED,
  186. VK_FORMAT_R16G16B16_UINT,
  187. VK_FORMAT_R16G16B16_UNORM,
  188. VK_FORMAT_R16G16B16_USCALED,
  189. VK_FORMAT_R16G16_SFLOAT,
  190. VK_FORMAT_R16G16_SINT,
  191. VK_FORMAT_R16G16_SNORM,
  192. VK_FORMAT_R16G16_SSCALED,
  193. VK_FORMAT_R16G16_UINT,
  194. VK_FORMAT_R16G16_UNORM,
  195. VK_FORMAT_R16G16_USCALED,
  196. VK_FORMAT_R16_SFLOAT,
  197. VK_FORMAT_R16_SINT,
  198. VK_FORMAT_R16_SNORM,
  199. VK_FORMAT_R16_SSCALED,
  200. VK_FORMAT_R16_UINT,
  201. VK_FORMAT_R16_UNORM,
  202. VK_FORMAT_R16_USCALED,
  203. VK_FORMAT_R32G32B32A32_SFLOAT,
  204. VK_FORMAT_R32G32B32A32_SINT,
  205. VK_FORMAT_R32G32B32A32_UINT,
  206. VK_FORMAT_R32G32B32_SFLOAT,
  207. VK_FORMAT_R32G32B32_SINT,
  208. VK_FORMAT_R32G32B32_UINT,
  209. VK_FORMAT_R32G32_SFLOAT,
  210. VK_FORMAT_R32G32_SINT,
  211. VK_FORMAT_R32G32_UINT,
  212. VK_FORMAT_R32_SFLOAT,
  213. VK_FORMAT_R32_SINT,
  214. VK_FORMAT_R32_UINT,
  215. VK_FORMAT_R4G4B4A4_UNORM_PACK16,
  216. VK_FORMAT_R4G4_UNORM_PACK8,
  217. VK_FORMAT_R5G5B5A1_UNORM_PACK16,
  218. VK_FORMAT_R5G6B5_UNORM_PACK16,
  219. VK_FORMAT_R8G8B8A8_SINT,
  220. VK_FORMAT_R8G8B8A8_SNORM,
  221. VK_FORMAT_R8G8B8A8_SRGB,
  222. VK_FORMAT_R8G8B8A8_SSCALED,
  223. VK_FORMAT_R8G8B8A8_UINT,
  224. VK_FORMAT_R8G8B8A8_UNORM,
  225. VK_FORMAT_R8G8B8A8_USCALED,
  226. VK_FORMAT_R8G8B8_SINT,
  227. VK_FORMAT_R8G8B8_SNORM,
  228. VK_FORMAT_R8G8B8_SSCALED,
  229. VK_FORMAT_R8G8B8_UINT,
  230. VK_FORMAT_R8G8B8_UNORM,
  231. VK_FORMAT_R8G8B8_USCALED,
  232. VK_FORMAT_R8G8_SINT,
  233. VK_FORMAT_R8G8_SNORM,
  234. VK_FORMAT_R8G8_SSCALED,
  235. VK_FORMAT_R8G8_UINT,
  236. VK_FORMAT_R8G8_UNORM,
  237. VK_FORMAT_R8G8_USCALED,
  238. VK_FORMAT_R8_SINT,
  239. VK_FORMAT_R8_SNORM,
  240. VK_FORMAT_R8_SSCALED,
  241. VK_FORMAT_R8_UINT,
  242. VK_FORMAT_R8_UNORM,
  243. VK_FORMAT_R8_USCALED,
  244. VK_FORMAT_S8_UINT,
  245. };
  246. std::unordered_map<VkFormat, VkFormatProperties> format_properties;
  247. for (const auto format : formats) {
  248. format_properties.emplace(format, physical.GetFormatProperties(format));
  249. }
  250. return format_properties;
  251. }
  252. #if defined(ANDROID) && defined(ARCHITECTURE_arm64)
  253. void OverrideBcnFormats(std::unordered_map<VkFormat, VkFormatProperties>& format_properties) {
  254. // These properties are extracted from Adreno driver 512.687.0
  255. constexpr VkFormatFeatureFlags tiling_features{
  256. VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT | VK_FORMAT_FEATURE_BLIT_SRC_BIT |
  257. VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT | VK_FORMAT_FEATURE_TRANSFER_SRC_BIT |
  258. VK_FORMAT_FEATURE_TRANSFER_DST_BIT};
  259. constexpr VkFormatFeatureFlags buffer_features{VK_FORMAT_FEATURE_UNIFORM_TEXEL_BUFFER_BIT};
  260. static constexpr std::array bcn_formats{
  261. VK_FORMAT_BC1_RGBA_SRGB_BLOCK, VK_FORMAT_BC1_RGBA_UNORM_BLOCK, VK_FORMAT_BC2_SRGB_BLOCK,
  262. VK_FORMAT_BC2_UNORM_BLOCK, VK_FORMAT_BC3_SRGB_BLOCK, VK_FORMAT_BC3_UNORM_BLOCK,
  263. VK_FORMAT_BC4_SNORM_BLOCK, VK_FORMAT_BC4_UNORM_BLOCK, VK_FORMAT_BC5_SNORM_BLOCK,
  264. VK_FORMAT_BC5_UNORM_BLOCK, VK_FORMAT_BC6H_SFLOAT_BLOCK, VK_FORMAT_BC6H_UFLOAT_BLOCK,
  265. VK_FORMAT_BC7_SRGB_BLOCK, VK_FORMAT_BC7_UNORM_BLOCK,
  266. };
  267. for (const auto format : bcn_formats) {
  268. format_properties[format].linearTilingFeatures = tiling_features;
  269. format_properties[format].optimalTilingFeatures = tiling_features;
  270. format_properties[format].bufferFeatures = buffer_features;
  271. }
  272. }
  273. #endif
  274. NvidiaArchitecture GetNvidiaArchitecture(vk::PhysicalDevice physical,
  275. const std::set<std::string, std::less<>>& exts) {
  276. if (exts.contains(VK_KHR_FRAGMENT_SHADING_RATE_EXTENSION_NAME)) {
  277. VkPhysicalDeviceFragmentShadingRatePropertiesKHR shading_rate_props{};
  278. shading_rate_props.sType =
  279. VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FRAGMENT_SHADING_RATE_PROPERTIES_KHR;
  280. VkPhysicalDeviceProperties2 physical_properties{};
  281. physical_properties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
  282. physical_properties.pNext = &shading_rate_props;
  283. physical.GetProperties2(physical_properties);
  284. if (shading_rate_props.primitiveFragmentShadingRateWithMultipleViewports) {
  285. // Only Ampere and newer support this feature
  286. return NvidiaArchitecture::AmpereOrNewer;
  287. }
  288. }
  289. if (exts.contains(VK_NV_SHADING_RATE_IMAGE_EXTENSION_NAME)) {
  290. return NvidiaArchitecture::Turing;
  291. }
  292. return NvidiaArchitecture::VoltaOrOlder;
  293. }
  294. std::vector<const char*> ExtensionListForVulkan(
  295. const std::set<std::string, std::less<>>& extensions) {
  296. std::vector<const char*> output;
  297. output.reserve(extensions.size());
  298. for (const auto& extension : extensions) {
  299. output.push_back(extension.c_str());
  300. }
  301. return output;
  302. }
  303. } // Anonymous namespace
  304. Device::Device(VkInstance instance_, vk::PhysicalDevice physical_, VkSurfaceKHR surface,
  305. const vk::InstanceDispatch& dld_)
  306. : instance{instance_}, dld{dld_}, physical{physical_},
  307. format_properties(GetFormatProperties(physical)) {
  308. // Get suitability and device properties.
  309. const bool is_suitable = GetSuitability(surface != nullptr);
  310. const VkDriverId driver_id = properties.driver.driverID;
  311. const auto device_id = properties.properties.deviceID;
  312. const bool is_radv = driver_id == VK_DRIVER_ID_MESA_RADV;
  313. const bool is_amd_driver =
  314. driver_id == VK_DRIVER_ID_AMD_PROPRIETARY || driver_id == VK_DRIVER_ID_AMD_OPEN_SOURCE;
  315. const bool is_amd = is_amd_driver || is_radv;
  316. const bool is_intel_windows = driver_id == VK_DRIVER_ID_INTEL_PROPRIETARY_WINDOWS;
  317. const bool is_intel_anv = driver_id == VK_DRIVER_ID_INTEL_OPEN_SOURCE_MESA;
  318. const bool is_nvidia = driver_id == VK_DRIVER_ID_NVIDIA_PROPRIETARY;
  319. const bool is_mvk = driver_id == VK_DRIVER_ID_MOLTENVK;
  320. const bool is_qualcomm = driver_id == VK_DRIVER_ID_QUALCOMM_PROPRIETARY;
  321. const bool is_turnip = driver_id == VK_DRIVER_ID_MESA_TURNIP;
  322. const bool is_s8gen2 = device_id == 0x43050a01;
  323. const bool is_arm = driver_id == VK_DRIVER_ID_ARM_PROPRIETARY;
  324. if ((is_mvk || is_qualcomm || is_turnip || is_arm) && !is_suitable) {
  325. LOG_WARNING(Render_Vulkan, "Unsuitable driver, continuing anyway");
  326. } else if (!is_suitable) {
  327. throw vk::Exception(VK_ERROR_INCOMPATIBLE_DRIVER);
  328. }
  329. SetupFamilies(surface);
  330. const auto queue_cis = GetDeviceQueueCreateInfos();
  331. // GetSuitability has already configured the linked list of features for us.
  332. // Reuse it here.
  333. const void* first_next = &features2;
  334. VkDeviceDiagnosticsConfigCreateInfoNV diagnostics_nv{};
  335. if (Settings::values.enable_nsight_aftermath && extensions.device_diagnostics_config) {
  336. nsight_aftermath_tracker = std::make_unique<NsightAftermathTracker>();
  337. diagnostics_nv = {
  338. .sType = VK_STRUCTURE_TYPE_DEVICE_DIAGNOSTICS_CONFIG_CREATE_INFO_NV,
  339. .pNext = &features2,
  340. .flags = VK_DEVICE_DIAGNOSTICS_CONFIG_ENABLE_SHADER_DEBUG_INFO_BIT_NV |
  341. VK_DEVICE_DIAGNOSTICS_CONFIG_ENABLE_RESOURCE_TRACKING_BIT_NV |
  342. VK_DEVICE_DIAGNOSTICS_CONFIG_ENABLE_AUTOMATIC_CHECKPOINTS_BIT_NV,
  343. };
  344. first_next = &diagnostics_nv;
  345. }
  346. is_blit_depth_stencil_supported = TestDepthStencilBlits();
  347. is_optimal_astc_supported = ComputeIsOptimalAstcSupported();
  348. is_warp_potentially_bigger = !extensions.subgroup_size_control ||
  349. properties.subgroup_size_control.maxSubgroupSize > GuestWarpSize;
  350. is_integrated = properties.properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU;
  351. is_virtual = properties.properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU;
  352. is_non_gpu = properties.properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_OTHER ||
  353. properties.properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU;
  354. supports_d24_depth =
  355. IsFormatSupported(VK_FORMAT_D24_UNORM_S8_UINT,
  356. VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT, FormatType::Optimal);
  357. supports_conditional_barriers = !(is_intel_anv || is_intel_windows);
  358. CollectPhysicalMemoryInfo();
  359. CollectToolingInfo();
  360. if (is_qualcomm || is_turnip) {
  361. LOG_WARNING(Render_Vulkan,
  362. "Qualcomm and Turnip drivers have broken VK_EXT_custom_border_color");
  363. extensions.custom_border_color = false;
  364. loaded_extensions.erase(VK_EXT_CUSTOM_BORDER_COLOR_EXTENSION_NAME);
  365. }
  366. if (is_qualcomm) {
  367. must_emulate_scaled_formats = true;
  368. LOG_WARNING(Render_Vulkan, "Qualcomm drivers have broken VK_EXT_extended_dynamic_state");
  369. extensions.extended_dynamic_state = false;
  370. loaded_extensions.erase(VK_EXT_EXTENDED_DYNAMIC_STATE_EXTENSION_NAME);
  371. LOG_WARNING(Render_Vulkan,
  372. "Qualcomm drivers have a slow VK_KHR_push_descriptor implementation");
  373. extensions.push_descriptor = false;
  374. loaded_extensions.erase(VK_KHR_PUSH_DESCRIPTOR_EXTENSION_NAME);
  375. #if defined(ANDROID) && defined(ARCHITECTURE_arm64)
  376. // Patch the driver to enable BCn textures.
  377. const auto major = (properties.properties.driverVersion >> 24) << 2;
  378. const auto minor = (properties.properties.driverVersion >> 12) & 0xFFFU;
  379. const auto vendor = properties.properties.vendorID;
  380. const auto patch_status = adrenotools_get_bcn_type(major, minor, vendor);
  381. if (patch_status == ADRENOTOOLS_BCN_PATCH) {
  382. LOG_INFO(Render_Vulkan, "Patching Adreno driver to support BCn texture formats");
  383. if (adrenotools_patch_bcn(
  384. reinterpret_cast<void*>(dld.vkGetPhysicalDeviceFormatProperties))) {
  385. OverrideBcnFormats(format_properties);
  386. } else {
  387. LOG_ERROR(Render_Vulkan, "Patch failed! Driver code may now crash");
  388. }
  389. } else if (patch_status == ADRENOTOOLS_BCN_BLOB) {
  390. LOG_INFO(Render_Vulkan, "Adreno driver supports BCn textures without patches");
  391. } else {
  392. LOG_WARNING(Render_Vulkan, "Adreno driver can't be patched to enable BCn textures");
  393. }
  394. #endif
  395. }
  396. if (is_arm) {
  397. must_emulate_scaled_formats = true;
  398. LOG_WARNING(Render_Vulkan, "ARM drivers have broken VK_EXT_extended_dynamic_state");
  399. extensions.extended_dynamic_state = false;
  400. loaded_extensions.erase(VK_EXT_EXTENDED_DYNAMIC_STATE_EXTENSION_NAME);
  401. LOG_WARNING(Render_Vulkan, "ARM drivers have broken VK_EXT_extended_dynamic_state2");
  402. features.extended_dynamic_state2.extendedDynamicState2 = false;
  403. features.extended_dynamic_state2.extendedDynamicState2LogicOp = false;
  404. features.extended_dynamic_state2.extendedDynamicState2PatchControlPoints = false;
  405. extensions.extended_dynamic_state2 = false;
  406. loaded_extensions.erase(VK_EXT_EXTENDED_DYNAMIC_STATE_2_EXTENSION_NAME);
  407. }
  408. if (is_nvidia) {
  409. const u32 nv_major_version = (properties.properties.driverVersion >> 22) & 0x3ff;
  410. const auto arch = GetNvidiaArchitecture(physical, supported_extensions);
  411. switch (arch) {
  412. case NvidiaArchitecture::AmpereOrNewer:
  413. LOG_WARNING(Render_Vulkan, "Ampere and newer have broken float16 math");
  414. features.shader_float16_int8.shaderFloat16 = false;
  415. break;
  416. case NvidiaArchitecture::Turing:
  417. break;
  418. case NvidiaArchitecture::VoltaOrOlder:
  419. if (nv_major_version < 527) {
  420. LOG_WARNING(Render_Vulkan, "Volta and older have broken VK_KHR_push_descriptor");
  421. extensions.push_descriptor = false;
  422. loaded_extensions.erase(VK_KHR_PUSH_DESCRIPTOR_EXTENSION_NAME);
  423. }
  424. break;
  425. }
  426. if (nv_major_version >= 510) {
  427. LOG_WARNING(Render_Vulkan, "NVIDIA Drivers >= 510 do not support MSAA image blits");
  428. cant_blit_msaa = true;
  429. }
  430. }
  431. if (extensions.extended_dynamic_state && is_radv) {
  432. // Mask driver version variant
  433. const u32 version = (properties.properties.driverVersion << 3) >> 3;
  434. if (version < VK_MAKE_API_VERSION(0, 21, 2, 0)) {
  435. LOG_WARNING(Render_Vulkan,
  436. "RADV versions older than 21.2 have broken VK_EXT_extended_dynamic_state");
  437. extensions.extended_dynamic_state = false;
  438. loaded_extensions.erase(VK_EXT_EXTENDED_DYNAMIC_STATE_EXTENSION_NAME);
  439. }
  440. }
  441. if (extensions.extended_dynamic_state2 && is_radv) {
  442. const u32 version = (properties.properties.driverVersion << 3) >> 3;
  443. if (version < VK_MAKE_API_VERSION(0, 22, 3, 1)) {
  444. LOG_WARNING(
  445. Render_Vulkan,
  446. "RADV versions older than 22.3.1 have broken VK_EXT_extended_dynamic_state2");
  447. features.extended_dynamic_state2.extendedDynamicState2 = false;
  448. features.extended_dynamic_state2.extendedDynamicState2LogicOp = false;
  449. features.extended_dynamic_state2.extendedDynamicState2PatchControlPoints = false;
  450. extensions.extended_dynamic_state2 = false;
  451. loaded_extensions.erase(VK_EXT_EXTENDED_DYNAMIC_STATE_2_EXTENSION_NAME);
  452. }
  453. }
  454. if (extensions.extended_dynamic_state2 && is_qualcomm) {
  455. const u32 version = (properties.properties.driverVersion << 3) >> 3;
  456. if (version >= VK_MAKE_API_VERSION(0, 0, 676, 0) &&
  457. version < VK_MAKE_API_VERSION(0, 0, 680, 0)) {
  458. // Qualcomm Adreno 7xx drivers do not properly support extended_dynamic_state2.
  459. LOG_WARNING(Render_Vulkan,
  460. "Qualcomm Adreno 7xx drivers have broken VK_EXT_extended_dynamic_state2");
  461. features.extended_dynamic_state2.extendedDynamicState2 = false;
  462. features.extended_dynamic_state2.extendedDynamicState2LogicOp = false;
  463. features.extended_dynamic_state2.extendedDynamicState2PatchControlPoints = false;
  464. extensions.extended_dynamic_state2 = false;
  465. loaded_extensions.erase(VK_EXT_EXTENDED_DYNAMIC_STATE_2_EXTENSION_NAME);
  466. }
  467. }
  468. if (extensions.extended_dynamic_state3 && is_radv) {
  469. LOG_WARNING(Render_Vulkan, "RADV has broken extendedDynamicState3ColorBlendEquation");
  470. features.extended_dynamic_state3.extendedDynamicState3ColorBlendEnable = false;
  471. features.extended_dynamic_state3.extendedDynamicState3ColorBlendEquation = false;
  472. dynamic_state3_blending = false;
  473. const u32 version = (properties.properties.driverVersion << 3) >> 3;
  474. if (version < VK_MAKE_API_VERSION(0, 23, 1, 0)) {
  475. LOG_WARNING(Render_Vulkan,
  476. "RADV versions older than 23.1.0 have broken depth clamp dynamic state");
  477. features.extended_dynamic_state3.extendedDynamicState3DepthClampEnable = false;
  478. dynamic_state3_enables = false;
  479. }
  480. }
  481. if (extensions.vertex_input_dynamic_state && is_radv) {
  482. // TODO(ameerj): Blacklist only offending driver versions
  483. // TODO(ameerj): Confirm if RDNA1 is affected
  484. const bool is_rdna2 =
  485. supported_extensions.contains(VK_KHR_FRAGMENT_SHADING_RATE_EXTENSION_NAME);
  486. if (is_rdna2) {
  487. LOG_WARNING(Render_Vulkan,
  488. "RADV has broken VK_EXT_vertex_input_dynamic_state on RDNA2 hardware");
  489. features.vertex_input_dynamic_state.vertexInputDynamicState = false;
  490. extensions.vertex_input_dynamic_state = false;
  491. loaded_extensions.erase(VK_EXT_VERTEX_INPUT_DYNAMIC_STATE_EXTENSION_NAME);
  492. }
  493. }
  494. if (extensions.vertex_input_dynamic_state && is_qualcomm) {
  495. const u32 version = (properties.properties.driverVersion << 3) >> 3;
  496. if (version >= VK_MAKE_API_VERSION(0, 0, 676, 0) &&
  497. version < VK_MAKE_API_VERSION(0, 0, 680, 0)) {
  498. // Qualcomm Adreno 7xx drivers do not properly support vertex_input_dynamic_state.
  499. LOG_WARNING(
  500. Render_Vulkan,
  501. "Qualcomm Adreno 7xx drivers have broken VK_EXT_vertex_input_dynamic_state");
  502. features.vertex_input_dynamic_state.vertexInputDynamicState = false;
  503. extensions.vertex_input_dynamic_state = false;
  504. loaded_extensions.erase(VK_EXT_VERTEX_INPUT_DYNAMIC_STATE_EXTENSION_NAME);
  505. }
  506. }
  507. sets_per_pool = 64;
  508. if (extensions.extended_dynamic_state3 && is_amd_driver &&
  509. !features.shader_float16_int8.shaderFloat16 &&
  510. properties.properties.driverVersion >= VK_MAKE_API_VERSION(0, 2, 0, 258)) {
  511. LOG_WARNING(Render_Vulkan, "AMD GCN4 has broken extendedDynamicState3ColorBlendEquation");
  512. features.extended_dynamic_state3.extendedDynamicState3ColorBlendEnable = false;
  513. features.extended_dynamic_state3.extendedDynamicState3ColorBlendEquation = false;
  514. dynamic_state3_blending = false;
  515. }
  516. if (is_amd_driver) {
  517. // AMD drivers need a higher amount of Sets per Pool in certain circumstances like in XC2.
  518. sets_per_pool = 96;
  519. // Disable VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT on AMD GCN4 and lower as it is broken.
  520. if (!features.shader_float16_int8.shaderFloat16) {
  521. LOG_WARNING(Render_Vulkan,
  522. "AMD GCN4 and earlier have broken VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT");
  523. has_broken_cube_compatibility = true;
  524. }
  525. }
  526. if (extensions.sampler_filter_minmax && is_amd) {
  527. // Disable ext_sampler_filter_minmax on AMD GCN4 and lower as it is broken.
  528. if (!features.shader_float16_int8.shaderFloat16) {
  529. LOG_WARNING(Render_Vulkan,
  530. "AMD GCN4 and earlier have broken VK_EXT_sampler_filter_minmax");
  531. extensions.sampler_filter_minmax = false;
  532. loaded_extensions.erase(VK_EXT_SAMPLER_FILTER_MINMAX_EXTENSION_NAME);
  533. }
  534. }
  535. if (extensions.vertex_input_dynamic_state && is_intel_windows) {
  536. const u32 version = (properties.properties.driverVersion << 3) >> 3;
  537. if (version < VK_MAKE_API_VERSION(27, 20, 100, 0)) {
  538. LOG_WARNING(Render_Vulkan, "Intel has broken VK_EXT_vertex_input_dynamic_state");
  539. extensions.vertex_input_dynamic_state = false;
  540. loaded_extensions.erase(VK_EXT_VERTEX_INPUT_DYNAMIC_STATE_EXTENSION_NAME);
  541. }
  542. }
  543. if (features.shader_float16_int8.shaderFloat16 && is_intel_windows) {
  544. // Intel's compiler crashes when using fp16 on Astral Chain, disable it for the time being.
  545. LOG_WARNING(Render_Vulkan, "Intel has broken float16 math");
  546. features.shader_float16_int8.shaderFloat16 = false;
  547. }
  548. if (is_intel_windows) {
  549. LOG_WARNING(Render_Vulkan, "Intel proprietary drivers do not support MSAA image blits");
  550. cant_blit_msaa = true;
  551. }
  552. has_broken_compute =
  553. CheckBrokenCompute(properties.driver.driverID, properties.properties.driverVersion) &&
  554. !Settings::values.enable_compute_pipelines.GetValue();
  555. if (is_intel_anv || (is_qualcomm && !is_s8gen2)) {
  556. LOG_WARNING(Render_Vulkan, "Driver does not support native BGR format");
  557. must_emulate_bgr565 = true;
  558. }
  559. if (extensions.push_descriptor && is_intel_anv) {
  560. const u32 version = (properties.properties.driverVersion << 3) >> 3;
  561. if (version >= VK_MAKE_API_VERSION(0, 22, 3, 0) &&
  562. version < VK_MAKE_API_VERSION(0, 23, 2, 0)) {
  563. // Disable VK_KHR_push_descriptor due to
  564. // mesa/mesa/-/commit/ff91c5ca42bc80aa411cb3fd8f550aa6fdd16bdc
  565. LOG_WARNING(Render_Vulkan,
  566. "ANV drivers 22.3.0 to 23.1.0 have broken VK_KHR_push_descriptor");
  567. extensions.push_descriptor = false;
  568. loaded_extensions.erase(VK_KHR_PUSH_DESCRIPTOR_EXTENSION_NAME);
  569. }
  570. }
  571. if (is_mvk) {
  572. LOG_WARNING(Render_Vulkan,
  573. "MVK driver breaks when using more than 16 vertex attributes/bindings");
  574. properties.properties.limits.maxVertexInputAttributes =
  575. std::min(properties.properties.limits.maxVertexInputAttributes, 16U);
  576. properties.properties.limits.maxVertexInputBindings =
  577. std::min(properties.properties.limits.maxVertexInputBindings, 16U);
  578. }
  579. logical = vk::Device::Create(physical, queue_cis, ExtensionListForVulkan(loaded_extensions),
  580. first_next, dld);
  581. graphics_queue = logical.GetQueue(graphics_family);
  582. present_queue = logical.GetQueue(present_family);
  583. VmaVulkanFunctions functions{};
  584. functions.vkGetInstanceProcAddr = dld.vkGetInstanceProcAddr;
  585. functions.vkGetDeviceProcAddr = dld.vkGetDeviceProcAddr;
  586. const VmaAllocatorCreateInfo allocator_info = {
  587. .flags = VMA_ALLOCATOR_CREATE_EXTERNALLY_SYNCHRONIZED_BIT,
  588. .physicalDevice = physical,
  589. .device = *logical,
  590. .preferredLargeHeapBlockSize = 0,
  591. .pAllocationCallbacks = nullptr,
  592. .pDeviceMemoryCallbacks = nullptr,
  593. .pHeapSizeLimit = nullptr,
  594. .pVulkanFunctions = &functions,
  595. .instance = instance,
  596. .vulkanApiVersion = VK_API_VERSION_1_1,
  597. .pTypeExternalMemoryHandleTypes = nullptr,
  598. };
  599. vk::Check(vmaCreateAllocator(&allocator_info, &allocator));
  600. }
  601. Device::~Device() {
  602. vmaDestroyAllocator(allocator);
  603. }
  604. VkFormat Device::GetSupportedFormat(VkFormat wanted_format, VkFormatFeatureFlags wanted_usage,
  605. FormatType format_type) const {
  606. if (IsFormatSupported(wanted_format, wanted_usage, format_type)) {
  607. return wanted_format;
  608. }
  609. // The wanted format is not supported by hardware, search for alternatives
  610. const VkFormat* alternatives = GetFormatAlternatives(wanted_format);
  611. if (alternatives == nullptr) {
  612. ASSERT_MSG(false,
  613. "Format={} with usage={} and type={} has no defined alternatives and host "
  614. "hardware does not support it",
  615. wanted_format, wanted_usage, format_type);
  616. return wanted_format;
  617. }
  618. std::size_t i = 0;
  619. for (VkFormat alternative = *alternatives; alternative; alternative = alternatives[++i]) {
  620. if (!IsFormatSupported(alternative, wanted_usage, format_type)) {
  621. continue;
  622. }
  623. LOG_DEBUG(Render_Vulkan,
  624. "Emulating format={} with alternative format={} with usage={} and type={}",
  625. wanted_format, alternative, wanted_usage, format_type);
  626. return alternative;
  627. }
  628. // No alternatives found, panic
  629. ASSERT_MSG(false,
  630. "Format={} with usage={} and type={} is not supported by the host hardware and "
  631. "doesn't support any of the alternatives",
  632. wanted_format, wanted_usage, format_type);
  633. return wanted_format;
  634. }
  635. void Device::ReportLoss() const {
  636. LOG_CRITICAL(Render_Vulkan, "Device loss occurred!");
  637. // Wait for the log to flush and for Nsight Aftermath to dump the results
  638. std::this_thread::sleep_for(std::chrono::seconds{15});
  639. }
  640. void Device::SaveShader(std::span<const u32> spirv) const {
  641. if (nsight_aftermath_tracker) {
  642. nsight_aftermath_tracker->SaveShader(spirv);
  643. }
  644. }
  645. bool Device::ComputeIsOptimalAstcSupported() const {
  646. // Disable for now to avoid converting ASTC twice.
  647. static constexpr std::array astc_formats = {
  648. VK_FORMAT_ASTC_4x4_UNORM_BLOCK, VK_FORMAT_ASTC_4x4_SRGB_BLOCK,
  649. VK_FORMAT_ASTC_5x4_UNORM_BLOCK, VK_FORMAT_ASTC_5x4_SRGB_BLOCK,
  650. VK_FORMAT_ASTC_5x5_UNORM_BLOCK, VK_FORMAT_ASTC_5x5_SRGB_BLOCK,
  651. VK_FORMAT_ASTC_6x5_UNORM_BLOCK, VK_FORMAT_ASTC_6x5_SRGB_BLOCK,
  652. VK_FORMAT_ASTC_6x6_UNORM_BLOCK, VK_FORMAT_ASTC_6x6_SRGB_BLOCK,
  653. VK_FORMAT_ASTC_8x5_UNORM_BLOCK, VK_FORMAT_ASTC_8x5_SRGB_BLOCK,
  654. VK_FORMAT_ASTC_8x6_UNORM_BLOCK, VK_FORMAT_ASTC_8x6_SRGB_BLOCK,
  655. VK_FORMAT_ASTC_8x8_UNORM_BLOCK, VK_FORMAT_ASTC_8x8_SRGB_BLOCK,
  656. VK_FORMAT_ASTC_10x5_UNORM_BLOCK, VK_FORMAT_ASTC_10x5_SRGB_BLOCK,
  657. VK_FORMAT_ASTC_10x6_UNORM_BLOCK, VK_FORMAT_ASTC_10x6_SRGB_BLOCK,
  658. VK_FORMAT_ASTC_10x8_UNORM_BLOCK, VK_FORMAT_ASTC_10x8_SRGB_BLOCK,
  659. VK_FORMAT_ASTC_10x10_UNORM_BLOCK, VK_FORMAT_ASTC_10x10_SRGB_BLOCK,
  660. VK_FORMAT_ASTC_12x10_UNORM_BLOCK, VK_FORMAT_ASTC_12x10_SRGB_BLOCK,
  661. VK_FORMAT_ASTC_12x12_UNORM_BLOCK, VK_FORMAT_ASTC_12x12_SRGB_BLOCK,
  662. };
  663. if (!features.features.textureCompressionASTC_LDR) {
  664. return false;
  665. }
  666. const auto format_feature_usage{
  667. VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT | VK_FORMAT_FEATURE_BLIT_SRC_BIT |
  668. VK_FORMAT_FEATURE_BLIT_DST_BIT | VK_FORMAT_FEATURE_TRANSFER_SRC_BIT |
  669. VK_FORMAT_FEATURE_TRANSFER_DST_BIT};
  670. for (const auto format : astc_formats) {
  671. const auto physical_format_properties{physical.GetFormatProperties(format)};
  672. if ((physical_format_properties.optimalTilingFeatures & format_feature_usage) == 0) {
  673. return false;
  674. }
  675. }
  676. return true;
  677. }
  678. bool Device::TestDepthStencilBlits() const {
  679. static constexpr VkFormatFeatureFlags required_features =
  680. VK_FORMAT_FEATURE_BLIT_SRC_BIT | VK_FORMAT_FEATURE_BLIT_DST_BIT;
  681. const auto test_features = [](VkFormatProperties props) {
  682. return (props.optimalTilingFeatures & required_features) == required_features;
  683. };
  684. return test_features(format_properties.at(VK_FORMAT_D32_SFLOAT_S8_UINT)) &&
  685. test_features(format_properties.at(VK_FORMAT_D24_UNORM_S8_UINT));
  686. }
  687. bool Device::IsFormatSupported(VkFormat wanted_format, VkFormatFeatureFlags wanted_usage,
  688. FormatType format_type) const {
  689. const auto it = format_properties.find(wanted_format);
  690. if (it == format_properties.end()) {
  691. UNIMPLEMENTED_MSG("Unimplemented format query={}", wanted_format);
  692. return true;
  693. }
  694. const auto supported_usage = GetFormatFeatures(it->second, format_type);
  695. return (supported_usage & wanted_usage) == wanted_usage;
  696. }
  697. std::string Device::GetDriverName() const {
  698. switch (properties.driver.driverID) {
  699. case VK_DRIVER_ID_AMD_PROPRIETARY:
  700. return "AMD";
  701. case VK_DRIVER_ID_AMD_OPEN_SOURCE:
  702. return "AMDVLK";
  703. case VK_DRIVER_ID_MESA_RADV:
  704. return "RADV";
  705. case VK_DRIVER_ID_NVIDIA_PROPRIETARY:
  706. return "NVIDIA";
  707. case VK_DRIVER_ID_INTEL_PROPRIETARY_WINDOWS:
  708. return "INTEL";
  709. case VK_DRIVER_ID_INTEL_OPEN_SOURCE_MESA:
  710. return "ANV";
  711. case VK_DRIVER_ID_MESA_LLVMPIPE:
  712. return "LAVAPIPE";
  713. default:
  714. return properties.driver.driverName;
  715. }
  716. }
  717. bool Device::ShouldBoostClocks() const {
  718. const auto driver_id = properties.driver.driverID;
  719. const auto vendor_id = properties.properties.vendorID;
  720. const auto device_id = properties.properties.deviceID;
  721. const bool validated_driver =
  722. driver_id == VK_DRIVER_ID_AMD_PROPRIETARY || driver_id == VK_DRIVER_ID_AMD_OPEN_SOURCE ||
  723. driver_id == VK_DRIVER_ID_MESA_RADV || driver_id == VK_DRIVER_ID_NVIDIA_PROPRIETARY ||
  724. driver_id == VK_DRIVER_ID_INTEL_PROPRIETARY_WINDOWS ||
  725. driver_id == VK_DRIVER_ID_INTEL_OPEN_SOURCE_MESA ||
  726. driver_id == VK_DRIVER_ID_QUALCOMM_PROPRIETARY || driver_id == VK_DRIVER_ID_MESA_TURNIP;
  727. const bool is_steam_deck = vendor_id == 0x1002 && device_id == 0x163F;
  728. const bool is_debugging = this->HasDebuggingToolAttached();
  729. return validated_driver && !is_steam_deck && !is_debugging;
  730. }
  731. bool Device::HasTimelineSemaphore() const {
  732. if (GetDriverID() == VK_DRIVER_ID_QUALCOMM_PROPRIETARY ||
  733. GetDriverID() == VK_DRIVER_ID_MESA_TURNIP) {
  734. // Timeline semaphores do not work properly on all Qualcomm drivers.
  735. // They generally work properly with Turnip drivers, but are problematic on some devices
  736. // (e.g. ZTE handsets with Snapdragon 870).
  737. return false;
  738. }
  739. return features.timeline_semaphore.timelineSemaphore;
  740. }
  741. bool Device::GetSuitability(bool requires_swapchain) {
  742. // Assume we will be suitable.
  743. bool suitable = true;
  744. // Configure properties.
  745. properties.properties = physical.GetProperties();
  746. // Set instance version.
  747. instance_version = properties.properties.apiVersion;
  748. // Minimum of API version 1.1 is required. (This is well-supported.)
  749. ASSERT(instance_version >= VK_API_VERSION_1_1);
  750. // Get available extensions.
  751. auto extension_properties = physical.EnumerateDeviceExtensionProperties();
  752. // Get the set of supported extensions.
  753. supported_extensions.clear();
  754. for (const VkExtensionProperties& property : extension_properties) {
  755. supported_extensions.insert(property.extensionName);
  756. }
  757. // Generate list of extensions to load.
  758. loaded_extensions.clear();
  759. #define EXTENSION(prefix, macro_name, var_name) \
  760. if (supported_extensions.contains(VK_##prefix##_##macro_name##_EXTENSION_NAME)) { \
  761. loaded_extensions.insert(VK_##prefix##_##macro_name##_EXTENSION_NAME); \
  762. extensions.var_name = true; \
  763. }
  764. #define FEATURE_EXTENSION(prefix, struct_name, macro_name, var_name) \
  765. if (supported_extensions.contains(VK_##prefix##_##macro_name##_EXTENSION_NAME)) { \
  766. loaded_extensions.insert(VK_##prefix##_##macro_name##_EXTENSION_NAME); \
  767. extensions.var_name = true; \
  768. }
  769. if (instance_version < VK_API_VERSION_1_2) {
  770. FOR_EACH_VK_FEATURE_1_2(FEATURE_EXTENSION);
  771. }
  772. if (instance_version < VK_API_VERSION_1_3) {
  773. FOR_EACH_VK_FEATURE_1_3(FEATURE_EXTENSION);
  774. }
  775. FOR_EACH_VK_FEATURE_EXT(FEATURE_EXTENSION);
  776. FOR_EACH_VK_EXTENSION(EXTENSION);
  777. #undef FEATURE_EXTENSION
  778. #undef EXTENSION
  779. // Some extensions are mandatory. Check those.
  780. #define CHECK_EXTENSION(extension_name) \
  781. if (!loaded_extensions.contains(extension_name)) { \
  782. LOG_ERROR(Render_Vulkan, "Missing required extension {}", extension_name); \
  783. suitable = false; \
  784. }
  785. #define LOG_EXTENSION(extension_name) \
  786. if (!loaded_extensions.contains(extension_name)) { \
  787. LOG_INFO(Render_Vulkan, "Device doesn't support extension {}", extension_name); \
  788. }
  789. FOR_EACH_VK_RECOMMENDED_EXTENSION(LOG_EXTENSION);
  790. FOR_EACH_VK_MANDATORY_EXTENSION(CHECK_EXTENSION);
  791. if (requires_swapchain) {
  792. CHECK_EXTENSION(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
  793. }
  794. #undef LOG_EXTENSION
  795. #undef CHECK_EXTENSION
  796. // Generate the linked list of features to test.
  797. features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
  798. // Set next pointer.
  799. void** next = &features2.pNext;
  800. // Test all features we know about. If the feature is not available in core at our
  801. // current API version, and was not enabled by an extension, skip testing the feature.
  802. // We set the structure sType explicitly here as it is zeroed by the constructor.
  803. #define FEATURE(prefix, struct_name, macro_name, var_name) \
  804. features.var_name.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_##macro_name##_FEATURES; \
  805. SetNext(next, features.var_name);
  806. #define EXT_FEATURE(prefix, struct_name, macro_name, var_name) \
  807. if (extensions.var_name) { \
  808. features.var_name.sType = \
  809. VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_##macro_name##_FEATURES_##prefix; \
  810. SetNext(next, features.var_name); \
  811. }
  812. FOR_EACH_VK_FEATURE_1_1(FEATURE);
  813. FOR_EACH_VK_FEATURE_EXT(EXT_FEATURE);
  814. if (instance_version >= VK_API_VERSION_1_2) {
  815. FOR_EACH_VK_FEATURE_1_2(FEATURE);
  816. } else {
  817. FOR_EACH_VK_FEATURE_1_2(EXT_FEATURE);
  818. }
  819. if (instance_version >= VK_API_VERSION_1_3) {
  820. FOR_EACH_VK_FEATURE_1_3(FEATURE);
  821. } else {
  822. FOR_EACH_VK_FEATURE_1_3(EXT_FEATURE);
  823. }
  824. #undef EXT_FEATURE
  825. #undef FEATURE
  826. // Perform the feature test.
  827. physical.GetFeatures2(features2);
  828. features.features = features2.features;
  829. // Some features are mandatory. Check those.
  830. #define CHECK_FEATURE(feature, name) \
  831. if (!features.feature.name) { \
  832. LOG_ERROR(Render_Vulkan, "Missing required feature {}", #name); \
  833. suitable = false; \
  834. }
  835. #define LOG_FEATURE(feature, name) \
  836. if (!features.feature.name) { \
  837. LOG_INFO(Render_Vulkan, "Device doesn't support feature {}", #name); \
  838. }
  839. FOR_EACH_VK_RECOMMENDED_FEATURE(LOG_FEATURE);
  840. FOR_EACH_VK_MANDATORY_FEATURE(CHECK_FEATURE);
  841. #undef LOG_FEATURE
  842. #undef CHECK_FEATURE
  843. // Generate linked list of properties.
  844. properties2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
  845. // Set next pointer.
  846. next = &properties2.pNext;
  847. // Get driver info.
  848. properties.driver.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRIVER_PROPERTIES;
  849. SetNext(next, properties.driver);
  850. // Retrieve subgroup properties.
  851. properties.subgroup_properties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_PROPERTIES;
  852. SetNext(next, properties.subgroup_properties);
  853. // Retrieve relevant extension properties.
  854. if (extensions.shader_float_controls) {
  855. properties.float_controls.sType =
  856. VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FLOAT_CONTROLS_PROPERTIES;
  857. SetNext(next, properties.float_controls);
  858. }
  859. if (extensions.push_descriptor) {
  860. properties.push_descriptor.sType =
  861. VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PUSH_DESCRIPTOR_PROPERTIES_KHR;
  862. SetNext(next, properties.push_descriptor);
  863. }
  864. if (extensions.subgroup_size_control) {
  865. properties.subgroup_size_control.sType =
  866. VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_SIZE_CONTROL_PROPERTIES;
  867. SetNext(next, properties.subgroup_size_control);
  868. }
  869. if (extensions.transform_feedback) {
  870. properties.transform_feedback.sType =
  871. VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TRANSFORM_FEEDBACK_PROPERTIES_EXT;
  872. SetNext(next, properties.transform_feedback);
  873. }
  874. // Perform the property fetch.
  875. physical.GetProperties2(properties2);
  876. properties.properties = properties2.properties;
  877. // Unload extensions if feature support is insufficient.
  878. RemoveUnsuitableExtensions();
  879. // Check limits.
  880. struct Limit {
  881. u32 minimum;
  882. u32 value;
  883. const char* name;
  884. };
  885. const VkPhysicalDeviceLimits& limits{properties.properties.limits};
  886. const std::array limits_report{
  887. Limit{65536, limits.maxUniformBufferRange, "maxUniformBufferRange"},
  888. Limit{16, limits.maxViewports, "maxViewports"},
  889. Limit{8, limits.maxColorAttachments, "maxColorAttachments"},
  890. Limit{8, limits.maxClipDistances, "maxClipDistances"},
  891. };
  892. for (const auto& [min, value, name] : limits_report) {
  893. if (value < min) {
  894. LOG_ERROR(Render_Vulkan, "{} has to be {} or greater but it is {}", name, min, value);
  895. suitable = false;
  896. }
  897. }
  898. // Return whether we were suitable.
  899. return suitable;
  900. }
  901. void Device::RemoveExtensionIfUnsuitable(bool is_suitable, const std::string& extension_name) {
  902. if (loaded_extensions.contains(extension_name) && !is_suitable) {
  903. LOG_WARNING(Render_Vulkan, "Removing unsuitable extension {}", extension_name);
  904. loaded_extensions.erase(extension_name);
  905. }
  906. }
  907. void Device::RemoveUnsuitableExtensions() {
  908. // VK_EXT_custom_border_color
  909. extensions.custom_border_color = features.custom_border_color.customBorderColors &&
  910. features.custom_border_color.customBorderColorWithoutFormat;
  911. RemoveExtensionIfUnsuitable(extensions.custom_border_color,
  912. VK_EXT_CUSTOM_BORDER_COLOR_EXTENSION_NAME);
  913. // VK_EXT_depth_clip_control
  914. extensions.depth_clip_control = features.depth_clip_control.depthClipControl;
  915. RemoveExtensionIfUnsuitable(extensions.depth_clip_control,
  916. VK_EXT_DEPTH_CLIP_CONTROL_EXTENSION_NAME);
  917. // VK_EXT_extended_dynamic_state
  918. extensions.extended_dynamic_state = features.extended_dynamic_state.extendedDynamicState;
  919. RemoveExtensionIfUnsuitable(extensions.extended_dynamic_state,
  920. VK_EXT_EXTENDED_DYNAMIC_STATE_EXTENSION_NAME);
  921. // VK_EXT_extended_dynamic_state2
  922. extensions.extended_dynamic_state2 = features.extended_dynamic_state2.extendedDynamicState2;
  923. RemoveExtensionIfUnsuitable(extensions.extended_dynamic_state2,
  924. VK_EXT_EXTENDED_DYNAMIC_STATE_2_EXTENSION_NAME);
  925. // VK_EXT_extended_dynamic_state3
  926. dynamic_state3_blending =
  927. features.extended_dynamic_state3.extendedDynamicState3ColorBlendEnable &&
  928. features.extended_dynamic_state3.extendedDynamicState3ColorBlendEquation &&
  929. features.extended_dynamic_state3.extendedDynamicState3ColorWriteMask;
  930. dynamic_state3_enables =
  931. features.extended_dynamic_state3.extendedDynamicState3DepthClampEnable &&
  932. features.extended_dynamic_state3.extendedDynamicState3LogicOpEnable;
  933. extensions.extended_dynamic_state3 = dynamic_state3_blending || dynamic_state3_enables;
  934. dynamic_state3_blending = dynamic_state3_blending && extensions.extended_dynamic_state3;
  935. dynamic_state3_enables = dynamic_state3_enables && extensions.extended_dynamic_state3;
  936. RemoveExtensionIfUnsuitable(extensions.extended_dynamic_state3,
  937. VK_EXT_EXTENDED_DYNAMIC_STATE_3_EXTENSION_NAME);
  938. // VK_EXT_provoking_vertex
  939. extensions.provoking_vertex =
  940. features.provoking_vertex.provokingVertexLast &&
  941. features.provoking_vertex.transformFeedbackPreservesProvokingVertex;
  942. RemoveExtensionIfUnsuitable(extensions.provoking_vertex,
  943. VK_EXT_PROVOKING_VERTEX_EXTENSION_NAME);
  944. // VK_KHR_shader_atomic_int64
  945. extensions.shader_atomic_int64 = features.shader_atomic_int64.shaderBufferInt64Atomics &&
  946. features.shader_atomic_int64.shaderSharedInt64Atomics;
  947. RemoveExtensionIfUnsuitable(extensions.shader_atomic_int64,
  948. VK_KHR_SHADER_ATOMIC_INT64_EXTENSION_NAME);
  949. // VK_EXT_shader_demote_to_helper_invocation
  950. extensions.shader_demote_to_helper_invocation =
  951. features.shader_demote_to_helper_invocation.shaderDemoteToHelperInvocation;
  952. RemoveExtensionIfUnsuitable(extensions.shader_demote_to_helper_invocation,
  953. VK_EXT_SHADER_DEMOTE_TO_HELPER_INVOCATION_EXTENSION_NAME);
  954. // VK_EXT_subgroup_size_control
  955. extensions.subgroup_size_control =
  956. features.subgroup_size_control.subgroupSizeControl &&
  957. properties.subgroup_size_control.minSubgroupSize <= GuestWarpSize &&
  958. properties.subgroup_size_control.maxSubgroupSize >= GuestWarpSize;
  959. RemoveExtensionIfUnsuitable(extensions.subgroup_size_control,
  960. VK_EXT_SUBGROUP_SIZE_CONTROL_EXTENSION_NAME);
  961. // VK_EXT_transform_feedback
  962. extensions.transform_feedback =
  963. features.transform_feedback.transformFeedback &&
  964. features.transform_feedback.geometryStreams &&
  965. properties.transform_feedback.maxTransformFeedbackStreams >= 4 &&
  966. properties.transform_feedback.maxTransformFeedbackBuffers > 0 &&
  967. properties.transform_feedback.transformFeedbackQueries &&
  968. properties.transform_feedback.transformFeedbackDraw;
  969. RemoveExtensionIfUnsuitable(extensions.transform_feedback,
  970. VK_EXT_TRANSFORM_FEEDBACK_EXTENSION_NAME);
  971. // VK_EXT_vertex_input_dynamic_state
  972. extensions.vertex_input_dynamic_state =
  973. features.vertex_input_dynamic_state.vertexInputDynamicState;
  974. RemoveExtensionIfUnsuitable(extensions.vertex_input_dynamic_state,
  975. VK_EXT_VERTEX_INPUT_DYNAMIC_STATE_EXTENSION_NAME);
  976. // VK_KHR_pipeline_executable_properties
  977. if (Settings::values.renderer_shader_feedback.GetValue()) {
  978. extensions.pipeline_executable_properties =
  979. features.pipeline_executable_properties.pipelineExecutableInfo;
  980. RemoveExtensionIfUnsuitable(extensions.pipeline_executable_properties,
  981. VK_KHR_PIPELINE_EXECUTABLE_PROPERTIES_EXTENSION_NAME);
  982. } else {
  983. extensions.pipeline_executable_properties = false;
  984. loaded_extensions.erase(VK_KHR_PIPELINE_EXECUTABLE_PROPERTIES_EXTENSION_NAME);
  985. }
  986. // VK_KHR_workgroup_memory_explicit_layout
  987. extensions.workgroup_memory_explicit_layout =
  988. features.features.shaderInt16 &&
  989. features.workgroup_memory_explicit_layout.workgroupMemoryExplicitLayout &&
  990. features.workgroup_memory_explicit_layout.workgroupMemoryExplicitLayout8BitAccess &&
  991. features.workgroup_memory_explicit_layout.workgroupMemoryExplicitLayout16BitAccess &&
  992. features.workgroup_memory_explicit_layout.workgroupMemoryExplicitLayoutScalarBlockLayout;
  993. RemoveExtensionIfUnsuitable(extensions.workgroup_memory_explicit_layout,
  994. VK_KHR_WORKGROUP_MEMORY_EXPLICIT_LAYOUT_EXTENSION_NAME);
  995. }
  996. void Device::SetupFamilies(VkSurfaceKHR surface) {
  997. const std::vector queue_family_properties = physical.GetQueueFamilyProperties();
  998. std::optional<u32> graphics;
  999. std::optional<u32> present;
  1000. for (u32 index = 0; index < static_cast<u32>(queue_family_properties.size()); ++index) {
  1001. if (graphics && (present || !surface)) {
  1002. break;
  1003. }
  1004. const VkQueueFamilyProperties& queue_family = queue_family_properties[index];
  1005. if (queue_family.queueCount == 0) {
  1006. continue;
  1007. }
  1008. if (queue_family.queueFlags & VK_QUEUE_GRAPHICS_BIT) {
  1009. graphics = index;
  1010. }
  1011. if (surface && physical.GetSurfaceSupportKHR(index, surface)) {
  1012. present = index;
  1013. }
  1014. }
  1015. if (!graphics) {
  1016. LOG_ERROR(Render_Vulkan, "Device lacks a graphics queue");
  1017. throw vk::Exception(VK_ERROR_FEATURE_NOT_PRESENT);
  1018. }
  1019. if (surface && !present) {
  1020. LOG_ERROR(Render_Vulkan, "Device lacks a present queue");
  1021. throw vk::Exception(VK_ERROR_FEATURE_NOT_PRESENT);
  1022. }
  1023. if (graphics) {
  1024. graphics_family = *graphics;
  1025. }
  1026. if (present) {
  1027. present_family = *present;
  1028. }
  1029. }
  1030. u64 Device::GetDeviceMemoryUsage() const {
  1031. VkPhysicalDeviceMemoryBudgetPropertiesEXT budget;
  1032. budget.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_BUDGET_PROPERTIES_EXT;
  1033. budget.pNext = nullptr;
  1034. physical.GetMemoryProperties(&budget);
  1035. u64 result{};
  1036. for (const size_t heap : valid_heap_memory) {
  1037. result += budget.heapUsage[heap];
  1038. }
  1039. return result;
  1040. }
  1041. void Device::CollectPhysicalMemoryInfo() {
  1042. // Account for resolution scaling in memory limits
  1043. const size_t normal_memory = 6_GiB;
  1044. const size_t scaler_memory = 1_GiB * Settings::values.resolution_info.ScaleUp(1);
  1045. // Calculate limits using memory budget
  1046. VkPhysicalDeviceMemoryBudgetPropertiesEXT budget{};
  1047. budget.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_BUDGET_PROPERTIES_EXT;
  1048. const auto mem_info =
  1049. physical.GetMemoryProperties(extensions.memory_budget ? &budget : nullptr);
  1050. const auto& mem_properties = mem_info.memoryProperties;
  1051. const size_t num_properties = mem_properties.memoryHeapCount;
  1052. device_access_memory = 0;
  1053. u64 device_initial_usage = 0;
  1054. u64 local_memory = 0;
  1055. for (size_t element = 0; element < num_properties; ++element) {
  1056. const bool is_heap_local =
  1057. (mem_properties.memoryHeaps[element].flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT) != 0;
  1058. if (!is_integrated && !is_heap_local) {
  1059. continue;
  1060. }
  1061. valid_heap_memory.push_back(element);
  1062. if (is_heap_local) {
  1063. local_memory += mem_properties.memoryHeaps[element].size;
  1064. }
  1065. if (extensions.memory_budget) {
  1066. device_initial_usage += budget.heapUsage[element];
  1067. device_access_memory += budget.heapBudget[element];
  1068. continue;
  1069. }
  1070. device_access_memory += mem_properties.memoryHeaps[element].size;
  1071. }
  1072. if (!is_integrated) {
  1073. const u64 reserve_memory = std::min<u64>(device_access_memory / 8, 1_GiB);
  1074. device_access_memory -= reserve_memory;
  1075. device_access_memory = std::min<u64>(device_access_memory, normal_memory + scaler_memory);
  1076. return;
  1077. }
  1078. const s64 available_memory = static_cast<s64>(device_access_memory - device_initial_usage);
  1079. device_access_memory = static_cast<u64>(std::max<s64>(
  1080. std::min<s64>(available_memory - 8_GiB, 4_GiB), std::min<s64>(local_memory, 4_GiB)));
  1081. }
  1082. void Device::CollectToolingInfo() {
  1083. if (!extensions.tooling_info) {
  1084. return;
  1085. }
  1086. auto tools{physical.GetPhysicalDeviceToolProperties()};
  1087. for (const VkPhysicalDeviceToolProperties& tool : tools) {
  1088. const std::string_view name = tool.name;
  1089. LOG_INFO(Render_Vulkan, "Attached debugging tool: {}", name);
  1090. has_renderdoc = has_renderdoc || name == "RenderDoc";
  1091. has_nsight_graphics = has_nsight_graphics || name == "NVIDIA Nsight Graphics";
  1092. }
  1093. }
  1094. std::vector<VkDeviceQueueCreateInfo> Device::GetDeviceQueueCreateInfos() const {
  1095. static constexpr float QUEUE_PRIORITY = 1.0f;
  1096. std::unordered_set<u32> unique_queue_families{graphics_family, present_family};
  1097. std::vector<VkDeviceQueueCreateInfo> queue_cis;
  1098. queue_cis.reserve(unique_queue_families.size());
  1099. for (const u32 queue_family : unique_queue_families) {
  1100. auto& ci = queue_cis.emplace_back(VkDeviceQueueCreateInfo{
  1101. .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
  1102. .pNext = nullptr,
  1103. .flags = 0,
  1104. .queueFamilyIndex = queue_family,
  1105. .queueCount = 1,
  1106. .pQueuePriorities = nullptr,
  1107. });
  1108. ci.pQueuePriorities = &QUEUE_PRIORITY;
  1109. }
  1110. return queue_cis;
  1111. }
  1112. } // namespace Vulkan