texture.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. // Copyright 2018 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <array>
  6. #include "common/assert.h"
  7. #include "common/bit_field.h"
  8. #include "common/common_funcs.h"
  9. #include "common/common_types.h"
  10. #include "video_core/memory_manager.h"
  11. namespace Tegra::Texture {
  12. enum class TextureFormat : u32 {
  13. R32_G32_B32_A32 = 0x01,
  14. R32_G32_B32 = 0x02,
  15. R16_G16_B16_A16 = 0x03,
  16. R32_G32 = 0x04,
  17. R32_B24G8 = 0x05,
  18. ETC2_RGB = 0x06,
  19. X8B8G8R8 = 0x07,
  20. A8R8G8B8 = 0x08,
  21. A2B10G10R10 = 0x09,
  22. ETC2_RGB_PTA = 0x0a,
  23. ETC2_RGBA = 0x0b,
  24. R16_G16 = 0x0c,
  25. G8R24 = 0x0d,
  26. G24R8 = 0x0e,
  27. R32 = 0x0f,
  28. BC6H_SF16 = 0x10,
  29. BC6H_UF16 = 0x11,
  30. A4B4G4R4 = 0x12,
  31. A5B5G5R1 = 0x13,
  32. A1B5G5R5 = 0x14,
  33. B5G6R5 = 0x15,
  34. B6G5R5 = 0x16,
  35. BC7U = 0x17,
  36. G8R8 = 0x18,
  37. EAC = 0x19,
  38. EACX2 = 0x1a,
  39. R16 = 0x1b,
  40. Y8_VIDEO = 0x1c,
  41. R8 = 0x1d,
  42. G4R4 = 0x1e,
  43. R1 = 0x1f,
  44. E5B9G9R9_SHAREDEXP = 0x20,
  45. BF10GF11RF11 = 0x21,
  46. G8B8G8R8 = 0x22,
  47. B8G8R8G8 = 0x23,
  48. DXT1 = 0x24,
  49. DXT23 = 0x25,
  50. DXT45 = 0x26,
  51. DXN1 = 0x27,
  52. DXN2 = 0x28,
  53. Z24S8 = 0x29,
  54. X8Z24 = 0x2a,
  55. S8Z24 = 0x2b,
  56. X4V4Z24__COV4R4V = 0x2c,
  57. X4V4Z24__COV8R8V = 0x2d,
  58. V8Z24__COV4R12V = 0x2e,
  59. ZF32 = 0x2f,
  60. ZF32_X24S8 = 0x30,
  61. X8Z24_X20V4S8__COV4R4V = 0x31,
  62. X8Z24_X20V4S8__COV8R8V = 0x32,
  63. ZF32_X20V4X8__COV4R4V = 0x33,
  64. ZF32_X20V4X8__COV8R8V = 0x34,
  65. ZF32_X20V4S8__COV4R4V = 0x35,
  66. ZF32_X20V4S8__COV8R8V = 0x36,
  67. X8Z24_X16V8S8__COV4R12V = 0x37,
  68. ZF32_X16V8X8__COV4R12V = 0x38,
  69. ZF32_X16V8S8__COV4R12V = 0x39,
  70. Z16 = 0x3a,
  71. V8Z24__COV8R24V = 0x3b,
  72. X8Z24_X16V8S8__COV8R24V = 0x3c,
  73. ZF32_X16V8X8__COV8R24V = 0x3d,
  74. ZF32_X16V8S8__COV8R24V = 0x3e,
  75. ASTC_2D_4X4 = 0x40,
  76. ASTC_2D_5X5 = 0x41,
  77. ASTC_2D_6X6 = 0x42,
  78. ASTC_2D_8X8 = 0x44,
  79. ASTC_2D_10X10 = 0x45,
  80. ASTC_2D_12X12 = 0x46,
  81. ASTC_2D_5X4 = 0x50,
  82. ASTC_2D_6X5 = 0x51,
  83. ASTC_2D_8X6 = 0x52,
  84. ASTC_2D_10X8 = 0x53,
  85. ASTC_2D_12X10 = 0x54,
  86. ASTC_2D_8X5 = 0x55,
  87. ASTC_2D_10X5 = 0x56,
  88. ASTC_2D_10X6 = 0x57,
  89. };
  90. enum class TextureType : u32 {
  91. Texture1D = 0,
  92. Texture2D = 1,
  93. Texture3D = 2,
  94. TextureCubemap = 3,
  95. Texture1DArray = 4,
  96. Texture2DArray = 5,
  97. Texture1DBuffer = 6,
  98. Texture2DNoMipmap = 7,
  99. TextureCubeArray = 8,
  100. };
  101. enum class TICHeaderVersion : u32 {
  102. OneDBuffer = 0,
  103. PitchColorKey = 1,
  104. Pitch = 2,
  105. BlockLinear = 3,
  106. BlockLinearColorKey = 4,
  107. };
  108. enum class ComponentType : u32 {
  109. SNORM = 1,
  110. UNORM = 2,
  111. SINT = 3,
  112. UINT = 4,
  113. SNORM_FORCE_FP16 = 5,
  114. UNORM_FORCE_FP16 = 6,
  115. FLOAT = 7
  116. };
  117. enum class SwizzleSource : u32 {
  118. Zero = 0,
  119. R = 2,
  120. G = 3,
  121. B = 4,
  122. A = 5,
  123. OneInt = 6,
  124. OneFloat = 7,
  125. };
  126. union TextureHandle {
  127. u32 raw;
  128. BitField<0, 20, u32> tic_id;
  129. BitField<20, 12, u32> tsc_id;
  130. };
  131. static_assert(sizeof(TextureHandle) == 4, "TextureHandle has wrong size");
  132. struct TICEntry {
  133. static constexpr u32 DefaultBlockHeight = 16;
  134. static constexpr u32 DefaultBlockDepth = 1;
  135. union {
  136. u32 raw;
  137. BitField<0, 7, TextureFormat> format;
  138. BitField<7, 3, ComponentType> r_type;
  139. BitField<10, 3, ComponentType> g_type;
  140. BitField<13, 3, ComponentType> b_type;
  141. BitField<16, 3, ComponentType> a_type;
  142. BitField<19, 3, SwizzleSource> x_source;
  143. BitField<22, 3, SwizzleSource> y_source;
  144. BitField<25, 3, SwizzleSource> z_source;
  145. BitField<28, 3, SwizzleSource> w_source;
  146. };
  147. u32 address_low;
  148. union {
  149. BitField<0, 16, u32> address_high;
  150. BitField<21, 3, TICHeaderVersion> header_version;
  151. };
  152. union {
  153. BitField<0, 3, u32> block_width;
  154. BitField<3, 3, u32> block_height;
  155. BitField<6, 3, u32> block_depth;
  156. BitField<10, 3, u32> tile_width_spacing;
  157. // High 16 bits of the pitch value
  158. BitField<0, 16, u32> pitch_high;
  159. BitField<26, 1, u32> use_header_opt_control;
  160. BitField<27, 1, u32> depth_texture;
  161. BitField<28, 4, u32> max_mip_level;
  162. };
  163. union {
  164. BitField<0, 16, u32> width_minus_1;
  165. BitField<22, 1, u32> srgb_conversion;
  166. BitField<23, 4, TextureType> texture_type;
  167. BitField<29, 3, u32> border_size;
  168. };
  169. union {
  170. BitField<0, 16, u32> height_minus_1;
  171. BitField<16, 14, u32> depth_minus_1;
  172. };
  173. union {
  174. BitField<6, 13, u32> mip_lod_bias;
  175. BitField<27, 3, u32> max_anisotropy;
  176. };
  177. union {
  178. BitField<0, 4, u32> res_min_mip_level;
  179. BitField<4, 4, u32> res_max_mip_level;
  180. BitField<12, 12, u32> min_lod_clamp;
  181. };
  182. GPUVAddr Address() const {
  183. return static_cast<GPUVAddr>((static_cast<GPUVAddr>(address_high) << 32) | address_low);
  184. }
  185. u32 Pitch() const {
  186. ASSERT(header_version == TICHeaderVersion::Pitch ||
  187. header_version == TICHeaderVersion::PitchColorKey);
  188. // The pitch value is 21 bits, and is 32B aligned.
  189. return pitch_high << 5;
  190. }
  191. u32 Width() const {
  192. return width_minus_1 + 1;
  193. }
  194. u32 Height() const {
  195. return height_minus_1 + 1;
  196. }
  197. u32 Depth() const {
  198. return depth_minus_1 + 1;
  199. }
  200. u32 BlockWidth() const {
  201. ASSERT(IsTiled());
  202. // The block height is stored in log2 format.
  203. return 1 << block_width;
  204. }
  205. u32 BlockHeight() const {
  206. ASSERT(IsTiled());
  207. // The block height is stored in log2 format.
  208. return 1 << block_height;
  209. }
  210. u32 BlockDepth() const {
  211. ASSERT(IsTiled());
  212. // The block height is stored in log2 format.
  213. return 1 << block_depth;
  214. }
  215. bool IsTiled() const {
  216. return header_version == TICHeaderVersion::BlockLinear ||
  217. header_version == TICHeaderVersion::BlockLinearColorKey;
  218. }
  219. bool IsSrgbConversionEnabled() const {
  220. return srgb_conversion != 0;
  221. }
  222. };
  223. static_assert(sizeof(TICEntry) == 0x20, "TICEntry has wrong size");
  224. enum class WrapMode : u32 {
  225. Wrap = 0,
  226. Mirror = 1,
  227. ClampToEdge = 2,
  228. Border = 3,
  229. ClampOGL = 4,
  230. MirrorOnceClampToEdge = 5,
  231. MirrorOnceBorder = 6,
  232. MirrorOnceClampOGL = 7,
  233. };
  234. enum class DepthCompareFunc : u32 {
  235. Never = 0,
  236. Less = 1,
  237. Equal = 2,
  238. LessEqual = 3,
  239. Greater = 4,
  240. NotEqual = 5,
  241. GreaterEqual = 6,
  242. Always = 7,
  243. };
  244. enum class TextureFilter : u32 {
  245. Nearest = 1,
  246. Linear = 2,
  247. };
  248. enum class TextureMipmapFilter : u32 {
  249. None = 1,
  250. Nearest = 2,
  251. Linear = 3,
  252. };
  253. struct TSCEntry {
  254. union {
  255. struct {
  256. union {
  257. BitField<0, 3, WrapMode> wrap_u;
  258. BitField<3, 3, WrapMode> wrap_v;
  259. BitField<6, 3, WrapMode> wrap_p;
  260. BitField<9, 1, u32> depth_compare_enabled;
  261. BitField<10, 3, DepthCompareFunc> depth_compare_func;
  262. BitField<13, 1, u32> srgb_conversion;
  263. BitField<20, 3, u32> max_anisotropy;
  264. };
  265. union {
  266. BitField<0, 2, TextureFilter> mag_filter;
  267. BitField<4, 2, TextureFilter> min_filter;
  268. BitField<6, 2, TextureMipmapFilter> mipmap_filter;
  269. BitField<9, 1, u32> cubemap_interface_filtering;
  270. BitField<12, 13, u32> mip_lod_bias;
  271. };
  272. union {
  273. BitField<0, 12, u32> min_lod_clamp;
  274. BitField<12, 12, u32> max_lod_clamp;
  275. BitField<24, 8, u32> srgb_border_color_r;
  276. };
  277. union {
  278. BitField<12, 8, u32> srgb_border_color_g;
  279. BitField<20, 8, u32> srgb_border_color_b;
  280. };
  281. std::array<f32, 4> border_color;
  282. };
  283. std::array<u8, 0x20> raw;
  284. };
  285. float GetMaxAnisotropy() const {
  286. return static_cast<float>(1U << max_anisotropy);
  287. }
  288. float GetMinLod() const {
  289. return static_cast<float>(min_lod_clamp) / 256.0f;
  290. }
  291. float GetMaxLod() const {
  292. return static_cast<float>(max_lod_clamp) / 256.0f;
  293. }
  294. float GetLodBias() const {
  295. // Sign extend the 13-bit value.
  296. constexpr u32 mask = 1U << (13 - 1);
  297. return static_cast<float>((mip_lod_bias ^ mask) - mask) / 256.0f;
  298. }
  299. std::array<float, 4> GetBorderColor() const {
  300. if (srgb_conversion) {
  301. return {srgb_border_color_r / 255.0f, srgb_border_color_g / 255.0f,
  302. srgb_border_color_b / 255.0f, border_color[3]};
  303. }
  304. return border_color;
  305. }
  306. };
  307. static_assert(sizeof(TSCEntry) == 0x20, "TSCEntry has wrong size");
  308. struct FullTextureInfo {
  309. u32 index;
  310. TICEntry tic;
  311. TSCEntry tsc;
  312. };
  313. /// Returns the number of bytes per pixel of the input texture format.
  314. u32 BytesPerPixel(TextureFormat format);
  315. } // namespace Tegra::Texture