CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/GPU/Software/SoftGpu.cpp
Views: 1401
1
// Copyright (c) 2012- PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#include <set>
19
#include "Common/System/Display.h"
20
#include "Common/GPU/OpenGL/GLFeatures.h"
21
22
#include "GPU/GPUState.h"
23
#include "GPU/ge_constants.h"
24
#include "GPU/Common/TextureDecoder.h"
25
#include "Common/Data/Convert/ColorConv.h"
26
#include "Common/GraphicsContext.h"
27
#include "Common/LogReporting.h"
28
#include "Core/Config.h"
29
#include "Core/ConfigValues.h"
30
#include "Core/Core.h"
31
#include "Core/Debugger/MemBlockInfo.h"
32
#include "Core/MemMap.h"
33
#include "Core/MemMapHelpers.h"
34
#include "Core/HLE/sceKernelInterrupt.h"
35
#include "Core/HLE/sceGe.h"
36
#include "Core/MIPS/MIPS.h"
37
#include "Core/Util/PPGeDraw.h"
38
#include "Common/Profiler/Profiler.h"
39
#include "Common/GPU/thin3d.h"
40
41
#include "GPU/Software/DrawPixel.h"
42
#include "GPU/Software/Rasterizer.h"
43
#include "GPU/Software/Sampler.h"
44
#include "GPU/Software/SoftGpu.h"
45
#include "GPU/Software/TransformUnit.h"
46
#include "GPU/Common/DrawEngineCommon.h"
47
#include "GPU/Common/PresentationCommon.h"
48
#include "Common/GPU/ShaderTranslation.h"
49
#include "GPU/Common/SplineCommon.h"
50
#include "GPU/Debugger/Debugger.h"
51
#include "GPU/Debugger/Record.h"
52
53
const int FB_WIDTH = 480;
54
const int FB_HEIGHT = 272;
55
56
uint8_t clut[1024];
57
FormatBuffer fb;
58
FormatBuffer depthbuf;
59
60
struct CommandInfo {
61
uint64_t flags;
62
SoftGPU::CmdFunc func;
63
};
64
static CommandInfo softgpuCmdInfo[256];
65
66
struct SoftwareCommandTableEntry {
67
uint8_t cmd;
68
uint8_t flags;
69
SoftDirty dirty;
70
SoftGPU::CmdFunc func;
71
};
72
73
// Software uses a different one, because dirty flags and execute funcs are a bit different.
74
const SoftwareCommandTableEntry softgpuCommandTable[] = {
75
{ GE_CMD_OFFSETADDR, FLAG_EXECUTE, SoftDirty::NONE, &GPUCommon::Execute_OffsetAddr },
76
{ GE_CMD_ORIGIN, FLAG_EXECUTE | FLAG_READS_PC, SoftDirty::NONE, &GPUCommon::Execute_Origin },
77
{ GE_CMD_JUMP, FLAG_EXECUTE | FLAG_READS_PC | FLAG_WRITES_PC, SoftDirty::NONE, &GPUCommon::Execute_Jump },
78
{ GE_CMD_CALL, FLAG_EXECUTE | FLAG_READS_PC | FLAG_WRITES_PC, SoftDirty::NONE, &SoftGPU::Execute_Call },
79
{ GE_CMD_RET, FLAG_EXECUTE | FLAG_READS_PC | FLAG_WRITES_PC, SoftDirty::NONE, &GPUCommon::Execute_Ret },
80
{ GE_CMD_END, FLAG_EXECUTE | FLAG_READS_PC | FLAG_WRITES_PC, SoftDirty::NONE, &GPUCommon::Execute_End },
81
{ GE_CMD_VADDR, FLAG_EXECUTE, SoftDirty::NONE, &GPUCommon::Execute_Vaddr },
82
{ GE_CMD_IADDR, FLAG_EXECUTE, SoftDirty::NONE, &GPUCommon::Execute_Iaddr },
83
{ GE_CMD_BJUMP, FLAG_EXECUTE | FLAG_READS_PC | FLAG_WRITES_PC, SoftDirty::NONE, &GPUCommon::Execute_BJump },
84
{ GE_CMD_BOUNDINGBOX, FLAG_EXECUTE, SoftDirty::NONE, &SoftGPU::Execute_BoundingBox },
85
86
{ GE_CMD_PRIM, FLAG_EXECUTE, SoftDirty::NONE, &SoftGPU::Execute_Prim },
87
{ GE_CMD_BEZIER, FLAG_EXECUTE, SoftDirty::NONE, &SoftGPU::Execute_Bezier },
88
{ GE_CMD_SPLINE, FLAG_EXECUTE, SoftDirty::NONE, &SoftGPU::Execute_Spline },
89
90
// Vertex type affects a number of things, mainly because of through.
91
{ GE_CMD_VERTEXTYPE, FLAG_EXECUTEONCHANGE, SoftDirty::TRANSFORM_BASIC, &SoftGPU::Execute_VertexType },
92
93
{ GE_CMD_LOADCLUT, FLAG_EXECUTE, SoftDirty::NONE, &SoftGPU::Execute_LoadClut },
94
95
// These two are actually processed in CMD_END, no flush needed.
96
{ GE_CMD_SIGNAL },
97
{ GE_CMD_FINISH },
98
99
// Changes that dirty the framebuffer or depthbuffer pointer/size.
100
{ GE_CMD_FRAMEBUFPTR, FLAG_EXECUTEONCHANGE, SoftDirty::BINNER_RANGE, &SoftGPU::Execute_FramebufPtr },
101
{ GE_CMD_FRAMEBUFWIDTH, FLAG_EXECUTEONCHANGE, SoftDirty::BINNER_RANGE | SoftDirty::PIXEL_BASIC | SoftDirty::PIXEL_CACHED, &SoftGPU::Execute_FramebufPtr },
102
{ GE_CMD_FRAMEBUFPIXFORMAT, FLAG_EXECUTEONCHANGE, SoftDirty::BINNER_RANGE | SoftDirty::PIXEL_BASIC | SoftDirty::PIXEL_STENCIL | SoftDirty::PIXEL_WRITEMASK, &SoftGPU::Execute_FramebufFormat },
103
{ GE_CMD_ZBUFPTR, FLAG_EXECUTEONCHANGE, SoftDirty::BINNER_RANGE, &SoftGPU::Execute_ZbufPtr },
104
{ GE_CMD_ZBUFWIDTH, FLAG_EXECUTEONCHANGE, SoftDirty::BINNER_RANGE | SoftDirty::PIXEL_BASIC | SoftDirty::PIXEL_CACHED, &SoftGPU::Execute_ZbufPtr },
105
106
{ GE_CMD_FOGCOLOR, 0, SoftDirty::PIXEL_CACHED },
107
{ GE_CMD_FOG1, 0, SoftDirty::TRANSFORM_FOG },
108
{ GE_CMD_FOG2, 0, SoftDirty::TRANSFORM_FOG },
109
110
{ GE_CMD_CLEARMODE, 0, SoftDirty::TRANSFORM_BASIC | SoftDirty::RAST_BASIC | SoftDirty::RAST_TEX | SoftDirty::SAMPLER_BASIC | SoftDirty::SAMPLER_TEXLIST | SoftDirty::PIXEL_BASIC | SoftDirty::PIXEL_ALPHA | SoftDirty::PIXEL_STENCIL | SoftDirty::PIXEL_CACHED | SoftDirty::BINNER_RANGE | SoftDirty::BINNER_OVERLAP },
111
{ GE_CMD_TEXTUREMAPENABLE, 0, SoftDirty::SAMPLER_BASIC | SoftDirty::SAMPLER_TEXLIST | SoftDirty::RAST_TEX | SoftDirty::TRANSFORM_BASIC | SoftDirty::BINNER_OVERLAP },
112
{ GE_CMD_FOGENABLE, 0, SoftDirty::PIXEL_BASIC | SoftDirty::PIXEL_CACHED | SoftDirty::TRANSFORM_BASIC | SoftDirty::TRANSFORM_FOG | SoftDirty::TRANSFORM_MATRIX },
113
{ GE_CMD_TEXMODE, 0, SoftDirty::SAMPLER_BASIC | SoftDirty::SAMPLER_TEXLIST | SoftDirty::RAST_TEX },
114
// Currently this doesn't affect any state, but maybe it should.
115
{ GE_CMD_TEXSHADELS },
116
{ GE_CMD_SHADEMODE, 0, SoftDirty::RAST_BASIC },
117
{ GE_CMD_TEXFUNC, 0, SoftDirty::SAMPLER_BASIC },
118
{ GE_CMD_COLORTEST, 0, SoftDirty::PIXEL_BASIC | SoftDirty::PIXEL_CACHED },
119
{ GE_CMD_ALPHATESTENABLE, 0, SoftDirty::PIXEL_BASIC | SoftDirty::PIXEL_ALPHA },
120
{ GE_CMD_COLORTESTENABLE, 0, SoftDirty::PIXEL_BASIC | SoftDirty::PIXEL_CACHED },
121
{ GE_CMD_COLORTESTMASK, 0, SoftDirty::PIXEL_CACHED },
122
123
{ GE_CMD_REVERSENORMAL, 0, SoftDirty::TRANSFORM_BASIC },
124
{ GE_CMD_LIGHTINGENABLE, 0, SoftDirty::TRANSFORM_BASIC | SoftDirty::TRANSFORM_MATRIX | SoftDirty::LIGHT_BASIC | SoftDirty::LIGHT_MATERIAL | SoftDirty::LIGHT_0 | SoftDirty::LIGHT_1 | SoftDirty::LIGHT_2 | SoftDirty::LIGHT_3 },
125
{ GE_CMD_LIGHTENABLE0, 0, SoftDirty::TRANSFORM_MATRIX | SoftDirty::LIGHT_BASIC | SoftDirty::LIGHT_MATERIAL | SoftDirty::LIGHT_0 },
126
{ GE_CMD_LIGHTENABLE1, 0, SoftDirty::TRANSFORM_MATRIX | SoftDirty::LIGHT_BASIC | SoftDirty::LIGHT_MATERIAL | SoftDirty::LIGHT_1 },
127
{ GE_CMD_LIGHTENABLE2, 0, SoftDirty::TRANSFORM_MATRIX | SoftDirty::LIGHT_BASIC | SoftDirty::LIGHT_MATERIAL | SoftDirty::LIGHT_2 },
128
{ GE_CMD_LIGHTENABLE3, 0, SoftDirty::TRANSFORM_MATRIX | SoftDirty::LIGHT_BASIC | SoftDirty::LIGHT_MATERIAL | SoftDirty::LIGHT_3 },
129
{ GE_CMD_LIGHTTYPE0, 0, SoftDirty::TRANSFORM_MATRIX | SoftDirty::LIGHT_0 },
130
{ GE_CMD_LIGHTTYPE1, 0, SoftDirty::TRANSFORM_MATRIX | SoftDirty::LIGHT_1 },
131
{ GE_CMD_LIGHTTYPE2, 0, SoftDirty::TRANSFORM_MATRIX | SoftDirty::LIGHT_2 },
132
{ GE_CMD_LIGHTTYPE3, 0, SoftDirty::TRANSFORM_MATRIX | SoftDirty::LIGHT_3 },
133
{ GE_CMD_MATERIALUPDATE, 0, SoftDirty::LIGHT_BASIC | SoftDirty::LIGHT_MATERIAL },
134
135
{ GE_CMD_LIGHTMODE, 0, SoftDirty::LIGHT_BASIC },
136
137
{ GE_CMD_TEXFILTER, 0, SoftDirty::SAMPLER_BASIC | SoftDirty::SAMPLER_TEXLIST | SoftDirty::RAST_TEX },
138
{ GE_CMD_TEXWRAP, 0, SoftDirty::SAMPLER_BASIC },
139
140
{ GE_CMD_ALPHATEST, 0, SoftDirty::PIXEL_BASIC | SoftDirty::PIXEL_ALPHA },
141
{ GE_CMD_COLORREF, 0, SoftDirty::PIXEL_CACHED },
142
{ GE_CMD_TEXENVCOLOR, 0, SoftDirty::PIXEL_CACHED },
143
144
// Currently, this is not part of state, just read on vertex processing.
145
{ GE_CMD_CULL },
146
{ GE_CMD_CULLFACEENABLE },
147
148
{ GE_CMD_DITHERENABLE, 0, SoftDirty::PIXEL_BASIC },
149
{ GE_CMD_STENCILOP, 0, SoftDirty::PIXEL_BASIC | SoftDirty::PIXEL_STENCIL },
150
{ GE_CMD_STENCILTEST, 0, SoftDirty::PIXEL_BASIC | SoftDirty::PIXEL_STENCIL },
151
{ GE_CMD_STENCILTESTENABLE, 0, SoftDirty::PIXEL_BASIC | SoftDirty::PIXEL_STENCIL },
152
{ GE_CMD_ALPHABLENDENABLE, 0, SoftDirty::PIXEL_BASIC | SoftDirty::PIXEL_ALPHA },
153
{ GE_CMD_BLENDMODE, 0, SoftDirty::PIXEL_BASIC | SoftDirty::PIXEL_ALPHA },
154
{ GE_CMD_BLENDFIXEDA, 0, SoftDirty::PIXEL_BASIC | SoftDirty::PIXEL_ALPHA | SoftDirty::PIXEL_CACHED },
155
{ GE_CMD_BLENDFIXEDB, 0, SoftDirty::PIXEL_BASIC | SoftDirty::PIXEL_ALPHA | SoftDirty::PIXEL_CACHED },
156
{ GE_CMD_MASKRGB, 0, SoftDirty::PIXEL_BASIC | SoftDirty::PIXEL_WRITEMASK },
157
{ GE_CMD_MASKALPHA, 0, SoftDirty::PIXEL_BASIC | SoftDirty::PIXEL_WRITEMASK },
158
{ GE_CMD_ZTEST, 0, SoftDirty::PIXEL_BASIC },
159
{ GE_CMD_ZTESTENABLE, 0, SoftDirty::PIXEL_BASIC | SoftDirty::BINNER_RANGE | SoftDirty::BINNER_OVERLAP },
160
{ GE_CMD_ZWRITEDISABLE, 0, SoftDirty::PIXEL_BASIC | SoftDirty::BINNER_RANGE | SoftDirty::BINNER_OVERLAP },
161
{ GE_CMD_LOGICOP, 0, SoftDirty::PIXEL_BASIC | SoftDirty::PIXEL_CACHED },
162
{ GE_CMD_LOGICOPENABLE, 0, SoftDirty::PIXEL_BASIC | SoftDirty::PIXEL_CACHED },
163
164
{ GE_CMD_TEXMAPMODE, 0, SoftDirty::TRANSFORM_BASIC | SoftDirty::RAST_TEX },
165
166
// These are read on every SubmitPrim, no need for dirtying or flushing.
167
{ GE_CMD_TEXSCALEU },
168
{ GE_CMD_TEXSCALEV },
169
{ GE_CMD_TEXOFFSETU },
170
{ GE_CMD_TEXOFFSETV },
171
172
{ GE_CMD_TEXSIZE0, 0, SoftDirty::SAMPLER_TEXLIST | SoftDirty::BINNER_OVERLAP },
173
{ GE_CMD_TEXSIZE1, 0, SoftDirty::SAMPLER_TEXLIST | SoftDirty::BINNER_OVERLAP },
174
{ GE_CMD_TEXSIZE2, 0, SoftDirty::SAMPLER_TEXLIST | SoftDirty::BINNER_OVERLAP },
175
{ GE_CMD_TEXSIZE3, 0, SoftDirty::SAMPLER_TEXLIST | SoftDirty::BINNER_OVERLAP },
176
{ GE_CMD_TEXSIZE4, 0, SoftDirty::SAMPLER_TEXLIST | SoftDirty::BINNER_OVERLAP },
177
{ GE_CMD_TEXSIZE5, 0, SoftDirty::SAMPLER_TEXLIST | SoftDirty::BINNER_OVERLAP },
178
{ GE_CMD_TEXSIZE6, 0, SoftDirty::SAMPLER_TEXLIST | SoftDirty::BINNER_OVERLAP },
179
{ GE_CMD_TEXSIZE7, 0, SoftDirty::SAMPLER_TEXLIST | SoftDirty::BINNER_OVERLAP },
180
{ GE_CMD_TEXFORMAT, 0, SoftDirty::SAMPLER_BASIC | SoftDirty::SAMPLER_TEXLIST | SoftDirty::BINNER_OVERLAP },
181
{ GE_CMD_TEXLEVEL, 0, SoftDirty::RAST_TEX },
182
{ GE_CMD_TEXLODSLOPE, 0, SoftDirty::RAST_TEX },
183
{ GE_CMD_TEXADDR0, 0, SoftDirty::SAMPLER_TEXLIST | SoftDirty::BINNER_OVERLAP },
184
{ GE_CMD_TEXADDR1, 0, SoftDirty::SAMPLER_TEXLIST | SoftDirty::BINNER_OVERLAP },
185
{ GE_CMD_TEXADDR2, 0, SoftDirty::SAMPLER_TEXLIST | SoftDirty::BINNER_OVERLAP },
186
{ GE_CMD_TEXADDR3, 0, SoftDirty::SAMPLER_TEXLIST | SoftDirty::BINNER_OVERLAP },
187
{ GE_CMD_TEXADDR4, 0, SoftDirty::SAMPLER_TEXLIST | SoftDirty::BINNER_OVERLAP },
188
{ GE_CMD_TEXADDR5, 0, SoftDirty::SAMPLER_TEXLIST | SoftDirty::BINNER_OVERLAP },
189
{ GE_CMD_TEXADDR6, 0, SoftDirty::SAMPLER_TEXLIST | SoftDirty::BINNER_OVERLAP },
190
{ GE_CMD_TEXADDR7, 0, SoftDirty::SAMPLER_TEXLIST | SoftDirty::BINNER_OVERLAP },
191
{ GE_CMD_TEXBUFWIDTH0, 0, SoftDirty::SAMPLER_TEXLIST | SoftDirty::BINNER_OVERLAP },
192
{ GE_CMD_TEXBUFWIDTH1, 0, SoftDirty::SAMPLER_TEXLIST | SoftDirty::BINNER_OVERLAP },
193
{ GE_CMD_TEXBUFWIDTH2, 0, SoftDirty::SAMPLER_TEXLIST | SoftDirty::BINNER_OVERLAP },
194
{ GE_CMD_TEXBUFWIDTH3, 0, SoftDirty::SAMPLER_TEXLIST | SoftDirty::BINNER_OVERLAP },
195
{ GE_CMD_TEXBUFWIDTH4, 0, SoftDirty::SAMPLER_TEXLIST | SoftDirty::BINNER_OVERLAP },
196
{ GE_CMD_TEXBUFWIDTH5, 0, SoftDirty::SAMPLER_TEXLIST | SoftDirty::BINNER_OVERLAP },
197
{ GE_CMD_TEXBUFWIDTH6, 0, SoftDirty::SAMPLER_TEXLIST | SoftDirty::BINNER_OVERLAP },
198
{ GE_CMD_TEXBUFWIDTH7, 0, SoftDirty::SAMPLER_TEXLIST | SoftDirty::BINNER_OVERLAP },
199
200
{ GE_CMD_CLUTADDR },
201
{ GE_CMD_CLUTADDRUPPER },
202
{ GE_CMD_CLUTFORMAT, 0, SoftDirty::SAMPLER_BASIC },
203
204
// Morph weights. TODO: Remove precomputation?
205
{ GE_CMD_MORPHWEIGHT0, FLAG_EXECUTEONCHANGE, SoftDirty::NONE, &GPUCommon::Execute_MorphWeight },
206
{ GE_CMD_MORPHWEIGHT1, FLAG_EXECUTEONCHANGE, SoftDirty::NONE, &GPUCommon::Execute_MorphWeight },
207
{ GE_CMD_MORPHWEIGHT2, FLAG_EXECUTEONCHANGE, SoftDirty::NONE, &GPUCommon::Execute_MorphWeight },
208
{ GE_CMD_MORPHWEIGHT3, FLAG_EXECUTEONCHANGE, SoftDirty::NONE, &GPUCommon::Execute_MorphWeight },
209
{ GE_CMD_MORPHWEIGHT4, FLAG_EXECUTEONCHANGE, SoftDirty::NONE, &GPUCommon::Execute_MorphWeight },
210
{ GE_CMD_MORPHWEIGHT5, FLAG_EXECUTEONCHANGE, SoftDirty::NONE, &GPUCommon::Execute_MorphWeight },
211
{ GE_CMD_MORPHWEIGHT6, FLAG_EXECUTEONCHANGE, SoftDirty::NONE, &GPUCommon::Execute_MorphWeight },
212
{ GE_CMD_MORPHWEIGHT7, FLAG_EXECUTEONCHANGE, SoftDirty::NONE, &GPUCommon::Execute_MorphWeight },
213
214
// No state of flushing required for patch parameters, currently.
215
{ GE_CMD_PATCHDIVISION },
216
{ GE_CMD_PATCHPRIMITIVE },
217
{ GE_CMD_PATCHFACING },
218
{ GE_CMD_PATCHCULLENABLE },
219
220
// Can probably ignore this one as we don't support AA lines.
221
{ GE_CMD_ANTIALIASENABLE, 0, SoftDirty::RAST_BASIC },
222
223
// Viewport and offset for positions.
224
{ GE_CMD_OFFSETX, 0, SoftDirty::RAST_OFFSET },
225
{ GE_CMD_OFFSETY, 0, SoftDirty::RAST_OFFSET },
226
{ GE_CMD_VIEWPORTXSCALE, 0, SoftDirty::TRANSFORM_VIEWPORT },
227
{ GE_CMD_VIEWPORTYSCALE, 0, SoftDirty::TRANSFORM_VIEWPORT },
228
{ GE_CMD_VIEWPORTXCENTER, 0, SoftDirty::TRANSFORM_VIEWPORT },
229
{ GE_CMD_VIEWPORTYCENTER, 0, SoftDirty::TRANSFORM_VIEWPORT },
230
{ GE_CMD_VIEWPORTZSCALE, 0, SoftDirty::TRANSFORM_VIEWPORT },
231
{ GE_CMD_VIEWPORTZCENTER, 0, SoftDirty::TRANSFORM_VIEWPORT },
232
{ GE_CMD_DEPTHCLAMPENABLE, 0, SoftDirty::TRANSFORM_BASIC },
233
234
// Z clipping.
235
{ GE_CMD_MINZ, 0, SoftDirty::PIXEL_BASIC | SoftDirty::PIXEL_CACHED },
236
{ GE_CMD_MAXZ, 0, SoftDirty::PIXEL_BASIC | SoftDirty::PIXEL_CACHED },
237
238
// Region doesn't seem to affect scissor or anything.
239
// As long as REGION1 is zero, REGION2 is effectively another scissor.
240
{ GE_CMD_REGION1, 0, SoftDirty::BINNER_RANGE },
241
{ GE_CMD_REGION2, 0, SoftDirty::BINNER_RANGE },
242
243
// Scissor, only used by the binner.
244
{ GE_CMD_SCISSOR1, 0, SoftDirty::BINNER_RANGE },
245
{ GE_CMD_SCISSOR2, 0, SoftDirty::BINNER_RANGE },
246
247
// Lighting base colors.
248
{ GE_CMD_AMBIENTCOLOR, 0, SoftDirty::LIGHT_MATERIAL },
249
{ GE_CMD_AMBIENTALPHA, 0, SoftDirty::LIGHT_MATERIAL },
250
{ GE_CMD_MATERIALDIFFUSE, 0, SoftDirty::LIGHT_MATERIAL | SoftDirty::LIGHT_0 | SoftDirty::LIGHT_1 | SoftDirty::LIGHT_2 | SoftDirty::LIGHT_3 },
251
// Not currently state, but maybe should be.
252
{ GE_CMD_MATERIALEMISSIVE, 0, SoftDirty::NONE },
253
{ GE_CMD_MATERIALAMBIENT, 0, SoftDirty::LIGHT_MATERIAL | SoftDirty::LIGHT_0 | SoftDirty::LIGHT_1 | SoftDirty::LIGHT_2 | SoftDirty::LIGHT_3 },
254
{ GE_CMD_MATERIALALPHA, 0, SoftDirty::LIGHT_MATERIAL | SoftDirty::LIGHT_0 | SoftDirty::LIGHT_1 | SoftDirty::LIGHT_2 | SoftDirty::LIGHT_3 },
255
{ GE_CMD_MATERIALSPECULAR, 0, SoftDirty::LIGHT_BASIC | SoftDirty::LIGHT_MATERIAL | SoftDirty::LIGHT_0 | SoftDirty::LIGHT_1 | SoftDirty::LIGHT_2 | SoftDirty::LIGHT_3 },
256
{ GE_CMD_MATERIALSPECULARCOEF, 0, SoftDirty::LIGHT_BASIC },
257
258
{ GE_CMD_LX0, 0, SoftDirty::LIGHT_0 },
259
{ GE_CMD_LY0, 0, SoftDirty::LIGHT_0 },
260
{ GE_CMD_LZ0, 0, SoftDirty::LIGHT_0 },
261
{ GE_CMD_LX1, 0, SoftDirty::LIGHT_1 },
262
{ GE_CMD_LY1, 0, SoftDirty::LIGHT_1 },
263
{ GE_CMD_LZ1, 0, SoftDirty::LIGHT_1 },
264
{ GE_CMD_LX2, 0, SoftDirty::LIGHT_2 },
265
{ GE_CMD_LY2, 0, SoftDirty::LIGHT_2 },
266
{ GE_CMD_LZ2, 0, SoftDirty::LIGHT_2 },
267
{ GE_CMD_LX3, 0, SoftDirty::LIGHT_3 },
268
{ GE_CMD_LY3, 0, SoftDirty::LIGHT_3 },
269
{ GE_CMD_LZ3, 0, SoftDirty::LIGHT_3 },
270
271
{ GE_CMD_LDX0, 0, SoftDirty::LIGHT_0 },
272
{ GE_CMD_LDY0, 0, SoftDirty::LIGHT_0 },
273
{ GE_CMD_LDZ0, 0, SoftDirty::LIGHT_0 },
274
{ GE_CMD_LDX1, 0, SoftDirty::LIGHT_1 },
275
{ GE_CMD_LDY1, 0, SoftDirty::LIGHT_1 },
276
{ GE_CMD_LDZ1, 0, SoftDirty::LIGHT_1 },
277
{ GE_CMD_LDX2, 0, SoftDirty::LIGHT_2 },
278
{ GE_CMD_LDY2, 0, SoftDirty::LIGHT_2 },
279
{ GE_CMD_LDZ2, 0, SoftDirty::LIGHT_2 },
280
{ GE_CMD_LDX3, 0, SoftDirty::LIGHT_3 },
281
{ GE_CMD_LDY3, 0, SoftDirty::LIGHT_3 },
282
{ GE_CMD_LDZ3, 0, SoftDirty::LIGHT_3 },
283
284
{ GE_CMD_LKA0, 0, SoftDirty::LIGHT_0 },
285
{ GE_CMD_LKB0, 0, SoftDirty::LIGHT_0 },
286
{ GE_CMD_LKC0, 0, SoftDirty::LIGHT_0 },
287
{ GE_CMD_LKA1, 0, SoftDirty::LIGHT_1 },
288
{ GE_CMD_LKB1, 0, SoftDirty::LIGHT_1 },
289
{ GE_CMD_LKC1, 0, SoftDirty::LIGHT_1 },
290
{ GE_CMD_LKA2, 0, SoftDirty::LIGHT_2 },
291
{ GE_CMD_LKB2, 0, SoftDirty::LIGHT_2 },
292
{ GE_CMD_LKC2, 0, SoftDirty::LIGHT_2 },
293
{ GE_CMD_LKA3, 0, SoftDirty::LIGHT_3 },
294
{ GE_CMD_LKB3, 0, SoftDirty::LIGHT_3 },
295
{ GE_CMD_LKC3, 0, SoftDirty::LIGHT_3 },
296
297
{ GE_CMD_LKS0, 0, SoftDirty::LIGHT_0 },
298
{ GE_CMD_LKS1, 0, SoftDirty::LIGHT_1 },
299
{ GE_CMD_LKS2, 0, SoftDirty::LIGHT_2 },
300
{ GE_CMD_LKS3, 0, SoftDirty::LIGHT_3 },
301
302
{ GE_CMD_LKO0, 0, SoftDirty::LIGHT_0 },
303
{ GE_CMD_LKO1, 0, SoftDirty::LIGHT_1 },
304
{ GE_CMD_LKO2, 0, SoftDirty::LIGHT_2 },
305
{ GE_CMD_LKO3, 0, SoftDirty::LIGHT_3 },
306
307
// Specific light colors.
308
{ GE_CMD_LAC0, 0, SoftDirty::LIGHT_MATERIAL | SoftDirty::LIGHT_0 },
309
{ GE_CMD_LDC0, 0, SoftDirty::LIGHT_BASIC | SoftDirty::LIGHT_MATERIAL | SoftDirty::LIGHT_0 },
310
{ GE_CMD_LSC0, 0, SoftDirty::LIGHT_BASIC | SoftDirty::LIGHT_MATERIAL | SoftDirty::LIGHT_0 },
311
{ GE_CMD_LAC1, 0, SoftDirty::LIGHT_MATERIAL | SoftDirty::LIGHT_1 },
312
{ GE_CMD_LDC1, 0, SoftDirty::LIGHT_BASIC | SoftDirty::LIGHT_MATERIAL | SoftDirty::LIGHT_1 },
313
{ GE_CMD_LSC1, 0, SoftDirty::LIGHT_BASIC | SoftDirty::LIGHT_MATERIAL | SoftDirty::LIGHT_1 },
314
{ GE_CMD_LAC2, 0, SoftDirty::LIGHT_MATERIAL | SoftDirty::LIGHT_2 },
315
{ GE_CMD_LDC2, 0, SoftDirty::LIGHT_BASIC | SoftDirty::LIGHT_MATERIAL | SoftDirty::LIGHT_2 },
316
{ GE_CMD_LSC2, 0, SoftDirty::LIGHT_BASIC | SoftDirty::LIGHT_MATERIAL | SoftDirty::LIGHT_2 },
317
{ GE_CMD_LAC3, 0, SoftDirty::LIGHT_MATERIAL | SoftDirty::LIGHT_3 },
318
{ GE_CMD_LDC3, 0, SoftDirty::LIGHT_BASIC | SoftDirty::LIGHT_MATERIAL | SoftDirty::LIGHT_3 },
319
{ GE_CMD_LSC3, 0, SoftDirty::LIGHT_BASIC | SoftDirty::LIGHT_MATERIAL | SoftDirty::LIGHT_3 },
320
321
// These are currently ignored, but might do flushing later.
322
{ GE_CMD_TEXFLUSH },
323
{ GE_CMD_TEXSYNC },
324
325
// These are just nop or part of other later commands.
326
{ GE_CMD_NOP },
327
{ GE_CMD_BASE },
328
{ GE_CMD_TRANSFERSRC },
329
{ GE_CMD_TRANSFERSRCW },
330
{ GE_CMD_TRANSFERDST },
331
{ GE_CMD_TRANSFERDSTW },
332
{ GE_CMD_TRANSFERSRCPOS },
333
{ GE_CMD_TRANSFERDSTPOS },
334
{ GE_CMD_TRANSFERSIZE },
335
336
// This will flush if necessary.
337
{ GE_CMD_TRANSFERSTART, FLAG_EXECUTE | FLAG_READS_PC, SoftDirty::NONE, &SoftGPU::Execute_BlockTransferStart },
338
339
// We cache the dither matrix, but the values affect little.
340
{ GE_CMD_DITH0, 0, SoftDirty::PIXEL_DITHER },
341
{ GE_CMD_DITH1, 0, SoftDirty::PIXEL_DITHER },
342
{ GE_CMD_DITH2, 0, SoftDirty::PIXEL_DITHER },
343
{ GE_CMD_DITH3, 0, SoftDirty::PIXEL_DITHER },
344
345
{ GE_CMD_WORLDMATRIXNUMBER, FLAG_EXECUTE, SoftDirty::NONE, &SoftGPU::Execute_WorldMtxNum },
346
{ GE_CMD_WORLDMATRIXDATA, FLAG_EXECUTE, SoftDirty::NONE, &SoftGPU::Execute_WorldMtxData },
347
{ GE_CMD_VIEWMATRIXNUMBER, FLAG_EXECUTE, SoftDirty::NONE, &SoftGPU::Execute_ViewMtxNum },
348
{ GE_CMD_VIEWMATRIXDATA, FLAG_EXECUTE, SoftDirty::NONE, &SoftGPU::Execute_ViewMtxData },
349
{ GE_CMD_PROJMATRIXNUMBER, FLAG_EXECUTE, SoftDirty::NONE, &SoftGPU::Execute_ProjMtxNum },
350
{ GE_CMD_PROJMATRIXDATA, FLAG_EXECUTE, SoftDirty::NONE, &SoftGPU::Execute_ProjMtxData },
351
// Currently not state.
352
{ GE_CMD_TGENMATRIXNUMBER, FLAG_EXECUTE, SoftDirty::NONE, &SoftGPU::Execute_TgenMtxNum },
353
{ GE_CMD_TGENMATRIXDATA, FLAG_EXECUTE, SoftDirty::NONE, &SoftGPU::Execute_TgenMtxData },
354
{ GE_CMD_BONEMATRIXNUMBER, FLAG_EXECUTE, SoftDirty::NONE, &SoftGPU::Execute_BoneMtxNum },
355
{ GE_CMD_BONEMATRIXDATA, FLAG_EXECUTE, SoftDirty::NONE, &SoftGPU::Execute_BoneMtxData },
356
357
// Vertex Screen/Texture/Color
358
{ GE_CMD_VSCX },
359
{ GE_CMD_VSCY },
360
{ GE_CMD_VSCZ },
361
{ GE_CMD_VTCS },
362
{ GE_CMD_VTCT },
363
{ GE_CMD_VTCQ },
364
{ GE_CMD_VCV },
365
{ GE_CMD_VAP, FLAG_EXECUTE, SoftDirty::NONE, &SoftGPU::Execute_ImmVertexAlphaPrim },
366
{ GE_CMD_VFC },
367
{ GE_CMD_VSCV },
368
369
// "Missing" commands (gaps in the sequence)
370
{ GE_CMD_UNKNOWN_03, FLAG_EXECUTE, SoftDirty::NONE, &GPUCommon::Execute_Unknown },
371
{ GE_CMD_UNKNOWN_0D, FLAG_EXECUTE, SoftDirty::NONE, &GPUCommon::Execute_Unknown },
372
{ GE_CMD_UNKNOWN_11, FLAG_EXECUTE, SoftDirty::NONE, &GPUCommon::Execute_Unknown },
373
{ GE_CMD_UNKNOWN_29, FLAG_EXECUTE, SoftDirty::NONE, &GPUCommon::Execute_Unknown },
374
{ GE_CMD_UNKNOWN_34, FLAG_EXECUTE, SoftDirty::NONE, &GPUCommon::Execute_Unknown },
375
{ GE_CMD_UNKNOWN_35, FLAG_EXECUTE, SoftDirty::NONE, &GPUCommon::Execute_Unknown },
376
{ GE_CMD_UNKNOWN_39, FLAG_EXECUTE, SoftDirty::NONE, &GPUCommon::Execute_Unknown },
377
{ GE_CMD_UNKNOWN_4E, FLAG_EXECUTE, SoftDirty::NONE, &GPUCommon::Execute_Unknown },
378
{ GE_CMD_UNKNOWN_4F, FLAG_EXECUTE, SoftDirty::NONE, &GPUCommon::Execute_Unknown },
379
{ GE_CMD_UNKNOWN_52, FLAG_EXECUTE, SoftDirty::NONE, &GPUCommon::Execute_Unknown },
380
{ GE_CMD_UNKNOWN_59, FLAG_EXECUTE, SoftDirty::NONE, &GPUCommon::Execute_Unknown },
381
{ GE_CMD_UNKNOWN_5A, FLAG_EXECUTE, SoftDirty::NONE, &GPUCommon::Execute_Unknown },
382
{ GE_CMD_UNKNOWN_B6, FLAG_EXECUTE, SoftDirty::NONE, &GPUCommon::Execute_Unknown },
383
{ GE_CMD_UNKNOWN_B7, FLAG_EXECUTE, SoftDirty::NONE, &GPUCommon::Execute_Unknown },
384
{ GE_CMD_UNKNOWN_D1, FLAG_EXECUTE, SoftDirty::NONE, &GPUCommon::Execute_Unknown },
385
{ GE_CMD_UNKNOWN_ED, FLAG_EXECUTE, SoftDirty::NONE, &GPUCommon::Execute_Unknown },
386
{ GE_CMD_UNKNOWN_EF, FLAG_EXECUTE, SoftDirty::NONE, &GPUCommon::Execute_Unknown },
387
{ GE_CMD_UNKNOWN_FA, FLAG_EXECUTE, SoftDirty::NONE, &GPUCommon::Execute_Unknown },
388
{ GE_CMD_UNKNOWN_FB, FLAG_EXECUTE, SoftDirty::NONE, &GPUCommon::Execute_Unknown },
389
{ GE_CMD_UNKNOWN_FC, FLAG_EXECUTE, SoftDirty::NONE, &GPUCommon::Execute_Unknown },
390
{ GE_CMD_UNKNOWN_FD, FLAG_EXECUTE, SoftDirty::NONE, &GPUCommon::Execute_Unknown },
391
{ GE_CMD_UNKNOWN_FE, FLAG_EXECUTE, SoftDirty::NONE, &GPUCommon::Execute_Unknown },
392
// Appears to be debugging related or something? Hit a lot in GoW.
393
{ GE_CMD_NOP_FF },
394
};
395
396
SoftGPU::SoftGPU(GraphicsContext *gfxCtx, Draw::DrawContext *draw)
397
: GPUCommon(gfxCtx, draw)
398
{
399
fb.data = Memory::GetPointerWrite(0x44000000); // TODO: correct default address?
400
depthbuf.data = Memory::GetPointerWrite(0x44000000); // TODO: correct default address?
401
402
memset(softgpuCmdInfo, 0, sizeof(softgpuCmdInfo));
403
404
// Convert the command table to a faster format, and check for dupes.
405
std::set<u8> dupeCheck;
406
for (size_t i = 0; i < ARRAY_SIZE(softgpuCommandTable); i++) {
407
const u8 cmd = softgpuCommandTable[i].cmd;
408
if (dupeCheck.find(cmd) != dupeCheck.end()) {
409
ERROR_LOG(Log::G3D, "Command table Dupe: %02x (%i)", (int)cmd, (int)cmd);
410
} else {
411
dupeCheck.insert(cmd);
412
}
413
softgpuCmdInfo[cmd].flags |= (uint64_t)softgpuCommandTable[i].flags | ((uint64_t)softgpuCommandTable[i].dirty << 8);
414
softgpuCmdInfo[cmd].func = softgpuCommandTable[i].func;
415
if ((softgpuCmdInfo[cmd].flags & (FLAG_EXECUTE | FLAG_EXECUTEONCHANGE)) && !softgpuCmdInfo[cmd].func) {
416
// Can't have FLAG_EXECUTE commands without a function pointer to execute.
417
Crash();
418
}
419
}
420
// Find commands missing from the table.
421
for (int i = 0; i < 0xEF; i++) {
422
if (dupeCheck.find((u8)i) == dupeCheck.end()) {
423
ERROR_LOG(Log::G3D, "Command missing from table: %02x (%i)", i, i);
424
}
425
}
426
427
memset(vramDirty_, (uint8_t)(SoftGPUVRAMDirty::DIRTY | SoftGPUVRAMDirty::REALLY_DIRTY), sizeof(vramDirty_));
428
// TODO: Is there a default?
429
displayFramebuf_ = 0;
430
displayStride_ = 512;
431
displayFormat_ = GE_FORMAT_8888;
432
433
Rasterizer::Init();
434
Sampler::Init();
435
drawEngine_ = new SoftwareDrawEngine();
436
if (!drawEngine_)
437
return;
438
439
drawEngine_->Init();
440
drawEngineCommon_ = drawEngine_;
441
442
// Push the initial CLUT buffer in case it's all zero (we push only on change.)
443
if (drawEngine_->transformUnit.IsStarted())
444
drawEngine_->transformUnit.NotifyClutUpdate(clut);
445
446
// No need to flush for simple parameter changes.
447
flushOnParams_ = false;
448
449
if (gfxCtx && draw) {
450
presentation_ = new PresentationCommon(draw_);
451
presentation_->SetLanguage(draw_->GetShaderLanguageDesc().shaderLanguage);
452
presentation_->UpdateDisplaySize(PSP_CoreParameter().pixelWidth, PSP_CoreParameter().pixelHeight);
453
presentation_->UpdateRenderSize(PSP_CoreParameter().renderWidth, PSP_CoreParameter().renderHeight);
454
}
455
456
NotifyConfigChanged();
457
NotifyDisplayResized();
458
NotifyRenderResized();
459
}
460
461
void SoftGPU::DeviceLost() {
462
if (presentation_)
463
presentation_->DeviceLost();
464
draw_ = nullptr;
465
if (fbTex) {
466
fbTex->Release();
467
fbTex = nullptr;
468
}
469
}
470
471
void SoftGPU::DeviceRestore(Draw::DrawContext *draw) {
472
draw_ = draw;
473
if (presentation_)
474
presentation_->DeviceRestore(draw_);
475
PPGeSetDrawContext(draw_);
476
}
477
478
SoftGPU::~SoftGPU() {
479
if (fbTex) {
480
fbTex->Release();
481
fbTex = nullptr;
482
}
483
484
delete presentation_;
485
delete drawEngine_;
486
487
Sampler::Shutdown();
488
Rasterizer::Shutdown();
489
}
490
491
void SoftGPU::SetDisplayFramebuffer(u32 framebuf, u32 stride, GEBufferFormat format) {
492
// Seems like this can point into RAM, but should be VRAM if not in RAM.
493
displayFramebuf_ = (framebuf & 0xFF000000) == 0 ? 0x44000000 | framebuf : framebuf;
494
displayStride_ = stride;
495
displayFormat_ = format;
496
GPUDebug::NotifyDisplay(framebuf, stride, format);
497
GPURecord::NotifyDisplay(framebuf, stride, format);
498
}
499
500
DSStretch g_DarkStalkerStretch;
501
502
void SoftGPU::ConvertTextureDescFrom16(Draw::TextureDesc &desc, int srcwidth, int srcheight, const uint16_t *overrideData) {
503
// TODO: This should probably be converted in a shader instead..
504
fbTexBuffer_.resize(srcwidth * srcheight);
505
const uint16_t *displayBuffer = overrideData;
506
if (!displayBuffer)
507
displayBuffer = (const uint16_t *)Memory::GetPointer(displayFramebuf_);
508
509
for (int y = 0; y < srcheight; ++y) {
510
u32 *buf_line = &fbTexBuffer_[y * srcwidth];
511
const u16 *fb_line = &displayBuffer[y * displayStride_];
512
513
switch (displayFormat_) {
514
case GE_FORMAT_565:
515
ConvertRGB565ToRGBA8888(buf_line, fb_line, srcwidth);
516
break;
517
518
case GE_FORMAT_5551:
519
ConvertRGBA5551ToRGBA8888(buf_line, fb_line, srcwidth);
520
break;
521
522
case GE_FORMAT_4444:
523
ConvertRGBA4444ToRGBA8888(buf_line, fb_line, srcwidth);
524
break;
525
526
default:
527
ERROR_LOG_REPORT(Log::G3D, "Software: Unexpected framebuffer format: %d", displayFormat_);
528
break;
529
}
530
}
531
532
desc.width = srcwidth;
533
desc.height = srcheight;
534
desc.initData.push_back((uint8_t *)fbTexBuffer_.data());
535
}
536
537
// Copies RGBA8 data from RAM to the currently bound render target.
538
void SoftGPU::CopyToCurrentFboFromDisplayRam(int srcwidth, int srcheight) {
539
if (!draw_ || !presentation_)
540
return;
541
float u0 = 0.0f;
542
float u1;
543
float v0 = 0.0f;
544
float v1 = 1.0f;
545
546
if (fbTex) {
547
fbTex->Release();
548
fbTex = nullptr;
549
}
550
551
// For accuracy, try to handle 0 stride - sometimes used.
552
if (displayStride_ == 0) {
553
srcheight = 1;
554
u1 = 1.0f;
555
} else {
556
u1 = (float)srcwidth / displayStride_;
557
}
558
559
Draw::TextureDesc desc{};
560
desc.type = Draw::TextureType::LINEAR2D;
561
desc.format = Draw::DataFormat::R8G8B8A8_UNORM;
562
desc.depth = 1;
563
desc.mipLevels = 1;
564
desc.tag = "SoftGPU";
565
bool hasImage = true;
566
567
OutputFlags outputFlags = g_Config.iDisplayFilter == SCALE_NEAREST ? OutputFlags::NEAREST : OutputFlags::LINEAR;
568
bool hasPostShader = presentation_ && presentation_->HasPostShader();
569
570
if (PSP_CoreParameter().compat.flags().DarkStalkersPresentHack && displayFormat_ == GE_FORMAT_5551 && g_DarkStalkerStretch != DSStretch::Off) {
571
const u8 *data = Memory::GetPointerWrite(0x04088000);
572
bool fillDesc = true;
573
if (draw_->GetDataFormatSupport(Draw::DataFormat::A1B5G5R5_UNORM_PACK16) & Draw::FMT_TEXTURE) {
574
// The perfect one.
575
desc.format = Draw::DataFormat::A1B5G5R5_UNORM_PACK16;
576
} else if (!hasPostShader && (draw_->GetDataFormatSupport(Draw::DataFormat::A1R5G5B5_UNORM_PACK16) & Draw::FMT_TEXTURE)) {
577
// RB swapped, compensate with a shader.
578
desc.format = Draw::DataFormat::A1R5G5B5_UNORM_PACK16;
579
outputFlags |= OutputFlags::RB_SWIZZLE;
580
} else {
581
ConvertTextureDescFrom16(desc, srcwidth, srcheight, (const uint16_t *)data);
582
fillDesc = false;
583
}
584
if (fillDesc) {
585
desc.width = displayStride_ == 0 ? srcwidth : displayStride_;
586
desc.height = srcheight;
587
desc.initData.push_back(data);
588
}
589
u0 = 64.5f / (float)desc.width;
590
u1 = 447.5f / (float)desc.width;
591
v0 = 16.0f / (float)desc.height;
592
v1 = 240.0f / (float)desc.height;
593
if (g_DarkStalkerStretch == DSStretch::Normal) {
594
outputFlags |= OutputFlags::PILLARBOX;
595
}
596
} else if (!Memory::IsValidAddress(displayFramebuf_) || srcwidth == 0 || srcheight == 0) {
597
hasImage = false;
598
u1 = 1.0f;
599
} else if (displayFormat_ == GE_FORMAT_8888) {
600
const u8 *data = Memory::GetPointer(displayFramebuf_);
601
desc.width = displayStride_ == 0 ? srcwidth : displayStride_;
602
desc.height = srcheight;
603
desc.initData.push_back(data);
604
desc.format = Draw::DataFormat::R8G8B8A8_UNORM;
605
} else if (displayFormat_ == GE_FORMAT_5551) {
606
const u8 *data = Memory::GetPointer(displayFramebuf_);
607
bool fillDesc = true;
608
if (draw_->GetDataFormatSupport(Draw::DataFormat::A1B5G5R5_UNORM_PACK16) & Draw::FMT_TEXTURE) {
609
// The perfect one.
610
desc.format = Draw::DataFormat::A1B5G5R5_UNORM_PACK16;
611
} else if (!hasPostShader && (draw_->GetDataFormatSupport(Draw::DataFormat::A1R5G5B5_UNORM_PACK16) & Draw::FMT_TEXTURE)) {
612
// RB swapped, compensate with a shader.
613
desc.format = Draw::DataFormat::A1R5G5B5_UNORM_PACK16;
614
outputFlags |= OutputFlags::RB_SWIZZLE;
615
} else {
616
ConvertTextureDescFrom16(desc, srcwidth, srcheight);
617
u1 = 1.0f;
618
fillDesc = false;
619
}
620
if (fillDesc) {
621
desc.width = displayStride_ == 0 ? srcwidth : displayStride_;
622
desc.height = srcheight;
623
desc.initData.push_back(data);
624
}
625
} else {
626
ConvertTextureDescFrom16(desc, srcwidth, srcheight);
627
u1 = 1.0f;
628
}
629
if (!hasImage) {
630
draw_->BindFramebufferAsRenderTarget(nullptr, { Draw::RPAction::CLEAR, Draw::RPAction::DONT_CARE, Draw::RPAction::DONT_CARE }, "CopyToCurrentFboFromDisplayRam");
631
return;
632
}
633
634
fbTex = draw_->CreateTexture(desc);
635
636
switch (GetGPUBackend()) {
637
case GPUBackend::OPENGL:
638
outputFlags |= OutputFlags::BACKBUFFER_FLIPPED;
639
break;
640
case GPUBackend::DIRECT3D9:
641
case GPUBackend::DIRECT3D11:
642
outputFlags |= OutputFlags::POSITION_FLIPPED;
643
break;
644
case GPUBackend::VULKAN:
645
break;
646
}
647
648
presentation_->SourceTexture(fbTex, desc.width, desc.height);
649
presentation_->CopyToOutput(outputFlags, g_Config.iInternalScreenRotation, u0, v0, u1, v1);
650
}
651
652
void SoftGPU::CopyDisplayToOutput(bool reallyDirty) {
653
drawEngine_->transformUnit.Flush("output");
654
// The display always shows 480x272.
655
CopyToCurrentFboFromDisplayRam(FB_WIDTH, FB_HEIGHT);
656
MarkDirty(displayFramebuf_, displayStride_, 272, displayFormat_, SoftGPUVRAMDirty::CLEAR);
657
}
658
659
void SoftGPU::MarkDirty(uint32_t addr, uint32_t stride, uint32_t height, GEBufferFormat fmt, SoftGPUVRAMDirty value) {
660
uint32_t bytes = height * stride * (fmt == GE_FORMAT_8888 ? 4 : 2);
661
MarkDirty(addr, bytes, value);
662
}
663
664
void SoftGPU::MarkDirty(uint32_t addr, uint32_t bytes, SoftGPUVRAMDirty value) {
665
// Only bother tracking if frameskipping.
666
if (g_Config.iFrameSkip == 0)
667
return;
668
if (!Memory::IsVRAMAddress(addr) || !Memory::IsVRAMAddress(addr + bytes - 1))
669
return;
670
if (lastDirtyAddr_ == addr && lastDirtySize_ == bytes && lastDirtyValue_ == value)
671
return;
672
673
uint32_t start = ((addr - PSP_GetVidMemBase()) & 0x001FFFFF) >> 10;
674
uint32_t end = start + ((bytes + 1023) >> 10);
675
if (end > sizeof(vramDirty_)) {
676
end = sizeof(vramDirty_);
677
}
678
if (value == SoftGPUVRAMDirty::CLEAR || value == (SoftGPUVRAMDirty::DIRTY | SoftGPUVRAMDirty::REALLY_DIRTY)) {
679
memset(vramDirty_ + start, (uint8_t)value, end - start);
680
} else {
681
for (uint32_t i = start; i < end; ++i) {
682
vramDirty_[i] |= (uint8_t)value;
683
}
684
}
685
686
lastDirtyAddr_ = addr;
687
lastDirtySize_ = bytes;
688
lastDirtyValue_ = value;
689
}
690
691
bool SoftGPU::ClearDirty(uint32_t addr, uint32_t stride, uint32_t height, GEBufferFormat fmt, SoftGPUVRAMDirty value) {
692
uint32_t bytes = height * stride * (fmt == GE_FORMAT_8888 ? 4 : 2);
693
return ClearDirty(addr, bytes, value);
694
}
695
696
bool SoftGPU::ClearDirty(uint32_t addr, uint32_t bytes, SoftGPUVRAMDirty value) {
697
if (!Memory::IsVRAMAddress(addr) || !Memory::IsVRAMAddress(addr + bytes - 1))
698
return false;
699
700
uint32_t start = ((addr - PSP_GetVidMemBase()) & 0x001FFFFF) >> 10;
701
uint32_t end = start + ((bytes + 1023) >> 10);
702
bool result = false;
703
for (uint32_t i = start; i < end; ++i) {
704
if (vramDirty_[i] & (uint8_t)value) {
705
result = true;
706
vramDirty_[i] &= ~(uint8_t)value;
707
}
708
}
709
710
lastDirtyAddr_ = 0;
711
lastDirtySize_ = 0;
712
713
return result;
714
}
715
716
void SoftGPU::NotifyRenderResized() {
717
// Force the render params to 480x272 so other things work.
718
if (g_Config.IsPortrait()) {
719
PSP_CoreParameter().renderWidth = 272;
720
PSP_CoreParameter().renderHeight = 480;
721
} else {
722
PSP_CoreParameter().renderWidth = 480;
723
PSP_CoreParameter().renderHeight = 272;
724
}
725
}
726
727
void SoftGPU::NotifyDisplayResized() {
728
displayResized_ = true;
729
}
730
731
void SoftGPU::CheckDisplayResized() {
732
if (displayResized_ && presentation_) {
733
presentation_->UpdateDisplaySize(PSP_CoreParameter().pixelWidth, PSP_CoreParameter().pixelHeight);
734
presentation_->UpdateRenderSize(PSP_CoreParameter().renderWidth, PSP_CoreParameter().renderHeight);
735
presentation_->UpdatePostShader();
736
displayResized_ = false;
737
}
738
}
739
740
void SoftGPU::CheckConfigChanged() {
741
if (configChanged_) {
742
drawEngineCommon_->NotifyConfigChanged();
743
BuildReportingInfo();
744
if (presentation_) {
745
presentation_->UpdatePostShader();
746
}
747
configChanged_ = false;
748
}
749
}
750
751
void SoftGPU::FastRunLoop(DisplayList &list) {
752
PROFILE_THIS_SCOPE("soft_runloop");
753
const auto *cmdInfo = softgpuCmdInfo;
754
int dc = downcount;
755
SoftDirty dirty = dirtyFlags_;
756
for (; dc > 0; --dc) {
757
u32 op = Memory::ReadUnchecked_U32(list.pc);
758
const u32 cmd = op >> 24;
759
const auto &info = cmdInfo[cmd];
760
const u32 diff = op ^ gstate.cmdmem[cmd];
761
if (diff == 0) {
762
if (info.flags & FLAG_EXECUTE) {
763
downcount = dc;
764
dirtyFlags_ = dirty;
765
(this->*info.func)(op, diff);
766
dirty = dirtyFlags_;
767
dc = downcount;
768
}
769
} else {
770
uint64_t flags = info.flags;
771
gstate.cmdmem[cmd] = op;
772
dirty |= SoftDirty(flags >> 8);
773
if (flags & (FLAG_EXECUTE | FLAG_EXECUTEONCHANGE)) {
774
downcount = dc;
775
dirtyFlags_ = dirty;
776
(this->*info.func)(op, diff);
777
dirty = dirtyFlags_;
778
dc = downcount;
779
}
780
}
781
list.pc += 4;
782
}
783
downcount = 0;
784
dirtyFlags_ = dirty;
785
}
786
787
bool SoftGPU::IsStarted() {
788
return drawEngine_ && drawEngine_->transformUnit.IsStarted();
789
}
790
791
void SoftGPU::ExecuteOp(u32 op, u32 diff) {
792
const u8 cmd = op >> 24;
793
const auto info = softgpuCmdInfo[cmd];
794
if (diff == 0) {
795
if (info.flags & FLAG_EXECUTE)
796
(this->*info.func)(op, diff);
797
} else {
798
dirtyFlags_ |= SoftDirty(info.flags >> 8);
799
if (info.flags & (FLAG_EXECUTE | FLAG_EXECUTEONCHANGE))
800
(this->*info.func)(op, diff);
801
}
802
}
803
804
void SoftGPU::Execute_BlockTransferStart(u32 op, u32 diff) {
805
u32 srcBasePtr = gstate.getTransferSrcAddress();
806
u32 srcStride = gstate.getTransferSrcStride();
807
808
u32 dstBasePtr = gstate.getTransferDstAddress();
809
u32 dstStride = gstate.getTransferDstStride();
810
811
int srcX = gstate.getTransferSrcX();
812
int srcY = gstate.getTransferSrcY();
813
814
int dstX = gstate.getTransferDstX();
815
int dstY = gstate.getTransferDstY();
816
817
int width = gstate.getTransferWidth();
818
int height = gstate.getTransferHeight();
819
820
int bpp = gstate.getTransferBpp();
821
822
// Use height less one to account for width, which can be greater or less than stride.
823
const uint32_t src = srcBasePtr + (srcY * srcStride + srcX) * bpp;
824
const uint32_t srcSize = (height - 1) * (srcStride + width) * bpp;
825
const uint32_t dst = dstBasePtr + (dstY * dstStride + dstX) * bpp;
826
const uint32_t dstSize = (height - 1) * (dstStride + width) * bpp;
827
828
// Need to flush both source and target, so we overwrite properly.
829
if (Memory::IsValidRange(src, srcSize) && Memory::IsValidRange(dst, dstSize)) {
830
drawEngine_->transformUnit.FlushIfOverlap("blockxfer", false, src, srcStride, width * bpp, height);
831
drawEngine_->transformUnit.FlushIfOverlap("blockxfer", true, dst, dstStride, width * bpp, height);
832
} else {
833
drawEngine_->transformUnit.Flush("blockxfer_wrap");
834
}
835
836
DoBlockTransfer(gstate_c.skipDrawReason);
837
838
// Could theoretically dirty the framebuffer.
839
MarkDirty(dst, dstSize, SoftGPUVRAMDirty::DIRTY | SoftGPUVRAMDirty::REALLY_DIRTY);
840
}
841
842
void SoftGPU::Execute_Prim(u32 op, u32 diff) {
843
u32 count = op & 0xFFFF;
844
// Upper bits are ignored.
845
GEPrimitiveType prim = static_cast<GEPrimitiveType>((op >> 16) & 7);
846
if (count == 0)
847
return;
848
FlushImm();
849
850
if (!Memory::IsValidAddress(gstate_c.vertexAddr)) {
851
ERROR_LOG_REPORT(Log::G3D, "Software: Bad vertex address %08x!", gstate_c.vertexAddr);
852
return;
853
}
854
855
const void *verts = Memory::GetPointerUnchecked(gstate_c.vertexAddr);
856
const void *indices = NULL;
857
if ((gstate.vertType & GE_VTYPE_IDX_MASK) != GE_VTYPE_IDX_NONE) {
858
if (!Memory::IsValidAddress(gstate_c.indexAddr)) {
859
ERROR_LOG_REPORT(Log::G3D, "Software: Bad index address %08x!", gstate_c.indexAddr);
860
return;
861
}
862
indices = Memory::GetPointerUnchecked(gstate_c.indexAddr);
863
}
864
865
cyclesExecuted += EstimatePerVertexCost() * count;
866
int bytesRead;
867
gstate_c.UpdateUVScaleOffset();
868
drawEngine_->transformUnit.SetDirty(dirtyFlags_);
869
drawEngine_->transformUnit.SubmitPrimitive(verts, indices, prim, count, gstate.vertType, &bytesRead, drawEngine_);
870
dirtyFlags_ = drawEngine_->transformUnit.GetDirty();
871
872
SoftGPUVRAMDirty mark = (gstate_c.skipDrawReason & SKIPDRAW_SKIPFRAME) != 0 ? SoftGPUVRAMDirty::DIRTY : SoftGPUVRAMDirty::DIRTY | SoftGPUVRAMDirty::REALLY_DIRTY;
873
MarkDirty(gstate.getFrameBufAddress(), gstate.FrameBufStride(), gstate.getRegionY2() + 1, gstate.FrameBufFormat(), mark);
874
875
// After drawing, we advance the vertexAddr (when non indexed) or indexAddr (when indexed).
876
// Some games rely on this, they don't bother reloading VADDR and IADDR.
877
// The VADDR/IADDR registers are NOT updated.
878
AdvanceVerts(gstate.vertType, count, bytesRead);
879
}
880
881
void SoftGPU::Execute_Bezier(u32 op, u32 diff) {
882
// This also make skipping drawing very effective.
883
if (gstate_c.skipDrawReason & (SKIPDRAW_SKIPFRAME | SKIPDRAW_NON_DISPLAYED_FB)) {
884
// TODO: Should this eat some cycles? Probably yes. Not sure if important.
885
return;
886
}
887
888
if (!Memory::IsValidAddress(gstate_c.vertexAddr)) {
889
ERROR_LOG_REPORT(Log::G3D, "Bad vertex address %08x!", gstate_c.vertexAddr);
890
return;
891
}
892
893
const void *control_points = Memory::GetPointerUnchecked(gstate_c.vertexAddr);
894
const void *indices = NULL;
895
if ((gstate.vertType & GE_VTYPE_IDX_MASK) != GE_VTYPE_IDX_NONE) {
896
if (!Memory::IsValidAddress(gstate_c.indexAddr)) {
897
ERROR_LOG_REPORT(Log::G3D, "Bad index address %08x!", gstate_c.indexAddr);
898
return;
899
}
900
indices = Memory::GetPointerUnchecked(gstate_c.indexAddr);
901
}
902
903
if ((gstate.vertType & GE_VTYPE_MORPHCOUNT_MASK) || vertTypeIsSkinningEnabled(gstate.vertType)) {
904
DEBUG_LOG_REPORT(Log::G3D, "Unusual bezier/spline vtype: %08x, morph: %d, bones: %d", gstate.vertType, (gstate.vertType & GE_VTYPE_MORPHCOUNT_MASK) >> GE_VTYPE_MORPHCOUNT_SHIFT, vertTypeGetNumBoneWeights(gstate.vertType));
905
}
906
907
Spline::BezierSurface surface;
908
surface.tess_u = gstate.getPatchDivisionU();
909
surface.tess_v = gstate.getPatchDivisionV();
910
surface.num_points_u = op & 0xFF;
911
surface.num_points_v = (op >> 8) & 0xFF;
912
surface.num_patches_u = (surface.num_points_u - 1) / 3;
913
surface.num_patches_v = (surface.num_points_v - 1) / 3;
914
surface.primType = gstate.getPatchPrimitiveType();
915
surface.patchFacing = gstate.patchfacing & 1;
916
917
SetDrawType(DRAW_BEZIER, PatchPrimToPrim(surface.primType));
918
919
int bytesRead = 0;
920
gstate_c.UpdateUVScaleOffset();
921
drawEngine_->transformUnit.SetDirty(dirtyFlags_);
922
drawEngineCommon_->SubmitCurve(control_points, indices, surface, gstate.vertType, &bytesRead, "bezier");
923
dirtyFlags_ = drawEngine_->transformUnit.GetDirty();
924
925
SoftGPUVRAMDirty mark = (gstate_c.skipDrawReason & SKIPDRAW_SKIPFRAME) != 0 ? SoftGPUVRAMDirty::DIRTY : SoftGPUVRAMDirty::DIRTY | SoftGPUVRAMDirty::REALLY_DIRTY;
926
MarkDirty(gstate.getFrameBufAddress(), gstate.FrameBufStride(), gstate.getRegionY2() + 1, gstate.FrameBufFormat(), mark);
927
928
// After drawing, we advance pointers - see SubmitPrim which does the same.
929
int count = surface.num_points_u * surface.num_points_v;
930
AdvanceVerts(gstate.vertType, count, bytesRead);
931
}
932
933
void SoftGPU::Execute_Spline(u32 op, u32 diff) {
934
// This also make skipping drawing very effective.
935
if (gstate_c.skipDrawReason & (SKIPDRAW_SKIPFRAME | SKIPDRAW_NON_DISPLAYED_FB)) {
936
// TODO: Should this eat some cycles? Probably yes. Not sure if important.
937
return;
938
}
939
940
if (!Memory::IsValidAddress(gstate_c.vertexAddr)) {
941
ERROR_LOG_REPORT(Log::G3D, "Bad vertex address %08x!", gstate_c.vertexAddr);
942
return;
943
}
944
945
const void *control_points = Memory::GetPointerUnchecked(gstate_c.vertexAddr);
946
const void *indices = NULL;
947
if ((gstate.vertType & GE_VTYPE_IDX_MASK) != GE_VTYPE_IDX_NONE) {
948
if (!Memory::IsValidAddress(gstate_c.indexAddr)) {
949
ERROR_LOG_REPORT(Log::G3D, "Bad index address %08x!", gstate_c.indexAddr);
950
return;
951
}
952
indices = Memory::GetPointerUnchecked(gstate_c.indexAddr);
953
}
954
955
if ((gstate.vertType & GE_VTYPE_MORPHCOUNT_MASK) || vertTypeIsSkinningEnabled(gstate.vertType)) {
956
DEBUG_LOG_REPORT(Log::G3D, "Unusual bezier/spline vtype: %08x, morph: %d, bones: %d", gstate.vertType, (gstate.vertType & GE_VTYPE_MORPHCOUNT_MASK) >> GE_VTYPE_MORPHCOUNT_SHIFT, vertTypeGetNumBoneWeights(gstate.vertType));
957
}
958
959
Spline::SplineSurface surface;
960
surface.tess_u = gstate.getPatchDivisionU();
961
surface.tess_v = gstate.getPatchDivisionV();
962
surface.type_u = (op >> 16) & 0x3;
963
surface.type_v = (op >> 18) & 0x3;
964
surface.num_points_u = op & 0xFF;
965
surface.num_points_v = (op >> 8) & 0xFF;
966
surface.num_patches_u = surface.num_points_u - 3;
967
surface.num_patches_v = surface.num_points_v - 3;
968
surface.primType = gstate.getPatchPrimitiveType();
969
surface.patchFacing = gstate.patchfacing & 1;
970
971
SetDrawType(DRAW_SPLINE, PatchPrimToPrim(surface.primType));
972
973
int bytesRead = 0;
974
gstate_c.UpdateUVScaleOffset();
975
drawEngine_->transformUnit.SetDirty(dirtyFlags_);
976
drawEngineCommon_->SubmitCurve(control_points, indices, surface, gstate.vertType, &bytesRead, "spline");
977
dirtyFlags_ = drawEngine_->transformUnit.GetDirty();
978
979
SoftGPUVRAMDirty mark = (gstate_c.skipDrawReason & SKIPDRAW_SKIPFRAME) != 0 ? SoftGPUVRAMDirty::DIRTY : SoftGPUVRAMDirty::DIRTY | SoftGPUVRAMDirty::REALLY_DIRTY;
980
MarkDirty(gstate.getFrameBufAddress(), gstate.FrameBufStride(), gstate.getRegionY2() + 1, gstate.FrameBufFormat(), mark);
981
982
// After drawing, we advance pointers - see SubmitPrim which does the same.
983
int count = surface.num_points_u * surface.num_points_v;
984
AdvanceVerts(gstate.vertType, count, bytesRead);
985
}
986
987
void SoftGPU::Execute_LoadClut(u32 op, u32 diff) {
988
u32 clutAddr = gstate.getClutAddress();
989
// Avoid the hack in getClutLoadBytes() to inaccurately allow more palette data.
990
u32 clutTotalBytes = (gstate.getClutLoadBlocks() & 0x3F) * 32;
991
if (clutTotalBytes > 1024)
992
clutTotalBytes = 1024;
993
994
// Might be copying drawing into the CLUT, so flush.
995
drawEngine_->transformUnit.FlushIfOverlap("loadclut", false, clutAddr, clutTotalBytes, clutTotalBytes, 1);
996
997
bool changed = false;
998
if (Memory::IsValidAddress(clutAddr)) {
999
u32 validSize = Memory::ValidSize(clutAddr, clutTotalBytes);
1000
changed = memcmp(clut, Memory::GetPointerUnchecked(clutAddr), validSize) != 0;
1001
if (changed)
1002
Memory::MemcpyUnchecked(clut, clutAddr, validSize);
1003
if (validSize < clutTotalBytes) {
1004
// Zero out the parts that were outside valid memory.
1005
memset((u8 *)clut + validSize, 0x00, clutTotalBytes - validSize);
1006
changed = true;
1007
}
1008
} else if (clutAddr != 0) {
1009
// Some invalid addresses trigger a crash, others fill with zero. We always fill zero.
1010
DEBUG_LOG(Log::G3D, "Software: Invalid CLUT address, filling with garbage instead of crashing");
1011
memset(clut, 0x00, clutTotalBytes);
1012
changed = true;
1013
}
1014
1015
if (changed)
1016
drawEngine_->transformUnit.NotifyClutUpdate(clut);
1017
dirtyFlags_ |= SoftDirty::SAMPLER_CLUT;
1018
}
1019
1020
void SoftGPU::Execute_FramebufPtr(u32 op, u32 diff) {
1021
// We assume fb.data won't change while we're drawing.
1022
if (diff) {
1023
drawEngine_->transformUnit.Flush("framebuf");
1024
fb.data = Memory::GetPointerWrite(gstate.getFrameBufAddress());
1025
}
1026
}
1027
1028
void SoftGPU::Execute_FramebufFormat(u32 op, u32 diff) {
1029
// We should flush, because ranges within bins may change.
1030
if (diff)
1031
drawEngine_->transformUnit.Flush("framebuf");
1032
}
1033
1034
void SoftGPU::Execute_BoundingBox(u32 op, u32 diff) {
1035
gstate_c.Dirty(DIRTY_CULL_PLANES);
1036
GPUCommon::Execute_BoundingBox(op, diff);
1037
}
1038
1039
void SoftGPU::Execute_ZbufPtr(u32 op, u32 diff) {
1040
// We assume depthbuf.data won't change while we're drawing.
1041
if (diff) {
1042
drawEngine_->transformUnit.Flush("depthbuf");
1043
// For the pointer, ignore memory mirrors. This also gives some buffer for draws that go outside.
1044
// TODO: Confirm how wrapping is handled in drawing. Adjust if we ever handle VRAM mirrors more accurately.
1045
depthbuf.data = Memory::GetPointerWrite(gstate.getDepthBufAddress() & 0x041FFFF0);
1046
}
1047
}
1048
1049
void SoftGPU::Execute_VertexType(u32 op, u32 diff) {
1050
if ((diff & GE_VTYPE_THROUGH_MASK) != 0) {
1051
// This affects a lot of things, but some don't matter if it's off - so defer to when it's back on.
1052
dirtyFlags_ |= SoftDirty::RAST_BASIC | SoftDirty::PIXEL_BASIC;
1053
if ((op & GE_VTYPE_THROUGH_MASK) == 0) {
1054
dirtyFlags_ |= SoftDirty::TRANSFORM_MATRIX | SoftDirty::TRANSFORM_VIEWPORT | SoftDirty::TRANSFORM_FOG;
1055
dirtyFlags_ |= SoftDirty::LIGHT_BASIC | SoftDirty::LIGHT_MATERIAL | SoftDirty::LIGHT_0 | SoftDirty::LIGHT_1 | SoftDirty::LIGHT_2 | SoftDirty::LIGHT_3;
1056
dirtyFlags_ |= SoftDirty::PIXEL_CACHED;
1057
}
1058
}
1059
}
1060
1061
void SoftGPU::Execute_WorldMtxNum(u32 op, u32 diff) {
1062
// Setting 0xFFFFF0 will reset to 0.
1063
gstate.worldmtxnum = (GE_CMD_WORLDMATRIXNUMBER << 24) | (op & 0xF);
1064
}
1065
1066
void SoftGPU::Execute_ViewMtxNum(u32 op, u32 diff) {
1067
gstate.viewmtxnum = (GE_CMD_VIEWMATRIXNUMBER << 24) | (op & 0xF);
1068
}
1069
1070
void SoftGPU::Execute_ProjMtxNum(u32 op, u32 diff) {
1071
gstate.projmtxnum = (GE_CMD_PROJMATRIXNUMBER << 24) | (op & 0xF);
1072
}
1073
1074
void SoftGPU::Execute_TgenMtxNum(u32 op, u32 diff) {
1075
gstate.texmtxnum = (GE_CMD_TGENMATRIXNUMBER << 24) | (op & 0xF);
1076
}
1077
1078
void SoftGPU::Execute_BoneMtxNum(u32 op, u32 diff) {
1079
// Setting any bits outside 0x7F are ignored and resets the internal counter.
1080
gstate.boneMatrixNumber = (GE_CMD_BONEMATRIXNUMBER << 24) | (op & 0x7F);
1081
}
1082
1083
void SoftGPU::Execute_WorldMtxData(u32 op, u32 diff) {
1084
int num = gstate.worldmtxnum & 0x00FFFFFF;
1085
if (num < 12) {
1086
u32 *target = (u32 *)&gstate.worldMatrix[num];
1087
u32 newVal = op << 8;
1088
if (newVal != *target) {
1089
*target = newVal;
1090
dirtyFlags_ |= SoftDirty::TRANSFORM_MATRIX;
1091
gstate_c.Dirty(DIRTY_CULL_PLANES);
1092
}
1093
}
1094
1095
// Also update the CPU visible values, which update differently.
1096
u32 *target = &matrixVisible.all[12 * 8 + (num & 0xF)];
1097
*target = op & 0x00FFFFFF;
1098
1099
num++;
1100
gstate.worldmtxnum = (GE_CMD_WORLDMATRIXNUMBER << 24) | (num & 0x00FFFFFF);
1101
gstate.worldmtxdata = GE_CMD_WORLDMATRIXDATA << 24;
1102
}
1103
1104
void SoftGPU::Execute_ViewMtxData(u32 op, u32 diff) {
1105
int num = gstate.viewmtxnum & 0x00FFFFFF;
1106
if (num < 12) {
1107
u32 *target = (u32 *)&gstate.viewMatrix[num];
1108
u32 newVal = op << 8;
1109
if (newVal != *target) {
1110
*target = newVal;
1111
dirtyFlags_ |= SoftDirty::TRANSFORM_MATRIX;
1112
gstate_c.Dirty(DIRTY_CULL_PLANES);
1113
}
1114
}
1115
1116
// Also update the CPU visible values, which update differently.
1117
u32 *target = &matrixVisible.all[12 * 8 + 12 + (num & 0xF)];
1118
*target = op & 0x00FFFFFF;
1119
1120
num++;
1121
gstate.viewmtxnum = (GE_CMD_VIEWMATRIXNUMBER << 24) | (num & 0x00FFFFFF);
1122
gstate.viewmtxdata = GE_CMD_VIEWMATRIXDATA << 24;
1123
}
1124
1125
void SoftGPU::Execute_ProjMtxData(u32 op, u32 diff) {
1126
int num = gstate.projmtxnum & 0x00FFFFFF;
1127
if (num < 16) {
1128
u32 *target = (u32 *)&gstate.projMatrix[num];
1129
u32 newVal = op << 8;
1130
if (newVal != *target) {
1131
*target = newVal;
1132
dirtyFlags_ |= SoftDirty::TRANSFORM_MATRIX;
1133
gstate_c.Dirty(DIRTY_CULL_PLANES);
1134
}
1135
}
1136
1137
// Also update the CPU visible values, which update differently.
1138
u32 *target = &matrixVisible.all[12 * 8 + 12 + 12 + (num & 0xF)];
1139
*target = op & 0x00FFFFFF;
1140
1141
num++;
1142
gstate.projmtxnum = (GE_CMD_PROJMATRIXNUMBER << 24) | (num & 0x00FFFFFF);
1143
gstate.projmtxdata = GE_CMD_PROJMATRIXDATA << 24;
1144
}
1145
1146
void SoftGPU::Execute_TgenMtxData(u32 op, u32 diff) {
1147
int num = gstate.texmtxnum & 0x00FFFFFF;
1148
if (num < 12) {
1149
u32 *target = (u32 *)&gstate.tgenMatrix[num];
1150
u32 newVal = op << 8;
1151
if (newVal != *target) {
1152
*target = newVal;
1153
// This is mainly used in vertex read, but also affects if we enable texture projection.
1154
dirtyFlags_ |= SoftDirty::RAST_TEX;
1155
}
1156
}
1157
1158
// Doesn't wrap to any other matrix.
1159
if ((num & 0xF) < 12) {
1160
matrixVisible.tgen[num & 0xF] = op & 0x00FFFFFF;
1161
}
1162
1163
num++;
1164
gstate.texmtxnum = (GE_CMD_TGENMATRIXNUMBER << 24) | (num & 0x00FFFFFF);
1165
gstate.texmtxdata = GE_CMD_TGENMATRIXDATA << 24;
1166
}
1167
1168
void SoftGPU::Execute_BoneMtxData(u32 op, u32 diff) {
1169
int num = gstate.boneMatrixNumber & 0x00FFFFFF;
1170
1171
if (num < 96) {
1172
u32 *target = (u32 *)&gstate.boneMatrix[num];
1173
u32 newVal = op << 8;
1174
// No dirtying, we read bone data during vertex read.
1175
*target = newVal;
1176
}
1177
1178
// Also update the CPU visible values, which update differently.
1179
u32 *target = &matrixVisible.all[(num & 0x7F)];
1180
*target = op & 0x00FFFFFF;
1181
1182
num++;
1183
gstate.boneMatrixNumber = (GE_CMD_BONEMATRIXNUMBER << 24) | (num & 0x00FFFFFF);
1184
gstate.boneMatrixData = GE_CMD_BONEMATRIXDATA << 24;
1185
}
1186
1187
static void CopyMatrix24(u32_le *result, const u32 *mtx, u32 count, u32 cmdbits) {
1188
for (u32 i = 0; i < count; ++i) {
1189
result[i] = mtx[i] | cmdbits;
1190
}
1191
}
1192
1193
bool SoftGPU::GetMatrix24(GEMatrixType type, u32_le *result, u32 cmdbits) {
1194
switch (type) {
1195
case GE_MTX_BONE0:
1196
case GE_MTX_BONE1:
1197
case GE_MTX_BONE2:
1198
case GE_MTX_BONE3:
1199
case GE_MTX_BONE4:
1200
case GE_MTX_BONE5:
1201
case GE_MTX_BONE6:
1202
case GE_MTX_BONE7:
1203
CopyMatrix24(result, matrixVisible.bone + (type - GE_MTX_BONE0) * 12, 12, cmdbits);
1204
break;
1205
case GE_MTX_TEXGEN:
1206
CopyMatrix24(result, matrixVisible.tgen, 12, cmdbits);
1207
break;
1208
case GE_MTX_WORLD:
1209
CopyMatrix24(result, matrixVisible.world, 12, cmdbits);
1210
break;
1211
case GE_MTX_VIEW:
1212
CopyMatrix24(result, matrixVisible.view, 12, cmdbits);
1213
break;
1214
case GE_MTX_PROJECTION:
1215
CopyMatrix24(result, matrixVisible.proj, 16, cmdbits);
1216
break;
1217
default:
1218
return false;
1219
}
1220
return true;
1221
}
1222
1223
void SoftGPU::ResetMatrices() {
1224
GPUCommon::ResetMatrices();
1225
dirtyFlags_ |= SoftDirty::TRANSFORM_MATRIX | SoftDirty::RAST_TEX;
1226
}
1227
1228
void SoftGPU::Execute_ImmVertexAlphaPrim(u32 op, u32 diff) {
1229
GPUCommon::Execute_ImmVertexAlphaPrim(op, diff);
1230
// We won't flush as often as hardware renderers, so we want to flush right away.
1231
FlushImm();
1232
}
1233
1234
void SoftGPU::Execute_Call(u32 op, u32 diff) {
1235
PROFILE_THIS_SCOPE("gpu_call");
1236
1237
const u32 target = gstate_c.getRelativeAddress(op & 0x00FFFFFC);
1238
if (!Memory::IsValidAddress(target)) {
1239
ERROR_LOG(Log::G3D, "CALL to illegal address %08x - ignoring! data=%06x", target, op & 0x00FFFFFF);
1240
if (g_Config.bIgnoreBadMemAccess) {
1241
return;
1242
}
1243
gpuState = GPUSTATE_ERROR;
1244
downcount = 0;
1245
return;
1246
}
1247
1248
const u32 retval = currentList->pc + 4;
1249
if (currentList->stackptr == ARRAY_SIZE(currentList->stack)) {
1250
ERROR_LOG(Log::G3D, "CALL: Stack full!");
1251
} else {
1252
auto &stackEntry = currentList->stack[currentList->stackptr++];
1253
stackEntry.pc = retval;
1254
stackEntry.offsetAddr = gstate_c.offsetAddr;
1255
// The base address is NOT saved/restored for a regular call.
1256
UpdatePC(currentList->pc, target - 4);
1257
currentList->pc = target - 4; // pc will be increased after we return, counteract that
1258
}
1259
}
1260
1261
void SoftGPU::FinishDeferred() {
1262
// Need to flush before going back to CPU, so drawing is appropriately visible.
1263
drawEngine_->transformUnit.Flush("finish");
1264
}
1265
1266
int SoftGPU::ListSync(int listid, int mode) {
1267
// Take this as a cue that we need to finish drawing.
1268
drawEngine_->transformUnit.Flush("listsync");
1269
return GPUCommon::ListSync(listid, mode);
1270
}
1271
1272
u32 SoftGPU::DrawSync(int mode) {
1273
// Take this as a cue that we need to finish drawing.
1274
drawEngine_->transformUnit.Flush("drawsync");
1275
return GPUCommon::DrawSync(mode);
1276
}
1277
1278
void SoftGPU::GetStats(char *buffer, size_t bufsize) {
1279
drawEngine_->transformUnit.GetStats(buffer, bufsize);
1280
}
1281
1282
void SoftGPU::InvalidateCache(u32 addr, int size, GPUInvalidationType type)
1283
{
1284
// Nothing to invalidate.
1285
}
1286
1287
void SoftGPU::PerformWriteFormattedFromMemory(u32 addr, int size, int width, GEBufferFormat format)
1288
{
1289
// Ignore.
1290
}
1291
1292
bool SoftGPU::PerformMemoryCopy(u32 dest, u32 src, int size, GPUCopyFlag flags) {
1293
// Nothing to update.
1294
InvalidateCache(dest, size, GPU_INVALIDATE_HINT);
1295
if (!(flags & GPUCopyFlag::DEBUG_NOTIFIED))
1296
GPURecord::NotifyMemcpy(dest, src, size);
1297
// Let's just be safe.
1298
MarkDirty(dest, size, SoftGPUVRAMDirty::DIRTY | SoftGPUVRAMDirty::REALLY_DIRTY);
1299
return false;
1300
}
1301
1302
bool SoftGPU::PerformMemorySet(u32 dest, u8 v, int size)
1303
{
1304
// Nothing to update.
1305
InvalidateCache(dest, size, GPU_INVALIDATE_HINT);
1306
GPURecord::NotifyMemset(dest, v, size);
1307
// Let's just be safe.
1308
MarkDirty(dest, size, SoftGPUVRAMDirty::DIRTY | SoftGPUVRAMDirty::REALLY_DIRTY);
1309
return false;
1310
}
1311
1312
bool SoftGPU::PerformReadbackToMemory(u32 dest, int size)
1313
{
1314
// Nothing to update.
1315
InvalidateCache(dest, size, GPU_INVALIDATE_HINT);
1316
return false;
1317
}
1318
1319
bool SoftGPU::PerformWriteColorFromMemory(u32 dest, int size)
1320
{
1321
// Nothing to update.
1322
InvalidateCache(dest, size, GPU_INVALIDATE_HINT);
1323
GPURecord::NotifyUpload(dest, size);
1324
return false;
1325
}
1326
1327
bool SoftGPU::PerformWriteStencilFromMemory(u32 dest, int size, WriteStencil flags)
1328
{
1329
return false;
1330
}
1331
1332
bool SoftGPU::FramebufferDirty() {
1333
if (g_Config.iFrameSkip != 0) {
1334
return ClearDirty(displayFramebuf_, displayStride_, 272, displayFormat_, SoftGPUVRAMDirty::DIRTY);
1335
}
1336
return true;
1337
}
1338
1339
bool SoftGPU::FramebufferReallyDirty() {
1340
if (g_Config.iFrameSkip != 0) {
1341
return ClearDirty(displayFramebuf_, displayStride_, 272, displayFormat_, SoftGPUVRAMDirty::REALLY_DIRTY);
1342
}
1343
return true;
1344
}
1345
1346
static DrawingCoords GetTargetSize(int stride) {
1347
int w = std::min(stride, std::max(gstate.getRegionX2(), gstate.getScissorX2()) + 1);
1348
int h = std::max(gstate.getRegionY2(), gstate.getScissorY2()) + 1;
1349
if (gstate.getRegionX2() == 1023 && gstate.getRegionY2() == 1023) {
1350
// Some games max out region, but always scissor to an appropriate size.
1351
// Both values always scissor, we just prefer region as it's usually a more stable size.
1352
w = std::max(stride, gstate.getScissorX2() + 1);
1353
h = std::max(272, gstate.getScissorY2() + 1);
1354
}
1355
1356
return DrawingCoords((s16)w, (s16)h);
1357
}
1358
1359
bool SoftGPU::GetCurrentFramebuffer(GPUDebugBuffer &buffer, GPUDebugFramebufferType type, int maxRes) {
1360
int stride = gstate.FrameBufStride();
1361
DrawingCoords size = GetTargetSize(stride);
1362
GEBufferFormat fmt = gstate.FrameBufFormat();
1363
const u8 *src = fb.data;
1364
1365
if (!Memory::IsValidAddress(displayFramebuf_))
1366
return false;
1367
1368
if (type == GPU_DBG_FRAMEBUF_DISPLAY) {
1369
size.x = 480;
1370
size.y = 272;
1371
stride = displayStride_;
1372
fmt = displayFormat_;
1373
src = Memory::GetPointer(displayFramebuf_);
1374
}
1375
1376
buffer.Allocate(size.x, size.y, fmt);
1377
1378
const int depth = fmt == GE_FORMAT_8888 ? 4 : 2;
1379
u8 *dst = buffer.GetData();
1380
const int byteWidth = size.x * depth;
1381
for (int16_t y = 0; y < size.y; ++y) {
1382
memcpy(dst, src, byteWidth);
1383
dst += byteWidth;
1384
src += stride * depth;
1385
}
1386
return true;
1387
}
1388
1389
bool SoftGPU::GetOutputFramebuffer(GPUDebugBuffer &buffer) {
1390
return GetCurrentFramebuffer(buffer, GPU_DBG_FRAMEBUF_DISPLAY, 1);
1391
}
1392
1393
bool SoftGPU::GetCurrentDepthbuffer(GPUDebugBuffer &buffer) {
1394
DrawingCoords size = GetTargetSize(gstate.DepthBufStride());
1395
buffer.Allocate(size.x, size.y, GPU_DBG_FORMAT_16BIT);
1396
1397
const int depth = 2;
1398
const u8 *src = depthbuf.data;
1399
u8 *dst = buffer.GetData();
1400
for (int16_t y = 0; y < size.y; ++y) {
1401
memcpy(dst, src, size.x * depth);
1402
dst += size.x * depth;
1403
src += gstate.DepthBufStride() * depth;
1404
}
1405
return true;
1406
}
1407
1408
static inline u8 GetPixelStencil(GEBufferFormat fmt, int fbStride, int x, int y) {
1409
if (fmt == GE_FORMAT_565) {
1410
// Always treated as 0 for comparison purposes.
1411
return 0;
1412
} else if (fmt == GE_FORMAT_5551) {
1413
return ((fb.Get16(x, y, fbStride) & 0x8000) != 0) ? 0xFF : 0;
1414
} else if (fmt == GE_FORMAT_4444) {
1415
return Convert4To8(fb.Get16(x, y, fbStride) >> 12);
1416
} else {
1417
return fb.Get32(x, y, fbStride) >> 24;
1418
}
1419
}
1420
1421
bool SoftGPU::GetCurrentStencilbuffer(GPUDebugBuffer &buffer) {
1422
DrawingCoords size = GetTargetSize(gstate.FrameBufStride());
1423
buffer.Allocate(size.x, size.y, GPU_DBG_FORMAT_8BIT);
1424
1425
u8 *row = buffer.GetData();
1426
for (int16_t y = 0; y < size.y; ++y) {
1427
for (int16_t x = 0; x < size.x; ++x) {
1428
row[x] = GetPixelStencil(gstate.FrameBufFormat(), gstate.FrameBufStride(), x, y);
1429
}
1430
row += size.x;
1431
}
1432
return true;
1433
}
1434
1435
bool SoftGPU::GetCurrentTexture(GPUDebugBuffer &buffer, int level, bool *isFramebuffer) {
1436
*isFramebuffer = false;
1437
return Rasterizer::GetCurrentTexture(buffer, level);
1438
}
1439
1440
bool SoftGPU::GetCurrentClut(GPUDebugBuffer &buffer)
1441
{
1442
const u32 bpp = gstate.getClutPaletteFormat() == GE_CMODE_32BIT_ABGR8888 ? 4 : 2;
1443
const u32 pixels = 1024 / bpp;
1444
1445
buffer.Allocate(pixels, 1, (GEBufferFormat)gstate.getClutPaletteFormat());
1446
memcpy(buffer.GetData(), clut, 1024);
1447
return true;
1448
}
1449
1450
bool SoftGPU::GetCurrentSimpleVertices(int count, std::vector<GPUDebugVertex> &vertices, std::vector<u16> &indices) {
1451
gstate_c.UpdateUVScaleOffset();
1452
return drawEngine_->transformUnit.GetCurrentSimpleVertices(count, vertices, indices);
1453
}
1454
1455
bool SoftGPU::DescribeCodePtr(const u8 *ptr, std::string &name) {
1456
std::string subname;
1457
if (Sampler::DescribeCodePtr(ptr, subname)) {
1458
name = "SamplerJit:" + subname;
1459
return true;
1460
}
1461
if (Rasterizer::DescribeCodePtr(ptr, subname)) {
1462
name = "RasterizerJit:" + subname;
1463
return true;
1464
}
1465
return GPUCommon::DescribeCodePtr(ptr, name);
1466
}
1467
1468