using Ryujinx.Common; using System; using System.Runtime.CompilerServices; using System.Runtime.Intrinsics; using static Ryujinx.Graphics.Texture.BlockLinearConstants; namespace Ryujinx.Graphics.Texture { public static class LayoutConverter { private const int HostStrideAlignment = 4; private static unsafe Span ConvertBlockLinearToLinear( int width, int height, int depth, int levels, int layers, int blockWidth, int blockHeight, int gobBlocksInY, int gobBlocksInZ, int gobBlocksInTileX, SizeInfo sizeInfo, ReadOnlySpan data) where T : unmanaged { int bytesPerPixel = Unsafe.SizeOf(); int outSize = GetTextureSize( width, height, depth, levels, layers, blockWidth, blockHeight, bytesPerPixel); Span output = new byte[outSize]; int outOffs = 0; int mipGobBlocksInY = gobBlocksInY; int mipGobBlocksInZ = gobBlocksInZ; int gobWidth = (GobStride / bytesPerPixel) * gobBlocksInTileX; int gobHeight = gobBlocksInY * GobHeight; for (int level = 0; level < levels; level++) { int w = Math.Max(1, width >> level); int h = Math.Max(1, height >> level); int d = Math.Max(1, depth >> level); w = BitUtils.DivRoundUp(w, blockWidth); h = BitUtils.DivRoundUp(h, blockHeight); while (h <= (mipGobBlocksInY >> 1) * GobHeight && mipGobBlocksInY != 1) { mipGobBlocksInY >>= 1; } while (d <= (mipGobBlocksInZ >> 1) && mipGobBlocksInZ != 1) { mipGobBlocksInZ >>= 1; } int strideTrunc = BitUtils.AlignDown(w * bytesPerPixel, 16); int strideTrunc64 = BitUtils.AlignDown(w * bytesPerPixel, 64); int xStart = strideTrunc / bytesPerPixel; int stride = BitUtils.AlignUp(w * bytesPerPixel, HostStrideAlignment); int outStrideGap = stride - w * bytesPerPixel; int alignment = gobWidth; if (d < gobBlocksInZ || w <= gobWidth || h <= gobHeight) { alignment = GobStride / bytesPerPixel; } int wAligned = BitUtils.AlignUp(w, alignment); BlockLinearLayout layoutConverter = new BlockLinearLayout( wAligned, h, d, mipGobBlocksInY, mipGobBlocksInZ, bytesPerPixel); fixed (byte* outputPtr = output, dataPtr = data) { byte* outPtr = outputPtr + outOffs; for (int layer = 0; layer < layers; layer++) { byte* inBaseOffset = dataPtr + (layer * sizeInfo.LayerSize + sizeInfo.GetMipOffset(level)); for (int z = 0; z < d; z++) { layoutConverter.SetZ(z); for (int y = 0; y < h; y++) { layoutConverter.SetY(y); for (int x = 0; x < strideTrunc64; x += 64, outPtr += 64) { byte* offset = inBaseOffset + layoutConverter.GetOffsetWithLineOffset64(x); byte* offset2 = offset + 0x20; byte* offset3 = offset + 0x100; byte* offset4 = offset + 0x120; Vector128 value = *(Vector128*)offset; Vector128 value2 = *(Vector128*)offset2; Vector128 value3 = *(Vector128*)offset3; Vector128 value4 = *(Vector128*)offset4; *(Vector128*)outPtr = value; *(Vector128*)(outPtr + 16) = value2; *(Vector128*)(outPtr + 32) = value3; *(Vector128*)(outPtr + 48) = value4; } for (int x = strideTrunc64; x < strideTrunc; x += 16, outPtr += 16) { byte* offset = inBaseOffset + layoutConverter.GetOffsetWithLineOffset16(x); *(Vector128*)outPtr = *(Vector128*)offset; } for (int x = xStart; x < w; x++, outPtr += bytesPerPixel) { byte* offset = inBaseOffset + layoutConverter.GetOffset(x); *(T*)outPtr = *(T*)offset; } outPtr += outStrideGap; } } } outOffs += stride * h * d * layers; } } return output; } public static Span ConvertBlockLinearToLinear( int width, int height, int depth, int levels, int layers, int blockWidth, int blockHeight, int bytesPerPixel, int gobBlocksInY, int gobBlocksInZ, int gobBlocksInTileX, SizeInfo sizeInfo, ReadOnlySpan data) { return bytesPerPixel switch { 1 => ConvertBlockLinearToLinear(width, height, depth, levels, layers, blockWidth, blockHeight, gobBlocksInY, gobBlocksInZ, gobBlocksInTileX, sizeInfo, data), 2 => ConvertBlockLinearToLinear(width, height, depth, levels, layers, blockWidth, blockHeight, gobBlocksInY, gobBlocksInZ, gobBlocksInTileX, sizeInfo, data), 4 => ConvertBlockLinearToLinear(width, height, depth, levels, layers, blockWidth, blockHeight, gobBlocksInY, gobBlocksInZ, gobBlocksInTileX, sizeInfo, data), 8 => ConvertBlockLinearToLinear(width, height, depth, levels, layers, blockWidth, blockHeight, gobBlocksInY, gobBlocksInZ, gobBlocksInTileX, sizeInfo, data), 12 => ConvertBlockLinearToLinear(width, height, depth, levels, layers, blockWidth, blockHeight, gobBlocksInY, gobBlocksInZ, gobBlocksInTileX, sizeInfo, data), 16 => ConvertBlockLinearToLinear>(width, height, depth, levels, layers, blockWidth, blockHeight, gobBlocksInY, gobBlocksInZ, gobBlocksInTileX, sizeInfo, data), _ => throw new NotSupportedException($"Unable to convert ${bytesPerPixel} bpp pixel format.") }; } public static Span ConvertLinearStridedToLinear( int width, int height, int blockWidth, int blockHeight, int stride, int bytesPerPixel, ReadOnlySpan data) { int w = BitUtils.DivRoundUp(width, blockWidth); int h = BitUtils.DivRoundUp(height, blockHeight); int outStride = BitUtils.AlignUp(w * bytesPerPixel, HostStrideAlignment); int lineSize = w * bytesPerPixel; Span output = new byte[h * outStride]; int outOffs = 0; int inOffs = 0; for (int y = 0; y < h; y++) { data.Slice(inOffs, lineSize).CopyTo(output.Slice(outOffs, lineSize)); inOffs += stride; outOffs += outStride; } return output; } private static unsafe Span ConvertLinearToBlockLinear( int width, int height, int depth, int levels, int layers, int blockWidth, int blockHeight, int gobBlocksInY, int gobBlocksInZ, int gobBlocksInTileX, SizeInfo sizeInfo, ReadOnlySpan data) where T : unmanaged { int bytesPerPixel = Unsafe.SizeOf(); Span output = new byte[sizeInfo.TotalSize]; int inOffs = 0; int mipGobBlocksInY = gobBlocksInY; int mipGobBlocksInZ = gobBlocksInZ; int gobWidth = (GobStride / bytesPerPixel) * gobBlocksInTileX; int gobHeight = gobBlocksInY * GobHeight; for (int level = 0; level < levels; level++) { int w = Math.Max(1, width >> level); int h = Math.Max(1, height >> level); int d = Math.Max(1, depth >> level); w = BitUtils.DivRoundUp(w, blockWidth); h = BitUtils.DivRoundUp(h, blockHeight); while (h <= (mipGobBlocksInY >> 1) * GobHeight && mipGobBlocksInY != 1) { mipGobBlocksInY >>= 1; } while (d <= (mipGobBlocksInZ >> 1) && mipGobBlocksInZ != 1) { mipGobBlocksInZ >>= 1; } int strideTrunc = BitUtils.AlignDown(w * bytesPerPixel, 16); int strideTrunc64 = BitUtils.AlignDown(w * bytesPerPixel, 64); int xStart = strideTrunc / bytesPerPixel; int stride = BitUtils.AlignUp(w * bytesPerPixel, HostStrideAlignment); int inStrideGap = stride - w * bytesPerPixel; int alignment = gobWidth; if (d < gobBlocksInZ || w <= gobWidth || h <= gobHeight) { alignment = GobStride / bytesPerPixel; } int wAligned = BitUtils.AlignUp(w, alignment); BlockLinearLayout layoutConverter = new BlockLinearLayout( wAligned, h, d, mipGobBlocksInY, mipGobBlocksInZ, bytesPerPixel); fixed (byte* outputPtr = output, dataPtr = data) { byte* inPtr = dataPtr + inOffs; for (int layer = 0; layer < layers; layer++) { byte* outBaseOffset = outputPtr + (layer * sizeInfo.LayerSize + sizeInfo.GetMipOffset(level)); for (int z = 0; z < d; z++) { layoutConverter.SetZ(z); for (int y = 0; y < h; y++) { layoutConverter.SetY(y); for (int x = 0; x < strideTrunc64; x += 64, inPtr += 64) { byte* offset = outBaseOffset + layoutConverter.GetOffsetWithLineOffset64(x); byte* offset2 = offset + 0x20; byte* offset3 = offset + 0x100; byte* offset4 = offset + 0x120; Vector128 value = *(Vector128*)inPtr; Vector128 value2 = *(Vector128*)(inPtr + 16); Vector128 value3 = *(Vector128*)(inPtr + 32); Vector128 value4 = *(Vector128*)(inPtr + 48); *(Vector128*)offset = value; *(Vector128*)offset2 = value2; *(Vector128*)offset3 = value3; *(Vector128*)offset4 = value4; } for (int x = strideTrunc64; x < strideTrunc; x += 16, inPtr += 16) { byte* offset = outBaseOffset + layoutConverter.GetOffsetWithLineOffset16(x); *(Vector128*)offset = *(Vector128*)inPtr; } for (int x = xStart; x < w; x++, inPtr += bytesPerPixel) { byte* offset = outBaseOffset + layoutConverter.GetOffset(x); *(T*)offset = *(T*)inPtr; } inPtr += inStrideGap; } } } inOffs += stride * h * d * layers; } } return output; } public static Span ConvertLinearToBlockLinear( int width, int height, int depth, int levels, int layers, int blockWidth, int blockHeight, int bytesPerPixel, int gobBlocksInY, int gobBlocksInZ, int gobBlocksInTileX, SizeInfo sizeInfo, ReadOnlySpan data) { return bytesPerPixel switch { 1 => ConvertLinearToBlockLinear(width, height, depth, levels, layers, blockWidth, blockHeight, gobBlocksInY, gobBlocksInZ, gobBlocksInTileX, sizeInfo, data), 2 => ConvertLinearToBlockLinear(width, height, depth, levels, layers, blockWidth, blockHeight, gobBlocksInY, gobBlocksInZ, gobBlocksInTileX, sizeInfo, data), 4 => ConvertLinearToBlockLinear(width, height, depth, levels, layers, blockWidth, blockHeight, gobBlocksInY, gobBlocksInZ, gobBlocksInTileX, sizeInfo, data), 8 => ConvertLinearToBlockLinear(width, height, depth, levels, layers, blockWidth, blockHeight, gobBlocksInY, gobBlocksInZ, gobBlocksInTileX, sizeInfo, data), 12 => ConvertLinearToBlockLinear(width, height, depth, levels, layers, blockWidth, blockHeight, gobBlocksInY, gobBlocksInZ, gobBlocksInTileX, sizeInfo, data), 16 => ConvertLinearToBlockLinear>(width, height, depth, levels, layers, blockWidth, blockHeight, gobBlocksInY, gobBlocksInZ, gobBlocksInTileX, sizeInfo, data), _ => throw new NotSupportedException($"Unable to convert ${bytesPerPixel} bpp pixel format.") }; } public static Span ConvertLinearToLinearStrided( int width, int height, int blockWidth, int blockHeight, int stride, int bytesPerPixel, ReadOnlySpan data) { int w = BitUtils.DivRoundUp(width, blockWidth); int h = BitUtils.DivRoundUp(height, blockHeight); int inStride = BitUtils.AlignUp(w * bytesPerPixel, HostStrideAlignment); int lineSize = width * bytesPerPixel; Span output = new byte[h * stride]; int inOffs = 0; int outOffs = 0; for (int y = 0; y < h; y++) { data.Slice(inOffs, lineSize).CopyTo(output.Slice(outOffs, lineSize)); inOffs += inStride; outOffs += stride; } return output; } private static int GetTextureSize( int width, int height, int depth, int levels, int layers, int blockWidth, int blockHeight, int bytesPerPixel) { int layerSize = 0; for (int level = 0; level < levels; level++) { int w = Math.Max(1, width >> level); int h = Math.Max(1, height >> level); int d = Math.Max(1, depth >> level); w = BitUtils.DivRoundUp(w, blockWidth); h = BitUtils.DivRoundUp(h, blockHeight); int stride = BitUtils.AlignUp(w * bytesPerPixel, HostStrideAlignment); layerSize += stride * h * d; } return layerSize * layers; } } }