SamplerPool.cs 3.0 KB

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