Skip to content

Commit

Permalink
LOAD refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Niko committed Feb 21, 2025
1 parent 6c9a1a5 commit fbb3a19
Show file tree
Hide file tree
Showing 22 changed files with 176 additions and 233 deletions.
2 changes: 1 addition & 1 deletion decompile/General/233/233_20_CS_LoadBoss.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void DECOMP_CS_LoadBoss(struct BossCutsceneData* bcd)
//if (bcd->bodyFile != 0)
{
// CTR Model File (body)
LOAD_AppendQueue(sdata->ptrBigfileCdPos_2, LT_DRAM, bcd->bodyFile+index, ptrDestination, -0x2);
LOAD_AppendQueue(sdata->ptrBigfileCdPos_2, LT_DRAM, bcd->bodyFile+index, ptrDestination, 0);
}

// CTR Model File (head)
Expand Down
2 changes: 1 addition & 1 deletion decompile/General/AltMods/Mods2.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void Oxide_HookAppendLoadingQueue()
// add oxide request
lqs_curr->subfileIndex = packIndex + 0xf*2 + 0x18D;
lqs_curr->ptrDestination = (void*)&data.podiumModel_firstPlace;
lqs_curr->callback.flags = 0xfffffffe;
lqs_curr->callback = 0;

// increase size
sdata->queueLength++;
Expand Down
11 changes: 11 additions & 0 deletions decompile/General/GAMEPAD/GAMEPAD_03_PollVsync.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,16 @@ void DECOMP_GAMEPAD_PollVsync(struct GamepadSystem *gGamepads)
void DebugProfiler_Subsection(int flag);
DebugProfiler_Subsection(1);
#endif

// wait two vsyncs for VRAM upload to finish
if (sdata->frameFinishedVRAM != 0)
{
sdata->frameFinishedVRAM--;

if (sdata->frameFinishedVRAM == 0)
{
sdata->queueReady = 1;
}
}
#endif
}
17 changes: 2 additions & 15 deletions decompile/General/LOAD/LOAD_16_DramFileCallback.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,7 @@ void DECOMP_LOAD_DramFileCallback(struct LoadQueueSlot* lqs)
}
}

void* callbackFunc = lqs->callback.funcPtr;

if(callbackFunc != 0)
{
// if function, and not flags
#ifndef REBUILD_PC
if(((unsigned int)callbackFunc & 0xff000000) == 0x80000000)
#else
if((int)callbackFunc > 0)
#endif
{
(*lqs->callback.funcPtr)(lqs);
}
}

sdata->queueReady = 1;
if (lqs->callbackFuncPtr != 0)
lqs->callbackFuncPtr(lqs);
}
40 changes: 1 addition & 39 deletions decompile/General/LOAD/LOAD_17_DramFile.c
Original file line number Diff line number Diff line change
@@ -1,40 +1,2 @@
#include <common.h>

void DECOMP_LOAD_DramFileCallback();

