surface.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. case Tegra::DepthFormat::X8Z24_UNORM:
  83. return PixelFormat::X8_D24_UNORM;
  84. default:
  85. UNIMPLEMENTED_MSG("Unimplemented format={}", format);
  86. return PixelFormat::S8_UINT_D24_UNORM;
  87. }
  88. }
  89. PixelFormat PixelFormatFromRenderTargetFormat(Tegra::RenderTargetFormat format) {
  90. switch (format) {
  91. case Tegra::RenderTargetFormat::R32G32B32A32_FLOAT:
  92. case Tegra::RenderTargetFormat::R32G32B32X32_FLOAT:
  93. return PixelFormat::R32G32B32A32_FLOAT;
  94. case Tegra::RenderTargetFormat::R32G32B32A32_SINT:
  95. case Tegra::RenderTargetFormat::R32G32B32X32_SINT:
  96. return PixelFormat::R32G32B32A32_SINT;
  97. case Tegra::RenderTargetFormat::R32G32B32A32_UINT:
  98. case Tegra::RenderTargetFormat::R32G32B32X32_UINT:
  99. return PixelFormat::R32G32B32A32_UINT;
  100. case Tegra::RenderTargetFormat::R16G16B16A16_UNORM:
  101. return PixelFormat::R16G16B16A16_UNORM;
  102. case Tegra::RenderTargetFormat::R16G16B16A16_SNORM:
  103. return PixelFormat::R16G16B16A16_SNORM;
  104. case Tegra::RenderTargetFormat::R16G16B16A16_SINT:
  105. return PixelFormat::R16G16B16A16_SINT;
  106. case Tegra::RenderTargetFormat::R16G16B16A16_UINT:
  107. return PixelFormat::R16G16B16A16_UINT;
  108. case Tegra::RenderTargetFormat::R16G16B16A16_FLOAT:
  109. return PixelFormat::R16G16B16A16_FLOAT;
  110. case Tegra::RenderTargetFormat::R32G32_FLOAT:
  111. return PixelFormat::R32G32_FLOAT;
  112. case Tegra::RenderTargetFormat::R32G32_SINT:
  113. return PixelFormat::R32G32_SINT;
  114. case Tegra::RenderTargetFormat::R32G32_UINT:
  115. return PixelFormat::R32G32_UINT;
  116. case Tegra::RenderTargetFormat::R16G16B16X16_FLOAT:
  117. return PixelFormat::R16G16B16X16_FLOAT;
  118. case Tegra::RenderTargetFormat::A8R8G8B8_UNORM:
  119. case Tegra::RenderTargetFormat::X8R8G8B8_UNORM:
  120. return PixelFormat::B8G8R8A8_UNORM;
  121. case Tegra::RenderTargetFormat::A8R8G8B8_SRGB:
  122. case Tegra::RenderTargetFormat::X8R8G8B8_SRGB:
  123. return PixelFormat::B8G8R8A8_SRGB;
  124. case Tegra::RenderTargetFormat::A2B10G10R10_UNORM:
  125. return PixelFormat::A2B10G10R10_UNORM;
  126. case Tegra::RenderTargetFormat::A2B10G10R10_UINT:
  127. return PixelFormat::A2B10G10R10_UINT;
  128. case Tegra::RenderTargetFormat::A2R10G10B10_UNORM:
  129. return PixelFormat::A2R10G10B10_UNORM;
  130. case Tegra::RenderTargetFormat::A8B8G8R8_UNORM:
  131. case Tegra::RenderTargetFormat::X8B8G8R8_UNORM:
  132. return PixelFormat::A8B8G8R8_UNORM;
  133. case Tegra::RenderTargetFormat::A8B8G8R8_SRGB:
  134. case Tegra::RenderTargetFormat::X8B8G8R8_SRGB:
  135. return PixelFormat::A8B8G8R8_SRGB;
  136. case Tegra::RenderTargetFormat::A8B8G8R8_SNORM:
  137. return PixelFormat::A8B8G8R8_SNORM;
  138. case Tegra::RenderTargetFormat::A8B8G8R8_SINT:
  139. return PixelFormat::A8B8G8R8_SINT;
  140. case Tegra::RenderTargetFormat::A8B8G8R8_UINT:
  141. return PixelFormat::A8B8G8R8_UINT;
  142. case Tegra::RenderTargetFormat::R16G16_UNORM:
  143. return PixelFormat::R16G16_UNORM;
  144. case Tegra::RenderTargetFormat::R16G16_SNORM:
  145. return PixelFormat::R16G16_SNORM;
  146. case Tegra::RenderTargetFormat::R16G16_SINT:
  147. return PixelFormat::R16G16_SINT;
  148. case Tegra::RenderTargetFormat::R16G16_UINT:
  149. return PixelFormat::R16G16_UINT;
  150. case Tegra::RenderTargetFormat::R16G16_FLOAT:
  151. return PixelFormat::R16G16_FLOAT;
  152. case Tegra::RenderTargetFormat::B10G11R11_FLOAT:
  153. return PixelFormat::B10G11R11_FLOAT;
  154. case Tegra::RenderTargetFormat::R32_SINT:
  155. return PixelFormat::R32_SINT;
  156. case Tegra::RenderTargetFormat::R32_UINT:
  157. return PixelFormat::R32_UINT;
  158. case Tegra::RenderTargetFormat::R32_FLOAT:
  159. return PixelFormat::R32_FLOAT;
  160. case Tegra::RenderTargetFormat::R5G6B5_UNORM:
  161. return PixelFormat::R5G6B5_UNORM;
  162. case Tegra::RenderTargetFormat::A1R5G5B5_UNORM:
  163. case Tegra::RenderTargetFormat::X1R5G5B5_UNORM:
  164. return PixelFormat::A1R5G5B5_UNORM;
  165. case Tegra::RenderTargetFormat::R8G8_UNORM:
  166. return PixelFormat::R8G8_UNORM;
  167. case Tegra::RenderTargetFormat::R8G8_SNORM:
  168. return PixelFormat::R8G8_SNORM;
  169. case Tegra::RenderTargetFormat::R8G8_SINT:
  170. return PixelFormat::R8G8_SINT;
  171. case Tegra::RenderTargetFormat::R8G8_UINT:
  172. return PixelFormat::R8G8_UINT;
  173. case Tegra::RenderTargetFormat::R16_UNORM:
  174. return PixelFormat::R16_UNORM;
  175. case Tegra::RenderTargetFormat::R16_SNORM:
  176. return PixelFormat::R16_SNORM;
  177. case Tegra::RenderTargetFormat::R16_SINT:
  178. return PixelFormat::R16_SINT;
  179. case Tegra::RenderTargetFormat::R16_UINT:
  180. return PixelFormat::R16_UINT;
  181. case Tegra::RenderTargetFormat::R16_FLOAT:
  182. return PixelFormat::R16_FLOAT;
  183. case Tegra::RenderTargetFormat::R8_UNORM:
  184. return PixelFormat::R8_UNORM;
  185. case Tegra::RenderTargetFormat::R8_SNORM:
  186. return PixelFormat::R8_SNORM;
  187. case Tegra::RenderTargetFormat::R8_SINT:
  188. return PixelFormat::R8_SINT;
  189. case Tegra::RenderTargetFormat::R8_UINT:
  190. return PixelFormat::R8_UINT;
  191. default:
  192. UNIMPLEMENTED_MSG("Unimplemented format={}", format);
  193. return PixelFormat::A8B8G8R8_UNORM;
  194. }
  195. }
  196. PixelFormat PixelFormatFromGPUPixelFormat(Service::android::PixelFormat format) {
  197. switch (format) {
  198. case Service::android::PixelFormat::Rgba8888:
  199. case Service::android::PixelFormat::Rgbx8888:
  200. return PixelFormat::A8B8G8R8_UNORM;
  201. case Service::android::PixelFormat::Rgb565:
  202. return PixelFormat::R5G6B5_UNORM;
  203. case Service::android::PixelFormat::Bgra8888:
  204. return PixelFormat::B8G8R8A8_UNORM;
  205. default:
  206. UNIMPLEMENTED_MSG("Unimplemented format={}", format);
  207. return PixelFormat::A8B8G8R8_UNORM;
  208. }
  209. }
  210. SurfaceType GetFormatType(PixelFormat pixel_format) {
  211. if (pixel_format < PixelFormat::MaxColorFormat) {
  212. return SurfaceType::ColorTexture;
  213. }
  214. if (pixel_format < PixelFormat::MaxDepthFormat) {
  215. return SurfaceType::Depth;
  216. }
  217. if (pixel_format < PixelFormat::MaxStencilFormat) {
  218. return SurfaceType::Stencil;
  219. }
  220. if (pixel_format < PixelFormat::MaxDepthStencilFormat) {
  221. return SurfaceType::DepthStencil;
  222. }
  223. // TODO(Subv): Implement the other formats
  224. ASSERT(false);
  225. return SurfaceType::Invalid;
  226. }
  227. bool IsPixelFormatASTC(PixelFormat format) {
  228. switch (format) {
  229. case PixelFormat::ASTC_2D_4X4_UNORM:
  230. case PixelFormat::ASTC_2D_5X4_UNORM:
  231. case PixelFormat::ASTC_2D_5X5_UNORM:
  232. case PixelFormat::ASTC_2D_8X8_UNORM:
  233. case PixelFormat::ASTC_2D_8X5_UNORM:
  234. case PixelFormat::ASTC_2D_4X4_SRGB:
  235. case PixelFormat::ASTC_2D_5X4_SRGB:
  236. case PixelFormat::ASTC_2D_5X5_SRGB:
  237. case PixelFormat::ASTC_2D_8X8_SRGB:
  238. case PixelFormat::ASTC_2D_8X5_SRGB:
  239. case PixelFormat::ASTC_2D_10X8_UNORM:
  240. case PixelFormat::ASTC_2D_10X8_SRGB:
  241. case PixelFormat::ASTC_2D_6X6_UNORM:
  242. case PixelFormat::ASTC_2D_6X6_SRGB:
  243. case PixelFormat::ASTC_2D_10X6_UNORM:
  244. case PixelFormat::ASTC_2D_10X6_SRGB:
  245. case PixelFormat::ASTC_2D_10X5_UNORM:
  246. case PixelFormat::ASTC_2D_10X5_SRGB:
  247. case PixelFormat::ASTC_2D_10X10_UNORM:
  248. case PixelFormat::ASTC_2D_10X10_SRGB:
  249. case PixelFormat::ASTC_2D_12X10_UNORM:
  250. case PixelFormat::ASTC_2D_12X10_SRGB:
  251. case PixelFormat::ASTC_2D_12X12_UNORM:
  252. case PixelFormat::ASTC_2D_12X12_SRGB:
  253. case PixelFormat::ASTC_2D_8X6_UNORM:
  254. case PixelFormat::ASTC_2D_8X6_SRGB:
  255. case PixelFormat::ASTC_2D_6X5_UNORM:
  256. case PixelFormat::ASTC_2D_6X5_SRGB:
  257. return true;
  258. default:
  259. return false;
  260. }
  261. }
  262. bool IsPixelFormatBCn(PixelFormat format) {
  263. switch (format) {
  264. case PixelFormat::BC1_RGBA_UNORM:
  265. case PixelFormat::BC2_UNORM:
  266. case PixelFormat::BC3_UNORM:
  267. case PixelFormat::BC4_UNORM:
  268. case PixelFormat::BC4_SNORM:
  269. case PixelFormat::BC5_UNORM:
  270. case PixelFormat::BC5_SNORM:
  271. case PixelFormat::BC1_RGBA_SRGB:
  272. case PixelFormat::BC2_SRGB:
  273. case PixelFormat::BC3_SRGB:
  274. case PixelFormat::BC7_UNORM:
  275. case PixelFormat::BC6H_UFLOAT:
  276. case PixelFormat::BC6H_SFLOAT:
  277. case PixelFormat::BC7_SRGB:
  278. return true;
  279. default:
  280. return false;
  281. }
  282. }
  283. bool IsPixelFormatSRGB(PixelFormat format) {
  284. switch (format) {
  285. case PixelFormat::A8B8G8R8_SRGB:
  286. case PixelFormat::B8G8R8A8_SRGB:
  287. case PixelFormat::BC1_RGBA_SRGB:
  288. case PixelFormat::BC2_SRGB:
  289. case PixelFormat::BC3_SRGB:
  290. case PixelFormat::BC7_SRGB:
  291. case PixelFormat::ASTC_2D_4X4_SRGB:
  292. case PixelFormat::ASTC_2D_8X8_SRGB:
  293. case PixelFormat::ASTC_2D_8X5_SRGB:
  294. case PixelFormat::ASTC_2D_5X4_SRGB:
  295. case PixelFormat::ASTC_2D_5X5_SRGB:
  296. case PixelFormat::ASTC_2D_10X6_SRGB:
  297. case PixelFormat::ASTC_2D_10X8_SRGB:
  298. case PixelFormat::ASTC_2D_6X6_SRGB:
  299. case PixelFormat::ASTC_2D_10X5_SRGB:
  300. case PixelFormat::ASTC_2D_10X10_SRGB:
  301. case PixelFormat::ASTC_2D_12X12_SRGB:
  302. case PixelFormat::ASTC_2D_12X10_SRGB:
  303. case PixelFormat::ASTC_2D_8X6_SRGB:
  304. case PixelFormat::ASTC_2D_6X5_SRGB:
  305. return true;
  306. default:
  307. return false;
  308. }
  309. }
  310. bool IsPixelFormatInteger(PixelFormat format) {
  311. switch (format) {
  312. case PixelFormat::A8B8G8R8_SINT:
  313. case PixelFormat::A8B8G8R8_UINT:
  314. case PixelFormat::A2B10G10R10_UINT:
  315. case PixelFormat::R8_SINT:
  316. case PixelFormat::R8_UINT:
  317. case PixelFormat::R16G16B16A16_SINT:
  318. case PixelFormat::R16G16B16A16_UINT:
  319. case PixelFormat::R32G32B32A32_UINT:
  320. case PixelFormat::R32G32B32A32_SINT:
  321. case PixelFormat::R32G32_SINT:
  322. case PixelFormat::R16_UINT:
  323. case PixelFormat::R16_SINT:
  324. case PixelFormat::R16G16_UINT:
  325. case PixelFormat::R16G16_SINT:
  326. case PixelFormat::R8G8_SINT:
  327. case PixelFormat::R8G8_UINT:
  328. case PixelFormat::R32G32_UINT:
  329. case PixelFormat::R32_UINT:
  330. case PixelFormat::R32_SINT:
  331. return true;
  332. default:
  333. return false;
  334. }
  335. }
  336. bool IsPixelFormatSignedInteger(PixelFormat format) {
  337. switch (format) {
  338. case PixelFormat::A8B8G8R8_SINT:
  339. case PixelFormat::R8_SINT:
  340. case PixelFormat::R16G16B16A16_SINT:
  341. case PixelFormat::R32G32B32A32_SINT:
  342. case PixelFormat::R32G32_SINT:
  343. case PixelFormat::R16_SINT:
  344. case PixelFormat::R16G16_SINT:
  345. case PixelFormat::R8G8_SINT:
  346. case PixelFormat::R32_SINT:
  347. return true;
  348. default:
  349. return false;
  350. }
  351. }
  352. size_t PixelComponentSizeBitsInteger(PixelFormat format) {
  353. switch (format) {
  354. case PixelFormat::A8B8G8R8_SINT:
  355. case PixelFormat::A8B8G8R8_UINT:
  356. case PixelFormat::R8_SINT:
  357. case PixelFormat::R8_UINT:
  358. case PixelFormat::R8G8_SINT:
  359. case PixelFormat::R8G8_UINT:
  360. return 8;
  361. case PixelFormat::A2B10G10R10_UINT:
  362. return 10;
  363. case PixelFormat::R16G16B16A16_SINT:
  364. case PixelFormat::R16G16B16A16_UINT:
  365. case PixelFormat::R16_UINT:
  366. case PixelFormat::R16_SINT:
  367. case PixelFormat::R16G16_UINT:
  368. case PixelFormat::R16G16_SINT:
  369. return 16;
  370. case PixelFormat::R32G32B32A32_UINT:
  371. case PixelFormat::R32G32B32A32_SINT:
  372. case PixelFormat::R32G32_SINT:
  373. case PixelFormat::R32G32_UINT:
  374. case PixelFormat::R32_UINT:
  375. case PixelFormat::R32_SINT:
  376. return 32;
  377. default:
  378. return 0;
  379. }
  380. }
  381. std::pair<u32, u32> GetASTCBlockSize(PixelFormat format) {
  382. return {DefaultBlockWidth(format), DefaultBlockHeight(format)};
  383. }
  384. u64 EstimatedDecompressedSize(u64 base_size, PixelFormat format) {
  385. constexpr u64 RGBA8_PIXEL_SIZE = 4;
  386. const u64 base_block_size = static_cast<u64>(DefaultBlockWidth(format)) *
  387. static_cast<u64>(DefaultBlockHeight(format)) * RGBA8_PIXEL_SIZE;
  388. return (base_size * base_block_size) / BytesPerBlock(format);
  389. }
  390. } // namespace VideoCore::Surface