2 * Glide64 - Glide video plugin for Nintendo 64 emulators.
3 * Copyright (c) 2002 Dave2001
4 * Copyright (c) 2003-2009 Sergey 'Gonetz' Lipski
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 //****************************************************************
23 // Glide64 - Glide Plugin for Nintendo 64 emulators
24 // Project started on December 29th, 2001
27 // Dave2001, original author, founded the project in 2001, left it in 2002
28 // Gugaman, joined the project in 2002, left it in 2002
29 // Sergey 'Gonetz' Lipski, joined the project in 2002, main author since fall of 2002
30 // Hiroshi 'KoolSmoky' Morii, joined the project in 2007
32 //****************************************************************
35 // * Write your name and (optional)email, commented by your work, so I know who did it, and so that you can find which parts you modified when it comes time to send it to me.
36 // * Do NOT send me the whole project or file that you modified. Take out your modified code sections, and tell me where to put them. If people sent the whole thing, I would have many different versions, but no idea how to combine them all.
38 //****************************************************************
40 // Created by Gonetz, 2008
42 //****************************************************************
44 /******************Turbo3D microcode*************************/
52 wxUint32 segBases[16];
53 /* the viewport to use */
66 wxUint32 renderState; /* render state */
67 wxUint32 textureState; /* texture state */
69 wxUint8 triCount; /* how many tris? */
70 wxUint8 vtxV0; /* where to load verts? */
71 wxUint8 vtxCount; /* how many verts? */
72 wxUint32 rdpCmds; /* ptr (segment address) to RDP DL */
79 wxUint8 flag, v2, v1, v0; /* flag is which one for flat shade */
83 static void t3dProcessRDP(wxUint32 a)
88 rdp.cmd0 = ((wxUint32*)gfx.RDRAM)[a++];
89 rdp.cmd1 = ((wxUint32*)gfx.RDRAM)[a++];
90 while (rdp.cmd0 + rdp.cmd1) {
91 gfx_instruction[0][rdp.cmd0>>24] ();
92 rdp.cmd0 = ((wxUint32*)gfx.RDRAM)[a++];
93 rdp.cmd1 = ((wxUint32*)gfx.RDRAM)[a++];
94 wxUint32 cmd = rdp.cmd0>>24;
95 if (cmd == 0xE4 || cmd == 0xE5)
97 rdp.cmd2 = ((wxUint32*)gfx.RDRAM)[a++];
98 rdp.cmd3 = ((wxUint32*)gfx.RDRAM)[a++];
105 static void t3dLoadGlobState(wxUint32 pgstate)
107 t3dGlobState *gstate = (t3dGlobState*)&gfx.RDRAM[segoffset(pgstate)];
108 FRDP ("Global state. pad0: %04lx, perspNorm: %04lx, flag: %08lx\n", gstate->pad0, gstate->perspNorm, gstate->flag);
109 rdp.cmd0 = gstate->othermode0;
110 rdp.cmd1 = gstate->othermode1;
113 for (int s = 0; s < 16; s++)
115 rdp.segment[s] = gstate->segBases[s];
116 FRDP ("segment: %08lx -> seg%d\n", rdp.segment[s], s);
119 short scale_x = gstate->vsacle0 / 4;
120 short scale_y = gstate->vsacle1 / 4;;
121 short scale_z = gstate->vsacle2;
122 short trans_x = gstate->vtrans0 / 4;
123 short trans_y = gstate->vtrans1 / 4;
124 short trans_z = gstate->vtrans2;
125 rdp.view_scale[0] = scale_x * rdp.scale_x;
126 rdp.view_scale[1] = -scale_y * rdp.scale_y;
127 rdp.view_scale[2] = 32.0f * scale_z;
128 rdp.view_trans[0] = trans_x * rdp.scale_x;
129 rdp.view_trans[1] = trans_y * rdp.scale_y;
130 rdp.view_trans[2] = 32.0f * trans_z;
131 rdp.update |= UPDATE_VIEWPORT;
132 FRDP ("viewport scale(%d, %d, %d), trans(%d, %d, %d)\n", scale_x, scale_y, scale_z,
133 trans_x, trans_y, trans_z);
135 t3dProcessRDP(segoffset(gstate->rdpCmds) >> 2);
138 static void t3d_vertex(wxUint32 addr, wxUint32 v0, wxUint32 n)
142 rdp.v0 = v0; // Current vertex
143 rdp.vn = n; // Number of vertices to copy
146 for (wxUint32 i=0; i < n; i+=16)
148 VERTEX *v = &rdp.vtx[v0 + (i>>4)];
149 x = (float)((short*)gfx.RDRAM)[(((addr+i) >> 1) + 0)^1];
150 y = (float)((short*)gfx.RDRAM)[(((addr+i) >> 1) + 1)^1];
151 z = (float)((short*)gfx.RDRAM)[(((addr+i) >> 1) + 2)^1];
152 v->flags = ((wxUint16*)gfx.RDRAM)[(((addr+i) >> 1) + 3)^1];
153 v->ou = 2.0f * (float)((short*)gfx.RDRAM)[(((addr+i) >> 1) + 4)^1];
154 v->ov = 2.0f * (float)((short*)gfx.RDRAM)[(((addr+i) >> 1) + 5)^1];
156 v->r = ((wxUint8*)gfx.RDRAM)[(addr+i + 12)^3];
157 v->g = ((wxUint8*)gfx.RDRAM)[(addr+i + 13)^3];
158 v->b = ((wxUint8*)gfx.RDRAM)[(addr+i + 14)^3];
159 v->a = ((wxUint8*)gfx.RDRAM)[(addr+i + 15)^3];
161 v->x = x*rdp.combined[0][0] + y*rdp.combined[1][0] + z*rdp.combined[2][0] + rdp.combined[3][0];
162 v->y = x*rdp.combined[0][1] + y*rdp.combined[1][1] + z*rdp.combined[2][1] + rdp.combined[3][1];
163 v->z = x*rdp.combined[0][2] + y*rdp.combined[1][2] + z*rdp.combined[2][2] + rdp.combined[3][2];
164 v->w = x*rdp.combined[0][3] + y*rdp.combined[1][3] + z*rdp.combined[2][3] + rdp.combined[3][3];
166 if (fabs(v->w) < 0.001) v->w = 0.001f;
167 v->oow = 1.0f / v->w;
168 v->x_w = v->x * v->oow;
169 v->y_w = v->y * v->oow;
170 v->z_w = v->z * v->oow;
172 v->uv_calculated = 0xFFFFFFFF;
173 v->screen_translated = 0;
177 if (v->x < -v->w) v->scr_off |= 1;
178 if (v->x > v->w) v->scr_off |= 2;
179 if (v->y < -v->w) v->scr_off |= 4;
180 if (v->y > v->w) v->scr_off |= 8;
181 if (v->w < 0.1f) v->scr_off |= 16;
182 #ifdef EXTREME_LOGGING
183 FRDP ("v%d - x: %f, y: %f, z: %f, w: %f, u: %f, v: %f, f: %f, z_w: %f, r=%d, g=%d, b=%d, a=%d\n", i>>4, v->x, v->y, v->z, v->w, v->ou*rdp.tiles[rdp.cur_tile].s_scale, v->ov*rdp.tiles[rdp.cur_tile].t_scale, v->f, v->z_w, v->r, v->g, v->b, v->a);
188 static void t3dLoadObject(wxUint32 pstate, wxUint32 pvtx, wxUint32 ptri)
190 LRDP("Loading Turbo3D object\n");
191 t3dState *ostate = (t3dState*)&gfx.RDRAM[segoffset(pstate)];
192 rdp.cur_tile = (ostate->textureState)&7;
193 FRDP("tile: %d\n", rdp.cur_tile);
194 if (rdp.tiles[rdp.cur_tile].s_scale < 0.001f)
195 rdp.tiles[rdp.cur_tile].s_scale = 0.015625;
196 if (rdp.tiles[rdp.cur_tile].t_scale < 0.001f)
197 rdp.tiles[rdp.cur_tile].t_scale = 0.015625;
199 #ifdef EXTREME_LOGGING
200 FRDP("renderState: %08lx, textureState: %08lx, othermode0: %08lx, othermode1: %08lx, rdpCmds: %08lx, triCount : %d, v0: %d, vn: %d\n", ostate->renderState, ostate->textureState,
201 ostate->othermode0, ostate->othermode1, ostate->rdpCmds, ostate->triCount, ostate->vtxV0, ostate->vtxCount);
204 rdp.cmd0 = ostate->othermode0;
205 rdp.cmd1 = ostate->othermode1;
208 rdp.cmd1 = ostate->renderState;
209 uc0_setgeometrymode();
211 if (!(ostate->flag&1)) //load matrix
213 wxUint32 addr = segoffset(pstate+sizeof(t3dState)) & BMASK;
214 load_matrix(rdp.combined, addr);
215 #ifdef EXTREME_LOGGING
216 FRDP ("{%f,%f,%f,%f}\n", rdp.combined[0][0], rdp.combined[0][1], rdp.combined[0][2], rdp.combined[0][3]);
217 FRDP ("{%f,%f,%f,%f}\n", rdp.combined[1][0], rdp.combined[1][1], rdp.combined[1][2], rdp.combined[1][3]);
218 FRDP ("{%f,%f,%f,%f}\n", rdp.combined[2][0], rdp.combined[2][1], rdp.combined[2][2], rdp.combined[2][3]);
219 FRDP ("{%f,%f,%f,%f}\n", rdp.combined[3][0], rdp.combined[3][1], rdp.combined[3][2], rdp.combined[3][3]);
223 rdp.geom_mode &= ~0x00020000;
224 rdp.geom_mode |= 0x00000200;
226 t3d_vertex(segoffset(pvtx) & BMASK, ostate->vtxV0, ostate->vtxCount);
228 t3dProcessRDP(segoffset(ostate->rdpCmds) >> 2);
233 wxUint32 a = segoffset(ptri);
234 for (int t=0; t < ostate->triCount; t++)
236 t3dTriN * tri = (t3dTriN*)&gfx.RDRAM[a];
238 FRDP("tri #%d - %d, %d, %d\n", t, tri->v0, tri->v1, tri->v2);
239 VERTEX *v[3] = { &rdp.vtx[tri->v0], &rdp.vtx[tri->v1], &rdp.vtx[tri->v2] };
251 static void Turbo3D()
253 LRDP("Start Turbo3D microcode\n");
254 settings.ucode = ucode_Fast3D;
255 wxUint32 a = 0, pgstate = 0, pstate = 0, pvtx = 0, ptri = 0;
257 a = rdp.pc[rdp.pc_i] & BMASK;
258 pgstate = ((wxUint32*)gfx.RDRAM)[a>>2];
259 pstate = ((wxUint32*)gfx.RDRAM)[(a>>2)+1];
260 pvtx = ((wxUint32*)gfx.RDRAM)[(a>>2)+2];
261 ptri = ((wxUint32*)gfx.RDRAM)[(a>>2)+3];
262 FRDP("GlobalState: %08lx, Object: %08lx, Vertices: %08lx, Triangles: %08lx\n", pgstate, pstate, pvtx, ptri);
269 t3dLoadGlobState(pgstate);
270 t3dLoadObject(pstate, pvtx, ptri);
271 // Go to the next instruction
272 rdp.pc[rdp.pc_i] += 16;
275 settings.ucode = ucode_Turbo3d;