Commit 0341ec93fee033cfb81d5a5fae988bd6bb12defc

Francisco Javier Trujillo Mata 2021-11-26T11:34:40

Improve performance when vsync is enabled and the game is not reaching the 60 FPS

diff --git a/src/render/psp/SDL_render_psp.c b/src/render/psp/SDL_render_psp.c
index ebeddc2..124ede3 100644
--- a/src/render/psp/SDL_render_psp.c
+++ b/src/render/psp/SDL_render_psp.c
@@ -80,6 +80,8 @@ typedef struct
     int             currentBlendMode;
 
     PSP_DrawStateCache drawstate;
+
+    SDL_bool        vblank_not_reached;                  /**< wether vblank wasn't reached */
 } PSP_RenderData;
 
 
@@ -175,6 +177,12 @@ TextureNextPow2(unsigned int w)
     return n;
 }
 
+static void psp_on_vblank(u32 sub, PSP_RenderData *data)
+{
+   if (data)
+      data->vblank_not_reached = SDL_FALSE;
+}
+
 
 static int
 PixelFormatToPSPFMT(Uint32 format)
@@ -1009,8 +1017,9 @@ PSP_RenderPresent(SDL_Renderer * renderer)
     sceGuFinish();
     sceGuSync(0,0);
 
-/*  if(data->vsync) */
+    if ((data->vsync) && (data->vblank_not_reached))
         sceDisplayWaitVblankStart();
+    data->vblank_not_reached = SDL_TRUE;
 
     data->backbuffer = data->frontbuffer;
     data->frontbuffer = vabsptr(sceGuSwapBuffers());
@@ -1044,6 +1053,10 @@ PSP_DestroyRenderer(SDL_Renderer * renderer)
 
         StartDrawing(renderer);
 
+        sceKernelDisableSubIntr(PSP_VBLANK_INT, 0);
+        sceKernelReleaseSubIntrHandler(PSP_VBLANK_INT,0);
+        sceDisplayWaitVblankStart();
+        sceGuDisplay(GU_FALSE);
         sceGuTerm();
 /*      vfree(data->backbuffer); */
 /*      vfree(data->frontbuffer); */
@@ -1179,6 +1192,11 @@ PSP_CreateRenderer(SDL_Window * window, Uint32 flags)
     sceDisplayWaitVblankStartCB();
     sceGuDisplay(GU_TRUE);
 
+    /* Improve performance when VSYC is enabled and it is not reaching the 60 FPS */
+    data->vblank_not_reached = SDL_TRUE;
+    sceKernelRegisterSubIntrHandler(PSP_VBLANK_INT, 0, psp_on_vblank, data);
+    sceKernelEnableSubIntr(PSP_VBLANK_INT, 0);
+
     return renderer;
 }