Skip to content

Softer shadows in raytracing and RTAO - #7653

Closed
The-E wants to merge 2 commits into
scp-fs2open:imgui-profilerfrom
The-E:feat/softer-shadows
Closed

Softer shadows in raytracing and RTAO#7653
The-E wants to merge 2 commits into
scp-fs2open:imgui-profilerfrom
The-E:feat/softer-shadows

Conversation

@The-E

@The-E The-E commented Jul 25, 2026

Copy link
Copy Markdown
Member

Adds methods for softening RT shadows. This mechanism works by setting an apparent light size either in the mission file, a lab override, or by deriving it from the sun bitmap. The current implementation limits the apparent size to 1.5°; this is an artificial constraint based around the limits of raytraced shadows - the wider the sun is, the sparser the rays to check whether something is in shadow become, and the more grainy the shadows appear.
There are table settings and FRED/qtFRED controls to set this in missions.

And because the methods required to do soft shadows are mostly identical to what you need to do in order to do raytraced Ambient Occlusion, I threw in that effect as well - but I seriously cannot recommend anyone use it. The visual impact is only noticeable when you really look for it, and the performance cost is pretty large.

@The-E
The-E force-pushed the feat/softer-shadows branch from 46ed13c to c975b84 Compare July 25, 2026 18:18
@Shivansps

Copy link
Copy Markdown
Contributor

please dont forget there is another shadowCascadeParams in shadow_map-g

@The-E

The-E commented Jul 25, 2026

Copy link
Copy Markdown
Member Author

please dont forget there is another shadowCascadeParams in shadow_map-g

done.

@The-E The-E added enhancement A new feature or upgrade of an existing feature to add additional functionality. graphics A feature or issue related to graphics (2d and 3d) vulkan Issues and Features related to the vulkan render backend labels Jul 26, 2026
@The-E
The-E force-pushed the feat/softer-shadows branch 4 times, most recently from c04b4c0 to a7c1cd7 Compare August 7, 2026 16:10
The-E and others added 2 commits August 7, 2026 18:38
…ular size

Raytraced shadow penumbras needed a hand-authored $SunAngularSize: in
stars.tbl, which no shipped content sets -- so in practice nothing ever
got a penumbra, and the shadow-mapped path's $Shadow Smoothness Factor:
had no relation to it at all. Derive a size from the sun bitmap instead
and drive both methods from the same number.

