Skip to content

LR-EDIT-EFFECTS-GRAIN: grain skips a degenerate frame instead of producing NaN pixels - #3003

Merged
aiongg merged 1 commit into
storytold:mainfrom
emmertex:upstream-pr/grain-degenerate-frame
Oct 31, 2026
Merged

aiongg merged 1 commit into
storytold:mainfrom
emmertex:upstream-pr/grain-degenerate-frame

Conversation

@emmertex

@emmertex emmertex commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Problem: with Grain on, a crop of zero width (e.g. dragging a crop edge onto the other) mapped pixels to NaN/infinite grain coordinates, so the render came out as NaN pixels (CPU path, and the GPU grain params).

Change (crates/pipeline/src/finish.rs): grain is skipped when the frame has no finite mapping (non-finite/zero scale or out_to_norm), grain_noise returns 0 for non-finite coordinates, and a non-finite grain term is never added. Kept minimal, since grain is being reworked in #2330 / #1060.

Tests: new grain_noise_is_finite_for_degenerate_coordinates (unit) and zero_width_crop_with_grain_renders_finite_pixels (hostile-input render, asserts every sample finite). Ran cargo test -p lightcraft-pipeline grain / zero_width (pass), cargo fmt, cargo clippy -p lightcraft-pipeline --all-targets -D warnings (clean).

Refs #2330, #1060

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved grain rendering stability for invalid or degenerate image coordinates and dimensions, including zero-width crops. This helps prevent non-finite pixel values when grain effects are enabled.

…oducing NaN pixels

A zero-width crop mapped output pixels to NaN/inf grain coordinates, so
grain turned the whole render into NaN (CPU path and GPU params). Grain is
now skipped when the frame has no finite mapping, grain_noise returns 0 for
non-finite coordinates, and a non-finite grain term is never added.

Sources: own design.

(cherry picked from commit 4023853)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between 387bed1 and f621d24.

Files selected for processing: 2
  • crates/pipeline/src/finish.rs
  • crates/pipeline/src/tests.rs

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.

Walkthrough

The pipeline validates grain parameters before creating them. Grain noise returns zero for non-finite coordinates, and the renderer skips non-finite grain values. Tests cover non-finite coordinates and a zero-width crop.

Changes: Grain finiteness

Layer / File(s) Summary
Grain guards and regression tests
crates/pipeline/src/finish.rs, crates/pipeline/src/tests.rs
Grain parameters are created only when scale, dimensions, and the output-to-normalized transform meet finite and non-degenerate checks. Non-finite coordinates produce zero noise, and non-finite grain values are not added to encoded color. Tests cover non-finite coordinates and a zero-width crop with grain enabled.

Priority: Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: echelon, mdlmarkham

Merge Risk: Minimal · up to f621d

No actionable merge-blocking risk remains from the reviewed change.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 57.14% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 7 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed 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 names the core change: grain now skips a degenerate frame rather than producing NaN pixels. The raw summary and PR objectives confirm this. The changes to grain_noise, FinishParams::new, gra…
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.
Full details: Title check

Explanation

The title names the core change: grain now skips a degenerate frame rather than producing NaN pixels. The raw summary and PR objectives confirm this. The changes to grain_noise, FinishParams::new, grain application, and the added tests all address non-finite grain output on zero-width crops. The title includes a tracker-style prefix (LR-EDIT-EFFECTS-GRAIN), which is acceptable and does not make it misleading.

Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

@FredApps FredApps left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Approving at f621d24. I read the whole diff.

  • Skip grain on a degenerate frame: FinishParams computes out_to_norm once and enables grain only when px_per_long, the output size and every entry of that matrix are finite and positive. A zero-width crop therefore turns grain off for both the CPU and GPU paths, since both read the same params.
  • Per-pixel guards: grain_noise returns 0 for a non-finite coordinate and the grain add is skipped when k is not finite, so a NaN cannot reach the pixel from either place.
  • Tests: a unit test covers NaN and infinite coordinates, and a render test with a zero-width crop and grain at 60 checks that every sample in the F32 output is finite.

The normal path is unchanged for a finite frame: the same values are computed in the same order. I found no defect. The gate is green at this head. I read the code and did not run it.

@aiongg
aiongg merged commit 7fd7eef into storytold:main Oct 31, 2026
6 checks passed
@emmertex
emmertex deleted the upstream-pr/grain-degenerate-frame branch November 5, 2026 01:48
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.

3 participants