Make browser main-loop ownership explicit when changing VSync mode
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 48/100
Research direction
Start with psyz/src/platform/sdl3_gl.c at Psyz_VideoSetVsyncMode and trace how ConfigureVSync calls SDL_GL_SetSwapInterval; compare that with the Emscripten backend behavior cited in the issue. The standalone scheduler.c reproduction shows the scheduler change, and the requested outcome is an explicit opt-out or a documented ownership contract. Decide which approach fits the project before changing the VSync behavior.
Written by the indexing model from the issue text.
Description
In an Emscripten application using emscripten_set_main_loop(..., 0, ...), changing PsyZ's VSync mode can also change the scheduler of the application's entire main loop. An application expecting RAF-driven callbacks can silently end up running on timers.
Reproduce without game code
Save the attached scheduler.c as assets/scheduler.c next to the following CMakeLists.txt:
CMakeLists.txt
cmake_minimum_required(VERSION 3.21)
project(psyz_scheduler_probe C CXX)
if(NOT EMSCRIPTEN OR NOT IS_DIRECTORY "${PSYZ_ROOT}/psyz")
message(FATAL_ERROR "Use emcmake and -DPSYZ_ROOT=<PsyZ checkout>")
endif()
add_compile_options(-pthread)
add_link_options(-pthread)
set(SDL_PTHREADS ON CACHE BOOL "" FORCE)
add_subdirectory("${PSYZ_ROOT}/psyz" psyz EXCLUDE_FROM_ALL)
add_executable(scheduler_probe assets/scheduler.c)
target_link_libraries(scheduler_probe PRIVATE psyz)
psyz_web_configure_target(scheduler_probe)
Build with PsyZ and Emscripten:
emcmake cmake -S . -B build -DPSYZ_ROOT=/path/to/psyz -DCMAKE_BUILD_TYPE=Release
cmake --build build --parallel 8
Serve build/ with COOP/COEP headers (same-origin / require-corp), open index.html, and inspect the browser console. No disc or game assets are required. The example initializes video inside an RAF-driven main loop and calls VSync(0) once to create the backend and pump deferred SDL setup. It then changes the three VSync modes and queries emscripten_get_main_loop_timing after each change.
Observed output from the standalone example with unmodified PsyZ and SDL:
PSYZ_VSYNC_ON: RAF, value=1
PSYZ_VSYNC_OFF: SETTIMEOUT, value=0
PSYZ_VSYNC_LIMITLESS: SETTIMEOUT, value=0
The intended outcome is not necessarily to change these defaults. An application-owned main loop needs either an explicit opt-out from scheduler changes or a clearly documented ownership contract.
Cause
Psyz_VideoSetVsyncMode calls ConfigureVSync, which calls SDL_GL_SetSwapInterval. In SDL's Emscripten backend, interval zero calls emscripten_set_main_loop_timing(EM_TIMING_SETTIMEOUT, 0); a positive interval selects RAF. The existing SDL main-callback/hint exception does not apply to our direct Emscripten loop.
This is an SDL side effect exposed through PsyZ, not evidence of a defect in SDL's implementation.
Why it mattered in our port
Our new high-refresh loop incorrectly assumed it was still driven by RAF. With a stationary scene and an unmapped key repeatedly pressed, its render rate dropped while an independent RAF counter stayed near 120/s. A Chrome trace showed MainLoop_runner executing under TimerFire. Restoring RAF and restarting the loop to invalidate pending timer runners removed the drop:
| Same key-repeat test | Render submissions/s | Simulation steps/s | Browser RAF callbacks/s |
|---|---|---|---|
| Timer-driven game loop | 111.12 | 30.09 | 119.86 |
| RAF-driven game loop | 120.01 | 29.97 | 120.01 |
These eight-second measurements are from our modified Rewired loop on macOS Chrome, not a claim that every PsyZ application loses FPS on keyboard input. The standalone example reproduces the scheduler change only, not the full performance regression. Render/RAF counters do not measure physical display delivery.
Reference
Standalone reproduction tested on macOS, Chrome 157.0.8103.38 and Emscripten 6.0.13-git.
- PsyZ:
35c673e. - SDL:
fa2c02b, Emscripten swap interval.
Related: #232. This issue is about ownership of browser scheduling; separating presentation from the emulated VBlank API is independent.
- Dominant language
- C
- Stars
- 107
- Forks
- 26
- Avg merge
- 2d 21h
- Merged PRs (30d)
- 5
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from Xeeynamo/psyz
-
Difficulty 1/5 Under an hour Newbie friendliness 90/100
-
Difficulty 4/5 3-5 days Newbie friendliness 48/100
-
Difficulty 5/5 Over a week Newbie friendliness 38/100
-
Difficulty 5/5 Over a week Newbie friendliness 38/100
-
Difficulty 5/5 Over a week Newbie friendliness 35/100
Similar issues
-
GXF: heap buffer overflow in parse_ad_cdp() — unbounded cc_count in CDP cc data sectionPossibly taken @pranayr710 claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
CCExtractor/ccextractor#2498 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 92/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
area: services kind: bug preview-effort: small priority: low
Difficulty 1/5 Under an hour Newbie friendliness 85/100
Maintainers usually reply within 1 day
-
Static Analysis failurePossibly taken A pull request linked to this issue is open or already merged. Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100