TextureDescriptor.cs 8.3 KB

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