Skip to content

Commit c4b8bf7

Browse files
committed
profiler template for many tests
1 parent 2cfeaa1 commit c4b8bf7

File tree

2 files changed

+104
-75
lines changed

2 files changed

+104
-75
lines changed

rewrite/src/profiler/benchmark.c

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,48 @@
1+
#ifdef REWRITE_PROFILER
2+
13
#include <ctr/nd.h>
24
#include <ctr/coll.h>
35
#include <ctr/test.h>
46

5-
// When running OG functions,
6-
// dont forget to undefine these in ctr\test.h:
7-
// TEST_MATH_IMPL, TEST_RNG_IMPL, TEST_COLL_IMPL
8-
#ifdef REWRITE_PROFILER
9-
10-
void RunBenchmark()
7+
void RunCollRW()
118
{
12-
// Sample input from no$psx
13-
// $a0: 1f800154 - 0, 0, 0
14-
// $a1: 1f800270 - 0000, 032A, EDE4
15-
// $a2: 1f800220 - FD00, 02EC, EA58
16-
// $a3: 1f800164 - FD6F, 02F5, EDCE
17-
SVec3 out = {};
18-
SVec3 v1 = {0x0, 0x32A, 0xEDE4};
19-
SVec3 v2 = {0xFD00, 0x2EC, 0xEA58};
20-
SVec3 point = {0xFD6F, 0x2F5, 0xEDCE};
21-
22-
// if 0 + COLL_XXX: Run the New function
23-
// if 1 + ND_COLL_XXX: Run the OG function
24-
#if 1
25-
// Revert the Hook from OG -> DECOMP
26-
PatchFunction_Beg((u32*)(&ND_COLL_ProjectPointToEdge));
27-
#endif
28-
29-
// Test 1000 times
30-
for(int i = 0; i < 100; i++)
31-
{
32-
ND_COLL_ProjectPointToEdge(&out, &v1, &v2, &point);
33-
}
9+
// Sample input from no$psx
10+
// $a0: 1f800154 - 0, 0, 0
11+
// $a1: 1f800270 - 0000, 032A, EDE4
12+
// $a2: 1f800220 - FD00, 02EC, EA58
13+
// $a3: 1f800164 - FD6F, 02F5, EDCE
14+
SVec3 out = {};
15+
SVec3 v1 = {0x0, 0x32A, 0xEDE4};
16+
SVec3 v2 = {0xFD00, 0x2EC, 0xEA58};
17+
SVec3 point = {0xFD6F, 0x2F5, 0xEDCE};
18+
19+
// million
20+
for(int i = 0; i < 1000000; i++)
21+
{
22+
COLL_ProjectPointToEdge(&out, &v1, &v2, &point);
23+
}
24+
}
3425

26+
void RunCollOG()
27+
{
28+
// Sample input from no$psx
29+
// $a0: 1f800154 - 0, 0, 0
30+
// $a1: 1f800270 - 0000, 032A, EDE4
31+
// $a2: 1f800220 - FD00, 02EC, EA58
32+
// $a3: 1f800164 - FD6F, 02F5, EDCE
33+
SVec3 out = {};
34+
SVec3 v1 = {0x0, 0x32A, 0xEDE4};
35+
SVec3 v2 = {0xFD00, 0x2EC, 0xEA58};
36+
SVec3 point = {0xFD6F, 0x2F5, 0xEDCE};
37+
38+
// Revert the Hook from OG -> DECOMP
39+
PatchFunction_Beg((u32*)(&ND_COLL_ProjectPointToEdge));
40+
41+
// million
42+
for(int i = 0; i < 1000000; i++)
43+
{
44+
ND_COLL_ProjectPointToEdge(&out, &v1, &v2, &point);
45+
}
3546
}
3647

37-
#endif // REWRITE_PROFILER
48+
#endif

rewrite/src/profiler/profiler.c

Lines changed: 64 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
#ifdef REWRITE_PROFILER
2+
13
#include <ctr/profiler.h>
24
#include <ctr/nd.h>
35

