Pool.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. namespace Ryujinx.Graphics.Gpu.Image
  3. {
  4. abstract class Pool<T> : IDisposable
  5. {
  6. protected const int DescriptorSize = 0x20;
  7. protected GpuContext Context;
  8. protected T[] Items;
  9. public ulong Address { get; }
  10. public ulong Size { get; }
  11. public Pool(GpuContext context, ulong address, int maximumId)
  12. {
  13. Context = context;
  14. int count = maximumId + 1;
  15. ulong size = (ulong)(uint)count * DescriptorSize;;
  16. Items = new T[count];
  17. Address = address;
  18. Size = size;
  19. }
  20. public abstract T Get(int id);
  21. public void SynchronizeMemory()
  22. {
  23. (ulong, ulong)[] modifiedRanges = Context.PhysicalMemory.GetModifiedRanges(Address, Size);
  24. for (int index = 0; index < modifiedRanges.Length; index++)
  25. {
  26. (ulong mAddress, ulong mSize) = modifiedRanges[index];
  27. if (mAddress < Address)
  28. {
  29. mAddress = Address;
  30. }
  31. ulong maxSize = Address + Size - mAddress;
  32. if (mSize > maxSize)
  33. {
  34. mSize = maxSize;
  35. }
  36. InvalidateRangeImpl(mAddress, mSize);
  37. }
  38. }
  39. public void InvalidateRange(ulong address, ulong size)
  40. {
  41. ulong endAddress = address + size;
  42. ulong texturePoolEndAddress = Address + Size;
  43. // If the range being invalidated is not overlapping the texture pool range,
  44. // then we don't have anything to do, exit early.
  45. if (address >= texturePoolEndAddress || endAddress <= Address)
  46. {
  47. return;
  48. }
  49. if (address < Address)
  50. {
  51. address = Address;
  52. }
  53. if (endAddress > texturePoolEndAddress)
  54. {
  55. endAddress = texturePoolEndAddress;
  56. }
  57. InvalidateRangeImpl(address, size);
  58. }
  59. protected abstract void InvalidateRangeImpl(ulong address, ulong size);
  60. protected abstract void Delete(T item);
  61. public void Dispose()
  62. {
  63. if (Items != null)
  64. {
  65. for (int index = 0; index < Items.Length; index++)
  66. {
  67. Delete(Items[index]);
  68. }
  69. Items = null;
  70. }
  71. }
  72. }
  73. }