Skip to content

cvcGL: bake the static shadow casters once, redraw only the moving ones - #1474

Merged
transfix merged 7 commits into
masterfrom
perf/cvcgl-shadow-casters
Nov 24, 2026
Merged

transfix merged 7 commits into
masterfrom
perf/cvcgl-shadow-casters

Conversation

@transfix

@transfix transfix commented Nov 23, 2026 •

Copy link
Copy Markdown
Owner

Why

In cvcdbg's demo3 (Humvees on the Austin city), Firefox misses a refresh on 5.7–8.3% of frames. The cost is the shadow bake. Since CVC-DBG/cvcdbg#311 the map is re-baked every frame a vehicle moves (a stale map let a Humvee drive into its own shadow), and each bake re-drew every caster from the sun, the city included. The bake did not get cheaper at 2048, so it is the draws, not the map's size.

What

StridedShadowBaker now bakes the static casters apart from the moving ones (SceneGraph::setShadowStaticCache, on by default).

  • Sorting. A caster that changed (moved, deformed, restyled) within the last 60 checks, or appeared since the last whole bake, is moving. The rest are static.
  • Whole bake. Draws the static casters into the maps the lookup reads. It keeps aside their colour and depth where the moving casters are about to be drawn (a save-under, in a saved map), then draws the moving casters over them, depth-tested. The maps hold exactly what one draw of every caster would, for the old bake's cost plus small copies.
  • Moving-only bake (only moving casters changed). Puts back the texels it kept aside where the moving casters were, keeps aside those under their new places, and draws them there, with the last whole bake's light cameras.
    • Footprints are each caster's bounds through the light camera, plus two texels.
    • Rectangles merge where one rectangle around two covers at most twice what they do (a convoy becomes one or two); past 16 they become one.
    • A vehicle's shadow is redrawn in the frame it moves, so there is no lag and no stale self-shadow.
  • What makes a bake whole, and re-sorts the casters:
    • a light changes;
    • the focus moves 2% of its radius (unchanged);
    • a static caster changes or leaves;
    • a moving one leaves the bounds the light cameras were fitted to;
    • invalidateShadowBake(), or a resolution change;
    • several moving casters have stopped and are most of the moving set (they become static).
  • When the cache is not used: with a casting camera light (VTK moves it with the view), under VTK's compositing z pass, or until both kinds of caster are present. Then the bake is as before, and no second set of maps is made.
  • Only 3-D props are casters now. A HUD's text or image actor never drew into the maps, but a changing one used to force a bake.
  • Supporting changes:
    • ShadowCasterFilterPass gains Preserve: the bake's camera pass without its clear, optionally scissored.
    • The baker's ReleaseGraphicsResources frees the static maps too, and makes the next frame bake.
    • Switching the cache off frees the static maps.
    • SceneGraph::shadowBakeStats() reports the bakes, how many were whole, the static and moving casters at the last bake, and the share of the maps it copied and drew into.

Cost: while both kinds of caster are present, a saved map and depth buffer per casting light at the shadow resolution: +128 MB at 4096 for one light, +8 MB at 1024. With several casting lights, each also gets a depth buffer of its own.

Measured

Native, synthetic, demo3's caster mix. On the GTX 1650, Release, vsync off, 1600×900, map 4096 focused, with the low-memory mapper forced as on WebGL and in demo3's fast draw path. The mix is:

  • one 980k-triangle static mesh;
  • 6 movers of 7k triangles and 24 of 290, all moved every frame.

Render plus glFinish per frame, cache on and off in interleaved blocks, two runs:

Run 1 Run 2
cache off 14.23 ms 14.33 ms
cache on 10.19 ms 10.18 ms
shadows off 4.07 ms 4.12 ms

In demo3 and demo3-ari themselves (native, follow camera, --capture-live, 300 frames):

static casters moving casters whole bakes map share of a moving-only bake
demo3 3 (the city, 978k cells) 30 (6 Humvees + 24 wheels) 95 of 540 (18%) 0.11%
demo3-ari 4 13 96 of 1080 (9%) 0.12%

The remaining whole bakes come from the shadow focus following the chase camera (2% of its radius). Native wall-clock frame times of the demos were too noisy on this shared box to compare (load 30–200, the GPU shared with other sessions).

