Sampler.cs 3.8 KB

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