[Bf-blender-cvs] [2f9f3d09a94] greasepencil-object: Fix alpha double blend

Antonio Vazquez noreply at git.blender.org
Tue Mar 27 18:03:37 CEST 2018


Commit: 2f9f3d09a943d035e06555476d528f72b60f49fc
Author: Antonio Vazquez
Date:   Tue Mar 27 18:03:26 2018 +0200
Branches: greasepencil-object
https://developer.blender.org/rB2f9f3d09a943d035e06555476d528f72b60f49fc

Fix alpha double blend

The color when used alpha factor always got darker. The problem was a double blend that produced a reduction in the color saturation.

Now, there is a premult of the alpha factor before doing last blend pass.

As a result of this change, now the drawing process can be simplified and a pass can be removed. Some cleanup done too.

Still need more work in render mode. Maybe the problem in render is related to Tonemapping.

Thanks Clément Foucault for his help solving this issue.

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

M	source/blender/draw/CMakeLists.txt
M	source/blender/draw/engines/gpencil/gpencil_engine.c
M	source/blender/draw/engines/gpencil/gpencil_engine.h
M	source/blender/draw/engines/gpencil/gpencil_vfx.c
R064	source/blender/draw/engines/gpencil/shaders/gpencil_front_depth_mix_frag.glsl	source/blender/draw/engines/gpencil/shaders/gpencil_simple_mix_frag.glsl
M	source/blender/draw/engines/gpencil/shaders/gpencil_zdepth_mix_frag.glsl

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

diff --git a/source/blender/draw/CMakeLists.txt b/source/blender/draw/CMakeLists.txt
index fd74026f6e3..06584779fba 100644
--- a/source/blender/draw/CMakeLists.txt
+++ b/source/blender/draw/CMakeLists.txt
@@ -253,7 +253,7 @@ data_to_c_simple(engines/gpencil/shaders/gpencil_stroke_vert.glsl SRC)
 data_to_c_simple(engines/gpencil/shaders/gpencil_stroke_geom.glsl SRC)
 data_to_c_simple(engines/gpencil/shaders/gpencil_stroke_frag.glsl SRC)
 data_to_c_simple(engines/gpencil/shaders/gpencil_zdepth_mix_frag.glsl SRC)
-data_to_c_simple(engines/gpencil/shaders/gpencil_front_depth_mix_frag.glsl SRC)
+data_to_c_simple(engines/gpencil/shaders/gpencil_simple_mix_frag.glsl SRC)
 data_to_c_simple(engines/gpencil/shaders/gpencil_point_vert.glsl SRC)
 data_to_c_simple(engines/gpencil/shaders/gpencil_point_geom.glsl SRC)
 data_to_c_simple(engines/gpencil/shaders/gpencil_point_frag.glsl SRC)
diff --git a/source/blender/draw/engines/gpencil/gpencil_engine.c b/source/blender/draw/engines/gpencil/gpencil_engine.c
index dabd6b5ed0e..ca38702954c 100644
--- a/source/blender/draw/engines/gpencil/gpencil_engine.c
+++ b/source/blender/draw/engines/gpencil/gpencil_engine.c
@@ -57,7 +57,7 @@ extern char datatoc_gpencil_stroke_vert_glsl[];
 extern char datatoc_gpencil_stroke_geom_glsl[];
 extern char datatoc_gpencil_stroke_frag_glsl[];
 extern char datatoc_gpencil_zdepth_mix_frag_glsl[];
-extern char datatoc_gpencil_front_depth_mix_frag_glsl[];
+extern char datatoc_gpencil_simple_mix_frag_glsl[];
 extern char datatoc_gpencil_point_vert_glsl[];
 extern char datatoc_gpencil_point_geom_glsl[];
 extern char datatoc_gpencil_point_frag_glsl[];
@@ -187,6 +187,7 @@ static void GPENCIL_engine_free(void)
 	DRW_SHADER_FREE_SAFE(e_data.gpencil_point_sh);
 	DRW_SHADER_FREE_SAFE(e_data.gpencil_edit_point_sh);
 	DRW_SHADER_FREE_SAFE(e_data.gpencil_fullscreen_sh);
