project64/Source/Project64-video/ucode05.cpp

348 lines
11 KiB
C++
Raw Permalink Normal View History

2021-03-02 02:13:17 +00:00
// Project64 - A Nintendo 64 emulator
// http://www.pj64-emu.com/
// Copyright(C) 2001-2021 Project64
// Copyright(C) 2003-2009 Sergey 'Gonetz' Lipski
// Copyright(C) 2002 Dave2001
// GNU/GPLv2 licensed: https://gnu.org/licenses/gpl-2.0.html
#include <Project64-video/Renderer/Renderer.h>
#include <Project64-video/rdp.h>
#include <Project64-video/Gfx_1.3.h>
#include <Project64-video/trace.h>
#include <Project64-video/ucode.h>
#include <math.h>
#include "3dmath.h"
#include "ucode00.h"
#include "util.h"
2013-04-04 21:22:19 +00:00
int cur_mtx = 0;
int billboarding = 0;
int vtx_last = 0;
uint32_t dma_offset_mtx = 0;
uint32_t dma_offset_vtx = 0;
2013-04-04 21:22:19 +00:00
void uc5_dma_offsets()
2013-04-04 21:22:19 +00:00
{
2016-01-25 11:14:01 +00:00
dma_offset_mtx = rdp.cmd0 & 0x00FFFFFF;
dma_offset_vtx = rdp.cmd1 & 0x00FFFFFF;
vtx_last = 0;
WriteTrace(TraceRDP, TraceDebug, "uc5:dma_offsets - mtx: %08lx, vtx: %08lx", dma_offset_mtx, dma_offset_vtx);
2013-04-04 21:22:19 +00:00
}
void uc5_matrix()
2013-04-04 21:22:19 +00:00
{
2016-01-25 11:14:01 +00:00
// Use segment offset to get the address
uint32_t addr = dma_offset_mtx + (segoffset(rdp.cmd1) & BMASK);
uint8_t n = (uint8_t)((rdp.cmd0 >> 16) & 0xF);
uint8_t multiply;
if (n == 0) //DKR
{
n = (uint8_t)((rdp.cmd0 >> 22) & 0x3);
multiply = 0;
}
else //JF
{
multiply = (uint8_t)((rdp.cmd0 >> 23) & 0x1);
}
cur_mtx = n;
WriteTrace(TraceRDP, TraceDebug, "uc5:matrix - #%d, addr: %08lx", n, addr);
2016-01-25 11:14:01 +00:00
if (multiply)
{
DECLAREALIGN16VAR(m[4][4]);
load_matrix(m, addr);
DECLAREALIGN16VAR(m_src[4][4]);
memcpy(m_src, rdp.dkrproj[0], 64);
MulMatrices(m, m_src, rdp.dkrproj[n]);
}
else
{
load_matrix(rdp.dkrproj[n], addr);
}
rdp.update |= UPDATE_MULT_MAT;
2013-04-04 21:22:19 +00:00
WriteTrace(TraceRDP, TraceVerbose, "{%f,%f,%f,%f}", rdp.dkrproj[n][0][0], rdp.dkrproj[n][0][1], rdp.dkrproj[n][0][2], rdp.dkrproj[n][0][3]);
WriteTrace(TraceRDP, TraceVerbose, "{%f,%f,%f,%f}", rdp.dkrproj[n][1][0], rdp.dkrproj[n][1][1], rdp.dkrproj[n][1][2], rdp.dkrproj[n][1][3]);
WriteTrace(TraceRDP, TraceVerbose, "{%f,%f,%f,%f}", rdp.dkrproj[n][2][0], rdp.dkrproj[n][2][1], rdp.dkrproj[n][2][2], rdp.dkrproj[n][2][3]);
WriteTrace(TraceRDP, TraceVerbose, "{%f,%f,%f,%f}", rdp.dkrproj[n][3][0], rdp.dkrproj[n][3][1], rdp.dkrproj[n][3][2], rdp.dkrproj[n][3][3]);
2016-01-25 11:14:01 +00:00
for (int i = 0; i < 3; i++)
2016-01-25 11:14:01 +00:00
{
WriteTrace(TraceRDP, TraceVerbose, "proj %d", i);
WriteTrace(TraceRDP, TraceVerbose, "{%f,%f,%f,%f}", rdp.dkrproj[i][0][0], rdp.dkrproj[i][0][1], rdp.dkrproj[i][0][2], rdp.dkrproj[i][0][3]);
WriteTrace(TraceRDP, TraceVerbose, "{%f,%f,%f,%f}", rdp.dkrproj[i][1][0], rdp.dkrproj[i][1][1], rdp.dkrproj[i][1][2], rdp.dkrproj[i][1][3]);
WriteTrace(TraceRDP, TraceVerbose, "{%f,%f,%f,%f}", rdp.dkrproj[i][2][0], rdp.dkrproj[i][2][1], rdp.dkrproj[i][2][2], rdp.dkrproj[i][2][3]);
WriteTrace(TraceRDP, TraceVerbose, "{%f,%f,%f,%f}", rdp.dkrproj[i][3][0], rdp.dkrproj[i][3][1], rdp.dkrproj[i][3][2], rdp.dkrproj[i][3][3]);
2016-01-25 11:14:01 +00:00
}
2013-04-04 21:22:19 +00:00
}
void uc5_vertex()
2013-04-04 21:22:19 +00:00
{
2016-01-25 11:14:01 +00:00
uint32_t addr = dma_offset_vtx + (segoffset(rdp.cmd1) & BMASK);
2013-04-04 21:22:19 +00:00
2016-01-25 11:14:01 +00:00
// | cccc cccc 1111 1??? 0000 0002 2222 2222 | cmd1 = address |
// c = vtx command
// 1 = method #1 of getting count
// 2 = method #2 of getting count
// ? = unknown, but used
// 0 = unused
2013-04-04 21:22:19 +00:00
2016-01-25 11:14:01 +00:00
int n = ((rdp.cmd0 >> 19) & 0x1F);// + 1;
if (g_settings->hacks(CSettings::hack_Diddy))
2016-01-25 11:14:01 +00:00
n++;
2013-04-04 21:22:19 +00:00
2016-01-25 11:14:01 +00:00
if (rdp.cmd0 & 0x00010000)
2013-04-04 21:22:19 +00:00
{
2016-01-25 11:14:01 +00:00
if (billboarding)
vtx_last = 1;
2013-04-04 21:22:19 +00:00
}
2016-01-25 11:14:01 +00:00
else
vtx_last = 0;
int first = ((rdp.cmd0 >> 9) & 0x1F) + vtx_last;
WriteTrace(TraceRDP, TraceDebug, "uc5:vertex - addr: %08lx, first: %d, count: %d, matrix: %08lx", addr, first, n, cur_mtx);
2016-01-25 11:14:01 +00:00
int prj = cur_mtx;
2013-04-04 21:22:19 +00:00
2016-01-25 11:14:01 +00:00
int start = 0;
float x, y, z;
for (int i = first; i < first + n; i++)
{
start = (i - first) * 10;
gfxVERTEX &v = rdp.vtx(i);
2016-01-25 11:14:01 +00:00
x = (float)((short*)gfx.RDRAM)[(((addr + start) >> 1) + 0) ^ 1];
y = (float)((short*)gfx.RDRAM)[(((addr + start) >> 1) + 1) ^ 1];
z = (float)((short*)gfx.RDRAM)[(((addr + start) >> 1) + 2) ^ 1];
2017-05-19 20:03:52 +00:00
v.x = x*rdp.dkrproj[prj][0][0] + y*rdp.dkrproj[prj][1][0] + z*rdp.dkrproj[prj][2][0] + rdp.dkrproj[prj][3][0];
v.y = x*rdp.dkrproj[prj][0][1] + y*rdp.dkrproj[prj][1][1] + z*rdp.dkrproj[prj][2][1] + rdp.dkrproj[prj][3][1];
v.z = x*rdp.dkrproj[prj][0][2] + y*rdp.dkrproj[prj][1][2] + z*rdp.dkrproj[prj][2][2] + rdp.dkrproj[prj][3][2];
v.w = x*rdp.dkrproj[prj][0][3] + y*rdp.dkrproj[prj][1][3] + z*rdp.dkrproj[prj][2][3] + rdp.dkrproj[prj][3][3];
2016-01-25 11:14:01 +00:00
if (billboarding)
{
2017-05-19 20:03:52 +00:00
v.x += rdp.vtx(0).x;
v.y += rdp.vtx(0).y;
v.z += rdp.vtx(0).z;
v.w += rdp.vtx(0).w;
2016-01-25 11:14:01 +00:00
}
2017-05-19 20:03:52 +00:00
if (fabs(v.w) < 0.001) v.w = 0.001f;
v.oow = 1.0f / v.w;
v.x_w = v.x * v.oow;
v.y_w = v.y * v.oow;
v.z_w = v.z * v.oow;
v.uv_calculated = 0xFFFFFFFF;
v.screen_translated = 0;
v.shade_mod = 0;
v.scr_off = 0;
if (v.x < -v.w) v.scr_off |= 1;
if (v.x > v.w) v.scr_off |= 2;
if (v.y < -v.w) v.scr_off |= 4;
if (v.y > v.w) v.scr_off |= 8;
if (v.w < 0.1f) v.scr_off |= 16;
if (fabs(v.z_w) > 1.0) v.scr_off |= 32;
v.r = ((uint8_t*)gfx.RDRAM)[(addr + start + 6) ^ 3];
v.g = ((uint8_t*)gfx.RDRAM)[(addr + start + 7) ^ 3];
v.b = ((uint8_t*)gfx.RDRAM)[(addr + start + 8) ^ 3];
v.a = ((uint8_t*)gfx.RDRAM)[(addr + start + 9) ^ 3];
2016-01-25 11:14:01 +00:00
CalculateFog(v);
2013-04-04 21:22:19 +00:00
2017-05-19 20:03:52 +00:00
WriteTrace(TraceRDP, TraceVerbose, "v%d - x: %f, y: %f, z: %f, w: %f, z_w: %f, r=%d, g=%d, b=%d, a=%d", i, v.x, v.y, v.z, v.w, v.z_w, v.r, v.g, v.b, v.a);
2016-01-25 11:14:01 +00:00
}
2013-04-04 21:22:19 +00:00
2016-01-25 11:14:01 +00:00
vtx_last += n;
2013-04-04 21:22:19 +00:00
}
void uc5_tridma()
2013-04-04 21:22:19 +00:00
{
2016-01-25 11:14:01 +00:00
vtx_last = 0; // we've drawn something, so the vertex index needs resetting
if (rdp.skip_drawing)
return;
// | cccc cccc 2222 0000 1111 1111 1111 0000 | cmd1 = address |
// c = tridma command
// 1 = method #1 of getting count
// 2 = method #2 of getting count
// 0 = unused
uint32_t addr = segoffset(rdp.cmd1) & BMASK;
int num = (rdp.cmd0 & 0xFFF0) >> 4;
//int num = ((rdp.cmd0 & 0x00F00000) >> 20) + 1; // same thing!
WriteTrace(TraceRDP, TraceDebug, "uc5:tridma #%d - addr: %08lx, count: %d", rdp.tri_n, addr, num);
2016-01-25 11:14:01 +00:00
int start, v0, v1, v2, flags;
for (int i = 0; i < num; i++)
2013-04-04 21:22:19 +00:00
{
2016-01-25 11:14:01 +00:00
start = i << 4;
v0 = gfx.RDRAM[addr + start];
v1 = gfx.RDRAM[addr + start + 1];
v2 = gfx.RDRAM[addr + start + 2];
WriteTrace(TraceRDP, TraceDebug, "tri #%d - %d, %d, %d", rdp.tri_n, v0, v1, v2);
2016-01-25 11:14:01 +00:00
gfxVERTEX *vtx[3] = {
2017-05-19 20:03:52 +00:00
&rdp.vtx(v0),
&rdp.vtx(v1),
&rdp.vtx(v2)
2016-01-25 11:14:01 +00:00
};
flags = gfx.RDRAM[addr + start + 3];
if (flags & 0x40) { // no cull
rdp.flags &= ~CULLMASK;
gfxCullMode(GFX_CULL_DISABLE);
2016-01-25 11:14:01 +00:00
}
else { // front cull
rdp.flags &= ~CULLMASK;
if (rdp.view_scale[0] < 0) {
rdp.flags |= CULL_BACK; // agh, backwards culling
gfxCullMode(GFX_CULL_POSITIVE);
2016-01-25 11:14:01 +00:00
}
else {
rdp.flags |= CULL_FRONT;
gfxCullMode(GFX_CULL_NEGATIVE);
2016-01-25 11:14:01 +00:00
}
}
start += 4;
2017-05-19 20:03:52 +00:00
vtx[0]->ou = (float)((short*)gfx.RDRAM)[((addr + start) >> 1) + 5] / 32.0f;
vtx[0]->ov = (float)((short*)gfx.RDRAM)[((addr + start) >> 1) + 4] / 32.0f;
vtx[1]->ou = (float)((short*)gfx.RDRAM)[((addr + start) >> 1) + 3] / 32.0f;
vtx[1]->ov = (float)((short*)gfx.RDRAM)[((addr + start) >> 1) + 2] / 32.0f;
vtx[2]->ou = (float)((short*)gfx.RDRAM)[((addr + start) >> 1) + 1] / 32.0f;
vtx[2]->ov = (float)((short*)gfx.RDRAM)[((addr + start) >> 1) + 0] / 32.0f;
2016-01-25 11:14:01 +00:00
2017-05-19 20:03:52 +00:00
vtx[0]->uv_calculated = 0xFFFFFFFF;
vtx[1]->uv_calculated = 0xFFFFFFFF;
vtx[2]->uv_calculated = 0xFFFFFFFF;
2016-01-25 11:14:01 +00:00
2017-05-19 20:03:52 +00:00
if (cull_tri(vtx))
2016-01-25 11:14:01 +00:00
rdp.tri_n++;
else
{
update();
2017-05-19 20:03:52 +00:00
draw_tri(vtx);
2016-01-25 11:14:01 +00:00
rdp.tri_n++;
}
2013-04-04 21:22:19 +00:00
}
}
void uc5_dl_in_mem()
2013-04-04 21:22:19 +00:00
{
2016-01-25 11:14:01 +00:00
uint32_t addr = segoffset(rdp.cmd1) & BMASK;
int count = (rdp.cmd0 & 0x00FF0000) >> 16;
WriteTrace(TraceRDP, TraceDebug, "uc5:dl_in_mem - addr: %08lx, count: %d", addr, count);
2016-01-25 11:14:01 +00:00
if (rdp.pc_i >= 9)
{
WriteTrace(TraceRDP, TraceWarning, "** DL stack overflow **");
2016-01-25 11:14:01 +00:00
return;
}
rdp.pc_i++; // go to the next PC in the stack
rdp.pc[rdp.pc_i] = addr; // jump to the address
rdp.dl_count = count + 1;
2013-04-04 21:22:19 +00:00
}
void uc5_moveword()
2013-04-04 21:22:19 +00:00
{
WriteTrace(TraceRDP, TraceDebug, "uc5:moveword ");
2013-04-04 21:22:19 +00:00
2016-01-25 11:14:01 +00:00
// Find which command this is (lowest byte of cmd0)
switch (rdp.cmd0 & 0xFF)
2013-04-04 21:22:19 +00:00
{
2016-01-25 11:14:01 +00:00
case 0x02: // moveword matrix 2 billboard
billboarding = (rdp.cmd1 & 1);
WriteTrace(TraceRDP, TraceDebug, "matrix billboard - %s", str_offon[billboarding]);
2016-01-25 11:14:01 +00:00
break;
case 0x04: // clip (verified same)
if (((rdp.cmd0 >> 8) & 0xFFFF) == 0x04)
{
rdp.clip_ratio = sqrt((float)rdp.cmd1);
rdp.update |= UPDATE_VIEWPORT;
}
WriteTrace(TraceRDP, TraceDebug, "clip %08lx, %08lx", rdp.cmd0, rdp.cmd1);
2016-01-25 11:14:01 +00:00
break;
case 0x06: // segment (verified same)
WriteTrace(TraceRDP, TraceDebug, "segment: %08lx -> seg%d", rdp.cmd1, (rdp.cmd0 >> 10) & 0x0F);
2016-01-25 11:14:01 +00:00
rdp.segment[(rdp.cmd0 >> 10) & 0x0F] = rdp.cmd1;
break;
case 0x08:
2013-04-04 21:22:19 +00:00
{
2016-01-25 11:14:01 +00:00
rdp.fog_multiplier = (short)(rdp.cmd1 >> 16);
rdp.fog_offset = (short)(rdp.cmd1 & 0x0000FFFF);
WriteTrace(TraceRDP, TraceDebug, "fog: multiplier: %f, offset: %f", rdp.fog_multiplier, rdp.fog_offset);
2016-01-25 11:14:01 +00:00
// rdp.update |= UPDATE_FOG_ENABLED;
2013-04-04 21:22:19 +00:00
}
break;
2016-01-25 11:14:01 +00:00
case 0x0a: // moveword matrix select
cur_mtx = (rdp.cmd1 >> 6) & 3;
WriteTrace(TraceRDP, TraceDebug, "matrix select - mtx: %d", cur_mtx);
2016-01-25 11:14:01 +00:00
break;
2013-04-04 21:22:19 +00:00
2016-01-25 11:14:01 +00:00
default:
WriteTrace(TraceRDP, TraceDebug, "(unknown) %02lx - IGNORED", rdp.cmd0 & 0xFF);
2016-01-25 11:14:01 +00:00
}
2013-04-04 21:22:19 +00:00
}
void uc5_setgeometrymode()
2013-04-04 21:22:19 +00:00
{
WriteTrace(TraceRDP, TraceDebug, "uc0:setgeometrymode %08lx", rdp.cmd1);
2013-04-04 21:22:19 +00:00
2016-01-25 11:14:01 +00:00
rdp.geom_mode |= rdp.cmd1;
2013-04-04 21:22:19 +00:00
2016-01-25 11:14:01 +00:00
if (rdp.cmd1 & 0x00000001) // Z-Buffer enable
2013-04-04 21:22:19 +00:00
{
2016-01-25 11:14:01 +00:00
if (!(rdp.flags & ZBUF_ENABLED))
{
rdp.flags |= ZBUF_ENABLED;
rdp.update |= UPDATE_ZBUF_ENABLED;
}
2013-04-04 21:22:19 +00:00
}
2016-01-25 11:14:01 +00:00
//Added by Gonetz
if (rdp.cmd1 & 0x00010000) // Fog enable
2013-04-04 21:22:19 +00:00
{
2016-01-25 11:14:01 +00:00
if (!(rdp.flags & FOG_ENABLED))
{
rdp.flags |= FOG_ENABLED;
rdp.update |= UPDATE_FOG_ENABLED;
}
2013-04-04 21:22:19 +00:00
}
}
void uc5_cleargeometrymode()
2013-04-04 21:22:19 +00:00
{
WriteTrace(TraceRDP, TraceDebug, "uc0:cleargeometrymode %08lx", rdp.cmd1);
2013-04-04 21:22:19 +00:00
2016-01-25 11:14:01 +00:00
rdp.geom_mode &= (~rdp.cmd1);
2013-04-04 21:22:19 +00:00
2016-01-25 11:14:01 +00:00
if (rdp.cmd1 & 0x00000001) // Z-Buffer enable
2013-04-04 21:22:19 +00:00
{
2016-01-25 11:14:01 +00:00
if (rdp.flags & ZBUF_ENABLED)
{
rdp.flags ^= ZBUF_ENABLED;
rdp.update |= UPDATE_ZBUF_ENABLED;
}
2013-04-04 21:22:19 +00:00
}
2016-01-25 11:14:01 +00:00
//Added by Gonetz
if (rdp.cmd1 & 0x00010000) // Fog enable
2013-04-04 21:22:19 +00:00
{
2016-01-25 11:14:01 +00:00
if (rdp.flags & FOG_ENABLED)
{
rdp.flags ^= FOG_ENABLED;
rdp.update |= UPDATE_FOG_ENABLED;
}
2013-04-04 21:22:19 +00:00
}
}