Pool.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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="T">Type of the GPU resource</typeparam>
  11. abstract class Pool<T> : IDisposable
  12. {
  13. protected const int DescriptorSize = 0x20;
  14. protected GpuContext Context;
  15. protected T[] Items;
  16. /// <summary>
  17. /// The maximum ID value of resources on the pool (inclusive).
  18. /// </summary>
  19. /// <remarks>
  20. /// The maximum amount of resources on the pool is equal to this value plus one.
  21. /// </remarks>
  22. public int MaximumId { get; }
  23. /// <summary>
  24. /// The address of the pool in guest memory.
  25. /// </summary>
  26. public ulong Address { get; }
  27. /// <summary>
  28. /// The size of the pool in bytes.
  29. /// </summary>
  30. public ulong Size { get; }
  31. private readonly CpuMultiRegionHandle _memoryTracking;
  32. public Pool(GpuContext context, ulong address, int maximumId)
  33. {
  34. Context = context;
  35. MaximumId = maximumId;
  36. int count = maximumId + 1;
  37. ulong size = (ulong)(uint)count * DescriptorSize;;
  38. Items = new T[count];
  39. Address = address;
  40. Size = size;
  41. _memoryTracking = context.PhysicalMemory.BeginGranularTracking(address, size);
  42. }
  43. /// <summary>
  44. /// Gets the GPU resource with the given ID.
  45. /// </summary>
  46. /// <param name="id">ID of the resource. This is effectively a zero-based index</param>
  47. /// <returns>The GPU resource with the given ID</returns>
  48. public abstract T Get(int id);
  49. /// <summary>
  50. /// Synchronizes host memory with guest memory.
  51. /// This causes invalidation of pool entries,
  52. /// if a modification of entries by the CPU is detected.
  53. /// </summary>
  54. public void SynchronizeMemory()
  55. {
  56. _memoryTracking.QueryModified((ulong mAddress, ulong mSize) =>
  57. {
  58. if (mAddress < Address)
  59. {
  60. mAddress = Address;
  61. }
  62. ulong maxSize = Address + Size - mAddress;
  63. if (mSize > maxSize)
  64. {
  65. mSize = maxSize;
  66. }
  67. InvalidateRangeImpl(mAddress, mSize);
  68. });
  69. }
  70. protected abstract void InvalidateRangeImpl(ulong address, ulong size);
  71. protected abstract void Delete(T item);
  72. /// <summary>
  73. /// Performs the disposal of all resources stored on the pool.
  74. /// It's an error to try using the pool after disposal.
  75. /// </summary>
  76. public void Dispose()
  77. {
  78. if (Items != null)
  79. {
  80. for (int index = 0; index < Items.Length; index++)
  81. {
  82. Delete(Items[index]);
  83. }
  84. Items = null;
  85. }
  86. _memoryTracking.Dispose();
  87. }
  88. }
  89. }