-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
GSDrawingContext.h
315 lines (277 loc) · 7.62 KB
/
GSDrawingContext.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
* Copyright (C) 2007-2009 Gabest
* http://www.gabest.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU Make; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA USA.
* http://www.gnu.org/copyleft/gpl.html
*
*/
#pragma once
// Ignore format for this file, as it spams a lot of warnings about uint64 and %llu.
#pragma clang diagnostic ignored "-Wformat"
#include "GS.h"
#include "GSLocalMemory.h"
class alignas(32) GSDrawingContext
{
public:
GIFRegXYOFFSET XYOFFSET;
GIFRegTEX0 TEX0;
GIFRegTEX1 TEX1;
GIFRegTEX2 TEX2;
GIFRegCLAMP CLAMP;
GIFRegMIPTBP1 MIPTBP1;
GIFRegMIPTBP2 MIPTBP2;
GIFRegSCISSOR SCISSOR;
GIFRegALPHA ALPHA;
GIFRegTEST TEST;
GIFRegFBA FBA;
GIFRegFRAME FRAME;
GIFRegZBUF ZBUF;
struct
{
GSVector4 in;
GSVector4i ex;
GSVector4 ofex;
GSVector4i ofxy;
} scissor;
struct
{
GSOffset* fb;
GSOffset* zb;
GSOffset* tex;
GSPixelOffset* fzb;
GSPixelOffset4* fzb4;
} offset;
struct
{
GIFRegXYOFFSET XYOFFSET;
GIFRegTEX0 TEX0;
GIFRegTEX1 TEX1;
GIFRegTEX2 TEX2;
GIFRegCLAMP CLAMP;
GIFRegMIPTBP1 MIPTBP1;
GIFRegMIPTBP2 MIPTBP2;
GIFRegSCISSOR SCISSOR;
GIFRegALPHA ALPHA;
GIFRegTEST TEST;
GIFRegFBA FBA;
GIFRegFRAME FRAME;
GIFRegZBUF ZBUF;
} stack;
bool m_fixed_tex0;
GSDrawingContext()
{
m_fixed_tex0 = false;
memset(&offset, 0, sizeof(offset));
Reset();
}
void Reset()
{
memset(&XYOFFSET, 0, sizeof(XYOFFSET));
memset(&TEX0, 0, sizeof(TEX0));
memset(&TEX1, 0, sizeof(TEX1));
memset(&TEX2, 0, sizeof(TEX2));
memset(&CLAMP, 0, sizeof(CLAMP));
memset(&MIPTBP1, 0, sizeof(MIPTBP1));
memset(&MIPTBP2, 0, sizeof(MIPTBP2));
memset(&SCISSOR, 0, sizeof(SCISSOR));
memset(&ALPHA, 0, sizeof(ALPHA));
memset(&TEST, 0, sizeof(TEST));
memset(&FBA, 0, sizeof(FBA));
memset(&FRAME, 0, sizeof(FRAME));
memset(&ZBUF, 0, sizeof(ZBUF));
}
void UpdateScissor()
{
ASSERT(XYOFFSET.OFX <= 0xf800 && XYOFFSET.OFY <= 0xf800);
scissor.ex.u16[0] = (uint16)((SCISSOR.SCAX0 << 4) + XYOFFSET.OFX - 0x8000);
scissor.ex.u16[1] = (uint16)((SCISSOR.SCAY0 << 4) + XYOFFSET.OFY - 0x8000);
scissor.ex.u16[2] = (uint16)((SCISSOR.SCAX1 << 4) + XYOFFSET.OFX - 0x8000);
scissor.ex.u16[3] = (uint16)((SCISSOR.SCAY1 << 4) + XYOFFSET.OFY - 0x8000);
scissor.ofex = GSVector4(
(int)((SCISSOR.SCAX0 << 4) + XYOFFSET.OFX),
(int)((SCISSOR.SCAY0 << 4) + XYOFFSET.OFY),
(int)((SCISSOR.SCAX1 << 4) + XYOFFSET.OFX),
(int)((SCISSOR.SCAY1 << 4) + XYOFFSET.OFY));
scissor.in = GSVector4(
(int)SCISSOR.SCAX0,
(int)SCISSOR.SCAY0,
(int)SCISSOR.SCAX1 + 1,
(int)SCISSOR.SCAY1 + 1);
scissor.ofxy = GSVector4i(
0x8000,
0x8000,
(int)XYOFFSET.OFX - 15,
(int)XYOFFSET.OFY - 15);
}
bool DepthRead() const
{
return TEST.ZTE && TEST.ZTST >= 2;
}
bool DepthWrite() const
{
if(TEST.ATE && TEST.ATST == ATST_NEVER && TEST.AFAIL != AFAIL_ZB_ONLY) // alpha test, all pixels fail, z buffer is not updated
{
return false;
}
return ZBUF.ZMSK == 0 && TEST.ZTE != 0; // ZTE == 0 is bug on the real hardware, write is blocked then
}
GIFRegTEX0 GetSizeFixedTEX0(int s_n, const GSVector4& st, bool linear, bool mipmap = false);
void ComputeFixedTEX0(const GSVector4& st);
bool HasFixedTEX0() const { return m_fixed_tex0;}
// Save & Restore before/after draw allow to correct/optimize current register for current draw
// Note: we could avoid the restore part if all renderer code is updated to use a local copy instead
void SaveReg()
{
stack.XYOFFSET = XYOFFSET;
stack.TEX0 = TEX0;
stack.TEX1 = TEX1;
stack.TEX2 = TEX2;
stack.CLAMP = CLAMP;
stack.MIPTBP1 = MIPTBP1;
stack.MIPTBP2 = MIPTBP2;
stack.SCISSOR = SCISSOR;
stack.ALPHA = ALPHA;
stack.TEST = TEST;
stack.FBA = FBA;
stack.FRAME = FRAME;
stack.ZBUF = ZBUF;
// This function is called before the draw so take opportunity to reset m_fixed_tex0
m_fixed_tex0 = false;
}
void RestoreReg()
{
XYOFFSET = stack.XYOFFSET;
TEX0 = stack.TEX0;
TEX1 = stack.TEX1;
TEX2 = stack.TEX2;
CLAMP = stack.CLAMP;
MIPTBP1 = stack.MIPTBP1;
MIPTBP2 = stack.MIPTBP2;
SCISSOR = stack.SCISSOR;
ALPHA = stack.ALPHA;
TEST = stack.TEST;
FBA = stack.FBA;
FRAME = stack.FRAME;
ZBUF = stack.ZBUF;
}
void Dump(const std::string& filename)
{
// Append on purpose so env + context are merged into a single file
FILE* fp = fopen(filename.c_str(), "at");
if (!fp) return;
fprintf(fp, "XYOFFSET\n"
"\tX:%u\n"
"\tY:%u\n\n"
, XYOFFSET.OFX, XYOFFSET.OFY);
fprintf(fp, "MIPTBP1\n"
"\tBP1:0x%llx\n"
"\tBW1:%llu\n"
"\tBP2:0x%llx\n"
"\tBW2:%llu\n"
"\tBP3:0x%llx\n"
"\tBW3:%llu\n\n"
, MIPTBP1.TBP1, MIPTBP1.TBW1, MIPTBP1.TBP2, MIPTBP1.TBW2, MIPTBP1.TBP3, MIPTBP1.TBW3);
fprintf(fp, "MIPTBP2\n"
"\tBP4:0x%llx\n"
"\tBW4:%llu\n"
"\tBP5:0x%llx\n"
"\tBW5:%llu\n"
"\tBP6:0x%llx\n"
"\tBW6:%llu\n\n"
, MIPTBP2.TBP4, MIPTBP2.TBW4, MIPTBP2.TBP5, MIPTBP2.TBW5, MIPTBP2.TBP6, MIPTBP2.TBW6);
fprintf(fp, "TEX0\n"
"\tTBP0:0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FIX);
const char *col[3] = {"Cs", "Cd", "0"};
const char *alpha[3] = {"As", "Ad", "Af"};
fprintf(fp, "\t=> (%s - %s) * %s + %s\n\n", col[ALPHA.A], col[ALPHA.B], alpha[ALPHA.C], col[ALPHA.D]);
fprintf(fp, "TEST\n"
"\tATE:%u\n"
"\tATST:%u\n"
"\tAREF:%u\n"
"\tAFAIL:%u\n"
"\tDATE:%u\n"
"\tDATM:%u\n"
"\tZTE:%u\n"
"\tZTST:%u\n\n"
, TEST.ATE, TEST.ATST, TEST.AREF, TEST.AFAIL, TEST.DATE, TEST.DATM, TEST.ZTE, TEST.ZTST);
fprintf(fp, "FBA\n"
"\tFBA:%u\n\n"
, FBA.FBA);
fprintf(fp, "FRAME\n"
"\tFBP (*32):0x%x\n"
"\tFBW:%u\n"
"\tPSM:0x%x\n"
"\tFBMSK:0x%x\n\n"
, FRAME.FBP*32, FRAME.FBW, FRAME.PSM, FRAME.FBMSK);
fprintf(fp, "ZBUF\n"
"\tZBP (*32):0x%x\n"
"\tPSM:0x%x\n"
"\tZMSK:%u\n\n"
, ZBUF.ZBP*32, ZBUF.PSM, ZBUF.ZMSK);
fclose(fp);
}
};