Repository navigation
cvcGL: bake the static shadow casters once, redraw only the moving ones - #1474
Merged
Merged
Conversation
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
added a commit
that referenced
this pull request
Nov 24, 2026
This was referenced Nov 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
StridedShadowBakernow bakes the static casters apart from the moving ones (SceneGraph::setShadowStaticCache, on by default).invalidateShadowBake(), or a resolution change;ShadowCasterFilterPassgainsPreserve: the bake's camera pass without its clear, optionally scissored.ReleaseGraphicsResourcesfrees the static maps too, and makes the next frame bake.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:
Render plus
glFinishper frame, cache on and off in interleaved blocks, two runs:In demo3 and demo3-ari themselves (native, follow camera,
--capture-live, 300 frames):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-cacheflag setssetShadowStaticCache(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.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
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).cvcgl_shadow_pass_reuseF. 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_programsA. Only the mover switches pass sets now, twice a frame. The static casters stay on the view's.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