decoders.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. /**
  19. * This table represents the internal swizzle of a gob, in format 16 bytes x 2 sector packing.
  20. * Calculates the offset of an (x, y) position within a swizzled texture.
  21. * Taken from the Tegra X1 Technical Reference Manual. pages 1187-1188
  22. */
  23. constexpr SwizzleTable MakeSwizzleTableConst() {
  24. SwizzleTable table{};
  25. for (u32 y = 0; y < table.size(); ++y) {
  26. for (u32 x = 0; x < table[0].size(); ++x) {
  27. table[y][x] = ((x % 64) / 32) * 256 + ((y % 8) / 2) * 64 + ((x % 32) / 16) * 32 +
  28. (y % 2) * 16 + (x % 16);
  29. }
  30. }
  31. return table;
  32. }
  33. constexpr SwizzleTable SWIZZLE_TABLE = MakeSwizzleTableConst();
  34. template <bool TO_LINEAR>
  35. void Swizzle(std::span<u8> output, std::span<const u8> input, u32 bytes_per_pixel, u32 width,
  36. u32 height, u32 depth, u32 block_height, u32 block_depth, u32 stride_alignment) {
  37. // The origin of the transformation can be configured here, leave it as zero as the current API
  38. // doesn't expose it.
  39. static constexpr u32 origin_x = 0;
  40. static constexpr u32 origin_y = 0;
  41. static constexpr u32 origin_z = 0;
  42. // We can configure here a custom pitch
  43. // As it's not exposed 'width * bpp' will be the expected pitch.
  44. const u32 pitch = width * bytes_per_pixel;
  45. const u32 stride = Common::AlignUpLog2(width, stride_alignment) * bytes_per_pixel;
  46. const u32 gobs_in_x = Common::DivCeilLog2(stride, GOB_SIZE_X_SHIFT);
  47. const u32 block_size = gobs_in_x << (GOB_SIZE_SHIFT + block_height + block_depth);
  48. const u32 slice_size =
  49. Common::DivCeilLog2(height, block_height + GOB_SIZE_Y_SHIFT) * block_size;
  50. const u32 block_height_mask = (1U << block_height) - 1;
  51. const u32 block_depth_mask = (1U << block_depth) - 1;
  52. const u32 x_shift = GOB_SIZE_SHIFT + block_height + block_depth;
  53. for (u32 slice = 0; slice < depth; ++slice) {
  54. const u32 z = slice + origin_z;
  55. const u32 offset_z = (z >> block_depth) * slice_size +
  56. ((z & block_depth_mask) << (GOB_SIZE_SHIFT + block_height));
  57. for (u32 line = 0; line < height; ++line) {
  58. const u32 y = line + origin_y;
  59. const auto& table = SWIZZLE_TABLE[y % GOB_SIZE_Y];
  60. const u32 block_y = y >> GOB_SIZE_Y_SHIFT;
  61. const u32 offset_y = (block_y >> block_height) * block_size +
  62. ((block_y & block_height_mask) << GOB_SIZE_SHIFT);
  63. for (u32 column = 0; column < width; ++column) {
  64. const u32 x = (column + origin_x) * bytes_per_pixel;
  65. const u32 offset_x = (x >> GOB_SIZE_X_SHIFT) << x_shift;
  66. const u32 base_swizzled_offset = offset_z + offset_y + offset_x;
  67. const u32 swizzled_offset = base_swizzled_offset + table[x % GOB_SIZE_X];
  68. const u32 unswizzled_offset =
  69. slice * pitch * height + line * pitch + column * bytes_per_pixel;
  70. u8* const dst = &output[TO_LINEAR ? swizzled_offset : unswizzled_offset];
  71. const u8* const src = &input[TO_LINEAR ? unswizzled_offset : swizzled_offset];
  72. std::memcpy(dst, src, bytes_per_pixel);
  73. }
  74. }
  75. }
  76. }
  77. } // Anonymous namespace
  78. SwizzleTable MakeSwizzleTable() {
  79. return SWIZZLE_TABLE;
  80. }
  81. void UnswizzleTexture(std::span<u8> output, std::span<const u8> input, u32 bytes_per_pixel,
  82. u32 width, u32 height, u32 depth, u32 block_height, u32 block_depth,
  83. u32 stride_alignment) {
  84. Swizzle<false>(output, input, bytes_per_pixel, width, height, depth, block_height, block_depth,
  85. stride_alignment);
  86. }
  87. void SwizzleTexture(std::span<u8> output, std::span<const u8> input, u32 bytes_per_pixel, u32 width,
  88. u32 height, u32 depth, u32 block_height, u32 block_depth,
  89. u32 stride_alignment) {
  90. Swizzle<true>(output, input, bytes_per_pixel, width, height, depth, block_height, block_depth,
  91. stride_alignment);
  92. }
  93. void SwizzleSubrect(u32 subrect_width, u32 subrect_height, u32 source_pitch, u32 swizzled_width,
  94. u32 bytes_per_pixel, u8* swizzled_data, const u8* unswizzled_data,
  95. u32 block_height_bit, u32 offset_x, u32 offset_y) {
  96. const u32 block_height = 1U << block_height_bit;
  97. const u32 image_width_in_gobs =
  98. (swizzled_width * bytes_per_pixel + (GOB_SIZE_X - 1)) / GOB_SIZE_X;
  99. for (u32 line = 0; line < subrect_height; ++line) {
  100. const u32 dst_y = line + offset_y;
  101. const u32 gob_address_y =
  102. (dst_y / (GOB_SIZE_Y * block_height)) * GOB_SIZE * block_height * image_width_in_gobs +
  103. ((dst_y % (GOB_SIZE_Y * block_height)) / GOB_SIZE_Y) * GOB_SIZE;
  104. const auto& table = SWIZZLE_TABLE[dst_y % GOB_SIZE_Y];
  105. for (u32 x = 0; x < subrect_width; ++x) {
  106. const u32 dst_x = x + offset_x;
  107. const u32 gob_address =
  108. gob_address_y + (dst_x * bytes_per_pixel / GOB_SIZE_X) * GOB_SIZE * block_height;
  109. const u32 swizzled_offset = gob_address + table[(dst_x * bytes_per_pixel) % GOB_SIZE_X];
  110. const u32 unswizzled_offset = line * source_pitch + x * bytes_per_pixel;
  111. const u8* const source_line = unswizzled_data + unswizzled_offset;
  112. u8* const dest_addr = swizzled_data + swizzled_offset;
  113. std::memcpy(dest_addr, source_line, bytes_per_pixel);
  114. }
  115. }
  116. }
  117. void UnswizzleSubrect(u32 line_length_in, u32 line_count, u32 pitch, u32 width, u32 bytes_per_pixel,
  118. u32 block_height, u32 origin_x, u32 origin_y, u8* output, const u8* input) {
  119. const u32 stride = width * bytes_per_pixel;
  120. const u32 gobs_in_x = (stride + GOB_SIZE_X - 1) / GOB_SIZE_X;
  121. const u32 block_size = gobs_in_x << (GOB_SIZE_SHIFT + block_height);
  122. const u32 block_height_mask = (1U << block_height) - 1;
  123. const u32 x_shift = GOB_SIZE_SHIFT + block_height;
  124. for (u32 line = 0; line < line_count; ++line) {
  125. const u32 src_y = line + origin_y;
  126. const auto& table = SWIZZLE_TABLE[src_y % GOB_SIZE_Y];
  127. const u32 block_y = src_y >> GOB_SIZE_Y_SHIFT;
  128. const u32 src_offset_y = (block_y >> block_height) * block_size +
  129. ((block_y & block_height_mask) << GOB_SIZE_SHIFT);
  130. for (u32 column = 0; column < line_length_in; ++column) {
  131. const u32 src_x = (column + origin_x) * bytes_per_pixel;
  132. const u32 src_offset_x = (src_x >> GOB_SIZE_X_SHIFT) << x_shift;
  133. const u32 swizzled_offset = src_offset_y + src_offset_x + table[src_x % GOB_SIZE_X];
  134. const u32 unswizzled_offset = line * pitch + column * bytes_per_pixel;
  135. std::memcpy(output + unswizzled_offset, input + swizzled_offset, bytes_per_pixel);
  136. }
  137. }
  138. }
  139. void SwizzleSliceToVoxel(u32 line_length_in, u32 line_count, u32 pitch, u32 width, u32 height,
  140. u32 bytes_per_pixel, u32 block_height, u32 block_depth, u32 origin_x,
  141. u32 origin_y, u8* output, const u8* input) {
  142. UNIMPLEMENTED_IF(origin_x > 0);
  143. UNIMPLEMENTED_IF(origin_y > 0);
  144. const u32 stride = width * bytes_per_pixel;
  145. const u32 gobs_in_x = (stride + GOB_SIZE_X - 1) / GOB_SIZE_X;
  146. const u32 block_size = gobs_in_x << (GOB_SIZE_SHIFT + block_height + block_depth);
  147. const u32 block_height_mask = (1U << block_height) - 1;
  148. const u32 x_shift = static_cast<u32>(GOB_SIZE_SHIFT) + block_height + block_depth;
  149. for (u32 line = 0; line < line_count; ++line) {
  150. const auto& table = SWIZZLE_TABLE[line % GOB_SIZE_Y];
  151. const u32 block_y = line / GOB_SIZE_Y;
  152. const u32 dst_offset_y =
  153. (block_y >> block_height) * block_size + (block_y & block_height_mask) * GOB_SIZE;
  154. for (u32 x = 0; x < line_length_in; ++x) {
  155. const u32 dst_offset =
  156. ((x / GOB_SIZE_X) << x_shift) + dst_offset_y + table[x % GOB_SIZE_X];
  157. const u32 src_offset = x * bytes_per_pixel + line * pitch;
  158. std::memcpy(output + dst_offset, input + src_offset, bytes_per_pixel);
  159. }
  160. }
  161. }
  162. void SwizzleKepler(const u32 width, const u32 height, const u32 dst_x, const u32 dst_y,
  163. const u32 block_height_bit, const std::size_t copy_size, const u8* source_data,
  164. u8* swizzle_data) {
  165. const u32 block_height = 1U << block_height_bit;
  166. const u32 image_width_in_gobs{(width + GOB_SIZE_X - 1) / GOB_SIZE_X};
  167. std::size_t count = 0;
  168. for (std::size_t y = dst_y; y < height && count < copy_size; ++y) {
  169. const std::size_t gob_address_y =
  170. (y / (GOB_SIZE_Y * block_height)) * GOB_SIZE * block_height * image_width_in_gobs +
  171. ((y % (GOB_SIZE_Y * block_height)) / GOB_SIZE_Y) * GOB_SIZE;
  172. const auto& table = SWIZZLE_TABLE[y % GOB_SIZE_Y];
  173. for (std::size_t x = dst_x; x < width && count < copy_size; ++x) {
  174. const std::size_t gob_address =
  175. gob_address_y + (x / GOB_SIZE_X) * GOB_SIZE * block_height;
  176. const std::size_t swizzled_offset = gob_address + table[x % GOB_SIZE_X];
  177. const u8* source_line = source_data + count;
  178. u8* dest_addr = swizzle_data + swizzled_offset;
  179. count++;
  180. std::memcpy(dest_addr, source_line, 1);
  181. }
  182. }
  183. }
  184. std::size_t CalculateSize(bool tiled, u32 bytes_per_pixel, u32 width, u32 height, u32 depth,
  185. u32 block_height, u32 block_depth) {
  186. if (tiled) {
  187. const u32 aligned_width = Common::AlignUpLog2(width * bytes_per_pixel, GOB_SIZE_X_SHIFT);
  188. const u32 aligned_height = Common::AlignUpLog2(height, GOB_SIZE_Y_SHIFT + block_height);
  189. const u32 aligned_depth = Common::AlignUpLog2(depth, GOB_SIZE_Z_SHIFT + block_depth);
  190. return aligned_width * aligned_height * aligned_depth;
  191. } else {
  192. return width * height * depth * bytes_per_pixel;
  193. }
  194. }
  195. u64 GetGOBOffset(u32 width, u32 height, u32 dst_x, u32 dst_y, u32 block_height,
  196. u32 bytes_per_pixel) {
  197. auto div_ceil = [](const u32 x, const u32 y) { return ((x + y - 1) / y); };
  198. const u32 gobs_in_block = 1 << block_height;
  199. const u32 y_blocks = GOB_SIZE_Y << block_height;
  200. const u32 x_per_gob = GOB_SIZE_X / bytes_per_pixel;
  201. const u32 x_blocks = div_ceil(width, x_per_gob);
  202. const u32 block_size = GOB_SIZE * gobs_in_block;
  203. const u32 stride = block_size * x_blocks;
  204. const u32 base = (dst_y / y_blocks) * stride + (dst_x / x_per_gob) * block_size;
  205. const u32 relative_y = dst_y % y_blocks;
  206. return base + (relative_y / GOB_SIZE_Y) * GOB_SIZE;
  207. }
  208. } // namespace Tegra::Texture