compatible_formats.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. // Missing formats:
  105. // PixelFormat::ASTC_2D_10X5_UNORM
  106. // PixelFormat::ASTC_2D_10X5_SRGB
  107. // Missing formats:
  108. // PixelFormat::ASTC_2D_10X6_SRGB
  109. constexpr std::array VIEW_CLASS_ASTC_10x6_RGBA{
  110. PixelFormat::ASTC_2D_10X6_UNORM,
  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. // Missing formats
  121. // ASTC_2D_12X10_UNORM,
  122. // ASTC_2D_12X10_SRGB,
  123. constexpr std::array VIEW_CLASS_ASTC_12x12_RGBA{
  124. PixelFormat::ASTC_2D_12X12_UNORM,
  125. PixelFormat::ASTC_2D_12X12_SRGB,
  126. };
  127. // Compatibility table taken from Table 4.X.1 in:
  128. // https://www.khronos.org/registry/OpenGL/extensions/ARB/ARB_copy_image.txt
  129. constexpr std::array COPY_CLASS_128_BITS{
  130. PixelFormat::R32G32B32A32_UINT, PixelFormat::R32G32B32A32_FLOAT, PixelFormat::R32G32B32A32_SINT,
  131. PixelFormat::BC2_UNORM, PixelFormat::BC2_SRGB, PixelFormat::BC3_UNORM,
  132. PixelFormat::BC3_SRGB, PixelFormat::BC5_UNORM, PixelFormat::BC5_SNORM,
  133. PixelFormat::BC7_UNORM, PixelFormat::BC7_SRGB, PixelFormat::BC6H_SFLOAT,
  134. PixelFormat::BC6H_UFLOAT,
  135. };
  136. // Missing formats:
  137. // PixelFormat::RGBA32I
  138. // COMPRESSED_RG_RGTC2
  139. constexpr std::array COPY_CLASS_64_BITS{
  140. PixelFormat::R16G16B16A16_FLOAT, PixelFormat::R16G16B16A16_UINT,
  141. PixelFormat::R16G16B16A16_UNORM, PixelFormat::R16G16B16A16_SNORM,
  142. PixelFormat::R16G16B16A16_SINT, PixelFormat::R32G32_UINT,
  143. PixelFormat::R32G32_FLOAT, PixelFormat::R32G32_SINT,
  144. PixelFormat::BC1_RGBA_UNORM, PixelFormat::BC1_RGBA_SRGB,
  145. };
  146. // Missing formats:
  147. // COMPRESSED_RGB_S3TC_DXT1_EXT
  148. // COMPRESSED_SRGB_S3TC_DXT1_EXT
  149. // COMPRESSED_RGBA_S3TC_DXT1_EXT
  150. // COMPRESSED_SIGNED_RED_RGTC1
  151. constexpr void Enable(Table& table, size_t format_a, size_t format_b) {
  152. table[format_a][format_b / 64] |= u64(1) << (format_b % 64);
  153. table[format_b][format_a / 64] |= u64(1) << (format_a % 64);
  154. }
  155. constexpr void Enable(Table& table, PixelFormat format_a, PixelFormat format_b) {
  156. Enable(table, static_cast<size_t>(format_a), static_cast<size_t>(format_b));
  157. }
  158. template <typename Range>
  159. constexpr void EnableRange(Table& table, const Range& range) {
  160. for (auto it_a = range.begin(); it_a != range.end(); ++it_a) {
  161. for (auto it_b = it_a; it_b != range.end(); ++it_b) {
  162. Enable(table, *it_a, *it_b);
  163. }
  164. }
  165. }
  166. constexpr bool IsSupported(const Table& table, PixelFormat format_a, PixelFormat format_b) {
  167. const size_t a = static_cast<size_t>(format_a);
  168. const size_t b = static_cast<size_t>(format_b);
  169. return ((table[a][b / 64] >> (b % 64)) & 1) != 0;
  170. }
  171. constexpr Table MakeViewTable() {
  172. Table view{};
  173. for (size_t i = 0; i < MaxPixelFormat; ++i) {
  174. // Identity is allowed
  175. Enable(view, i, i);
  176. }
  177. EnableRange(view, VIEW_CLASS_128_BITS);
  178. EnableRange(view, VIEW_CLASS_96_BITS);
  179. EnableRange(view, VIEW_CLASS_64_BITS);
  180. EnableRange(view, VIEW_CLASS_16_BITS);
  181. EnableRange(view, VIEW_CLASS_8_BITS);
  182. EnableRange(view, VIEW_CLASS_RGTC1_RED);
  183. EnableRange(view, VIEW_CLASS_RGTC2_RG);
  184. EnableRange(view, VIEW_CLASS_BPTC_UNORM);
  185. EnableRange(view, VIEW_CLASS_BPTC_FLOAT);
  186. EnableRange(view, VIEW_CLASS_ASTC_4x4_RGBA);
  187. EnableRange(view, VIEW_CLASS_ASTC_5x4_RGBA);
  188. EnableRange(view, VIEW_CLASS_ASTC_5x5_RGBA);
  189. EnableRange(view, VIEW_CLASS_ASTC_6x5_RGBA);
  190. EnableRange(view, VIEW_CLASS_ASTC_6x6_RGBA);
  191. EnableRange(view, VIEW_CLASS_ASTC_8x5_RGBA);
  192. EnableRange(view, VIEW_CLASS_ASTC_8x8_RGBA);
  193. EnableRange(view, VIEW_CLASS_ASTC_10x6_RGBA);
  194. EnableRange(view, VIEW_CLASS_ASTC_10x8_RGBA);
  195. EnableRange(view, VIEW_CLASS_ASTC_10x10_RGBA);
  196. EnableRange(view, VIEW_CLASS_ASTC_12x12_RGBA);
  197. return view;
  198. }
  199. constexpr Table MakeCopyTable() {
  200. Table copy = MakeViewTable();
  201. EnableRange(copy, COPY_CLASS_128_BITS);
  202. EnableRange(copy, COPY_CLASS_64_BITS);
  203. return copy;
  204. }
  205. constexpr Table MakeNativeBgrViewTable() {
  206. Table copy = MakeViewTable();
  207. EnableRange(copy, VIEW_CLASS_32_BITS);
  208. return copy;
  209. }
  210. constexpr Table MakeNonNativeBgrViewTable() {
  211. Table copy = MakeViewTable();
  212. EnableRange(copy, VIEW_CLASS_32_BITS_NO_BGR);
  213. return copy;
  214. }
  215. constexpr Table MakeNativeBgrCopyTable() {
  216. Table copy = MakeCopyTable();
  217. EnableRange(copy, VIEW_CLASS_32_BITS);
  218. return copy;
  219. }
  220. constexpr Table MakeNonNativeBgrCopyTable() {
  221. Table copy = MakeCopyTable();
  222. EnableRange(copy, VIEW_CLASS_32_BITS);
  223. return copy;
  224. }
  225. } // Anonymous namespace
  226. bool IsViewCompatible(PixelFormat format_a, PixelFormat format_b, bool broken_views,
  227. bool native_bgr) {
  228. if (broken_views) {
  229. // If format views are broken, only accept formats that are identical.
  230. return format_a == format_b;
  231. }
  232. static constexpr Table BGR_TABLE = MakeNativeBgrViewTable();
  233. static constexpr Table NO_BGR_TABLE = MakeNonNativeBgrViewTable();
  234. return IsSupported(native_bgr ? BGR_TABLE : NO_BGR_TABLE, format_a, format_b);
  235. }
  236. bool IsCopyCompatible(PixelFormat format_a, PixelFormat format_b, bool native_bgr) {
  237. static constexpr Table BGR_TABLE = MakeNativeBgrCopyTable();
  238. static constexpr Table NO_BGR_TABLE = MakeNonNativeBgrCopyTable();
  239. return IsSupported(native_bgr ? BGR_TABLE : NO_BGR_TABLE, format_a, format_b);
  240. }
  241. } // namespace VideoCore::Surface