Skip to content

Commit

Permalink
dynamic gpu deferring
Browse files Browse the repository at this point in the history
  • Loading branch information
Niko committed Jul 11, 2024
1 parent 8a28ea8 commit 8bf5fa0
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 14 deletions.
7 changes: 5 additions & 2 deletions decompile/General/AltMods/OnlineCTR/global.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,12 @@ struct OnlineCTR
// Last windowsClientSync counter
char lastWindowsClientSync;

char desiredFPS;

// control when PSX and PC send/recv
int sleepControl;
int desiredFPS;
char sleepControl;
char gpuSubmitTooLate;
char enableDeferredGPU;
};

#define MAX_LAPS 7
Expand Down
5 changes: 3 additions & 2 deletions decompile/General/MAIN/MainFrame_GameLogic.c
Original file line number Diff line number Diff line change
Expand Up @@ -275,11 +275,12 @@ void DECOMP_MainFrame_GameLogic(struct GameTracker* gGT, struct GamepadSystem* g
}
}

octr->desiredFPS = FPS_DOUBLE(30);
octr->sleepControl = 1;
octr->desiredFPS = FPS_DOUBLE(30);

// stall
FrameStall();
if(octr->enableDeferredGPU == 1)
FrameStall();
}

for(int other = 1; other < 8; other++)
Expand Down
21 changes: 21 additions & 0 deletions decompile/General/MAIN/MainFrame_RenderFrame.c
Original file line number Diff line number Diff line change
Expand Up @@ -1680,6 +1680,10 @@ int ReadyToBreak(struct GameTracker* gGT)
gGT->vSync_between_drawSync > 6;
}

#ifdef USE_ONLINE
#include "../AltMods/OnlineCTR/global.h"
#endif

void RenderVSYNC(struct GameTracker* gGT)
{
// render checkered flag
Expand All @@ -1688,6 +1692,10 @@ void RenderVSYNC(struct GameTracker* gGT)
VSync(0);
}

#ifdef USE_ONLINE
int boolFirstFrame = 1;
#endif

while(1)
{

Expand All @@ -1699,9 +1707,22 @@ void RenderVSYNC(struct GameTracker* gGT)

if(ReadyToFlip(gGT))
{

#ifdef USE_ONLINE
if(boolFirstFrame)
octr->gpuSubmitTooLate = 1;
#endif

// quit, end of stall
return;
}

#ifdef USE_ONLINE
// gpu submission is not too late,
// we got to this while() loop before
// the flip was ready, so we're on-time
boolFirstFrame = 0;
#endif

#ifndef REBUILD_PC
if(ReadyToBreak(gGT))
Expand Down
51 changes: 41 additions & 10 deletions mods/Windows/OnlineCTR/Network_PC/Client/CL_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1170,11 +1170,24 @@ int main()
atexit(enet_deinitialize);
printf("Client: Waiting for the OnlineCTR binary to load... ");

// 5ms sleep by default
int sleepCount = 5000;
int enableDeferredGPU = 1;

while (1)
{
// To do: Check for PS1 system clock tick then run the client update
octr->windowsClientSync++;

if (octr->windowsClientSync == 0)
{
// On Niko's computer
// 30fps 1x resolution = 4500
// 30fps 9x resolution = 2500
// 60fps = 0
//printf("Debug: SleepCount=%d\n", sleepCount);
}

// should rename to room selection
if (octr->CurrState >= LAUNCH_PICK_ROOM)
DisconSELECT();
Expand All @@ -1188,16 +1201,34 @@ int main()
// Send data
if (octr->CurrState >= 0)
ClientState[octr->CurrState]();

// wait a bit, to RECV other messages
// 1,000,000 = 1 second
// 33,333 = 1 frame
// 15000 = half frame,
// duckstation overclock will compensate
if(octr->desiredFPS == 30)
usleep(15000); // half-frame 30fps
else
usleep(3000); // fifth-frame 60fps

// check for frame lag
if (octr->gpuSubmitTooLate == 1)
{
octr->gpuSubmitTooLate = 0;

// if 1-9 frame stalls
if (sleepCount >= 500)
{
// remove from sleep
sleepCount -= 500;
}

// if 10+ frame stalls
else
{
sleepCount = 0;
enableDeferredGPU = 0;
}
}

// PC writes to PSX,
// PSX is read-only
octr->enableDeferredGPU = enableDeferredGPU;

// delay GPU between SEND and RECV
if(enableDeferredGPU == 1)
usleep(sleepCount);

// now check for new RECV message
ProcessNewMessages();
Expand Down

0 comments on commit 8bf5fa0

Please sign in to comment.