Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gpencil_draw_cache_impl.c « gpencil « engines « draw « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d1033a94ffa6c81319897de1308bd2ad8bb7a517 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
/*
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2008, Blender Foundation
 * This is a new part of Blender
 *
 * Contributor(s): Antonio Vazquez
 *
 * ***** END GPL LICENSE BLOCK *****
 */

/** \file draw/engines/gpencil/gpencil_draw_cache_impl.c
 *  \ingroup draw
 */

#include "BLI_polyfill_2d.h"
#include "BLI_math_color.h"

#include "DNA_meshdata_types.h"
#include "DNA_gpencil_types.h"
#include "DNA_screen_types.h"
#include "DNA_view3d_types.h"

#include "BKE_action.h"
#include "BKE_deform.h"
#include "BKE_gpencil.h"

#include "DRW_render.h"

#include "GPU_immediate.h"
#include "GPU_draw.h"

#include "ED_gpencil.h"
#include "ED_view3d.h"

#include "UI_resources.h"

#include "gpencil_engine.h"

/* Helper to add stroke point to vbo */
static void gpencil_set_stroke_point(
        GPUVertBuf *vbo, const bGPDspoint *pt, int idx,
        uint pos_id, uint color_id,
        uint thickness_id, uint uvdata_id, short thickness,
        const float ink[4])
{

	float alpha = ink[3] * pt->strength;
	CLAMP(alpha, GPENCIL_STRENGTH_MIN, 1.0f);
	float col[4];
	ARRAY_SET_ITEMS(col, ink[0], ink[1], ink[2], alpha);

	GPU_vertbuf_attr_set(vbo, color_id, idx, col);

	/* transfer both values using the same shader variable */
	float uvdata[2] = { pt->uv_fac, pt->uv_rot };
	GPU_vertbuf_attr_set(vbo, uvdata_id, idx, uvdata);

	/* the thickness of the stroke must be affected by zoom, so a pixel scale is calculated */
	float thick = max_ff(pt->pressure * thickness, 1.0f);
	GPU_vertbuf_attr_set(vbo, thickness_id, idx, &thick);

	GPU_vertbuf_attr_set(vbo, pos_id, idx, &pt->x);
}

/* Helper to add a new fill point and texture coordinates to vertex buffer */
static void gpencil_set_fill_point(
	GPUVertBuf *vbo, int idx, bGPDspoint *pt, const float fcolor[4], float uv[2],
	uint pos_id, uint color_id, uint text_id)
{
	GPU_vertbuf_attr_set(vbo, pos_id, idx, &pt->x);
	GPU_vertbuf_attr_set(vbo, color_id, idx, fcolor);
	GPU_vertbuf_attr_set(vbo, text_id, idx, uv);
}

static void gpencil_vbo_ensure_size(GpencilBatchCacheElem *be, int totvertex)
{
	if (be->vbo->vertex_alloc <= be->vbo_len + totvertex) {
		uint newsize = be->vbo->vertex_alloc + (((totvertex / GPENCIL_VBO_BLOCK_SIZE) + 1) * GPENCIL_VBO_BLOCK_SIZE);
		GPU_vertbuf_data_resize(be->vbo, newsize);
	}
}

