a7141ff93bbcf30d4746fbd61913a1e450d53168
[blender.git] / source / blender / draw / engines / gpencil / gpencil_engine.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_engine.c
23  *  \ingroup draw
24  */
25 #include "DRW_engine.h"
26 #include "DRW_render.h"
27
28 #include "BKE_camera.h"
29 #include "BKE_object.h"
30 #include "BKE_paint.h"
31 #include "BKE_gpencil.h"
32 #include "BKE_shader_fx.h"
33
34 #include "DNA_gpencil_types.h"
35 #include "DNA_view3d_types.h"
36
37 #include "draw_mode_engines.h"
38
39 #include "UI_resources.h"
40
41 #include "GPU_texture.h"
42
43 #include "gpencil_engine.h"
44
45 #include "DEG_depsgraph_query.h"
46
47 #include "ED_screen.h"
48 #include "ED_gpencil.h"
49
50 #include "WM_api.h"
51
52 extern char datatoc_gpencil_fill_vert_glsl[];
53 extern char datatoc_gpencil_fill_frag_glsl[];
54 extern char datatoc_gpencil_stroke_vert_glsl[];
55 extern char datatoc_gpencil_stroke_geom_glsl[];
56 extern char datatoc_gpencil_stroke_frag_glsl[];
57 extern char datatoc_gpencil_zdepth_mix_frag_glsl[];
58 extern char datatoc_gpencil_simple_mix_frag_glsl[];
59 extern char datatoc_gpencil_point_vert_glsl[];
60 extern char datatoc_gpencil_point_geom_glsl[];
61 extern char datatoc_gpencil_point_frag_glsl[];
62 extern char datatoc_gpencil_background_frag_glsl[];
63 extern char datatoc_gpencil_paper_frag_glsl[];
64 extern char datatoc_gpencil_edit_point_vert_glsl[];
65 extern char datatoc_gpencil_edit_point_geom_glsl[];
66 extern char datatoc_gpencil_edit_point_frag_glsl[];
67 extern char datatoc_gpencil_blend_frag_glsl[];
68
69 /* *********** STATIC *********** */
70 static GPENCIL_e_data e_data = {NULL}; /* Engine data */
71
72 /* *********** FUNCTIONS *********** */
73
74 /* create a multisample buffer if not present */
75 void DRW_gpencil_multisample_ensure(GPENCIL_Data *vedata, int rect_w, int rect_h)
76 {
77         GPENCIL_FramebufferList *fbl = vedata->fbl;
78         GPENCIL_StorageList *stl = ((GPENCIL_Data *)vedata)->stl;
79         GPENCIL_TextureList *txl = ((GPENCIL_Data *)vedata)->txl;
80
81         short samples = stl->storage->multisamples;
82
83         if (samples > 0) {
84                 if (!fbl->multisample_fb) {
85                         fbl->multisample_fb = GPU_framebuffer_create();
86                         if (fbl->multisample_fb) {
87                                 if (txl->multisample_color == NULL) {
88                                         txl->multisample_color = GPU_texture_create_2D_multisample(
89                                                 rect_w, rect_h, GPU_RGBA16F, NULL, samples, NULL);
90                                 }
91                                 if (txl->multisample_depth == NULL) {
92                                         txl->multisample_depth = GPU_texture_create_2D_multisample(
93                                                 rect_w, rect_h, GPU_DEPTH24_STENCIL8, NULL, samples, NULL);
94                                 }
95                                 GPU_framebuffer_ensure_config(
96                                         &fbl->multisample_fb, {
97                                             GPU_ATTACHMENT_TEXTURE(txl->multisample_depth),
98                                             GPU_ATTACHMENT_TEXTURE(txl->multisample_color)
99                                         });
100                         }
101                 }
102         }
103 }
104
105 static void GPENCIL_create_framebuffers(void *vedata)
106 {
107         GPENCIL_FramebufferList *fbl = ((GPENCIL_Data *)vedata)->fbl;
108         GPENCIL_StorageList *stl = ((GPENCIL_Data *)vedata)->stl;
109
110         /* Go full 32bits for rendering */
111         GPUTextureFormat fb_format = DRW_state_is_image_render() ? GPU_RGBA32F : GPU_RGBA16F;
112
113         if (DRW_state_is_fbo()) {
114                 const float *viewport_size = DRW_viewport_size_get();
115                 const int size[2] = { (int)viewport_size[0], (int)viewport_size[1] };
116
117                 /* create multiframe framebuffer for AA */
118                 if (stl->storage->multisamples > 0) {
119                         DRW_gpencil_multisample_ensure(vedata, size[0], size[1]);
120                 }
121
122                 /* temp textures */
123                 e_data.temp_depth_tx_a = DRW_texture_pool_query_2D(size[0], size[1], GPU_DEPTH24_STENCIL8,
124                         &draw_engine_gpencil_type);
125                 e_data.temp_color_tx_a = DRW_texture_pool_query_2D(size[0], size[1], fb_format,
126                         &draw_engine_gpencil_type);
127                 GPU_framebuffer_ensure_config(
128                         &fbl->temp_fb_a, {
129                             GPU_ATTACHMENT_TEXTURE(e_data.temp_depth_tx_a),
130                             GPU_ATTACHMENT_TEXTURE(e_data.temp_color_tx_a)
131                         });
132
133                 e_data.temp_depth_tx_b = DRW_texture_pool_query_2D(
134                         size[0], size[1], GPU_DEPTH24_STENCIL8,
135                         &draw_engine_gpencil_type);
136                 e_data.temp_color_tx_b = DRW_texture_pool_query_2D(
137                         size[0], size[1], fb_format,
138                         &draw_engine_gpencil_type);
139                 GPU_framebuffer_ensure_config(
140                         &fbl->temp_fb_b, {
141                             GPU_ATTACHMENT_TEXTURE(e_data.temp_depth_tx_b),
142                             GPU_ATTACHMENT_TEXTURE(e_data.temp_color_tx_b)
143                         });
144
145                 /* used for rim and shadow FX effects */
146                 e_data.temp_depth_tx_fx = DRW_texture_pool_query_2D(
147                         size[0], size[1], GPU_DEPTH24_STENCIL8,
148                         &draw_engine_gpencil_type);
149                 e_data.temp_color_tx_fx = DRW_texture_pool_query_2D(
150                         size[0], size[1], fb_format,
151                         &draw_engine_gpencil_type);
152                 GPU_framebuffer_ensure_config(
153                         &fbl->temp_fb_fx, {
154                             GPU_ATTACHMENT_TEXTURE(e_data.temp_depth_tx_fx),
155                             GPU_ATTACHMENT_TEXTURE(e_data.temp_color_tx_fx),
156                         });
157
158                 /* background framebuffer to speed up drawing process (always 16 bits) */
159                 e_data.background_depth_tx = DRW_texture_pool_query_2D(
160                         size[0], size[1], GPU_DEPTH24_STENCIL8,
161                         &draw_engine_gpencil_type);
162                 e_data.background_color_tx = DRW_texture_pool_query_2D(
163                         size[0], size[1], GPU_RGBA32F,
164                         &draw_engine_gpencil_type);
165                 GPU_framebuffer_ensure_config(
166                         &fbl->background_fb, {
167                             GPU_ATTACHMENT_TEXTURE(e_data.background_depth_tx),
168                             GPU_ATTACHMENT_TEXTURE(e_data.background_color_tx)
169                         });
170         }
171 }
172
173 static void GPENCIL_create_shaders(void)
174 {
175         /* normal fill shader */
176         if (!e_data.gpencil_fill_sh) {
177                 e_data.gpencil_fill_sh = DRW_shader_create(
178                         datatoc_gpencil_fill_vert_glsl, NULL,
179                         datatoc_gpencil_fill_frag_glsl, NULL);
180         }
181
182         /* normal stroke shader using geometry to display lines (line mode) */
183         if (!e_data.gpencil_stroke_sh) {
184                 e_data.gpencil_stroke_sh = DRW_shader_create(
185                         datatoc_gpencil_stroke_vert_glsl,
186                         datatoc_gpencil_stroke_geom_glsl,
187                         datatoc_gpencil_stroke_frag_glsl,
188                         NULL);
189         }
190
191         /* dot/rectangle mode for normal strokes using geometry */
192         if (!e_data.gpencil_point_sh) {
193                 e_data.gpencil_point_sh = DRW_shader_create(
194                         datatoc_gpencil_point_vert_glsl,
195                         datatoc_gpencil_point_geom_glsl,
196                         datatoc_gpencil_point_frag_glsl,
197                         NULL);
198         }
199         /* used for edit points or strokes with one point only */
200         if (!e_data.gpencil_edit_point_sh) {
201                 e_data.gpencil_edit_point_sh = DRW_shader_create(
202                         datatoc_gpencil_edit_point_vert_glsl,
203                         datatoc_gpencil_edit_point_geom_glsl,
204                         datatoc_gpencil_edit_point_frag_glsl, NULL);
205         }
206
207         /* used for edit lines for edit modes */
208         if (!e_data.gpencil_line_sh) {
209                 e_data.gpencil_line_sh = GPU_shader_get_builtin_shader(GPU_SHADER_3D_FLAT_COLOR);
210         }
211
212         /* used to filling during drawing */
213         if (!e_data.gpencil_drawing_fill_sh) {
214                 e_data.gpencil_drawing_fill_sh = GPU_shader_get_builtin_shader(GPU_SHADER_3D_SMOOTH_COLOR);
215         }
216
217         /* full screen for mix zdepth*/
218         if (!e_data.gpencil_fullscreen_sh) {
219                 e_data.gpencil_fullscreen_sh = DRW_shader_create_fullscreen(datatoc_gpencil_zdepth_mix_frag_glsl, NULL);
220         }
221         if (!e_data.gpencil_simple_fullscreen_sh) {
222                 e_data.gpencil_simple_fullscreen_sh = DRW_shader_create_fullscreen(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(datatoc_gpencil_blend_frag_glsl, NULL);
228         }
229
230         /* shaders for use when drawing */
231         if (!e_data.gpencil_background_sh) {
232                 e_data.gpencil_background_sh = DRW_shader_create_fullscreen(datatoc_gpencil_background_frag_glsl, NULL);
233         }
234         if (!e_data.gpencil_paper_sh) {
235                 e_data.gpencil_paper_sh = DRW_shader_create_fullscreen(datatoc_gpencil_paper_frag_glsl, NULL);
236         }
237 }
238
239 void GPENCIL_engine_init(void *vedata)
240 {
241         GPENCIL_StorageList *stl = ((GPENCIL_Data *)vedata)->stl;
242         /* init storage */
243         if (!stl->storage) {
244                 stl->storage = MEM_callocN(sizeof(GPENCIL_Storage), "GPENCIL_Storage");
245
246                 /* unit matrix */
247                 unit_m4(stl->storage->unit_matrix);
248         }
249
250         stl->storage->multisamples = U.gpencil_multisamples;
251
252         /* create framebuffers */
253         GPENCIL_create_framebuffers(vedata);
254
255         /* create shaders */
256         GPENCIL_create_shaders();
257         GPENCIL_create_fx_shaders(&e_data);
258
259         /* blank texture used if no texture defined for fill shader */
260         if (!e_data.gpencil_blank_texture) {
261                 float rect[16][16][4] = {{{0.0f}}};
262                 e_data.gpencil_blank_texture = DRW_texture_create_2D(16, 16, GPU_RGBA8, DRW_TEX_FILTER, (float *)rect);
263         }
264 }
265
266 static void GPENCIL_engine_free(void)
267 {
268         /* only free custom shaders, builtin shaders are freed in blender close */
269         DRW_SHADER_FREE_SAFE(e_data.gpencil_fill_sh);
270         DRW_SHADER_FREE_SAFE(e_data.gpencil_stroke_sh);
271         DRW_SHADER_FREE_SAFE(e_data.gpencil_point_sh);
272         DRW_SHADER_FREE_SAFE(e_data.gpencil_edit_point_sh);
273         DRW_SHADER_FREE_SAFE(e_data.gpencil_fullscreen_sh);
274         DRW_SHADER_FREE_SAFE(e_data.gpencil_simple_fullscreen_sh);
275         DRW_SHADER_FREE_SAFE(e_data.gpencil_blend_fullscreen_sh);
276         DRW_SHADER_FREE_SAFE(e_data.gpencil_background_sh);
277         DRW_SHADER_FREE_SAFE(e_data.gpencil_paper_sh);
278
279         DRW_TEXTURE_FREE_SAFE(e_data.gpencil_blank_texture);
280
281         GPU_BATCH_DISCARD_SAFE(e_data.batch_buffer_stroke);
282         MEM_SAFE_FREE(e_data.batch_buffer_stroke);
283
284         GPU_BATCH_DISCARD_SAFE(e_data.batch_buffer_fill);
285         MEM_SAFE_FREE(e_data.batch_buffer_fill);
286
287         GPU_BATCH_DISCARD_SAFE(e_data.batch_grid);
288         MEM_SAFE_FREE(e_data.batch_grid);
289
290         /* effects */
291         GPENCIL_delete_fx_shaders(&e_data);
292 }
293
294 void GPENCIL_cache_init(void *vedata)
295 {
296         GPENCIL_PassList *psl = ((GPENCIL_Data *)vedata)->psl;
297         GPENCIL_StorageList *stl = ((GPENCIL_Data *)vedata)->stl;
298         const DRWContextState *draw_ctx = DRW_context_state_get();
299         Scene *scene = draw_ctx->scene;
300         View3D *v3d = draw_ctx->v3d;
301
302         /* Special handling for when active object is GP object (e.g. for draw mode) */
303         Object *obact = draw_ctx->obact;
304         bGPdata *obact_gpd = NULL;
305         MaterialGPencilStyle *gp_style = NULL;
306
307         if (obact && (obact->type == OB_GPENCIL) && (obact->data)) {
308                 obact_gpd = (bGPdata *)obact->data;
309                 gp_style = BKE_material_gpencil_settings_get(obact, obact->actcol);
310         }
311
312         if (!stl->g_data) {
313                 /* Alloc transient pointers */
314                 stl->g_data = MEM_mallocN(sizeof(g_data), "g_data");
315                 stl->storage->xray = GP_XRAY_FRONT; /* used for drawing */
316                 stl->storage->stroke_style = GP_STYLE_STROKE_STYLE_SOLID; /* used for drawing */
317         }
318         stl->storage->tonemapping = 0;
319
320         stl->g_data->shgrps_edit_line = NULL;
321         stl->g_data->shgrps_edit_point = NULL;
322
323         if (!stl->shgroups) {
324                 /* Alloc maximum size because count strokes is very slow and can be very complex due onion skinning.
325                  */
326                 stl->shgroups = MEM_mallocN(sizeof(GPENCIL_shgroup) * GPENCIL_MAX_SHGROUPS, "GPENCIL_shgroup");
327         }
328
329         /* init gp objects cache */
330         stl->g_data->gp_cache_used = 0;
331         stl->g_data->gp_cache_size = 0;
332         stl->g_data->gp_object_cache = NULL;
333
334         {
335                 /* Stroke pass */
336                 psl->stroke_pass = DRW_pass_create(
337                         "GPencil Stroke Pass",
338                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_ALWAYS | DRW_STATE_BLEND);
339                 stl->storage->shgroup_id = 0;
340
341                 /* edit pass */
342                 psl->edit_pass = DRW_pass_create(
343                         "GPencil Edit Pass",
344                         DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND);
345
346                 /* detect if playing animation */
347                 if (draw_ctx->evil_C) {
348
349                         bool playing = ED_screen_animation_playing(CTX_wm_manager(draw_ctx->evil_C)) != NULL;
350                         if (playing != stl->storage->is_playing) {
351                                 stl->storage->reset_cache = true;
352                         }
353                         stl->storage->is_playing = playing;
354                 }
355                 else {
356                         stl->storage->is_playing = false;
357                         stl->storage->reset_cache = false;
358                 }
359                 /* save render state */
360                 stl->storage->is_render = DRW_state_is_image_render();
361                 stl->storage->is_mat_preview = (bool)stl->storage->is_render && STREQ(scene->id.name + 2, "preview");
362
363                 if (obact_gpd) {
364                         /* for some reason, when press play there is a delay in the animation flag check
365                          * and this produces errors. To be sure, we set cache as dirty because the frame
366                          * is changing.
367                          */
368                         if (stl->storage->is_playing == true) {
369                                 obact_gpd->flag |= GP_DATA_CACHE_IS_DIRTY;
370                         }
371                         /* if render, set as dirty to update all data */
372                         else if (stl->storage->is_render == true) {
373                                 obact_gpd->flag |= GP_DATA_CACHE_IS_DIRTY;
374                         }
375                 }
376
377                 /* save simplify flags (can change while drawing, so it's better to save) */
378                 stl->storage->simplify_fill = GP_SIMPLIFY_FILL(scene, stl->storage->is_playing);
379                 stl->storage->simplify_modif = GP_SIMPLIFY_MODIF(scene, stl->storage->is_playing);
380                 stl->storage->simplify_fx = GP_SIMPLIFY_FX(scene, stl->storage->is_playing);
381                 stl->storage->simplify_blend = GP_SIMPLIFY_BLEND(scene, stl->storage->is_playing);
382
383                 /* save pixsize */
384                 stl->storage->pixsize = DRW_viewport_pixelsize_get();
385                 if ((!DRW_state_is_opengl_render()) && (stl->storage->is_render)) {
386                         stl->storage->pixsize = &stl->storage->render_pixsize;
387                 }
388
389                 /* detect if painting session */
390                 if ((obact_gpd) &&
391                     (obact_gpd->flag & GP_DATA_STROKE_PAINTMODE) &&
392                     (stl->storage->is_playing == false))
393                 {
394                         /* need the original to avoid cow overhead while drawing */
395                         bGPdata *gpd_orig = (bGPdata *)DEG_get_original_id(&obact_gpd->id);
396                         if (((gpd_orig->runtime.sbuffer_sflag & GP_STROKE_ERASER) == 0) &&
397                             (gpd_orig->runtime.sbuffer_size > 0) &&
398                             ((gpd_orig->flag & GP_DATA_STROKE_POLYGON) == 0))
399                         {
400                                 stl->g_data->session_flag |= GP_DRW_PAINT_PAINTING;
401                         }
402                         else {
403                                 stl->g_data->session_flag = GP_DRW_PAINT_IDLE;
404                         }
405                 }
406                 else {
407                         /* if not drawing mode */
408                         stl->g_data->session_flag = GP_DRW_PAINT_HOLD;
409                 }
410
411                 if (gp_style) {
412                         stl->storage->stroke_style = gp_style->stroke_style;
413                         stl->storage->color_type = GPENCIL_COLOR_SOLID;
414                         if (gp_style->stroke_style == GP_STYLE_STROKE_STYLE_TEXTURE) {
415                                 stl->storage->color_type = GPENCIL_COLOR_TEXTURE;
416                                 if (gp_style->flag & GP_STYLE_STROKE_PATTERN) {
417                                         stl->storage->color_type = GPENCIL_COLOR_PATTERN;
418                                 }
419                         }
420                 }
421                 else {
422                         stl->storage->stroke_style = GP_STYLE_STROKE_STYLE_SOLID;
423                         stl->storage->color_type = GPENCIL_COLOR_SOLID;
424                 }
425
426                 /* drawing buffer pass for drawing the stroke that is being drawing by the user. The data
427                  * is stored in sbuffer
428                  */
429                 psl->drawing_pass = DRW_pass_create(
430                         "GPencil Drawing Pass",
431                         DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_ALWAYS);
432
433                 /* full screen pass to combine the result with default framebuffer */
434                 struct GPUBatch *quad = DRW_cache_fullscreen_quad_get();
435                 psl->mix_pass = DRW_pass_create(
436                         "GPencil Mix Pass",
437                         DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS);
438                 DRWShadingGroup *mix_shgrp = DRW_shgroup_create(e_data.gpencil_fullscreen_sh, psl->mix_pass);
439                 DRW_shgroup_call_add(mix_shgrp, quad, NULL);
440                 DRW_shgroup_uniform_texture_ref(mix_shgrp, "strokeColor", &e_data.input_color_tx);
441                 DRW_shgroup_uniform_texture_ref(mix_shgrp, "strokeDepth", &e_data.input_depth_tx);
442                 DRW_shgroup_uniform_int(mix_shgrp, "tonemapping", &stl->storage->tonemapping, 1);
443
444                 /* mix pass no blend used to copy between passes. A separated pass is required
445                  * because if mix_pass is used, the acumulation of blend degrade the colors.
446                  *
447                  * This pass is used too to take the snapshot used for background_pass. This image
448                  * will be used as the background while the user is drawing.
449                  */
450                 psl->mix_pass_noblend = DRW_pass_create(
451                         "GPencil Mix Pass no blend",
452                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS);
453                 DRWShadingGroup *mix_shgrp_noblend = DRW_shgroup_create(e_data.gpencil_fullscreen_sh, psl->mix_pass_noblend);
454                 DRW_shgroup_call_add(mix_shgrp_noblend, quad, NULL);
455                 DRW_shgroup_uniform_texture_ref(mix_shgrp_noblend, "strokeColor", &e_data.input_color_tx);
456                 DRW_shgroup_uniform_texture_ref(mix_shgrp_noblend, "strokeDepth", &e_data.input_depth_tx);
457                 DRW_shgroup_uniform_int(mix_shgrp_noblend, "tonemapping", &stl->storage->tonemapping, 1);
458
459                 /* Painting session pass (used only to speedup while the user is drawing )
460                  * This pass is used to show the snapshot of the current grease pencil strokes captured
461                  * when the user starts to draw (see comments above).
462                  * In this way, the previous strokes don't need to be redraw and the drawing process
463                  * is far to agile.
464                  */
465                 psl->background_pass = DRW_pass_create(
466                         "GPencil Background Painting Session Pass",
467                         DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS);
468                 DRWShadingGroup *background_shgrp = DRW_shgroup_create(e_data.gpencil_background_sh, psl->background_pass);
469                 DRW_shgroup_call_add(background_shgrp, quad, NULL);
470                 DRW_shgroup_uniform_texture_ref(background_shgrp, "strokeColor", &e_data.background_color_tx);
471                 DRW_shgroup_uniform_texture_ref(background_shgrp, "strokeDepth", &e_data.background_depth_tx);
472
473                 /* pass for drawing paper (only if viewport)
474                  * In render, the v3d is null so the paper is disabled
475                  * The paper is way to isolate the drawing in complex scene and to have a cleaner
476                  * drawing area.
477                  */
478                 if (v3d) {
479                         psl->paper_pass = DRW_pass_create(
480                                 "GPencil Paper Pass",
481                                 DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND);
482                         DRWShadingGroup *paper_shgrp = DRW_shgroup_create(e_data.gpencil_paper_sh, psl->paper_pass);
483                         DRW_shgroup_call_add(paper_shgrp, quad, NULL);
484                         DRW_shgroup_uniform_vec3(paper_shgrp, "color", v3d->shading.background_color, 1);
485                         DRW_shgroup_uniform_float(paper_shgrp, "opacity", &v3d->overlay.gpencil_paper_opacity, 1);
486                 }
487
488                 /* grid pass */
489                 if (v3d) {
490                         psl->grid_pass = DRW_pass_create(
491                                 "GPencil Grid Pass",
492                                 DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_ALWAYS);
493                         stl->g_data->shgrps_grid = DRW_shgroup_create(e_data.gpencil_line_sh, psl->grid_pass);
494                 }
495
496                 /* blend layers pass */
497                 psl->blend_pass = DRW_pass_create(
498                         "GPencil Blend Layers Pass",
499                         DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS);
500                 DRWShadingGroup *blend_shgrp = DRW_shgroup_create(e_data.gpencil_blend_fullscreen_sh, psl->blend_pass);
501                 DRW_shgroup_call_add(blend_shgrp, quad, NULL);
502                 DRW_shgroup_uniform_texture_ref(blend_shgrp, "strokeColor", &e_data.temp_color_tx_a);
503                 DRW_shgroup_uniform_texture_ref(blend_shgrp, "strokeDepth", &e_data.temp_depth_tx_a);
504                 DRW_shgroup_uniform_texture_ref(blend_shgrp, "blendColor", &e_data.temp_color_tx_fx);
505                 DRW_shgroup_uniform_texture_ref(blend_shgrp, "blendDepth", &e_data.temp_depth_tx_fx);
506                 DRW_shgroup_uniform_int(blend_shgrp, "mode", &stl->storage->blend_mode, 1);
507                 DRW_shgroup_uniform_int(blend_shgrp, "clamp_layer", &stl->storage->clamp_layer, 1);
508                 DRW_shgroup_uniform_float(blend_shgrp, "blend_opacity", &stl->storage->blend_opacity, 1);
509
510                 /* create effects passes */
511                 if (!stl->storage->simplify_fx) {
512                         GPENCIL_create_fx_passes(psl);
513                 }
514         }
515 }
516
517 static void gpencil_add_draw_data(void *vedata, Object *ob)
518 {
519         GPENCIL_StorageList *stl = ((GPENCIL_Data *)vedata)->stl;
520         bGPdata *gpd = (bGPdata *)ob->data;
521         const bool is_multiedit = (bool)GPENCIL_MULTIEDIT_SESSIONS_ON(gpd);
522
523         int i = stl->g_data->gp_cache_used - 1;
524         tGPencilObjectCache *cache_ob = &stl->g_data->gp_object_cache[i];
525
526         if (!cache_ob->is_dup_ob) {
527                 /* fill shading groups */
528                 if (!is_multiedit) {
529                         DRW_gpencil_populate_datablock(&e_data, vedata, ob, cache_ob);
530                 }
531                 else {
532                         DRW_gpencil_populate_multiedit(&e_data, vedata, ob, cache_ob);
533                 }
534         }
535
536         /* FX passses */
537         cache_ob->has_fx = false;
538         if ((!stl->storage->simplify_fx) &&
539             (BKE_shaderfx_has_gpencil(ob)))
540         {
541                 cache_ob->has_fx = true;
542                 if ((!stl->storage->simplify_fx) && (!is_multiedit)) {
543                         DRW_gpencil_fx_prepare(&e_data, vedata, cache_ob);
544                 }
545         }
546
547 }
548
549 void GPENCIL_cache_populate(void *vedata, Object *ob)
550 {
551         /* object must be visible */
552         if (!DRW_object_is_visible_in_active_context(ob)) {
553                 return;
554         }
555
556         GPENCIL_StorageList *stl = ((GPENCIL_Data *)vedata)->stl;
557         const DRWContextState *draw_ctx = DRW_context_state_get();
558         Scene *scene = draw_ctx->scene;
559         ToolSettings *ts = scene->toolsettings;
560         View3D *v3d = draw_ctx->v3d;
561
562         if (ob->type == OB_GPENCIL && ob->data) {
563                 bGPdata *gpd = (bGPdata *)ob->data;
564
565                 /* when start/stop animation the cache must be set as dirty to reset all data */
566                 if (stl->storage->reset_cache) {
567                         gpd->flag |= GP_DATA_CACHE_IS_DIRTY;
568                         stl->storage->reset_cache = false;
569                 }
570
571                 if ((stl->g_data->session_flag & GP_DRW_PAINT_READY) == 0) {
572
573                         /* save gp objects for drawing later */
574                         stl->g_data->gp_object_cache = gpencil_object_cache_add(
575                                 stl->g_data->gp_object_cache, ob,
576                                 &stl->g_data->gp_cache_size, &stl->g_data->gp_cache_used);
577
578                         /* load drawing data */
579                         gpencil_add_draw_data(vedata, ob);
580                 }
581
582                 /* draw current painting strokes
583                 * (only if region is equal to originated paint region)
584                 */
585                 if ((draw_ctx->obact == ob) &&
586                     ((gpd->runtime.ar == NULL) || (gpd->runtime.ar == draw_ctx->ar)))
587                 {
588                         DRW_gpencil_populate_buffer_strokes(&e_data, vedata, ts, ob);
589                 }
590
591                 /* grid */
592                 if ((v3d) &&
593                     ((v3d->flag2 & V3D_RENDER_OVERRIDE) == 0) &&
594                     (v3d->gp_flag & V3D_GP_SHOW_GRID) &&
595                     (ob->type == OB_GPENCIL) && (ob == draw_ctx->obact))
596                 {
597                         GPU_BATCH_DISCARD_SAFE(e_data.batch_grid);
598                         MEM_SAFE_FREE(e_data.batch_grid);
599
600                         e_data.batch_grid = DRW_gpencil_get_grid(ob);
601
602                         /* define grid orientation */
603                         if (ts->gp_sculpt.lock_axis != GP_LOCKAXIS_VIEW) {
604                                 copy_m4_m4(stl->storage->grid_matrix, ob->obmat);
605                         }
606                         else {
607                                 /* align always to view */
608                                 invert_m4_m4(stl->storage->grid_matrix, draw_ctx->rv3d->viewmat);
609                                 /* copy ob location */
610                                 copy_v3_v3(stl->storage->grid_matrix[3], ob->obmat[3]);
611                         }
612
613                         DRW_shgroup_call_add(
614                                 stl->g_data->shgrps_grid,
615                                 e_data.batch_grid,
616                                 stl->storage->grid_matrix);
617                 }
618         }
619 }
620
621 void GPENCIL_cache_finish(void *vedata)
622 {
623         DRW_gpencil_populate_particles(&e_data, vedata);
624 }
625
626 /* helper function to sort inverse gpencil objects using qsort */
627 static int gpencil_object_cache_compare_zdepth(const void *a1, const void *a2)
628 {
629         const tGPencilObjectCache *ps1 = a1, *ps2 = a2;
630
631         if (ps1->zdepth < ps2->zdepth) return 1;
632         else if (ps1->zdepth > ps2->zdepth) return -1;
633
634         return 0;
635 }
636
637 /* prepare a texture with full viewport screenshot for fast drawing */
638 static void gpencil_prepare_fast_drawing(
639         GPENCIL_StorageList *stl, DefaultFramebufferList *dfbl,
640         GPENCIL_FramebufferList *fbl, DRWPass *pass,
641         const float clearcol[4])
642 {
643         if (stl->g_data->session_flag & (GP_DRW_PAINT_IDLE | GP_DRW_PAINT_FILLING)) {
644                 GPU_framebuffer_bind(fbl->background_fb);
645                 /* clean only in first loop cycle */
646                 if (stl->g_data->session_flag & GP_DRW_PAINT_IDLE) {
647                         GPU_framebuffer_clear_color_depth(fbl->background_fb, clearcol, 1.0f);
648                         stl->g_data->session_flag = GP_DRW_PAINT_FILLING;
649                 }
650                 /* repeat pass to fill temp texture */
651                 DRW_draw_pass(pass);
652                 /* set default framebuffer again */
653                 GPU_framebuffer_bind(dfbl->default_fb);
654         }
655 }
656
657 static void gpencil_free_obj_runtime(GPENCIL_StorageList *stl)
658 {
659         /* reset all cache flags */
660         for (int i = 0; i < stl->g_data->gp_cache_used; i++) {
661                 tGPencilObjectCache *cache_ob = &stl->g_data->gp_object_cache[i];
662                 bGPdata *gpd = cache_ob->gpd;
663                 gpd->flag &= ~GP_DATA_CACHE_IS_DIRTY;
664
665                 /* free shgrp array */
666                 cache_ob->tot_layers = 0;
667                 MEM_SAFE_FREE(cache_ob->shgrp_array);
668         }
669
670         /* free the cache itself */
671         MEM_SAFE_FREE(stl->g_data->gp_object_cache);
672 }
673
674 static void gpencil_draw_pass_range(
675         GPENCIL_FramebufferList *fbl, GPENCIL_StorageList *stl,
676         GPENCIL_PassList *psl, GPENCIL_TextureList *txl,
677         GPUFrameBuffer *fb,
678         DRWShadingGroup *init_shgrp, DRWShadingGroup *end_shgrp, bool multi)
679 {
680         if (init_shgrp == NULL) {
681                 return;
682         }
683
684         /* previews don't use AA */
685         if ((!stl->storage->is_mat_preview) && (multi)) {
686                 MULTISAMPLE_GP_SYNC_ENABLE(stl->storage->multisamples, fbl);
687         }
688
689         DRW_draw_pass_subset(
690                 psl->stroke_pass, init_shgrp, end_shgrp);
691
692         if ((!stl->storage->is_mat_preview) && (multi)) {
693                 MULTISAMPLE_GP_SYNC_DISABLE(stl->storage->multisamples, fbl, fb, txl);
694         }
695
696 }
697
698 /* draw scene */
699 void GPENCIL_draw_scene(void *ved)
700 {
701         GPENCIL_Data *vedata = (GPENCIL_Data *)ved;
702         GPENCIL_StorageList *stl = ((GPENCIL_Data *)vedata)->stl;
703
704         GPENCIL_PassList *psl = ((GPENCIL_Data *)vedata)->psl;
705         GPENCIL_FramebufferList *fbl = ((GPENCIL_Data *)vedata)->fbl;
706         DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
707         GPENCIL_TextureList *txl = ((GPENCIL_Data *)vedata)->txl;
708
709         tGPencilObjectCache *cache_ob;
710         tGPencilObjectCache_shgrp *array_elm = NULL;
711         DRWShadingGroup *init_shgrp = NULL;
712         DRWShadingGroup *end_shgrp = NULL;
713
714         const float clearcol[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
715
716         const DRWContextState *draw_ctx = DRW_context_state_get();
717         View3D *v3d = draw_ctx->v3d;
718         Object *obact = draw_ctx->obact;
719         const bool playing = stl->storage->is_playing;
720         const bool is_render = stl->storage->is_render;
721
722         /* paper pass to display a comfortable area to draw over complex scenes with geometry */
723         if ((!is_render) && (obact) && (obact->type == OB_GPENCIL)) {
724                 if (((v3d->flag2 & V3D_RENDER_OVERRIDE) == 0) &&
725                     (v3d->gp_flag & V3D_GP_SHOW_PAPER))
726                 {
727                         DRW_draw_pass(psl->paper_pass);
728                 }
729         }
730
731         /* if we have a painting session, we use fast viewport drawing method */
732         if ((!is_render) && (stl->g_data->session_flag & GP_DRW_PAINT_PAINTING)) {
733                 GPU_framebuffer_bind(dfbl->default_fb);
734
735                 MULTISAMPLE_GP_SYNC_ENABLE(stl->storage->multisamples, fbl);
736
737                 DRW_draw_pass(psl->background_pass);
738                 DRW_draw_pass(psl->drawing_pass);
739
740                 MULTISAMPLE_GP_SYNC_DISABLE(stl->storage->multisamples, fbl, dfbl->default_fb, txl);
741
742                 /* free memory */
743                 gpencil_free_obj_runtime(stl);
744
745                 /* grid pass */
746                 if ((!is_render) && (obact) && (obact->type == OB_GPENCIL)) {
747                         if (((v3d->flag2 & V3D_RENDER_OVERRIDE) == 0) &&
748                             (v3d->gp_flag & V3D_GP_SHOW_GRID))
749                         {
750                                 DRW_draw_pass(psl->grid_pass);
751                         }
752                 }
753
754                 return;
755         }
756
757         if (DRW_state_is_fbo()) {
758
759                 /* Draw all pending objects */
760                 if (stl->g_data->gp_cache_used > 0) {
761
762                         /* sort by zdepth */
763                         qsort(stl->g_data->gp_object_cache, stl->g_data->gp_cache_used,
764                               sizeof(tGPencilObjectCache), gpencil_object_cache_compare_zdepth);
765
766                         for (int i = 0; i < stl->g_data->gp_cache_used; i++) {
767                                 cache_ob = &stl->g_data->gp_object_cache[i];
768                                 bGPdata *gpd = cache_ob->gpd;
769                                 init_shgrp = NULL;
770                                 /* Render stroke in separated framebuffer */
771                                 GPU_framebuffer_bind(fbl->temp_fb_a);
772                                 GPU_framebuffer_clear_color_depth(fbl->temp_fb_a, clearcol, 1.0f);
773
774                                 /* Stroke Pass:
775                                  * draw only a subset that usually starts with a fill and ends with stroke
776                                  */
777                                 bool use_blend = false;
778                                 if (cache_ob->tot_layers > 0) {
779                                         for (int e = 0; e < cache_ob->tot_layers; e++) {
780                                                 bool is_last = e == cache_ob->tot_layers - 1 ? true : false;
781                                                 array_elm = &cache_ob->shgrp_array[e];
782
783                                                 if (((array_elm->mode == eGplBlendMode_Normal) &&
784                                                      (!use_blend) && (!array_elm->clamp_layer)) ||
785                                                     (e == 0))
786                                                 {
787                                                         if (init_shgrp == NULL) {
788                                                                 init_shgrp = array_elm->init_shgrp;
789                                                         }
790                                                         end_shgrp = array_elm->end_shgrp;
791                                                 }
792                                                 else {
793                                                         use_blend = true;
794                                                         /* draw pending groups */
795                                                         gpencil_draw_pass_range(
796                                                                 fbl, stl, psl, txl, fbl->temp_fb_a,
797                                                                 init_shgrp, end_shgrp, is_last);
798
799                                                         /* draw current group in separated texture */
800                                                         init_shgrp = array_elm->init_shgrp;
801                                                         end_shgrp = array_elm->end_shgrp;
802
803                                                         GPU_framebuffer_bind(fbl->temp_fb_fx);
804                                                         GPU_framebuffer_clear_color_depth(fbl->temp_fb_fx, clearcol, 1.0f);
805                                                         gpencil_draw_pass_range(
806                                                                 fbl, stl, psl, txl, fbl->temp_fb_fx,
807                                                                 init_shgrp, end_shgrp,
808                                                                 is_last);
809
810                                                         /* Blend A texture and FX texture */
811                                                         GPU_framebuffer_bind(fbl->temp_fb_b);
812                                                         GPU_framebuffer_clear_color_depth(fbl->temp_fb_b, clearcol, 1.0f);
813                                                         stl->storage->blend_mode = array_elm->mode;
814                                                         stl->storage->clamp_layer = (int)array_elm->clamp_layer;
815                                                         stl->storage->blend_opacity = array_elm->blend_opacity;
816                                                         DRW_draw_pass(psl->blend_pass);
817
818                                                         /* Copy B texture to A texture to follow loop */
819                                                         e_data.input_depth_tx = e_data.temp_depth_tx_b;
820                                                         e_data.input_color_tx = e_data.temp_color_tx_b;
821
822                                                         GPU_framebuffer_bind(fbl->temp_fb_a);
823                                                         GPU_framebuffer_clear_color_depth(fbl->temp_fb_a, clearcol, 1.0f);
824                                                         DRW_draw_pass(psl->mix_pass_noblend);
825
826                                                         /* prepare next group */
827                                                         init_shgrp = NULL;
828                                                 }
829
830                                         }
831                                         /* last group */
832                                         gpencil_draw_pass_range(
833                                                 fbl, stl, psl, txl, fbl->temp_fb_a,
834                                                 init_shgrp, end_shgrp,
835                                                 true);
836                                 }
837
838                                 /* Current buffer drawing */
839                                 if ((!is_render) && (cache_ob->is_dup_ob == false)) {
840                                         DRW_draw_pass(psl->drawing_pass);
841                                 }
842                                 /* fx passes */
843                                 if (cache_ob->has_fx == true) {
844                                         stl->storage->tonemapping = 0;
845                                         DRW_gpencil_fx_draw(&e_data, vedata, cache_ob);
846                                 }
847
848                                 e_data.input_depth_tx = e_data.temp_depth_tx_a;
849                                 e_data.input_color_tx = e_data.temp_color_tx_a;
850
851                                 /* Combine with scene buffer */
852                                 if ((!is_render) || (fbl->main == NULL)) {
853                                         GPU_framebuffer_bind(dfbl->default_fb);
854                                 }
855                                 else {
856                                         GPU_framebuffer_bind(fbl->main);
857                                 }
858                                 /* tonemapping */
859                                 stl->storage->tonemapping = stl->storage->is_render ? 1 : 0;
860
861                                 DRW_draw_pass(psl->mix_pass);
862
863                                 /* prepare for fast drawing */
864                                 if (!is_render) {
865                                         if (!playing) {
866                                                 gpencil_prepare_fast_drawing(stl, dfbl, fbl, psl->mix_pass_noblend, clearcol);
867                                         }
868                                 }
869                                 else {
870                                         /* if render, the cache must be dirty for next loop */
871                                         gpd->flag |= GP_DATA_CACHE_IS_DIRTY;
872                                 }
873                         }
874                         /* edit points */
875                         if ((!is_render) && (!playing)) {
876                                 DRW_draw_pass(psl->edit_pass);
877                         }
878                 }
879                 /* grid pass */
880                 if ((!is_render) && (obact) && (obact->type == OB_GPENCIL)) {
881                         if (((v3d->flag2 & V3D_RENDER_OVERRIDE) == 0) &&
882                             (v3d->gp_flag & V3D_GP_SHOW_GRID))
883                         {
884                                 DRW_draw_pass(psl->grid_pass);
885                         }
886                 }
887         }
888         /* free memory */
889         gpencil_free_obj_runtime(stl);
890
891         /* reset  */
892         if (DRW_state_is_fbo()) {
893                 /* attach again default framebuffer */
894                 if (!is_render) {
895                         GPU_framebuffer_bind(dfbl->default_fb);
896                 }
897
898                 /* the temp texture is ready. Now we can use fast screen drawing */
899                 if (stl->g_data->session_flag & GP_DRW_PAINT_FILLING) {
900                         stl->g_data->session_flag = GP_DRW_PAINT_READY;
901                 }
902         }
903 }
904
905 static const DrawEngineDataSize GPENCIL_data_size = DRW_VIEWPORT_DATA_SIZE(GPENCIL_Data);
906
907 DrawEngineType draw_engine_gpencil_type = {
908         NULL, NULL,
909         N_("GpencilMode"),
910         &GPENCIL_data_size,
911         &GPENCIL_engine_init,
912         &GPENCIL_engine_free,
913         &GPENCIL_cache_init,
914         &GPENCIL_cache_populate,
915         &GPENCIL_cache_finish,
916         NULL,
917         &GPENCIL_draw_scene,
918         NULL,
919         NULL,
920         &GPENCIL_render_to_image,
921 };