void* DECOMP_LOAD_DramFile(void* bigfilePtr, int subfileIndex, int* ptrDestination, int callbackOrFlags)
{
struct LoadQueueSlot lqs;

if(callbackOrFlags == -1)
{
lqs.ptrBigfileCdPos = bigfilePtr;
lqs.flags = 0;
lqs.type = LT_DRAM;
lqs.subfileIndex = subfileIndex;
lqs.ptrDestination = DECOMP_LOAD_ReadFile(bigfilePtr, LT_DRAM, subfileIndex, 0, 0);
lqs.callback.funcPtr = NULL;

DECOMP_LOAD_DramFileCallback(&lqs);

// optimization, dont use instructions writing
// return value, let it use whatever sits on
// the register. Return value is never used
// because callback.funcptr is zero
return NULL; //was just `return;`
}

if(callbackOrFlags == -2)
{
void* ptrDest = DECOMP_LOAD_ReadFile(bigfilePtr, LT_DRAM, subfileIndex, 0, DECOMP_LOAD_DramFileCallback);

*ptrDestination = (int)ptrDest;
data.currSlot.ptrDestination = ptrDest;

return ptrDest;
}

// valid callback

return DECOMP_LOAD_ReadFile(bigfilePtr, LT_DRAM, subfileIndex, ptrDestination, DECOMP_LOAD_DramFileCallback);
}
// -- dead code --
52 changes: 27 additions & 25 deletions decompile/General/LOAD/LOAD_18_VramFileCallback.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,39 @@ void DECOMP_LOAD_VramFileCallback(struct LoadQueueSlot* lqs)
{
int* vramBuf = lqs->ptrDestination;

if(vramBuf != 0)
#if 0
// failure is impossible,
// this callback only happens on CdlComplete,
// which indicates a successful read
if(vramBuf == 0) while(1) {}
#endif

struct VramHeader* vh = (struct VramHeader*)vramBuf;

// if multiple TIMs are packed together
if(vramBuf[0] == 0x20)
{
struct VramHeader* vh = (struct VramHeader*)vramBuf;
int size = vramBuf[1];
vh = (struct VramHeader*)&vramBuf[2];

// if multiple TIMs are packed together
if(vramBuf[0] == 0x20)
{
int size = vramBuf[1];
vh = (struct VramHeader*)&vramBuf[2];

while(size != 0)
{
LoadImage(&vh->rect, VRAMHEADER_GETPIXLES(vh));

vramBuf = (int*)vh;
vramBuf = &vramBuf[size>>2];
size = vramBuf[0];
vh = (struct VramHeader*)&vramBuf[1];
}
}

// if just one TIM
else
while(size != 0)
{
LoadImage(&vh->rect, VRAMHEADER_GETPIXLES(vh));

vramBuf = (int*)vh;
vramBuf = &vramBuf[size>>2];
size = vramBuf[0];
vh = (struct VramHeader*)&vramBuf[1];
}
}

sdata->frameFinishedVRAM = sdata->gGT->frameTimer_VsyncCallback;
// if just one TIM
else
{
LoadImage(&vh->rect, VRAMHEADER_GETPIXLES(vh));
}

// BUGFIX: The OG CTR can crash in the loading screen
if (sdata->frameFinishedVRAM == 0)
sdata->frameFinishedVRAM = 1;
// wait 2 frames before starting the next load queue,
// this is handled in vsync callback.
sdata->frameFinishedVRAM = 2;
}
48 changes: 14 additions & 34 deletions decompile/General/LOAD/LOAD_19_VramFile.c
Original file line number Diff line number Diff line change
@@ -1,42 +1,22 @@
#include <common.h>

void* DECOMP_LOAD_VramFile(void* bigfilePtr, int subfileIndex, int* ptrDestination, int callbackOrFlags)
void* DECOMP_LOAD_VramFile(void* bigfilePtr, int subfileIndex)
{
struct LoadQueueSlot lqs;

if(callbackOrFlags == -1)
{
lqs.ptrBigfileCdPos = bigfilePtr;
lqs.flags = 0;
lqs.type = LT_VRAM;
lqs.subfileIndex = subfileIndex;
lqs.ptrDestination = DECOMP_LOAD_ReadFile(bigfilePtr, LT_VRAM, subfileIndex, 0, 0);
lqs.callback.funcPtr = NULL;

DECOMP_LOAD_VramFileCallback(&lqs);

VSync(2);

sdata->frameFinishedVRAM = 0;

// optimization, dont use instructions writing
// return value, let it use whatever sits on
// the register. Return value is never used
// because callback.funcptr is zero
return NULL; //was just `return;`
}

if(callbackOrFlags == -2)
{
void* ptrDest = DECOMP_LOAD_ReadFile(bigfilePtr, LT_VRAM, subfileIndex, 0, &DECOMP_LOAD_VramFileCallback);
*ptrDestination = (int)ptrDest;
data.currSlot.ptrDestination = ptrDest;
return ptrDest;
}
// callback=0 means this calls CdSync and is not async
lqs.ptrDestination = DECOMP_LOAD_ReadFile(bigfilePtr, subfileIndex, 0, 0);

// undo allocation, next LOAD queue request
// will overwrite where VRAM was in RAM
DECOMP_MEMPACK_ReallocMem(0);

// run callback
DECOMP_LOAD_VramFileCallback(&lqs);

// valid callback
// wait 2 frames for LoadImage to finish
VSync(2);

return DECOMP_LOAD_ReadFile(bigfilePtr, LT_VRAM, subfileIndex, ptrDestination, &DECOMP_LOAD_VramFileCallback);
// do NOT trigger code in vsync callback to handle queue
sdata->frameFinishedVRAM = 0;
}
9 changes: 5 additions & 4 deletions decompile/General/LOAD/LOAD_21_ReadFile.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <common.h>

