Repository navigation
LR-EDIT-LIGHT-SHADOWS: fix Highlights/Shadows block artefacts in fine detail; 4× faster GPU median - #3462
Conversation
From process V2 the Highlights/Shadows base was the edge-aware filter of a separable median of log luminance (radius 0.15 % of the long edge). Dark lettering on a bright panel (a bus number in a 7000 px raw: strokes and gaps about one radius wide) makes every row and column window close to half letter, half panel, so the median picked one tone or the other in rectangles: it filled the gaps between the digits, their counters and the corners where they nearly touch with the letters' tone, and parts of the strokes with the panel's. Shadows +75 then lifted those patches of panel by up to ~1.3 EV into bright yellow blocks, and lifted the strokes unevenly. The base now clamps each value to the quartiles of its window (`local::quartile_clamp_hv`, ranks r - r/2 and r + r/2 of 2r+1; at r = 1 the 3-tap median). A line thinner than about r/2 in an even surround is still a minority of every window and takes the surround's tone, as storytold#632 wants; a window that mixes two tones leaves its pixels alone, and steps stay exact. The storytold#632 twig/stalk tests keep their margins; twig meshes covering ~50 % of their area keep less contrast than under the median (0.69 of Exposure's at -100, as V1). GPU: the kernels count the centre's rank first and select only for pixels outside the band, from the nearer end; the planes stage of a 32 MP render with Shadows went from ~228 ms to ~165 ms. CPU cost unchanged. CPU and GPU match. Lands in place on V2 under the bug-fix rule of docs/process-versions.md (the blocks are output in neither the photo nor the edit), noted there. RENDER_CACHE_VERSION 72 -> 73 so cached thumbnails re-render. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Note Reviews pausedIt 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 Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
Render-quality fixes landed in place are recorded in docs/process-versions.md (as storytold#3272 was), not in the release notes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…n two medians The interquartile clamp fixed the lettering but stopped treating isolated lines between r/2 and r px wide as detail: on a corpus Sony A7 III (shadows +100) twigs of 3-6 px against a dark wall were lifted less than the wall, flattening them as V1 did. Light lines 6-10 px wide leaked 0.3-0.6 of the way into the base (the median: none). A fixed window can't tell an isolated line from the gap between two strokes; a wider neighbourhood can (a twig's is mostly wall, a gap's is mostly panel). `local::detail_free` therefore clamps each value between its separable median at r (main's `median_hv`) and the median of a 3x wider neighbourhood (the median at r of a box-downscaled copy, enlarged bilinearly). Both medians on the same side of the pixel (a twig, a speck): it takes the nearer one. They disagree (a gap between strokes): it keeps its own value. Steps stay exact (the fine median keeps them). - Lettering test: 0 panel px over 1 EV (median: 360; worst 0.8 EV). - New test: isolated lines of 1-9 px stay out of the base as under the median; the quartile clamp fails it (6 px line 0.31 into the base). - GPU: main's median kernels back, the coarse median through the existing box/bilinear resize, and a `clamp_between_k` map kernel. CPU and GPU match (max 1 level). - 32 MP render with Shadows: GPU planes ~231 -> ~254 ms, CPU base ~400 -> ~465 ms; the same +10 % / +14 % on an ISO 3200 file. - Corpus A/B (451 pinned raws x Shadows +100 / Highlights -100, old vs new, full size): no failures; RMSE median 0.18 % / 0.10 %, max 0.86 %; worst 8x8 block median 10 / 7 levels, max 61. The 22 largest local changes inspected show no new artefacts, and one more instance of the bug fixed (a bright block in the gap between two posts, Hasselblad coverage-6568). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
The bus in the description is the photo from #3460 ( The reporter's view: in the desktop app at 300 %, on the GPU, CPU against GPU, full size: at most 1 level apart, none over 1, no fallback to the CPU, no driver errors logged.
The cost is higher on this card than the description's +10 %. GPU planes on the #3460 photo with those sliders, median of 6 alternating runs, each in a fresh process:
That's about 195 ms in a full-size render that takes about 7 s in all, so it's small for an export. It may matter more on older cards than the +10 % suggests. Not tested: macOS, Linux, other GPUs, and drafts while a slider moves (I only looked at finished renders). I didn't repeat your corpus run. |
For reference, original tests were done on an Apple M2 Max on MacOS. |
…s base The base's separable median dominated the GPU planes stage: 191 ms of the 226 ms base on a 6960×4640 plane (r = 10, M2 Max). Each pixel ran a partial selection sort over a private 49-tap array, which drivers keep in scratch memory because it is indexed dynamically. median_h / median_v now load each 16×16 workgroup's taps into workgroup memory and select by rank, 4 neighbouring outputs per thread, which share all but their first and last 3 taps. Same selection, so the output is bit for bit the CPU's (new test median_matches_cpu_bit_for_bit, every r 1–24, edge tiles, ties). Median 191 → 47 ms; full-size GPU planes on the storytold#3460 CR3 with Whites +54, Blacks +31, Shadows +75: main 231, before this commit 257, now 113 ms. Renders are byte-identical to before on GPU and CPU. CPU: detail_free enlarges the coarse median while it clamps, into the fine median in place, instead of storing two more full-size planes (same sums; detail_free_matches_its_reference checks it bit for bit). ~10 ms. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QQTJhXnxfpjcQxB77dvuYA
|
Thanks for the 1070 numbers. Looking into it, the coarse median wasn't the main cost: the fine median the base already had on fcac37b rewrites On the #3460 CR3 with Whites +54, Blacks +31, Shadows +75, full size, M2 Max (5 runs alternating):
The tile layout also avoids workgroup-memory bank conflicts, which NVIDIA penalises more than Apple. I haven't been able to measure it on NVIDIA. Could you rerun your 1070 timing on fcac37b? The CPU path changes only a little (~10 ms; its median was already a sorted sliding window), so the CPU base is still about 16 % over |
FredApps
left a comment
There was a problem hiding this comment.
ReviewerA — source-read COMMENT (not an approval)
I read the first hunk: RENDER_CACHE_VERSION goes from 72 to 73 (invalidating cached thumbnails because the pipeline's output changes), and per the title (LR-EDIT-LIGHT-SHADOWS) the Shadows adjustment no longer leaves bright blocks beside lettering. Bumping the cache version with a change in the output is the right rule. The change itself is in the pipeline, which I did not read. A fix for artefacts around high-contrast edges is in the part of the algorithm that builds a smooth base (a local filter): please attach before and after crops of the lettering case, and add a regression test with a synthetic image (black text on white, Shadows at +100) asserting that no pixel next to the text exceeds the maximum of its neighbourhood. Nothing was run.
I ran nothing and have no exact-head CI result for this PR, so this is not an approval. Where a diff was long I read its code hunks rather than every generated or documentation line.
Status: waiting_author.
|
The rewrite helps on the 1070 too: full-size planes are now well under
The planes are the #3460 photo with Whites +54, Blacks +31 and Shadows +75, medians of 5 to 7 alternating runs, each in a fresh process. As pushed, What the delay looks like: in the desktop app, launch to a finished render at 300 % goes from 9.5 s on Cause and fix: wgpu zeroes compilation_options: wgpu::PipelineCompilationOptions { zero_initialize_workgroup_memory: false, ..Default::default() },It looks safe here: With that line:
One failure I don't think comes from this PR: Not tested: macOS, Linux, other GPUs, drafts while a slider moves, and the full app launch with the line (the 1.4 s is the compile time the CLI logs). |
…rnels wgpu zeroes `var<workgroup>` arrays by default; on DX12 that is one assignment per element of `med_tile`, and FXC took about 87 s over the median kernels at every start of a release build (GTX 1070, reported on storytold#3462). `nn.rs` and `faces.rs` already turn it off. The median kernels are the only ones `build_jobs` builds that use workgroup memory, and their load loop writes every element `median4` reads before the barrier. Renders are byte-identical (the storytold#3460 CR3, Whites +54, Blacks +31, Shadows +75, M2 Max). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QQTJhXBE6yZLw3xqWysb6P
|
@insideTheFlux Thanks, good catch. 14e8783 turns off workgroup-memory zeroing in Agreed on |
|
@FredApps Both are already in the PR. The description has before/after crops of the lettering case at 300 % (the bus fleet number), plus the Hasselblad gap and synthetic lettering. The regression test is |
|
Have now tested also on Linux with an AMD integrated GPU: Ryzen 5 7545U (RADV Phoenix2, Vulkan, Mesa), Fedora 44, kernel 7.2.7. This is 14e8783 merged onto Tests: Timings: each row is 5 runs alternating
Vulkan shows nothing like the FXC startup delay. CPU against GPU (this PR): full size, at most 1 level apart in all six cases, with no warnings and no CPU fallback:
Unedited, the R100 and
Not from this PR, but a warning for anyone testing on an integrated GPU: Not tested: the desktop app (command line only), drafts while a slider moves, and the #3460 bus CR3 (not in the corpus). 🤖 Generated with Claude Code |
|
Confirmed on Windows at 14e8783 (GTX 1070, DX12, FXC, release build): the kernels now compile in about 1.4 s, against about 87 s at fcac37b and 0.8 s on |
…de lettering under Shadows (V2 bug fix)
Conflict: docs/process-versions.md. Kept storytold#3462's updated V2 wording (the two-median clamp) and moved "(the latest)" to storytold#3407's new V3 row.
|
@aiongg assigning this to you and requesting your review. Context: after merging current main, main's locked legacy test The preferred route is to gate the new base behind a new process version so V1–V4 stay pixel-identical, and to consider splitting out the DX12 workgroup-memory-zeroing speedup (no pixel change) so it can land on its own. Could you also run the macOS testing where required? In particular, the macOS-only hash test |
…cess V3 Main froze V1 and V2 with locked samples (tests_legacy), and this branch changed V2's Highlights/Shadows base. V2 keeps its plain median now and `detail_free` applies from V3 (`local::base_keeps_gaps`, on the CPU and the GPU), which is not in a release yet (v0.18.1's latest is V2). V3 and V4 keep the same base as each other. The faster median kernels and the workgroup-memory change don't change pixels and apply to all. RENDER_CACHE_VERSION 76. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…process V7 v0.19.0 released processes V3 to V6, so the lettering fix can no longer change V3: it becomes process V7 (V6 plus `local::detail_free`), and V1 to V6 render as before. `local::base_keeps_gaps` is true from V7; the docs, checklist rows and tests follow. The unknown-process test reads one past the latest instead of a fixed 7. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@FredApps Done in d2d39a0, on the route you preferred:
Run after the merge, on Linux (Ryzen 5 7545U, RADV): |
…process V8 Crystal grain (storytold#3630) took process V7 on main, so the Highlights/Shadows detail-free base now starts at V8 (V7 plus `local::detail_free`). V1 to V7 keep the plain median and render as before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
Approving at d4bdd065.
The route I asked for on 11-05 is in. The detail-free base applies only from the new process V8 (settings.rs), so V1 to V7 render as before. only_process_v8_on_takes_the_detail_free_base checks every earlier process, main's locked legacy samples pass unchanged, and docs/process-versions.md has the V8 row. The rest of my original comment is covered too: the description has before/after crops, and gaps_between_letters_keep_their_own_tone_in_the_base is a synthetic regression that, by your run, fails with the old median.
The GPU side:
- The new median is checked against the CPU bit for bit (
median_matches_cpu_bit_for_bit, every r from 1 to 24, edge tiles and ties). - Workgroup-memory zeroing is turned off only in
build_jobs. I checked that the only kernels it builds with workgroup memory are the newblur.wgslmedians.faces_*.wgslandnn_conv.wgslbuild their own pipelines and already had zeroing off.
insideTheFlux's Windows numbers (1.4 s compile, faster planes) and your RADV run cover the drivers I can't test.
What validated it: the Linux gate passes on this head. Windows was skipped by its path filter.
What I didn't run: I didn't build it or run the GPU tests locally, because the box has no GPU.
Rule 15 (not blocking): the description ends with "Generated with Claude Code" and seven commits carry Co-Authored-By: Claude trailers. Please leave AI attribution out of both.
Fixes #3460
Related: #2936 (slow GPU kernel compiles on Windows/DX12). This PR builds the GPU kernels without wgpu's workgroup-memory zeroing, which took a DX12 release start from ~87 s to 1.4 s on a GTX 1070 with the new median kernels; it may shorten the compiles there too, but does not close it.
Problem
Raising Shadows on a photo with dark lettering on a bright panel makes bright blocks grow beside the
letters. On a Canon CR3 of a bus (6960×4640) with a black fleet number on a yellow panel, Shadows +75
puts bright yellow rectangles in the gaps where the digits pinch together and inside the 4's counter.
At Shadows +5 there is none of this; at +75 it shows at any zoom. Narrow gaps between dark objects
generally can do it: the pinned corpus has the same thing in the gap between two posts under a machine
(Hasselblad,
coverage-6568.fff).Cause
From process V2 the Highlights/Shadows base (
local::tone_base) is the edge-aware filter of aseparable median of log luminance, at a radius of 0.15 % of the long edge (10 px here), so that thin
detail (twigs, lines, #632) takes its surroundings' shift. Text strokes and gaps are about that wide,
so each row and column window is close to half letter and half panel, and the median picks one tone
or the other in rectangles: it fills the gaps between the digits and the 4's counter with the letters'
tone. The guided filter keeps those blocks, so panel pixels whose base reads as deep shadow get
Shadows' full lift (
2^(1.7·s·w^1.5), up to ~1.3 EV at +75).Examples
Top: before, bottom: after.
The fleet number on the bus photo, Whites +54, Blacks +31, Shadows +75, at 300 %:
The gap between two posts in the corpus' Hasselblad
coverage-6568.fff(CC0, raw.pixls.us),Shadows +100, at 300 %:
Synthetic lettering at the scale of a bus number in a 7000 px photo, Shadows +75, at 200 %:
Change
local::detail_free(CPU) and its GPU twin: each value of log luminance is clamped between itsseparable median at r (as before) and the median of a 3× wider neighbourhood (the median at r of a
box-downscaled copy, enlarged bilinearly, like
guided_fast's coefficients). The base is the guidedfilter of that plane.
as under the median alone (16 Bit tiffs/other files treated as 8bit #632 unchanged).
disagree and the gap keeps its own tone. The strokes themselves count as fine dark detail on the
panel (both medians agree) and keep their contrast, as twigs do.
clamp_between_kmap kernel.CPU and GPU match (max 1 level).
median_h/median_v): each workgroup loads its taps intoworkgroup memory and selects by rank, 4 neighbouring outputs per thread. The old partial selection
sort over a private 49-tap array sat in scratch memory. Same selection, so bit for bit the CPU's
(
median_matches_cpu_bit_for_bit, every r 1–24); renders are byte-identical to the old kernels.On 6960×4640 at r = 10 (M2 Max) the median takes 191 → 47 ms.
nn.rsandfaces.rsalreadyare): on DX12 it made FXC take ~87 s over the new median kernels at every start of a release build
(GTX 1070, 1.4 s without it). Every element of
med_tilethe kernels read is loaded before the barrier;renders are byte-identical.
231 ms on
main→ 113 ms here (257 ms with this PR's first kernels); CPU base ~388 → ~453 ms.The CPU path enlarges the coarse median while it clamps, into the fine median in place
(
detail_free_matches_its_reference, bit for bit).Process::LATEST). Existing edits keep their process and render as before, soRENDER_CACHE_VERSIONis unchanged (it is 76 onmain).process-versions.md(V8 row, a paragraph on the lettering fix, revision history),parity-checklist.md(LR-EDIT-LIGHT-HIGHLIGHTS / -SHADOWS notes).
An earlier revision of this PR clamped each value to the quartiles of its window instead. It fixed
the lettering but stopped treating isolated lines between r/2 and r wide as detail (twigs of 3–6 px
against a dark wall were lifted less than the wall, as under V1), which the corpus run below caught.
A fixed window can't tell an isolated line from a gap between strokes; a wider neighbourhood can.
Process version
The new base applies from a new process, V8 (V7 plus
local::detail_free;local::base_keeps_gapsis true from V8, CPU and GPU). v0.19.0 shipped V3 to V6, and crystal grain (#3630) took V7 onmain, so V1 to V7 render exactly as before:tests_legacystill locks V1 and V2, andonly_process_v8_on_takes_the_detail_free_basechecks that every earlier process keeps the plain median. New photos get V8; photos already edited keep the blocks until Update to Current Process.The faster median kernels and the workgroup-memory change don't change pixels, so they apply to every process. They stay in this PR: the zeroing only costs time on the new median kernels (on
mainthe kernels compile in 0.8 s on the GTX 1070), so on its own it would speed up nothing.How it was checked
gaps_between_letters_keep_their_own_tone_in_the_base: a row of synthetic "8"ssized like the bus number. With the median, 360 panel pixels sit more than 1 EV above their base
(worst 2.25 EV); now none do (worst 0.8 EV). It fails with the median alone.
isolated_lines_up_to_the_detail_radius_stay_out_of_the_base: light lines 1–9 px wideon a dark ground (r = 10) stay out of the base, as under the median. It fails with the quartile
clamp (a 6 px line 0.31 of the way into the base).
detail_free_keeps_steps_and_regions: steps and flat fields pass through unchanged;tiny, empty and NaN planes don't panic.
and the median reference tests pass unchanged.
lightcraft-gpuintegration tests on a real adapter, includinghighlights_shadows_base_matches_on_every_process(max difference 1 level).cargo xtask corpus --download) rendered at full size beforeand after, with Shadows +100 and with Highlights −100 (902 pairs, GPU). No failures. RMSE median
0.18 % (Shadows) / 0.10 % (Highlights), max 0.86 %; pixels changed by more than 3 % at most 0.07 %
of a photo; worst 8×8-block change median 10 / 7 levels, 99th percentile 44 / 41, max 61. I looked
at the 22 largest local changes (old over new, at the worst block): no new artefacts. Most are a
slightly different tone in small gaps among foliage or branches; one is the Hasselblad gap above.
Blacks +31 and Shadows +5 / +75, and looked at the number, the plate, foliage against the sky and a
brick lattice fence.
median_matches_cpu_bit_for_bit(GPU, M2 Max) anddetail_free_matches_its_reference(CPU).mainand moving the base to V7 (Linux, Ryzen 5 7545U, RADV):cargo test --releaseoflightcraft-develop,-pipeline(includingtests_legacy),-catalog,-engine, and-gpuon the real adapter (skippingrunning_out_of_device_memory_falls_back_without_stopping_the_gpu, which fills RAM on an APU; see above);cargo fmt,cargo clippy --workspace --all-targets -D warnings,cargo xtask layers,roots,assets,parityandwasm. Not run: the fullcargo xtask ci, and the macOS-onlytests_light::v1_and_v2_render_as_before_v3(V1 and V2 don't change, so it shouldn't need re-capturing).mainagain (with crystal grain as V7) and moving the base to V8 (Linux, Ryzen 5 7545U, RADV):cargo test --releaseoflightcraft-develop,-pipeline(includingtests_legacyand the grain tests),-catalog,-engine, and-gpuon the real adapter (skippingrunning_out_of_device_memory_falls_back_without_stopping_the_gpu);cargo fmt,cargo clippy --workspace --all-targets -D warnings,cargo xtask layers,roots,assetsandparity. Not run:wasmand the fullcargo xtask ci.cargo xtask ci --quick(withCRAFT_FONTS_DIR): all steps pass. After the median change: all steps pass again (withoutCRAFT_FONTS_DIR), and the CR3 rendersbyte-identical to the first kernels on GPU and CPU.
Summary by CodeRabbit