surface.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include "common/common_types.h"
  5. #include "common/math_util.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={}", static_cast<u32>(texture_type));
  29. UNREACHABLE();
  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={}", static_cast<u32>(target));
  47. UNREACHABLE();
  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={}", static_cast<u32>(target));
  65. UNREACHABLE();
  66. return false;
  67. }
  68. }
  69. PixelFormat PixelFormatFromDepthFormat(Tegra::DepthFormat format) {
  70. switch (format) {
  71. case Tegra::DepthFormat::S8_Z24_UNORM:
  72. return PixelFormat::S8Z24;
  73. case Tegra::DepthFormat::Z24_S8_UNORM:
  74. return PixelFormat::Z24S8;
  75. case Tegra::DepthFormat::Z32_FLOAT:
  76. return PixelFormat::Z32F;
  77. case Tegra::DepthFormat::Z16_UNORM:
  78. return PixelFormat::Z16;
  79. case Tegra::DepthFormat::Z32_S8_X24_FLOAT:
  80. return PixelFormat::Z32FS8;
  81. default:
  82. LOG_CRITICAL(HW_GPU, "Unimplemented format={}", static_cast<u32>(format));
  83. UNREACHABLE();
  84. return PixelFormat::S8Z24;
  85. }
  86. }
  87. PixelFormat PixelFormatFromRenderTargetFormat(Tegra::RenderTargetFormat format) {
  88. switch (format) {
  89. case Tegra::RenderTargetFormat::RGBA8_SRGB:
  90. return PixelFormat::RGBA8_SRGB;
  91. case Tegra::RenderTargetFormat::RGBA8_UNORM:
  92. return PixelFormat::ABGR8U;
  93. case Tegra::RenderTargetFormat::RGBA8_SNORM:
  94. return PixelFormat::ABGR8S;
  95. case Tegra::RenderTargetFormat::RGBA8_UINT:
  96. return PixelFormat::ABGR8UI;
  97. case Tegra::RenderTargetFormat::BGRA8_SRGB:
  98. return PixelFormat::BGRA8_SRGB;
  99. case Tegra::RenderTargetFormat::BGRA8_UNORM:
  100. return PixelFormat::BGRA8;
  101. case Tegra::RenderTargetFormat::RGB10_A2_UNORM:
  102. return PixelFormat::A2B10G10R10U;
  103. case Tegra::RenderTargetFormat::RGBA16_FLOAT:
  104. return PixelFormat::RGBA16F;
  105. case Tegra::RenderTargetFormat::RGBA16_UNORM:
  106. return PixelFormat::RGBA16U;
  107. case Tegra::RenderTargetFormat::RGBA16_UINT:
  108. return PixelFormat::RGBA16UI;
  109. case Tegra::RenderTargetFormat::RGBA32_FLOAT:
  110. return PixelFormat::RGBA32F;
  111. case Tegra::RenderTargetFormat::RG32_FLOAT:
  112. return PixelFormat::RG32F;
  113. case Tegra::RenderTargetFormat::R11G11B10_FLOAT:
  114. return PixelFormat::R11FG11FB10F;
  115. case Tegra::RenderTargetFormat::B5G6R5_UNORM:
  116. return PixelFormat::B5G6R5U;
  117. case Tegra::RenderTargetFormat::BGR5A1_UNORM:
  118. return PixelFormat::A1B5G5R5U;
  119. case Tegra::RenderTargetFormat::RGBA32_UINT:
  120. return PixelFormat::RGBA32UI;
  121. case Tegra::RenderTargetFormat::R8_UNORM:
  122. return PixelFormat::R8U;
  123. case Tegra::RenderTargetFormat::R8_UINT:
  124. return PixelFormat::R8UI;
  125. case Tegra::RenderTargetFormat::RG16_FLOAT:
  126. return PixelFormat::RG16F;
  127. case Tegra::RenderTargetFormat::RG16_UINT:
  128. return PixelFormat::RG16UI;
  129. case Tegra::RenderTargetFormat::RG16_SINT:
  130. return PixelFormat::RG16I;
  131. case Tegra::RenderTargetFormat::RG16_UNORM:
  132. return PixelFormat::RG16;
  133. case Tegra::RenderTargetFormat::RG16_SNORM:
  134. return PixelFormat::RG16S;
  135. case Tegra::RenderTargetFormat::RG8_UNORM:
  136. return PixelFormat::RG8U;
  137. case Tegra::RenderTargetFormat::RG8_SNORM:
  138. return PixelFormat::RG8S;
  139. case Tegra::RenderTargetFormat::R16_FLOAT:
  140. return PixelFormat::R16F;
  141. case Tegra::RenderTargetFormat::R16_UNORM:
  142. return PixelFormat::R16U;
  143. case Tegra::RenderTargetFormat::R16_SNORM:
  144. return PixelFormat::R16S;
  145. case Tegra::RenderTargetFormat::R16_UINT:
  146. return PixelFormat::R16UI;
  147. case Tegra::RenderTargetFormat::R16_SINT:
  148. return PixelFormat::R16I;
  149. case Tegra::RenderTargetFormat::R32_FLOAT:
  150. return PixelFormat::R32F;
  151. case Tegra::RenderTargetFormat::R32_SINT:
  152. return PixelFormat::R32I;
  153. case Tegra::RenderTargetFormat::R32_UINT:
  154. return PixelFormat::R32UI;
  155. case Tegra::RenderTargetFormat::RG32_UINT:
  156. return PixelFormat::RG32UI;
  157. case Tegra::RenderTargetFormat::RGBX16_FLOAT:
  158. return PixelFormat::RGBX16F;
  159. default:
  160. LOG_CRITICAL(HW_GPU, "Unimplemented format={}", static_cast<u32>(format));
  161. UNREACHABLE();
  162. return PixelFormat::RGBA8_SRGB;
  163. }
  164. }
  165. PixelFormat PixelFormatFromGPUPixelFormat(Tegra::FramebufferConfig::PixelFormat format) {
  166. switch (format) {
  167. case Tegra::FramebufferConfig::PixelFormat::ABGR8:
  168. return PixelFormat::ABGR8U;
  169. case Tegra::FramebufferConfig::PixelFormat::RGB565:
  170. return PixelFormat::B5G6R5U;
  171. case Tegra::FramebufferConfig::PixelFormat::BGRA8:
  172. return PixelFormat::BGRA8;
  173. default:
  174. UNIMPLEMENTED_MSG("Unimplemented format={}", static_cast<u32>(format));
  175. return PixelFormat::ABGR8U;
  176. }
  177. }
  178. SurfaceType GetFormatType(PixelFormat pixel_format) {
  179. if (static_cast<std::size_t>(pixel_format) <
  180. static_cast<std::size_t>(PixelFormat::MaxColorFormat)) {
  181. return SurfaceType::ColorTexture;
  182. }
  183. if (static_cast<std::size_t>(pixel_format) <
  184. static_cast<std::size_t>(PixelFormat::MaxDepthFormat)) {
  185. return SurfaceType::Depth;
  186. }
  187. if (static_cast<std::size_t>(pixel_format) <
  188. static_cast<std::size_t>(PixelFormat::MaxDepthStencilFormat)) {
  189. return SurfaceType::DepthStencil;
  190. }
  191. // TODO(Subv): Implement the other formats
  192. ASSERT(false);
  193. return SurfaceType::Invalid;
  194. }
  195. bool IsPixelFormatASTC(PixelFormat format) {
  196. switch (format) {
  197. case PixelFormat::ASTC_2D_4X4:
  198. case PixelFormat::ASTC_2D_5X4:
  199. case PixelFormat::ASTC_2D_5X5:
  200. case PixelFormat::ASTC_2D_8X8:
  201. case PixelFormat::ASTC_2D_8X5:
  202. case PixelFormat::ASTC_2D_4X4_SRGB:
  203. case PixelFormat::ASTC_2D_5X4_SRGB:
  204. case PixelFormat::ASTC_2D_5X5_SRGB:
  205. case PixelFormat::ASTC_2D_8X8_SRGB:
  206. case PixelFormat::ASTC_2D_8X5_SRGB:
  207. case PixelFormat::ASTC_2D_10X8:
  208. case PixelFormat::ASTC_2D_10X8_SRGB:
  209. case PixelFormat::ASTC_2D_6X6:
  210. case PixelFormat::ASTC_2D_6X6_SRGB:
  211. case PixelFormat::ASTC_2D_10X10:
  212. case PixelFormat::ASTC_2D_10X10_SRGB:
  213. case PixelFormat::ASTC_2D_12X12:
  214. case PixelFormat::ASTC_2D_12X12_SRGB:
  215. case PixelFormat::ASTC_2D_8X6:
  216. case PixelFormat::ASTC_2D_8X6_SRGB:
  217. case PixelFormat::ASTC_2D_6X5:
  218. case PixelFormat::ASTC_2D_6X5_SRGB:
  219. return true;
  220. default:
  221. return false;
  222. }
  223. }
  224. bool IsPixelFormatSRGB(PixelFormat format) {
  225. switch (format) {
  226. case PixelFormat::RGBA8_SRGB:
  227. case PixelFormat::BGRA8_SRGB:
  228. case PixelFormat::DXT1_SRGB:
  229. case PixelFormat::DXT23_SRGB:
  230. case PixelFormat::DXT45_SRGB:
  231. case PixelFormat::BC7U_SRGB:
  232. case PixelFormat::ASTC_2D_4X4_SRGB:
  233. case PixelFormat::ASTC_2D_8X8_SRGB:
  234. case PixelFormat::ASTC_2D_8X5_SRGB:
  235. case PixelFormat::ASTC_2D_5X4_SRGB:
  236. case PixelFormat::ASTC_2D_5X5_SRGB:
  237. case PixelFormat::ASTC_2D_10X8_SRGB:
  238. case PixelFormat::ASTC_2D_6X6_SRGB:
  239. case PixelFormat::ASTC_2D_10X10_SRGB:
  240. case PixelFormat::ASTC_2D_12X12_SRGB:
  241. case PixelFormat::ASTC_2D_8X6_SRGB:
  242. case PixelFormat::ASTC_2D_6X5_SRGB:
  243. return true;
  244. default:
  245. return false;
  246. }
  247. }
  248. std::pair<u32, u32> GetASTCBlockSize(PixelFormat format) {
  249. return {GetDefaultBlockWidth(format), GetDefaultBlockHeight(format)};
  250. }
  251. bool IsFormatBCn(PixelFormat format) {
  252. switch (format) {
  253. case PixelFormat::DXT1:
  254. case PixelFormat::DXT23:
  255. case PixelFormat::DXT45:
  256. case PixelFormat::DXN1:
  257. case PixelFormat::DXN2SNORM:
  258. case PixelFormat::DXN2UNORM:
  259. case PixelFormat::BC7U:
  260. case PixelFormat::BC6H_UF16:
  261. case PixelFormat::BC6H_SF16:
  262. case PixelFormat::DXT1_SRGB:
  263. case PixelFormat::DXT23_SRGB:
  264. case PixelFormat::DXT45_SRGB:
  265. case PixelFormat::BC7U_SRGB:
  266. return true;
  267. default:
  268. return false;
  269. }
  270. }
  271. } // namespace VideoCore::Surface