void* DECOMP_LOAD_ReadFile(struct BigHeader* bigfile, u_int loadType, int subfileIndex, void *ptrDst, void * callback)
void* DECOMP_LOAD_ReadFile(struct BigHeader* bigfile, /*u_int loadType,*/ int subfileIndex, void *ptrDst, void * callback)
{
// param1 is the Pointer to CD position of BIGFILE

Expand Down Expand Up @@ -33,9 +33,6 @@ void* DECOMP_LOAD_ReadFile(struct BigHeader* bigfile, u_int loadType, int subfil
// undo sector-align alloc,
// allocate just "needed" bytes
DECOMP_MEMPACK_ReallocMem(eSize);

if (loadType == LT_VRAM)
DECOMP_MEMPACK_ReallocMem(0);
}

sdata->callbackCdReadSuccess = 0;
Expand Down Expand Up @@ -84,6 +81,10 @@ void* DECOMP_LOAD_ReadFile(struct BigHeader* bigfile, u_int loadType, int subfil
}

#endif

#if defined(REBUILD_PC) || defined(USE_PCDRV)
DECOMP_LOAD_ReadFileASyncCallback(CdlComplete, NULL);
#endif

return ptrDst;
}
14 changes: 7 additions & 7 deletions decompile/General/LOAD/LOAD_31_DriverMPK.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ void highMp_DriverMPK(unsigned int param_1,int levelLOD, void (*param_3)(struct
// high lod CTR model
DECOMP_LOAD_AppendQueue(param_1,LT_DRAM,
BI_RACERMODELHI + data.characterIDs[i],
&data.driverModelExtras[i],(void(*)(struct LoadQueueSlot*))0xfffffffe);
&data.driverModelExtras[i],(void(*)(struct LoadQueueSlot*))0);
}

// Time Trial MPK
Expand Down Expand Up @@ -111,7 +111,7 @@ void DECOMP_LOAD_DriverMPK(unsigned int param_1,int levelLOD, void (*param_3)(st
// low lod CTR model
DECOMP_LOAD_AppendQueue(param_1,LT_DRAM,
BI_RACERMODELLOW + data.characterIDs[i],
&data.driverModelExtras[i],(void(*)(struct LoadQueueSlot*))0xfffffffe);
&data.driverModelExtras[i],(void(*)(struct LoadQueueSlot*))0);
}

// load 4P MPK of fourth player
Expand Down Expand Up @@ -173,7 +173,7 @@ void DECOMP_LOAD_DriverMPK(unsigned int param_1,int levelLOD, void (*param_3)(st
// high lod model (temporary workaround)
DECOMP_LOAD_AppendQueue(param_1,LT_DRAM,
BI_RACERMODELHI + 0xF,
&data.driverModelExtras[0],(void(*)(struct LoadQueueSlot*))0xfffffffe);
&data.driverModelExtras[0],(void(*)(struct LoadQueueSlot*))0);
}

