Sampler.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using Ryujinx.Graphics.GAL;
  2. using System;
  3. namespace Ryujinx.Graphics.Gpu.Image
  4. {
  5. /// <summary>
  6. /// Cached sampler entry for sampler pools.
  7. /// </summary>
  8. class Sampler : IDisposable
  9. {
  10. /// <summary>
  11. /// True if the sampler is disposed, false otherwise.
  12. /// </summary>
  13. public bool IsDisposed { get; private set; }
  14. /// <summary>
  15. /// Host sampler object.
  16. /// </summary>
  17. private readonly ISampler _hostSampler;
  18. /// <summary>
  19. /// Host sampler object, with anisotropy forced.
  20. /// </summary>
  21. private readonly ISampler _anisoSampler;
  22. /// <summary>
  23. /// Creates a new instance of the cached sampler.
  24. /// </summary>
  25. /// <param name="context">The GPU context the sampler belongs to</param>
  26. /// <param name="descriptor">The Maxwell sampler descriptor</param>
  27. public Sampler(GpuContext context, SamplerDescriptor descriptor)
  28. {
  29. MinFilter minFilter = descriptor.UnpackMinFilter();
  30. MagFilter magFilter = descriptor.UnpackMagFilter();
  31. bool seamlessCubemap = descriptor.UnpackSeamlessCubemap();
  32. AddressMode addressU = descriptor.UnpackAddressU();
  33. AddressMode addressV = descriptor.UnpackAddressV();
  34. AddressMode addressP = descriptor.UnpackAddressP();
  35. CompareMode compareMode = descriptor.UnpackCompareMode();
  36. CompareOp compareOp = descriptor.UnpackCompareOp();
  37. ColorF color = new ColorF(
  38. descriptor.BorderColorR,
  39. descriptor.BorderColorG,
  40. descriptor.BorderColorB,
  41. descriptor.BorderColorA);
  42. float minLod = descriptor.UnpackMinLod();
  43. float maxLod = descriptor.UnpackMaxLod();
  44. float mipLodBias = descriptor.UnpackMipLodBias();
  45. float maxRequestedAnisotropy = descriptor.UnpackMaxAnisotropy();
  46. float maxSupportedAnisotropy = context.Capabilities.MaximumSupportedAnisotropy;
  47. _hostSampler = context.Renderer.CreateSampler(new SamplerCreateInfo(
  48. minFilter,
  49. magFilter,
  50. seamlessCubemap,
  51. addressU,
  52. addressV,
  53. addressP,
  54. compareMode,
  55. compareOp,
  56. color,
  57. minLod,
  58. maxLod,
  59. mipLodBias,
  60. Math.Min(maxRequestedAnisotropy, maxSupportedAnisotropy)));
  61. if (GraphicsConfig.MaxAnisotropy >= 0 && GraphicsConfig.MaxAnisotropy <= 16 && (minFilter == MinFilter.LinearMipmapNearest || minFilter == MinFilter.LinearMipmapLinear))
  62. {
  63. maxRequestedAnisotropy = GraphicsConfig.MaxAnisotropy;
  64. _anisoSampler = context.Renderer.CreateSampler(new SamplerCreateInfo(
  65. minFilter,
  66. magFilter,
  67. seamlessCubemap,
  68. addressU,
  69. addressV,
  70. addressP,
  71. compareMode,
  72. compareOp,
  73. color,
  74. minLod,
  75. maxLod,
  76. mipLodBias,
  77. Math.Min(maxRequestedAnisotropy, maxSupportedAnisotropy)));
  78. }
  79. }
  80. /// <summary>
  81. /// Gets a host sampler for the given texture.
  82. /// </summary>
  83. /// <param name="texture">Texture to be sampled</param>
  84. /// <returns>A host sampler</returns>
  85. public ISampler GetHostSampler(Texture texture)
  86. {
  87. return _anisoSampler != null && texture?.CanForceAnisotropy == true ? _anisoSampler : _hostSampler;
  88. }
  89. /// <summary>
  90. /// Disposes the host sampler object.
  91. /// </summary>
  92. public void Dispose()
  93. {
  94. IsDisposed = true;
  95. _hostSampler.Dispose();
  96. _anisoSampler?.Dispose();
  97. }
  98. }
  99. }