MemoryAllocator.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using Silk.NET.Vulkan;
  2. using System;
  3. using System.Collections.Generic;
  4. namespace Ryujinx.Graphics.Vulkan
  5. {
  6. class MemoryAllocator : IDisposable
  7. {
  8. private ulong MaxDeviceMemoryUsageEstimate = 16UL * 1024 * 1024 * 1024;
  9. private readonly Vk _api;
  10. private readonly PhysicalDevice _physicalDevice;
  11. private readonly Device _device;
  12. private readonly List<MemoryAllocatorBlockList> _blockLists;
  13. private readonly int _blockAlignment;
  14. private readonly PhysicalDeviceMemoryProperties _physicalDeviceMemoryProperties;
  15. public MemoryAllocator(Vk api, PhysicalDevice physicalDevice, Device device, uint maxMemoryAllocationCount)
  16. {
  17. _api = api;
  18. _physicalDevice = physicalDevice;
  19. _device = device;
  20. _blockLists = new List<MemoryAllocatorBlockList>();
  21. _blockAlignment = (int)Math.Min(int.MaxValue, MaxDeviceMemoryUsageEstimate / (ulong)maxMemoryAllocationCount);
  22. _api.GetPhysicalDeviceMemoryProperties(_physicalDevice, out _physicalDeviceMemoryProperties);
  23. }
  24. public MemoryAllocation AllocateDeviceMemory(
  25. MemoryRequirements requirements,
  26. MemoryPropertyFlags flags = 0,
  27. bool isBuffer = false)
  28. {
  29. int memoryTypeIndex = FindSuitableMemoryTypeIndex(requirements.MemoryTypeBits, flags);
  30. if (memoryTypeIndex < 0)
  31. {
  32. return default;
  33. }
  34. bool map = flags.HasFlag(MemoryPropertyFlags.HostVisibleBit);
  35. return Allocate(memoryTypeIndex, requirements.Size, requirements.Alignment, map, isBuffer);
  36. }
  37. private MemoryAllocation Allocate(int memoryTypeIndex, ulong size, ulong alignment, bool map, bool isBuffer)
  38. {
  39. for (int i = 0; i < _blockLists.Count; i++)
  40. {
  41. var bl = _blockLists[i];
  42. if (bl.MemoryTypeIndex == memoryTypeIndex && bl.ForBuffer == isBuffer)
  43. {
  44. lock (bl)
  45. {
  46. return bl.Allocate(size, alignment, map);
  47. }
  48. }
  49. }
  50. var newBl = new MemoryAllocatorBlockList(_api, _device, memoryTypeIndex, _blockAlignment, isBuffer);
  51. _blockLists.Add(newBl);
  52. return newBl.Allocate(size, alignment, map);
  53. }
  54. private int FindSuitableMemoryTypeIndex(
  55. uint memoryTypeBits,
  56. MemoryPropertyFlags flags)
  57. {
  58. for (int i = 0; i < _physicalDeviceMemoryProperties.MemoryTypeCount; i++)
  59. {
  60. var type = _physicalDeviceMemoryProperties.MemoryTypes[i];
  61. if ((memoryTypeBits & (1 << i)) != 0)
  62. {
  63. if (type.PropertyFlags.HasFlag(flags))
  64. {
  65. return i;
  66. }
  67. }
  68. }
  69. return -1;
  70. }
  71. public static bool IsDeviceMemoryShared(Vk api, PhysicalDevice physicalDevice)
  72. {
  73. // The device is regarded as having shared memory if all heaps have the device local bit.
  74. api.GetPhysicalDeviceMemoryProperties(physicalDevice, out var properties);
  75. for (int i = 0; i < properties.MemoryHeapCount; i++)
  76. {
  77. if (!properties.MemoryHeaps[i].Flags.HasFlag(MemoryHeapFlags.DeviceLocalBit))
  78. {
  79. return false;
  80. }
  81. }
  82. return true;
  83. }
  84. public void Dispose()
  85. {
  86. for (int i = 0; i < _blockLists.Count; i++)
  87. {
  88. _blockLists[i].Dispose();
  89. }
  90. }
  91. }
  92. }