Skip to content

feat: every lamp and fire in reach casts a cached shadow (GD3D11-like) - no more light through walls - #559

Open
BoroBongo wants to merge 4 commits into
feature/pcvr-shadows-lightingfrom
feature/pcvr-cached-lamp-shadows
Open

BoroBongo wants to merge 4 commits into
feature/pcvr-shadows-lightingfrom
feature/pcvr-cached-lamp-shadows

Conversation

@BoroBongo

Copy link
Copy Markdown
Contributor

What

Every lamp and fire in reach casts a shadow from walls and VOBs, like in GD3D11. Candles inside houses no longer light the street, and distant buildings no longer show red patches through their walls.

Stacked on #555 (base feature/pcvr-shadows-lighting), so the diff shows only the cache.

  • LightShadowCacheDomain (owned by StationaryLightsService): a 16 bit Texture2DArray, 256 lights × 6 faces of 128×128 (48 MB). Every pooled lamp/fire in reach gets its surroundings drawn once into its six faces: the distance to the nearest wall or VOB. Lights baked into lightmaps and moving spell lights are excluded.
  • GothicLightCache pass in Lit-World, Lit-AlphaToCoverage and Lit-SingleMesh (GothicShadowCaster.hlsl with GOTHIC_LIGHT_CACHE): writes the distance to the light. It has its own LightMode, so URP never draws it.
  • Lookup (StationaryLighting.hlsl, StationaryLightContribution): the light's slot is packed into its color alpha (indoor + 2 × (slot + 1)), and the world shader compares distances with a 2×2 bilinear filter. A light without a slot falls back to the roof rule of feat: PC shadows - sun/moon, lamps, torch and spells, the hero's own shadow, SSAO, darker nights (GD3D11-like) #555. PC only: Quest's shaders compile it away.
  • On only with the "Shadows" option on and URP's additional-light shadows (quality High). It follows the option at once, also while standing still.

Decision record: Docs/adr/2026-11-23-cached-lamp-shadows.md. Technical: Docs/world-lighting.md → "Shadows and darkness".

Why

URP draws every shadow casting light again in every frame, six times, and takes about 40 at most. With 40 the frame rate tanked, and all other lamps lit through walls. GD3D11 keeps a light's shadow map and draws it again only when something changes; URP has no such cache (HDRP has).

How to test manually

PCVR build, quality High, Shadows on. G2, Khorinis at night:

  • Walk the alleys around the market: no orange or red patches on outside walls from candles/fires inside houses; distant buildings have no light bleeding through.
  • Doorways of lit houses and passages: the light inside stops at the doorway.
  • Lamps on house walls light the wall below them (not cut off).
  • Walk through the city: no hitches when new lamps come into reach.
  • Shadows → off: the cache switches off too (the roof rule still keeps candles off the outside walls).
  • Quest build: looks as before.

Tested so far:

  • In the editor: the cache content checked by reading the faces back from the GPU and comparing them with raycasts from 5 lamps (89–99% of 400 directions match), plus before/after screenshots at the same spot.
  • In a PC build with the keyboard/mouse VR simulator.
  • Not tested with a headset yet, and nothing on Quest 2 or Quest 3 standalone.

Save/load

Nothing saved. The cache is rebuilt after loading (about a second).

Known gaps / follow-ups

  • An opened door keeps the closed door's shadow until something else makes its lamps draw again. VOBs without a collider cast no cached shadow.
  • 128 texels per face: softer than URP's near shadows. Bigger faces for big fires could come later.
  • 48 MB of video memory on PC.

BoroBongo and others added 4 commits November 23, 2026 02:12
…) - candles in houses no longer light the street and distant walls

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… pass keeps Lit/World SRP batched

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…light's draw list built once (review #559)

- a time budget (at least 1, at most 4 lights per frame) instead of a fixed 3 - walking through a town refilled the queue
- world chunk bounds read once per world; a light's casters, materials and passes gathered once, not per cube face
- a full collider buffer (1024) is logged once per light
- comment why the projection isn't run through GL.GetGPUProjectionMatrix (the engine converts it, like URP's shadows)
- ADR: the collider check costs 4 sphere queries per frame - cheap, not free

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@BoroBongo

Copy link
Copy Markdown
Contributor Author

Review (architecture-reviewer + gothic-reviewer x2) on 9337d2a

Verdicts: architecture fits with concerns, gothic run 1 and run 2 approve with changes.

Fixed in 89cfc68 (with #555's fixes merged), follow-ups in #565.

Finding Answer
Projection not passed through GL.GetGPUProjectionMatrix (gothic run 1, High/verify) Disagree, with a comment added in code. CommandBuffer.SetViewProjectionMatrices converts the projection for the graphics API itself: clip z range, reversed z, and the y flip when drawing into a render texture. URP renders its own shadow maps exactly this way (ShadowUtils.RenderShadowSlice passes the raw projection, and GetShadowTransform only compensates reversed z for sampling, never the y flip). So the render-texture rows follow OpenGL's convention on D3D11/12, Vulkan, Metal and GL alike, and converting it ourselves would flip the faces. It was checked on D3D11 with a GPU readback against raycasts (89-99% of directions match for 5 lights). Quest doesn't use the cache.
Per-frame cost while walking through a town; GatherCasters native calls; per-face repeated work (arch Q1, gothic) Fixed: a CPU time budget of 0.75 ms (at least 1, at most 4 lights per frame) replaces the fixed 3. World chunk bounds are read once per world. A light's casters, bounds, materials and passes are gathered once, not per cube face.
Collider buffer (1024) overflow is silent (gothic Medium) Fixed: logged once per light (LogWarning).
ADR claims "steady state costs nothing" (arch Major, now part) Fixed: the ADR says 4 sphere queries per frame (cheap, not free) and describes the time budget.
Polling -> MarkDirty(Bounds) signal (arch Major, later) Follow-up #565
Service size / shadows Domain split (arch Major, later) Follow-up #564 (together with #555's shadow code)
Count-only change detection, exact position compare; chunk.enabled snapshot; collider/renderer on different GameObjects; no Dispose; C#/HLSL contract in 3 places; Capacity vs pool size; cache pass compiled into Quest; CBUFFER copies; Dynamic shader lit through walls; drain log spam; _passIndices growth; RemoveAll delegate; architecture.md row; ADR "why not a renderer feature" Follow-up #565
Q: is SHADER_API_MOBILE defined for the Quest/Pico Vulkan target? Yes: Android Vulkan defines SHADER_API_MOBILE, the same gate MAX_TOTAL_STATIONARY_LIGHTS already relies on. To be confirmed in an APK build (listed in #564's Quest checks).

Compiled in the Editor with no errors. Still to check by playtesting: walking through Khorinis (frame time while the queue refills).

@BoroBongo

Copy link
Copy Markdown
Contributor Author

Editor test of the review fixes (G2, Khorinis at night): 201 lamps cached, with no collider overflow warning. The cache refills by itself after a quality-level switch (Low and back to High). Time budget, measured per Render call while redrawing all 201 lamps: median 0.89 ms, p90 1.03 ms, max 2.0 ms, 3-4 lamps per frame (about 0.25 ms per lamp, about 0.4 ms before the draw-list change). The whole cache is redrawn in 60 frames. No exceptions.

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