VulkanInitialization.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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 const string AppName = "Ryujinx.Graphics.Vulkan";
  17. private const int QueuesCount = 2;
  18. public static string[] DesirableExtensions { get; } = new string[]
  19. {
  20. ExtConditionalRendering.ExtensionName,
  21. ExtExtendedDynamicState.ExtensionName,
  22. KhrDrawIndirectCount.ExtensionName,
  23. KhrPushDescriptor.ExtensionName,
  24. "VK_EXT_custom_border_color",
  25. "VK_EXT_descriptor_indexing", // Enabling this works around an issue with disposed buffer bindings on RADV.
  26. "VK_EXT_fragment_shader_interlock",
  27. "VK_EXT_index_type_uint8",
  28. "VK_EXT_robustness2",
  29. "VK_KHR_shader_float16_int8",
  30. "VK_EXT_shader_subgroup_ballot",
  31. "VK_EXT_subgroup_size_control",
  32. "VK_NV_geometry_shader_passthrough"
  33. };
  34. public static string[] RequiredExtensions { get; } = new string[]
  35. {
  36. KhrSwapchain.ExtensionName,
  37. "VK_EXT_shader_subgroup_vote",
  38. ExtTransformFeedback.ExtensionName
  39. };
  40. private static string[] _excludedMessages = new string[]
  41. {
  42. // NOTE: Done on purpose right now.
  43. "UNASSIGNED-CoreValidation-Shader-OutputNotConsumed",
  44. // TODO: Figure out if fixable
  45. "VUID-vkCmdDrawIndexed-None-04584",
  46. // TODO: Might be worth looking into making this happy to possibly optimize copies.
  47. "UNASSIGNED-CoreValidation-DrawState-InvalidImageLayout",
  48. // TODO: Fix this, it's causing too much noise right now.
  49. "VUID-VkSubpassDependency-srcSubpass-00867"
  50. };
  51. internal static Instance CreateInstance(Vk api, GraphicsDebugLevel logLevel, string[] requiredExtensions, out ExtDebugUtils debugUtils, out DebugUtilsMessengerEXT debugUtilsMessenger)
  52. {
  53. var enabledLayers = new List<string>();
  54. void AddAvailableLayer(string layerName)
  55. {
  56. uint layerPropertiesCount;
  57. api.EnumerateInstanceLayerProperties(&layerPropertiesCount, null).ThrowOnError();
  58. LayerProperties[] layerProperties = new LayerProperties[layerPropertiesCount];
  59. fixed (LayerProperties* pLayerProperties = layerProperties)
  60. {
  61. api.EnumerateInstanceLayerProperties(&layerPropertiesCount, layerProperties).ThrowOnError();
  62. for (int i = 0; i < layerPropertiesCount; i++)
  63. {
  64. string currentLayerName = Marshal.PtrToStringAnsi((IntPtr)pLayerProperties[i].LayerName);
  65. if (currentLayerName == layerName)
  66. {
  67. enabledLayers.Add(layerName);
  68. return;
  69. }
  70. }
  71. }
  72. Logger.Warning?.Print(LogClass.Gpu, $"Missing layer {layerName}");
  73. }
  74. if (logLevel != GraphicsDebugLevel.None)
  75. {
  76. AddAvailableLayer("VK_LAYER_KHRONOS_validation");
  77. }
  78. var enabledExtensions = requiredExtensions.Append(ExtDebugUtils.ExtensionName).ToArray();
  79. var appName = Marshal.StringToHGlobalAnsi(AppName);
  80. var applicationInfo = new ApplicationInfo
  81. {
  82. PApplicationName = (byte*)appName,
  83. ApplicationVersion = 1,
  84. PEngineName = (byte*)appName,
  85. EngineVersion = 1,
  86. ApiVersion = Vk.Version12.Value
  87. };
  88. IntPtr* ppEnabledExtensions = stackalloc IntPtr[enabledExtensions.Length];
  89. IntPtr* ppEnabledLayers = stackalloc IntPtr[enabledLayers.Count];
  90. for (int i = 0; i < enabledExtensions.Length; i++)
  91. {
  92. ppEnabledExtensions[i] = Marshal.StringToHGlobalAnsi(enabledExtensions[i]);
  93. }
  94. for (int i = 0; i < enabledLayers.Count; i++)
  95. {
  96. ppEnabledLayers[i] = Marshal.StringToHGlobalAnsi(enabledLayers[i]);
  97. }
  98. var instanceCreateInfo = new InstanceCreateInfo
  99. {
  100. SType = StructureType.InstanceCreateInfo,
  101. PApplicationInfo = &applicationInfo,
  102. PpEnabledExtensionNames = (byte**)ppEnabledExtensions,
  103. PpEnabledLayerNames = (byte**)ppEnabledLayers,
  104. EnabledExtensionCount = (uint)enabledExtensions.Length,
  105. EnabledLayerCount = (uint)enabledLayers.Count
  106. };
  107. api.CreateInstance(in instanceCreateInfo, null, out var instance).ThrowOnError();
  108. Marshal.FreeHGlobal(appName);
  109. for (int i = 0; i < enabledExtensions.Length; i++)
  110. {
  111. Marshal.FreeHGlobal(ppEnabledExtensions[i]);
  112. }
  113. for (int i = 0; i < enabledLayers.Count; i++)
  114. {
  115. Marshal.FreeHGlobal(ppEnabledLayers[i]);
  116. }
  117. CreateDebugMessenger(api, logLevel, instance, out debugUtils, out debugUtilsMessenger);
  118. return instance;
  119. }
  120. private unsafe static uint DebugMessenger(
  121. DebugUtilsMessageSeverityFlagsEXT messageSeverity,
  122. DebugUtilsMessageTypeFlagsEXT messageTypes,
  123. DebugUtilsMessengerCallbackDataEXT* pCallbackData,
  124. void* pUserData)
  125. {
  126. var msg = Marshal.PtrToStringAnsi((IntPtr)pCallbackData->PMessage);
  127. foreach (string excludedMessagePart in _excludedMessages)
  128. {
  129. if (msg.Contains(excludedMessagePart))
  130. {
  131. return 0;
  132. }
  133. }
  134. if (messageSeverity.HasFlag(DebugUtilsMessageSeverityFlagsEXT.DebugUtilsMessageSeverityErrorBitExt))
  135. {
  136. Logger.Error?.Print(LogClass.Gpu, msg);
  137. //throw new Exception(msg);
  138. }
  139. else if (messageSeverity.HasFlag(DebugUtilsMessageSeverityFlagsEXT.DebugUtilsMessageSeverityWarningBitExt))
  140. {
  141. Logger.Warning?.Print(LogClass.Gpu, msg);
  142. }
  143. else if (messageSeverity.HasFlag(DebugUtilsMessageSeverityFlagsEXT.DebugUtilsMessageSeverityInfoBitExt))
  144. {
  145. Logger.Info?.Print(LogClass.Gpu, msg);
  146. }
  147. else // if (messageSeverity.HasFlag(DebugUtilsMessageSeverityFlagsEXT.DebugUtilsMessageSeverityVerboseBitExt))
  148. {
  149. Logger.Debug?.Print(LogClass.Gpu, msg);
  150. }
  151. return 0;
  152. }
  153. internal static PhysicalDevice FindSuitablePhysicalDevice(Vk api, Instance instance, SurfaceKHR surface, string preferredGpuId)
  154. {
  155. uint physicalDeviceCount;
  156. api.EnumeratePhysicalDevices(instance, &physicalDeviceCount, null).ThrowOnError();
  157. PhysicalDevice[] physicalDevices = new PhysicalDevice[physicalDeviceCount];
  158. fixed (PhysicalDevice* pPhysicalDevices = physicalDevices)
  159. {
  160. api.EnumeratePhysicalDevices(instance, &physicalDeviceCount, pPhysicalDevices).ThrowOnError();
  161. }
  162. // First we try to pick the the user preferred GPU.
  163. for (int i = 0; i < physicalDevices.Length; i++)
  164. {
  165. if (IsPreferredAndSuitableDevice(api, physicalDevices[i], surface, preferredGpuId))
  166. {
  167. return physicalDevices[i];
  168. }
  169. }
  170. // If we fail to do that, just use the first compatible GPU.
  171. for (int i = 0; i < physicalDevices.Length; i++)
  172. {
  173. if (IsSuitableDevice(api, physicalDevices[i], surface))
  174. {
  175. return physicalDevices[i];
  176. }
  177. }
  178. throw new VulkanException("Initialization failed, none of the available GPUs meets the minimum requirements.");
  179. }
  180. internal static DeviceInfo[] GetSuitablePhysicalDevices(Vk api)
  181. {
  182. var appName = Marshal.StringToHGlobalAnsi(AppName);
  183. var applicationInfo = new ApplicationInfo
  184. {
  185. PApplicationName = (byte*)appName,
  186. ApplicationVersion = 1,
  187. PEngineName = (byte*)appName,
  188. EngineVersion = 1,
  189. ApiVersion = Vk.Version12.Value
  190. };
  191. var instanceCreateInfo = new InstanceCreateInfo
  192. {
  193. SType = StructureType.InstanceCreateInfo,
  194. PApplicationInfo = &applicationInfo,
  195. PpEnabledExtensionNames = null,
  196. PpEnabledLayerNames = null,
  197. EnabledExtensionCount = 0,
  198. EnabledLayerCount = 0
  199. };
  200. api.CreateInstance(in instanceCreateInfo, null, out var instance).ThrowOnError();
  201. Marshal.FreeHGlobal(appName);
  202. uint physicalDeviceCount;
  203. api.EnumeratePhysicalDevices(instance, &physicalDeviceCount, null).ThrowOnError();
  204. PhysicalDevice[] physicalDevices = new PhysicalDevice[physicalDeviceCount];
  205. fixed (PhysicalDevice* pPhysicalDevices = physicalDevices)
  206. {
  207. api.EnumeratePhysicalDevices(instance, &physicalDeviceCount, pPhysicalDevices).ThrowOnError();
  208. }
  209. DeviceInfo[] devices = new DeviceInfo[physicalDevices.Length];
  210. for (int i = 0; i < physicalDevices.Length; i++)
  211. {
  212. var physicalDevice = physicalDevices[i];
  213. api.GetPhysicalDeviceProperties(physicalDevice, out var properties);
  214. devices[i] = new DeviceInfo(
  215. StringFromIdPair(properties.VendorID, properties.DeviceID),
  216. VendorUtils.GetNameFromId(properties.VendorID),
  217. Marshal.PtrToStringAnsi((IntPtr)properties.DeviceName),
  218. properties.DeviceType == PhysicalDeviceType.DiscreteGpu);
  219. }
  220. api.DestroyInstance(instance, null);
  221. return devices;
  222. }
  223. public static string StringFromIdPair(uint vendorId, uint deviceId)
  224. {
  225. return $"0x{vendorId:X}_0x{deviceId:X}";
  226. }
  227. private static bool IsPreferredAndSuitableDevice(Vk api, PhysicalDevice physicalDevice, SurfaceKHR surface, string preferredGpuId)
  228. {
  229. api.GetPhysicalDeviceProperties(physicalDevice, out var properties);
  230. if (StringFromIdPair(properties.VendorID, properties.DeviceID) != preferredGpuId)
  231. {
  232. return false;
  233. }
  234. return IsSuitableDevice(api, physicalDevice, surface);
  235. }
  236. private static bool IsSuitableDevice(Vk api, PhysicalDevice physicalDevice, SurfaceKHR surface)
  237. {
  238. int extensionMatches = 0;
  239. uint propertiesCount;
  240. api.EnumerateDeviceExtensionProperties(physicalDevice, (byte*)null, &propertiesCount, null).ThrowOnError();
  241. ExtensionProperties[] extensionProperties = new ExtensionProperties[propertiesCount];
  242. fixed (ExtensionProperties* pExtensionProperties = extensionProperties)
  243. {
  244. api.EnumerateDeviceExtensionProperties(physicalDevice, (byte*)null, &propertiesCount, pExtensionProperties).ThrowOnError();
  245. for (int i = 0; i < propertiesCount; i++)
  246. {
  247. string extensionName = Marshal.PtrToStringAnsi((IntPtr)pExtensionProperties[i].ExtensionName);
  248. if (RequiredExtensions.Contains(extensionName))
  249. {
  250. extensionMatches++;
  251. }
  252. }
  253. }
  254. return extensionMatches == RequiredExtensions.Length && FindSuitableQueueFamily(api, physicalDevice, surface, out _) != InvalidIndex;
  255. }
  256. internal static uint FindSuitableQueueFamily(Vk api, PhysicalDevice physicalDevice, SurfaceKHR surface, out uint queueCount)
  257. {
  258. const QueueFlags RequiredFlags = QueueFlags.QueueGraphicsBit | QueueFlags.QueueComputeBit;
  259. var khrSurface = new KhrSurface(api.Context);
  260. uint propertiesCount;
  261. api.GetPhysicalDeviceQueueFamilyProperties(physicalDevice, &propertiesCount, null);
  262. QueueFamilyProperties[] properties = new QueueFamilyProperties[propertiesCount];
  263. fixed (QueueFamilyProperties* pProperties = properties)
  264. {
  265. api.GetPhysicalDeviceQueueFamilyProperties(physicalDevice, &propertiesCount, pProperties);
  266. }
  267. for (uint index = 0; index < propertiesCount; index++)
  268. {
  269. var queueFlags = properties[index].QueueFlags;
  270. khrSurface.GetPhysicalDeviceSurfaceSupport(physicalDevice, index, surface, out var surfaceSupported).ThrowOnError();
  271. if (queueFlags.HasFlag(RequiredFlags) && surfaceSupported)
  272. {
  273. queueCount = properties[index].QueueCount;
  274. return index;
  275. }
  276. }
  277. queueCount = 0;
  278. return InvalidIndex;
  279. }
  280. public static Device CreateDevice(Vk api, PhysicalDevice physicalDevice, uint queueFamilyIndex, string[] supportedExtensions, uint queueCount)
  281. {
  282. if (queueCount > QueuesCount)
  283. {
  284. queueCount = QueuesCount;
  285. }
  286. float* queuePriorities = stackalloc float[(int)queueCount];
  287. for (int i = 0; i < queueCount; i++)
  288. {
  289. queuePriorities[i] = 1f;
  290. }
  291. var queueCreateInfo = new DeviceQueueCreateInfo()
  292. {
  293. SType = StructureType.DeviceQueueCreateInfo,
  294. QueueFamilyIndex = queueFamilyIndex,
  295. QueueCount = queueCount,
  296. PQueuePriorities = queuePriorities
  297. };
  298. api.GetPhysicalDeviceProperties(physicalDevice, out var properties);
  299. bool useRobustBufferAccess = VendorUtils.FromId(properties.VendorID) == Vendor.Nvidia;
  300. var supportedFeatures = api.GetPhysicalDeviceFeature(physicalDevice);
  301. var features = new PhysicalDeviceFeatures()
  302. {
  303. DepthBiasClamp = true,
  304. DepthClamp = true,
  305. DualSrcBlend = true,
  306. FragmentStoresAndAtomics = true,
  307. GeometryShader = true,
  308. ImageCubeArray = true,
  309. IndependentBlend = true,
  310. LogicOp = true,
  311. MultiViewport = true,
  312. PipelineStatisticsQuery = true,
  313. SamplerAnisotropy = true,
  314. ShaderClipDistance = true,
  315. ShaderFloat64 = supportedFeatures.ShaderFloat64,
  316. ShaderImageGatherExtended = true,
  317. ShaderStorageImageMultisample = supportedFeatures.ShaderStorageImageMultisample,
  318. // ShaderStorageImageReadWithoutFormat = true,
  319. // ShaderStorageImageWriteWithoutFormat = true,
  320. TessellationShader = true,
  321. VertexPipelineStoresAndAtomics = true,
  322. RobustBufferAccess = useRobustBufferAccess
  323. };
  324. void* pExtendedFeatures = null;
  325. var featuresTransformFeedback = new PhysicalDeviceTransformFeedbackFeaturesEXT()
  326. {
  327. SType = StructureType.PhysicalDeviceTransformFeedbackFeaturesExt,
  328. PNext = pExtendedFeatures,
  329. TransformFeedback = true
  330. };
  331. pExtendedFeatures = &featuresTransformFeedback;
  332. var featuresRobustness2 = new PhysicalDeviceRobustness2FeaturesEXT()
  333. {
  334. SType = StructureType.PhysicalDeviceRobustness2FeaturesExt,
  335. PNext = pExtendedFeatures,
  336. NullDescriptor = true
  337. };
  338. pExtendedFeatures = &featuresRobustness2;
  339. var featuresExtendedDynamicState = new PhysicalDeviceExtendedDynamicStateFeaturesEXT()
  340. {
  341. SType = StructureType.PhysicalDeviceExtendedDynamicStateFeaturesExt,
  342. PNext = pExtendedFeatures,
  343. ExtendedDynamicState = supportedExtensions.Contains(ExtExtendedDynamicState.ExtensionName)
  344. };
  345. pExtendedFeatures = &featuresExtendedDynamicState;
  346. var featuresVk11 = new PhysicalDeviceVulkan11Features()
  347. {
  348. SType = StructureType.PhysicalDeviceVulkan11Features,
  349. PNext = pExtendedFeatures,
  350. ShaderDrawParameters = true
  351. };
  352. pExtendedFeatures = &featuresVk11;
  353. var featuresVk12 = new PhysicalDeviceVulkan12Features()
  354. {
  355. SType = StructureType.PhysicalDeviceVulkan12Features,
  356. PNext = pExtendedFeatures,
  357. DescriptorIndexing = supportedExtensions.Contains("VK_EXT_descriptor_indexing"),
  358. DrawIndirectCount = supportedExtensions.Contains(KhrDrawIndirectCount.ExtensionName)
  359. };
  360. pExtendedFeatures = &featuresVk12;
  361. PhysicalDeviceIndexTypeUint8FeaturesEXT featuresIndexU8;
  362. if (supportedExtensions.Contains("VK_EXT_index_type_uint8"))
  363. {
  364. featuresIndexU8 = new PhysicalDeviceIndexTypeUint8FeaturesEXT()
  365. {
  366. SType = StructureType.PhysicalDeviceIndexTypeUint8FeaturesExt,
  367. PNext = pExtendedFeatures,
  368. IndexTypeUint8 = true
  369. };
  370. pExtendedFeatures = &featuresIndexU8;
  371. }
  372. PhysicalDeviceFragmentShaderInterlockFeaturesEXT featuresFragmentShaderInterlock;
  373. if (supportedExtensions.Contains("VK_EXT_fragment_shader_interlock"))
  374. {
  375. featuresFragmentShaderInterlock = new PhysicalDeviceFragmentShaderInterlockFeaturesEXT()
  376. {
  377. SType = StructureType.PhysicalDeviceFragmentShaderInterlockFeaturesExt,
  378. PNext = pExtendedFeatures,
  379. FragmentShaderPixelInterlock = true
  380. };
  381. pExtendedFeatures = &featuresFragmentShaderInterlock;
  382. }
  383. PhysicalDeviceSubgroupSizeControlFeaturesEXT featuresSubgroupSizeControl;
  384. if (supportedExtensions.Contains("VK_EXT_subgroup_size_control"))
  385. {
  386. featuresSubgroupSizeControl = new PhysicalDeviceSubgroupSizeControlFeaturesEXT()
  387. {
  388. SType = StructureType.PhysicalDeviceSubgroupSizeControlFeaturesExt,
  389. PNext = pExtendedFeatures,
  390. SubgroupSizeControl = true
  391. };
  392. pExtendedFeatures = &featuresSubgroupSizeControl;
  393. }
  394. var enabledExtensions = RequiredExtensions.Union(DesirableExtensions.Intersect(supportedExtensions)).ToArray();
  395. IntPtr* ppEnabledExtensions = stackalloc IntPtr[enabledExtensions.Length];
  396. for (int i = 0; i < enabledExtensions.Length; i++)
  397. {
  398. ppEnabledExtensions[i] = Marshal.StringToHGlobalAnsi(enabledExtensions[i]);
  399. }
  400. var deviceCreateInfo = new DeviceCreateInfo()
  401. {
  402. SType = StructureType.DeviceCreateInfo,
  403. PNext = pExtendedFeatures,
  404. QueueCreateInfoCount = 1,
  405. PQueueCreateInfos = &queueCreateInfo,
  406. PpEnabledExtensionNames = (byte**)ppEnabledExtensions,
  407. EnabledExtensionCount = (uint)enabledExtensions.Length,
  408. PEnabledFeatures = &features
  409. };
  410. api.CreateDevice(physicalDevice, in deviceCreateInfo, null, out var device).ThrowOnError();
  411. for (int i = 0; i < enabledExtensions.Length; i++)
  412. {
  413. Marshal.FreeHGlobal(ppEnabledExtensions[i]);
  414. }
  415. return device;
  416. }
  417. public static string[] GetSupportedExtensions(Vk api, PhysicalDevice physicalDevice)
  418. {
  419. uint propertiesCount;
  420. api.EnumerateDeviceExtensionProperties(physicalDevice, (byte*)null, &propertiesCount, null).ThrowOnError();
  421. ExtensionProperties[] extensionProperties = new ExtensionProperties[propertiesCount];
  422. fixed (ExtensionProperties* pExtensionProperties = extensionProperties)
  423. {
  424. api.EnumerateDeviceExtensionProperties(physicalDevice, (byte*)null, &propertiesCount, pExtensionProperties).ThrowOnError();
  425. }
  426. return extensionProperties.Select(x => Marshal.PtrToStringAnsi((IntPtr)x.ExtensionName)).ToArray();
  427. }
  428. internal static CommandBufferPool CreateCommandBufferPool(Vk api, Device device, Queue queue, object queueLock, uint queueFamilyIndex)
  429. {
  430. return new CommandBufferPool(api, device, queue, queueLock, queueFamilyIndex);
  431. }
  432. internal unsafe static void CreateDebugMessenger(
  433. Vk api,
  434. GraphicsDebugLevel logLevel,
  435. Instance instance,
  436. out ExtDebugUtils debugUtils,
  437. out DebugUtilsMessengerEXT debugUtilsMessenger)
  438. {
  439. debugUtils = default;
  440. if (logLevel != GraphicsDebugLevel.None)
  441. {
  442. if (!api.TryGetInstanceExtension(instance, out debugUtils))
  443. {
  444. debugUtilsMessenger = default;
  445. return;
  446. }
  447. var filterLogType = logLevel switch
  448. {
  449. GraphicsDebugLevel.Error => DebugUtilsMessageTypeFlagsEXT.DebugUtilsMessageTypeValidationBitExt,
  450. GraphicsDebugLevel.Slowdowns => DebugUtilsMessageTypeFlagsEXT.DebugUtilsMessageTypeValidationBitExt |
  451. DebugUtilsMessageTypeFlagsEXT.DebugUtilsMessageTypePerformanceBitExt,
  452. GraphicsDebugLevel.All => DebugUtilsMessageTypeFlagsEXT.DebugUtilsMessageTypeGeneralBitExt |
  453. DebugUtilsMessageTypeFlagsEXT.DebugUtilsMessageTypeValidationBitExt |
  454. DebugUtilsMessageTypeFlagsEXT.DebugUtilsMessageTypePerformanceBitExt,
  455. _ => throw new ArgumentException($"Invalid log level \"{logLevel}\".")
  456. };
  457. var filterLogSeverity = logLevel switch
  458. {
  459. GraphicsDebugLevel.Error => DebugUtilsMessageSeverityFlagsEXT.DebugUtilsMessageSeverityErrorBitExt,
  460. GraphicsDebugLevel.Slowdowns => DebugUtilsMessageSeverityFlagsEXT.DebugUtilsMessageSeverityErrorBitExt |
  461. DebugUtilsMessageSeverityFlagsEXT.DebugUtilsMessageSeverityWarningBitExt,
  462. GraphicsDebugLevel.All => DebugUtilsMessageSeverityFlagsEXT.DebugUtilsMessageSeverityInfoBitExt |
  463. DebugUtilsMessageSeverityFlagsEXT.DebugUtilsMessageSeverityWarningBitExt |
  464. DebugUtilsMessageSeverityFlagsEXT.DebugUtilsMessageSeverityVerboseBitExt |
  465. DebugUtilsMessageSeverityFlagsEXT.DebugUtilsMessageSeverityErrorBitExt,
  466. _ => throw new ArgumentException($"Invalid log level \"{logLevel}\".")
  467. };
  468. var debugUtilsMessengerCreateInfo = new DebugUtilsMessengerCreateInfoEXT()
  469. {
  470. SType = StructureType.DebugUtilsMessengerCreateInfoExt,
  471. MessageType = filterLogType,
  472. MessageSeverity = filterLogSeverity,
  473. PfnUserCallback = new PfnDebugUtilsMessengerCallbackEXT(DebugMessenger)
  474. };
  475. debugUtils.CreateDebugUtilsMessenger(instance, in debugUtilsMessengerCreateInfo, null, out debugUtilsMessenger).ThrowOnError();
  476. }
  477. else
  478. {
  479. debugUtilsMessenger = default;
  480. }
  481. }
  482. }
  483. }