surface.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. // SPDX-FileCopyrightText: 2014 Citra Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "common/common_types.h"
  4. #include "common/math_util.h"
  5. #include "video_core/surface.h"
  6. namespace VideoCore::Surface {
  7. SurfaceTarget SurfaceTargetFromTextureType(Tegra::Texture::TextureType texture_type) {
  8. switch (texture_type) {
  9. case Tegra::Texture::TextureType::Texture1D:
  10. return SurfaceTarget::Texture1D;
  11. case Tegra::Texture::TextureType::Texture1DBuffer:
  12. return SurfaceTarget::TextureBuffer;
  13. case Tegra::Texture::TextureType::Texture2D:
  14. case Tegra::Texture::TextureType::Texture2DNoMipmap:
  15. return SurfaceTarget::Texture2D;
  16. case Tegra::Texture::TextureType::Texture3D:
  17. return SurfaceTarget::Texture3D;
  18. case Tegra::Texture::TextureType::TextureCubemap:
  19. return SurfaceTarget::TextureCubemap;
  20. case Tegra::Texture::TextureType::TextureCubeArray:
  21. return SurfaceTarget::TextureCubeArray;
  22. case Tegra::Texture::TextureType::Texture1DArray:
  23. return SurfaceTarget::Texture1DArray;
  24. case Tegra::Texture::TextureType::Texture2DArray:
  25. return SurfaceTarget::Texture2DArray;
  26. default:
  27. LOG_CRITICAL(HW_GPU, "Unimplemented texture_type={}", texture_type);
  28. ASSERT(false);
  29. return SurfaceTarget::Texture2D;
  30. }
  31. }
  32. bool SurfaceTargetIsLayered(SurfaceTarget target) {
  33. switch (target) {
  34. case SurfaceTarget::Texture1D:
  35. case SurfaceTarget::TextureBuffer:
  36. case SurfaceTarget::Texture2D:
  37. case SurfaceTarget::Texture3D:
  38. return false;
  39. case SurfaceTarget::Texture1DArray:
  40. case SurfaceTarget::Texture2DArray:
  41. case SurfaceTarget::TextureCubemap:
  42. case SurfaceTarget::TextureCubeArray:
  43. return true;
  44. default:
  45. LOG_CRITICAL(HW_GPU, "Unimplemented surface_target={}", target);
  46. ASSERT(false);
  47. return false;
  48. }
  49. }
  50. bool SurfaceTargetIsArray(SurfaceTarget target) {
  51. switch (target) {
  52. case SurfaceTarget::Texture1D:
  53. case SurfaceTarget::TextureBuffer:
  54. case SurfaceTarget::Texture2D:
  55. case SurfaceTarget::Texture3D:
  56. case SurfaceTarget::TextureCubemap:
  57. return false;
  58. case SurfaceTarget::Texture1DArray:
  59. case SurfaceTarget::Texture2DArray:
  60. case SurfaceTarget::TextureCubeArray:
  61. return true;
  62. default:
  63. LOG_CRITICAL(HW_GPU, "Unimplemented surface_target={}", target);
  64. ASSERT(false);
  65. return false;
  66. }
  67. }
  68. PixelFormat PixelFormatFromDepthFormat(Tegra::DepthFormat format) {
  69. switch (format) {
  70. case Tegra::DepthFormat::Z24_UNORM_S8_UINT:
  71. return PixelFormat::S8_UINT_D24_UNORM;
  72. case Tegra::DepthFormat::S8Z24_UNORM:
  73. return PixelFormat::D24_UNORM_S8_UINT;
  74. case Tegra::DepthFormat::Z32_FLOAT:
  75. return PixelFormat::D32_FLOAT;
  76. case Tegra::DepthFormat::Z16_UNORM:
  77. return PixelFormat::D16_UNORM;
  78. case Tegra::DepthFormat::S8_UINT:
  79. return PixelFormat::S8_UINT;
  80. case Tegra::DepthFormat::Z32_FLOAT_X24S8_UINT:
  81. return PixelFormat::D32_FLOAT_S8_UINT;
  82. default:
  83. UNIMPLEMENTED_MSG("Unimplemented format={}", format);
  84. return PixelFormat::S8_UINT_D24_UNORM;
  85. }
  86. }
  87. PixelFormat PixelFormatFromRenderTargetFormat(Tegra::RenderTargetFormat format) {
  88. switch (format) {
  89. case Tegra::RenderTargetFormat::R32G32B32A32_FLOAT:
  90. case Tegra::RenderTargetFormat::R32G32B32X32_FLOAT:
  91. return PixelFormat::R32G32B32A32_FLOAT;
  92. case Tegra::RenderTargetFormat::R32G32B32A32_SINT:
  93. case Tegra::RenderTargetFormat::R32G32B32X32_SINT:
  94. return PixelFormat::R32G32B32A32_SINT;
  95. case Tegra::RenderTargetFormat::R32G32B32A32_UINT:
  96. case Tegra::RenderTargetFormat::R32G32B32X32_UINT:
  97. return PixelFormat::R32G32B32A32_UINT;
  98. case Tegra::RenderTargetFormat::R16G16B16A16_UNORM:
  99. return PixelFormat::R16G16B16A16_UNORM;
  100. case Tegra::RenderTargetFormat::R16G16B16A16_SNORM:
  101. return PixelFormat::R16G16B16A16_SNORM;
  102. case Tegra::RenderTargetFormat::R16G16B16A16_SINT:
  103. return PixelFormat::R16G16B16A16_SINT;
  104. case Tegra::RenderTargetFormat::R16G16B16A16_UINT:
  105. return PixelFormat::R16G16B16A16_UINT;
  106. case Tegra::RenderTargetFormat::R16G16B16A16_FLOAT:
  107. return PixelFormat::R16G16B16A16_FLOAT;
  108. case Tegra::RenderTargetFormat::R32G32_FLOAT:
  109. return PixelFormat::R32G32_FLOAT;
  110. case Tegra::RenderTargetFormat::R32G32_SINT:
  111. return PixelFormat::R32G32_SINT;
  112. case Tegra::RenderTargetFormat::R32G32_UINT:
  113. return PixelFormat::R32G32_UINT;
  114. case Tegra::RenderTargetFormat::R16G16B16X16_FLOAT:
  115. return PixelFormat::R16G16B16X16_FLOAT;
  116. case Tegra::RenderTargetFormat::A8R8G8B8_UNORM:
  117. case Tegra::RenderTargetFormat::X8R8G8B8_UNORM:
  118. return PixelFormat::B8G8R8A8_UNORM;
  119. case Tegra::RenderTargetFormat::A8R8G8B8_SRGB:
  120. case Tegra::RenderTargetFormat::X8R8G8B8_SRGB:
  121. return PixelFormat::B8G8R8A8_SRGB;
  122. case Tegra::RenderTargetFormat::A2B10G10R10_UNORM:
  123. return PixelFormat::A2B10G10R10_UNORM;
  124. case Tegra::RenderTargetFormat::A2B10G10R10_UINT:
  125. return PixelFormat::A2B10G10R10_UINT;
  126. case Tegra::RenderTargetFormat::A2R10G10B10_UNORM:
  127. return PixelFormat::A2R10G10B10_UNORM;
  128. case Tegra::RenderTargetFormat::A8B8G8R8_UNORM:
  129. case Tegra::RenderTargetFormat::X8B8G8R8_UNORM:
  130. return PixelFormat::A8B8G8R8_UNORM;
  131. case Tegra::RenderTargetFormat::A8B8G8R8_SRGB:
  132. case Tegra::RenderTargetFormat::X8B8G8R8_SRGB:
  133. return PixelFormat::A8B8G8R8_SRGB;
  134. case Tegra::RenderTargetFormat::A8B8G8R8_SNORM:
  135. return PixelFormat::A8B8G8R8_SNORM;
  136. case Tegra::RenderTargetFormat::A8B8G8R8_SINT:
  137. return PixelFormat::A8B8G8R8_SINT;
  138. case Tegra::RenderTargetFormat::A8B8G8R8_UINT:
  139. return PixelFormat::A8B8G8R8_UINT;
  140. case Tegra::RenderTargetFormat::R16G16_UNORM:
  141. return PixelFormat::R16G16_UNORM;
  142. case Tegra::RenderTargetFormat::R16G16_SNORM:
  143. return PixelFormat::R16G16_SNORM;
  144. case Tegra::RenderTargetFormat::R16G16_SINT:
  145. return PixelFormat::R16G16_SINT;
  146. case Tegra::RenderTargetFormat::R16G16_UINT:
  147. return PixelFormat::R16G16_UINT;
  148. case Tegra::RenderTargetFormat::R16G16_FLOAT:
  149. return PixelFormat::R16G16_FLOAT;
  150. case Tegra::RenderTargetFormat::B10G11R11_FLOAT:
  151. return PixelFormat::B10G11R11_FLOAT;
  152. case Tegra::RenderTargetFormat::R32_SINT:
  153. return PixelFormat::R32_SINT;
  154. case Tegra::RenderTargetFormat::R32_UINT:
  155. return PixelFormat::R32_UINT;
  156. case Tegra::RenderTargetFormat::R32_FLOAT:
  157. return PixelFormat::R32_FLOAT;
  158. case Tegra::RenderTargetFormat::R5G6B5_UNORM:
  159. return PixelFormat::R5G6B5_UNORM;
  160. case Tegra::RenderTargetFormat::A1R5G5B5_UNORM:
  161. case Tegra::RenderTargetFormat::X1R5G5B5_UNORM:
  162. return PixelFormat::A1R5G5B5_UNORM;
  163. case Tegra::RenderTargetFormat::R8G8_UNORM:
  164. return PixelFormat::R8G8_UNORM;
  165. case Tegra::RenderTargetFormat::R8G8_SNORM:
  166. return PixelFormat::R8G8_SNORM;
  167. case Tegra::RenderTargetFormat::R8G8_SINT:
  168. return PixelFormat::R8G8_SINT;
  169. case Tegra::RenderTargetFormat::R8G8_UINT:
  170. return PixelFormat::R8G8_UINT;
  171. case Tegra::RenderTargetFormat::R16_UNORM:
  172. return PixelFormat::R16_UNORM;
  173. case Tegra::RenderTargetFormat::R16_SNORM:
  174. return PixelFormat::R16_SNORM;
  175. case Tegra::RenderTargetFormat::R16_SINT:
  176. return PixelFormat::R16_SINT;
  177. case Tegra::RenderTargetFormat::R16_UINT:
  178. return PixelFormat::R16_UINT;
  179. case Tegra::RenderTargetFormat::R16_FLOAT:
  180. return PixelFormat::R16_FLOAT;
  181. case Tegra::RenderTargetFormat::R8_UNORM:
  182. return PixelFormat::R8_UNORM;
  183. case Tegra::RenderTargetFormat::R8_SNORM:
  184. return PixelFormat::R8_SNORM;
  185. case Tegra::RenderTargetFormat::R8_SINT:
  186. return PixelFormat::R8_SINT;
  187. case Tegra::RenderTargetFormat::R8_UINT:
  188. return PixelFormat::R8_UINT;
  189. default:
  190. UNIMPLEMENTED_MSG("Unimplemented format={}", format);
  191. return PixelFormat::A8B8G8R8_UNORM;
  192. }
  193. }
  194. PixelFormat PixelFormatFromGPUPixelFormat(Service::android::PixelFormat format) {
  195. switch (format) {
  196. case Service::android::PixelFormat::Rgba8888:
  197. return PixelFormat::A8B8G8R8_UNORM;
  198. case Service::android::PixelFormat::Rgb565:
  199. return PixelFormat::R5G6B5_UNORM;
  200. case Service::android::PixelFormat::Bgra8888:
  201. return PixelFormat::B8G8R8A8_UNORM;
  202. default:
  203. UNIMPLEMENTED_MSG("Unimplemented format={}", format);
  204. return PixelFormat::A8B8G8R8_UNORM;
  205. }
  206. }
  207. SurfaceType GetFormatType(PixelFormat pixel_format) {
  208. if (pixel_format < PixelFormat::MaxColorFormat) {
  209. return SurfaceType::ColorTexture;
  210. }
  211. if (pixel_format < PixelFormat::MaxDepthFormat) {
  212. return SurfaceType::Depth;
  213. }
  214. if (pixel_format < PixelFormat::MaxStencilFormat) {
  215. return SurfaceType::Stencil;
  216. }
  217. if (pixel_format < PixelFormat::MaxDepthStencilFormat) {
  218. return SurfaceType::DepthStencil;
  219. }
  220. // TODO(Subv): Implement the other formats
  221. ASSERT(false);
  222. return SurfaceType::Invalid;
  223. }
  224. bool IsPixelFormatASTC(PixelFormat format) {
  225. switch (format) {
  226. case PixelFormat::ASTC_2D_4X4_UNORM:
  227. case PixelFormat::ASTC_2D_5X4_UNORM:
  228. case PixelFormat::ASTC_2D_5X5_UNORM:
  229. case PixelFormat::ASTC_2D_8X8_UNORM:
  230. case PixelFormat::ASTC_2D_8X5_UNORM:
  231. case PixelFormat::ASTC_2D_4X4_SRGB:
  232. case PixelFormat::ASTC_2D_5X4_SRGB:
  233. case PixelFormat::ASTC_2D_5X5_SRGB:
  234. case PixelFormat::ASTC_2D_8X8_SRGB:
  235. case PixelFormat::ASTC_2D_8X5_SRGB:
  236. case PixelFormat::ASTC_2D_10X8_UNORM:
  237. case PixelFormat::ASTC_2D_10X8_SRGB:
  238. case PixelFormat::ASTC_2D_6X6_UNORM:
  239. case PixelFormat::ASTC_2D_6X6_SRGB:
  240. case PixelFormat::ASTC_2D_10X6_UNORM:
  241. case PixelFormat::ASTC_2D_10X6_SRGB:
  242. case PixelFormat::ASTC_2D_10X5_UNORM:
  243. case PixelFormat::ASTC_2D_10X5_SRGB:
  244. case PixelFormat::ASTC_2D_10X10_UNORM:
  245. case PixelFormat::ASTC_2D_10X10_SRGB:
  246. case PixelFormat::ASTC_2D_12X10_UNORM:
  247. case PixelFormat::ASTC_2D_12X10_SRGB:
  248. case PixelFormat::ASTC_2D_12X12_UNORM:
  249. case PixelFormat::ASTC_2D_12X12_SRGB:
  250. case PixelFormat::ASTC_2D_8X6_UNORM:
  251. case PixelFormat::ASTC_2D_8X6_SRGB:
  252. case PixelFormat::ASTC_2D_6X5_UNORM:
  253. case PixelFormat::ASTC_2D_6X5_SRGB:
  254. return true;
  255. default:
  256. return false;
  257. }
  258. }
  259. bool IsPixelFormatBCn(PixelFormat format) {
  260. switch (format) {
  261. case PixelFormat::BC1_RGBA_UNORM:
  262. case PixelFormat::BC2_UNORM:
  263. case PixelFormat::BC3_UNORM:
  264. case PixelFormat::BC4_UNORM:
  265. case PixelFormat::BC4_SNORM:
  266. case PixelFormat::BC5_UNORM:
  267. case PixelFormat::BC5_SNORM:
  268. case PixelFormat::BC1_RGBA_SRGB:
  269. case PixelFormat::BC2_SRGB:
  270. case PixelFormat::BC3_SRGB:
  271. case PixelFormat::BC7_UNORM:
  272. case PixelFormat::BC6H_UFLOAT:
  273. case PixelFormat::BC6H_SFLOAT:
  274. case PixelFormat::BC7_SRGB:
  275. return true;
  276. default:
  277. return false;
  278. }
  279. }
  280. bool IsPixelFormatSRGB(PixelFormat format) {
  281. switch (format) {
  282. case PixelFormat::A8B8G8R8_SRGB:
  283. case PixelFormat::B8G8R8A8_SRGB:
  284. case PixelFormat::BC1_RGBA_SRGB:
  285. case PixelFormat::BC2_SRGB:
  286. case PixelFormat::BC3_SRGB:
  287. case PixelFormat::BC7_SRGB:
  288. case PixelFormat::ASTC_2D_4X4_SRGB:
  289. case PixelFormat::ASTC_2D_8X8_SRGB:
  290. case PixelFormat::ASTC_2D_8X5_SRGB:
  291. case PixelFormat::ASTC_2D_5X4_SRGB:
  292. case PixelFormat::ASTC_2D_5X5_SRGB:
  293. case PixelFormat::ASTC_2D_10X6_SRGB:
  294. case PixelFormat::ASTC_2D_10X8_SRGB:
  295. case PixelFormat::ASTC_2D_6X6_SRGB:
  296. case PixelFormat::ASTC_2D_10X5_SRGB:
  297. case PixelFormat::ASTC_2D_10X10_SRGB:
  298. case PixelFormat::ASTC_2D_12X12_SRGB:
  299. case PixelFormat::ASTC_2D_12X10_SRGB:
  300. case PixelFormat::ASTC_2D_8X6_SRGB:
  301. case PixelFormat::ASTC_2D_6X5_SRGB:
  302. return true;
  303. default:
  304. return false;
  305. }
  306. }
  307. bool IsPixelFormatInteger(PixelFormat format) {
  308. switch (format) {
  309. case PixelFormat::A8B8G8R8_SINT:
  310. case PixelFormat::A8B8G8R8_UINT:
  311. case PixelFormat::A2B10G10R10_UINT:
  312. case PixelFormat::R8_SINT:
  313. case PixelFormat::R8_UINT:
  314. case PixelFormat::R16G16B16A16_SINT:
  315. case PixelFormat::R16G16B16A16_UINT:
  316. case PixelFormat::R32G32B32A32_UINT:
  317. case PixelFormat::R32G32B32A32_SINT:
  318. case PixelFormat::R32G32_SINT:
  319. case PixelFormat::R16_UINT:
  320. case PixelFormat::R16_SINT:
  321. case PixelFormat::R16G16_UINT:
  322. case PixelFormat::R16G16_SINT:
  323. case PixelFormat::R8G8_SINT:
  324. case PixelFormat::R8G8_UINT:
  325. case PixelFormat::R32G32_UINT:
  326. case PixelFormat::R32_UINT:
  327. case PixelFormat::R32_SINT:
  328. return true;
  329. default:
  330. return false;
  331. }
  332. }
  333. bool IsPixelFormatSignedInteger(PixelFormat format) {
  334. switch (format) {
  335. case PixelFormat::A8B8G8R8_SINT:
  336. case PixelFormat::R8_SINT:
  337. case PixelFormat::R16G16B16A16_SINT:
  338. case PixelFormat::R32G32B32A32_SINT:
  339. case PixelFormat::R32G32_SINT:
  340. case PixelFormat::R16_SINT:
  341. case PixelFormat::R16G16_SINT:
  342. case PixelFormat::R8G8_SINT:
  343. case PixelFormat::R32_SINT:
  344. return true;
  345. default:
  346. return false;
  347. }
  348. }
  349. size_t PixelComponentSizeBitsInteger(PixelFormat format) {
  350. switch (format) {
  351. case PixelFormat::A8B8G8R8_SINT:
  352. case PixelFormat::A8B8G8R8_UINT:
  353. case PixelFormat::R8_SINT:
  354. case PixelFormat::R8_UINT:
  355. case PixelFormat::R8G8_SINT:
  356. case PixelFormat::R8G8_UINT:
  357. return 8;
  358. case PixelFormat::A2B10G10R10_UINT:
  359. return 10;
  360. case PixelFormat::R16G16B16A16_SINT:
  361. case PixelFormat::R16G16B16A16_UINT:
  362. case PixelFormat::R16_UINT:
  363. case PixelFormat::R16_SINT:
  364. case PixelFormat::R16G16_UINT:
  365. case PixelFormat::R16G16_SINT:
  366. return 16;
  367. case PixelFormat::R32G32B32A32_UINT:
  368. case PixelFormat::R32G32B32A32_SINT:
  369. case PixelFormat::R32G32_SINT:
  370. case PixelFormat::R32G32_UINT:
  371. case PixelFormat::R32_UINT:
  372. case PixelFormat::R32_SINT:
  373. return 32;
  374. default:
  375. return 0;
  376. }
  377. }
  378. std::pair<u32, u32> GetASTCBlockSize(PixelFormat format) {
  379. return {DefaultBlockWidth(format), DefaultBlockHeight(format)};
  380. }
  381. u64 EstimatedDecompressedSize(u64 base_size, PixelFormat format) {
  382. constexpr u64 RGBA8_PIXEL_SIZE = 4;
  383. const u64 base_block_size = static_cast<u64>(DefaultBlockWidth(format)) *
  384. static_cast<u64>(DefaultBlockHeight(format)) * RGBA8_PIXEL_SIZE;
  385. return (base_size * base_block_size) / BytesPerBlock(format);
  386. }
  387. } // namespace VideoCore::Surface