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