Skip to content

cvcGL: sync-to-vblank off for every cvcgl_* / pycvc GL ctest, not per test - #583

Merged
transfix merged 2 commits into
masterfrom
fix/cvcgl-tests-vsync-off
Oct 19, 2026
Merged

transfix merged 2 commits into
masterfrom
fix/cvcgl-tests-vsync-off

Conversation

@transfix

@transfix transfix commented Oct 8, 2026 •

Copy link
Copy Markdown
Owner

Problem

NVIDIA's GLX driver throttles buffer swaps of an unmapped (offscreen) window to about one per second while sync-to-vblank is on. VTK's offscreen X window calls glXSwapBuffers once per Render(), so an offscreen cvcGL test spends minutes of wall time on seconds of CPU. #512 found this: cvcgl_streaming_overlays took 4m15s wall for 1.7 s CPU, and 4.7 s with __GL_SYNC_TO_VBLANK=0 vblank_mode=0. #512 and two later PRs worked around it one test at a time, with setenv before the first GL context. Every other offscreen GL test (cvcgl_renderer, cvcgl_viewport, cvcgl_lighting, the shadow tests, …) is still throttled.

Change

  • src/cvcGL/CMakeLists.txt: appends __GL_SYNC_TO_VBLANK=0 and vblank_mode=0 to the ENVIRONMENT of every test in the directory. It uses the same DIRECTORY PROPERTY TESTS sweep as the cvcgl_gl_context resource lock just above it, so a new test gets it without asking. APPEND keeps any per-test ENVIRONMENT. The headless and node tests ignore it.
  • bindings/pycvc/CMakeLists.txt: the thirteen tests in the GL block (pycvc_gl_*, pycvc_ari, pycvc_vtk, pycvc_texture) each repeated the same PYTHONPATH/LD_LIBRARY_PATH string. They now share one _PYCVC_GL_TEST_ENV, which also carries the two variables, and a new test copying the pattern picks it up.
  • The per-test workarounds are removed:

There is no setenv and no source change to the library, so Windows and wasm build exactly as before.

One behaviour change: ctest's ENVIRONMENT overrides the caller's environment, whereas the old setenv(…, 0) let an explicit setting win. To time a test with vsync on, run its binary directly. The CMake comment says so.

Why not stop offscreen windows swapping (option a)

vtkRenderWindow::SetSwapBuffers(false) on cvcGL's offscreen ViewportManager window was the first choice. It isn't clean in VTK 9.5:

  • The front buffer goes stale. vtkOpenGLRenderWindow::Frame() gates the whole render-to-display framebuffer resolve on SwapBuffers, not only the platform swap. cvcGL's own writePNG / frameRGB read the back buffer and would be fine. Any front-buffer read through renderWindow(), for example a vtkWindowToImageFilter left at its default ReadFrontBuffer=1, would silently return a stale frame.
  • It doesn't stick. vtkHardwareSelector::EndSelection() calls SwapBuffersOn() unconditionally, without restoring the previous value. cvcgl_lowmem_fastdraw picks on a SceneRenderer window, so the throttle would come back mid-test.
  • The SetDoubleBuffer(0) variant is taken. It would skip only the platform swap, but DoubleBuffer is FrameYield's lever on wasm, and cvcgl_frame_yield asserts that it stays 1 natively.

Production offscreen renders on NVIDIA (outside ctest) are therefore still throttled unless the process sets __GL_SYNC_TO_VBLANK=0. That would be a separate change in the library.

Measurement: 5.1x less wall time on NVIDIA

Measured 2026-10-19 on an NVIDIA GeForce GTX 1650 (driver 595.99.02, Ubuntu 24.04), through the X server of the box's own GNOME session (glxinfo -B: NVIDIA, GL 4.6). nvidia-settings -q SyncToVBlank reads 1, and neither variable was set in the caller. Release, gcc 13.3, deps from cvcpkg (install-deps cvcpkg/recipes/libcvc plus vtk 9.5.0, imgui, sdl3). before is master 9f4587b3; after is this branch's merge b8f89a6f, the same master plus this PR. Both have the same 69 cvcgl_* tests; ctest --show-only=json-v1 shows the variable on 0 of them before and on 69 after.

ctest -R cvcgl_ (NVIDIA, Release) wall user + sys
before (master 9f4587b3) 534.6 s 99.4 + 9.4 s
after (this branch b8f89a6f) 105.3 s 98.8 + 8.2 s
after, second run 102.8 s 98.0 + 8.2 s

The CPU time is the same on both sides; the 430 s difference was all spent waiting on the swap throttle. The biggest drops per test:

test before after
cvcgl_shadow_casters 91.7 s 0.6 s
cvcgl_volren_node 90.1 s 54.8 s
cvcgl_volslice_node 84.5 s 0.5 s
cvcgl_shadow_caster_growth 66.4 s 0.7 s
cvcgl_shadow_pass_reuse 58.6 s 0.5 s
cvcgl_renderer 38.7 s 1.0 s
cvcgl_camera_idle 12.7 s 0.7 s
cvcgl_lod_node 12.4 s 2.5 s
cvcgl_viewport 10.5 s 0.6 s

As predicted, the five tests that already had their own workaround read the same on both sides (each within 0.11 s: cvcgl_streaming_overlays 2.15 / 2.26 s, cvcgl_lowmem_fastdraw 2.56 / 2.58 s).

A pre-existing race surfaced. In the first after run, cvcgl_stage_caster_truth segfaulted; the second run passed 69/69. It is not caused by this PR. Running the binary directly 20 times per case:

binary vsync on (unset) __GL_SYNC_TO_VBLANK=0
master 9f4587b3 20/20 pass 19/20 (1 SIGSEGV)
this branch 20/20 pass 19/20 (1 SIGSEGV)

A gdb backtrace catches two threads in the scene graph at once:

  • Worker thread: state_publisher::worker → flush → cvc::state::value → GraphicsNode::handleStateChanged → SceneNode::runOnMainThread, which runs the lambda inline there and crashes in GraphicsNode::updateOwnClipWorld.
  • Main thread, at the same time: in StageLighting::apply → SceneGraph::addLight → NullGraphicNode::syncBoundsToChildren → updateBoundingBoxNode → BBoxNode::createCoordinateLabels, creating VTK objects.

runOnMainThread runs work inline whenever the node has no scene event sink yet (!events), so a node still being added can take VTK work on the publisher's thread. With vsync on it never crashed in 40 runs, so the throttle's timing hides it (the exact mechanism is not traced). Any run without the throttle can hit it, whether this PR on NVIDIA or another fast driver. It is a separate fix.