+	DRW_SHADER_FREE_SAFE(e_data.gpencil_simple_fullscreen_sh);
 	DRW_SHADER_FREE_SAFE(e_data.gpencil_vfx_blur_sh);
 	DRW_SHADER_FREE_SAFE(e_data.gpencil_vfx_wave_sh);
 	DRW_SHADER_FREE_SAFE(e_data.gpencil_vfx_pixel_sh);
@@ -194,7 +195,6 @@ static void GPENCIL_engine_free(void)
 	DRW_SHADER_FREE_SAFE(e_data.gpencil_vfx_flip_sh);
 	DRW_SHADER_FREE_SAFE(e_data.gpencil_vfx_light_sh);
 	DRW_SHADER_FREE_SAFE(e_data.gpencil_painting_sh);
-	DRW_SHADER_FREE_SAFE(e_data.gpencil_front_depth_sh);
 	DRW_SHADER_FREE_SAFE(e_data.gpencil_paper_sh);
 
 	DRW_TEXTURE_FREE_SAFE(e_data.gpencil_blank_texture);
@@ -254,6 +254,9 @@ static void GPENCIL_cache_init(void *vedata)
 	if (!e_data.gpencil_fullscreen_sh) {
 		e_data.gpencil_fullscreen_sh = DRW_shader_create_fullscreen(datatoc_gpencil_zdepth_mix_frag_glsl, NULL);
 	}
+	if (!e_data.gpencil_simple_fullscreen_sh) {
+		e_data.gpencil_simple_fullscreen_sh = DRW_shader_create_fullscreen(datatoc_gpencil_simple_mix_frag_glsl, NULL);
+	}
 	if (!e_data.gpencil_vfx_blur_sh) {
 		e_data.gpencil_vfx_blur_sh = DRW_shader_create_fullscreen(datatoc_gpencil_gaussian_blur_frag_glsl, NULL);
 	}
@@ -275,9 +278,6 @@ static void GPENCIL_cache_init(void *vedata)
 	if (!e_data.gpencil_painting_sh) {
 		e_data.gpencil_painting_sh = DRW_shader_create_fullscreen(datatoc_gpencil_painting_frag_glsl, NULL);
 	}
-	if (!e_data.gpencil_front_depth_sh) {
-		e_data.gpencil_front_depth_sh = DRW_shader_create_fullscreen(datatoc_gpencil_front_depth_mix_frag_glsl, NULL);
-	}
 	if (!e_data.gpencil_paper_sh) {
 		e_data.gpencil_paper_sh = DRW_shader_create_fullscreen(datatoc_gpencil_paper_frag_glsl, NULL);
 	}
@@ -388,12 +388,11 @@ static void GPENCIL_cache_init(void *vedata)
 		/* vfx copy pass from txtb to txta */
 		struct Gwn_Batch *vfxquad = DRW_cache_fullscreen_quad_get();
 		psl->vfx_copy_pass = DRW_pass_create("GPencil VFX Copy b to a Pass", DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS);
-		DRWShadingGroup *vfx_copy_shgrp = DRW_shgroup_create(e_data.gpencil_fullscreen_sh, psl->vfx_copy_pass);
+		DRWShadingGroup *vfx_copy_shgrp = DRW_shgroup_create(e_data.gpencil_simple_fullscreen_sh, psl->vfx_copy_pass);
 		stl->g_data->tot_sh++;
 		DRW_shgroup_call_add(vfx_copy_shgrp, vfxquad, NULL);
 		DRW_shgroup_uniform_texture_ref(vfx_copy_shgrp, "strokeColor", &e_data.vfx_color_tx_b);
 		DRW_shgroup_uniform_texture_ref(vfx_copy_shgrp, "strokeDepth", &e_data.vfx_depth_tx_b);
-		DRW_shgroup_uniform_int(vfx_copy_shgrp, "tonemapping", &stl->storage->tonemapping, 1);
 
 		/* VFX pass */
 		psl->vfx_wave_pass = DRW_pass_create("GPencil VFX Wave Pass", DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS);