4-
#ifdef REWRITE_PROFILER
5-
66
// No Vehicle.h
77
struct MetaPhys
88
{
@@ -42,69 +42,87 @@ struct MetaPhys
4242
#include <namespace_UI.h>
4343
#include <regionsEXE.h>
4444

45+
void ND_EnterCriticalSection();
46+
void ND_ExitCriticalSection();
47+
4548
void ND_DrawOTag(int a);
4649
void ND_DecalFont_DrawLine(char* a, int b, int c, int d, int e);
4750

48-
4951
int ND_GetRCnt(int a);
52+
int ND_ResetRCnt(int a);
5053

51-
int Debug_GetPreciseTime()
52-
{
53-
int sysClock =
54-
ND_GetRCnt(0xf2000001) +
55-
sdata->rcntTotalUnits;
56-
57-
return sysClock;
58-
}
54+
// OG = Original
55+
// RW = Rewrite
56+
int timeOG_COLL_ProjectPointToEdge;
57+
int timeRW_COLL_ProjectPointToEdge;
5958

60-
// Jam this here, cause byte budget
61-
static int timeRed=0;
62-
static int timeStart=0;
63-
static int timeEnd=0;
64-
void Hook_DrawOTag(int a)
65-
{
66-
// Calculate ONE time, then save persistently
67-
if (timeRed == 0)
68-
{
69-
timeStart = Debug_GetPreciseTime();
59+
#define JMP(dest) (((unsigned long)dest & 0x3FFFFFF) >> 2 | 0x8000000)
60+
#define JAL(dest) (((unsigned long)dest & 0x3FFFFFF) >> 2 | 0xC000000)
7061

71-
void RunBenchmark();
72-
RunBenchmark();
62+
void Hook_DrawOTag(int a);
63+
void RunCollOG();
64+
void RunCollRW();
7365

74-
timeEnd = Debug_GetPreciseTime();
66+
// When running OG functions,
67+
// dont forget to undefine these in ctr\test.h:
68+
// TEST_MATH_IMPL, TEST_RNG_IMPL, TEST_COLL_IMPL
7569

70+
// Also, required to run RW function before OG,
71+
// because of how assembly hooking system works
7672

77-
timeRed = timeEnd - timeStart;
78-
}
73+
void LoadProfilerPatches()
74+
{
75+
*(int*)0x800379b0 = JAL(Hook_DrawOTag);
76+
}
7977

80-
struct GameTracker* gGT = sdata->gGT;
78+
int doThisOnce=0;
79+
void RunAllBenchmarks()
80+
{
81+
if(doThisOnce != 0)
82+
return;
83+
84+
int r = 0;
85+
doThisOnce = 1;
86+
87+
ND_ResetRCnt(0xf2000001);
88+
RunCollRW();
89+
r = ND_GetRCnt(0xf2000001);
90+
timeRW_COLL_ProjectPointToEdge = r;
91+
92+
ND_ResetRCnt(0xf2000001);
93+
RunCollOG();
94+
r = ND_GetRCnt(0xf2000001);
95+
timeOG_COLL_ProjectPointToEdge = r;
96+
}
8197

98+
void Hook_DrawOTag(int a)
99+
{
100+
ND_EnterCriticalSection();
101+
RunAllBenchmarks();
102+
ND_ExitCriticalSection();
103+
104+
// Why does this break on console?
105+
// struct GameTracker* gGT = sdata->gGT;
106+
struct GameTracker* gGT = (struct GameTracker*)0x80096b20;
107+
82108
if((gGT->gameMode1 & (LOADING|1)) == 0)
83109
{
84110
// reset depth to CLOSEST
85111
gGT->pushBuffer_UI.ptrOT =
86-
gGT->otSwapchainDB[gGT->swapchainIndex];
87-
112+
gGT->otSwapchainDB[gGT->swapchainIndex];
88113

89-
#ifndef REBUILD_PC
90-
char* string = (char*)0x1f800000;
91-
#else
92114
char string[128];
93-
#endif
94-
95-
ND_sprintf(string, "RED %d", timeRed);
96-
ND_DecalFont_DrawLine(string, 0x14, 0x5C, FONT_SMALL, 0);
115+
116+
ND_DecalFont_DrawLine("1s 15720", 0x14, 0x8, FONT_SMALL, 0);
117+
118+
ND_sprintf(string, "RW %d", timeRW_COLL_ProjectPointToEdge);
119+
ND_DecalFont_DrawLine(string, 0x14, 0x10, FONT_SMALL, 0);
120+
121+
ND_sprintf(string, "OG %d", timeOG_COLL_ProjectPointToEdge);
122+
ND_DecalFont_DrawLine(string, 0x14, 0x18, FONT_SMALL, 0);
97123
}
98-
124+
99125
ND_DrawOTag(a);
100126
}
101127

102-
#define JMP(dest) (((unsigned long)dest & 0x3FFFFFF) >> 2 | 0x8000000)
103-
#define JAL(dest) (((unsigned long)dest & 0x3FFFFFF) >> 2 | 0xC000000)
104-
105-
void LoadProfilerPatches()
106-
{
107-
*(int*)0x800379b0 = JAL(Hook_DrawOTag);
108-
}
109-
110-
#endif // REWRITE_PROFILER
128+
#endif

0 commit comments

Comments
 (0)