Browser. cvcdbg main (#316, JSPI and wasm exceptions) built for wasm-mt against this branch. Cache on and off are the same build: a local, uncommitted --no-static-cache flag sets setShadowStaticCache(false). Headless Firefox 158 and Chromium 145 on the GTX 1650, 1600×900, ?prof&flags=follow, map 4096. Each run is 150 s with the first 40 s dropped, runs interleaved, box load 15–25.

frames over 20 ms, cache off frames over 20 ms, cache on render, off → on
demo3, Firefox (5 runs each) 1.3% (0.5–2.9) 2.3% (1.8–3.1) 10.8 → 10.3 ms
demo3-ari, Firefox 7.4% 2.7% 10.7 → 7.6 ms
demo3, Chromium 0.0% 0.0% 8.2 → 7.8 ms
demo3, Firefox, camera not following (no save-under yet) 13.3% 1.8% 14.5 → 13.5 ms

Before the save-under (d2f1ecce9), a whole bake copied the whole map, and demo3 in Firefox missed 4.7–5.6% against 1.5–2.0%. With the save-under, a whole bake costs no more than before. In demo3 with the follow camera, Firefox still misses about a point more with the cache. Its CPU per frame is lower, so that point most likely comes from the 6% of bakes that are still whole, now drawn in two passes.

WebGL calls per frame (?glcount, Chromium) fall only 2–6% (demo3 3,160 → 3,086). The 30 moving vehicle parts still make about 1,500 calls per bake, and that is Firefox's remaining shadow cost.

Tests

  • New cvcgl_shadow_static_cache. Two scenes alike in every way, one cached, one not. Every frame both make the same change, and every frame's picture must be the same, byte for byte (on Apple's GL, within 2 levels).
    • A vehicle drives through a building's shadow for 38 frames:
      • each frame matches the reference and differs from the frame before;
      • its shadow is on the ground beside it in the frame it moved to;
      • the building's shadow is on its roof;
      • after the first bakes, every bake draws only the vehicle, over 0.12% of the map (0 whole).
    • These each give the reference picture, and a whole bake where expected: a building moved, hidden and shown; the moving vehicle hidden and shown; a car hidden and shown; the sun turned; a caster added; the focus set, moved and cleared.
    • Cars that stop become static in one whole bake, once they are most of the moving set.
    • With the cache off, every bake is whole and nothing is cached.
    • At interval 2, the bakes stride as before, and stale frames match too.
  • The new test fails, as it should, on three broken builds:
  • cvcgl_shadow_pass_reuse F. A moving box adds exactly one texture, framebuffer and renderbuffer. A resolution change re-makes them, and the cache off or shadows off frees them. No VTK errors.
  • cvcgl_shadow_pass_programs A. Only the mover switches pass sets now, twice a frame. The static casters stay on the view's.
  • Locally (Linux, GTX 1650, CVC_REQUIRE_RENDER=1), all pass:
    • cvcgl_shadow_static_cache, cvcgl_shadow_casters, cvcgl_shadow_pass_reuse, cvcgl_shadow_pass_programs;
    • cvcgl_city_sun_shadows, cvcgl_shadow_caster_growth, cvcgl_shadow_volume;
    • cvcgl_lod_node, cvcgl_texture_rows, cvcgl_ariadne_scenes, cvcgl_room_view, cvcgl_multi_app.

Not in this PR

  • The web demos get this only through a libcvc release and a cvcdbg pin. No release is cut here; the 4.0.6 tag is tracked separately.
  • With a moving shadow focus, demo3 still makes a whole bake every 2% of the focus radius it travels: 6% of its bakes in the browser.
  • Firefox's remaining cost is per WebGL call: the moving casters' draws in the bake, at about 50 calls each. Fewer or leaner bake draws for them would be the next step.

A frame where a vehicle moved re-baked the shadow maps, and each bake re-drew
every caster from every casting light: over a city, every building and the
terrain. That was nearly all of cvcdbg's demo3 render in Firefox, where it
misses a refresh on 5.7-8.3% of frames; the bake did not get cheaper at a
smaller map, so it is the draws.

StridedShadowBaker now sorts the casters by what they did: one that changed
within the last 60 checks (or appeared since the last whole bake) is moving,
the rest are static. A whole bake draws the static casters into maps of their
own (colour and depth, one framebuffer per casting light), blits them into the
maps the lookup reads and draws the moving casters over them, depth-tested:
the maps hold what one draw of every caster would. While only moving casters
change, a bake is that blit and the moving casters, with the last whole bake's
light cameras, so a moving caster's shadow is redrawn in the frame it moves.
A light, the focus, a static caster changing or leaving, a moving one leaving
the bounds the cameras were fitted to, or most of the moving ones stopping
makes a whole bake, which sorts them again.

