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