VulkanPhysicalDevice.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using Ryujinx.Common.Utilities;
  2. using Ryujinx.Graphics.GAL;
  3. using Silk.NET.Vulkan;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Collections.Immutable;
  7. using System.Linq;
  8. using System.Runtime.InteropServices;
  9. namespace Ryujinx.Graphics.Vulkan
  10. {
  11. readonly struct VulkanPhysicalDevice
  12. {
  13. public readonly PhysicalDevice PhysicalDevice;
  14. public readonly PhysicalDeviceFeatures PhysicalDeviceFeatures;
  15. public readonly PhysicalDeviceProperties PhysicalDeviceProperties;
  16. public readonly PhysicalDeviceMemoryProperties PhysicalDeviceMemoryProperties;
  17. public readonly QueueFamilyProperties[] QueueFamilyProperties;
  18. public readonly string DeviceName;
  19. public readonly IReadOnlySet<string> DeviceExtensions;
  20. public VulkanPhysicalDevice(Vk api, PhysicalDevice physicalDevice)
  21. {
  22. PhysicalDevice = physicalDevice;
  23. PhysicalDeviceFeatures = api.GetPhysicalDeviceFeature(PhysicalDevice);
  24. api.GetPhysicalDeviceProperties(PhysicalDevice, out var physicalDeviceProperties);
  25. PhysicalDeviceProperties = physicalDeviceProperties;
  26. api.GetPhysicalDeviceMemoryProperties(PhysicalDevice, out PhysicalDeviceMemoryProperties);
  27. unsafe
  28. {
  29. DeviceName = Marshal.PtrToStringAnsi((IntPtr)physicalDeviceProperties.DeviceName);
  30. }
  31. uint propertiesCount = 0;
  32. api.GetPhysicalDeviceQueueFamilyProperties(physicalDevice, SpanHelpers.AsSpan(ref propertiesCount), Span<QueueFamilyProperties>.Empty);
  33. QueueFamilyProperties = new QueueFamilyProperties[propertiesCount];
  34. api.GetPhysicalDeviceQueueFamilyProperties(physicalDevice, SpanHelpers.AsSpan(ref propertiesCount), QueueFamilyProperties);
  35. api.EnumerateDeviceExtensionProperties(PhysicalDevice, Span<byte>.Empty, SpanHelpers.AsSpan(ref propertiesCount), Span<ExtensionProperties>.Empty).ThrowOnError();
  36. ExtensionProperties[] extensionProperties = new ExtensionProperties[propertiesCount];
  37. api.EnumerateDeviceExtensionProperties(PhysicalDevice, Span<byte>.Empty, SpanHelpers.AsSpan(ref propertiesCount), extensionProperties).ThrowOnError();
  38. unsafe
  39. {
  40. DeviceExtensions = extensionProperties.Select(x => Marshal.PtrToStringAnsi((IntPtr)x.ExtensionName)).ToImmutableHashSet();
  41. }
  42. }
  43. public string Id => $"0x{PhysicalDeviceProperties.VendorID:X}_0x{PhysicalDeviceProperties.DeviceID:X}";
  44. public bool IsDeviceExtensionPresent(string extension) => DeviceExtensions.Contains(extension);
  45. public DeviceInfo ToDeviceInfo()
  46. {
  47. return new DeviceInfo(
  48. Id,
  49. VendorUtils.GetNameFromId(PhysicalDeviceProperties.VendorID),
  50. DeviceName,
  51. PhysicalDeviceProperties.DeviceType == PhysicalDeviceType.DiscreteGpu);
  52. }
  53. }
  54. }