VulkanInitialization.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. using Ryujinx.Common.Configuration;
  2. using Ryujinx.Common.Logging;
  3. using Ryujinx.Graphics.GAL;
  4. using Silk.NET.Vulkan;
  5. using Silk.NET.Vulkan.Extensions.EXT;
  6. using Silk.NET.Vulkan.Extensions.KHR;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Runtime.InteropServices;
  11. namespace Ryujinx.Graphics.Vulkan
  12. {
  13. public unsafe static class VulkanInitialization
  14. {
  15. private const uint InvalidIndex = uint.MaxValue;
  16. private static uint MinimalVulkanVersion = Vk.Version11.Value;
  17. private static uint MinimalInstanceVulkanVersion = Vk.Version12.Value;
  18. private static uint MaximumVulkanVersion = Vk.Version12.Value;
  19. private const string AppName = "Ryujinx.Graphics.Vulkan";
  20. private const int QueuesCount = 2;
  21. private static readonly string[] _desirableExtensions = new string[]
  22. {
  23. ExtConditionalRendering.ExtensionName,
  24. ExtExtendedDynamicState.ExtensionName,
  25. ExtTransformFeedback.ExtensionName,
  26. KhrDrawIndirectCount.ExtensionName,
  27. KhrPushDescriptor.ExtensionName,
  28. "VK_EXT_blend_operation_advanced",
  29. "VK_EXT_custom_border_color",
  30. "VK_EXT_descriptor_indexing", // Enabling this works around an issue with disposed buffer bindings on RADV.
  31. "VK_EXT_fragment_shader_interlock",
  32. "VK_EXT_index_type_uint8",
  33. "VK_EXT_primitive_topology_list_restart",
  34. "VK_EXT_robustness2",
  35. "VK_EXT_shader_stencil_export",
  36. "VK_KHR_shader_float16_int8",
  37. "VK_EXT_shader_subgroup_ballot",
  38. "VK_EXT_subgroup_size_control",
  39. "VK_NV_geometry_shader_passthrough",
  40. "VK_KHR_portability_subset", // By spec, we should enable this if present.
  41. };
  42. private static readonly string[] _requiredExtensions = new string[]
  43. {
  44. KhrSwapchain.ExtensionName
  45. };
  46. internal static Instance CreateInstance(Vk api, GraphicsDebugLevel logLevel, string[] requiredExtensions)
  47. {
  48. var enabledLayers = new List<string>();
  49. void AddAvailableLayer(string layerName)
  50. {
  51. uint layerPropertiesCount;
  52. api.EnumerateInstanceLayerProperties(&layerPropertiesCount, null).ThrowOnError();
  53. LayerProperties[] layerProperties = new LayerProperties[layerPropertiesCount];
  54. fixed (LayerProperties* pLayerProperties = layerProperties)
  55. {
  56. api.EnumerateInstanceLayerProperties(&layerPropertiesCount, layerProperties).ThrowOnError();
  57. for (int i = 0; i < layerPropertiesCount; i++)
  58. {
  59. string currentLayerName = Marshal.PtrToStringAnsi((IntPtr)pLayerProperties[i].LayerName);
  60. if (currentLayerName == layerName)
  61. {
  62. enabledLayers.Add(layerName);
  63. return;
  64. }
  65. }
  66. }
  67. Logger.Warning?.Print(LogClass.Gpu, $"Missing layer {layerName}");
  68. }
  69. if (logLevel != GraphicsDebugLevel.None)
  70. {
  71. AddAvailableLayer("VK_LAYER_KHRONOS_validation");
  72. }
  73. var enabledExtensions = requiredExtensions;
  74. if (api.IsInstanceExtensionPresent("VK_EXT_debug_utils"))
  75. {
  76. enabledExtensions = enabledExtensions.Append(ExtDebugUtils.ExtensionName).ToArray();
  77. }
  78. var appName = Marshal.StringToHGlobalAnsi(AppName);
  79. var applicationInfo = new ApplicationInfo
  80. {
  81. PApplicationName = (byte*)appName,
  82. ApplicationVersion = 1,
  83. PEngineName = (byte*)appName,
  84. EngineVersion = 1,
  85. ApiVersion = MaximumVulkanVersion
  86. };
  87. IntPtr* ppEnabledExtensions = stackalloc IntPtr[enabledExtensions.Length];
  88. IntPtr* ppEnabledLayers = stackalloc IntPtr[enabledLayers.Count];
  89. for (int i = 0; i < enabledExtensions.Length; i++)
  90. {
  91. ppEnabledExtensions[i] = Marshal.StringToHGlobalAnsi(enabledExtensions[i]);
  92. }
  93. for (int i = 0; i < enabledLayers.Count; i++)
  94. {
  95. ppEnabledLayers[i] = Marshal.StringToHGlobalAnsi(enabledLayers[i]);
  96. }
  97. var instanceCreateInfo = new InstanceCreateInfo
  98. {
  99. SType = StructureType.InstanceCreateInfo,
  100. PApplicationInfo = &applicationInfo,
  101. PpEnabledExtensionNames = (byte**)ppEnabledExtensions,
  102. PpEnabledLayerNames = (byte**)ppEnabledLayers,
  103. EnabledExtensionCount = (uint)enabledExtensions.Length,
  104. EnabledLayerCount = (uint)enabledLayers.Count
  105. };
  106. api.CreateInstance(in instanceCreateInfo, null, out var instance).ThrowOnError();
  107. Marshal.FreeHGlobal(appName);
  108. for (int i = 0; i < enabledExtensions.Length; i++)
  109. {
  110. Marshal.FreeHGlobal(ppEnabledExtensions[i]);
  111. }
  112. for (int i = 0; i < enabledLayers.Count; i++)
  113. {
  114. Marshal.FreeHGlobal(ppEnabledLayers[i]);
  115. }
  116. return instance;
  117. }
  118. internal static PhysicalDevice FindSuitablePhysicalDevice(Vk api, Instance instance, SurfaceKHR surface, string preferredGpuId)
  119. {
  120. uint physicalDeviceCount;
  121. api.EnumeratePhysicalDevices(instance, &physicalDeviceCount, null).ThrowOnError();
  122. PhysicalDevice[] physicalDevices = new PhysicalDevice[physicalDeviceCount];
  123. fixed (PhysicalDevice* pPhysicalDevices = physicalDevices)
  124. {
  125. api.EnumeratePhysicalDevices(instance, &physicalDeviceCount, pPhysicalDevices).ThrowOnError();
  126. }
  127. // First we try to pick the the user preferred GPU.
  128. for (int i = 0; i < physicalDevices.Length; i++)
  129. {
  130. if (IsPreferredAndSuitableDevice(api, physicalDevices[i], surface, preferredGpuId))
  131. {
  132. return physicalDevices[i];
  133. }
  134. }
  135. // If we fail to do that, just use the first compatible GPU.
  136. for (int i = 0; i < physicalDevices.Length; i++)
  137. {
  138. if (IsSuitableDevice(api, physicalDevices[i], surface))
  139. {
  140. return physicalDevices[i];
  141. }
  142. }
  143. throw new VulkanException("Initialization failed, none of the available GPUs meets the minimum requirements.");
  144. }
  145. internal static DeviceInfo[] GetSuitablePhysicalDevices(Vk api)
  146. {
  147. var appName = Marshal.StringToHGlobalAnsi(AppName);
  148. var applicationInfo = new ApplicationInfo
  149. {
  150. PApplicationName = (byte*)appName,
  151. ApplicationVersion = 1,
  152. PEngineName = (byte*)appName,
  153. EngineVersion = 1,
  154. ApiVersion = MaximumVulkanVersion
  155. };
  156. var instanceCreateInfo = new InstanceCreateInfo
  157. {
  158. SType = StructureType.InstanceCreateInfo,
  159. PApplicationInfo = &applicationInfo,
  160. PpEnabledExtensionNames = null,
  161. PpEnabledLayerNames = null,
  162. EnabledExtensionCount = 0,
  163. EnabledLayerCount = 0
  164. };
  165. api.CreateInstance(in instanceCreateInfo, null, out var instance).ThrowOnError();
  166. // We ensure that vkEnumerateInstanceVersion is present (added in 1.1).
  167. // If the instance doesn't support it, no device is going to be 1.1 compatible.
  168. if (api.GetInstanceProcAddr(instance, "vkEnumerateInstanceVersion") == IntPtr.Zero)
  169. {
  170. api.DestroyInstance(instance, null);
  171. return Array.Empty<DeviceInfo>();
  172. }
  173. // We currently assume that the instance is compatible with Vulkan 1.2
  174. // TODO: Remove this once we relax our initialization codepaths.
  175. uint instanceApiVerison = 0;
  176. api.EnumerateInstanceVersion(ref instanceApiVerison).ThrowOnError();
  177. if (instanceApiVerison < MinimalInstanceVulkanVersion)
  178. {
  179. api.DestroyInstance(instance, null);
  180. return Array.Empty<DeviceInfo>();
  181. }
  182. Marshal.FreeHGlobal(appName);
  183. uint physicalDeviceCount;
  184. api.EnumeratePhysicalDevices(instance, &physicalDeviceCount, null).ThrowOnError();
  185. PhysicalDevice[] physicalDevices = new PhysicalDevice[physicalDeviceCount];
  186. fixed (PhysicalDevice* pPhysicalDevices = physicalDevices)
  187. {
  188. api.EnumeratePhysicalDevices(instance, &physicalDeviceCount, pPhysicalDevices).ThrowOnError();
  189. }
  190. DeviceInfo[] devices = new DeviceInfo[physicalDevices.Length];
  191. for (int i = 0; i < physicalDevices.Length; i++)
  192. {
  193. var physicalDevice = physicalDevices[i];
  194. api.GetPhysicalDeviceProperties(physicalDevice, out var properties);
  195. if (properties.ApiVersion < MinimalVulkanVersion)
  196. {
  197. continue;
  198. }
  199. devices[i] = new DeviceInfo(
  200. StringFromIdPair(properties.VendorID, properties.DeviceID),
  201. VendorUtils.GetNameFromId(properties.VendorID),
  202. Marshal.PtrToStringAnsi((IntPtr)properties.DeviceName),
  203. properties.DeviceType == PhysicalDeviceType.DiscreteGpu);
  204. }
  205. api.DestroyInstance(instance, null);
  206. return devices;
  207. }
  208. public static string StringFromIdPair(uint vendorId, uint deviceId)
  209. {
  210. return $"0x{vendorId:X}_0x{deviceId:X}";
  211. }
  212. private static bool IsPreferredAndSuitableDevice(Vk api, PhysicalDevice physicalDevice, SurfaceKHR surface, string preferredGpuId)
  213. {
  214. api.GetPhysicalDeviceProperties(physicalDevice, out var properties);
  215. if (StringFromIdPair(properties.VendorID, properties.DeviceID) != preferredGpuId)
  216. {
  217. return false;
  218. }
  219. return IsSuitableDevice(api, physicalDevice, surface);
  220. }
  221. private static bool IsSuitableDevice(Vk api, PhysicalDevice physicalDevice, SurfaceKHR surface)
  222. {
  223. int extensionMatches = 0;
  224. uint propertiesCount;
  225. api.EnumerateDeviceExtensionProperties(physicalDevice, (byte*)null, &propertiesCount, null).ThrowOnError();
  226. ExtensionProperties[] extensionProperties = new ExtensionProperties[propertiesCount];
  227. fixed (ExtensionProperties* pExtensionProperties = extensionProperties)
  228. {
  229. api.EnumerateDeviceExtensionProperties(physicalDevice, (byte*)null, &propertiesCount, pExtensionProperties).ThrowOnError();
  230. for (int i = 0; i < propertiesCount; i++)
  231. {
  232. string extensionName = Marshal.PtrToStringAnsi((IntPtr)pExtensionProperties[i].ExtensionName);
  233. if (_requiredExtensions.Contains(extensionName))
  234. {
  235. extensionMatches++;
  236. }
  237. }
  238. }
  239. return extensionMatches == _requiredExtensions.Length && FindSuitableQueueFamily(api, physicalDevice, surface, out _) != InvalidIndex;
  240. }
  241. internal static uint FindSuitableQueueFamily(Vk api, PhysicalDevice physicalDevice, SurfaceKHR surface, out uint queueCount)
  242. {
  243. const QueueFlags RequiredFlags = QueueFlags.GraphicsBit | QueueFlags.ComputeBit;
  244. var khrSurface = new KhrSurface(api.Context);
  245. uint propertiesCount;
  246. api.GetPhysicalDeviceQueueFamilyProperties(physicalDevice, &propertiesCount, null);
  247. QueueFamilyProperties[] properties = new QueueFamilyProperties[propertiesCount];
  248. fixed (QueueFamilyProperties* pProperties = properties)
  249. {
  250. api.GetPhysicalDeviceQueueFamilyProperties(physicalDevice, &propertiesCount, pProperties);
  251. }
  252. for (uint index = 0; index < propertiesCount; index++)
  253. {
  254. var queueFlags = properties[index].QueueFlags;
  255. khrSurface.GetPhysicalDeviceSurfaceSupport(physicalDevice, index, surface, out var surfaceSupported).ThrowOnError();
  256. if (queueFlags.HasFlag(RequiredFlags) && surfaceSupported)
  257. {
  258. queueCount = properties[index].QueueCount;
  259. return index;
  260. }
  261. }
  262. queueCount = 0;
  263. return InvalidIndex;
  264. }
  265. public static Device CreateDevice(Vk api, PhysicalDevice physicalDevice, uint queueFamilyIndex, string[] supportedExtensions, uint queueCount)
  266. {
  267. if (queueCount > QueuesCount)
  268. {
  269. queueCount = QueuesCount;
  270. }
  271. float* queuePriorities = stackalloc float[(int)queueCount];
  272. for (int i = 0; i < queueCount; i++)
  273. {
  274. queuePriorities[i] = 1f;
  275. }
  276. var queueCreateInfo = new DeviceQueueCreateInfo()
  277. {
  278. SType = StructureType.DeviceQueueCreateInfo,
  279. QueueFamilyIndex = queueFamilyIndex,
  280. QueueCount = queueCount,
  281. PQueuePriorities = queuePriorities
  282. };
  283. api.GetPhysicalDeviceProperties(physicalDevice, out var properties);
  284. bool useRobustBufferAccess = VendorUtils.FromId(properties.VendorID) == Vendor.Nvidia;
  285. PhysicalDeviceFeatures2 features2 = new PhysicalDeviceFeatures2()
  286. {
  287. SType = StructureType.PhysicalDeviceFeatures2
  288. };
  289. PhysicalDeviceVulkan11Features supportedFeaturesVk11 = new PhysicalDeviceVulkan11Features()
  290. {
  291. SType = StructureType.PhysicalDeviceVulkan11Features,
  292. PNext = features2.PNext
  293. };
  294. features2.PNext = &supportedFeaturesVk11;
  295. PhysicalDeviceCustomBorderColorFeaturesEXT supportedFeaturesCustomBorderColor = new PhysicalDeviceCustomBorderColorFeaturesEXT()
  296. {
  297. SType = StructureType.PhysicalDeviceCustomBorderColorFeaturesExt,
  298. PNext = features2.PNext
  299. };
  300. if (supportedExtensions.Contains("VK_EXT_custom_border_color"))
  301. {
  302. features2.PNext = &supportedFeaturesCustomBorderColor;
  303. }
  304. PhysicalDevicePrimitiveTopologyListRestartFeaturesEXT supportedFeaturesPrimitiveTopologyListRestart = new PhysicalDevicePrimitiveTopologyListRestartFeaturesEXT()
  305. {
  306. SType = StructureType.PhysicalDevicePrimitiveTopologyListRestartFeaturesExt,
  307. PNext = features2.PNext
  308. };
  309. if (supportedExtensions.Contains("VK_EXT_primitive_topology_list_restart"))
  310. {
  311. features2.PNext = &supportedFeaturesPrimitiveTopologyListRestart;
  312. }
  313. PhysicalDeviceTransformFeedbackFeaturesEXT supportedFeaturesTransformFeedback = new PhysicalDeviceTransformFeedbackFeaturesEXT()
  314. {
  315. SType = StructureType.PhysicalDeviceTransformFeedbackFeaturesExt,
  316. PNext = features2.PNext
  317. };
  318. if (supportedExtensions.Contains(ExtTransformFeedback.ExtensionName))
  319. {
  320. features2.PNext = &supportedFeaturesTransformFeedback;
  321. }
  322. PhysicalDeviceRobustness2FeaturesEXT supportedFeaturesRobustness2 = new PhysicalDeviceRobustness2FeaturesEXT()
  323. {
  324. SType = StructureType.PhysicalDeviceRobustness2FeaturesExt
  325. };
  326. if (supportedExtensions.Contains("VK_EXT_robustness2"))
  327. {
  328. supportedFeaturesRobustness2.PNext = features2.PNext;
  329. features2.PNext = &supportedFeaturesRobustness2;
  330. }
  331. api.GetPhysicalDeviceFeatures2(physicalDevice, &features2);
  332. var supportedFeatures = features2.Features;
  333. var features = new PhysicalDeviceFeatures()
  334. {
  335. DepthBiasClamp = true,
  336. DepthClamp = supportedFeatures.DepthClamp,
  337. DualSrcBlend = supportedFeatures.DualSrcBlend,
  338. FragmentStoresAndAtomics = true,
  339. GeometryShader = supportedFeatures.GeometryShader,
  340. ImageCubeArray = true,
  341. IndependentBlend = true,
  342. LogicOp = supportedFeatures.LogicOp,
  343. OcclusionQueryPrecise = supportedFeatures.OcclusionQueryPrecise,
  344. MultiViewport = supportedFeatures.MultiViewport,
  345. PipelineStatisticsQuery = supportedFeatures.PipelineStatisticsQuery,
  346. SamplerAnisotropy = true,
  347. ShaderClipDistance = true,
  348. ShaderFloat64 = supportedFeatures.ShaderFloat64,
  349. ShaderImageGatherExtended = supportedFeatures.ShaderImageGatherExtended,
  350. ShaderStorageImageMultisample = supportedFeatures.ShaderStorageImageMultisample,
  351. // ShaderStorageImageReadWithoutFormat = true,
  352. // ShaderStorageImageWriteWithoutFormat = true,
  353. TessellationShader = supportedFeatures.TessellationShader,
  354. VertexPipelineStoresAndAtomics = true,
  355. RobustBufferAccess = useRobustBufferAccess
  356. };
  357. void* pExtendedFeatures = null;
  358. PhysicalDeviceTransformFeedbackFeaturesEXT featuresTransformFeedback;
  359. if (supportedExtensions.Contains(ExtTransformFeedback.ExtensionName))
  360. {
  361. featuresTransformFeedback = new PhysicalDeviceTransformFeedbackFeaturesEXT()
  362. {
  363. SType = StructureType.PhysicalDeviceTransformFeedbackFeaturesExt,
  364. PNext = pExtendedFeatures,
  365. TransformFeedback = supportedFeaturesTransformFeedback.TransformFeedback
  366. };
  367. pExtendedFeatures = &featuresTransformFeedback;
  368. }
  369. PhysicalDevicePrimitiveTopologyListRestartFeaturesEXT featuresPrimitiveTopologyListRestart;
  370. if (supportedExtensions.Contains("VK_EXT_primitive_topology_list_restart"))
  371. {
  372. featuresPrimitiveTopologyListRestart = new PhysicalDevicePrimitiveTopologyListRestartFeaturesEXT()
  373. {
  374. SType = StructureType.PhysicalDevicePrimitiveTopologyListRestartFeaturesExt,
  375. PNext = pExtendedFeatures,
  376. PrimitiveTopologyListRestart = supportedFeaturesPrimitiveTopologyListRestart.PrimitiveTopologyListRestart,
  377. PrimitiveTopologyPatchListRestart = supportedFeaturesPrimitiveTopologyListRestart.PrimitiveTopologyPatchListRestart
  378. };
  379. pExtendedFeatures = &featuresPrimitiveTopologyListRestart;
  380. }
  381. PhysicalDeviceRobustness2FeaturesEXT featuresRobustness2;
  382. if (supportedExtensions.Contains("VK_EXT_robustness2"))
  383. {
  384. featuresRobustness2 = new PhysicalDeviceRobustness2FeaturesEXT()
  385. {
  386. SType = StructureType.PhysicalDeviceRobustness2FeaturesExt,
  387. PNext = pExtendedFeatures,
  388. NullDescriptor = supportedFeaturesRobustness2.NullDescriptor
  389. };
  390. pExtendedFeatures = &featuresRobustness2;
  391. }
  392. var featuresExtendedDynamicState = new PhysicalDeviceExtendedDynamicStateFeaturesEXT()
  393. {
  394. SType = StructureType.PhysicalDeviceExtendedDynamicStateFeaturesExt,
  395. PNext = pExtendedFeatures,
  396. ExtendedDynamicState = supportedExtensions.Contains(ExtExtendedDynamicState.ExtensionName)
  397. };
  398. pExtendedFeatures = &featuresExtendedDynamicState;
  399. var featuresVk11 = new PhysicalDeviceVulkan11Features()
  400. {
  401. SType = StructureType.PhysicalDeviceVulkan11Features,
  402. PNext = pExtendedFeatures,
  403. ShaderDrawParameters = supportedFeaturesVk11.ShaderDrawParameters
  404. };
  405. pExtendedFeatures = &featuresVk11;
  406. var featuresVk12 = new PhysicalDeviceVulkan12Features()
  407. {
  408. SType = StructureType.PhysicalDeviceVulkan12Features,
  409. PNext = pExtendedFeatures,
  410. DescriptorIndexing = supportedExtensions.Contains("VK_EXT_descriptor_indexing"),
  411. DrawIndirectCount = supportedExtensions.Contains(KhrDrawIndirectCount.ExtensionName),
  412. UniformBufferStandardLayout = supportedExtensions.Contains("VK_KHR_uniform_buffer_standard_layout")
  413. };
  414. pExtendedFeatures = &featuresVk12;
  415. PhysicalDeviceIndexTypeUint8FeaturesEXT featuresIndexU8;
  416. if (supportedExtensions.Contains("VK_EXT_index_type_uint8"))
  417. {
  418. featuresIndexU8 = new PhysicalDeviceIndexTypeUint8FeaturesEXT()
  419. {
  420. SType = StructureType.PhysicalDeviceIndexTypeUint8FeaturesExt,
  421. PNext = pExtendedFeatures,
  422. IndexTypeUint8 = true
  423. };
  424. pExtendedFeatures = &featuresIndexU8;
  425. }
  426. PhysicalDeviceFragmentShaderInterlockFeaturesEXT featuresFragmentShaderInterlock;
  427. if (supportedExtensions.Contains("VK_EXT_fragment_shader_interlock"))
  428. {
  429. featuresFragmentShaderInterlock = new PhysicalDeviceFragmentShaderInterlockFeaturesEXT()
  430. {
  431. SType = StructureType.PhysicalDeviceFragmentShaderInterlockFeaturesExt,
  432. PNext = pExtendedFeatures,
  433. FragmentShaderPixelInterlock = true
  434. };
  435. pExtendedFeatures = &featuresFragmentShaderInterlock;
  436. }
  437. PhysicalDeviceSubgroupSizeControlFeaturesEXT featuresSubgroupSizeControl;
  438. if (supportedExtensions.Contains("VK_EXT_subgroup_size_control"))
  439. {
  440. featuresSubgroupSizeControl = new PhysicalDeviceSubgroupSizeControlFeaturesEXT()
  441. {
  442. SType = StructureType.PhysicalDeviceSubgroupSizeControlFeaturesExt,
  443. PNext = pExtendedFeatures,
  444. SubgroupSizeControl = true
  445. };
  446. pExtendedFeatures = &featuresSubgroupSizeControl;
  447. }
  448. PhysicalDeviceCustomBorderColorFeaturesEXT featuresCustomBorderColor;
  449. if (supportedExtensions.Contains("VK_EXT_custom_border_color") &&
  450. supportedFeaturesCustomBorderColor.CustomBorderColors &&
  451. supportedFeaturesCustomBorderColor.CustomBorderColorWithoutFormat)
  452. {
  453. featuresCustomBorderColor = new PhysicalDeviceCustomBorderColorFeaturesEXT()
  454. {
  455. SType = StructureType.PhysicalDeviceCustomBorderColorFeaturesExt,
  456. PNext = pExtendedFeatures,
  457. CustomBorderColors = true,
  458. CustomBorderColorWithoutFormat = true,
  459. };
  460. pExtendedFeatures = &featuresCustomBorderColor;
  461. }
  462. var enabledExtensions = _requiredExtensions.Union(_desirableExtensions.Intersect(supportedExtensions)).ToArray();
  463. IntPtr* ppEnabledExtensions = stackalloc IntPtr[enabledExtensions.Length];
  464. for (int i = 0; i < enabledExtensions.Length; i++)
  465. {
  466. ppEnabledExtensions[i] = Marshal.StringToHGlobalAnsi(enabledExtensions[i]);
  467. }
  468. var deviceCreateInfo = new DeviceCreateInfo()
  469. {
  470. SType = StructureType.DeviceCreateInfo,
  471. PNext = pExtendedFeatures,
  472. QueueCreateInfoCount = 1,
  473. PQueueCreateInfos = &queueCreateInfo,
  474. PpEnabledExtensionNames = (byte**)ppEnabledExtensions,
  475. EnabledExtensionCount = (uint)enabledExtensions.Length,
  476. PEnabledFeatures = &features
  477. };
  478. api.CreateDevice(physicalDevice, in deviceCreateInfo, null, out var device).ThrowOnError();
  479. for (int i = 0; i < enabledExtensions.Length; i++)
  480. {
  481. Marshal.FreeHGlobal(ppEnabledExtensions[i]);
  482. }
  483. return device;
  484. }
  485. public static string[] GetSupportedExtensions(Vk api, PhysicalDevice physicalDevice)
  486. {
  487. uint propertiesCount;
  488. api.EnumerateDeviceExtensionProperties(physicalDevice, (byte*)null, &propertiesCount, null).ThrowOnError();
  489. ExtensionProperties[] extensionProperties = new ExtensionProperties[propertiesCount];
  490. fixed (ExtensionProperties* pExtensionProperties = extensionProperties)
  491. {
  492. api.EnumerateDeviceExtensionProperties(physicalDevice, (byte*)null, &propertiesCount, pExtensionProperties).ThrowOnError();
  493. }
  494. return extensionProperties.Select(x => Marshal.PtrToStringAnsi((IntPtr)x.ExtensionName)).ToArray();
  495. }
  496. }
  497. }