SamplerPool.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. UpdateModifiedSequence();
  46. }
  47. SequenceNumber = Context.SequenceNumber;
  48. SynchronizeMemory();
  49. }
  50. Sampler sampler = Items[id];
  51. if (sampler == null)
  52. {
  53. SamplerDescriptor descriptor = GetDescriptor(id);
  54. sampler = new Sampler(Context, descriptor);
  55. Items[id] = sampler;
  56. DescriptorCache[id] = descriptor;
  57. }
  58. return sampler;
  59. }
  60. /// <summary>
  61. /// Checks if the pool was modified, and returns the last sequence number where a modification was detected.
  62. /// </summary>
  63. /// <returns>A number that increments each time a modification is detected</returns>
  64. public int CheckModified()
  65. {
  66. if (SequenceNumber != Context.SequenceNumber)
  67. {
  68. SequenceNumber = Context.SequenceNumber;
  69. if (_forcedAnisotropy != GraphicsConfig.MaxAnisotropy)
  70. {
  71. _forcedAnisotropy = GraphicsConfig.MaxAnisotropy;
  72. for (int i = 0; i < Items.Length; i++)
  73. {
  74. if (Items[i] != null)
  75. {
  76. Items[i].Dispose();
  77. Items[i] = null;
  78. }
  79. }
  80. UpdateModifiedSequence();
  81. }
  82. SynchronizeMemory();
  83. }
  84. return ModifiedSequenceNumber;
  85. }
  86. /// <summary>
  87. /// Implementation of the sampler pool range invalidation.
  88. /// </summary>
  89. /// <param name="address">Start address of the range of the sampler pool</param>
  90. /// <param name="size">Size of the range being invalidated</param>
  91. protected override void InvalidateRangeImpl(ulong address, ulong size)
  92. {
  93. ulong endAddress = address + size;
  94. for (; address < endAddress; address += DescriptorSize)
  95. {
  96. int id = (int)((address - Address) / DescriptorSize);
  97. Sampler sampler = Items[id];
  98. if (sampler != null)
  99. {
  100. SamplerDescriptor descriptor = GetDescriptor(id);
  101. // If the descriptors are the same, the sampler is still valid.
  102. if (descriptor.Equals(ref DescriptorCache[id]))
  103. {
  104. continue;
  105. }
  106. sampler.Dispose();
  107. Items[id] = null;
  108. }
  109. }
  110. }
  111. /// <summary>
  112. /// Deletes a given sampler pool entry.
  113. /// The host memory used by the sampler is released by the driver.
  114. /// </summary>
  115. /// <param name="item">The entry to be deleted</param>
  116. protected override void Delete(Sampler item)
  117. {
  118. item?.Dispose();
  119. }
  120. }
  121. }