Skip to content

fix(ISHDR): guard contrastedColorModified against zero avgValue#2126

Merged
alandtse merged 2 commits into
community-shaders:devfrom
mvanhorn:osc/2104-ishdr-zero-guard
Apr 17, 2026
Merged

fix(ISHDR): guard contrastedColorModified against zero avgValue#2126
alandtse merged 2 commits into
community-shaders:devfrom
mvanhorn:osc/2104-ishdr-zero-guard

Conversation

@mvanhorn
Copy link
Copy Markdown
Contributor

@mvanhorn mvanhorn commented Apr 16, 2026

Summary

Guard the contrast modification path in ISHDR.hlsl against division by zero when avgValue.x is 0.

Why this matters

Line 178 divides tintedColor by avgValue.x without checking for zero. When avgValue.x is 0, the division produces INF, which propagates through pow() and contaminates outputColor. The earlier exposure path (lines ~168-169) already guards this same value with if (avgValue.x != 0), but the contrast modification skips the check. Flagged by @alandtse during review of PR #2103.

Changes

package/Shaders/ISHDR.hlsl line 178: Replace raw avgValue.x with max(avgValue.x, 1e-6) (epsilon clamp). Both the divisor and the multiplier use the clamped value so the result stays mathematically consistent.

Testing

HLSL static analysis. The change is a single float substitution with no control flow changes.

Fixes #2104

This contribution was developed with AI assistance (Claude Code).

Summary by CodeRabbit

  • Bug Fixes
    • Prevented rare division-by-zero that could produce extreme or unstable shadow contrast in certain lighting, improving rendering stability.
    • Ensured more consistent shadow-contrast behavior in low-intensity/near-zero average lighting scenarios.

Replace raw avgValue.x with max(avgValue.x, 1e-6) in the contrast
modification path to prevent INF/NaN when the average luminance is
zero. The earlier exposure path already guards this value; this
brings the contrast path in line.

Fixes community-shaders#2104
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 16, 2026

📝 Walkthrough

Walkthrough

The shader package/Shaders/ISHDR.hlsl now includes Common/Math.hlsli and replaces direct uses of avgValue.x in the shadow-contrast computation with a clamped safeAvgValue = max(avgValue.x, EPSILON_DIVISION), preventing division-by-zero/near-zero in contrast math.

Changes

Cohort / File(s) Summary
Shader Safety & Includes
package/Shaders/ISHDR.hlsl, Common/Math.hlsli
Added Common/Math.hlsli include and replaced direct avgValue.x usage with safeAvgValue = max(avgValue.x, EPSILON_DIVISION) in the shadow-contrast calculation; used safeAvgValue for divisions, scaling, and exponentiation inputs to avoid division-by-zero/near-zero issues.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related issues

Possibly related PRs

Suggested reviewers

  • alandtse
  • doodlum

Poem

🐰 In shader fields where shadows play,

A tiny zero crept one day.
I clamped it safe, with EPSILON fine,
Now contrast hums along the line.
Hooray — no NaNs, the pixels shine! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely summarizes the main change: guarding the contrast modification against division by zero when avgValue is zero.
Linked Issues check ✅ Passed The PR implements all coding requirements from issue #2104: replaces avgValue.x with a clamped safeAvgValue in the contrastedColorModified computation and uses EPSILON_DIVISION constant for consistency.
Out of Scope Changes check ✅ Passed The changes are narrowly scoped to the stated objective: adding a safe average value computation and including Math.hlsli for EPSILON_DIVISION, with no extraneous modifications.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ 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.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 16, 2026

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

Copy link
Copy Markdown
Collaborator

@alandtse alandtse left a comment

Choose a reason for hiding this comment

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

Minor correction to avoid a hardcoded magic number.

Comment thread package/Shaders/ISHDR.hlsl Outdated
Reviewer flagged the hardcoded 1e-6 as a magic number. Replace with the
existing EPSILON_DIVISION constant from Common/Math.hlsli, matching how
BRDF.hlsli and StencilCS.hlsl guard divisions elsewhere in the codebase.
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.

🧹 Nitpick comments (1)
package/Shaders/ISHDR.hlsl (1)

1-1: Optional: tighten PR title to match repo convention limits.

Suggested title: fix(ishdr): guard contrast path for zero avg value
As per coding guidelines, commit titles should follow type(scope): description, use lowercase description, and stay within 50 characters.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@package/Shaders/ISHDR.hlsl` at line 1, Update the PR title to follow the repo
convention "type(scope): description" in lowercase and within ~50 characters;
replace the current title with the suggested one "fix(ishdr): guard contrast
path for zero avg value" so it matches the required format and succinctly
describes the change (scope: ISHDR).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@package/Shaders/ISHDR.hlsl`:
- Line 1: Update the PR title to follow the repo convention "type(scope):
description" in lowercase and within ~50 characters; replace the current title
with the suggested one "fix(ishdr): guard contrast path for zero avg value" so
it matches the required format and succinctly describes the change (scope:
ISHDR).

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 7307c682-e4a1-431d-8c1d-931e48ce8e82

📥 Commits

Reviewing files that changed from the base of the PR and between 6dc9262 and a13eab5.

📒 Files selected for processing (1)
  • package/Shaders/ISHDR.hlsl

@alandtse alandtse merged commit 4c0a4ea into community-shaders:dev Apr 17, 2026
17 checks passed
YtzyFvra pushed a commit to YtzyFvra/skyrim-community-shaders that referenced this pull request Apr 19, 2026
…unity-shaders#2126)

Co-authored-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com>
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.

fix(ISHDR): guard contrastedColorModified against zero avgValue.x

3 participants