Path: blob/master/libmupen64plus/mupen64plus-video-glide64mk2/src/Glide64/turbo3D.h
2 views
/*1* Glide64 - Glide video plugin for Nintendo 64 emulators.2* Copyright (c) 2002 Dave20013* Copyright (c) 2003-2009 Sergey 'Gonetz' Lipski4*5* This program is free software; you can redistribute it and/or modify6* it under the terms of the GNU General Public License as published by7* the Free Software Foundation; either version 2 of the License, or8* any later version.9*10* This program is distributed in the hope that it will be useful,11* but WITHOUT ANY WARRANTY; without even the implied warranty of12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the13* GNU General Public License for more details.14*15* You should have received a copy of the GNU General Public License16* along with this program; if not, write to the Free Software17* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA18*/1920//****************************************************************21//22// Glide64 - Glide Plugin for Nintendo 64 emulators23// Project started on December 29th, 200124//25// Authors:26// Dave2001, original author, founded the project in 2001, left it in 200227// Gugaman, joined the project in 2002, left it in 200228// Sergey 'Gonetz' Lipski, joined the project in 2002, main author since fall of 200229// Hiroshi 'KoolSmoky' Morii, joined the project in 200730//31//****************************************************************32//33// To modify Glide64:34// * 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.35// * 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.36//37//****************************************************************38//39// Created by Gonetz, 200840//41//****************************************************************4243/******************Turbo3D microcode*************************/4445struct t3dGlobState {46wxUint16 pad0;47wxUint16 perspNorm;48wxUint32 flag;49wxUint32 othermode0;50wxUint32 othermode1;51wxUint32 segBases[16];52/* the viewport to use */53short vsacle1;54short vsacle0;55short vsacle3;56short vsacle2;57short vtrans1;58short vtrans0;59short vtrans3;60short vtrans2;61wxUint32 rdpCmds;62};6364struct t3dState {65wxUint32 renderState; /* render state */66wxUint32 textureState; /* texture state */67wxUint8 flag;68wxUint8 triCount; /* how many tris? */69wxUint8 vtxV0; /* where to load verts? */70wxUint8 vtxCount; /* how many verts? */71wxUint32 rdpCmds; /* ptr (segment address) to RDP DL */72wxUint32 othermode0;73wxUint32 othermode1;74};757677struct t3dTriN{78wxUint8 flag, v2, v1, v0; /* flag is which one for flat shade */79};808182static void t3dProcessRDP(wxUint32 a)83{84if (a)85{86rdp.LLE = 1;87rdp.cmd0 = ((wxUint32*)gfx.RDRAM)[a++];88rdp.cmd1 = ((wxUint32*)gfx.RDRAM)[a++];89while (rdp.cmd0 + rdp.cmd1) {90gfx_instruction[0][rdp.cmd0>>24] ();91rdp.cmd0 = ((wxUint32*)gfx.RDRAM)[a++];92rdp.cmd1 = ((wxUint32*)gfx.RDRAM)[a++];93wxUint32 cmd = rdp.cmd0>>24;94if (cmd == 0xE4 || cmd == 0xE5)95{96rdp.cmd2 = ((wxUint32*)gfx.RDRAM)[a++];97rdp.cmd3 = ((wxUint32*)gfx.RDRAM)[a++];98}99}100rdp.LLE = 0;101}102}103104static void t3dLoadGlobState(wxUint32 pgstate)105{106t3dGlobState *gstate = (t3dGlobState*)&gfx.RDRAM[segoffset(pgstate)];107FRDP ("Global state. pad0: %04lx, perspNorm: %04lx, flag: %08lx\n", gstate->pad0, gstate->perspNorm, gstate->flag);108rdp.cmd0 = gstate->othermode0;109rdp.cmd1 = gstate->othermode1;110rdp_setothermode();111112for (int s = 0; s < 16; s++)113{114rdp.segment[s] = gstate->segBases[s];115FRDP ("segment: %08lx -> seg%d\n", rdp.segment[s], s);116}117118short scale_x = gstate->vsacle0 / 4;119short scale_y = gstate->vsacle1 / 4;;120short scale_z = gstate->vsacle2;121short trans_x = gstate->vtrans0 / 4;122short trans_y = gstate->vtrans1 / 4;123short trans_z = gstate->vtrans2;124rdp.view_scale[0] = scale_x * rdp.scale_x;125rdp.view_scale[1] = -scale_y * rdp.scale_y;126rdp.view_scale[2] = 32.0f * scale_z;127rdp.view_trans[0] = trans_x * rdp.scale_x;128rdp.view_trans[1] = trans_y * rdp.scale_y;129rdp.view_trans[2] = 32.0f * trans_z;130rdp.update |= UPDATE_VIEWPORT;131FRDP ("viewport scale(%d, %d, %d), trans(%d, %d, %d)\n", scale_x, scale_y, scale_z,132trans_x, trans_y, trans_z);133134t3dProcessRDP(segoffset(gstate->rdpCmds) >> 2);135}136137static void t3d_vertex(wxUint32 addr, wxUint32 v0, wxUint32 n)138{139float x, y, z;140141rdp.v0 = v0; // Current vertex142rdp.vn = n; // Number of vertices to copy143n <<= 4;144145for (wxUint32 i=0; i < n; i+=16)146{147VERTEX *v = &rdp.vtx[v0 + (i>>4)];148x = (float)((short*)gfx.RDRAM)[(((addr+i) >> 1) + 0)^1];149y = (float)((short*)gfx.RDRAM)[(((addr+i) >> 1) + 1)^1];150z = (float)((short*)gfx.RDRAM)[(((addr+i) >> 1) + 2)^1];151v->flags = ((wxUint16*)gfx.RDRAM)[(((addr+i) >> 1) + 3)^1];152v->ou = 2.0f * (float)((short*)gfx.RDRAM)[(((addr+i) >> 1) + 4)^1];153v->ov = 2.0f * (float)((short*)gfx.RDRAM)[(((addr+i) >> 1) + 5)^1];154v->uv_scaled = 0;155v->r = ((wxUint8*)gfx.RDRAM)[(addr+i + 12)^3];156v->g = ((wxUint8*)gfx.RDRAM)[(addr+i + 13)^3];157v->b = ((wxUint8*)gfx.RDRAM)[(addr+i + 14)^3];158v->a = ((wxUint8*)gfx.RDRAM)[(addr+i + 15)^3];159160v->x = x*rdp.combined[0][0] + y*rdp.combined[1][0] + z*rdp.combined[2][0] + rdp.combined[3][0];161v->y = x*rdp.combined[0][1] + y*rdp.combined[1][1] + z*rdp.combined[2][1] + rdp.combined[3][1];162v->z = x*rdp.combined[0][2] + y*rdp.combined[1][2] + z*rdp.combined[2][2] + rdp.combined[3][2];163v->w = x*rdp.combined[0][3] + y*rdp.combined[1][3] + z*rdp.combined[2][3] + rdp.combined[3][3];164165if (fabs(v->w) < 0.001) v->w = 0.001f;166v->oow = 1.0f / v->w;167v->x_w = v->x * v->oow;168v->y_w = v->y * v->oow;169v->z_w = v->z * v->oow;170171v->uv_calculated = 0xFFFFFFFF;172v->screen_translated = 0;173v->shade_mod = 0;174175v->scr_off = 0;176if (v->x < -v->w) v->scr_off |= 1;177if (v->x > v->w) v->scr_off |= 2;178if (v->y < -v->w) v->scr_off |= 4;179if (v->y > v->w) v->scr_off |= 8;180if (v->w < 0.1f) v->scr_off |= 16;181#ifdef EXTREME_LOGGING182FRDP ("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);183#endif184}185}186187static void t3dLoadObject(wxUint32 pstate, wxUint32 pvtx, wxUint32 ptri)188{189LRDP("Loading Turbo3D object\n");190t3dState *ostate = (t3dState*)&gfx.RDRAM[segoffset(pstate)];191rdp.cur_tile = (ostate->textureState)&7;192FRDP("tile: %d\n", rdp.cur_tile);193if (rdp.tiles[rdp.cur_tile].s_scale < 0.001f)194rdp.tiles[rdp.cur_tile].s_scale = 0.015625;195if (rdp.tiles[rdp.cur_tile].t_scale < 0.001f)196rdp.tiles[rdp.cur_tile].t_scale = 0.015625;197198#ifdef EXTREME_LOGGING199FRDP("renderState: %08lx, textureState: %08lx, othermode0: %08lx, othermode1: %08lx, rdpCmds: %08lx, triCount : %d, v0: %d, vn: %d\n", ostate->renderState, ostate->textureState,200ostate->othermode0, ostate->othermode1, ostate->rdpCmds, ostate->triCount, ostate->vtxV0, ostate->vtxCount);201#endif202203rdp.cmd0 = ostate->othermode0;204rdp.cmd1 = ostate->othermode1;205rdp_setothermode();206207rdp.cmd1 = ostate->renderState;208uc0_setgeometrymode();209210if (!(ostate->flag&1)) //load matrix211{212wxUint32 addr = segoffset(pstate+sizeof(t3dState)) & BMASK;213load_matrix(rdp.combined, addr);214#ifdef EXTREME_LOGGING215FRDP ("{%f,%f,%f,%f}\n", rdp.combined[0][0], rdp.combined[0][1], rdp.combined[0][2], rdp.combined[0][3]);216FRDP ("{%f,%f,%f,%f}\n", rdp.combined[1][0], rdp.combined[1][1], rdp.combined[1][2], rdp.combined[1][3]);217FRDP ("{%f,%f,%f,%f}\n", rdp.combined[2][0], rdp.combined[2][1], rdp.combined[2][2], rdp.combined[2][3]);218FRDP ("{%f,%f,%f,%f}\n", rdp.combined[3][0], rdp.combined[3][1], rdp.combined[3][2], rdp.combined[3][3]);219#endif220}221222rdp.geom_mode &= ~0x00020000;223rdp.geom_mode |= 0x00000200;224if (pvtx) //load vtx225t3d_vertex(segoffset(pvtx) & BMASK, ostate->vtxV0, ostate->vtxCount);226227t3dProcessRDP(segoffset(ostate->rdpCmds) >> 2);228229if (ptri)230{231update ();232wxUint32 a = segoffset(ptri);233for (int t=0; t < ostate->triCount; t++)234{235t3dTriN * tri = (t3dTriN*)&gfx.RDRAM[a];236a += 4;237FRDP("tri #%d - %d, %d, %d\n", t, tri->v0, tri->v1, tri->v2);238VERTEX *v[3] = { &rdp.vtx[tri->v0], &rdp.vtx[tri->v1], &rdp.vtx[tri->v2] };239if (cull_tri(v))240rdp.tri_n ++;241else242{243draw_tri (v);244rdp.tri_n ++;245}246}247}248}249250static void Turbo3D()251{252LRDP("Start Turbo3D microcode\n");253settings.ucode = ucode_Fast3D;254wxUint32 a = 0, pgstate = 0, pstate = 0, pvtx = 0, ptri = 0;255do {256a = rdp.pc[rdp.pc_i] & BMASK;257pgstate = ((wxUint32*)gfx.RDRAM)[a>>2];258pstate = ((wxUint32*)gfx.RDRAM)[(a>>2)+1];259pvtx = ((wxUint32*)gfx.RDRAM)[(a>>2)+2];260ptri = ((wxUint32*)gfx.RDRAM)[(a>>2)+3];261FRDP("GlobalState: %08lx, Object: %08lx, Vertices: %08lx, Triangles: %08lx\n", pgstate, pstate, pvtx, ptri);262if (!pstate)263{264rdp.halt = 1;265break;266}267if (pgstate)268t3dLoadGlobState(pgstate);269t3dLoadObject(pstate, pvtx, ptri);270// Go to the next instruction271rdp.pc[rdp.pc_i] += 16;272} while (pstate);273// rdp_fullsync();274settings.ucode = ucode_Turbo3d;275}276277278