VulkanRenderer.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  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.Queries;
  7. using Silk.NET.Vulkan;
  8. using Silk.NET.Vulkan.Extensions.EXT;
  9. using Silk.NET.Vulkan.Extensions.KHR;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Runtime.InteropServices;
  14. namespace Ryujinx.Graphics.Vulkan
  15. {
  16. public sealed class VulkanRenderer : IRenderer
  17. {
  18. private Instance _instance;
  19. private SurfaceKHR _surface;
  20. private PhysicalDevice _physicalDevice;
  21. private Device _device;
  22. private uint _queueFamilyIndex;
  23. private WindowBase _window;
  24. internal FormatCapabilities FormatCapabilities { get; private set; }
  25. internal HardwareCapabilities Capabilities;
  26. internal Vk Api { get; private set; }
  27. internal KhrSurface SurfaceApi { get; private set; }
  28. internal KhrSwapchain SwapchainApi { get; private set; }
  29. internal ExtConditionalRendering ConditionalRenderingApi { get; private set; }
  30. internal ExtExtendedDynamicState ExtendedDynamicStateApi { get; private set; }
  31. internal KhrPushDescriptor PushDescriptorApi { get; private set; }
  32. internal ExtTransformFeedback TransformFeedbackApi { get; private set; }
  33. internal KhrDrawIndirectCount DrawIndirectCountApi { get; private set; }
  34. internal ExtDebugReport DebugReportApi { get; private set; }
  35. internal uint QueueFamilyIndex { get; private set; }
  36. public bool IsOffScreen { get; }
  37. internal Queue Queue { get; private set; }
  38. internal Queue BackgroundQueue { get; private set; }
  39. internal object BackgroundQueueLock { get; private set; }
  40. internal object QueueLock { get; private set; }
  41. internal MemoryAllocator MemoryAllocator { get; private set; }
  42. internal CommandBufferPool CommandBufferPool { get; private set; }
  43. internal DescriptorSetManager DescriptorSetManager { get; private set; }
  44. internal PipelineLayoutCache PipelineLayoutCache { get; private set; }
  45. internal BackgroundResources BackgroundResources { get; private set; }
  46. internal BufferManager BufferManager { get; private set; }
  47. internal HashSet<ShaderCollection> Shaders { get; }
  48. internal HashSet<ITexture> Textures { get; }
  49. internal HashSet<SamplerHolder> Samplers { get; }
  50. private Counters _counters;
  51. private SyncManager _syncManager;
  52. private PipelineFull _pipeline;
  53. private DebugReportCallbackEXT _debugReportCallback;
  54. internal HelperShader HelperShader { get; private set; }
  55. internal PipelineFull PipelineInternal => _pipeline;
  56. public IPipeline Pipeline => _pipeline;
  57. public IWindow Window => _window;
  58. private readonly Func<Instance, Vk, SurfaceKHR> _getSurface;
  59. private readonly Func<string[]> _getRequiredExtensions;
  60. private readonly string _preferredGpuId;
  61. internal Vendor Vendor { get; private set; }
  62. internal bool IsAmdWindows { get; private set; }
  63. internal bool IsIntelWindows { get; private set; }
  64. internal bool IsAmdGcn { get; private set; }
  65. public string GpuVendor { get; private set; }
  66. public string GpuRenderer { get; private set; }
  67. public string GpuVersion { get; private set; }
  68. public bool PreferThreading => true;
  69. public event EventHandler<ScreenCaptureImageInfo> ScreenCaptured;
  70. public VulkanRenderer(Func<Instance, Vk, SurfaceKHR> surfaceFunc, Func<string[]> requiredExtensionsFunc, string preferredGpuId)
  71. {
  72. _getSurface = surfaceFunc;
  73. _getRequiredExtensions = requiredExtensionsFunc;
  74. _preferredGpuId = preferredGpuId;
  75. Shaders = new HashSet<ShaderCollection>();
  76. Textures = new HashSet<ITexture>();
  77. Samplers = new HashSet<SamplerHolder>();
  78. }
  79. public VulkanRenderer(Instance instance, Device device, PhysicalDevice physicalDevice, Queue queue, uint queueFamilyIndex, object lockObject)
  80. {
  81. _instance = instance;
  82. _physicalDevice = physicalDevice;
  83. _device = device;
  84. _queueFamilyIndex = queueFamilyIndex;
  85. Queue = queue;
  86. QueueLock = lockObject;
  87. IsOffScreen = true;
  88. Shaders = new HashSet<ShaderCollection>();
  89. Textures = new HashSet<ITexture>();
  90. Samplers = new HashSet<SamplerHolder>();
  91. }
  92. private unsafe void LoadFeatures(string[] supportedExtensions, uint maxQueueCount, uint queueFamilyIndex)
  93. {
  94. FormatCapabilities = new FormatCapabilities(Api, _physicalDevice);
  95. var supportedFeatures = Api.GetPhysicalDeviceFeature(_physicalDevice);
  96. if (Api.TryGetDeviceExtension(_instance, _device, out ExtConditionalRendering conditionalRenderingApi))
  97. {
  98. ConditionalRenderingApi = conditionalRenderingApi;
  99. }
  100. if (Api.TryGetDeviceExtension(_instance, _device, out ExtExtendedDynamicState extendedDynamicStateApi))
  101. {
  102. ExtendedDynamicStateApi = extendedDynamicStateApi;
  103. }
  104. if (Api.TryGetDeviceExtension(_instance, _device, out KhrPushDescriptor pushDescriptorApi))
  105. {
  106. PushDescriptorApi = pushDescriptorApi;
  107. }
  108. if (Api.TryGetDeviceExtension(_instance, _device, out ExtTransformFeedback transformFeedbackApi))
  109. {
  110. TransformFeedbackApi = transformFeedbackApi;
  111. }
  112. if (Api.TryGetDeviceExtension(_instance, _device, out KhrDrawIndirectCount drawIndirectCountApi))
  113. {
  114. DrawIndirectCountApi = drawIndirectCountApi;
  115. }
  116. if (maxQueueCount >= 2)
  117. {
  118. Api.GetDeviceQueue(_device, queueFamilyIndex, 1, out var backgroundQueue);
  119. BackgroundQueue = backgroundQueue;
  120. BackgroundQueueLock = new object();
  121. }
  122. PhysicalDeviceProperties2 properties2 = new PhysicalDeviceProperties2()
  123. {
  124. SType = StructureType.PhysicalDeviceProperties2
  125. };
  126. PhysicalDeviceSubgroupSizeControlPropertiesEXT propertiesSubgroupSizeControl = new PhysicalDeviceSubgroupSizeControlPropertiesEXT()
  127. {
  128. SType = StructureType.PhysicalDeviceSubgroupSizeControlPropertiesExt
  129. };
  130. if (Capabilities.SupportsSubgroupSizeControl)
  131. {
  132. properties2.PNext = &propertiesSubgroupSizeControl;
  133. }
  134. bool supportsTransformFeedback = supportedExtensions.Contains(ExtTransformFeedback.ExtensionName);
  135. PhysicalDeviceTransformFeedbackPropertiesEXT propertiesTransformFeedback = new PhysicalDeviceTransformFeedbackPropertiesEXT()
  136. {
  137. SType = StructureType.PhysicalDeviceTransformFeedbackPropertiesExt
  138. };
  139. if (supportsTransformFeedback)
  140. {
  141. propertiesTransformFeedback.PNext = properties2.PNext;
  142. properties2.PNext = &propertiesTransformFeedback;
  143. }
  144. Api.GetPhysicalDeviceProperties2(_physicalDevice, &properties2);
  145. PhysicalDeviceFeatures2 features2 = new PhysicalDeviceFeatures2()
  146. {
  147. SType = StructureType.PhysicalDeviceFeatures2
  148. };
  149. PhysicalDeviceRobustness2FeaturesEXT featuresRobustness2 = new PhysicalDeviceRobustness2FeaturesEXT()
  150. {
  151. SType = StructureType.PhysicalDeviceRobustness2FeaturesExt
  152. };
  153. if (supportedExtensions.Contains("VK_EXT_robustness2"))
  154. {
  155. features2.PNext = &featuresRobustness2;
  156. }
  157. Api.GetPhysicalDeviceFeatures2(_physicalDevice, &features2);
  158. Capabilities = new HardwareCapabilities(
  159. supportedExtensions.Contains("VK_EXT_index_type_uint8"),
  160. supportedExtensions.Contains("VK_EXT_custom_border_color"),
  161. supportedExtensions.Contains(KhrDrawIndirectCount.ExtensionName),
  162. supportedExtensions.Contains("VK_EXT_fragment_shader_interlock"),
  163. supportedExtensions.Contains("VK_NV_geometry_shader_passthrough"),
  164. supportedExtensions.Contains("VK_EXT_subgroup_size_control"),
  165. supportedExtensions.Contains(ExtConditionalRendering.ExtensionName),
  166. supportedExtensions.Contains(ExtExtendedDynamicState.ExtensionName),
  167. features2.Features.MultiViewport,
  168. featuresRobustness2.NullDescriptor,
  169. supportedExtensions.Contains(KhrPushDescriptor.ExtensionName),
  170. supportsTransformFeedback,
  171. propertiesTransformFeedback.TransformFeedbackQueries,
  172. supportedFeatures.GeometryShader,
  173. propertiesSubgroupSizeControl.MinSubgroupSize,
  174. propertiesSubgroupSizeControl.MaxSubgroupSize,
  175. propertiesSubgroupSizeControl.RequiredSubgroupSizeStages);
  176. ref var properties = ref properties2.Properties;
  177. MemoryAllocator = new MemoryAllocator(Api, _device, properties.Limits.MaxMemoryAllocationCount);
  178. CommandBufferPool = VulkanInitialization.CreateCommandBufferPool(Api, _device, Queue, QueueLock, queueFamilyIndex);
  179. DescriptorSetManager = new DescriptorSetManager(_device);
  180. PipelineLayoutCache = new PipelineLayoutCache();
  181. BackgroundResources = new BackgroundResources(this, _device);
  182. BufferManager = new BufferManager(this, _physicalDevice, _device);
  183. _syncManager = new SyncManager(this, _device);
  184. _pipeline = new PipelineFull(this, _device);
  185. _pipeline.Initialize();
  186. HelperShader = new HelperShader(this, _device);
  187. _counters = new Counters(this, _device, _pipeline);
  188. }
  189. private unsafe void SetupContext(GraphicsDebugLevel logLevel)
  190. {
  191. var api = Vk.GetApi();
  192. Api = api;
  193. _instance = VulkanInitialization.CreateInstance(api, logLevel, _getRequiredExtensions(), out ExtDebugReport debugReport, out _debugReportCallback);
  194. DebugReportApi = debugReport;
  195. if (api.TryGetInstanceExtension(_instance, out KhrSurface surfaceApi))
  196. {
  197. SurfaceApi = surfaceApi;
  198. }
  199. _surface = _getSurface(_instance, api);
  200. _physicalDevice = VulkanInitialization.FindSuitablePhysicalDevice(api, _instance, _surface, _preferredGpuId);
  201. var queueFamilyIndex = VulkanInitialization.FindSuitableQueueFamily(api, _physicalDevice, _surface, out uint maxQueueCount);
  202. var supportedExtensions = VulkanInitialization.GetSupportedExtensions(api, _physicalDevice);
  203. _device = VulkanInitialization.CreateDevice(api, _physicalDevice, queueFamilyIndex, supportedExtensions, maxQueueCount);
  204. if (api.TryGetDeviceExtension(_instance, _device, out KhrSwapchain swapchainApi))
  205. {
  206. SwapchainApi = swapchainApi;
  207. }
  208. api.GetDeviceQueue(_device, queueFamilyIndex, 0, out var queue);
  209. Queue = queue;
  210. QueueLock = new object();
  211. LoadFeatures(supportedExtensions, maxQueueCount, queueFamilyIndex);
  212. _window = new Window(this, _surface, _physicalDevice, _device);
  213. }
  214. private unsafe void SetupOffScreenContext(GraphicsDebugLevel logLevel)
  215. {
  216. var api = Vk.GetApi();
  217. Api = api;
  218. VulkanInitialization.CreateDebugCallbacks(api, logLevel, _instance, out var debugReport, out _debugReportCallback);
  219. DebugReportApi = debugReport;
  220. var supportedExtensions = VulkanInitialization.GetSupportedExtensions(api, _physicalDevice);
  221. uint propertiesCount;
  222. api.GetPhysicalDeviceQueueFamilyProperties(_physicalDevice, &propertiesCount, null);
  223. QueueFamilyProperties[] queueFamilyProperties = new QueueFamilyProperties[propertiesCount];
  224. fixed (QueueFamilyProperties* pProperties = queueFamilyProperties)
  225. {
  226. api.GetPhysicalDeviceQueueFamilyProperties(_physicalDevice, &propertiesCount, pProperties);
  227. }
  228. LoadFeatures(supportedExtensions, queueFamilyProperties[0].QueueCount, _queueFamilyIndex);
  229. _window = new ImageWindow(this, _physicalDevice, _device);
  230. }
  231. public BufferHandle CreateBuffer(int size)
  232. {
  233. return BufferManager.CreateWithHandle(this, size, false);
  234. }
  235. public IProgram CreateProgram(ShaderSource[] sources, ShaderInfo info)
  236. {
  237. bool isCompute = sources.Length == 1 && sources[0].Stage == ShaderStage.Compute;
  238. if (info.State.HasValue || isCompute)
  239. {
  240. return new ShaderCollection(this, _device, sources, info.State ?? default, info.FromCache);
  241. }
  242. else
  243. {
  244. return new ShaderCollection(this, _device, sources);
  245. }
  246. }
  247. internal ShaderCollection CreateProgramWithMinimalLayout(ShaderSource[] sources)
  248. {
  249. return new ShaderCollection(this, _device, sources, isMinimal: true);
  250. }
  251. public ISampler CreateSampler(GAL.SamplerCreateInfo info)
  252. {
  253. return new SamplerHolder(this, _device, info);
  254. }
  255. public ITexture CreateTexture(TextureCreateInfo info, float scale)
  256. {
  257. if (info.Target == Target.TextureBuffer)
  258. {
  259. return new TextureBuffer(this, info, scale);
  260. }
  261. return CreateTextureView(info, scale);
  262. }
  263. internal TextureView CreateTextureView(TextureCreateInfo info, float scale)
  264. {
  265. // This should be disposed when all views are destroyed.
  266. using var storage = CreateTextureStorage(info, scale);
  267. return storage.CreateView(info, 0, 0);
  268. }
  269. internal TextureStorage CreateTextureStorage(TextureCreateInfo info, float scale)
  270. {
  271. return new TextureStorage(this, _physicalDevice, _device, info, scale);
  272. }
  273. public void DeleteBuffer(BufferHandle buffer)
  274. {
  275. BufferManager.Delete(buffer);
  276. }
  277. internal void FlushAllCommands()
  278. {
  279. _pipeline?.FlushCommandsImpl();
  280. }
  281. public ReadOnlySpan<byte> GetBufferData(BufferHandle buffer, int offset, int size)
  282. {
  283. return BufferManager.GetData(buffer, offset, size);
  284. }
  285. public Capabilities GetCapabilities()
  286. {
  287. FormatFeatureFlags compressedFormatFeatureFlags =
  288. FormatFeatureFlags.FormatFeatureSampledImageBit |
  289. FormatFeatureFlags.FormatFeatureSampledImageFilterLinearBit |
  290. FormatFeatureFlags.FormatFeatureBlitSrcBit |
  291. FormatFeatureFlags.FormatFeatureTransferSrcBit |
  292. FormatFeatureFlags.FormatFeatureTransferDstBit;
  293. bool supportsBc123CompressionFormat = FormatCapabilities.OptimalFormatsSupport(compressedFormatFeatureFlags,
  294. GAL.Format.Bc1RgbaSrgb,
  295. GAL.Format.Bc1RgbaUnorm,
  296. GAL.Format.Bc2Srgb,
  297. GAL.Format.Bc2Unorm,
  298. GAL.Format.Bc3Srgb,
  299. GAL.Format.Bc3Unorm);
  300. bool supportsBc45CompressionFormat = FormatCapabilities.OptimalFormatsSupport(compressedFormatFeatureFlags,
  301. GAL.Format.Bc4Snorm,
  302. GAL.Format.Bc4Unorm,
  303. GAL.Format.Bc5Snorm,
  304. GAL.Format.Bc5Unorm);
  305. bool supportsBc67CompressionFormat = FormatCapabilities.OptimalFormatsSupport(compressedFormatFeatureFlags,
  306. GAL.Format.Bc6HSfloat,
  307. GAL.Format.Bc6HUfloat,
  308. GAL.Format.Bc7Srgb,
  309. GAL.Format.Bc7Unorm);
  310. Api.GetPhysicalDeviceFeatures(_physicalDevice, out var features);
  311. Api.GetPhysicalDeviceProperties(_physicalDevice, out var properties);
  312. var limits = properties.Limits;
  313. return new Capabilities(
  314. api: TargetApi.Vulkan,
  315. GpuVendor,
  316. hasFrontFacingBug: IsIntelWindows,
  317. hasVectorIndexingBug: Vendor == Vendor.Qualcomm,
  318. supportsAstcCompression: features.TextureCompressionAstcLdr,
  319. supportsBc123Compression: supportsBc123CompressionFormat,
  320. supportsBc45Compression: supportsBc45CompressionFormat,
  321. supportsBc67Compression: supportsBc67CompressionFormat,
  322. supports3DTextureCompression: true,
  323. supportsBgraFormat: true,
  324. supportsR4G4Format: false,
  325. supportsFragmentShaderInterlock: Capabilities.SupportsFragmentShaderInterlock,
  326. supportsFragmentShaderOrderingIntel: false,
  327. supportsGeometryShaderPassthrough: Capabilities.SupportsGeometryShaderPassthrough,
  328. supportsImageLoadFormatted: features.ShaderStorageImageReadWithoutFormat,
  329. supportsMismatchingViewFormat: true,
  330. supportsCubemapView: !IsAmdGcn,
  331. supportsNonConstantTextureOffset: false,
  332. supportsShaderBallot: false,
  333. supportsTextureShadowLod: false,
  334. supportsViewportSwizzle: false,
  335. supportsIndirectParameters: Capabilities.SupportsIndirectParameters,
  336. maximumUniformBuffersPerStage: Constants.MaxUniformBuffersPerStage,
  337. maximumStorageBuffersPerStage: Constants.MaxStorageBuffersPerStage,
  338. maximumTexturesPerStage: Constants.MaxTexturesPerStage,
  339. maximumImagesPerStage: Constants.MaxImagesPerStage,
  340. maximumComputeSharedMemorySize: (int)limits.MaxComputeSharedMemorySize,
  341. maximumSupportedAnisotropy: (int)limits.MaxSamplerAnisotropy,
  342. storageBufferOffsetAlignment: (int)limits.MinStorageBufferOffsetAlignment);
  343. }
  344. public HardwareInfo GetHardwareInfo()
  345. {
  346. return new HardwareInfo(GpuVendor, GpuRenderer);
  347. }
  348. public static DeviceInfo[] GetPhysicalDevices()
  349. {
  350. try
  351. {
  352. return VulkanInitialization.GetSuitablePhysicalDevices(Vk.GetApi());
  353. }
  354. catch (Exception)
  355. {
  356. // If we got an exception here, Vulkan is most likely not supported.
  357. return Array.Empty<DeviceInfo>();
  358. }
  359. }
  360. private static string ParseStandardVulkanVersion(uint version)
  361. {
  362. return $"{version >> 22}.{(version >> 12) & 0x3FF}.{version & 0xFFF}";
  363. }
  364. private static string ParseDriverVersion(ref PhysicalDeviceProperties properties)
  365. {
  366. uint driverVersionRaw = properties.DriverVersion;
  367. // NVIDIA differ from the standard here and uses a different format.
  368. if (properties.VendorID == 0x10DE)
  369. {
  370. return $"{(driverVersionRaw >> 22) & 0x3FF}.{(driverVersionRaw >> 14) & 0xFF}.{(driverVersionRaw >> 6) & 0xFF}.{driverVersionRaw & 0x3F}";
  371. }
  372. else
  373. {
  374. return ParseStandardVulkanVersion(driverVersionRaw);
  375. }
  376. }
  377. private unsafe void PrintGpuInformation()
  378. {
  379. Api.GetPhysicalDeviceProperties(_physicalDevice, out var properties);
  380. string vendorName = VendorUtils.GetNameFromId(properties.VendorID);
  381. Vendor = VendorUtils.FromId(properties.VendorID);
  382. IsAmdWindows = Vendor == Vendor.Amd && RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
  383. IsIntelWindows = Vendor == Vendor.Intel && RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
  384. GpuVendor = vendorName;
  385. GpuRenderer = Marshal.PtrToStringAnsi((IntPtr)properties.DeviceName);
  386. GpuVersion = $"Vulkan v{ParseStandardVulkanVersion(properties.ApiVersion)}, Driver v{ParseDriverVersion(ref properties)}";
  387. IsAmdGcn = Vendor == Vendor.Amd && VendorUtils.AmdGcnRegex.IsMatch(GpuRenderer);
  388. Logger.Notice.Print(LogClass.Gpu, $"{GpuVendor} {GpuRenderer} ({GpuVersion})");
  389. }
  390. public void Initialize(GraphicsDebugLevel logLevel)
  391. {
  392. if (IsOffScreen)
  393. {
  394. SetupOffScreenContext(logLevel);
  395. }
  396. else
  397. {
  398. SetupContext(logLevel);
  399. }
  400. PrintGpuInformation();
  401. }
  402. public void PreFrame()
  403. {
  404. _syncManager.Cleanup();
  405. }
  406. public ICounterEvent ReportCounter(CounterType type, EventHandler<ulong> resultHandler, bool hostReserved)
  407. {
  408. return _counters.QueueReport(type, resultHandler, hostReserved);
  409. }
  410. public void ResetCounter(CounterType type)
  411. {
  412. _counters.QueueReset(type);
  413. }
  414. public void SetBufferData(BufferHandle buffer, int offset, ReadOnlySpan<byte> data)
  415. {
  416. BufferManager.SetData(buffer, offset, data, _pipeline.CurrentCommandBuffer, _pipeline.EndRenderPass);
  417. }
  418. public void UpdateCounters()
  419. {
  420. _counters.Update();
  421. }
  422. public void BackgroundContextAction(Action action, bool alwaysBackground = false)
  423. {
  424. action();
  425. }
  426. public void CreateSync(ulong id)
  427. {
  428. _syncManager.Create(id);
  429. }
  430. public IProgram LoadProgramBinary(byte[] programBinary, bool isFragment, ShaderInfo info)
  431. {
  432. throw new NotImplementedException();
  433. }
  434. public void WaitSync(ulong id)
  435. {
  436. _syncManager.Wait(id);
  437. }
  438. public void Screenshot()
  439. {
  440. _window.ScreenCaptureRequested = true;
  441. }
  442. public void OnScreenCaptured(ScreenCaptureImageInfo bitmap)
  443. {
  444. ScreenCaptured?.Invoke(this, bitmap);
  445. }
  446. public unsafe void Dispose()
  447. {
  448. CommandBufferPool.Dispose();
  449. BackgroundResources.Dispose();
  450. _counters.Dispose();
  451. _window.Dispose();
  452. HelperShader.Dispose();
  453. _pipeline.Dispose();
  454. BufferManager.Dispose();
  455. DescriptorSetManager.Dispose();
  456. PipelineLayoutCache.Dispose();
  457. MemoryAllocator.Dispose();
  458. if (_debugReportCallback.Handle != 0)
  459. {
  460. DebugReportApi.DestroyDebugReportCallback(_instance, _debugReportCallback, null);
  461. }
  462. foreach (var shader in Shaders)
  463. {
  464. shader.Dispose();
  465. }
  466. foreach (var texture in Textures)
  467. {
  468. texture.Release();
  469. }
  470. foreach (var sampler in Samplers)
  471. {
  472. sampler.Dispose();
  473. }
  474. if (!IsOffScreen)
  475. {
  476. SurfaceApi.DestroySurface(_instance, _surface, null);
  477. Api.DestroyDevice(_device, null);
  478. // Last step destroy the instance
  479. Api.DestroyInstance(_instance, null);
  480. }
  481. }
  482. }
  483. }