TextureDescriptor.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. /// Unpacks the texture sRGB format flag.
  142. /// </summary>
  143. /// <returns>True if the texture is sRGB, false otherwise</returns>
  144. public bool UnpackSrgb()
  145. {
  146. return (Word4 & (1 << 22)) != 0;
  147. }
  148. /// <summary>
  149. /// Unpacks the texture target.
  150. /// </summary>
  151. /// <returns>The texture target</returns>
  152. public TextureTarget UnpackTextureTarget()
  153. {
  154. return (TextureTarget)((Word4 >> 23) & 0xf);
  155. }
  156. /// <summary>
  157. /// Unpack the base level texture height size, or array layers for 1D array textures.
  158. /// Should be ignored for 1D or buffer textures.
  159. /// </summary>
  160. /// <returns>The texture height or layers count</returns>
  161. public int UnpackHeight()
  162. {
  163. return (int)(Word5 & 0xffff) + 1;
  164. }
  165. /// <summary>
  166. /// Unpack the base level texture depth size, number of array layers or cubemap faces.
  167. /// The meaning of this value depends on the texture target.
  168. /// </summary>
  169. /// <returns>The texture depth, layer or faces count</returns>
  170. public int UnpackDepth()
  171. {
  172. return (int)((Word5 >> 16) & 0x3fff) + 1;
  173. }
  174. /// <summary>
  175. /// Unpacks the texture coordinates normalized flag.
  176. /// When this is true, texture coordinates are expected to be in the [0, 1] range on the shader.
  177. /// When this is false, texture coordinates are expected to be in the [0, W], [0, H] and [0, D] range.
  178. /// It must be set to false (by the guest driver) for rectangle textures.
  179. /// </summary>
  180. /// <returns>The texture coordinates normalized flag</returns>
  181. public bool UnpackTextureCoordNormalized()
  182. {
  183. return (Word5 & (1 << 31)) != 0;
  184. }
  185. /// <summary>
  186. /// Unpacks the base mipmap level of the texture.
  187. /// </summary>
  188. /// <returns>The base mipmap level of the texture</returns>
  189. public int UnpackBaseLevel()
  190. {
  191. return (int)(Word7 & 0xf);
  192. }
  193. /// <summary>
  194. /// Unpacks the maximum mipmap level (inclusive) of the texture.
  195. /// Usually equal to Levels minus 1.
  196. /// </summary>
  197. /// <returns>The maximum mipmap level (inclusive) of the texture</returns>
  198. public int UnpackMaxLevelInclusive()
  199. {
  200. return (int)((Word7 >> 4) & 0xf);
  201. }
  202. /// <summary>
  203. /// Unpacks the multisampled texture samples count in each direction.
  204. /// Must be ignored for non-multisample textures.
  205. /// </summary>
  206. /// <returns>The multisample counts enum</returns>
  207. public TextureMsaaMode UnpackTextureMsaaMode()
  208. {
  209. return (TextureMsaaMode)((Word7 >> 8) & 0xf);
  210. }
  211. /// <summary>
  212. /// Create the equivalent of this TextureDescriptor for the shader cache.
  213. /// </summary>
  214. /// <returns>The equivalent of this TextureDescriptor for the shader cache.</returns>
  215. public GuestTextureDescriptor ToCache()
  216. {
  217. GuestTextureDescriptor result = new GuestTextureDescriptor
  218. {
  219. Handle = uint.MaxValue,
  220. Format = UnpackFormat(),
  221. Target = UnpackTextureTarget(),
  222. IsSrgb = UnpackSrgb(),
  223. IsTextureCoordNormalized = UnpackTextureCoordNormalized(),
  224. };
  225. return result;
  226. }
  227. /// <summary>
  228. /// Check if two descriptors are equal.
  229. /// </summary>
  230. /// <param name="other">The descriptor to compare against</param>
  231. /// <returns>True if they are equal, false otherwise</returns>
  232. public bool Equals(ref TextureDescriptor other)
  233. {
  234. return Unsafe.As<TextureDescriptor, Vector256<byte>>(ref this).Equals(Unsafe.As<TextureDescriptor, Vector256<byte>>(ref other));
  235. }
  236. }
  237. }