fcadf29625306d8e91b4c7ad3e030f93e2cab607
[blender.git] / source / blender / draw / engines / gpencil / gpencil_shader_fx.c
1 /*
2  * Copyright 2017, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Antonio Vazquez
19  *
20  */
21
22 /** \file blender/draw/engines/gpencil/gpencil_shader_fx.c
23  *  \ingroup draw
24  */
25 #include "DNA_gpencil_types.h"
26 #include "DNA_shader_fx_types.h"
27 #include "DNA_screen_types.h"
28 #include "DNA_view3d_types.h"
29 #include "DNA_camera_types.h"
30
31 #include "BKE_gpencil.h"
32 #include "BKE_shader_fx.h"
33
34 #include "DRW_engine.h"
35 #include "DRW_render.h"
36
37 #include "BKE_camera.h"
38
39 #include "ED_view3d.h"
40 #include "ED_gpencil.h"
41
42 #include "gpencil_engine.h"
43
44 extern char datatoc_gpencil_fx_blur_frag_glsl[];
45 extern char datatoc_gpencil_fx_colorize_frag_glsl[];
46 extern char datatoc_gpencil_fx_flip_frag_glsl[];
47 extern char datatoc_gpencil_fx_light_frag_glsl[];
48 extern char datatoc_gpencil_fx_pixel_frag_glsl[];
49 extern char datatoc_gpencil_fx_rim_prepare_frag_glsl[];
50 extern char datatoc_gpencil_fx_rim_resolve_frag_glsl[];
51 extern char datatoc_gpencil_fx_shadow_prepare_frag_glsl[];
52 extern char datatoc_gpencil_fx_shadow_resolve_frag_glsl[];
53 extern char datatoc_gpencil_fx_glow_prepare_frag_glsl[];
54 extern char datatoc_gpencil_fx_glow_resolve_frag_glsl[];
55 extern char datatoc_gpencil_fx_swirl_frag_glsl[];
56 extern char datatoc_gpencil_fx_wave_frag_glsl[];
57
58 /* verify if this fx is active */
59 static bool effect_is_active(bGPdata *gpd, ShaderFxData *fx, bool is_render)
60 {
61         if (fx == NULL) {
62                 return false;
63         }
64
65         if (gpd == NULL) {
66                 return false;
67         }
68
69         bool is_edit = GPENCIL_ANY_EDIT_MODE(gpd);
70         if (((fx->mode & eShaderFxMode_Editmode) == 0) && (is_edit)) {
71                 return false;
72         }
73
74         if (((fx->mode & eShaderFxMode_Realtime) && (is_render == false)) ||
75             ((fx->mode & eShaderFxMode_Render) && (is_render == true)))
76         {
77                 return true;
78         }
79
80         return false;
81 }
82
83 /**
84  * Get normal of draw using one stroke of visible layer
85  * \param gpd        GP datablock
86  * \param r_point    Point on plane
87  * \param r_normal   Normal vector
88  */
89 static bool get_normal_vector(bGPdata *gpd, float r_point[3], float r_normal[3])
90 {
91         for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) {
92                 if (gpl->flag & GP_LAYER_HIDE)
93                         continue;
94
95                 /* get frame  */
96                 bGPDframe *gpf = gpl->actframe;
97                 if (gpf == NULL)
98                         continue;
99                 for (bGPDstroke *gps = gpf->strokes.first; gps; gps = gps->next) {
100                         if (gps->totpoints >= 3) {
101                                 bGPDspoint *pt = &gps->points[0];
102                                 BKE_gpencil_stroke_normal(gps, r_normal);
103                                 /* in some weird situations, the normal cannot be calculated, so try next stroke */
104                                 if ((r_normal[0] != 0.0f) || (r_normal[1] != 0.0f) || (r_normal[2] != 0.0f)) {
105                                         copy_v3_v3(r_point, &pt->x);
106                                         return true;
107                                 }
108                         }
109                 }
110         }
111
112         return false;
113 }
114
115 /* helper to get near and far depth of field values */
116 static void GPENCIL_dof_nearfar(Object *camera, float coc, float nearfar[2])
117 {
118         if (camera == NULL) {
119                 return;
120         }
121
122         const DRWContextState *draw_ctx = DRW_context_state_get();
123         Scene *scene = draw_ctx->scene;
124         Camera *cam = (Camera *)camera->data;
125
126         float fstop = cam->gpu_dof.fstop;
127         float focus_dist = BKE_camera_object_dof_distance(camera);
128         float focal_len = cam->lens;
129
130         /* this is factor that converts to the scene scale. focal length and sensor are expressed in mm
131          * unit.scale_length is how many meters per blender unit we have. We want to convert to blender units though
132          * because the shader reads coordinates in world space, which is in blender units.
133          * Note however that focus_distance is already in blender units and shall not be scaled here (see T48157). */
134         float scale = (scene->unit.system) ? scene->unit.scale_length : 1.0f;
135         float scale_camera = 0.001f / scale;
136         /* we want radius here for the aperture number  */
137         float aperture_scaled = 0.5f * scale_camera * focal_len / fstop;
138         float focal_len_scaled = scale_camera * focal_len;
139
140         float hyperfocal = (focal_len_scaled * focal_len_scaled) / (aperture_scaled * coc);
141         nearfar[0] = (hyperfocal * focus_dist) / (hyperfocal + focal_len);
142         nearfar[1] = (hyperfocal * focus_dist) / (hyperfocal - focal_len);
143 }
144
145 /* ****************  Shader Effects ***************************** */
146
147 /* Gaussian Blur FX
148  * The effect is done using two shading groups because is faster to apply horizontal
149  * and vertical in different operations.
150  */
151 static void DRW_gpencil_fx_blur(
152         ShaderFxData *fx, int ob_idx, GPENCIL_e_data *e_data,
153         GPENCIL_Data *vedata, tGPencilObjectCache *cache)
154 {
155         if (fx == NULL) {
156                 return;
157         }
158
159         BlurShaderFxData *fxd = (BlurShaderFxData *)fx;
160
161         GPENCIL_StorageList *stl = ((GPENCIL_Data *)vedata)->stl;
162         GPENCIL_PassList *psl = ((GPENCIL_Data *)vedata)->psl;
163         const DRWContextState *draw_ctx = DRW_context_state_get();
164         View3D *v3d = draw_ctx->v3d;
165         RegionView3D *rv3d = draw_ctx->rv3d;
166         DRWShadingGroup *fx_shgrp;
167
168         fxd->blur[0] = fxd->radius[0];
169         fxd->blur[1] = fxd->radius[1];
170
171         /* init weight */
172         if (fxd->flag & FX_BLUR_DOF_MODE) {
173                 /* viewport and opengl render */
174                 Object *camera = NULL;
175                 if (rv3d) {
176                         if (rv3d->persp == RV3D_CAMOB) {
177                                 camera = v3d->camera;
178                         }
179                 }
180                 else {
181                         camera = stl->storage->camera;
182                 }
183
184                 if (camera) {
185                         float nearfar[2];
186                         GPENCIL_dof_nearfar(camera, fxd->coc, nearfar);
187                         float zdepth = stl->g_data->gp_object_cache[ob_idx].zdepth;
188                         /* the object is on focus area */
189                         if ((zdepth >= nearfar[0]) && (zdepth <= nearfar[1])) {
190                                 fxd->blur[0] = 0;
191                                 fxd->blur[1] = 0;
192                         }
193                         else {
194                                 float f;
195                                 if (zdepth < nearfar[0]) {
196                                         f = nearfar[0] - zdepth;
197                                 }
198                                 else {
199                                         f = zdepth - nearfar[1];
200                                 }
201                                 fxd->blur[0] = f;
202                                 fxd->blur[1] = f;
203                                 CLAMP2(&fxd->blur[0], 0, fxd->radius[0]);
204                         }
205                 }
206                 else {
207                         /* if not camera view, the blur is disabled */
208                         fxd->blur[0] = 0;
209                         fxd->blur[1] = 0;
210                 }
211         }
212
213         GPUBatch *fxquad = DRW_cache_fullscreen_quad_get();
214
215         fx_shgrp = DRW_shgroup_create(
216                 e_data->gpencil_fx_blur_sh,
217                 psl->fx_shader_pass_blend);
218         DRW_shgroup_call_add(fx_shgrp, fxquad, NULL);
219         DRW_shgroup_uniform_texture_ref(fx_shgrp, "strokeColor", &e_data->temp_color_tx_a);
220         DRW_shgroup_uniform_texture_ref(fx_shgrp, "strokeDepth", &e_data->temp_depth_tx_a);
221         DRW_shgroup_uniform_vec2(fx_shgrp, "Viewport", DRW_viewport_size_get(), 1);
222         DRW_shgroup_uniform_int(fx_shgrp, "blur", &fxd->blur[0], 2);
223
224         DRW_shgroup_uniform_vec3(fx_shgrp, "loc", &cache->loc[0], 1);
225         DRW_shgroup_uniform_float(fx_shgrp, "pixsize", stl->storage->pixsize, 1);
226         DRW_shgroup_uniform_float(fx_shgrp, "pixfactor", &cache->pixfactor, 1);
227
228         fxd->runtime.fx_sh = fx_shgrp;
229 }
230
231 /* Colorize FX */
232 static void DRW_gpencil_fx_colorize(
233         ShaderFxData *fx, GPENCIL_e_data *e_data, GPENCIL_Data *vedata)
234 {
235         if (fx == NULL) {
236                 return;
237         }
238         ColorizeShaderFxData *fxd = (ColorizeShaderFxData *)fx;
239         GPENCIL_PassList *psl = ((GPENCIL_Data *)vedata)->psl;
240         DRWShadingGroup *fx_shgrp;
241
242         GPUBatch *fxquad = DRW_cache_fullscreen_quad_get();
243         fx_shgrp = DRW_shgroup_create(e_data->gpencil_fx_colorize_sh, psl->fx_shader_pass);
244         DRW_shgroup_call_add(fx_shgrp, fxquad, NULL);
245         DRW_shgroup_uniform_texture_ref(fx_shgrp, "strokeColor", &e_data->temp_color_tx_a);
246         DRW_shgroup_uniform_texture_ref(fx_shgrp, "strokeDepth", &e_data->temp_depth_tx_a);
247         DRW_shgroup_uniform_vec4(fx_shgrp, "low_color", &fxd->low_color[0], 1);
248         DRW_shgroup_uniform_vec4(fx_shgrp, "high_color", &fxd->high_color[0], 1);
249         DRW_shgroup_uniform_int(fx_shgrp, "mode", &fxd->mode, 1);
250         DRW_shgroup_uniform_float(fx_shgrp, "factor", &fxd->factor, 1);
251
252         fxd->runtime.fx_sh = fx_shgrp;
253 }
254
255 /* Flip FX */
256 static void DRW_gpencil_fx_flip(
257         ShaderFxData *fx, GPENCIL_e_data *e_data, GPENCIL_Data *vedata)
258 {
259         if (fx == NULL) {
260                 return;
261         }
262         FlipShaderFxData *fxd = (FlipShaderFxData *)fx;
263         GPENCIL_PassList *psl = ((GPENCIL_Data *)vedata)->psl;
264         DRWShadingGroup *fx_shgrp;
265
266         fxd->flipmode = 100;
267         /* the number works as bit flag */
268         if (fxd->flag & FX_FLIP_HORIZONTAL) {
269                 fxd->flipmode += 10;
270         }
271         if (fxd->flag & FX_FLIP_VERTICAL) {
272                 fxd->flipmode += 1;
273         }
274
275         GPUBatch *fxquad = DRW_cache_fullscreen_quad_get();
276         fx_shgrp = DRW_shgroup_create(e_data->gpencil_fx_flip_sh, psl->fx_shader_pass);
277         DRW_shgroup_call_add(fx_shgrp, fxquad, NULL);
278         DRW_shgroup_uniform_texture_ref(fx_shgrp, "strokeColor", &e_data->temp_color_tx_a);
279         DRW_shgroup_uniform_texture_ref(fx_shgrp, "strokeDepth", &e_data->temp_depth_tx_a);
280         DRW_shgroup_uniform_int(fx_shgrp, "flipmode", &fxd->flipmode, 1);
281
282         DRW_shgroup_uniform_vec2(fx_shgrp, "wsize", DRW_viewport_size_get(), 1);
283
284         fxd->runtime.fx_sh = fx_shgrp;
285 }
286
287 /* Light FX */
288 static void DRW_gpencil_fx_light(
289         ShaderFxData *fx, GPENCIL_e_data *e_data, GPENCIL_Data *vedata,
290         tGPencilObjectCache *cache)
291 {
292         if (fx == NULL) {
293                 return;
294         }
295         LightShaderFxData *fxd = (LightShaderFxData *)fx;
296
297         if (fxd->object == NULL) {
298                 return;
299         }
300         GPENCIL_StorageList *stl = ((GPENCIL_Data *)vedata)->stl;
301         GPENCIL_PassList *psl = ((GPENCIL_Data *)vedata)->psl;
302         DRWShadingGroup *fx_shgrp;
303
304         GPUBatch *fxquad = DRW_cache_fullscreen_quad_get();
305         fx_shgrp = DRW_shgroup_create(e_data->gpencil_fx_light_sh, psl->fx_shader_pass);
306         DRW_shgroup_call_add(fx_shgrp, fxquad, NULL);
307         DRW_shgroup_uniform_texture_ref(fx_shgrp, "strokeColor", &e_data->temp_color_tx_a);
308         DRW_shgroup_uniform_texture_ref(fx_shgrp, "strokeDepth", &e_data->temp_depth_tx_a);
309
310         DRW_shgroup_uniform_vec2(fx_shgrp, "Viewport", DRW_viewport_size_get(), 1);
311
312         /* location of the light using obj location as origin */
313         copy_v3_v3(fxd->loc, &fxd->object->loc[0]);
314
315         /* Calc distance to strokes plane
316          * The w component of location is used to transfer the distance to drawing plane
317          */
318         float r_point[3], r_normal[3];
319         float r_plane[4];
320         bGPdata *gpd = cache->gpd;
321         if (!get_normal_vector(gpd, r_point, r_normal)) {
322                 return;
323         }
324         mul_mat3_m4_v3(cache->obmat, r_normal); /* only rotation component */
325         plane_from_point_normal_v3(r_plane, r_point, r_normal);
326         float dt = dist_to_plane_v3(fxd->object->loc, r_plane);
327         fxd->loc[3] = dt; /* use last element to save it */
328
329         DRW_shgroup_uniform_vec4(fx_shgrp, "loc", &fxd->loc[0], 1);
330
331         DRW_shgroup_uniform_float(fx_shgrp, "energy", &fxd->energy, 1);
332         DRW_shgroup_uniform_float(fx_shgrp, "ambient", &fxd->ambient, 1);
333
334         DRW_shgroup_uniform_float(fx_shgrp, "pixsize", stl->storage->pixsize, 1);
335         DRW_shgroup_uniform_float(fx_shgrp, "pixfactor", &cache->pixfactor, 1);
336
337         fxd->runtime.fx_sh = fx_shgrp;
338 }
339
340 /* Pixelate FX */
341 static void DRW_gpencil_fx_pixel(
342         ShaderFxData *fx, GPENCIL_e_data *e_data, GPENCIL_Data *vedata,
343         tGPencilObjectCache *cache)
344 {
345         if (fx == NULL) {
346                 return;
347         }
348         PixelShaderFxData *fxd = (PixelShaderFxData *)fx;
349
350         GPENCIL_StorageList *stl = ((GPENCIL_Data *)vedata)->stl;
351         GPENCIL_PassList *psl = ((GPENCIL_Data *)vedata)->psl;
352         DRWShadingGroup *fx_shgrp;
353         bGPdata *gpd = cache->gpd;
354
355         fxd->size[2] = (int)fxd->flag & FX_PIXEL_USE_LINES;
356
357         GPUBatch *fxquad = DRW_cache_fullscreen_quad_get();
358         fx_shgrp = DRW_shgroup_create(e_data->gpencil_fx_pixel_sh, psl->fx_shader_pass);
359         DRW_shgroup_call_add(fx_shgrp, fxquad, NULL);
360         DRW_shgroup_uniform_texture_ref(fx_shgrp, "strokeColor", &e_data->temp_color_tx_a);
361         DRW_shgroup_uniform_texture_ref(fx_shgrp, "strokeDepth", &e_data->temp_depth_tx_a);
362         DRW_shgroup_uniform_int(fx_shgrp, "size", &fxd->size[0], 3);
363         DRW_shgroup_uniform_vec4(fx_shgrp, "color", &fxd->rgba[0], 1);
364
365         DRW_shgroup_uniform_vec3(fx_shgrp, "loc", &cache->loc[0], 1);
366         DRW_shgroup_uniform_float(fx_shgrp, "pixsize", stl->storage->pixsize, 1);
367         DRW_shgroup_uniform_float(fx_shgrp, "pixfactor", &gpd->pixfactor, 1);
368
369         fxd->runtime.fx_sh = fx_shgrp;
370 }
371
372 /* Rim FX */
373 static void DRW_gpencil_fx_rim(
374         ShaderFxData *fx, GPENCIL_e_data *e_data, GPENCIL_Data *vedata,
375         tGPencilObjectCache *cache)
376 {
377         if (fx == NULL) {
378                 return;
379         }
380         RimShaderFxData *fxd = (RimShaderFxData *)fx;
381
382         GPENCIL_StorageList *stl = ((GPENCIL_Data *)vedata)->stl;
383         GPENCIL_PassList *psl = ((GPENCIL_Data *)vedata)->psl;
384         DRWShadingGroup *fx_shgrp;
385
386         GPUBatch *fxquad = DRW_cache_fullscreen_quad_get();
387         /* prepare pass */
388         fx_shgrp = DRW_shgroup_create(
389                 e_data->gpencil_fx_rim_prepare_sh,
390                 psl->fx_shader_pass_blend);
391         DRW_shgroup_call_add(fx_shgrp, fxquad, NULL);
392         DRW_shgroup_uniform_texture_ref(fx_shgrp, "strokeColor", &e_data->temp_color_tx_a);
393         DRW_shgroup_uniform_texture_ref(fx_shgrp, "strokeDepth", &e_data->temp_depth_tx_a);
394         DRW_shgroup_uniform_vec2(fx_shgrp, "Viewport", DRW_viewport_size_get(), 1);
395
396         DRW_shgroup_uniform_int(fx_shgrp, "offset", &fxd->offset[0], 2);
397         DRW_shgroup_uniform_vec3(fx_shgrp, "rim_color", &fxd->rim_rgb[0], 1);
398         DRW_shgroup_uniform_vec3(fx_shgrp, "mask_color", &fxd->mask_rgb[0], 1);
399
400         DRW_shgroup_uniform_vec3(fx_shgrp, "loc", &cache->loc[0], 1);
401         DRW_shgroup_uniform_float(fx_shgrp, "pixsize", stl->storage->pixsize, 1);
402         DRW_shgroup_uniform_float(fx_shgrp, "pixfactor", &cache->pixfactor, 1);
403
404         fxd->runtime.fx_sh = fx_shgrp;
405
406         /* blur pass */
407         fx_shgrp = DRW_shgroup_create(
408                 e_data->gpencil_fx_blur_sh,
409                 psl->fx_shader_pass_blend);
410         DRW_shgroup_call_add(fx_shgrp, fxquad, NULL);
411         DRW_shgroup_uniform_texture_ref(fx_shgrp, "strokeColor", &e_data->temp_color_tx_fx);
412         DRW_shgroup_uniform_texture_ref(fx_shgrp, "strokeDepth", &e_data->temp_depth_tx_fx);
413         DRW_shgroup_uniform_vec2(fx_shgrp, "Viewport", DRW_viewport_size_get(), 1);
414         DRW_shgroup_uniform_int(fx_shgrp, "blur", &fxd->blur[0], 2);
415
416         DRW_shgroup_uniform_vec3(fx_shgrp, "loc", &cache->loc[0], 1);
417         DRW_shgroup_uniform_float(fx_shgrp, "pixsize", stl->storage->pixsize, 1);
418         DRW_shgroup_uniform_float(fx_shgrp, "pixfactor", &cache->pixfactor, 1);
419
420         fxd->runtime.fx_sh_b = fx_shgrp;
421
422         /* resolve pass */
423         fx_shgrp = DRW_shgroup_create(
424                 e_data->gpencil_fx_rim_resolve_sh,
425                 psl->fx_shader_pass_blend);
426         DRW_shgroup_call_add(fx_shgrp, fxquad, NULL);
427         DRW_shgroup_uniform_texture_ref(fx_shgrp, "strokeColor", &e_data->temp_color_tx_a);
428         DRW_shgroup_uniform_texture_ref(fx_shgrp, "strokeDepth", &e_data->temp_depth_tx_a);
429         DRW_shgroup_uniform_texture_ref(fx_shgrp, "strokeRim", &e_data->temp_color_tx_fx);
430         DRW_shgroup_uniform_vec3(fx_shgrp, "mask_color", &fxd->mask_rgb[0], 1);
431         DRW_shgroup_uniform_int(fx_shgrp, "mode", &fxd->mode, 1);
432
433         fxd->runtime.fx_sh_c = fx_shgrp;
434 }
435
436 /* Shadow FX */
437 static void DRW_gpencil_fx_shadow(
438         ShaderFxData *fx, GPENCIL_e_data *e_data, GPENCIL_Data *vedata,
439         tGPencilObjectCache *cache)
440 {
441         if (fx == NULL) {
442                 return;
443         }
444         ShadowShaderFxData *fxd = (ShadowShaderFxData *)fx;
445         if ((!fxd->object) && (fxd->flag & FX_SHADOW_USE_OBJECT)) {
446                 return;
447         }
448
449         GPENCIL_StorageList *stl = ((GPENCIL_Data *)vedata)->stl;
450         GPENCIL_PassList *psl = ((GPENCIL_Data *)vedata)->psl;
451         DRWShadingGroup *fx_shgrp;
452
453         GPUBatch *fxquad = DRW_cache_fullscreen_quad_get();
454         /* prepare pass */
455         fx_shgrp = DRW_shgroup_create(
456                 e_data->gpencil_fx_shadow_prepare_sh,
457                 psl->fx_shader_pass_blend);
458         DRW_shgroup_call_add(fx_shgrp, fxquad, NULL);
459         DRW_shgroup_uniform_texture_ref(fx_shgrp, "strokeColor", &e_data->temp_color_tx_a);
460         DRW_shgroup_uniform_texture_ref(fx_shgrp, "strokeDepth", &e_data->temp_depth_tx_a);
461         DRW_shgroup_uniform_vec2(fx_shgrp, "Viewport", DRW_viewport_size_get(), 1);
462
463         DRW_shgroup_uniform_int(fx_shgrp, "offset", &fxd->offset[0], 2);
464         DRW_shgroup_uniform_float(fx_shgrp, "scale", &fxd->scale[0], 2);
465         DRW_shgroup_uniform_float(fx_shgrp, "rotation", &fxd->rotation, 1);
466         DRW_shgroup_uniform_vec4(fx_shgrp, "shadow_color", &fxd->shadow_rgba[0], 1);
467
468         if ((fxd->object) && (fxd->flag & FX_SHADOW_USE_OBJECT)) {
469                 DRW_shgroup_uniform_vec3(fx_shgrp, "loc", &fxd->object->loc[0], 1);
470         }
471         else {
472                 DRW_shgroup_uniform_vec3(fx_shgrp, "loc", &cache->loc[0], 1);
473         }
474
475         const int nowave = -1;
476         if (fxd->flag & FX_SHADOW_USE_WAVE) {
477                 DRW_shgroup_uniform_int(fx_shgrp, "orientation", &fxd->orientation, 1);
478         }
479         else {
480                 DRW_shgroup_uniform_int(fx_shgrp, "orientation", &nowave, 1);
481         }
482         DRW_shgroup_uniform_float(fx_shgrp, "amplitude", &fxd->amplitude, 1);
483         DRW_shgroup_uniform_float(fx_shgrp, "period", &fxd->period, 1);
484         DRW_shgroup_uniform_float(fx_shgrp, "phase", &fxd->phase, 1);
485
486         DRW_shgroup_uniform_float(fx_shgrp, "pixsize", stl->storage->pixsize, 1);
487         DRW_shgroup_uniform_float(fx_shgrp, "pixfactor", &cache->pixfactor, 1);
488
489         fxd->runtime.fx_sh = fx_shgrp;
490
491         /* blur pass */
492         fx_shgrp = DRW_shgroup_create(
493                 e_data->gpencil_fx_blur_sh,
494                 psl->fx_shader_pass_blend);
495         DRW_shgroup_call_add(fx_shgrp, fxquad, NULL);
496         DRW_shgroup_uniform_texture_ref(fx_shgrp, "strokeColor", &e_data->temp_color_tx_fx);
497         DRW_shgroup_uniform_texture_ref(fx_shgrp, "strokeDepth", &e_data->temp_depth_tx_fx);
498         DRW_shgroup_uniform_vec2(fx_shgrp, "Viewport", DRW_viewport_size_get(), 1);
499         DRW_shgroup_uniform_int(fx_shgrp, "blur", &fxd->blur[0], 2);
500
501         DRW_shgroup_uniform_vec3(fx_shgrp, "loc", &cache->loc[0], 1);
502         DRW_shgroup_uniform_float(fx_shgrp, "pixsize", stl->storage->pixsize, 1);
503         DRW_shgroup_uniform_float(fx_shgrp, "pixfactor", &cache->pixfactor, 1);
504
505         fxd->runtime.fx_sh_b = fx_shgrp;
506
507         /* resolve pass */
508         fx_shgrp = DRW_shgroup_create(
509                 e_data->gpencil_fx_shadow_resolve_sh,
510                 psl->fx_shader_pass_blend);
511         DRW_shgroup_call_add(fx_shgrp, fxquad, NULL);
512         DRW_shgroup_uniform_texture_ref(fx_shgrp, "strokeColor", &e_data->temp_color_tx_a);
513         DRW_shgroup_uniform_texture_ref(fx_shgrp, "strokeDepth", &e_data->temp_depth_tx_a);
514         DRW_shgroup_uniform_texture_ref(fx_shgrp, "shadowColor", &e_data->temp_color_tx_fx);
515         DRW_shgroup_uniform_texture_ref(fx_shgrp, "shadowDepth", &e_data->temp_depth_tx_fx);
516
517         fxd->runtime.fx_sh_c = fx_shgrp;
518 }
519
520 /* Glow FX */
521 static void DRW_gpencil_fx_glow(
522         ShaderFxData *fx, GPENCIL_e_data *e_data, GPENCIL_Data *vedata,
523         tGPencilObjectCache *cache)
524 {
525         if (fx == NULL) {
526                 return;
527         }
528         GlowShaderFxData *fxd = (GlowShaderFxData *)fx;
529
530         GPENCIL_StorageList *stl = ((GPENCIL_Data *)vedata)->stl;
531         GPENCIL_PassList *psl = ((GPENCIL_Data *)vedata)->psl;
532         DRWShadingGroup *fx_shgrp;
533
534         GPUBatch *fxquad = DRW_cache_fullscreen_quad_get();
535         /* prepare pass */
536         fx_shgrp = DRW_shgroup_create(
537                 e_data->gpencil_fx_glow_prepare_sh,
538                 psl->fx_shader_pass_blend);
539         DRW_shgroup_call_add(fx_shgrp, fxquad, NULL);
540         DRW_shgroup_uniform_texture_ref(fx_shgrp, "strokeColor", &e_data->temp_color_tx_a);
541         DRW_shgroup_uniform_texture_ref(fx_shgrp, "strokeDepth", &e_data->temp_depth_tx_a);
542
543         DRW_shgroup_uniform_vec3(fx_shgrp, "glow_color", &fxd->glow_color[0], 1);
544         DRW_shgroup_uniform_vec3(fx_shgrp, "select_color", &fxd->select_color[0], 1);
545         DRW_shgroup_uniform_int(fx_shgrp, "mode", &fxd->mode, 1);
546         DRW_shgroup_uniform_float(fx_shgrp, "threshold", &fxd->threshold, 1);
547
548         fxd->runtime.fx_sh = fx_shgrp;
549
550         /* blur pass */
551         fx_shgrp = DRW_shgroup_create(
552                 e_data->gpencil_fx_blur_sh,
553                 psl->fx_shader_pass_blend);
554         DRW_shgroup_call_add(fx_shgrp, fxquad, NULL);
555         DRW_shgroup_uniform_texture_ref(fx_shgrp, "strokeColor", &e_data->temp_color_tx_fx);
556         DRW_shgroup_uniform_texture_ref(fx_shgrp, "strokeDepth", &e_data->temp_depth_tx_fx);
557         DRW_shgroup_uniform_vec2(fx_shgrp, "Viewport", DRW_viewport_size_get(), 1);
558         DRW_shgroup_uniform_int(fx_shgrp, "blur", &fxd->blur[0], 2);
559
560         DRW_shgroup_uniform_vec3(fx_shgrp, "loc", &cache->loc[0], 1);
561         DRW_shgroup_uniform_float(fx_shgrp, "pixsize", stl->storage->pixsize, 1);
562         DRW_shgroup_uniform_float(fx_shgrp, "pixfactor", &cache->pixfactor, 1);
563
564         fxd->runtime.fx_sh_b = fx_shgrp;
565
566         /* resolve pass */
567         fx_shgrp = DRW_shgroup_create(
568                 e_data->gpencil_fx_glow_resolve_sh,
569                 psl->fx_shader_pass_blend);
570         DRW_shgroup_call_add(fx_shgrp, fxquad, NULL);
571         DRW_shgroup_uniform_texture_ref(fx_shgrp, "strokeColor", &e_data->temp_color_tx_a);
572         DRW_shgroup_uniform_texture_ref(fx_shgrp, "strokeDepth", &e_data->temp_depth_tx_a);
573         DRW_shgroup_uniform_texture_ref(fx_shgrp, "glowColor", &e_data->temp_color_tx_fx);
574         DRW_shgroup_uniform_texture_ref(fx_shgrp, "glowDepth", &e_data->temp_depth_tx_fx);
575
576         /* reuse field */
577         DRW_shgroup_uniform_int(fx_shgrp, "alpha_mode", &fxd->blur[1], 1);
578
579         fxd->runtime.fx_sh_c = fx_shgrp;
580 }
581
582 /* Swirl FX */
583 static void DRW_gpencil_fx_swirl(
584         ShaderFxData *fx, GPENCIL_e_data *e_data, GPENCIL_Data *vedata,
585         tGPencilObjectCache *cache)
586 {
587         if (fx == NULL) {
588                 return;
589         }
590         SwirlShaderFxData *fxd = (SwirlShaderFxData *)fx;
591         if (fxd->object == NULL) {
592                 return;
593         }
594
595         GPENCIL_StorageList *stl = ((GPENCIL_Data *)vedata)->stl;
596         GPENCIL_PassList *psl = ((GPENCIL_Data *)vedata)->psl;
597         DRWShadingGroup *fx_shgrp;
598
599         fxd->transparent = (int)fxd->flag & FX_SWIRL_MAKE_TRANSPARENT;
600
601         GPUBatch *fxquad = DRW_cache_fullscreen_quad_get();
602         fx_shgrp = DRW_shgroup_create(e_data->gpencil_fx_swirl_sh, psl->fx_shader_pass);
603         DRW_shgroup_call_add(fx_shgrp, fxquad, NULL);
604         DRW_shgroup_uniform_texture_ref(fx_shgrp, "strokeColor", &e_data->temp_color_tx_a);
605         DRW_shgroup_uniform_texture_ref(fx_shgrp, "strokeDepth", &e_data->temp_depth_tx_a);
606
607         DRW_shgroup_uniform_vec2(fx_shgrp, "Viewport", DRW_viewport_size_get(), 1);
608
609         DRW_shgroup_uniform_vec3(fx_shgrp, "loc", &fxd->object->loc[0], 1);
610
611         DRW_shgroup_uniform_int(fx_shgrp, "radius", &fxd->radius, 1);
612         DRW_shgroup_uniform_float(fx_shgrp, "angle", &fxd->angle, 1);
613         DRW_shgroup_uniform_int(fx_shgrp, "transparent", &fxd->transparent, 1);
614
615         DRW_shgroup_uniform_float(fx_shgrp, "pixsize", stl->storage->pixsize, 1);
616         DRW_shgroup_uniform_float(fx_shgrp, "pixfactor", &cache->pixfactor, 1);
617
618         fxd->runtime.fx_sh = fx_shgrp;
619 }
620
621 /* Wave Distorsion FX */
622 static void DRW_gpencil_fx_wave(
623         ShaderFxData *fx, GPENCIL_e_data *e_data, GPENCIL_Data *vedata)
624 {
625         if (fx == NULL) {
626                 return;
627         }
628
629         WaveShaderFxData *fxd = (WaveShaderFxData *)fx;
630
631         GPENCIL_PassList *psl = ((GPENCIL_Data *)vedata)->psl;
632         GPUBatch *fxquad = DRW_cache_fullscreen_quad_get();
633
634         DRWShadingGroup *fx_shgrp = DRW_shgroup_create(e_data->gpencil_fx_wave_sh, psl->fx_shader_pass);
635         DRW_shgroup_call_add(fx_shgrp, fxquad, NULL);
636         DRW_shgroup_uniform_texture_ref(fx_shgrp, "strokeColor", &e_data->temp_color_tx_a);
637         DRW_shgroup_uniform_texture_ref(fx_shgrp, "strokeDepth", &e_data->temp_depth_tx_a);
638         DRW_shgroup_uniform_float(fx_shgrp, "amplitude", &fxd->amplitude, 1);
639         DRW_shgroup_uniform_float(fx_shgrp, "period", &fxd->period, 1);
640         DRW_shgroup_uniform_float(fx_shgrp, "phase", &fxd->phase, 1);
641         DRW_shgroup_uniform_int(fx_shgrp, "orientation", &fxd->orientation, 1);
642         DRW_shgroup_uniform_vec2(fx_shgrp, "wsize", DRW_viewport_size_get(), 1);
643
644         fxd->runtime.fx_sh = fx_shgrp;
645 }
646
647 /* ************************************************************** */
648
649 /* create all FX shaders */
650 void GPENCIL_create_fx_shaders(GPENCIL_e_data *e_data)
651 {
652         /* fx shaders (all in screen space) */
653         if (!e_data->gpencil_fx_blur_sh) {
654                 e_data->gpencil_fx_blur_sh = DRW_shader_create_fullscreen(
655                         datatoc_gpencil_fx_blur_frag_glsl, NULL);
656         }
657         if (!e_data->gpencil_fx_colorize_sh) {
658                 e_data->gpencil_fx_colorize_sh = DRW_shader_create_fullscreen(
659                         datatoc_gpencil_fx_colorize_frag_glsl, NULL);
660         }
661         if (!e_data->gpencil_fx_flip_sh) {
662                 e_data->gpencil_fx_flip_sh = DRW_shader_create_fullscreen(
663                         datatoc_gpencil_fx_flip_frag_glsl, NULL);
664         }
665         if (!e_data->gpencil_fx_light_sh) {
666                 e_data->gpencil_fx_light_sh = DRW_shader_create_fullscreen(
667                         datatoc_gpencil_fx_light_frag_glsl, NULL);
668         }
669         if (!e_data->gpencil_fx_pixel_sh) {
670                 e_data->gpencil_fx_pixel_sh = DRW_shader_create_fullscreen(
671                         datatoc_gpencil_fx_pixel_frag_glsl, NULL);
672         }
673         if (!e_data->gpencil_fx_rim_prepare_sh) {
674                 e_data->gpencil_fx_rim_prepare_sh = DRW_shader_create_fullscreen(
675                         datatoc_gpencil_fx_rim_prepare_frag_glsl, NULL);
676
677                 e_data->gpencil_fx_rim_resolve_sh = DRW_shader_create_fullscreen(
678                         datatoc_gpencil_fx_rim_resolve_frag_glsl, NULL);
679         }
680         if (!e_data->gpencil_fx_shadow_prepare_sh) {
681                 e_data->gpencil_fx_shadow_prepare_sh = DRW_shader_create_fullscreen(
682                         datatoc_gpencil_fx_shadow_prepare_frag_glsl, NULL);
683
684                 e_data->gpencil_fx_shadow_resolve_sh = DRW_shader_create_fullscreen(
685                         datatoc_gpencil_fx_shadow_resolve_frag_glsl, NULL);
686         }
687         if (!e_data->gpencil_fx_glow_prepare_sh) {
688                 e_data->gpencil_fx_glow_prepare_sh = DRW_shader_create_fullscreen(
689                         datatoc_gpencil_fx_glow_prepare_frag_glsl, NULL);
690
691                 e_data->gpencil_fx_glow_resolve_sh = DRW_shader_create_fullscreen(
692                         datatoc_gpencil_fx_glow_resolve_frag_glsl, NULL);
693         }
694         if (!e_data->gpencil_fx_swirl_sh) {
695                 e_data->gpencil_fx_swirl_sh = DRW_shader_create_fullscreen(
696                         datatoc_gpencil_fx_swirl_frag_glsl, NULL);
697         }
698         if (!e_data->gpencil_fx_wave_sh) {
699                 e_data->gpencil_fx_wave_sh = DRW_shader_create_fullscreen(
700                         datatoc_gpencil_fx_wave_frag_glsl, NULL);
701         }
702 }
703
704 /* free FX shaders */
705 void GPENCIL_delete_fx_shaders(GPENCIL_e_data *e_data)
706 {
707         DRW_SHADER_FREE_SAFE(e_data->gpencil_fx_blur_sh);
708         DRW_SHADER_FREE_SAFE(e_data->gpencil_fx_colorize_sh);
709         DRW_SHADER_FREE_SAFE(e_data->gpencil_fx_flip_sh);
710         DRW_SHADER_FREE_SAFE(e_data->gpencil_fx_light_sh);
711         DRW_SHADER_FREE_SAFE(e_data->gpencil_fx_pixel_sh);
712         DRW_SHADER_FREE_SAFE(e_data->gpencil_fx_rim_prepare_sh);
713         DRW_SHADER_FREE_SAFE(e_data->gpencil_fx_rim_resolve_sh);
714         DRW_SHADER_FREE_SAFE(e_data->gpencil_fx_shadow_prepare_sh);
715         DRW_SHADER_FREE_SAFE(e_data->gpencil_fx_shadow_resolve_sh);
716         DRW_SHADER_FREE_SAFE(e_data->gpencil_fx_glow_prepare_sh);
717         DRW_SHADER_FREE_SAFE(e_data->gpencil_fx_glow_resolve_sh);
718         DRW_SHADER_FREE_SAFE(e_data->gpencil_fx_swirl_sh);
719         DRW_SHADER_FREE_SAFE(e_data->gpencil_fx_wave_sh);
720 }
721
722 /* create all passes used by FX */
723 void GPENCIL_create_fx_passes(GPENCIL_PassList *psl)
724 {
725         psl->fx_shader_pass = DRW_pass_create(
726                 "GPencil Shader FX Pass",
727                 DRW_STATE_WRITE_COLOR |
728                 DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS);
729         psl->fx_shader_pass_blend = DRW_pass_create(
730                 "GPencil Shader FX Pass",
731                 DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND |
732                 DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS);
733 }
734
735
736 /* prepare fx shading groups */
737 void DRW_gpencil_fx_prepare(
738         GPENCIL_e_data *e_data, GPENCIL_Data *vedata,
739         tGPencilObjectCache *cache)
740 {
741         GPENCIL_StorageList *stl = ((GPENCIL_Data *)vedata)->stl;
742         int ob_idx = cache->idx;
743
744         if (cache->shader_fx.first == NULL) {
745                 return;
746         }
747         /* loop FX */
748         for (ShaderFxData *fx = cache->shader_fx.first; fx; fx = fx->next) {
749                 if (effect_is_active(cache->gpd, fx, stl->storage->is_render)) {
750                         switch (fx->type) {
751                                 case eShaderFxType_Blur:
752                                         DRW_gpencil_fx_blur(fx, ob_idx, e_data, vedata, cache);
753                                         break;
754                                 case eShaderFxType_Colorize:
755                                         DRW_gpencil_fx_colorize(fx, e_data, vedata);
756                                         break;
757                                 case eShaderFxType_Flip:
758                                         DRW_gpencil_fx_flip(fx, e_data, vedata);
759                                         break;
760                                 case eShaderFxType_Light:
761                                         DRW_gpencil_fx_light(fx, e_data, vedata, cache);
762                                         break;
763                                 case eShaderFxType_Pixel:
764                                         DRW_gpencil_fx_pixel(fx, e_data, vedata, cache);
765                                         break;
766                                 case eShaderFxType_Rim:
767                                         DRW_gpencil_fx_rim(fx, e_data, vedata, cache);
768                                         break;
769                                 case eShaderFxType_Shadow:
770                                         DRW_gpencil_fx_shadow(fx, e_data, vedata, cache);
771                                         break;
772                                 case eShaderFxType_Glow:
773                                         DRW_gpencil_fx_glow(fx, e_data, vedata, cache);
774                                         break;
775                                 case eShaderFxType_Swirl:
776                                         DRW_gpencil_fx_swirl(fx, e_data, vedata, cache);
777                                         break;
778                                 case eShaderFxType_Wave:
779                                         DRW_gpencil_fx_wave(fx, e_data, vedata);
780                                         break;
781                                 default:
782                                         break;
783                         }
784                 }
785         }
786
787 }
788
789 /* helper to draw one FX pass and do ping-pong copy */
790 static void gpencil_draw_fx_pass(
791         GPENCIL_e_data *e_data,
792         GPENCIL_PassList *psl,
793         GPENCIL_FramebufferList *fbl,
794         DRWShadingGroup *shgrp, bool blend)
795 {
796         if (shgrp == NULL) {
797                 return;
798         }
799
800         const float clearcol[4] = {0.0f};
801         GPU_framebuffer_bind(fbl->temp_fb_b);
802         GPU_framebuffer_clear_color_depth(fbl->temp_fb_b, clearcol, 1.0f);
803
804         /* draw effect pass in temp texture (B) using as source the previous image
805          * existing in the other temp texture (A) */
806         if (!blend) {
807                 DRW_draw_pass_subset(psl->fx_shader_pass, shgrp, shgrp);
808         }
809         else {
810                 DRW_draw_pass_subset(psl->fx_shader_pass_blend, shgrp, shgrp);
811         }
812
813         /* copy pass from b to a for ping-pong frame buffers */
814         e_data->input_depth_tx = e_data->temp_depth_tx_b;
815         e_data->input_color_tx = e_data->temp_color_tx_b;
816
817         GPU_framebuffer_bind(fbl->temp_fb_a);
818         GPU_framebuffer_clear_color_depth(fbl->temp_fb_a, clearcol, 1.0f);
819         DRW_draw_pass(psl->mix_pass_noblend);
820 }
821
822 /* helper to manage gaussian blur passes */
823 static void draw_gpencil_blur_passes(
824         GPENCIL_e_data *e_data,
825         GPENCIL_Data *vedata,
826         BlurShaderFxData *fxd)
827 {
828         if (fxd->runtime.fx_sh == NULL) {
829                 return;
830         }
831
832         GPENCIL_PassList *psl = ((GPENCIL_Data *)vedata)->psl;
833         GPENCIL_FramebufferList *fbl = ((GPENCIL_Data *)vedata)->fbl;
834         DRWShadingGroup *shgrp = fxd->runtime.fx_sh;
835         int samples = fxd->samples;
836
837         float bx = fxd->blur[0];
838         float by = fxd->blur[1];
839
840         /* the blur is done in two steps (Hor/Ver) because is faster and
841          * gets better result
842          *
843          * samples could be 0 and disable de blur effects because sometimes
844          * is easier animate the number of samples only, instead to animate the
845          * hide/unhide and the number of samples to make some effects.
846          */
847         for (int b = 0; b < samples; b++) {
848                 /* horizontal */
849                 if (bx > 0) {
850                         fxd->blur[0] = bx;
851                         fxd->blur[1] = 0;
852                         gpencil_draw_fx_pass(e_data, psl, fbl, shgrp, true);
853                 }
854                 /* vertical */
855                 if (by > 0) {
856                         fxd->blur[0] = 0;
857                         fxd->blur[1] = by;
858                         gpencil_draw_fx_pass(e_data, psl, fbl, shgrp, true);
859                 }
860         }
861 }
862
863 /* blur intermediate pass */
864 static void draw_gpencil_midpass_blur(
865         GPENCIL_Data *vedata,
866         ShaderFxData_Runtime *runtime)
867 {
868         GPENCIL_PassList *psl = ((GPENCIL_Data *)vedata)->psl;
869         GPENCIL_FramebufferList *fbl = ((GPENCIL_Data *)vedata)->fbl;
870         const float clearcol[4] = {0.0f};
871
872         GPU_framebuffer_bind(fbl->temp_fb_b);
873         GPU_framebuffer_clear_color_depth(fbl->temp_fb_b, clearcol, 1.0f);
874         DRW_draw_pass_subset(psl->fx_shader_pass_blend,
875                 runtime->fx_sh_b, runtime->fx_sh_b);
876
877         /* copy pass from b for ping-pong frame buffers */
878         GPU_framebuffer_bind(fbl->temp_fb_fx);
879         GPU_framebuffer_clear_color_depth(fbl->temp_fb_fx, clearcol, 1.0f);
880         DRW_draw_pass(psl->mix_pass_noblend);
881 }
882
883 /* do blur of mid passes */
884 static void draw_gpencil_do_blur(
885         GPENCIL_e_data *e_data,
886         GPENCIL_Data *vedata,
887         ShaderFxData_Runtime *runtime,
888         int samples, int bx, int by, int blur[2])
889 {
890         e_data->input_depth_tx = e_data->temp_depth_tx_b;
891         e_data->input_color_tx = e_data->temp_color_tx_b;
892
893         if ((samples > 0) && ((bx > 0) || (by > 0))) {
894                 for (int x = 0; x < samples; x++) {
895
896                         /* horizontal */
897                         blur[0] = bx;
898                         blur[1] = 0;
899                         draw_gpencil_midpass_blur(vedata, runtime);
900
901                         /* Vertical */
902                         blur[0] = 0;
903                         blur[1] = by;
904                         draw_gpencil_midpass_blur(vedata, runtime);
905
906                         blur[0] = bx;
907                         blur[1] = by;
908                 }
909         }
910 }
911
912 /* helper to draw RIM passes */
913 static void draw_gpencil_rim_passes(
914         GPENCIL_e_data *e_data,
915         GPENCIL_Data *vedata,
916         RimShaderFxData *fxd)
917 {
918         if (fxd->runtime.fx_sh_b == NULL) {
919                 return;
920         }
921
922         GPENCIL_PassList *psl = ((GPENCIL_Data *)vedata)->psl;
923         GPENCIL_FramebufferList *fbl = ((GPENCIL_Data *)vedata)->fbl;
924
925         const float clearcol[4] = {0.0f};
926
927         /* prepare mask */
928         GPU_framebuffer_bind(fbl->temp_fb_fx);
929         GPU_framebuffer_clear_color_depth(fbl->temp_fb_fx, clearcol, 1.0f);
930         DRW_draw_pass_subset(
931                 psl->fx_shader_pass_blend,
932                 fxd->runtime.fx_sh, fxd->runtime.fx_sh);
933
934         /* blur rim */
935         draw_gpencil_do_blur(
936                 e_data, vedata, &fxd->runtime,
937                 fxd->samples,
938                 fxd->blur[0], fxd->blur[1],
939                 &fxd->blur[0]);
940
941         /* resolve */
942         GPU_framebuffer_bind(fbl->temp_fb_b);
943         GPU_framebuffer_clear_color_depth(fbl->temp_fb_b, clearcol, 1.0f);
944         DRW_draw_pass_subset(
945                 psl->fx_shader_pass_blend,
946                 fxd->runtime.fx_sh_c, fxd->runtime.fx_sh_c);
947
948         /* copy pass from b to a for ping-pong frame buffers */
949         e_data->input_depth_tx = e_data->temp_depth_tx_b;
950         e_data->input_color_tx = e_data->temp_color_tx_b;
951
952         GPU_framebuffer_bind(fbl->temp_fb_a);
953         GPU_framebuffer_clear_color_depth(fbl->temp_fb_a, clearcol, 1.0f);
954         DRW_draw_pass(psl->mix_pass_noblend);
955 }
956
957 /* helper to draw SHADOW passes */
958 static void draw_gpencil_shadow_passes(
959         GPENCIL_e_data *e_data,
960         GPENCIL_Data *vedata,
961         ShadowShaderFxData *fxd)
962 {
963         if (fxd->runtime.fx_sh_b == NULL) {
964                 return;
965         }
966
967         GPENCIL_PassList *psl = ((GPENCIL_Data *)vedata)->psl;
968         GPENCIL_FramebufferList *fbl = ((GPENCIL_Data *)vedata)->fbl;
969         const float clearcol[4] = {0.0f};
970
971         /* prepare shadow */
972         GPU_framebuffer_bind(fbl->temp_fb_fx);
973         GPU_framebuffer_clear_color_depth(fbl->temp_fb_fx, clearcol, 1.0f);
974         DRW_draw_pass_subset(
975                 psl->fx_shader_pass_blend,
976                 fxd->runtime.fx_sh, fxd->runtime.fx_sh);
977
978         /* blur shadow */
979         draw_gpencil_do_blur(
980                 e_data, vedata, &fxd->runtime,
981                 fxd->samples,
982                 fxd->blur[0], fxd->blur[1],
983                 &fxd->blur[0]);
984
985         /* resolve */
986         GPU_framebuffer_bind(fbl->temp_fb_b);
987         GPU_framebuffer_clear_color_depth(fbl->temp_fb_b, clearcol, 1.0f);
988         DRW_draw_pass_subset(
989                 psl->fx_shader_pass_blend,
990                 fxd->runtime.fx_sh_c, fxd->runtime.fx_sh_c);
991
992         /* copy pass from b to a for ping-pong frame buffers */
993         e_data->input_depth_tx = e_data->temp_depth_tx_b;
994         e_data->input_color_tx = e_data->temp_color_tx_b;
995
996         GPU_framebuffer_bind(fbl->temp_fb_a);
997         GPU_framebuffer_clear_color_depth(fbl->temp_fb_a, clearcol, 1.0f);
998         DRW_draw_pass(psl->mix_pass_noblend);
999 }
1000
1001 /* helper to draw GLOW passes */
1002 static void draw_gpencil_glow_passes(
1003         GPENCIL_e_data *e_data,
1004         GPENCIL_Data *vedata,
1005         GlowShaderFxData *fxd)
1006 {
1007         if (fxd->runtime.fx_sh_b == NULL) {
1008                 return;
1009         }
1010
1011         GPENCIL_PassList *psl = ((GPENCIL_Data *)vedata)->psl;
1012         GPENCIL_FramebufferList *fbl = ((GPENCIL_Data *)vedata)->fbl;
1013
1014         const float clearcol[4] = {0.0f};
1015
1016         /* prepare glow */
1017         GPU_framebuffer_bind(fbl->temp_fb_fx);
1018         GPU_framebuffer_clear_color_depth(fbl->temp_fb_fx, clearcol, 1.0f);
1019         DRW_draw_pass_subset(
1020                 psl->fx_shader_pass_blend,
1021                 fxd->runtime.fx_sh, fxd->runtime.fx_sh);
1022
1023         /* blur glow */
1024         draw_gpencil_do_blur(
1025                 e_data, vedata, &fxd->runtime,
1026                 fxd->samples,
1027                 fxd->blur[0], fxd->blur[0],
1028                 &fxd->blur[0]);
1029
1030         /* resolve */
1031         GPU_framebuffer_bind(fbl->temp_fb_b);
1032         GPU_framebuffer_clear_color_depth(fbl->temp_fb_b, clearcol, 1.0f);
1033
1034         /* reuses blur field to keep alpha mode */
1035         fxd->blur[1] = (fxd->flag & FX_GLOW_USE_ALPHA) ? 1 : 0;
1036
1037         DRW_draw_pass_subset(
1038                 psl->fx_shader_pass_blend,
1039                 fxd->runtime.fx_sh_c, fxd->runtime.fx_sh_c);
1040
1041         /* copy pass from b to a for ping-pong frame buffers */
1042         e_data->input_depth_tx = e_data->temp_depth_tx_b;
1043         e_data->input_color_tx = e_data->temp_color_tx_b;
1044
1045         GPU_framebuffer_bind(fbl->temp_fb_a);
1046         GPU_framebuffer_clear_color_depth(fbl->temp_fb_a, clearcol, 1.0f);
1047         DRW_draw_pass(psl->mix_pass_noblend);
1048 }
1049
1050 /* apply all object fx effects */
1051 void DRW_gpencil_fx_draw(
1052         GPENCIL_e_data *e_data,
1053         GPENCIL_Data *vedata, tGPencilObjectCache *cache)
1054 {
1055         GPENCIL_StorageList *stl = ((GPENCIL_Data *)vedata)->stl;
1056         GPENCIL_PassList *psl = ((GPENCIL_Data *)vedata)->psl;
1057         GPENCIL_FramebufferList *fbl = ((GPENCIL_Data *)vedata)->fbl;
1058
1059         /* loop FX modifiers */
1060         for (ShaderFxData *fx = cache->shader_fx.first; fx; fx = fx->next) {
1061                 if (effect_is_active(cache->gpd, fx, stl->storage->is_render)) {
1062                         switch (fx->type) {
1063
1064                                 case eShaderFxType_Blur:
1065                                 {
1066                                         BlurShaderFxData *fxd = (BlurShaderFxData *)fx;
1067                                         draw_gpencil_blur_passes(e_data, vedata, fxd);
1068                                         break;
1069                                 }
1070                                 case eShaderFxType_Colorize:
1071                                 {
1072                                         ColorizeShaderFxData *fxd = (ColorizeShaderFxData *)fx;
1073                                         gpencil_draw_fx_pass(e_data, psl, fbl, fxd->runtime.fx_sh, false);
1074                                         break;
1075                                 }
1076                                 case eShaderFxType_Flip:
1077                                 {
1078                                         FlipShaderFxData *fxd = (FlipShaderFxData *)fx;
1079                                         gpencil_draw_fx_pass(e_data, psl, fbl, fxd->runtime.fx_sh, false);
1080                                         break;
1081                                 }
1082                                 case eShaderFxType_Light:
1083                                 {
1084                                         LightShaderFxData *fxd = (LightShaderFxData *)fx;
1085                                         gpencil_draw_fx_pass(e_data, psl, fbl, fxd->runtime.fx_sh, false);
1086                                         break;
1087                                 }
1088                                 case eShaderFxType_Pixel:
1089                                 {
1090                                         PixelShaderFxData *fxd = (PixelShaderFxData *)fx;
1091                                         gpencil_draw_fx_pass(e_data, psl, fbl, fxd->runtime.fx_sh, false);
1092                                         break;
1093                                 }
1094                                 case eShaderFxType_Rim:
1095                                 {
1096                                         RimShaderFxData *fxd = (RimShaderFxData *)fx;
1097                                         draw_gpencil_rim_passes(e_data, vedata, fxd);
1098                                         break;
1099                                 }
1100                                 case eShaderFxType_Shadow:
1101                                 {
1102                                         ShadowShaderFxData *fxd = (ShadowShaderFxData *)fx;
1103                                         draw_gpencil_shadow_passes(e_data, vedata, fxd);
1104                                         break;
1105                                 }
1106                                 case eShaderFxType_Glow:
1107                                 {
1108                                         GlowShaderFxData *fxd = (GlowShaderFxData *)fx;
1109                                         draw_gpencil_glow_passes(e_data, vedata, fxd);
1110                                         break;
1111                                 }
1112                                 case eShaderFxType_Swirl:
1113                                 {
1114                                         SwirlShaderFxData *fxd = (SwirlShaderFxData *)fx;
1115                                         gpencil_draw_fx_pass(e_data, psl, fbl, fxd->runtime.fx_sh, false);
1116                                         break;
1117                                 }
1118                                 case eShaderFxType_Wave:
1119                                 {
1120                                         WaveShaderFxData *fxd = (WaveShaderFxData *)fx;
1121                                         gpencil_draw_fx_pass(e_data, psl, fbl, fxd->runtime.fx_sh, false);
1122                                         break;
1123                                 }
1124                                 default:
1125                                         break;
1126                         }
1127                 }
1128         }
1129 }