@@ -420,14 +419,6 @@ static void GPENCIL_cache_init(void *vedata)
 		DRW_shgroup_uniform_texture_ref(painting_shgrp, "strokeColor", &e_data.painting_color_tx);
 		DRW_shgroup_uniform_texture_ref(painting_shgrp, "strokeDepth", &e_data.painting_depth_tx);
 
-		/* pass for current stroke drawing in front of all */
-		struct Gwn_Batch *frontquad = DRW_cache_fullscreen_quad_get();
-		psl->mix_pass_front = DRW_pass_create("GPencil Mix Front Depth Pass", DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS);
-		DRWShadingGroup *mix_front_shgrp = DRW_shgroup_create(e_data.gpencil_front_depth_sh, psl->mix_pass_front);
-		stl->g_data->tot_sh++;
-		DRW_shgroup_call_add(mix_front_shgrp, frontquad, NULL);
-		DRW_shgroup_uniform_texture_ref(mix_front_shgrp, "strokeColor", &e_data.temp_color_tx);
-
 		/* pass for drawing paper (only if viewport)
 		 * In render, the v3d is null
 		 */
@@ -734,30 +725,9 @@ static void GPENCIL_draw_scene(void *vedata)
 		MULTISAMPLE_SYNC_ENABLE(dfbl);
 		
 		DRW_draw_pass(psl->painting_pass);
-		
-		MULTISAMPLE_SYNC_DISABLE(dfbl);
-
-		/* Current stroke must pass through the temp framebuffer to get same alpha values in blend */
-		GPU_framebuffer_texture_attach(fbl->temp_fb, e_data.temp_depth_tx, 0, 0);
-		GPU_framebuffer_texture_attach(fbl->temp_fb, e_data.temp_color_tx, 0, 0);
-
-		GPU_framebuffer_bind(fbl->temp_fb);
-		GPU_framebuffer_clear_color_depth(fbl->temp_fb, clearcol, 1.0f);
-		MULTISAMPLE_GP_SYNC_ENABLE(dfbl, fbl);
-
 		DRW_draw_pass(psl->drawing_pass);
 
-		MULTISAMPLE_GP_SYNC_DISABLE(dfbl, fbl);
-
-		/* send to default framebuffer */
-		GPU_framebuffer_bind(dfbl->default_fb);
-		DRW_draw_pass(psl->mix_pass_front);
-
-		GPU_framebuffer_texture_detach(fbl->temp_fb, e_data.temp_depth_tx);
-		GPU_framebuffer_texture_detach(fbl->temp_fb, e_data.temp_color_tx);
-
-		/* attach again default framebuffer after detach textures */
-		GPU_framebuffer_bind(dfbl->default_fb);
+		MULTISAMPLE_SYNC_DISABLE(dfbl);
 
 		/* free memory */
 		gpencil_free_obj_list(stl);
@@ -839,9 +809,9 @@ static void GPENCIL_draw_scene(void *vedata)
 				}
 				/* tonemapping */
 				stl->storage->tonemapping = stl->storage->is_render ? 1 : 0;
+
 				DRW_draw_pass(psl->mix_pass);
 
