Skip to content

cvcGL shadows: an idle conference room bakes none, and a bake keeps its shaders and skips the framebuffer status wait - #996

Merged
transfix merged 4 commits into
masterfrom
perf/shadow-rebake
Nov 3, 2026
Merged

transfix merged 4 commits into
masterfrom
perf/shadow-rebake

Conversation

@transfix

@transfix transfix commented Nov 3, 2026 •

Copy link
Copy Markdown
Owner

The browser conference spent most of its render time on shadows, and motion juddered on a 144 Hz screen ("the camera jumps around" under mouse look). There were three causes:

  1. An idle room re-baked the shadow maps. room_view re-posed every avatar marker every frame, and a standing avatar's shown pose changes by rounding from frame to frame. Measured natively under mouse look, the rotation's cosines move by 2e-16 and the position by up to 3e-8 m. Every pose applied moves a shadow caster, and a moved caster re-bakes the maps.
    • Natively, standing still with mouse look on: 170 bakes in 35 s (none without mouse look).
    • In the browser: 52 bakes in 6 s idle, and a bake nearly every frame for seconds after you stop walking.
  2. Every bake rebuilt every caster's shader source twice. Under shadows, each caster is drawn once by the shadow bake and once by the shadow pass. VTK builds a mapper's shader for the render passes on its actor and rebuilds it whenever that set differs from the last draw's. So each draw ran a dozen-odd Substitute calls over ~16 KB, then the shader cache's copy and MD5, only to find the program it had two draws before. In an earlier browser profile, vtkOpenGLLowMemoryPolyDataMapper::UpdateShaders was about 19% of all CPU time.
  3. Every bake waited on the GPU process. vtkOpenGLFramebufferObject::StartNonOrtho calls glCheckFramebufferStatus on every call, which is once per casting light per bake, and on WebGL that call is synchronous. Profiled while walking with 2 fixed, it was 7.9% of all CPU time, inside a bake that took 17.8%.

