compatible_formats.cpp 9.3 KB

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