diff options
| author | 2018-11-18 20:13:00 -0800 | |
|---|---|---|
| committer | 2018-11-18 20:13:00 -0800 | |
| commit | f02b125ac8903db5d2dad351a9c68b2a062c4467 (patch) | |
| tree | e5970b430e2f8e27be93a075ab99eaf59b31e413 | |
| parent | Merge pull request #1693 from Tinob/master (diff) | |
| parent | textures/decoders: Replace magic numbers (diff) | |
| download | yuzu-f02b125ac8903db5d2dad351a9c68b2a062c4467.tar.gz yuzu-f02b125ac8903db5d2dad351a9c68b2a062c4467.tar.xz yuzu-f02b125ac8903db5d2dad351a9c68b2a062c4467.zip | |
Merge pull request #1717 from FreddyFunk/swizzle-gob
textures/decoders: Replace magic numbers
| -rw-r--r-- | src/video_core/textures/decoders.cpp | 70 |
1 files changed, 33 insertions, 37 deletions
diff --git a/src/video_core/textures/decoders.cpp b/src/video_core/textures/decoders.cpp index c9160b467..7eabd34f1 100644 --- a/src/video_core/textures/decoders.cpp +++ b/src/video_core/textures/decoders.cpp | |||
| @@ -37,8 +37,14 @@ struct alignas(64) SwizzleTable { | |||
| 37 | std::array<std::array<u16, M>, N> values{}; | 37 | std::array<std::array<u16, M>, N> values{}; |
| 38 | }; | 38 | }; |
| 39 | 39 | ||
| 40 | constexpr auto legacy_swizzle_table = SwizzleTable<8, 64, 1>(); | 40 | constexpr u32 gob_size_x = 64; |
| 41 | constexpr auto fast_swizzle_table = SwizzleTable<8, 4, 16>(); | 41 | constexpr u32 gob_size_y = 8; |
| 42 | constexpr u32 gob_size_z = 1; | ||
| 43 | constexpr u32 gob_size = gob_size_x * gob_size_y * gob_size_z; | ||
| 44 | constexpr u32 fast_swizzle_align = 16; | ||
| 45 | |||
| 46 | constexpr auto legacy_swizzle_table = SwizzleTable<gob_size_y, gob_size_x, gob_size_z>(); | ||
| 47 | constexpr auto fast_swizzle_table = SwizzleTable<gob_size_y, 4, fast_swizzle_align>(); | ||
| 42 | 48 | ||
| 43 | /** | 49 | /** |
| 44 | * This function manages ALL the GOBs(Group of Bytes) Inside a single block. | 50 | * This function manages ALL the GOBs(Group of Bytes) Inside a single block. |
| @@ -52,10 +58,7 @@ void PreciseProcessBlock(u8* const swizzled_data, u8* const unswizzled_data, con | |||
| 52 | const u32 bytes_per_pixel, const u32 out_bytes_per_pixel) { | 58 | const u32 bytes_per_pixel, const u32 out_bytes_per_pixel) { |
| 53 | std::array<u8*, 2> data_ptrs; | 59 | std::array<u8*, 2> data_ptrs; |
| 54 | u32 z_address = tile_offset; | 60 | u32 z_address = tile_offset; |
| 55 | const u32 gob_size_x = 64; | 61 | |
| 56 | const u32 gob_size_y = 8; | ||
| 57 | const u32 gob_size_z = 1; | ||
| 58 | const u32 gob_size = gob_size_x * gob_size_y * gob_size_z; | ||
| 59 | for (u32 z = z_start; z < z_end; z++) { | 62 | for (u32 z = z_start; z < z_end; z++) { |
| 60 | u32 y_address = z_address; | 63 | u32 y_address = z_address; |
| 61 | u32 pixel_base = layer_z * z + y_start * stride_x; | 64 | u32 pixel_base = layer_z * z + y_start * stride_x; |
| @@ -90,23 +93,19 @@ void FastProcessBlock(u8* const swizzled_data, u8* const unswizzled_data, const | |||
| 90 | u32 z_address = tile_offset; | 93 | u32 z_address = tile_offset; |
| 91 | const u32 x_startb = x_start * bytes_per_pixel; | 94 | const u32 x_startb = x_start * bytes_per_pixel; |
| 92 | const u32 x_endb = x_end * bytes_per_pixel; | 95 | const u32 x_endb = x_end * bytes_per_pixel; |
| 93 | constexpr u32 copy_size = 16; | 96 | |
| 94 | constexpr u32 gob_size_x = 64; | ||
| 95 | constexpr u32 gob_size_y = 8; | ||
| 96 | constexpr u32 gob_size_z = 1; | ||
| 97 | const u32 gob_size = gob_size_x * gob_size_y * gob_size_z; | ||
| 98 | for (u32 z = z_start; z < z_end; z++) { | 97 | for (u32 z = z_start; z < z_end; z++) { |
| 99 | u32 y_address = z_address; | 98 | u32 y_address = z_address; |
| 100 | u32 pixel_base = layer_z * z + y_start * stride_x; | 99 | u32 pixel_base = layer_z * z + y_start * stride_x; |
| 101 | for (u32 y = y_start; y < y_end; y++) { | 100 | for (u32 y = y_start; y < y_end; y++) { |
| 102 | const auto& table = fast_swizzle_table[y % gob_size_y]; | 101 | const auto& table = fast_swizzle_table[y % gob_size_y]; |
| 103 | for (u32 xb = x_startb; xb < x_endb; xb += copy_size) { | 102 | for (u32 xb = x_startb; xb < x_endb; xb += fast_swizzle_align) { |
| 104 | const u32 swizzle_offset{y_address + table[(xb / copy_size) % 4]}; | 103 | const u32 swizzle_offset{y_address + table[(xb / fast_swizzle_align) % 4]}; |
| 105 | const u32 out_x = xb * out_bytes_per_pixel / bytes_per_pixel; | 104 | const u32 out_x = xb * out_bytes_per_pixel / bytes_per_pixel; |
| 106 | const u32 pixel_index{out_x + pixel_base}; | 105 | const u32 pixel_index{out_x + pixel_base}; |
| 107 | data_ptrs[unswizzle] = swizzled_data + swizzle_offset; | 106 | data_ptrs[unswizzle] = swizzled_data + swizzle_offset; |
| 108 | data_ptrs[!unswizzle] = unswizzled_data + pixel_index; | 107 | data_ptrs[!unswizzle] = unswizzled_data + pixel_index; |
| 109 | std::memcpy(data_ptrs[0], data_ptrs[1], copy_size); | 108 | std::memcpy(data_ptrs[0], data_ptrs[1], fast_swizzle_align); |
| 110 | } | 109 | } |
| 111 | pixel_base += stride_x; | 110 | pixel_base += stride_x; |
| 112 | if ((y + 1) % gob_size_y == 0) | 111 | if ((y + 1) % gob_size_y == 0) |
| @@ -132,17 +131,15 @@ void SwizzledData(u8* const swizzled_data, u8* const unswizzled_data, const bool | |||
| 132 | auto div_ceil = [](const u32 x, const u32 y) { return ((x + y - 1) / y); }; | 131 | auto div_ceil = [](const u32 x, const u32 y) { return ((x + y - 1) / y); }; |
| 133 | const u32 stride_x = width * out_bytes_per_pixel; | 132 | const u32 stride_x = width * out_bytes_per_pixel; |
| 134 | const u32 layer_z = height * stride_x; | 133 | const u32 layer_z = height * stride_x; |
| 135 | constexpr u32 gob_x_bytes = 64; | 134 | const u32 gob_elements_x = gob_size_x / bytes_per_pixel; |
| 136 | const u32 gob_elements_x = gob_x_bytes / bytes_per_pixel; | 135 | constexpr u32 gob_elements_y = gob_size_y; |
| 137 | constexpr u32 gob_elements_y = 8; | 136 | constexpr u32 gob_elements_z = gob_size_z; |
| 138 | constexpr u32 gob_elements_z = 1; | ||
| 139 | const u32 block_x_elements = gob_elements_x; | 137 | const u32 block_x_elements = gob_elements_x; |
| 140 | const u32 block_y_elements = gob_elements_y * block_height; | 138 | const u32 block_y_elements = gob_elements_y * block_height; |
| 141 | const u32 block_z_elements = gob_elements_z * block_depth; | 139 | const u32 block_z_elements = gob_elements_z * block_depth; |
| 142 | const u32 blocks_on_x = div_ceil(width, block_x_elements); | 140 | const u32 blocks_on_x = div_ceil(width, block_x_elements); |
| 143 | const u32 blocks_on_y = div_ceil(height, block_y_elements); | 141 | const u32 blocks_on_y = div_ceil(height, block_y_elements); |
| 144 | const u32 blocks_on_z = div_ceil(depth, block_z_elements); | 142 | const u32 blocks_on_z = div_ceil(depth, block_z_elements); |
| 145 | constexpr u32 gob_size = gob_x_bytes * gob_elements_y * gob_elements_z; | ||
| 146 | const u32 xy_block_size = gob_size * block_height; | 143 | const u32 xy_block_size = gob_size * block_height; |
| 147 | const u32 block_size = xy_block_size * block_depth; | 144 | const u32 block_size = xy_block_size * block_depth; |
| 148 | u32 tile_offset = 0; | 145 | u32 tile_offset = 0; |
| @@ -173,7 +170,7 @@ void SwizzledData(u8* const swizzled_data, u8* const unswizzled_data, const bool | |||
| 173 | void CopySwizzledData(u32 width, u32 height, u32 depth, u32 bytes_per_pixel, | 170 | void CopySwizzledData(u32 width, u32 height, u32 depth, u32 bytes_per_pixel, |
| 174 | u32 out_bytes_per_pixel, u8* const swizzled_data, u8* const unswizzled_data, | 171 | u32 out_bytes_per_pixel, u8* const swizzled_data, u8* const unswizzled_data, |
| 175 | bool unswizzle, u32 block_height, u32 block_depth) { | 172 | bool unswizzle, u32 block_height, u32 block_depth) { |
| 176 | if (bytes_per_pixel % 3 != 0 && (width * bytes_per_pixel) % 16 == 0) { | 173 | if (bytes_per_pixel % 3 != 0 && (width * bytes_per_pixel) % fast_swizzle_align == 0) { |
| 177 | SwizzledData<true>(swizzled_data, unswizzled_data, unswizzle, width, height, depth, | 174 | SwizzledData<true>(swizzled_data, unswizzled_data, unswizzle, width, height, depth, |
| 178 | bytes_per_pixel, out_bytes_per_pixel, block_height, block_depth); | 175 | bytes_per_pixel, out_bytes_per_pixel, block_height, block_depth); |
| 179 | } else { | 176 | } else { |
| @@ -250,15 +247,17 @@ std::vector<u8> UnswizzleTexture(VAddr address, u32 tile_size_x, u32 tile_size_y | |||
| 250 | void SwizzleSubrect(u32 subrect_width, u32 subrect_height, u32 source_pitch, u32 swizzled_width, | 247 | void SwizzleSubrect(u32 subrect_width, u32 subrect_height, u32 source_pitch, u32 swizzled_width, |
| 251 | u32 bytes_per_pixel, VAddr swizzled_data, VAddr unswizzled_data, | 248 | u32 bytes_per_pixel, VAddr swizzled_data, VAddr unswizzled_data, |
| 252 | u32 block_height) { | 249 | u32 block_height) { |
| 253 | const u32 image_width_in_gobs{(swizzled_width * bytes_per_pixel + 63) / 64}; | 250 | const u32 image_width_in_gobs{(swizzled_width * bytes_per_pixel + (gob_size_x - 1)) / |
| 251 | gob_size_x}; | ||
| 254 | for (u32 line = 0; line < subrect_height; ++line) { | 252 | for (u32 line = 0; line < subrect_height; ++line) { |
| 255 | const u32 gob_address_y = | 253 | const u32 gob_address_y = |
| 256 | (line / (8 * block_height)) * 512 * block_height * image_width_in_gobs + | 254 | (line / (gob_size_y * block_height)) * gob_size * block_height * image_width_in_gobs + |
| 257 | (line % (8 * block_height) / 8) * 512; | 255 | ((line % (gob_size_y * block_height)) / gob_size_y) * gob_size; |
| 258 | const auto& table = legacy_swizzle_table[line % 8]; | 256 | const auto& table = legacy_swizzle_table[line % gob_size_y]; |
| 259 | for (u32 x = 0; x < subrect_width; ++x) { | 257 | for (u32 x = 0; x < subrect_width; ++x) { |
| 260 | const u32 gob_address = gob_address_y + (x * bytes_per_pixel / 64) * 512 * block_height; | 258 | const u32 gob_address = |
| 261 | const u32 swizzled_offset = gob_address + table[(x * bytes_per_pixel) % 64]; | 259 | gob_address_y + (x * bytes_per_pixel / gob_size_x) * gob_size * block_height; |
| 260 | const u32 swizzled_offset = gob_address + table[(x * bytes_per_pixel) % gob_size_x]; | ||
| 262 | const VAddr source_line = unswizzled_data + line * source_pitch + x * bytes_per_pixel; | 261 | const VAddr source_line = unswizzled_data + line * source_pitch + x * bytes_per_pixel; |
| 263 | const VAddr dest_addr = swizzled_data + swizzled_offset; | 262 | const VAddr dest_addr = swizzled_data + swizzled_offset; |
| 264 | 263 | ||
| @@ -272,13 +271,13 @@ void UnswizzleSubrect(u32 subrect_width, u32 subrect_height, u32 dest_pitch, u32 | |||
| 272 | u32 block_height, u32 offset_x, u32 offset_y) { | 271 | u32 block_height, u32 offset_x, u32 offset_y) { |
| 273 | for (u32 line = 0; line < subrect_height; ++line) { | 272 | for (u32 line = 0; line < subrect_height; ++line) { |
| 274 | const u32 y2 = line + offset_y; | 273 | const u32 y2 = line + offset_y; |
| 275 | const u32 gob_address_y = | 274 | const u32 gob_address_y = (y2 / (gob_size_y * block_height)) * gob_size * block_height + |
| 276 | (y2 / (8 * block_height)) * 512 * block_height + (y2 % (8 * block_height) / 8) * 512; | 275 | ((y2 % (gob_size_y * block_height)) / gob_size_y) * gob_size; |
| 277 | const auto& table = legacy_swizzle_table[y2 % 8]; | 276 | const auto& table = legacy_swizzle_table[y2 % gob_size_y]; |
| 278 | for (u32 x = 0; x < subrect_width; ++x) { | 277 | for (u32 x = 0; x < subrect_width; ++x) { |
| 279 | const u32 x2 = (x + offset_x) * bytes_per_pixel; | 278 | const u32 x2 = (x + offset_x) * bytes_per_pixel; |
| 280 | const u32 gob_address = gob_address_y + (x2 / 64) * 512 * block_height; | 279 | const u32 gob_address = gob_address_y + (x2 / gob_size_x) * gob_size * block_height; |
| 281 | const u32 swizzled_offset = gob_address + table[x2 % 64]; | 280 | const u32 swizzled_offset = gob_address + table[x2 % gob_size_x]; |
| 282 | const VAddr dest_line = unswizzled_data + line * dest_pitch + x * bytes_per_pixel; | 281 | const VAddr dest_line = unswizzled_data + line * dest_pitch + x * bytes_per_pixel; |
| 283 | const VAddr source_addr = swizzled_data + swizzled_offset; | 282 | const VAddr source_addr = swizzled_data + swizzled_offset; |
| 284 | 283 | ||
| @@ -332,12 +331,9 @@ std::vector<u8> DecodeTexture(const std::vector<u8>& texture_data, TextureFormat | |||
| 332 | std::size_t CalculateSize(bool tiled, u32 bytes_per_pixel, u32 width, u32 height, u32 depth, | 331 | std::size_t CalculateSize(bool tiled, u32 bytes_per_pixel, u32 width, u32 height, u32 depth, |
| 333 | u32 block_height, u32 block_depth) { | 332 | u32 block_height, u32 block_depth) { |
| 334 | if (tiled) { | 333 | if (tiled) { |
| 335 | constexpr u32 gobs_in_x = 64; | 334 | const u32 aligned_width = Common::AlignUp(width * bytes_per_pixel, gob_size_x); |
| 336 | constexpr u32 gobs_in_y = 8; | 335 | const u32 aligned_height = Common::AlignUp(height, gob_size_y * block_height); |
| 337 | constexpr u32 gobs_in_z = 1; | 336 | const u32 aligned_depth = Common::AlignUp(depth, gob_size_z * block_depth); |
| 338 | const u32 aligned_width = Common::AlignUp(width * bytes_per_pixel, gobs_in_x); | ||
| 339 | const u32 aligned_height = Common::AlignUp(height, gobs_in_y * block_height); | ||
| 340 | const u32 aligned_depth = Common::AlignUp(depth, gobs_in_z * block_depth); | ||
| 341 | return aligned_width * aligned_height * aligned_depth; | 337 | return aligned_width * aligned_height * aligned_depth; |
| 342 | } else { | 338 | } else { |
| 343 | return width * height * depth * bytes_per_pixel; | 339 | return width * height * depth * bytes_per_pixel; |