To reproduce, use a box with an NVIDIA GPU and an X server (VTK's offscreen GLX path), with the driver's sync-to-vblank at its default (on):

glxinfo -B | grep -E 'OpenGL (vendor|renderer)'   # must say NVIDIA
git fetch origin
git worktree add ../vsync-before origin/master
git worktree add ../vsync-after  origin/fix/cvcgl-tests-vsync-off
for t in before after; do
  cmake -S ../vsync-$t -B ../vsync-$t/build -G Ninja -DCMAKE_BUILD_TYPE=Release \
        -DCVC_BUILD_TESTS=ON -DCVC_BUILD_CVCGL=ON -DCMAKE_PREFIX_PATH="<deps prefix>"
  cmake --build ../vsync-$t/build --parallel
done
for t in before after; do
  env -u __GL_SYNC_TO_VBLANK -u vblank_mode \
    /usr/bin/time -f "$t: %e s wall, %U s user + %S s sys" \
    ctest --test-dir ../vsync-$t/build -R cvcgl_ 2>&1 | tail -n 70
done

before is master, which already has the five per-test workarounds. The delta is therefore the rest of the suite. If the X server belongs to a desktop session, also export its DISPLAY and XAUTHORITY.

What ran locally

  • The CMake mechanism was checked in a stand-in project (CMake 3.22, the same constructs), with the caller's environment set to __GL_SYNC_TO_VBLANK=1:
    • the directory sweep plus APPEND adds both variables to every test;
    • a test's existing ENVIRONMENT is kept;
    • the shared pycvc list keeps the $<TARGET_FILE_DIR:…> generator expression;
    • the tests see __GL_SYNC_TO_VBLANK=0, which is where the override note above comes from.
  • The VTK 9.5.0 reading behind "why not (a)" comes from vtkOpenGLRenderWindow::Frame / ReadPixels, vtkXOpenGLRenderWindow::Frame, vtkHardwareSelector and vtkWindowToImageFilter at v9.5.0.
  • clang-format 18.1.8: no changes on the touched C++ files. The C++ changes are deletions only. <cstdlib> stays in streaming_test_util.h because CVC_REQUIRE_RENDER still uses it, and os stays imported in both Python tests.
  • First draft not built here. The box it was written on can't configure libcvc (no VTK or Boost dev packages).

Merge with master (2026-10-19)

  • master was 131 commits ahead. The only conflict was in bindings/pycvc/CMakeLists.txt: master added pycvc_gl_world_geometry with the old inline PYTHONPATH/LD_LIBRARY_PATH string. It now uses _PYCVC_GL_TEST_ENV like the rest of the block.
  • master added no new disableSwapThrottle() caller and no new per-test vblank setenv. The only one left is cvcgl_lowmem_bench's, on purpose (above).
  • Windows build (MSVC 2022, VTK 9.5.0 and the other deps from a cvcpkg prefix, -DCVC_BUILD_TESTS=ON -DCVC_BUILD_CVCGL=ON -DCVC_BUILD_PYCVC_GL=ON):
    • ctest --show-only=json-v1: all 68 cvcgl_* tests and all 13 pycvc GL tests carry __GL_SYNC_TO_VBLANK=0 and vblank_mode=0, and the pycvc ones keep their PYTHONPATH/LD_LIBRARY_PATH.
    • cvcgl_streaming_geometry, cvcgl_streaming_overlays, cvcgl_clip_children, cvcgl_clip_planes and cvcgl_lowmem_fastdraw (the five that lost their own workaround) pass with real offscreen renders and pixel checks; ctest -V shows both variables in their environment. Windows renders through WGL, not GLX, so this checks correctness, not the speedup.

… test

NVIDIA's GLX driver throttles buffer swaps of an unmapped (offscreen) window
to about one per second while sync-to-vblank is on, so an offscreen GL test
spends minutes of wall time on seconds of CPU (#512: cvcgl_streaming_overlays
took 4m15s wall for 1.7 s CPU, 4.7 s with __GL_SYNC_TO_VBLANK=0). #512 and
later tests worked around it one test at a time, with setenv before the first
GL context. Every other offscreen GL test was still throttled.

- src/cvcGL/CMakeLists.txt: append __GL_SYNC_TO_VBLANK=0 and vblank_mode=0 to
  the ENVIRONMENT of every test in the directory. It uses the same
  DIRECTORY TESTS sweep as the cvcgl_gl_context resource lock, so a new test
  gets it without asking. APPEND keeps any per-test ENVIRONMENT.
- bindings/pycvc/CMakeLists.txt: the twelve GL-block tests (pycvc_gl_*,
  pycvc_ari, pycvc_vtk, pycvc_texture) each repeated the same
  PYTHONPATH/LD_LIBRARY_PATH string. They now share _PYCVC_GL_TEST_ENV, which
  also carries the two variables.
- Drop the per-test workarounds: streaming_test_util.h's
  disableSwapThrottle() and its four callers (cvcgl_streaming_geometry,
  cvcgl_streaming_overlays, cvcgl_clip_children, cvcgl_clip_planes), the
  vblank lines in cvcgl_lowmem_fastdraw, and os.environ.setdefault in
  test_pycvc_gl_streaming.py and test_pycvc_gl_clip.py. cvcgl_lowmem_bench
  keeps its own: it is not a ctest, so the ENVIRONMENT never reaches it.

No setenv is needed, so Windows and wasm build as before. ctest's
ENVIRONMENT overrides the caller's environment; to time a test with vsync on,
run its binary directly.

Why not disable buffer swaps on cvcGL's offscreen windows instead
(vtkRenderWindow::SetSwapBuffers(false))? In VTK 9.5, SwapBuffers off also
skips the render-to-display framebuffer resolve in
vtkOpenGLRenderWindow::Frame(). writePNG/frameRGB read the back buffer and
would be fine. A front-buffer read through renderWindow(), for example a
default vtkWindowToImageFilter, would return a stale frame. vtkHardwareSelector
also calls SwapBuffersOn() unconditionally after every selection
(cvcgl_lowmem_fastdraw picks on a SceneRenderer window). The DoubleBuffer
variant collides with FrameYield, which owns DoubleBuffer, and with
cvcgl_frame_yield's check that DoubleBuffer stays 1 natively.
@transfix
transfix marked this pull request as ready for review October 19, 2026 20:42
…nc-off

# Conflicts:
#	bindings/pycvc/CMakeLists.txt
@transfix
transfix enabled auto-merge (squash) October 19, 2026 21:09
@transfix
transfix merged commit a25ad5a into master Oct 19, 2026
20 of 26 checks passed
transfix added a commit that referenced this pull request Oct 20, 2026
…every cvcgl_* ctest)

Master's #583 removed cvcgl_test::disableSwapThrottle() and sets
__GL_SYNC_TO_VBLANK=0 / vblank_mode=0 on every test in src/cvcGL through
ctest's ENVIRONMENT, which covers cvcgl_ari_golden too; the merge left the
call behind and package-linux / libcvc-debug failed to compile it.
@transfix
transfix deleted the fix/cvcgl-tests-vsync-off branch October 29, 2026 20:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant