TextureDescriptor.cs 8.4 KB

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