Skip to content

Commit 6c4f57a

Browse files
committed
OptiSharpen: hm16 limiter against outlines around bright HDR lights
1 parent b7b07d2 commit 6c4f57a

14 files changed

Lines changed: 15316 additions & 9615 deletions

‎OptiScaler/shaders/rcas/precompile/opti_sharpen_fp16p.hlsl‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ RWTexture2D<float4> Dest : register(u0);
6060

6161
// =====================================================================================================
6262
// GENERATED by scripts/export_hlsl.py, do not edit by hand. Networks in half precision (min16float). Packed form (per-lane mads on column weights).
63-
// sharpener commit e48176f
64-
// Natural (Model 0): runs/Natural-D10/seed1_best.pt (low_p 4) (strength x 0.75), seed 1, step 10000, val loss 0.06060
65-
// Crisp (Model 1): runs/Crisp-D10-p50/seed0_best.pt (hc 2) (low_p 4), seed 0, step 10000, val loss 0.06466
63+
// sharpener commit 5a02ec0
64+
// Natural (Model 0): runs/Natural-D10/seed1_best.pt (hm 16) (low_p 4) (strength x 0.75), seed 1, step 10000, val loss 0.06060
65+
// Crisp (Model 1): runs/Crisp-D10-p50/seed0_best.pt (hc 2) (hm 16) (low_p 4), seed 0, step 10000, val loss 0.06466
6666
// Model config: {"slope": 0.2, "eps": 0.0001, "flat_t": 0.01, "ratio_t": 0.05, "luma": [0.2125999927520752, 0.7152000069618225, 0.0722000002861023]}; hidden / K: Natural 12 / 0.15, Crisp 12 / 0.15
6767
// Windows (network input + mean / clamp + flat test): Natural 25 / 13, Crisp 25 / 13 taps (9 = 3x3, 13 = 3x3 + the four at distance 2)
6868
// =====================================================================================================
@@ -83,6 +83,9 @@ float LowP() { return (Model == 1) ? kLowP1 : kLowP0; }
8383
static const float kHighContrast0 = 0.0; // Natural: residual / (1 + (range / mean / hc) ** 2), 0 = off
8484
static const float kHighContrast1 = 2.0; // Crisp
8585
float HighContrast() { return (Model == 1) ? kHighContrast1 : kHighContrast0; }
86+
static const float kHighContrastMin0 = 16.0; // Natural: residual / (1 + ((mean - min) / min / hm) ** 2), 0 = off
87+
static const float kHighContrastMin1 = 16.0; // Crisp
88+
float HighContrastMin() { return (Model == 1) ? kHighContrastMin1 : kHighContrastMin0; }
8689

8790
bool ClampLowRel() { return true; } // undershoot limited to K x min
8891
static const float kClampK = 0.150000006;
@@ -942,6 +945,14 @@ void CSMain(uint3 DTid : SV_DispatchThreadID, uint3 GTid : SV_GroupThreadID, uin
942945
float hq = (mx - mn) * inv / HighContrast();
943946
r /= 1.0 + hq * hq;
944947
}
948+
// HighContrastMin() > 0: the same against the darkest neighbour, 1 / (1 + ((mean - min) / min / hm) ** 2). Unbounded, so
949+
// it also holds next to 1e5 x HDR lamps, where range / mean saturates at ~2-3; the mean (all taps) because it scales
950+
// the residual, while the bounds taps can miss a lamp two pixels away
951+
if (HighContrastMin() > 0.0)
952+
{
953+
float hq = max(mean - mn, 0.0) / max(mn, kEps) / HighContrastMin();
954+
r /= 1.0 + hq * hq;
955+
}
945956
float lumaOut = lumaC + r * mean * (intensity * StrengthScale()); // per-model scale: the slider top = the model's best point
946957

947958
// Clamp to the neighbourhood range, overshoot K per model (0: none beyond the [min, max])
68 Bytes
Binary file not shown.