SceneGraph::setShadowStaticCache (on by default) turns it off;
shadowBakeStats() reports bakes, whole bakes and the two layers.
ShadowCasterFilterPass gains Preserve (the bake's camera pass without its
clear), and the baker's ReleaseGraphicsResources frees the static maps and
makes the next frame bake.

cvcgl_shadow_static_cache renders a cached scene and an uncached one through
the same changes and pins the same pictures byte for byte, among them a
vehicle driving through a building's shadow with no whole bake.
cvcgl_shadow_pass_reuse pins the static maps' GL objects made once and freed;
cvcgl_shadow_pass_programs counts the mover's pass-set switches alone.
…e and were

The copy of the static casters' maps was the whole map: at 4096, 64 MB of
colour and 64 MB of depth read and written, about what the clear and the
static casters' draw it replaced cost the GPU. Now a bake of the moving
casters copies and redraws only the texels their bounds cover through the
light's camera, now and at the last bake (two texels of margin), with the
blit and the draws' scissor confined to that rectangle; everywhere else the
map already holds the static casters alone. In the test's scene that is
0.12% of the map.

shadowBakeStats().map_share reports the share of the maps the last bake drew
into. cvcgl_shadow_static_cache pins it small, and the pictures stay byte for
byte those of the uncached scene, with a moving caster hidden and shown too.
… casters

Two things kept a bake of the moving casters copying most of the map in
cvcdbg's demo3 and its Ariadne port. A HUD's text and image actors (2-D props,
unmarked) counted as moving casters with no bounds, so the footprint fell back
to the whole map; and the port's six vehicles drive far apart, so one
rectangle around them all covered half of it.

Only a 3-D prop draws into a depth map, so a 2-D one is no longer a caster at
all: it neither makes a bake nor joins a layer (it drew nothing in the bake
before either). And each caster's footprint is its own rectangle, now and at
the last bake, merged only where they overlap (a vehicle and its wheels, one
bake and the next) and into one past 16: a handful of copies instead of one
large one. In both demos a bake of the moving casters now copies and draws
into about 0.1% of the 4096 map.
…o more than before

In Firefox, demo3 (map 4096, a chase camera's shadow focus) missed more
refreshes with the cache than without (5% of frames against 2%): about one bake
in seven is still whole there, and a whole bake drew the static casters into
their own maps and then copied all of those (colour and depth) into the maps
the lookup reads, twice the old bake's GPU work at once. With the focus still,
the cache cut the misses from 13% to 2%.

Now a whole bake draws the static casters straight into the maps the lookup
reads, keeps aside their colour and depth where the moving casters are about
to be drawn (the saved map: only those texels, copied), and draws the moving
casters. A bake of the moving casters alone puts back the texels it kept aside
where they were, keeps aside those under their new places, and draws them
there. A whole bake is the old bake plus small copies; the maps are the same.
With several casting lights each gets a depth buffer of its own for this, as
the moving casters are drawn against it from bake to bake.

cvcgl_shadow_static_cache gains a spot casting beside the sun (F), and fails
without the copies back, as it should.
…o is at most twice them

A convoy's vehicles each had a rectangle of their own, and a bake of the moving
casters copies each twice (back, and aside): about a dozen copies a bake in
demo3. Two rectangles now merge when the one around both covers at most twice
what they do, so a convoy is one or two copies, while casters far apart (the
Ariadne port's) are still not copied with all the map between them.
…ers they drew stand

The baker notes each caster's pose when the maps draw it, and the shadow pass
shades a caster that moved since at that pose (#1466). A whole bake notes every
caster it drew; a bake of the moving casters alone notes those it redrew, so a
moving caster is shaded where it stands, and only between strided bakes at its
bake pose.
…count

Its shadow section counts each frame's GL work and wants nothing created. The
static cache (setShadowStaticCache) makes its saved map the first time a caster
moves, so that one-time texture landed in frame 1 (createTexture 1, on every
lane). A frame moving the link first makes it before the count.
@transfix
transfix merged commit c0521be into master Nov 24, 2026
31 checks passed
@transfix
transfix deleted the perf/cvcgl-shadow-casters branch November 24, 2026 09:32
transfix added a commit that referenced this pull request Nov 24, 2026
# Conflicts:
#	src/cvc/CMakeLists.txt
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