Skip to content

Commit 4a4a54c

Browse files
authored
Added simple gfx trace system (Kenix3#559)
* Rebased * Fixed tidy * Fixed tidy again * Added trace on F3DGwords * Fixed static_assert to count for Trace * Wrapped gbi trace into an ifdef * Fixed tidy
1 parent 5e34a4e commit 4a4a54c

File tree

4 files changed

+105
-14
lines changed

4 files changed

+105
-14
lines changed

include/libultraship/libultra/gbi.h

+67-8
Original file line numberDiff line numberDiff line change
@@ -1643,19 +1643,40 @@ typedef struct {
16431643
unsigned long w3;
16441644
} TexRect;
16451645

1646+
#ifdef USE_GBI_TRACE
1647+
/*
1648+
* Trace structure
1649+
*/
1650+
typedef struct {
1651+
const char* file;
1652+
int idx;
1653+
bool valid;
1654+
} Trace;
1655+
1656+
#define MakeTrace() \
1657+
, { \
1658+
__FILE__, __LINE__, true \
1659+
}
1660+
#else
1661+
#define MakeTrace()
1662+
#endif
16461663
/*
16471664
* Generic Gfx Packet
16481665
*/
16491666
typedef struct {
16501667
uintptr_t w0;
16511668
uintptr_t w1;
1652-
1653-
// unsigned long long w0;
1654-
// unsigned long long w1;
1669+
#ifdef USE_GBI_TRACE
1670+
Trace trace;
1671+
#endif
16551672
} Gwords;
16561673

16571674
#ifdef __cplusplus
1658-
static_assert(sizeof(Gwords) == 2 * sizeof(void*), "Display list size is bad");
1675+
#ifdef USE_GBI_TRACE
1676+
static_assert((sizeof(Gwords) == 2 * sizeof(void*) + sizeof(Trace)), "Display list size is bad");
1677+
#else
1678+
static_assert((sizeof(Gwords) == 2 * sizeof(void*)), "Display list size is bad");
1679+
#endif
16591680
#endif
16601681

16611682
/*
@@ -1711,28 +1732,54 @@ typedef union Gfx {
17111732
#define gsDma0p(c, s, l) \
17121733
{ _SHIFTL((c), 24, 8) | _SHIFTL((l), 0, 24), (uintptr_t)(s) }
17131734

1735+
#ifdef USE_GBI_TRACE
17141736
#define gDma1p(pkt, c, s, l, p) \
17151737
_DW({ \
17161738
Gfx* _g = (Gfx*)(pkt); \
17171739
\
17181740
_g->words.w0 = (_SHIFTL((c), 24, 8) | _SHIFTL((p), 16, 8) | _SHIFTL((l), 0, 16)); \
17191741
_g->words.w1 = (uintptr_t)(s); \
1742+
_g->words.trace.file = __FILE__; \
1743+
_g->words.trace.idx = __LINE__; \
1744+
_g->words.trace.valid = true; \
17201745
})
1721-
1746+
#else
1747+
#define gDma1p(pkt, c, s, l, p) \
1748+
_DW({ \
1749+
Gfx* _g = (Gfx*)(pkt); \
1750+
\
1751+
_g->words.w0 = (_SHIFTL((c), 24, 8) | _SHIFTL((p), 16, 8) | _SHIFTL((l), 0, 16)); \
1752+
_g->words.w1 = (uintptr_t)(s); \
1753+
})
1754+
#endif
17221755
#define gsDma1p(c, s, l, p) \
1723-
{ (_SHIFTL((c), 24, 8) | _SHIFTL((p), 16, 8) | _SHIFTL((l), 0, 16)), (uintptr_t)(s) }
1756+
{ (_SHIFTL((c), 24, 8) | _SHIFTL((p), 16, 8) | _SHIFTL((l), 0, 16)), (uintptr_t)(s)MakeTrace() }
17241757

1758+
#ifdef USE_GBI_TRACE
1759+
#define gDma2p(pkt, c, adrs, len, idx, ofs) \
1760+
_DW({ \
1761+
Gfx* _g = (Gfx*)(pkt); \
1762+
_g->words.w0 = \
1763+
(_SHIFTL((c), 24, 8) | _SHIFTL(((len)-1) / 8, 19, 5) | _SHIFTL((ofs) / 8, 8, 8) | _SHIFTL((idx), 0, 8)); \
1764+
_g->words.w1 = (uintptr_t)(adrs); \
1765+
_g->words.trace.file = __FILE__; \
1766+
_g->words.trace.idx = __LINE__; \
1767+
_g->words.trace.valid = true; \
1768+
})
1769+
#else
17251770
#define gDma2p(pkt, c, adrs, len, idx, ofs) \
17261771
_DW({ \
17271772
Gfx* _g = (Gfx*)(pkt); \
17281773
_g->words.w0 = \
17291774
(_SHIFTL((c), 24, 8) | _SHIFTL(((len)-1) / 8, 19, 5) | _SHIFTL((ofs) / 8, 8, 8) | _SHIFTL((idx), 0, 8)); \
17301775
_g->words.w1 = (uintptr_t)(adrs); \
17311776
})
1777+
#endif
1778+
17321779
#define gsDma2p(c, adrs, len, idx, ofs) \
17331780
{ \
17341781
(_SHIFTL((c), 24, 8) | _SHIFTL(((len)-1) / 8, 19, 5) | _SHIFTL((ofs) / 8, 8, 8) | _SHIFTL((idx), 0, 8)), \
1735-
(uintptr_t)(adrs) \
1782+
(uintptr_t)(adrs)MakeTrace() \
17361783
}
17371784

17381785
#define gSPNoOp(pkt) gDma0p(pkt, G_SPNOOP, 0, 0)
@@ -1756,14 +1803,26 @@ typedef union Gfx {
17561803
* | |seg| address |
17571804
* +-+---+-----------------------------+
17581805
*/
1806+
#ifdef USE_GBI_TRACE
17591807
#define __gSPVertex(pkt, v, n, v0) \
17601808
_DW({ \
17611809
Gfx* _g = (Gfx*)(pkt); \
17621810
_g->words.w0 = _SHIFTL(G_VTX, 24, 8) | _SHIFTL((n), 12, 8) | _SHIFTL((v0) + (n), 1, 7); \
17631811
_g->words.w1 = (uintptr_t)(v); \
1812+
_g->words.trace.file = __FILE__; \
1813+
_g->words.trace.idx = __LINE__; \
1814+
_g->words.trace.valid = true; \
17641815
})
1816+
#else
1817+
#define __gSPVertex(pkt, v, n, v0) \
1818+
_DW({ \
1819+
Gfx* _g = (Gfx*)(pkt); \
1820+
_g->words.w0 = _SHIFTL(G_VTX, 24, 8) | _SHIFTL((n), 12, 8) | _SHIFTL((v0) + (n), 1, 7); \
1821+
_g->words.w1 = (uintptr_t)(v); \
1822+
})
1823+
#endif
17651824
#define gsSPVertex(v, n, v0) \
1766-
{ (_SHIFTL(G_VTX, 24, 8) | _SHIFTL((n), 12, 8) | _SHIFTL((v0) + (n), 1, 7)), (uintptr_t)(v) }
1825+
{ (_SHIFTL(G_VTX, 24, 8) | _SHIFTL((n), 12, 8) | _SHIFTL((v0) + (n), 1, 7)), (uintptr_t)(v)MakeTrace() }
17671826

17681827
#elif (defined(F3DEX_GBI) || defined(F3DLP_GBI))
17691828
/*

src/graphic/Fast3D/gfx_pc.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -4009,6 +4009,13 @@ static void gfx_step() {
40094009
auto cmd0 = cmd;
40104010
int8_t opcode = (int8_t)(cmd->words.w0 >> 24);
40114011

4012+
#ifdef USE_GBI_TRACE
4013+
if (cmd->words.trace.valid && CVarGetInteger("gEnableGFXTrace", 0)) {
4014+
SPDLOG_INFO("Trace File: {}", cmd->words.trace.file);
4015+
SPDLOG_INFO("Trace Line: {}", cmd->words.trace.idx);
4016+
}
4017+
#endif
4018+
40124019
if (opcode == F3DEX2_G_LOAD_UCODE) {
40134020
gfx_set_ucode_handler((UcodeHandlers)(cmd->words.w0 & 0xFFFFFF));
40144021
++cmd;

src/graphic/Fast3D/lus_gbi.h

+18
Original file line numberDiff line numberDiff line change
@@ -1112,17 +1112,35 @@ typedef union {
11121112
long int force_structure_alignment[4];
11131113
} F3DHilite;
11141114

1115+
#ifdef USE_GBI_TRACE
1116+
/*
1117+
* Trace structure
1118+
*/
1119+
typedef struct {
1120+
const char* file;
1121+
int idx;
1122+
bool valid;
1123+
} F3DTrace;
1124+
#endif
1125+
11151126
/*
11161127
* Generic Gfx Packet
11171128
*/
11181129
typedef struct {
11191130
uintptr_t w0;
11201131
uintptr_t w1;
1132+
#ifdef USE_GBI_TRACE
1133+
F3DTrace trace;
1134+
#endif
11211135
} F3DGwords;
11221136

11231137
#ifdef __cplusplus
1138+
#ifdef USE_GBI_TRACE
1139+
static_assert(sizeof(F3DGwords) == 2 * sizeof(void*) + sizeof(F3DTrace), "Display list size is bad");
1140+
#else
11241141
static_assert(sizeof(F3DGwords) == 2 * sizeof(void*), "Display list size is bad");
11251142
#endif
1143+
#endif
11261144

11271145
/*
11281146
* This union is the fundamental type of the display list.

src/resource/factory/DisplayListFactory.cpp

+13-6
Original file line numberDiff line numberDiff line change
@@ -166,24 +166,31 @@ std::shared_ptr<Ship::IResource> ResourceFactoryBinaryDisplayListV0::ReadResourc
166166
reader->ReadInt8();
167167
}
168168

169+
size_t idx = 0;
169170
while (true) {
170171
Gfx command;
171172
command.words.w0 = reader->ReadUInt32();
172173
command.words.w1 = reader->ReadUInt32();
173174

174-
displayList->Instructions.push_back(command);
175-
176175
int8_t opcode = (int8_t)(command.words.w0 >> 24);
176+
bool isExpanded = opcode == G_SETTIMG_OTR_HASH || opcode == G_DL_OTR_HASH || opcode == G_VTX_OTR_HASH ||
177+
opcode == G_BRANCH_Z_OTR || opcode == G_MARKER || opcode == G_MTX_OTR;
177178

178179
// These are 128-bit commands, so read an extra 64 bits...
179-
if (opcode == G_SETTIMG_OTR_HASH || opcode == G_DL_OTR_HASH || opcode == G_VTX_OTR_HASH ||
180-
opcode == G_BRANCH_Z_OTR || opcode == G_MARKER || opcode == G_MTX_OTR || opcode == G_MOVEMEM_OTR) {
180+
if (isExpanded) {
181+
displayList->Instructions.push_back(command);
181182
command.words.w0 = reader->ReadUInt32();
182183
command.words.w1 = reader->ReadUInt32();
183-
184-
displayList->Instructions.push_back(command);
185184
}
186185

186+
#ifdef USE_GBI_TRACE
187+
command.words.trace.file = file->InitData->Path.c_str();
188+
command.words.trace.idx = idx++;
189+
command.words.trace.valid = true;
190+
#endif
191+
192+
displayList->Instructions.push_back(command);
193+
187194
if (opcode == GetEndOpcodeByUCode(ucode)) {
188195
break;
189196
}

0 commit comments

Comments
 (0)