‎OptiScaler/shaders/rcas/precompile/opti_sharpen_fp16p_Shader.h‎

Lines changed: 2291 additions & 1445 deletions
Large diffs are not rendered by default.

‎OptiScaler/shaders/rcas/precompile/opti_sharpen_fp16p_Shader_Vk.h‎

Lines changed: 4734 additions & 2980 deletions
Large diffs are not rendered by default.
192 Bytes
Binary file not shown.

‎OptiScaler/shaders/rcas/precompile/opti_sharpen_fp32.hlsl‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ RWTexture2D<float4> Dest : register(u0);
6060

6161
// =====================================================================================================
6262
// GENERATED by scripts/export_hlsl.py, do not edit by hand.
63-
// sharpener commit e48176f
64-
// Natural (Model 0): runs/Natural-D10/seed1_best.pt (low_p 4) (strength x 0.75), seed 1, step 10000, val loss 0.06060
65-
// Crisp (Model 1): runs/Crisp-D10-p50/seed0_best.pt (hc 2) (low_p 4), seed 0, step 10000, val loss 0.06466
63+
// sharpener commit 5a02ec0
64+
// Natural (Model 0): runs/Natural-D10/seed1_best.pt (hm 16) (low_p 4) (strength x 0.75), seed 1, step 10000, val loss 0.06060
65+
// Crisp (Model 1): runs/Crisp-D10-p50/seed0_best.pt (hc 2) (hm 16) (low_p 4), seed 0, step 10000, val loss 0.06466
6666
// Model config: {"slope": 0.2, "eps": 0.0001, "flat_t": 0.01, "ratio_t": 0.05, "luma": [0.2125999927520752, 0.7152000069618225, 0.0722000002861023]}; hidden / K: Natural 12 / 0.15, Crisp 12 / 0.15
6767
// Windows (network input + mean / clamp + flat test): Natural 25 / 13, Crisp 25 / 13 taps (9 = 3x3, 13 = 3x3 + the four at distance 2)
6868
// =====================================================================================================
@@ -83,6 +83,9 @@ float LowP() { return (Model == 1) ? kLowP1 : kLowP0; }
8383
static const float kHighContrast0 = 0.0; // Natural: residual / (1 + (range / mean / hc) ** 2), 0 = off
8484
static const float kHighContrast1 = 2.0; // Crisp
8585
float HighContrast() { return (Model == 1) ? kHighContrast1 : kHighContrast0; }
86+
static const float kHighContrastMin0 = 16.0; // Natural: residual / (1 + ((mean - min) / min / hm) ** 2), 0 = off
87+
static const float kHighContrastMin1 = 16.0; // Crisp
88+
float HighContrastMin() { return (Model == 1) ? kHighContrastMin1 : kHighContrastMin0; }
8689

8790
bool ClampLowRel() { return true; } // undershoot limited to K x min
8891
static const float kClampK = 0.150000006;
@@ -766,6 +769,14 @@ void CSMain(uint3 DTid : SV_DispatchThreadID, uint3 GTid : SV_GroupThreadID, uin
766769
float hq = (mx - mn) * inv / HighContrast();
767770
r /= 1.0 + hq * hq;
768771
}
772+
// HighContrastMin() > 0: the same against the darkest neighbour, 1 / (1 + ((mean - min) / min / hm) ** 2). Unbounded, so
773+
// it also holds next to 1e5 x HDR lamps, where range / mean saturates at ~2-3; the mean (all taps) because it scales
774+
// the residual, while the bounds taps can miss a lamp two pixels away
775+
if (HighContrastMin() > 0.0)
776+
{
777+
float hq = max(mean - mn, 0.0) / max(mn, kEps) / HighContrastMin();
778+
r /= 1.0 + hq * hq;
779+
}
769780
float lumaOut = lumaC + r * mean * (intensity * StrengthScale()); // per-model scale: the slider top = the model's best point
770781

771782
// Clamp to the neighbourhood range, overshoot K per model (0: none beyond the [min, max])
60 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)