Make browser main-loop ownership explicit when changing VSync mode

Aperta
#239 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
48/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
c
Ambito
backend, web-dev

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
C
Stelle
107
Fork
26
Merge medio
2g 21h
PR unite (30g)
5

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di Xeeynamo/psyz

Tutte le issue di Xeeynamo/psyz

Issue simili

Altre issue su C

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.