compatible_formats.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. // SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <array>
  4. #include <cstddef>
  5. #include "common/common_types.h"
  6. #include "video_core/compatible_formats.h"
  7. #include "video_core/surface.h"
  8. namespace VideoCore::Surface {
  9. namespace {
  10. using Table = std::array<std::array<u64, 2>, MaxPixelFormat>;
  11. // Compatibility table taken from Table 3.X.2 in:
  12. // https://www.khronos.org/registry/OpenGL/extensions/ARB/ARB_texture_view.txt
  13. constexpr std::array VIEW_CLASS_128_BITS{
  14. PixelFormat::R32G32B32A32_FLOAT,
  15. PixelFormat::R32G32B32A32_UINT,
  16. PixelFormat::R32G32B32A32_SINT,
  17. };
  18. constexpr std::array VIEW_CLASS_96_BITS{
  19. PixelFormat::R32G32B32_FLOAT,
  20. };
  21. // Missing formats:
  22. // PixelFormat::RGB32UI,
  23. // PixelFormat::RGB32I,
  24. constexpr std::array VIEW_CLASS_64_BITS{
  25. PixelFormat::R32G32_FLOAT, PixelFormat::R32G32_UINT,
  26. PixelFormat::R32G32_SINT, PixelFormat::R16G16B16A16_FLOAT,
  27. PixelFormat::R16G16B16A16_UNORM, PixelFormat::R16G16B16A16_SNORM,
  28. PixelFormat::R16G16B16A16_UINT, PixelFormat::R16G16B16A16_SINT,
  29. };
  30. // TODO: How should we handle 48 bits?
  31. constexpr std::array VIEW_CLASS_32_BITS{
  32. PixelFormat::R16G16_FLOAT, PixelFormat::B10G11R11_FLOAT, PixelFormat::R32_FLOAT,
  33. PixelFormat::A2B10G10R10_UNORM, PixelFormat::R16G16_UINT, PixelFormat::R32_UINT,
  34. PixelFormat::R16G16_SINT, PixelFormat::R32_SINT, PixelFormat::A8B8G8R8_UNORM,
  35. PixelFormat::R16G16_UNORM, PixelFormat::A8B8G8R8_SNORM, PixelFormat::R16G16_SNORM,
  36. PixelFormat::A8B8G8R8_SRGB, PixelFormat::E5B9G9R9_FLOAT, PixelFormat::B8G8R8A8_UNORM,
  37. PixelFormat::B8G8R8A8_SRGB, PixelFormat::A8B8G8R8_UINT, PixelFormat::A8B8G8R8_SINT,
  38. PixelFormat::A2B10G10R10_UINT,
  39. };
  40. constexpr std::array VIEW_CLASS_32_BITS_NO_BGR{
  41. PixelFormat::R16G16_FLOAT, PixelFormat::B10G11R11_FLOAT, PixelFormat::R32_FLOAT,
  42. PixelFormat::A2B10G10R10_UNORM, PixelFormat::R16G16_UINT, PixelFormat::R32_UINT,
  43. PixelFormat::R16G16_SINT, PixelFormat::R32_SINT, PixelFormat::A8B8G8R8_UNORM,
  44. PixelFormat::R16G16_UNORM, PixelFormat::A8B8G8R8_SNORM, PixelFormat::R16G16_SNORM,
  45. PixelFormat::A8B8G8R8_SRGB, PixelFormat::E5B9G9R9_FLOAT, PixelFormat::A8B8G8R8_UINT,
  46. PixelFormat::A8B8G8R8_SINT, PixelFormat::A2B10G10R10_UINT,
  47. };
  48. // TODO: How should we handle 24 bits?
  49. constexpr std::array VIEW_CLASS_16_BITS{
  50. PixelFormat::R16_FLOAT, PixelFormat::R8G8_UINT, PixelFormat::R16_UINT,
  51. PixelFormat::R16_SINT, PixelFormat::R8G8_UNORM, PixelFormat::R16_UNORM,
  52. PixelFormat::R8G8_SNORM, PixelFormat::R16_SNORM, PixelFormat::R8G8_SINT,
  53. };
  54. constexpr std::array VIEW_CLASS_8_BITS{
  55. PixelFormat::R8_UINT,
  56. PixelFormat::R8_UNORM,
  57. PixelFormat::R8_SINT,
  58. PixelFormat::R8_SNORM,
  59. };
  60. constexpr std::array VIEW_CLASS_RGTC1_RED{
  61. PixelFormat::BC4_UNORM,
  62. PixelFormat::BC4_SNORM,
  63. };
  64. constexpr std::array VIEW_CLASS_RGTC2_RG{
  65. PixelFormat::BC5_UNORM,
  66. PixelFormat::BC5_SNORM,
  67. };
  68. constexpr std::array VIEW_CLASS_BPTC_UNORM{
  69. PixelFormat::BC7_UNORM,
  70. PixelFormat::BC7_SRGB,
  71. };
  72. constexpr std::array VIEW_CLASS_BPTC_FLOAT{
  73. PixelFormat::BC6H_SFLOAT,
  74. PixelFormat::BC6H_UFLOAT,
  75. };
  76. constexpr std::array VIEW_CLASS_ASTC_4x4_RGBA{
  77. PixelFormat::ASTC_2D_4X4_UNORM,
  78. PixelFormat::ASTC_2D_4X4_SRGB,
  79. };
  80. constexpr std::array VIEW_CLASS_ASTC_5x4_RGBA{
  81. PixelFormat::ASTC_2D_5X4_UNORM,
  82. PixelFormat::ASTC_2D_5X4_SRGB,
  83. };
  84. constexpr std::array VIEW_CLASS_ASTC_5x5_RGBA{
  85. PixelFormat::ASTC_2D_5X5_UNORM,
  86. PixelFormat::ASTC_2D_5X5_SRGB,
  87. };
  88. constexpr std::array VIEW_CLASS_ASTC_6x5_RGBA{
  89. PixelFormat::ASTC_2D_6X5_UNORM,
  90. PixelFormat::ASTC_2D_6X5_SRGB,
  91. };
  92. constexpr std::array VIEW_CLASS_ASTC_6x6_RGBA{
  93. PixelFormat::ASTC_2D_6X6_UNORM,
  94. PixelFormat::ASTC_2D_6X6_SRGB,
  95. };
  96. constexpr std::array VIEW_CLASS_ASTC_8x5_RGBA{
  97. PixelFormat::ASTC_2D_8X5_UNORM,
  98. PixelFormat::ASTC_2D_8X5_SRGB,
  99. };
  100. constexpr std::array VIEW_CLASS_ASTC_8x8_RGBA{
  101. PixelFormat::ASTC_2D_8X8_UNORM,
  102. PixelFormat::ASTC_2D_8X8_SRGB,
  103. };
  104. constexpr std::array VIEW_CLASS_ASTC_10x5_RGBA{
  105. PixelFormat::ASTC_2D_10X5_UNORM,
  106. PixelFormat::ASTC_2D_10X5_SRGB,
  107. };
  108. constexpr std::array VIEW_CLASS_ASTC_10x6_RGBA{
  109. PixelFormat::ASTC_2D_10X6_UNORM,
  110. PixelFormat::ASTC_2D_10X6_SRGB,
  111. };
  112. constexpr std::array VIEW_CLASS_ASTC_10x8_RGBA{
  113. PixelFormat::ASTC_2D_10X8_UNORM,
  114. PixelFormat::ASTC_2D_10X8_SRGB,
  115. };
  116. constexpr std::array VIEW_CLASS_ASTC_10x10_RGBA{
  117. PixelFormat::ASTC_2D_10X10_UNORM,
  118. PixelFormat::ASTC_2D_10X10_SRGB,
  119. };
  120. constexpr std::array VIEW_CLASS_ASTC_12x10_RGBA{
  121. PixelFormat::ASTC_2D_12X10_UNORM,
  122. PixelFormat::ASTC_2D_12X10_SRGB,
  123. };
  124. constexpr std::array VIEW_CLASS_ASTC_12x12_RGBA{
  125. PixelFormat::ASTC_2D_12X12_UNORM,
  126. PixelFormat::ASTC_2D_12X12_SRGB,
  127. };
  128. // Compatibility table taken from Table 4.X.1 in:
  129. // https://www.khronos.org/registry/OpenGL/extensions/ARB/ARB_copy_image.txt
  130. constexpr std::array COPY_CLASS_128_BITS{
  131. PixelFormat::R32G32B32A32_UINT, PixelFormat::R32G32B32A32_FLOAT, PixelFormat::R32G32B32A32_SINT,
  132. PixelFormat::BC2_UNORM, PixelFormat::BC2_SRGB, PixelFormat::BC3_UNORM,
  133. PixelFormat::BC3_SRGB, PixelFormat::BC5_UNORM, PixelFormat::BC5_SNORM,
  134. PixelFormat::BC7_UNORM, PixelFormat::BC7_SRGB, PixelFormat::BC6H_SFLOAT,
  135. PixelFormat::BC6H_UFLOAT,
  136. };
  137. // Missing formats:
  138. // PixelFormat::RGBA32I
  139. // COMPRESSED_RG_RGTC2
  140. constexpr std::array COPY_CLASS_64_BITS{
  141. PixelFormat::R16G16B16A16_FLOAT, PixelFormat::R16G16B16A16_UINT,
  142. PixelFormat::R16G16B16A16_UNORM, PixelFormat::R16G16B16A16_SNORM,
  143. PixelFormat::R16G16B16A16_SINT, PixelFormat::R32G32_UINT,
  144. PixelFormat::R32G32_FLOAT, PixelFormat::R32G32_SINT,
  145. PixelFormat::BC1_RGBA_UNORM, PixelFormat::BC1_RGBA_SRGB,
  146. };
  147. // Missing formats:
  148. // COMPRESSED_RGB_S3TC_DXT1_EXT
  149. // COMPRESSED_SRGB_S3TC_DXT1_EXT
  150. // COMPRESSED_RGBA_S3TC_DXT1_EXT
  151. // COMPRESSED_SIGNED_RED_RGTC1
  152. constexpr void Enable(Table& table, size_t format_a, size_t format_b) {
  153. table[format_a][format_b / 64] |= u64(1) << (format_b % 64);
  154. table[format_b][format_a / 64] |= u64(1) << (format_a % 64);
  155. }
  156. constexpr void Enable(Table& table, PixelFormat format_a, PixelFormat format_b) {
  157. Enable(table, static_cast<size_t>(format_a), static_cast<size_t>(format_b));
  158. }
  159. template <typename Range>
  160. constexpr void EnableRange(Table& table, const Range& range) {
  161. for (auto it_a = range.begin(); it_a != range.end(); ++it_a) {
  162. for (auto it_b = it_a; it_b != range.end(); ++it_b) {
  163. Enable(table, *it_a, *it_b);
  164. }
  165. }
  166. }
  167. constexpr bool IsSupported(const Table& table, PixelFormat format_a, PixelFormat format_b) {
  168. const size_t a = static_cast<size_t>(format_a);
  169. const size_t b = static_cast<size_t>(format_b);
  170. return ((table[a][b / 64] >> (b % 64)) & 1) != 0;
  171. }
  172. constexpr Table MakeViewTable() {
  173. Table view{};
  174. for (size_t i = 0; i < MaxPixelFormat; ++i) {
  175. // Identity is allowed
  176. Enable(view, i, i);
  177. }
  178. EnableRange(view, VIEW_CLASS_128_BITS);
  179. EnableRange(view, VIEW_CLASS_96_BITS);
  180. EnableRange(view, VIEW_CLASS_64_BITS);
  181. EnableRange(view, VIEW_CLASS_16_BITS);
  182. EnableRange(view, VIEW_CLASS_8_BITS);
  183. EnableRange(view, VIEW_CLASS_RGTC1_RED);
  184. EnableRange(view, VIEW_CLASS_RGTC2_RG);
  185. EnableRange(view, VIEW_CLASS_BPTC_UNORM);
  186. EnableRange(view, VIEW_CLASS_BPTC_FLOAT);
  187. EnableRange(view, VIEW_CLASS_ASTC_4x4_RGBA);
  188. EnableRange(view, VIEW_CLASS_ASTC_5x4_RGBA);
  189. EnableRange(view, VIEW_CLASS_ASTC_5x5_RGBA);
  190. EnableRange(view, VIEW_CLASS_ASTC_6x5_RGBA);
  191. EnableRange(view, VIEW_CLASS_ASTC_6x6_RGBA);
  192. EnableRange(view, VIEW_CLASS_ASTC_8x5_RGBA);
  193. EnableRange(view, VIEW_CLASS_ASTC_8x8_RGBA);
  194. EnableRange(view, VIEW_CLASS_ASTC_10x5_RGBA);
  195. EnableRange(view, VIEW_CLASS_ASTC_10x6_RGBA);
  196. EnableRange(view, VIEW_CLASS_ASTC_10x8_RGBA);
  197. EnableRange(view, VIEW_CLASS_ASTC_10x10_RGBA);
  198. EnableRange(view, VIEW_CLASS_ASTC_12x10_RGBA);
  199. EnableRange(view, VIEW_CLASS_ASTC_12x12_RGBA);
  200. return view;
  201. }
  202. constexpr Table MakeCopyTable() {
  203. Table copy = MakeViewTable();
  204. EnableRange(copy, COPY_CLASS_128_BITS);
  205. EnableRange(copy, COPY_CLASS_64_BITS);
  206. return copy;
  207. }
  208. constexpr Table MakeNativeBgrViewTable() {
  209. Table copy = MakeViewTable();
  210. EnableRange(copy, VIEW_CLASS_32_BITS);
  211. return copy;
  212. }
  213. constexpr Table MakeNonNativeBgrViewTable() {
  214. Table copy = MakeViewTable();
  215. EnableRange(copy, VIEW_CLASS_32_BITS_NO_BGR);
  216. return copy;
  217. }
  218. constexpr Table MakeNativeBgrCopyTable() {
  219. Table copy = MakeCopyTable();
  220. EnableRange(copy, VIEW_CLASS_32_BITS);
  221. return copy;
  222. }
  223. constexpr Table MakeNonNativeBgrCopyTable() {
  224. Table copy = MakeCopyTable();
  225. EnableRange(copy, VIEW_CLASS_32_BITS);
  226. return copy;
  227. }
  228. } // Anonymous namespace
  229. bool IsViewCompatible(PixelFormat format_a, PixelFormat format_b, bool broken_views,
  230. bool native_bgr) {
  231. if (format_a == format_b) {
  232. return true;
  233. }
  234. if (broken_views) {
  235. // If format views are broken, only accept formats that are identical.
  236. return format_a == format_b;
  237. }
  238. static constexpr Table BGR_TABLE = MakeNativeBgrViewTable();
  239. static constexpr Table NO_BGR_TABLE = MakeNonNativeBgrViewTable();
  240. return IsSupported(native_bgr ? BGR_TABLE : NO_BGR_TABLE, format_a, format_b);
  241. }
  242. bool IsCopyCompatible(PixelFormat format_a, PixelFormat format_b, bool native_bgr) {
  243. if (format_a == format_b) {
  244. return true;
  245. }
  246. static constexpr Table BGR_TABLE = MakeNativeBgrCopyTable();
  247. static constexpr Table NO_BGR_TABLE = MakeNonNativeBgrCopyTable();
  248. return IsSupported(native_bgr ? BGR_TABLE : NO_BGR_TABLE, format_a, format_b);
  249. }
  250. } // namespace VideoCore::Surface