-
Notifications
You must be signed in to change notification settings - Fork 168
Adding 30 FPS VSYNC sample #308
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| TARGET = vsync30FPS | ||
| OBJS = vsync30FPS.o ../common/callbacks.o | ||
|
|
||
| INCDIR = | ||
| CFLAGS = -Wall -O2 | ||
| CXXFLAGS = $(CFLAGS) -fno-exceptions -fno-rtti | ||
| ASFLAGS = $(CFLAGS) | ||
|
|
||
| LIBDIR = | ||
| LDFLAGS = | ||
| LIBS= -lpspgu | ||
|
|
||
| EXTRA_TARGETS = EBOOT.PBP | ||
| PSP_EBOOT_TITLE = VSYNC 30FPS Sample | ||
|
|
||
| PSPSDK=$(shell psp-config --pspsdk-path) | ||
| include $(PSPSDK)/lib/build.mak |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,185 @@ | ||
| /* | ||
| * PSP Software Development Kit - https://github.com/pspdev | ||
| * ----------------------------------------------------------------------- | ||
| * Licensed under the BSD license, see LICENSE in PSPSDK root for details. | ||
| * | ||
| * VSYNC 30FPS Sample | ||
| * With the sceKernelRegisterSubIntrHandler(PSP_VBLANK_INT, 0, on_vblank, NULL);, | ||
| * the on_vblank function is going to be executed each PSP_VBLANK_INT interruption. | ||
| * To force 30 FPS, we must ensure the VBLANK interruption happens exactly twice, | ||
| * in order to achieve this, we have used 2 semaphores, so before swapping buffers, | ||
| * we need to ensure that both semaphores are being signaled already, | ||
| * and this is what vsync30 method does. | ||
| * | ||
| * How there are plenty of applications whose FPS are set to 30FPS, | ||
| * this is just an example of how we could achieve this. | ||
| * | ||
| * Copyright (c) 2005 Jesper Svennevid | ||
| * Copyright (c) 2025, Francisco Javier Trujillo Mata <[email protected]> | ||
| */ | ||
|
|
||
| #include <pspkernel.h> | ||
| #include <pspdisplay.h> | ||
| #include <pspdebug.h> | ||
| #include <stdlib.h> | ||
| #include <stdio.h> | ||
| #include <math.h> | ||
| #include <string.h> | ||
| #include <time.h> | ||
|
|
||
| #include <pspgu.h> | ||
|
|
||
| #include "../common/callbacks.h" | ||
|
|
||
| PSP_MODULE_INFO("VSYNC 30FPS Sample", 0, 1, 1); | ||
| PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER); | ||
|
|
||
| #define BUF_WIDTH (512) | ||
| #define SCR_WIDTH (480) | ||
| #define SCR_HEIGHT (272) | ||
|
|
||
| static unsigned int __attribute__((aligned(16))) list[262144]; | ||
| static unsigned int __attribute__((aligned(16))) pixels[512 * 272]; | ||
|
|
||
| static int vsync_sema_1st_id; | ||
| static int vsync_sema_2nd_id; | ||
| static int vsync_sema_id = -1; | ||
|
|
||
| struct Vertex | ||
| { | ||
| unsigned short u, v; | ||
| unsigned short color; | ||
| short x, y, z; | ||
| }; | ||
|
|
||
| static void on_vblank(int sub, void *data) | ||
| { | ||
| sceKernelSignalSema(vsync_sema_id ? vsync_sema_2nd_id : vsync_sema_1st_id, 1); | ||
| vsync_sema_id ^= 1; | ||
| } | ||
|
|
||
| static void vsync30(void) | ||
| { | ||
| sceKernelWaitSema(vsync_sema_1st_id, 1, NULL); | ||
| sceKernelWaitSema(vsync_sema_2nd_id, 1, NULL); | ||
| } | ||
|
|
||
| static void prepare_sema(void) | ||
| { | ||
| vsync_sema_1st_id = sceKernelCreateSema("vsync_sema_1st", 0, 0, 1, NULL); | ||
| vsync_sema_2nd_id = sceKernelCreateSema("vsync_sema_2nd", 0, 0, 1, NULL); | ||
| } | ||
|
|
||
| int main(int argc, char *argv[]) | ||
| { | ||
| unsigned int x, y; | ||
|
|
||
| setupCallbacks(); | ||
|
|
||
| // setup | ||
|
|
||
| void *fbp0 = guGetStaticVramBuffer(BUF_WIDTH, SCR_HEIGHT, GU_PSM_8888); | ||
| void *fbp1 = guGetStaticVramBuffer(BUF_WIDTH, SCR_HEIGHT, GU_PSM_8888); | ||
| void *zbp = guGetStaticVramBuffer(BUF_WIDTH, SCR_HEIGHT, GU_PSM_4444); | ||
|
|
||
| pspDebugScreenInit(); | ||
| sceGuInit(); | ||
|
|
||
| sceGuStart(GU_DIRECT, list); | ||
| sceGuDrawBuffer(GU_PSM_8888, fbp0, BUF_WIDTH); | ||
| sceGuDispBuffer(SCR_WIDTH, SCR_HEIGHT, fbp1, BUF_WIDTH); | ||
| sceGuDepthBuffer(zbp, BUF_WIDTH); | ||
| sceGuOffset(2048 - (SCR_WIDTH / 2), 2048 - (SCR_HEIGHT / 2)); | ||
| sceGuViewport(2048, 2048, SCR_WIDTH, SCR_HEIGHT); | ||
| sceGuDepthRange(65535, 0); | ||
| sceGuScissor(0, 0, SCR_WIDTH, SCR_HEIGHT); | ||
| sceGuEnable(GU_SCISSOR_TEST); | ||
| sceGuFrontFace(GU_CW); | ||
| sceGuClear(GU_COLOR_BUFFER_BIT | GU_DEPTH_BUFFER_BIT); | ||
| sceGuFinish(); | ||
| sceGuSync(GU_SYNC_FINISH, GU_SYNC_WHAT_DONE); | ||
|
|
||
| prepare_sema(); | ||
| vsync_sema_id = 0; | ||
| sceKernelRegisterSubIntrHandler(PSP_VBLANK_INT, 0, on_vblank, NULL); | ||
| sceKernelEnableSubIntr(PSP_VBLANK_INT, 0); | ||
|
|
||
| sceDisplayWaitVblankStart(); | ||
| sceGuDisplay(1); | ||
|
|
||
| int val = 0; | ||
|
|
||
| // generate dummy image to blit | ||
|
|
||
| for (y = 0; y < 272; ++y) | ||
| { | ||
| unsigned int *row = &pixels[y * 512]; | ||
| for (x = 0; x < 480; ++x) | ||
| { | ||
| row[x] = x * y; | ||
| } | ||
| } | ||
|
|
||
| sceKernelDcacheWritebackAll(); | ||
|
|
||
| float curr_ms = 1.0f; | ||
| struct timeval time_slices[16]; | ||
|
|
||
| void *framebuffer = 0; | ||
|
|
||
| while (running()) | ||
| { | ||
| sceGuStart(GU_DIRECT, list); | ||
|
|
||
| // copy image from ram to vram | ||
|
|
||
| sceGuCopyImage(GU_PSM_8888, 0, 0, 480, 272, 512, pixels, 0, 0, 512, (void *)(0x04000000 + (u32)framebuffer)); | ||
| sceGuTexSync(); | ||
|
|
||
| sceGuFinish(); | ||
| sceGuSync(GU_SYNC_FINISH, GU_SYNC_WHAT_DONE); | ||
|
|
||
| float curr_fps = 1.0f / curr_ms; | ||
|
|
||
| vsync30(); | ||
| pspDebugScreenSetOffset((int)framebuffer); | ||
|
|
||
| pspDebugScreenSetXY(0, 0); | ||
| pspDebugScreenPrintf("%d.%03d FPS", (int)curr_fps, (int)((curr_fps - (int)curr_fps) * 1000.0f)); | ||
|
|
||
| framebuffer = sceGuSwapBuffers(); | ||
| // simple frame rate counter | ||
|
|
||
| gettimeofday(&time_slices[val & 15], 0); | ||
|
|
||
| val++; | ||
|
|
||
| if (!(val & 15)) | ||
| { | ||
| struct timeval last_time = time_slices[0]; | ||
| unsigned int i; | ||
|
|
||
| curr_ms = 0; | ||
| for (i = 1; i < 16; ++i) | ||
| { | ||
| struct timeval curr_time = time_slices[i]; | ||
|
|
||
| if (last_time.tv_usec > curr_time.tv_usec) | ||
| { | ||
| curr_time.tv_sec++; | ||
| curr_time.tv_usec -= 1000000; | ||
| } | ||
|
|
||
| curr_ms += ((curr_time.tv_usec - last_time.tv_usec) + (curr_time.tv_sec - last_time.tv_sec) * 1000000) * (1.0f / 1000000.0f); | ||
|
|
||
| last_time = time_slices[i]; | ||
| } | ||
| curr_ms /= 15.0f; | ||
| } | ||
| } | ||
|
|
||
| sceGuTerm(); | ||
|
|
||
| sceKernelExitGame(); | ||
| return 0; | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.