-				stl->storage->tonemapping = 0;
 				/* prepare for fast drawing */
 				if (!is_render) {
 					gpencil_prepare_fast_drawing(stl, dfbl, fbl, psl->mix_pass_noblend, clearcol);
diff --git a/source/blender/draw/engines/gpencil/gpencil_engine.h b/source/blender/draw/engines/gpencil/gpencil_engine.h
index 0ee821464a9..f8e78e361bb 100644
--- a/source/blender/draw/engines/gpencil/gpencil_engine.h
+++ b/source/blender/draw/engines/gpencil/gpencil_engine.h
@@ -197,7 +197,6 @@ typedef struct GPENCIL_PassList {
 	struct DRWPass *drawing_pass;
 	struct DRWPass *mix_pass;
 	struct DRWPass *mix_pass_noblend;
-	struct DRWPass *mix_pass_front;
 	struct DRWPass *vfx_copy_pass;
 	struct DRWPass *vfx_wave_pass;
 	struct DRWPass *vfx_blur_pass_1;
@@ -272,6 +271,7 @@ typedef struct GPENCIL_e_data {
 	struct GPUShader *gpencil_line_sh;
 	struct GPUShader *gpencil_drawing_fill_sh;
 	struct GPUShader *gpencil_fullscreen_sh;
+	struct GPUShader *gpencil_simple_fullscreen_sh;
 	struct GPUShader *gpencil_vfx_blur_sh;
 	struct GPUShader *gpencil_vfx_wave_sh;
 	struct GPUShader *gpencil_vfx_pixel_sh;
@@ -279,7 +279,6 @@ typedef struct GPENCIL_e_data {
 	struct GPUShader *gpencil_vfx_flip_sh;
 	struct GPUShader *gpencil_vfx_light_sh;
 	struct GPUShader *gpencil_painting_sh;
-	struct GPUShader *gpencil_front_depth_sh;
 	struct GPUShader *gpencil_paper_sh;
 	/* temp depth texture */
 	struct GPUTexture *temp_depth_tx;
diff --git a/source/blender/draw/engines/gpencil/gpencil_vfx.c b/source/blender/draw/engines/gpencil/gpencil_vfx.c
index 4b500dd42d7..ebfcb79e98a 100644
--- a/source/blender/draw/engines/gpencil/gpencil_vfx.c
+++ b/source/blender/draw/engines/gpencil/gpencil_vfx.c
@@ -99,12 +99,11 @@ static void DRW_gpencil_vfx_copy(
 	GPENCIL_StorageList *stl = ((GPENCIL_Data *)vedata)->stl;
 
 	struct Gwn_Batch *vfxquad = DRW_cache_fullscreen_quad_get();
-	DRWShadingGroup *vfx_shgrp = DRW_shgroup_create(e_data->gpencil_fullscreen_sh, psl->vfx_wave_pass);
+	DRWShadingGroup *vfx_shgrp = DRW_shgroup_create(e_data->gpencil_simple_fullscreen_sh, psl->vfx_wave_pass);
 	++stl->g_data->tot_sh;
 	DRW_shgroup_call_add(vfx_shgrp, vfxquad, NULL);
 	DRW_shgroup_uniform_texture_ref(vfx_shgrp, "strokeColor", &e_data->temp_color_tx);
 	DRW_shgroup_uniform_texture_ref(vfx_shgrp, "strokeDepth", &e_data->temp_depth_tx);
-	DRW_shgroup_uniform_int(vfx_shgrp, "tonemapping", &stl->storage->tonemapping, 1);
 
 	cache->vfx_wave_sh = vfx_shgrp;
 }
diff --git a/source/blender/draw/engines/gpencil/shaders/gpencil_front_depth_mix_frag.glsl b/source/blender/draw/engines/gpencil/shaders/gpencil_simple_mix_frag.glsl
similarity index 64%
rename from source/blender/draw/engines/gpencil/shaders/gpencil_front_depth_mix_frag.glsl
rename to source/blender/draw/engines/gpencil/shaders/gpencil_simple_mix_frag.glsl
index caf49b9e450..dd54e38c3d0 100644
--- a/source/blender/draw/engines/gpencil/shaders/gpencil_front_depth_mix_frag.glsl
+++ b/source/blender/draw/engines/gpencil/shaders/gpencil_simple_mix_frag.glsl
@@ -3,12 +3,13 @@ in vec4 uvcoordsvar;
 out vec4 FragColor;
 
 uniform sampler2D strokeColor;
-
+uniform sampler2D strokeDepth;
 void main()
 {
 	ivec2 uv = ivec2(gl_FragCoord.xy);
+	float stroke_depth = texelFetch(strokeDepth, uv, 0).r;
 	vec4 stroke_color =  texelFetch(strokeColor, uv, 0).rgba;
 
 	FragColor = stroke_color;
-	gl_FragDepth = 0.0; /* always on front */
+	gl_FragDepth = stroke_depth;
 }
diff --git a/source/blender/draw/engines/gpencil/shaders/gpenc

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list