decoders.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. // Copyright 2018 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <cmath>
  5. #include <cstring>
  6. #include "common/alignment.h"
  7. #include "common/assert.h"
  8. #include "core/memory.h"
  9. #include "video_core/gpu.h"
  10. #include "video_core/textures/decoders.h"
  11. #include "video_core/textures/texture.h"
  12. namespace Tegra::Texture {
  13. /**
  14. * This table represents the internal swizzle of a gob,
  15. * in format 16 bytes x 2 sector packing.
  16. * Calculates the offset of an (x, y) position within a swizzled texture.
  17. * Taken from the Tegra X1 Technical Reference Manual. pages 1187-1188
  18. */
  19. template <std::size_t N, std::size_t M, u32 Align>
  20. struct alignas(64) SwizzleTable {
  21. static_assert(M * Align == 64, "Swizzle Table does not align to GOB");
  22. constexpr SwizzleTable() {
  23. for (u32 y = 0; y < N; ++y) {
  24. for (u32 x = 0; x < M; ++x) {
  25. const u32 x2 = x * Align;
  26. values[y][x] = static_cast<u16>(((x2 % 64) / 32) * 256 + ((y % 8) / 2) * 64 +
  27. ((x2 % 32) / 16) * 32 + (y % 2) * 16 + (x2 % 16));
  28. }
  29. }
  30. }
  31. const std::array<u16, M>& operator[](std::size_t index) const {
  32. return values[index];
  33. }
  34. std::array<std::array<u16, M>, N> values{};
  35. };
  36. constexpr auto legacy_swizzle_table = SwizzleTable<8, 64, 1>();
  37. constexpr auto fast_swizzle_table = SwizzleTable<8, 4, 16>();
  38. static void LegacySwizzleData(u32 width, u32 height, u32 bytes_per_pixel, u32 out_bytes_per_pixel,
  39. u8* swizzled_data, u8* unswizzled_data, bool unswizzle,
  40. u32 block_height) {
  41. std::array<u8*, 2> data_ptrs;
  42. const std::size_t stride = width * bytes_per_pixel;
  43. const std::size_t gobs_in_x = 64;
  44. const std::size_t gobs_in_y = 8;
  45. const std::size_t gobs_size = gobs_in_x * gobs_in_y;
  46. const std::size_t image_width_in_gobs{(stride + gobs_in_x - 1) / gobs_in_x};
  47. for (std::size_t y = 0; y < height; ++y) {
  48. const std::size_t gob_y_address =
  49. (y / (gobs_in_y * block_height)) * gobs_size * block_height * image_width_in_gobs +
  50. (y % (gobs_in_y * block_height) / gobs_in_y) * gobs_size;
  51. const auto& table = legacy_swizzle_table[y % gobs_in_y];
  52. for (std::size_t x = 0; x < width; ++x) {
  53. const std::size_t gob_address =
  54. gob_y_address + (x * bytes_per_pixel / gobs_in_x) * gobs_size * block_height;
  55. const std::size_t x2 = x * bytes_per_pixel;
  56. const std::size_t swizzle_offset = gob_address + table[x2 % gobs_in_x];
  57. const std::size_t pixel_index = (x + y * width) * out_bytes_per_pixel;
  58. data_ptrs[unswizzle] = swizzled_data + swizzle_offset;
  59. data_ptrs[!unswizzle] = unswizzled_data + pixel_index;
  60. std::memcpy(data_ptrs[0], data_ptrs[1], bytes_per_pixel);
  61. }
  62. }
  63. }
  64. static void FastSwizzleData(u32 width, u32 height, u32 bytes_per_pixel, u32 out_bytes_per_pixel,
  65. u8* swizzled_data, u8* unswizzled_data, bool unswizzle,
  66. u32 block_height) {
  67. std::array<u8*, 2> data_ptrs;
  68. const std::size_t stride{width * bytes_per_pixel};
  69. const std::size_t gobs_in_x = 64;
  70. const std::size_t gobs_in_y = 8;
  71. const std::size_t gobs_size = gobs_in_x * gobs_in_y;
  72. const std::size_t image_width_in_gobs{(stride + gobs_in_x - 1) / gobs_in_x};
  73. const std::size_t copy_size{16};
  74. for (std::size_t y = 0; y < height; ++y) {
  75. const std::size_t initial_gob =
  76. (y / (gobs_in_y * block_height)) * gobs_size * block_height * image_width_in_gobs +
  77. (y % (gobs_in_y * block_height) / gobs_in_y) * gobs_size;
  78. const std::size_t pixel_base{y * width * out_bytes_per_pixel};
  79. const auto& table = fast_swizzle_table[y % gobs_in_y];
  80. for (std::size_t xb = 0; xb < stride; xb += copy_size) {
  81. const std::size_t gob_address{initial_gob +
  82. (xb / gobs_in_x) * gobs_size * block_height};
  83. const std::size_t swizzle_offset{gob_address + table[(xb / 16) % 4]};
  84. const std::size_t out_x = xb * out_bytes_per_pixel / bytes_per_pixel;
  85. const std::size_t pixel_index{out_x + pixel_base};
  86. data_ptrs[unswizzle] = swizzled_data + swizzle_offset;
  87. data_ptrs[!unswizzle] = unswizzled_data + pixel_index;
  88. std::memcpy(data_ptrs[0], data_ptrs[1], copy_size);
  89. }
  90. }
  91. }
  92. void Fast3DProcessGobs(u8* swizzled_data, u8* unswizzled_data, bool unswizzle, const u32 x_start,
  93. const u32 y_start, const u32 z_start, const u32 x_end, const u32 y_end,
  94. const u32 z_end, const u32 tile_offset, const u32 xy_block_size,
  95. const u32 layer_z, const u32 stride_x, const u32 bytes_per_pixel,
  96. const u32 out_bytes_per_pixel) {
  97. std::array<u8*, 2> data_ptrs;
  98. u32 z_adress = tile_offset;
  99. const u32 x_startb = x_start * bytes_per_pixel;
  100. const u32 x_endb = x_end * bytes_per_pixel;
  101. const u32 copy_size = 16;
  102. const u32 gob_size = 64 * 8 * 1;
  103. for (u32 z = z_start; z < z_end; z++) {
  104. u32 y_adress = z_adress;
  105. u32 pixel_base = layer_z * z + y_start * stride_x;
  106. for (u32 y = y_start; y < y_end; y++) {
  107. const auto& table = fast_swizzle_table[y % 8];
  108. for (u32 xb = x_startb; xb < x_endb; xb += copy_size) {
  109. const u32 swizzle_offset{y_adress + table[(xb / 16) % 4]};
  110. const u32 out_x = xb * out_bytes_per_pixel / bytes_per_pixel;
  111. const u32 pixel_index{out_x + pixel_base};
  112. data_ptrs[unswizzle] = swizzled_data + swizzle_offset;
  113. data_ptrs[!unswizzle] = unswizzled_data + pixel_index;
  114. std::memcpy(data_ptrs[0], data_ptrs[1], copy_size);
  115. }
  116. pixel_base += stride_x;
  117. if ((y + 1) % 8 == 0)
  118. y_adress += gob_size;
  119. }
  120. z_adress += xy_block_size;
  121. }
  122. }
  123. void Fast3DSwizzledData(u8* swizzled_data, u8* unswizzled_data, bool unswizzle, u32 width,
  124. u32 height, u32 depth, u32 bytes_per_pixel, u32 out_bytes_per_pixel,
  125. u32 block_height, u32 block_depth) {
  126. auto div_ceil = [](u32 x, u32 y) { return ((x + y - 1) / y); };
  127. const u32 stride_x = width * out_bytes_per_pixel;
  128. const u32 layer_z = height * stride_x;
  129. const u32 gob_x_bytes = 64;
  130. const u32 gob_elements_x = gob_x_bytes / bytes_per_pixel;
  131. const u32 gob_elements_y = 8;
  132. const u32 gob_elements_z = 1;
  133. const u32 block_x_elements = gob_elements_x;
  134. const u32 block_y_elements = gob_elements_y * block_height;
  135. const u32 block_z_elements = gob_elements_z * block_depth;
  136. const u32 blocks_on_x = div_ceil(width, block_x_elements);
  137. const u32 blocks_on_y = div_ceil(height, block_y_elements);
  138. const u32 blocks_on_z = div_ceil(depth, block_z_elements);
  139. const u32 blocks = blocks_on_x * blocks_on_y * blocks_on_z;
  140. const u32 gob_size = 64 * 8 * 1;
  141. const u32 xy_block_size = gob_size * block_height;
  142. const u32 block_size = xy_block_size * block_depth;
  143. u32 tile_offset = 0;
  144. for (u32 zb = 0; zb < blocks_on_z; zb++) {
  145. const u32 z_start = zb * block_z_elements;
  146. const u32 z_end = std::min(depth, z_start + block_z_elements);
  147. for (u32 yb = 0; yb < blocks_on_y; yb++) {
  148. const u32 y_start = yb * block_y_elements;
  149. const u32 y_end = std::min(height, y_start + block_y_elements);
  150. for (u32 xb = 0; xb < blocks_on_x; xb++) {
  151. const u32 x_start = xb * block_x_elements;
  152. const u32 x_end = std::min(width, x_start + block_x_elements);
  153. Fast3DProcessGobs(swizzled_data, unswizzled_data, unswizzle, x_start, y_start,
  154. z_start, x_end, y_end, z_end, tile_offset, xy_block_size, layer_z,
  155. stride_x, bytes_per_pixel, out_bytes_per_pixel);
  156. tile_offset += block_size;
  157. }
  158. }
  159. }
  160. }
  161. void CopySwizzledData(u32 width, u32 height, u32 bytes_per_pixel, u32 out_bytes_per_pixel,
  162. u8* swizzled_data, u8* unswizzled_data, bool unswizzle, u32 block_height) {
  163. if (bytes_per_pixel % 3 != 0 && (width * bytes_per_pixel) % 16 == 0) {
  164. Fast3DSwizzledData(swizzled_data, unswizzled_data, unswizzle, width, height, 1U,
  165. bytes_per_pixel, out_bytes_per_pixel, block_height, 1U);
  166. } else {
  167. LegacySwizzleData(width, height, bytes_per_pixel, out_bytes_per_pixel, swizzled_data,
  168. unswizzled_data, unswizzle, block_height);
  169. }
  170. }
  171. u32 BytesPerPixel(TextureFormat format) {
  172. switch (format) {
  173. case TextureFormat::DXT1:
  174. case TextureFormat::DXN1:
  175. // In this case a 'pixel' actually refers to a 4x4 tile.
  176. return 8;
  177. case TextureFormat::DXT23:
  178. case TextureFormat::DXT45:
  179. case TextureFormat::DXN2:
  180. case TextureFormat::BC7U:
  181. case TextureFormat::BC6H_UF16:
  182. case TextureFormat::BC6H_SF16:
  183. // In this case a 'pixel' actually refers to a 4x4 tile.
  184. return 16;
  185. case TextureFormat::R32_G32_B32:
  186. return 12;
  187. case TextureFormat::ASTC_2D_4X4:
  188. case TextureFormat::ASTC_2D_8X8:
  189. case TextureFormat::A8R8G8B8:
  190. case TextureFormat::A2B10G10R10:
  191. case TextureFormat::BF10GF11RF11:
  192. case TextureFormat::R32:
  193. case TextureFormat::R16_G16:
  194. return 4;
  195. case TextureFormat::A1B5G5R5:
  196. case TextureFormat::B5G6R5:
  197. case TextureFormat::G8R8:
  198. case TextureFormat::R16:
  199. return 2;
  200. case TextureFormat::R8:
  201. return 1;
  202. case TextureFormat::R16_G16_B16_A16:
  203. return 8;
  204. case TextureFormat::R32_G32_B32_A32:
  205. return 16;
  206. case TextureFormat::R32_G32:
  207. return 8;
  208. default:
  209. UNIMPLEMENTED_MSG("Format not implemented");
  210. break;
  211. }
  212. }
  213. std::vector<u8> UnswizzleTexture(VAddr address, u32 tile_size, u32 bytes_per_pixel, u32 width,
  214. u32 height, u32 block_height) {
  215. std::vector<u8> unswizzled_data(width * height * bytes_per_pixel);
  216. CopySwizzledData(width / tile_size, height / tile_size, bytes_per_pixel, bytes_per_pixel,
  217. Memory::GetPointer(address), unswizzled_data.data(), true, block_height);
  218. return unswizzled_data;
  219. }
  220. std::vector<u8> DecodeTexture(const std::vector<u8>& texture_data, TextureFormat format, u32 width,
  221. u32 height) {
  222. std::vector<u8> rgba_data;
  223. // TODO(Subv): Implement.
  224. switch (format) {
  225. case TextureFormat::DXT1:
  226. case TextureFormat::DXT23:
  227. case TextureFormat::DXT45:
  228. case TextureFormat::DXN1:
  229. case TextureFormat::DXN2:
  230. case TextureFormat::BC7U:
  231. case TextureFormat::BC6H_UF16:
  232. case TextureFormat::BC6H_SF16:
  233. case TextureFormat::ASTC_2D_4X4:
  234. case TextureFormat::ASTC_2D_8X8:
  235. case TextureFormat::A8R8G8B8:
  236. case TextureFormat::A2B10G10R10:
  237. case TextureFormat::A1B5G5R5:
  238. case TextureFormat::B5G6R5:
  239. case TextureFormat::R8:
  240. case TextureFormat::G8R8:
  241. case TextureFormat::BF10GF11RF11:
  242. case TextureFormat::R32_G32_B32_A32:
  243. case TextureFormat::R32_G32:
  244. case TextureFormat::R32:
  245. case TextureFormat::R16:
  246. case TextureFormat::R16_G16:
  247. case TextureFormat::R32_G32_B32:
  248. // TODO(Subv): For the time being just forward the same data without any decoding.
  249. rgba_data = texture_data;
  250. break;
  251. default:
  252. UNIMPLEMENTED_MSG("Format not implemented");
  253. break;
  254. }
  255. return rgba_data;
  256. }
  257. std::size_t CalculateSize(bool tiled, u32 bytes_per_pixel, u32 width, u32 height, u32 depth,
  258. u32 block_height, u32 block_depth) {
  259. if (tiled) {
  260. const u32 gobs_in_x = 64 / bytes_per_pixel;
  261. const u32 gobs_in_y = 8;
  262. const u32 gobs_in_z = 1;
  263. const u32 aligned_width = Common::AlignUp(width, gobs_in_x);
  264. const u32 aligned_height = Common::AlignUp(height, gobs_in_y * block_height);
  265. const u32 aligned_depth = Common::AlignUp(depth, gobs_in_z * block_depth);
  266. return aligned_width * aligned_height * aligned_depth * bytes_per_pixel;
  267. } else {
  268. return width * height * depth * bytes_per_pixel;
  269. }
  270. }
  271. } // namespace Tegra::Texture