morton.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. // Copyright 2018 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <array>
  5. #include <cstring>
  6. #include "common/assert.h"
  7. #include "common/common_types.h"
  8. #include "core/memory.h"
  9. #include "video_core/morton.h"
  10. #include "video_core/surface.h"
  11. #include "video_core/textures/decoders.h"
  12. namespace VideoCore {
  13. using Surface::GetBytesPerPixel;
  14. using Surface::PixelFormat;
  15. using MortonCopyFn = void (*)(u32, u32, u32, u32, u32, u32, u8*, std::size_t, VAddr);
  16. using ConversionArray = std::array<MortonCopyFn, Surface::MaxPixelFormat>;
  17. template <bool morton_to_linear, PixelFormat format>
  18. static void MortonCopy(u32 stride, u32 block_height, u32 height, u32 block_depth, u32 depth,
  19. u32 tile_width_spacing, u8* buffer, std::size_t buffer_size, VAddr addr) {
  20. constexpr u32 bytes_per_pixel = GetBytesPerPixel(format);
  21. // With the BCn formats (DXT and DXN), each 4x4 tile is swizzled instead of just individual
  22. // pixel values.
  23. const u32 tile_size_x{GetDefaultBlockWidth(format)};
  24. const u32 tile_size_y{GetDefaultBlockHeight(format)};
  25. if constexpr (morton_to_linear) {
  26. Tegra::Texture::UnswizzleTexture(buffer, addr, tile_size_x, tile_size_y, bytes_per_pixel,
  27. stride, height, depth, block_height, block_depth,
  28. tile_width_spacing);
  29. } else {
  30. Tegra::Texture::CopySwizzledData(
  31. (stride + tile_size_x - 1) / tile_size_x, (height + tile_size_y - 1) / tile_size_y,
  32. depth, bytes_per_pixel, bytes_per_pixel, Memory::GetPointer(addr), buffer, false,
  33. block_height, block_depth, tile_width_spacing);
  34. }
  35. }
  36. static constexpr ConversionArray morton_to_linear_fns = {
  37. // clang-format off
  38. MortonCopy<true, PixelFormat::ABGR8U>,
  39. MortonCopy<true, PixelFormat::ABGR8S>,
  40. MortonCopy<true, PixelFormat::ABGR8UI>,
  41. MortonCopy<true, PixelFormat::B5G6R5U>,
  42. MortonCopy<true, PixelFormat::A2B10G10R10U>,
  43. MortonCopy<true, PixelFormat::A1B5G5R5U>,
  44. MortonCopy<true, PixelFormat::R8U>,
  45. MortonCopy<true, PixelFormat::R8UI>,
  46. MortonCopy<true, PixelFormat::RGBA16F>,
  47. MortonCopy<true, PixelFormat::RGBA16U>,
  48. MortonCopy<true, PixelFormat::RGBA16UI>,
  49. MortonCopy<true, PixelFormat::R11FG11FB10F>,
  50. MortonCopy<true, PixelFormat::RGBA32UI>,
  51. MortonCopy<true, PixelFormat::DXT1>,
  52. MortonCopy<true, PixelFormat::DXT23>,
  53. MortonCopy<true, PixelFormat::DXT45>,
  54. MortonCopy<true, PixelFormat::DXN1>,
  55. MortonCopy<true, PixelFormat::DXN2UNORM>,
  56. MortonCopy<true, PixelFormat::DXN2SNORM>,
  57. MortonCopy<true, PixelFormat::BC7U>,
  58. MortonCopy<true, PixelFormat::BC6H_UF16>,
  59. MortonCopy<true, PixelFormat::BC6H_SF16>,
  60. MortonCopy<true, PixelFormat::ASTC_2D_4X4>,
  61. MortonCopy<true, PixelFormat::G8R8U>,
  62. MortonCopy<true, PixelFormat::G8R8S>,
  63. MortonCopy<true, PixelFormat::BGRA8>,
  64. MortonCopy<true, PixelFormat::RGBA32F>,
  65. MortonCopy<true, PixelFormat::RG32F>,
  66. MortonCopy<true, PixelFormat::R32F>,
  67. MortonCopy<true, PixelFormat::R16F>,
  68. MortonCopy<true, PixelFormat::R16U>,
  69. MortonCopy<true, PixelFormat::R16S>,
  70. MortonCopy<true, PixelFormat::R16UI>,
  71. MortonCopy<true, PixelFormat::R16I>,
  72. MortonCopy<true, PixelFormat::RG16>,
  73. MortonCopy<true, PixelFormat::RG16F>,
  74. MortonCopy<true, PixelFormat::RG16UI>,
  75. MortonCopy<true, PixelFormat::RG16I>,
  76. MortonCopy<true, PixelFormat::RG16S>,
  77. MortonCopy<true, PixelFormat::RGB32F>,
  78. MortonCopy<true, PixelFormat::RGBA8_SRGB>,
  79. MortonCopy<true, PixelFormat::RG8U>,
  80. MortonCopy<true, PixelFormat::RG8S>,
  81. MortonCopy<true, PixelFormat::RG32UI>,
  82. MortonCopy<true, PixelFormat::R32UI>,
  83. MortonCopy<true, PixelFormat::ASTC_2D_8X8>,
  84. MortonCopy<true, PixelFormat::ASTC_2D_8X5>,
  85. MortonCopy<true, PixelFormat::ASTC_2D_5X4>,
  86. MortonCopy<true, PixelFormat::BGRA8_SRGB>,
  87. MortonCopy<true, PixelFormat::DXT1_SRGB>,
  88. MortonCopy<true, PixelFormat::DXT23_SRGB>,
  89. MortonCopy<true, PixelFormat::DXT45_SRGB>,
  90. MortonCopy<true, PixelFormat::BC7U_SRGB>,
  91. MortonCopy<true, PixelFormat::ASTC_2D_4X4_SRGB>,
  92. MortonCopy<true, PixelFormat::ASTC_2D_8X8_SRGB>,
  93. MortonCopy<true, PixelFormat::ASTC_2D_8X5_SRGB>,
  94. MortonCopy<true, PixelFormat::ASTC_2D_5X4_SRGB>,
  95. MortonCopy<true, PixelFormat::ASTC_2D_5X5>,
  96. MortonCopy<true, PixelFormat::ASTC_2D_5X5_SRGB>,
  97. MortonCopy<true, PixelFormat::ASTC_2D_10X8>,
  98. MortonCopy<true, PixelFormat::ASTC_2D_10X8_SRGB>,
  99. MortonCopy<true, PixelFormat::Z32F>,
  100. MortonCopy<true, PixelFormat::Z16>,
  101. MortonCopy<true, PixelFormat::Z24S8>,
  102. MortonCopy<true, PixelFormat::S8Z24>,
  103. MortonCopy<true, PixelFormat::Z32FS8>,
  104. // clang-format on
  105. };
  106. static constexpr ConversionArray linear_to_morton_fns = {
  107. // clang-format off
  108. MortonCopy<false, PixelFormat::ABGR8U>,
  109. MortonCopy<false, PixelFormat::ABGR8S>,
  110. MortonCopy<false, PixelFormat::ABGR8UI>,
  111. MortonCopy<false, PixelFormat::B5G6R5U>,
  112. MortonCopy<false, PixelFormat::A2B10G10R10U>,
  113. MortonCopy<false, PixelFormat::A1B5G5R5U>,
  114. MortonCopy<false, PixelFormat::R8U>,
  115. MortonCopy<false, PixelFormat::R8UI>,
  116. MortonCopy<false, PixelFormat::RGBA16F>,
  117. MortonCopy<false, PixelFormat::RGBA16U>,
  118. MortonCopy<false, PixelFormat::RGBA16UI>,
  119. MortonCopy<false, PixelFormat::R11FG11FB10F>,
  120. MortonCopy<false, PixelFormat::RGBA32UI>,
  121. MortonCopy<false, PixelFormat::DXT1>,
  122. MortonCopy<false, PixelFormat::DXT23>,
  123. MortonCopy<false, PixelFormat::DXT45>,
  124. MortonCopy<false, PixelFormat::DXN1>,
  125. MortonCopy<false, PixelFormat::DXN2UNORM>,
  126. MortonCopy<false, PixelFormat::DXN2SNORM>,
  127. MortonCopy<false, PixelFormat::BC7U>,
  128. MortonCopy<false, PixelFormat::BC6H_UF16>,
  129. MortonCopy<false, PixelFormat::BC6H_SF16>,
  130. // TODO(Subv): Swizzling ASTC formats are not supported
  131. nullptr,
  132. MortonCopy<false, PixelFormat::G8R8U>,
  133. MortonCopy<false, PixelFormat::G8R8S>,
  134. MortonCopy<false, PixelFormat::BGRA8>,
  135. MortonCopy<false, PixelFormat::RGBA32F>,
  136. MortonCopy<false, PixelFormat::RG32F>,
  137. MortonCopy<false, PixelFormat::R32F>,
  138. MortonCopy<false, PixelFormat::R16F>,
  139. MortonCopy<false, PixelFormat::R16U>,
  140. MortonCopy<false, PixelFormat::R16S>,
  141. MortonCopy<false, PixelFormat::R16UI>,
  142. MortonCopy<false, PixelFormat::R16I>,
  143. MortonCopy<false, PixelFormat::RG16>,
  144. MortonCopy<false, PixelFormat::RG16F>,
  145. MortonCopy<false, PixelFormat::RG16UI>,
  146. MortonCopy<false, PixelFormat::RG16I>,
  147. MortonCopy<false, PixelFormat::RG16S>,
  148. MortonCopy<false, PixelFormat::RGB32F>,
  149. MortonCopy<false, PixelFormat::RGBA8_SRGB>,
  150. MortonCopy<false, PixelFormat::RG8U>,
  151. MortonCopy<false, PixelFormat::RG8S>,
  152. MortonCopy<false, PixelFormat::RG32UI>,
  153. MortonCopy<false, PixelFormat::R32UI>,
  154. nullptr,
  155. nullptr,
  156. nullptr,
  157. MortonCopy<false, PixelFormat::BGRA8_SRGB>,
  158. MortonCopy<false, PixelFormat::DXT1_SRGB>,
  159. MortonCopy<false, PixelFormat::DXT23_SRGB>,
  160. MortonCopy<false, PixelFormat::DXT45_SRGB>,
  161. MortonCopy<false, PixelFormat::BC7U_SRGB>,
  162. nullptr,
  163. nullptr,
  164. nullptr,
  165. nullptr,
  166. nullptr,
  167. nullptr,
  168. nullptr,
  169. nullptr,
  170. MortonCopy<false, PixelFormat::Z32F>,
  171. MortonCopy<false, PixelFormat::Z16>,
  172. MortonCopy<false, PixelFormat::Z24S8>,
  173. MortonCopy<false, PixelFormat::S8Z24>,
  174. MortonCopy<false, PixelFormat::Z32FS8>,
  175. // clang-format on
  176. };
  177. static MortonCopyFn GetSwizzleFunction(MortonSwizzleMode mode, Surface::PixelFormat format) {
  178. switch (mode) {
  179. case MortonSwizzleMode::MortonToLinear:
  180. return morton_to_linear_fns[static_cast<std::size_t>(format)];
  181. case MortonSwizzleMode::LinearToMorton:
  182. return linear_to_morton_fns[static_cast<std::size_t>(format)];
  183. }
  184. UNREACHABLE();
  185. }
  186. /// 8x8 Z-Order coordinate from 2D coordinates
  187. static u32 MortonInterleave(u32 x, u32 y) {
  188. static const u32 xlut[] = {0x00, 0x01, 0x04, 0x05, 0x10, 0x11, 0x14, 0x15};
  189. static const u32 ylut[] = {0x00, 0x02, 0x08, 0x0a, 0x20, 0x22, 0x28, 0x2a};
  190. return xlut[x % 8] + ylut[y % 8];
  191. }
  192. /// Calculates the offset of the position of the pixel in Morton order
  193. static u32 GetMortonOffset(u32 x, u32 y, u32 bytes_per_pixel) {
  194. // Images are split into 8x8 tiles. Each tile is composed of four 4x4 subtiles each
  195. // of which is composed of four 2x2 subtiles each of which is composed of four texels.
  196. // Each structure is embedded into the next-bigger one in a diagonal pattern, e.g.
  197. // texels are laid out in a 2x2 subtile like this:
  198. // 2 3
  199. // 0 1
  200. //
  201. // The full 8x8 tile has the texels arranged like this:
  202. //
  203. // 42 43 46 47 58 59 62 63
  204. // 40 41 44 45 56 57 60 61
  205. // 34 35 38 39 50 51 54 55
  206. // 32 33 36 37 48 49 52 53
  207. // 10 11 14 15 26 27 30 31
  208. // 08 09 12 13 24 25 28 29
  209. // 02 03 06 07 18 19 22 23
  210. // 00 01 04 05 16 17 20 21
  211. //
  212. // This pattern is what's called Z-order curve, or Morton order.
  213. const unsigned int block_height = 8;
  214. const unsigned int coarse_x = x & ~7;
  215. u32 i = MortonInterleave(x, y);
  216. const unsigned int offset = coarse_x * block_height;
  217. return (i + offset) * bytes_per_pixel;
  218. }
  219. static u32 MortonInterleave128(u32 x, u32 y) {
  220. // 128x128 Z-Order coordinate from 2D coordinates
  221. static constexpr u32 xlut[] = {
  222. 0x0000, 0x0001, 0x0002, 0x0003, 0x0008, 0x0009, 0x000a, 0x000b, 0x0040, 0x0041, 0x0042,
  223. 0x0043, 0x0048, 0x0049, 0x004a, 0x004b, 0x0800, 0x0801, 0x0802, 0x0803, 0x0808, 0x0809,
  224. 0x080a, 0x080b, 0x0840, 0x0841, 0x0842, 0x0843, 0x0848, 0x0849, 0x084a, 0x084b, 0x1000,
  225. 0x1001, 0x1002, 0x1003, 0x1008, 0x1009, 0x100a, 0x100b, 0x1040, 0x1041, 0x1042, 0x1043,
  226. 0x1048, 0x1049, 0x104a, 0x104b, 0x1800, 0x1801, 0x1802, 0x1803, 0x1808, 0x1809, 0x180a,
  227. 0x180b, 0x1840, 0x1841, 0x1842, 0x1843, 0x1848, 0x1849, 0x184a, 0x184b, 0x2000, 0x2001,
  228. 0x2002, 0x2003, 0x2008, 0x2009, 0x200a, 0x200b, 0x2040, 0x2041, 0x2042, 0x2043, 0x2048,
  229. 0x2049, 0x204a, 0x204b, 0x2800, 0x2801, 0x2802, 0x2803, 0x2808, 0x2809, 0x280a, 0x280b,
  230. 0x2840, 0x2841, 0x2842, 0x2843, 0x2848, 0x2849, 0x284a, 0x284b, 0x3000, 0x3001, 0x3002,
  231. 0x3003, 0x3008, 0x3009, 0x300a, 0x300b, 0x3040, 0x3041, 0x3042, 0x3043, 0x3048, 0x3049,
  232. 0x304a, 0x304b, 0x3800, 0x3801, 0x3802, 0x3803, 0x3808, 0x3809, 0x380a, 0x380b, 0x3840,
  233. 0x3841, 0x3842, 0x3843, 0x3848, 0x3849, 0x384a, 0x384b, 0x0000, 0x0001, 0x0002, 0x0003,
  234. 0x0008, 0x0009, 0x000a, 0x000b, 0x0040, 0x0041, 0x0042, 0x0043, 0x0048, 0x0049, 0x004a,
  235. 0x004b, 0x0800, 0x0801, 0x0802, 0x0803, 0x0808, 0x0809, 0x080a, 0x080b, 0x0840, 0x0841,
  236. 0x0842, 0x0843, 0x0848, 0x0849, 0x084a, 0x084b, 0x1000, 0x1001, 0x1002, 0x1003, 0x1008,
  237. 0x1009, 0x100a, 0x100b, 0x1040, 0x1041, 0x1042, 0x1043, 0x1048, 0x1049, 0x104a, 0x104b,
  238. 0x1800, 0x1801, 0x1802, 0x1803, 0x1808, 0x1809, 0x180a, 0x180b, 0x1840, 0x1841, 0x1842,
  239. 0x1843, 0x1848, 0x1849, 0x184a, 0x184b, 0x2000, 0x2001, 0x2002, 0x2003, 0x2008, 0x2009,
  240. 0x200a, 0x200b, 0x2040, 0x2041, 0x2042, 0x2043, 0x2048, 0x2049, 0x204a, 0x204b, 0x2800,
  241. 0x2801, 0x2802, 0x2803, 0x2808, 0x2809, 0x280a, 0x280b, 0x2840, 0x2841, 0x2842, 0x2843,
  242. 0x2848, 0x2849, 0x284a, 0x284b, 0x3000, 0x3001, 0x3002, 0x3003, 0x3008, 0x3009, 0x300a,
  243. 0x300b, 0x3040, 0x3041, 0x3042, 0x3043, 0x3048, 0x3049, 0x304a, 0x304b, 0x3800, 0x3801,
  244. 0x3802, 0x3803, 0x3808, 0x3809, 0x380a, 0x380b, 0x3840, 0x3841, 0x3842, 0x3843, 0x3848,
  245. 0x3849, 0x384a, 0x384b, 0x0000, 0x0001, 0x0002, 0x0003, 0x0008, 0x0009, 0x000a, 0x000b,
  246. 0x0040, 0x0041, 0x0042, 0x0043, 0x0048, 0x0049, 0x004a, 0x004b, 0x0800, 0x0801, 0x0802,
  247. 0x0803, 0x0808, 0x0809, 0x080a, 0x080b, 0x0840, 0x0841, 0x0842, 0x0843, 0x0848, 0x0849,
  248. 0x084a, 0x084b, 0x1000, 0x1001, 0x1002, 0x1003, 0x1008, 0x1009, 0x100a, 0x100b, 0x1040,
  249. 0x1041, 0x1042, 0x1043, 0x1048, 0x1049, 0x104a, 0x104b, 0x1800, 0x1801, 0x1802, 0x1803,
  250. 0x1808, 0x1809, 0x180a, 0x180b, 0x1840, 0x1841, 0x1842, 0x1843, 0x1848, 0x1849, 0x184a,
  251. 0x184b, 0x2000, 0x2001, 0x2002, 0x2003, 0x2008, 0x2009, 0x200a, 0x200b, 0x2040, 0x2041,
  252. 0x2042, 0x2043, 0x2048, 0x2049, 0x204a, 0x204b, 0x2800, 0x2801, 0x2802, 0x2803, 0x2808,
  253. 0x2809, 0x280a, 0x280b, 0x2840, 0x2841, 0x2842, 0x2843, 0x2848, 0x2849, 0x284a, 0x284b,
  254. 0x3000, 0x3001, 0x3002, 0x3003, 0x3008, 0x3009, 0x300a, 0x300b, 0x3040, 0x3041, 0x3042,
  255. 0x3043, 0x3048, 0x3049, 0x304a, 0x304b, 0x3800, 0x3801, 0x3802, 0x3803, 0x3808, 0x3809,
  256. 0x380a, 0x380b, 0x3840, 0x3841, 0x3842, 0x3843, 0x3848, 0x3849, 0x384a, 0x384b,
  257. };
  258. static constexpr u32 ylut[] = {
  259. 0x0000, 0x0004, 0x0010, 0x0014, 0x0020, 0x0024, 0x0030, 0x0034, 0x0080, 0x0084, 0x0090,
  260. 0x0094, 0x00a0, 0x00a4, 0x00b0, 0x00b4, 0x0100, 0x0104, 0x0110, 0x0114, 0x0120, 0x0124,
  261. 0x0130, 0x0134, 0x0180, 0x0184, 0x0190, 0x0194, 0x01a0, 0x01a4, 0x01b0, 0x01b4, 0x0200,
  262. 0x0204, 0x0210, 0x0214, 0x0220, 0x0224, 0x0230, 0x0234, 0x0280, 0x0284, 0x0290, 0x0294,
  263. 0x02a0, 0x02a4, 0x02b0, 0x02b4, 0x0300, 0x0304, 0x0310, 0x0314, 0x0320, 0x0324, 0x0330,
  264. 0x0334, 0x0380, 0x0384, 0x0390, 0x0394, 0x03a0, 0x03a4, 0x03b0, 0x03b4, 0x0400, 0x0404,
  265. 0x0410, 0x0414, 0x0420, 0x0424, 0x0430, 0x0434, 0x0480, 0x0484, 0x0490, 0x0494, 0x04a0,
  266. 0x04a4, 0x04b0, 0x04b4, 0x0500, 0x0504, 0x0510, 0x0514, 0x0520, 0x0524, 0x0530, 0x0534,
  267. 0x0580, 0x0584, 0x0590, 0x0594, 0x05a0, 0x05a4, 0x05b0, 0x05b4, 0x0600, 0x0604, 0x0610,
  268. 0x0614, 0x0620, 0x0624, 0x0630, 0x0634, 0x0680, 0x0684, 0x0690, 0x0694, 0x06a0, 0x06a4,
  269. 0x06b0, 0x06b4, 0x0700, 0x0704, 0x0710, 0x0714, 0x0720, 0x0724, 0x0730, 0x0734, 0x0780,
  270. 0x0784, 0x0790, 0x0794, 0x07a0, 0x07a4, 0x07b0, 0x07b4, 0x0000, 0x0004, 0x0010, 0x0014,
  271. 0x0020, 0x0024, 0x0030, 0x0034, 0x0080, 0x0084, 0x0090, 0x0094, 0x00a0, 0x00a4, 0x00b0,
  272. 0x00b4, 0x0100, 0x0104, 0x0110, 0x0114, 0x0120, 0x0124, 0x0130, 0x0134, 0x0180, 0x0184,
  273. 0x0190, 0x0194, 0x01a0, 0x01a4, 0x01b0, 0x01b4, 0x0200, 0x0204, 0x0210, 0x0214, 0x0220,
  274. 0x0224, 0x0230, 0x0234, 0x0280, 0x0284, 0x0290, 0x0294, 0x02a0, 0x02a4, 0x02b0, 0x02b4,
  275. 0x0300, 0x0304, 0x0310, 0x0314, 0x0320, 0x0324, 0x0330, 0x0334, 0x0380, 0x0384, 0x0390,
  276. 0x0394, 0x03a0, 0x03a4, 0x03b0, 0x03b4, 0x0400, 0x0404, 0x0410, 0x0414, 0x0420, 0x0424,
  277. 0x0430, 0x0434, 0x0480, 0x0484, 0x0490, 0x0494, 0x04a0, 0x04a4, 0x04b0, 0x04b4, 0x0500,
  278. 0x0504, 0x0510, 0x0514, 0x0520, 0x0524, 0x0530, 0x0534, 0x0580, 0x0584, 0x0590, 0x0594,
  279. 0x05a0, 0x05a4, 0x05b0, 0x05b4, 0x0600, 0x0604, 0x0610, 0x0614, 0x0620, 0x0624, 0x0630,
  280. 0x0634, 0x0680, 0x0684, 0x0690, 0x0694, 0x06a0, 0x06a4, 0x06b0, 0x06b4, 0x0700, 0x0704,
  281. 0x0710, 0x0714, 0x0720, 0x0724, 0x0730, 0x0734, 0x0780, 0x0784, 0x0790, 0x0794, 0x07a0,
  282. 0x07a4, 0x07b0, 0x07b4, 0x0000, 0x0004, 0x0010, 0x0014, 0x0020, 0x0024, 0x0030, 0x0034,
  283. 0x0080, 0x0084, 0x0090, 0x0094, 0x00a0, 0x00a4, 0x00b0, 0x00b4, 0x0100, 0x0104, 0x0110,
  284. 0x0114, 0x0120, 0x0124, 0x0130, 0x0134, 0x0180, 0x0184, 0x0190, 0x0194, 0x01a0, 0x01a4,
  285. 0x01b0, 0x01b4, 0x0200, 0x0204, 0x0210, 0x0214, 0x0220, 0x0224, 0x0230, 0x0234, 0x0280,
  286. 0x0284, 0x0290, 0x0294, 0x02a0, 0x02a4, 0x02b0, 0x02b4, 0x0300, 0x0304, 0x0310, 0x0314,
  287. 0x0320, 0x0324, 0x0330, 0x0334, 0x0380, 0x0384, 0x0390, 0x0394, 0x03a0, 0x03a4, 0x03b0,
  288. 0x03b4, 0x0400, 0x0404, 0x0410, 0x0414, 0x0420, 0x0424, 0x0430, 0x0434, 0x0480, 0x0484,
  289. 0x0490, 0x0494, 0x04a0, 0x04a4, 0x04b0, 0x04b4, 0x0500, 0x0504, 0x0510, 0x0514, 0x0520,
  290. 0x0524, 0x0530, 0x0534, 0x0580, 0x0584, 0x0590, 0x0594, 0x05a0, 0x05a4, 0x05b0, 0x05b4,
  291. 0x0600, 0x0604, 0x0610, 0x0614, 0x0620, 0x0624, 0x0630, 0x0634, 0x0680, 0x0684, 0x0690,
  292. 0x0694, 0x06a0, 0x06a4, 0x06b0, 0x06b4, 0x0700, 0x0704, 0x0710, 0x0714, 0x0720, 0x0724,
  293. 0x0730, 0x0734, 0x0780, 0x0784, 0x0790, 0x0794, 0x07a0, 0x07a4, 0x07b0, 0x07b4,
  294. };
  295. return xlut[x % 128] + ylut[y % 128];
  296. }
  297. static u32 GetMortonOffset128(u32 x, u32 y, u32 bytes_per_pixel) {
  298. // Calculates the offset of the position of the pixel in Morton order
  299. // Framebuffer images are split into 128x128 tiles.
  300. constexpr u32 block_height = 128;
  301. const u32 coarse_x = x & ~127;
  302. const u32 i = MortonInterleave128(x, y);
  303. const u32 offset = coarse_x * block_height;
  304. return (i + offset) * bytes_per_pixel;
  305. }
  306. void MortonSwizzle(MortonSwizzleMode mode, Surface::PixelFormat format, u32 stride,
  307. u32 block_height, u32 height, u32 block_depth, u32 depth, u32 tile_width_spacing,
  308. u8* buffer, std::size_t buffer_size, VAddr addr) {
  309. GetSwizzleFunction(mode, format)(stride, block_height, height, block_depth, depth,
  310. tile_width_spacing, buffer, buffer_size, addr);
  311. }
  312. void MortonCopyPixels128(u32 width, u32 height, u32 bytes_per_pixel, u32 linear_bytes_per_pixel,
  313. u8* morton_data, u8* linear_data, bool morton_to_linear) {
  314. u8* data_ptrs[2];
  315. for (u32 y = 0; y < height; ++y) {
  316. for (u32 x = 0; x < width; ++x) {
  317. const u32 coarse_y = y & ~127;
  318. const u32 morton_offset =
  319. GetMortonOffset128(x, y, bytes_per_pixel) + coarse_y * width * bytes_per_pixel;
  320. const u32 linear_pixel_index = (x + y * width) * linear_bytes_per_pixel;
  321. data_ptrs[morton_to_linear ? 1 : 0] = morton_data + morton_offset;
  322. data_ptrs[morton_to_linear ? 0 : 1] = &linear_data[linear_pixel_index];
  323. std::memcpy(data_ptrs[0], data_ptrs[1], bytes_per_pixel);
  324. }
  325. }
  326. }
  327. } // namespace VideoCore