[Bf-blender-cvs] [24ceb20f545] greasepencil-object: WIP: Basic shader for gaussian blur

Antonio Vazquez noreply at git.blender.org
Tue Aug 8 11:19:05 CEST 2017


Commit: 24ceb20f545138137324f841370c979ac2727832
Author: Antonio Vazquez
Date:   Sat Aug 5 17:04:57 2017 +0200
Branches: greasepencil-object
https://developer.blender.org/rB24ceb20f545138137324f841370c979ac2727832

WIP: Basic shader for gaussian blur

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

M	source/blender/draw/CMakeLists.txt
M	source/blender/draw/engines/gpencil/gpencil_draw_cache_impl.c
M	source/blender/draw/engines/gpencil/gpencil_engine.c
M	source/blender/draw/engines/gpencil/gpencil_engine.h
A	source/blender/draw/engines/gpencil/shaders/gpencil_gaussian_blur_frag.glsl

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

diff --git a/source/blender/draw/CMakeLists.txt b/source/blender/draw/CMakeLists.txt
index b1ec5e91504..d27ac5b7947 100644
--- a/source/blender/draw/CMakeLists.txt
+++ b/source/blender/draw/CMakeLists.txt
@@ -211,6 +211,7 @@ data_to_c_simple(engines/gpencil/shaders/gpencil_zdepth_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)
+data_to_c_simple(engines/gpencil/shaders/gpencil_gaussian_blur_frag.glsl SRC)
 
 list(APPEND INC
 )
diff --git a/source/blender/draw/engines/gpencil/gpencil_draw_cache_impl.c b/source/blender/draw/engines/gpencil/gpencil_draw_cache_impl.c
index 71478b1b947..9b2aa0ed935 100644
--- a/source/blender/draw/engines/gpencil/gpencil_draw_cache_impl.c
+++ b/source/blender/draw/engines/gpencil/gpencil_draw_cache_impl.c
@@ -79,6 +79,8 @@ void gpencil_object_cache_add(tGPencilObjectCache *cache, Object *ob, int *gp_ca
 	cache[*gp_cache_used].ob = ob;
 	cache[*gp_cache_used].init_grp = 0;
 	cache[*gp_cache_used].end_grp = -1;
+	cache[*gp_cache_used].init_vfx_sh = NULL;
+	cache[*gp_cache_used].end_vfx_sh = NULL;
 
 	/* calculate zdepth from point of view */
 	float zdepth = 0.0;
diff --git a/source/blender/draw/engines/gpencil/gpencil_engine.c b/source/blender/draw/engines/gpencil/gpencil_engine.c
index 7557d5da1dd..a3589f2b867 100644
--- a/source/blender/draw/engines/gpencil/gpencil_engine.c
+++ b/source/blender/draw/engines/gpencil/gpencil_engine.c
@@ -45,6 +45,7 @@ extern char datatoc_gpencil_zdepth_mix_frag_glsl[];
 extern char datatoc_gpencil_point_vert_glsl[];
 extern char datatoc_gpencil_point_geom_glsl[];
 extern char datatoc_gpencil_point_frag_glsl[];
+extern char datatoc_gpencil_gaussian_blur_frag_glsl[];
 
 /* *********** STATIC *********** */
 static GPENCIL_e_data e_data = {NULL}; /* Engine data */
@@ -109,6 +110,8 @@ static void GPENCIL_engine_init(void *vedata)
 	}
 
 	unit_m4(stl->storage->unit_matrix);
+	ARRAY_SET_ITEMS(stl->storage->blur1, 1.0f, 0.0f); /* horz */
+	ARRAY_SET_ITEMS(stl->storage->blur2, 0.0f, 1.0f); /* vert */
 
 	/* blank texture used if no texture defined for fill shader */
 	if (!e_data.gpencil_blank_texture) {
@@ -124,6 +127,8 @@ static void GPENCIL_engine_free(void)
 	DRW_SHADER_FREE_SAFE(e_data.gpencil_stroke_sh);
 	DRW_SHADER_FREE_SAFE(e_data.gpencil_point_sh);
 	DRW_SHADER_FREE_SAFE(e_data.gpencil_fullscreen_sh);
+	DRW_SHADER_FREE_SAFE(e_data.gpencil_vfx_blur_sh);
+
 	DRW_TEXTURE_FREE_SAFE(e_data.gpencil_blank_texture);
 }
 
