Pool.cs 2.5 KB

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