GPencil: Cleanup comment text
[blender.git] / source / blender / draw / engines / gpencil / gpencil_draw_utils.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * Copyright 2017, Blender Foundation.
17  */
18
19 /** \file
20  * \ingroup draw
21  */
22
23 #include "BLI_polyfill_2d.h"
24
25 #include "DRW_render.h"
26
27 #include "BKE_gpencil.h"
28 #include "BKE_gpencil_modifier.h"
29 #include "BKE_image.h"
30 #include "BKE_material.h"
31 #include "BKE_paint.h"
32
33 #include "BLI_hash.h"
34
35 #include "ED_gpencil.h"
36
37 #include "DNA_gpencil_types.h"
38 #include "DNA_material_types.h"
39 #include "DNA_view3d_types.h"
40 #include "DNA_gpencil_modifier_types.h"
41
42 /* If builtin shaders are needed */
43 #include "GPU_shader.h"
44 #include "GPU_texture.h"
45
46 /* For EvaluationContext... */
47 #include "DEG_depsgraph.h"
48 #include "DEG_depsgraph_query.h"
49
50 #include "IMB_imbuf_types.h"
51
52 #include "gpencil_engine.h"
53
54 #include "UI_resources.h"
55
56 /* fill type to communicate to shader */
57 #define SOLID 0
58 #define GRADIENT 1
59 #define RADIAL 2
60 #define CHESS 3
61 #define TEXTURE 4
62 #define PATTERN 5
63
64 #define GP_SET_SRC_GPS(src_gps) if (src_gps) src_gps = src_gps->next
65
66 /* Get number of vertex for using in GPU VBOs */
67 static void gpencil_calc_vertex(
68         GPENCIL_StorageList *stl, tGPencilObjectCache *cache_ob,
69         GpencilBatchCache *cache, bGPdata *gpd,
70         int cfra_eval)
71 {
72         if (!cache->is_dirty) {
73                 return;
74         }
75
76         Object *ob = cache_ob->ob;
77         const DRWContextState *draw_ctx = DRW_context_state_get();
78         const bool main_onion = draw_ctx->v3d != NULL ? (draw_ctx->v3d->gp_flag & V3D_GP_SHOW_ONION_SKIN) : true;
79         const bool playing = stl->storage->is_playing;
80         const bool overlay = draw_ctx->v3d != NULL ? (bool)((draw_ctx->v3d->flag2 & V3D_HIDE_OVERLAYS) == 0) : true;
81         const bool do_onion = (bool)((gpd->flag & GP_DATA_STROKE_WEIGHTMODE) == 0) &&
82                 overlay && main_onion && DRW_gpencil_onion_active(gpd) && !playing;
83
84         const bool time_remap = BKE_gpencil_has_time_modifiers(ob);
85         const bool is_multiedit = (bool)GPENCIL_MULTIEDIT_SESSIONS_ON(gpd);
86
87         cache_ob->tot_vertex = 0;
88         cache_ob->tot_triangles = 0;
89
90         for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) {
91                 bGPDframe *init_gpf = NULL;
92                 const bool is_onion = ((do_onion) && (gpl->onion_flag & GP_LAYER_ONIONSKIN));
93                 if (gpl->flag & GP_LAYER_HIDE) {
94                         continue;
95                 }
96
97                 /* if multiedit or onion skin need to count all frames of the layer */
98                 if ((is_multiedit) || (is_onion)) {
99                         init_gpf = gpl->frames.first;
100                 }
101                 else {
102                         /* verify time modifiers */
103                         if ((time_remap) && (!stl->storage->simplify_modif)) {
104                                 int remap_cfra = BKE_gpencil_time_modifier(
105                                         draw_ctx->depsgraph, draw_ctx->scene, ob, gpl, cfra_eval,
106                                         stl->storage->is_render);
107                                 init_gpf = BKE_gpencil_layer_getframe(gpl, remap_cfra, GP_GETFRAME_USE_PREV);
108                         }
109                         else {
110                                 init_gpf = gpl->actframe;
111                         }
112                 }
113
114                 if (init_gpf == NULL) {
115                         continue;
116                 }
117
118                 for (bGPDframe *gpf = init_gpf; gpf; gpf = gpf->next) {
119                         for (bGPDstroke *gps = gpf->strokes.first; gps; gps = gps->next) {
120                                 cache_ob->tot_vertex += gps->totpoints + 3;
121                                 cache_ob->tot_triangles += gps->totpoints - 1;
122                         }
123                         if ((!is_multiedit) && (!is_onion)) {
124                                 break;
125                         }
126                 }
127         }
128
129         cache->b_fill.tot_vertex = cache_ob->tot_triangles * 3;
130         cache->b_stroke.tot_vertex = cache_ob->tot_vertex;
131         cache->b_point.tot_vertex = cache_ob->tot_vertex;
132         cache->b_edit.tot_vertex = cache_ob->tot_vertex;
133         cache->b_edlin.tot_vertex = cache_ob->tot_vertex;
134
135         /* some modifiers can change the number of points */
136         int factor = 0;
137         GpencilModifierData *md;
138         for (md = ob->greasepencil_modifiers.first; md; md = md->next) {
139                 const GpencilModifierTypeInfo *mti = BKE_gpencil_modifierType_getInfo(md->type);
140                 /* only modifiers that change size */
141                 if (mti && mti->getDuplicationFactor) {
142                         factor = mti->getDuplicationFactor(md);
143
144                         cache->b_fill.tot_vertex *= factor;
145                         cache->b_stroke.tot_vertex *= factor;
146                         cache->b_point.tot_vertex *= factor;
147                         cache->b_edit.tot_vertex *= factor;
148                         cache->b_edlin.tot_vertex *= factor;
149                 }
150         }
151 }
152
153 /* Helper for doing all the checks on whether a stroke can be drawn */
154 static bool gpencil_can_draw_stroke(
155         struct MaterialGPencilStyle *gp_style, const bGPDstroke *gps,
156         const bool onion, const bool is_mat_preview)
157 {
158         /* skip stroke if it doesn't have any valid data */
159         if ((gps->points == NULL) || (gps->totpoints < 1) || (gp_style == NULL)) {
160                 return false;
161         }
162
163         /* if mat preview render always visible */
164         if (is_mat_preview) {
165                 return true;
166         }
167
168         /* check if the color is visible */
169         if ((gp_style == NULL) ||
170             (gp_style->flag & GP_STYLE_COLOR_HIDE) ||
171             (onion && (gp_style->flag & GP_STYLE_COLOR_ONIONSKIN)))
172         {
173                 return false;
174         }
175
176         /* stroke can be drawn */
177         return true;
178 }
179
180 /* calc bounding box in 2d using flat projection data */
181 static void gpencil_calc_2d_bounding_box(
182         const float(*points2d)[2], int totpoints, float minv[2], float maxv[2])
183 {
184         minv[0] = points2d[0][0];
185         minv[1] = points2d[0][1];
186         maxv[0] = points2d[0][0];
187         maxv[1] = points2d[0][1];
188
189         for (int i = 1; i < totpoints; i++) {
190                 /* min */
191                 if (points2d[i][0] < minv[0]) {
192                         minv[0] = points2d[i][0];
193                 }
194                 if (points2d[i][1] < minv[1]) {
195                         minv[1] = points2d[i][1];
196                 }
197                 /* max */
198                 if (points2d[i][0] > maxv[0]) {
199                         maxv[0] = points2d[i][0];
200                 }
201                 if (points2d[i][1] > maxv[1]) {
202                         maxv[1] = points2d[i][1];
203                 }
204         }
205         /* use a perfect square */
206         if (maxv[0] > maxv[1]) {
207                 maxv[1] = maxv[0];
208         }
209         else {
210                 maxv[0] = maxv[1];
211         }
212 }
213
214 /* calc texture coordinates using flat projected points */
215 static void gpencil_calc_stroke_fill_uv(
216         const float(*points2d)[2], int totpoints, float minv[2], float maxv[2], float(*r_uv)[2])
217 {
218         float d[2];
219         d[0] = maxv[0] - minv[0];
220         d[1] = maxv[1] - minv[1];
221         for (int i = 0; i < totpoints; i++) {
222                 r_uv[i][0] = (points2d[i][0] - minv[0]) / d[0];
223                 r_uv[i][1] = (points2d[i][1] - minv[1]) / d[1];
224         }
225 }
226
227 /* recalc the internal geometry caches for fill and uvs */
228 static void DRW_gpencil_recalc_geometry_caches(
229         Object *ob, bGPDlayer *gpl, MaterialGPencilStyle *gp_style, bGPDstroke *gps)
230 {
231         if (gps->flag & GP_STROKE_RECALC_GEOMETRY) {
232                 /* Calculate triangles cache for filling area (must be done only after changes) */
233                 if ((gps->tot_triangles == 0) || (gps->triangles == NULL)) {
234                         if ((gps->totpoints > 2) &&
235                             ((gp_style->fill_rgba[3] > GPENCIL_ALPHA_OPACITY_THRESH) ||
236                              (gp_style->fill_style > 0) || (gpl->blend_mode != eGplBlendMode_Normal)))
237                         {
238                                 DRW_gpencil_triangulate_stroke_fill(ob, gps);
239                         }
240                 }
241
242                 /* calc uv data along the stroke */
243                 ED_gpencil_calc_stroke_uv(ob, gps);
244
245                 /* clear flag */
246                 gps->flag &= ~GP_STROKE_RECALC_GEOMETRY;
247         }
248 }
249
250 static void set_wireframe_color(
251         Object *ob, bGPDlayer *gpl, View3D *v3d,
252         GPENCIL_StorageList *stl,
253         MaterialGPencilStyle *gp_style, int id, const bool is_fill)
254 {
255         const DRWContextState *draw_ctx = DRW_context_state_get();
256         World *world = draw_ctx->scene->world;
257
258         float color[4];
259         if (((gp_style->stroke_rgba[3] < GPENCIL_ALPHA_OPACITY_THRESH) ||
260              (((gp_style->flag & GP_STYLE_STROKE_SHOW) == 0))) &&
261             (gp_style->fill_rgba[3] >= GPENCIL_ALPHA_OPACITY_THRESH))
262         {
263                 copy_v4_v4(color, gp_style->fill_rgba);
264         }
265         else {
266                 copy_v4_v4(color, gp_style->stroke_rgba);
267         }
268         float alpha = color[3];
269
270         /* wire color */
271         if ((v3d) && (id > -1)) {
272                 const char type = (
273                         (stl->shgroups[id].shading_type[0] == OB_WIRE) ?
274                         v3d->shading.wire_color_type :
275                         v3d->shading.color_type);
276                 /* if fill and wire, use background color */
277                 if ((is_fill) && (stl->shgroups[id].shading_type[0] == OB_WIRE)) {
278                         if (v3d->shading.background_type == V3D_SHADING_BACKGROUND_THEME) {
279                                 UI_GetThemeColor4fv(TH_BACK, stl->shgroups[id].wire_color);
280                                 stl->shgroups[id].wire_color[3] = 1.0f;
281                         }
282                         else if (v3d->shading.background_type == V3D_SHADING_BACKGROUND_WORLD) {
283                                 color[0] = world->horr;
284                                 color[1] = world->horg;
285                                 color[2] = world->horb;
286                                 color[3] = 1.0f;
287                                 linearrgb_to_srgb_v4(stl->shgroups[id].wire_color, color);
288                         }
289                         else {
290                                 copy_v3_v3(color, v3d->shading.background_color);
291                                 color[3] = 1.0f;
292                                 linearrgb_to_srgb_v4(stl->shgroups[id].wire_color, color);
293                         }
294                         return;
295                 }
296
297                 /* strokes */
298                 switch (type) {
299                         case V3D_SHADING_SINGLE_COLOR:
300                         {
301                                 if (stl->shgroups[id].shading_type[0] == OB_WIRE) {
302                                         UI_GetThemeColor4fv(TH_WIRE, color);
303                                 }
304                                 else {
305                                         copy_v3_v3(color, v3d->shading.single_color);
306                                 }
307                                 color[3] = alpha;
308                                 linearrgb_to_srgb_v4(stl->shgroups[id].wire_color, color);
309                                 break;
310                         }
311                         case V3D_SHADING_OBJECT_COLOR:
312                         {
313                                 copy_v4_v4(color, ob->color);
314                                 color[3] = alpha;
315                                 linearrgb_to_srgb_v4(stl->shgroups[id].wire_color, color);
316                                 break;
317                         }
318                         case V3D_SHADING_RANDOM_COLOR:
319                         {
320                                 uint gpl_hash = 1;
321                                 uint ob_hash = BLI_ghashutil_strhash_p_murmur(ob->id.name);
322                                 if (gpl) {
323                                         gpl_hash = BLI_ghashutil_strhash_p_murmur(gpl->info);
324                                 }
325
326                                 float hue = BLI_hash_int_01(ob_hash * gpl_hash);
327                                 float hsv[3] = { hue, 0.40f, 0.8f };
328                                 float wire_col[3];
329                                 hsv_to_rgb_v(hsv, &wire_col[0]);
330
331                                 stl->shgroups[id].wire_color[0] = wire_col[0];
332                                 stl->shgroups[id].wire_color[1] = wire_col[1];
333                                 stl->shgroups[id].wire_color[2] = wire_col[2];
334                                 stl->shgroups[id].wire_color[3] = alpha;
335                                 break;
336                         }
337                         default:
338                         {
339                                 copy_v4_v4(stl->shgroups[id].wire_color, color);
340                                 break;
341                         }
342                 }
343         }
344         else {
345                 copy_v4_v4(stl->shgroups[id].wire_color, color);
346         }
347
348         /* if solid, the alpha must be set to 1.0 */
349         if (stl->shgroups[id].shading_type[0] == OB_SOLID) {
350                 stl->shgroups[id].wire_color[3] = 1.0f;
351         }
352 }
353
354 /* create shading group for filling */
355 static DRWShadingGroup *DRW_gpencil_shgroup_fill_create(
356         GPENCIL_e_data *e_data, GPENCIL_Data *vedata, DRWPass *pass,
357         GPUShader *shader, Object *ob, bGPdata *gpd, bGPDlayer *gpl,
358         MaterialGPencilStyle *gp_style, int id, int shading_type[2])
359 {
360         GPENCIL_StorageList *stl = ((GPENCIL_Data *)vedata)->stl;
361         const DRWContextState *draw_ctx = DRW_context_state_get();
362         View3D *v3d = draw_ctx->v3d;
363
364         /* e_data.gpencil_fill_sh */
365         DRWShadingGroup *grp = DRW_shgroup_create(shader, pass);
366
367         DRW_shgroup_uniform_vec4(grp, "color2", gp_style->mix_rgba, 1);
368
369         /* set style type */
370         switch (gp_style->fill_style) {
371                 case GP_STYLE_FILL_STYLE_SOLID:
372                         stl->shgroups[id].fill_style = SOLID;
373                         break;
374                 case GP_STYLE_FILL_STYLE_GRADIENT:
375                         if (gp_style->gradient_type == GP_STYLE_GRADIENT_LINEAR) {
376                                 stl->shgroups[id].fill_style = GRADIENT;
377                         }
378                         else {
379                                 stl->shgroups[id].fill_style = RADIAL;
380                         }
381                         break;
382                 case GP_STYLE_FILL_STYLE_CHESSBOARD:
383                         stl->shgroups[id].fill_style = CHESS;
384                         break;
385                 case GP_STYLE_FILL_STYLE_TEXTURE:
386                         if (gp_style->flag & GP_STYLE_FILL_PATTERN) {
387                                 stl->shgroups[id].fill_style = PATTERN;
388                         }
389                         else {
390                                 stl->shgroups[id].fill_style = TEXTURE;
391                         }
392                         break;
393                 default:
394                         stl->shgroups[id].fill_style = GP_STYLE_FILL_STYLE_SOLID;
395                         break;
396         }
397         DRW_shgroup_uniform_int(grp, "fill_type", &stl->shgroups[id].fill_style, 1);
398
399         DRW_shgroup_uniform_float(grp, "mix_factor", &gp_style->mix_factor, 1);
400
401         DRW_shgroup_uniform_float(grp, "gradient_angle", &gp_style->gradient_angle, 1);
402         DRW_shgroup_uniform_float(grp, "gradient_radius", &gp_style->gradient_radius, 1);
403         DRW_shgroup_uniform_float(grp, "pattern_gridsize", &gp_style->pattern_gridsize, 1);
404         DRW_shgroup_uniform_vec2(grp, "gradient_scale", gp_style->gradient_scale, 1);
405         DRW_shgroup_uniform_vec2(grp, "gradient_shift", gp_style->gradient_shift, 1);
406
407         DRW_shgroup_uniform_float(grp, "texture_angle", &gp_style->texture_angle, 1);
408         DRW_shgroup_uniform_vec2(grp, "texture_scale", gp_style->texture_scale, 1);
409         DRW_shgroup_uniform_vec2(grp, "texture_offset", gp_style->texture_offset, 1);
410         DRW_shgroup_uniform_float(grp, "texture_opacity", &gp_style->texture_opacity, 1);
411         DRW_shgroup_uniform_float(grp, "layer_opacity", &gpl->opacity, 1);
412
413         stl->shgroups[id].texture_mix = gp_style->flag & GP_STYLE_COLOR_TEX_MIX ? 1 : 0;
414         DRW_shgroup_uniform_int(grp, "texture_mix", &stl->shgroups[id].texture_mix, 1);
415
416         stl->shgroups[id].texture_flip = gp_style->flag & GP_STYLE_COLOR_FLIP_FILL ? 1 : 0;
417         DRW_shgroup_uniform_int(grp, "texture_flip", &stl->shgroups[id].texture_flip, 1);
418
419         stl->shgroups[id].xray_mode = (ob->dtx & OB_DRAWXRAY) ? GP_XRAY_FRONT : GP_XRAY_3DSPACE;
420         DRW_shgroup_uniform_int(grp, "xraymode", &stl->shgroups[id].xray_mode, 1);
421         DRW_shgroup_uniform_int(grp, "drawmode", (const int *) &gpd->draw_mode, 1);
422
423         /* viewport x-ray */
424         DRW_shgroup_uniform_int(grp, "viewport_xray", &stl->storage->is_xray, 1);
425
426         /* shading type */
427         stl->shgroups[id].shading_type[0] = GPENCIL_USE_SOLID(stl) ? (int)OB_RENDER : shading_type[0];
428         if (v3d) {
429                 stl->shgroups[id].shading_type[1] = (
430                         (stl->shgroups[id].shading_type[0] == OB_WIRE) ?
431                         v3d->shading.wire_color_type :
432                         v3d->shading.color_type);
433         }
434
435         DRW_shgroup_uniform_int(grp, "shading_type", &stl->shgroups[id].shading_type[0], 2);
436
437         /* wire color */
438         set_wireframe_color(ob, gpl, v3d, stl, gp_style, id, true);
439         DRW_shgroup_uniform_vec4(grp, "wire_color", stl->shgroups[id].wire_color, 1);
440
441         /* image texture */
442         if ((gp_style->flag & GP_STYLE_COLOR_TEX_MIX) ||
443             (gp_style->fill_style & GP_STYLE_FILL_STYLE_TEXTURE))
444         {
445                 ImBuf *ibuf;
446                 Image *image = gp_style->ima;
447                 ImageUser iuser = { NULL };
448                 void *lock;
449
450                 iuser.ok = true;
451
452                 ibuf = BKE_image_acquire_ibuf(image, &iuser, &lock);
453
454                 if (ibuf == NULL || ibuf->rect == NULL) {
455                         BKE_image_release_ibuf(image, ibuf, NULL);
456                 }
457                 else {
458                         GPUTexture *texture = GPU_texture_from_blender(gp_style->ima, &iuser, GL_TEXTURE_2D, true);
459                         DRW_shgroup_uniform_texture(grp, "myTexture", texture);
460
461                         stl->shgroups[id].texture_clamp = gp_style->flag & GP_STYLE_COLOR_TEX_CLAMP ? 1 : 0;
462                         DRW_shgroup_uniform_int(grp, "texture_clamp", &stl->shgroups[id].texture_clamp, 1);
463
464                         BKE_image_release_ibuf(image, ibuf, NULL);
465                 }
466         }
467         else {
468                 /* if no texture defined, need a blank texture to avoid errors in draw manager */
469                 DRW_shgroup_uniform_texture(grp, "myTexture", e_data->gpencil_blank_texture);
470                 stl->shgroups[id].texture_clamp = 0;
471                 DRW_shgroup_uniform_int(grp, "texture_clamp", &stl->shgroups[id].texture_clamp, 1);
472         }
473
474         return grp;
475 }
476
477 /* check if some onion is enabled */
478 bool DRW_gpencil_onion_active(bGPdata *gpd)
479 {
480         for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) {
481                 if (gpl->onion_flag & GP_LAYER_ONIONSKIN) {
482                         return true;
483                 }
484         }
485         return false;
486 }
487
488 /* create shading group for strokes */
489 DRWShadingGroup *DRW_gpencil_shgroup_stroke_create(
490         GPENCIL_e_data *e_data, GPENCIL_Data *vedata, DRWPass *pass, GPUShader *shader, Object *ob,
491         bGPdata *gpd, bGPDlayer *gpl, bGPDstroke *gps,
492         MaterialGPencilStyle *gp_style, int id,
493         bool onion, const float scale, const int shading_type[2])
494 {
495         GPENCIL_StorageList *stl = ((GPENCIL_Data *)vedata)->stl;
496         const float *viewport_size = DRW_viewport_size_get();
497         const DRWContextState *draw_ctx = DRW_context_state_get();
498         View3D *v3d = draw_ctx->v3d;
499
500         /* e_data.gpencil_stroke_sh */
501         DRWShadingGroup *grp = DRW_shgroup_create(shader, pass);
502
503         DRW_shgroup_uniform_vec2(grp, "Viewport", viewport_size, 1);
504
505         DRW_shgroup_uniform_float(grp, "pixsize", stl->storage->pixsize, 1);
506
507         /* avoid wrong values */
508         if ((gpd) && (gpd->pixfactor == 0)) {
509                 gpd->pixfactor = GP_DEFAULT_PIX_FACTOR;
510         }
511
512         /* object scale and depth */
513         if ((ob) && (id > -1)) {
514                 stl->shgroups[id].obj_scale = scale;
515                 DRW_shgroup_uniform_float(grp, "objscale", &stl->shgroups[id].obj_scale, 1);
516                 stl->shgroups[id].keep_size = (int)((gpd) && (gpd->flag & GP_DATA_STROKE_KEEPTHICKNESS));
517                 DRW_shgroup_uniform_int(grp, "keep_size", &stl->shgroups[id].keep_size, 1);
518
519                 stl->shgroups[id].stroke_style = gp_style->stroke_style;
520                 stl->shgroups[id].color_type = GPENCIL_COLOR_SOLID;
521                 if ((gp_style->stroke_style == GP_STYLE_STROKE_STYLE_TEXTURE) && (!onion)) {
522                         stl->shgroups[id].color_type = GPENCIL_COLOR_TEXTURE;
523                         if (gp_style->flag & GP_STYLE_STROKE_PATTERN) {
524                                 stl->shgroups[id].color_type = GPENCIL_COLOR_PATTERN;
525                         }
526                 }
527                 DRW_shgroup_uniform_int(grp, "color_type", &stl->shgroups[id].color_type, 1);
528                 DRW_shgroup_uniform_float(grp, "pixfactor", &gpd->pixfactor, 1);
529
530                 stl->shgroups[id].caps_mode[0] = gps->caps[0];
531                 stl->shgroups[id].caps_mode[1] = gps->caps[1];
532                 DRW_shgroup_uniform_int(grp, "caps_mode", &stl->shgroups[id].caps_mode[0], 2);
533
534                 /* viewport x-ray */
535                 DRW_shgroup_uniform_int(grp, "viewport_xray", &stl->storage->is_xray, 1);
536
537                 stl->shgroups[id].shading_type[0] = (GPENCIL_USE_SOLID(stl) || onion) ? (int)OB_RENDER : shading_type[0];
538                 if (v3d) {
539                         stl->shgroups[id].shading_type[1] = (
540                                 (stl->shgroups[id].shading_type[0] == OB_WIRE) ?
541                                 v3d->shading.wire_color_type :
542                                 v3d->shading.color_type);
543                 }
544                 DRW_shgroup_uniform_int(grp, "shading_type", &stl->shgroups[id].shading_type[0], 2);
545
546                 /* wire color */
547                 set_wireframe_color(ob, gpl, v3d, stl, gp_style, id, false);
548                 DRW_shgroup_uniform_vec4(grp, "wire_color", stl->shgroups[id].wire_color, 1);
549         }
550         else {
551                 stl->storage->obj_scale = 1.0f;
552                 stl->storage->keep_size = 0;
553                 stl->storage->pixfactor = GP_DEFAULT_PIX_FACTOR;
554                 DRW_shgroup_uniform_float(grp, "objscale", &stl->storage->obj_scale, 1);
555                 DRW_shgroup_uniform_int(grp, "keep_size", &stl->storage->keep_size, 1);
556                 DRW_shgroup_uniform_int(grp, "color_type", &stl->storage->color_type, 1);
557                 if (gpd) {
558                         DRW_shgroup_uniform_float(grp, "pixfactor", &gpd->pixfactor, 1);
559                 }
560                 else {
561                         DRW_shgroup_uniform_float(grp, "pixfactor", &stl->storage->pixfactor, 1);
562                 }
563                 const int zero[2] = { 0, 0 };
564                 DRW_shgroup_uniform_int(grp, "caps_mode", &zero[0], 2);
565
566                 /* viewport x-ray */
567                 DRW_shgroup_uniform_int(grp, "viewport_xray", &stl->storage->is_xray, 1);
568                 DRW_shgroup_uniform_int(grp, "shading_type", (const int *)&stl->storage->shade_render, 2);
569         }
570
571         if ((gpd) && (id > -1)) {
572                 stl->shgroups[id].xray_mode = (ob->dtx & OB_DRAWXRAY) ? GP_XRAY_FRONT : GP_XRAY_3DSPACE;
573                 DRW_shgroup_uniform_int(grp, "xraymode", &stl->shgroups[id].xray_mode, 1);
574         }
575         else {
576                 /* for drawing always on predefined z-depth */
577                 DRW_shgroup_uniform_int(grp, "xraymode", &stl->storage->xray, 1);
578         }
579
580         /* image texture for pattern */
581         if ((gp_style) && (gp_style->stroke_style == GP_STYLE_STROKE_STYLE_TEXTURE) && (!onion)) {
582                 ImBuf *ibuf;
583                 Image *image = gp_style->sima;
584                 ImageUser iuser = { NULL };
585                 void *lock;
586
587                 iuser.ok = true;
588
589                 ibuf = BKE_image_acquire_ibuf(image, &iuser, &lock);
590
591                 if (ibuf == NULL || ibuf->rect == NULL) {
592                         BKE_image_release_ibuf(image, ibuf, NULL);
593                 }
594                 else {
595                         GPUTexture *texture = GPU_texture_from_blender(gp_style->sima, &iuser, GL_TEXTURE_2D, true);
596                         DRW_shgroup_uniform_texture(grp, "myTexture", texture);
597
598                         BKE_image_release_ibuf(image, ibuf, NULL);
599                 }
600         }
601         else {
602                 /* if no texture defined, need a blank texture to avoid errors in draw manager */
603                 DRW_shgroup_uniform_texture(grp, "myTexture", e_data->gpencil_blank_texture);
604         }
605
606         return grp;
607 }
608
609 /* create shading group for points */
610 static DRWShadingGroup *DRW_gpencil_shgroup_point_create(
611         GPENCIL_e_data *e_data, GPENCIL_Data *vedata, DRWPass *pass, GPUShader *shader, Object *ob,
612         bGPdata *gpd, bGPDlayer *gpl,
613         MaterialGPencilStyle *gp_style, int id, bool onion,
614         const float scale, const int shading_type[2])
615 {
616         GPENCIL_StorageList *stl = ((GPENCIL_Data *)vedata)->stl;
617         const float *viewport_size = DRW_viewport_size_get();
618         const DRWContextState *draw_ctx = DRW_context_state_get();
619         View3D *v3d = draw_ctx->v3d;
620
621         /* e_data.gpencil_stroke_sh */
622         DRWShadingGroup *grp = DRW_shgroup_create(shader, pass);
623
624         DRW_shgroup_uniform_vec2(grp, "Viewport", viewport_size, 1);
625         DRW_shgroup_uniform_float(grp, "pixsize", stl->storage->pixsize, 1);
626
627         /* avoid wrong values */
628         if ((gpd) && (gpd->pixfactor == 0)) {
629                 gpd->pixfactor = GP_DEFAULT_PIX_FACTOR;
630         }
631
632         /* object scale and depth */
633         if ((ob) && (id > -1)) {
634                 stl->shgroups[id].obj_scale = scale;
635                 DRW_shgroup_uniform_float(grp, "objscale", &stl->shgroups[id].obj_scale, 1);
636                 stl->shgroups[id].keep_size = (int)((gpd) && (gpd->flag & GP_DATA_STROKE_KEEPTHICKNESS));
637                 DRW_shgroup_uniform_int(grp, "keep_size", &stl->shgroups[id].keep_size, 1);
638
639                 stl->shgroups[id].mode = gp_style->mode;
640                 stl->shgroups[id].stroke_style = gp_style->stroke_style;
641                 stl->shgroups[id].color_type = GPENCIL_COLOR_SOLID;
642                 if ((gp_style->stroke_style == GP_STYLE_STROKE_STYLE_TEXTURE) && (!onion)) {
643                         stl->shgroups[id].color_type = GPENCIL_COLOR_TEXTURE;
644                         if (gp_style->flag & GP_STYLE_STROKE_PATTERN) {
645                                 stl->shgroups[id].color_type = GPENCIL_COLOR_PATTERN;
646                         }
647                 }
648                 DRW_shgroup_uniform_int(grp, "color_type", &stl->shgroups[id].color_type, 1);
649                 DRW_shgroup_uniform_int(grp, "mode", &stl->shgroups[id].mode, 1);
650                 DRW_shgroup_uniform_float(grp, "pixfactor", &gpd->pixfactor, 1);
651
652                 /* viewport x-ray */
653                 DRW_shgroup_uniform_int(grp, "viewport_xray", &stl->storage->is_xray, 1);
654
655                 stl->shgroups[id].shading_type[0] = (GPENCIL_USE_SOLID(stl) || onion) ? (int)OB_RENDER : shading_type[0];
656                 if (v3d) {
657                         stl->shgroups[id].shading_type[1] = (
658                                 (stl->shgroups[id].shading_type[0] == OB_WIRE) ?
659                                 v3d->shading.wire_color_type :
660                                 v3d->shading.color_type);
661                 }
662                 DRW_shgroup_uniform_int(grp, "shading_type", &stl->shgroups[id].shading_type[0], 2);
663
664                 /* wire color */
665                 set_wireframe_color(ob, gpl, v3d, stl, gp_style, id, false);
666                 DRW_shgroup_uniform_vec4(grp, "wire_color", stl->shgroups[id].wire_color, 1);
667
668         }
669         else {
670                 stl->storage->obj_scale = 1.0f;
671                 stl->storage->keep_size = 0;
672                 stl->storage->pixfactor = GP_DEFAULT_PIX_FACTOR;
673                 stl->storage->mode = gp_style->mode;
674                 DRW_shgroup_uniform_float(grp, "objscale", &stl->storage->obj_scale, 1);
675                 DRW_shgroup_uniform_int(grp, "keep_size", &stl->storage->keep_size, 1);
676                 DRW_shgroup_uniform_int(grp, "color_type", &stl->storage->color_type, 1);
677                 DRW_shgroup_uniform_int(grp, "mode", &stl->storage->mode, 1);
678                 if (gpd) {
679                         DRW_shgroup_uniform_float(grp, "pixfactor", &gpd->pixfactor, 1);
680                 }
681                 else {
682                         DRW_shgroup_uniform_float(grp, "pixfactor", &stl->storage->pixfactor, 1);
683                 }
684                 /* viewport x-ray */
685                 DRW_shgroup_uniform_int(grp, "viewport_xray", &stl->storage->is_xray, 1);
686                 DRW_shgroup_uniform_int(grp, "shading_type", (const int *)&stl->storage->shade_render, 2);
687         }
688
689         if ((gpd) && (id > -1)) {
690                 stl->shgroups[id].xray_mode = (ob->dtx & OB_DRAWXRAY) ? GP_XRAY_FRONT : GP_XRAY_3DSPACE;
691                 DRW_shgroup_uniform_int(grp, "xraymode", (const int *)&stl->shgroups[id].xray_mode, 1);
692         }
693         else {
694                 /* for drawing always on predefined z-depth */
695                 DRW_shgroup_uniform_int(grp, "xraymode", &stl->storage->xray, 1);
696         }
697
698         /* image texture */
699         if ((gp_style) && (gp_style->stroke_style == GP_STYLE_STROKE_STYLE_TEXTURE) && (!onion)) {
700                 ImBuf *ibuf;
701                 Image *image = gp_style->sima;
702                 ImageUser iuser = { NULL };
703                 void *lock;
704
705                 iuser.ok = true;
706
707                 ibuf = BKE_image_acquire_ibuf(image, &iuser, &lock);
708
709                 if (ibuf == NULL || ibuf->rect == NULL) {
710                         BKE_image_release_ibuf(image, ibuf, NULL);
711                 }
712                 else {
713                         GPUTexture *texture = GPU_texture_from_blender(gp_style->sima, &iuser, GL_TEXTURE_2D, true);
714                         DRW_shgroup_uniform_texture(grp, "myTexture", texture);
715
716                         BKE_image_release_ibuf(image, ibuf, NULL);
717                 }
718         }
719         else {
720                 /* if no texture defined, need a blank texture to avoid errors in draw manager */
721                 DRW_shgroup_uniform_texture(grp, "myTexture", e_data->gpencil_blank_texture);
722         }
723
724         return grp;
725 }
726
727 /* add fill vertex info  */
728 static void gpencil_add_fill_vertexdata(
729         GpencilBatchCache *cache,
730         Object *ob, bGPDlayer *gpl, bGPDframe *gpf, bGPDstroke *gps,
731         float opacity, const float tintcolor[4], const bool onion, const bool custonion)
732 {
733         MaterialGPencilStyle *gp_style = BKE_material_gpencil_settings_get(ob, gps->mat_nr + 1);
734         if (gps->totpoints >= 3) {
735                 float tfill[4];
736                 /* set color using material, tint color and opacity */
737                 interp_v3_v3v3(tfill, gps->runtime.tmp_fill_rgba, tintcolor, tintcolor[3]);
738                 tfill[3] = gps->runtime.tmp_fill_rgba[3] * opacity;
739                 if ((tfill[3] > GPENCIL_ALPHA_OPACITY_THRESH) ||
740                     (gp_style->fill_style > 0) ||
741                     (gpl->blend_mode != eGplBlendMode_Normal))
742                 {
743                         if (cache->is_dirty) {
744                                 const float *color;
745                                 if (!onion) {
746                                         color = tfill;
747                                 }
748                                 else {
749                                         if (custonion) {
750                                                 color = tintcolor;
751                                         }
752                                         else {
753                                                 ARRAY_SET_ITEMS(tfill, UNPACK3(gps->runtime.tmp_fill_rgba), tintcolor[3]);
754                                                 color = tfill;
755                                         }
756                                 }
757                                 /* create vertex data */
758                                 const int old_len = cache->b_fill.vbo_len;
759                                 DRW_gpencil_get_fill_geom(&cache->b_fill, ob, gps, color);
760
761                                 /* add to list of groups */
762                                 if (old_len < cache->b_fill.vbo_len) {
763                                         cache->grp_cache = gpencil_group_cache_add(
764                                                 cache->grp_cache, gpl, gpf, gps,
765                                                 eGpencilBatchGroupType_Fill, onion,
766                                                 cache->b_fill.vbo_len,
767                                                 &cache->grp_size, &cache->grp_used);
768                                 }
769                         }
770                 }
771         }
772 }
773
774 /* add stroke vertex info */
775 static void gpencil_add_stroke_vertexdata(
776         GpencilBatchCache *cache,
777         Object *ob, bGPDlayer *gpl, bGPDframe *gpf, bGPDstroke *gps,
778         const float opacity, const float tintcolor[4], const bool onion,
779         const bool custonion)
780 {
781         float tcolor[4];
782         float ink[4];
783         short sthickness;
784         MaterialGPencilStyle *gp_style = BKE_material_gpencil_settings_get(ob, gps->mat_nr + 1);
785
786         /* set color using base color, tint color and opacity */
787         if (cache->is_dirty) {
788                 if (!onion) {
789                         /* if special stroke, use fill color as stroke color */
790                         if (gps->flag & GP_STROKE_NOFILL) {
791                                 interp_v3_v3v3(tcolor, gps->runtime.tmp_fill_rgba, tintcolor, tintcolor[3]);
792                                 tcolor[3] = gps->runtime.tmp_fill_rgba[3] * opacity;
793                         }
794                         else {
795                                 interp_v3_v3v3(tcolor, gps->runtime.tmp_stroke_rgba, tintcolor, tintcolor[3]);
796                                 tcolor[3] = gps->runtime.tmp_stroke_rgba[3] * opacity;
797                         }
798                         copy_v4_v4(ink, tcolor);
799                 }
800                 else {
801                         if (custonion) {
802                                 copy_v4_v4(ink, tintcolor);
803                         }
804                         else {
805                                 ARRAY_SET_ITEMS(tcolor, UNPACK3(gps->runtime.tmp_stroke_rgba), opacity);
806                                 copy_v4_v4(ink, tcolor);
807                         }
808                 }
809
810                 sthickness = gps->thickness + gpl->line_change;
811                 CLAMP_MIN(sthickness, 1);
812
813                 if ((gps->totpoints > 1) && (gp_style->mode == GP_STYLE_MODE_LINE)) {
814                         /* create vertex data */
815                         const int old_len = cache->b_stroke.vbo_len;
816                         DRW_gpencil_get_stroke_geom(&cache->b_stroke, gps, sthickness, ink);
817
818                         /* add to list of groups */
819                         if (old_len < cache->b_stroke.vbo_len) {
820                                 cache->grp_cache = gpencil_group_cache_add(
821                                         cache->grp_cache, gpl, gpf, gps,
822                                         eGpencilBatchGroupType_Stroke, onion,
823                                         cache->b_stroke.vbo_len,
824                                         &cache->grp_size, &cache->grp_used);
825                         }
826                 }
827                 else {
828                         /* create vertex data */
829                         const int old_len = cache->b_point.vbo_len;
830                         DRW_gpencil_get_point_geom(&cache->b_point, gps, sthickness, ink);
831
832                         /* add to list of groups */
833                         if (old_len < cache->b_point.vbo_len) {
834                                 cache->grp_cache = gpencil_group_cache_add(
835                                         cache->grp_cache, gpl, gpf, gps, eGpencilBatchGroupType_Point, onion,
836                                         cache->b_point.vbo_len,
837                                         &cache->grp_size, &cache->grp_used);
838                         }
839                 }
840         }
841 }
842
843 /* add edit points vertex info */
844 static void gpencil_add_editpoints_vertexdata(
845         GpencilBatchCache *cache, Object *ob,
846         bGPdata *gpd, bGPDlayer *gpl, bGPDframe *gpf, bGPDstroke *gps)
847 {
848         const DRWContextState *draw_ctx = DRW_context_state_get();
849         View3D *v3d = draw_ctx->v3d;
850         MaterialGPencilStyle *gp_style = BKE_material_gpencil_settings_get(ob, gps->mat_nr + 1);
851
852         /* alpha factor for edit points/line to make them more subtle */
853         float edit_alpha = v3d->vertex_opacity;
854
855         if (GPENCIL_ANY_EDIT_MODE(gpd)) {
856                 Object *obact = DRW_context_state_get()->obact;
857                 if ((!obact) || (obact->type != OB_GPENCIL)) {
858                         return;
859                 }
860                 const bool is_weight_paint = (gpd) && (gpd->flag & GP_DATA_STROKE_WEIGHTMODE);
861
862                 if (cache->is_dirty) {
863                         if ((obact == ob) &&
864                             ((v3d->flag2 & V3D_HIDE_OVERLAYS) == 0) &&
865                             (v3d->gp_flag & V3D_GP_SHOW_EDIT_LINES))
866                         {
867                                 /* line of the original stroke */
868                                 DRW_gpencil_get_edlin_geom(&cache->b_edlin, gps, edit_alpha, gpd->flag);
869
870                                 /* add to list of groups */
871                                 cache->grp_cache = gpencil_group_cache_add(
872                                         cache->grp_cache, gpl, gpf, gps,
873                                         eGpencilBatchGroupType_Edlin, false,
874                                         cache->b_edlin.vbo_len,
875                                         &cache->grp_size, &cache->grp_used);
876                         }
877                         /* edit points */
878                         if ((gps->flag & GP_STROKE_SELECT) || (is_weight_paint)) {
879                                 if ((gpl->flag & GP_LAYER_UNLOCK_COLOR) || ((gp_style->flag & GP_STYLE_COLOR_LOCKED) == 0)) {
880                                         if (obact == ob) {
881                                                 DRW_gpencil_get_edit_geom(&cache->b_edit, gps, edit_alpha, gpd->flag);
882
883                                                 /* add to list of groups */
884                                                 cache->grp_cache = gpencil_group_cache_add(
885                                                         cache->grp_cache, gpl, gpf, gps,
886                                                         eGpencilBatchGroupType_Edit, false,
887                                                         cache->b_edit.vbo_len,
888                                                         &cache->grp_size, &cache->grp_used);
889                                         }
890                                 }
891                         }
892                 }
893         }
894 }
895
896 /* main function to draw strokes */
897 static void gpencil_draw_strokes(
898         GpencilBatchCache *cache, GPENCIL_e_data *e_data, void *vedata, Object *ob,
899         bGPdata *gpd, bGPDlayer *gpl, bGPDframe *src_gpf, bGPDframe *derived_gpf,
900         const float opacity, const float tintcolor[4],
901         const bool custonion, tGPencilObjectCache *cache_ob)
902 {
903         GPENCIL_PassList *psl = ((GPENCIL_Data *)vedata)->psl;
904         GPENCIL_StorageList *stl = ((GPENCIL_Data *)vedata)->stl;
905         const DRWContextState *draw_ctx = DRW_context_state_get();
906         Scene *scene = draw_ctx->scene;
907         View3D *v3d = draw_ctx->v3d;
908         bGPDstroke *gps, *src_gps;
909         float viewmatrix[4][4];
910         const bool is_multiedit = (bool)GPENCIL_MULTIEDIT_SESSIONS_ON(gpd);
911         const bool playing = stl->storage->is_playing;
912         const bool is_render = (bool)stl->storage->is_render;
913         const bool is_mat_preview = (bool)stl->storage->is_mat_preview;
914         const bool overlay_multiedit = v3d != NULL ? (v3d->gp_flag & V3D_GP_SHOW_MULTIEDIT_LINES) : true;
915
916         /* Get evaluation context */
917         /* NOTE: We must check if C is valid, otherwise we get crashes when trying to save files
918          * (i.e. the thumbnail offscreen rendering fails)
919          */
920         Depsgraph *depsgraph = DRW_context_state_get()->depsgraph;
921
922         /* get parent matrix and save as static data */
923         ED_gpencil_parent_location(depsgraph, ob, gpd, gpl, viewmatrix);
924         copy_m4_m4(derived_gpf->runtime.viewmatrix, viewmatrix);
925
926         if ((cache_ob != NULL) && (cache_ob->is_dup_ob)) {
927                 copy_m4_m4(derived_gpf->runtime.viewmatrix, cache_ob->obmat);
928         }
929
930         /* apply geometry modifiers */
931         if ((cache->is_dirty) && (ob->greasepencil_modifiers.first) && (!is_multiedit)) {
932                 if (!stl->storage->simplify_modif) {
933                         if (BKE_gpencil_has_geometry_modifiers(ob)) {
934                                 BKE_gpencil_geometry_modifiers(depsgraph, ob, gpl, derived_gpf, stl->storage->is_render);
935                         }
936                 }
937         }
938
939         if (src_gpf) {
940                 src_gps = src_gpf->strokes.first;
941         }
942         else {
943                 src_gps = NULL;
944         }
945
946         for (gps = derived_gpf->strokes.first; gps; gps = gps->next) {
947                 MaterialGPencilStyle *gp_style = BKE_material_gpencil_settings_get(ob, gps->mat_nr + 1);
948
949                 /* check if stroke can be drawn */
950                 if (gpencil_can_draw_stroke(gp_style, gps, false, is_mat_preview) == false) {
951                         GP_SET_SRC_GPS(src_gps);
952                         continue;
953                 }
954
955                 /* be sure recalc all cache in source stroke to avoid recalculation when frame change
956                  * and improve fps */
957                 if (src_gps) {
958                         DRW_gpencil_recalc_geometry_caches(ob, gpl, gp_style, src_gps);
959                 }
960
961                 /* if the fill has any value, it's considered a fill and is not drawn if simplify fill is enabled */
962                 if ((stl->storage->simplify_fill) &&
963                     (scene->r.simplify_gpencil & SIMPLIFY_GPENCIL_REMOVE_FILL_LINE))
964                 {
965                         if ((gp_style->fill_rgba[3] > GPENCIL_ALPHA_OPACITY_THRESH) ||
966                             (gp_style->fill_style > GP_STYLE_FILL_STYLE_SOLID) ||
967                             (gpl->blend_mode != eGplBlendMode_Normal))
968                         {
969                                 GP_SET_SRC_GPS(src_gps);
970                                 continue;
971                         }
972                 }
973
974                 if ((gpl->actframe->framenum == derived_gpf->framenum) ||
975                     (!is_multiedit) || (overlay_multiedit))
976                 {
977                         /* copy color to temp fields to apply temporal changes in the stroke */
978                         copy_v4_v4(gps->runtime.tmp_stroke_rgba, gp_style->stroke_rgba);
979                         copy_v4_v4(gps->runtime.tmp_fill_rgba, gp_style->fill_rgba);
980
981                         /* apply modifiers (only modify geometry, but not create ) */
982                         if ((cache->is_dirty) && (ob->greasepencil_modifiers.first) && (!is_multiedit)) {
983                                 if (!stl->storage->simplify_modif) {
984                                         BKE_gpencil_stroke_modifiers(depsgraph, ob, gpl, derived_gpf, gps, stl->storage->is_render);
985                                 }
986                         }
987
988                         /* hide any blend layer */
989                         if ((!stl->storage->simplify_blend) ||
990                             (gpl->blend_mode == eGplBlendMode_Normal))
991                         {
992                                 /* fill */
993                                 if ((gp_style->flag & GP_STYLE_FILL_SHOW) &&
994                                     (!stl->storage->simplify_fill) &&
995                                     ((gps->flag & GP_STROKE_NOFILL) == 0))
996                                 {
997                                         gpencil_add_fill_vertexdata(
998                                                 cache, ob, gpl, derived_gpf, gps,
999                                                 opacity, tintcolor, false, custonion);
1000                                 }
1001                                 /* stroke */
1002                                 /* No fill strokes, must show stroke always */
1003                                 if (((gp_style->flag & GP_STYLE_STROKE_SHOW) ||
1004                                      (gps->flag & GP_STROKE_NOFILL)) &&
1005                                     ((gp_style->stroke_rgba[3] > GPENCIL_ALPHA_OPACITY_THRESH) ||
1006                                      (gpl->blend_mode == eGplBlendMode_Normal)))
1007                                 {
1008                                         /* recalc strokes uv (geometry can be changed by modifiers) */
1009                                         if (gps->flag & GP_STROKE_RECALC_GEOMETRY) {
1010                                                 ED_gpencil_calc_stroke_uv(ob, gps);
1011                                         }
1012
1013                                         gpencil_add_stroke_vertexdata(
1014                                                 cache, ob, gpl, derived_gpf, gps,
1015                                                 opacity, tintcolor, false, custonion);
1016                                 }
1017                         }
1018                 }
1019
1020                 /* edit points (only in edit mode and not play animation not render) */
1021                 if ((draw_ctx->obact == ob) && (src_gps) &&
1022                     (!playing) && (!is_render) && (!cache_ob->is_dup_ob))
1023                 {
1024                         if ((gpl->flag & GP_LAYER_LOCKED) == 0) {
1025                                 if (!stl->g_data->shgrps_edit_line) {
1026                                         stl->g_data->shgrps_edit_line = DRW_shgroup_create(e_data->gpencil_line_sh, psl->edit_pass);
1027                                 }
1028                                 if (!stl->g_data->shgrps_edit_point) {
1029                                         stl->g_data->shgrps_edit_point = DRW_shgroup_create(e_data->gpencil_edit_point_sh, psl->edit_pass);
1030                                         const float *viewport_size = DRW_viewport_size_get();
1031                                         DRW_shgroup_uniform_vec2(stl->g_data->shgrps_edit_point, "Viewport", viewport_size, 1);
1032                                 }
1033
1034                                 gpencil_add_editpoints_vertexdata(cache, ob, gpd, gpl, derived_gpf, src_gps);
1035                         }
1036                 }
1037
1038                 GP_SET_SRC_GPS(src_gps);
1039         }
1040 }
1041
1042 /* get alpha factor for onion strokes */
1043 static void gpencil_get_onion_alpha(float color[4], bGPdata *gpd)
1044 {
1045 #define MIN_ALPHA_VALUE 0.01f
1046
1047         /* if fade is disabled, opacity is equal in all frames */
1048         if ((gpd->onion_flag & GP_ONION_FADE) == 0) {
1049                 color[3] = gpd->onion_factor;
1050         }
1051         else {
1052                 /* add override opacity factor */
1053                 color[3] += gpd->onion_factor - 0.5f;
1054         }
1055
1056         CLAMP(color[3], MIN_ALPHA_VALUE, 1.0f);
1057 }
1058
1059 /* function to draw strokes for onion only */
1060 static void gpencil_draw_onion_strokes(
1061         GpencilBatchCache *cache, void *vedata, Object *ob,
1062         bGPdata *gpd, bGPDlayer *gpl, bGPDframe *gpf,
1063         const float opacity, const float tintcolor[4], const bool custonion)
1064 {
1065         GPENCIL_StorageList *stl = ((GPENCIL_Data *)vedata)->stl;
1066         Depsgraph *depsgraph = DRW_context_state_get()->depsgraph;
1067
1068         float viewmatrix[4][4];
1069
1070         /* get parent matrix and save as static data */
1071         ED_gpencil_parent_location(depsgraph, ob, gpd, gpl, viewmatrix);
1072         copy_m4_m4(gpf->runtime.viewmatrix, viewmatrix);
1073
1074         for (bGPDstroke *gps = gpf->strokes.first; gps; gps = gps->next) {
1075                 MaterialGPencilStyle *gp_style = BKE_material_gpencil_settings_get(ob, gps->mat_nr + 1);
1076                 if (gp_style == NULL) {
1077                         continue;
1078                 }
1079                 copy_v4_v4(gps->runtime.tmp_stroke_rgba, gp_style->stroke_rgba);
1080                 copy_v4_v4(gps->runtime.tmp_fill_rgba, gp_style->fill_rgba);
1081
1082                 int id = stl->storage->shgroup_id;
1083                 /* check if stroke can be drawn */
1084                 if (gpencil_can_draw_stroke(gp_style, gps, true, false) == false) {
1085                         continue;
1086                 }
1087                 /* limit the number of shading groups */
1088                 if (id >= GPENCIL_MAX_SHGROUPS) {
1089                         continue;
1090                 }
1091
1092                 /* stroke */
1093                 gpencil_add_stroke_vertexdata(
1094                         cache, ob, gpl, gpf, gps, opacity, tintcolor,
1095                         true, custonion);
1096
1097                 stl->storage->shgroup_id++;
1098         }
1099 }
1100
1101 /* draw onion-skinning for a layer */
1102 static void gpencil_draw_onionskins(
1103         GpencilBatchCache *cache, void *vedata,
1104         Object *ob, bGPdata *gpd, bGPDlayer *gpl, bGPDframe *gpf)
1105 {
1106
1107         const float default_color[3] = { UNPACK3(U.gpencil_new_layer_col) };
1108         const float alpha = 1.0f;
1109         float color[4];
1110         int idx;
1111         float fac = 1.0f;
1112         int step = 0;
1113         int mode = 0;
1114         bool colflag = false;
1115         bGPDframe *gpf_loop = NULL;
1116         int last = gpf->framenum;
1117
1118         colflag = (bool)gpd->onion_flag & GP_ONION_GHOST_PREVCOL;
1119
1120         /* -------------------------------
1121          * 1) Draw Previous Frames First
1122          * ------------------------------- */
1123         step = gpd->gstep;
1124         mode = gpd->onion_mode;
1125
1126         if (gpd->onion_flag & GP_ONION_GHOST_PREVCOL) {
1127                 copy_v3_v3(color, gpd->gcolor_prev);
1128         }
1129         else {
1130                 copy_v3_v3(color, default_color);
1131         }
1132
1133         idx = 0;
1134         for (bGPDframe *gf = gpf->prev; gf; gf = gf->prev) {
1135                 /* only selected frames */
1136                 if ((mode == GP_ONION_MODE_SELECTED) && ((gf->flag & GP_FRAME_SELECT) == 0)) {
1137                         continue;
1138                 }
1139                 /* absolute range */
1140                 if (mode == GP_ONION_MODE_ABSOLUTE) {
1141                         if ((gpf->framenum - gf->framenum) > step) {
1142                                 break;
1143                         }
1144                 }
1145                 /* relative range */
1146                 if (mode == GP_ONION_MODE_RELATIVE) {
1147                         idx++;
1148                         if (idx > step) {
1149                                 break;
1150                         }
1151
1152                 }
1153                 /* alpha decreases with distance from curframe index */
1154                 if (mode != GP_ONION_MODE_SELECTED) {
1155                         if (mode == GP_ONION_MODE_ABSOLUTE) {
1156                                 fac = 1.0f - ((float)(gpf->framenum - gf->framenum) / (float)(step + 1));
1157                         }
1158                         else {
1159                                 fac = 1.0f - ((float)idx / (float)(step + 1));
1160                         }
1161                         color[3] = alpha * fac * 0.66f;
1162                 }
1163                 else {
1164                         idx++;
1165                         fac = alpha - ((1.1f - (1.0f / (float)idx)) * 0.66f);
1166                         color[3] = fac;
1167                 }
1168
1169                 /* if loop option, save the frame to use later */
1170                 if ((mode != GP_ONION_MODE_ABSOLUTE) && (gpd->onion_flag & GP_ONION_LOOP)) {
1171                         gpf_loop = gf;
1172                 }
1173
1174                 gpencil_get_onion_alpha(color, gpd);
1175                 gpencil_draw_onion_strokes(cache, vedata, ob, gpd, gpl, gf, color[3], color, colflag);
1176         }
1177         /* -------------------------------
1178          * 2) Now draw next frames
1179          * ------------------------------- */
1180         step = gpd->gstep_next;
1181         mode = gpd->onion_mode;
1182
1183         if (gpd->onion_flag & GP_ONION_GHOST_NEXTCOL) {
1184                 copy_v3_v3(color, gpd->gcolor_next);
1185         }
1186         else {
1187                 copy_v3_v3(color, default_color);
1188         }
1189
1190         idx = 0;
1191         for (bGPDframe *gf = gpf->next; gf; gf = gf->next) {
1192                 /* only selected frames */
1193                 if ((mode == GP_ONION_MODE_SELECTED) && ((gf->flag & GP_FRAME_SELECT) == 0)) {
1194                         continue;
1195                 }
1196                 /* absolute range */
1197                 if (mode == GP_ONION_MODE_ABSOLUTE) {
1198                         if ((gf->framenum - gpf->framenum) > step) {
1199                                 break;
1200                         }
1201                 }
1202                 /* relative range */
1203                 if (mode == GP_ONION_MODE_RELATIVE) {
1204                         idx++;
1205                         if (idx > step) {
1206                                 break;
1207                         }
1208
1209                 }
1210                 /* alpha decreases with distance from curframe index */
1211                 if (mode != GP_ONION_MODE_SELECTED) {
1212                         if (mode == GP_ONION_MODE_ABSOLUTE) {
1213                                 fac = 1.0f - ((float)(gf->framenum - gpf->framenum) / (float)(step + 1));
1214                         }
1215                         else {
1216                                 fac = 1.0f - ((float)idx / (float)(step + 1));
1217                         }
1218                         color[3] = alpha * fac * 0.66f;
1219                 }
1220                 else {
1221                         idx++;
1222                         fac = alpha - ((1.1f - (1.0f / (float)idx)) * 0.66f);
1223                         color[3] = fac;
1224                 }
1225
1226                 gpencil_get_onion_alpha(color, gpd);
1227                 gpencil_draw_onion_strokes(cache, vedata, ob, gpd, gpl, gf, color[3], color, colflag);
1228                 if (last < gf->framenum) {
1229                         last = gf->framenum;
1230                 }
1231         }
1232
1233         /* Draw first frame in blue for loop mode */
1234         if ((gpd->onion_flag & GP_ONION_LOOP) && (gpf_loop != NULL)) {
1235                 if ((last == gpf->framenum) || (gpf->next == NULL)) {
1236                         gpencil_get_onion_alpha(color, gpd);
1237                         gpencil_draw_onion_strokes(
1238                                 cache, vedata, ob, gpd, gpl,
1239                                 gpf_loop, color[3], color, colflag);
1240                 }
1241         }
1242 }
1243
1244 static void gpencil_copy_frame(bGPDframe *gpf, bGPDframe *derived_gpf)
1245 {
1246         derived_gpf->prev = gpf->prev;
1247         derived_gpf->next = gpf->next;
1248         derived_gpf->framenum = gpf->framenum;
1249         derived_gpf->flag = gpf->flag;
1250         derived_gpf->key_type = gpf->key_type;
1251         derived_gpf->runtime = gpf->runtime;
1252         copy_m4_m4(derived_gpf->runtime.viewmatrix, gpf->runtime.viewmatrix);
1253
1254         /* copy strokes */
1255         BLI_listbase_clear(&derived_gpf->strokes);
1256         for (bGPDstroke *gps_src = gpf->strokes.first; gps_src; gps_src = gps_src->next) {
1257                 /* make copy of source stroke */
1258                 bGPDstroke *gps_dst = BKE_gpencil_stroke_duplicate(gps_src);
1259                 BLI_addtail(&derived_gpf->strokes, gps_dst);
1260         }
1261 }
1262
1263 /* Triangulate stroke for high quality fill (this is done only if cache is null or stroke was modified) */
1264 void DRW_gpencil_triangulate_stroke_fill(Object *ob, bGPDstroke *gps)
1265 {
1266         BLI_assert(gps->totpoints >= 3);
1267
1268         bGPdata *gpd = (bGPdata *)ob->data;
1269
1270         /* allocate memory for temporary areas */
1271         gps->tot_triangles = gps->totpoints - 2;
1272         uint(*tmp_triangles)[3] = MEM_mallocN(sizeof(*tmp_triangles) * gps->tot_triangles, "GP Stroke temp triangulation");
1273         float(*points2d)[2] = MEM_mallocN(sizeof(*points2d) * gps->totpoints, "GP Stroke temp 2d points");
1274         float(*uv)[2] = MEM_mallocN(sizeof(*uv) * gps->totpoints, "GP Stroke temp 2d uv data");
1275
1276         int direction = 0;
1277
1278         /* convert to 2d and triangulate */
1279         BKE_gpencil_stroke_2d_flat(gps->points, gps->totpoints, points2d, &direction);
1280         BLI_polyfill_calc(points2d, (uint)gps->totpoints, direction, tmp_triangles);
1281
1282         /* calc texture coordinates automatically */
1283         float minv[2];
1284         float maxv[2];
1285         /* first needs bounding box data */
1286         if (gpd->flag & GP_DATA_UV_ADAPTIVE) {
1287                 gpencil_calc_2d_bounding_box(points2d, gps->totpoints, minv, maxv);
1288         }
1289         else {
1290                 ARRAY_SET_ITEMS(minv, -1.0f, -1.0f);
1291                 ARRAY_SET_ITEMS(maxv, 1.0f, 1.0f);
1292         }
1293
1294         /* calc uv data */
1295         gpencil_calc_stroke_fill_uv(points2d, gps->totpoints, minv, maxv, uv);
1296
1297         /* Number of triangles */
1298         gps->tot_triangles = gps->totpoints - 2;
1299         /* save triangulation data in stroke cache */
1300         if (gps->tot_triangles > 0) {
1301                 if (gps->triangles == NULL) {
1302                         gps->triangles = MEM_callocN(sizeof(*gps->triangles) * gps->tot_triangles, "GP Stroke triangulation");
1303                 }
1304                 else {
1305                         gps->triangles = MEM_recallocN(gps->triangles, sizeof(*gps->triangles) * gps->tot_triangles);
1306                 }
1307
1308                 for (int i = 0; i < gps->tot_triangles; i++) {
1309                         bGPDtriangle *stroke_triangle = &gps->triangles[i];
1310                         memcpy(gps->triangles[i].verts, tmp_triangles[i], sizeof(uint[3]));
1311                         /* copy texture coordinates */
1312                         copy_v2_v2(stroke_triangle->uv[0], uv[tmp_triangles[i][0]]);
1313                         copy_v2_v2(stroke_triangle->uv[1], uv[tmp_triangles[i][1]]);
1314                         copy_v2_v2(stroke_triangle->uv[2], uv[tmp_triangles[i][2]]);
1315                 }
1316         }
1317         else {
1318                 /* No triangles needed - Free anything allocated previously */
1319                 if (gps->triangles) {
1320                         MEM_freeN(gps->triangles);
1321                 }
1322
1323                 gps->triangles = NULL;
1324         }
1325
1326         /* disable recalculation flag */
1327         if (gps->flag & GP_STROKE_RECALC_GEOMETRY) {
1328                 gps->flag &= ~GP_STROKE_RECALC_GEOMETRY;
1329         }
1330
1331         /* clear memory */
1332         MEM_SAFE_FREE(tmp_triangles);
1333         MEM_SAFE_FREE(points2d);
1334         MEM_SAFE_FREE(uv);
1335 }
1336
1337 /* draw stroke in drawing buffer */
1338 void DRW_gpencil_populate_buffer_strokes(GPENCIL_e_data *e_data, void *vedata, ToolSettings *ts, Object *ob)
1339 {
1340         GPENCIL_PassList *psl = ((GPENCIL_Data *)vedata)->psl;
1341         GPENCIL_StorageList *stl = ((GPENCIL_Data *)vedata)->stl;
1342         const DRWContextState *draw_ctx = DRW_context_state_get();
1343         View3D *v3d = draw_ctx->v3d;
1344         const bool overlay = v3d != NULL ? (bool)((v3d->flag2 & V3D_HIDE_OVERLAYS) == 0) : true;
1345         Brush *brush = BKE_paint_brush(&ts->gp_paint->paint);
1346         bGPdata *gpd_eval = ob->data;
1347         /* need the original to avoid cow overhead while drawing */
1348         bGPdata *gpd = (bGPdata *)DEG_get_original_id(&gpd_eval->id);
1349
1350         MaterialGPencilStyle *gp_style = NULL;
1351         float obscale = mat4_to_scale(ob->obmat);
1352
1353         /* use the brush material */
1354         Material *ma = BKE_gpencil_get_material_from_brush(brush);
1355         if (ma != NULL) {
1356                 gp_style = ma->gp_style;
1357         }
1358         /* this is not common, but avoid any special situations when brush could be without material */
1359         if (gp_style == NULL) {
1360                 gp_style = BKE_material_gpencil_settings_get(ob, ob->actcol);
1361         }
1362
1363         /* drawing strokes */
1364         /* Check if may need to draw the active stroke cache, only if this layer is the active layer
1365          * that is being edited. (Stroke buffer is currently stored in gp-data)
1366          */
1367         if (gpd->runtime.sbuffer_size > 0) {
1368                 if ((gpd->runtime.sbuffer_sflag & GP_STROKE_ERASER) == 0) {
1369                         /* It should also be noted that sbuffer contains temporary point types
1370                          * i.e. tGPspoints NOT bGPDspoints
1371                          */
1372                         short lthick = brush->size * obscale;
1373                         /* if only one point, don't need to draw buffer because the user has no time to see it */
1374                         if (gpd->runtime.sbuffer_size > 1) {
1375                                 if ((gp_style) && (gp_style->mode == GP_STYLE_MODE_LINE)) {
1376                                         stl->g_data->shgrps_drawing_stroke = DRW_gpencil_shgroup_stroke_create(
1377                                                 e_data, vedata, psl->drawing_pass, e_data->gpencil_stroke_sh, NULL,
1378                                                 gpd, NULL, NULL, gp_style, -1,
1379                                                 false, 1.0f, (const int *)stl->storage->shade_render);
1380                                 }
1381                                 else {
1382                                         stl->g_data->shgrps_drawing_stroke = DRW_gpencil_shgroup_point_create(
1383                                                 e_data, vedata, psl->drawing_pass, e_data->gpencil_point_sh, NULL,
1384                                                 gpd, NULL, gp_style, -1,
1385                                                 false, 1.0f, (const int *)stl->storage->shade_render);
1386                                 }
1387
1388                                 /* clean previous version of the batch */
1389                                 if (stl->storage->buffer_stroke) {
1390                                         GPU_BATCH_DISCARD_SAFE(e_data->batch_buffer_stroke);
1391                                         MEM_SAFE_FREE(e_data->batch_buffer_stroke);
1392                                         stl->storage->buffer_stroke = false;
1393                                 }
1394
1395                                 /* use unit matrix because the buffer is in screen space and does not need conversion */
1396                                 if (gpd->runtime.mode == GP_STYLE_MODE_LINE) {
1397                                         e_data->batch_buffer_stroke = DRW_gpencil_get_buffer_stroke_geom(
1398                                                 gpd, lthick);
1399                                 }
1400                                 else {
1401                                         e_data->batch_buffer_stroke = DRW_gpencil_get_buffer_point_geom(
1402                                                 gpd, lthick);
1403                                 }
1404
1405                                 /* buffer strokes, must show stroke always */
1406                                 DRW_shgroup_call_add(
1407                                             stl->g_data->shgrps_drawing_stroke,
1408                                             e_data->batch_buffer_stroke,
1409                                             stl->storage->unit_matrix);
1410
1411                                 if ((gpd->runtime.sbuffer_size >= 3) &&
1412                                     (gpd->runtime.sfill[3] > GPENCIL_ALPHA_OPACITY_THRESH) &&
1413                                     ((gpd->runtime.sbuffer_sflag & GP_STROKE_NOFILL) == 0) &&
1414                                     ((brush->gpencil_settings->flag & GP_BRUSH_DISSABLE_LASSO) == 0) &&
1415                                     (gp_style->flag & GP_STYLE_FILL_SHOW))
1416                                 {
1417                                         /* if not solid, fill is simulated with solid color */
1418                                         if (gpd->runtime.bfill_style > 0) {
1419                                                 gpd->runtime.sfill[3] = 0.5f;
1420                                         }
1421                                         stl->g_data->shgrps_drawing_fill = DRW_shgroup_create(
1422                                                 e_data->gpencil_drawing_fill_sh, psl->drawing_pass);
1423
1424                                         /* clean previous version of the batch */
1425                                         if (stl->storage->buffer_fill) {
1426                                                 GPU_BATCH_DISCARD_SAFE(e_data->batch_buffer_fill);
1427                                                 MEM_SAFE_FREE(e_data->batch_buffer_fill);
1428                                                 stl->storage->buffer_fill = false;
1429                                         }
1430
1431                                         e_data->batch_buffer_fill = DRW_gpencil_get_buffer_fill_geom(gpd);
1432                                         DRW_shgroup_call_add(
1433                                                 stl->g_data->shgrps_drawing_fill,
1434                                                 e_data->batch_buffer_fill,
1435                                                 stl->storage->unit_matrix);
1436                                         stl->storage->buffer_fill = true;
1437                                 }
1438                                 stl->storage->buffer_stroke = true;
1439                         }
1440                 }
1441         }
1442
1443         /* control points for primitives and speed guide */
1444         const bool is_cppoint = (gpd->runtime.tot_cp_points > 0);
1445         const bool is_speed_guide = (ts->gp_sculpt.guide.use_guide && (draw_ctx->object_mode == OB_MODE_PAINT_GPENCIL));
1446         const bool is_show_gizmo = (((v3d->gizmo_flag & V3D_GIZMO_HIDE) == 0) && ((v3d->gizmo_flag & V3D_GIZMO_HIDE_TOOL) == 0));
1447
1448         if ((overlay) && (is_cppoint || is_speed_guide) && (is_show_gizmo) &&
1449             ((gpd->runtime.sbuffer_sflag & GP_STROKE_ERASER) == 0))
1450         {
1451                 DRWShadingGroup *shgrp = DRW_shgroup_create(
1452                         e_data->gpencil_edit_point_sh, psl->drawing_pass);
1453                 const float *viewport_size = DRW_viewport_size_get();
1454                 DRW_shgroup_uniform_vec2(shgrp, "Viewport", viewport_size, 1);
1455
1456                 /* clean previous version of the batch */
1457                 if (stl->storage->buffer_ctrlpoint) {
1458                         GPU_BATCH_DISCARD_SAFE(e_data->batch_buffer_ctrlpoint);
1459                         MEM_SAFE_FREE(e_data->batch_buffer_ctrlpoint);
1460                         stl->storage->buffer_ctrlpoint = false;
1461                 }
1462
1463                 e_data->batch_buffer_ctrlpoint = DRW_gpencil_get_buffer_ctrlpoint_geom(gpd);
1464
1465                 DRW_shgroup_call_add(
1466                         shgrp,
1467                         e_data->batch_buffer_ctrlpoint,
1468                         stl->storage->unit_matrix);
1469
1470                 stl->storage->buffer_ctrlpoint = true;
1471         }
1472 }
1473
1474 /* create all missing batches */
1475 static void DRW_gpencil_create_batches(GpencilBatchCache *cache)
1476 {
1477         if ((cache->b_point.vbo) && (cache->b_point.batch == NULL)) {
1478                 cache->b_point.batch = GPU_batch_create_ex(GPU_PRIM_POINTS, cache->b_point.vbo, NULL, GPU_BATCH_OWNS_VBO);
1479         }
1480         if ((cache->b_stroke.vbo) && (cache->b_stroke.batch == NULL)) {
1481                 cache->b_stroke.batch = GPU_batch_create_ex(GPU_PRIM_LINE_STRIP_ADJ, cache->b_stroke.vbo, NULL, GPU_BATCH_OWNS_VBO);
1482         }
1483         if ((cache->b_fill.vbo) && (cache->b_fill.batch == NULL)) {
1484                 cache->b_fill.batch = GPU_batch_create_ex(GPU_PRIM_TRIS, cache->b_fill.vbo, NULL, GPU_BATCH_OWNS_VBO);
1485         }
1486         if ((cache->b_edit.vbo) && (cache->b_edit.batch == NULL)) {
1487                 cache->b_edit.batch = GPU_batch_create_ex(GPU_PRIM_POINTS, cache->b_edit.vbo, NULL, GPU_BATCH_OWNS_VBO);
1488         }
1489         if ((cache->b_edlin.vbo) && (cache->b_edlin.batch == NULL)) {
1490                 cache->b_edlin.batch = GPU_batch_create_ex(GPU_PRIM_LINE_STRIP, cache->b_edlin.vbo, NULL, GPU_BATCH_OWNS_VBO);
1491         }
1492 }
1493
1494 /* create all shading groups */
1495 static void DRW_gpencil_shgroups_create(
1496         GPENCIL_e_data *e_data, void *vedata,
1497         Object *ob,
1498         GpencilBatchCache *cache, tGPencilObjectCache *cache_ob)
1499 {
1500         GPENCIL_StorageList *stl = ((GPENCIL_Data *)vedata)->stl;
1501         GPENCIL_PassList *psl = ((GPENCIL_Data *)vedata)->psl;
1502         bGPdata *gpd = (bGPdata *)ob->data;
1503         DRWPass *stroke_pass = GPENCIL_3D_DRAWMODE(ob, gpd) ? psl->stroke_pass_3d : psl->stroke_pass_2d;
1504
1505         GpencilBatchGroup *elm = NULL;
1506         DRWShadingGroup *shgrp = NULL;
1507         tGPencilObjectCache_shgrp *array_elm = NULL;
1508
1509         bGPDlayer *gpl = NULL;
1510         bGPDlayer *gpl_prev = NULL;
1511         int idx = 0;
1512         bool tag_first = false;
1513
1514         int start_stroke = 0;
1515         int start_point = 0;
1516         int start_fill = 0;
1517         int start_edit = 0;
1518         int start_edlin = 0;
1519
1520         for (int i = 0; i < cache->grp_used; i++) {
1521                 elm = &cache->grp_cache[i];
1522                 array_elm = &cache_ob->shgrp_array[idx];
1523                 const float scale = cache_ob->scale;
1524
1525                 /* save last group when change */
1526                 if (gpl_prev == NULL) {
1527                         gpl_prev = elm->gpl;
1528                         tag_first = true;
1529                 }
1530                 else {
1531                         if (elm->gpl != gpl_prev) {
1532                                 /* first layer is always blend Normal */
1533                                 array_elm->mode = idx == 0 ? eGplBlendMode_Normal: gpl->blend_mode;
1534                                 array_elm->end_shgrp = shgrp;
1535                                 gpl_prev = elm->gpl;
1536                                 tag_first = true;
1537                                 idx++;
1538                         }
1539                 }
1540
1541                 gpl = elm->gpl;
1542                 bGPDframe *gpf = elm->gpf;
1543                 bGPDstroke *gps = elm->gps;
1544                 MaterialGPencilStyle *gp_style = BKE_material_gpencil_settings_get(ob, gps->mat_nr + 1);
1545                 /* if the user switch used material from data to object,
1546                  * the material could not be available */
1547                 if (gp_style == NULL) {
1548                         break;
1549                 }
1550
1551                 /* limit the number of shading groups */
1552                 if (i >= GPENCIL_MAX_SHGROUPS) {
1553                         break;
1554                 }
1555
1556                 switch (elm->type) {
1557                         case eGpencilBatchGroupType_Stroke:
1558                         {
1559                                 const int len = elm->vertex_idx - start_stroke;
1560
1561                                 shgrp = DRW_gpencil_shgroup_stroke_create(
1562                                         e_data, vedata, stroke_pass, e_data->gpencil_stroke_sh,
1563                                         ob, gpd, gpl, gps, gp_style, stl->storage->shgroup_id, elm->onion,
1564                                         scale, cache_ob->shading_type);
1565
1566                                 DRW_shgroup_call_range_add(
1567                                         shgrp, cache->b_stroke.batch,
1568                                         (!cache_ob->is_dup_ob) ? gpf->runtime.viewmatrix : cache_ob->obmat,
1569                                         start_stroke, len);
1570
1571                                 stl->storage->shgroup_id++;
1572                                 start_stroke = elm->vertex_idx;
1573                                 break;
1574                         }
1575                         case eGpencilBatchGroupType_Point:
1576                         {
1577                                 const int len = elm->vertex_idx - start_point;
1578
1579                                 shgrp = DRW_gpencil_shgroup_point_create(
1580                                         e_data, vedata, stroke_pass, e_data->gpencil_point_sh,
1581                                         ob, gpd, gpl, gp_style, stl->storage->shgroup_id, elm->onion,
1582                                         scale, cache_ob->shading_type);
1583
1584                                 DRW_shgroup_call_range_add(
1585                                         shgrp, cache->b_point.batch,
1586                                         (!cache_ob->is_dup_ob) ? gpf->runtime.viewmatrix : cache_ob->obmat,
1587                                         start_point, len);
1588
1589                                 stl->storage->shgroup_id++;
1590                                 start_point = elm->vertex_idx;
1591                                 break;
1592                         }
1593                         case eGpencilBatchGroupType_Fill:
1594                         {
1595                                 const int len = elm->vertex_idx - start_fill;
1596
1597                                 shgrp = DRW_gpencil_shgroup_fill_create(
1598                                         e_data, vedata, stroke_pass, e_data->gpencil_fill_sh,
1599                                         ob, gpd, gpl, gp_style, stl->storage->shgroup_id,
1600                                         cache_ob->shading_type);
1601
1602                                 DRW_shgroup_call_range_add(
1603                                         shgrp, cache->b_fill.batch,
1604                                         (!cache_ob->is_dup_ob) ? gpf->runtime.viewmatrix : cache_ob->obmat,
1605                                         start_fill, len);
1606
1607                                 stl->storage->shgroup_id++;
1608                                 start_fill = elm->vertex_idx;
1609                                 break;
1610                         }
1611                         case eGpencilBatchGroupType_Edit:
1612                         {
1613                                 if (stl->g_data->shgrps_edit_point) {
1614                                         const int len = elm->vertex_idx - start_edit;
1615                                         /* use always the same group */
1616                                         DRW_shgroup_call_range_add(
1617                                                 stl->g_data->shgrps_edit_point,
1618                                                 cache->b_edit.batch,
1619                                                 (!cache_ob->is_dup_ob) ? gpf->runtime.viewmatrix : cache_ob->obmat,
1620                                                 start_edit, len);
1621
1622                                         start_edit = elm->vertex_idx;
1623                                 }
1624                                 break;
1625                         }
1626                         case eGpencilBatchGroupType_Edlin:
1627                         {
1628                                 if (stl->g_data->shgrps_edit_line) {
1629                                         const int len = elm->vertex_idx - start_edlin;
1630                                         /* use always the same group */
1631                                         DRW_shgroup_call_range_add(
1632                                                 stl->g_data->shgrps_edit_line,
1633                                                 cache->b_edlin.batch,
1634                                                 (!cache_ob->is_dup_ob) ? gpf->runtime.viewmatrix : cache_ob->obmat,
1635                                                 start_edlin, len);
1636
1637                                         start_edlin = elm->vertex_idx;
1638                                 }
1639                                 break;
1640                         }
1641                         default:
1642                         {
1643                                 break;
1644                         }
1645                 }
1646                 /* save first group */
1647                 if ((shgrp != NULL) && (tag_first)) {
1648                         array_elm = &cache_ob->shgrp_array[idx];
1649                         array_elm->mode = idx == 0 ? eGplBlendMode_Normal: gpl->blend_mode;
1650                         array_elm->clamp_layer = gpl->flag & GP_LAYER_USE_MASK;
1651                         array_elm->blend_opacity = gpl->opacity;
1652                         array_elm->init_shgrp = shgrp;
1653                         cache_ob->tot_layers++;
1654
1655                         tag_first = false;
1656                 }
1657         }
1658
1659         /* save last group */
1660         if (shgrp != NULL) {
1661                 array_elm->mode = idx == 0 ? eGplBlendMode_Normal : gpl->blend_mode;
1662                 array_elm->end_shgrp = shgrp;
1663         }
1664 }
1665 /* populate a datablock for multiedit (no onions, no modifiers) */
1666 void DRW_gpencil_populate_multiedit(
1667         GPENCIL_e_data *e_data, void *vedata, Object *ob,
1668         tGPencilObjectCache *cache_ob)
1669 {
1670         bGPdata *gpd = (bGPdata *)ob->data;
1671         bGPDframe *gpf = NULL;
1672
1673         GPENCIL_StorageList *stl = ((GPENCIL_Data *)vedata)->stl;
1674         const DRWContextState *draw_ctx = DRW_context_state_get();
1675         int cfra_eval = (int)DEG_get_ctime(draw_ctx->depsgraph);
1676         GpencilBatchCache *cache = gpencil_batch_cache_get(ob, cfra_eval);
1677
1678         /* check if playing animation */
1679         const bool playing = stl->storage->is_playing;
1680
1681         /* calc max size of VBOs */
1682         gpencil_calc_vertex(stl, cache_ob, cache, gpd, cfra_eval);
1683
1684         /* draw strokes */
1685         for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) {
1686                 /* don't draw layer if hidden */
1687                 if (gpl->flag & GP_LAYER_HIDE) {
1688                         continue;
1689                 }
1690
1691                 /* list of frames to draw */
1692                 if (!playing) {
1693                         for (gpf = gpl->frames.first; gpf; gpf = gpf->next) {
1694                                 if ((gpf == gpl->actframe) || (gpf->flag & GP_FRAME_SELECT)) {
1695                                         gpencil_draw_strokes(
1696                                                 cache, e_data, vedata, ob, gpd, gpl, gpf, gpf,
1697                                                 gpl->opacity, gpl->tintcolor, false, cache_ob);
1698                                 }
1699                         }
1700                 }
1701                 else {
1702                         gpf = BKE_gpencil_layer_getframe(gpl, cfra_eval, GP_GETFRAME_USE_PREV);
1703                         if (gpf) {
1704                                 gpencil_draw_strokes(
1705                                         cache, e_data, vedata, ob, gpd, gpl, gpf, gpf,
1706                                         gpl->opacity, gpl->tintcolor, false, cache_ob);
1707                         }
1708                 }
1709
1710         }
1711
1712         /* create batchs and shading groups */
1713         DRW_gpencil_create_batches(cache);
1714         DRW_gpencil_shgroups_create(e_data, vedata, ob, cache, cache_ob);
1715
1716         cache->is_dirty = false;
1717 }
1718
1719 /* helper for populate a complete grease pencil datablock */
1720 void DRW_gpencil_populate_datablock(
1721         GPENCIL_e_data *e_data, void *vedata,
1722         Object *ob,
1723         tGPencilObjectCache *cache_ob)
1724 {
1725         GPENCIL_StorageList *stl = ((GPENCIL_Data *)vedata)->stl;
1726         const DRWContextState *draw_ctx = DRW_context_state_get();
1727         const ViewLayer *view_layer = DEG_get_evaluated_view_layer(draw_ctx->depsgraph);
1728         Scene *scene = draw_ctx->scene;
1729
1730         bGPdata *gpd = (bGPdata *)ob->data;
1731
1732         View3D *v3d = draw_ctx->v3d;
1733         int cfra_eval = (int)DEG_get_ctime(draw_ctx->depsgraph);
1734
1735         bGPDframe *derived_gpf = NULL;
1736         const bool main_onion = v3d != NULL ? (v3d->gp_flag & V3D_GP_SHOW_ONION_SKIN) : true;
1737         const bool do_onion = (bool)((gpd->flag & GP_DATA_STROKE_WEIGHTMODE) == 0) &&
1738                 main_onion && DRW_gpencil_onion_active(gpd);
1739         const bool overlay = v3d != NULL ? (bool)((v3d->flag2 & V3D_HIDE_OVERLAYS) == 0) : true;
1740         const bool time_remap = BKE_gpencil_has_time_modifiers(ob);
1741
1742         float opacity;
1743         bGPDframe *gpf = NULL;
1744         bGPDlayer *gpl_active = BKE_gpencil_layer_getactive(gpd);
1745
1746         /* check if playing animation */
1747         const bool playing = stl->storage->is_playing;
1748
1749         GpencilBatchCache *cache = gpencil_batch_cache_get(ob, cfra_eval);
1750
1751         /* if object is duplicate, only create shading groups */
1752         if (cache_ob->is_dup_ob) {
1753                 DRW_gpencil_shgroups_create(e_data, vedata, ob, cache, cache_ob);
1754                 return;
1755         }
1756
1757         /* calc max size of VBOs */
1758         gpencil_calc_vertex(stl, cache_ob, cache, gpd, cfra_eval);
1759
1760         /* init general modifiers data */
1761         if (!stl->storage->simplify_modif) {
1762                 if ((cache->is_dirty) && (ob->greasepencil_modifiers.first)) {
1763                         BKE_gpencil_lattice_init(ob);
1764                 }
1765         }
1766         /* draw normal strokes */
1767         for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) {
1768                 /* don't draw layer if hidden */
1769                 if (gpl->flag & GP_LAYER_HIDE) {
1770                         continue;
1771                 }
1772
1773                 const bool is_solomode = GPENCIL_PAINT_MODE(gpd) &&
1774                         (!playing) && (!stl->storage->is_render) &&
1775                         (gpl->flag & GP_LAYER_SOLO_MODE);
1776
1777                 /* filter view layer to gp layers in the same view layer (for compo) */
1778                 if ((stl->storage->is_render) && (gpl->viewlayername[0] != '\0')) {
1779                         if (!STREQ(view_layer->name, gpl->viewlayername)) {
1780                                 continue;
1781                         }
1782                 }
1783
1784                 /* remap time */
1785                 int remap_cfra = cfra_eval;
1786                 if ((time_remap) && (!stl->storage->simplify_modif)) {
1787                         remap_cfra = BKE_gpencil_time_modifier(
1788                                 draw_ctx->depsgraph, scene, ob, gpl, cfra_eval,
1789                                 stl->storage->is_render);
1790                 }
1791
1792                 gpf = BKE_gpencil_layer_getframe(gpl, remap_cfra, GP_GETFRAME_USE_PREV);
1793                 if (gpf == NULL) {
1794                         continue;
1795                 }
1796
1797                 /* if solo mode, display only frames with keyframe in the current frame */
1798                 if ((is_solomode) && (gpf->framenum != remap_cfra)) {
1799                         continue;
1800                 }
1801
1802                 opacity = gpl->opacity;
1803                 /* if pose mode, maybe the overlay to fade geometry is enabled */
1804                 if ((draw_ctx->obact) && (draw_ctx->object_mode == OB_MODE_POSE) &&
1805                     (v3d->overlay.flag & V3D_OVERLAY_BONE_SELECT))
1806                 {
1807                         opacity = opacity * v3d->overlay.xray_alpha_bone;
1808                 }
1809                 /* fade no active layers */
1810                 if ((overlay) && (draw_ctx->object_mode == OB_MODE_PAINT_GPENCIL) &&
1811                     (v3d->gp_flag & V3D_GP_FADE_NOACTIVE_LAYERS) &&
1812                     (draw_ctx->obact) && (draw_ctx->obact == ob) &&
1813                     (gpl != gpl_active))
1814                 {
1815                         opacity = opacity * v3d->overlay.gpencil_fade_layer;
1816                 }
1817
1818                 /* create derived frames array data or expand */
1819                 int derived_idx = BLI_findindex(&gpd->layers, gpl);
1820                 derived_gpf = &cache->derived_array[derived_idx];
1821
1822                 /* if no derived frame or dirty cache, create a new one */
1823                 if ((derived_gpf == NULL) || (cache->is_dirty)) {
1824                         if (derived_gpf != NULL) {
1825                                 /* first clear temp data */
1826                                 BKE_gpencil_free_frame_runtime_data(derived_gpf);
1827                         }
1828                         /* create new data (do not assign new memory)*/
1829                         gpencil_copy_frame(gpf, derived_gpf);
1830                 }
1831
1832                 /* draw onion skins */
1833                 if (!ID_IS_LINKED(&gpd->id)) {
1834                         if ((do_onion) && (gpl->onion_flag & GP_LAYER_ONIONSKIN) &&
1835                             ((!playing) || (gpd->onion_flag & GP_ONION_GHOST_ALWAYS)) &&
1836                             (!cache_ob->is_dup_ob) && (gpd->id.us <= 1))
1837                         {
1838                                 if (((!stl->storage->is_render) && (overlay)) ||
1839                                     ((stl->storage->is_render) && (gpd->onion_flag & GP_ONION_GHOST_ALWAYS)))
1840                                 {
1841                                         gpencil_draw_onionskins(cache, vedata, ob, gpd, gpl, gpf);
1842                                 }
1843                         }
1844                 }
1845                 /* draw normal strokes */
1846                 gpencil_draw_strokes(
1847                         cache, e_data, vedata, ob, gpd, gpl, gpf, derived_gpf,
1848                         opacity, gpl->tintcolor, false, cache_ob);
1849         }
1850
1851         /* clear any lattice data */
1852         if ((cache->is_dirty) && (ob->greasepencil_modifiers.first)) {
1853                 BKE_gpencil_lattice_clear(ob);
1854         }
1855
1856         /* create batchs and shading groups */
1857         DRW_gpencil_create_batches(cache);
1858         DRW_gpencil_shgroups_create(e_data, vedata, ob, cache, cache_ob);
1859
1860         cache->is_dirty = false;
1861 }
1862
1863 void DRW_gpencil_populate_particles(GPENCIL_e_data *e_data, GHash *gh_objects, void *vedata)
1864 {
1865         GPENCIL_StorageList *stl = ((GPENCIL_Data *)vedata)->stl;
1866
1867         /* add particles */
1868         for (int i = 0; i < stl->g_data->gp_cache_used; i++) {
1869                 tGPencilObjectCache *cache_ob = &stl->g_data->gp_object_cache[i];
1870                 if (cache_ob->is_dup_ob) {
1871                         /* reasign duplicate objects because memory for particles is not available
1872                          * and need to use the original datablock and runtime data */
1873                         Object *ob = (Object *)BLI_ghash_lookup(gh_objects, cache_ob->name);
1874                         if (ob) {
1875                                 cache_ob->ob = ob;
1876                                 cache_ob->gpd = (bGPdata *)ob->data;
1877                                 GpencilBatchCache *cache = ob->runtime.gpencil_cache;
1878                                 if (cache != NULL) {
1879                                         DRW_gpencil_shgroups_create(e_data, vedata, ob, cache, cache_ob);
1880                                 }
1881                         }
1882                 }
1883         }
1884 }