Skip to content

Commit 9125cf8

Browse files
authored
Merge pull request #97 from rickgaiser/hires_ratelimit
Add fps rate limiter
2 parents df911e9 + 326b3f7 commit 9125cf8

4 files changed

Lines changed: 39 additions & 0 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ add_object_library_macros(GS_HIRES_OBJS ee/gs/src/gsHires.c
165165
_gsKit_create_passes
166166
gsKit_hires_sync
167167
gsKit_hires_flip
168+
gsKit_hires_flip_ext
168169
gsKit_hires_prepare_bg
169170
gsKit_hires_set_bg
170171
gsKit_hires_init_screen

ee/gs/include/gsCore.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ struct gsVertex
8282
};
8383
typedef struct gsVertex GSVERTEX;
8484

85+
/// Flip mode for gsKit_hires_flip_ext
86+
typedef enum {
87+
GSFLIP_DIRECT = 0, ///< Flip immediately, no vsync wait
88+
GSFLIP_VSYNC = 1, ///< Wait for vsync before flipping
89+
GSFLIP_RATE_LIMIT_1 = 2, ///< Limit to 60fps (1 vsync per frame)
90+
GSFLIP_RATE_LIMIT_2 = 3 ///< Limit to 30fps (2 vsyncs per frame)
91+
} GSFLIP_MODE;
92+
8593
#ifdef __cplusplus
8694
extern "C" {
8795
#endif

ee/gs/include/gsHires.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ void gsKit_hires_sync(GSGLOBAL *gsGlobal);
2828
/// Flips Draw Queue
2929
void gsKit_hires_flip(GSGLOBAL *gsGlobal);
3030

31+
/// Flips Draw Queue with specified flip mode
32+
void gsKit_hires_flip_ext(GSGLOBAL *gsGlobal, GSFLIP_MODE mode);
33+
3134
/// Converts PSM and interlacing for use as background image
3235
void gsKit_hires_prepare_bg(GSGLOBAL *gsGlobal, GSTEXTURE * tex);
3336

ee/gs/src/gsHires.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,35 @@ void gsKit_hires_sync(GSGLOBAL *gsGlobal)
260260

261261
#if F_gsKit_hires_flip
262262
void gsKit_hires_flip(GSGLOBAL *gsGlobal)
263+
{
264+
gsKit_hires_flip_ext(gsGlobal, GSFLIP_DIRECT);
265+
}
266+
#endif
267+
268+
#if F_gsKit_hires_flip_ext
269+
// State tracking for rate limiting
270+
static u32 __last_flip_vsync_count = 0;
271+
272+
void gsKit_hires_flip_ext(GSGLOBAL *gsGlobal, GSFLIP_MODE mode)
263273
{
264274
u32 iY;
275+
u32 vsync_end;
276+
277+
// Handle vsync waiting based on mode
278+
switch (mode) {
279+
case GSFLIP_DIRECT: vsync_end = __vsync_count; break;
280+
case GSFLIP_VSYNC: vsync_end = __vsync_count + 1; break;
281+
case GSFLIP_RATE_LIMIT_1: vsync_end = __last_flip_vsync_count + 1; break;
282+
case GSFLIP_RATE_LIMIT_2: vsync_end = __last_flip_vsync_count + 2; break;
283+
}
284+
285+
// Wait for required vsyncs
286+
while (__vsync_count < vsync_end) {
287+
WaitSema(__sema_vsync_id);
288+
}
289+
290+
// Update last flip vsync count
291+
__last_flip_vsync_count = __vsync_count;
265292

266293
// HACK: The start of the first displayed line
267294
// this is needed by the hsync interrupt but must be

0 commit comments

Comments
 (0)