2018-03-20 02:00:29 +03:00
|
|
|
// Copyright 2018 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2020-12-30 08:25:23 +03:00
|
|
|
#include <array>
|
2018-07-18 07:15:20 +03:00
|
|
|
#include <cmath>
|
2018-03-22 23:17:10 +03:00
|
|
|
#include <cstring>
|
2020-12-30 08:25:23 +03:00
|
|
|
#include <span>
|
|
|
|
#include <utility>
|
|
|
|
|
2018-10-12 21:21:53 +03:00
|
|
|
#include "common/alignment.h"
|
2018-03-22 23:17:10 +03:00
|
|
|
#include "common/assert.h"
|
2020-07-05 00:42:10 +03:00
|
|
|
#include "common/bit_util.h"
|
2020-12-30 08:25:23 +03:00
|
|
|
#include "common/div_ceil.h"
|
2018-07-02 20:42:04 +03:00
|
|
|
#include "video_core/gpu.h"
|
2018-03-20 02:00:29 +03:00
|
|
|
#include "video_core/textures/decoders.h"
|
2018-03-22 23:17:10 +03:00
|
|
|
#include "video_core/textures/texture.h"
|
2018-03-20 02:00:29 +03:00
|
|
|
|
2018-07-21 01:14:17 +03:00
|
|
|
namespace Tegra::Texture {
|
2020-12-30 08:25:23 +03:00
|
|
|
namespace {
|
|
|
|
template <bool TO_LINEAR>
|
|
|
|
void Swizzle(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) {
|
|
|
|
// 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 23:17:10 +03:00
|
|
|
|
2020-12-30 08:25:23 +03:00
|
|
|
// We can configure here a custom pitch
|
|
|
|
// As it's not exposed 'width * bpp' will be the expected pitch.
|
|
|
|
const u32 pitch = width * bytes_per_pixel;
|
2021-01-15 10:02:37 +03:00
|
|
|
const u32 stride = Common::AlignUpLog2(width, stride_alignment) * bytes_per_pixel;
|
2018-10-11 21:33:50 +03:00
|
|
|
|
2020-12-30 08: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);
|
|
|
|
const u32 slice_size =
|
|
|
|
Common::DivCeilLog2(height, block_height + GOB_SIZE_Y_SHIFT) * block_size;
|
2018-10-11 05:03:38 +03:00
|
|
|
|
2020-12-30 08: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;
|
|
|
|
const auto& table = SWIZZLE_TABLE[y % GOB_SIZE_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);
|
|
|
|
|
|
|
|
for (u32 column = 0; column < width; ++column) {
|
|
|
|
const u32 x = (column + origin_x) * bytes_per_pixel;
|
|
|
|
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 + table[x % GOB_SIZE_X];
|
|
|
|
|
|
|
|
const u32 unswizzled_offset =
|
|
|
|
slice * pitch * height + line * pitch + column * bytes_per_pixel;
|
|
|
|
|
2021-06-04 10:35:13 +03:00
|
|
|
if (const auto offset = (TO_LINEAR ? unswizzled_offset : swizzled_offset);
|
|
|
|
offset >= input.size()) {
|
|
|
|
// TODO(Rodrigo): This is an out of bounds access that should never happen. To
|
2021-06-04 12:12:14 +03:00
|
|
|
// avoid crashing the emulator, break.
|
2021-06-04 10:35:13 +03:00
|
|
|
ASSERT_MSG(false, "offset {} exceeds input size {}!", offset, input.size());
|
2021-06-04 12:12:14 +03:00
|
|
|
break;
|
2021-06-04 10:35:13 +03:00
|
|
|
}
|
|
|
|
|
2020-12-30 08:25:23 +03:00
|
|
|
u8* const dst = &output[TO_LINEAR ? swizzled_offset : unswizzled_offset];
|
|
|
|
const u8* const src = &input[TO_LINEAR ? unswizzled_offset : swizzled_offset];
|
|
|
|
std::memcpy(dst, src, bytes_per_pixel);
|
2018-10-11 05:03:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-17 05:43:33 +03:00
|
|
|
} // Anonymous namespace
|
|
|
|
|
2020-12-30 08:25:23 +03: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) {
|
|
|
|
Swizzle<false>(output, input, bytes_per_pixel, width, height, depth, block_height, block_depth,
|
|
|
|
stride_alignment);
|
2018-11-19 06:53:03 +03:00
|
|
|
}
|
|
|
|
|
2020-12-30 08:25:23 +03: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) {
|
|
|
|
Swizzle<true>(output, input, bytes_per_pixel, width, height, depth, block_height, block_depth,
|
|
|
|
stride_alignment);
|
2018-03-22 23:17:10 +03:00
|
|
|
}
|
|
|
|
|
2018-10-18 04:26:40 +03:00
|
|
|
void SwizzleSubrect(u32 subrect_width, u32 subrect_height, u32 source_pitch, u32 swizzled_width,
|
2020-07-05 00:42:10 +03:00
|
|
|
u32 bytes_per_pixel, u8* swizzled_data, const u8* unswizzled_data,
|
2019-07-25 02:18:17 +03:00
|
|
|
u32 block_height_bit, u32 offset_x, u32 offset_y) {
|
2019-06-13 23:41:16 +03:00
|
|
|
const u32 block_height = 1U << block_height_bit;
|
2020-07-05 00:42:10 +03:00
|
|
|
const u32 image_width_in_gobs =
|
|
|
|
(swizzled_width * bytes_per_pixel + (GOB_SIZE_X - 1)) / GOB_SIZE_X;
|
2018-10-18 04:26:40 +03:00
|
|
|
for (u32 line = 0; line < subrect_height; ++line) {
|
2019-07-25 02:18:17 +03:00
|
|
|
const u32 dst_y = line + offset_y;
|
2018-10-18 04:26:40 +03:00
|
|
|
const u32 gob_address_y =
|
2020-07-05 00:42:10 +03:00
|
|
|
(dst_y / (GOB_SIZE_Y * block_height)) * GOB_SIZE * block_height * image_width_in_gobs +
|
|
|
|
((dst_y % (GOB_SIZE_Y * block_height)) / GOB_SIZE_Y) * GOB_SIZE;
|
2020-12-30 08:25:23 +03:00
|
|
|
const auto& table = SWIZZLE_TABLE[dst_y % GOB_SIZE_Y];
|
2018-10-18 04:26:40 +03:00
|
|
|
for (u32 x = 0; x < subrect_width; ++x) {
|
2019-07-25 02:18:17 +03:00
|
|
|
const u32 dst_x = x + offset_x;
|
2018-11-17 03:55:28 +03:00
|
|
|
const u32 gob_address =
|
2020-07-05 00:42:10 +03:00
|
|
|
gob_address_y + (dst_x * bytes_per_pixel / GOB_SIZE_X) * GOB_SIZE * block_height;
|
|
|
|
const u32 swizzled_offset = gob_address + table[(dst_x * bytes_per_pixel) % GOB_SIZE_X];
|
|
|
|
const u32 unswizzled_offset = line * source_pitch + x * bytes_per_pixel;
|
2018-10-18 04:26:40 +03:00
|
|
|
|
2020-07-05 00:42:10 +03:00
|
|
|
const u8* const source_line = unswizzled_data + unswizzled_offset;
|
|
|
|
u8* const dest_addr = swizzled_data + swizzled_offset;
|
2019-02-24 08:15:35 +03:00
|
|
|
std::memcpy(dest_addr, source_line, bytes_per_pixel);
|
2018-10-18 04:26:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-30 07:38:44 +03:00
|
|
|
void UnswizzleSubrect(u32 line_length_in, u32 line_count, u32 pitch, u32 width, u32 bytes_per_pixel,
|
|
|
|
u32 block_height, u32 origin_x, u32 origin_y, u8* output, const u8* input) {
|
|
|
|
const u32 stride = width * bytes_per_pixel;
|
|
|
|
const u32 gobs_in_x = (stride + GOB_SIZE_X - 1) / GOB_SIZE_X;
|
|
|
|
const u32 block_size = gobs_in_x << (GOB_SIZE_SHIFT + block_height);
|
|
|
|
|
|
|
|
const u32 block_height_mask = (1U << block_height) - 1;
|
2020-12-30 08:25:23 +03:00
|
|
|
const u32 x_shift = GOB_SIZE_SHIFT + block_height;
|
2020-07-30 07:38:44 +03:00
|
|
|
|
|
|
|
for (u32 line = 0; line < line_count; ++line) {
|
|
|
|
const u32 src_y = line + origin_y;
|
2020-12-30 08:25:23 +03:00
|
|
|
const auto& table = SWIZZLE_TABLE[src_y % GOB_SIZE_Y];
|
2020-07-30 07:38:44 +03:00
|
|
|
|
|
|
|
const u32 block_y = src_y >> GOB_SIZE_Y_SHIFT;
|
|
|
|
const u32 src_offset_y = (block_y >> block_height) * block_size +
|
|
|
|
((block_y & block_height_mask) << GOB_SIZE_SHIFT);
|
|
|
|
for (u32 column = 0; column < line_length_in; ++column) {
|
|
|
|
const u32 src_x = (column + origin_x) * bytes_per_pixel;
|
|
|
|
const u32 src_offset_x = (src_x >> GOB_SIZE_X_SHIFT) << x_shift;
|
|
|
|
|
|
|
|
const u32 swizzled_offset = src_offset_y + src_offset_x + table[src_x % GOB_SIZE_X];
|
|
|
|
const u32 unswizzled_offset = line * pitch + column * bytes_per_pixel;
|
2018-10-18 04:26:40 +03:00
|
|
|
|
2020-07-30 07:38:44 +03:00
|
|
|
std::memcpy(output + unswizzled_offset, input + swizzled_offset, bytes_per_pixel);
|
2018-10-18 04:26:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-05 00:42:10 +03:00
|
|
|
void SwizzleSliceToVoxel(u32 line_length_in, u32 line_count, u32 pitch, u32 width, u32 height,
|
|
|
|
u32 bytes_per_pixel, u32 block_height, u32 block_depth, u32 origin_x,
|
|
|
|
u32 origin_y, u8* output, const u8* input) {
|
|
|
|
UNIMPLEMENTED_IF(origin_x > 0);
|
|
|
|
UNIMPLEMENTED_IF(origin_y > 0);
|
|
|
|
|
|
|
|
const u32 stride = width * bytes_per_pixel;
|
|
|
|
const u32 gobs_in_x = (stride + GOB_SIZE_X - 1) / GOB_SIZE_X;
|
|
|
|
const u32 block_size = gobs_in_x << (GOB_SIZE_SHIFT + block_height + block_depth);
|
|
|
|
|
|
|
|
const u32 block_height_mask = (1U << block_height) - 1;
|
2020-07-30 07:38:44 +03:00
|
|
|
const u32 x_shift = static_cast<u32>(GOB_SIZE_SHIFT) + block_height + block_depth;
|
2020-07-05 00:42:10 +03:00
|
|
|
|
|
|
|
for (u32 line = 0; line < line_count; ++line) {
|
2020-12-30 08:25:23 +03:00
|
|
|
const auto& table = SWIZZLE_TABLE[line % GOB_SIZE_Y];
|
2020-07-05 00:42:10 +03:00
|
|
|
const u32 block_y = line / GOB_SIZE_Y;
|
|
|
|
const u32 dst_offset_y =
|
|
|
|
(block_y >> block_height) * block_size + (block_y & block_height_mask) * GOB_SIZE;
|
|
|
|
for (u32 x = 0; x < line_length_in; ++x) {
|
|
|
|
const u32 dst_offset =
|
|
|
|
((x / GOB_SIZE_X) << x_shift) + dst_offset_y + table[x % GOB_SIZE_X];
|
|
|
|
const u32 src_offset = x * bytes_per_pixel + line * pitch;
|
|
|
|
std::memcpy(output + dst_offset, input + src_offset, bytes_per_pixel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-16 19:00:46 +03:00
|
|
|
void SwizzleKepler(const u32 width, const u32 height, const u32 dst_x, const u32 dst_y,
|
2019-06-13 23:41:16 +03:00
|
|
|
const u32 block_height_bit, const std::size_t copy_size, const u8* source_data,
|
2019-04-16 19:00:46 +03:00
|
|
|
u8* swizzle_data) {
|
2019-06-13 23:41:16 +03:00
|
|
|
const u32 block_height = 1U << block_height_bit;
|
2020-07-05 00:42:10 +03:00
|
|
|
const u32 image_width_in_gobs{(width + GOB_SIZE_X - 1) / GOB_SIZE_X};
|
2019-04-16 04:06:04 +03:00
|
|
|
std::size_t count = 0;
|
2019-04-16 19:00:46 +03:00
|
|
|
for (std::size_t y = dst_y; y < height && count < copy_size; ++y) {
|
|
|
|
const std::size_t gob_address_y =
|
2020-07-05 00:42:10 +03:00
|
|
|
(y / (GOB_SIZE_Y * block_height)) * GOB_SIZE * block_height * image_width_in_gobs +
|
|
|
|
((y % (GOB_SIZE_Y * block_height)) / GOB_SIZE_Y) * GOB_SIZE;
|
2020-12-30 08:25:23 +03:00
|
|
|
const auto& table = SWIZZLE_TABLE[y % GOB_SIZE_Y];
|
2019-04-16 19:00:46 +03:00
|
|
|
for (std::size_t x = dst_x; x < width && count < copy_size; ++x) {
|
|
|
|
const std::size_t gob_address =
|
2020-07-05 00:42:10 +03:00
|
|
|
gob_address_y + (x / GOB_SIZE_X) * GOB_SIZE * block_height;
|
|
|
|
const std::size_t swizzled_offset = gob_address + table[x % GOB_SIZE_X];
|
2019-04-16 04:06:04 +03:00
|
|
|
const u8* source_line = source_data + count;
|
|
|
|
u8* dest_addr = swizzle_data + swizzled_offset;
|
|
|
|
count++;
|
|
|
|
|
|
|
|
std::memcpy(dest_addr, source_line, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-12 21:21:53 +03:00
|
|
|
std::size_t CalculateSize(bool tiled, u32 bytes_per_pixel, u32 width, u32 height, u32 depth,
|
|
|
|
u32 block_height, u32 block_depth) {
|
|
|
|
if (tiled) {
|
2021-01-15 10: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 07:44:16 +03:00
|
|
|
return aligned_width * aligned_height * aligned_depth;
|
2018-10-12 21:21:53 +03:00
|
|
|
} else {
|
|
|
|
return width * height * depth * bytes_per_pixel;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-28 06:57:24 +03:00
|
|
|
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;
|
2020-07-05 00: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-28 06:57:24 +03:00
|
|
|
const u32 x_blocks = div_ceil(width, x_per_gob);
|
2020-07-05 00:42:10 +03:00
|
|
|
const u32 block_size = GOB_SIZE * gobs_in_block;
|
2020-04-28 06:57:24 +03: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-05 00:42:10 +03:00
|
|
|
return base + (relative_y / GOB_SIZE_Y) * GOB_SIZE;
|
2020-04-28 06:57:24 +03:00
|
|
|
}
|
|
|
|
|
2018-07-21 01:14:17 +03:00
|
|
|
} // namespace Tegra::Texture
|