2025-09-21 21:58:59 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2025 Eden Emulator Project
|
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
2022-04-23 04:59:50 -04:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
|
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2018-03-19 18:00:29 -05:00
|
|
|
|
2020-12-30 02:25:23 -03:00
|
|
|
#include <array>
|
2018-07-18 00:15:20 -04:00
|
|
|
#include <cmath>
|
2018-03-22 15:17:10 -05:00
|
|
|
#include <cstring>
|
2020-12-30 02:25:23 -03:00
|
|
|
#include <span>
|
|
|
|
|
|
2018-10-12 14:21:53 -04:00
|
|
|
#include "common/alignment.h"
|
2018-03-22 15:17:10 -05:00
|
|
|
#include "common/assert.h"
|
2020-07-04 18:42:10 -03:00
|
|
|
#include "common/bit_util.h"
|
2020-12-30 02:25:23 -03:00
|
|
|
#include "common/div_ceil.h"
|
2018-07-02 12:42:04 -05:00
|
|
|
#include "video_core/gpu.h"
|
2018-03-19 18:00:29 -05:00
|
|
|
#include "video_core/textures/decoders.h"
|
|
|
|
|
|
2018-07-20 18:14:17 -04:00
|
|
|
namespace Tegra::Texture {
|
2020-12-30 02:25:23 -03:00
|
|
|
namespace {
|
2026-01-10 12:27:57 +01:00
|
|
|
constexpr u32 pdep(u32 mask, u32 value) {
|
2022-08-09 00:04:45 +01:00
|
|
|
u32 result = 0;
|
|
|
|
|
u32 m = mask;
|
|
|
|
|
for (u32 bit = 1; m; bit += bit) {
|
|
|
|
|
if (value & bit)
|
2022-10-21 02:34:06 -04:00
|
|
|
result |= m & (~m + 1);
|
2022-08-09 00:04:45 +01:00
|
|
|
m &= m - 1;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-10 12:27:57 +01:00
|
|
|
void incrpdep(u32 mask, u32 incr_amount, u32& value) {
|
|
|
|
|
u32 swizzled_incr = pdep(mask, incr_amount);
|
2022-08-09 00:04:45 +01:00
|
|
|
value = ((value | ~mask) + swizzled_incr) & mask;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-10 12:27:57 +01:00
|
|
|
void SwizzleImpl(bool to_linear, u32 bytes_per_pixel, std::span<u8> output, std::span<const u8> input, u32 width, u32 height, u32 depth, u32 block_height, u32 block_depth, u32 stride) {
|
2020-12-30 02:25:23 -03:00
|
|
|
// 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;
|
2018-03-22 15:17:10 -05:00
|
|
|
|
2020-12-30 02:25:23 -03:00
|
|
|
// We can configure here a custom pitch
|
2026-01-10 12:27:57 +01:00
|
|
|
// As it's not exposed 'width * bytes_per_pixel' will be the expected pitch.
|
|
|
|
|
const u32 pitch = width * bytes_per_pixel;
|
2018-10-11 14:33:50 -04:00
|
|
|
|
2020-12-30 02:25:23 -03:00
|
|
|
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);
|
2026-01-10 12:27:57 +01:00
|
|
|
const u32 slice_size = Common::DivCeilLog2(height, block_height + GOB_SIZE_Y_SHIFT) * block_size;
|
2018-10-10 22:03:38 -04:00
|
|
|
|
2020-12-30 02:25:23 -03:00
|
|
|
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;
|
2026-01-10 12:27:57 +01:00
|
|
|
const u32 swizzled_y = pdep(SWIZZLE_Y_BITS, y);
|
2020-12-30 02:25:23 -03:00
|
|
|
|
|
|
|
|
const u32 block_y = y >> GOB_SIZE_Y_SHIFT;
|
2026-01-10 12:27:57 +01:00
|
|
|
const u32 offset_y = (block_y >> block_height) * block_size + ((block_y & block_height_mask) << GOB_SIZE_SHIFT);
|
2020-12-30 02:25:23 -03:00
|
|
|
|
2026-01-10 12:27:57 +01:00
|
|
|
u32 swizzled_x = pdep(SWIZZLE_X_BITS, origin_x * bytes_per_pixel);
|
|
|
|
|
for (u32 column = 0; column < width; ++column, incrpdep(SWIZZLE_X_BITS, bytes_per_pixel, swizzled_x)) {
|
|
|
|
|
const u32 x = (column + origin_x) * bytes_per_pixel;
|
2020-12-30 02:25:23 -03:00
|
|
|
const u32 offset_x = (x >> GOB_SIZE_X_SHIFT) << x_shift;
|
|
|
|
|
|
|
|
|
|
const u32 base_swizzled_offset = offset_z + offset_y + offset_x;
|
2022-08-09 00:04:45 +01:00
|
|
|
const u32 swizzled_offset = base_swizzled_offset + (swizzled_x | swizzled_y);
|
2020-12-30 02:25:23 -03:00
|
|
|
|
2026-01-10 12:27:57 +01:00
|
|
|
const u32 unswizzled_offset = slice * pitch * height + line * pitch + column * bytes_per_pixel;
|
|
|
|
|
u8* const dst = &output[to_linear ? swizzled_offset : unswizzled_offset];
|
|
|
|
|
const u8* const src = &input[to_linear ? unswizzled_offset : swizzled_offset];
|
2020-12-30 02:25:23 -03:00
|
|
|
|
2026-01-10 12:27:57 +01:00
|
|
|
std::memcpy(dst, src, bytes_per_pixel);
|
2018-10-10 22:03:38 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-02 15:18:58 +00:00
|
|
|
|
2026-01-10 12:27:57 +01:00
|
|
|
void SwizzleSubrectImpl(bool to_linear, u32 bytes_per_pixel, std::span<u8> output, std::span<const u8> 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) {
|
2022-08-14 02:36:36 -07:00
|
|
|
// 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
|
2026-01-10 12:27:57 +01:00
|
|
|
// As it's not exposed 'width * bytes_per_pixel' will be the expected pitch.
|
2022-08-14 02:36:36 -07:00
|
|
|
const u32 pitch = pitch_linear;
|
2026-01-10 12:27:57 +01:00
|
|
|
const u32 stride = Common::AlignUpLog2(width * bytes_per_pixel, GOB_SIZE_X_SHIFT);
|
2022-08-14 02:36:36 -07:00
|
|
|
|
|
|
|
|
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);
|
2026-01-10 12:27:57 +01:00
|
|
|
const u32 slice_size = Common::DivCeilLog2(height, block_height + GOB_SIZE_Y_SHIFT) * block_size;
|
2022-08-14 02:36:36 -07:00
|
|
|
|
|
|
|
|
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;
|
2025-09-09 20:47:49 +02:00
|
|
|
u32 extent_y = (std::min)(num_lines, height - origin_y);
|
2022-08-14 02:36:36 -07:00
|
|
|
|
|
|
|
|
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));
|
2025-09-09 20:47:49 +02:00
|
|
|
const u32 lines_in_y = (std::min)(unprocessed_lines, extent_y);
|
2022-08-14 02:36:36 -07:00
|
|
|
for (u32 line = 0; line < lines_in_y; ++line) {
|
|
|
|
|
const u32 y = line + origin_y;
|
2026-01-10 12:27:57 +01:00
|
|
|
const u32 swizzled_y = pdep(SWIZZLE_Y_BITS, y);
|
2022-08-14 02:36:36 -07:00
|
|
|
|
|
|
|
|
const u32 block_y = y >> GOB_SIZE_Y_SHIFT;
|
2026-01-10 12:27:57 +01:00
|
|
|
const u32 offset_y = (block_y >> block_height) * block_size + ((block_y & block_height_mask) << GOB_SIZE_SHIFT);
|
2022-08-14 02:36:36 -07:00
|
|
|
|
2026-01-10 12:27:57 +01:00
|
|
|
u32 swizzled_x = pdep(SWIZZLE_X_BITS, origin_x * bytes_per_pixel);
|
|
|
|
|
for (u32 column = 0; column < extent_x; ++column, incrpdep(SWIZZLE_X_BITS, bytes_per_pixel, swizzled_x)) {
|
|
|
|
|
const u32 x = (column + origin_x) * bytes_per_pixel;
|
2022-08-14 02:36:36 -07:00
|
|
|
const u32 offset_x = (x >> GOB_SIZE_X_SHIFT) << x_shift;
|
|
|
|
|
|
|
|
|
|
const u32 base_swizzled_offset = offset_z + offset_y + offset_x;
|
|
|
|
|
const u32 swizzled_offset = base_swizzled_offset + (swizzled_x | swizzled_y);
|
|
|
|
|
|
2026-01-10 12:27:57 +01:00
|
|
|
const u32 unswizzled_offset = slice * pitch * height + line * pitch + column * bytes_per_pixel;
|
2022-08-14 02:36:36 -07:00
|
|
|
|
2026-01-10 12:27:57 +01:00
|
|
|
u8* const dst = &output[to_linear ? swizzled_offset : unswizzled_offset];
|
|
|
|
|
const u8* const src = &input[to_linear ? unswizzled_offset : swizzled_offset];
|
2022-08-14 02:36:36 -07:00
|
|
|
|
2026-01-10 12:27:57 +01:00
|
|
|
std::memcpy(dst, src, bytes_per_pixel);
|
2022-08-14 02:36:36 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
unprocessed_lines -= lines_in_y;
|
|
|
|
|
if (unprocessed_lines == 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-10 12:27:57 +01:00
|
|
|
void Swizzle(bool to_linear, std::span<u8> output, std::span<const u8> input, u32 bytes_per_pixel, u32 width, u32 height, u32 depth, u32 block_height, u32 block_depth, u32 stride_alignment) {
|
2021-08-02 15:18:58 +00:00
|
|
|
switch (bytes_per_pixel) {
|
2026-01-10 12:27:57 +01:00
|
|
|
case 1:
|
|
|
|
|
case 2:
|
|
|
|
|
case 3:
|
|
|
|
|
case 4:
|
|
|
|
|
case 6:
|
|
|
|
|
case 8:
|
|
|
|
|
case 12:
|
|
|
|
|
case 16:
|
|
|
|
|
return SwizzleImpl(to_linear, bytes_per_pixel, output, input, width, height, depth, block_height, block_depth, stride_alignment);
|
2021-08-02 15:18:58 +00:00
|
|
|
default:
|
2022-06-07 17:02:29 -04:00
|
|
|
ASSERT_MSG(false, "Invalid bytes_per_pixel={}", bytes_per_pixel);
|
2022-11-11 04:12:37 -08:00
|
|
|
break;
|
2021-08-02 15:18:58 +00:00
|
|
|
}
|
|
|
|
|
}
|
2018-03-22 15:17:10 -05:00
|
|
|
|
2021-08-12 04:45:25 +00:00
|
|
|
} // Anonymous namespace
|
|
|
|
|
|
2026-01-10 12:27:57 +01:00
|
|
|
void UnswizzleTexture(std::span<u8> output, std::span<const u8> input, u32 bytes_per_pixel, u32 width, u32 height, u32 depth, u32 block_height, u32 block_depth, u32 stride_alignment) {
|
2022-04-03 02:18:03 +02:00
|
|
|
const u32 stride = Common::AlignUpLog2(width, stride_alignment) * bytes_per_pixel;
|
2026-01-10 12:27:57 +01:00
|
|
|
const u32 new_bpp = (std::min)(4U, u32(std::countr_zero(width * bytes_per_pixel)));
|
2022-04-03 02:18:03 +02:00
|
|
|
width = (width * bytes_per_pixel) >> new_bpp;
|
|
|
|
|
bytes_per_pixel = 1U << new_bpp;
|
2026-01-10 12:27:57 +01:00
|
|
|
Swizzle(false, output, input, bytes_per_pixel, width, height, depth, block_height, block_depth, stride);
|
2021-08-12 04:45:25 +00:00
|
|
|
}
|
|
|
|
|
|
2026-01-10 12:27:57 +01:00
|
|
|
void SwizzleTexture(std::span<u8> output, std::span<const u8> input, u32 bytes_per_pixel, u32 width, u32 height, u32 depth, u32 block_height, u32 block_depth, u32 stride_alignment) {
|
2022-04-03 02:18:03 +02:00
|
|
|
const u32 stride = Common::AlignUpLog2(width, stride_alignment) * bytes_per_pixel;
|
2026-01-10 12:27:57 +01:00
|
|
|
const u32 new_bpp = (std::min)(4U, u32(std::countr_zero(width * bytes_per_pixel)));
|
2022-04-03 02:18:03 +02:00
|
|
|
width = (width * bytes_per_pixel) >> new_bpp;
|
|
|
|
|
bytes_per_pixel = 1U << new_bpp;
|
2026-01-10 12:27:57 +01:00
|
|
|
Swizzle(true, output, input, bytes_per_pixel, width, height, depth, block_height, block_depth, stride);
|
2021-08-12 04:45:25 +00:00
|
|
|
}
|
|
|
|
|
|
2026-01-10 12:27:57 +01:00
|
|
|
void SwizzleSubrect(std::span<u8> output, std::span<const u8> 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) {
|
2021-08-12 04:45:25 +00:00
|
|
|
switch (bytes_per_pixel) {
|
2026-01-10 12:27:57 +01:00
|
|
|
case 1:
|
|
|
|
|
case 2:
|
|
|
|
|
case 3:
|
|
|
|
|
case 4:
|
|
|
|
|
case 6:
|
|
|
|
|
case 8:
|
|
|
|
|
case 12:
|
|
|
|
|
case 16:
|
|
|
|
|
return SwizzleSubrectImpl(true, bytes_per_pixel, output, input, width, height, depth, origin_x, origin_y, extent_x, extent_y, block_height, block_depth, pitch_linear);
|
2021-08-12 04:45:25 +00:00
|
|
|
default:
|
2022-06-07 17:02:29 -04:00
|
|
|
ASSERT_MSG(false, "Invalid bytes_per_pixel={}", bytes_per_pixel);
|
2022-11-11 04:12:37 -08:00
|
|
|
break;
|
2021-08-12 04:45:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-10 12:27:57 +01:00
|
|
|
void UnswizzleSubrect(std::span<u8> output, std::span<const u8> 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) {
|
2021-08-12 04:45:25 +00:00
|
|
|
switch (bytes_per_pixel) {
|
2026-01-10 12:27:57 +01:00
|
|
|
case 1:
|
|
|
|
|
case 2:
|
|
|
|
|
case 3:
|
|
|
|
|
case 4:
|
|
|
|
|
case 6:
|
|
|
|
|
case 8:
|
|
|
|
|
case 12:
|
|
|
|
|
case 16:
|
|
|
|
|
return SwizzleSubrectImpl(false, bytes_per_pixel, output, input, width, height, depth, origin_x, origin_y, extent_x, extent_y, block_height, block_depth, pitch_linear);
|
2021-08-12 04:45:25 +00:00
|
|
|
default:
|
2022-06-07 17:02:29 -04:00
|
|
|
ASSERT_MSG(false, "Invalid bytes_per_pixel={}", bytes_per_pixel);
|
2022-11-11 04:12:37 -08:00
|
|
|
break;
|
2021-08-12 04:45:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-10 12:27:57 +01:00
|
|
|
std::size_t CalculateSize(bool tiled, u32 bytes_per_pixel, u32 width, u32 height, u32 depth, u32 block_height, u32 block_depth) {
|
2018-10-12 14:21:53 -04:00
|
|
|
if (tiled) {
|
2021-01-15 04:02:37 -03:00
|
|
|
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);
|
2019-04-14 01:44:16 -03:00
|
|
|
return aligned_width * aligned_height * aligned_depth;
|
2018-10-12 14:21:53 -04:00
|
|
|
} else {
|
|
|
|
|
return width * height * depth * bytes_per_pixel;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-10 12:27:57 +01:00
|
|
|
u64 GetGOBOffset(u32 width, u32 height, u32 dst_x, u32 dst_y, u32 block_height, u32 bytes_per_pixel) {
|
2020-04-27 23:57:24 -04:00
|
|
|
auto div_ceil = [](const u32 x, const u32 y) { return ((x + y - 1) / y); };
|
|
|
|
|
const u32 gobs_in_block = 1 << block_height;
|
2020-07-04 18:42:10 -03:00
|
|
|
const u32 y_blocks = GOB_SIZE_Y << block_height;
|
|
|
|
|
const u32 x_per_gob = GOB_SIZE_X / bytes_per_pixel;
|
2020-04-27 23:57:24 -04:00
|
|
|
const u32 x_blocks = div_ceil(width, x_per_gob);
|
2020-07-04 18:42:10 -03:00
|
|
|
const u32 block_size = GOB_SIZE * gobs_in_block;
|
2020-04-27 23:57:24 -04:00
|
|
|
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;
|
2020-07-04 18:42:10 -03:00
|
|
|
return base + (relative_y / GOB_SIZE_Y) * GOB_SIZE;
|
2020-04-27 23:57:24 -04:00
|
|
|
}
|
|
|
|
|
|
2018-07-20 18:14:17 -04:00
|
|
|
} // namespace Tegra::Texture
|