surface.cpp 15 KB

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