VulkanRenderer.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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. PhysicalDeviceShaderFloat16Int8FeaturesKHR featuresShaderInt8 = new PhysicalDeviceShaderFloat16Int8FeaturesKHR()
  154. {
  155. SType = StructureType.PhysicalDeviceShaderFloat16Int8Features
  156. };
  157. if (supportedExtensions.Contains("VK_EXT_robustness2"))
  158. {
  159. features2.PNext = &featuresRobustness2;
  160. }
  161. if (supportedExtensions.Contains("VK_KHR_shader_float16_int8"))
  162. {
  163. featuresShaderInt8.PNext = features2.PNext;
  164. features2.PNext = &featuresShaderInt8;
  165. }
  166. Api.GetPhysicalDeviceFeatures2(_physicalDevice, &features2);
  167. Capabilities = new HardwareCapabilities(
  168. supportedExtensions.Contains("VK_EXT_index_type_uint8"),
  169. supportedExtensions.Contains("VK_EXT_custom_border_color"),
  170. supportedExtensions.Contains(KhrDrawIndirectCount.ExtensionName),
  171. supportedExtensions.Contains("VK_EXT_fragment_shader_interlock"),
  172. supportedExtensions.Contains("VK_NV_geometry_shader_passthrough"),
  173. supportedExtensions.Contains("VK_EXT_subgroup_size_control"),
  174. featuresShaderInt8.ShaderInt8,
  175. supportedExtensions.Contains(ExtConditionalRendering.ExtensionName),
  176. supportedExtensions.Contains(ExtExtendedDynamicState.ExtensionName),
  177. features2.Features.MultiViewport,
  178. featuresRobustness2.NullDescriptor,
  179. supportedExtensions.Contains(KhrPushDescriptor.ExtensionName),
  180. supportsTransformFeedback,
  181. propertiesTransformFeedback.TransformFeedbackQueries,
  182. supportedFeatures.GeometryShader,
  183. propertiesSubgroupSizeControl.MinSubgroupSize,
  184. propertiesSubgroupSizeControl.MaxSubgroupSize,
  185. propertiesSubgroupSizeControl.RequiredSubgroupSizeStages);
  186. ref var properties = ref properties2.Properties;
  187. MemoryAllocator = new MemoryAllocator(Api, _device, properties.Limits.MaxMemoryAllocationCount);
  188. CommandBufferPool = VulkanInitialization.CreateCommandBufferPool(Api, _device, Queue, QueueLock, queueFamilyIndex);
  189. DescriptorSetManager = new DescriptorSetManager(_device);
  190. PipelineLayoutCache = new PipelineLayoutCache();
  191. BackgroundResources = new BackgroundResources(this, _device);
  192. BufferManager = new BufferManager(this, _physicalDevice, _device);
  193. _syncManager = new SyncManager(this, _device);
  194. _pipeline = new PipelineFull(this, _device);
  195. _pipeline.Initialize();
  196. HelperShader = new HelperShader(this, _device);
  197. _counters = new Counters(this, _device, _pipeline);
  198. }
  199. private unsafe void SetupContext(GraphicsDebugLevel logLevel)
  200. {
  201. var api = Vk.GetApi();
  202. Api = api;
  203. _instance = VulkanInitialization.CreateInstance(api, logLevel, _getRequiredExtensions(), out ExtDebugReport debugReport, out _debugReportCallback);
  204. DebugReportApi = debugReport;
  205. if (api.TryGetInstanceExtension(_instance, out KhrSurface surfaceApi))
  206. {
  207. SurfaceApi = surfaceApi;
  208. }
  209. _surface = _getSurface(_instance, api);
  210. _physicalDevice = VulkanInitialization.FindSuitablePhysicalDevice(api, _instance, _surface, _preferredGpuId);
  211. var queueFamilyIndex = VulkanInitialization.FindSuitableQueueFamily(api, _physicalDevice, _surface, out uint maxQueueCount);
  212. var supportedExtensions = VulkanInitialization.GetSupportedExtensions(api, _physicalDevice);
  213. _device = VulkanInitialization.CreateDevice(api, _physicalDevice, queueFamilyIndex, supportedExtensions, maxQueueCount);
  214. if (api.TryGetDeviceExtension(_instance, _device, out KhrSwapchain swapchainApi))
  215. {
  216. SwapchainApi = swapchainApi;
  217. }
  218. api.GetDeviceQueue(_device, queueFamilyIndex, 0, out var queue);
  219. Queue = queue;
  220. QueueLock = new object();
  221. LoadFeatures(supportedExtensions, maxQueueCount, queueFamilyIndex);
  222. _window = new Window(this, _surface, _physicalDevice, _device);
  223. }
  224. private unsafe void SetupOffScreenContext(GraphicsDebugLevel logLevel)
  225. {
  226. var api = Vk.GetApi();
  227. Api = api;
  228. VulkanInitialization.CreateDebugCallbacks(api, logLevel, _instance, out var debugReport, out _debugReportCallback);
  229. DebugReportApi = debugReport;
  230. var supportedExtensions = VulkanInitialization.GetSupportedExtensions(api, _physicalDevice);
  231. uint propertiesCount;
  232. api.GetPhysicalDeviceQueueFamilyProperties(_physicalDevice, &propertiesCount, null);
  233. QueueFamilyProperties[] queueFamilyProperties = new QueueFamilyProperties[propertiesCount];
  234. fixed (QueueFamilyProperties* pProperties = queueFamilyProperties)
  235. {
  236. api.GetPhysicalDeviceQueueFamilyProperties(_physicalDevice, &propertiesCount, pProperties);
  237. }
  238. LoadFeatures(supportedExtensions, queueFamilyProperties[0].QueueCount, _queueFamilyIndex);
  239. _window = new ImageWindow(this, _physicalDevice, _device);
  240. }
  241. public BufferHandle CreateBuffer(int size)
  242. {
  243. return BufferManager.CreateWithHandle(this, size, false);
  244. }
  245. public IProgram CreateProgram(ShaderSource[] sources, ShaderInfo info)
  246. {
  247. bool isCompute = sources.Length == 1 && sources[0].Stage == ShaderStage.Compute;
  248. if (info.State.HasValue || isCompute)
  249. {
  250. return new ShaderCollection(this, _device, sources, info.State ?? default, info.FromCache);
  251. }
  252. else
  253. {
  254. return new ShaderCollection(this, _device, sources);
  255. }
  256. }
  257. internal ShaderCollection CreateProgramWithMinimalLayout(ShaderSource[] sources)
  258. {
  259. return new ShaderCollection(this, _device, sources, isMinimal: true);
  260. }
  261. public ISampler CreateSampler(GAL.SamplerCreateInfo info)
  262. {
  263. return new SamplerHolder(this, _device, info);
  264. }
  265. public ITexture CreateTexture(TextureCreateInfo info, float scale)
  266. {
  267. if (info.Target == Target.TextureBuffer)
  268. {
  269. return new TextureBuffer(this, info, scale);
  270. }
  271. return CreateTextureView(info, scale);
  272. }
  273. internal TextureView CreateTextureView(TextureCreateInfo info, float scale)
  274. {
  275. // This should be disposed when all views are destroyed.
  276. using var storage = CreateTextureStorage(info, scale);
  277. return storage.CreateView(info, 0, 0);
  278. }
  279. internal TextureStorage CreateTextureStorage(TextureCreateInfo info, float scale)
  280. {
  281. return new TextureStorage(this, _physicalDevice, _device, info, scale);
  282. }
  283. public void DeleteBuffer(BufferHandle buffer)
  284. {
  285. BufferManager.Delete(buffer);
  286. }
  287. internal void FlushAllCommands()
  288. {
  289. _pipeline?.FlushCommandsImpl();
  290. }
  291. public ReadOnlySpan<byte> GetBufferData(BufferHandle buffer, int offset, int size)
  292. {
  293. return BufferManager.GetData(buffer, offset, size);
  294. }
  295. public unsafe Capabilities GetCapabilities()
  296. {
  297. FormatFeatureFlags compressedFormatFeatureFlags =
  298. FormatFeatureFlags.FormatFeatureSampledImageBit |
  299. FormatFeatureFlags.FormatFeatureSampledImageFilterLinearBit |
  300. FormatFeatureFlags.FormatFeatureBlitSrcBit |
  301. FormatFeatureFlags.FormatFeatureTransferSrcBit |
  302. FormatFeatureFlags.FormatFeatureTransferDstBit;
  303. bool supportsBc123CompressionFormat = FormatCapabilities.OptimalFormatsSupport(compressedFormatFeatureFlags,
  304. GAL.Format.Bc1RgbaSrgb,
  305. GAL.Format.Bc1RgbaUnorm,
  306. GAL.Format.Bc2Srgb,
  307. GAL.Format.Bc2Unorm,
  308. GAL.Format.Bc3Srgb,
  309. GAL.Format.Bc3Unorm);
  310. bool supportsBc45CompressionFormat = FormatCapabilities.OptimalFormatsSupport(compressedFormatFeatureFlags,
  311. GAL.Format.Bc4Snorm,
  312. GAL.Format.Bc4Unorm,
  313. GAL.Format.Bc5Snorm,
  314. GAL.Format.Bc5Unorm);
  315. bool supportsBc67CompressionFormat = FormatCapabilities.OptimalFormatsSupport(compressedFormatFeatureFlags,
  316. GAL.Format.Bc6HSfloat,
  317. GAL.Format.Bc6HUfloat,
  318. GAL.Format.Bc7Srgb,
  319. GAL.Format.Bc7Unorm);
  320. PhysicalDeviceVulkan12Features featuresVk12 = new PhysicalDeviceVulkan12Features()
  321. {
  322. SType = StructureType.PhysicalDeviceVulkan12Features
  323. };
  324. PhysicalDeviceFeatures2 features2 = new PhysicalDeviceFeatures2()
  325. {
  326. SType = StructureType.PhysicalDeviceFeatures2,
  327. PNext = &featuresVk12
  328. };
  329. Api.GetPhysicalDeviceFeatures2(_physicalDevice, &features2);
  330. Api.GetPhysicalDeviceProperties(_physicalDevice, out var properties);
  331. var limits = properties.Limits;
  332. return new Capabilities(
  333. api: TargetApi.Vulkan,
  334. GpuVendor,
  335. hasFrontFacingBug: IsIntelWindows,
  336. hasVectorIndexingBug: Vendor == Vendor.Qualcomm,
  337. supportsAstcCompression: features2.Features.TextureCompressionAstcLdr,
  338. supportsBc123Compression: supportsBc123CompressionFormat,
  339. supportsBc45Compression: supportsBc45CompressionFormat,
  340. supportsBc67Compression: supportsBc67CompressionFormat,
  341. supports3DTextureCompression: true,
  342. supportsBgraFormat: true,
  343. supportsR4G4Format: false,
  344. supportsFragmentShaderInterlock: Capabilities.SupportsFragmentShaderInterlock,
  345. supportsFragmentShaderOrderingIntel: false,
  346. supportsGeometryShaderPassthrough: Capabilities.SupportsGeometryShaderPassthrough,
  347. supportsImageLoadFormatted: features2.Features.ShaderStorageImageReadWithoutFormat,
  348. supportsMismatchingViewFormat: true,
  349. supportsCubemapView: !IsAmdGcn,
  350. supportsNonConstantTextureOffset: false,
  351. supportsShaderBallot: false,
  352. supportsTextureShadowLod: false,
  353. supportsViewportIndex: featuresVk12.ShaderOutputViewportIndex,
  354. supportsViewportSwizzle: false,
  355. supportsIndirectParameters: Capabilities.SupportsIndirectParameters,
  356. maximumUniformBuffersPerStage: Constants.MaxUniformBuffersPerStage,
  357. maximumStorageBuffersPerStage: Constants.MaxStorageBuffersPerStage,
  358. maximumTexturesPerStage: Constants.MaxTexturesPerStage,
  359. maximumImagesPerStage: Constants.MaxImagesPerStage,
  360. maximumComputeSharedMemorySize: (int)limits.MaxComputeSharedMemorySize,
  361. maximumSupportedAnisotropy: (int)limits.MaxSamplerAnisotropy,
  362. storageBufferOffsetAlignment: (int)limits.MinStorageBufferOffsetAlignment);
  363. }
  364. public HardwareInfo GetHardwareInfo()
  365. {
  366. return new HardwareInfo(GpuVendor, GpuRenderer);
  367. }
  368. public static DeviceInfo[] GetPhysicalDevices()
  369. {
  370. try
  371. {
  372. return VulkanInitialization.GetSuitablePhysicalDevices(Vk.GetApi());
  373. }
  374. catch (Exception)
  375. {
  376. // If we got an exception here, Vulkan is most likely not supported.
  377. return Array.Empty<DeviceInfo>();
  378. }
  379. }
  380. private static string ParseStandardVulkanVersion(uint version)
  381. {
  382. return $"{version >> 22}.{(version >> 12) & 0x3FF}.{version & 0xFFF}";
  383. }
  384. private static string ParseDriverVersion(ref PhysicalDeviceProperties properties)
  385. {
  386. uint driverVersionRaw = properties.DriverVersion;
  387. // NVIDIA differ from the standard here and uses a different format.
  388. if (properties.VendorID == 0x10DE)
  389. {
  390. return $"{(driverVersionRaw >> 22) & 0x3FF}.{(driverVersionRaw >> 14) & 0xFF}.{(driverVersionRaw >> 6) & 0xFF}.{driverVersionRaw & 0x3F}";
  391. }
  392. else
  393. {
  394. return ParseStandardVulkanVersion(driverVersionRaw);
  395. }
  396. }
  397. private unsafe void PrintGpuInformation()
  398. {
  399. Api.GetPhysicalDeviceProperties(_physicalDevice, out var properties);
  400. string vendorName = VendorUtils.GetNameFromId(properties.VendorID);
  401. Vendor = VendorUtils.FromId(properties.VendorID);
  402. IsAmdWindows = Vendor == Vendor.Amd && RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
  403. IsIntelWindows = Vendor == Vendor.Intel && RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
  404. GpuVendor = vendorName;
  405. GpuRenderer = Marshal.PtrToStringAnsi((IntPtr)properties.DeviceName);
  406. GpuVersion = $"Vulkan v{ParseStandardVulkanVersion(properties.ApiVersion)}, Driver v{ParseDriverVersion(ref properties)}";
  407. IsAmdGcn = Vendor == Vendor.Amd && VendorUtils.AmdGcnRegex.IsMatch(GpuRenderer);
  408. Logger.Notice.Print(LogClass.Gpu, $"{GpuVendor} {GpuRenderer} ({GpuVersion})");
  409. }
  410. public void Initialize(GraphicsDebugLevel logLevel)
  411. {
  412. if (IsOffScreen)
  413. {
  414. SetupOffScreenContext(logLevel);
  415. }
  416. else
  417. {
  418. SetupContext(logLevel);
  419. }
  420. PrintGpuInformation();
  421. }
  422. public bool NeedsVertexBufferAlignment(int attrScalarAlignment, out int alignment)
  423. {
  424. if (Vendor != Vendor.Nvidia)
  425. {
  426. // Vulkan requires that vertex attributes are globally aligned by their component size,
  427. // so buffer strides that don't divide by the largest scalar element are invalid.
  428. // Guest applications do this, NVIDIA GPUs are OK with it, others are not.
  429. alignment = attrScalarAlignment;
  430. return true;
  431. }
  432. alignment = 1;
  433. return false;
  434. }
  435. public void PreFrame()
  436. {
  437. _syncManager.Cleanup();
  438. }
  439. public ICounterEvent ReportCounter(CounterType type, EventHandler<ulong> resultHandler, bool hostReserved)
  440. {
  441. return _counters.QueueReport(type, resultHandler, hostReserved);
  442. }
  443. public void ResetCounter(CounterType type)
  444. {
  445. _counters.QueueReset(type);
  446. }
  447. public void SetBufferData(BufferHandle buffer, int offset, ReadOnlySpan<byte> data)
  448. {
  449. BufferManager.SetData(buffer, offset, data, _pipeline.CurrentCommandBuffer, _pipeline.EndRenderPass);
  450. }
  451. public void UpdateCounters()
  452. {
  453. _counters.Update();
  454. }
  455. public void BackgroundContextAction(Action action, bool alwaysBackground = false)
  456. {
  457. action();
  458. }
  459. public void CreateSync(ulong id)
  460. {
  461. _syncManager.Create(id);
  462. }
  463. public IProgram LoadProgramBinary(byte[] programBinary, bool isFragment, ShaderInfo info)
  464. {
  465. throw new NotImplementedException();
  466. }
  467. public void WaitSync(ulong id)
  468. {
  469. _syncManager.Wait(id);
  470. }
  471. public void Screenshot()
  472. {
  473. _window.ScreenCaptureRequested = true;
  474. }
  475. public void OnScreenCaptured(ScreenCaptureImageInfo bitmap)
  476. {
  477. ScreenCaptured?.Invoke(this, bitmap);
  478. }
  479. public unsafe void Dispose()
  480. {
  481. CommandBufferPool.Dispose();
  482. BackgroundResources.Dispose();
  483. _counters.Dispose();
  484. _window.Dispose();
  485. HelperShader.Dispose();
  486. _pipeline.Dispose();
  487. BufferManager.Dispose();
  488. DescriptorSetManager.Dispose();
  489. PipelineLayoutCache.Dispose();
  490. MemoryAllocator.Dispose();
  491. if (_debugReportCallback.Handle != 0)
  492. {
  493. DebugReportApi.DestroyDebugReportCallback(_instance, _debugReportCallback, null);
  494. }
  495. foreach (var shader in Shaders)
  496. {
  497. shader.Dispose();
  498. }
  499. foreach (var texture in Textures)
  500. {
  501. texture.Release();
  502. }
  503. foreach (var sampler in Samplers)
  504. {
  505. sampler.Dispose();
  506. }
  507. if (!IsOffScreen)
  508. {
  509. SurfaceApi.DestroySurface(_instance, _surface, null);
  510. Api.DestroyDevice(_device, null);
  511. // Last step destroy the instance
  512. Api.DestroyInstance(_instance, null);
  513. }
  514. }
  515. }
  516. }