Skip to content

fix(unified-water): validate water cells and clear stale LOD#2425

Merged
SkrubbySkrubInAShrub merged 1 commit into
community-shaders:devfrom
FIocker:fix/unified-water-cell-height-validation
May 27, 2026
Merged

fix(unified-water): validate water cells and clear stale LOD#2425
SkrubbySkrubInAShrub merged 1 commit into
community-shaders:devfrom
FIocker:fix/unified-water-cell-height-validation

Conversation

@FIocker
Copy link
Copy Markdown
Contributor

@FIocker FIocker commented May 26, 2026

Fixes bad Unified Water tiles being generated from cells where height data could not be read cleanly and cull UW LODs when entering interiors

Changes:

  • Skip cells with missing height data instead of treating them as valid water
  • Reject non-finite and sentinel water/land heights before generating UW instructions
  • Ignore invalid cached water instructions at runtime, so stale bad caches do not keep rendering broken tiles
  • Clear the active runtime cache when leaving an exterior worldspace
  • Cull Unified Water's generated LOD tree while an interior cell is active

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 26, 2026

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 60b428ae-d6e8-4eea-8997-9dbe3d747ed0

📥 Commits

Reviewing files that changed from the base of the PR and between 1f6a178 and 60c8f3d.

📒 Files selected for processing (3)
  • src/Features/UnifiedWater.cpp
  • src/Features/UnifiedWater.h
  • src/Features/UnifiedWater/WaterCache.cpp
🚧 Files skipped from review as they are similar to previous changes (3)
  • src/Features/UnifiedWater.h
  • src/Features/UnifiedWater/WaterCache.cpp
  • src/Features/UnifiedWater.cpp

📝 Walkthrough

Walkthrough

Adds exterior/interior state and recursive LOD water culling across worldspace/terrain/shader hooks; introduces IsValidCellHeight, sentinel initialization, precache fallback/missing-data/invalid-height counters in BuildDiskCache, and filters/skips invalid or out-of-bounds runtime cache instructions.

Changes

Unified water LOD culling and exterior state

Layer / File(s) Summary
Interior/exterior helpers and LOD cull update
src/Features/UnifiedWater.cpp
Adds interior-cell helper, IsExteriorWorldspaceActive() and UpdateWaterLODCull(); updates TES_SetWorldSpace::thunk and TES_DestroySkyCell::thunk to set/clear an atomic exterior flag, update current worldspace, and call UpdateWaterLODCull().
Terrain attach/detach and shader integration
src/Features/UnifiedWater.cpp
Calls UpdateWaterLODCull() from BGSTerrainBlock_Attach/Detach, invokes it inside BSWaterShader_SetupGeometry::thunk and requires IsExteriorWorldspaceActive() for flowmap/geometry/displacement setup, and tightens TESWaterSystem_UpdateDisplacementMeshPosition::thunk early-return to require flowmap + exterior active.
Header atomic state and declarations
src/Features/UnifiedWater.h
Adds <atomic>, an std::atomic_bool exteriorWorldspaceActive member, and private declarations for IsExteriorWorldspaceActive() and UpdateWaterLODCull().

Water cache validation and runtime guarding

Layer / File(s) Summary
Height validation helper
src/Features/UnifiedWater/WaterCache.cpp
Adds <cmath> and local IsValidCellHeight with finiteness, sentinel exclusion (FLT_MIN/FLT_MAX), and an absolute maximum bound.
Disk cache sentinel init, fallback, and counters
src/Features/UnifiedWater/WaterCache.cpp
Updates BuildDiskCache to initialize formID, landHeight, waterHeight to sentinels; on TryGetCellData failure use precache heights or skip the tile; validate heights with IsValidCellHeight; set or force formID=0 for invalid/non-water tiles; increment precacheFallbackCount/skippedMissingCellDataCount/skippedInvalidHeightCount; add conditional debug log.
Runtime cache filtering and bounds checks
src/Features/UnifiedWater/WaterCache.cpp
Adds skippedInvalidInstructionCount in TryBuildRuntimeCache; skips cached instructions whose waterHeight fails IsValidCellHeight; bounds-checks computed LOD-relative (lodX,lodY) and warns+skips out-of-bounds instructions; adds conditional debug log and an EOF brace adjustment.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Suggested reviewers

  • alandtse
  • davo0411
  • SkrubbySkrubInAShrub
  • sicsix

