diff options
| author | TSR Berry <20988865+TSRBerry@users.noreply.github.com> | 2023-04-08 01:22:00 +0200 |
|---|---|---|
| committer | Mary <thog@protonmail.com> | 2023-04-27 23:51:14 +0200 |
| commit | cee712105850ac3385cd0091a923438167433f9f (patch) | |
| tree | 4a5274b21d8b7f938c0d0ce18736d3f2993b11b1 /src/Ryujinx.Graphics.Vic | |
| parent | cd124bda587ef09668a971fa1cac1c3f0cfc9f21 (diff) | |
Move solution and projects to src
Diffstat (limited to 'src/Ryujinx.Graphics.Vic')
28 files changed, 2470 insertions, 0 deletions
diff --git a/src/Ryujinx.Graphics.Vic/Blender.cs b/src/Ryujinx.Graphics.Vic/Blender.cs new file mode 100644 index 00000000..e49b5903 --- /dev/null +++ b/src/Ryujinx.Graphics.Vic/Blender.cs @@ -0,0 +1,278 @@ +using Ryujinx.Graphics.Vic.Image; +using Ryujinx.Graphics.Vic.Types; +using System; +using System.Diagnostics; +using System.Runtime.CompilerServices; +using System.Runtime.Intrinsics; +using System.Runtime.Intrinsics.Arm; +using System.Runtime.Intrinsics.X86; + +namespace Ryujinx.Graphics.Vic +{ + static class Blender + { + public static void BlendOne(Surface dst, Surface src, ref SlotStruct slot, Rectangle targetRect) + { + int x1 = targetRect.X; + int y1 = targetRect.Y; + int x2 = Math.Min(src.Width, x1 + targetRect.Width); + int y2 = Math.Min(src.Height, y1 + targetRect.Height); + + if (((x1 | x2) & 3) == 0) + { + if (Sse41.IsSupported) + { + BlendOneSse41(dst, src, ref slot, x1, y1, x2, y2); + return; + } + else if (AdvSimd.IsSupported) + { + BlendOneAdvSimd(dst, src, ref slot, x1, y1, x2, y2); + return; + } + } + + for (int y = y1; y < y2; y++) + { + for (int x = x1; x < x2; x++) + { + int inR = src.GetR(x, y); + int inG = src.GetG(x, y); + int inB = src.GetB(x, y); + + MatrixMultiply(ref slot.ColorMatrixStruct, inR, inG, inB, out int r, out int g, out int b); + + r = Math.Clamp(r, slot.SlotConfig.SoftClampLow, slot.SlotConfig.SoftClampHigh); + g = Math.Clamp(g, slot.SlotConfig.SoftClampLow, slot.SlotConfig.SoftClampHigh); + b = Math.Clamp(b, slot.SlotConfig.SoftClampLow, slot.SlotConfig.SoftClampHigh); + + dst.SetR(x, y, (ushort)r); + dst.SetG(x, y, (ushort)g); + dst.SetB(x, y, (ushort)b); + dst.SetA(x, y, src.GetA(x, y)); + } + } + } + + private unsafe static void BlendOneSse41(Surface dst, Surface src, ref SlotStruct slot, int x1, int y1, int x2, int y2) + { + Debug.Assert(((x1 | x2) & 3) == 0); + + ref MatrixStruct mtx = ref slot.ColorMatrixStruct; + + int one = 1 << (mtx.MatrixRShift + 8); + + Vector128<int> col1 = Vector128.Create(mtx.MatrixCoeff00, mtx.MatrixCoeff10, mtx.MatrixCoeff20, 0); + Vector128<int> col2 = Vector128.Create(mtx.MatrixCoeff01, mtx.MatrixCoeff11, mtx.MatrixCoeff21, 0); + Vector128<int> col3 = Vector128.Create(mtx.MatrixCoeff02, mtx.MatrixCoeff12, mtx.MatrixCoeff22, one); + Vector128<int> col4 = Vector128.Create(mtx.MatrixCoeff03, mtx.MatrixCoeff13, mtx.MatrixCoeff23, 0); + + Vector128<int> rShift = Vector128.CreateScalar(mtx.MatrixRShift); + Vector128<ushort> clMin = Vector128.Create((ushort)slot.SlotConfig.SoftClampLow); + Vector128<ushort> clMax = Vector128.Create((ushort)slot.SlotConfig.SoftClampHigh); + + fixed (Pixel* srcPtr = src.Data, dstPtr = dst.Data) + { + Pixel* ip = srcPtr; + Pixel* op = dstPtr; + + for (int y = y1; y < y2; y++, ip += src.Width, op += dst.Width) + { + for (int x = x1; x < x2; x += 4) + { + Vector128<int> pixel1 = Sse41.ConvertToVector128Int32((ushort*)(ip + (uint)x)); + Vector128<int> pixel2 = Sse41.ConvertToVector128Int32((ushort*)(ip + (uint)x + 1)); + Vector128<int> pixel3 = Sse41.ConvertToVector128Int32((ushort*)(ip + (uint)x + 2)); + Vector128<int> pixel4 = Sse41.ConvertToVector128Int32((ushort*)(ip + (uint)x + 3)); + + Vector128<ushort> pixel12, pixel34; + + if (mtx.MatrixEnable) + { + pixel12 = Sse41.PackUnsignedSaturate( + MatrixMultiplySse41(pixel1, col1, col2, col3, col4, rShift), + MatrixMultiplySse41(pixel2, col1, col2, col3, col4, rShift)); + pixel34 = Sse41.PackUnsignedSaturate( + MatrixMultiplySse41(pixel3, col1, col2, col3, col4, rShift), + MatrixMultiplySse41(pixel4, col1, col2, col3, col4, rShift)); + } + else + { + pixel12 = Sse41.PackUnsignedSaturate(pixel1, pixel2); + pixel34 = Sse41.PackUnsignedSaturate(pixel3, pixel4); + } + + pixel12 = Sse41.Min(pixel12, clMax); + pixel34 = Sse41.Min(pixel34, clMax); + pixel12 = Sse41.Max(pixel12, clMin); + pixel34 = Sse41.Max(pixel34, clMin); + + Sse2.Store((ushort*)(op + (uint)x + 0), pixel12); + Sse2.Store((ushort*)(op + (uint)x + 2), pixel34); + } + } + } + } + + private unsafe static void BlendOneAdvSimd(Surface dst, Surface src, ref SlotStruct slot, int x1, int y1, int x2, int y2) + { + Debug.Assert(((x1 | x2) & 3) == 0); + + ref MatrixStruct mtx = ref slot.ColorMatrixStruct; + + Vector128<int> col1 = Vector128.Create(mtx.MatrixCoeff00, mtx.MatrixCoeff10, mtx.MatrixCoeff20, 0); + Vector128<int> col2 = Vector128.Create(mtx.MatrixCoeff01, mtx.MatrixCoeff11, mtx.MatrixCoeff21, 0); + Vector128<int> col3 = Vector128.Create(mtx.MatrixCoeff02, mtx.MatrixCoeff12, mtx.MatrixCoeff22, 0); + Vector128<int> col4 = Vector128.Create(mtx.MatrixCoeff03, mtx.MatrixCoeff13, mtx.MatrixCoeff23, 0); + + Vector128<int> rShift = Vector128.Create(-mtx.MatrixRShift); + Vector128<int> selMask = Vector128.Create(0, 0, 0, -1); + Vector128<ushort> clMin = Vector128.Create((ushort)slot.SlotConfig.SoftClampLow); + Vector128<ushort> clMax = Vector128.Create((ushort)slot.SlotConfig.SoftClampHigh); + + fixed (Pixel* srcPtr = src.Data, dstPtr = dst.Data) + { + Pixel* ip = srcPtr; + Pixel* op = dstPtr; + + if (mtx.MatrixEnable) + { + for (int y = y1; y < y2; y++, ip += src.Width, op += dst.Width) + { + for (int x = x1; x < x2; x += 4) + { + Vector128<ushort> pixel12 = AdvSimd.LoadVector128((ushort*)(ip + (uint)x)); + Vector128<ushort> pixel34 = AdvSimd.LoadVector128((ushort*)(ip + (uint)x + 2)); + + Vector128<uint> pixel1 = AdvSimd.ZeroExtendWideningLower(pixel12.GetLower()); + Vector128<uint> pixel2 = AdvSimd.ZeroExtendWideningUpper(pixel12); + Vector128<uint> pixel3 = AdvSimd.ZeroExtendWideningLower(pixel34.GetLower()); + Vector128<uint> pixel4 = AdvSimd.ZeroExtendWideningUpper(pixel34); + + Vector128<int> t1 = MatrixMultiplyAdvSimd(pixel1.AsInt32(), col1, col2, col3, col4, rShift, selMask); + Vector128<int> t2 = MatrixMultiplyAdvSimd(pixel2.AsInt32(), col1, col2, col3, col4, rShift, selMask); + Vector128<int> t3 = MatrixMultiplyAdvSimd(pixel3.AsInt32(), col1, col2, col3, col4, rShift, selMask); + Vector128<int> t4 = MatrixMultiplyAdvSimd(pixel4.AsInt32(), col1, col2, col3, col4, rShift, selMask); + + Vector64<ushort> lower1 = AdvSimd.ExtractNarrowingSaturateUnsignedLower(t1); + Vector64<ushort> lower3 = AdvSimd.ExtractNarrowingSaturateUnsignedLower(t3); + + pixel12 = AdvSimd.ExtractNarrowingSaturateUnsignedUpper(lower1, t2); + pixel34 = AdvSimd.ExtractNarrowingSaturateUnsignedUpper(lower3, t4); + + pixel12 = AdvSimd.Min(pixel12, clMax); + pixel34 = AdvSimd.Min(pixel34, clMax); + pixel12 = AdvSimd.Max(pixel12, clMin); + pixel34 = AdvSimd.Max(pixel34, clMin); + + AdvSimd.Store((ushort*)(op + (uint)x + 0), pixel12); + AdvSimd.Store((ushort*)(op + (uint)x + 2), pixel34); + } + } + } + else + { + for (int y = y1; y < y2; y++, ip += src.Width, op += dst.Width) + { + for (int x = x1; x < x2; x += 4) + { + Vector128<ushort> pixel12 = AdvSimd.LoadVector128((ushort*)(ip + (uint)x)); + Vector128<ushort> pixel34 = AdvSimd.LoadVector128((ushort*)(ip + (uint)x + 2)); + + pixel12 = AdvSimd.Min(pixel12, clMax); + pixel34 = AdvSimd.Min(pixel34, clMax); + pixel12 = AdvSimd.Max(pixel12, clMin); + pixel34 = AdvSimd.Max(pixel34, clMin); + + AdvSimd.Store((ushort*)(op + (uint)x + 0), pixel12); + AdvSimd.Store((ushort*)(op + (uint)x + 2), pixel34); + } + } + } + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static void MatrixMultiply(ref MatrixStruct mtx, int x, int y, int z, out int r, out int g, out int b) + { + if (mtx.MatrixEnable) + { + r = x * mtx.MatrixCoeff00 + y * mtx.MatrixCoeff01 + z * mtx.MatrixCoeff02; + g = x * mtx.MatrixCoeff10 + y * mtx.MatrixCoeff11 + z * mtx.MatrixCoeff12; + b = x * mtx.MatrixCoeff20 + y * mtx.MatrixCoeff21 + z * mtx.MatrixCoeff22; + + r >>= mtx.MatrixRShift; + g >>= mtx.MatrixRShift; + b >>= mtx.MatrixRShift; + + r += mtx.MatrixCoeff03; + g += mtx.MatrixCoeff13; + b += mtx.MatrixCoeff23; + + r >>= 8; + g >>= 8; + b >>= 8; + } + else + { + r = x; + g = y; + b = z; + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static Vector128<int> MatrixMultiplySse41( + Vector128<int> pixel, + Vector128<int> col1, + Vector128<int> col2, + Vector128<int> col3, + Vector128<int> col4, + Vector128<int> rShift) + { + Vector128<int> x = Sse2.Shuffle(pixel, 0); + Vector128<int> y = Sse2.Shuffle(pixel, 0x55); + Vector128<int> z = Sse2.Shuffle(pixel, 0xea); + + col1 = Sse41.MultiplyLow(col1, x); + col2 = Sse41.MultiplyLow(col2, y); + col3 = Sse41.MultiplyLow(col3, z); + + Vector128<int> res = Sse2.Add(col3, Sse2.Add(col1, col2)); + + res = Sse2.ShiftRightArithmetic(res, rShift); + res = Sse2.Add(res, col4); + res = Sse2.ShiftRightArithmetic(res, 8); + + return res; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static Vector128<int> MatrixMultiplyAdvSimd( + Vector128<int> pixel, + Vector128<int> col1, + Vector128<int> col2, + Vector128<int> col3, + Vector128<int> col4, + Vector128<int> rShift, + Vector128<int> selectMask) + { + Vector128<int> x = AdvSimd.DuplicateSelectedScalarToVector128(pixel, 0); + Vector128<int> y = AdvSimd.DuplicateSelectedScalarToVector128(pixel, 1); + Vector128<int> z = AdvSimd.DuplicateSelectedScalarToVector128(pixel, 2); + + col1 = AdvSimd.Multiply(col1, x); + col2 = AdvSimd.Multiply(col2, y); + col3 = AdvSimd.Multiply(col3, z); + + Vector128<int> res = AdvSimd.Add(col3, AdvSimd.Add(col1, col2)); + + res = AdvSimd.ShiftArithmetic(res, rShift); + res = AdvSimd.Add(res, col4); + res = AdvSimd.ShiftRightArithmetic(res, 8); + res = AdvSimd.BitwiseSelect(selectMask, pixel, res); + + return res; + } + } +} diff --git a/src/Ryujinx.Graphics.Vic/Image/BufferPool.cs b/src/Ryujinx.Graphics.Vic/Image/BufferPool.cs new file mode 100644 index 00000000..cde7e6eb --- /dev/null +++ b/src/Ryujinx.Graphics.Vic/Image/BufferPool.cs @@ -0,0 +1,103 @@ +using System; + +namespace Ryujinx.Graphics.Vic.Image +{ + class BufferPool<T> + { + /// <summary> + /// Maximum number of buffers on the pool. + /// </summary> + private const int MaxBuffers = 4; + + /// <summary> + /// Maximum size of a buffer that can be added on the pool. + /// If the required buffer is larger than this, it won't be + /// added to the pool to avoid long term high memory usage. + /// </summary> + private const int MaxBufferSize = 2048 * 2048; + + private struct PoolItem + { + public bool InUse; + public T[] Buffer; + } + + private readonly PoolItem[] _pool = new PoolItem[MaxBuffers]; + + /// <summary> + /// Rents a buffer with the exact size requested. + /// </summary> + /// <param name="length">Size of the buffer</param> + /// <param name="buffer">Span of the requested size</param> + /// <returns>The index of the buffer on the pool</returns> + public int Rent(int length, out Span<T> buffer) + { + int index = RentMinimum(length, out T[] bufferArray); + + buffer = new Span<T>(bufferArray).Slice(0, length); + + return index; + } + + /// <summary> + /// Rents a buffer with a size greater than or equal to the requested size. + /// </summary> + /// <param name="length">Size of the buffer</param> + /// <param name="buffer">Array with a length greater than or equal to the requested length</param> + /// <returns>The index of the buffer on the pool</returns> + public int RentMinimum(int length, out T[] buffer) + { + if ((uint)length > MaxBufferSize) + { + buffer = new T[length]; + return -1; + } + + // Try to find a buffer that is larger or the same size of the requested one. + // This will avoid an allocation. + for (int i = 0; i < MaxBuffers; i++) + { + ref PoolItem item = ref _pool[i]; + + if (!item.InUse && item.Buffer != null && item.Buffer.Length >= length) + { + buffer = item.Buffer; + item.InUse = true; + return i; + } + } + + buffer = new T[length]; + + // Try to add the new buffer to the pool. + // We try to find a slot that is not in use, and replace the buffer in it. + for (int i = 0; i < MaxBuffers; i++) + { + ref PoolItem item = ref _pool[i]; + + if (!item.InUse) + { + item.Buffer = buffer; + item.InUse = true; + return i; + } + } + + return -1; + } + + /// <summary> + /// Returns a buffer returned from <see cref="Rent(int)"/> to the pool. + /// </summary> + /// <param name="index">Index of the buffer on the pool</param> + public void Return(int index) + { + if (index < 0) + { + return; + } + + _pool[index].InUse = false; + } + } +} diff --git a/src/Ryujinx.Graphics.Vic/Image/InputSurface.cs b/src/Ryujinx.Graphics.Vic/Image/InputSurface.cs new file mode 100644 index 00000000..15ac0460 --- /dev/null +++ b/src/Ryujinx.Graphics.Vic/Image/InputSurface.cs @@ -0,0 +1,86 @@ +using System; + +namespace Ryujinx.Graphics.Vic.Image +{ + ref struct RentedBuffer + { + public static RentedBuffer Empty => new RentedBuffer(Span<byte>.Empty, -1); + + public Span<byte> Data; + public int Index; + + public RentedBuffer(Span<byte> data, int index) + { + Data = data; + Index = index; + } + + public void Return(BufferPool<byte> pool) + { + if (Index != -1) + { + pool.Return(Index); + } + } + } + + ref struct InputSurface + { + public ReadOnlySpan<byte> Buffer0; + public ReadOnlySpan<byte> Buffer1; + public ReadOnlySpan<byte> Buffer2; + + public int Buffer0Index; + public int Buffer1Index; + public int Buffer2Index; + + public int Width; + public int Height; + + public int UvWidth; + public int UvHeight; + + public void Initialize() + { + Buffer0Index = -1; + Buffer1Index = -1; + Buffer2Index = -1; + } + + public void SetBuffer0(RentedBuffer buffer) + { + Buffer0 = buffer.Data; + Buffer0Index = buffer.Index; + } + + public void SetBuffer1(RentedBuffer buffer) + { + Buffer1 = buffer.Data; + Buffer1Index = buffer.Index; + } + + public void SetBuffer2(RentedBuffer buffer) + { + Buffer2 = buffer.Data; + Buffer2Index = buffer.Index; + } + + public void Return(BufferPool<byte> pool) + { + if (Buffer0Index != -1) + { + pool.Return(Buffer0Index); + } + + if (Buffer1Index != -1) + { + pool.Return(Buffer1Index); + } + + if (Buffer2Index != -1) + { + pool.Return(Buffer2Index); + } + } + } +} diff --git a/src/Ryujinx.Graphics.Vic/Image/Pixel.cs b/src/Ryujinx.Graphics.Vic/Image/Pixel.cs new file mode 100644 index 00000000..35f25d16 --- /dev/null +++ b/src/Ryujinx.Graphics.Vic/Image/Pixel.cs @@ -0,0 +1,10 @@ +namespace Ryujinx.Graphics.Vic.Image +{ + struct Pixel + { + public ushort R; + public ushort G; + public ushort B; + public ushort A; + } +} diff --git a/src/Ryujinx.Graphics.Vic/Image/Surface.cs b/src/Ryujinx.Graphics.Vic/Image/Surface.cs new file mode 100644 index 00000000..f393eb15 --- /dev/null +++ b/src/Ryujinx.Graphics.Vic/Image/Surface.cs @@ -0,0 +1,46 @@ +using System; +using System.Runtime.CompilerServices; + +namespace Ryujinx.Graphics.Vic.Image +{ + readonly struct Surface : IDisposable + { + private readonly int _bufferIndex; + + private readonly BufferPool<Pixel> _pool; + + public Pixel[] Data { get; } + + public int Width { get; } + public int Height { get; } + + public Surface(BufferPool<Pixel> pool, int width, int height) + { + _bufferIndex = pool.RentMinimum(width * height, out Pixel[] data); + _pool = pool; + Data = data; + Width = width; + Height = height; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public ushort GetR(int x, int y) => Data[y * Width + x].R; + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public ushort GetG(int x, int y) => Data[y * Width + x].G; + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public ushort GetB(int x, int y) => Data[y * Width + x].B; + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public ushort GetA(int x, int y) => Data[y * Width + x].A; + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void SetR(int x, int y, ushort value) => Data[y * Width + x].R = value; + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void SetG(int x, int y, ushort value) => Data[y * Width + x].G = value; + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void SetB(int x, int y, ushort value) => Data[y * Width + x].B = value; + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void SetA(int x, int y, ushort value) => Data[y * Width + x].A = value; + + public void Dispose() => _pool.Return(_bufferIndex); + } +} diff --git a/src/Ryujinx.Graphics.Vic/Image/SurfaceCommon.cs b/src/Ryujinx.Graphics.Vic/Image/SurfaceCommon.cs new file mode 100644 index 00000000..10cdefe2 --- /dev/null +++ b/src/Ryujinx.Graphics.Vic/Image/SurfaceCommon.cs @@ -0,0 +1,33 @@ +using Ryujinx.Common; +using Ryujinx.Graphics.Texture; + +namespace Ryujinx.Graphics.Vic.Image +{ + static class SurfaceCommon + { + public static int GetPitch(int width, int bytesPerPixel) + { + return BitUtils.AlignUp(width * bytesPerPixel, 256); + } + + public static int GetBlockLinearSize(int width, int height, int bytesPerPixel, int gobBlocksInY) + { + return SizeCalculator.GetBlockLinearTextureSize(width, height, 1, 1, 1, 1, 1, bytesPerPixel, gobBlocksInY, 1, 1).TotalSize; + } + + public static ulong ExtendOffset(uint offset) + { + return (ulong)offset << 8; + } + + public static ushort Upsample(byte value) + { + return (ushort)(value << 2); + } + + public static byte Downsample(ushort value) + { + return (byte)(value >> 2); + } + } +} diff --git a/src/Ryujinx.Graphics.Vic/Image/SurfaceReader.cs b/src/Ryujinx.Graphics.Vic/Image/SurfaceReader.cs new file mode 100644 index 00000000..10fd9d8d --- /dev/null +++ b/src/Ryujinx.Graphics.Vic/Image/SurfaceReader.cs @@ -0,0 +1,495 @@ +using Ryujinx.Common.Logging; +using Ryujinx.Common.Memory; +using Ryujinx.Graphics.Texture; +using Ryujinx.Graphics.Vic.Types; +using System; +using System.Runtime.CompilerServices; +using System.Runtime.Intrinsics; +using System.Runtime.Intrinsics.Arm; +using System.Runtime.Intrinsics.X86; +using static Ryujinx.Graphics.Vic.Image.SurfaceCommon; + +namespace Ryujinx.Graphics.Vic.Image +{ + static class SurfaceReader + { + public static Surface Read( + ResourceManager rm, + ref SlotConfig config, + ref SlotSurfaceConfig surfaceConfig, + ref Array8<PlaneOffsets> offsets) + { + switch (surfaceConfig.SlotPixelFormat) + { + case PixelFormat.Y8___V8U8_N420: return ReadNv12(rm, ref config, ref surfaceConfig, ref offsets); + } + + Logger.Error?.Print(LogClass.Vic, $"Unsupported pixel format \"{surfaceConfig.SlotPixelFormat}\"."); + + int lw = surfaceConfig.SlotLumaWidth + 1; + int lh = surfaceConfig.SlotLumaHeight + 1; + + return new Surface(rm.SurfacePool, lw, lh); + } + + private unsafe static Surface ReadNv12( + ResourceManager rm, + ref SlotConfig config, + ref SlotSurfaceConfig surfaceConfig, + ref Array8<PlaneOffsets> offsets) + { + InputSurface input = ReadSurface(rm, ref config, ref surfaceConfig, ref offsets, 1, 2); + + int width = input.Width; + int height = input.Height; + + int yStride = GetPitch(width, 1); + int uvStride = GetPitch(input.UvWidth, 2); + + Surface output = new Surface(rm.SurfacePool, width, height); + + if (Sse41.IsSupported) + { + Vector128<byte> shufMask = Vector128.Create( + (byte)0, (byte)2, (byte)3, (byte)1, + (byte)4, (byte)6, (byte)7, (byte)5, + (byte)8, (byte)10, (byte)11, (byte)9, + (byte)12, (byte)14, (byte)15, (byte)13); + Vector128<short> alphaMask = Vector128.Create(0xff << 24).AsInt16(); + + int yStrideGap = yStride - width; + int uvStrideGap = uvStride - input.UvWidth; + + int widthTrunc = width & ~0xf; + + fixed (Pixel* dstPtr = output.Data) + { + Pixel* op = dstPtr; + + fixed (byte* src0Ptr = input.Buffer0, src1Ptr = input.Buffer1) + { + byte* i0p = src0Ptr; + + for (int y = 0; y < height; y++) + { + byte* i1p = src1Ptr + (y >> 1) * uvStride; + + int x = 0; + + for (; x < widthTrunc; x += 16, i0p += 16, i1p += 16) + { + Vector128<short> ya0 = Sse41.ConvertToVector128Int16(i0p); + Vector128<short> ya1 = Sse41.ConvertToVector128Int16(i0p + 8); + + Vector128<byte> uv = Sse2.LoadVector128(i1p); + + Vector128<short> uv0 = Sse2.UnpackLow(uv.AsInt16(), uv.AsInt16()); + Vector128<short> uv1 = Sse2.UnpackHigh(uv.AsInt16(), uv.AsInt16()); + + Vector128<short> rgba0 = Sse2.UnpackLow(ya0, uv0); + Vector128<short> rgba1 = Sse2.UnpackHigh(ya0, uv0); + Vector128<short> rgba2 = Sse2.UnpackLow(ya1, uv1); + Vector128<short> rgba3 = Sse2.UnpackHigh(ya1, uv1); + + rgba0 = Ssse3.Shuffle(rgba0.AsByte(), shufMask).AsInt16(); + rgba1 = Ssse3.Shuffle(rgba1.AsByte(), shufMask).AsInt16(); + rgba2 = Ssse3.Shuffle(rgba2.AsByte(), shufMask).AsInt16(); + rgba3 = Ssse3.Shuffle(rgba3.AsByte(), shufMask).AsInt16(); + + rgba0 = Sse2.Or(rgba0, alphaMask); + rgba1 = Sse2.Or(rgba1, alphaMask); + rgba2 = Sse2.Or(rgba2, alphaMask); + rgba3 = Sse2.Or(rgba3, alphaMask); + + Vector128<short> rgba16_0 = Sse41.ConvertToVector128Int16(rgba0.AsByte()); + Vector128<short> rgba16_1 = Sse41.ConvertToVector128Int16(HighToLow(rgba0.AsByte())); + Vector128<short> rgba16_2 = Sse41.ConvertToVector128Int16(rgba1.AsByte()); + Vector128<short> rgba16_3 = Sse41.ConvertToVector128Int16(HighToLow(rgba1.AsByte())); + Vector128<short> rgba16_4 = Sse41.ConvertToVector128Int16(rgba2.AsByte()); + Vector128<short> rgba16_5 = Sse41.ConvertToVector128Int16(HighToLow(rgba2.AsByte())); + Vector128<short> rgba16_6 = Sse41.ConvertToVector128Int16(rgba3.AsByte()); + Vector128<short> rgba16_7 = Sse41.ConvertToVector128Int16(HighToLow(rgba3.AsByte())); + + rgba16_0 = Sse2.ShiftLeftLogical(rgba16_0, 2); + rgba16_1 = Sse2.ShiftLeftLogical(rgba16_1, 2); + rgba16_2 = Sse2.ShiftLeftLogical(rgba16_2, 2); + rgba16_3 = Sse2.ShiftLeftLogical(rgba16_3, 2); + rgba16_4 = Sse2.ShiftLeftLogical(rgba16_4, 2); + rgba16_5 = Sse2.ShiftLeftLogical(rgba16_5, 2); + rgba16_6 = Sse2.ShiftLeftLogical(rgba16_6, 2); + rgba16_7 = Sse2.ShiftLeftLogical(rgba16_7, 2); + + Sse2.Store((short*)(op + (uint)x + 0), rgba16_0); + Sse2.Store((short*)(op + (uint)x + 2), rgba16_1); + Sse2.Store((short*)(op + (uint)x + 4), rgba16_2); + Sse2.Store((short*)(op + (uint)x + 6), rgba16_3); + Sse2.Store((short*)(op + (uint)x + 8), rgba16_4); + Sse2.Store((short*)(op + (uint)x + 10), rgba16_5); + Sse2.Store((short*)(op + (uint)x + 12), rgba16_6); + Sse2.Store((short*)(op + (uint)x + 14), rgba16_7); + } + + for (; x < width; x++, i1p += (x & 1) * 2) + { + Pixel* px = op + (uint)x; + + px->R = Upsample(*i0p++); + px->G = Upsample(*i1p); + px->B = Upsample(*(i1p + 1)); + px->A = 0x3ff; + } + + op += width; + i0p += yStrideGap; + i1p += uvStrideGap; + } + } + } + } + else if (AdvSimd.Arm64.IsSupported) + { + Vector128<int> alphaMask = Vector128.Create(0xffu << 24).AsInt32(); + + int yStrideGap = yStride - width; + int uvStrideGap = uvStride - input.UvWidth; + + int widthTrunc = width & ~0xf; + + fixed (Pixel* dstPtr = output.Data) + { + Pixel* op = dstPtr; + + fixed (byte* src0Ptr = input.Buffer0, src1Ptr = input.Buffer1) + { + byte* i0p = src0Ptr; + + for (int y = 0; y < height; y++) + { + byte* i1p = src1Ptr + (y >> 1) * uvStride; + + int x = 0; + + for (; x < widthTrunc; x += 16, i0p += 16, i1p += 16) + { + Vector128<byte> ya = AdvSimd.LoadVector128(i0p); + Vector128<byte> uv = AdvSimd.LoadVector128(i1p); + + Vector128<short> ya0 = AdvSimd.ZeroExtendWideningLower(ya.GetLower()).AsInt16(); + Vector128<short> ya1 = AdvSimd.ZeroExtendWideningUpper(ya).AsInt16(); + + Vector128<short> uv0 = AdvSimd.Arm64.ZipLow(uv.AsInt16(), uv.AsInt16()); + Vector128<short> uv1 = AdvSimd.Arm64.ZipHigh(uv.AsInt16(), uv.AsInt16()); + + ya0 = AdvSimd.ShiftLeftLogical(ya0, 8); + ya1 = AdvSimd.ShiftLeftLogical(ya1, 8); + + Vector128<short> rgba0 = AdvSimd.Arm64.ZipLow(ya0, uv0); + Vector128<short> rgba1 = AdvSimd.Arm64.ZipHigh(ya0, uv0); + Vector128<short> rgba2 = AdvSimd.Arm64.ZipLow(ya1, uv1); + Vector128<short> rgba3 = AdvSimd.Arm64.ZipHigh(ya1, uv1); + + rgba0 = AdvSimd.ShiftRightLogicalAdd(alphaMask, rgba0.AsInt32(), 8).AsInt16(); + rgba1 = AdvSimd.ShiftRightLogicalAdd(alphaMask, rgba1.AsInt32(), 8).AsInt16(); + rgba2 = AdvSimd.ShiftRightLogicalAdd(alphaMask, rgba2.AsInt32(), 8).AsInt16(); + rgba3 = AdvSimd.ShiftRightLogicalAdd(alphaMask, rgba3.AsInt32(), 8).AsInt16(); + + Vector128<short> rgba16_0 = AdvSimd.ZeroExtendWideningLower(rgba0.AsByte().GetLower()).AsInt16(); + Vector128<short> rgba16_1 = AdvSimd.ZeroExtendWideningUpper(rgba0.AsByte()).AsInt16(); + Vector128<short> rgba16_2 = AdvSimd.ZeroExtendWideningLower(rgba1.AsByte().GetLower()).AsInt16(); + Vector128<short> rgba16_3 = AdvSimd.ZeroExtendWideningUpper(rgba1.AsByte()).AsInt16(); + Vector128<short> rgba16_4 = AdvSimd.ZeroExtendWideningLower(rgba2.AsByte().GetLower()).AsInt16(); + Vector128<short> rgba16_5 = AdvSimd.ZeroExtendWideningUpper(rgba2.AsByte()).AsInt16(); + Vector128<short> rgba16_6 = AdvSimd.ZeroExtendWideningLower(rgba3.AsByte().GetLower()).AsInt16(); + Vector128<short> rgba16_7 = AdvSimd.ZeroExtendWideningUpper(rgba3.AsByte()).AsInt16(); + + rgba16_0 = AdvSimd.ShiftLeftLogical(rgba16_0, 2); + rgba16_1 = AdvSimd.ShiftLeftLogical(rgba16_1, 2); + rgba16_2 = AdvSimd.ShiftLeftLogical(rgba16_2, 2); + rgba16_3 = AdvSimd.ShiftLeftLogical(rgba16_3, 2); + rgba16_4 = AdvSimd.ShiftLeftLogical(rgba16_4, 2); + rgba16_5 = AdvSimd.ShiftLeftLogical(rgba16_5, 2); + rgba16_6 = AdvSimd.ShiftLeftLogical(rgba16_6, 2); + rgba16_7 = AdvSimd.ShiftLeftLogical(rgba16_7, 2); + + AdvSimd.Store((short*)(op + (uint)x + 0), rgba16_0); + AdvSimd.Store((short*)(op + (uint)x + 2), rgba16_1); + AdvSimd.Store((short*)(op + (uint)x + 4), rgba16_2); + AdvSimd.Store((short*)(op + (uint)x + 6), rgba16_3); + AdvSimd.Store((short*)(op + (uint)x + 8), rgba16_4); + AdvSimd.Store((short*)(op + (uint)x + 10), rgba16_5); + AdvSimd.Store((short*)(op + (uint)x + 12), rgba16_6); + AdvSimd.Store((short*)(op + (uint)x + 14), rgba16_7); + } + + for (; x < width; x++, i1p += (x & 1) * 2) + { + Pixel* px = op + (uint)x; + + px->R = Upsample(*i0p++); + px->G = Upsample(*i1p); + px->B = Upsample(*(i1p + 1)); + px->A = 0x3ff; + } + + op += width; + i0p += yStrideGap; + i1p += uvStrideGap; + } + } + } + } + else + { + for (int y = 0; y < height; y++) + { + int uvBase = (y >> 1) * uvStride; + + for (int x = 0; x < width; x++) + { + output.SetR(x, y, Upsample(input.Buffer0[y * yStride + x])); + + int uvOffs = uvBase + (x & ~1); + + output.SetG(x, y, Upsample(input.Buffer1[uvOffs])); + output.SetB(x, y, Upsample(input.Buffer1[uvOffs + 1])); + output.SetA(x, y, 0x3ff); + } + } + } + + input.Return(rm.BufferPool); + + return output; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static Vector128<byte> HighToLow(Vector128<byte> value) + { + return Sse.MoveHighToLow(value.AsSingle(), value.AsSingle()).AsByte(); + } + + private static InputSurface ReadSurface( + ResourceManager rm, + ref SlotConfig config, + ref SlotSurfaceConfig surfaceConfig, + ref Array8<PlaneOffsets> offsets, + int bytesPerPixel, + int planes) + { + InputSurface surface = new InputSurface(); + + surface.Initialize(); + + int gobBlocksInY = 1 << surfaceConfig.SlotBlkHeight; + + bool linear = surfaceConfig.SlotBlkKind == 0; + + int lw = surfaceConfig.SlotLumaWidth + 1; + int lh = surfaceConfig.SlotLumaHeight + 1; + + int cw = surfaceConfig.SlotChromaWidth + 1; + int ch = surfaceConfig.SlotChromaHeight + 1; + + // Interlaced inputs have double the height when deinterlaced. + int heightShift = config.FrameFormat.IsField() ? 1 : 0; + + surface.Width = lw; + surface.Height = lh << heightShift; + surface.UvWidth = cw; + surface.UvHeight = ch << heightShift; + + if (planes > 0) + { + surface.SetBuffer0(ReadBuffer(rm, ref config, ref offsets, linear, 0, lw, lh, bytesPerPixel, gobBlocksInY)); + } + + if (planes > 1) + { + surface.SetBuffer1(ReadBuffer(rm, ref config, ref offsets, linear, 1, cw, ch, planes == 2 ? 2 : 1, gobBlocksInY)); + } + + if (planes > 2) + { + surface.SetBuffer2(ReadBuffer(rm, ref config, ref offsets, linear, 2, cw, ch, 1, gobBlocksInY)); + } + + return surface; + } + + private static RentedBuffer ReadBuffer( + ResourceManager rm, + scoped ref SlotConfig config, + scoped ref Array8<PlaneOffsets> offsets, + bool linear, + int plane, + int width, + int height, + int bytesPerPixel, + int gobBlocksInY) + { + FrameFormat frameFormat = config.FrameFormat; + bool isLuma = plane == 0; + bool isField = frameFormat.IsField(); + bool isTopField = frameFormat.IsTopField(isLuma); + int stride = GetPitch(width, bytesPerPixel); + uint offset = GetOffset(ref offsets[0], plane); + + int dstStart = 0; + int dstStride = stride; + + if (isField) + { + dstStart = isTopField ? 0 : stride; + dstStride = stride * 2; + } + + RentedBuffer buffer; + + if (linear) + { + buffer = ReadBufferLinear(rm, offset, width, height, dstStart, dstStride, bytesPerPixel); + } + else + { + buffer = ReadBufferBlockLinear(rm, offset, width, height, dstStart, dstStride, bytesPerPixel, gobBlocksInY); + } + + if (isField || frameFormat.IsInterlaced()) + { + RentedBuffer prevBuffer = RentedBuffer.Empty; + RentedBuffer nextBuffer = RentedBuffer.Empty; + + if (config.PrevFieldEnable) + { + prevBuffer = ReadBufferNoDeinterlace(rm, ref offsets[1], linear, plane, width, height, bytesPerPixel, gobBlocksInY); + } + + if (config.NextFieldEnable) + { + nextBuffer = ReadBufferNoDeinterlace(rm, ref offsets[2], linear, plane, width, height, bytesPerPixel, gobBlocksInY); + } + + int w = width * bytesPerPixel; + + switch (config.DeinterlaceMode) + { + case DeinterlaceMode.Weave: + Scaler.DeinterlaceWeave(buffer.Data, prevBuffer.Data, w, stride, isTopField); + break; + case DeinterlaceMode.BobField: + Scaler.DeinterlaceBob(buffer.Data, w, stride, isTopField); + break; + case DeinterlaceMode.Bob: + bool isCurrentTop = isLuma ? config.IsEven : config.ChromaEven; + Scaler.DeinterlaceBob(buffer.Data, w, stride, isCurrentTop ^ frameFormat.IsInterlacedBottomFirst()); + break; + case DeinterlaceMode.NewBob: + case DeinterlaceMode.Disi1: + Scaler.DeinterlaceMotionAdaptive(buffer.Data, prevBuffer.Data, nextBuffer.Data, w, stride, isTopField); + break; + case DeinterlaceMode.WeaveLumaBobFieldChroma: + if (isLuma) + { + Scaler.DeinterlaceWeave(buffer.Data, prevBuffer.Data, w, stride, isTopField); + } + else + { + Scaler.DeinterlaceBob(buffer.Data, w, stride, isTopField); + } + break; + default: + Logger.Error?.Print(LogClass.Vic, $"Unsupported deinterlace mode \"{config.DeinterlaceMode}\"."); + break; + } + + prevBuffer.Return(rm.BufferPool); + nextBuffer.Return(rm.BufferPool); + } + + return buffer; + } + + private static uint GetOffset(ref PlaneOffsets offsets, int plane) + { + return plane switch + { + 0 => offsets.LumaOffset, + 1 => offsets.ChromaUOffset, + 2 => offsets.ChromaVOffset, + _ => throw new ArgumentOutOfRangeException(nameof(plane)) + }; + } + + private static RentedBuffer ReadBufferNoDeinterlace( + ResourceManager rm, + ref PlaneOffsets offsets, + bool linear, + int plane, + int width, + int height, + int bytesPerPixel, + int gobBlocksInY) + { + int stride = GetPitch(width, bytesPerPixel); + uint offset = GetOffset(ref offsets, plane); + + if (linear) + { + return ReadBufferLinear(rm, offset, width, height, 0, stride, bytesPerPixel); + } + + return ReadBufferBlockLinear(rm, offset, width, height, 0, stride, bytesPerPixel, gobBlocksInY); + } + + private static RentedBuffer ReadBufferLinear( + ResourceManager rm, + uint offset, + int width, + int height, + int dstStart, + int dstStride, + int bytesPerPixel) + { + int srcStride = GetPitch(width, bytesPerPixel); + int inSize = srcStride * height; + + ReadOnlySpan<byte> src = rm.Gmm.GetSpan(ExtendOffset(offset), inSize); + + int outSize = dstStride * height; + int bufferIndex = rm.BufferPool.RentMinimum(outSize, out byte[] buffer); + Span<byte> dst = buffer; + dst = dst.Slice(0, outSize); + + for (int y = 0; y < height; y++) + { + src.Slice(y * srcStride, srcStride).CopyTo(dst.Slice(dstStart + y * dstStride, srcStride)); + } + + return new RentedBuffer(dst, bufferIndex); + } + + private static RentedBuffer ReadBufferBlockLinear( + ResourceManager rm, + uint offset, + int width, + int height, + int dstStart, + int dstStride, + int bytesPerPixel, + int gobBlocksInY) + { + int inSize = GetBlockLinearSize(width, height, bytesPerPixel, gobBlocksInY); + + ReadOnlySpan<byte> src = rm.Gmm.GetSpan(ExtendOffset(offset), inSize); + + int outSize = dstStride * height; + int bufferIndex = rm.BufferPool.RentMinimum(outSize, out byte[] buffer); + Span<byte> dst = buffer; + dst = dst.Slice(0, outSize); + + LayoutConverter.ConvertBlockLinearToLinear(dst.Slice(dstStart), width, height, dstStride, bytesPerPixel, gobBlocksInY, src); + + return new RentedBuffer(dst, bufferIndex); + } + } +} diff --git a/src/Ryujinx.Graphics.Vic/Image/SurfaceWriter.cs b/src/Ryujinx.Graphics.Vic/Image/SurfaceWriter.cs new file mode 100644 index 00000000..37d261f9 --- /dev/null +++ b/src/Ryujinx.Graphics.Vic/Image/SurfaceWriter.cs @@ -0,0 +1,667 @@ +using Ryujinx.Common.Logging; +using Ryujinx.Graphics.Texture; +using Ryujinx.Graphics.Vic.Types; +using System; +using System.Runtime.Intrinsics; +using System.Runtime.Intrinsics.Arm; +using System.Runtime.Intrinsics.X86; +using static Ryujinx.Graphics.Vic.Image.SurfaceCommon; + +namespace Ryujinx.Graphics.Vic.Image +{ + class SurfaceWriter + { + public static void Write(ResourceManager rm, Surface input, ref OutputSurfaceConfig config, ref PlaneOffsets offsets) + { + switch (config.OutPixelFormat) + { + case PixelFormat.A8B8G8R8: + case PixelFormat.X8B8G8R8: + WriteA8B8G8R8(rm, input, ref config, ref offsets); + break; + case PixelFormat.A8R8G8B8: + WriteA8R8G8B8(rm, input, ref config, ref offsets); + break; + case PixelFormat.Y8___V8U8_N420: + WriteNv12(rm, input, ref config, ref offsets); + break; + default: + Logger.Error?.Print(LogClass.Vic, $"Unsupported pixel format \"{config.OutPixelFormat}\"."); + break; + } + } + + private unsafe static void WriteA8B8G8R8(ResourceManager rm, Surface input, ref OutputSurfaceConfig config, ref PlaneOffsets offsets) + { + int width = input.Width; + int height = input.Height; + int stride = GetPitch(width, 4); + + int dstIndex = rm.BufferPool.Rent(height * stride, out Span<byte> dst); + + if (Sse2.IsSupported) + { + int widthTrunc = width & ~7; + int strideGap = stride - width * 4; + + fixed (Pixel* srcPtr = input.Data) + { + Pixel* ip = srcPtr; + + fixed (byte* dstPtr = dst) + { + byte* op = dstPtr; + + for (int y = 0; y < height; y++, ip += input.Width) + { + int x = 0; + + for (; x < widthTrunc; x += 8) + { + Vector128<ushort> pixel12 = Sse2.LoadVector128((ushort*)(ip + (uint)x)); + Vector128<ushort> pixel34 = Sse2.LoadVector128((ushort*)(ip + (uint)x + 2)); + Vector128<ushort> pixel56 = Sse2.LoadVector128((ushort*)(ip + (uint)x + 4)); + Vector128<ushort> pixel78 = Sse2.LoadVector128((ushort*)(ip + (uint)x + 6)); + + pixel12 = Sse2.ShiftRightLogical(pixel12, 2); + pixel34 = Sse2.ShiftRightLogical(pixel34, 2); + pixel56 = Sse2.ShiftRightLogical(pixel56, 2); + pixel78 = Sse2.ShiftRightLogical(pixel78, 2); + + Vector128<byte> pixel1234 = Sse2.PackUnsignedSaturate(pixel12.AsInt16(), pixel34.AsInt16()); + Vector128<byte> pixel5678 = Sse2.PackUnsignedSaturate(pixel56.AsInt16(), pixel78.AsInt16()); + + Sse2.Store(op + 0x00, pixel1234); + Sse2.Store(op + 0x10, pixel5678); + + op += 0x20; + } + + for (; x < width; x++) + { + Pixel* px = ip + (uint)x; + + *(op + 0) = Downsample(px->R); + *(op + 1) = Downsample(px->G); + *(op + 2) = Downsample(px->B); + *(op + 3) = Downsample(px->A); + + op += 4; + } + + op += strideGap; + } + } + } + } + else if (AdvSimd.IsSupported) + { + int widthTrunc = width & ~7; + int strideGap = stride - width * 4; + + fixed (Pixel* srcPtr = input.Data) + { + Pixel* ip = srcPtr; + + fixed (byte* dstPtr = dst) + { + byte* op = dstPtr; + + for (int y = 0; y < height; y++, ip += input.Width) + { + int x = 0; + + for (; x < widthTrunc; x += 8) + { + Vector128<ushort> pixel12 = AdvSimd.LoadVector128((ushort*)(ip + (uint)x)); + Vector128<ushort> pixel34 = AdvSimd.LoadVector128((ushort*)(ip + (uint)x + 2)); + Vector128<ushort> pixel56 = AdvSimd.LoadVector128((ushort*)(ip + (uint)x + 4)); + Vector128<ushort> pixel78 = AdvSimd.LoadVector128((ushort*)(ip + (uint)x + 6)); + + pixel12 = AdvSimd.ShiftRightLogical(pixel12, 2); + pixel34 = AdvSimd.ShiftRightLogical(pixel34, 2); + pixel56 = AdvSimd.ShiftRightLogical(pixel56, 2); + pixel78 = AdvSimd.ShiftRightLogical(pixel78, 2); + + Vector64<byte> lower12 = AdvSimd.ExtractNarrowingLower(pixel12.AsUInt16()); + Vector64<byte> lower56 = AdvSimd.ExtractNarrowingLower(pixel56.AsUInt16()); + + Vector128<byte> pixel1234 = AdvSimd.ExtractNarrowingUpper(lower12, pixel34.AsUInt16()); + Vector128<byte> pixel5678 = AdvSimd.ExtractNarrowingUpper(lower56, pixel78.AsUInt16()); + + AdvSimd.Store(op + 0x00, pixel1234); + AdvSimd.Store(op + 0x10, pixel5678); + + op += 0x20; + } + + for (; x < width; x++) + { + Pixel* px = ip + (uint)x; + + *(op + 0) = Downsample(px->R); + *(op + 1) = Downsample(px->G); + *(op + 2) = Downsample(px->B); + *(op + 3) = Downsample(px->A); + + op += 4; + } + + op += strideGap; + } + } + } + } + else + { + for (int y = 0; y < height; y++) + { + int baseOffs = y * stride; + + for (int x = 0; x < width; x++) + { + int offs = baseOffs + x * 4; + + dst[offs + 0] = Downsample(input.GetR(x, y)); + dst[offs + 1] = Downsample(input.GetG(x, y)); + dst[offs + 2] = Downsample(input.GetB(x, y)); + dst[offs + 3] = Downsample(input.GetA(x, y)); + } + } + } + + bool outLinear = config.OutBlkKind == 0; + + int gobBlocksInY = 1 << config.OutBlkHeight; + + WriteBuffer(rm, dst, offsets.LumaOffset, outLinear, width, height, 4, gobBlocksInY); + + rm.BufferPool.Return(dstIndex); + } + + private unsafe static void WriteA8R8G8B8(ResourceManager rm, Surface input, ref OutputSurfaceConfig config, ref PlaneOffsets offsets) + { + int width = input.Width; + int height = input.Height; + int stride = GetPitch(width, 4); + + int dstIndex = rm.BufferPool.Rent(height * stride, out Span<byte> dst); + + if (Ssse3.IsSupported) + { + Vector128<byte> shuffleMask = Vector128.Create( + (byte)2, (byte)1, (byte)0, (byte)3, + (byte)6, (byte)5, (byte)4, (byte)7, + (byte)10, (byte)9, (byte)8, (byte)11, + (byte)14, (byte)13, (byte)12, (byte)15); + + int widthTrunc = width & ~7; + int strideGap = stride - width * 4; + + fixed (Pixel* srcPtr = input.Data) + { + Pixel* ip = srcPtr; + + fixed (byte* dstPtr = dst) + { + byte* op = dstPtr; + + for (int y = 0; y < height; y++, ip += input.Width) + { + int x = 0; + + for (; x < widthTrunc; x += 8) + { + Vector128<ushort> pixel12 = Sse2.LoadVector128((ushort*)(ip + (uint)x)); + Vector128<ushort> pixel34 = Sse2.LoadVector128((ushort*)(ip + (uint)x + 2)); + Vector128<ushort> pixel56 = Sse2.LoadVector128((ushort*)(ip + (uint)x + 4)); + Vector128<ushort> pixel78 = Sse2.LoadVector128((ushort*)(ip + (uint)x + 6)); + + pixel12 = Sse2.ShiftRightLogical(pixel12, 2); + pixel34 = Sse2.ShiftRightLogical(pixel34, 2); + pixel56 = Sse2.ShiftRightLogical(pixel56, 2); + pixel78 = Sse2.ShiftRightLogical(pixel78, 2); + + Vector128<byte> pixel1234 = Sse2.PackUnsignedSaturate(pixel12.AsInt16(), pixel34.AsInt16()); + Vector128<byte> pixel5678 = Sse2.PackUnsignedSaturate(pixel56.AsInt16(), pixel78.AsInt16()); + + pixel1234 = Ssse3.Shuffle(pixel1234, shuffleMask); + pixel5678 = Ssse3.Shuffle(pixel5678, shuffleMask); + + Sse2.Store(op + 0x00, pixel1234); + Sse2.Store(op + 0x10, pixel5678); + + op += 0x20; + } + + for (; x < width; x++) + { + Pixel* px = ip + (uint)x; + + *(op + 0) = Downsample(px->B); + *(op + 1) = Downsample(px->G); + *(op + 2) = Downsample(px->R); + *(op + 3) = Downsample(px->A); + + op += 4; + } + + op += strideGap; + } + } + } + } + else + { + for (int y = 0; y < height; y++) + { + int baseOffs = y * stride; + + for (int x = 0; x < width; x++) + { + int offs = baseOffs + x * 4; + + dst[offs + 0] = Downsample(input.GetB(x, y)); + dst[offs + 1] = Downsample(input.GetG(x, y)); + dst[offs + 2] = Downsample(input.GetR(x, y)); + dst[offs + 3] = Downsample(input.GetA(x, y)); + } + } + } + + bool outLinear = config.OutBlkKind == 0; + + int gobBlocksInY = 1 << config.OutBlkHeight; + + WriteBuffer(rm, dst, offsets.LumaOffset, outLinear, width, height, 4, gobBlocksInY); + + rm.BufferPool.Return(dstIndex); + } + + private unsafe static void WriteNv12(ResourceManager rm, Surface input, ref OutputSurfaceConfig config, ref PlaneOffsets offsets) + { + int gobBlocksInY = 1 << config.OutBlkHeight; + + bool outLinear = config.OutBlkKind == 0; + + int width = Math.Min(config.OutLumaWidth + 1, input.Width); + int height = Math.Min(config.OutLumaHeight + 1, input.Height); + int yStride = GetPitch(config.OutLumaWidth + 1, 1); + + int dstYIndex = rm.BufferPool.Rent((config.OutLumaHeight + 1) * yStride, out Span<byte> dstY); + + if (Sse41.IsSupported) + { + Vector128<ushort> mask = Vector128.Create(0xffffUL).AsUInt16(); + + int widthTrunc = width & ~0xf; + int strideGap = yStride - width; + + fixed (Pixel* srcPtr = input.Data) + { + Pixel* ip = srcPtr; + + fixed (byte* dstPtr = dstY) + { + byte* op = dstPtr; + + for (int y = 0; y < height; y++, ip += input.Width) + { + int x = 0; + + for (; x < widthTrunc; x += 16) + { + byte* baseOffset = (byte*)(ip + (ulong)(uint)x); + + Vector128<ushort> pixelp1 = Sse2.LoadVector128((ushort*)baseOffset); + Vector128<ushort> pixelp2 = Sse2.LoadVector128((ushort*)(baseOffset + 0x10)); + Vector128<ushort> pixelp3 = Sse2.LoadVector128((ushort*)(baseOffset + 0x20)); + Vector128<ushort> pixelp4 = Sse2.LoadVector128((ushort*)(baseOffset + 0x30)); + Vector128<ushort> pixelp5 = Sse2.LoadVector128((ushort*)(baseOffset + 0x40)); + Vector128<ushort> pixelp6 = Sse2.LoadVector128((ushort*)(baseOffset + 0x50)); + Vector128<ushort> pixelp7 = Sse2.LoadVector128((ushort*)(baseOffset + 0x60)); + Vector128<ushort> pixelp8 = Sse2.LoadVector128((ushort*)(baseOffset + 0x70)); + + pixelp1 = Sse2.And(pixelp1, mask); + pixelp2 = Sse2.And(pixelp2, mask); + pixelp3 = Sse2.And(pixelp3, mask); + pixelp4 = Sse2.And(pixelp4, mask); + pixelp5 = Sse2.And(pixelp5, mask); + pixelp6 = Sse2.And(pixelp6, mask); + pixelp7 = Sse2.And(pixelp7, mask); + pixelp8 = Sse2.And(pixelp8, mask); + + Vector128<ushort> pixelq1 = Sse41.PackUnsignedSaturate(pixelp1.AsInt32(), pixelp2.AsInt32()); + Vector128<ushort> pixelq2 = Sse41.PackUnsignedSaturate(pixelp3.AsInt32(), pixelp4.AsInt32()); + Vector128<ushort> pixelq3 = Sse41.PackUnsignedSaturate(pixelp5.AsInt32(), pixelp6.AsInt32()); + Vector128<ushort> pixelq4 = Sse41.PackUnsignedSaturate(pixelp7.AsInt32(), pixelp8.AsInt32()); + + pixelq1 = Sse41.PackUnsignedSaturate(pixelq1.AsInt32(), pixelq2.AsInt32()); + pixelq2 = Sse41.PackUnsignedSaturate(pixelq3.AsInt32(), pixelq4.AsInt32()); + + pixelq1 = Sse2.ShiftRightLogical(pixelq1, 2); + pixelq2 = Sse2.ShiftRightLogical(pixelq2, 2); + + Vector128<byte> pixel = Sse2.PackUnsignedSaturate(pixelq1.AsInt16(), pixelq2.AsInt16()); + + Sse2.Store(op, pixel); + + op += 0x10; + } + + for (; x < width; x++) + { + Pixel* px = ip + (uint)x; + + *op++ = Downsample(px->R); + } + + op += strideGap; + } + } + } + } + else if (AdvSimd.IsSupported) + { + Vector128<ushort> mask = Vector128.Create(0xffffUL).AsUInt16(); + + int widthTrunc = width & ~0xf; + int strideGap = yStride - width; + + fixed (Pixel* srcPtr = input.Data) + { + Pixel* ip = srcPtr; + + fixed (byte* dstPtr = dstY) + { + byte* op = dstPtr; + + for (int y = 0; y < height; y++, ip += input.Width) + { + int x = 0; + + for (; x < widthTrunc; x += 16) + { + byte* baseOffset = (byte*)(ip + (ulong)(uint)x); + + Vector128<ushort> pixelp1 = AdvSimd.LoadVector128((ushort*)baseOffset); + Vector128<ushort> pixelp2 = AdvSimd.LoadVector128((ushort*)(baseOffset + 0x10)); + Vector128<ushort> pixelp3 = AdvSimd.LoadVector128((ushort*)(baseOffset + 0x20)); + Vector128<ushort> pixelp4 = AdvSimd.LoadVector128((ushort*)(baseOffset + 0x30)); + Vector128<ushort> pixelp5 = AdvSimd.LoadVector128((ushort*)(baseOffset + 0x40)); + Vector128<ushort> pixelp6 = AdvSimd.LoadVector128((ushort*)(baseOffset + 0x50)); + Vector128<ushort> pixelp7 = AdvSimd.LoadVector128((ushort*)(baseOffset + 0x60)); + Vector128<ushort> pixelp8 = AdvSimd.LoadVector128((ushort*)(baseOffset + 0x70)); + + pixelp1 = AdvSimd.And(pixelp1, mask); + pixelp2 = AdvSimd.And(pixelp2, mask); + pixelp3 = AdvSimd.And(pixelp3, mask); + pixelp4 = AdvSimd.And(pixelp4, mask); + pixelp5 = AdvSimd.And(pixelp5, mask); + pixelp6 = AdvSimd.And(pixelp6, mask); + pixelp7 = AdvSimd.And(pixelp7, mask); + pixelp8 = AdvSimd.And(pixelp8, mask); + + Vector64<ushort> lowerp1 = AdvSimd.ExtractNarrowingLower(pixelp1.AsUInt32()); + Vector64<ushort> lowerp3 = AdvSimd.ExtractNarrowingLower(pixelp3.AsUInt32()); + Vector64<ushort> lowerp5 = AdvSimd.ExtractNarrowingLower(pixelp5.AsUInt32()); + Vector64<ushort> lowerp7 = AdvSimd.ExtractNarrowingLower(pixelp7.AsUInt32()); + + Vector128<ushort> pixelq1 = AdvSimd.ExtractNarrowingUpper(lowerp1, pixelp2.AsUInt32()); + Vector128<ushort> pixelq2 = AdvSimd.ExtractNarrowingUpper(lowerp3, pixelp4.AsUInt32()); + Vector128<ushort> pixelq3 = AdvSimd.ExtractNarrowingUpper(lowerp5, pixelp6.AsUInt32()); + Vector128<ushort> pixelq4 = AdvSimd.ExtractNarrowingUpper(lowerp7, pixelp8.AsUInt32()); + + Vector64<ushort> lowerq1 = AdvSimd.ExtractNarrowingLower(pixelq1.AsUInt32()); + Vector64<ushort> lowerq3 = AdvSimd.ExtractNarrowingLower(pixelq3.AsUInt32()); + + pixelq1 = AdvSimd.ExtractNarrowingUpper(lowerq1, pixelq2.AsUInt32()); + pixelq2 = AdvSimd.ExtractNarrowingUpper(lowerq3, pixelq4.AsUInt32()); + + pixelq1 = AdvSimd.ShiftRightLogical(pixelq1, 2); + pixelq2 = AdvSimd.ShiftRightLogical(pixelq2, 2); + + Vector64<byte> pixelLower = AdvSimd.ExtractNarrowingLower(pixelq1.AsUInt16()); + + Vector128<byte> pixel = AdvSimd.ExtractNarrowingUpper(pixelLower, pixelq2.AsUInt16()); + + AdvSimd.Store(op, pixel); + + op += 0x10; + } + + for (; x < width; x++) + { + Pixel* px = ip + (uint)x; + + *op++ = Downsample(px->R); + } + + op += strideGap; + } + } + } + } + else + { + for (int y = 0; y < height; y++) + { + for (int x = 0; x < width; x++) + { + dstY[y * yStride + x] = Downsample(input.GetR(x, y)); + } + } + } + + WriteBuffer( + rm, + dstY, + offsets.LumaOffset, + outLinear, + config.OutLumaWidth + 1, + config.OutLumaHeight + 1, + 1, + gobBlocksInY); + + rm.BufferPool.Return(dstYIndex); + + int uvWidth = Math.Min(config.OutChromaWidth + 1, (width + 1) >> 1); + int uvHeight = Math.Min(config.OutChromaHeight + 1, (height + 1) >> 1); + int uvStride = GetPitch(config.OutChromaWidth + 1, 2); + + int dstUvIndex = rm.BufferPool.Rent((config.OutChromaHeight + 1) * uvStride, out Span<byte> dstUv); + + if (Sse2.IsSupported) + { + int widthTrunc = uvWidth & ~7; + int strideGap = uvStride - uvWidth * 2; + + fixed (Pixel* srcPtr = input.Data) + { + Pixel* ip = srcPtr; + + fixed (byte* dstPtr = dstUv) + { + byte* op = dstPtr; + + for (int y = 0; y < uvHeight; y++, ip += input.Width * 2) + { + int x = 0; + + for (; x < widthTrunc; x += 8) + { + byte* baseOffset = (byte*)ip + (ulong)(uint)x * 16; + + Vector128<uint> pixel1 = Sse2.LoadScalarVector128((uint*)(baseOffset + 0x02)); + Vector128<uint> pixel2 = Sse2.LoadScalarVector128((uint*)(baseOffset + 0x12)); + Vector128<uint> pixel3 = Sse2.LoadScalarVector128((uint*)(baseOffset + 0x22)); + Vector128<uint> pixel4 = Sse2.LoadScalarVector128((uint*)(baseOffset + 0x32)); + Vector128<uint> pixel5 = Sse2.LoadScalarVector128((uint*)(baseOffset + 0x42)); + Vector128<uint> pixel6 = Sse2.LoadScalarVector128((uint*)(baseOffset + 0x52)); + Vector128<uint> pixel7 = Sse2.LoadScalarVector128((uint*)(baseOffset + 0x62)); + Vector128<uint> pixel8 = Sse2.LoadScalarVector128((uint*)(baseOffset + 0x72)); + + Vector128<uint> pixel12 = Sse2.UnpackLow(pixel1, pixel2); + Vector128<uint> pixel34 = Sse2.UnpackLow(pixel3, pixel4); + Vector128<uint> pixel56 = Sse2.UnpackLow(pixel5, pixel6); + Vector128<uint> pixel78 = Sse2.UnpackLow(pixel7, pixel8); + + Vector128<ulong> pixel1234 = Sse2.UnpackLow(pixel12.AsUInt64(), pixel34.AsUInt64()); + Vector128<ulong> pixel5678 = Sse2.UnpackLow(pixel56.AsUInt64(), pixel78.AsUInt64()); + + pixel1234 = Sse2.ShiftRightLogical(pixel1234, 2); + pixel5678 = Sse2.ShiftRightLogical(pixel5678, 2); + + Vector128<byte> pixel = Sse2.PackUnsignedSaturate(pixel1234.AsInt16(), pixel5678.AsInt16()); + + Sse2.Store(op, pixel); + + op += 0x10; + } + + for (; x < uvWidth; x++) + { + Pixel* px = ip + (uint)(x << 1); + + *op++ = Downsample(px->G); + *op++ = Downsample(px->B); + } + + op += strideGap; + } + } + } + } + else if (AdvSimd.Arm64.IsSupported) + { + int widthTrunc = uvWidth & ~7; + int strideGap = uvStride - uvWidth * 2; + + fixed (Pixel* srcPtr = input.Data) + { + Pixel* ip = srcPtr; + + fixed (byte* dstPtr = dstUv) + { + byte* op = dstPtr; + + for (int y = 0; y < uvHeight; y++, ip += input.Width * 2) + { + int x = 0; + + for (; x < widthTrunc; x += 8) + { + byte* baseOffset = (byte*)ip + (ulong)(uint)x * 16; + + Vector128<uint> pixel1 = AdvSimd.LoadAndReplicateToVector128((uint*)(baseOffset + 0x02)); + Vector128<uint> pixel2 = AdvSimd.LoadAndReplicateToVector128((uint*)(baseOffset + 0x12)); + Vector128<uint> pixel3 = AdvSimd.LoadAndReplicateToVector128((uint*)(baseOffset + 0x22)); + Vector128<uint> pixel4 = AdvSimd.LoadAndReplicateToVector128((uint*)(baseOffset + 0x32)); + Vector128<uint> pixel5 = AdvSimd.LoadAndReplicateToVector128((uint*)(baseOffset + 0x42)); + Vector128<uint> pixel6 = AdvSimd.LoadAndReplicateToVector128((uint*)(baseOffset + 0x52)); + Vector128<uint> pixel7 = AdvSimd.LoadAndReplicateToVector128((uint*)(baseOffset + 0x62)); + Vector128<uint> pixel8 = AdvSimd.LoadAndReplicateToVector128((uint*)(baseOffset + 0x72)); + + Vector128<uint> pixel12 = AdvSimd.Arm64.ZipLow(pixel1, pixel2); + Vector128<uint> pixel34 = AdvSimd.Arm64.ZipLow(pixel3, pixel4); + Vector128<uint> pixel56 = AdvSimd.Arm64.ZipLow(pixel5, pixel6); + Vector128<uint> pixel78 = AdvSimd.Arm64.ZipLow(pixel7, pixel8); + + Vector128<ulong> pixel1234 = AdvSimd.Arm64.ZipLow(pixel12.AsUInt64(), pixel34.AsUInt64()); + Vector128<ulong> pixel5678 = AdvSimd.Arm64.ZipLow(pixel56.AsUInt64(), pixel78.AsUInt64()); + + pixel1234 = AdvSimd.ShiftRightLogical(pixel1234, 2); + pixel5678 = AdvSimd.ShiftRightLogical(pixel5678, 2); + + Vector64<byte> pixelLower = AdvSimd.ExtractNarrowingLower(pixel1234.AsUInt16()); + + Vector128<byte> pixel = AdvSimd.ExtractNarrowingUpper(pixelLower, pixel5678.AsUInt16()); + + AdvSimd.Store(op, pixel); + + op += 0x10; + } + + for (; x < uvWidth; x++) + { + Pixel* px = ip + (uint)(x << 1); + + *op++ = Downsample(px->G); + *op++ = Downsample(px->B); + } + + op += strideGap; + } + } + } + } + else + { + for (int y = 0; y < uvHeight; y++) + { + for (int x = 0; x < uvWidth; x++) + { + int xx = x << 1; + int yy = y << 1; + + int uvOffs = y * uvStride + xx; + + dstUv[uvOffs + 0] = Downsample(input.GetG(xx, yy)); + dstUv[uvOffs + 1] = Downsample(input.GetB(xx, yy)); + } + } + } + + WriteBuffer( + rm, + dstUv, + offsets.ChromaUOffset, + outLinear, + config.OutChromaWidth + 1, + config.OutChromaHeight + 1, 2, + gobBlocksInY); + + rm.BufferPool.Return(dstUvIndex); + } + + private static void WriteBuffer( + ResourceManager rm, + ReadOnlySpan<byte> src, + uint offset, + bool linear, + int width, + int height, + int bytesPerPixel, + int gobBlocksInY) + { + if (linear) + { + rm.Gmm.WriteMapped(ExtendOffset(offset), src); + return; + } + + WriteBuffer(rm, src, offset, width, height, bytesPerPixel, gobBlocksInY); + } + + private static void WriteBuffer( + ResourceManager rm, + ReadOnlySpan<byte> src, + uint offset, + int width, + int height, + int bytesPerPixel, + int gobBlocksInY) + { + int outSize = GetBlockLinearSize(width, height, bytesPerPixel, gobBlocksInY); + int dstStride = GetPitch(width, bytesPerPixel); + + int dstIndex = rm.BufferPool.Rent(outSize, out Span<byte> dst); + + LayoutConverter.ConvertLinearToBlockLinear(dst, width, height, dstStride, bytesPerPixel, gobBlocksInY, src); + + rm.Gmm.WriteMapped(ExtendOffset(offset), dst); + + rm.BufferPool.Return(dstIndex); + } + } +} diff --git a/src/Ryujinx.Graphics.Vic/Rectangle.cs b/src/Ryujinx.Graphics.Vic/Rectangle.cs new file mode 100644 index 00000000..8a8dd63a --- /dev/null +++ b/src/Ryujinx.Graphics.Vic/Rectangle.cs @@ -0,0 +1,18 @@ +namespace Ryujinx.Graphics.Vic +{ + readonly struct Rectangle + { + public readonly int X; + public readonly int Y; + public readonly int Width; + public readonly int Height; + + public Rectangle(int x, int y, int width, int height) + { + X = x; + Y = y; + Width = width; + Height = height; + } + } +}
\ No newline at end of file diff --git a/src/Ryujinx.Graphics.Vic/ResourceManager.cs b/src/Ryujinx.Graphics.Vic/ResourceManager.cs new file mode 100644 index 00000000..7c3f507e --- /dev/null +++ b/src/Ryujinx.Graphics.Vic/ResourceManager.cs @@ -0,0 +1,19 @@ +using Ryujinx.Graphics.Gpu.Memory; +using Ryujinx.Graphics.Vic.Image; + +namespace Ryujinx.Graphics.Vic +{ + readonly struct ResourceManager + { + public MemoryManager Gmm { get; } + public BufferPool<Pixel> SurfacePool { get; } + public BufferPool<byte> BufferPool { get; } + + public ResourceManager(MemoryManager gmm, BufferPool<Pixel> surfacePool, BufferPool<byte> bufferPool) + { + Gmm = gmm; + SurfacePool = surfacePool; + BufferPool = bufferPool; + } + } +} diff --git a/src/Ryujinx.Graphics.Vic/Ryujinx.Graphics.Vic.csproj b/src/Ryujinx.Graphics.Vic/Ryujinx.Graphics.Vic.csproj new file mode 100644 index 00000000..2a7cdd98 --- /dev/null +++ b/src/Ryujinx.Graphics.Vic/Ryujinx.Graphics.Vic.csproj @@ -0,0 +1,16 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <TargetFramework>net7.0</TargetFramework> + <AllowUnsafeBlocks>true</AllowUnsafeBlocks> + </PropertyGroup> + + <ItemGroup> + <ProjectReference Include="..\Ryujinx.Common\Ryujinx.Common.csproj" /> + <ProjectReference Include="..\Ryujinx.Graphics.Device\Ryujinx.Graphics.Device.csproj" /> + <ProjectReference Include="..\Ryujinx.Graphics.Gpu\Ryujinx.Graphics.Gpu.csproj" /> + <ProjectReference Include="..\Ryujinx.Graphics.Host1x\Ryujinx.Graphics.Host1x.csproj" /> + <ProjectReference Include="..\Ryujinx.Graphics.Texture\Ryujinx.Graphics.Texture.csproj" /> + </ItemGroup> + +</Project> diff --git a/src/Ryujinx.Graphics.Vic/Scaler.cs b/src/Ryujinx.Graphics.Vic/Scaler.cs new file mode 100644 index 00000000..18ae66c4 --- /dev/null +++ b/src/Ryujinx.Graphics.Vic/Scaler.cs @@ -0,0 +1,124 @@ +using System; +using System.Runtime.Intrinsics; +using System.Runtime.Intrinsics.X86; + +namespace Ryujinx.Graphics.Vic +{ + static class Scaler + { + public static void DeinterlaceWeave(Span<byte> data, ReadOnlySpan<byte> prevData, int width, int fieldSize, bool isTopField) + { + // Prev I Curr I Curr P + // TTTTTTTT BBBBBBBB TTTTTTTT + // -------- -------- BBBBBBBB + + if (isTopField) + { + for (int offset = 0; offset < data.Length; offset += fieldSize * 2) + { + prevData.Slice(offset >> 1, width).CopyTo(data.Slice(offset + fieldSize, width)); + } + } + else + { + for (int offset = 0; offset < data.Length; offset += fieldSize * 2) + { + prevData.Slice(offset >> 1, width).CopyTo(data.Slice(offset, width)); + } + } + } + + public static void DeinterlaceBob(Span<byte> data, int width, int fieldSize, bool isTopField) + { + // Curr I Curr P + // TTTTTTTT TTTTTTTT + // -------- TTTTTTTT + + if (isTopField) + { + for (int offset = 0; offset < data.Length; offset += fieldSize * 2) + { + data.Slice(offset, width).CopyTo(data.Slice(offset + fieldSize, width)); + } + } + else + { + for (int offset = 0; offset < data.Length; offset += fieldSize * 2) + { + data.Slice(offset + fieldSize, width).CopyTo(data.Slice(offset, width)); + } + } + } + + public unsafe static void DeinterlaceMotionAdaptive( + Span<byte> data, + ReadOnlySpan<byte> prevData, + ReadOnlySpan<byte> nextData, + int width, + int fieldSize, + bool isTopField) + { + // Very simple motion adaptive algorithm. + // If the pixel changed between previous and next frame, use Bob, otherwise use Weave. + // + // Example pseudo code: + // C_even = (P_even == N_even) ? P_even : C_odd + // Where: C is current frame, P is previous frame and N is next frame, and even/odd are the fields. + // + // Note: This does not fully match the hardware algorithm. + // The motion adaptive deinterlacing implemented on hardware is considerably more complex, + // and hard to implement accurately without proper documentation as for example, the + // method used for motion estimation is unknown. + + int start = isTopField ? fieldSize : 0; + int otherFieldOffset = isTopField ? -fieldSize : fieldSize; + + fixed (byte* pData = data, pPrevData = prevData, pNextData = nextData) + { + for (int offset = start; offset < data.Length; offset += fieldSize * 2) + { + int refOffset = (offset - start) >> 1; + int x = 0; + + if (Avx2.IsSupported) + { + for (; x < (width & ~0x1f); x += 32) + { + Vector256<byte> prevPixels = Avx.LoadVector256(pPrevData + refOffset + x); + Vector256<byte> nextPixels = Avx.LoadVector256(pNextData + refOffset + x); + Vector256<byte> bob = Avx.LoadVector256(pData + offset + otherFieldOffset + x); + Vector256<byte> diff = Avx2.CompareEqual(prevPixels, nextPixels); + Avx.Store(pData + offset + x, Avx2.BlendVariable(bob, prevPixels, diff)); + } + } + else if (Sse41.IsSupported) + { + for (; x < (width & ~0xf); x += 16) + { + Vector128<byte> prevPixels = Sse2.LoadVector128(pPrevData + refOffset + x); + Vector128<byte> nextPixels = Sse2.LoadVector128(pNextData + refOffset + x); + Vector128<byte> bob = Sse2.LoadVector128(pData + offset + otherFieldOffset + x); + Vector128<byte> diff = Sse2.CompareEqual(prevPixels, nextPixels); + Sse2.Store(pData + offset + x, Sse41.BlendVariable(bob, prevPixels, diff)); + } + } + + for (; x < width; x++) + { + byte prevPixel = prevData[refOffset + x]; + byte nextPixel = nextData[refOffset + x]; + + if (nextPixel != prevPixel) + { + data[offset + x] = data[offset + otherFieldOffset + x]; + } + else + { + data[offset + x] = prevPixel; + } + } + } + } + } + } +}
\ No newline at end of file diff --git a/src/Ryujinx.Graphics.Vic/Types/BlendingSlotStruct.cs b/src/Ryujinx.Graphics.Vic/Types/BlendingSlotStruct.cs new file mode 100644 index 00000000..86da41d2 --- /dev/null +++ b/src/Ryujinx.Graphics.Vic/Types/BlendingSlotStruct.cs @@ -0,0 +1,29 @@ +using Ryujinx.Common.Utilities; + +namespace Ryujinx.Graphics.Vic.Types +{ + struct BlendingSlotStruct + { + private long _word0; + private long _word1; + + public int AlphaK1 => (int)_word0.Extract(0, 10); + public int AlphaK2 => (int)_word0.Extract(16, 10); + public int SrcFactCMatchSelect => (int)_word0.Extract(32, 3); + public int DstFactCMatchSelect => (int)_word0.Extract(36, 3); + public int SrcFactAMatchSelect => (int)_word0.Extract(40, 3); + public int DstFactAMatchSelect => (int)_word0.Extract(44, 3); + public int OverrideR => (int)_word1.Extract(66, 10); + public int OverrideG => (int)_word1.Extract(76, 10); + public int OverrideB => (int)_word1.Extract(86, 10); + public int OverrideA => (int)_word1.Extract(96, 10); + public bool UseOverrideR => _word1.Extract(108); + public bool UseOverrideG => _word1.Extract(109); + public bool UseOverrideB => _word1.Extract(110); + public bool UseOverrideA => _word1.Extract(111); + public bool MaskR => _word1.Extract(112); + public bool MaskG => _word1.Extract(113); + public bool MaskB => _word1.Extract(114); + public bool MaskA => _word1.Extract(115); + } +} diff --git a/src/Ryujinx.Graphics.Vic/Types/ClearRectStruct.cs b/src/Ryujinx.Graphics.Vic/Types/ClearRectStruct.cs new file mode 100644 index 00000000..ae582a92 --- /dev/null +++ b/src/Ryujinx.Graphics.Vic/Types/ClearRectStruct.cs @@ -0,0 +1,21 @@ +using Ryujinx.Common.Utilities; + +namespace Ryujinx.Graphics.Vic.Types +{ + struct ClearRectStruct + { +#pragma warning disable CS0649 + private long _word0; + private long _word1; +#pragma warning restore CS0649 + + public int ClearRect0Left => (int)_word0.Extract(0, 14); + public int ClearRect0Right => (int)_word0.Extract(16, 14); + public int ClearRect0Top => (int)_word0.Extract(32, 14); + public int ClearRect0Bottom => (int)_word0.Extract(48, 14); + public int ClearRect1Left => (int)_word1.Extract(64, 14); + public int ClearRect1Right => (int)_word1.Extract(80, 14); + public int ClearRect1Top => (int)_word1.Extract(96, 14); + public int ClearRect1Bottom => (int)_word1.Extract(112, 14); + } +} diff --git a/src/Ryujinx.Graphics.Vic/Types/ConfigStruct.cs b/src/Ryujinx.Graphics.Vic/Types/ConfigStruct.cs new file mode 100644 index 00000000..5edc8183 --- /dev/null +++ b/src/Ryujinx.Graphics.Vic/Types/ConfigStruct.cs @@ -0,0 +1,16 @@ +using Ryujinx.Common.Memory; + +namespace Ryujinx.Graphics.Vic.Types +{ + struct ConfigStruct + { +#pragma warning disable CS0649 + public PipeConfig PipeConfig; + public OutputConfig OutputConfig; + public OutputSurfaceConfig OutputSurfaceConfig; + public MatrixStruct OutColorMatrix; + public Array4<ClearRectStruct> ClearRectStruct; + public Array8<SlotStruct> SlotStruct; +#pragma warning restore CS0649 + } +} diff --git a/src/Ryujinx.Graphics.Vic/Types/DeinterlaceMode.cs b/src/Ryujinx.Graphics.Vic/Types/DeinterlaceMode.cs new file mode 100644 index 00000000..aa0654f0 --- /dev/null +++ b/src/Ryujinx.Graphics.Vic/Types/DeinterlaceMode.cs @@ -0,0 +1,12 @@ +namespace Ryujinx.Graphics.Vic.Types +{ + enum DeinterlaceMode + { + Weave, + BobField, + Bob, + NewBob, + Disi1, + WeaveLumaBobFieldChroma + } +}
\ No newline at end of file diff --git a/src/Ryujinx.Graphics.Vic/Types/FrameFormat.cs b/src/Ryujinx.Graphics.Vic/Types/FrameFormat.cs new file mode 100644 index 00000000..91f5751b --- /dev/null +++ b/src/Ryujinx.Graphics.Vic/Types/FrameFormat.cs @@ -0,0 +1,79 @@ +namespace Ryujinx.Graphics.Vic.Types +{ + enum FrameFormat + { + Progressive, + InterlacedTopFieldFirst, + InterlacedBottomFieldFirst, + TopField, + BottomField, + SubPicProgressive, + SubPicInterlacedTopFieldFirst, + SubPicInterlacedBottomFieldFirst, + SubPicTopField, + SubPicBottomField, + TopFieldChromaBottom, + BottomFieldChromaTop, + SubPicTopFieldChromaBottom, + SubPicBottomFieldChromaTop + } + + static class FrameFormatExtensions + { + public static bool IsField(this FrameFormat frameFormat) + { + switch (frameFormat) + { + case FrameFormat.TopField: + case FrameFormat.BottomField: + case FrameFormat.SubPicTopField: + case FrameFormat.SubPicBottomField: + case FrameFormat.TopFieldChromaBottom: + case FrameFormat.BottomFieldChromaTop: + case FrameFormat.SubPicTopFieldChromaBottom: + case FrameFormat.SubPicBottomFieldChromaTop: + return true; + } + + return false; + } + + public static bool IsInterlaced(this FrameFormat frameFormat) + { + switch (frameFormat) + { + case FrameFormat.InterlacedTopFieldFirst: + case FrameFormat.InterlacedBottomFieldFirst: + case FrameFormat.SubPicInterlacedTopFieldFirst: + case FrameFormat.SubPicInterlacedBottomFieldFirst: + return true; + } + + return false; + } + + public static bool IsInterlacedBottomFirst(this FrameFormat frameFormat) + { + return frameFormat == FrameFormat.InterlacedBottomFieldFirst || + frameFormat == FrameFormat.SubPicInterlacedBottomFieldFirst; + } + + public static bool IsTopField(this FrameFormat frameFormat, bool isLuma) + { + switch (frameFormat) + { + case FrameFormat.TopField: + case FrameFormat.SubPicTopField: + return true; + case FrameFormat.TopFieldChromaBottom: + case FrameFormat.SubPicTopFieldChromaBottom: + return isLuma; + case FrameFormat.BottomFieldChromaTop: + case FrameFormat.SubPicBottomFieldChromaTop: + return !isLuma; + } + + return false; + } + } +}
\ No newline at end of file diff --git a/src/Ryujinx.Graphics.Vic/Types/LumaKeyStruct.cs b/src/Ryujinx.Graphics.Vic/Types/LumaKeyStruct.cs new file mode 100644 index 00000000..5d83bd71 --- /dev/null +++ b/src/Ryujinx.Graphics.Vic/Types/LumaKeyStruct.cs @@ -0,0 +1,19 @@ +using Ryujinx.Common.Utilities; + +namespace Ryujinx.Graphics.Vic.Types +{ + struct LumaKeyStruct + { + private long _word0; + private long _word1; + + public int LumaCoeff0 => (int)_word0.Extract(0, 20); + public int LumaCoeff1 => (int)_word0.Extract(20, 20); + public int LumaCoeff2 => (int)_word0.Extract(40, 20); + public int LumaRShift => (int)_word0.Extract(60, 4); + public int LumaCoeff3 => (int)_word1.Extract(64, 20); + public int LumaKeyLower => (int)_word1.Extract(84, 10); + public int LumaKeyUpper => (int)_word1.Extract(94, 10); + public bool LumaKeyEnabled => _word1.Extract(104); + } +} diff --git a/src/Ryujinx.Graphics.Vic/Types/MatrixStruct.cs b/src/Ryujinx.Graphics.Vic/Types/MatrixStruct.cs new file mode 100644 index 00000000..c0a4c34e --- /dev/null +++ b/src/Ryujinx.Graphics.Vic/Types/MatrixStruct.cs @@ -0,0 +1,27 @@ +using Ryujinx.Common.Utilities; + +namespace Ryujinx.Graphics.Vic.Types +{ + struct MatrixStruct + { + private long _word0; + private long _word1; + private long _word2; + private long _word3; + + public int MatrixCoeff00 => (int)_word0.ExtractSx(0, 20); + public int MatrixCoeff10 => (int)_word0.ExtractSx(20, 20); + public int MatrixCoeff20 => (int)_word0.ExtractSx(40, 20); + public int MatrixRShift => (int)_word0.Extract(60, 4); + public int MatrixCoeff01 => (int)_word1.ExtractSx(64, 20); + public int MatrixCoeff11 => (int)_word1.ExtractSx(84, 20); + public int MatrixCoeff21 => (int)_word1.ExtractSx(104, 20); + public bool MatrixEnable => _word1.Extract(127); + public int MatrixCoeff02 => (int)_word2.ExtractSx(128, 20); + public int MatrixCoeff12 => (int)_word2.ExtractSx(148, 20); + public int MatrixCoeff22 => (int)_word2.ExtractSx(168, 20); + public int MatrixCoeff03 => (int)_word3.ExtractSx(192, 20); + public int MatrixCoeff13 => (int)_word3.ExtractSx(212, 20); + public int MatrixCoeff23 => (int)_word3.ExtractSx(232, 20); + } +} diff --git a/src/Ryujinx.Graphics.Vic/Types/OutputConfig.cs b/src/Ryujinx.Graphics.Vic/Types/OutputConfig.cs new file mode 100644 index 00000000..7b866994 --- /dev/null +++ b/src/Ryujinx.Graphics.Vic/Types/OutputConfig.cs @@ -0,0 +1,27 @@ +using Ryujinx.Common.Utilities; + +namespace Ryujinx.Graphics.Vic.Types +{ + struct OutputConfig + { +#pragma warning disable CS0649 + private long _word0; + private long _word1; +#pragma warning restore CS0649 + + public int AlphaFillMode => (int)_word0.Extract(0, 3); + public int AlphaFillSlot => (int)_word0.Extract(3, 3); + public int BackgroundAlpha => (int)_word0.Extract(6, 10); + public int BackgroundR => (int)_word0.Extract(16, 10); + public int BackgroundG => (int)_word0.Extract(26, 10); + public int BackgroundB => (int)_word0.Extract(36, 10); + public int RegammaMode => (int)_word0.Extract(46, 2); + public bool OutputFlipX => _word0.Extract(48); + public bool OutputFlipY => _word0.Extract(49); + public bool OutputTranspose => _word0.Extract(50); + public int TargetRectLeft => (int)_word1.Extract(64, 14); + public int TargetRectRight => (int)_word1.Extract(80, 14); + public int TargetRectTop => (int)_word1.Extract(96, 14); + public int TargetRectBottom => (int)_word1.Extract(112, 14); + } +} diff --git a/src/Ryujinx.Graphics.Vic/Types/OutputSurfaceConfig.cs b/src/Ryujinx.Graphics.Vic/Types/OutputSurfaceConfig.cs new file mode 100644 index 00000000..6a8b21e1 --- /dev/null +++ b/src/Ryujinx.Graphics.Vic/Types/OutputSurfaceConfig.cs @@ -0,0 +1,24 @@ +using Ryujinx.Common.Utilities; + +namespace Ryujinx.Graphics.Vic.Types +{ + struct OutputSurfaceConfig + { +#pragma warning disable CS0649 + private long _word0; + private long _word1; +#pragma warning restore CS0649 + + public PixelFormat OutPixelFormat => (PixelFormat)_word0.Extract(0, 7); + public int OutChromaLocHoriz => (int)_word0.Extract(7, 2); + public int OutChromaLocVert => (int)_word0.Extract(9, 2); + public int OutBlkKind => (int)_word0.Extract(11, 4); + public int OutBlkHeight => (int)_word0.Extract(15, 4); + public int OutSurfaceWidth => (int)_word0.Extract(32, 14); + public int OutSurfaceHeight => (int)_word0.Extract(46, 14); + public int OutLumaWidth => (int)_word1.Extract(64, 14); + public int OutLumaHeight => (int)_word1.Extract(78, 14); + public int OutChromaWidth => (int)_word1.Extract(96, 14); + public int OutChromaHeight => (int)_word1.Extract(110, 14); + } +} diff --git a/src/Ryujinx.Graphics.Vic/Types/PipeConfig.cs b/src/Ryujinx.Graphics.Vic/Types/PipeConfig.cs new file mode 100644 index 00000000..76720eb1 --- /dev/null +++ b/src/Ryujinx.Graphics.Vic/Types/PipeConfig.cs @@ -0,0 +1,15 @@ +using Ryujinx.Common.Utilities; + +namespace Ryujinx.Graphics.Vic.Types +{ + struct PipeConfig + { +#pragma warning disable CS0169, CS0649 + private long _word0; + private long _word1; +#pragma warning restore CS0169, CS0649 + + public int DownsampleHoriz => (int)_word0.Extract(0, 11); + public int DownsampleVert => (int)_word0.Extract(16, 11); + } +} diff --git a/src/Ryujinx.Graphics.Vic/Types/PixelFormat.cs b/src/Ryujinx.Graphics.Vic/Types/PixelFormat.cs new file mode 100644 index 00000000..72dc7899 --- /dev/null +++ b/src/Ryujinx.Graphics.Vic/Types/PixelFormat.cs @@ -0,0 +1,81 @@ +namespace Ryujinx.Graphics.Vic.Types +{ + enum PixelFormat + { + A8, + L8, + A4L4, + L4A4, + R8, + A8L8, + L8A8, + R8G8, + G8R8, + B5G6R5, + R5G6B5, + B6G5R5, + R5G5B6, + A1B5G5R5, + A1R5G5B5, + B5G5R5A1, + R5G5B5A1, + A5B5G5R1, + A5R1G5B5, + B5G5R1A5, + R1G5B5A5, + X1B5G5R5, + X1R5G5B5, + B5G5R5X1, + R5G5B5X1, + A4B4G4R4, + A4R4G4B4, + B4G4R4A4, + R4G4B4A4, + B8_G8_R8, + R8_G8_B8, + A8B8G8R8, + A8R8G8B8, + B8G8R8A8, + R8G8B8A8, + X8B8G8R8, + X8R8G8B8, + B8G8R8X8, + R8G8B8X8, + A2B10G10R10, + A2R10G10B10, + B10G10R10A2, + R10G10B10A2, + A4P4, + P4A4, + P8A845, + A8P8, + P8, + P1, + U8V8, + V8U8, + A8Y8U8V8, + V8U8Y8A8, + Y8_U8_V8, + Y8_V8_U8, + U8_V8_Y8, + V8_U8_Y8, + Y8_U8__Y8_V8, + Y8_V8__Y8_U8, + U8_Y8__V8_Y8, + V8_Y8__U8_Y8, + Y8___U8V8_N444, + Y8___V8U8_N444, + Y8___U8V8_N422, + Y8___V8U8_N422, + Y8___U8V8_N422R, + Y8___V8U8_N422R, + Y8___U8V8_N420, + Y8___V8U8_N420, + Y8___U8___V8_N444, + Y8___U8___V8_N422, + Y8___U8___V8_N422R, + Y8___U8___V8_N420, + U8, + V8 + } +} diff --git a/src/Ryujinx.Graphics.Vic/Types/SlotConfig.cs b/src/Ryujinx.Graphics.Vic/Types/SlotConfig.cs new file mode 100644 index 00000000..aba61add --- /dev/null +++ b/src/Ryujinx.Graphics.Vic/Types/SlotConfig.cs @@ -0,0 +1,65 @@ +using Ryujinx.Common.Utilities; + +namespace Ryujinx.Graphics.Vic.Types +{ + struct SlotConfig + { + private long _word0; + private long _word1; + private long _word2; + private long _word3; + private long _word4; + private long _word5; + private long _word6; + private long _word7; + + public bool SlotEnable => _word0.Extract(0); + public bool DeNoise => _word0.Extract(1); + public bool AdvancedDenoise => _word0.Extract(2); + public bool CadenceDetect => _word0.Extract(3); + public bool MotionMap => _word0.Extract(4); + public bool MMapCombine => _word0.Extract(5); + public bool IsEven => _word0.Extract(6); + public bool ChromaEven => _word0.Extract(7); + public bool CurrentFieldEnable => _word0.Extract(8); + public bool PrevFieldEnable => _word0.Extract(9); + public bool NextFieldEnable => _word0.Extract(10); + public bool NextNrFieldEnable => _word0.Extract(11); + public bool CurMotionFieldEnable => _word0.Extract(12); + public bool PrevMotionFieldEnable => _word0.Extract(13); + public bool PpMotionFieldEnable => _word0.Extract(14); + public bool CombMotionFieldEnable => _word0.Extract(15); + public FrameFormat FrameFormat => (FrameFormat)_word0.Extract(16, 4); + public int FilterLengthY => (int)_word0.Extract(20, 2); + public int FilterLengthX => (int)_word0.Extract(22, 2); + public int Panoramic => (int)_word0.Extract(24, 12); + public int DetailFltClamp => (int)_word0.Extract(58, 6); + public int FilterNoise => (int)_word1.Extract(64, 10); + public int FilterDetail => (int)_word1.Extract(74, 10); + public int ChromaNoise => (int)_word1.Extract(84, 10); + public int ChromaDetail => (int)_word1.Extract(94, 10); + public DeinterlaceMode DeinterlaceMode => (DeinterlaceMode)_word1.Extract(104, 4); + public int MotionAccumWeight => (int)_word1.Extract(108, 3); + public int NoiseIir => (int)_word1.Extract(111, 11); + public int LightLevel => (int)_word1.Extract(122, 4); + public int SoftClampLow => (int)_word2.Extract(128, 10); + public int SoftClampHigh => (int)_word2.Extract(138, 10); + public int PlanarAlpha => (int)_word2.Extract(160, 10); + public bool ConstantAlpha => _word2.Extract(170); + public int StereoInterleave => (int)_word2.Extract(171, 3); + public bool ClipEnabled => _word2.Extract(174); + public int ClearRectMask => (int)_word2.Extract(175, 8); + public int DegammaMode => (int)_word2.Extract(183, 2); + public bool DecompressEnable => _word2.Extract(186); + public int DecompressCtbCount => (int)_word3.Extract(192, 8); + public int DecompressZbcColor => (int)_word3.Extract(200, 32); + public int SourceRectLeft => (int)_word4.Extract(256, 30); + public int SourceRectRight => (int)_word4.Extract(288, 30); + public int SourceRectTop => (int)_word5.Extract(320, 30); + public int SourceRectBottom => (int)_word5.Extract(352, 30); + public int DstRectLeft => (int)_word6.Extract(384, 14); + public int DstRectRight => (int)_word6.Extract(400, 14); + public int DstRectTop => (int)_word6.Extract(416, 14); + public int DstRectBottom => (int)_word6.Extract(432, 14); + } +} diff --git a/src/Ryujinx.Graphics.Vic/Types/SlotStruct.cs b/src/Ryujinx.Graphics.Vic/Types/SlotStruct.cs new file mode 100644 index 00000000..96c6cce5 --- /dev/null +++ b/src/Ryujinx.Graphics.Vic/Types/SlotStruct.cs @@ -0,0 +1,12 @@ +namespace Ryujinx.Graphics.Vic.Types +{ + struct SlotStruct + { + public SlotConfig SlotConfig; + public SlotSurfaceConfig SlotSurfaceConfig; + public LumaKeyStruct LumaKeyStruct; + public MatrixStruct ColorMatrixStruct; + public MatrixStruct GamutMatrixStruct; + public BlendingSlotStruct BlendingSlotStruct; + } +} diff --git a/src/Ryujinx.Graphics.Vic/Types/SlotSurfaceConfig.cs b/src/Ryujinx.Graphics.Vic/Types/SlotSurfaceConfig.cs new file mode 100644 index 00000000..4492c85f --- /dev/null +++ b/src/Ryujinx.Graphics.Vic/Types/SlotSurfaceConfig.cs @@ -0,0 +1,23 @@ +using Ryujinx.Common.Utilities; + +namespace Ryujinx.Graphics.Vic.Types +{ + struct SlotSurfaceConfig + { + private long _word0; + private long _word1; + + public PixelFormat SlotPixelFormat => (PixelFormat)_word0.Extract(0, 7); + public int SlotChromaLocHoriz => (int)_word0.Extract(7, 2); + public int SlotChromaLocVert => (int)_word0.Extract(9, 2); + public int SlotBlkKind => (int)_word0.Extract(11, 4); + public int SlotBlkHeight => (int)_word0.Extract(15, 4); + public int SlotCacheWidth => (int)_word0.Extract(19, 3); + public int SlotSurfaceWidth => (int)_word0.Extract(32, 14); + public int SlotSurfaceHeight => (int)_word0.Extract(46, 14); + public int SlotLumaWidth => (int)_word1.Extract(64, 14); + public int SlotLumaHeight => (int)_word1.Extract(78, 14); + public int SlotChromaWidth => (int)_word1.Extract(96, 14); + public int SlotChromaHeight => (int)_word1.Extract(110, 14); + } +} diff --git a/src/Ryujinx.Graphics.Vic/VicDevice.cs b/src/Ryujinx.Graphics.Vic/VicDevice.cs new file mode 100644 index 00000000..8b66727d --- /dev/null +++ b/src/Ryujinx.Graphics.Vic/VicDevice.cs @@ -0,0 +1,74 @@ +using Ryujinx.Graphics.Device; +using Ryujinx.Graphics.Gpu.Memory; +using Ryujinx.Graphics.Vic.Image; +using Ryujinx.Graphics.Vic.Types; +using System; +using System.Collections.Generic; + +namespace Ryujinx.Graphics.Vic +{ + public class VicDevice : IDeviceState + { + private readonly MemoryManager _gmm; + private readonly ResourceManager _rm; + private readonly DeviceState<VicRegisters> _state; + + public VicDevice(MemoryManager gmm) + { + _gmm = gmm; + _rm = new ResourceManager(gmm, new BufferPool<Pixel>(), new BufferPool<byte>()); + _state = new DeviceState<VicRegisters>(new Dictionary<string, RwCallback> + { + { nameof(VicRegisters.Execute), new RwCallback(Execute, null) } + }); + } + + public int Read(int offset) => _state.Read(offset); + public void Write(int offset, int data) => _state.Write(offset, data); + + private void Execute(int data) + { + ConfigStruct config = ReadIndirect<ConfigStruct>(_state.State.SetConfigStructOffset); + + using Surface output = new Surface( + _rm.SurfacePool, + config.OutputSurfaceConfig.OutSurfaceWidth + 1, + config.OutputSurfaceConfig.OutSurfaceHeight + 1); + + for (int i = 0; i < config.SlotStruct.Length; i++) + { + ref SlotStruct slot = ref config.SlotStruct[i]; + + if (!slot.SlotConfig.SlotEnable) + { + continue; + } + + ref var offsets = ref _state.State.SetSurfacexSlotx[i]; + + using Surface src = SurfaceReader.Read(_rm, ref slot.SlotConfig, ref slot.SlotSurfaceConfig, ref offsets); + + int x1 = config.OutputConfig.TargetRectLeft; + int y1 = config.OutputConfig.TargetRectTop; + int x2 = config.OutputConfig.TargetRectRight + 1; + int y2 = config.OutputConfig.TargetRectBottom + 1; + + int targetX = Math.Min(x1, x2); + int targetY = Math.Min(y1, y2); + int targetW = Math.Min(output.Width - targetX, Math.Abs(x2 - x1)); + int targetH = Math.Min(output.Height - targetY, Math.Abs(y2 - y1)); + + Rectangle targetRect = new Rectangle(targetX, targetY, targetW, targetH); + + Blender.BlendOne(output, src, ref slot, targetRect); + } + + SurfaceWriter.Write(_rm, output, ref config.OutputSurfaceConfig, ref _state.State.SetOutputSurface); + } + + private T ReadIndirect<T>(uint offset) where T : unmanaged + { + return _gmm.Read<T>((ulong)offset << 8); + } + } +} diff --git a/src/Ryujinx.Graphics.Vic/VicRegisters.cs b/src/Ryujinx.Graphics.Vic/VicRegisters.cs new file mode 100644 index 00000000..1c11b554 --- /dev/null +++ b/src/Ryujinx.Graphics.Vic/VicRegisters.cs @@ -0,0 +1,51 @@ +using Ryujinx.Common.Memory; + +namespace Ryujinx.Graphics.Vic +{ + struct PlaneOffsets + { +#pragma warning disable CS0649 + public uint LumaOffset; + public uint ChromaUOffset; + public uint ChromaVOffset; +#pragma warning restore CS0649 + } + + struct VicRegisters + { +#pragma warning disable CS0649 + public Array64<uint> Reserved0; + public uint Nop; + public Array15<uint> Reserved104; + public uint PmTrigger; + public Array47<uint> Reserved144; + public uint SetApplicationID; + public uint SetWatchdogTimer; + public Array14<uint> Reserved208; + public uint SemaphoreA; + public uint SemaphoreB; + public uint SemaphoreC; + public uint CtxSaveArea; + public uint CtxSwitch; + public Array43<uint> Reserved254; + public uint Execute; + public uint SemaphoreD; + public Array62<uint> Reserved308; + public Array8<Array8<PlaneOffsets>> SetSurfacexSlotx; + public uint SetPictureIndex; + public uint SetControlParams; + public uint SetConfigStructOffset; + public uint SetFilterStructOffset; + public uint SetPaletteOffset; + public uint SetHistOffset; + public uint SetContextId; + public uint SetFceUcodeSize; + public PlaneOffsets SetOutputSurface; + public uint SetFceUcodeOffset; + public Array4<uint> Reserved730; + public Array8<uint> SetSlotContextId; + public Array8<uint> SetCompTagBufferOffset; + public Array8<uint> SetHistoryBufferOffset; +#pragma warning restore CS0649 + } +} |
