SamplerPool.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using Ryujinx.Graphics.Gpu.Memory;
  2. namespace Ryujinx.Graphics.Gpu.Image
  3. {
  4. /// <summary>
  5. /// Sampler pool.
  6. /// </summary>
  7. class SamplerPool : Pool<Sampler, SamplerDescriptor>
  8. {
  9. /// <summary>
  10. /// Constructs a new instance of the sampler pool.
  11. /// </summary>
  12. /// <param name="context">GPU context that the sampler pool belongs to</param>
  13. /// <param name="physicalMemory">Physical memory where the sampler descriptors are mapped</param>
  14. /// <param name="address">Address of the sampler pool in guest memory</param>
  15. /// <param name="maximumId">Maximum sampler ID of the sampler pool (equal to maximum samplers minus one)</param>
  16. public SamplerPool(GpuContext context, PhysicalMemory physicalMemory, ulong address, int maximumId) : base(context, physicalMemory, address, maximumId) { }
  17. /// <summary>
  18. /// Gets the sampler with the given ID.
  19. /// </summary>
  20. /// <param name="id">ID of the sampler. This is effectively a zero-based index</param>
  21. /// <returns>The sampler with the given ID</returns>
  22. public override Sampler Get(int id)
  23. {
  24. if ((uint)id >= Items.Length)
  25. {
  26. return null;
  27. }
  28. if (SequenceNumber != Context.SequenceNumber)
  29. {
  30. SequenceNumber = Context.SequenceNumber;
  31. SynchronizeMemory();
  32. }
  33. Sampler sampler = Items[id];
  34. if (sampler == null)
  35. {
  36. SamplerDescriptor descriptor = GetDescriptor(id);
  37. sampler = new Sampler(Context, descriptor);
  38. Items[id] = sampler;
  39. DescriptorCache[id] = descriptor;
  40. }
  41. return sampler;
  42. }
  43. /// <summary>
  44. /// Implementation of the sampler pool range invalidation.
  45. /// </summary>
  46. /// <param name="address">Start address of the range of the sampler pool</param>
  47. /// <param name="size">Size of the range being invalidated</param>
  48. protected override void InvalidateRangeImpl(ulong address, ulong size)
  49. {
  50. ulong endAddress = address + size;
  51. for (; address < endAddress; address += DescriptorSize)
  52. {
  53. int id = (int)((address - Address) / DescriptorSize);
  54. Sampler sampler = Items[id];
  55. if (sampler != null)
  56. {
  57. SamplerDescriptor descriptor = GetDescriptor(id);
  58. // If the descriptors are the same, the sampler is still valid.
  59. if (descriptor.Equals(ref DescriptorCache[id]))
  60. {
  61. continue;
  62. }
  63. sampler.Dispose();
  64. Items[id] = null;
  65. }
  66. }
  67. }
  68. /// <summary>
  69. /// Deletes a given sampler pool entry.
  70. /// The host memory used by the sampler is released by the driver.
  71. /// </summary>
  72. /// <param name="item">The entry to be deleted</param>
  73. protected override void Delete(Sampler item)
  74. {
  75. item?.Dispose();
  76. }
  77. }
  78. }