MemoryAllocator.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 Device _device;
  11. private readonly List<MemoryAllocatorBlockList> _blockLists;
  12. private int _blockAlignment;
  13. public MemoryAllocator(Vk api, Device device, uint maxMemoryAllocationCount)
  14. {
  15. _api = api;
  16. _device = device;
  17. _blockLists = new List<MemoryAllocatorBlockList>();
  18. _blockAlignment = (int)Math.Min(int.MaxValue, MaxDeviceMemoryUsageEstimate / (ulong)maxMemoryAllocationCount);
  19. }
  20. public MemoryAllocation AllocateDeviceMemory(
  21. PhysicalDevice physicalDevice,
  22. MemoryRequirements requirements,
  23. MemoryPropertyFlags flags = 0)
  24. {
  25. return AllocateDeviceMemory(physicalDevice, requirements, flags, flags);
  26. }
  27. public MemoryAllocation AllocateDeviceMemory(
  28. PhysicalDevice physicalDevice,
  29. MemoryRequirements requirements,
  30. MemoryPropertyFlags flags,
  31. MemoryPropertyFlags alternativeFlags)
  32. {
  33. int memoryTypeIndex = FindSuitableMemoryTypeIndex(_api, physicalDevice, requirements.MemoryTypeBits, flags, alternativeFlags);
  34. if (memoryTypeIndex < 0)
  35. {
  36. return default;
  37. }
  38. bool map = flags.HasFlag(MemoryPropertyFlags.HostVisibleBit);
  39. return Allocate(memoryTypeIndex, requirements.Size, requirements.Alignment, map);
  40. }
  41. private MemoryAllocation Allocate(int memoryTypeIndex, ulong size, ulong alignment, bool map)
  42. {
  43. for (int i = 0; i < _blockLists.Count; i++)
  44. {
  45. var bl = _blockLists[i];
  46. if (bl.MemoryTypeIndex == memoryTypeIndex)
  47. {
  48. lock (bl)
  49. {
  50. return bl.Allocate(size, alignment, map);
  51. }
  52. }
  53. }
  54. var newBl = new MemoryAllocatorBlockList(_api, _device, memoryTypeIndex, _blockAlignment);
  55. _blockLists.Add(newBl);
  56. return newBl.Allocate(size, alignment, map);
  57. }
  58. private static int FindSuitableMemoryTypeIndex(
  59. Vk api,
  60. PhysicalDevice physicalDevice,
  61. uint memoryTypeBits,
  62. MemoryPropertyFlags flags,
  63. MemoryPropertyFlags alternativeFlags)
  64. {
  65. int bestCandidateIndex = -1;
  66. api.GetPhysicalDeviceMemoryProperties(physicalDevice, out var properties);
  67. for (int i = 0; i < properties.MemoryTypeCount; i++)
  68. {
  69. var type = properties.MemoryTypes[i];
  70. if ((memoryTypeBits & (1 << i)) != 0)
  71. {
  72. if (type.PropertyFlags.HasFlag(flags))
  73. {
  74. return i;
  75. }
  76. else if (type.PropertyFlags.HasFlag(alternativeFlags))
  77. {
  78. bestCandidateIndex = i;
  79. }
  80. }
  81. }
  82. return bestCandidateIndex;
  83. }
  84. public void Dispose()
  85. {
  86. for (int i = 0; i < _blockLists.Count; i++)
  87. {
  88. _blockLists[i].Dispose();
  89. }
  90. }
  91. }
  92. }