Skip to content

Commit

Permalink
Make progress on frameLoadVertex
Browse files Browse the repository at this point in the history
  • Loading branch information
cadmic committed Oct 8, 2024
1 parent f1a28a9 commit 9bed1e2
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 73 deletions.
4 changes: 2 additions & 2 deletions config/oot-e/symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1487,9 +1487,9 @@ PSMTXTransApply = .text:0x8009CED8; // type:function size:0x4C
PSMTXScale = .text:0x8009CF24; // type:function size:0x28
PSMTXMultVec = .text:0x8009CF4C; // type:function size:0x54
C_MTXOrtho = .text:0x8009CFA0; // type:function size:0x98
fn_8009D008 = .text:0x8009D038; // type:function size:0x34
PSMTX44Identity = .text:0x8009D038; // type:function size:0x34
PSMTX44Concat = .text:0x8009D06C; // type:function size:0x104
fn_8009D140 = .text:0x8009D170; // type:function size:0x64
PSMTXTranspose = .text:0x8009D170; // type:function size:0x64
PSMTX44MultVecNoW = .text:0x8009D1D4; // type:function size:0x54
__GXDefaultTexRegionCallback = .text:0x8009D228; // type:function size:0xF4 scope:local
__GXDefaultTlutRegionCallback = .text:0x8009D31C; // type:function size:0x24 scope:local
Expand Down
4 changes: 2 additions & 2 deletions config/oot-j/symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1487,9 +1487,9 @@ PSMTXTransApply = .text:0x8009CEA8; // type:function size:0x4C
PSMTXScale = .text:0x8009CEF4; // type:function size:0x28
PSMTXMultVec = .text:0x8009CF1C; // type:function size:0x54
C_MTXOrtho = .text:0x8009CF70; // type:function size:0x98
fn_8009D008 = .text:0x8009D008; // type:function size:0x34
PSMTX44Identity = .text:0x8009D008; // type:function size:0x34
PSMTX44Concat = .text:0x8009D03C; // type:function size:0x104
fn_8009D140 = .text:0x8009D140; // type:function size:0x64
PSMTXTranspose = .text:0x8009D140; // type:function size:0x64
PSMTX44MultVecNoW = .text:0x8009D1A4; // type:function size:0x54
__GXDefaultTexRegionCallback = .text:0x8009D1F8; // type:function size:0xF4 scope:local
__GXDefaultTlutRegionCallback = .text:0x8009D2EC; // type:function size:0x24 scope:local
Expand Down
4 changes: 2 additions & 2 deletions config/oot-u/symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1487,9 +1487,9 @@ PSMTXTransApply = .text:0x8009CEB4; // type:function size:0x4C
PSMTXScale = .text:0x8009CF00; // type:function size:0x28
PSMTXMultVec = .text:0x8009CF28; // type:function size:0x54
C_MTXOrtho = .text:0x8009CF7C; // type:function size:0x98
fn_8009D008 = .text:0x8009D014; // type:function size:0x34
PSMTX44Identity = .text:0x8009D014; // type:function size:0x34
PSMTX44Concat = .text:0x8009D048; // type:function size:0x104
fn_8009D140 = .text:0x8009D14C; // type:function size:0x64
PSMTXTranspose = .text:0x8009D14C; // type:function size:0x64
PSMTX44MultVecNoW = .text:0x8009D1B0; // type:function size:0x54
__GXDefaultTexRegionCallback = .text:0x8009D204; // type:function size:0xF4 scope:local
__GXDefaultTlutRegionCallback = .text:0x8009D2F8; // type:function size:0x24 scope:local
Expand Down
4 changes: 3 additions & 1 deletion include/emulator/frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,9 @@ typedef struct Frame {
/* 0x3E850 */ MatrixHint aMatrixHint[64];
/* 0x3F150 */ Mtx44 unknown;
/* 0x3F190 */ Mtx44 unknown2;
/* 0x3F1D0 */ s32 pad2[0x12]; // 1 Mtx44 + 2 floats?
/* 0x3F1D0 */ Mtx44 unknown3;
/* 0x3F210 */ f32 unk_3F210;
/* 0x3F214 */ f32 unk_3F214;
/* 0x3F218 */ u8 primLODmin;
/* 0x3F219 */ u8 primLODfrac;
/* 0x3F21A */ u8 lastTile;
Expand Down
198 changes: 136 additions & 62 deletions src/emulator/frame.c
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,9 @@ static u32 gHackCreditsColor;
bool gNoSwapBuffer;
static bool snScissorChanged;

// TODO: identify this
static s32 lbl_8025D07C;

// .bss
static u32 sConstantBufAddr[6] ATTRIBUTE_ALIGN(32);
static u16 sTempZBuf[N64_FRAME_WIDTH * N64_FRAME_HEIGHT / 16][4][4] ATTRIBUTE_ALIGN(32);
Expand Down Expand Up @@ -749,7 +752,92 @@ static bool frameLoadTile(Frame* pFrame, FrameTexture** ppTexture, s32 iTileCode

// fn_8004A020

// fn_8004A314
static inline void fn_8004A314_inline(Mtx44 mtx, f32 a[4], f32 d) {
f32 length;

a[0] = (d * mtx[0][2]) + mtx[0][3];
a[1] = (d * mtx[1][2]) + mtx[1][3];
a[2] = (d * mtx[2][2]) + mtx[2][3];
a[3] = (d * mtx[3][2]) + mtx[3][3];

length = sqrt(SQ(a[0]) + SQ(a[1]) + SQ(a[2]));

a[0] /= length;
a[1] /= length;
a[2] /= length;
a[3] /= length;
}

void fn_8004A314(Frame* pFrame) {
Mtx44 sp28;
f32 sp18[4];
f32 sp8[4];

f32 temp_f1_2;
f32 temp_f2_4;
f32 temp;

if (pFrame->matrixProjection[0][3] != 0.0f || pFrame->matrixProjection[1][3] != 0.0f ||
pFrame->matrixProjection[2][3] != 0.0f) {
PSMTXTranspose(pFrame->matrixProjection, sp28);
PSMTX44Identity(pFrame->unknown2);
PSMTX44Identity(pFrame->unknown3);

pFrame->unknown2[2][0] = -sp28[3][0];
pFrame->unknown2[2][1] = -sp28[3][1];
pFrame->unknown2[2][2] = -sp28[3][2];
pFrame->unknown2[2][3] = -sp28[3][3];

if (sp28[3][0] != 0.0f) {
pFrame->unknown3[2][2] = -sp28[2][0] / sp28[3][0];
} else if (sp28[3][1] != 0.0f) {
pFrame->unknown3[2][2] = -sp28[2][1] / sp28[3][1];
} else {
pFrame->unknown3[2][2] = -sp28[2][2] / sp28[3][2];
}

pFrame->unknown3[2][3] = -((pFrame->unknown3[2][2] * pFrame->unknown2[2][3]) - sp28[2][3]);
pFrame->unknown3[3][2] = -1.0f;
pFrame->unknown3[3][3] = 0.0f;

pFrame->unknown3[1][2] = 1.0f;
pFrame->unknown2[1][0] = sp28[1][0] - pFrame->unknown2[2][0];
pFrame->unknown2[1][1] = sp28[1][1] - pFrame->unknown2[2][1];
pFrame->unknown2[1][2] = sp28[1][2] - pFrame->unknown2[2][2];
pFrame->unknown2[1][3] = sp28[1][3] - pFrame->unknown2[2][3];

pFrame->unknown3[0][2] = 1.0f;
pFrame->unknown2[0][0] = sp28[0][0] - pFrame->unknown2[2][0];
pFrame->unknown2[0][1] = sp28[0][1] - pFrame->unknown2[2][1];
pFrame->unknown2[0][2] = sp28[0][2] - pFrame->unknown2[2][2];
pFrame->unknown2[0][3] = sp28[0][3] - pFrame->unknown2[2][3];

PSMTXTranspose(pFrame->unknown2, pFrame->unknown2);
PSMTXTranspose(pFrame->unknown3, pFrame->unknown);

fn_8004A314_inline(pFrame->unknown, sp18, -1.0f);
fn_8004A314_inline(pFrame->unknown, sp8, 1.0f);

temp_f1_2 = sp8[3] * 0.1f;
temp_f2_4 = sp18[3] + temp_f1_2;

pFrame->unknown3[2][2] = temp_f1_2 / temp_f2_4;
pFrame->unknown3[2][3] = temp_f1_2 * sp18[3] / temp_f2_4;

pFrame->unk_3F210 = sp18[3];
pFrame->unk_3F214 = -sp8[3];
pFrame->eTypeProjection = FMP_PERSPECTIVE;
} else {
memcpy(pFrame->unknown2, pFrame->matrixProjection, sizeof(Mtx44));
PSMTX44Identity(pFrame->unknown3);
memcpy(pFrame->unknown, pFrame->unknown3, sizeof(Mtx44));
pFrame->unknown3[2][2] = -0.25f;
pFrame->unknown3[2][3] = -0.5f;
pFrame->unk_3F210 = 1.0f;
pFrame->unk_3F214 = 0.0f;
pFrame->eTypeProjection = FMP_ORTHOGRAPHIC;
}
}

// fn_8004A69C

Expand Down Expand Up @@ -929,10 +1017,10 @@ static void frameDrawSyncCallback(u16 nToken) {
}
}

bool fn_8004BF58(Frame* arg0) {
bool fn_8004BF58(Frame* pFrame) {
GXColor color;

frameDrawSetup2D(arg0);
frameDrawSetup2D(pFrame);
GXSetZMode(GX_DISABLE, GX_ALWAYS, GX_DISABLE);
GXSetZCompLoc(GX_TRUE);
GXSetColorUpdate(GX_ENABLE);
Expand Down Expand Up @@ -1974,20 +2062,6 @@ static bool frameCheckTriangleDivide(Frame* pFrame, Primitive* pPrimitive) {
#pragma GLOBAL_ASM("asm/non_matchings/frame/frameDrawTriangle_C3T3.s")
#else
bool frameDrawTriangle_C3T3(Frame* pFrame, Primitive* pPrimitive) {
u32 pad[20];

if (gpSystem->eTypeROM == CZLJ && pPrimitive->nCount == 3 && (pFrame->aMode[FMT_OTHER0] & 0xC00) == 0xC00) {
Mtx44Ptr pMatrix = pFrame->aMatrixModel[pFrame->iMatrixModel];
Vertex* vtx = &pFrame->aVertex[pPrimitive->anData[0]];
if ((vtx->rSum == 53.0f && pMatrix[3][0] == -3080.0f && pMatrix[3][2] == 6067.0f) ||
(pMatrix[3][0] == -31.0f && pMatrix[3][2] == 1669.0f)) {
if (pMatrix[3][0] == -31.0f && pMatrix[3][2] == 1669.0f) {
gHackCreditsColor = true;
}
return true;
}
}

if (pFrame->nModeVtx != 0x17) {
GXClearVtxDesc();
GXSetVtxDesc(GX_VA_POS, GX_DIRECT);
Expand Down Expand Up @@ -2402,23 +2476,6 @@ static bool frameDrawRectTexture(Frame* pFrame, Rectangle* pRectangle) {
f32 rT1;
s32 pad;

if (gpSystem->eTypeROM == NN6J) {
if (pRectangle->nX0 == 0 && pRectangle->nY0 == 0 && pRectangle->nX1 == 1208 && pRectangle->nY1 == 20) {
if (pFrame->aBuffer[FBT_IMAGE].nAddress != 0x3B5000 && pFrame->aBuffer[FBT_IMAGE].nAddress != 0x3DA800 &&
!pFrame->bBackBufferDrawn) {
ZeldaDrawFrameNoBlend(pFrame, pFrame->nTempBuffer);
pFrame->bBackBufferDrawn = true;
nCounter = 0;
}
}
if (pFrame->bBackBufferDrawn == true) {
nCounter += 1;
if (nCounter < 40) {
return true;
}
}
}

if (sSpecialZeldaHackON) {
return true;
}
Expand All @@ -2437,19 +2494,6 @@ static bool frameDrawRectTexture(Frame* pFrame, Rectangle* pRectangle) {
rY0 = (pRectangle->nY0 + 3) >> 2;
rY1 = (pRectangle->nY1 + 3) >> 2;

// TODO: regalloc hacks
// (void)pRectangle->nY0;
// if (gpSystem->eTypeROM == CZLJ) {
// if (pRectangle->nX0 == 816 && pRectangle->nY0 == 560) {
// if (gnCountMapHack < 0 && ++gnCountMapHack == 0) {
// gnCountMapHack = 1;
// } else if (gnCountMapHack > 0) {
// gnCountMapHack--;
// return true;
// }
// }
// }

if (pRectangle->bFlip) {
rS0 = pRectangle->rS;
rT0 = pRectangle->rT;
Expand All @@ -2470,9 +2514,6 @@ static bool frameDrawRectTexture(Frame* pFrame, Rectangle* pRectangle) {
}

rDepth = 0.0f;
if (pFrame->bOverrideDepth) {
rDepth = -1001.0;
}

if (pFrame->nModeVtx != 0xF) {
GXClearVtxDesc();
Expand Down Expand Up @@ -3609,6 +3650,7 @@ bool frameLoadVertex(Frame* pFrame, void* pBuffer, s32 iVertex0, s32 nCount) {
f32 colorT;
f32 rS;
f32 rT;
Vec3f vec;
f32 arNormal[3];
f32 arPosition[3];
Vertex* pVertex;
Expand All @@ -3628,7 +3670,6 @@ bool frameLoadVertex(Frame* pFrame, void* pBuffer, s32 iVertex0, s32 nCount) {
f32 rDiffuse;
f32 rInverseW;
f32 rInverseLength;
Vec3f vec;
f32 distance;

pnData8 = pBuffer;
Expand All @@ -3642,13 +3683,15 @@ bool frameLoadVertex(Frame* pFrame, void* pBuffer, s32 iVertex0, s32 nCount) {
// TODO: volatile hacks
if (!(*(volatile u32*)&pFrame->nMode & 0x400000)) {
if (!(pFrame->nMode & 0x08000000)) {
fn_8004A314();
fn_8004A314(pFrame);
pFrame->nMode |= 0x08000000;
}
PSMTX44Concat(matrixModel, pFrame->matrixProjectionExtra, pFrame->matrixView);
PSMTX44Concat(matrixModel, pFrame->unknown2, pFrame->matrixView);
pFrame->nMode |= 0x400000;
}

matrixView = pFrame->matrixView;

if (pFrame->aMode[FMT_GEOMETRY] & 0x20) {
nLight = pFrame->nCountLight;
nTexGen = pFrame->aMode[FMT_GEOMETRY] & 0x180;
Expand All @@ -3659,6 +3702,7 @@ bool frameLoadVertex(Frame* pFrame, void* pBuffer, s32 iVertex0, s32 nCount) {
if (!pLight->bTransformed || !(pFrame->nMode & 0x200000)) {
PSMTX44MultVecNoW(matrixModel, &pLight->rVecOrigTowards, &vec);
rInverseLength = sqrt(SQ(vec.x) + SQ(vec.y) + SQ(vec.z));
rInverseLength = 1.0f / rInverseLength;
pLight->rVectorX = vec.x * rInverseLength;
pLight->rVectorY = vec.y * rInverseLength;
pLight->rVectorZ = vec.z * rInverseLength;
Expand All @@ -3683,6 +3727,7 @@ bool frameLoadVertex(Frame* pFrame, void* pBuffer, s32 iVertex0, s32 nCount) {
mag = SQ(pFrame->lookAt.rS.x) + SQ(pFrame->lookAt.rS.y) + SQ(pFrame->lookAt.rS.z);
if (mag > 0.0f) {
rInverseLength = sqrt(mag);
rInverseLength = 1.0f / rInverseLength;
pFrame->lookAt.rS.x *= rInverseLength;
pFrame->lookAt.rS.y *= rInverseLength;
pFrame->lookAt.rS.z *= rInverseLength;
Expand All @@ -3691,6 +3736,7 @@ bool frameLoadVertex(Frame* pFrame, void* pBuffer, s32 iVertex0, s32 nCount) {
mag = SQ(pFrame->lookAt.rT.x) + SQ(pFrame->lookAt.rT.y) + SQ(pFrame->lookAt.rT.z);
if (mag > 0.0f) {
rInverseLength = sqrt(mag);
rInverseLength = 1.0f / rInverseLength;
pFrame->lookAt.rT.x *= rInverseLength;
pFrame->lookAt.rT.y *= rInverseLength;
pFrame->lookAt.rT.z *= rInverseLength;
Expand Down Expand Up @@ -3725,24 +3771,49 @@ bool frameLoadVertex(Frame* pFrame, void* pBuffer, s32 iVertex0, s32 nCount) {

iLight = nLight;
pLight = &aLight[iLight];
if (gpSystem->eTypeROM == NFXJ) {
while ((rColorR = pLight->rColorR) + (rColorG = pLight->rColorG) + (rColorB = pLight->rColorB) ==
0.0f) {
pLight++;
}
pLight = &aLight[iLight];
} else {
if (!(gpSystem->eTypeROM == NFXJ || gpSystem->eTypeROM == NFXE || gpSystem->eTypeROM == NFXP)) {
rColorR = pLight->rColorR;
rColorG = pLight->rColorG;
rColorB = pLight->rColorB;
} else {
rColorR = aLight[7].rColorR;
rColorG = aLight[7].rColorG;
rColorB = aLight[7].rColorB;

if (lbl_8025D07C == 0 && 20.0 == rColorR && 30.0 == rColorG && 50.0 == rColorB) {
lbl_8025D07C = 1;
} else if (lbl_8025D07C != 0) {
if (rColorR < 20.0 && rColorG < 30.0 && rColorB < 50.0) {
s32 temp_r16;
u8 sp38[10][3] = {
{0x14, 0x1E, 0x32}, {0x12, 0x1B, 0x2F}, {0x10, 0x18, 0x28}, {0x0E, 0x15, 0x23},
{0x0C, 0x12, 0x1E}, {0x0A, 0x0F, 0x19}, {0x08, 0x0C, 0x14}, {0x06, 0x09, 0x0F},
{0x04, 0x06, 0x0A}, {0x02, 0x03, 0x05},
};

temp_r16 = 10 - (((s32)rColorR + 1) / 2);
if (temp_r16 == 10) {
temp_r16 = 9;
}
rColorR = sp38[temp_r16][0];
rColorG = sp38[temp_r16][1];
rColorB = sp38[temp_r16][2];
} else if (rColorR > 20.0 && rColorG > 30.0 && rColorB >= 50.0) {
lbl_8025D07C = 0;
}
}
pLight = &aLight[iLight];
}

while (--iLight >= 0) {
pLight--;
if ((pFrame->aMode[1] & 0x800) && pLight->kc != 0.0f) {
// TODO: fake?
s16 coordX = pLight->coordX;

distance = sqrtf(SQ(pLight->coordX - arPosition[0]) + SQ(pLight->coordY - arPosition[1]) +
SQ(pLight->coordZ - arPosition[2]));
pLight->rVectorX = (pLight->coordX - arPosition[0]) / distance;
pLight->rVectorX = (coordX - arPosition[0]) / distance;
pLight->rVectorY = (pLight->coordY - arPosition[1]) / distance;
pLight->rVectorZ = (pLight->coordZ - arPosition[2]) / distance;
rDiffuse = (pLight->rVectorX * arNormal[0] + pLight->rVectorY * arNormal[1] +
Expand Down Expand Up @@ -3821,14 +3892,17 @@ bool frameLoadVertex(Frame* pFrame, void* pBuffer, s32 iVertex0, s32 nCount) {

if (nTexGen == 0) {
s16tof32Scaled32Pair(&pnData16[4], &pVertex->rS);
if (gpSystem->eTypeROM == NSMJ && pFrame->bBlurOn && pVertex->rS == 0.0) {
pVertex->rS -= 2.0;
}
}

pVertex++;
pnData8 += 0x10;
pnData16 += 0x8;
}

if (gpSystem->eTypeROM = NSMJ && pFrame->bBlurOn) {
if (gpSystem->eTypeROM == NSMJ && pFrame->bBlurOn) {
pFrame->bBlurOn = false;
}

Expand Down
6 changes: 2 additions & 4 deletions src/revolution/mtx/mtx44.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ void C_MTXOrtho(Mtx44 mtx, f32 t, f32 b, f32 l, f32 r, f32 n, f32 f) {
mtx[3][3] = 1.0f;
}

//! TODO: name this
void fn_8009D008(Mtx44 m, Mtx44* src, Mtx44* dst) {
void PSMTX44Identity(const register Mtx44 m) {
f32 one = 1.0f;
f32 zero = 0.0f;

Expand Down Expand Up @@ -122,8 +121,7 @@ ASM void PSMTX44Concat(const register Mtx44 mA, const register Mtx44 mB, registe
#endif // clang-format on
}

//! TODO: name this
ASM void fn_8009D140(Mtx44 m, Mtx44* src, Mtx44* dst){
ASM void PSMTXTranspose(const register Mtx44 src, register Mtx44 dst){
#ifdef __MWERKS__ // clang-format off
nofralloc

Expand Down

0 comments on commit 9bed1e2

Please sign in to comment.