VulkanRenderer.cs 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  1. using Ryujinx.Common.Configuration;
  2. using Ryujinx.Common.Logging;
  3. using Ryujinx.Graphics.GAL;
  4. using Ryujinx.Graphics.Shader;
  5. using Ryujinx.Graphics.Shader.Translation;
  6. using Ryujinx.Graphics.Vulkan.MoltenVK;
  7. using Ryujinx.Graphics.Vulkan.Queries;
  8. using Silk.NET.Vulkan;
  9. using Silk.NET.Vulkan.Extensions.EXT;
  10. using Silk.NET.Vulkan.Extensions.KHR;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Runtime.InteropServices;
  14. using Format = Ryujinx.Graphics.GAL.Format;
  15. using PrimitiveTopology = Ryujinx.Graphics.GAL.PrimitiveTopology;
  16. using SamplerCreateInfo = Ryujinx.Graphics.GAL.SamplerCreateInfo;
  17. namespace Ryujinx.Graphics.Vulkan
  18. {
  19. public sealed class VulkanRenderer : IRenderer
  20. {
  21. private VulkanInstance _instance;
  22. private SurfaceKHR _surface;
  23. private VulkanPhysicalDevice _physicalDevice;
  24. private Device _device;
  25. private WindowBase _window;
  26. private bool _initialized;
  27. public uint ProgramCount { get; set; } = 0;
  28. internal FormatCapabilities FormatCapabilities { get; private set; }
  29. internal HardwareCapabilities Capabilities;
  30. internal Vk Api { get; private set; }
  31. internal KhrSurface SurfaceApi { get; private set; }
  32. internal KhrSwapchain SwapchainApi { get; private set; }
  33. internal ExtConditionalRendering ConditionalRenderingApi { get; private set; }
  34. internal ExtExtendedDynamicState ExtendedDynamicStateApi { get; private set; }
  35. internal KhrPushDescriptor PushDescriptorApi { get; private set; }
  36. internal ExtTransformFeedback TransformFeedbackApi { get; private set; }
  37. internal KhrDrawIndirectCount DrawIndirectCountApi { get; private set; }
  38. internal ExtAttachmentFeedbackLoopDynamicState DynamicFeedbackLoopApi { get; private set; }
  39. internal uint QueueFamilyIndex { get; private set; }
  40. internal Queue Queue { get; private set; }
  41. internal Queue BackgroundQueue { get; private set; }
  42. internal object BackgroundQueueLock { get; private set; }
  43. internal object QueueLock { get; private set; }
  44. internal MemoryAllocator MemoryAllocator { get; private set; }
  45. internal HostMemoryAllocator HostMemoryAllocator { get; private set; }
  46. internal CommandBufferPool CommandBufferPool { get; private set; }
  47. internal PipelineLayoutCache PipelineLayoutCache { get; private set; }
  48. internal BackgroundResources BackgroundResources { get; private set; }
  49. internal Action<Action> InterruptAction { get; private set; }
  50. internal SyncManager SyncManager { get; private set; }
  51. internal BufferManager BufferManager { get; private set; }
  52. internal HashSet<ShaderCollection> Shaders { get; }
  53. internal HashSet<ITexture> Textures { get; }
  54. internal HashSet<SamplerHolder> Samplers { get; }
  55. private VulkanDebugMessenger _debugMessenger;
  56. private Counters _counters;
  57. private PipelineFull _pipeline;
  58. internal HelperShader HelperShader { get; private set; }
  59. internal PipelineFull PipelineInternal => _pipeline;
  60. internal BarrierBatch Barriers { get; private set; }
  61. public IPipeline Pipeline => _pipeline;
  62. public IWindow Window => _window;
  63. private readonly Func<Instance, Vk, SurfaceKHR> _getSurface;
  64. private readonly Func<string[]> _getRequiredExtensions;
  65. private readonly string _preferredGpuId;
  66. private int[] _pdReservedBindings;
  67. private readonly static int[] _pdReservedBindingsNvn = { 3, 18, 21, 36, 30 };
  68. private readonly static int[] _pdReservedBindingsOgl = { 17, 18, 34, 35, 36 };
  69. internal Vendor Vendor { get; private set; }
  70. internal bool IsAmdWindows { get; private set; }
  71. internal bool IsIntelWindows { get; private set; }
  72. internal bool IsAmdGcn { get; private set; }
  73. internal bool IsNvidiaPreTuring { get; private set; }
  74. internal bool IsIntelArc { get; private set; }
  75. internal bool IsQualcommProprietary { get; private set; }
  76. internal bool IsMoltenVk { get; private set; }
  77. internal bool IsTBDR { get; private set; }
  78. internal bool IsSharedMemory { get; private set; }
  79. public string GpuVendor { get; private set; }
  80. public string GpuDriver { get; private set; }
  81. public string GpuRenderer { get; private set; }
  82. public string GpuVersion { get; private set; }
  83. public bool PreferThreading => true;
  84. public event EventHandler<ScreenCaptureImageInfo> ScreenCaptured;
  85. public VulkanRenderer(Vk api, Func<Instance, Vk, SurfaceKHR> getSurface, Func<string[]> requiredExtensionsFunc, string preferredGpuId)
  86. {
  87. _getSurface = getSurface;
  88. _getRequiredExtensions = requiredExtensionsFunc;
  89. _preferredGpuId = preferredGpuId;
  90. Api = api;
  91. Shaders = [];
  92. Textures = [];
  93. Samplers = [];
  94. // Any device running on MacOS is using MoltenVK, even Intel and AMD vendors.
  95. if (IsMoltenVk = OperatingSystem.IsMacOS())
  96. MVKInitialization.Initialize();
  97. }
  98. public static VulkanRenderer Create(
  99. string preferredGpuId,
  100. Func<Instance, Vk, SurfaceKHR> getSurface,
  101. Func<string[]> getRequiredExtensions
  102. ) => new(Vk.GetApi(), getSurface, getRequiredExtensions, preferredGpuId);
  103. private unsafe void LoadFeatures(uint maxQueueCount, uint queueFamilyIndex)
  104. {
  105. FormatCapabilities = new FormatCapabilities(Api, _physicalDevice.PhysicalDevice);
  106. if (Api.TryGetDeviceExtension(_instance.Instance, _device, out ExtConditionalRendering conditionalRenderingApi))
  107. {
  108. ConditionalRenderingApi = conditionalRenderingApi;
  109. }
  110. if (Api.TryGetDeviceExtension(_instance.Instance, _device, out ExtExtendedDynamicState extendedDynamicStateApi))
  111. {
  112. ExtendedDynamicStateApi = extendedDynamicStateApi;
  113. }
  114. if (Api.TryGetDeviceExtension(_instance.Instance, _device, out KhrPushDescriptor pushDescriptorApi))
  115. {
  116. PushDescriptorApi = pushDescriptorApi;
  117. }
  118. if (Api.TryGetDeviceExtension(_instance.Instance, _device, out ExtTransformFeedback transformFeedbackApi))
  119. {
  120. TransformFeedbackApi = transformFeedbackApi;
  121. }
  122. if (Api.TryGetDeviceExtension(_instance.Instance, _device, out KhrDrawIndirectCount drawIndirectCountApi))
  123. {
  124. DrawIndirectCountApi = drawIndirectCountApi;
  125. }
  126. if (Api.TryGetDeviceExtension(_instance.Instance, _device, out ExtAttachmentFeedbackLoopDynamicState dynamicFeedbackLoopApi))
  127. {
  128. DynamicFeedbackLoopApi = dynamicFeedbackLoopApi;
  129. }
  130. if (maxQueueCount >= 2)
  131. {
  132. Api.GetDeviceQueue(_device, queueFamilyIndex, 1, out var backgroundQueue);
  133. BackgroundQueue = backgroundQueue;
  134. BackgroundQueueLock = new object();
  135. }
  136. PhysicalDeviceProperties2 properties2 = new()
  137. {
  138. SType = StructureType.PhysicalDeviceProperties2,
  139. };
  140. PhysicalDeviceSubgroupProperties propertiesSubgroup = new()
  141. {
  142. SType = StructureType.PhysicalDeviceSubgroupProperties,
  143. PNext = properties2.PNext,
  144. };
  145. properties2.PNext = &propertiesSubgroup;
  146. PhysicalDeviceBlendOperationAdvancedPropertiesEXT propertiesBlendOperationAdvanced = new()
  147. {
  148. SType = StructureType.PhysicalDeviceBlendOperationAdvancedPropertiesExt,
  149. };
  150. bool supportsBlendOperationAdvanced = _physicalDevice.IsDeviceExtensionPresent("VK_EXT_blend_operation_advanced");
  151. if (supportsBlendOperationAdvanced)
  152. {
  153. propertiesBlendOperationAdvanced.PNext = properties2.PNext;
  154. properties2.PNext = &propertiesBlendOperationAdvanced;
  155. }
  156. bool supportsTransformFeedback = _physicalDevice.IsDeviceExtensionPresent(ExtTransformFeedback.ExtensionName);
  157. PhysicalDeviceTransformFeedbackPropertiesEXT propertiesTransformFeedback = new()
  158. {
  159. SType = StructureType.PhysicalDeviceTransformFeedbackPropertiesExt,
  160. };
  161. if (supportsTransformFeedback)
  162. {
  163. propertiesTransformFeedback.PNext = properties2.PNext;
  164. properties2.PNext = &propertiesTransformFeedback;
  165. }
  166. PhysicalDevicePortabilitySubsetPropertiesKHR propertiesPortabilitySubset = new()
  167. {
  168. SType = StructureType.PhysicalDevicePortabilitySubsetPropertiesKhr,
  169. };
  170. bool supportsPushDescriptors = _physicalDevice.IsDeviceExtensionPresent(KhrPushDescriptor.ExtensionName);
  171. PhysicalDevicePushDescriptorPropertiesKHR propertiesPushDescriptor = new PhysicalDevicePushDescriptorPropertiesKHR()
  172. {
  173. SType = StructureType.PhysicalDevicePushDescriptorPropertiesKhr
  174. };
  175. if (supportsPushDescriptors)
  176. {
  177. propertiesPushDescriptor.PNext = properties2.PNext;
  178. properties2.PNext = &propertiesPushDescriptor;
  179. }
  180. PhysicalDeviceFeatures2 features2 = new()
  181. {
  182. SType = StructureType.PhysicalDeviceFeatures2,
  183. };
  184. PhysicalDevicePrimitiveTopologyListRestartFeaturesEXT featuresPrimitiveTopologyListRestart = new()
  185. {
  186. SType = StructureType.PhysicalDevicePrimitiveTopologyListRestartFeaturesExt,
  187. };
  188. PhysicalDeviceRobustness2FeaturesEXT featuresRobustness2 = new()
  189. {
  190. SType = StructureType.PhysicalDeviceRobustness2FeaturesExt,
  191. };
  192. PhysicalDeviceShaderFloat16Int8FeaturesKHR featuresShaderInt8 = new()
  193. {
  194. SType = StructureType.PhysicalDeviceShaderFloat16Int8Features,
  195. };
  196. PhysicalDeviceCustomBorderColorFeaturesEXT featuresCustomBorderColor = new()
  197. {
  198. SType = StructureType.PhysicalDeviceCustomBorderColorFeaturesExt,
  199. };
  200. PhysicalDeviceDepthClipControlFeaturesEXT featuresDepthClipControl = new()
  201. {
  202. SType = StructureType.PhysicalDeviceDepthClipControlFeaturesExt,
  203. };
  204. PhysicalDeviceAttachmentFeedbackLoopLayoutFeaturesEXT featuresAttachmentFeedbackLoop = new()
  205. {
  206. SType = StructureType.PhysicalDeviceAttachmentFeedbackLoopLayoutFeaturesExt,
  207. };
  208. PhysicalDeviceAttachmentFeedbackLoopDynamicStateFeaturesEXT featuresDynamicAttachmentFeedbackLoop = new()
  209. {
  210. SType = StructureType.PhysicalDeviceAttachmentFeedbackLoopDynamicStateFeaturesExt,
  211. };
  212. PhysicalDevicePortabilitySubsetFeaturesKHR featuresPortabilitySubset = new()
  213. {
  214. SType = StructureType.PhysicalDevicePortabilitySubsetFeaturesKhr,
  215. };
  216. if (_physicalDevice.IsDeviceExtensionPresent("VK_EXT_primitive_topology_list_restart"))
  217. {
  218. features2.PNext = &featuresPrimitiveTopologyListRestart;
  219. }
  220. if (_physicalDevice.IsDeviceExtensionPresent("VK_EXT_robustness2"))
  221. {
  222. featuresRobustness2.PNext = features2.PNext;
  223. features2.PNext = &featuresRobustness2;
  224. }
  225. if (_physicalDevice.IsDeviceExtensionPresent("VK_KHR_shader_float16_int8"))
  226. {
  227. featuresShaderInt8.PNext = features2.PNext;
  228. features2.PNext = &featuresShaderInt8;
  229. }
  230. if (_physicalDevice.IsDeviceExtensionPresent("VK_EXT_custom_border_color"))
  231. {
  232. featuresCustomBorderColor.PNext = features2.PNext;
  233. features2.PNext = &featuresCustomBorderColor;
  234. }
  235. bool supportsDepthClipControl = _physicalDevice.IsDeviceExtensionPresent("VK_EXT_depth_clip_control");
  236. if (supportsDepthClipControl)
  237. {
  238. featuresDepthClipControl.PNext = features2.PNext;
  239. features2.PNext = &featuresDepthClipControl;
  240. }
  241. bool supportsAttachmentFeedbackLoop = _physicalDevice.IsDeviceExtensionPresent("VK_EXT_attachment_feedback_loop_layout");
  242. if (supportsAttachmentFeedbackLoop)
  243. {
  244. featuresAttachmentFeedbackLoop.PNext = features2.PNext;
  245. features2.PNext = &featuresAttachmentFeedbackLoop;
  246. }
  247. bool supportsDynamicAttachmentFeedbackLoop = _physicalDevice.IsDeviceExtensionPresent("VK_EXT_attachment_feedback_loop_dynamic_state");
  248. if (supportsDynamicAttachmentFeedbackLoop)
  249. {
  250. featuresDynamicAttachmentFeedbackLoop.PNext = features2.PNext;
  251. features2.PNext = &featuresDynamicAttachmentFeedbackLoop;
  252. }
  253. bool usePortability = _physicalDevice.IsDeviceExtensionPresent("VK_KHR_portability_subset");
  254. if (usePortability)
  255. {
  256. propertiesPortabilitySubset.PNext = properties2.PNext;
  257. properties2.PNext = &propertiesPortabilitySubset;
  258. featuresPortabilitySubset.PNext = features2.PNext;
  259. features2.PNext = &featuresPortabilitySubset;
  260. }
  261. Api.GetPhysicalDeviceProperties2(_physicalDevice.PhysicalDevice, &properties2);
  262. Api.GetPhysicalDeviceFeatures2(_physicalDevice.PhysicalDevice, &features2);
  263. var portabilityFlags = PortabilitySubsetFlags.None;
  264. uint vertexBufferAlignment = 1;
  265. if (usePortability)
  266. {
  267. vertexBufferAlignment = propertiesPortabilitySubset.MinVertexInputBindingStrideAlignment;
  268. portabilityFlags |= featuresPortabilitySubset.TriangleFans ? 0 : PortabilitySubsetFlags.NoTriangleFans;
  269. portabilityFlags |= featuresPortabilitySubset.PointPolygons ? 0 : PortabilitySubsetFlags.NoPointMode;
  270. portabilityFlags |= featuresPortabilitySubset.ImageView2DOn3DImage ? 0 : PortabilitySubsetFlags.No3DImageView;
  271. portabilityFlags |= featuresPortabilitySubset.SamplerMipLodBias ? 0 : PortabilitySubsetFlags.NoLodBias;
  272. }
  273. bool supportsCustomBorderColor = _physicalDevice.IsDeviceExtensionPresent("VK_EXT_custom_border_color") &&
  274. featuresCustomBorderColor.CustomBorderColors &&
  275. featuresCustomBorderColor.CustomBorderColorWithoutFormat;
  276. ref var properties = ref properties2.Properties;
  277. var hasDriverProperties = _physicalDevice.TryGetPhysicalDeviceDriverPropertiesKHR(Api, out var driverProperties);
  278. Vendor = VendorUtils.FromId(properties.VendorID);
  279. IsAmdWindows = Vendor == Vendor.Amd && OperatingSystem.IsWindows();
  280. IsIntelWindows = Vendor == Vendor.Intel && OperatingSystem.IsWindows();
  281. IsTBDR =
  282. Vendor == Vendor.Apple ||
  283. Vendor == Vendor.Qualcomm ||
  284. Vendor == Vendor.ARM ||
  285. Vendor == Vendor.Broadcom ||
  286. Vendor == Vendor.ImgTec;
  287. GpuVendor = VendorUtils.GetNameFromId(properties.VendorID);
  288. GpuDriver = hasDriverProperties && !OperatingSystem.IsMacOS() ?
  289. VendorUtils.GetFriendlyDriverName(driverProperties.DriverID) : GpuVendor; // Fallback to vendor name if driver is unavailable or on MacOS where vendor is preferred.
  290. fixed (byte* deviceName = properties.DeviceName)
  291. {
  292. GpuRenderer = Marshal.PtrToStringAnsi((nint)deviceName);
  293. }
  294. GpuVersion = $"Vulkan v{ParseStandardVulkanVersion(properties.ApiVersion)}, Driver v{ParseDriverVersion(ref properties)}";
  295. IsAmdGcn = !IsMoltenVk && Vendor == Vendor.Amd && VendorUtils.AmdGcnRegex().IsMatch(GpuRenderer);
  296. if (Vendor == Vendor.Nvidia)
  297. {
  298. var match = VendorUtils.NvidiaConsumerClassRegex().Match(GpuRenderer);
  299. if (match != null && int.TryParse(match.Groups[2].Value, out int gpuNumber))
  300. {
  301. IsNvidiaPreTuring = gpuNumber < 2000;
  302. }
  303. else if (GpuRenderer.Contains("TITAN") && !GpuRenderer.Contains("RTX"))
  304. {
  305. IsNvidiaPreTuring = true;
  306. }
  307. }
  308. else if (Vendor == Vendor.Intel)
  309. {
  310. IsIntelArc = GpuRenderer.StartsWith("Intel(R) Arc(TM)");
  311. }
  312. IsQualcommProprietary = hasDriverProperties && driverProperties.DriverID == DriverId.QualcommProprietary;
  313. ulong minResourceAlignment = Math.Max(
  314. Math.Max(
  315. properties.Limits.MinStorageBufferOffsetAlignment,
  316. properties.Limits.MinUniformBufferOffsetAlignment),
  317. properties.Limits.MinTexelBufferOffsetAlignment
  318. );
  319. SampleCountFlags supportedSampleCounts =
  320. properties.Limits.FramebufferColorSampleCounts &
  321. properties.Limits.FramebufferDepthSampleCounts &
  322. properties.Limits.FramebufferStencilSampleCounts;
  323. Capabilities = new HardwareCapabilities(
  324. _physicalDevice.IsDeviceExtensionPresent("VK_EXT_index_type_uint8"),
  325. supportsCustomBorderColor,
  326. supportsBlendOperationAdvanced,
  327. propertiesBlendOperationAdvanced.AdvancedBlendCorrelatedOverlap,
  328. propertiesBlendOperationAdvanced.AdvancedBlendNonPremultipliedSrcColor,
  329. propertiesBlendOperationAdvanced.AdvancedBlendNonPremultipliedDstColor,
  330. _physicalDevice.IsDeviceExtensionPresent(KhrDrawIndirectCount.ExtensionName),
  331. _physicalDevice.IsDeviceExtensionPresent("VK_EXT_fragment_shader_interlock"),
  332. _physicalDevice.IsDeviceExtensionPresent("VK_NV_geometry_shader_passthrough"),
  333. features2.Features.ShaderFloat64,
  334. featuresShaderInt8.ShaderInt8,
  335. _physicalDevice.IsDeviceExtensionPresent("VK_EXT_shader_stencil_export"),
  336. features2.Features.ShaderStorageImageMultisample,
  337. _physicalDevice.IsDeviceExtensionPresent(ExtConditionalRendering.ExtensionName),
  338. _physicalDevice.IsDeviceExtensionPresent(ExtExtendedDynamicState.ExtensionName),
  339. features2.Features.MultiViewport && !(IsMoltenVk && Vendor == Vendor.Amd), // Workaround for AMD on MoltenVK issue
  340. featuresRobustness2.NullDescriptor || IsMoltenVk,
  341. supportsPushDescriptors && !IsMoltenVk,
  342. propertiesPushDescriptor.MaxPushDescriptors,
  343. featuresPrimitiveTopologyListRestart.PrimitiveTopologyListRestart,
  344. featuresPrimitiveTopologyListRestart.PrimitiveTopologyPatchListRestart,
  345. supportsTransformFeedback,
  346. propertiesTransformFeedback.TransformFeedbackQueries,
  347. features2.Features.OcclusionQueryPrecise,
  348. _physicalDevice.PhysicalDeviceFeatures.PipelineStatisticsQuery,
  349. _physicalDevice.PhysicalDeviceFeatures.GeometryShader,
  350. _physicalDevice.PhysicalDeviceFeatures.TessellationShader,
  351. _physicalDevice.IsDeviceExtensionPresent("VK_NV_viewport_array2"),
  352. _physicalDevice.IsDeviceExtensionPresent(ExtExternalMemoryHost.ExtensionName),
  353. supportsDepthClipControl && featuresDepthClipControl.DepthClipControl,
  354. supportsAttachmentFeedbackLoop && featuresAttachmentFeedbackLoop.AttachmentFeedbackLoopLayout,
  355. supportsDynamicAttachmentFeedbackLoop && featuresDynamicAttachmentFeedbackLoop.AttachmentFeedbackLoopDynamicState,
  356. propertiesSubgroup.SubgroupSize,
  357. supportedSampleCounts,
  358. portabilityFlags,
  359. vertexBufferAlignment,
  360. properties.Limits.SubTexelPrecisionBits,
  361. minResourceAlignment);
  362. IsSharedMemory = MemoryAllocator.IsDeviceMemoryShared(_physicalDevice);
  363. MemoryAllocator = new MemoryAllocator(Api, _physicalDevice, _device);
  364. Api.TryGetDeviceExtension(_instance.Instance, _device, out ExtExternalMemoryHost hostMemoryApi);
  365. HostMemoryAllocator = new HostMemoryAllocator(MemoryAllocator, Api, hostMemoryApi, _device);
  366. CommandBufferPool = new CommandBufferPool(Api, _device, Queue, QueueLock, queueFamilyIndex, IsQualcommProprietary);
  367. PipelineLayoutCache = new PipelineLayoutCache();
  368. BackgroundResources = new BackgroundResources(this, _device);
  369. BufferManager = new BufferManager(this, _device);
  370. SyncManager = new SyncManager(this, _device);
  371. _pipeline = new PipelineFull(this, _device);
  372. _pipeline.Initialize();
  373. HelperShader = new HelperShader(this, _device);
  374. Barriers = new BarrierBatch(this);
  375. _counters = new Counters(this, _device, _pipeline);
  376. }
  377. private void SetupContext(GraphicsDebugLevel logLevel)
  378. {
  379. _instance = VulkanInitialization.CreateInstance(Api, logLevel, _getRequiredExtensions());
  380. _debugMessenger = new VulkanDebugMessenger(Api, _instance.Instance, logLevel);
  381. if (Api.TryGetInstanceExtension(_instance.Instance, out KhrSurface surfaceApi))
  382. {
  383. SurfaceApi = surfaceApi;
  384. }
  385. _surface = _getSurface(_instance.Instance, Api);
  386. _physicalDevice = VulkanInitialization.FindSuitablePhysicalDevice(Api, _instance, _surface, _preferredGpuId);
  387. var queueFamilyIndex = VulkanInitialization.FindSuitableQueueFamily(Api, _physicalDevice, _surface, out uint maxQueueCount);
  388. _device = VulkanInitialization.CreateDevice(Api, _physicalDevice, queueFamilyIndex, maxQueueCount);
  389. if (Api.TryGetDeviceExtension(_instance.Instance, _device, out KhrSwapchain swapchainApi))
  390. {
  391. SwapchainApi = swapchainApi;
  392. }
  393. Api.GetDeviceQueue(_device, queueFamilyIndex, 0, out var queue);
  394. Queue = queue;
  395. QueueLock = new object();
  396. LoadFeatures(maxQueueCount, queueFamilyIndex);
  397. QueueFamilyIndex = queueFamilyIndex;
  398. _window = new Window(this, _surface, _physicalDevice.PhysicalDevice, _device);
  399. _initialized = true;
  400. }
  401. internal int[] GetPushDescriptorReservedBindings(bool isOgl)
  402. {
  403. // The first call of this method determines what push descriptor layout is used for all shaders on this renderer.
  404. // This is chosen to minimize shaders that can't fit their uniforms on the device's max number of push descriptors.
  405. if (_pdReservedBindings == null)
  406. {
  407. if (Capabilities.MaxPushDescriptors <= Constants.MaxUniformBuffersPerStage * 2)
  408. {
  409. _pdReservedBindings = isOgl ? _pdReservedBindingsOgl : _pdReservedBindingsNvn;
  410. }
  411. else
  412. {
  413. _pdReservedBindings = Array.Empty<int>();
  414. }
  415. }
  416. return _pdReservedBindings;
  417. }
  418. public BufferHandle CreateBuffer(int size, BufferAccess access)
  419. {
  420. return BufferManager.CreateWithHandle(this, size, access.HasFlag(BufferAccess.SparseCompatible), access.Convert(), access.HasFlag(BufferAccess.Stream));
  421. }
  422. public BufferHandle CreateBuffer(nint pointer, int size)
  423. {
  424. return BufferManager.CreateHostImported(this, pointer, size);
  425. }
  426. public BufferHandle CreateBufferSparse(ReadOnlySpan<BufferRange> storageBuffers)
  427. {
  428. return BufferManager.CreateSparse(this, storageBuffers);
  429. }
  430. public IImageArray CreateImageArray(int size, bool isBuffer)
  431. {
  432. return new ImageArray(this, size, isBuffer);
  433. }
  434. public IProgram CreateProgram(ShaderSource[] sources, ShaderInfo info)
  435. {
  436. ProgramCount++;
  437. bool isCompute = sources.Length == 1 && sources[0].Stage == ShaderStage.Compute;
  438. if (info.State.HasValue || isCompute)
  439. {
  440. return new ShaderCollection(this, _device, sources, info.ResourceLayout, info.State ?? default, info.FromCache);
  441. }
  442. return new ShaderCollection(this, _device, sources, info.ResourceLayout);
  443. }
  444. internal ShaderCollection CreateProgramWithMinimalLayout(ShaderSource[] sources, ResourceLayout resourceLayout, SpecDescription[] specDescription = null)
  445. {
  446. return new ShaderCollection(this, _device, sources, resourceLayout, specDescription, isMinimal: true);
  447. }
  448. public ISampler CreateSampler(SamplerCreateInfo info)
  449. {
  450. return new SamplerHolder(this, _device, info);
  451. }
  452. public ITexture CreateTexture(TextureCreateInfo info)
  453. {
  454. if (info.Target == Target.TextureBuffer)
  455. {
  456. return new TextureBuffer(this, info);
  457. }
  458. return CreateTextureView(info);
  459. }
  460. public ITextureArray CreateTextureArray(int size, bool isBuffer)
  461. {
  462. return new TextureArray(this, size, isBuffer);
  463. }
  464. internal TextureView CreateTextureView(TextureCreateInfo info)
  465. {
  466. // This should be disposed when all views are destroyed.
  467. var storage = CreateTextureStorage(info);
  468. return storage.CreateView(info, 0, 0);
  469. }
  470. internal TextureStorage CreateTextureStorage(TextureCreateInfo info)
  471. {
  472. return new TextureStorage(this, _device, info);
  473. }
  474. public void DeleteBuffer(BufferHandle buffer)
  475. {
  476. BufferManager.Delete(buffer);
  477. }
  478. internal void FlushAllCommands()
  479. {
  480. _pipeline?.FlushCommandsImpl();
  481. }
  482. internal void RegisterFlush()
  483. {
  484. SyncManager.RegisterFlush();
  485. // Periodically free unused regions of the staging buffer to avoid doing it all at once.
  486. BufferManager.StagingBuffer.FreeCompleted();
  487. }
  488. public PinnedSpan<byte> GetBufferData(BufferHandle buffer, int offset, int size)
  489. {
  490. return BufferManager.GetData(buffer, offset, size);
  491. }
  492. public unsafe Capabilities GetCapabilities()
  493. {
  494. FormatFeatureFlags compressedFormatFeatureFlags =
  495. FormatFeatureFlags.SampledImageBit |
  496. FormatFeatureFlags.SampledImageFilterLinearBit |
  497. FormatFeatureFlags.BlitSrcBit |
  498. FormatFeatureFlags.TransferSrcBit |
  499. FormatFeatureFlags.TransferDstBit;
  500. bool supportsBc123CompressionFormat = FormatCapabilities.OptimalFormatsSupport(compressedFormatFeatureFlags,
  501. Format.Bc1RgbaSrgb,
  502. Format.Bc1RgbaUnorm,
  503. Format.Bc2Srgb,
  504. Format.Bc2Unorm,
  505. Format.Bc3Srgb,
  506. Format.Bc3Unorm);
  507. bool supportsBc45CompressionFormat = FormatCapabilities.OptimalFormatsSupport(compressedFormatFeatureFlags,
  508. Format.Bc4Snorm,
  509. Format.Bc4Unorm,
  510. Format.Bc5Snorm,
  511. Format.Bc5Unorm);
  512. bool supportsBc67CompressionFormat = FormatCapabilities.OptimalFormatsSupport(compressedFormatFeatureFlags,
  513. Format.Bc6HSfloat,
  514. Format.Bc6HUfloat,
  515. Format.Bc7Srgb,
  516. Format.Bc7Unorm);
  517. bool supportsEtc2CompressionFormat = FormatCapabilities.OptimalFormatsSupport(compressedFormatFeatureFlags,
  518. Format.Etc2RgbaSrgb,
  519. Format.Etc2RgbaUnorm,
  520. Format.Etc2RgbPtaSrgb,
  521. Format.Etc2RgbPtaUnorm,
  522. Format.Etc2RgbSrgb,
  523. Format.Etc2RgbUnorm);
  524. bool supports5BitComponentFormat = FormatCapabilities.OptimalFormatsSupport(compressedFormatFeatureFlags,
  525. Format.R5G6B5Unorm,
  526. Format.R5G5B5A1Unorm,
  527. Format.R5G5B5X1Unorm,
  528. Format.B5G6R5Unorm,
  529. Format.B5G5R5A1Unorm,
  530. Format.A1B5G5R5Unorm);
  531. bool supportsR4G4B4A4Format = FormatCapabilities.OptimalFormatsSupport(compressedFormatFeatureFlags,
  532. Format.R4G4B4A4Unorm);
  533. bool supportsAstcFormats = FormatCapabilities.OptimalFormatsSupport(compressedFormatFeatureFlags,
  534. Format.Astc4x4Unorm,
  535. Format.Astc5x4Unorm,
  536. Format.Astc5x5Unorm,
  537. Format.Astc6x5Unorm,
  538. Format.Astc6x6Unorm,
  539. Format.Astc8x5Unorm,
  540. Format.Astc8x6Unorm,
  541. Format.Astc8x8Unorm,
  542. Format.Astc10x5Unorm,
  543. Format.Astc10x6Unorm,
  544. Format.Astc10x8Unorm,
  545. Format.Astc10x10Unorm,
  546. Format.Astc12x10Unorm,
  547. Format.Astc12x12Unorm,
  548. Format.Astc4x4Srgb,
  549. Format.Astc5x4Srgb,
  550. Format.Astc5x5Srgb,
  551. Format.Astc6x5Srgb,
  552. Format.Astc6x6Srgb,
  553. Format.Astc8x5Srgb,
  554. Format.Astc8x6Srgb,
  555. Format.Astc8x8Srgb,
  556. Format.Astc10x5Srgb,
  557. Format.Astc10x6Srgb,
  558. Format.Astc10x8Srgb,
  559. Format.Astc10x10Srgb,
  560. Format.Astc12x10Srgb,
  561. Format.Astc12x12Srgb);
  562. PhysicalDeviceVulkan12Features featuresVk12 = new()
  563. {
  564. SType = StructureType.PhysicalDeviceVulkan12Features,
  565. };
  566. PhysicalDeviceFeatures2 features2 = new()
  567. {
  568. SType = StructureType.PhysicalDeviceFeatures2,
  569. PNext = &featuresVk12,
  570. };
  571. Api.GetPhysicalDeviceFeatures2(_physicalDevice.PhysicalDevice, &features2);
  572. var limits = _physicalDevice.PhysicalDeviceProperties.Limits;
  573. var mainQueueProperties = _physicalDevice.QueueFamilyProperties[QueueFamilyIndex];
  574. SystemMemoryType memoryType;
  575. if (IsSharedMemory)
  576. {
  577. memoryType = SystemMemoryType.UnifiedMemory;
  578. }
  579. else
  580. {
  581. memoryType = Vendor == Vendor.Nvidia ?
  582. SystemMemoryType.DedicatedMemorySlowStorage :
  583. SystemMemoryType.DedicatedMemory;
  584. }
  585. return new Capabilities(
  586. api: TargetApi.Vulkan,
  587. GpuVendor,
  588. memoryType: memoryType,
  589. hasFrontFacingBug: IsIntelWindows,
  590. hasVectorIndexingBug: IsQualcommProprietary,
  591. needsFragmentOutputSpecialization: IsMoltenVk,
  592. reduceShaderPrecision: IsMoltenVk,
  593. supportsAstcCompression: features2.Features.TextureCompressionAstcLdr && supportsAstcFormats,
  594. supportsBc123Compression: supportsBc123CompressionFormat,
  595. supportsBc45Compression: supportsBc45CompressionFormat,
  596. supportsBc67Compression: supportsBc67CompressionFormat,
  597. supportsEtc2Compression: supportsEtc2CompressionFormat,
  598. supports3DTextureCompression: true,
  599. supportsBgraFormat: true,
  600. supportsR4G4Format: false,
  601. supportsR4G4B4A4Format: supportsR4G4B4A4Format,
  602. supportsScaledVertexFormats: FormatCapabilities.SupportsScaledVertexFormats(),
  603. supportsSnormBufferTextureFormat: true,
  604. supports5BitComponentFormat: supports5BitComponentFormat,
  605. supportsSparseBuffer: features2.Features.SparseBinding && mainQueueProperties.QueueFlags.HasFlag(QueueFlags.SparseBindingBit),
  606. supportsBlendEquationAdvanced: Capabilities.SupportsBlendEquationAdvanced,
  607. supportsFragmentShaderInterlock: Capabilities.SupportsFragmentShaderInterlock,
  608. supportsFragmentShaderOrderingIntel: false,
  609. supportsGeometryShader: Capabilities.SupportsGeometryShader,
  610. supportsGeometryShaderPassthrough: Capabilities.SupportsGeometryShaderPassthrough,
  611. supportsTransformFeedback: Capabilities.SupportsTransformFeedback,
  612. supportsImageLoadFormatted: features2.Features.ShaderStorageImageReadWithoutFormat,
  613. supportsLayerVertexTessellation: featuresVk12.ShaderOutputLayer,
  614. supportsMismatchingViewFormat: true,
  615. supportsCubemapView: !IsAmdGcn,
  616. supportsNonConstantTextureOffset: false,
  617. supportsQuads: false,
  618. supportsSeparateSampler: true,
  619. supportsShaderBallot: false,
  620. supportsShaderBarrierDivergence: Vendor != Vendor.Intel,
  621. supportsShaderFloat64: Capabilities.SupportsShaderFloat64,
  622. supportsTextureGatherOffsets: features2.Features.ShaderImageGatherExtended && !IsMoltenVk,
  623. supportsTextureShadowLod: false,
  624. supportsVertexStoreAndAtomics: features2.Features.VertexPipelineStoresAndAtomics,
  625. supportsViewportIndexVertexTessellation: featuresVk12.ShaderOutputViewportIndex,
  626. supportsViewportMask: Capabilities.SupportsViewportArray2,
  627. supportsViewportSwizzle: false,
  628. supportsIndirectParameters: true,
  629. supportsDepthClipControl: Capabilities.SupportsDepthClipControl,
  630. uniformBufferSetIndex: PipelineBase.UniformSetIndex,
  631. storageBufferSetIndex: PipelineBase.StorageSetIndex,
  632. textureSetIndex: PipelineBase.TextureSetIndex,
  633. imageSetIndex: PipelineBase.ImageSetIndex,
  634. extraSetBaseIndex: PipelineBase.DescriptorSetLayouts,
  635. maximumExtraSets: Math.Max(0, (int)limits.MaxBoundDescriptorSets - PipelineBase.DescriptorSetLayouts),
  636. maximumUniformBuffersPerStage: Constants.MaxUniformBuffersPerStage,
  637. maximumStorageBuffersPerStage: Constants.MaxStorageBuffersPerStage,
  638. maximumTexturesPerStage: Constants.MaxTexturesPerStage,
  639. maximumImagesPerStage: Constants.MaxImagesPerStage,
  640. maximumComputeSharedMemorySize: (int)limits.MaxComputeSharedMemorySize,
  641. maximumSupportedAnisotropy: (int)limits.MaxSamplerAnisotropy,
  642. shaderSubgroupSize: (int)Capabilities.SubgroupSize,
  643. storageBufferOffsetAlignment: (int)limits.MinStorageBufferOffsetAlignment,
  644. textureBufferOffsetAlignment: (int)limits.MinTexelBufferOffsetAlignment,
  645. gatherBiasPrecision: IsIntelWindows || IsAmdWindows ? (int)Capabilities.SubTexelPrecisionBits : 0,
  646. maximumGpuMemory: GetTotalGPUMemory());
  647. }
  648. private ulong GetTotalGPUMemory()
  649. {
  650. ulong totalMemory = 0;
  651. Api.GetPhysicalDeviceMemoryProperties(_physicalDevice.PhysicalDevice, out PhysicalDeviceMemoryProperties memoryProperties);
  652. for (int i = 0; i < memoryProperties.MemoryHeapCount; i++)
  653. {
  654. var heap = memoryProperties.MemoryHeaps[i];
  655. if ((heap.Flags & MemoryHeapFlags.DeviceLocalBit) == MemoryHeapFlags.DeviceLocalBit)
  656. {
  657. totalMemory += heap.Size;
  658. }
  659. }
  660. return totalMemory;
  661. }
  662. public HardwareInfo GetHardwareInfo()
  663. {
  664. return new HardwareInfo(GpuVendor, GpuRenderer, GpuDriver);
  665. }
  666. /// <summary>
  667. /// Gets the available Vulkan devices using the default Vulkan API
  668. /// object returned by <see cref="Vk.GetApi()"/>
  669. /// </summary>
  670. /// <returns></returns>
  671. public static DeviceInfo[] GetPhysicalDevices()
  672. {
  673. try
  674. {
  675. return VulkanInitialization.GetSuitablePhysicalDevices(Vk.GetApi());
  676. }
  677. catch (Exception ex)
  678. {
  679. Logger.Error?.PrintMsg(LogClass.Gpu, $"Error querying Vulkan devices: {ex.Message}");
  680. return Array.Empty<DeviceInfo>();
  681. }
  682. }
  683. public static DeviceInfo[] GetPhysicalDevices(Vk api)
  684. {
  685. try
  686. {
  687. return VulkanInitialization.GetSuitablePhysicalDevices(api);
  688. }
  689. catch (Exception)
  690. {
  691. // If we got an exception here, Vulkan is most likely not supported.
  692. return Array.Empty<DeviceInfo>();
  693. }
  694. }
  695. private static string ParseStandardVulkanVersion(uint version)
  696. {
  697. return $"{version >> 22}.{(version >> 12) & 0x3FF}.{version & 0xFFF}";
  698. }
  699. private static string ParseDriverVersion(ref PhysicalDeviceProperties properties)
  700. {
  701. uint driverVersionRaw = properties.DriverVersion;
  702. // NVIDIA differ from the standard here and uses a different format.
  703. if (properties.VendorID == 0x10DE)
  704. {
  705. return $"{(driverVersionRaw >> 22) & 0x3FF}.{(driverVersionRaw >> 14) & 0xFF}.{(driverVersionRaw >> 6) & 0xFF}.{driverVersionRaw & 0x3F}";
  706. }
  707. return ParseStandardVulkanVersion(driverVersionRaw);
  708. }
  709. internal PrimitiveTopology TopologyRemap(PrimitiveTopology topology)
  710. {
  711. return topology switch
  712. {
  713. PrimitiveTopology.Quads => PrimitiveTopology.Triangles,
  714. PrimitiveTopology.QuadStrip => PrimitiveTopology.TriangleStrip,
  715. PrimitiveTopology.TriangleFan or PrimitiveTopology.Polygon => Capabilities.PortabilitySubset.HasFlag(PortabilitySubsetFlags.NoTriangleFans)
  716. ? PrimitiveTopology.Triangles
  717. : topology,
  718. _ => topology,
  719. };
  720. }
  721. internal bool TopologyUnsupported(PrimitiveTopology topology)
  722. {
  723. return topology switch
  724. {
  725. PrimitiveTopology.Quads => true,
  726. PrimitiveTopology.TriangleFan or PrimitiveTopology.Polygon => Capabilities.PortabilitySubset.HasFlag(PortabilitySubsetFlags.NoTriangleFans),
  727. _ => false,
  728. };
  729. }
  730. private void PrintGpuInformation()
  731. {
  732. Logger.Notice.Print(LogClass.Gpu, $"{GpuVendor} {GpuRenderer} ({GpuVersion})");
  733. Logger.Notice.Print(LogClass.Gpu, $"GPU Memory: {GetTotalGPUMemory() / (1024 * 1024)} MiB");
  734. }
  735. public void Initialize(GraphicsDebugLevel logLevel)
  736. {
  737. SetupContext(logLevel);
  738. PrintGpuInformation();
  739. }
  740. internal bool NeedsVertexBufferAlignment(int attrScalarAlignment, out int alignment)
  741. {
  742. if (Capabilities.VertexBufferAlignment > 1)
  743. {
  744. alignment = (int)Capabilities.VertexBufferAlignment;
  745. return true;
  746. }
  747. else if (Vendor != Vendor.Nvidia)
  748. {
  749. // Vulkan requires that vertex attributes are globally aligned by their component size,
  750. // so buffer strides that don't divide by the largest scalar element are invalid.
  751. // Guest applications do this, NVIDIA GPUs are OK with it, others are not.
  752. alignment = attrScalarAlignment;
  753. return true;
  754. }
  755. alignment = 1;
  756. return false;
  757. }
  758. public void PreFrame()
  759. {
  760. SyncManager.Cleanup();
  761. }
  762. public ICounterEvent ReportCounter(CounterType type, EventHandler<ulong> resultHandler, float divisor, bool hostReserved)
  763. {
  764. return _counters.QueueReport(type, resultHandler, divisor, hostReserved);
  765. }
  766. public void ResetCounter(CounterType type)
  767. {
  768. _counters.QueueReset(type);
  769. }
  770. public void SetBufferData(BufferHandle buffer, int offset, ReadOnlySpan<byte> data)
  771. {
  772. BufferManager.SetData(buffer, offset, data, _pipeline.CurrentCommandBuffer, _pipeline.EndRenderPassDelegate);
  773. }
  774. public void UpdateCounters()
  775. {
  776. _counters.Update();
  777. }
  778. public void ResetCounterPool()
  779. {
  780. _counters.ResetCounterPool();
  781. }
  782. public void ResetFutureCounters(CommandBuffer cmd, int count)
  783. {
  784. _counters?.ResetFutureCounters(cmd, count);
  785. }
  786. public void BackgroundContextAction(Action action, bool alwaysBackground = false)
  787. {
  788. action();
  789. }
  790. public void CreateSync(ulong id, bool strict)
  791. {
  792. SyncManager.Create(id, strict);
  793. }
  794. public IProgram LoadProgramBinary(byte[] programBinary, bool isFragment, ShaderInfo info)
  795. {
  796. throw new NotImplementedException();
  797. }
  798. public void WaitSync(ulong id)
  799. {
  800. SyncManager.Wait(id);
  801. }
  802. public ulong GetCurrentSync()
  803. {
  804. return SyncManager.GetCurrent();
  805. }
  806. public void SetInterruptAction(Action<Action> interruptAction)
  807. {
  808. InterruptAction = interruptAction;
  809. }
  810. public void Screenshot()
  811. {
  812. _window.ScreenCaptureRequested = true;
  813. }
  814. public void OnScreenCaptured(ScreenCaptureImageInfo bitmap)
  815. {
  816. ScreenCaptured?.Invoke(this, bitmap);
  817. }
  818. public bool SupportsRenderPassBarrier(PipelineStageFlags flags)
  819. {
  820. return !(IsMoltenVk || IsQualcommProprietary);
  821. }
  822. public unsafe void Dispose()
  823. {
  824. if (!_initialized)
  825. {
  826. return;
  827. }
  828. CommandBufferPool.Dispose();
  829. BackgroundResources.Dispose();
  830. _counters.Dispose();
  831. _window.Dispose();
  832. HelperShader.Dispose();
  833. _pipeline.Dispose();
  834. BufferManager.Dispose();
  835. PipelineLayoutCache.Dispose();
  836. Barriers.Dispose();
  837. MemoryAllocator.Dispose();
  838. foreach (var shader in Shaders)
  839. {
  840. shader.Dispose();
  841. }
  842. foreach (var texture in Textures)
  843. {
  844. texture.Release();
  845. }
  846. foreach (var sampler in Samplers)
  847. {
  848. sampler.Dispose();
  849. }
  850. SurfaceApi.DestroySurface(_instance.Instance, _surface, null);
  851. Api.DestroyDevice(_device, null);
  852. _debugMessenger.Dispose();
  853. // Last step destroy the instance
  854. _instance.Dispose();
  855. }
  856. public bool PrepareHostMapping(nint address, ulong size)
  857. {
  858. return Capabilities.SupportsHostImportedMemory &&
  859. HostMemoryAllocator.TryImport(BufferManager.HostImportedBufferMemoryRequirements, BufferManager.DefaultBufferMemoryFlags, address, size);
  860. }
  861. }
  862. }