surface.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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_SNORM:
  108. return PixelFormat::RGBA16S;
  109. case Tegra::RenderTargetFormat::RGBA16_UINT:
  110. return PixelFormat::RGBA16UI;
  111. case Tegra::RenderTargetFormat::RGBA32_FLOAT:
  112. return PixelFormat::RGBA32F;
  113. case Tegra::RenderTargetFormat::RG32_FLOAT:
  114. return PixelFormat::RG32F;
  115. case Tegra::RenderTargetFormat::R11G11B10_FLOAT:
  116. return PixelFormat::R11FG11FB10F;
  117. case Tegra::RenderTargetFormat::B5G6R5_UNORM:
  118. return PixelFormat::B5G6R5U;
  119. case Tegra::RenderTargetFormat::BGR5A1_UNORM:
  120. return PixelFormat::A1B5G5R5U;
  121. case Tegra::RenderTargetFormat::RGBA32_UINT:
  122. return PixelFormat::RGBA32UI;
  123. case Tegra::RenderTargetFormat::R8_UNORM:
  124. return PixelFormat::R8U;
  125. case Tegra::RenderTargetFormat::R8_UINT:
  126. return PixelFormat::R8UI;
  127. case Tegra::RenderTargetFormat::RG16_FLOAT:
  128. return PixelFormat::RG16F;
  129. case Tegra::RenderTargetFormat::RG16_UINT:
  130. return PixelFormat::RG16UI;
  131. case Tegra::RenderTargetFormat::RG16_SINT:
  132. return PixelFormat::RG16I;
  133. case Tegra::RenderTargetFormat::RG16_UNORM:
  134. return PixelFormat::RG16;
  135. case Tegra::RenderTargetFormat::RG16_SNORM:
  136. return PixelFormat::RG16S;
  137. case Tegra::RenderTargetFormat::RG8_UNORM:
  138. return PixelFormat::RG8U;
  139. case Tegra::RenderTargetFormat::RG8_SNORM:
  140. return PixelFormat::RG8S;
  141. case Tegra::RenderTargetFormat::RG8_UINT:
  142. return PixelFormat::RG8UI;
  143. case Tegra::RenderTargetFormat::R16_FLOAT:
  144. return PixelFormat::R16F;
  145. case Tegra::RenderTargetFormat::R16_UNORM:
  146. return PixelFormat::R16U;
  147. case Tegra::RenderTargetFormat::R16_SNORM:
  148. return PixelFormat::R16S;
  149. case Tegra::RenderTargetFormat::R16_UINT:
  150. return PixelFormat::R16UI;
  151. case Tegra::RenderTargetFormat::R16_SINT:
  152. return PixelFormat::R16I;
  153. case Tegra::RenderTargetFormat::R32_FLOAT:
  154. return PixelFormat::R32F;
  155. case Tegra::RenderTargetFormat::R32_SINT:
  156. return PixelFormat::R32I;
  157. case Tegra::RenderTargetFormat::R32_UINT:
  158. return PixelFormat::R32UI;
  159. case Tegra::RenderTargetFormat::RG32_UINT:
  160. return PixelFormat::RG32UI;
  161. case Tegra::RenderTargetFormat::RGBX16_FLOAT:
  162. return PixelFormat::RGBX16F;
  163. default:
  164. LOG_CRITICAL(HW_GPU, "Unimplemented format={}", static_cast<u32>(format));
  165. UNREACHABLE();
  166. return PixelFormat::RGBA8_SRGB;
  167. }
  168. }
  169. PixelFormat PixelFormatFromGPUPixelFormat(Tegra::FramebufferConfig::PixelFormat format) {
  170. switch (format) {
  171. case Tegra::FramebufferConfig::PixelFormat::ABGR8:
  172. return PixelFormat::ABGR8U;
  173. case Tegra::FramebufferConfig::PixelFormat::RGB565:
  174. return PixelFormat::B5G6R5U;
  175. case Tegra::FramebufferConfig::PixelFormat::BGRA8:
  176. return PixelFormat::BGRA8;
  177. default:
  178. UNIMPLEMENTED_MSG("Unimplemented format={}", static_cast<u32>(format));
  179. return PixelFormat::ABGR8U;
  180. }
  181. }
  182. SurfaceType GetFormatType(PixelFormat pixel_format) {
  183. if (static_cast<std::size_t>(pixel_format) <
  184. static_cast<std::size_t>(PixelFormat::MaxColorFormat)) {
  185. return SurfaceType::ColorTexture;
  186. }
  187. if (static_cast<std::size_t>(pixel_format) <
  188. static_cast<std::size_t>(PixelFormat::MaxDepthFormat)) {
  189. return SurfaceType::Depth;
  190. }
  191. if (static_cast<std::size_t>(pixel_format) <
  192. static_cast<std::size_t>(PixelFormat::MaxDepthStencilFormat)) {
  193. return SurfaceType::DepthStencil;
  194. }
  195. // TODO(Subv): Implement the other formats
  196. ASSERT(false);
  197. return SurfaceType::Invalid;
  198. }
  199. bool IsPixelFormatASTC(PixelFormat format) {
  200. switch (format) {
  201. case PixelFormat::ASTC_2D_4X4:
  202. case PixelFormat::ASTC_2D_5X4:
  203. case PixelFormat::ASTC_2D_5X5:
  204. case PixelFormat::ASTC_2D_8X8:
  205. case PixelFormat::ASTC_2D_8X5:
  206. case PixelFormat::ASTC_2D_4X4_SRGB:
  207. case PixelFormat::ASTC_2D_5X4_SRGB:
  208. case PixelFormat::ASTC_2D_5X5_SRGB:
  209. case PixelFormat::ASTC_2D_8X8_SRGB:
  210. case PixelFormat::ASTC_2D_8X5_SRGB:
  211. case PixelFormat::ASTC_2D_10X8:
  212. case PixelFormat::ASTC_2D_10X8_SRGB:
  213. case PixelFormat::ASTC_2D_6X6:
  214. case PixelFormat::ASTC_2D_6X6_SRGB:
  215. case PixelFormat::ASTC_2D_10X10:
  216. case PixelFormat::ASTC_2D_10X10_SRGB:
  217. case PixelFormat::ASTC_2D_12X12:
  218. case PixelFormat::ASTC_2D_12X12_SRGB:
  219. case PixelFormat::ASTC_2D_8X6:
  220. case PixelFormat::ASTC_2D_8X6_SRGB:
  221. case PixelFormat::ASTC_2D_6X5:
  222. case PixelFormat::ASTC_2D_6X5_SRGB:
  223. return true;
  224. default:
  225. return false;
  226. }
  227. }
  228. bool IsPixelFormatSRGB(PixelFormat format) {
  229. switch (format) {
  230. case PixelFormat::RGBA8_SRGB:
  231. case PixelFormat::BGRA8_SRGB:
  232. case PixelFormat::DXT1_SRGB:
  233. case PixelFormat::DXT23_SRGB:
  234. case PixelFormat::DXT45_SRGB:
  235. case PixelFormat::BC7U_SRGB:
  236. case PixelFormat::ASTC_2D_4X4_SRGB:
  237. case PixelFormat::ASTC_2D_8X8_SRGB:
  238. case PixelFormat::ASTC_2D_8X5_SRGB:
  239. case PixelFormat::ASTC_2D_5X4_SRGB:
  240. case PixelFormat::ASTC_2D_5X5_SRGB:
  241. case PixelFormat::ASTC_2D_10X8_SRGB:
  242. case PixelFormat::ASTC_2D_6X6_SRGB:
  243. case PixelFormat::ASTC_2D_10X10_SRGB:
  244. case PixelFormat::ASTC_2D_12X12_SRGB:
  245. case PixelFormat::ASTC_2D_8X6_SRGB:
  246. case PixelFormat::ASTC_2D_6X5_SRGB:
  247. return true;
  248. default:
  249. return false;
  250. }
  251. }
  252. std::pair<u32, u32> GetASTCBlockSize(PixelFormat format) {
  253. return {GetDefaultBlockWidth(format), GetDefaultBlockHeight(format)};
  254. }
  255. bool IsFormatBCn(PixelFormat format) {
  256. switch (format) {
  257. case PixelFormat::DXT1:
  258. case PixelFormat::DXT23:
  259. case PixelFormat::DXT45:
  260. case PixelFormat::DXN1:
  261. case PixelFormat::DXN2SNORM:
  262. case PixelFormat::DXN2UNORM:
  263. case PixelFormat::BC7U:
  264. case PixelFormat::BC6H_UF16:
  265. case PixelFormat::BC6H_SF16:
  266. case PixelFormat::DXT1_SRGB:
  267. case PixelFormat::DXT23_SRGB:
  268. case PixelFormat::DXT45_SRGB:
  269. case PixelFormat::BC7U_SRGB:
  270. return true;
  271. default:
  272. return false;
  273. }
  274. }
  275. } // namespace VideoCore::Surface