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
|
using Ryujinx.Graphics.Memory;
using Ryujinx.Graphics.Texture;
using System.Collections.Generic;
namespace Ryujinx.Graphics.Graphics3d
{
class NvGpuEngineP2mf : INvGpuEngine
{
public int[] Registers { get; private set; }
private NvGpu _gpu;
private Dictionary<int, NvGpuMethod> _methods;
private int _copyStartX;
private int _copyStartY;
private int _copyWidth;
private int _copyHeight;
private int _copyGobBlockHeight;
private long _copyAddress;
private int _copyOffset;
private int _copySize;
private bool _copyLinear;
private byte[] _buffer;
public NvGpuEngineP2mf(NvGpu gpu)
{
_gpu = gpu;
Registers = new int[0x80];
_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(0x6c, 1, 1, Execute);
AddMethod(0x6d, 1, 1, PushData);
}
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;
long dstAddress = MakeInt64From2xInt32(NvGpuEngineP2mfReg.DstAddress);
int dstPitch = ReadRegister(NvGpuEngineP2mfReg.DstPitch);
int dstBlkDim = ReadRegister(NvGpuEngineP2mfReg.DstBlockDim);
int dstX = ReadRegister(NvGpuEngineP2mfReg.DstX);
int dstY = ReadRegister(NvGpuEngineP2mfReg.DstY);
int dstWidth = ReadRegister(NvGpuEngineP2mfReg.DstWidth);
int dstHeight = ReadRegister(NvGpuEngineP2mfReg.DstHeight);
int lineLengthIn = ReadRegister(NvGpuEngineP2mfReg.LineLengthIn);
int lineCount = ReadRegister(NvGpuEngineP2mfReg.LineCount);
_copyLinear = (control & 1) != 0;
_copyGobBlockHeight = 1 << ((dstBlkDim >> 4) & 0xf);
_copyStartX = dstX;
_copyStartY = dstY;
_copyWidth = dstWidth;
_copyHeight = dstHeight;
_copyAddress = dstAddress;
_copyOffset = 0;
_copySize = lineLengthIn * lineCount;
_buffer = new byte[_copySize];
}
private void PushData(NvGpuVmm vmm, GpuMethodCall methCall)
{
if (_buffer == null)
{
return;
}
for (int shift = 0; shift < 32 && _copyOffset < _copySize; shift += 8, _copyOffset++)
{
_buffer[_copyOffset] = (byte)(methCall.Argument >> shift);
}
if (methCall.IsLastCall)
{
if (_copyLinear)
{
vmm.WriteBytes(_copyAddress, _buffer);
}
else
{
BlockLinearSwizzle swizzle = new BlockLinearSwizzle(
_copyWidth,
_copyHeight, 1,
_copyGobBlockHeight, 1, 1);
int srcOffset = 0;
for (int y = _copyStartY; y < _copyHeight && srcOffset < _copySize; y++)
for (int x = _copyStartX; x < _copyWidth && srcOffset < _copySize; x++)
{
int dstOffset = swizzle.GetSwizzleOffset(x, y, 0);
vmm.WriteByte(_copyAddress + dstOffset, _buffer[srcOffset++]);
}
}
_buffer = null;
}
}
private long MakeInt64From2xInt32(NvGpuEngineP2mfReg 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(NvGpuEngineP2mfReg reg)
{
return Registers[(int)reg];
}
private void WriteRegister(NvGpuEngineP2mfReg reg, int value)
{
Registers[(int)reg] = value;
}
}
}
|