aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Graphics.Nvdec/Image/SurfaceWriter.cs
blob: cc5c251bec06702b56ce7d996f2174f6d5d229b6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using Ryujinx.Common;
using Ryujinx.Graphics.Gpu.Memory;
using Ryujinx.Graphics.Texture;
using Ryujinx.Graphics.Video;
using System;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;
using static Ryujinx.Graphics.Nvdec.Image.SurfaceCommon;
using static Ryujinx.Graphics.Nvdec.MemoryExtensions;

namespace Ryujinx.Graphics.Nvdec.Image
{
    static class SurfaceWriter
    {
        public static void Write(MemoryManager gmm, ISurface surface, uint lumaOffset, uint chromaOffset)
        {
            int lumaSize = GetBlockLinearSize(surface.Width, surface.Height, 1);

            using var luma = gmm.GetWritableRegion(ExtendOffset(lumaOffset), lumaSize);

            WriteLuma(
                luma.Memory.Span,
                surface.YPlane.AsSpan(),
                surface.Stride,
                surface.Width,
                surface.Height);

            int chromaSize = GetBlockLinearSize(surface.UvWidth, surface.UvHeight, 2);

            using var chroma = gmm.GetWritableRegion(ExtendOffset(chromaOffset), chromaSize);

            WriteChroma(
                chroma.Memory.Span,
                surface.UPlane.AsSpan(),
                surface.VPlane.AsSpan(),
                surface.UvStride,
                surface.UvWidth,
                surface.UvHeight);
        }

        public static void WriteInterlaced(
            MemoryManager gmm,
            ISurface surface,
            uint lumaTopOffset,
            uint chromaTopOffset,
            uint lumaBottomOffset,
            uint chromaBottomOffset)
        {
            int lumaSize = GetBlockLinearSize(surface.Width, surface.Height / 2, 1);

            using var lumaTop = gmm.GetWritableRegion(ExtendOffset(lumaTopOffset), lumaSize);
            using var lumaBottom = gmm.GetWritableRegion(ExtendOffset(lumaBottomOffset), lumaSize);

            WriteLuma(
                lumaTop.Memory.Span,
                surface.YPlane.AsSpan(),
                surface.Stride * 2,
                surface.Width,
                surface.Height / 2);

            WriteLuma(
                lumaBottom.Memory.Span,
                surface.YPlane.AsSpan().Slice(surface.Stride),
                surface.Stride * 2,
                surface.Width,
                surface.Height / 2);

            int chromaSize = GetBlockLinearSize(surface.UvWidth, surface.UvHeight / 2, 2);

            using var chromaTop = gmm.GetWritableRegion(ExtendOffset(chromaTopOffset), chromaSize);
            using var chromaBottom = gmm.GetWritableRegion(ExtendOffset(chromaBottomOffset), chromaSize);

            WriteChroma(
                chromaTop.Memory.Span,
                surface.UPlane.AsSpan(),
                surface.VPlane.AsSpan(),
                surface.UvStride * 2,
                surface.UvWidth,
                surface.UvHeight / 2);

            WriteChroma(
                chromaBottom.Memory.Span,
                surface.UPlane.AsSpan().Slice(surface.UvStride),
                surface.VPlane.AsSpan().Slice(surface.UvStride),
                surface.UvStride * 2,
                surface.UvWidth,
                surface.UvHeight / 2);
        }

        private static void WriteLuma(Span<byte> dst, ReadOnlySpan<byte> src, int srcStride, int width, int height)
        {
            LayoutConverter.ConvertLinearToBlockLinear(dst, width, height, srcStride, 1, 2, src);
        }

        private unsafe static void WriteChroma(
            Span<byte> dst,
            ReadOnlySpan<byte> srcU,
            ReadOnlySpan<byte> srcV,
            int srcStride,
            int width,
            int height)
        {
            OffsetCalculator calc = new OffsetCalculator(width, height, 0, false, 2, 2);

            if (Sse2.IsSupported)
            {
                int strideTrunc64 = BitUtils.AlignDown(width * 2, 64);

                int inStrideGap = srcStride - width;

                fixed (byte* outputPtr = dst, srcUPtr = srcU, srcVPtr = srcV)
                {
                    byte* inUPtr = srcUPtr;
                    byte* inVPtr = srcVPtr;

                    for (int y = 0; y < height; y++)
                    {
                        calc.SetY(y);

                        for (int x = 0; x < strideTrunc64; x += 64, inUPtr += 32, inVPtr += 32)
                        {
                            byte* offset = outputPtr + calc.GetOffsetWithLineOffset64(x);
                            byte* offset2 = offset + 0x20;
                            byte* offset3 = offset + 0x100;
                            byte* offset4 = offset + 0x120;

                            Vector128<byte> value = *(Vector128<byte>*)inUPtr;
                            Vector128<byte> value2 = *(Vector128<byte>*)inVPtr;
                            Vector128<byte> value3 = *(Vector128<byte>*)(inUPtr + 16);
                            Vector128<byte> value4 = *(Vector128<byte>*)(inVPtr + 16);

                            Vector128<byte> uv0 = Sse2.UnpackLow(value, value2);
                            Vector128<byte> uv1 = Sse2.UnpackHigh(value, value2);
                            Vector128<byte> uv2 = Sse2.UnpackLow(value3, value4);
                            Vector128<byte> uv3 = Sse2.UnpackHigh(value3, value4);

                            *(Vector128<byte>*)offset = uv0;
                            *(Vector128<byte>*)offset2 = uv1;
                            *(Vector128<byte>*)offset3 = uv2;
                            *(Vector128<byte>*)offset4 = uv3;
                        }

                        for (int x = strideTrunc64 / 2; x < width; x++, inUPtr++, inVPtr++)
                        {
                            byte* offset = outputPtr + calc.GetOffset(x);

                            *offset = *inUPtr;
                            *(offset + 1) = *inVPtr;
                        }

                        inUPtr += inStrideGap;
                        inVPtr += inStrideGap;
                    }
                }
            }
            else
            {
                for (int y = 0; y < height; y++)
                {
                    int srcBaseOffset = y * srcStride;

                    calc.SetY(y);

                    for (int x = 0; x < width; x++)
                    {
                        int dstOffset = calc.GetOffset(x);

                        dst[dstOffset + 0] = srcU[srcBaseOffset + x];
                        dst[dstOffset + 1] = srcV[srcBaseOffset + x];
                    }
                }
            }
        }
    }
}