Browser VBlank waits follow requestAnimationFrame frequency in PS1 timing mode

Open
#232 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
Clearly specified
Activity status
Active
Tech stack
c

Research direction

Read psyz/src/platform/sdl3_common.h::WaitForNextFrame and psyz/src/web/web_timing.c to trace how browser callbacks and emulated VBlank timing interact. Start by reproducing the issue with the presentation sample and its controlled timer diagnostic; compare iteration rates at different callback frequencies. Done when internal-limiter PS1 timing follows emulated time within host performance limits, with presentation cadence treated separately.

Written by the indexing model from the issue text.

Description

In PsyZ 35c673ecf1c4ba799a812d701918c2c52935475b, the browser branch of
WaitForNextFrame waits for one requestAnimationFrame callback per requested
VBlank. It does not use the emulated NTSC/PAL duration there, even when
PSYZ_VSYNC_OFF selects the internal limiter.

Reproduction

Run the shared presentation sample, which selects
NTSC, the internal limiter and a two-VBlank wait per application iteration.
On actual hardware, compare it on different display refresh rates.

For a repeatable software diagnostic, measure.js replaces RAF with
a controlled timer callback before loading the sample. The measured iteration
rate changes substantially with callback frequency:

Requested callback rate Unmodified PsyZ Local timing prototype
30/s 13.3/s 25.8/s
60/s 25.4/s 28.3/s
120/s 44.1/s 28.3/s

Controlled callback experiment

These are software-timer measurements in headless Chromium, not physical
30/60/120 Hz monitor measurements
. Timer, main-loop and rendering overhead are
included. The NTSC two-VBlank target is about 29.97 iterations/s; the local
prototype is closer but this test does not establish perfect pacing.

Expected behavior and scope

Internal-limiter/PS1 timing should be governed by emulated time, independently
of callback frequency, within host performance limits. Presentation cadence and
emulated VBlank advancement are separate concerns. Explicit display-sync or
uncapped modes may intentionally behave differently.

The relevant paths are psyz/src/platform/sdl3_common.h::WaitForNextFrame and
psyz/src/web/web_timing.c. Our current delay-based workaround is evidence for
the diagnosis, not a complete solution for one-VBlank loops, background tabs,
PAL, variable refresh or catch-up policy.

To run the optional timer experiment, install playwright in a scratch directory, save the attached script as measure.cjs (CommonJS), and run:

node measure.cjs http://127.0.0.1:8201/index.html

Set CHROME to your installed Chromium executable, or install Playwright's Chromium with npx playwright install chromium. The script overrides RAF only in its isolated test pages.

Recorded browser environment: Emscripten 6.0.13, Chromium 157 (headless SwiftShader), macOS arm64, device scale factor 1. Comparisons use local port prototypes, not hardware reference captures.

Build and run the standalone presentation sample

Download presentation.c and shell.html into the same directory. No game source, executable or assets are needed. Add this CMakeLists.txt:

cmake_minimum_required(VERSION 3.21)
project(presentation_repro C CXX)
set(CMAKE_C_STANDARD 11)
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(presentation presentation.c)
target_link_libraries(presentation PRIVATE psyz)
target_link_options(presentation PRIVATE -sASSERTIONS=1 -sPTHREAD_POOL_SIZE=2)
psyz_web_configure_target(presentation OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/site")
get_target_property(options presentation LINK_OPTIONS)
list(FILTER options EXCLUDE REGEX "^--shell-file=")
set_property(TARGET presentation PROPERTY LINK_OPTIONS "${options}")
target_link_options(presentation PRIVATE "--shell-file=${CMAKE_CURRENT_https://github.com/user-attachments/files/34581629/presentation.c_DIR}/shell.html")

Use a PsyZ checkout at the recorded revision, with its SDL submodule initialized:

emcmake cmake -S . -B build -DPSYZ_ROOT=/path/to/psyz -DCMAKE_BUILD_TYPE=Release
cmake --build build -j8

Serve build/site locally with Cross-Origin-Opener-Policy: same-origin and Cross-Origin-Embedder-Policy: require-corp, as required by Emscripten pthreads, then open index.html.

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.