aboutsummaryrefslogtreecommitdiff
path: root/GPU3D_Soft.cpp
blob: 7a901dd7540d3795a9819687f667a45c6a64197d (plain)
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
/*
    Copyright 2016-2017 StapleButter

    This file is part of melonDS.

    melonDS 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 3 of the License, or (at your option)
    any later version.

    melonDS 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 melonDS. If not, see http://www.gnu.org/licenses/.
*/

#include <stdio.h>
#include <string.h>
#include "NDS.h"
#include "GPU3D.h"


namespace GPU3D
{
namespace SoftRenderer
{

u8 ColorBuffer[256*192 * 4];
u32 DepthBuffer[256*192];


bool Init()
{
    return true;
}

void DeInit()
{
    //
}

void Reset()
{
    memset(ColorBuffer, 0, 256*192 * 4);
    memset(DepthBuffer, 0, 256*192 * 4);
}


void RenderPixel(u32 attr, s32 x, s32 y, s32 z, u8 vr, u8 vg, u8 vb)
{
    u32* depth = &DepthBuffer[(256*y) + x];

    bool passdepth = false;
    if (attr & (1<<14))
    {
        s32 diff = *depth - z;
        if ((u32)(diff + 0x200) <= 0x400)
            passdepth = true;
    }
    else
    if (z < *depth)
        passdepth = true;

    if (!passdepth) return;

    u8* pixel = &ColorBuffer[((256*y) + x) * 4];
    pixel[0] = vr;
    pixel[1] = vg;
    pixel[2] = vb;
    pixel[3] = 31; // TODO: alpha

    // TODO: optional update for translucent pixels
    *depth = z;
}

void RenderPolygon(Polygon* polygon)
{
    int nverts = polygon->NumVertices;

    int vtop = 0, vbot = 0;
    s32 ytop = 191, ybot = 0;
    s32 scrcoords[10][4];

    // find the topmost and bottommost vertices of the polygon

    for (int i = 0; i < nverts; i++)
    {
        Vertex* vtx = polygon->Vertices[i];

        s32 posX, posY, posZ;
        s32 w = vtx->Position[3];
        if (w == 0)
        {
            posX = 0;
            posY = 0;
            posZ = 0;
        }
        else
        {
            // TODO: find a way to avoid doing 3 divisions :/
            posX = ((s64)vtx->Position[0] << 12) / w;
            posY = ((s64)vtx->Position[1] << 12) / w;
            posZ = ((s64)vtx->Position[2] << 12) / w;
        }
        //s32 posX = vtx->Position[0];
        //s32 posY = vtx->Position[1];

        //printf("xy: %08X %08X %08X\n", vtx->Position[0], vtx->Position[1], vtx->Position[3]);
        //printf("w_inv: %08X   res: %08X %08X\n", w_inv, posX, posY);

        s32 scrX = (((posX + 0x1000) * Viewport[2]) >> 13) + Viewport[0];
        s32 scrY = (((posY + 0x1000) * Viewport[3]) >> 13) + Viewport[1];
        s32 scrZ = (((s64)(posZ + 0x1000) * 0xFFFFFF) >> 13);
        s32 scrW = (((s64)(w    + 0x1000) * 0xFFFFFF) >> 13);
        if (scrX > 255) scrX = 255;
        if (scrY > 191) scrY = 191;
        if (scrZ > 0xFFFFFF) scrZ = 0xFFFFFF;
        if (scrX < 0) { printf("!! bad X %d\n", scrX); scrX = 0;}
        if (scrY < 0) { printf("!! bad Y %d\n", scrY); scrY = 0;}
        if (scrZ < 0) { printf("!! bad Z %d %d\n", scrZ, vtx->Position[2]); scrZ = 0;}

        scrcoords[i][0] = scrX;
        scrcoords[i][1] = 191 - scrY;
        scrcoords[i][2] = scrZ;
        scrcoords[i][3] = scrW;

        if (scrcoords[i][1] < ytop)
        {
            ytop = scrcoords[i][1];
            vtop = i;
        }
        if (scrcoords[i][1] > ybot)
        {
            ybot = scrcoords[i][1];
            vbot = i;
        }
        //if (vtx->Color[0]==63 && vtx->Color[1]==0 && vtx->Color[2]==0)
        //printf("v%d: %d,%d  Z=%f  W=%f  %d  %d\n", i, scrX, 191-scrY, vtx->Position[2]/4096.0f, vtx->Position[3]/4096.0f,
        //       polygon->FacingView, vtx->Clipped);
    }

    // draw, line per line

    int lcur = vtop, rcur = vtop;
    int lnext, rnext;
    s32 lstep, rstep;
    //s32 xmin, xmax;

    if (polygon->FacingView)
    {
        lnext = lcur + 1;
        if (lnext >= nverts) lnext = 0;
        rnext = rcur - 1;
        if (rnext < 0) rnext = nverts - 1;
    }
    else
    {
        lnext = lcur - 1;
        if (lnext < 0) lnext = nverts - 1;
        rnext = rcur + 1;
        if (rnext >= nverts) rnext = 0;
    }

    /*if ((scrcoords[lnext][1] - scrcoords[lcur][1]) == 0) lstep = 0; else
    lstep = ((scrcoords[lnext][0] - scrcoords[lcur][0]) << 12) / (scrcoords[lnext][1] - scrcoords[lcur][1]);
    if ((scrcoords[rnext][1] - scrcoords[rcur][1]) == 0) rstep = 0; else
    rstep = ((scrcoords[rnext][0] - scrcoords[rcur][0]) << 12) / (scrcoords[rnext][1] - scrcoords[rcur][1]);*/

    //xmin = scrcoords[lcur][0] << 12;
    //xmax = scrcoords[rcur][0] << 12;

    for (s32 y = ytop; y <= ybot; y++)
    {
        if (y < ybot)
        {
            while (y == scrcoords[lnext][1])
            {
                lcur = lnext;

                if (polygon->FacingView)
                {
                    lnext = lcur + 1;
                    if (lnext >= nverts) lnext = 0;
                }
                else
                {
                    lnext = lcur - 1;
                    if (lnext < 0) lnext = nverts - 1;
                }

                //lstep = ((scrcoords[lnext][0] - scrcoords[lcur][0]) << 12) / (scrcoords[lnext][1] - scrcoords[lcur][1]);
                //xmin = scrcoords[lcur][0] << 12;
                if (lcur == vbot) break;
            }

            while (y == scrcoords[rnext][1])
            {
                rcur = rnext;

                if (polygon->FacingView)
                {
                    rnext = rcur - 1;
                    if (rnext < 0) rnext = nverts - 1;
                }
                else
                {
                    rnext = rcur + 1;
                    if (rnext >= nverts) rnext = 0;
                }

                //rstep = ((scrcoords[rnext][0] - scrcoords[rcur][0]) << 12) / (scrcoords[rnext][1] - scrcoords[rcur][1]);
                //xmax = scrcoords[rcur][0] << 12;
                if (rcur == vbot) break;
            }
        }

        Vertex* vlcur = polygon->Vertices[lcur];
        Vertex* vlnext = polygon->Vertices[lnext];
        Vertex* vrcur = polygon->Vertices[rcur];
        Vertex* vrnext = polygon->Vertices[rnext];

        s32 lfactor, rfactor;

        if (scrcoords[lnext][1] == scrcoords[lcur][1])
            lfactor = 0;
        else
            lfactor = ((y - scrcoords[lcur][1]) << 12) / (scrcoords[lnext][1] - scrcoords[lcur][1]);

        if (scrcoords[rnext][1] == scrcoords[rcur][1])
            rfactor = 0;
        else
            rfactor = ((y - scrcoords[rcur][1]) << 12) / (scrcoords[rnext][1] - scrcoords[rcur][1]);

        s32 xl = scrcoords[lcur][0] + (((scrcoords[lnext][0] - scrcoords[lcur][0]) * lfactor) >> 12);
        s32 xr = scrcoords[rcur][0] + (((scrcoords[rnext][0] - scrcoords[rcur][0]) * rfactor) >> 12);

        if (xl<0 || xr>255) continue; // hax

        //if (vlcur->Color[0]==0 && vlcur->Color[1]==63 && vlcur->Color[2]==0)
            /*printf("y:%d  xleft:%d  xright:%d   %d,%d  %d,%d   |   left: %d to %d   right: %d to %d\n",
                   y, xl, xr, lcur, rcur, vtop, vbot,
                   scrcoords[lcur][0], scrcoords[lnext][0],
                   scrcoords[rcur][0], scrcoords[rnext][0]);*/

        s32 zl = scrcoords[lcur][2] + (((s64)(scrcoords[lnext][2] - scrcoords[lcur][2]) * lfactor) >> 12);
        s32 zr = scrcoords[rcur][2] + (((s64)(scrcoords[rnext][2] - scrcoords[rcur][2]) * rfactor) >> 12);

        //s32 wl = scrcoords[lcur][3] + (((s64)(scrcoords[lnext][3] - scrcoords[lcur][3]) * lfactor) >> 12);
        //s32 wr = scrcoords[rcur][3] + (((s64)(scrcoords[rnext][3] - scrcoords[rcur][3]) * rfactor) >> 12);

        u8 rl = vlcur->Color[0] + (((vlnext->Color[0] - vlcur->Color[0]) * lfactor) >> 12);
        u8 gl = vlcur->Color[1] + (((vlnext->Color[1] - vlcur->Color[1]) * lfactor) >> 12);
        u8 bl = vlcur->Color[2] + (((vlnext->Color[2] - vlcur->Color[2]) * lfactor) >> 12);

        u8 rr = vrcur->Color[0] + (((vrnext->Color[0] - vrcur->Color[0]) * rfactor) >> 12);
        u8 gr = vrcur->Color[1] + (((vrnext->Color[1] - vrcur->Color[1]) * rfactor) >> 12);
        u8 br = vrcur->Color[2] + (((vrnext->Color[2] - vrcur->Color[2]) * rfactor) >> 12);

        s32 xdiv;
        if (xr == xl)
            xdiv = 0;
        else
            xdiv = 0x1000 / (xr - xl);

        for (s32 x = xl; x <= xr; x++)
        {
            s32 xfactor = (x - xl) * xdiv;

            s32 z = zl + (((s64)(zr - zl) * xfactor) >> 12);

            //s32 z = (((zr - zl) * xfactor) >> 12);
            //if (zr!=zl) z = (z << 12) / (zr - zl);
            //s32 w = wl + (((s64)(wr - wl) * xfactor) >> 12);
            //w >>= 12;
            //if (w!=0) xfactor = ((s64)xfactor * 0xFFFFFF) / w;
            //xfactor = (xfactor * w) >> 12;

            //s32 z_inv = ((z>>12)==0) ? 0x1000 : 0x1000000 / (z >> 12);
            //xfactor = (xfactor * z_inv) >> 12;
            //if (z) xfactor = (xfactor << 12) / z;

            // TODO: get rid of this shit
            if (x<0 || x>255 || y<0 || y>191)
            {
                //printf("BAD COORDS!! %d %d\n", x, y);
                x = 0; y = 0;
            }

            // possible optimization: only do color interpolation if the depth test passes
            u8 vr = rl + (((rr - rl) * xfactor) >> 12);
            u8 vg = gl + (((gr - gl) * xfactor) >> 12);
            u8 vb = bl + (((br - bl) * xfactor) >> 12);
            RenderPixel(polygon->Attr, x, y, z, vr, vg, vb);

            // Z debug
            /*u8 zerp = (w * 63) / 0xFFFFFF;
            pixel[0] = zerp;
            pixel[1] = zerp;
            pixel[2] = zerp;*/
        }
    }

    // DEBUG CODE
    /*for (int i = 0; i < nverts; i++)
    {
        s32 x = scrcoords[i][0];
        s32 y = scrcoords[i][1];

        u8* pixel = &ColorBuffer[((256*y) + x) * 4];
            pixel[0] = 63;
            pixel[1] = 63;
            pixel[2] = 63;
            pixel[3] = 31;
    }*/
}

void RenderFrame(Vertex* vertices, Polygon* polygons, int npolys)
{
    // TODO: render translucent polygons last

    // TODO proper clear color/depth support!
    for (int i = 0; i < 256*192; i++)
    {
        ((u32*)ColorBuffer)[i] = 0x00000000;
        DepthBuffer[i] = 0xFFFFFF;
    }

    for (int i = 0; i < npolys; i++)
    {
        /*printf("polygon %d: %d %d %d\n", i, polygons[i].Vertices[0]->Color[0], polygons[i].Vertices[0]->Color[1], polygons[i].Vertices[0]->Color[2]);
        for (int j = 0; j < polygons[i].NumVertices; j++)
            printf("  %d: %f %f %f\n",
                   j,
                   polygons[i].Vertices[j]->Position[0]/4096.0f,
                   polygons[i].Vertices[j]->Position[1]/4096.0f,
                   polygons[i].Vertices[j]->Position[2]/4096.0f);
*/
        //printf("polygon %d\n", i);
        //if (!polygons[i].Vertices[0]->Clipped) continue;
        //printf("polygon %d\n", i);
        RenderPolygon(&polygons[i]);
    }
}

u8* GetLine(int line)
{
    return &ColorBuffer[line * 256 * 4];
}

}
}