Poem

🐰 I hop where ripples meet the land,
I set a flag with careful paw.
Heights I check, and skips I count,
LODs rest when exteriors stand tall.
A rabbit hums — the cache is calm.

🚥 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 accurately captures the two main changes: validating water cells (via height checks and sentinel exclusions) and clearing stale LOD (via interior-triggered culling and invalid cache rejection).
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

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 OpenGrep (1.22.0)

OpenGrep fatal error (exit code 2): [00.20][ERROR]: Error: exception Unix_error: No such file or directory stat src/Features/UnifiedWater.cpp
Raised by primitive operation at UTmp.replace_named_pipe_by_regular_file_if_needed in file "libs/commons/UTmp.ml", line 145, characters 8-27
Called from Scan_CLI.replace_target_roots_by_regular_files_where_needed.(fun) in file "src/osemgrep/cli_scan/Scan_CLI.ml", lines 1086-1087, characters 19-65
Called from List_.fast_map in file "libs/commons/List_.ml", line 81, characters 17-20
Called fro


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.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/Features/UnifiedWater/WaterCache.cpp`:
- Around line 378-389: The current validHeights only checks finiteness and
FLT_MIN/MAX but the absolute-value range check (fabs(waterHeight) < 50000.0f) is
applied only to waterHeight later, allowing garbage landHeight to slip through;
update the validHeights computation to include both fabs(waterHeight) < 50000.0f
and fabs(landHeight) < 50000.0f so the later conditional can rely on
validHeights, keep the same branch that assigns worldSpace->worldWater->formID
when validHeights && waterHeight > landHeight, and ensure
skippedInvalidHeightCount continues to increment when !validHeights (so
out-of-range waterHeight/landHeight are counted).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 5d4a3ad0-f969-4083-b76d-d6580a130a00

📥 Commits

Reviewing files that changed from the base of the PR and between d7e2fa4 and f0f1fcf.

📒 Files selected for processing (1)
  • src/Features/UnifiedWater/WaterCache.cpp

Comment thread src/Features/UnifiedWater/WaterCache.cpp Outdated
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 26, 2026

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

Comment thread src/Features/UnifiedWater/WaterCache.cpp Outdated
@FIocker FIocker force-pushed the fix/unified-water-cell-height-validation branch from f0f1fcf to 257b358 Compare May 26, 2026 21:41
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
src/Features/UnifiedWater/WaterCache.cpp (1)

1-1: Add an issue-closing reference in the PR description if there is a tracked bug.

For traceability, consider adding Fixes #<id> or Closes #<id> in the PR body.

As per coding guidelines, "Issue References (if PR fixes bugs or implements features): Suggest adding appropriate GitHub keywords".

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/Features/UnifiedWater/WaterCache.cpp` at line 1, The PR is missing an
issue-closing reference for tracked bugs; update the PR description/body to
include a GitHub keyword like "Fixes #<id>" or "Closes #<id>" referencing the
relevant issue number so the PR will auto-close the tracked bug (no code changes
needed; just edit the PR body to add the appropriate line).
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/Features/UnifiedWater/WaterCache.cpp`:
- Around line 387-390: When assigning landHeight and waterHeight from the
precache array, guard the access by validating idx against the container size:
before using preCache.heights[idx] (the code around preCache, heights, idx,
landHeight, waterHeight, precacheFallbackCount), check that idx <
preCache.heights.size(); if not, handle the malformed precache by skipping the
read and applying a safe fallback (e.g., leave default heights, log or mark the
entry as invalid, still increment precacheFallbackCount as intended) to avoid
out-of-bounds reads and crashes during cache generation.

---

Nitpick comments:
In `@src/Features/UnifiedWater/WaterCache.cpp`:
- Line 1: The PR is missing an issue-closing reference for tracked bugs; update
the PR description/body to include a GitHub keyword like "Fixes #<id>" or
"Closes #<id>" referencing the relevant issue number so the PR will auto-close
the tracked bug (no code changes needed; just edit the PR body to add the
appropriate line).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 69373218-2091-4beb-8d02-54f77d6c1488

📥 Commits

Reviewing files that changed from the base of the PR and between f0f1fcf and 257b358.

📒 Files selected for processing (1)
  • src/Features/UnifiedWater/WaterCache.cpp

Comment thread src/Features/UnifiedWater/WaterCache.cpp Outdated
@FIocker FIocker force-pushed the fix/unified-water-cell-height-validation branch from 257b358 to cdb725b Compare May 26, 2026 23:06
@FIocker FIocker changed the title fix(unified-water): validate cell water heights fix(unified-water): validate water cells and clear stale LOD May 26, 2026
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/Features/UnifiedWater.cpp`:
- Line 543: UpdateWaterLODCull() iterates the whole generated water subtree and
is being called from hot paths (geometry setup and displacement update hooks),
causing O(n) per-frame/per-pass work; modify the callers to avoid repeated full
reculls by caching the last applied exterior and cull state (e.g., store
lastExteriorState and lastCullState on the singleton or an associated
WaterLODManager) and only call singleton.UpdateWaterLODCull() when those cached
values differ from the new state; update the geometry setup and displacement
update hooks to check and update the cache before invoking UpdateWaterLODCull(),
and add a config/user toggle to disable automatic LOD recull or run it
asynchronously to limit render-path performance impact.
- Around line 366-371: UpdateWaterLODCull currently calls SetAppCulledRecursive
on *gWaterLOD which clears child-level SetAppCulled flags set by
BGSTerrainNode_UpdateWaterMeshSubVisibility::thunk; change the call so it only
sets the root node's cull state (e.g., call SetAppCulled on *gWaterLOD instead
of SetAppCulledRecursive) so child explicit cull flags are preserved when
exterior water comes back.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: e7977f73-117f-49c6-9bb2-3421fedfb745

