GPencil: Cleanup - Remove storage Grid matrix and replace with local variable
[blender.git] / source / blender / draw / engines / gpencil / gpencil_engine.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * Copyright 2017, Blender Foundation.
17  */
18
19 /** \file
20  * \ingroup draw
21  */
22 #include "DRW_engine.h"
23 #include "DRW_render.h"
24
25 #include "BKE_gpencil.h"
26 #include "BKE_library.h"
27 #include "BKE_object.h"
28 #include "BKE_paint.h"
29 #include "BKE_shader_fx.h"
30
31 #include "DNA_gpencil_types.h"
32 #include "DNA_view3d_types.h"
33
34 #include "draw_mode_engines.h"
35
36 #include "GPU_texture.h"
37
38 #include "gpencil_engine.h"
39
40 #include "DEG_depsgraph_query.h"
41
42 #include "ED_view3d.h"
43 #include "ED_screen.h"
44
45 #include "UI_resources.h"
46
47 extern char datatoc_gpencil_fill_vert_glsl[];
48 extern char datatoc_gpencil_fill_frag_glsl[];
49 extern char datatoc_gpencil_stroke_vert_glsl[];
50 extern char datatoc_gpencil_stroke_geom_glsl[];
51 extern char datatoc_gpencil_stroke_frag_glsl[];
52 extern char datatoc_gpencil_zdepth_mix_frag_glsl[];
53 extern char datatoc_gpencil_simple_mix_frag_glsl[];
54 extern char datatoc_gpencil_point_vert_glsl[];
55 extern char datatoc_gpencil_point_geom_glsl[];
56 extern char datatoc_gpencil_point_frag_glsl[];
57 extern char datatoc_gpencil_background_frag_glsl[];
58 extern char datatoc_gpencil_paper_frag_glsl[];
59 extern char datatoc_gpencil_edit_point_vert_glsl[];
60 extern char datatoc_gpencil_edit_point_geom_glsl[];
61 extern char datatoc_gpencil_edit_point_frag_glsl[];
62 extern char datatoc_gpencil_blend_frag_glsl[];
63
64 extern char datatoc_common_view_lib_glsl[];
65
66 /* *********** STATIC *********** */
67 static GPENCIL_e_data e_data = {NULL}; /* Engine data */
68
69 /* *********** FUNCTIONS *********** */
70
71 /* create a multisample buffer if not present */
72 void DRW_gpencil_multisample_ensure(GPENCIL_Data *vedata, int rect_w, int rect_h)
73 {
74   GPENCIL_FramebufferList *fbl = vedata->fbl;
75   GPENCIL_StorageList *stl = ((GPENCIL_Data *)vedata)->stl;
76   GPENCIL_TextureList *txl = ((GPENCIL_Data *)vedata)->txl;
77
78   short samples = stl->storage->multisamples;
79
80   if (samples > 0) {
81     if (!fbl->multisample_fb) {
82       fbl->multisample_fb = GPU_framebuffer_create();
83       if (fbl->multisample_fb) {
84         if (txl->multisample_color == NULL) {
85           txl->multisample_color = GPU_texture_create_2d_multisample(
86               rect_w, rect_h, GPU_RGBA16F, NULL, samples, NULL);
87         }
88         if (txl->multisample_depth == NULL) {
89           txl->multisample_depth = GPU_texture_create_2d_multisample(
90               rect_w, rect_h, GPU_DEPTH_COMPONENT24, NULL, samples, NULL);
91         }
92         GPU_framebuffer_ensure_config(&fbl->multisample_fb,
93                                       {GPU_ATTACHMENT_TEXTURE(txl->multisample_depth),
94                                        GPU_ATTACHMENT_TEXTURE(txl->multisample_color)});
95       }
96     }
97   }
98 }
99
100 static void GPENCIL_create_framebuffers(void *vedata)
101 {
102   GPENCIL_FramebufferList *fbl = ((GPENCIL_Data *)vedata)->fbl;
103   GPENCIL_StorageList *stl = ((GPENCIL_Data *)vedata)->stl;
104
105   /* Go full 32bits for rendering */
106   eGPUTextureFormat fb_format = DRW_state_is_image_render() ? GPU_RGBA32F : GPU_RGBA16F;
107
108   if (DRW_state_is_fbo()) {
109     const float *viewport_size = DRW_viewport_size_get();
110     const int size[2] = {(int)viewport_size[0], (int)viewport_size[1]};
111
112     /* create multiframe framebuffer for AA */
113     if ((stl->storage->framebuffer_flag & GP_FRAMEBUFFER_MULTISAMPLE) &&
114         (stl->storage->multisamples > 0)) {
115       DRW_gpencil_multisample_ensure(vedata, size[0], size[1]);
116     }
117
118     /* Framebufers for basic object drawing */
119     if (stl->storage->framebuffer_flag & GP_FRAMEBUFFER_BASIC) {
120       /* temp textures for ping-pong buffers */
121       e_data.temp_depth_tx_a = DRW_texture_pool_query_2d(
122           size[0], size[1], GPU_DEPTH_COMPONENT24, &draw_engine_gpencil_type);
123       e_data.temp_color_tx_a = DRW_texture_pool_query_2d(
124           size[0], size[1], fb_format, &draw_engine_gpencil_type);
125       GPU_framebuffer_ensure_config(&fbl->temp_fb_a,
126                                     {
127                                         GPU_ATTACHMENT_TEXTURE(e_data.temp_depth_tx_a),
128                                         GPU_ATTACHMENT_TEXTURE(e_data.temp_color_tx_a),
129                                     });
130
131       e_data.temp_depth_tx_b = DRW_texture_pool_query_2d(
132           size[0], size[1], GPU_DEPTH_COMPONENT24, &draw_engine_gpencil_type);
133       e_data.temp_color_tx_b = DRW_texture_pool_query_2d(
134           size[0], size[1], fb_format, &draw_engine_gpencil_type);
135       GPU_framebuffer_ensure_config(&fbl->temp_fb_b,
136                                     {
137                                         GPU_ATTACHMENT_TEXTURE(e_data.temp_depth_tx_b),
138                                         GPU_ATTACHMENT_TEXTURE(e_data.temp_color_tx_b),
139                                     });
140
141       /* used for FX effects and Layer blending */
142       e_data.temp_depth_tx_fx = DRW_texture_pool_query_2d(
143           size[0], size[1], GPU_DEPTH_COMPONENT24, &draw_engine_gpencil_type);
144       e_data.temp_color_tx_fx = DRW_texture_pool_query_2d(
145           size[0], size[1], fb_format, &draw_engine_gpencil_type);
146       GPU_framebuffer_ensure_config(&fbl->temp_fb_fx,
147                                     {
148                                         GPU_ATTACHMENT_TEXTURE(e_data.temp_depth_tx_fx),
149                                         GPU_ATTACHMENT_TEXTURE(e_data.temp_color_tx_fx),
150                                     });
151     }
152
153     /* background framebuffer to speed up drawing process (always 16 bits) */
154     if (stl->storage->framebuffer_flag & GP_FRAMEBUFFER_DRAW) {
155       e_data.background_depth_tx = DRW_texture_pool_query_2d(
156           size[0], size[1], GPU_DEPTH_COMPONENT24, &draw_engine_gpencil_type);
157       e_data.background_color_tx = DRW_texture_pool_query_2d(
158           size[0], size[1], GPU_RGBA32F, &draw_engine_gpencil_type);
159       GPU_framebuffer_ensure_config(&fbl->background_fb,
160                                     {
161                                         GPU_ATTACHMENT_TEXTURE(e_data.background_depth_tx),
162                                         GPU_ATTACHMENT_TEXTURE(e_data.background_color_tx),
163                                     });
164     }
165   }
166 }
167
168 static void GPENCIL_create_shaders(void)
169 {
170   /* normal fill shader */
171   if (!e_data.gpencil_fill_sh) {
172     e_data.gpencil_fill_sh = DRW_shader_create_with_lib(datatoc_gpencil_fill_vert_glsl,
173                                                         NULL,
174                                                         datatoc_gpencil_fill_frag_glsl,
175                                                         datatoc_common_view_lib_glsl,
176                                                         NULL);
177   }
178
179   /* normal stroke shader using geometry to display lines (line mode) */
180   if (!e_data.gpencil_stroke_sh) {
181     e_data.gpencil_stroke_sh = DRW_shader_create_with_lib(datatoc_gpencil_stroke_vert_glsl,
182                                                           datatoc_gpencil_stroke_geom_glsl,
183                                                           datatoc_gpencil_stroke_frag_glsl,
184                                                           datatoc_common_view_lib_glsl,
185                                                           NULL);
186   }
187
188   /* dot/rectangle mode for normal strokes using geometry */
189   if (!e_data.gpencil_point_sh) {
190     e_data.gpencil_point_sh = DRW_shader_create_with_lib(datatoc_gpencil_point_vert_glsl,
191                                                          datatoc_gpencil_point_geom_glsl,
192                                                          datatoc_gpencil_point_frag_glsl,
193                                                          datatoc_common_view_lib_glsl,
194                                                          NULL);
195   }
196   /* used for edit points or strokes with one point only */
197   if (!e_data.gpencil_edit_point_sh) {
198     e_data.gpencil_edit_point_sh = DRW_shader_create_with_lib(datatoc_gpencil_edit_point_vert_glsl,
199                                                               datatoc_gpencil_edit_point_geom_glsl,
200                                                               datatoc_gpencil_edit_point_frag_glsl,
201                                                               datatoc_common_view_lib_glsl,
202                                                               NULL);
203   }
204
205   /* used for edit lines for edit modes */
206   if (!e_data.gpencil_line_sh) {
207     e_data.gpencil_line_sh = GPU_shader_get_builtin_shader(GPU_SHADER_3D_FLAT_COLOR);
208   }
209
210   /* used to filling during drawing */
211   if (!e_data.gpencil_drawing_fill_sh) {
212     e_data.gpencil_drawing_fill_sh = GPU_shader_get_builtin_shader(GPU_SHADER_3D_SMOOTH_COLOR);
213   }
214
215   /* full screen for mix zdepth*/
216   if (!e_data.gpencil_fullscreen_sh) {
217     e_data.gpencil_fullscreen_sh = DRW_shader_create_fullscreen(
218         datatoc_gpencil_zdepth_mix_frag_glsl, NULL);
219   }
220   if (!e_data.gpencil_simple_fullscreen_sh) {
221     e_data.gpencil_simple_fullscreen_sh = DRW_shader_create_fullscreen(
222         datatoc_gpencil_simple_mix_frag_glsl, NULL);
223   }
224
225   /* blend */
226   if (!e_data.gpencil_blend_fullscreen_sh) {
227     e_data.gpencil_blend_fullscreen_sh = DRW_shader_create_fullscreen(
228         datatoc_gpencil_blend_frag_glsl, NULL);
229   }
230
231   /* shaders for use when drawing */
232   if (!e_data.gpencil_background_sh) {
233     e_data.gpencil_background_sh = DRW_shader_create_fullscreen(
234         datatoc_gpencil_background_frag_glsl, NULL);
235   }
236   if (!e_data.gpencil_paper_sh) {
237     e_data.gpencil_paper_sh = DRW_shader_create_fullscreen(datatoc_gpencil_paper_frag_glsl, NULL);
238   }
239 }
240
241 void GPENCIL_engine_init(void *vedata)
242 {
243   GPENCIL_StorageList *stl = ((GPENCIL_Data *)vedata)->stl;
244   /* init storage */
245   if (!stl->storage) {
246     stl->storage = MEM_callocN(sizeof(GPENCIL_Storage), "GPENCIL_Storage");
247
248     /* unit matrix */
249     unit_m4(stl->storage->unit_matrix);
250     stl->storage->shade_render[0] = OB_RENDER;
251     stl->storage->shade_render[1] = 0;
252   }
253
254   stl->storage->multisamples = U.gpencil_multisamples;
255
256   /* create shaders */
257   GPENCIL_create_shaders();
258   GPENCIL_create_fx_shaders(&e_data);
259
260   /* blank texture used if no texture defined for fill shader */
261   if (!e_data.gpencil_blank_texture) {
262     float rect[16][16][4] = {{{0.0f}}};
263     e_data.gpencil_blank_texture = DRW_texture_create_2d(
264         16, 16, GPU_RGBA8, DRW_TEX_FILTER, (float *)rect);
265   }
266 }
267
268 static void GPENCIL_engine_free(void)
269 {
270   /* only free custom shaders, builtin shaders are freed in blender close */
271   DRW_SHADER_FREE_SAFE(e_data.gpencil_fill_sh);
272   DRW_SHADER_FREE_SAFE(e_data.gpencil_stroke_sh);
273   DRW_SHADER_FREE_SAFE(e_data.gpencil_point_sh);
274   DRW_SHADER_FREE_SAFE(e_data.gpencil_edit_point_sh);
275   DRW_SHADER_FREE_SAFE(e_data.gpencil_fullscreen_sh);
276   DRW_SHADER_FREE_SAFE(e_data.gpencil_simple_fullscreen_sh);
277   DRW_SHADER_FREE_SAFE(e_data.gpencil_blend_fullscreen_sh);
278   DRW_SHADER_FREE_SAFE(e_data.gpencil_background_sh);
279   DRW_SHADER_FREE_SAFE(e_data.gpencil_paper_sh);
280
281   DRW_TEXTURE_FREE_SAFE(e_data.gpencil_blank_texture);
282
283   GPU_BATCH_DISCARD_SAFE(e_data.batch_buffer_stroke);
284   MEM_SAFE_FREE(e_data.batch_buffer_stroke);
285
286   GPU_BATCH_DISCARD_SAFE(e_data.batch_buffer_fill);
287   MEM_SAFE_FREE(e_data.batch_buffer_fill);
288
289   GPU_BATCH_DISCARD_SAFE(e_data.batch_buffer_ctrlpoint);
290   MEM_SAFE_FREE(e_data.batch_buffer_ctrlpoint);
291
292   GPU_BATCH_DISCARD_SAFE(e_data.batch_grid);
293   MEM_SAFE_FREE(e_data.batch_grid);
294
295   /* effects */
296   GPENCIL_delete_fx_shaders(&e_data);
297 }
298
299 void GPENCIL_cache_init(void *vedata)
300 {
301   GPENCIL_PassList *psl = ((GPENCIL_Data *)vedata)->psl;
302   GPENCIL_StorageList *stl = ((GPENCIL_Data *)vedata)->stl;
303   const DRWContextState *draw_ctx = DRW_context_state_get();
304   Scene *scene = draw_ctx->scene;
305   ToolSettings *ts = scene->toolsettings;
306   View3D *v3d = draw_ctx->v3d;
307   Brush *brush = BKE_paint_brush(&ts->gp_paint->paint);
308
309   /* Special handling for when active object is GP object (e.g. for draw mode) */
310   Object *obact = draw_ctx->obact;
311   bGPdata *obact_gpd = NULL;
312   MaterialGPencilStyle *gp_style = NULL;
313
314   if (obact && (obact->type == OB_GPENCIL) && (obact->data)) {
315     obact_gpd = (bGPdata *)obact->data;
316     /* use the brush material */
317     Material *ma = BKE_gpencil_object_material_get_from_brush(obact, brush);
318     if (ma != NULL) {
319       gp_style = ma->gp_style;
320     }
321     /* this is not common, but avoid any special situations when brush could be without material */
322     if (gp_style == NULL) {
323       gp_style = BKE_material_gpencil_settings_get(obact, obact->actcol);
324     }
325   }
326
327   if (!stl->g_data) {
328     /* Alloc transient pointers */
329     stl->g_data = MEM_mallocN(sizeof(g_data), "g_data");
330     stl->storage->xray = GP_XRAY_FRONT;                       /* used for drawing */
331     stl->storage->stroke_style = GP_STYLE_STROKE_STYLE_SOLID; /* used for drawing */
332   }
333   stl->storage->tonemapping = 0;
334
335   stl->g_data->shgrps_edit_line = NULL;
336   stl->g_data->shgrps_edit_point = NULL;
337
338   if (!stl->shgroups) {
339     /* Alloc maximum size because count strokes is very slow and can be very complex due onion
340      * skinning.
341      */
342     stl->shgroups = MEM_mallocN(sizeof(GPENCIL_shgroup) * GPENCIL_MAX_SHGROUPS, "GPENCIL_shgroup");
343   }
344
345   /* init gp objects cache */
346   stl->g_data->gp_cache_used = 0;
347   stl->g_data->gp_cache_size = 0;
348   stl->g_data->gp_object_cache = NULL;
349   stl->g_data->do_instances = false;
350
351   {
352     /* Stroke pass 2D */
353     psl->stroke_pass_2d = DRW_pass_create("GPencil Stroke Pass",
354                                           DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH |
355                                               DRW_STATE_DEPTH_ALWAYS | DRW_STATE_BLEND_ALPHA);
356     stl->storage->shgroup_id = 0;
357     /* Stroke pass 3D */
358     psl->stroke_pass_3d = DRW_pass_create("GPencil Stroke Pass",
359                                           DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH |
360                                               DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_BLEND_ALPHA);
361     stl->storage->shgroup_id = 0;
362
363     /* edit pass */
364     psl->edit_pass = DRW_pass_create("GPencil Edit Pass",
365                                      DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND_ALPHA);
366
367     /* detect if playing animation */
368     if (draw_ctx->evil_C) {
369
370       bool playing = ED_screen_animation_playing(CTX_wm_manager(draw_ctx->evil_C)) != NULL;
371       if (playing != stl->storage->is_playing) {
372         stl->storage->reset_cache = true;
373       }
374       stl->storage->is_playing = playing;
375     }
376     else {
377       stl->storage->is_playing = false;
378       stl->storage->reset_cache = false;
379     }
380     /* save render state */
381     stl->storage->is_render = DRW_state_is_image_render();
382     stl->storage->is_mat_preview = (bool)stl->storage->is_render &&
383                                    STREQ(scene->id.name + 2, "preview");
384
385     if (obact_gpd) {
386       /* for some reason, when press play there is a delay in the animation flag check
387        * and this produces errors. To be sure, we set cache as dirty because the frame
388        * is changing.
389        */
390       if (stl->storage->is_playing == true) {
391         obact_gpd->flag |= GP_DATA_CACHE_IS_DIRTY;
392       }
393       /* if render, set as dirty to update all data */
394       else if (stl->storage->is_render == true) {
395         obact_gpd->flag |= GP_DATA_CACHE_IS_DIRTY;
396       }
397     }
398
399     /* save simplify flags (can change while drawing, so it's better to save) */
400     stl->storage->simplify_fill = GP_SIMPLIFY_FILL(scene, stl->storage->is_playing);
401     stl->storage->simplify_modif = GP_SIMPLIFY_MODIF(scene, stl->storage->is_playing);
402     stl->storage->simplify_fx = GP_SIMPLIFY_FX(scene, stl->storage->is_playing);
403     stl->storage->simplify_blend = GP_SIMPLIFY_BLEND(scene, stl->storage->is_playing);
404
405     /* xray mode */
406     if (v3d) {
407       stl->storage->is_xray = XRAY_ACTIVE(v3d);
408     }
409     else {
410       stl->storage->is_xray = 0;
411     }
412
413     /* save pixsize */
414     stl->storage->pixsize = DRW_viewport_pixelsize_get();
415     if ((!DRW_state_is_opengl_render()) && (stl->storage->is_render)) {
416       stl->storage->pixsize = &stl->storage->render_pixsize;
417     }
418
419     /* detect if painting session */
420     if ((obact_gpd) && (obact_gpd->flag & GP_DATA_STROKE_PAINTMODE) &&
421         (stl->storage->is_playing == false)) {
422       /* need the original to avoid cow overhead while drawing */
423       bGPdata *gpd_orig = (bGPdata *)DEG_get_original_id(&obact_gpd->id);
424       if (((gpd_orig->runtime.sbuffer_sflag & GP_STROKE_ERASER) == 0) &&
425           (gpd_orig->runtime.sbuffer_size > 0) &&
426           ((gpd_orig->flag & GP_DATA_STROKE_POLYGON) == 0) && !DRW_state_is_depth()) {
427         stl->g_data->session_flag |= GP_DRW_PAINT_PAINTING;
428       }
429       else {
430         stl->g_data->session_flag = GP_DRW_PAINT_IDLE;
431       }
432     }
433     else {
434       /* if not drawing mode */
435       stl->g_data->session_flag = GP_DRW_PAINT_HOLD;
436     }
437
438     if (gp_style) {
439       stl->storage->stroke_style = gp_style->stroke_style;
440       stl->storage->color_type = GPENCIL_COLOR_SOLID;
441       if (gp_style->stroke_style == GP_STYLE_STROKE_STYLE_TEXTURE) {
442         stl->storage->color_type = GPENCIL_COLOR_TEXTURE;
443         if (gp_style->flag & GP_STYLE_STROKE_PATTERN) {
444           stl->storage->color_type = GPENCIL_COLOR_PATTERN;
445         }
446       }
447     }
448     else {
449       stl->storage->stroke_style = GP_STYLE_STROKE_STYLE_SOLID;
450       stl->storage->color_type = GPENCIL_COLOR_SOLID;
451     }
452
453     /* drawing buffer pass for drawing the stroke that is being drawing by the user. The data
454      * is stored in sbuffer
455      */
456     psl->drawing_pass = DRW_pass_create("GPencil Drawing Pass",
457                                         DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND_ALPHA |
458                                             DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_ALWAYS);
459
460     /* full screen pass to combine the result with default framebuffer */
461     struct GPUBatch *quad = DRW_cache_fullscreen_quad_get();
462     psl->mix_pass = DRW_pass_create("GPencil Mix Pass",
463                                     DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND_ALPHA |
464                                         DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS);
465     DRWShadingGroup *mix_shgrp = DRW_shgroup_create(e_data.gpencil_fullscreen_sh, psl->mix_pass);
466     DRW_shgroup_call(mix_shgrp, quad, NULL);
467     DRW_shgroup_uniform_texture_ref(mix_shgrp, "strokeColor", &e_data.input_color_tx);
468     DRW_shgroup_uniform_texture_ref(mix_shgrp, "strokeDepth", &e_data.input_depth_tx);
469     DRW_shgroup_uniform_int(mix_shgrp, "tonemapping", &stl->storage->tonemapping, 1);
470     DRW_shgroup_uniform_int(mix_shgrp, "do_select", &stl->storage->do_select_outline, 1);
471     DRW_shgroup_uniform_vec4(mix_shgrp, "select_color", stl->storage->select_color, 1);
472
473     /* mix pass no blend used to copy between passes. A separated pass is required
474      * because if mix_pass is used, the acumulation of blend degrade the colors.
475      *
476      * This pass is used too to take the snapshot used for background_pass. This image
477      * will be used as the background while the user is drawing.
478      */
479     psl->mix_pass_noblend = DRW_pass_create("GPencil Mix Pass no blend",
480                                             DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH |
481                                                 DRW_STATE_DEPTH_LESS);
482     DRWShadingGroup *mix_shgrp_noblend = DRW_shgroup_create(e_data.gpencil_fullscreen_sh,
483                                                             psl->mix_pass_noblend);
484     DRW_shgroup_call(mix_shgrp_noblend, quad, NULL);
485     DRW_shgroup_uniform_texture_ref(mix_shgrp_noblend, "strokeColor", &e_data.input_color_tx);
486     DRW_shgroup_uniform_texture_ref(mix_shgrp_noblend, "strokeDepth", &e_data.input_depth_tx);
487     DRW_shgroup_uniform_int(mix_shgrp_noblend, "tonemapping", &stl->storage->tonemapping, 1);
488     DRW_shgroup_uniform_int(mix_shgrp_noblend, "do_select", &stl->storage->do_select_outline, 1);
489     DRW_shgroup_uniform_vec4(mix_shgrp_noblend, "select_color", stl->storage->select_color, 1);
490
491     /* Painting session pass (used only to speedup while the user is drawing )
492      * This pass is used to show the snapshot of the current grease pencil strokes captured
493      * when the user starts to draw (see comments above).
494      * In this way, the previous strokes don't need to be redraw and the drawing process
495      * is far to agile.
496      */
497     psl->background_pass = DRW_pass_create("GPencil Background Painting Session Pass",
498                                            DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND_ALPHA |
499                                                DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS);
500     DRWShadingGroup *background_shgrp = DRW_shgroup_create(e_data.gpencil_background_sh,
501                                                            psl->background_pass);
502     DRW_shgroup_call(background_shgrp, quad, NULL);
503     DRW_shgroup_uniform_texture_ref(background_shgrp, "strokeColor", &e_data.background_color_tx);
504     DRW_shgroup_uniform_texture_ref(background_shgrp, "strokeDepth", &e_data.background_depth_tx);
505
506     /* pass for drawing paper (only if viewport)
507      * In render, the v3d is null so the paper is disabled
508      * The paper is way to isolate the drawing in complex scene and to have a cleaner
509      * drawing area.
510      */
511     if (v3d) {
512       psl->paper_pass = DRW_pass_create("GPencil Paper Pass",
513                                         DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND_ALPHA);
514       DRWShadingGroup *paper_shgrp = DRW_shgroup_create(e_data.gpencil_paper_sh, psl->paper_pass);
515       DRW_shgroup_call(paper_shgrp, quad, NULL);
516       DRW_shgroup_uniform_vec3(paper_shgrp, "color", v3d->shading.background_color, 1);
517       DRW_shgroup_uniform_float(paper_shgrp, "opacity", &v3d->overlay.gpencil_paper_opacity, 1);
518     }
519
520     /* grid pass */
521     if (v3d) {
522       psl->grid_pass = DRW_pass_create("GPencil Grid Pass",
523                                        DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND_ALPHA |
524                                            DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_ALWAYS);
525       stl->g_data->shgrps_grid = DRW_shgroup_create(e_data.gpencil_line_sh, psl->grid_pass);
526     }
527
528     /* blend layers pass */
529     psl->blend_pass = DRW_pass_create("GPencil Blend Layers Pass",
530                                       DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND_ALPHA |
531                                           DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS);
532     DRWShadingGroup *blend_shgrp = DRW_shgroup_create(e_data.gpencil_blend_fullscreen_sh,
533                                                       psl->blend_pass);
534     DRW_shgroup_call(blend_shgrp, quad, NULL);
535     DRW_shgroup_uniform_texture_ref(blend_shgrp, "strokeColor", &e_data.temp_color_tx_a);
536     DRW_shgroup_uniform_texture_ref(blend_shgrp, "strokeDepth", &e_data.temp_depth_tx_a);
537     DRW_shgroup_uniform_texture_ref(blend_shgrp, "blendColor", &e_data.temp_color_tx_fx);
538     DRW_shgroup_uniform_texture_ref(blend_shgrp, "blendDepth", &e_data.temp_depth_tx_fx);
539     DRW_shgroup_uniform_int(blend_shgrp, "mode", &stl->storage->blend_mode, 1);
540     DRW_shgroup_uniform_int(blend_shgrp, "clamp_layer", &stl->storage->clamp_layer, 1);
541     DRW_shgroup_uniform_float(blend_shgrp, "blend_opacity", &stl->storage->blend_opacity, 1);
542     DRW_shgroup_uniform_int(mix_shgrp, "tonemapping", &stl->storage->tonemapping, 1);
543
544     /* create effects passes */
545     if (!stl->storage->simplify_fx) {
546       GPENCIL_create_fx_passes(psl);
547     }
548   }
549 }
550
551 static void gpencil_add_draw_data(void *vedata, Object *ob)
552 {
553   GPENCIL_StorageList *stl = ((GPENCIL_Data *)vedata)->stl;
554   bGPdata *gpd = (bGPdata *)ob->data;
555   const bool is_multiedit = (bool)GPENCIL_MULTIEDIT_SESSIONS_ON(gpd);
556
557   int i = stl->g_data->gp_cache_used - 1;
558   tGPencilObjectCache *cache_ob = &stl->g_data->gp_object_cache[i];
559
560   if (!cache_ob->is_dup_ob) {
561     /* fill shading groups */
562     if ((!is_multiedit) || (stl->storage->is_render)) {
563       DRW_gpencil_populate_datablock(&e_data, vedata, ob, cache_ob);
564     }
565     else {
566       DRW_gpencil_populate_multiedit(&e_data, vedata, ob, cache_ob);
567     }
568   }
569
570   /* FX passses */
571   cache_ob->has_fx = false;
572   if ((!stl->storage->simplify_fx) && (!ELEM(cache_ob->shading_type[0], OB_WIRE, OB_SOLID)) &&
573       (BKE_shaderfx_has_gpencil(ob))) {
574     cache_ob->has_fx = true;
575     if ((!stl->storage->simplify_fx) && (!is_multiedit)) {
576       DRW_gpencil_fx_prepare(&e_data, vedata, cache_ob);
577     }
578   }
579 }
580
581 void GPENCIL_cache_populate(void *vedata, Object *ob)
582 {
583   /* object must be visible */
584   if (!(DRW_object_visibility_in_active_context(ob) & OB_VISIBLE_SELF)) {
585     return;
586   }
587
588   GPENCIL_StorageList *stl = ((GPENCIL_Data *)vedata)->stl;
589   const DRWContextState *draw_ctx = DRW_context_state_get();
590   Scene *scene = draw_ctx->scene;
591   ToolSettings *ts = scene->toolsettings;
592   View3D *v3d = draw_ctx->v3d;
593   const View3DCursor *cursor = &scene->cursor;
594   float grid_matrix[4][4];
595
596   if (ob->type == OB_GPENCIL && ob->data) {
597     bGPdata *gpd = (bGPdata *)ob->data;
598
599     /* enable multisample and basic framebuffer creation */
600     stl->storage->framebuffer_flag |= GP_FRAMEBUFFER_MULTISAMPLE;
601     stl->storage->framebuffer_flag |= GP_FRAMEBUFFER_BASIC;
602
603     /* when start/stop animation the cache must be set as dirty to reset all data */
604     if (stl->storage->reset_cache) {
605       gpd->flag |= GP_DATA_CACHE_IS_DIRTY;
606       stl->storage->reset_cache = false;
607     }
608
609     if ((stl->g_data->session_flag & GP_DRW_PAINT_READY) == 0) {
610       /* bound box object are not visible, only external box*/
611       if (ob->dt != OB_BOUNDBOX) {
612         /* save gp objects for drawing later */
613         stl->g_data->gp_object_cache = gpencil_object_cache_add(stl->g_data->gp_object_cache,
614                                                                 ob,
615                                                                 &stl->g_data->gp_cache_size,
616                                                                 &stl->g_data->gp_cache_used);
617
618         /* enable instance loop */
619         if (!stl->g_data->do_instances) {
620           tGPencilObjectCache *cache_ob =
621               &stl->g_data->gp_object_cache[stl->g_data->gp_cache_used - 1];
622           stl->g_data->do_instances = cache_ob->is_dup_ob;
623         }
624
625         /* load drawing data */
626         gpencil_add_draw_data(vedata, ob);
627       }
628     }
629
630     /* draw current painting strokes
631      * (only if region is equal to originated paint region)
632      *
633      * Need to use original data because to use the copy of data, the paint
634      * operator must update depsgraph and this makes that first events of the
635      * mouse are missed if the datablock is very big due the time required to
636      * copy the datablock. The search of the original data is faster than a
637      * full datablock copy.
638      * Using the original data doesn't require a copy and the feel when drawing
639      * is far better.
640      */
641
642     bGPdata *gpd_orig = (bGPdata *)DEG_get_original_id(&gpd->id);
643     if ((draw_ctx->obact == ob) &&
644         ((gpd_orig->runtime.ar == NULL) || (gpd_orig->runtime.ar == draw_ctx->ar))) {
645       DRW_gpencil_populate_buffer_strokes(&e_data, vedata, ts, ob);
646     }
647
648     /* grid */
649     if ((v3d) && ((v3d->flag2 & V3D_HIDE_OVERLAYS) == 0) && (v3d->gp_flag & V3D_GP_SHOW_GRID) &&
650         (ob->type == OB_GPENCIL) && (ob == draw_ctx->obact) &&
651         ((ts->gpencil_v3d_align & GP_PROJECT_DEPTH_VIEW) == 0) &&
652         ((ts->gpencil_v3d_align & GP_PROJECT_DEPTH_STROKE) == 0)) {
653       GPU_BATCH_DISCARD_SAFE(e_data.batch_grid);
654       MEM_SAFE_FREE(e_data.batch_grid);
655
656       e_data.batch_grid = DRW_gpencil_get_grid(ob);
657
658       /* define grid orientation */
659       switch (ts->gp_sculpt.lock_axis) {
660         case GP_LOCKAXIS_VIEW: {
661           /* align always to view */
662           invert_m4_m4(grid_matrix, draw_ctx->rv3d->viewmat);
663           /* copy ob location */
664           copy_v3_v3(grid_matrix[3], ob->obmat[3]);
665           break;
666         }
667         case GP_LOCKAXIS_CURSOR: {
668           float scale[3] = {1.0f, 1.0f, 1.0f};
669           loc_eul_size_to_mat4(grid_matrix, cursor->location, cursor->rotation_euler, scale);
670           break;
671         }
672         default: {
673           copy_m4_m4(grid_matrix, ob->obmat);
674           break;
675         }
676       }
677
678       /* Move the origin to Object or Cursor */
679       if (ts->gpencil_v3d_align & GP_PROJECT_CURSOR) {
680         copy_v3_v3(grid_matrix[3], cursor->location);
681       }
682       else {
683         copy_v3_v3(grid_matrix[3], ob->obmat[3]);
684       }
685
686       DRW_shgroup_call(stl->g_data->shgrps_grid, e_data.batch_grid, grid_matrix);
687     }
688   }
689 }
690
691 void GPENCIL_cache_finish(void *vedata)
692 {
693   GPENCIL_StorageList *stl = ((GPENCIL_Data *)vedata)->stl;
694   tGPencilObjectCache *cache_ob = NULL;
695   Object *ob = NULL;
696
697   /* create data for instances */
698   if (stl->g_data->do_instances) {
699     GHash *gh_objects = BLI_ghash_str_new(__func__);
700     /* create hash of real object (non duplicated) */
701     for (int i = 0; i < stl->g_data->gp_cache_used; i++) {
702       cache_ob = &stl->g_data->gp_object_cache[i];
703       if (!cache_ob->is_dup_ob) {
704         ob = cache_ob->ob;
705         char *name = BKE_id_to_unique_string_key(&ob->id);
706         BLI_ghash_insert(gh_objects, name, cache_ob->ob);
707       }
708     }
709
710     /* draw particles */
711     DRW_gpencil_populate_particles(&e_data, gh_objects, vedata);
712
713     /* free hash */
714     BLI_ghash_free(gh_objects, MEM_freeN, NULL);
715   }
716
717   if (stl->g_data->session_flag & (GP_DRW_PAINT_IDLE | GP_DRW_PAINT_FILLING)) {
718     stl->storage->framebuffer_flag |= GP_FRAMEBUFFER_DRAW;
719   }
720
721   /* create framebuffers (only for normal drawing) */
722   if (!DRW_state_is_select() || !DRW_state_is_depth()) {
723     GPENCIL_create_framebuffers(vedata);
724   }
725 }
726
727 /* helper function to sort inverse gpencil objects using qsort */
728 static int gpencil_object_cache_compare_zdepth(const void *a1, const void *a2)
729 {
730   const tGPencilObjectCache *ps1 = a1, *ps2 = a2;
731
732   if (ps1->zdepth < ps2->zdepth) {
733     return 1;
734   }
735   else if (ps1->zdepth > ps2->zdepth) {
736     return -1;
737   }
738
739   return 0;
740 }
741
742 /* prepare a texture with full viewport screenshot for fast drawing */
743 static void gpencil_prepare_fast_drawing(GPENCIL_StorageList *stl,
744                                          DefaultFramebufferList *dfbl,
745                                          GPENCIL_FramebufferList *fbl,
746                                          DRWPass *pass,
747                                          const float clearcol[4])
748 {
749   if (stl->g_data->session_flag & (GP_DRW_PAINT_IDLE | GP_DRW_PAINT_FILLING)) {
750     GPU_framebuffer_bind(fbl->background_fb);
751     /* clean only in first loop cycle */
752     if (stl->g_data->session_flag & GP_DRW_PAINT_IDLE) {
753       GPU_framebuffer_clear_color_depth(fbl->background_fb, clearcol, 1.0f);
754       stl->g_data->session_flag = GP_DRW_PAINT_FILLING;
755     }
756     /* repeat pass to fill temp texture */
757     DRW_draw_pass(pass);
758     /* set default framebuffer again */
759     GPU_framebuffer_bind(dfbl->default_fb);
760   }
761 }
762
763 static void gpencil_free_obj_runtime(GPENCIL_StorageList *stl)
764 {
765   if (stl->g_data->gp_object_cache == NULL) {
766     return;
767   }
768
769   /* reset all cache flags */
770   for (int i = 0; i < stl->g_data->gp_cache_used; i++) {
771     tGPencilObjectCache *cache_ob = &stl->g_data->gp_object_cache[i];
772     if (cache_ob) {
773       bGPdata *gpd = cache_ob->gpd;
774       gpd->flag &= ~GP_DATA_CACHE_IS_DIRTY;
775
776       /* free shgrp array */
777       cache_ob->tot_layers = 0;
778       MEM_SAFE_FREE(cache_ob->name);
779       MEM_SAFE_FREE(cache_ob->shgrp_array);
780     }
781   }
782
783   /* free the cache itself */
784   MEM_SAFE_FREE(stl->g_data->gp_object_cache);
785 }
786
787 static void gpencil_draw_pass_range(GPENCIL_FramebufferList *fbl,
788                                     GPENCIL_StorageList *stl,
789                                     GPENCIL_PassList *psl,
790                                     GPENCIL_TextureList *txl,
791                                     GPUFrameBuffer *fb,
792                                     Object *ob,
793                                     bGPdata *gpd,
794                                     DRWShadingGroup *init_shgrp,
795                                     DRWShadingGroup *end_shgrp,
796                                     bool multi)
797 {
798   if (init_shgrp == NULL) {
799     return;
800   }
801
802   /* previews don't use AA */
803   if ((!stl->storage->is_mat_preview) && (multi)) {
804     MULTISAMPLE_GP_SYNC_ENABLE(stl->storage->multisamples, fbl);
805   }
806
807   DRW_draw_pass_subset(GPENCIL_3D_DRAWMODE(ob, gpd) ? psl->stroke_pass_3d : psl->stroke_pass_2d,
808                        init_shgrp,
809                        end_shgrp);
810
811   if ((!stl->storage->is_mat_preview) && (multi)) {
812     MULTISAMPLE_GP_SYNC_DISABLE(stl->storage->multisamples, fbl, fb, txl);
813   }
814 }
815
816 /* draw strokes to use for selection */
817 static void drw_gpencil_select_render(GPENCIL_StorageList *stl, GPENCIL_PassList *psl)
818 {
819   tGPencilObjectCache *cache_ob;
820   tGPencilObjectCache_shgrp *array_elm = NULL;
821   DRWShadingGroup *init_shgrp = NULL;
822   DRWShadingGroup *end_shgrp = NULL;
823
824   /* Draw all pending objects */
825   if ((stl->g_data->gp_cache_used > 0) && (stl->g_data->gp_object_cache)) {
826     /* sort by zdepth */
827     qsort(stl->g_data->gp_object_cache,
828           stl->g_data->gp_cache_used,
829           sizeof(tGPencilObjectCache),
830           gpencil_object_cache_compare_zdepth);
831
832     for (int i = 0; i < stl->g_data->gp_cache_used; i++) {
833       cache_ob = &stl->g_data->gp_object_cache[i];
834       if (cache_ob) {
835         Object *ob = cache_ob->ob;
836         bGPdata *gpd = cache_ob->gpd;
837         init_shgrp = NULL;
838         if (cache_ob->tot_layers > 0) {
839           for (int e = 0; e < cache_ob->tot_layers; e++) {
840             array_elm = &cache_ob->shgrp_array[e];
841             if (init_shgrp == NULL) {
842               init_shgrp = array_elm->init_shgrp;
843             }
844             end_shgrp = array_elm->end_shgrp;
845           }
846           /* draw group */
847           DRW_draw_pass_subset(GPENCIL_3D_DRAWMODE(ob, gpd) ? psl->stroke_pass_3d :
848                                                               psl->stroke_pass_2d,
849                                init_shgrp,
850                                end_shgrp);
851         }
852         /* the cache must be dirty for next loop */
853         gpd->flag |= GP_DATA_CACHE_IS_DIRTY;
854       }
855     }
856   }
857 }
858
859 /* draw scene */
860 void GPENCIL_draw_scene(void *ved)
861 {
862   GPENCIL_Data *vedata = (GPENCIL_Data *)ved;
863   GPENCIL_StorageList *stl = ((GPENCIL_Data *)vedata)->stl;
864
865   GPENCIL_PassList *psl = ((GPENCIL_Data *)vedata)->psl;
866   GPENCIL_FramebufferList *fbl = ((GPENCIL_Data *)vedata)->fbl;
867   DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
868   GPENCIL_TextureList *txl = ((GPENCIL_Data *)vedata)->txl;
869
870   tGPencilObjectCache *cache_ob;
871   tGPencilObjectCache_shgrp *array_elm = NULL;
872   DRWShadingGroup *init_shgrp = NULL;
873   DRWShadingGroup *end_shgrp = NULL;
874
875   const float clearcol[4] = {0.0f, 0.0f, 0.0f, 0.0f};
876
877   const DRWContextState *draw_ctx = DRW_context_state_get();
878   View3D *v3d = draw_ctx->v3d;
879   Object *obact = draw_ctx->obact;
880   const bool playing = stl->storage->is_playing;
881   const bool is_render = stl->storage->is_render;
882   bGPdata *gpd_act = (obact) && (obact->type == OB_GPENCIL) ? (bGPdata *)obact->data : NULL;
883   const bool is_edit = GPENCIL_ANY_EDIT_MODE(gpd_act);
884   const bool overlay = v3d != NULL ? (bool)((v3d->flag2 & V3D_HIDE_OVERLAYS) == 0) : true;
885
886   /* if the draw is for select, do a basic drawing and return */
887   if (DRW_state_is_select() || DRW_state_is_depth()) {
888     drw_gpencil_select_render(stl, psl);
889     /* free memory */
890     gpencil_free_obj_runtime(stl);
891     return;
892   }
893
894   /* paper pass to display a comfortable area to draw over complex scenes with geometry */
895   if ((!is_render) && (obact) && (obact->type == OB_GPENCIL)) {
896     if (((v3d->flag2 & V3D_HIDE_OVERLAYS) == 0) && (v3d->gp_flag & V3D_GP_SHOW_PAPER)) {
897       DRW_draw_pass(psl->paper_pass);
898     }
899   }
900
901   /* if we have a painting session, we use fast viewport drawing method */
902   if ((!is_render) && (stl->g_data->session_flag & GP_DRW_PAINT_PAINTING)) {
903     GPU_framebuffer_bind(dfbl->default_fb);
904
905     MULTISAMPLE_GP_SYNC_ENABLE(stl->storage->multisamples, fbl);
906     if (obact->dt != OB_BOUNDBOX) {
907       DRW_draw_pass(psl->background_pass);
908     }
909     DRW_draw_pass(psl->drawing_pass);
910
911     MULTISAMPLE_GP_SYNC_DISABLE(stl->storage->multisamples, fbl, dfbl->default_fb, txl);
912
913     /* free memory */
914     gpencil_free_obj_runtime(stl);
915
916     /* grid pass */
917     if ((!is_render) && (obact) && (obact->type == OB_GPENCIL)) {
918       if (((v3d->flag2 & V3D_HIDE_OVERLAYS) == 0) && (v3d->gp_flag & V3D_GP_SHOW_GRID)) {
919         DRW_draw_pass(psl->grid_pass);
920       }
921     }
922     return;
923   }
924
925   if (DRW_state_is_fbo()) {
926
927     /* Draw all pending objects */
928     if (stl->g_data->gp_cache_used > 0) {
929       /* sort by zdepth */
930       qsort(stl->g_data->gp_object_cache,
931             stl->g_data->gp_cache_used,
932             sizeof(tGPencilObjectCache),
933             gpencil_object_cache_compare_zdepth);
934
935       for (int i = 0; i < stl->g_data->gp_cache_used; i++) {
936         cache_ob = &stl->g_data->gp_object_cache[i];
937         Object *ob = cache_ob->ob;
938         bGPdata *gpd = cache_ob->gpd;
939         init_shgrp = NULL;
940         /* Render stroke in separated framebuffer */
941         GPU_framebuffer_bind(fbl->temp_fb_a);
942         GPU_framebuffer_clear_color_depth(fbl->temp_fb_a, clearcol, 1.0f);
943
944         /* Stroke Pass:
945          * draw only a subset that usually starts with a fill and ends with stroke
946          */
947         bool use_blend = false;
948         if (cache_ob->tot_layers > 0) {
949           for (int e = 0; e < cache_ob->tot_layers; e++) {
950             bool is_last = (e == cache_ob->tot_layers - 1) ? true : false;
951             array_elm = &cache_ob->shgrp_array[e];
952
953             if (((array_elm->mode == eGplBlendMode_Normal) && (!use_blend) &&
954                  (!array_elm->clamp_layer)) ||
955                 (e == 0)) {
956               if (init_shgrp == NULL) {
957                 init_shgrp = array_elm->init_shgrp;
958               }
959               end_shgrp = array_elm->end_shgrp;
960             }
961             else {
962               use_blend = true;
963               /* draw pending groups */
964               gpencil_draw_pass_range(
965                   fbl, stl, psl, txl, fbl->temp_fb_a, ob, gpd, init_shgrp, end_shgrp, is_last);
966
967               /* draw current group in separated texture */
968               init_shgrp = array_elm->init_shgrp;
969               end_shgrp = array_elm->end_shgrp;
970
971               GPU_framebuffer_bind(fbl->temp_fb_fx);
972               GPU_framebuffer_clear_color_depth(fbl->temp_fb_fx, clearcol, 1.0f);
973               gpencil_draw_pass_range(
974                   fbl, stl, psl, txl, fbl->temp_fb_fx, ob, gpd, init_shgrp, end_shgrp, is_last);
975
976               /* Blend A texture and FX texture */
977               GPU_framebuffer_bind(fbl->temp_fb_b);
978               GPU_framebuffer_clear_color_depth(fbl->temp_fb_b, clearcol, 1.0f);
979               stl->storage->blend_mode = array_elm->mode;
980               stl->storage->clamp_layer = (int)array_elm->clamp_layer;
981               stl->storage->blend_opacity = array_elm->blend_opacity;
982               stl->storage->tonemapping = stl->storage->is_render ? 1 : 0;
983               DRW_draw_pass(psl->blend_pass);
984               stl->storage->tonemapping = 0;
985
986               /* Copy B texture to A texture to follow loop */
987               e_data.input_depth_tx = e_data.temp_depth_tx_b;
988               e_data.input_color_tx = e_data.temp_color_tx_b;
989
990               GPU_framebuffer_bind(fbl->temp_fb_a);
991               GPU_framebuffer_clear_color_depth(fbl->temp_fb_a, clearcol, 1.0f);
992               DRW_draw_pass(psl->mix_pass_noblend);
993
994               /* prepare next group */
995               init_shgrp = NULL;
996             }
997           }
998           /* last group */
999           gpencil_draw_pass_range(
1000               fbl, stl, psl, txl, fbl->temp_fb_a, ob, gpd, init_shgrp, end_shgrp, true);
1001         }
1002
1003         /* Current buffer drawing */
1004         if ((!is_render) && (cache_ob->is_dup_ob == false)) {
1005           DRW_draw_pass(psl->drawing_pass);
1006         }
1007         /* fx passes */
1008         if (cache_ob->has_fx == true) {
1009           stl->storage->tonemapping = 0;
1010           DRW_gpencil_fx_draw(&e_data, vedata, cache_ob);
1011         }
1012
1013         e_data.input_depth_tx = e_data.temp_depth_tx_a;
1014         e_data.input_color_tx = e_data.temp_color_tx_a;
1015
1016         /* Combine with scene buffer */
1017         if ((!is_render) || (fbl->main == NULL)) {
1018           GPU_framebuffer_bind(dfbl->default_fb);
1019         }
1020         else {
1021           GPU_framebuffer_bind(fbl->main);
1022         }
1023         /* tonemapping */
1024         stl->storage->tonemapping = is_render ? 1 : 0;
1025         /* active select flag and selection color */
1026         if (!is_render) {
1027           UI_GetThemeColorShadeAlpha4fv(
1028               (ob == draw_ctx->obact) ? TH_ACTIVE : TH_SELECT, 0, -40, stl->storage->select_color);
1029         }
1030         stl->storage->do_select_outline = ((overlay) && (ob->base_flag & BASE_SELECTED) &&
1031                                            (ob->mode == OB_MODE_OBJECT) && (!is_render) &&
1032                                            (!playing) && (v3d->flag & V3D_SELECT_OUTLINE));
1033
1034         /* if active object is not object mode, disable for all objects */
1035         if ((stl->storage->do_select_outline) && (draw_ctx->obact) &&
1036             (draw_ctx->obact->mode != OB_MODE_OBJECT)) {
1037           stl->storage->do_select_outline = 0;
1038         }
1039
1040         /* draw mix pass */
1041         DRW_draw_pass(psl->mix_pass);
1042
1043         /* disable select flag */
1044         stl->storage->do_select_outline = 0;
1045
1046         /* prepare for fast drawing */
1047         if (!is_render) {
1048           if (!playing) {
1049             gpencil_prepare_fast_drawing(stl, dfbl, fbl, psl->mix_pass_noblend, clearcol);
1050           }
1051         }
1052         else {
1053           /* if render, the cache must be dirty for next loop */
1054           gpd->flag |= GP_DATA_CACHE_IS_DIRTY;
1055         }
1056       }
1057       /* edit points */
1058       if ((!is_render) && (!playing) && (is_edit)) {
1059         DRW_draw_pass(psl->edit_pass);
1060       }
1061     }
1062     /* grid pass */
1063     if ((!is_render) && (obact) && (obact->type == OB_GPENCIL)) {
1064       if (((v3d->flag2 & V3D_HIDE_OVERLAYS) == 0) && (v3d->gp_flag & V3D_GP_SHOW_GRID)) {
1065         DRW_draw_pass(psl->grid_pass);
1066       }
1067     }
1068   }
1069   /* free memory */
1070   gpencil_free_obj_runtime(stl);
1071
1072   /* reset  */
1073   if (DRW_state_is_fbo()) {
1074     /* attach again default framebuffer */
1075     if (!is_render) {
1076       GPU_framebuffer_bind(dfbl->default_fb);
1077     }
1078
1079     /* the temp texture is ready. Now we can use fast screen drawing */
1080     if (stl->g_data->session_flag & GP_DRW_PAINT_FILLING) {
1081       stl->g_data->session_flag = GP_DRW_PAINT_READY;
1082     }
1083   }
1084 }
1085
1086 static const DrawEngineDataSize GPENCIL_data_size = DRW_VIEWPORT_DATA_SIZE(GPENCIL_Data);
1087
1088 DrawEngineType draw_engine_gpencil_type = {
1089     NULL,
1090     NULL,
1091     N_("GpencilMode"),
1092     &GPENCIL_data_size,
1093     &GPENCIL_engine_init,
1094     &GPENCIL_engine_free,
1095     &GPENCIL_cache_init,
1096     &GPENCIL_cache_populate,
1097     &GPENCIL_cache_finish,
1098     NULL,
1099     &GPENCIL_draw_scene,
1100     NULL,
1101     NULL,
1102     &GPENCIL_render_to_image,
1103 };