aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Graphics/NvGpuEngineM2mf.cs
blob: 5ee18ea9e8052da724650d47517b5cf5120bd465 (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
176
177
178
179
180
181
182
183
184
185
186
187
using Ryujinx.Graphics.Memory;
using Ryujinx.Graphics.Texture;
using System.Collections.Generic;

namespace Ryujinx.Graphics
{
    class NvGpuEngineM2mf : INvGpuEngine
    {
        public int[] Registers { get; private set; }

        private NvGpu Gpu;

        private Dictionary<int, NvGpuMethod> Methods;

        public NvGpuEngineM2mf(NvGpu Gpu)
        {
            this.Gpu = Gpu;

            Registers = new int[0x1d6];

            Methods = new Dictionary<int, NvGpuMethod>();

            void AddMethod(int Meth, int Count, int Stride, NvGpuMethod Method)
            {
                while (Count-- > 0)
                {
                    Methods.Add(Meth, Method);

                    Meth += Stride;
                }
            }

            AddMethod(0xc0, 1, 1, Execute);
        }

        public void CallMethod(NvGpuVmm Vmm, GpuMethodCall MethCall)
        {
            if (Methods.TryGetValue(MethCall.Method, out NvGpuMethod Method))
            {
                Method(Vmm, MethCall);
            }
            else
            {
                WriteRegister(MethCall);
            }
        }

        private void Execute(NvGpuVmm Vmm, GpuMethodCall MethCall)
        {
            //TODO: Some registers and copy modes are still not implemented.
            int Control = MethCall.Argument;

            bool SrcLinear = ((Control >> 7) & 1) != 0;
            bool DstLinear = ((Control >> 8) & 1) != 0;
            bool Copy2d    = ((Control >> 9) & 1) != 0;

            long SrcAddress = MakeInt64From2xInt32(NvGpuEngineM2mfReg.SrcAddress);
            long DstAddress = MakeInt64From2xInt32(NvGpuEngineM2mfReg.DstAddress);

            int SrcPitch = ReadRegister(NvGpuEngineM2mfReg.SrcPitch);
            int DstPitch = ReadRegister(NvGpuEngineM2mfReg.DstPitch);

            int XCount = ReadRegister(NvGpuEngineM2mfReg.XCount);
            int YCount = ReadRegister(NvGpuEngineM2mfReg.YCount);

            int Swizzle = ReadRegister(NvGpuEngineM2mfReg.Swizzle);

            int DstBlkDim = ReadRegister(NvGpuEngineM2mfReg.DstBlkDim);
            int DstSizeX  = ReadRegister(NvGpuEngineM2mfReg.DstSizeX);
            int DstSizeY  = ReadRegister(NvGpuEngineM2mfReg.DstSizeY);
            int DstSizeZ  = ReadRegister(NvGpuEngineM2mfReg.DstSizeZ);
            int DstPosXY  = ReadRegister(NvGpuEngineM2mfReg.DstPosXY);
            int DstPosZ   = ReadRegister(NvGpuEngineM2mfReg.DstPosZ);

            int SrcBlkDim = ReadRegister(NvGpuEngineM2mfReg.SrcBlkDim);
            int SrcSizeX  = ReadRegister(NvGpuEngineM2mfReg.SrcSizeX);
            int SrcSizeY  = ReadRegister(NvGpuEngineM2mfReg.SrcSizeY);
            int SrcSizeZ  = ReadRegister(NvGpuEngineM2mfReg.SrcSizeZ);
            int SrcPosXY  = ReadRegister(NvGpuEngineM2mfReg.SrcPosXY);
            int SrcPosZ   = ReadRegister(NvGpuEngineM2mfReg.SrcPosZ);

            int SrcCpp = ((Swizzle >> 20) & 7) + 1;
            int DstCpp = ((Swizzle >> 24) & 7) + 1;

            int DstPosX = (DstPosXY >>  0) & 0xffff;
            int DstPosY = (DstPosXY >> 16) & 0xffff;

            int SrcPosX = (SrcPosXY >>  0) & 0xffff;
            int SrcPosY = (SrcPosXY >> 16) & 0xffff;

            int SrcBlockHeight = 1 << ((SrcBlkDim >> 4) & 0xf);
            int DstBlockHeight = 1 << ((DstBlkDim >> 4) & 0xf);

            long SrcPA = Vmm.GetPhysicalAddress(SrcAddress);
            long DstPA = Vmm.GetPhysicalAddress(DstAddress);

            if (Copy2d)
            {
                if (SrcLinear)
                {
                    SrcPosX = SrcPosY = SrcPosZ = 0;
                }

                if (DstLinear)
                {
                    DstPosX = DstPosY = DstPosZ = 0;
                }

                if (SrcLinear && DstLinear)
                {
                    for (int Y = 0; Y < YCount; Y++)
                    {
                        int SrcOffset = (SrcPosY + Y) * SrcPitch + SrcPosX * SrcCpp;
                        int DstOffset = (DstPosY + Y) * DstPitch + DstPosX * DstCpp;

                        long Src = SrcPA + (uint)SrcOffset;
                        long Dst = DstPA + (uint)DstOffset;

                        Vmm.Memory.CopyBytes(Src, Dst, XCount * SrcCpp);
                    }
                }
                else
                {
                    ISwizzle SrcSwizzle;

                    if (SrcLinear)
                    {
                        SrcSwizzle = new LinearSwizzle(SrcPitch, SrcCpp);
                    }
                    else
                    {
                        SrcSwizzle = new BlockLinearSwizzle(SrcSizeX, SrcCpp, SrcBlockHeight);
                    }

                    ISwizzle DstSwizzle;

                    if (DstLinear)
                    {
                        DstSwizzle = new LinearSwizzle(DstPitch, DstCpp);
                    }
                    else
                    {
                        DstSwizzle = new BlockLinearSwizzle(DstSizeX, DstCpp, DstBlockHeight);
                    }

                    for (int Y = 0; Y < YCount; Y++)
                    for (int X = 0; X < XCount; X++)
                    {
                        int SrcOffset = SrcSwizzle.GetSwizzleOffset(SrcPosX + X, SrcPosY + Y);
                        int DstOffset = DstSwizzle.GetSwizzleOffset(DstPosX + X, DstPosY + Y);

                        long Src = SrcPA + (uint)SrcOffset;
                        long Dst = DstPA + (uint)DstOffset;

                        Vmm.Memory.CopyBytes(Src, Dst, SrcCpp);
                    }
                }
            }
            else
            {
                Vmm.Memory.CopyBytes(SrcPA, DstPA, XCount);
            }
        }

        private long MakeInt64From2xInt32(NvGpuEngineM2mfReg Reg)
        {
            return
                (long)Registers[(int)Reg + 0] << 32 |
                (uint)Registers[(int)Reg + 1];
        }

        private void WriteRegister(GpuMethodCall MethCall)
        {
            Registers[MethCall.Method] = MethCall.Argument;
        }

        private int ReadRegister(NvGpuEngineM2mfReg Reg)
        {
            return Registers[(int)Reg];
        }

        private void WriteRegister(NvGpuEngineM2mfReg Reg, int Value)
        {
            Registers[(int)Reg] = Value;
        }
    }
}