// SPDX-FileCopyrightText: Copyright 2025 suyu 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
// (Triforce) The Tegra GPU's block-linear texture layout undone, ported from suyu's video_core/textures/decoders.cpp
// (SwizzleImpl with TO_LINEAR false, depth 1). A texture is stored in GOBs (64 bytes by 8 rows, 512 bytes), stacked
// 2^blockHeight GOBs tall into blocks; blocks run left to right, then the next row of blocks.
//
// Inside a GOB this is the Switch 2's order, as in our suyu's decoders.h: the Tegra X1's (x bits 0-3, 5, 8 / y bits 4,
// 6, 7) with offset bits 5 and 6 swapped, so x bit 4 lands on bit 6 and y bit 1 on bit 5. (The X1's order, which most
// Switch tools use, scrambles this game's textures in 4x4-block patches.)
namespace Triforce.Core.GameFiles;
/// Tegra block-linear to linear.
public static class BlockLinear
{
const int GobSizeXShift = 6; // 64 bytes
const int GobSizeYShift = 3; // 8 rows
const int GobSizeShift = GobSizeXShift + GobSizeYShift; // 512 bytes
/// The position of a byte inside its GOB: pdep of x into SWIZZLE_X_BITS 0b1_0100_1111 and of y into
/// SWIZZLE_Y_BITS 0b0_1011_0000.
static int SwizzleX(int x) => (x & 15) | (x & 16) << 2 | (x & 32) << 3;
static int SwizzleY(int y) => (y & 3) << 4 | (y & 4) << 5;
/// The size a block-linear image takes, in bytes.
public static long Size(int width, int height, int bytesPerElement, int blockHeightLog2)
{
var alignedWidth = (width * bytesPerElement + 63) & ~63;
var rows = 1 << (GobSizeYShift + blockHeightLog2);
var alignedHeight = (height + rows - 1) / rows * rows;
return (long)alignedWidth * alignedHeight;
}
///
/// A block-linear image made linear. width and height count elements (pixels, or blocks of a compressed format);
/// bytesPerElement is 1, 2, 4, 8 or 16. The result is width * height * bytesPerElement bytes, rows one after another.
///
public static byte[] Deswizzle(ReadOnlySpan input, int width, int height, int bytesPerElement, int blockHeightLog2)
{
if (bytesPerElement is not (1 or 2 or 4 or 8 or 16))
throw new NotSupportedException(bytesPerElement + " bytes per element");
// ((Triforce, 11-26) the sizes checked before anything is allocated: a header could ask for 2 GB, then be refused)
ImageLimits.Check(width, height);
if ((long)width * bytesPerElement * height > int.MaxValue || blockHeightLog2 is < 0 or > 5) throw new InvalidDataException("a texture larger than the game has");
if (Size(width, height, bytesPerElement, blockHeightLog2) > input.Length)
throw new InvalidDataException("the texture's data is shorter than its size calls for");
var pitch = width * bytesPerElement;
var output = new byte[pitch * height];
var gobsInX = (pitch + 63) >> GobSizeXShift;
var blockSize = gobsInX << (GobSizeShift + blockHeightLog2);
var blockHeightMask = (1 << blockHeightLog2) - 1;
var xShift = GobSizeShift + blockHeightLog2;
for (var y = 0; y < height; y++)
{
var swizzledY = SwizzleY(y);
var blockY = y >> GobSizeYShift;
var offsetY = (blockY >> blockHeightLog2) * blockSize + ((blockY & blockHeightMask) << GobSizeShift);
var row = output.AsSpan(y * pitch, pitch);
for (var x = 0; x < pitch; x += bytesPerElement)
{
var offsetX = (x >> GobSizeXShift) << xShift;
var at = offsetY + offsetX + (SwizzleX(x & 63) | swizzledY);
input.Slice(at, bytesPerElement).CopyTo(row.Slice(x, bytesPerElement));
}
}
return output;
}
/// ((Triforce, 11-29) The other way: a linear image written into block-linear order, into output (at least
/// Size bytes; bytes the image does not cover are left as they are). For the Character page's recoloured textures.
public static void Swizzle(ReadOnlySpan linear, Span output, int width, int height, int bytesPerElement, int blockHeightLog2)
{
if (bytesPerElement is not (1 or 2 or 4 or 8 or 16))
throw new NotSupportedException(bytesPerElement + " bytes per element");
if (Size(width, height, bytesPerElement, blockHeightLog2) > output.Length)
throw new InvalidDataException("the texture's data is shorter than its size calls for");
var pitch = width * bytesPerElement;
var gobsInX = (pitch + 63) >> GobSizeXShift;
var blockSize = gobsInX << (GobSizeShift + blockHeightLog2);
var blockHeightMask = (1 << blockHeightLog2) - 1;
var xShift = GobSizeShift + blockHeightLog2;
for (var y = 0; y < height; y++)
{
var swizzledY = SwizzleY(y);
var blockY = y >> GobSizeYShift;
var offsetY = (blockY >> blockHeightLog2) * blockSize + ((blockY & blockHeightMask) << GobSizeShift);
var row = linear.Slice(y * pitch, pitch);
for (var x = 0; x < pitch; x += bytesPerElement)
{
var offsetX = (x >> GobSizeXShift) << xShift;
var at = offsetY + offsetX + (SwizzleX(x & 63) | swizzledY);
row.Slice(x, bytesPerElement).CopyTo(output.Slice(at, bytesPerElement));
}
}
}
/// The block height (log2, in GOBs) a smaller mip level uses: the texture's, halved while half of it still
/// covers the level's height (the GPU's rule).
public static int MipBlockHeight(int heightInElements, int blockHeightLog2)
{
var bh = blockHeightLog2;
while (bh > 0 && (8 << (bh - 1)) >= heightInElements) bh--;
return bh;
}
}