SamplerDescriptor.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. using Ryujinx.Graphics.GAL;
  2. using System.Runtime.CompilerServices;
  3. using System.Runtime.Intrinsics;
  4. namespace Ryujinx.Graphics.Gpu.Image
  5. {
  6. /// <summary>
  7. /// Maxwell sampler descriptor structure.
  8. /// This structure defines the sampler descriptor as it is packed on the GPU sampler pool region.
  9. /// </summary>
  10. struct SamplerDescriptor
  11. {
  12. private static readonly float[] _f5ToF32ConversionLut = new float[]
  13. {
  14. 0.0f,
  15. 0.055555556f,
  16. 0.1f,
  17. 0.13636364f,
  18. 0.16666667f,
  19. 0.1923077f,
  20. 0.21428572f,
  21. 0.23333333f,
  22. 0.25f,
  23. 0.2777778f,
  24. 0.3f,
  25. 0.3181818f,
  26. 0.33333334f,
  27. 0.34615386f,
  28. 0.35714287f,
  29. 0.36666667f,
  30. 0.375f,
  31. 0.3888889f,
  32. 0.4f,
  33. 0.4090909f,
  34. 0.41666666f,
  35. 0.42307693f,
  36. 0.42857143f,
  37. 0.43333334f,
  38. 0.4375f,
  39. 0.44444445f,
  40. 0.45f,
  41. 0.45454547f,
  42. 0.45833334f,
  43. 0.46153846f,
  44. 0.4642857f,
  45. 0.46666667f
  46. };
  47. private static readonly float[] _maxAnisotropyLut = new float[]
  48. {
  49. 1, 2, 4, 6, 8, 10, 12, 16
  50. };
  51. private const float Frac8ToF32 = 1.0f / 256.0f;
  52. #pragma warning disable CS0649
  53. public uint Word0;
  54. public uint Word1;
  55. public uint Word2;
  56. public uint Word3;
  57. public float BorderColorR;
  58. public float BorderColorG;
  59. public float BorderColorB;
  60. public float BorderColorA;
  61. #pragma warning restore CS0649
  62. /// <summary>
  63. /// Unpacks the texture wrap mode along the X axis.
  64. /// </summary>
  65. /// <returns>The texture wrap mode enum</returns>
  66. public AddressMode UnpackAddressU()
  67. {
  68. return (AddressMode)(Word0 & 7);
  69. }
  70. // <summary>
  71. /// Unpacks the texture wrap mode along the Y axis.
  72. /// </summary>
  73. /// <returns>The texture wrap mode enum</returns>
  74. public AddressMode UnpackAddressV()
  75. {
  76. return (AddressMode)((Word0 >> 3) & 7);
  77. }
  78. // <summary>
  79. /// Unpacks the texture wrap mode along the Z axis.
  80. /// </summary>
  81. /// <returns>The texture wrap mode enum</returns>
  82. public AddressMode UnpackAddressP()
  83. {
  84. return (AddressMode)((Word0 >> 6) & 7);
  85. }
  86. /// <summary>
  87. /// Unpacks the compare mode used for depth comparison on the shader, for
  88. /// depth buffer texture.
  89. /// This is only relevant for shaders with shadow samplers.
  90. /// </summary>
  91. /// <returns>The depth comparison mode enum</returns>
  92. public CompareMode UnpackCompareMode()
  93. {
  94. return (CompareMode)((Word0 >> 9) & 1);
  95. }
  96. /// <summary>
  97. /// Unpacks the compare operation used for depth comparison on the shader, for
  98. /// depth buffer texture.
  99. /// This is only relevant for shaders with shadow samplers.
  100. /// </summary>
  101. /// <returns>The depth comparison operation enum</returns>
  102. public CompareOp UnpackCompareOp()
  103. {
  104. return (CompareOp)(((Word0 >> 10) & 7) + 1);
  105. }
  106. /// <summary>
  107. /// Unpacks and converts the maximum anisotropy value used for texture anisotropic filtering.
  108. /// </summary>
  109. /// <returns>The maximum anisotropy</returns>
  110. public float UnpackMaxAnisotropy()
  111. {
  112. return _maxAnisotropyLut[(Word0 >> 20) & 7];
  113. }
  114. /// <summary>
  115. /// Unpacks the texture magnification filter.
  116. /// This defines the filtering used when the texture covers an area on the screen
  117. /// that is larger than the texture size.
  118. /// </summary>
  119. /// <returns>The magnification filter</returns>
  120. public MagFilter UnpackMagFilter()
  121. {
  122. return (MagFilter)(Word1 & 3);
  123. }
  124. /// <summary>
  125. /// Unpacks the texture minification filter.
  126. /// This defines the filtering used when the texture covers an area on the screen
  127. /// that is smaller than the texture size.
  128. /// </summary>
  129. /// <returns>The minification filter</returns>
  130. public MinFilter UnpackMinFilter()
  131. {
  132. SamplerMinFilter minFilter = (SamplerMinFilter)((Word1 >> 4) & 3);
  133. SamplerMipFilter mipFilter = (SamplerMipFilter)((Word1 >> 6) & 3);
  134. return ConvertFilter(minFilter, mipFilter);
  135. }
  136. /// <summary>
  137. /// Converts two minification and filter enum, to a single minification enum,
  138. /// including mipmap filtering information, as expected from the host API.
  139. /// </summary>
  140. /// <param name="minFilter">The minification filter</param>
  141. /// <param name="mipFilter">The mipmap level filter</param>
  142. /// <returns>The combined, host API compatible filter enum</returns>
  143. private static MinFilter ConvertFilter(SamplerMinFilter minFilter, SamplerMipFilter mipFilter)
  144. {
  145. switch (mipFilter)
  146. {
  147. case SamplerMipFilter.None:
  148. switch (minFilter)
  149. {
  150. case SamplerMinFilter.Nearest: return MinFilter.Nearest;
  151. case SamplerMinFilter.Linear: return MinFilter.Linear;
  152. }
  153. break;
  154. case SamplerMipFilter.Nearest:
  155. switch (minFilter)
  156. {
  157. case SamplerMinFilter.Nearest: return MinFilter.NearestMipmapNearest;
  158. case SamplerMinFilter.Linear: return MinFilter.LinearMipmapNearest;
  159. }
  160. break;
  161. case SamplerMipFilter.Linear:
  162. switch (minFilter)
  163. {
  164. case SamplerMinFilter.Nearest: return MinFilter.NearestMipmapLinear;
  165. case SamplerMinFilter.Linear: return MinFilter.LinearMipmapLinear;
  166. }
  167. break;
  168. }
  169. return MinFilter.Nearest;
  170. }
  171. /// <summary>
  172. /// Unpacks the seamless cubemap flag.
  173. /// </summary>
  174. /// <returns>The seamless cubemap flag</returns>
  175. public bool UnpackSeamlessCubemap()
  176. {
  177. return (Word1 & (1 << 9)) != 0;
  178. }
  179. /// <summary>
  180. /// Unpacks the reduction filter, used with texture minification linear filtering.
  181. /// This describes how the final value will be computed from neighbouring pixels.
  182. /// </summary>
  183. /// <returns>The reduction filter</returns>
  184. public ReductionFilter UnpackReductionFilter()
  185. {
  186. return (ReductionFilter)((Word1 >> 10) & 3);
  187. }
  188. /// <summary>
  189. /// Unpacks the level-of-detail bias value.
  190. /// This is a bias added to the level-of-detail value as computed by the GPU, used to select
  191. /// which mipmap level to use from a given texture.
  192. /// </summary>
  193. /// <returns>The level-of-detail bias value</returns>
  194. public float UnpackMipLodBias()
  195. {
  196. int fixedValue = (int)(Word1 >> 12) & 0x1fff;
  197. fixedValue = (fixedValue << 19) >> 19;
  198. return fixedValue * Frac8ToF32;
  199. }
  200. /// <summary>
  201. /// Unpacks the level-of-detail snap value.
  202. /// </summary>
  203. /// <returns>The level-of-detail snap value</returns>
  204. public float UnpackLodSnap()
  205. {
  206. return _f5ToF32ConversionLut[(Word1 >> 26) & 0x1f];
  207. }
  208. /// <summary>
  209. /// Unpacks the minimum level-of-detail value.
  210. /// </summary>
  211. /// <returns>The minimum level-of-detail value</returns>
  212. public float UnpackMinLod()
  213. {
  214. return (Word2 & 0xfff) * Frac8ToF32;
  215. }
  216. /// <summary>
  217. /// Unpacks the maximum level-of-detail value.
  218. /// </summary>
  219. /// <returns>The maximum level-of-detail value</returns>
  220. public float UnpackMaxLod()
  221. {
  222. return ((Word2 >> 12) & 0xfff) * Frac8ToF32;
  223. }
  224. /// <summary>
  225. /// Check if two descriptors are equal.
  226. /// </summary>
  227. /// <param name="other">The descriptor to compare against</param>
  228. /// <returns>True if they are equal, false otherwise</returns>
  229. public bool Equals(ref SamplerDescriptor other)
  230. {
  231. return Unsafe.As<SamplerDescriptor, Vector256<byte>>(ref this).Equals(Unsafe.As<SamplerDescriptor, Vector256<byte>>(ref other));
  232. }
  233. }
  234. }