/* create batch geometry data for points stroke shader */
void DRW_gpencil_get_point_geom(GpencilBatchCacheElem *be, bGPDstroke *gps, short thickness, const float ink[4])
{
	int totvertex = gps->totpoints;
	if (be->vbo == NULL) {
		be->pos_id = GPU_vertformat_attr_add(&be->format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
		be->color_id = GPU_vertformat_attr_add(&be->format, "color", GPU_COMP_F32, 4, GPU_FETCH_FLOAT);
		be->thickness_id = GPU_vertformat_attr_add(&be->format, "thickness", GPU_COMP_F32, 1, GPU_FETCH_FLOAT);
		be->uvdata_id = GPU_vertformat_attr_add(&be->format, "uvdata", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);

		be->vbo = GPU_vertbuf_create_with_format(&be->format);
		GPU_vertbuf_data_alloc(be->vbo, be->tot_vertex);
		be->vbo_len = 0;
	}
	gpencil_vbo_ensure_size(be, totvertex);

	/* draw stroke curve */
	const bGPDspoint *pt = gps->points;
	float alpha;
	float col[4];

	for (int i = 0; i < gps->totpoints; i++, pt++) {
		/* set point */
		alpha = ink[3] * pt->strength;
		CLAMP(alpha, GPENCIL_STRENGTH_MIN, 1.0f);
		ARRAY_SET_ITEMS(col, ink[0], ink[1], ink[2], alpha);

		float thick = max_ff(pt->pressure * thickness, 1.0f);

		GPU_vertbuf_attr_set(be->vbo, be->color_id, be->vbo_len, col);
		GPU_vertbuf_attr_set(be->vbo, be->thickness_id, be->vbo_len, &thick);

		/* transfer both values using the same shader variable */
		float uvdata[2] = { pt->uv_fac, pt->uv_rot };
		GPU_vertbuf_attr_set(be->vbo, be->uvdata_id, be->vbo_len, uvdata);

		GPU_vertbuf_attr_set(be->vbo, be->pos_id, be->vbo_len, &pt->x);
		be->vbo_len++;
	}
}

/* create batch geometry data for stroke shader */
void DRW_gpencil_get_stroke_geom(struct GpencilBatchCacheElem *be, bGPDstroke *gps, short thickness, const float ink[4])
{
	bGPDspoint *points = gps->points;
	int totpoints = gps->totpoints;
	/* if cyclic needs more vertex */
	int cyclic_add = (gps->flag & GP_STROKE_CYCLIC) ? 1 : 0;
	int totvertex = totpoints + cyclic_add + 2;

	if (be->vbo == NULL) {
		be->pos_id = GPU_vertformat_attr_add(&be->format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
		be->color_id = GPU_vertformat_attr_add(&be->format, "color", GPU_COMP_F32, 4, GPU_FETCH_FLOAT);
		be->thickness_id = GPU_vertformat_attr_add(&be->format, "thickness", GPU_COMP_F32, 1, GPU_FETCH_FLOAT);
		be->uvdata_id = GPU_vertformat_attr_add(&be->format, "uvdata", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);

		be->vbo = GPU_vertbuf_create_with_format(&be->format);
		GPU_vertbuf_data_alloc(be->vbo, be->tot_vertex);
		be->vbo_len = 0;
	}
	gpencil_vbo_ensure_size(be, totvertex);

	/* draw stroke curve */
	const bGPDspoint *pt = points;
	for (int i = 0; i < totpoints; i++, pt++) {
		/* first point for adjacency (not drawn) */
		if (i == 0) {
			if (gps->flag & GP_STROKE_CYCLIC && totpoints > 2) {
				gpencil_set_stroke_point(
					be->vbo, &points[totpoints - 1], be->vbo_len,
					be->pos_id, be->color_id, be->thickness_id, be->uvdata_id, thickness, ink);
				be->vbo_len++;
			}
			else {
				gpencil_set_stroke_point(
					be->vbo, &points[1], be->vbo_len,
					be->pos_id, be->color_id, be->thickness_id, be->uvdata_id, thickness, ink);
				be->vbo_len++;
			}
		}
		/* set point */
		gpencil_set_stroke_point(
			be->vbo, pt, be->vbo_len,
			be->pos_id, be->color_id, be->thickness_id, be->uvdata_id, thickness, ink);
		be->vbo_len++;
	}

	if (gps->flag & GP_STROKE_CYCLIC && totpoints > 2) {
		/* draw line to first point to complete the cycle */
		gpencil_set_stroke_point(
			be->vbo, &points[0], be->vbo_len,
			be->pos_id, be->color_id, be->thickness_id, be->uvdata_id, thickness, ink);
		be->vbo_len++;
		/* now add adjacency point (not drawn) */
		gpencil_set_stroke_point(
			be->vbo, &points[1], be->vbo_len,
			be->pos_id, be->color_id, be->thickness_id, be->uvdata_id, thickness, ink);
		be->vbo_len++;
	}
	/* last adjacency point (not drawn) */
	else {
		gpencil_set_stroke_point(
			be->vbo, &points[totpoints - 2], be->vbo_len,
			be->pos_id, be->color_id, be->thickness_id, be->uvdata_id, thickness, ink);
		be->vbo_len++;
	}
}

/* create batch geometry data for stroke shader */
void DRW_gpencil_get_fill_geom(struct GpencilBatchCacheElem *be, Object *ob, bGPDstroke *gps, const float color[4])
{
	BLI_assert(gps->totpoints >= 3);

	/* Calculate triangles cache for filling area (must be done only after changes) */
	if ((gps->flag & GP_STROKE_RECALC_CACHES) || (gps->tot_triangles == 0) || (gps->triangles == NULL)) {
		DRW_gpencil_triangulate_stroke_fill(ob, gps);
		ED_gpencil_calc_stroke_uv(ob, gps);
	}

	BLI_assert(gps->tot_triangles >= 1);
	int totvertex = gps->tot_triangles * 3;

	if (be->vbo == NULL) {
		be->pos_id = GPU_vertformat_attr_add(&be->format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
		be->color_id = GPU_vertformat_attr_add(&be->format, "color", GPU_COMP_F32, 4, GPU_FETCH_FLOAT);
		be->uvdata_id = GPU_vertformat_attr_add(&be->format, "texCoord", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);

		be->vbo = GPU_vertbuf_create_with_format(&be->format);
		GPU_vertbuf_data_alloc(be->vbo, be->tot_vertex);
		be->vbo_len = 0;
	}
	gpencil_vbo_ensure_size(be, totvertex);

	/* Draw all triangles for filling the polygon (cache must be calculated before) */
	bGPDtriangle *stroke_triangle = gps->triangles;
	for (int i = 0; i < gps->tot_triangles; i++, stroke_triangle++) {
		for (int j = 0; j < 3; j++) {
			gpencil_set_fill_point(
				be->vbo, be->vbo_len, &gps->points[stroke_triangle->verts[j]], color, stroke_triangle->uv[j],
				be->pos_id, be->color_id, be->uvdata_id);
			be->vbo_len++;
		}
	}
}

/* create batch geometry data for current buffer stroke shader */
GPUBatch *DRW_gpencil_get_buffer_stroke_geom(bGPdata *gpd, short thickness)
{
	const DRWContextState *draw_ctx = DRW_context_state_get();
	Scene *scene = draw_ctx->scene;
	ARegion *ar = draw_ctx->ar;
	RegionView3D *rv3d = draw_ctx->rv3d;
	ToolSettings *ts = scene->toolsettings;
	Object *ob = draw_ctx->obact;

	tGPspoint *points = gpd->runtime.sbuffer;
	int totpoints = gpd->runtime.sbuffer_size;
	/* if cyclic needs more vertex */
	int cyclic_add = (gpd->runtime.sbuffer_sflag & GP_STROKE_CYCLIC) ? 1 : 0;
	int totvertex = totpoints + cyclic_add + 2;

	static GPUVertFormat format = { 0 };
	static uint pos_id, color_id, thickness_id, uvdata_id;
	if (format.attr_len == 0) {
		pos_id = GPU_vertformat_attr_add(&format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
		color_id = GPU_vertformat_attr_add(&format, "color", GPU_COMP_F32, 4, GPU_FETCH_FLOAT);
		thickness_id = GPU_vertformat_attr_add(&format, "thickness", GPU_COMP_F32, 1, GPU_FETCH_FLOAT);
		uvdata_id = GPU_vertformat_attr_add(&format, "uvdata", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
	}

	GPUVertBuf *vbo = GPU_vertbuf_create_with_format(&format);
	GPU_vertbuf_data_alloc(vbo, totvertex);

	/* draw stroke curve */
	const tGPspoint *tpt = points;
	bGPDspoint pt, pt2, pt3;
	int idx = 0;

	/* get origin to reproject point */
	float origin[3];
	bGPDlayer *gpl = BKE_gpencil_layer_getactive(gpd);
	ED_gp_get_drawing_reference(scene, ob, gpl, ts->gpencil_v3d_align, origin);

	for (int i = 0; i < totpoints; i++, tpt++) {
		ED_gpencil_tpoint_to_point(ar, origin, tpt, &pt);
		ED_gp_project_point_to_plane(ob, rv3d, origin, ts->gp_sculpt.lock_axis - 1, &pt);

		/* first point for adjacency (not drawn) */
		if (i == 0) {
			if (gpd->runtime.sbuffer_sflag & GP_STROKE_CYCLIC && totpoints > 2) {
				ED_gpencil_tpoint_to_point(ar, origin, &points[totpoints - 1], &pt2);
				gpencil_set_stroke_point(
					vbo, &pt2, idx,
					pos_id, color_id, thickness_id, uvdata_id, thickness, gpd->runtime.scolor);
				idx++;
			}
			else {
				ED_gpencil_tpoint_to_point(ar, origin, &points[1], &pt2);
				gpencil_set_stroke_point(
					vbo, &pt2, idx,
					pos_id, color_id, thickness_id, uvdata_id, thickness, gpd->runtime.scolor);
				idx++;
			}
		}

		/* set point */
		gpencil_set_stroke_point(
		        vbo, &pt, idx,
		        pos_id, color_id, thickness_id, uvdata_id, thickness, gpd->runtime.scolor);
		idx++;
	}

	/* last adjacency point (not drawn) */
	if (gpd->runtime.sbuffer_sflag & GP_STROKE_CYCLIC && totpoints > 2) {
		/* draw line to first point to complete the cycle */
		ED_gpencil_tpoint_to_point(ar, origin, &points[0], &pt2);
		gpencil_set_stroke_point(
			vbo, &pt2, idx,
			pos_id, color_id, thickness_id, uvdata_id, thickness, gpd->runtime.scolor);
		idx++;
		/* now add adjacency point (not drawn) */
		ED_gpencil_tpoint_to_point(ar, origin, &points[1], &pt3);
		gpencil_set_stroke_point(
			vbo, &pt3, idx,
			pos_id, color_id, thickness_id, uvdata_id, thickness, gpd->runtime.scolor);
		idx++;
	}
	/* last adjacency point (not drawn) */
	else {
		ED_gpencil_tpoint_to_point(ar, origin, &points[totpoints - 2], &pt2);
		gpencil_set_stroke_point(
			vbo, &pt2, idx,
			pos_id, color_id, thickness_id, uvdata_id, thickness, gpd->runtime.scolor);
		idx++;
	}

	return GPU_batch_create_ex(GPU_PRIM_LINE_STRIP_ADJ, vbo, NULL, GPU_BATCH_OWNS_VBO);
}

/* create batch geometry data for current buffer point shader */
GPUBatch *DRW_gpencil_get_buffer_point_geom(bGPdata *gpd, short thickness)
{
	const DRWContextState *draw_ctx = DRW_context_state_get();
	Scene *scene = draw_ctx->scene;
	ARegion *ar = draw_ctx->ar;
	RegionView3D *rv3d = draw_ctx->rv3d;
	ToolSettings *ts = scene->toolsettings;
	Object *ob = draw_ctx->obact;

	tGPspoint *points = gpd->runtime.sbuffer;
	int totpoints = gpd->runtime.sbuffer_size;

	static GPUVertFormat format = { 0 };
	static uint pos_id, color_id, thickness_id, uvdata_id;
	if (format.attr_len == 0) {
		pos_id = GPU_vertformat_attr_add(&format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
		color_id = GPU_vertformat_attr_add(&format, "color", GPU_COMP_F32, 4, GPU_FETCH_FLOAT);
		thickness_id = GPU_vertformat_attr_add(&format, "thickness", GPU_COMP_F32, 1, GPU_FETCH_FLOAT);
		uvdata_id = GPU_vertformat_attr_add(&format, "uvdata", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
	}

	GPUVertBuf *vbo = GPU_vertbuf_create_with_format(&format);
	GPU_vertbuf_data_alloc(vbo, totpoints);

	/* draw stroke curve */
	const tGPspoint *tpt = points;
	bGPDspoint pt;
	int idx = 0;

	/* get origin to reproject point */
	float origin[3];
	bGPDlayer *gpl = BKE_gpencil_layer_getactive(gpd);
	ED_gp_get_drawing_reference(scene, ob, gpl, ts->gpencil_v3d_align, origin);

	for (int i = 0; i < totpoints; i++, tpt++) {
		ED_gpencil_tpoint_to_point(ar, origin, tpt, &pt);
		ED_gp_project_point_to_plane(ob, rv3d, origin, ts->gp_sculpt.lock_axis - 1, &pt);

		/* set point */
		gpencil_set_stroke_point(
		        vbo, &pt, idx,
		        pos_id, color_id, thickness_id, uvdata_id,
		        thickness, gpd->runtime.scolor);
		idx++;
	}

	return GPU_batch_create_ex(GPU_PRIM_POINTS, vbo, NULL, GPU_BATCH_OWNS_VBO);
}

/* create batch geometry data for current buffer control point shader */
GPUBatch *DRW_gpencil_get_buffer_ctrlpoint_geom(bGPdata *gpd)
{
	bGPDcontrolpoint *cps = gpd->runtime.cp_points;
	int totpoints = gpd->runtime.tot_cp_points;

	static GPUVertFormat format = { 0 };
	static uint pos_id, color_id, size_id;
	if (format.attr_len == 0) {
		pos_id = GPU_vertformat_attr_add(&format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
		size_id = GPU_vertformat_attr_add(&format, "size", GPU_COMP_F32, 1, GPU_FETCH_FLOAT);
		color_id = GPU_vertformat_attr_add(&format, "color", GPU_COMP_F32, 4, GPU_FETCH_FLOAT);
	}

	GPUVertBuf *vbo = GPU_vertbuf_create_with_format(&format);
	GPU_vertbuf_data_alloc(vbo, totpoints);

	int idx = 0;
	for (int i = 0; i < gpd->runtime.tot_cp_points; i++) {
		bGPDcontrolpoint *cp = &cps[i];
		float color[4];
		copy_v3_v3(color, cp->color);
		color[3] = 0.8f;
		GPU_vertbuf_attr_set(vbo, color_id, idx, color);

		/* scale size */
		float size = cp->size * 0.8f;
		GPU_vertbuf_attr_set(vbo, size_id, idx, &size);

		GPU_vertbuf_attr_set(vbo, pos_id, idx, &cp->x);
		idx++;
	}

	return GPU_batch_create_ex(GPU_PRIM_POINTS, vbo, NULL, GPU_BATCH_OWNS_VBO);
}

/* create batch geometry data for current buffer fill shader */
GPUBatch *DRW_gpencil_get_buffer_fill_geom(bGPdata *gpd)
{
	if (gpd == NULL) {
		return NULL;
	}

	const tGPspoint *points = gpd->runtime.sbuffer;
	int totpoints = gpd->runtime.sbuffer_size;
	if (totpoints < 3) {
		return NULL;
	}

	const DRWContextState *draw_ctx = DRW_context_state_get();
	Scene *scene = draw_ctx->scene;
	ARegion *ar = draw_ctx->ar;
	ToolSettings *ts = scene->toolsettings;
	Object *ob = draw_ctx->obact;

	/* get origin to reproject point */
	float origin[3];
	bGPDlayer *gpl = BKE_gpencil_layer_getactive(gpd);
	ED_gp_get_drawing_reference(scene, ob, gpl, ts->gpencil_v3d_align, origin);

	int tot_triangles = totpoints - 2;
	/* allocate memory for temporary areas */
	uint (*tmp_triangles)[3] = MEM_mallocN(sizeof(*tmp_triangles) * tot_triangles, __func__);
	float (*points2d)[2] = MEM_mallocN(sizeof(*points2d) * totpoints, __func__);

	/* Convert points to array and triangulate
	 * Here a cache is not used because while drawing the information changes all the time, so the cache
	 * would be recalculated constantly, so it is better to do direct calculation for each function call
	 */
	for (int i = 0; i < totpoints; i++) {
		const tGPspoint *pt = &points[i];
		points2d[i][0] = pt->x;
		points2d[i][1] = pt->y;
	}
	BLI_polyfill_calc(points2d, (uint)totpoints, 0, tmp_triangles);

	static GPUVertFormat format = { 0 };
	static uint pos_id, color_id;
	if (format.attr_len == 0) {
		pos_id = GPU_vertformat_attr_add(&format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
		color_id = GPU_vertformat_attr_add(&format, "color", GPU_COMP_F32, 4, GPU_FETCH_FLOAT);
	}

	GPUVertBuf *vbo = GPU_vertbuf_create_with_format(&format);

	/* draw triangulation data */
	if (tot_triangles > 0) {
		GPU_vertbuf_data_alloc(vbo, tot_triangles * 3);

		const tGPspoint *tpt;
		bGPDspoint pt;

		int idx = 0;
		for (int i = 0; i < tot_triangles; i++) {
			for (int j = 0; j < 3; j++) {
				tpt = &points[tmp_triangles[i][j]];
				ED_gpencil_tpoint_to_point(ar, origin, tpt, &pt);
				GPU_vertbuf_attr_set(vbo, pos_id, idx, &pt.x);
				GPU_vertbuf_attr_set(vbo, color_id, idx, gpd->runtime.sfill);
				idx++;
			}
		}
	}

	/* clear memory */
	if (tmp_triangles) {
		MEM_freeN(tmp_triangles);
	}
	if (points2d) {
		MEM_freeN(points2d);
	}

	return GPU_batch_create_ex(GPU_PRIM_TRIS, vbo, NULL, GPU_BATCH_OWNS_VBO);
}

/* Draw selected verts for strokes being edited */
void DRW_gpencil_get_edit_geom(struct GpencilBatchCacheElem *be, bGPDstroke *gps, float alpha, short dflag)
{
	const DRWContextState *draw_ctx = DRW_context_state_get();
	Object *ob = draw_ctx->obact;
	bGPdata *gpd = ob->data;
	const bool is_weight_paint = (gpd) && (gpd->flag & GP_DATA_STROKE_WEIGHTMODE);

	int vgindex = ob->actdef - 1;
	if (!BLI_findlink(&ob->defbase, vgindex)) {
		vgindex = -1;
	}

	/* Get size of verts:
	 * - The selected state needs to be larger than the unselected state so that
	 *   they stand out more.
	 * - We use the theme setting for size of the unselected verts
	 */
	float bsize = UI_GetThemeValuef(TH_GP_VERTEX_SIZE);
	float vsize;
	if ((int)bsize > 8) {
		vsize = 10.0f;
		bsize = 8.0f;
	}
	else {
		vsize = bsize + 2;
	}

	/* for now, we assume that the base color of the points is not too close to the real color */
	float selectColor[4];
	UI_GetThemeColor3fv(TH_GP_VERTEX_SELECT, selectColor);
	selectColor[3] = alpha;

	float unselectColor[4];
	UI_GetThemeColor3fv(TH_GP_VERTEX, unselectColor);
	unselectColor[3] = alpha;

	if (be->vbo == NULL) {
		be->pos_id = GPU_vertformat_attr_add(&be->format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
		be->color_id = GPU_vertformat_attr_add(&be->format, "color", GPU_COMP_F32, 4, GPU_FETCH_FLOAT);
		be->thickness_id = GPU_vertformat_attr_add(&be->format, "size", GPU_COMP_F32, 1, GPU_FETCH_FLOAT);

		be->vbo = GPU_vertbuf_create_with_format(&be->format);
		GPU_vertbuf_data_alloc(be->vbo, gps->totpoints);
		be->vbo_len = 0;
	}
	gpencil_vbo_ensure_size(be, gps->totpoints);

	/* Draw start and end point differently if enabled stroke direction hint */
	bool show_direction_hint = (dflag & GP_DATA_SHOW_DIRECTION) && (gps->totpoints > 1);

	/* Draw all the stroke points (selected or not) */
	bGPDspoint *pt = gps->points;
	MDeformVert *dvert = gps->dvert;

	float fcolor[4];
	float fsize = 0;
	for (int i = 0; i < gps->totpoints; i++, pt++) {
		/* weight paint */
		if (is_weight_paint) {
			float weight = (dvert && dvert->dw) ? defvert_find_weight(dvert, vgindex) : 0.0f;
			float hue = 2.0f * (1.0f - weight) / 3.0f;
			hsv_to_rgb(hue, 1.0f, 1.0f, &selectColor[0], &selectColor[1], &selectColor[2]);
			selectColor[3] = 1.0f;
			copy_v4_v4(fcolor, selectColor);
			fsize = vsize;
		}
		else {
			if (show_direction_hint && i == 0) {
				/* start point in green bigger */
				ARRAY_SET_ITEMS(fcolor, 0.0f, 1.0f, 0.0f, 1.0f);
				fsize = vsize + 4;
			}
			else if (show_direction_hint && (i == gps->totpoints - 1)) {
				/* end point in red smaller */
				ARRAY_SET_ITEMS(fcolor, 1.0f, 0.0f, 0.0f, 1.0f);
				fsize = vsize + 1;
			}
			else if (pt->flag & GP_SPOINT_SELECT) {
				copy_v4_v4(fcolor, selectColor);
				fsize = vsize;
			}
			else {
				copy_v4_v4(fcolor, unselectColor);
				fsize = bsize;
			}
		}

		GPU_vertbuf_attr_set(be->vbo, be->color_id, be->vbo_len, fcolor);
		GPU_vertbuf_attr_set(be->vbo, be->thickness_id, be->vbo_len, &fsize);
		GPU_vertbuf_attr_set(be->vbo, be->pos_id, be->vbo_len, &pt->x);
		be->vbo_len++;
		if (gps->dvert != NULL) {
			dvert++;
		}
	}
}

/* Draw lines for strokes being edited */
void DRW_gpencil_get_edlin_geom(struct GpencilBatchCacheElem *be, bGPDstroke *gps, float alpha, short UNUSED(dflag))
{
	const DRWContextState *draw_ctx = DRW_context_state_get();
	Object *ob = draw_ctx->obact;
	bGPdata *gpd = ob->data;
	const bool is_weight_paint = (gpd) && (gpd->flag & GP_DATA_STROKE_WEIGHTMODE);

	int vgindex = ob->actdef - 1;
	if (!BLI_findlink(&ob->defbase, vgindex)) {
		vgindex = -1;
	}

	float selectColor[4];
	UI_GetThemeColor3fv(TH_GP_VERTEX_SELECT, selectColor);
	selectColor[3] = alpha;
	float linecolor[4];
	copy_v4_v4(linecolor, gpd->line_color);

	if (be->vbo == NULL) {
		be->pos_id = GPU_vertformat_attr_add(&be->format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
		be->color_id = GPU_vertformat_attr_add(&be->format, "color", GPU_COMP_F32, 4, GPU_FETCH_FLOAT);

		be->vbo = GPU_vertbuf_create_with_format(&be->format);
		GPU_vertbuf_data_alloc(be->vbo, gps->totpoints);
		be->vbo_len = 0;
	}
	gpencil_vbo_ensure_size(be, gps->totpoints);

	/* Draw all the stroke lines (selected or not) */
	bGPDspoint *pt = gps->points;
	MDeformVert *dvert = gps->dvert;

	float fcolor[4];
	for (int i = 0; i < gps->totpoints; i++, pt++) {
		/* weight paint */
		if (is_weight_paint) {
			float weight = (dvert && dvert->dw) ? defvert_find_weight(dvert, vgindex) : 0.0f;
			float hue = 2.0f * (1.0f - weight) / 3.0f;
			hsv_to_rgb(hue, 1.0f, 1.0f, &selectColor[0], &selectColor[1], &selectColor[2]);
			selectColor[3] = 1.0f;
			copy_v4_v4(fcolor, selectColor);
		}
		else {
			if (pt->flag & GP_SPOINT_SELECT) {
				copy_v4_v4(fcolor, selectColor);
			}
			else {
				copy_v4_v4(fcolor, linecolor);
			}
		}

		GPU_vertbuf_attr_set(be->vbo, be->color_id, be->vbo_len, fcolor);
		GPU_vertbuf_attr_set(be->vbo, be->pos_id, be->vbo_len, &pt->x);
		be->vbo_len++;

		if (gps->dvert != NULL) {
			dvert++;
		}
	}
}

static void set_grid_point(
        GPUVertBuf *vbo, int idx, float col_grid[4],
        uint pos_id, uint color_id,
        float v1, float v2, const int axis)
{
	GPU_vertbuf_attr_set(vbo, color_id, idx, col_grid);

	float pos[3];
	/* Set the grid in the selected axis */
	switch (axis) {
		case GP_LOCKAXIS_X:
		{
			ARRAY_SET_ITEMS(pos, 0.0f, v1, v2);
			break;
		}
		case GP_LOCKAXIS_Y:
		{
			ARRAY_SET_ITEMS(pos, v1, 0.0f, v2);
			break;
		}
		case GP_LOCKAXIS_Z:
		default:  /* view aligned */
		{
			ARRAY_SET_ITEMS(pos, v1, v2, 0.0f);
			break;
		}
	}

	GPU_vertbuf_attr_set(vbo, pos_id, idx, pos);
}

/* Draw grid lines */
GPUBatch *DRW_gpencil_get_grid(Object *ob)
{
	const DRWContextState *draw_ctx = DRW_context_state_get();
	Scene *scene = draw_ctx->scene;
	ToolSettings *ts = scene->toolsettings;
	View3D *v3d = draw_ctx->v3d;
	bGPdata *gpd = (bGPdata *)ob->data;
	const bool do_center = (gpd->grid.lines <= 0) ? false : true;

	float col_grid[4];

	/* verify we have something to draw and valid values */
	if (gpd->grid.scale[0] == 0.0f) {
		gpd->grid.scale[0] = 1.0f;
	}
	if (gpd->grid.scale[1] == 0.0f) {
		gpd->grid.scale[1] = 1.0f;
	}

	if (v3d->overlay.gpencil_grid_opacity < 0.1f) {
		v3d->overlay.gpencil_grid_opacity = 0.1f;
	}

	/* set color */
	copy_v3_v3(col_grid, gpd->grid.color);
	col_grid[3] = v3d->overlay.gpencil_grid_opacity;

	const int axis = ts->gp_sculpt.lock_axis;

	const char *grid_unit = NULL;
	const int gridlines = (gpd->grid.lines <= 0) ? 1 : gpd->grid.lines;
	const float grid_w = gpd->grid.scale[0] * ED_scene_grid_scale(scene, &grid_unit);
	const float grid_h = gpd->grid.scale[1] * ED_scene_grid_scale(scene, &grid_unit);
	const float space_w = (grid_w / gridlines);
	const float space_h = (grid_h / gridlines);
	const float offset[2] = { gpd->grid.offset[0], gpd->grid.offset[1] };

	const uint vertex_len = 2 * (gridlines * 4 + 2);

	static GPUVertFormat format = { 0 };
	static uint pos_id, color_id;
	if (format.attr_len == 0) {
		pos_id = GPU_vertformat_attr_add(&format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
		color_id = GPU_vertformat_attr_add(&format, "color", GPU_COMP_F32, 4, GPU_FETCH_FLOAT);
	}

	GPUVertBuf *vbo = GPU_vertbuf_create_with_format(&format);
	GPU_vertbuf_data_alloc(vbo, vertex_len);

	int idx = 0;

	for (int a = 1; a <= gridlines; a++) {
		const float line_w = a * space_w;
		const float line_h = a * space_h;

		set_grid_point(vbo, idx, col_grid, pos_id, color_id, -grid_w + offset[0], -line_h + offset[1], axis);
		idx++;
		set_grid_point(vbo, idx, col_grid, pos_id, color_id, +grid_w + offset[0], -line_h + offset[1], axis);
		idx++;
		set_grid_point(vbo, idx, col_grid, pos_id, color_id, -grid_w + offset[0], +line_h + offset[1], axis);
		idx++;
		set_grid_point(vbo, idx, col_grid, pos_id, color_id, +grid_w + offset[0], +line_h + offset[1], axis);
		idx++;

		set_grid_point(vbo, idx, col_grid, pos_id, color_id, -line_w + offset[0], -grid_h + offset[1], axis);
		idx++;
		set_grid_point(vbo, idx, col_grid, pos_id, color_id, -line_w + offset[0], +grid_h + offset[1], axis);
		idx++;
		set_grid_point(vbo, idx, col_grid, pos_id, color_id, +line_w + offset[0], -grid_h + offset[1], axis);
		idx++;
		set_grid_point(vbo, idx, col_grid, pos_id, color_id, +line_w + offset[0], +grid_h + offset[1], axis);
		idx++;
	}
	/* center lines */
	if (do_center) {
		set_grid_point(vbo, idx, col_grid, pos_id, color_id, -grid_w + offset[0], 0.0f + offset[1], axis);
		idx++;
		set_grid_point(vbo, idx, col_grid, pos_id, color_id, +grid_w + offset[0], 0.0f + offset[1], axis);
		idx++;

		set_grid_point(vbo, idx, col_grid, pos_id, color_id, 0.0f + offset[0], -grid_h + offset[1], axis);
		idx++;
		set_grid_point(vbo, idx, col_grid, pos_id, color_id, 0.0f + offset[0], +grid_h + offset[1], axis);
		idx++;
	}
	return GPU_batch_create_ex(GPU_PRIM_LINES, vbo, NULL, GPU_BATCH_OWNS_VBO);
}