@@ -160,6 +165,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_vfx_blur_sh) {
+		e_data.gpencil_vfx_blur_sh = DRW_shader_create_fullscreen(datatoc_gpencil_gaussian_blur_frag_glsl, NULL);
+	}
 
 	{
 		/* Stroke pass */
@@ -211,15 +219,20 @@ static void GPENCIL_cache_init(void *vedata)
 		DRW_shgroup_call_add(mix_shgrp, quad, NULL);
 		DRW_shgroup_uniform_buffer(mix_shgrp, "strokeColor", &e_data.temp_fbcolor_color_tx);
 		DRW_shgroup_uniform_buffer(mix_shgrp, "strokeDepth", &e_data.temp_fbcolor_depth_tx);
+
+		/* VFX pass */
+		psl->vfx_pass = DRW_pass_create("GPencil VFX Pass", DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS);
 	}
 }
 
 static void GPENCIL_cache_populate(void *vedata, Object *ob)
 {
 	GPENCIL_StorageList *stl = ((GPENCIL_Data *)vedata)->stl;
+	GPENCIL_PassList *psl = ((GPENCIL_Data *)vedata)->psl;
 	const DRWContextState *draw_ctx = DRW_context_state_get();
 	Scene *scene = draw_ctx->scene;
 	ToolSettings *ts = scene->toolsettings;
+	tGPencilObjectCache *cache;
 
 	/* object datablock (this is not draw now) */
 	if (ob->type == OB_GPENCIL && ob->gpd) {
@@ -234,6 +247,26 @@ static void GPENCIL_cache_populate(void *vedata, Object *ob)
 		gpencil_array_modifiers(stl, ob);
 		/* draw current painting strokes */
 		DRW_gpencil_populate_buffer_strokes(vedata, ts, ob->gpd);
+
+		/* VFX pass */
+		struct Gwn_Batch *vfxquad = DRW_cache_fullscreen_quad_get();
+		cache = &stl->g_data->gp_object_cache[stl->g_data->gp_cache_used - 1];
+
+		/* horizontal blur */
+		DRWShadingGroup *vfx_shgrp = DRW_shgroup_create(e_data.gpencil_vfx_blur_sh, psl->vfx_pass);
+		DRW_shgroup_call_add(vfx_shgrp, vfxquad, NULL);
+		DRW_shgroup_uniform_buffer(vfx_shgrp, "strokeColor", &e_data.temp_fbcolor_color_tx);
+		DRW_shgroup_uniform_buffer(vfx_shgrp, "strokeDepth", &e_data.temp_fbcolor_depth_tx);
+		DRW_shgroup_uniform_vec2(vfx_shgrp, "dir", stl->storage->blur1, 1);
+		cache->init_vfx_sh = vfx_shgrp;
+
+		/* vertical blur */
+		vfx_shgrp = DRW_shgroup_create(e_data.gpencil_vfx_blur_sh, psl->vfx_pass);
+		DRW_shgroup_call_add(vfx_shgrp, vfxquad, NULL);
+		DRW_shgroup_uniform_buffer(vfx_shgrp, "strokeColor", &e_data.temp_fbcolor_color_tx);
+		DRW_shgroup_uniform_buffer(vfx_shgrp, "strokeDepth", &e_data.temp_fbcolor_depth_tx);
+		DRW_shgroup_uniform_vec2(vfx_shgrp, "dir", stl->storage->blur2, 1);
+		cache->end_vfx_sh = vfx_shgrp;
 	}
 }
 
@@ -284,6 +317,7 @@ static void GPENCIL_draw_scene(void *vedata)
 	DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
 	float clearcol[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
 	int init_grp, end_grp;
+	tGPencilObjectCache *cache;
 
 	/* attach temp textures */
 	DRW_framebuffer_texture_attach(fbl->temp_color_fb, e_data.temp_fbcolor_depth_tx, 0, 0);
@@ -297,9 +331,10 @@ static void GPENCIL_draw_scene(void *vedata)
 			sizeof(tGPencilObjectCache), gpencil_object_cache_compare_zdepth);
 
 		for (int i = 0; i < stl->g_data->gp_cache_used; ++i) {
-			Object *ob = stl->g_data->gp_object_cache[i].ob;
-			init_grp = stl->g_data->gp_object_cache[i].init_grp;
-			end_grp = stl->g_data->gp_object_cache[i].end_grp;
+			cache = &stl->g_data->gp_object_cache[i];
+			Object *ob = cache->ob;
+			init_grp = cache->init_grp;
+			end_grp = cache->end_grp;
 			/* Render stroke in separated framebuffer */
 			DRW_framebuffer_bind(fbl->temp_color_fb);
 			DRW_framebuffer_clear(true, true, false, clearcol, 1.0f);
@@ -321,6 +356,12 @@ static void GPENCIL_draw_scene(void *vedata)
 				DRW_draw_pass(psl->drawing_pass);
 			}
 
