VulkanRenderer.cs 24 KB

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