~registry/dolphin-emu/triforce

« back to all changes in this revision

Viewing changes to Source/Core/VideoBackends/Software/Src/Rasterizer.cpp

  • Committer: Sérgio Benjamim
  • Date: 2015-02-13 05:54:40 UTC
  • Revision ID: sergio_br2@yahoo.com.br-20150213055440-ey2rt3sjpy27km78
Dolphin Triforce branch from code.google, commit b957980 (4.0-315).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2013 Dolphin Emulator Project
 
2
// Licensed under GPLv2
 
3
// Refer to the license.txt file included.
 
4
 
 
5
#include "Common.h"
 
6
 
 
7
#include "Rasterizer.h"
 
8
#include "HwRasterizer.h"
 
9
#include "EfbInterface.h"
 
10
#include "BPMemLoader.h"
 
11
#include "XFMemLoader.h"
 
12
#include "Tev.h"
 
13
#include "SWPixelEngine.h"
 
14
#include "SWStatistics.h"
 
15
#include "SWVideoConfig.h"
 
16
 
 
17
 
 
18
#define BLOCK_SIZE 2
 
19
 
 
20
#define CLAMP(x, a, b) (x>b)?b:(x<a)?a:x
 
21
 
 
22
// returns approximation of log2(f) in s28.4
 
23
// results are close enough to use for LOD
 
24
static inline s32 FixedLog2(float f)
 
25
{
 
26
        u32 *x = (u32*)&f;
 
27
        s32 logInt = ((*x & 0x7F800000) >> 19) - 2032; // integer part
 
28
        s32 logFract = (*x & 0x007fffff) >> 19; // approximate fractional part
 
29
 
 
30
        return logInt + logFract;
 
31
}
 
32
 
 
33
namespace Rasterizer
 
