TextureDescriptor.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. using Ryujinx.Graphics.Gpu.Shader.Cache.Definition;
  2. using System.Runtime.CompilerServices;
  3. using System.Runtime.Intrinsics;
  4. namespace Ryujinx.Graphics.Gpu.Image
  5. {
  6. /// <summary>
  7. /// Maxwell texture descriptor, as stored on the GPU texture pool memory region.
  8. /// </summary>
  9. struct TextureDescriptor : ITextureDescriptor
  10. {
  11. #pragma warning disable CS0649
  12. public uint Word0;
  13. public uint Word1;
  14. public uint Word2;
  15. public uint Word3;
  16. public uint Word4;
  17. public uint Word5;
  18. public uint Word6;
  19. public uint Word7;
  20. #pragma warning restore CS0649
  21. /// <summary>
  22. /// Unpacks Maxwell texture format integer.
  23. /// </summary>
  24. /// <returns>The texture format integer</returns>
  25. public uint UnpackFormat()
  26. {
  27. return Word0 & 0x8007ffff;
  28. }
  29. /// <summary>
  30. /// Unpacks the swizzle component for the texture red color channel.
  31. /// </summary>
  32. /// <returns>The swizzle component</returns>
  33. public TextureComponent UnpackSwizzleR()
  34. {
  35. return(TextureComponent)((Word0 >> 19) & 7);
  36. }
  37. /// <summary>
  38. /// Unpacks the swizzle component for the texture green color channel.
  39. /// </summary>
  40. /// <returns>The swizzle component</returns>
  41. public TextureComponent UnpackSwizzleG()
  42. {
  43. return(TextureComponent)((Word0 >> 22) & 7);
  44. }
  45. /// <summary>
  46. /// Unpacks the swizzle component for the texture blue color channel.
  47. /// </summary>
  48. /// <returns>The swizzle component</returns>
  49. public TextureComponent UnpackSwizzleB()
  50. {
  51. return(TextureComponent)((Word0 >> 25) & 7);
  52. }
  53. /// <summary>
  54. /// Unpacks the swizzle component for the texture alpha color channel.
  55. /// </summary>
  56. /// <returns>The swizzle component</returns>
  57. public TextureComponent UnpackSwizzleA()
  58. {
  59. return(TextureComponent)((Word0 >> 28) & 7);
  60. }
  61. /// <summary>
  62. /// Unpacks the 40-bits texture GPU virtual address.
  63. /// </summary>
  64. /// <returns>The GPU virtual address</returns>
  65. public ulong UnpackAddress()
  66. {
  67. return Word1 | ((ulong)(Word2 & 0xffff) << 32);
  68. }
  69. /// <summary>
  70. /// Unpacks texture descriptor type for this texture descriptor.
  71. /// This defines the texture layout, among other things.
  72. /// </summary>
  73. /// <returns>The texture descriptor type</returns>
  74. public TextureDescriptorType UnpackTextureDescriptorType()
  75. {
  76. return (TextureDescriptorType)((Word2 >> 21) & 7);
  77. }
  78. /// <summary>
  79. /// Unpacks the texture stride (bytes per line) for linear textures only.
  80. /// Always 32-bytes aligned.
  81. /// </summary>
  82. /// <returns>The linear texture stride</returns>
  83. public int UnpackStride()
  84. {
  85. return (int)(Word3 & 0xffff) << 5;
  86. }
  87. /// <summary>
  88. /// Unpacks the GOB block size in X (width) for block linear textures.
  89. /// Must be always 1, ignored by the GPU.
  90. /// </summary>
  91. /// <returns>THe GOB block X size</returns>
  92. public int UnpackGobBlocksInX()
  93. {
  94. return 1 << (int)(Word3 & 7);
  95. }
  96. /// <summary>
  97. /// Unpacks the GOB block size in Y (height) for block linear textures.
  98. /// Must be always a power of 2, with a maximum value of 32.
  99. /// </summary>
  100. /// <returns>THe GOB block Y size</returns>
  101. public int UnpackGobBlocksInY()
  102. {
  103. return 1 << (int)((Word3 >> 3) & 7);
  104. }
  105. /// <summary>
  106. /// Unpacks the GOB block size in Z (depth) for block linear textures.
  107. /// Must be always a power of 2, with a maximum value of 32.
  108. /// Must be 1 for any texture target other than 3D textures.
  109. /// </summary>
  110. /// <returns>The GOB block Z size</returns>
  111. public int UnpackGobBlocksInZ()
  112. {
  113. return 1 << (int)((Word3 >> 6) & 7);
  114. }
  115. /// <summary>
  116. /// Number of GOB blocks per tile in the X direction.
  117. /// This is only used for sparse textures, should be 1 otherwise.
  118. /// </summary>
  119. /// <returns>The number of GOB blocks per tile</returns>
  120. public int UnpackGobBlocksInTileX()
  121. {
  122. return 1 << (int)((Word3 >> 10) & 7);
  123. }
  124. /// <summary>
  125. /// Unpacks the number of mipmap levels of the texture.
  126. /// </summary>
  127. /// <returns>The number of mipmap levels</returns>
  128. public int UnpackLevels()
  129. {
  130. return (int)(Word3 >> 28) + 1;
  131. }
  132. /// <summary>
  133. /// Unpack the base level texture width size.
  134. /// </summary>
  135. /// <returns>The texture width</returns>
  136. public int UnpackWidth()
  137. {
  138. return (int)(Word4 & 0xffff) + 1;
  139. }
  140. /// <summary>
  141. /// Unpack the width of a buffer texture.
  142. /// </summary>
  143. /// <returns>The texture width</returns>
  144. public int UnpackBufferTextureWidth()
  145. {
  146. return (int)((Word4 & 0xffff) | (Word3 << 16)) + 1;
  147. }
  148. /// <summary>
  149. /// Unpacks the texture sRGB format flag.
  150. /// </summary>
  151. /// <returns>True if the texture is sRGB, false otherwise</returns>
  152. public bool UnpackSrgb()
  153. {
  154. return (Word4 & (1 << 22)) != 0;
  155. }
  156. /// <summary>
  157. /// Unpacks the texture target.
  158. /// </summary>
  159. /// <returns>The texture target</returns>
  160. public TextureTarget UnpackTextureTarget()
  161. {
  162. return (TextureTarget)((Word4 >> 23) & 0xf);
  163. }
  164. /// <summary>
  165. /// Unpack the base level texture height size, or array layers for 1D array textures.
  166. /// Should be ignored for 1D or buffer textures.
  167. /// </summary>
  168. /// <returns>The texture height or layers count</returns>
  169. public int UnpackHeight()
  170. {
  171. return (int)(Word5 & 0xffff) + 1;
  172. }
  173. /// <summary>
  174. /// Unpack the base level texture depth size, number of array layers or cubemap faces.
  175. /// The meaning of this value depends on the texture target.
  176. /// </summary>
  177. /// <returns>The texture depth, layer or faces count</returns>
  178. public int UnpackDepth()
  179. {
  180. return (int)((Word5 >> 16) & 0x3fff) + 1;
  181. }
  182. /// <summary>
  183. /// Unpacks the texture coordinates normalized flag.
  184. /// When this is true, texture coordinates are expected to be in the [0, 1] range on the shader.
  185. /// When this is false, texture coordinates are expected to be in the [0, W], [0, H] and [0, D] range.
  186. /// It must be set to false (by the guest driver) for rectangle textures.
  187. /// </summary>
  188. /// <returns>The texture coordinates normalized flag</returns>
  189. public bool UnpackTextureCoordNormalized()
  190. {
  191. return (Word5 & (1 << 31)) != 0;
  192. }
  193. /// <summary>
  194. /// Unpacks the base mipmap level of the texture.
  195. /// </summary>
  196. /// <returns>The base mipmap level of the texture</returns>
  197. public int UnpackBaseLevel()
  198. {
  199. return (int)(Word7 & 0xf);
  200. }
  201. /// <summary>
  202. /// Unpacks the maximum mipmap level (inclusive) of the texture.
  203. /// Usually equal to Levels minus 1.
  204. /// </summary>
  205. /// <returns>The maximum mipmap level (inclusive) of the texture</returns>
  206. public int UnpackMaxLevelInclusive()
  207. {
  208. return (int)((Word7 >> 4) & 0xf);
  209. }
  210. /// <summary>
  211. /// Unpacks the multisampled texture samples count in each direction.
  212. /// Must be ignored for non-multisample textures.
  213. /// </summary>
  214. /// <returns>The multisample counts enum</returns>
  215. public TextureMsaaMode UnpackTextureMsaaMode()
  216. {
  217. return (TextureMsaaMode)((Word7 >> 8) & 0xf);
  218. }
  219. /// <summary>
  220. /// Create the equivalent of this TextureDescriptor for the shader cache.
  221. /// </summary>
  222. /// <returns>The equivalent of this TextureDescriptor for the shader cache.</returns>
  223. public GuestTextureDescriptor ToCache()
  224. {
  225. GuestTextureDescriptor result = new GuestTextureDescriptor
  226. {
  227. Handle = uint.MaxValue,
  228. Format = UnpackFormat(),
  229. Target = UnpackTextureTarget(),
  230. IsSrgb = UnpackSrgb(),
  231. IsTextureCoordNormalized = UnpackTextureCoordNormalized(),
  232. };
  233. return result;
  234. }
  235. /// <summary>
  236. /// Check if two descriptors are equal.
  237. /// </summary>
  238. /// <param name="other">The descriptor to compare against</param>
  239. /// <returns>True if they are equal, false otherwise</returns>
  240. public bool Equals(ref TextureDescriptor other)
  241. {
  242. return Unsafe.As<TextureDescriptor, Vector256<byte>>(ref this).Equals(Unsafe.As<TextureDescriptor, Vector256<byte>>(ref other));
  243. }
  244. }
  245. }