SamplerPool.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using Ryujinx.Graphics.Gpu.Memory;
  2. using System.Collections.Generic;
  3. namespace Ryujinx.Graphics.Gpu.Image
  4. {
  5. /// <summary>
  6. /// Sampler pool.
  7. /// </summary>
  8. class SamplerPool : Pool<Sampler, SamplerDescriptor>, IPool<SamplerPool>
  9. {
  10. private float _forcedAnisotropy;
  11. /// <summary>
  12. /// Linked list node used on the sampler pool cache.
  13. /// </summary>
  14. public LinkedListNode<SamplerPool> CacheNode { get; set; }
  15. /// <summary>
  16. /// Timestamp used by the sampler pool cache, updated on every use of this sampler pool.
  17. /// </summary>
  18. public ulong CacheTimestamp { get; set; }
  19. /// <summary>
  20. /// Creates a new instance of the sampler pool.
  21. /// </summary>
  22. /// <param name="context">GPU context that the sampler pool belongs to</param>
  23. /// <param name="physicalMemory">Physical memory where the sampler descriptors are mapped</param>
  24. /// <param name="address">Address of the sampler pool in guest memory</param>
  25. /// <param name="maximumId">Maximum sampler ID of the sampler pool (equal to maximum samplers minus one)</param>
  26. public SamplerPool(GpuContext context, PhysicalMemory physicalMemory, ulong address, int maximumId) : base(context, physicalMemory, address, maximumId)
  27. {
  28. _forcedAnisotropy = GraphicsConfig.MaxAnisotropy;
  29. }
  30. /// <summary>
  31. /// Gets the sampler with the given ID.
  32. /// </summary>
  33. /// <param name="id">ID of the sampler. This is effectively a zero-based index</param>
  34. /// <returns>The sampler with the given ID</returns>
  35. public override Sampler Get(int id)
  36. {
  37. if ((uint)id >= Items.Length)
  38. {
  39. return null;
  40. }
  41. if (SequenceNumber != Context.SequenceNumber)
  42. {
  43. if (_forcedAnisotropy != GraphicsConfig.MaxAnisotropy)
  44. {
  45. _forcedAnisotropy = GraphicsConfig.MaxAnisotropy;
  46. for (int i = 0; i < Items.Length; i++)
  47. {
  48. if (Items[i] != null)
  49. {
  50. Items[i].Dispose();
  51. Items[i] = null;
  52. }
  53. }
  54. UpdateModifiedSequence();
  55. }
  56. SequenceNumber = Context.SequenceNumber;
  57. SynchronizeMemory();
  58. }
  59. Sampler sampler = Items[id];
  60. if (sampler == null)
  61. {
  62. SamplerDescriptor descriptor = GetDescriptor(id);
  63. sampler = new Sampler(Context, descriptor);
  64. Items[id] = sampler;
  65. DescriptorCache[id] = descriptor;
  66. }
  67. return sampler;
  68. }
  69. /// <summary>
  70. /// Checks if the pool was modified, and returns the last sequence number where a modification was detected.
  71. /// </summary>
  72. /// <returns>A number that increments each time a modification is detected</returns>
  73. public int CheckModified()
  74. {
  75. if (SequenceNumber != Context.SequenceNumber)
  76. {
  77. SequenceNumber = Context.SequenceNumber;
  78. if (_forcedAnisotropy != GraphicsConfig.MaxAnisotropy)
  79. {
  80. _forcedAnisotropy = GraphicsConfig.MaxAnisotropy;
  81. for (int i = 0; i < Items.Length; i++)
  82. {
  83. if (Items[i] != null)
  84. {
  85. Items[i].Dispose();
  86. Items[i] = null;
  87. }
  88. }
  89. UpdateModifiedSequence();
  90. }
  91. SynchronizeMemory();
  92. }
  93. return ModifiedSequenceNumber;
  94. }
  95. /// <summary>
  96. /// Implementation of the sampler pool range invalidation.
  97. /// </summary>
  98. /// <param name="address">Start address of the range of the sampler pool</param>
  99. /// <param name="size">Size of the range being invalidated</param>
  100. protected override void InvalidateRangeImpl(ulong address, ulong size)
  101. {
  102. ulong endAddress = address + size;
  103. for (; address < endAddress; address += DescriptorSize)
  104. {
  105. int id = (int)((address - Address) / DescriptorSize);
  106. Sampler sampler = Items[id];
  107. if (sampler != null)
  108. {
  109. SamplerDescriptor descriptor = GetDescriptor(id);
  110. // If the descriptors are the same, the sampler is still valid.
  111. if (descriptor.Equals(ref DescriptorCache[id]))
  112. {
  113. continue;
  114. }
  115. sampler.Dispose();
  116. Items[id] = null;
  117. }
  118. }
  119. }
  120. /// <summary>
  121. /// Deletes a given sampler pool entry.
  122. /// The host memory used by the sampler is released by the driver.
  123. /// </summary>
  124. /// <param name="item">The entry to be deleted</param>
  125. protected override void Delete(Sampler item)
  126. {
  127. item?.Dispose();
  128. }
  129. }
  130. }