// get rid of oxide cause MPK is too big
Expand All @@ -192,7 +192,7 @@ void DECOMP_LOAD_DriverMPK(unsigned int param_1,int levelLOD, void (*param_3)(st
// high lod model
DECOMP_LOAD_AppendQueue(param_1,LT_DRAM,
BI_RACERMODELHI + data.characterIDs[0],
&data.driverModelExtras[0],(void(*)(struct LoadQueueSlot*))0xfffffffe);
&data.driverModelExtras[0],(void(*)(struct LoadQueueSlot*))0);

// time trial mpk
lastFileIndexMPK = BI_TIMETRIALPACK + data.characterIDs[1];
Expand All @@ -214,7 +214,7 @@ void DECOMP_LOAD_DriverMPK(unsigned int param_1,int levelLOD, void (*param_3)(st
// high lod model
DECOMP_LOAD_AppendQueue(param_1,LT_DRAM,
BI_RACERMODELHI + data.characterIDs[0],
&data.driverModelExtras[0],(void(*)(struct LoadQueueSlot*))0xfffffffe);
&data.driverModelExtras[0],(void(*)(struct LoadQueueSlot*))0);

// pack of four AIs with bosses
lastFileIndexMPK = BI_2PARCADEPACK + 7;
Expand All @@ -241,7 +241,7 @@ void DECOMP_LOAD_DriverMPK(unsigned int param_1,int levelLOD, void (*param_3)(st
// high lod CTR model
DECOMP_LOAD_AppendQueue(param_1,LT_DRAM,
BI_RACERMODELHI + data.characterIDs[i],
&data.driverModelExtras[i],(void(*)(struct LoadQueueSlot*))0xfffffffe);
&data.driverModelExtras[i],(void(*)(struct LoadQueueSlot*))0);
}

// time trial mpk
Expand All @@ -258,7 +258,7 @@ void DECOMP_LOAD_DriverMPK(unsigned int param_1,int levelLOD, void (*param_3)(st
// med lod CTR model
DECOMP_LOAD_AppendQueue(param_1,LT_DRAM,
BI_RACERMODELMED + data.characterIDs[i],
&data.driverModelExtras[i],(void(*)(struct LoadQueueSlot*))0xfffffffe);
&data.driverModelExtras[i],(void(*)(struct LoadQueueSlot*))0);
}

// this also loads MPK
Expand Down
2 changes: 1 addition & 1 deletion decompile/General/LOAD/LOAD_32_LangFile.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void DECOMP_LOAD_LangFile(int bigfilePtr, int lang)

lngFile =
DECOMP_LOAD_ReadFile(
(struct BigHeader*)bigfilePtr, 1, BI_LANGUAGEFILE + lang,
(struct BigHeader*)bigfilePtr, BI_LANGUAGEFILE + lang,
(void*)sdata->lngFile, NULL);

// This is not ReadFileAsync, this is ReadFile,
Expand Down
4 changes: 2 additions & 2 deletions decompile/General/LOAD/LOAD_34_AppendQueue.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ void DECOMP_LOAD_AppendQueue(int bigfile, int type, int fileIndex, void* destina
{
struct LoadQueueSlot* lqs;

#ifdef USE_BIGQUEUE
#ifndef USE_BIGQUEUE
if(sdata->queueLength >= 8)
return;
#endif
Expand All @@ -15,7 +15,7 @@ void DECOMP_LOAD_AppendQueue(int bigfile, int type, int fileIndex, void* destina
lqs->type = type;
lqs->subfileIndex = fileIndex;
lqs->ptrDestination = destinationPtr;
lqs->callback.funcPtr = callback;
lqs->callbackFuncPtr = callback;

sdata->queueLength++;

Expand Down
5 changes: 2 additions & 3 deletions decompile/General/LOAD/LOAD_35_CDRequestCallback.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
void DECOMP_LOAD_CDRequestCallback(struct LoadQueueSlot* lqs)
{
sdata->queueReady = 1;

if (lqs->callback.funcPtr != 0)
lqs->callback.funcPtr(lqs);
if (lqs->callbackFuncPtr != 0)
lqs->callbackFuncPtr(lqs);
}
Loading

0 comments on commit fbb3a19

Please sign in to comment.