Skip to content

Commit fcb7184

Browse files
garamond13Filoppi
authored andcommitted
Luma: Swith to safer Luma bloom threshold implementation
Equivelant to the previos implementation, but safe for large knee sharpness.
1 parent 28e5d5b commit fcb7184

1 file changed

Lines changed: 22 additions & 2 deletions

File tree

‎Shaders/Includes/Bloom.hlsl‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,26 @@ void bloom_main_vs(uint vid : SV_VertexID, out float4 pos : SV_Position, out flo
6565
// f(x) = x - x / (1 + h / (t - h) * (|x| / t)^p)
6666
//
6767
// This can break for large p (20+)!
68+
//float3 luma_bloom_threshold(float3 color)
69+
//{
70+
// // Rename.
71+
// const float t = LUMA_BLOOM_THRESHOLD_SOFT_THRESHOLD;
72+
// const float h = LUMA_BLOOM_THRESHOLD_KNEE_HEIGHT;
73+
// const float p = LUMA_BLOOM_THRESHOLD_KNEE_SHARPNESS;
74+
//
75+
// color = max(0.0, color);
76+
// const float k = max(color.r, max(color.g, color.b));
77+
// const float u = h / (t - h) * pow(k / t, p);
78+
// return color * (u / (1.0 + u)); // Optimized, color * scale(k) / k
79+
//}
80+
81+
// t - soft threshold, should be in range (0, +inf)
82+
// h - knee height, should be in range (0, t)
83+
// p - knee sharpness, should be in range (1, +inf)
84+
//
85+
// f(x) = x - x / (1 + exp2(log2(h / (t - h)) + p * log2(|x| / t)))
86+
//
87+
// This is equivalent to f(x) = x - x / (1 + h / (t - h) * (|x| / t)^p) implementation, but safe for large p.
6888
float3 luma_bloom_threshold(float3 color)
6989
{
7090
// Rename.
@@ -74,8 +94,8 @@ float3 luma_bloom_threshold(float3 color)
7494

7595
color = max(0.0, color);
7696
const float k = max(color.r, max(color.g, color.b));
77-
const float u = h / (t - h) * pow(k / t, p);
78-
return color * (u / (1.0 + u));
97+
const float u = log2(h * rcp(t - h)) + p * log2(max(1e-6, k * rcp(t)));
98+
return color * rcp(1.0 + exp2(-u)); // Optimized, color * scale(k) / k
7999
}
80100

81101
float get_gaussian_weight(float x)

0 commit comments

Comments
 (0)