surface.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <climits>
  6. #include <utility>
  7. #include "common/assert.h"
  8. #include "common/common_types.h"
  9. #include "common/logging/log.h"
  10. #include "video_core/gpu.h"
  11. #include "video_core/textures/texture.h"
  12. namespace VideoCore::Surface {
  13. enum class PixelFormat {
  14. ABGR8U = 0,
  15. ABGR8S = 1,
  16. ABGR8UI = 2,
  17. B5G6R5U = 3,
  18. A2B10G10R10U = 4,
  19. A1B5G5R5U = 5,
  20. R8U = 6,
  21. R8UI = 7,
  22. RGBA16F = 8,
  23. RGBA16U = 9,
  24. RGBA16UI = 10,
  25. R11FG11FB10F = 11,
  26. RGBA32UI = 12,
  27. DXT1 = 13,
  28. DXT23 = 14,
  29. DXT45 = 15,
  30. DXN1 = 16, // This is also known as BC4
  31. DXN2UNORM = 17,
  32. DXN2SNORM = 18,
  33. BC7U = 19,
  34. BC6H_UF16 = 20,
  35. BC6H_SF16 = 21,
  36. ASTC_2D_4X4 = 22,
  37. G8R8U = 23,
  38. G8R8S = 24,
  39. BGRA8 = 25,
  40. RGBA32F = 26,
  41. RG32F = 27,
  42. R32F = 28,
  43. R16F = 29,
  44. R16U = 30,
  45. R16S = 31,
  46. R16UI = 32,
  47. R16I = 33,
  48. RG16 = 34,
  49. RG16F = 35,
  50. RG16UI = 36,
  51. RG16I = 37,
  52. RG16S = 38,
  53. RGB32F = 39,
  54. RGBA8_SRGB = 40,
  55. RG8U = 41,
  56. RG8S = 42,
  57. RG32UI = 43,
  58. R32UI = 44,
  59. ASTC_2D_8X8 = 45,
  60. ASTC_2D_8X5 = 46,
  61. ASTC_2D_5X4 = 47,
  62. BGRA8_SRGB = 48,
  63. DXT1_SRGB = 49,
  64. DXT23_SRGB = 50,
  65. DXT45_SRGB = 51,
  66. BC7U_SRGB = 52,
  67. ASTC_2D_4X4_SRGB = 53,
  68. ASTC_2D_8X8_SRGB = 54,
  69. ASTC_2D_8X5_SRGB = 55,
  70. ASTC_2D_5X4_SRGB = 56,
  71. ASTC_2D_5X5 = 57,
  72. ASTC_2D_5X5_SRGB = 58,
  73. ASTC_2D_10X8 = 59,
  74. ASTC_2D_10X8_SRGB = 60,
  75. MaxColorFormat,
  76. // Depth formats
  77. Z32F = 61,
  78. Z16 = 62,
  79. MaxDepthFormat,
  80. // DepthStencil formats
  81. Z24S8 = 63,
  82. S8Z24 = 64,
  83. Z32FS8 = 65,
  84. MaxDepthStencilFormat,
  85. Max = MaxDepthStencilFormat,
  86. Invalid = 255,
  87. };
  88. static constexpr std::size_t MaxPixelFormat = static_cast<std::size_t>(PixelFormat::Max);
  89. enum class ComponentType {
  90. Invalid = 0,
  91. SNorm = 1,
  92. UNorm = 2,
  93. SInt = 3,
  94. UInt = 4,
  95. Float = 5,
  96. };
  97. enum class SurfaceType {
  98. ColorTexture = 0,
  99. Depth = 1,
  100. DepthStencil = 2,
  101. Fill = 3,
  102. Invalid = 4,
  103. };
  104. enum class SurfaceTarget {
  105. Texture1D,
  106. Texture2D,
  107. Texture3D,
  108. Texture1DArray,
  109. Texture2DArray,
  110. TextureCubemap,
  111. TextureCubeArray,
  112. };
  113. constexpr std::array<u32, MaxPixelFormat> compression_factor_table = {{
  114. 1, // ABGR8U
  115. 1, // ABGR8S
  116. 1, // ABGR8UI
  117. 1, // B5G6R5U
  118. 1, // A2B10G10R10U
  119. 1, // A1B5G5R5U
  120. 1, // R8U
  121. 1, // R8UI
  122. 1, // RGBA16F
  123. 1, // RGBA16U
  124. 1, // RGBA16UI
  125. 1, // R11FG11FB10F
  126. 1, // RGBA32UI
  127. 4, // DXT1
  128. 4, // DXT23
  129. 4, // DXT45
  130. 4, // DXN1
  131. 4, // DXN2UNORM
  132. 4, // DXN2SNORM
  133. 4, // BC7U
  134. 4, // BC6H_UF16
  135. 4, // BC6H_SF16
  136. 4, // ASTC_2D_4X4
  137. 1, // G8R8U
  138. 1, // G8R8S
  139. 1, // BGRA8
  140. 1, // RGBA32F
  141. 1, // RG32F
  142. 1, // R32F
  143. 1, // R16F
  144. 1, // R16U
  145. 1, // R16S
  146. 1, // R16UI
  147. 1, // R16I
  148. 1, // RG16
  149. 1, // RG16F
  150. 1, // RG16UI
  151. 1, // RG16I
  152. 1, // RG16S
  153. 1, // RGB32F
  154. 1, // RGBA8_SRGB
  155. 1, // RG8U
  156. 1, // RG8S
  157. 1, // RG32UI
  158. 1, // R32UI
  159. 4, // ASTC_2D_8X8
  160. 4, // ASTC_2D_8X5
  161. 4, // ASTC_2D_5X4
  162. 1, // BGRA8_SRGB
  163. 4, // DXT1_SRGB
  164. 4, // DXT23_SRGB
  165. 4, // DXT45_SRGB
  166. 4, // BC7U_SRGB
  167. 4, // ASTC_2D_4X4_SRGB
  168. 4, // ASTC_2D_8X8_SRGB
  169. 4, // ASTC_2D_8X5_SRGB
  170. 4, // ASTC_2D_5X4_SRGB
  171. 4, // ASTC_2D_5X5
  172. 4, // ASTC_2D_5X5_SRGB
  173. 4, // ASTC_2D_10X8
  174. 4, // ASTC_2D_10X8_SRGB
  175. 1, // Z32F
  176. 1, // Z16
  177. 1, // Z24S8
  178. 1, // S8Z24
  179. 1, // Z32FS8
  180. }};
  181. /**
  182. * Gets the compression factor for the specified PixelFormat. This applies to just the
  183. * "compressed width" and "compressed height", not the overall compression factor of a
  184. * compressed image. This is used for maintaining proper surface sizes for compressed
  185. * texture formats.
  186. */
  187. static constexpr u32 GetCompressionFactor(PixelFormat format) {
  188. if (format == PixelFormat::Invalid)
  189. return 0;
  190. ASSERT(static_cast<std::size_t>(format) < compression_factor_table.size());
  191. return compression_factor_table[static_cast<std::size_t>(format)];
  192. }
  193. constexpr std::array<u32, MaxPixelFormat> block_width_table = {{
  194. 1, // ABGR8U
  195. 1, // ABGR8S
  196. 1, // ABGR8UI
  197. 1, // B5G6R5U
  198. 1, // A2B10G10R10U
  199. 1, // A1B5G5R5U
  200. 1, // R8U
  201. 1, // R8UI
  202. 1, // RGBA16F
  203. 1, // RGBA16U
  204. 1, // RGBA16UI
  205. 1, // R11FG11FB10F
  206. 1, // RGBA32UI
  207. 4, // DXT1
  208. 4, // DXT23
  209. 4, // DXT45
  210. 4, // DXN1
  211. 4, // DXN2UNORM
  212. 4, // DXN2SNORM
  213. 4, // BC7U
  214. 4, // BC6H_UF16
  215. 4, // BC6H_SF16
  216. 4, // ASTC_2D_4X4
  217. 1, // G8R8U
  218. 1, // G8R8S
  219. 1, // BGRA8
  220. 1, // RGBA32F
  221. 1, // RG32F
  222. 1, // R32F
  223. 1, // R16F
  224. 1, // R16U
  225. 1, // R16S
  226. 1, // R16UI
  227. 1, // R16I
  228. 1, // RG16
  229. 1, // RG16F
  230. 1, // RG16UI
  231. 1, // RG16I
  232. 1, // RG16S
  233. 1, // RGB32F
  234. 1, // RGBA8_SRGB
  235. 1, // RG8U
  236. 1, // RG8S
  237. 1, // RG32UI
  238. 1, // R32UI
  239. 8, // ASTC_2D_8X8
  240. 8, // ASTC_2D_8X5
  241. 5, // ASTC_2D_5X4
  242. 1, // BGRA8_SRGB
  243. 4, // DXT1_SRGB
  244. 4, // DXT23_SRGB
  245. 4, // DXT45_SRGB
  246. 4, // BC7U_SRGB
  247. 4, // ASTC_2D_4X4_SRGB
  248. 8, // ASTC_2D_8X8_SRGB
  249. 8, // ASTC_2D_8X5_SRGB
  250. 5, // ASTC_2D_5X4_SRGB
  251. 5, // ASTC_2D_5X5
  252. 5, // ASTC_2D_5X5_SRGB
  253. 10, // ASTC_2D_10X8
  254. 10, // ASTC_2D_10X8_SRGB
  255. 1, // Z32F
  256. 1, // Z16
  257. 1, // Z24S8
  258. 1, // S8Z24
  259. 1, // Z32FS8
  260. }};
  261. static constexpr u32 GetDefaultBlockWidth(PixelFormat format) {
  262. if (format == PixelFormat::Invalid)
  263. return 0;
  264. ASSERT(static_cast<std::size_t>(format) < block_width_table.size());
  265. return block_width_table[static_cast<std::size_t>(format)];
  266. }
  267. constexpr std::array<u32, MaxPixelFormat> block_height_table = {{
  268. 1, // ABGR8U
  269. 1, // ABGR8S
  270. 1, // ABGR8UI
  271. 1, // B5G6R5U
  272. 1, // A2B10G10R10U
  273. 1, // A1B5G5R5U
  274. 1, // R8U
  275. 1, // R8UI
  276. 1, // RGBA16F
  277. 1, // RGBA16U
  278. 1, // RGBA16UI
  279. 1, // R11FG11FB10F
  280. 1, // RGBA32UI
  281. 4, // DXT1
  282. 4, // DXT23
  283. 4, // DXT45
  284. 4, // DXN1
  285. 4, // DXN2UNORM
  286. 4, // DXN2SNORM
  287. 4, // BC7U
  288. 4, // BC6H_UF16
  289. 4, // BC6H_SF16
  290. 4, // ASTC_2D_4X4
  291. 1, // G8R8U
  292. 1, // G8R8S
  293. 1, // BGRA8
  294. 1, // RGBA32F
  295. 1, // RG32F
  296. 1, // R32F
  297. 1, // R16F
  298. 1, // R16U
  299. 1, // R16S
  300. 1, // R16UI
  301. 1, // R16I
  302. 1, // RG16
  303. 1, // RG16F
  304. 1, // RG16UI
  305. 1, // RG16I
  306. 1, // RG16S
  307. 1, // RGB32F
  308. 1, // RGBA8_SRGB
  309. 1, // RG8U
  310. 1, // RG8S
  311. 1, // RG32UI
  312. 1, // R32UI
  313. 8, // ASTC_2D_8X8
  314. 5, // ASTC_2D_8X5
  315. 4, // ASTC_2D_5X4
  316. 1, // BGRA8_SRGB
  317. 4, // DXT1_SRGB
  318. 4, // DXT23_SRGB
  319. 4, // DXT45_SRGB
  320. 4, // BC7U_SRGB
  321. 4, // ASTC_2D_4X4_SRGB
  322. 8, // ASTC_2D_8X8_SRGB
  323. 5, // ASTC_2D_8X5_SRGB
  324. 4, // ASTC_2D_5X4_SRGB
  325. 5, // ASTC_2D_5X5
  326. 5, // ASTC_2D_5X5_SRGB
  327. 8, // ASTC_2D_10X8
  328. 8, // ASTC_2D_10X8_SRGB
  329. 1, // Z32F
  330. 1, // Z16
  331. 1, // Z24S8
  332. 1, // S8Z24
  333. 1, // Z32FS8
  334. }};
  335. static constexpr u32 GetDefaultBlockHeight(PixelFormat format) {
  336. if (format == PixelFormat::Invalid)
  337. return 0;
  338. ASSERT(static_cast<std::size_t>(format) < block_height_table.size());
  339. return block_height_table[static_cast<std::size_t>(format)];
  340. }
  341. constexpr std::array<u32, MaxPixelFormat> bpp_table = {{
  342. 32, // ABGR8U
  343. 32, // ABGR8S
  344. 32, // ABGR8UI
  345. 16, // B5G6R5U
  346. 32, // A2B10G10R10U
  347. 16, // A1B5G5R5U
  348. 8, // R8U
  349. 8, // R8UI
  350. 64, // RGBA16F
  351. 64, // RGBA16U
  352. 64, // RGBA16UI
  353. 32, // R11FG11FB10F
  354. 128, // RGBA32UI
  355. 64, // DXT1
  356. 128, // DXT23
  357. 128, // DXT45
  358. 64, // DXN1
  359. 128, // DXN2UNORM
  360. 128, // DXN2SNORM
  361. 128, // BC7U
  362. 128, // BC6H_UF16
  363. 128, // BC6H_SF16
  364. 128, // ASTC_2D_4X4
  365. 16, // G8R8U
  366. 16, // G8R8S
  367. 32, // BGRA8
  368. 128, // RGBA32F
  369. 64, // RG32F
  370. 32, // R32F
  371. 16, // R16F
  372. 16, // R16U
  373. 16, // R16S
  374. 16, // R16UI
  375. 16, // R16I
  376. 32, // RG16
  377. 32, // RG16F
  378. 32, // RG16UI
  379. 32, // RG16I
  380. 32, // RG16S
  381. 96, // RGB32F
  382. 32, // RGBA8_SRGB
  383. 16, // RG8U
  384. 16, // RG8S
  385. 64, // RG32UI
  386. 32, // R32UI
  387. 128, // ASTC_2D_8X8
  388. 128, // ASTC_2D_8X5
  389. 128, // ASTC_2D_5X4
  390. 32, // BGRA8_SRGB
  391. 64, // DXT1_SRGB
  392. 128, // DXT23_SRGB
  393. 128, // DXT45_SRGB
  394. 128, // BC7U
  395. 128, // ASTC_2D_4X4_SRGB
  396. 128, // ASTC_2D_8X8_SRGB
  397. 128, // ASTC_2D_8X5_SRGB
  398. 128, // ASTC_2D_5X4_SRGB
  399. 128, // ASTC_2D_5X5
  400. 128, // ASTC_2D_5X5_SRGB
  401. 128, // ASTC_2D_10X8
  402. 128, // ASTC_2D_10X8_SRGB
  403. 32, // Z32F
  404. 16, // Z16
  405. 32, // Z24S8
  406. 32, // S8Z24
  407. 64, // Z32FS8
  408. }};
  409. static constexpr u32 GetFormatBpp(PixelFormat format) {
  410. if (format == PixelFormat::Invalid)
  411. return 0;
  412. ASSERT(static_cast<std::size_t>(format) < bpp_table.size());
  413. return bpp_table[static_cast<std::size_t>(format)];
  414. }
  415. /// Returns the sizer in bytes of the specified pixel format
  416. static constexpr u32 GetBytesPerPixel(PixelFormat pixel_format) {
  417. if (pixel_format == PixelFormat::Invalid) {
  418. return 0;
  419. }
  420. return GetFormatBpp(pixel_format) / CHAR_BIT;
  421. }
  422. SurfaceTarget SurfaceTargetFromTextureType(Tegra::Texture::TextureType texture_type);
  423. bool SurfaceTargetIsLayered(SurfaceTarget target);
  424. PixelFormat PixelFormatFromDepthFormat(Tegra::DepthFormat format);
  425. PixelFormat PixelFormatFromRenderTargetFormat(Tegra::RenderTargetFormat format);
  426. PixelFormat PixelFormatFromTextureFormat(Tegra::Texture::TextureFormat format,
  427. Tegra::Texture::ComponentType component_type,
  428. bool is_srgb);
  429. ComponentType ComponentTypeFromTexture(Tegra::Texture::ComponentType type);
  430. ComponentType ComponentTypeFromRenderTarget(Tegra::RenderTargetFormat format);
  431. PixelFormat PixelFormatFromGPUPixelFormat(Tegra::FramebufferConfig::PixelFormat format);
  432. ComponentType ComponentTypeFromDepthFormat(Tegra::DepthFormat format);
  433. SurfaceType GetFormatType(PixelFormat pixel_format);
  434. bool IsPixelFormatASTC(PixelFormat format);
  435. std::pair<u32, u32> GetASTCBlockSize(PixelFormat format);
  436. /// Returns true if the specified PixelFormat is a BCn format, e.g. DXT or DXN
  437. bool IsFormatBCn(PixelFormat format);
  438. } // namespace VideoCore::Surface