g3_render_rect_screen_aligned_2d() sizes the sun quad so its rad is the
tangent of the half-angle it subtends, making tan(angular radius) equal
to 0.05 * the mission's +Scale: * the emitting disc's fraction of the
bitmap. That fraction is the area of the pixels at or above 90% of the
brightest one, converted to an equivalent radius. Across 90 sun bitmaps
from retail, the MediaVPs, Blue Planet and BtA it lands at a median of
0.261 (retail's chunkier art at 0.487), and the threshold acts as a
constant factor rather than a per-bitmap judgement call -- Spearman rho
0.96 between a 90% and a 50% cutoff.

Taken literally that yields suns 3.9-8.5x Sol's apparent diameter once
+Scale: is folded in (median 1.55, up to 5.0 in retail's own missions),
so the measurement is scaled by 0.25 and clamped to 1.5 degrees, putting
typical content near Sol. Derivation is the default: $SunAngularSize: 0
asks for hard shadows, an explicit value still wins, and missions can
also set +AngularSize: on an individual sun to override both, with FRED
and QtFRED UI (validation and fallback included) to author it. Bitmaps
that can't be read -- and the deliberately blank sun bitmaps mods ship
to get a light source with no visible disc -- fall back to hard shadows
rather than to the widest possible penumbra.

That angular size now sizes the raytraced penumbra cone directly
(traceShadowRayCone() in shadows.sdr), with sample count following the
same Shadow_quality tier that picks the shadow map's resolution (1 ray
at Low, up to 16 at Ultra) rather than being a separate setting. The
shadow-mapped path's tabled smoothness values were tuned by eye against
Sol-sized retail/MediaVPs art, so shadow_smoothness_scale() in
shadows.cpp scales them by the active sun's size relative to Sol,
clamped to what the shadow map can represent (shadow_clamp_smoothness()).
The two methods still disagree about occluder distance -- the raytracer
uses the real one, the shadow map's tabled values stand in for a blocker
distance pinned to the cascade's own extent -- so RT_SHADOW_SUN_SIZE_CALIBRATION
in shadows.sdr is a constant factor that makes a given $SunAngularSize:
read as roughly the same softness either way; it's the number to revisit
if the shadow map ever grows a real blocker search.

Also adds RTAO (raytraced ambient occlusion) sharing the same TLAS as
the shadow rays, with lighting-profile-driven radius/strength, and lab
controls for RT shadow quality (Low/High), the local light cap, RT
shadow samples, RTAO, and an angular-size override for comparing both
shadow methods against the same sun. All are safe to change live: they
only gate which lights are picked as shadow casters or scale existing
uniforms while filling the per-frame light data.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
The shadow-mapped path's penumbra width was a fixed per-cascade value
(shadow_smoothness_scale()'s ceiling), so every occluder blurred its
shadow by the same amount regardless of how close it actually was to
the receiver -- unlike the raytraced path, which gets that for free
from traceShadowRayCone()'s real occluder distance. Close that gap
with PCSS: search nearby texels for occluders, then scale the filter
radius by how far away they were.

The blocker search (pcssBlockerSearch() in shadows.sdr) needs raw,
uncompared depth reads, which the existing PCF sampler can't do -- it's
bound with a compare mode for the hardware shadow-compare instructions
the filtered sampling itself relies on. So shadow maps now get a second
sampler, shadow_map_raw, bound to the same texture with compare mode
off; on OpenGL that needs a second sampler object and GL 3.3
(shadow_contact_hardening_supported()), Vulkan can always bind it.
Where the raw sampler isn't available, or the new Graphics.ShadowContactHardening
option is off, shadows_start_render() sets Shadow_penumbra_scale[cascade]
to -1 as a sentinel and pcssPenumbraRadius() (shadows.sdr) falls straight
back to the fixed radius, bit-identical to the pre-contact-hardening
behavior and skipping the extra texture reads.

When it is on, shadows_start_render() computes each cascade's penumbra
scale from its own frustum extents and the sun's tanθ; the shader turns
that plus the blocker search's average depth into a per-pixel radius,
clamped to [1 texel, the old fixed ceiling]. A blocker search that finds
nothing doesn't fall back to "no shadow" -- a sparse 8-tap search can
miss a thin occluder, so it instead runs the full filter at the hardest
(1-texel) radius, trading a touch of extra blur for not popping a real
shadow to nothing.

Drops RT_SHADOW_SUN_SIZE_CALIBRATION accordingly: that constant existed
only to fake a blocker-distance-driven penumbra on the shadow-mapped path
by pre-scaling the raytraced cone to match it, and the shadow-mapped path
now has a real blocker search of its own, so the raytraced cone goes back
to its physically correct size.
@The-E
The-E force-pushed the feat/softer-shadows branch from a7c1cd7 to 01baf0d Compare August 7, 2026 16:40
@The-E
The-E changed the base branch from master to imgui-profiler August 7, 2026 16:40
@The-E

The-E commented Aug 7, 2026

Copy link
Copy Markdown
Member Author

Superseded by #7695 — recreated on a same-repo branch so the stack (#7694#7695#7696#7697) can be registered as a real GitHub Stack (cross-fork PRs aren't supported by that feature).

@The-E The-E closed this Aug 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement A new feature or upgrade of an existing feature to add additional functionality. graphics A feature or issue related to graphics (2d and 3d) vulkan Issues and Features related to the vulkan render backend

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants