// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project // SPDX-License-Identifier: GPL-3.0-or-later // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project // SPDX-License-Identifier: GPL-2.0-or-later #include #include #include #include #include #include "common/alignment.h" #include "common/assert.h" #include "common/bit_util.h" #include "common/div_ceil.h" #include "video_core/gpu.h" #include "video_core/textures/decoders.h" namespace Tegra::Texture { namespace { template constexpr u32 pdep(u32 value) { u32 result = 0; u32 m = mask; for (u32 bit = 1; m; bit += bit) { if (value & bit) result |= m & (~m + 1); m &= m - 1; } return result; } constexpr u32 SWIZZLE_RUN_BYTES = 16; constexpr u32 SWIZZLE_RUN_SHIFT = 4; constexpr u32 SWIZZLE_RUN_MASK = SWIZZLE_RUN_BYTES - 1; constexpr u32 SWIZZLE_RUN_INDEX_MASK = GOB_SIZE_X / SWIZZLE_RUN_BYTES - 1; static_assert((SWIZZLE_X_BITS & SWIZZLE_RUN_MASK) == SWIZZLE_RUN_MASK, "A swizzled run is only contiguous while the low bits of X map to themselves"); constexpr std::array SWIZZLE_X_RUN_TABLE = [] { std::array table{}; for (u32 index = 0; index < static_cast(table.size()); ++index) { table[index] = pdep(index << SWIZZLE_RUN_SHIFT); } return table; }(); template void incrpdep(u32& value) { static constexpr u32 swizzled_incr = pdep(incr_amount); value = ((value | ~mask) + swizzled_incr) & mask; } template void SwizzleRow(std::span output, std::span input, u32 offset_zy, u32 swizzled_y, u32 x_shift, u32 x, u32 num_bytes, u32 linear) { const auto copy = [&](u32 swizzled_x, u32 count) { const u32 swizzled = offset_zy + ((x >> GOB_SIZE_X_SHIFT) << x_shift) + (swizzled_x | swizzled_y); u8* const dst = &output[TO_LINEAR ? swizzled : linear]; const u8* const src = &input[TO_LINEAR ? linear : swizzled]; std::memcpy(dst, src, count); x += count; linear += count; }; u32 swizzled_run = SWIZZLE_X_RUN_TABLE[(x >> SWIZZLE_RUN_SHIFT) & SWIZZLE_RUN_INDEX_MASK]; u32 remaining = num_bytes; const u32 head = (std::min)(SWIZZLE_RUN_BYTES - (x & SWIZZLE_RUN_MASK), remaining) & SWIZZLE_RUN_MASK; if (head != 0) { copy(swizzled_run | (x & SWIZZLE_RUN_MASK), head); remaining -= head; incrpdep(swizzled_run); } while (remaining >= SWIZZLE_RUN_BYTES) { copy(swizzled_run, SWIZZLE_RUN_BYTES); remaining -= SWIZZLE_RUN_BYTES; incrpdep(swizzled_run); } if (remaining != 0) { copy(swizzled_run, remaining); } } template void SwizzleImpl(std::span output, std::span input, u32 width, u32 height, u32 depth, u32 block_height, u32 block_depth, u32 stride) { // The origin of the transformation can be configured here, leave it as zero as the current API // doesn't expose it. static constexpr u32 origin_x = 0; static constexpr u32 origin_y = 0; static constexpr u32 origin_z = 0; // We can configure here a custom pitch // As it's not exposed 'width * BYTES_PER_PIXEL' will be the expected pitch. const u32 pitch = width * BYTES_PER_PIXEL; const u32 gobs_in_x = Common::DivCeilLog2(stride, GOB_SIZE_X_SHIFT); const u32 block_size = gobs_in_x << (GOB_SIZE_SHIFT + block_height + block_depth); const u32 slice_size = Common::DivCeilLog2(height, block_height + GOB_SIZE_Y_SHIFT) * block_size; const u32 block_height_mask = (1U << block_height) - 1; const u32 block_depth_mask = (1U << block_depth) - 1; const u32 x_shift = GOB_SIZE_SHIFT + block_height + block_depth; for (u32 slice = 0; slice < depth; ++slice) { const u32 z = slice + origin_z; const u32 offset_z = (z >> block_depth) * slice_size + ((z & block_depth_mask) << (GOB_SIZE_SHIFT + block_height)); for (u32 line = 0; line < height; ++line) { const u32 y = line + origin_y; const u32 swizzled_y = pdep(y); const u32 block_y = y >> GOB_SIZE_Y_SHIFT; const u32 offset_y = (block_y >> block_height) * block_size + ((block_y & block_height_mask) << GOB_SIZE_SHIFT); SwizzleRow(output, input, offset_z + offset_y, swizzled_y, x_shift, origin_x * BYTES_PER_PIXEL, width * BYTES_PER_PIXEL, slice * pitch * height + line * pitch); } } } template void SwizzleSubrectImpl(std::span output, std::span input, u32 width, u32 height, u32 depth, u32 origin_x, u32 origin_y, u32 extent_x, u32 num_lines, u32 block_height, u32 block_depth, u32 pitch_linear) { // The origin of the transformation can be configured here, leave it as zero as the current API // doesn't expose it. static constexpr u32 origin_z = 0; // We can configure here a custom pitch // As it's not exposed 'width * BYTES_PER_PIXEL' will be the expected pitch. const u32 pitch = pitch_linear; const u32 stride = Common::AlignUpLog2(width * BYTES_PER_PIXEL, GOB_SIZE_X_SHIFT); const u32 gobs_in_x = Common::DivCeilLog2(stride, GOB_SIZE_X_SHIFT); const u32 block_size = gobs_in_x << (GOB_SIZE_SHIFT + block_height + block_depth); const u32 slice_size = Common::DivCeilLog2(height, block_height + GOB_SIZE_Y_SHIFT) * block_size; const u32 block_height_mask = (1U << block_height) - 1; const u32 block_depth_mask = (1U << block_depth) - 1; const u32 x_shift = GOB_SIZE_SHIFT + block_height + block_depth; u32 unprocessed_lines = num_lines; u32 extent_y = (std::min)(num_lines, height - origin_y); for (u32 slice = 0; slice < depth; ++slice) { const u32 z = slice + origin_z; const u32 offset_z = (z >> block_depth) * slice_size + ((z & block_depth_mask) << (GOB_SIZE_SHIFT + block_height)); const u32 lines_in_y = (std::min)(unprocessed_lines, extent_y); for (u32 line = 0; line < lines_in_y; ++line) { const u32 y = line + origin_y; const u32 swizzled_y = pdep(y); const u32 block_y = y >> GOB_SIZE_Y_SHIFT; const u32 offset_y = (block_y >> block_height) * block_size + ((block_y & block_height_mask) << GOB_SIZE_SHIFT); SwizzleRow(output, input, offset_z + offset_y, swizzled_y, x_shift, origin_x * BYTES_PER_PIXEL, extent_x * BYTES_PER_PIXEL, slice * pitch * height + line * pitch); } unprocessed_lines -= lines_in_y; if (unprocessed_lines == 0) { return; } } } template void Swizzle(std::span output, std::span input, u32 bytes_per_pixel, u32 width, u32 height, u32 depth, u32 block_height, u32 block_depth, u32 stride_alignment) { switch (bytes_per_pixel) { #define BPP_CASE(x) \ case x: \ return SwizzleImpl(output, input, width, height, depth, block_height, \ block_depth, stride_alignment); BPP_CASE(1) BPP_CASE(2) BPP_CASE(3) BPP_CASE(4) BPP_CASE(6) BPP_CASE(8) BPP_CASE(12) BPP_CASE(16) #undef BPP_CASE default: ASSERT_MSG(false, "Invalid bytes_per_pixel={}", bytes_per_pixel); break; } } } // Anonymous namespace void UnswizzleTexture(std::span output, std::span input, u32 bytes_per_pixel, u32 width, u32 height, u32 depth, u32 block_height, u32 block_depth, u32 stride_alignment) { const u32 stride = Common::AlignUpLog2(width, stride_alignment) * bytes_per_pixel; const u32 new_bpp = (std::min)(4U, static_cast(std::countr_zero(width * bytes_per_pixel))); width = (width * bytes_per_pixel) >> new_bpp; bytes_per_pixel = 1U << new_bpp; Swizzle(output, input, bytes_per_pixel, width, height, depth, block_height, block_depth, stride); } void SwizzleTexture(std::span output, std::span input, u32 bytes_per_pixel, u32 width, u32 height, u32 depth, u32 block_height, u32 block_depth, u32 stride_alignment) { const u32 stride = Common::AlignUpLog2(width, stride_alignment) * bytes_per_pixel; const u32 new_bpp = (std::min)(4U, static_cast(std::countr_zero(width * bytes_per_pixel))); width = (width * bytes_per_pixel) >> new_bpp; bytes_per_pixel = 1U << new_bpp; Swizzle(output, input, bytes_per_pixel, width, height, depth, block_height, block_depth, stride); } void SwizzleSubrect(std::span output, std::span input, u32 bytes_per_pixel, u32 width, u32 height, u32 depth, u32 origin_x, u32 origin_y, u32 extent_x, u32 extent_y, u32 block_height, u32 block_depth, u32 pitch_linear) { switch (bytes_per_pixel) { #define BPP_CASE(x) \ case x: \ return SwizzleSubrectImpl(output, input, width, height, depth, origin_x, \ origin_y, extent_x, extent_y, block_height, \ block_depth, pitch_linear); BPP_CASE(1) BPP_CASE(2) BPP_CASE(3) BPP_CASE(4) BPP_CASE(6) BPP_CASE(8) BPP_CASE(12) BPP_CASE(16) #undef BPP_CASE default: ASSERT_MSG(false, "Invalid bytes_per_pixel={}", bytes_per_pixel); break; } } void UnswizzleSubrect(std::span output, std::span input, u32 bytes_per_pixel, u32 width, u32 height, u32 depth, u32 origin_x, u32 origin_y, u32 extent_x, u32 extent_y, u32 block_height, u32 block_depth, u32 pitch_linear) { switch (bytes_per_pixel) { #define BPP_CASE(x) \ case x: \ return SwizzleSubrectImpl(output, input, width, height, depth, origin_x, \ origin_y, extent_x, extent_y, block_height, \ block_depth, pitch_linear); BPP_CASE(1) BPP_CASE(2) BPP_CASE(3) BPP_CASE(4) BPP_CASE(6) BPP_CASE(8) BPP_CASE(12) BPP_CASE(16) #undef BPP_CASE default: ASSERT_MSG(false, "Invalid bytes_per_pixel={}", bytes_per_pixel); break; } } std::size_t CalculateSize(bool tiled, u32 bytes_per_pixel, u32 width, u32 height, u32 depth, u32 block_height, u32 block_depth) { if (tiled) { const u32 aligned_width = Common::AlignUpLog2(width * bytes_per_pixel, GOB_SIZE_X_SHIFT); const u32 aligned_height = Common::AlignUpLog2(height, GOB_SIZE_Y_SHIFT + block_height); const u32 aligned_depth = Common::AlignUpLog2(depth, GOB_SIZE_Z_SHIFT + block_depth); return aligned_width * aligned_height * aligned_depth; } else { return width * height * depth * bytes_per_pixel; } } u64 GetGOBOffset(u32 width, u32 height, u32 dst_x, u32 dst_y, u32 block_height, u32 bytes_per_pixel) { auto div_ceil = [](const u32 x, const u32 y) { return ((x + y - 1) / y); }; const u32 gobs_in_block = 1 << block_height; const u32 y_blocks = GOB_SIZE_Y << block_height; const u32 x_per_gob = GOB_SIZE_X / bytes_per_pixel; const u32 x_blocks = div_ceil(width, x_per_gob); const u32 block_size = GOB_SIZE * gobs_in_block; const u32 stride = block_size * x_blocks; const u32 base = (dst_y / y_blocks) * stride + (dst_x / x_per_gob) * block_size; const u32 relative_y = dst_y % y_blocks; return base + (relative_y / GOB_SIZE_Y) * GOB_SIZE; } } // namespace Tegra::Texture