Merge remote-tracking branch 'origin/master' into blender2.8
[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                 DRW_shgroup_uniform_int(mix_shgrp, "tonemapping", &stl->storage->tonemapping, 1);
510
511                 /* create effects passes */
512                 if (!stl->storage->simplify_fx) {
513                         GPENCIL_create_fx_passes(psl);
514                 }
515         }
516 }
517
518 static void gpencil_add_draw_data(void *vedata, Object *ob)
519 {
520         GPENCIL_StorageList *stl = ((GPENCIL_Data *)vedata)->stl;
521         bGPdata *gpd = (bGPdata *)ob->data;
522         const bool is_multiedit = (bool)GPENCIL_MULTIEDIT_SESSIONS_ON(gpd);
523
524         int i = stl->g_data->gp_cache_used - 1;
525         tGPencilObjectCache *cache_ob = &stl->g_data->gp_object_cache[i];
526
527         if (!cache_ob->is_dup_ob) {
528                 /* fill shading groups */
529                 if (!is_multiedit) {
530                         DRW_gpencil_populate_datablock(&e_data, vedata, ob, cache_ob);
531                 }
532                 else {
533                         DRW_gpencil_populate_multiedit(&e_data, vedata, ob, cache_ob);
534                 }
535         }
536
537         /* FX passses */
538         cache_ob->has_fx = false;
539         if ((!stl->storage->simplify_fx) &&
540             (BKE_shaderfx_has_gpencil(ob)))
541         {
542                 cache_ob->has_fx = true;
543                 if ((!stl->storage->simplify_fx) && (!is_multiedit)) {
544                         DRW_gpencil_fx_prepare(&e_data, vedata, cache_ob);
545                 }
546         }
547
548 }
549
550 void GPENCIL_cache_populate(void *vedata, Object *ob)
551 {
552         /* object must be visible */
553         if (!DRW_object_is_visible_in_active_context(ob)) {
554                 return;
555         }
556
557         GPENCIL_StorageList *stl = ((GPENCIL_Data *)vedata)->stl;
558         const DRWContextState *draw_ctx = DRW_context_state_get();
559         Scene *scene = draw_ctx->scene;
560         ToolSettings *ts = scene->toolsettings;
561         View3D *v3d = draw_ctx->v3d;
562
563         if (ob->type == OB_GPENCIL && ob->data) {
564                 bGPdata *gpd = (bGPdata *)ob->data;
565
566                 /* when start/stop animation the cache must be set as dirty to reset all data */
567                 if (stl->storage->reset_cache) {
568                         gpd->flag |= GP_DATA_CACHE_IS_DIRTY;
569                         stl->storage->reset_cache = false;
570                 }
571
572                 if ((stl->g_data->session_flag & GP_DRW_PAINT_READY) == 0) {
573
574                         /* save gp objects for drawing later */
575                         stl->g_data->gp_object_cache = gpencil_object_cache_add(
576                                 stl->g_data->gp_object_cache, ob,
577                                 &stl->g_data->gp_cache_size, &stl->g_data->gp_cache_used);
578
579                         /* load drawing data */
580                         gpencil_add_draw_data(vedata, ob);
581                 }
582
583                 /* draw current painting strokes
584                 * (only if region is equal to originated paint region)
585                 */
586                 if ((draw_ctx->obact == ob) &&
587                     ((gpd->runtime.ar == NULL) || (gpd->runtime.ar == draw_ctx->ar)))
588                 {
589                         DRW_gpencil_populate_buffer_strokes(&e_data, vedata, ts, ob);
590                 }
591
592                 /* grid */
593                 if ((v3d) &&
594                     ((v3d->flag2 & V3D_RENDER_OVERRIDE) == 0) &&
595                     (v3d->gp_flag & V3D_GP_SHOW_GRID) &&
596                     (ob->type == OB_GPENCIL) && (ob == draw_ctx->obact))
597                 {
598                         GPU_BATCH_DISCARD_SAFE(e_data.batch_grid);
599                         MEM_SAFE_FREE(e_data.batch_grid);
600
601                         e_data.batch_grid = DRW_gpencil_get_grid(ob);
602
603                         /* define grid orientation */
604                         if (ts->gp_sculpt.lock_axis != GP_LOCKAXIS_VIEW) {
605                                 copy_m4_m4(stl->storage->grid_matrix, ob->obmat);
606                         }
607                         else {
608                                 /* align always to view */
609                                 invert_m4_m4(stl->storage->grid_matrix, draw_ctx->rv3d->viewmat);
610                                 /* copy ob location */
611                                 copy_v3_v3(stl->storage->grid_matrix[3], ob->obmat[3]);
612                         }
613
614                         DRW_shgroup_call_add(
615                                 stl->g_data->shgrps_grid,
616                                 e_data.batch_grid,
617                                 stl->storage->grid_matrix);
618                 }
619         }
620 }
621
622 void GPENCIL_cache_finish(void *vedata)
623 {
624         DRW_gpencil_populate_particles(&e_data, vedata);
625 }
626
627 /* helper function to sort inverse gpencil objects using qsort */
628 static int gpencil_object_cache_compare_zdepth(const void *a1, const void *a2)
629 {
630         const tGPencilObjectCache *ps1 = a1, *ps2 = a2;
631
632         if (ps1->zdepth < ps2->zdepth) return 1;
633         else if (ps1->zdepth > ps2->zdepth) return -1;
634
635         return 0;
636 }
637
638 /* prepare a texture with full viewport screenshot for fast drawing */
639 static void gpencil_prepare_fast_drawing(
640         GPENCIL_StorageList *stl, DefaultFramebufferList *dfbl,
641         GPENCIL_FramebufferList *fbl, DRWPass *pass,
642         const float clearcol[4])
643 {
644         if (stl->g_data->session_flag & (GP_DRW_PAINT_IDLE | GP_DRW_PAINT_FILLING)) {
645                 GPU_framebuffer_bind(fbl->background_fb);
646                 /* clean only in first loop cycle */
647                 if (stl->g_data->session_flag & GP_DRW_PAINT_IDLE) {
648                         GPU_framebuffer_clear_color_depth(fbl->background_fb, clearcol, 1.0f);
649                         stl->g_data->session_flag = GP_DRW_PAINT_FILLING;
650                 }
651                 /* repeat pass to fill temp texture */
652                 DRW_draw_pass(pass);
653                 /* set default framebuffer again */
654                 GPU_framebuffer_bind(dfbl->default_fb);
655         }
656 }
657
658 static void gpencil_free_obj_runtime(GPENCIL_StorageList *stl)
659 {
660         /* reset all cache flags */
661         for (int i = 0; i < stl->g_data->gp_cache_used; i++) {
662                 tGPencilObjectCache *cache_ob = &stl->g_data->gp_object_cache[i];
663                 bGPdata *gpd = cache_ob->gpd;
664                 gpd->flag &= ~GP_DATA_CACHE_IS_DIRTY;
665
666                 /* free shgrp array */
667                 cache_ob->tot_layers = 0;
668                 MEM_SAFE_FREE(cache_ob->shgrp_array);
669         }
670
671         /* free the cache itself */
672         MEM_SAFE_FREE(stl->g_data->gp_object_cache);
673 }
674
675 static void gpencil_draw_pass_range(
676         GPENCIL_FramebufferList *fbl, GPENCIL_StorageList *stl,
677         GPENCIL_PassList *psl, GPENCIL_TextureList *txl,
678         GPUFrameBuffer *fb,
679         DRWShadingGroup *init_shgrp, DRWShadingGroup *end_shgrp, bool multi)
680 {
681         if (init_shgrp == NULL) {
682                 return;
683         }
684
685         /* previews don't use AA */
686         if ((!stl->storage->is_mat_preview) && (multi)) {
687                 MULTISAMPLE_GP_SYNC_ENABLE(stl->storage->multisamples, fbl);
688         }
689
690         DRW_draw_pass_subset(
691                 psl->stroke_pass, init_shgrp, end_shgrp);
692
693         if ((!stl->storage->is_mat_preview) && (multi)) {
694                 MULTISAMPLE_GP_SYNC_DISABLE(stl->storage->multisamples, fbl, fb, txl);
695         }
696
697 }
698
699 /* draw scene */
700 void GPENCIL_draw_scene(void *ved)
701 {
702         GPENCIL_Data *vedata = (GPENCIL_Data *)ved;
703         GPENCIL_StorageList *stl = ((GPENCIL_Data *)vedata)->stl;
704
705         GPENCIL_PassList *psl = ((GPENCIL_Data *)vedata)->psl;
706         GPENCIL_FramebufferList *fbl = ((GPENCIL_Data *)vedata)->fbl;
707         DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
708         GPENCIL_TextureList *txl = ((GPENCIL_Data *)vedata)->txl;
709
710         tGPencilObjectCache *cache_ob;
711         tGPencilObjectCache_shgrp *array_elm = NULL;
712         DRWShadingGroup *init_shgrp = NULL;
713         DRWShadingGroup *end_shgrp = NULL;
714
715         const float clearcol[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
716
717         const DRWContextState *draw_ctx = DRW_context_state_get();
718         View3D *v3d = draw_ctx->v3d;
719         Object *obact = draw_ctx->obact;
720         const bool playing = stl->storage->is_playing;
721         const bool is_render = stl->storage->is_render;
722
723         /* paper pass to display a comfortable area to draw over complex scenes with geometry */
724         if ((!is_render) && (obact) && (obact->type == OB_GPENCIL)) {
725                 if (((v3d->flag2 & V3D_RENDER_OVERRIDE) == 0) &&
726                     (v3d->gp_flag & V3D_GP_SHOW_PAPER))
727                 {
728                         DRW_draw_pass(psl->paper_pass);
729                 }
730         }
731
732         /* if we have a painting session, we use fast viewport drawing method */
733         if ((!is_render) && (stl->g_data->session_flag & GP_DRW_PAINT_PAINTING)) {
734                 GPU_framebuffer_bind(dfbl->default_fb);
735
736                 MULTISAMPLE_GP_SYNC_ENABLE(stl->storage->multisamples, fbl);
737
738                 DRW_draw_pass(psl->background_pass);
739                 DRW_draw_pass(psl->drawing_pass);
740
741                 MULTISAMPLE_GP_SYNC_DISABLE(stl->storage->multisamples, fbl, dfbl->default_fb, txl);
742
743                 /* free memory */
744                 gpencil_free_obj_runtime(stl);
745
746                 /* grid pass */
747                 if ((!is_render) && (obact) && (obact->type == OB_GPENCIL)) {
748                         if (((v3d->flag2 & V3D_RENDER_OVERRIDE) == 0) &&
749                             (v3d->gp_flag & V3D_GP_SHOW_GRID))
750                         {
751                                 DRW_draw_pass(psl->grid_pass);
752                         }
753                 }
754
755                 return;
756         }
757
758         if (DRW_state_is_fbo()) {
759
760                 /* Draw all pending objects */
761                 if (stl->g_data->gp_cache_used > 0) {
762
763                         /* sort by zdepth */
764                         qsort(stl->g_data->gp_object_cache, stl->g_data->gp_cache_used,
765                               sizeof(tGPencilObjectCache), gpencil_object_cache_compare_zdepth);
766
767                         for (int i = 0; i < stl->g_data->gp_cache_used; i++) {
768                                 cache_ob = &stl->g_data->gp_object_cache[i];
769                                 bGPdata *gpd = cache_ob->gpd;
770                                 init_shgrp = NULL;
771                                 /* Render stroke in separated framebuffer */
772                                 GPU_framebuffer_bind(fbl->temp_fb_a);
773                                 GPU_framebuffer_clear_color_depth(fbl->temp_fb_a, clearcol, 1.0f);
774
775                                 /* Stroke Pass:
776                                  * draw only a subset that usually starts with a fill and ends with stroke
777                                  */
778                                 bool use_blend = false;
779                                 if (cache_ob->tot_layers > 0) {
780                                         for (int e = 0; e < cache_ob->tot_layers; e++) {
781                                                 bool is_last = e == cache_ob->tot_layers - 1 ? true : false;
782                                                 array_elm = &cache_ob->shgrp_array[e];
783
784                                                 if (((array_elm->mode == eGplBlendMode_Normal) &&
785                                                      (!use_blend) && (!array_elm->clamp_layer)) ||
786                                                     (e == 0))
787                                                 {
788                                                         if (init_shgrp == NULL) {
789                                                                 init_shgrp = array_elm->init_shgrp;
790                                                         }
791                                                         end_shgrp = array_elm->end_shgrp;
792                                                 }
793                                                 else {
794                                                         use_blend = true;
795                                                         /* draw pending groups */
796                                                         gpencil_draw_pass_range(
797                                                                 fbl, stl, psl, txl, fbl->temp_fb_a,
798                                                                 init_shgrp, end_shgrp, is_last);
799
800                                                         /* draw current group in separated texture */
801                                                         init_shgrp = array_elm->init_shgrp;
802                                                         end_shgrp = array_elm->end_shgrp;
803
804                                                         GPU_framebuffer_bind(fbl->temp_fb_fx);
805                                                         GPU_framebuffer_clear_color_depth(fbl->temp_fb_fx, clearcol, 1.0f);
806                                                         gpencil_draw_pass_range(
807                                                                 fbl, stl, psl, txl, fbl->temp_fb_fx,
808                                                                 init_shgrp, end_shgrp,
809                                                                 is_last);
810
811                                                         /* Blend A texture and FX texture */
812                                                         GPU_framebuffer_bind(fbl->temp_fb_b);
813                                                         GPU_framebuffer_clear_color_depth(fbl->temp_fb_b, clearcol, 1.0f);
814                                                         stl->storage->blend_mode = array_elm->mode;
815                                                         stl->storage->clamp_layer = (int)array_elm->clamp_layer;
816                                                         stl->storage->blend_opacity = array_elm->blend_opacity;
817                                                         stl->storage->tonemapping = stl->storage->is_render ? 1 : 0;
818                                                         DRW_draw_pass(psl->blend_pass);
819                                                         stl->storage->tonemapping = 0;
820
821                                                         /* Copy B texture to A texture to follow loop */
822                                                         e_data.input_depth_tx = e_data.temp_depth_tx_b;
823                                                         e_data.input_color_tx = e_data.temp_color_tx_b;
824
825                                                         GPU_framebuffer_bind(fbl->temp_fb_a);
826                                                         GPU_framebuffer_clear_color_depth(fbl->temp_fb_a, clearcol, 1.0f);
827                                                         DRW_draw_pass(psl->mix_pass_noblend);
828
829                                                         /* prepare next group */
830                                                         init_shgrp = NULL;
831                                                 }
832
833                                         }
834                                         /* last group */
835                                         gpencil_draw_pass_range(
836                                                 fbl, stl, psl, txl, fbl->temp_fb_a,
837                                                 init_shgrp, end_shgrp,
838                                                 true);
839                                 }
840
841                                 /* Current buffer drawing */
842                                 if ((!is_render) && (cache_ob->is_dup_ob == false)) {
843                                         DRW_draw_pass(psl->drawing_pass);
844                                 }
845                                 /* fx passes */
846                                 if (cache_ob->has_fx == true) {
847                                         stl->storage->tonemapping = 0;
848                                         DRW_gpencil_fx_draw(&e_data, vedata, cache_ob);
849                                 }
850
851                                 e_data.input_depth_tx = e_data.temp_depth_tx_a;
852                                 e_data.input_color_tx = e_data.temp_color_tx_a;
853
854                                 /* Combine with scene buffer */
855                                 if ((!is_render) || (fbl->main == NULL)) {
856                                         GPU_framebuffer_bind(dfbl->default_fb);
857                                 }
858                                 else {
859                                         GPU_framebuffer_bind(fbl->main);
860                                 }
861                                 /* tonemapping */
862                                 stl->storage->tonemapping = stl->storage->is_render ? 1 : 0;
863
864                                 DRW_draw_pass(psl->mix_pass);
865
866                                 /* prepare for fast drawing */
867                                 if (!is_render) {
868                                         if (!playing) {
869                                                 gpencil_prepare_fast_drawing(stl, dfbl, fbl, psl->mix_pass_noblend, clearcol);
870                                         }
871                                 }
872                                 else {
873                                         /* if render, the cache must be dirty for next loop */
874                                         gpd->flag |= GP_DATA_CACHE_IS_DIRTY;
875                                 }
876                         }
877                         /* edit points */
878                         if ((!is_render) && (!playing)) {
879                                 DRW_draw_pass(psl->edit_pass);
880                         }
881                 }
882                 /* grid pass */
883                 if ((!is_render) && (obact) && (obact->type == OB_GPENCIL)) {
884                         if (((v3d->flag2 & V3D_RENDER_OVERRIDE) == 0) &&
885                             (v3d->gp_flag & V3D_GP_SHOW_GRID))
886                         {
887                                 DRW_draw_pass(psl->grid_pass);
888                         }
889                 }
890         }
891         /* free memory */
892         gpencil_free_obj_runtime(stl);
893
894         /* reset  */
895         if (DRW_state_is_fbo()) {
896                 /* attach again default framebuffer */
897                 if (!is_render) {
898                         GPU_framebuffer_bind(dfbl->default_fb);
899                 }
900
901                 /* the temp texture is ready. Now we can use fast screen drawing */
902                 if (stl->g_data->session_flag & GP_DRW_PAINT_FILLING) {
903                         stl->g_data->session_flag = GP_DRW_PAINT_READY;
904                 }
905         }
906 }
907
908 static const DrawEngineDataSize GPENCIL_data_size = DRW_VIEWPORT_DATA_SIZE(GPENCIL_Data);
909
910 DrawEngineType draw_engine_gpencil_type = {
911         NULL, NULL,
912         N_("GpencilMode"),
913         &GPENCIL_data_size,
914         &GPENCIL_engine_init,
915         &GPENCIL_engine_free,
916         &GPENCIL_cache_init,
917         &GPENCIL_cache_populate,
918         &GPENCIL_cache_finish,
919         NULL,
920         &GPENCIL_draw_scene,
921         NULL,
922         NULL,
923         &GPENCIL_render_to_image,
924 };