SamplerPool.cs 3.2 KB

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