📥 Commits

Reviewing files that changed from the base of the PR and between 257b358 and cdb725b.

📒 Files selected for processing (3)
  • src/Features/UnifiedWater.cpp
  • src/Features/UnifiedWater.h
  • src/Features/UnifiedWater/WaterCache.cpp
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/Features/UnifiedWater/WaterCache.cpp

Comment thread src/Features/UnifiedWater.cpp
Comment thread src/Features/UnifiedWater.cpp Outdated
@FIocker FIocker force-pushed the fix/unified-water-cell-height-validation branch from cdb725b to 1f6a178 Compare May 27, 2026 10:00
@FIocker FIocker force-pushed the fix/unified-water-cell-height-validation branch from 70092a0 to 60c8f3d Compare May 27, 2026 11:24
@SkrubbySkrubInAShrub SkrubbySkrubInAShrub merged commit 35b6f05 into community-shaders:dev May 27, 2026
11 checks passed
@FIocker FIocker changed the title fix(unified-water): validate water cells and clear stale LOD fix(unified-water): validate water cells and stabilize LOD water across transitions May 28, 2026
@FIocker FIocker changed the title fix(unified-water): validate water cells and stabilize LOD water across transitions fix(unified-water): validate water cells and clear stale LOD May 28, 2026
IgorAlanAlbuquerque pushed a commit to IgorAlanAlbuquerque/skyrim-community-shaders that referenced this pull request May 29, 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