+			/* vfx pass */
+			if ((cache->init_vfx_sh) && (cache->init_vfx_sh)) {
+				DRW_draw_pass_subset(psl->vfx_pass,
+					cache->init_vfx_sh,
+					cache->end_vfx_sh);
+			}
 			/* Combine with scene buffer */
 			DRW_framebuffer_bind(dfbl->default_fb);
 
diff --git a/source/blender/draw/engines/gpencil/gpencil_engine.h b/source/blender/draw/engines/gpencil/gpencil_engine.h
index ff09c6edcff..a5dd73f2f14 100644
--- a/source/blender/draw/engines/gpencil/gpencil_engine.h
+++ b/source/blender/draw/engines/gpencil/gpencil_engine.h
@@ -46,6 +46,8 @@ struct GPENCIL_StorageList;
 typedef struct tGPencilObjectCache {
 	struct Object *ob;
 	int init_grp, end_grp;
+	DRWShadingGroup *init_vfx_sh;
+	DRWShadingGroup *end_vfx_sh;
 	float zdepth;
 } tGPencilObjectCache;
 
@@ -72,6 +74,7 @@ typedef struct GPENCIL_Storage {
 	int xray;
 	int keep_size;
 	float obj_scale;
+	float blur1[2], blur2[2];
 } GPENCIL_Storage;
 
 typedef struct GPENCIL_StorageList {
@@ -85,6 +88,7 @@ typedef struct GPENCIL_PassList {
 	struct DRWPass *edit_pass;
 	struct DRWPass *drawing_pass;
 	struct DRWPass *mix_pass;
+	struct DRWPass *vfx_pass;
 } GPENCIL_PassList;
 
 typedef struct GPENCIL_FramebufferList {
@@ -128,6 +132,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_vfx_blur_sh;
 	/* temp depth texture */
 	struct GPUTexture *temp_fbcolor_depth_tx;
 	struct GPUTexture *temp_fbcolor_color_tx;
diff --git a/source/blender/draw/engines/gpencil/shaders/gpencil_gaussian_blur_frag.glsl b/source/blender/draw/engines/gpencil/shaders/gpencil_gaussian_blur_frag.glsl
new file mode 100644
index 00000000000..f65a04ddd4a
--- /dev/null
+++ b/source/blender/draw/engines/gpencil/shaders/gpencil_gaussian_blur_frag.glsl
@@ -0,0 +1,36 @@
+
+out vec4 FragColor;
+
+uniform sampler2D strokeColor;
+uniform sampler2D strokeDepth;
+
+/* next 2 fields must be uniforms, now, valuer are only for testing */
+float resolution = 100.0;
+float radius = 300.0;
+uniform vec2 dir;
+
+void main()
+{
+	ivec2 uv = ivec2(gl_FragCoord.xy);
+	float stroke_depth = texelFetch(strokeDepth, uv, 0).r;
+	gl_FragDepth = stroke_depth;
+
+	float blur = radius/resolution; 
+	float hstep = dir.x;
+    float vstep = dir.y;
+	vec4 outcolor = vec4(0.0);
+	/* apply blurring, using a 9-tap filter with predefined gaussian weights (base on code written by Matt DesLauriers)*/
+    outcolor += texelFetch(strokeColor, ivec2(uv.x - 4.0 * blur * hstep, uv.y - 4.0 * blur * vstep), 0) * 0.0162162162;
+    outcolor += texelFetch(strokeColor, ivec2(uv.x - 3.0 * blur * hstep, uv.y - 3.0 * blur * vstep), 0) * 0.0540540541;
+    outcolor += texelFetch(strokeColor, ivec2(uv.x - 2.0 * blur * hstep, uv.y - 2.0 * blur * vstep), 0) * 0.1216216216;
+    outcolor += texelFetch(strokeColor, ivec2(uv.x - 1.0 * blur * hstep, uv.y - 1.0 * blur * vstep), 0) * 0.1945945946;
+
+    outcolor += texelFetch(strokeColor, ivec2(uv.x, uv.y), 0) * 0.2270270270;
+
+    outcolor += texelFetch(strokeColor, ivec2(uv.x + 1.0 * blur * hstep, uv.y + 1.0 * blur * vstep), 0) * 0.1945945946;
+    outcolor += texelFetch(strokeColor, ivec2(uv.x + 2.0 * blur * hstep, uv.y + 2.0 * blur * vstep), 0) * 0.1216216216;
+    outcolor += texelFetch(strokeColor, ivec2(uv.x + 3.0 * blur * hstep, uv.y + 3.0 * blur * vstep), 0) * 0.0540540541;
+    outcolor += texelFetch(strokeColor, ivec2(uv.x + 4.0 * blur * hstep, uv.y + 4.0 * blur * vstep), 0) * 0.0162162162;
+
+	FragColor = outcolor;
+}




More information about the Bf-blender-cvs mailing list