Skip to content

Commit

Permalink
Fix up mtx and vtx otr opcodes (#674)
Browse files Browse the repository at this point in the history
  • Loading branch information
Archez authored Sep 15, 2024
1 parent d7a316c commit d64dd46
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 19 deletions.
2 changes: 1 addition & 1 deletion include/libultraship/libultra/gbi.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@
#define G_TRI1_OTR 0x26
#define G_DL_OTR_FILEPATH 0x27
#define G_PUSHCD 0x28
#define G_MTX_OTR2 0x29
#define G_MTX_OTR_FILEPATH 0x29
#define G_DL_OTR_HASH 0x31
#define G_VTX_OTR_HASH 0x32
#define G_MARKER 0x33
Expand Down
71 changes: 57 additions & 14 deletions src/graphic/Fast3D/gfx_pc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2774,12 +2774,43 @@ bool gfx_mtx_handler_f3d(F3DGfx** cmd0) {
return false;
}

bool gfx_mtx_otr_filepath_handler_custom_f3dex2(F3DGfx** cmd0) {
F3DGfx* cmd = *cmd0;
const char* fileName = (const char*)cmd->words.w1;
const int32_t* mtx = (const int32_t*)ResourceGetDataByName((const char*)fileName);

if (mtx != NULL) {
gfx_sp_matrix(C0(0, 8) ^ F3DEX2_G_MTX_PUSH, mtx);
}

return false;
}

bool gfx_mtx_otr_filepath_handler_custom_f3d(F3DGfx** cmd0) {
F3DGfx* cmd = *cmd0;
const char* fileName = (const char*)cmd->words.w1;
const int32_t* mtx = (const int32_t*)ResourceGetDataByName((const char*)fileName);

if (mtx != NULL) {
gfx_sp_matrix(C0(16, 8), mtx);
}

return false;
}

bool gfx_mtx_otr_filepath_handler_custom(F3DGfx** cmd0) {
if (ucode_handler_index == ucode_f3dex2) {
return gfx_mtx_otr_filepath_handler_custom_f3dex2(cmd0);
} else {
return gfx_mtx_otr_filepath_handler_custom_f3d(cmd0);
}
}

bool gfx_mtx_otr_handler_custom_f3dex2(F3DGfx** cmd0) {
(*cmd0)++;
F3DGfx* cmd = *cmd0;

const uint64_t hash = ((uint64_t)cmd->words.w0 << 32) + cmd->words.w1;

const int32_t* mtx = (const int32_t*)ResourceGetDataByCrc(hash);

if (mtx != NULL) {
Expand All @@ -2796,14 +2827,25 @@ bool gfx_mtx_otr_handler_custom_f3d(F3DGfx** cmd0) {
F3DGfx* cmd = *cmd0;

const uint64_t hash = ((uint64_t)cmd->words.w0 << 32) + cmd->words.w1;

const int32_t* mtx = (const int32_t*)ResourceGetDataByCrc(hash);

gfx_sp_matrix(C0(16, 8), (const int32_t*)seg_addr(cmd->words.w1));
if (mtx != NULL) {
cmd--;
gfx_sp_matrix(C0(16, 8), mtx);
cmd++;
}

return false;
}

bool gfx_mtx_otr_handler_custom(F3DGfx** cmd0) {
if (ucode_handler_index == ucode_f3dex2) {
return gfx_mtx_otr_handler_custom_f3dex2(cmd0);
} else {
return gfx_mtx_otr_handler_custom_f3d(cmd0);
}
}

bool gfx_pop_mtx_handler_f3dex2(F3DGfx** cmd0) {
F3DGfx* cmd = *cmd0;

Expand Down Expand Up @@ -2951,13 +2993,13 @@ bool gfx_vtx_hash_handler_custom(F3DGfx** cmd0) {
bool gfx_vtx_otr_filepath_handler_custom(F3DGfx** cmd0) {
F3DGfx* cmd = *cmd0;
char* fileName = (char*)cmd->words.w1;
cmd++;
(*cmd0)++;
cmd = *cmd0;
size_t vtxCnt = cmd->words.w0;
size_t vtxIdxOff = cmd->words.w1 >> 16;
size_t vtxDataOff = cmd->words.w1 & 0xFFFF;
F3DVtx* vtx = (F3DVtx*)ResourceGetDataByName((const char*)fileName);
vtx += vtxDataOff;
cmd--;

gfx_sp_vertex(vtxCnt, vtxIdxOff, vtx);
return false;
Expand Down Expand Up @@ -3747,13 +3789,16 @@ static constexpr UcodeHandler otrHandlers = {
{ OTR_G_SETTIMG_OTR_FILEPATH,
{ "G_SETTIMG_OTR_FILEPATH", gfx_set_timg_otr_filepath_handler_custom } }, // G_SETTIMG_OTR_FILEPATH (0x25)
{ OTR_G_TRI1_OTR, { "G_TRI1_OTR", gfx_tri1_otr_handler_f3dex2 } }, // G_TRI1_OTR (0x26)
{ OTR_G_DL_OTR_FILEPATH, { "G_DL_OTR_FILEPATH", gfx_dl_otr_filepath_handler_custom } }, // G_DL_OTR_FILEPATH (0x27)
{ OTR_G_PUSHCD, { "G_PUSHCD", gfx_pushcd_handler_custom } }, // G_PUSHCD (0x28)
{ OTR_G_DL_OTR_HASH, { "G_DL_OTR_HASH", gfx_dl_otr_hash_handler_custom } }, // G_DL_OTR_HASH (0x31)
{ OTR_G_VTX_OTR_HASH, { "G_VTX_OTR_HASH", gfx_vtx_hash_handler_custom } }, // G_VTX_OTR_HASH (0x32)
{ OTR_G_MARKER, { "G_MARKER", gfx_marker_handler_otr } }, // G_MARKER (0X33)
{ OTR_G_DL_OTR_FILEPATH, { "G_DL_OTR_FILEPATH", gfx_dl_otr_filepath_handler_custom } }, // G_DL_OTR_FILEPATH (0x27)
{ OTR_G_PUSHCD, { "G_PUSHCD", gfx_pushcd_handler_custom } }, // G_PUSHCD (0x28)
{ OTR_G_MTX_OTR_FILEPATH,
{ "G_MTX_OTR_FILEPATH", gfx_mtx_otr_filepath_handler_custom } }, // G_MTX_OTR_FILEPATH (0x29)
{ OTR_G_DL_OTR_HASH, { "G_DL_OTR_HASH", gfx_dl_otr_hash_handler_custom } }, // G_DL_OTR_HASH (0x31)
{ OTR_G_VTX_OTR_HASH, { "G_VTX_OTR_HASH", gfx_vtx_hash_handler_custom } }, // G_VTX_OTR_HASH (0x32)
{ OTR_G_MARKER, { "G_MARKER", gfx_marker_handler_otr } }, // G_MARKER (0X33)
{ OTR_G_INVALTEXCACHE, { "G_INVALTEXCACHE", gfx_invalidate_tex_cache_handler_f3dex2 } }, // G_INVALTEXCACHE (0X34)
{ OTR_G_BRANCH_Z_OTR, { "G_BRANCH_Z_OTR", gfx_branch_z_otr_handler_f3dex2 } }, // G_BRANCH_Z_OTR (0x35)
{ OTR_G_MTX_OTR, { "G_MTX_OTR", gfx_mtx_otr_handler_custom } }, // G_MTX_OTR (0x36)
{ OTR_G_TEXRECT_WIDE, { "G_TEXRECT_WIDE", gfx_tex_rect_wide_handler_custom } }, // G_TEXRECT_WIDE (0x37)
{ OTR_G_FILLWIDERECT, { "G_FILLWIDERECT", gfx_fill_wide_rect_handler_custom } }, // G_FILLWIDERECT (0x38)
{ OTR_G_SETGRAYSCALE, { "G_SETGRAYSCALE", gfx_set_grayscale_handler_custom } }, // G_SETGRAYSCALE (0x39)
Expand Down Expand Up @@ -3786,7 +3831,6 @@ static constexpr UcodeHandler f3dex2Handlers = {
{ F3DEX2_G_QUAD, { "G_QUAD", gfx_quad_handler_f3dex2 } },
{ F3DEX2_G_SETOTHERMODE_L, { "G_SETOTHERMODE_L", gfx_othermode_l_handler_f3dex2 } },
{ F3DEX2_G_SETOTHERMODE_H, { "G_SETOTHERMODE_H", gfx_othermode_h_handler_f3dex2 } },
{ OTR_G_MTX_OTR, { "G_MTX_OTR", gfx_mtx_otr_handler_custom_f3dex2 } },
};

static constexpr UcodeHandler f3dexHandlers = {
Expand All @@ -3809,8 +3853,7 @@ static constexpr UcodeHandler f3dexHandlers = {
{ F3DEX_G_TRI2, { "G_TRI2", gfx_tri2_handler_f3dex } },
{ F3DEX_G_SPNOOP, { "G_SPNOOP", gfx_spnoop_command_handler_f3dex2 } },
{ F3DEX_G_RDPHALF_1, { "G_RDPHALF_1", gfx_stubbed_command_handler } },
{ OTR_G_MTX_OTR2, { "G_MTX_OTR2", gfx_mtx_otr_handler_custom_f3d } }, // G_MTX_OTR2 (0x29) Is this the right code?
{ F3DEX_G_QUAD, { "G_QUAD", gfx_quad_handler_f3dex } }
{ F3DEX_G_QUAD, { "G_QUAD", gfx_quad_handler_f3dex } },
};

static constexpr UcodeHandler f3dHandlers = {
Expand All @@ -3832,7 +3875,7 @@ static constexpr UcodeHandler f3dHandlers = {
{ F3DEX_G_ENDDL, { "G_ENDDL", gfx_end_dl_handler_common } },
{ F3DEX_G_TRI2, { "G_TRI2", gfx_tri2_handler_f3dex } },
{ F3DEX_G_SPNOOP, { "G_SPNOOP", gfx_spnoop_command_handler_f3dex2 } },
{ F3DEX_G_RDPHALF_1, { "G_RDPHALF_1", gfx_stubbed_command_handler } }
{ F3DEX_G_RDPHALF_1, { "G_RDPHALF_1", gfx_stubbed_command_handler } },
};

// LUSTODO: These S2DEX commands have different opcode numbers on F3DEX2 vs other ucodes. More research needs to be done
Expand Down
2 changes: 1 addition & 1 deletion src/graphic/Fast3D/lus_gbi.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ constexpr int8_t OTR_G_SETTIMG_OTR_FILEPATH = OPCODE(0x25);
constexpr int8_t OTR_G_TRI1_OTR = OPCODE(0x26);
constexpr int8_t OTR_G_DL_OTR_FILEPATH = OPCODE(0x27);
constexpr int8_t OTR_G_PUSHCD = OPCODE(0x28);
constexpr int8_t OTR_G_MTX_OTR2 = OPCODE(0x29);
constexpr int8_t OTR_G_MTX_OTR_FILEPATH = OPCODE(0x29);
constexpr int8_t OTR_G_DL_OTR_HASH = OPCODE(0x31);
constexpr int8_t OTR_G_VTX_OTR_HASH = OPCODE(0x32);
constexpr int8_t OTR_G_MARKER = OPCODE(0x33);
Expand Down
2 changes: 1 addition & 1 deletion src/resource/factory/DisplayListFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ std::shared_ptr<Ship::IResource> ResourceFactoryXMLDisplayListV0::ReadResource(s
g = { gsSPMatrix(0, paramInt) };

g.words.w0 &= 0x00FFFFFF;
g.words.w0 += (G_MTX_OTR2 << 24);
g.words.w0 += (G_MTX_OTR_FILEPATH << 24);
char* str = (char*)malloc(fName.size() + 1);
g.words.w1 = (uintptr_t)str;
dl->Strings.push_back(str);
Expand Down
4 changes: 2 additions & 2 deletions src/window/gui/GfxDebuggerWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,9 +363,9 @@ void GfxDebuggerWindow::DrawDisasNode(const F3DGfx* cmd, std::vector<const F3DGf
break;
}

case OTR_G_MTX_OTR2: {
case OTR_G_MTX_OTR_FILEPATH: {
const char* fileName = (char*)cmd->words.w1;
nodeWithText(cmd0, fmt::format("G_MTX_OTR2: {}", fileName));
nodeWithText(cmd0, fmt::format("G_MTX_OTR_FILEPATH: {}", fileName));

cmd++;
break;
Expand Down

0 comments on commit d64dd46

Please sign in to comment.