-
Notifications
You must be signed in to change notification settings - Fork 5
/
GPUDrawScanline.cpp
495 lines (394 loc) · 10.7 KB
/
GPUDrawScanline.cpp
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/*
* 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
*
*/
#include "stdafx.h"
#include "GPUDrawScanline.h"
GPUDrawScanline::GPUDrawScanline()
: m_sp_map("GPUSetupPrim", &m_local)
, m_ds_map("GPUDrawScanline", &m_local)
{
memset(&m_local, 0, sizeof(m_local));
m_local.gd = &m_global;
}
GPUDrawScanline::~GPUDrawScanline()
{
}
void GPUDrawScanline::BeginDraw(const GSRasterizerData* data)
{
memcpy(&m_global, &((const SharedData*)data)->global, sizeof(m_global));
if(m_global.sel.tme && m_global.sel.twin)
{
uint32 u, v;
u = ~(m_global.twin.x << 3) & 0xff; // TWW
v = ~(m_global.twin.y << 3) & 0xff; // TWH
m_local.twin[0].u = GSVector4i((u << 16) | u);
m_local.twin[0].v = GSVector4i((v << 16) | v);
u = m_global.twin.z << 3; // TWX
v = m_global.twin.w << 3; // TWY
m_local.twin[1].u = GSVector4i((u << 16) | u) & ~m_local.twin[0].u;
m_local.twin[1].v = GSVector4i((v << 16) | v) & ~m_local.twin[0].v;
}
m_ds = m_ds_map[m_global.sel];
m_de = NULL;
m_dr = NULL; // TODO
// doesn't need all bits => less functions generated
GPUScanlineSelector sel;
sel.key = 0;
sel.iip = m_global.sel.iip;
sel.tfx = m_global.sel.tfx;
sel.twin = m_global.sel.twin;
sel.sprite = m_global.sel.sprite;
m_sp = m_sp_map[sel];
}
void GPUDrawScanline::EndDraw(uint64 frame, uint64 ticks, int actual, int total)
{
m_ds_map.UpdateStats(frame, ticks, actual, total);
}
#ifndef ENABLE_JIT_RASTERIZER
void GPUDrawScanline::SetupPrim(const GSVertexSW* vertex, const uint32* index, const GSVertexSW& dscan)
{
GPUScanlineSelector sel = m_global.sel;
const GSVector4* shift = GPUSetupPrimCodeGenerator::m_shift;
if(sel.tme && !sel.twin)
{
if(sel.sprite)
{
GSVector4i t = (GSVector4i(vertex[index[1]].t) >> 8) - GSVector4i::x00000001();
t = t.ps32(t);
t = t.upl16(t);
m_local.twin[2].u = t.xxxx();
m_local.twin[2].v = t.yyyy();
}
else
{
// TODO: not really needed
m_local.twin[2].u = GSVector4i::x00ff();
m_local.twin[2].v = GSVector4i::x00ff();
}
}
if(sel.tme || sel.iip && sel.tfx != 3)
{
GSVector4 dt = dscan.t;
GSVector4 dc = dscan.c;
GSVector4i dtc8 = GSVector4i(dt * shift[0]).ps32(GSVector4i(dc * shift[0]));
if(sel.tme)
{
m_local.d8.st = dtc8.upl16(dtc8);
}
if(sel.iip && sel.tfx != 3)
{
m_local.d8.c = dtc8.uph16(dtc8);
}
if(sel.tme)
{
GSVector4 dtx = dt.xxxx();
GSVector4 dty = dt.yyyy();
m_local.d.s = GSVector4i(dtx * shift[1]).ps32(GSVector4i(dtx * shift[2]));
m_local.d.t = GSVector4i(dty * shift[1]).ps32(GSVector4i(dty * shift[2]));
}
if(sel.iip && sel.tfx != 3)
{
GSVector4 dcx = dc.xxxx();
GSVector4 dcy = dc.yyyy();
GSVector4 dcz = dc.zzzz();
m_local.d.r = GSVector4i(dcx * shift[1]).ps32(GSVector4i(dcx * shift[2]));
m_local.d.g = GSVector4i(dcy * shift[1]).ps32(GSVector4i(dcy * shift[2]));
m_local.d.b = GSVector4i(dcz * shift[1]).ps32(GSVector4i(dcz * shift[2]));
}
}
}
void GPUDrawScanline::DrawScanline(int pixels, int left, int top, const GSVertexSW& scan)
{
// TODO: not tested yet, probably bogus
GPUScanlineSelector sel = m_global.sel;
GSVector4i s, t;
GSVector4i uf, vf;
GSVector4i rf, gf, bf;
GSVector4i dither;
// Init
uint16* fb = (uint16*)m_global.vm + (top << (10 + sel.scalex)) + left;
int steps = pixels - 8;
if(sel.dtd)
{
dither = GSVector4i::load<false>(&GPUDrawScanlineCodeGenerator::m_dither[top & 3][left & 3]);
}
if(sel.tme)
{
GSVector4i vt = GSVector4i(scan.t).xxzzl();
s = vt.xxxx().add16(m_local.d.s);
t = vt.yyyy();
if(!sel.sprite)
{
t = t.add16(m_local.d.t);
}
else
{
if(sel.ltf)
{
vf = t.sll16(1).srl16(1);
}
}
}
if(sel.tfx != 3)
{
GSVector4i vc = GSVector4i(scan.c).xxzzlh();
rf = vc.xxxx();
gf = vc.yyyy();
bf = vc.zzzz();
if(sel.iip)
{
rf = rf.add16(m_local.d.r);
gf = gf.add16(m_local.d.g);
bf = bf.add16(m_local.d.b);
}
}
while(1)
{
do
{
GSVector4i test = GPUDrawScanlineCodeGenerator::m_test[7 + (steps & (steps >> 31))];
GSVector4i fd = GSVector4i::load(fb, fb + 8);
GSVector4i r, g, b, a;
// TestMask
if(sel.me)
{
test |= fd.sra16(15);
if(test.alltrue()) continue;
}
// SampleTexture
if(sel.tme)
{
GSVector4i u0, v0, u1, v1;
GSVector4i addr00, addr01, addr10, addr11;
GSVector4i c00, c01, c10, c11;
if(sel.ltf)
{
u0 = s.sub16(GSVector4i(0x00200020)); // - 0.125f
v0 = t.sub16(GSVector4i(0x00200020)); // - 0.125f
uf = u0.sll16(8).srl16(1);
vf = v0.sll16(8).srl16(1);;
}
else
{
u0 = s;
v0 = t;
}
u0 = u0.srl16(8);
v0 = v0.srl16(8);
if(sel.ltf)
{
u1 = u0.add16(GSVector4i::x0001());
v1 = v0.add16(GSVector4i::x0001());
if(sel.twin)
{
u0 = (u0 & m_local.twin[0].u).add16(m_local.twin[1].u);
v0 = (v0 & m_local.twin[0].v).add16(m_local.twin[1].v);
u1 = (u1 & m_local.twin[0].u).add16(m_local.twin[1].u);
v1 = (v1 & m_local.twin[0].v).add16(m_local.twin[1].v);
}
else
{
u0 = u0.min_i16(m_local.twin[2].u);
v0 = v0.min_i16(m_local.twin[2].v);
u1 = u1.min_i16(m_local.twin[2].u);
v1 = v1.min_i16(m_local.twin[2].v);
}
addr00 = v0.sll16(8) | u0;
addr01 = v0.sll16(8) | u1;
addr10 = v1.sll16(8) | u0;
addr11 = v1.sll16(8) | u1;
// TODO
if(sel.tlu)
{
c00 = addr00.gather16_16((const uint16*)m_global.vm, m_global.clut);
c01 = addr01.gather16_16((const uint16*)m_global.vm, m_global.clut);
c10 = addr10.gather16_16((const uint16*)m_global.vm, m_global.clut);
c11 = addr11.gather16_16((const uint16*)m_global.vm, m_global.clut);
}
else
{
c00 = addr00.gather16_16((const uint16*)m_global.vm);
c01 = addr01.gather16_16((const uint16*)m_global.vm);
c10 = addr10.gather16_16((const uint16*)m_global.vm);
c11 = addr11.gather16_16((const uint16*)m_global.vm);
}
GSVector4i r00 = c00.sll16(11).srl16(8);
GSVector4i r01 = c01.sll16(11).srl16(8);
GSVector4i r10 = c10.sll16(11).srl16(8);
GSVector4i r11 = c11.sll16(11).srl16(8);
r00 = r00.lerp16<0>(r01, uf);
r10 = r10.lerp16<0>(r11, uf);
GSVector4i g00 = c00.sll16(6).srl16(11).sll16(3);
GSVector4i g01 = c01.sll16(6).srl16(11).sll16(3);
GSVector4i g10 = c10.sll16(6).srl16(11).sll16(3);
GSVector4i g11 = c11.sll16(6).srl16(11).sll16(3);
g00 = g00.lerp16<0>(g01, uf);
g10 = g10.lerp16<0>(g11, uf);
GSVector4i b00 = c00.sll16(1).srl16(11).sll16(3);
GSVector4i b01 = c01.sll16(1).srl16(11).sll16(3);
GSVector4i b10 = c10.sll16(1).srl16(11).sll16(3);
GSVector4i b11 = c11.sll16(1).srl16(11).sll16(3);
b00 = b00.lerp16<0>(b01, uf);
b10 = b10.lerp16<0>(b11, uf);
GSVector4i a00 = c00.sra16(15).sll16(8);
GSVector4i a01 = c01.sra16(15).sll16(8);
GSVector4i a10 = c10.sra16(15).sll16(8);
GSVector4i a11 = c11.sra16(15).sll16(8);
a00 = a00.lerp16<0>(a01, uf);
a10 = a10.lerp16<0>(a11, uf);
r = r00.lerp16<0>(r10, vf);
g = g00.lerp16<0>(g10, vf);
b = b00.lerp16<0>(b10, vf);
a = a00.lerp16<0>(a10, vf);
test |= (r | g | b | a).eq16(GSVector4i::zero()); // mask out blank pixels (not perfect)
a = a.gt16(GSVector4i::zero());
}
else
{
if(sel.twin)
{
u0 = (u0 & m_local.twin[0].u).add16(m_local.twin[1].u);
v0 = (v0 & m_local.twin[0].v).add16(m_local.twin[1].v);
}
else
{
u0 = u0.min_i16(m_local.twin[2].u);
v0 = v0.min_i16(m_local.twin[2].v);
}
addr00 = v0.sll16(8) | u0;
// TODO
if(sel.tlu)
{
c00 = addr00.gather16_16((const uint16*)m_global.vm, m_global.clut);
}
else
{
c00 = addr00.gather16_16((const uint16*)m_global.vm);
}
r = (c00 << 3) & 0x00f800f8;
g = (c00 >> 2) & 0x00f800f8;
b = (c00 >> 7) & 0x00f800f8;
a = c00.sra16(15);
test |= c00.eq16(GSVector4i::zero()); // mask out blank pixels
}
}
// ColorTFX
switch(sel.tfx)
{
case 0: // none (tfx = 0)
case 1: // none (tfx = tge)
r = rf.srl16(7);
g = gf.srl16(7);
b = bf.srl16(7);
break;
case 2: // modulate (tfx = tme | tge)
r = r.modulate16<1>(rf).clamp8();
g = g.modulate16<1>(gf).clamp8();
b = b.modulate16<1>(bf).clamp8();
break;
case 3: // decal (tfx = tme)
break;
default:
__assume(0);
}
// AlphaBlend
if(sel.abe)
{
GSVector4i rs = r;
GSVector4i gs = g;
GSVector4i bs = b;
GSVector4i rd = (fd & 0x001f001f) << 3;
GSVector4i gd = (fd & 0x03e003e0) >> 2;
GSVector4i bd = (fd & 0x7c007c00) >> 7;
switch(sel.abr)
{
case 0:
r = rd.avg8(rs);
g = gd.avg8(gs);
b = bd.avg8(bs);
break;
case 1:
r = rd.addus8(rs);
g = gd.addus8(gs);
b = bd.addus8(bs);
break;
case 2:
r = rd.subus8(rs);
g = gd.subus8(gs);
b = bd.subus8(bs);
break;
case 3:
r = rd.addus8(rs.srl16(2));
g = gd.addus8(gs.srl16(2));
b = bd.addus8(bs.srl16(2));
break;
default:
__assume(0);
}
if(sel.tme)
{
r = rs.blend8(rd, a);
g = gs.blend8(gd, a);
b = bs.blend8(bd, a);
}
}
// Dither
if(sel.dtd)
{
r = r.addus8(dither);
g = g.addus8(dither);
b = b.addus8(dither);
}
// WriteFrame
GSVector4i fs = r | g | b | (sel.md ? GSVector4i(0x80008000) : sel.tme ? a : GSVector4i::zero());
fs = fs.blend8(fd, test);
GSVector4i::store(fb, fb + 8, fs);
}
while(0);
if(steps <= 0) break;
steps -= 8;
fb += 8;
if(sel.tme)
{
GSVector4i st = m_local.d8.st;
s = s.add16(st.xxxx());
t = t.add16(st.yyyy());
}
if(sel.tfx != 3) // != decal
{
if(sel.iip)
{
GSVector4i c = m_local.d8.c;
rf = rf.add16(c.xxxx());
gf = gf.add16(c.yyyy());
bf = bf.add16(c.zzzz());
}
}
}
}
void GPUDrawScanline::DrawEdge(int pixels, int left, int top, const GSVertexSW& scan)
{
ASSERT(0);
}
void GPUDrawScanline::DrawRect(const GSVector4i& r, const GSVertexSW& v)
{
// TODO
}
#endif