Skip to content

refactor(ssgi): tidy GI compute shader internals#2225

Merged
alandtse merged 3 commits into
community-shaders:devfrom
alandtse:perf/ssgi-optimizations
Apr 30, 2026
Merged

refactor(ssgi): tidy GI compute shader internals#2225
alandtse merged 3 commits into
community-shaders:devfrom
alandtse:perf/ssgi-optimizations

Conversation

@alandtse
Copy link
Copy Markdown
Collaborator

@alandtse alandtse commented Apr 28, 2026

Summary

  • Replace 9-tap box history clamp with 5-tap SVGF cross pattern (Schied 2017); skip on saturated history (lerpFactor < 0.15)
  • Express GI inner-loop math identities at source level: per-slice logLenOmega for the mip selection, rsqrt(dot(v,v)) for cosNorm (also guards a degenerate-normal NaN edge case via max(..., 1e-20)), and folding sampleBackPos into sampleDelta - viewVec * Thickness
  • Remove dead srcPrevAo SRV declaration from gi.cs.hlsl (declared at t5 but never read); drop the matching C++ bind and collapse srcPrevY/CoCg/GISpecular/Normal from t6..t9 down to t5..t8

All math changes are bit-equivalent for non-degenerate inputs. No new settings.

Test plan

  • hlslkit-compile validates SSGI variants (HALF_RES / QUARTER_RES / full, GI on/off, GI_SPECULAR on/off, TEMPORAL_DENOISER on/off, VR + flat)
  • Visual: SSGI looks unchanged on stationary scenes (interior cell, conversation)
  • Visual: no new motion-induced artefacts in VR (head turn, fast camera pan)
  • Tracy: SSGI - GI per-dispatch cost stays within scene-noise of upstream baseline

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Improvements
    • More stable and cheaper Screen Space Global Illumination (GI) sampling via safer projection math and guarded mip-level selection.
    • Refined temporal denoising: history clamping now uses a 5-tap cross neighborhood and is applied conditionally for stronger blending stability.
    • Added numeric safety for degenerate vectors to prevent instability in GI calculations.

Replace the 9-tap box history clamp with a 5-tap cross pattern (SVGF,
Schied 2017). Named-neighbour reads (yL/yR/yU/yD) replace a nested
loop, making the algorithm explicit at source level.

The clamp short-circuits on saturated history (lerpFactor < 0.15,
~7+ accumulated frames). The temporal blend has self-stabilised by
that point, so the bounding effect is marginal. Disocclusion remains
covered: motion-clamped accumFrames keeps lerpFactor above 0.15 in
transient regions.

Universal change (VR + flat).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 28, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 4bfb62fb-a59b-4dca-8da9-ae1f77858bb8

📥 Commits

Reviewing files that changed from the base of the PR and between 964bc16 and 4eb7c7d.

📒 Files selected for processing (3)
  • features/Screen Space GI/Shaders/ScreenSpaceGI/gi.cs.hlsl
  • package/Shaders/Common/Math.hlsli
  • src/Features/ScreenSpaceGI.cpp
✅ Files skipped from review due to trivial changes (1)
  • package/Shaders/Common/Math.hlsli
🚧 Files skipped from review as they are similar to previous changes (1)
  • features/Screen Space GI/Shaders/ScreenSpaceGI/gi.cs.hlsl

📝 Walkthrough

Walkthrough

Reworks Screen Space GI shader sampling and temporal history handling (guarded rsqrt, revised mip selection, direct back-side vectors, 5-tap cross clamping with lerp threshold) and updates C++ SRV bindings to match the HLSL parameter reordering. No public API changes.

Changes

