surface.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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={}", 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={}", 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={}", target);
  65. UNREACHABLE();
  66. return false;
  67. }
  68. }
  69. PixelFormat PixelFormatFromDepthFormat(Tegra::DepthFormat format) {
  70. switch (format) {
  71. case Tegra::DepthFormat::S8_UINT_Z24_UNORM:
  72. return PixelFormat::S8_UINT_D24_UNORM;
  73. case Tegra::DepthFormat::D24S8_UNORM:
  74. return PixelFormat::D24_UNORM_S8_UINT;
  75. case Tegra::DepthFormat::D32_FLOAT:
  76. return PixelFormat::D32_FLOAT;
  77. case Tegra::DepthFormat::D16_UNORM:
  78. return PixelFormat::D16_UNORM;
  79. case Tegra::DepthFormat::D32_FLOAT_S8X24_UINT:
  80. return PixelFormat::D32_FLOAT_S8_UINT;
  81. default:
  82. UNIMPLEMENTED_MSG("Unimplemented format={}", format);
  83. return PixelFormat::S8_UINT_D24_UNORM;
  84. }
  85. }
  86. PixelFormat PixelFormatFromRenderTargetFormat(Tegra::RenderTargetFormat format) {
  87. switch (format) {
  88. case Tegra::RenderTargetFormat::R32B32G32A32_FLOAT:
  89. return PixelFormat::R32G32B32A32_FLOAT;
  90. case Tegra::RenderTargetFormat::R32G32B32A32_SINT:
  91. return PixelFormat::R32G32B32A32_SINT;
  92. case Tegra::RenderTargetFormat::R32G32B32A32_UINT:
  93. return PixelFormat::R32G32B32A32_UINT;
  94. case Tegra::RenderTargetFormat::R16G16B16A16_UNORM:
  95. return PixelFormat::R16G16B16A16_UNORM;
  96. case Tegra::RenderTargetFormat::R16G16B16A16_SNORM:
  97. return PixelFormat::R16G16B16A16_SNORM;
  98. case Tegra::RenderTargetFormat::R16G16B16A16_SINT:
  99. return PixelFormat::R16G16B16A16_SINT;
  100. case Tegra::RenderTargetFormat::R16G16B16A16_UINT:
  101. return PixelFormat::R16G16B16A16_UINT;
  102. case Tegra::RenderTargetFormat::R16G16B16A16_FLOAT:
  103. return PixelFormat::R16G16B16A16_FLOAT;
  104. case Tegra::RenderTargetFormat::R32G32_FLOAT:
  105. return PixelFormat::R32G32_FLOAT;
  106. case Tegra::RenderTargetFormat::R32G32_SINT:
  107. return PixelFormat::R32G32_SINT;
  108. case Tegra::RenderTargetFormat::R32G32_UINT:
  109. return PixelFormat::R32G32_UINT;
  110. case Tegra::RenderTargetFormat::R16G16B16X16_FLOAT:
  111. return PixelFormat::R16G16B16X16_FLOAT;
  112. case Tegra::RenderTargetFormat::B8G8R8A8_UNORM:
  113. return PixelFormat::B8G8R8A8_UNORM;
  114. case Tegra::RenderTargetFormat::B8G8R8A8_SRGB:
  115. return PixelFormat::B8G8R8A8_SRGB;
  116. case Tegra::RenderTargetFormat::A2B10G10R10_UNORM:
  117. return PixelFormat::A2B10G10R10_UNORM;
  118. case Tegra::RenderTargetFormat::A2B10G10R10_UINT:
  119. return PixelFormat::A2B10G10R10_UINT;
  120. case Tegra::RenderTargetFormat::A8B8G8R8_UNORM:
  121. return PixelFormat::A8B8G8R8_UNORM;
  122. case Tegra::RenderTargetFormat::A8B8G8R8_SRGB:
  123. return PixelFormat::A8B8G8R8_SRGB;
  124. case Tegra::RenderTargetFormat::A8B8G8R8_SNORM:
  125. return PixelFormat::A8B8G8R8_SNORM;
  126. case Tegra::RenderTargetFormat::A8B8G8R8_SINT:
  127. return PixelFormat::A8B8G8R8_SINT;
  128. case Tegra::RenderTargetFormat::A8B8G8R8_UINT:
  129. return PixelFormat::A8B8G8R8_UINT;
  130. case Tegra::RenderTargetFormat::R16G16_UNORM:
  131. return PixelFormat::R16G16_UNORM;
  132. case Tegra::RenderTargetFormat::R16G16_SNORM:
  133. return PixelFormat::R16G16_SNORM;
  134. case Tegra::RenderTargetFormat::R16G16_SINT:
  135. return PixelFormat::R16G16_SINT;
  136. case Tegra::RenderTargetFormat::R16G16_UINT:
  137. return PixelFormat::R16G16_UINT;
  138. case Tegra::RenderTargetFormat::R16G16_FLOAT:
  139. return PixelFormat::R16G16_FLOAT;
  140. case Tegra::RenderTargetFormat::B10G11R11_FLOAT:
  141. return PixelFormat::B10G11R11_FLOAT;
  142. case Tegra::RenderTargetFormat::R32_SINT:
  143. return PixelFormat::R32_SINT;
  144. case Tegra::RenderTargetFormat::R32_UINT:
  145. return PixelFormat::R32_UINT;
  146. case Tegra::RenderTargetFormat::R32_FLOAT:
  147. return PixelFormat::R32_FLOAT;
  148. case Tegra::RenderTargetFormat::R5G6B5_UNORM:
  149. return PixelFormat::R5G6B5_UNORM;
  150. case Tegra::RenderTargetFormat::A1R5G5B5_UNORM:
  151. return PixelFormat::A1R5G5B5_UNORM;
  152. case Tegra::RenderTargetFormat::R8G8_UNORM:
  153. return PixelFormat::R8G8_UNORM;
  154. case Tegra::RenderTargetFormat::R8G8_SNORM:
  155. return PixelFormat::R8G8_SNORM;
  156. case Tegra::RenderTargetFormat::R8G8_SINT:
  157. return PixelFormat::R8G8_SINT;
  158. case Tegra::RenderTargetFormat::R8G8_UINT:
  159. return PixelFormat::R8G8_UINT;
  160. case Tegra::RenderTargetFormat::R16_UNORM:
  161. return PixelFormat::R16_UNORM;
  162. case Tegra::RenderTargetFormat::R16_SNORM:
  163. return PixelFormat::R16_SNORM;
  164. case Tegra::RenderTargetFormat::R16_SINT:
  165. return PixelFormat::R16_SINT;
  166. case Tegra::RenderTargetFormat::R16_UINT:
  167. return PixelFormat::R16_UINT;
  168. case Tegra::RenderTargetFormat::R16_FLOAT:
  169. return PixelFormat::R16_FLOAT;
  170. case Tegra::RenderTargetFormat::R8_UNORM:
  171. return PixelFormat::R8_UNORM;
  172. case Tegra::RenderTargetFormat::R8_SNORM:
  173. return PixelFormat::R8_SNORM;
  174. case Tegra::RenderTargetFormat::R8_SINT:
  175. return PixelFormat::R8_SINT;
  176. case Tegra::RenderTargetFormat::R8_UINT:
  177. return PixelFormat::R8_UINT;
  178. default:
  179. UNIMPLEMENTED_MSG("Unimplemented format={}", format);
  180. return PixelFormat::A8B8G8R8_UNORM;
  181. }
  182. }
  183. PixelFormat PixelFormatFromGPUPixelFormat(Tegra::FramebufferConfig::PixelFormat format) {
  184. switch (format) {
  185. case Tegra::FramebufferConfig::PixelFormat::A8B8G8R8_UNORM:
  186. return PixelFormat::A8B8G8R8_UNORM;
  187. case Tegra::FramebufferConfig::PixelFormat::RGB565_UNORM:
  188. return PixelFormat::R5G6B5_UNORM;
  189. case Tegra::FramebufferConfig::PixelFormat::B8G8R8A8_UNORM:
  190. return PixelFormat::B8G8R8A8_UNORM;
  191. default:
  192. UNIMPLEMENTED_MSG("Unimplemented format={}", format);
  193. return PixelFormat::A8B8G8R8_UNORM;
  194. }
  195. }
  196. SurfaceType GetFormatType(PixelFormat pixel_format) {
  197. if (static_cast<std::size_t>(pixel_format) <
  198. static_cast<std::size_t>(PixelFormat::MaxColorFormat)) {
  199. return SurfaceType::ColorTexture;
  200. }
  201. if (static_cast<std::size_t>(pixel_format) <
  202. static_cast<std::size_t>(PixelFormat::MaxDepthFormat)) {
  203. return SurfaceType::Depth;
  204. }
  205. if (static_cast<std::size_t>(pixel_format) <
  206. static_cast<std::size_t>(PixelFormat::MaxDepthStencilFormat)) {
  207. return SurfaceType::DepthStencil;
  208. }
  209. // TODO(Subv): Implement the other formats
  210. ASSERT(false);
  211. return SurfaceType::Invalid;
  212. }
  213. bool IsPixelFormatASTC(PixelFormat format) {
  214. switch (format) {
  215. case PixelFormat::ASTC_2D_4X4_UNORM:
  216. case PixelFormat::ASTC_2D_5X4_UNORM:
  217. case PixelFormat::ASTC_2D_5X5_UNORM:
  218. case PixelFormat::ASTC_2D_8X8_UNORM:
  219. case PixelFormat::ASTC_2D_8X5_UNORM:
  220. case PixelFormat::ASTC_2D_4X4_SRGB:
  221. case PixelFormat::ASTC_2D_5X4_SRGB:
  222. case PixelFormat::ASTC_2D_5X5_SRGB:
  223. case PixelFormat::ASTC_2D_8X8_SRGB:
  224. case PixelFormat::ASTC_2D_8X5_SRGB:
  225. case PixelFormat::ASTC_2D_10X8_UNORM:
  226. case PixelFormat::ASTC_2D_10X8_SRGB:
  227. case PixelFormat::ASTC_2D_6X6_UNORM:
  228. case PixelFormat::ASTC_2D_6X6_SRGB:
  229. case PixelFormat::ASTC_2D_10X10_UNORM:
  230. case PixelFormat::ASTC_2D_10X10_SRGB:
  231. case PixelFormat::ASTC_2D_12X12_UNORM:
  232. case PixelFormat::ASTC_2D_12X12_SRGB:
  233. case PixelFormat::ASTC_2D_8X6_UNORM:
  234. case PixelFormat::ASTC_2D_8X6_SRGB:
  235. case PixelFormat::ASTC_2D_6X5_UNORM:
  236. case PixelFormat::ASTC_2D_6X5_SRGB:
  237. return true;
  238. default:
  239. return false;
  240. }
  241. }
  242. bool IsPixelFormatSRGB(PixelFormat format) {
  243. switch (format) {
  244. case PixelFormat::A8B8G8R8_SRGB:
  245. case PixelFormat::B8G8R8A8_SRGB:
  246. case PixelFormat::BC1_RGBA_SRGB:
  247. case PixelFormat::BC2_SRGB:
  248. case PixelFormat::BC3_SRGB:
  249. case PixelFormat::BC7_SRGB:
  250. case PixelFormat::ASTC_2D_4X4_SRGB:
  251. case PixelFormat::ASTC_2D_8X8_SRGB:
  252. case PixelFormat::ASTC_2D_8X5_SRGB:
  253. case PixelFormat::ASTC_2D_5X4_SRGB:
  254. case PixelFormat::ASTC_2D_5X5_SRGB:
  255. case PixelFormat::ASTC_2D_10X8_SRGB:
  256. case PixelFormat::ASTC_2D_6X6_SRGB:
  257. case PixelFormat::ASTC_2D_10X10_SRGB:
  258. case PixelFormat::ASTC_2D_12X12_SRGB:
  259. case PixelFormat::ASTC_2D_8X6_SRGB:
  260. case PixelFormat::ASTC_2D_6X5_SRGB:
  261. return true;
  262. default:
  263. return false;
  264. }
  265. }
  266. std::pair<u32, u32> GetASTCBlockSize(PixelFormat format) {
  267. return {GetDefaultBlockWidth(format), GetDefaultBlockHeight(format)};
  268. }
  269. } // namespace VideoCore::Surface