texture.h 9.8 KB

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