Make browser main-loop ownership explicit when changing VSync mode

Open
#239 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
48/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
c
Domain
backend, web-dev

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

Same unmapped-key repeat test before and after restoring RAF

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.

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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Xeeynamo/psyz

All issues in Xeeynamo/psyz

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.