[Bf-blender-cvs] [c23f33ac9e9] blender2.8: View3D: Atenuate banding artifacts on background gradient.

Clément Foucault noreply at git.blender.org
Sat Apr 21 16:49:03 CEST 2018


Commit: c23f33ac9e94d98cc1f593653f0e3abb031ecae0
Author: Clément Foucault
Date:   Sat Apr 21 16:45:47 2018 +0200
Branches: blender2.8
https://developer.blender.org/rBc23f33ac9e94d98cc1f593653f0e3abb031ecae0

View3D: Atenuate banding artifacts on background gradient.

Dithering the output color for 8bit precision framebuffer with bayer matrix.
On my tests the bayer matrux patterns are not noticeable at all.

Note that it also does that in opengl rendered mode which can be in a much
higher bitdepth. We can fix that if that's a problem in the future but I
doubt it will.

===================================================================

M	source/blender/draw/intern/draw_view.c
M	source/blender/gpu/CMakeLists.txt
M	source/blender/gpu/GPU_shader.h
M	source/blender/gpu/intern/gpu_shader.c
A	source/blender/gpu/shaders/gpu_shader_2D_smooth_color_dithered_frag.glsl

===================================================================

diff --git a/source/blender/draw/intern/draw_view.c b/source/blender/draw/intern/draw_view.c
index c65ed55561e..fd433442d64 100644
--- a/source/blender/draw/intern/draw_view.c
+++ b/source/blender/draw/intern/draw_view.c
@@ -579,7 +579,7 @@ void DRW_draw_background(void)
 		gpuLoadIdentity();
 		gpuLoadProjectionMatrix(m);
 
-		immBindBuiltinProgram(GPU_SHADER_2D_SMOOTH_COLOR);
+		immBindBuiltinProgram(GPU_SHADER_2D_SMOOTH_COLOR_DITHER);
 
 		UI_GetThemeColor3ubv(TH_LOW_GRAD, col_lo);
 		UI_GetThemeColor3ubv(TH_HIGH_GRAD, col_hi);
diff --git a/source/blender/gpu/CMakeLists.txt b/source/blender/gpu/CMakeLists.txt
index f915191fc28..231a1d0030a 100644
--- a/source/blender/gpu/CMakeLists.txt
+++ b/source/blender/gpu/CMakeLists.txt
@@ -143,6 +143,7 @@ data_to_c_simple(shaders/gpu_shader_2D_line_dashed_geom.glsl SRC)
 data_to_c_simple(shaders/gpu_shader_2D_smooth_color_vert.glsl SRC)
 data_to_c_simple(shaders/gpu_shader_2D_smooth_color_frag.glsl SRC)
 data_to_c_simple(shaders/gpu_shader_2D_smooth_color_uniform_alpha_vert.glsl SRC)
+data_to_c_simple(shaders/gpu_shader_2D_smooth_color_dithered_frag.glsl SRC)
 data_to_c_simple(shaders/gpu_shader_2D_image_vert.glsl SRC)
 data_to_c_simple(shaders/gpu_shader_2D_image_rect_vert.glsl SRC)
 data_to_c_simple(shaders/gpu_shader_2D_image_multi_rect_vert.glsl SRC)
diff --git a/source/blender/gpu/GPU_shader.h b/source/blender/gpu/GPU_shader.h
index 9131f56a07e..9f126953727 100644
--- a/source/blender/gpu/GPU_shader.h
+++ b/source/blender/gpu/GPU_shader.h
@@ -112,6 +112,7 @@ typedef enum GPUBuiltinShader {
 	GPU_SHADER_2D_FLAT_COLOR,
 	GPU_SHADER_2D_SMOOTH_COLOR,
 	GPU_SHADER_2D_SMOOTH_COLOR_UNIFORM_ALPHA,
+	GPU_SHADER_2D_SMOOTH_COLOR_DITHER,
 	GPU_SHADER_2D_IMAGE,
 	GPU_SHADER_2D_IMAGE_COLOR,
 	GPU_SHADER_2D_IMAGE_ALPHA_COLOR,
diff --git a/source/blender/gpu/intern/gpu_shader.c b/source/blender/gpu/intern/gpu_shader.c
index db464498a27..8956c8fae30 100644
--- a/source/blender/gpu/intern/gpu_shader.c
+++ b/source/blender/gpu/intern/gpu_shader.c
@@ -66,6 +66,7 @@ extern char datatoc_gpu_shader_2D_flat_color_vert_glsl[];
 extern char datatoc_gpu_shader_2D_smooth_color_uniform_alpha_vert_glsl[];
 extern char datatoc_gpu_shader_2D_smooth_color_vert_glsl[];
 extern char datatoc_gpu_shader_2D_smooth_color_frag_glsl[];
+extern char datatoc_gpu_shader_2D_smooth_color_dithered_frag_glsl[];
 extern char datatoc_gpu_shader_2D_image_vert_glsl[];
 extern char datatoc_gpu_shader_2D_image_rect_vert_glsl[];
 extern char datatoc_gpu_shader_2D_image_multi_rect_vert_glsl[];
@@ -702,6 +703,8 @@ GPUShader *GPU_shader_get_builtin_shader(GPUBuiltinShader shader)
 		                                 datatoc_gpu_shader_2D_smooth_color_frag_glsl },
 		[GPU_SHADER_2D_SMOOTH_COLOR_UNIFORM_ALPHA] = { datatoc_gpu_shader_2D_smooth_color_uniform_alpha_vert_glsl,
 		                                               datatoc_gpu_shader_2D_smooth_color_frag_glsl },
+		[GPU_SHADER_2D_SMOOTH_COLOR_DITHER] = { datatoc_gpu_shader_2D_smooth_color_vert_glsl,
+		                                        datatoc_gpu_shader_2D_smooth_color_dithered_frag_glsl },
 		[GPU_SHADER_2D_IMAGE_LINEAR_TO_SRGB] = { datatoc_gpu_shader_2D_image_vert_glsl,
 		                                         datatoc_gpu_shader_image_linear_frag_glsl },
 		[GPU_SHADER_2D_IMAGE] = { datatoc_gpu_shader_2D_image_vert_glsl,
diff --git a/source/blender/gpu/shaders/gpu_shader_2D_smooth_color_dithered_frag.glsl b/source/blender/gpu/shaders/gpu_shader_2D_smooth_color_dithered_frag.glsl
new file mode 100644
index 00000000000..145ed16248a
--- /dev/null
+++ b/source/blender/gpu/shaders/gpu_shader_2D_smooth_color_dithered_frag.glsl
@@ -0,0 +1,20 @@
+
+noperspective in vec4 finalColor;
+out vec4 fragColor;
+
+/* 4x4 bayer matrix prepared for 8bit UNORM precision error. */
+#define P(x) (((x + 0.5) * (1.0 / 16.0) - 0.5) * (1.0 / 255.0))
+const vec4 dither_mat4x4[4] = vec4[4](
+	vec4( P(0.0),  P(8.0),  P(2.0), P(10.0)),
+	vec4(P(12.0),  P(4.0), P(14.0),  P(6.0)),
+	vec4( P(3.0), P(11.0),  P(1.0),  P(9.0)),
+	vec4(P(15.0),  P(7.0), P(13.0),  P(5.0))
+);
+
+void main()
+{
+	ivec2 tx1 = ivec2(gl_FragCoord.xy) % 4;
+	ivec2 tx2 = ivec2(gl_FragCoord.xy) % 2;
+	float dither_noise = dither_mat4x4[tx1.x][tx1.y];
+	fragColor = finalColor + dither_noise;
+}



More information about the Bf-blender-cvs mailing list