From 4d1d2b3900b8773565eebcd55c9246c4c5528056 Mon Sep 17 00:00:00 2001 From: clayjohn Date: Fri, 29 Oct 2021 21:41:57 -0700 Subject: Weight glow samples to reduce fireflies --- servers/rendering/renderer_rd/shaders/copy.glsl | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'servers') diff --git a/servers/rendering/renderer_rd/shaders/copy.glsl b/servers/rendering/renderer_rd/shaders/copy.glsl index 4110a95ddb..75ee4c55f7 100644 --- a/servers/rendering/renderer_rd/shaders/copy.glsl +++ b/servers/rendering/renderer_rd/shaders/copy.glsl @@ -132,6 +132,13 @@ void main() { local_cache[dest_index + 16] = textureLod(source_color, quad_center_uv + vec2(0.0, 1.0 / params.section.w), 0); local_cache[dest_index + 16 + 1] = textureLod(source_color, quad_center_uv + vec2(1.0 / params.section.zw), 0); } + if (bool(params.flags & FLAG_GLOW_FIRST_PASS)) { + // Tonemap initial samples to reduce weight of fireflies: https://graphicrants.blogspot.com/2013/12/tone-mapping.html + local_cache[dest_index] /= 1.0 + dot(local_cache[dest_index].rgb, vec3(0.299, 0.587, 0.114)); + local_cache[dest_index + 1] /= 1.0 + dot(local_cache[dest_index + 1].rgb, vec3(0.299, 0.587, 0.114)); + local_cache[dest_index + 16] /= 1.0 + dot(local_cache[dest_index + 16].rgb, vec3(0.299, 0.587, 0.114)); + local_cache[dest_index + 16 + 1] /= 1.0 + dot(local_cache[dest_index + 16 + 1].rgb, vec3(0.299, 0.587, 0.114)); + } memoryBarrierShared(); barrier(); @@ -177,6 +184,11 @@ void main() { color += temp_cache[index - 2] * 0.140367; color += temp_cache[index - 3] * 0.106595; + if (bool(params.flags & FLAG_GLOW_FIRST_PASS)) { + // Undo tonemap to restore range: https://graphicrants.blogspot.com/2013/12/tone-mapping.html + color /= 1.0 - dot(color.rgb, vec3(0.299, 0.587, 0.114)); + } + color *= params.glow_strength; if (bool(params.flags & FLAG_GLOW_FIRST_PASS)) { @@ -186,7 +198,7 @@ void main() { #endif color *= params.glow_exposure; - float luminance = max(color.r, max(color.g, color.b)); + float luminance = dot(color.rgb, vec3(0.299, 0.587, 0.114)); float feedback = max(smoothstep(params.glow_hdr_threshold, params.glow_hdr_threshold + params.glow_hdr_scale, luminance), params.glow_bloom); color = min(color * feedback, vec4(params.glow_luminance_cap)); -- cgit v1.2.3