Pool.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using Ryujinx.Graphics.Gpu.Memory;
  2. using System;
  3. namespace Ryujinx.Graphics.Gpu.Image
  4. {
  5. /// <summary>
  6. /// Represents a pool of GPU resources, such as samplers or textures.
  7. /// </summary>
  8. /// <typeparam name="T">Type of the GPU resource</typeparam>
  9. abstract class Pool<T> : IDisposable
  10. {
  11. protected const int DescriptorSize = 0x20;
  12. protected GpuContext Context;
  13. protected T[] Items;
  14. /// <summary>
  15. /// The maximum ID value of resources on the pool (inclusive).
  16. /// </summary>
  17. /// <remarks>
  18. /// The maximum amount of resources on the pool is equal to this value plus one.
  19. /// </remarks>
  20. public int MaximumId { get; }
  21. /// <summary>
  22. /// The address of the pool in guest memory.
  23. /// </summary>
  24. public ulong Address { get; }
  25. /// <summary>
  26. /// The size of the pool in bytes.
  27. /// </summary>
  28. public ulong Size { get; }
  29. public Pool(GpuContext context, ulong address, int maximumId)
  30. {
  31. Context = context;
  32. MaximumId = maximumId;
  33. int count = maximumId + 1;
  34. ulong size = (ulong)(uint)count * DescriptorSize;;
  35. Items = new T[count];
  36. Address = address;
  37. Size = size;
  38. }
  39. /// <summary>
  40. /// Gets the GPU resource with the given ID.
  41. /// </summary>
  42. /// <param name="id">ID of the resource. This is effectively a zero-based index</param>
  43. /// <returns>The GPU resource with the given ID</returns>
  44. public abstract T Get(int id);
  45. /// <summary>
  46. /// Synchronizes host memory with guest memory.
  47. /// This causes invalidation of pool entries,
  48. /// if a modification of entries by the CPU is detected.
  49. /// </summary>
  50. public void SynchronizeMemory()
  51. {
  52. (ulong, ulong)[] modifiedRanges = Context.PhysicalMemory.GetModifiedRanges(Address, Size, ResourceName.TexturePool);
  53. for (int index = 0; index < modifiedRanges.Length; index++)
  54. {
  55. (ulong mAddress, ulong mSize) = modifiedRanges[index];
  56. if (mAddress < Address)
  57. {
  58. mAddress = Address;
  59. }
  60. ulong maxSize = Address + Size - mAddress;
  61. if (mSize > maxSize)
  62. {
  63. mSize = maxSize;
  64. }
  65. InvalidateRangeImpl(mAddress, mSize);
  66. }
  67. }
  68. /// <summary>
  69. /// Invalidates a range of memory of the GPU resource pool.
  70. /// Entries that falls inside the speicified range will be invalidated,
  71. /// causing all the data to be reloaded from guest memory.
  72. /// </summary>
  73. /// <param name="address">The start address of the range to invalidate</param>
  74. /// <param name="size">The size of the range to invalidate</param>
  75. public void InvalidateRange(ulong address, ulong size)
  76. {
  77. ulong endAddress = address + size;
  78. ulong texturePoolEndAddress = Address + Size;
  79. // If the range being invalidated is not overlapping the texture pool range,
  80. // then we don't have anything to do, exit early.
  81. if (address >= texturePoolEndAddress || endAddress <= Address)
  82. {
  83. return;
  84. }
  85. if (address < Address)
  86. {
  87. address = Address;
  88. }
  89. if (endAddress > texturePoolEndAddress)
  90. {
  91. endAddress = texturePoolEndAddress;
  92. }
  93. size = endAddress - address;
  94. InvalidateRangeImpl(address, size);
  95. }
  96. protected abstract void InvalidateRangeImpl(ulong address, ulong size);
  97. protected abstract void Delete(T item);
  98. /// <summary>
  99. /// Performs the disposal of all resources stored on the pool.
  100. /// It's an error to try using the pool after disposal.
  101. /// </summary>
  102. public void Dispose()
  103. {
  104. if (Items != null)
  105. {
  106. for (int index = 0; index < Items.Length; index++)
  107. {
  108. Delete(Items[index]);
  109. }
  110. Items = null;
  111. }
  112. }
  113. }
  114. }