SamplerPool.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System;
  2. using System.Runtime.InteropServices;
  3. namespace Ryujinx.Graphics.Gpu.Image
  4. {
  5. /// <summary>
  6. /// Sampler pool.
  7. /// </summary>
  8. class SamplerPool : Pool<Sampler>
  9. {
  10. private int _sequenceNumber;
  11. /// <summary>
  12. /// Constructs a new instance of the sampler pool.
  13. /// </summary>
  14. /// <param name="context">GPU context that the sampler pool belongs to</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, ulong address, int maximumId) : base(context, 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. ulong address = Address + (ulong)(uint)id * DescriptorSize;
  38. ReadOnlySpan<byte> data = Context.PhysicalMemory.GetSpan(address, DescriptorSize);
  39. SamplerDescriptor descriptor = MemoryMarshal.Cast<byte, SamplerDescriptor>(data)[0];
  40. sampler = new Sampler(Context, descriptor);
  41. Items[id] = sampler;
  42. }
  43. return sampler;
  44. }
  45. /// <summary>
  46. /// Implementation of the sampler pool range invalidation.
  47. /// </summary>
  48. /// <param name="address">Start address of the range of the sampler pool</param>
  49. /// <param name="size">Size of the range being invalidated</param>
  50. protected override void InvalidateRangeImpl(ulong address, ulong size)
  51. {
  52. ulong endAddress = address + size;
  53. for (; address < endAddress; address += DescriptorSize)
  54. {
  55. int id = (int)((address - Address) / DescriptorSize);
  56. Sampler sampler = Items[id];
  57. if (sampler != null)
  58. {
  59. sampler.Dispose();
  60. Items[id] = null;
  61. }
  62. }
  63. }
  64. /// <summary>
  65. /// Deletes a given sampler pool entry.
  66. /// The host memory used by the sampler is released by the driver.
  67. /// </summary>
  68. /// <param name="item">The entry to be deleted</param>
  69. protected override void Delete(Sampler item)
  70. {
  71. item?.Dispose();
  72. }
  73. }
  74. }