Skip to content

Commit

Permalink
fixed divide by 0 shader artifact
Browse files Browse the repository at this point in the history
  • Loading branch information
Russ committed Sep 3, 2024
1 parent c827dc9 commit 9528866
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Core/Resources/CL/Debayer.cl.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,14 @@ PRIVATE half2 SynthesiseNonGreen(half Green, half TopLeftGreen, half TopRightGre
half SouthGreen = (BottomLeftGreen + BottomRightGreen) * (half)0.5f;
half NorthGreenDiff = fabs(NorthGreen - Green);
half SouthGreenDiff = fabs(SouthGreen - Green);
half VerticalWeight = NorthGreenDiff / (NorthGreenDiff + SouthGreenDiff);
half VerticalWeight = NorthGreenDiff / fmax(DEBAYER_DIFF_EPSILON, NorthGreenDiff + SouthGreenDiff);
half VerticalInterpolated = mix(Above, Below, clamp(VerticalWeight, (half)0.0f, (half)1.0f));

half WestGreen = (TopLeftGreen + BottomLeftGreen) * (half)0.5f;
half EastGreen = (TopRightGreen + BottomRightGreen) * (half)0.5f;
half WestGreenDiff = fabs(WestGreen - Green);
half EastGreenDiff = fabs(EastGreen - Green);
half HorizontalWeight = WestGreenDiff / (WestGreenDiff + EastGreenDiff);
half HorizontalWeight = WestGreenDiff / fmax(DEBAYER_DIFF_EPSILON, WestGreenDiff + EastGreenDiff);
half HorizontalInterpolated = mix(Left, Right, clamp(HorizontalWeight, (half)0.0f, (half)1.0f));

return make_half2(HorizontalInterpolated, VerticalInterpolated);
Expand Down
10 changes: 6 additions & 4 deletions Core/Resources/CL/HighlightRecovery.cl.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@
// Feather in stops to interpolate between highest/lowest channels
#define HIGHLIGHT_RECOVERY_CHANNEL_FEATHER_STOPS (1.0f)

#define HIGHLIGHT_RECOVERY_EPSILON ((half)0.0001f)

PRIVATE half3 SynthesiseThreeChannels(half3 cameraWhite)
{
return cameraWhite;
}

PRIVATE half3 SynthesiseTwoChannels(half3 rgb, half3 cameraWhite, eRGBChannel firstClipped, eRGBChannel secondClipped, eRGBChannel unclippedChannel)
{
half scale = IndexHalf3(rgb,unclippedChannel) / IndexHalf3(cameraWhite, unclippedChannel);
half scale = IndexHalf3(rgb,unclippedChannel) / fmax(HIGHLIGHT_RECOVERY_EPSILON, IndexHalf3(cameraWhite, unclippedChannel));
half3 synthesised = cameraWhite * scale;
half3 rgbOut = fmax(rgb, synthesised);

Expand All @@ -36,7 +38,7 @@ PRIVATE half3 SynthesiseTwoChannels(half3 rgb, half3 cameraWhite, eRGBChannel fi
rgbOut3[unclippedChannel] = IndexHalf3(rgb,unclippedChannel);

if ( IndexHalf3(rgb,firstClipped) > IndexHalf3(synthesised,firstClipped) || IndexHalf3(rgb,secondClipped) > IndexHalf3(synthesised,secondClipped) ) {
half avgScale = ((IndexHalf3(rgb,firstClipped) / IndexHalf3(synthesised,firstClipped)) + (IndexHalf3(rgb,secondClipped) / IndexHalf3(synthesised,secondClipped))) * 0.5f;
half avgScale = ((IndexHalf3(rgb,firstClipped) / fmax(HIGHLIGHT_RECOVERY_EPSILON, IndexHalf3(synthesised,firstClipped))) + (IndexHalf3(rgb,secondClipped) / fmax(HIGHLIGHT_RECOVERY_EPSILON, IndexHalf3(synthesised,secondClipped)))) * 0.5f;
rgbOut3[firstClipped] = (rgbOut3[firstClipped] * 0.4f) + (IndexHalf3(synthesised,firstClipped) * avgScale * 0.6f);
rgbOut3[secondClipped] = (rgbOut3[secondClipped] * 0.4f) + (IndexHalf3(synthesised,secondClipped) * avgScale * 0.6f);
}
Expand All @@ -62,11 +64,11 @@ PRIVATE half3 SynthesiseOneChannel(half3 rgb, half3 cameraWhite, eRGBChannel cli
}

// Scale camera white based on middle channel
half3 cameraWhiteScaled = cameraWhite * (IndexHalf3(cameraWhite,midChannel) / IndexHalf3(rgb,midChannel));
half3 cameraWhiteScaled = cameraWhite * (IndexHalf3(cameraWhite,midChannel) / fmax(HIGHLIGHT_RECOVERY_EPSILON, IndexHalf3(rgb,midChannel)));

// Use the 2 channel synthesis on the highest two channels
// Interpolate based on which channel of the two is higher
half channelDiffStops = log2(IndexHalf3(rgb,firstUnclipped) / IndexHalf3(rgb,secondUnclipped));
half channelDiffStops = log2(IndexHalf3(rgb,firstUnclipped) / fmax(HIGHLIGHT_RECOVERY_EPSILON, IndexHalf3(rgb,secondUnclipped)));
half channelDiffLerp = smoothstep((half)(HIGHLIGHT_RECOVERY_CHANNEL_FEATHER_STOPS * -0.5f), (half)(HIGHLIGHT_RECOVERY_CHANNEL_FEATHER_STOPS * 0.5f), channelDiffStops);
half3 synthesisedHighestChannels = mix(SynthesiseTwoChannels(rgb, cameraWhite, clippedChannel, secondUnclipped, firstUnclipped),
SynthesiseTwoChannels(rgb, cameraWhite, clippedChannel, firstUnclipped, secondUnclipped), channelDiffLerp);
Expand Down

0 comments on commit 9528866

Please sign in to comment.