SamplerPool.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 float _forcedAnisotropy;
  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. {
  19. _forcedAnisotropy = GraphicsConfig.MaxAnisotropy;
  20. }
  21. /// <summary>
  22. /// Gets the sampler with the given ID.
  23. /// </summary>
  24. /// <param name="id">ID of the sampler. This is effectively a zero-based index</param>
  25. /// <returns>The sampler with the given ID</returns>
  26. public override Sampler Get(int id)
  27. {
  28. if ((uint)id >= Items.Length)
  29. {
  30. return null;
  31. }
  32. if (SequenceNumber != Context.SequenceNumber)
  33. {
  34. if (_forcedAnisotropy != GraphicsConfig.MaxAnisotropy)
  35. {
  36. _forcedAnisotropy = GraphicsConfig.MaxAnisotropy;
  37. for (int i = 0; i < Items.Length; i++)
  38. {
  39. if (Items[i] != null)
  40. {
  41. Items[i].Dispose();
  42. Items[i] = null;
  43. }
  44. }
  45. }
  46. SequenceNumber = Context.SequenceNumber;
  47. SynchronizeMemory();
  48. }
  49. Sampler sampler = Items[id];
  50. if (sampler == null)
  51. {
  52. SamplerDescriptor descriptor = GetDescriptor(id);
  53. sampler = new Sampler(Context, descriptor);
  54. Items[id] = sampler;
  55. DescriptorCache[id] = descriptor;
  56. }
  57. return sampler;
  58. }
  59. /// <summary>
  60. /// Implementation of the sampler pool range invalidation.
  61. /// </summary>
  62. /// <param name="address">Start address of the range of the sampler pool</param>
  63. /// <param name="size">Size of the range being invalidated</param>
  64. protected override void InvalidateRangeImpl(ulong address, ulong size)
  65. {
  66. ulong endAddress = address + size;
  67. for (; address < endAddress; address += DescriptorSize)
  68. {
  69. int id = (int)((address - Address) / DescriptorSize);
  70. Sampler sampler = Items[id];
  71. if (sampler != null)
  72. {
  73. SamplerDescriptor descriptor = GetDescriptor(id);
  74. // If the descriptors are the same, the sampler is still valid.
  75. if (descriptor.Equals(ref DescriptorCache[id]))
  76. {
  77. continue;
  78. }
  79. sampler.Dispose();
  80. Items[id] = null;
  81. }
  82. }
  83. }
  84. /// <summary>
  85. /// Deletes a given sampler pool entry.
  86. /// The host memory used by the sampler is released by the driver.
  87. /// </summary>
  88. /// <param name="item">The entry to be deleted</param>
  89. protected override void Delete(Sampler item)
  90. {
  91. item?.Dispose();
  92. }
  93. }
  94. }