34
{
 
35
Slope ZSlope;
 
36
Slope WSlope;
 
37
Slope ColorSlopes[2][4];
 
38
Slope TexSlopes[8][3];
 
39
 
 
40
s32 vertex0X;
 
41
s32 vertex0Y;
 
42
float vertexOffsetX;
 
43
float vertexOffsetY;
 
44
 
 
45
s32 scissorLeft = 0;
 
46
s32 scissorTop = 0;
 
47
s32 scissorRight = 0;
 
48
s32 scissorBottom = 0;
 
49
 
 
50
Tev tev;
 
51
RasterBlock rasterBlock;
 
52
 
 
53
void DoState(PointerWrap &p)
 
54
{
 
55
        ZSlope.DoState(p);
 
56
        WSlope.DoState(p);
 
57
        for (int i=0;i<2;++i)
 
58
                for (int n=0; n<4; ++n)
 
59
                        ColorSlopes[i][n].DoState(p);
 
60
        for (int i=0;i<8;++i)
 
61
                for (int n=0; n<3; ++n)
 
62
                        TexSlopes[i][n].DoState(p);
 
63
        p.Do(vertex0X);
 
64
        p.Do(vertex0Y);
 
65
        p.Do(vertexOffsetX);
 
66
        p.Do(vertexOffsetY);
 
67
        p.Do(scissorLeft);
 
68
        p.Do(scissorTop);
 
69
        p.Do(scissorRight);
 
70
        p.Do(scissorBottom);
 
71
        tev.DoState(p);
 
72
        p.Do(rasterBlock);
 
73
}
 
74
 
 
75
void Init()
 
76
{
 
77
        tev.Init();
 
78
 
 
79
        // Set initial z reference plane in the unlikely case that zfreeze is enabled when drawing the first primitive.
 
80
        // TODO: This is just a guess!
 
81
        ZSlope.dfdx = ZSlope.dfdy = 0.f;
 
82
        ZSlope.f0 = 1.f;
 
83
}
 
84
 
 
85
inline int iround(float x)
 
86
{
 
87
        int t;
 
88
 
 
89
#if defined(_WIN32) && !defined(_M_X64)
 
90
        __asm
 
91
        {
 
92
                fld  x
 
93
                fistp t
 
94
        }
 
95
#else
 
96
        t = (int)x;
 
97
        if((x - t) >= 0.5)
 
98
                return t + 1;
 
99
#endif
 
100
 
 
101
        return t;
 
102
}
 
103
 
 
104
void SetScissor()
 
105
{
 
106
        int xoff = bpmem.scissorOffset.x * 2 - 342;
 
107
        int yoff = bpmem.scissorOffset.y * 2 - 342;
 
108
 
 
109
        scissorLeft = bpmem.scissorTL.x - xoff - 342;
 
110
        if (scissorLeft < 0) scissorLeft = 0;
 
111
 
 
112
        scissorTop = bpmem.scissorTL.y - yoff - 342;
 
113
        if (scissorTop < 0) scissorTop = 0;
 
114
 
 
115
        scissorRight = bpmem.scissorBR.x - xoff - 341;
 
116
        if (scissorRight > EFB_WIDTH) scissorRight = EFB_WIDTH;
 
117
 
 
118
        scissorBottom = bpmem.scissorBR.y - yoff - 341;
 
119
        if (scissorBottom > EFB_HEIGHT) scissorBottom = EFB_HEIGHT;
 
120
}
 
121
 
 
122
void SetTevReg(int reg, int comp, bool konst, s16 color)
 
123
{
 
124
        tev.SetRegColor(reg, comp, konst, color);
 
125
}
 
126
 
 
127
inline void Draw(s32 x, s32 y, s32 xi, s32 yi)
 
128
{
 
129
        INCSTAT(swstats.thisFrame.rasterizedPixels);
 
130
 
 
131
        float dx = vertexOffsetX + (float)(x - vertex0X);
 
132
        float dy = vertexOffsetY + (float)(y - vertex0Y);
 
133
 
 
134
        s32 z = (s32)ZSlope.GetValue(dx, dy);
 
135
        if (z < 0 || z > 0x00ffffff)
 
136
                return;
 
137
 
 
138
        if (bpmem.UseEarlyDepthTest() && g_SWVideoConfig.bZComploc)
 
139
        {
 
140
                // TODO: Test if perf regs are incremented even if test is disabled
 
141
                SWPixelEngine::pereg.IncZInputQuadCount(true);
 
142
                if (bpmem.zmode.testenable)
 
143
                {
 
144
                        // early z
 
145
                        if (!EfbInterface::ZCompare(x, y, z))
 
146
                                return;
 
147
                }
 
148
                SWPixelEngine::pereg.IncZOutputQuadCount(true);
 
149
        }
 
150
 
 
151
        RasterBlockPixel& pixel = rasterBlock.Pixel[xi][yi];
 
152
 
 
153
        tev.Position[0] = x;
 
154
        tev.Position[1] = y;
 
155
        tev.Position[2] = z;
 
156
 
 
157
        //  colors
 
158
        for (unsigned int i = 0; i < bpmem.genMode.numcolchans; i++)
 
159
        {
 
160
                for(int comp = 0; comp < 4; comp++)
 
161
                {
 
162
                        u16 color = (u16)ColorSlopes[i][comp].GetValue(dx, dy);
 
163
 
 
164
                        // clamp color value to 0
 
165
                        u16 mask = ~(color >> 8);
 
166
 
 
167
                        tev.Color[i][comp] = color & mask;
 
168
                }
 
169
        }
 
170
 
 
171
        // tex coords
 
172
        for (unsigned int i = 0; i < bpmem.genMode.numtexgens; i++)
 
173
        {
 
174
                // multiply by 128 because TEV stores UVs as s17.7
 
175
                tev.Uv[i].s = (s32)(pixel.Uv[i][0] * 128);
 
176
                tev.Uv[i].t = (s32)(pixel.Uv[i][1] * 128);
 
177
        }
 
178
 
 
179
        for (unsigned int i = 0; i < bpmem.genMode.numindstages; i++)
 
180
        {
 
181
                tev.IndirectLod[i] = rasterBlock.IndirectLod[i];
 
182
                tev.IndirectLinear[i] = rasterBlock.IndirectLinear[i];
 
183
        }
 
184
 
 
185
        for (unsigned int i = 0; i <= bpmem.genMode.numtevstages; i++)
 
186
        {
 
187
                tev.TextureLod[i] = rasterBlock.TextureLod[i];
 
188
                tev.TextureLinear[i] = rasterBlock.TextureLinear[i];
 
189
        }
 
190
 
 
191
        tev.Draw();
 
192
}
 
193
 
 
194
void InitTriangle(float X1, float Y1, s32 xi, s32 yi)
 
195
{
 
196
        vertex0X = xi;
 
197
        vertex0Y = yi;
 
198
 
 
199
        // adjust a little less than 0.5
 
200
        const float adjust = 0.495f;
 
201
 
 
202
        vertexOffsetX = ((float)xi - X1) + adjust;
 
203
        vertexOffsetY = ((float)yi - Y1) + adjust;
 
204
}
 
205
 
 
206
void InitSlope(Slope *slope, float f1, float f2, float f3, float DX31, float DX12, float DY12, float DY31)
 
207
{
 
208
        float DF31 = f3 - f1;
 
209
        float DF21 = f2 - f1;
 
210
        float a = DF31 * -DY12 - DF21 * DY31;
 
211
        float b = DX31 * DF21 + DX12 * DF31;
 
212
        float c = -DX12 * DY31 - DX31 * -DY12;
 
213
        slope->dfdx = -a / c;
 
214
        slope->dfdy = -b / c;
 
215
        slope->f0 = f1;
 
216
}
 
217
 
 
218
inline void CalculateLOD(s32 &lod, bool &linear, u32 texmap, u32 texcoord)
 
219
{
 
220
        FourTexUnits& texUnit = bpmem.tex[(texmap >> 2) & 1];
 
221
        u8 subTexmap = texmap & 3;
 
222
 
 
223
        // LOD calculation requires data from the texture mode for bias, etc.
 
224
        // it does not seem to use the actual texture size
 
225
        TexMode0& tm0 = texUnit.texMode0[subTexmap];
 
226
        TexMode1& tm1 = texUnit.texMode1[subTexmap];
 
227
 
 
228
        float sDelta, tDelta;
 
229
        if (tm0.diag_lod)
 
230
        {
 
231
                float *uv0 = rasterBlock.Pixel[0][0].Uv[texcoord];
 
232
                float *uv1 = rasterBlock.Pixel[1][1].Uv[texcoord];
 
233
 
 
234
                sDelta = fabsf(uv0[0] - uv1[0]);
 
235
                tDelta = fabsf(uv0[1] - uv1[1]);
 
236
        }
 
237
        else
 
238
        {
 
239
                float *uv0 = rasterBlock.Pixel[0][0].Uv[texcoord];
 
240
                float *uv1 = rasterBlock.Pixel[1][0].Uv[texcoord];
 
241
                float *uv2 = rasterBlock.Pixel[0][1].Uv[texcoord];
 
242
 
 
243
                sDelta = max(fabsf(uv0[0] - uv1[0]), fabsf(uv0[0] - uv2[0]));
 
244
                tDelta = max(fabsf(uv0[1] - uv1[1]), fabsf(uv0[1] - uv2[1]));
 
245
        }
 
246
 
 
247
        // get LOD in s28.4
 
248
        lod = FixedLog2(max(sDelta, tDelta));
 
249
 
 
250
        // bias is s2.5
 
251
        int bias = tm0.lod_bias;
 
252
        bias >>= 1;
 
253
        lod += bias;
 
254
 
 
255
        linear = ((lod > 0 && (tm0.min_filter & 4)) || (lod <= 0 && tm0.mag_filter));
 
256
 
 
257
        // order of checks matters
 
258
        // should be:
 
259
        // if lod > max then max
 
260
        // else if lod < min then min
 
261
        lod = CLAMP(lod, (s32)tm1.min_lod, (s32)tm1.max_lod);
 
262
}
 
263
 
 
264
void BuildBlock(s32 blockX, s32 blockY)
 
265
{
 
266
        for (s32 yi = 0; yi < BLOCK_SIZE; yi++)
 
267
        {
 
268
                for (s32 xi = 0; xi < BLOCK_SIZE; xi++)
 
269
                {
 
270
                        RasterBlockPixel& pixel = rasterBlock.Pixel[xi][yi];
 
271
 
 
272
                        float dx = vertexOffsetX + (float)(xi + blockX - vertex0X);
 
273
                        float dy = vertexOffsetY + (float)(yi + blockY - vertex0Y);
 
274
 
 
275
                        float invW = 1.0f / WSlope.GetValue(dx, dy);
 
276
                        pixel.InvW = invW;
 
277
 
 
278
                        // tex coords
 
279
                        for (unsigned int i = 0; i < bpmem.genMode.numtexgens; i++)
 
280
                        {
 
281
                                float projection = invW;
 
282
                                if (swxfregs.texMtxInfo[i].projection)
 
283
                                {
 
284
                                        float q = TexSlopes[i][2].GetValue(dx, dy) * invW;
 
285
                                        if (q != 0.0f)
 
286
                                                projection = invW / q;
 
287
                                }
 
288
 
 
289
                                pixel.Uv[i][0] = TexSlopes[i][0].GetValue(dx, dy) * projection;
 
290
                                pixel.Uv[i][1] = TexSlopes[i][1].GetValue(dx, dy) * projection;
 
291
                        }
 
292
                }
 
293
        }
 
294
 
 
295
        u32 indref = bpmem.tevindref.hex;
 
296
        for (unsigned int i = 0; i < bpmem.genMode.numindstages; i++)
 
297
        {
 
298
                u32 texmap = indref & 3;
 
299
                indref >>= 3;
 
300
                u32 texcoord = indref & 3;
 
301
                indref >>= 3;
 
302
 
 
303
                CalculateLOD(rasterBlock.IndirectLod[i], rasterBlock.IndirectLinear[i], texmap, texcoord);
 
304
        }
 
305
 
 
306
        for (unsigned int i = 0; i <= bpmem.genMode.numtevstages; i++)
 
307
        {
 
308
                int stageOdd = i&1;
 
309
                TwoTevStageOrders &order = bpmem.tevorders[i >> 1];
 
310
                if(order.getEnable(stageOdd))
 
311
                {
 
312
                        u32 texmap = order.getTexMap(stageOdd);
 
313
                        u32 texcoord = order.getTexCoord(stageOdd);
 
314
 
 
315
                        CalculateLOD(rasterBlock.TextureLod[i], rasterBlock.TextureLinear[i], texmap, texcoord);
 
316
                }
 
317
        }
 
318
}
 
319
 
 
320
void DrawTriangleFrontFace(OutputVertexData *v0, OutputVertexData *v1, OutputVertexData *v2)
 
321
{
 
322
        INCSTAT(swstats.thisFrame.numTrianglesDrawn);
 
323
 
 
324
        if (g_SWVideoConfig.bHwRasterizer)
 
325
        {
 
326
                HwRasterizer::DrawTriangleFrontFace(v0, v1, v2);
 
327
                return;
 
328
        }
 
329
 
 
330
        // adapted from http://www.devmaster.net/forums/showthread.php?t=1884
 
331
 
 
332
        // 28.4 fixed-pou32 coordinates. rounded to nearest and adjusted to match hardware output
 
333
        // could also take floor and adjust -8
 
334
        const s32 Y1 = iround(16.0f * v0->screenPosition[1]) - 9;
 
335
        const s32 Y2 = iround(16.0f * v1->screenPosition[1]) - 9;
 
336
        const s32 Y3 = iround(16.0f * v2->screenPosition[1]) - 9;
 
337
 
 
338
        const s32 X1 = iround(16.0f * v0->screenPosition[0]) - 9;
 
339
        const s32 X2 = iround(16.0f * v1->screenPosition[0]) - 9;
 
340
        const s32 X3 = iround(16.0f * v2->screenPosition[0]) - 9;
 
341
 
 
342
        // Deltas
 
343
        const s32 DX12 = X1 - X2;
 
344
        const s32 DX23 = X2 - X3;
 
345
        const s32 DX31 = X3 - X1;
 
346
 
 
347
        const s32 DY12 = Y1 - Y2;
 
348
        const s32 DY23 = Y2 - Y3;
 
349
        const s32 DY31 = Y3 - Y1;
 
350
 
 
351
        // Fixed-pos32 deltas
 
352
        const s32 FDX12 = DX12 << 4;
 
353
        const s32 FDX23 = DX23 << 4;
 
354
        const s32 FDX31 = DX31 << 4;
 
355
 
 
356
        const s32 FDY12 = DY12 << 4;
 
357
        const s32 FDY23 = DY23 << 4;
 
358
        const s32 FDY31 = DY31 << 4;
 
359
 
 
360
        // Bounding rectangle
 
361
        s32 minx = (min(min(X1, X2), X3) + 0xF) >> 4;
 
362
        s32 maxx = (max(max(X1, X2), X3) + 0xF) >> 4;
 
363
        s32 miny = (min(min(Y1, Y2), Y3) + 0xF) >> 4;
 
364
        s32 maxy = (max(max(Y1, Y2), Y3) + 0xF) >> 4;
 
365
 
 
366
        // scissor
 
367
        minx = max(minx, scissorLeft);
 
368
        maxx = min(maxx, scissorRight);
 
369
        miny = max(miny, scissorTop);
 
370
        maxy = min(maxy, scissorBottom);
 
371
 
 
372
        if (minx >= maxx || miny >= maxy)
 
373
                return;
 
374
 
 
375
        // Setup slopes
 
376
        float fltx1 = v0->screenPosition.x;
 
377
        float flty1 = v0->screenPosition.y;
 
378
        float fltdx31 = v2->screenPosition.x - fltx1;
 
379
        float fltdx12 = fltx1 - v1->screenPosition.x;
 
380
        float fltdy12 = flty1 - v1->screenPosition.y;
 
381
        float fltdy31 = v2->screenPosition.y - flty1;
 
382
 
 
383
        InitTriangle(fltx1, flty1, (X1 + 0xF) >> 4, (Y1 + 0xF) >> 4);
 
384
 
 
385
        float w[3] = { 1.0f / v0->projectedPosition.w, 1.0f / v1->projectedPosition.w, 1.0f / v2->projectedPosition.w };
 
386
        InitSlope(&WSlope, w[0], w[1], w[2], fltdx31, fltdx12, fltdy12, fltdy31);
 
387
 
 
388
        if (!bpmem.genMode.zfreeze || !g_SWVideoConfig.bZFreeze)
 
389
                InitSlope(&ZSlope, v0->screenPosition[2], v1->screenPosition[2], v2->screenPosition[2], fltdx31, fltdx12, fltdy12, fltdy31);
 
390
 
 
391
        for(unsigned int i = 0; i < bpmem.genMode.numcolchans; i++)
 
392
        {
 
393
                for(int comp = 0; comp < 4; comp++)
 
394
                        InitSlope(&ColorSlopes[i][comp], v0->color[i][comp], v1->color[i][comp], v2->color[i][comp], fltdx31, fltdx12, fltdy12, fltdy31);
 
395
        }
 
396
 
 
397
        for(unsigned int i = 0; i < bpmem.genMode.numtexgens; i++)
 
398
        {
 
399
                for(int comp = 0; comp < 3; comp++)
 
400
                        InitSlope(&TexSlopes[i][comp], v0->texCoords[i][comp] * w[0], v1->texCoords[i][comp] * w[1], v2->texCoords[i][comp] * w[2], fltdx31, fltdx12, fltdy12, fltdy31);
 
401
        }
 
402
 
 
403
        // Start in corner of 8x8 block
 
404
        minx &= ~(BLOCK_SIZE - 1);
 
405
        miny &= ~(BLOCK_SIZE - 1);
 
406
 
 
407
        // Half-edge constants
 
408
        s32 C1 = DY12 * X1 - DX12 * Y1;
 
409
        s32 C2 = DY23 * X2 - DX23 * Y2;
 
410
        s32 C3 = DY31 * X3 - DX31 * Y3;
 
411
 
 
412
        // Correct for fill convention
 
413
        if(DY12 < 0 || (DY12 == 0 && DX12 > 0)) C1++;
 
414
        if(DY23 < 0 || (DY23 == 0 && DX23 > 0)) C2++;
 
415
        if(DY31 < 0 || (DY31 == 0 && DX31 > 0)) C3++;
 
416
 
 
417
        // Loop through blocks
 
418
        for(s32 y = miny; y < maxy; y += BLOCK_SIZE)
 
419
        {
 
420
                for(s32 x = minx; x < maxx; x += BLOCK_SIZE)
 
421
                {
 
422
                        // Corners of block
 
423
                        s32 x0 = x << 4;
 
424
                        s32 x1 = (x + BLOCK_SIZE - 1) << 4;
 
425
                        s32 y0 = y << 4;
 
426
                        s32 y1 = (y + BLOCK_SIZE - 1) << 4;
 
427
 
 
428
                        // Evaluate half-space functions
 
429
                        bool a00 = C1 + DX12 * y0 - DY12 * x0 > 0;
 
430
                        bool a10 = C1 + DX12 * y0 - DY12 * x1 > 0;
 
431
                        bool a01 = C1 + DX12 * y1 - DY12 * x0 > 0;
 
432
                        bool a11 = C1 + DX12 * y1 - DY12 * x1 > 0;
 
433
                        int a = (a00 << 0) | (a10 << 1) | (a01 << 2) | (a11 << 3);
 
434
 
 
435
                        bool b00 = C2 + DX23 * y0 - DY23 * x0 > 0;
 
436
                        bool b10 = C2 + DX23 * y0 - DY23 * x1 > 0;
 
437
                        bool b01 = C2 + DX23 * y1 - DY23 * x0 > 0;
 
438
                        bool b11 = C2 + DX23 * y1 - DY23 * x1 > 0;
 
439
                        int b = (b00 << 0) | (b10 << 1) | (b01 << 2) | (b11 << 3);
 
440
 
 
441
                        bool c00 = C3 + DX31 * y0 - DY31 * x0 > 0;
 
442
                        bool c10 = C3 + DX31 * y0 - DY31 * x1 > 0;
 
443
                        bool c01 = C3 + DX31 * y1 - DY31 * x0 > 0;
 
444
                        bool c11 = C3 + DX31 * y1 - DY31 * x1 > 0;
 
445
                        int c = (c00 << 0) | (c10 << 1) | (c01 << 2) | (c11 << 3);
 
446
 
 
447
                        // Skip block when outside an edge
 
448
                        if(a == 0x0 || b == 0x0 || c == 0x0)
 
449
                                continue;
 
450
 
 
451
                        BuildBlock(x, y);
 
452
 
 
453
                        // Accept whole block when totally covered
 
454
                        if(a == 0xF && b == 0xF && c == 0xF)
 
455
                        {
 
456
                                for(s32 iy = 0; iy < BLOCK_SIZE; iy++)
 
457
                                {
 
458
                                        for(s32 ix = 0; ix < BLOCK_SIZE; ix++)
 
459
                                        {
 
460
                                                Draw(x + ix, y + iy, ix, iy);
 
461
                                        }
 
462
                                }
 
463
                        }
 
464
                        else // Partially covered block
 
465
                        {
 
466
                                s32 CY1 = C1 + DX12 * y0 - DY12 * x0;
 
467
                                s32 CY2 = C2 + DX23 * y0 - DY23 * x0;
 
468
                                s32 CY3 = C3 + DX31 * y0 - DY31 * x0;
 
469
 
 
470
                                for(s32 iy = 0; iy < BLOCK_SIZE; iy++)
 
471
                                {
 
472
                                        s32 CX1 = CY1;
 
473
                                        s32 CX2 = CY2;
 
474
                                        s32 CX3 = CY3;
 
475
 
 
476
                                        for(s32 ix = 0; ix < BLOCK_SIZE; ix++)
 
477
                                        {
 
478
                                                if(CX1 > 0 && CX2 > 0 && CX3 > 0)
 
479
                                                {
 
480
                                                        Draw(x + ix, y + iy, ix, iy);
 
481
                                                }
 
482
 
 
483
                                                CX1 -= FDY12;
 
484
                                                CX2 -= FDY23;
 
485
                                                CX3 -= FDY31;
 
486
                                        }
 
487
 
 
488
                                        CY1 += FDX12;
 
489
                                        CY2 += FDX23;
 
490
                                        CY3 += FDX31;
 
491
                                }
 
492
                        }
 
493
                }
 
494
        }
 
495
}
 
496
 
 
497
 
 
498
}