diff --git a/Ryujinx.Graphics.Texture/LayoutConverter.cs b/Ryujinx.Graphics.Texture/LayoutConverter.cs index 525271c4c3..060f067753 100644 --- a/Ryujinx.Graphics.Texture/LayoutConverter.cs +++ b/Ryujinx.Graphics.Texture/LayoutConverter.cs @@ -1,5 +1,6 @@ using Ryujinx.Common; using System; +using System.Runtime.CompilerServices; using System.Runtime.Intrinsics; using static Ryujinx.Graphics.Texture.BlockLinearConstants; @@ -9,7 +10,7 @@ namespace Ryujinx.Graphics.Texture { private const int HostStrideAlignment = 4; - public static Span ConvertBlockLinearToLinear( + private static unsafe Span ConvertBlockLinearToLinear( int width, int height, int depth, @@ -17,13 +18,14 @@ namespace Ryujinx.Graphics.Texture int layers, int blockWidth, int blockHeight, - int bytesPerPixel, int gobBlocksInY, int gobBlocksInZ, int gobBlocksInTileX, SizeInfo sizeInfo, - ReadOnlySpan data) + ReadOnlySpan data) where T : unmanaged { + int bytesPerPixel = Unsafe.SizeOf(); + int outSize = GetTextureSize( width, height, @@ -89,77 +91,89 @@ namespace Ryujinx.Graphics.Texture mipGobBlocksInZ, bytesPerPixel); - unsafe bool Convert(Span output, ReadOnlySpan data) where T : unmanaged + fixed (byte* outputPtr = output, dataPtr = data) { - fixed (byte* outputPtr = output, dataPtr = data) + byte* outPtr = outputPtr + outOffs; + for (int layer = 0; layer < layers; layer++) { - 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++) { - 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.SetZ(z); - for (int y = 0; y < h; y++) + layoutConverter.SetY(y); + + for (int x = 0; x < strideTrunc64; x += 64, outPtr += 64) { - layoutConverter.SetY(y); + byte* offset = inBaseOffset + layoutConverter.GetOffsetWithLineOffset64(x); + byte* offset2 = offset + 0x20; + byte* offset3 = offset + 0x100; + byte* offset4 = offset + 0x120; - 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 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; + *(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 true; + outOffs += stride * h * d * layers; } - - bool _ = bytesPerPixel switch - { - 1 => Convert(output, data), - 2 => Convert(output, data), - 4 => Convert(output, data), - 8 => Convert(output, data), - 12 => Convert(output, data), - 16 => Convert>(output, data), - _ => throw new NotSupportedException($"Unable to convert ${bytesPerPixel} bpp pixel format.") - }; } 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, @@ -191,7 +205,7 @@ namespace Ryujinx.Graphics.Texture return output; } - public static Span ConvertLinearToBlockLinear( + private static unsafe Span ConvertLinearToBlockLinear( int width, int height, int depth, @@ -199,13 +213,14 @@ namespace Ryujinx.Graphics.Texture int layers, int blockWidth, int blockHeight, - int bytesPerPixel, int gobBlocksInY, int gobBlocksInZ, int gobBlocksInTileX, SizeInfo sizeInfo, - ReadOnlySpan data) + ReadOnlySpan data) where T : unmanaged { + int bytesPerPixel = Unsafe.SizeOf(); + Span output = new byte[sizeInfo.TotalSize]; int inOffs = 0; @@ -261,78 +276,90 @@ namespace Ryujinx.Graphics.Texture mipGobBlocksInZ, bytesPerPixel); - unsafe bool Convert(Span output, ReadOnlySpan data) where T : unmanaged + fixed (byte* outputPtr = output, dataPtr = data) { - fixed (byte* outputPtr = output, dataPtr = data) + byte* inPtr = dataPtr + inOffs; + for (int layer = 0; layer < layers; layer++) { - 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++) { - 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.SetZ(z); - for (int y = 0; y < h; y++) + layoutConverter.SetY(y); + + for (int x = 0; x < strideTrunc64; x += 64, inPtr += 64) { - layoutConverter.SetY(y); + byte* offset = outBaseOffset + layoutConverter.GetOffsetWithLineOffset64(x); + byte* offset2 = offset + 0x20; + byte* offset3 = offset + 0x100; + byte* offset4 = offset + 0x120; - 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 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; + *(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 true; + inOffs += stride * h * d * layers; } - - bool _ = bytesPerPixel switch - { - 1 => Convert(output, data), - 2 => Convert(output, data), - 4 => Convert(output, data), - 8 => Convert(output, data), - 12 => Convert(output, data), - 16 => Convert>(output, data), - _ => throw new NotSupportedException($"Unable to convert ${bytesPerPixel} bpp pixel format.") - }; } 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,