decoders.cpp 14 KB

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