Pool.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using Ryujinx.Common;
  2. using Ryujinx.Cpu.Tracking;
  3. using Ryujinx.Graphics.Gpu.Memory;
  4. using System;
  5. namespace Ryujinx.Graphics.Gpu.Image
  6. {
  7. /// <summary>
  8. /// Represents a pool of GPU resources, such as samplers or textures.
  9. /// </summary>
  10. /// <typeparam name="T1">Type of the GPU resource</typeparam>
  11. /// <typeparam name="T2">Type of the descriptor</typeparam>
  12. abstract class Pool<T1, T2> : IDisposable where T2 : unmanaged
  13. {
  14. protected const int DescriptorSize = 0x20;
  15. protected GpuContext Context;
  16. protected T1[] Items;
  17. protected T2[] DescriptorCache;
  18. /// <summary>
  19. /// The maximum ID value of resources on the pool (inclusive).
  20. /// </summary>
  21. /// <remarks>
  22. /// The maximum amount of resources on the pool is equal to this value plus one.
  23. /// </remarks>
  24. public int MaximumId { get; }
  25. /// <summary>
  26. /// The address of the pool in guest memory.
  27. /// </summary>
  28. public ulong Address { get; }
  29. /// <summary>
  30. /// The size of the pool in bytes.
  31. /// </summary>
  32. public ulong Size { get; }
  33. private readonly CpuMultiRegionHandle _memoryTracking;
  34. private readonly Action<ulong, ulong> _modifiedDelegate;
  35. public Pool(GpuContext context, ulong address, int maximumId)
  36. {
  37. Context = context;
  38. MaximumId = maximumId;
  39. int count = maximumId + 1;
  40. ulong size = (ulong)(uint)count * DescriptorSize;
  41. Items = new T1[count];
  42. DescriptorCache = new T2[count];
  43. Address = address;
  44. Size = size;
  45. _memoryTracking = context.PhysicalMemory.BeginGranularTracking(address, size);
  46. _modifiedDelegate = RegionModified;
  47. }
  48. /// <summary>
  49. /// Gets the descriptor for a given ID.
  50. /// </summary>
  51. /// <param name="id">ID of the descriptor. This is effectively a zero-based index</param>
  52. /// <returns>The descriptor</returns>
  53. public T2 GetDescriptor(int id)
  54. {
  55. return Context.PhysicalMemory.Read<T2>(Address + (ulong)id * DescriptorSize);
  56. }
  57. /// <summary>
  58. /// Gets the GPU resource with the given ID.
  59. /// </summary>
  60. /// <param name="id">ID of the resource. This is effectively a zero-based index</param>
  61. /// <returns>The GPU resource with the given ID</returns>
  62. public abstract T1 Get(int id);
  63. /// <summary>
  64. /// Synchronizes host memory with guest memory.
  65. /// This causes invalidation of pool entries,
  66. /// if a modification of entries by the CPU is detected.
  67. /// </summary>
  68. public void SynchronizeMemory()
  69. {
  70. _memoryTracking.QueryModified(_modifiedDelegate);
  71. }
  72. /// <summary>
  73. /// Indicate that a region of the pool was modified, and must be loaded from memory.
  74. /// </summary>
  75. /// <param name="mAddress">Start address of the modified region</param>
  76. /// <param name="mSize">Size of the modified region</param>
  77. private void RegionModified(ulong mAddress, ulong mSize)
  78. {
  79. if (mAddress < Address)
  80. {
  81. mAddress = Address;
  82. }
  83. ulong maxSize = Address + Size - mAddress;
  84. if (mSize > maxSize)
  85. {
  86. mSize = maxSize;
  87. }
  88. InvalidateRangeImpl(mAddress, mSize);
  89. }
  90. protected abstract void InvalidateRangeImpl(ulong address, ulong size);
  91. protected abstract void Delete(T1 item);
  92. /// <summary>
  93. /// Performs the disposal of all resources stored on the pool.
  94. /// It's an error to try using the pool after disposal.
  95. /// </summary>
  96. public void Dispose()
  97. {
  98. if (Items != null)
  99. {
  100. for (int index = 0; index < Items.Length; index++)
  101. {
  102. Delete(Items[index]);
  103. }
  104. Items = null;
  105. }
  106. _memoryTracking.Dispose();
  107. }
  108. }
  109. }