Changes

  • room_view: a marker is re-posed only once its pose matrix has moved 1e-4 from the one it shows (0.1 mm, or under a hundredth of a degree). The visor and worn parts move with it or not at all. Natively, an idle room with mouse look now bakes 3-4 times in 35 s, all at startup.
  • LowMemoryPolyDataMapper (the WebGL mapper) keeps one shader per render-pass set ("pass programs").
    • Leaving a pass set, it keeps that set's program, sources, GLSL mods and build stamp. Coming back, it puts them back, so VTK's own up-to-date test judges the restored shader: the passes' shader-stage MTimes, the mods, selection, PBR and the shader property.
    • A kept shader is dropped when a pass it was built for has been deleted (weak pointers, so a reused address can't match), or when the actor's property or textures changed after it was built.
    • All kept shaders are dropped when the color or normal source or the array declarations change.
    • Sources are shared, one copy per program, so a scene of thousands of casters doesn't hold thousands of copies.
    • CVCGL_LOWMEM_PASS_PROGRAMS=0 turns it off, for A/B runs.
  • FilteredShadowMapPass reports a shader-stage MTime that moves when the set of casting lights changes. Its shader depends on which lights cast, and until now only the bake/view alternation forced a rebuild for that. With shaders kept, it has to say so: without it, the new test's section C shows the shadow on the wrong light (a max difference of 145 levels).
  • ShadowBakeFramebuffer: the bake's framebuffer asks glCheckFramebufferStatus once per (shadow map, size). The answer can't change while those stay the same, because the depth buffer belongs to the FBO and is resized with it.
  • SceneGraph::shadowBakes(): bakes since shadows were turned on, for tests and frame-cost diagnosis.
  • The conference document's comment no longer says the maps are "re-baked each frame".

Who gets what:

  • The shader cache covers every GeometryNode-based node drawn by the WebGL mapper, so all of cvcGL in the browser, not just the conference.
  • The framebuffer change covers every cvcGL scene with shadows on, browser and desktop.
  • The idle-room fix is room_view's own. Other cvcGL apps that re-pose shadow casters every frame by rounding-level amounts still re-bake.
  • Native desktop builds use VTK's classic mapper, which this PR doesn't change. It still rebuilds on each pass switch, which costs much less there.

Measured

Headless Chrome (D3D11 ANGLE), 1600x900, against a local relay, in mouse look. One round is idle 6 s, turning 4 s, walking 4 s, walking while turning 4 s, then 2 s stopped. The times are arigl's AriRuntime::render per frame (ms, median), from a temporary frame timer.

  • The base is the currently deployed branch plus that timer. Each column adds to the one before it.
  • "shadows off" is the last build started with the document's shadows off.
  • Two interleaved rounds, shown as round 1 / round 2; the machine ran a little slower in round 2.
phase before + room fix + pass programs + framebuffer shadows off
idle 2.1 / 3.3 (max 10.6 / 27.1) 2.3 / 2.3 2.2 / 3.4 2.5 / 3.1
turning 9.9 / 12.3 8.0 / 6.9 4.0 / 5.6 2.1 / 3.5
walking 9.7 / 11.9 7.6 / 7.5 3.7 / 5.7 2.5 / 3.5
walking + turning 9.8 / 14.2 7.4 / 8.2 5.0 / 5.8 2.2 / 3.7
just stopped 11.2 / 4.1 (p95 12.7 / 14.2) 2.0 / 2.3 2.3 / 3.1 2.2 / 2.9
  • A walking frame now costs about 1-2 ms more than with shadows off (it was 7-8 ms more). Its p95 is 5.9-6.9 ms, inside a 144 Hz frame (6.9 ms).
  • What remains is real work: drawing the casters a second time into the map, and the shadow lookup.
  • With pass programs off (room fix only), moving frames cost the same as before (15-17 ms in a slower stretch of the same session). Idle frames lose their stray bakes.
  • The rounds were measured before the source-sharing commit, which only removes string copies.

Not done: a shadow update interval above 1 while only avatars move. It would save about another millisecond on every other frame, at the price of an avatar's shadow trailing it by a frame.

Tests

  • New cvcgl_shadow_pass_programs, with the low-memory mapper forced (desktop GL only, for the driver call counts):
    • With a caster moving every frame (a bake every frame), there are 0 shader builds over 12 frames. With pass programs off there are 72, two per caster per frame.
    • Frames match pass programs off (every draw building afresh), each compared frame a bake and a view, to two levels a channel, across:
      • the shadowed scene;
      • a change of which light casts, with the light count unchanged (this section fails without the stage MTime);
      • edge visibility switched on;
      • shadows off and on again.
    • Frames that bake issue no more glCheckFramebufferStatus than frames that don't: 0 extra in 8 bakes, against 8 with VTK's StartNonOrtho.
  • cvcgl_room_view: an idle room, the facing flipping by one ULP each frame, makes 0 bakes in 60 frames (59 with the threshold at 0). A turn and a walk still bake.
  • cvcgl_lowmem_fastdraw: the check that pinned "a shadow bake takes the full lookup" now expects the kept shader back (no build, no lookup). The old behaviour is still checked with CVCGL_LOWMEM_PASS_PROGRAMS=0. Both pass all 288 checks.
  • Native Windows (MSVC 14.44, shared, RelWithDebInfo): ctest -R ^cvcgl passes all 88 tests on the final head.
  • Pre-existing, not from this PR: with CVCGL_LOWMEM_MAPPER=force (not run in CI), cvcgl_shadow_casters fails one byte-exact footprint check. It fails the same way with every shadow change here reverted.

No test pins the browser timings above. They were measured by hand in headless Chrome.

… shader per render pass

The browser conference spent most of its render time on shadows: the shadow maps were re-baked
with nobody moving, and every bake rebuilt every caster's shader source twice.

- room_view: a marker is re-posed only once its pose has moved 1e-4 (0.1 mm, or under a
  hundredth of a degree) from the one it shows. A standing avatar's shown pose changes by
  rounding from frame to frame (measured natively under mouse look: rotation 2e-16, position up
  to 3e-8 m), and every pose applied moves a shadow caster: 170 bakes in 35 s standing still,
  natively; 0 now. The visor and parts move with the marker or not at all.
- LowMemoryPolyDataMapper (the WebGL mapper) keeps a shader per set of render passes. Under
  shadows each caster is drawn by the bake and by the shadow pass, and VTK rebuilt its shader
  (a dozen-odd Substitute calls over ~16 KB, then the cache's copy and MD5) at every switch. The
  shader kept for a set is put back with its sources, GLSL mods and build stamp, so VTK's own
  up-to-date test judges it; it is dropped when a pass it was built for is gone, when the
  actor's property or textures changed since, or with the rest when the color or normal source
  or the array declarations change. CVCGL_LOWMEM_PASS_PROGRAMS=0 turns it off for A/B runs.
- FilteredShadowMapPass reports a shader-stage MTime: its shader follows which lights cast,
  and only the alternation used to rebuild for that.
- SceneGraph::shadowBakes(), for the tests and for telling a frame's cost apart.

Tests: cvcgl_shadow_pass_programs (new: no shader builds while a caster moves, against two a
caster a frame without; frames equal to pass programs off across a change of casting light, a
shader-changing property and shadows off and on), and cvcgl_room_view's idle room (no bake
while the facing wanders by rounding; turning and walking bake).
With pass programs a bake's draws re-bind the shader kept for their pass set: no build and no lookup, where the check pinned the full lookup every render-pass switch used to cost (still pinned with CVCGL_LOWMEM_PASS_PROGRAMS=0). The scene's stats sum the two new counters.
… every bake

vtkOpenGLFramebufferObject::StartNonOrtho asks glCheckFramebufferStatus on every call, so once per casting light in every bake. On WebGL that waits on the GPU process: in the browser conference, walking, it was 8% of all CPU, all of it inside the bake. The bake's framebuffer is now a ShadowBakeFramebuffer whose startBake asks once per (map, size); the answer cannot change while those stay. cvcgl_shadow_pass_programs section F counts the calls at the driver (so the test is desktop GL only now): 0 in 8 frames that bake, against 8 with VTK's StartNonOrtho.
… per program

Each kept pass program held its own copy of the vertex and fragment sources (~16 KB each), so a scene of thousands of shadow casters kept tens of MB of identical text on WebGL. The shader cache has one program per distinct source, so the sources are now shared by program (weakly registered, kept alive by the holders, which keep the program alive too), and a pass switch no longer copies them out of the mapper.
@transfix transfix changed the title cvcGL: an idle conference room bakes no shadows, and a caster keeps a shader per render pass cvcGL shadows: an idle conference room bakes none, and a bake keeps its shaders and skips the framebuffer status wait Nov 3, 2026
@transfix
transfix merged commit d9f559f into master Nov 3, 2026
42 of 49 checks passed
@transfix
transfix deleted the perf/shadow-rebake branch November 3, 2026 20:54
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