Pool.cs 2.6 KB

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