Cohort / File(s) Summary
Shader: SSGI sampling & temporal clamping
features/Screen Space GI/Shaders/ScreenSpaceGI/gi.cs.hlsl
Removed srcPrevAo, shifted temporal input registers; replaced division with guarded rsqrt(max(..., 1e-20)); changed mip-level calc to split-form using log2(s) + per-slice logLenOmega (guarded); compute back-side horizon vectors directly from sampleDelta; replace 3x3 clamp with 5-tap cross neighborhood and apply clamping only when lerpFactor >= 0.15.
Common math constants
package/Shaders/Common/Math.hlsli
Added EPSILON_LENGTH_SQ constant to guard squared-length prior to rsqrt.
C++ SRV binding adjustments
src/Features/ScreenSpaceGI.cpp
Reordered SRV bindings for the GI compute pass: removed AO SRV slot and shifted texIlY, texIlCoCg, texGiSpecular, texNormal down one slot to match updated HLSL parameter ordering.

Sequence Diagram(s)

sequenceDiagram
  participant CPU as DrawSSGI (CPU)
  participant GPU as Compute Shader (GI)
  participant SRV as Texture SRVs
  participant UAV as GI UAVs

  CPU->>SRV: Bind SRVs (`texIlY`,`texIlCoCg`,`texGiSpecular`,`texNormal`,...)
  CPU->>GPU: Dispatch giCompute (with updated SRV slots)
  GPU->>SRV: Sample temporal histories, normals, indirect maps
  GPU->>GPU: Compute GI using guarded rsqrt, split mip selection, back-side vectors
  GPU-->>GPU: Apply temporal denoiser (5-tap cross clamp when lerpFactor >= 0.15)
  GPU->>UAV: Write GI outputs (radiance, normal mips, specular)
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested reviewers

  • davo0411
  • doodlum
  • jiayev

Poem

🐰 In pixel meadows where light hops free,
I guard each length with gentle glee—rsqrt and me.
Five taps in a cross, I nibble the noise away,
Mips and horizons stitched at play.
Hop, patch, render—brightened day! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'refactor(ssgi): tidy GI compute shader internals' accurately summarizes the main objective of the changeset—internal refactoring of the GI compute shader with improved math stability, history clamping logic, and removal of dead code.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions
Copy link
Copy Markdown

No actionable suggestions for changed features.

alandtse and others added 2 commits April 28, 2026 01:41
Three mathematically equivalent rewrites in the GI trace inner loop,
expressing the underlying identities at source level rather than
relying on compiler hoisting:

1. Per-slice constant logLenOmega = 0.5 * log2(dot(omega, omega)).
   Per-step mip becomes log2(s) + logLenOmega for sampleOffset = s *
   omega with s >= 0.

2. rcpProjectedNormalVecLength = rsqrt(dot(v, v)) for cosNorm. The
   max(..., 1e-20) clamp also guards a degenerate-normal NaN edge
   case the original could produce.

3. sampleBackHorizonVec = normalize(sampleDelta - viewVec * Thickness)
   directly, instead of building sampleBackPos as an intermediate.
   Same for the GI-radius back-horizon vector.

All three are bit-equivalent for non-degenerate inputs.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
gi.cs.hlsl declared Texture2D<float> srcPrevAo at t5 but never sampled
it. The C++ dispatch still bound texAo[inputAoTexIdx]->srv to slot 5
on every GI dispatch. Remove the HLSL declaration, drop the C++ bind,
and shift the remaining textures (srcPrevY/CoCg/GISpecular/Normal)
from t6-t9 down to t5-t8 to keep the SRV layout dense.

No functional change. Mainly tidies up dead code; minor CPU savings
from one less SRV bind per frame.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@alandtse alandtse force-pushed the perf/ssgi-optimizations branch from 964bc16 to 4eb7c7d Compare April 28, 2026 08:41
@github-actions
Copy link
Copy Markdown

✅ A pre-release build is available for this PR:
Download

@alandtse alandtse merged commit 43ba6fd into community-shaders:dev Apr 30, 2026
15 checks passed
ParticleTroned added a commit to ParticleTroned/skyrim-community-shaders that referenced this pull request May 2, 2026
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.

2 participants