texture.h 9.2 KB

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