decoders.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <array>
  4. #include <cmath>
  5. #include <cstring>
  6. #include <span>
  7. #include "common/alignment.h"
  8. #include "common/assert.h"
  9. #include "common/bit_util.h"
  10. #include "common/div_ceil.h"
  11. #include "video_core/gpu.h"
  12. #include "video_core/textures/decoders.h"
  13. namespace Tegra::Texture {
  14. namespace {
  15. template <u32 mask>
  16. constexpr u32 pdep(u32 value) {
  17. u32 result = 0;
  18. u32 m = mask;
  19. for (u32 bit = 1; m; bit += bit) {
  20. if (value & bit)
  21. result |= m & -m;
  22. m &= m - 1;
  23. }
  24. return result;
  25. }
  26. template <u32 mask, u32 incr_amount>
  27. void incrpdep(u32& value) {
  28. constexpr u32 swizzled_incr = pdep<mask>(incr_amount);
  29. value = ((value | ~mask) + swizzled_incr) & mask;
  30. }
  31. template <bool TO_LINEAR, u32 BYTES_PER_PIXEL>
  32. void SwizzleImpl(std::span<u8> output, std::span<const u8> input, u32 width, u32 height, u32 depth,
  33. u32 block_height, u32 block_depth, u32 stride_alignment) {
  34. // The origin of the transformation can be configured here, leave it as zero as the current API
  35. // doesn't expose it.
  36. static constexpr u32 origin_x = 0;
  37. static constexpr u32 origin_y = 0;
  38. static constexpr u32 origin_z = 0;
  39. // We can configure here a custom pitch
  40. // As it's not exposed 'width * BYTES_PER_PIXEL' will be the expected pitch.
  41. const u32 pitch = width * BYTES_PER_PIXEL;
  42. const u32 stride = Common::AlignUpLog2(width, stride_alignment) * BYTES_PER_PIXEL;
  43. const u32 gobs_in_x = Common::DivCeilLog2(stride, GOB_SIZE_X_SHIFT);
  44. const u32 block_size = gobs_in_x << (GOB_SIZE_SHIFT + block_height + block_depth);
  45. const u32 slice_size =
  46. Common::DivCeilLog2(height, block_height + GOB_SIZE_Y_SHIFT) * block_size;
  47. const u32 block_height_mask = (1U << block_height) - 1;
  48. const u32 block_depth_mask = (1U << block_depth) - 1;
  49. const u32 x_shift = GOB_SIZE_SHIFT + block_height + block_depth;
  50. for (u32 slice = 0; slice < depth; ++slice) {
  51. const u32 z = slice + origin_z;
  52. const u32 offset_z = (z >> block_depth) * slice_size +
  53. ((z & block_depth_mask) << (GOB_SIZE_SHIFT + block_height));
  54. for (u32 line = 0; line < height; ++line) {
  55. const u32 y = line + origin_y;
  56. const u32 swizzled_y = pdep<SWIZZLE_Y_BITS>(y);
  57. const u32 block_y = y >> GOB_SIZE_Y_SHIFT;
  58. const u32 offset_y = (block_y >> block_height) * block_size +
  59. ((block_y & block_height_mask) << GOB_SIZE_SHIFT);
  60. u32 swizzled_x = pdep<SWIZZLE_X_BITS>(origin_x * BYTES_PER_PIXEL);
  61. for (u32 column = 0; column < width;
  62. ++column, incrpdep<SWIZZLE_X_BITS, BYTES_PER_PIXEL>(swizzled_x)) {
  63. const u32 x = (column + origin_x) * BYTES_PER_PIXEL;
  64. const u32 offset_x = (x >> GOB_SIZE_X_SHIFT) << x_shift;
  65. const u32 base_swizzled_offset = offset_z + offset_y + offset_x;
  66. const u32 swizzled_offset = base_swizzled_offset + (swizzled_x | swizzled_y);
  67. const u32 unswizzled_offset =
  68. slice * pitch * height + line * pitch + column * BYTES_PER_PIXEL;
  69. u8* const dst = &output[TO_LINEAR ? swizzled_offset : unswizzled_offset];
  70. const u8* const src = &input[TO_LINEAR ? unswizzled_offset : swizzled_offset];
  71. std::memcpy(dst, src, BYTES_PER_PIXEL);
  72. }
  73. }
  74. }
  75. }
  76. template <bool TO_LINEAR>
  77. void Swizzle(std::span<u8> output, std::span<const u8> input, u32 bytes_per_pixel, u32 width,
  78. u32 height, u32 depth, u32 block_height, u32 block_depth, u32 stride_alignment) {
  79. switch (bytes_per_pixel) {
  80. #define BPP_CASE(x) \
  81. case x: \
  82. return SwizzleImpl<TO_LINEAR, x>(output, input, width, height, depth, block_height, \
  83. block_depth, stride_alignment);
  84. BPP_CASE(1)
  85. BPP_CASE(2)
  86. BPP_CASE(3)
  87. BPP_CASE(4)
  88. BPP_CASE(6)
  89. BPP_CASE(8)
  90. BPP_CASE(12)
  91. BPP_CASE(16)
  92. #undef BPP_CASE
  93. default:
  94. ASSERT_MSG(false, "Invalid bytes_per_pixel={}", bytes_per_pixel);
  95. }
  96. }
  97. template <u32 BYTES_PER_PIXEL>
  98. void SwizzleSubrect(u32 subrect_width, u32 subrect_height, u32 source_pitch, u32 swizzled_width,
  99. u8* swizzled_data, const u8* unswizzled_data, u32 block_height_bit,
  100. u32 offset_x, u32 offset_y) {
  101. const u32 block_height = 1U << block_height_bit;
  102. const u32 image_width_in_gobs =
  103. (swizzled_width * BYTES_PER_PIXEL + (GOB_SIZE_X - 1)) / GOB_SIZE_X;
  104. for (u32 line = 0; line < subrect_height; ++line) {
  105. const u32 dst_y = line + offset_y;
  106. const u32 gob_address_y =
  107. (dst_y / (GOB_SIZE_Y * block_height)) * GOB_SIZE * block_height * image_width_in_gobs +
  108. ((dst_y % (GOB_SIZE_Y * block_height)) / GOB_SIZE_Y) * GOB_SIZE;
  109. const u32 swizzled_y = pdep<SWIZZLE_Y_BITS>(dst_y);
  110. u32 swizzled_x = pdep<SWIZZLE_X_BITS>(offset_x * BYTES_PER_PIXEL);
  111. for (u32 x = 0; x < subrect_width;
  112. ++x, incrpdep<SWIZZLE_X_BITS, BYTES_PER_PIXEL>(swizzled_x)) {
  113. const u32 dst_x = x + offset_x;
  114. const u32 gob_address =
  115. gob_address_y + (dst_x * BYTES_PER_PIXEL / GOB_SIZE_X) * GOB_SIZE * block_height;
  116. const u32 swizzled_offset = gob_address + (swizzled_x | swizzled_y);
  117. const u32 unswizzled_offset = line * source_pitch + x * BYTES_PER_PIXEL;
  118. const u8* const source_line = unswizzled_data + unswizzled_offset;
  119. u8* const dest_addr = swizzled_data + swizzled_offset;
  120. std::memcpy(dest_addr, source_line, BYTES_PER_PIXEL);
  121. }
  122. }
  123. }
  124. template <u32 BYTES_PER_PIXEL>
  125. void UnswizzleSubrect(u32 line_length_in, u32 line_count, u32 pitch, u32 width, u32 block_height,
  126. u32 origin_x, u32 origin_y, u8* output, const u8* input) {
  127. const u32 stride = width * BYTES_PER_PIXEL;
  128. const u32 gobs_in_x = (stride + GOB_SIZE_X - 1) / GOB_SIZE_X;
  129. const u32 block_size = gobs_in_x << (GOB_SIZE_SHIFT + block_height);
  130. const u32 block_height_mask = (1U << block_height) - 1;
  131. const u32 x_shift = GOB_SIZE_SHIFT + block_height;
  132. for (u32 line = 0; line < line_count; ++line) {
  133. const u32 src_y = line + origin_y;
  134. const u32 swizzled_y = pdep<SWIZZLE_Y_BITS>(src_y);
  135. const u32 block_y = src_y >> GOB_SIZE_Y_SHIFT;
  136. const u32 src_offset_y = (block_y >> block_height) * block_size +
  137. ((block_y & block_height_mask) << GOB_SIZE_SHIFT);
  138. u32 swizzled_x = pdep<SWIZZLE_X_BITS>(origin_x * BYTES_PER_PIXEL);
  139. for (u32 column = 0; column < line_length_in;
  140. ++column, incrpdep<SWIZZLE_X_BITS, BYTES_PER_PIXEL>(swizzled_x)) {
  141. const u32 src_x = (column + origin_x) * BYTES_PER_PIXEL;
  142. const u32 src_offset_x = (src_x >> GOB_SIZE_X_SHIFT) << x_shift;
  143. const u32 swizzled_offset = src_offset_y + src_offset_x + (swizzled_x | swizzled_y);
  144. const u32 unswizzled_offset = line * pitch + column * BYTES_PER_PIXEL;
  145. std::memcpy(output + unswizzled_offset, input + swizzled_offset, BYTES_PER_PIXEL);
  146. }
  147. }
  148. }
  149. template <u32 BYTES_PER_PIXEL>
  150. void SwizzleSliceToVoxel(u32 line_length_in, u32 line_count, u32 pitch, u32 width, u32 height,
  151. u32 block_height, u32 block_depth, u32 origin_x, u32 origin_y, u8* output,
  152. const u8* input) {
  153. UNIMPLEMENTED_IF(origin_x > 0);
  154. UNIMPLEMENTED_IF(origin_y > 0);
  155. const u32 stride = width * BYTES_PER_PIXEL;
  156. const u32 gobs_in_x = (stride + GOB_SIZE_X - 1) / GOB_SIZE_X;
  157. const u32 block_size = gobs_in_x << (GOB_SIZE_SHIFT + block_height + block_depth);
  158. const u32 block_height_mask = (1U << block_height) - 1;
  159. const u32 x_shift = static_cast<u32>(GOB_SIZE_SHIFT) + block_height + block_depth;
  160. for (u32 line = 0; line < line_count; ++line) {
  161. const u32 swizzled_y = pdep<SWIZZLE_Y_BITS>(line);
  162. const u32 block_y = line / GOB_SIZE_Y;
  163. const u32 dst_offset_y =
  164. (block_y >> block_height) * block_size + (block_y & block_height_mask) * GOB_SIZE;
  165. u32 swizzled_x = 0;
  166. for (u32 x = 0; x < line_length_in; ++x, incrpdep<SWIZZLE_X_BITS, 1>(swizzled_x)) {
  167. const u32 dst_offset =
  168. ((x / GOB_SIZE_X) << x_shift) + dst_offset_y + (swizzled_x | swizzled_y);
  169. const u32 src_offset = x * BYTES_PER_PIXEL + line * pitch;
  170. std::memcpy(output + dst_offset, input + src_offset, BYTES_PER_PIXEL);
  171. }
  172. }
  173. }
  174. } // Anonymous namespace
  175. void UnswizzleTexture(std::span<u8> output, std::span<const u8> input, u32 bytes_per_pixel,
  176. u32 width, u32 height, u32 depth, u32 block_height, u32 block_depth,
  177. u32 stride_alignment) {
  178. Swizzle<false>(output, input, bytes_per_pixel, width, height, depth, block_height, block_depth,
  179. stride_alignment);
  180. }
  181. void SwizzleTexture(std::span<u8> output, std::span<const u8> input, u32 bytes_per_pixel, u32 width,
  182. u32 height, u32 depth, u32 block_height, u32 block_depth,
  183. u32 stride_alignment) {
  184. Swizzle<true>(output, input, bytes_per_pixel, width, height, depth, block_height, block_depth,
  185. stride_alignment);
  186. }
  187. void SwizzleSubrect(u32 subrect_width, u32 subrect_height, u32 source_pitch, u32 swizzled_width,
  188. u32 bytes_per_pixel, u8* swizzled_data, const u8* unswizzled_data,
  189. u32 block_height_bit, u32 offset_x, u32 offset_y) {
  190. switch (bytes_per_pixel) {
  191. #define BPP_CASE(x) \
  192. case x: \
  193. return SwizzleSubrect<x>(subrect_width, subrect_height, source_pitch, swizzled_width, \
  194. swizzled_data, unswizzled_data, block_height_bit, offset_x, \
  195. offset_y);
  196. BPP_CASE(1)
  197. BPP_CASE(2)
  198. BPP_CASE(3)
  199. BPP_CASE(4)
  200. BPP_CASE(6)
  201. BPP_CASE(8)
  202. BPP_CASE(12)
  203. BPP_CASE(16)
  204. #undef BPP_CASE
  205. default:
  206. ASSERT_MSG(false, "Invalid bytes_per_pixel={}", bytes_per_pixel);
  207. }
  208. }
  209. void UnswizzleSubrect(u32 line_length_in, u32 line_count, u32 pitch, u32 width, u32 bytes_per_pixel,
  210. u32 block_height, u32 origin_x, u32 origin_y, u8* output, const u8* input) {
  211. switch (bytes_per_pixel) {
  212. #define BPP_CASE(x) \
  213. case x: \
  214. return UnswizzleSubrect<x>(line_length_in, line_count, pitch, width, block_height, \
  215. origin_x, origin_y, output, input);
  216. BPP_CASE(1)
  217. BPP_CASE(2)
  218. BPP_CASE(3)
  219. BPP_CASE(4)
  220. BPP_CASE(6)
  221. BPP_CASE(8)
  222. BPP_CASE(12)
  223. BPP_CASE(16)
  224. #undef BPP_CASE
  225. default:
  226. ASSERT_MSG(false, "Invalid bytes_per_pixel={}", bytes_per_pixel);
  227. }
  228. }
  229. void SwizzleSliceToVoxel(u32 line_length_in, u32 line_count, u32 pitch, u32 width, u32 height,
  230. u32 bytes_per_pixel, u32 block_height, u32 block_depth, u32 origin_x,
  231. u32 origin_y, u8* output, const u8* input) {
  232. switch (bytes_per_pixel) {
  233. #define BPP_CASE(x) \
  234. case x: \
  235. return SwizzleSliceToVoxel<x>(line_length_in, line_count, pitch, width, height, \
  236. block_height, block_depth, origin_x, origin_y, output, \
  237. input);
  238. BPP_CASE(1)
  239. BPP_CASE(2)
  240. BPP_CASE(3)
  241. BPP_CASE(4)
  242. BPP_CASE(6)
  243. BPP_CASE(8)
  244. BPP_CASE(12)
  245. BPP_CASE(16)
  246. #undef BPP_CASE
  247. default:
  248. ASSERT_MSG(false, "Invalid bytes_per_pixel={}", bytes_per_pixel);
  249. }
  250. }
  251. void SwizzleKepler(const u32 width, const u32 height, const u32 dst_x, const u32 dst_y,
  252. const u32 block_height_bit, const std::size_t copy_size, const u8* source_data,
  253. u8* swizzle_data) {
  254. const u32 block_height = 1U << block_height_bit;
  255. const u32 image_width_in_gobs{(width + GOB_SIZE_X - 1) / GOB_SIZE_X};
  256. std::size_t count = 0;
  257. for (std::size_t y = dst_y; y < height && count < copy_size; ++y) {
  258. const std::size_t gob_address_y =
  259. (y / (GOB_SIZE_Y * block_height)) * GOB_SIZE * block_height * image_width_in_gobs +
  260. ((y % (GOB_SIZE_Y * block_height)) / GOB_SIZE_Y) * GOB_SIZE;
  261. const u32 swizzled_y = pdep<SWIZZLE_Y_BITS>(static_cast<u32>(y));
  262. u32 swizzled_x = pdep<SWIZZLE_X_BITS>(dst_x);
  263. for (std::size_t x = dst_x; x < width && count < copy_size;
  264. ++x, incrpdep<SWIZZLE_X_BITS, 1>(swizzled_x)) {
  265. const std::size_t gob_address =
  266. gob_address_y + (x / GOB_SIZE_X) * GOB_SIZE * block_height;
  267. const std::size_t swizzled_offset = gob_address + (swizzled_x | swizzled_y);
  268. const u8* source_line = source_data + count;
  269. u8* dest_addr = swizzle_data + swizzled_offset;
  270. count++;
  271. *dest_addr = *source_line;
  272. }
  273. }
  274. }
  275. std::size_t CalculateSize(bool tiled, u32 bytes_per_pixel, u32 width, u32 height, u32 depth,
  276. u32 block_height, u32 block_depth) {
  277. if (tiled) {
  278. const u32 aligned_width = Common::AlignUpLog2(width * bytes_per_pixel, GOB_SIZE_X_SHIFT);
  279. const u32 aligned_height = Common::AlignUpLog2(height, GOB_SIZE_Y_SHIFT + block_height);
  280. const u32 aligned_depth = Common::AlignUpLog2(depth, GOB_SIZE_Z_SHIFT + block_depth);
  281. return aligned_width * aligned_height * aligned_depth;
  282. } else {
  283. return width * height * depth * bytes_per_pixel;
  284. }
  285. }
  286. u64 GetGOBOffset(u32 width, u32 height, u32 dst_x, u32 dst_y, u32 block_height,
  287. u32 bytes_per_pixel) {
  288. auto div_ceil = [](const u32 x, const u32 y) { return ((x + y - 1) / y); };
  289. const u32 gobs_in_block = 1 << block_height;
  290. const u32 y_blocks = GOB_SIZE_Y << block_height;
  291. const u32 x_per_gob = GOB_SIZE_X / bytes_per_pixel;
  292. const u32 x_blocks = div_ceil(width, x_per_gob);
  293. const u32 block_size = GOB_SIZE * gobs_in_block;
  294. const u32 stride = block_size * x_blocks;
  295. const u32 base = (dst_y / y_blocks) * stride + (dst_x / x_per_gob) * block_size;
  296. const u32 relative_y = dst_y % y_blocks;
  297. return base + (relative_y / GOB_SIZE_Y) * GOB_SIZE;
  298. }
  299. } // namespace Tegra::Texture