aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Graphics/CdmaProcessor.cs
blob: 4ff12fbf50012993239258ad089eecc06a116d74 (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
using Ryujinx.Graphics.Memory;
using System.Collections.Generic;

namespace Ryujinx.Graphics
{
    public class CdmaProcessor
    {
        private const int MethSetMethod = 0x10;
        private const int MethSetData   = 0x11;

        private NvGpu _gpu;

        public CdmaProcessor(NvGpu gpu)
        {
            _gpu = gpu;
        }

        public void PushCommands(NvGpuVmm vmm, int[] cmdBuffer)
        {
            List<ChCommand> commands = new List<ChCommand>();

            ChClassId currentClass = 0;

            for (int index = 0; index < cmdBuffer.Length; index++)
            {
                int cmd = cmdBuffer[index];

                int value        = (cmd >> 0)  & 0xffff;
                int methodOffset = (cmd >> 16) & 0xfff;

                ChSubmissionMode submissionMode = (ChSubmissionMode)((cmd >> 28) & 0xf);

                switch (submissionMode)
                {
                    case ChSubmissionMode.SetClass: currentClass = (ChClassId)(value >> 6); break;

                    case ChSubmissionMode.Incrementing:
                    {
                        int count = value;

                        for (int argIdx = 0; argIdx < count; argIdx++)
                        {
                            int argument = cmdBuffer[++index];

                            commands.Add(new ChCommand(currentClass, methodOffset + argIdx, argument));
                        }

                        break;
                    }

                    case ChSubmissionMode.NonIncrementing:
                    {
                        int count = value;

                        int[] arguments = new int[count];

                        for (int argIdx = 0; argIdx < count; argIdx++)
                        {
                            arguments[argIdx] = cmdBuffer[++index];
                        }

                        commands.Add(new ChCommand(currentClass, methodOffset, arguments));

                        break;
                    }
                }
            }

            ProcessCommands(vmm, commands.ToArray());
        }

        private void ProcessCommands(NvGpuVmm vmm, ChCommand[] commands)
        {
            int methodOffset = 0;

            foreach (ChCommand command in commands)
            {
                switch (command.MethodOffset)
                {
                    case MethSetMethod: methodOffset = command.Arguments[0]; break;

                    case MethSetData:
                    {
                        if (command.ClassId == ChClassId.NvDec)
                        {
                            _gpu.VideoDecoder.Process(vmm, methodOffset, command.Arguments);
                        }
                        else if (command.ClassId == ChClassId.GraphicsVic)
                        {
                            _gpu.VideoImageComposer.Process(vmm, methodOffset, command.Arguments);
                        }

                        break;
                    }
                }
            }
        }
    }
}