SamplerPool.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. namespace Ryujinx.Graphics.Gpu.Image
  2. {
  3. /// <summary>
  4. /// Sampler pool.
  5. /// </summary>
  6. class SamplerPool : Pool<Sampler>
  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 = Context.PhysicalMemory.Read<SamplerDescriptor>(Address + (ulong)id * DescriptorSize);
  36. sampler = new Sampler(Context, descriptor);
  37. Items[id] = sampler;
  38. }
  39. return sampler;
  40. }
  41. /// <summary>
  42. /// Implementation of the sampler pool range invalidation.
  43. /// </summary>
  44. /// <param name="address">Start address of the range of the sampler pool</param>
  45. /// <param name="size">Size of the range being invalidated</param>
  46. protected override void InvalidateRangeImpl(ulong address, ulong size)
  47. {
  48. ulong endAddress = address + size;
  49. for (; address < endAddress; address += DescriptorSize)
  50. {
  51. int id = (int)((address - Address) / DescriptorSize);
  52. Sampler sampler = Items[id];
  53. if (sampler != null)
  54. {
  55. sampler.Dispose();
  56. Items[id] = null;
  57. }
  58. }
  59. }
  60. /// <summary>
  61. /// Deletes a given sampler pool entry.
  62. /// The host memory used by the sampler is released by the driver.
  63. /// </summary>
  64. /// <param name="item">The entry to be deleted</param>
  65. protected override void Delete(Sampler item)
  66. {
  67. item?.Dispose();
  68. }
  69. }
  70. }