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