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

draw_manager_data.c « intern « draw « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: dccb869c133165997ef1d60e96cd4400d7db4b4e (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
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
/*
 * Copyright 2016, Blender Foundation.
 *
 * 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.
 *
 * Contributor(s): Blender Institute
 *
 */

/** \file blender/draw/intern/draw_manager_data.c
 *  \ingroup draw
 */

#include "draw_manager.h"

#include "BKE_curve.h"
#include "BKE_global.h"
#include "BKE_mesh.h"
#include "BKE_object.h"
#include "BKE_paint.h"
#include "BKE_pbvh.h"

#include "DNA_curve_types.h"
#include "DNA_mesh_types.h"
#include "DNA_meta_types.h"

#include "BLI_link_utils.h"
#include "BLI_mempool.h"

#include "intern/gpu_codegen.h"

struct GPUVertFormat *g_pos_format = NULL;

extern struct GPUUniformBuffer *view_ubo; /* draw_manager_exec.c */

/* -------------------------------------------------------------------- */

/** \name Uniform Buffer Object (DRW_uniformbuffer)
 * \{ */

GPUUniformBuffer *DRW_uniformbuffer_create(int size, const void *data)
{
	return GPU_uniformbuffer_create(size, data, NULL);
}

void DRW_uniformbuffer_update(GPUUniformBuffer *ubo, const void *data)
{
	GPU_uniformbuffer_update(ubo, data);
}

void DRW_uniformbuffer_free(GPUUniformBuffer *ubo)
{
	GPU_uniformbuffer_free(ubo);
}

/** \} */

/* -------------------------------------------------------------------- */

/** \name Uniforms (DRW_shgroup_uniform)
 * \{ */

static void drw_shgroup_uniform_create_ex(DRWShadingGroup *shgroup, int loc,
                                            DRWUniformType type, const void *value, int length, int arraysize)
{
	DRWUniform *uni = BLI_mempool_alloc(DST.vmempool->uniforms);
	uni->location = loc;
	uni->type = type;
	uni->length = length;
	uni->arraysize = arraysize;

	switch (type) {
		case DRW_UNIFORM_INT_COPY:
			uni->ivalue = *((int *)value);
			break;
		case DRW_UNIFORM_BOOL_COPY:
			uni->ivalue = (int)*((bool *)value);
			break;
		case DRW_UNIFORM_FLOAT_COPY:
			uni->fvalue = *((float *)value);
			break;
		default:
			uni->pvalue = value;
			break;
	}

	BLI_LINKS_PREPEND(shgroup->uniforms, uni);
}

static void drw_shgroup_builtin_uniform(
        DRWShadingGroup *shgroup, int builtin, const void *value, int length, int arraysize)
{
	int loc = GPU_shader_get_builtin_uniform(shgroup->shader, builtin);

	if (loc != -1) {
		drw_shgroup_uniform_create_ex(shgroup, loc, DRW_UNIFORM_FLOAT, value, length, arraysize);
	}
}

static void drw_shgroup_uniform(DRWShadingGroup *shgroup, const char *name,
                                  DRWUniformType type, const void *value, int length, int arraysize)
{
	int location;
	if (ELEM(type, DRW_UNIFORM_BLOCK, DRW_UNIFORM_BLOCK_PERSIST)) {
		location = GPU_shader_get_uniform_block(shgroup->shader, name);
	}
	else {
		location = GPU_shader_get_uniform(shgroup->shader, name);
	}

	if (location == -1) {
		if (G.debug & G_DEBUG)
			fprintf(stderr, "Uniform '%s' not found!\n", name);
		/* Nice to enable eventually, for now eevee uses uniforms that might not exist. */
		// BLI_assert(0);
		return;
	}

	BLI_assert(arraysize > 0 && arraysize <= 16);
	BLI_assert(length >= 0 && length <= 16);

	drw_shgroup_uniform_create_ex(shgroup, location, type, value, length, arraysize);

#ifndef NDEBUG
	/* Save uniform name to easily identify it when debugging. */
	BLI_strncpy(shgroup->uniforms->name, name, MAX_UNIFORM_NAME);
#endif
}

void DRW_shgroup_uniform_texture(DRWShadingGroup *shgroup, const char *name, const GPUTexture *tex)
{
	BLI_assert(tex != NULL);
	drw_shgroup_uniform(shgroup, name, DRW_UNIFORM_TEXTURE, tex, 0, 1);
}

/* Same as DRW_shgroup_uniform_texture but is garanteed to be bound if shader does not change between shgrp. */
void DRW_shgroup_uniform_texture_persistent(DRWShadingGroup *shgroup, const char *name, const GPUTexture *tex)
{
	BLI_assert(tex != NULL);
	drw_shgroup_uniform(shgroup, name, DRW_UNIFORM_TEXTURE_PERSIST, tex, 0, 1);
}

void DRW_shgroup_uniform_block(DRWShadingGroup *shgroup, const char *name, const GPUUniformBuffer *ubo)
{
	BLI_assert(ubo != NULL);
	drw_shgroup_uniform(shgroup, name, DRW_UNIFORM_BLOCK, ubo, 0, 1);
}

/* Same as DRW_shgroup_uniform_block but is garanteed to be bound if shader does not change between shgrp. */
void DRW_shgroup_uniform_block_persistent(DRWShadingGroup *shgroup, const char *name, const GPUUniformBuffer *ubo)
{
	BLI_assert(ubo != NULL);
	drw_shgroup_uniform(shgroup, name, DRW_UNIFORM_BLOCK_PERSIST, ubo, 0, 1);
}

void DRW_shgroup_uniform_texture_ref(DRWShadingGroup *shgroup, const char *name, GPUTexture **tex)
{
	drw_shgroup_uniform(shgroup, name, DRW_UNIFORM_TEXTURE_REF, tex, 0, 1);
}

void DRW_shgroup_uniform_bool(DRWShadingGroup *shgroup, const char *name, const int *value, int arraysize)
{
	drw_shgroup_uniform(shgroup, name, DRW_UNIFORM_BOOL, value, 1, arraysize);
}

void DRW_shgroup_uniform_float(DRWShadingGroup *shgroup, const char *name, const float *value, int arraysize)
{
	drw_shgroup_uniform(shgroup, name, DRW_UNIFORM_FLOAT, value, 1, arraysize);
}

void DRW_shgroup_uniform_vec2(DRWShadingGroup *shgroup, const char *name, const float *value, int arraysize)
{
	drw_shgroup_uniform(shgroup, name, DRW_UNIFORM_FLOAT, value, 2, arraysize);
}

void DRW_shgroup_uniform_vec3(DRWShadingGroup *shgroup, const char *name, const float *value, int arraysize)
{
	drw_shgroup_uniform(shgroup, name, DRW_UNIFORM_FLOAT, value, 3, arraysize);
}

void DRW_shgroup_uniform_vec4(DRWShadingGroup *shgroup, const char *name, const float *value, int arraysize)
{
	drw_shgroup_uniform(shgroup, name, DRW_UNIFORM_FLOAT, value, 4, arraysize);
}

void DRW_shgroup_uniform_short_to_int(DRWShadingGroup *shgroup, const char *name, const short *value, int arraysize)
{
	drw_shgroup_uniform(shgroup, name, DRW_UNIFORM_SHORT_TO_INT, value, 1, arraysize);
}

void DRW_shgroup_uniform_short_to_float(DRWShadingGroup *shgroup, const char *name, const short *value, int arraysize)
{
	drw_shgroup_uniform(shgroup, name, DRW_UNIFORM_SHORT_TO_FLOAT, value, 1, arraysize);
}

void DRW_shgroup_uniform_int(DRWShadingGroup *shgroup, const char *name, const int *value, int arraysize)
{
	drw_shgroup_uniform(shgroup, name, DRW_UNIFORM_INT, value, 1, arraysize);
}

void DRW_shgroup_uniform_ivec2(DRWShadingGroup *shgroup, const char *name, const int *value, int arraysize)
{
	drw_shgroup_uniform(shgroup, name, DRW_UNIFORM_INT, value, 2, arraysize);
}

void DRW_shgroup_uniform_ivec3(DRWShadingGroup *shgroup, const char *name, const int *value, int arraysize)
{
	drw_shgroup_uniform(shgroup, name, DRW_UNIFORM_INT, value, 3, arraysize);
}

void DRW_shgroup_uniform_mat3(DRWShadingGroup *shgroup, const char *name, const float (*value)[3])
{
	drw_shgroup_uniform(shgroup, name, DRW_UNIFORM_FLOAT, (float *)value, 9, 1);
}

void DRW_shgroup_uniform_mat4(DRWShadingGroup *shgroup, const char *name, const float (*value)[4])
{
	drw_shgroup_uniform(shgroup, name, DRW_UNIFORM_FLOAT, (float *)value, 16, 1);
}

/* Stores the int instead of a pointer. */
void DRW_shgroup_uniform_int_copy(DRWShadingGroup *shgroup, const char *name, const int value)
{
	drw_shgroup_uniform(shgroup, name, DRW_UNIFORM_INT_COPY, &value, 1, 1);
}

void DRW_shgroup_uniform_bool_copy(DRWShadingGroup *shgroup, const char *name, const bool value)
{
	drw_shgroup_uniform(shgroup, name, DRW_UNIFORM_BOOL_COPY, &value, 1, 1);
}

void DRW_shgroup_uniform_float_copy(DRWShadingGroup *shgroup, const char *name, const float value)
{
	drw_shgroup_uniform(shgroup, name, DRW_UNIFORM_FLOAT_COPY, &value, 1, 1);
}


/** \} */

/* -------------------------------------------------------------------- */

/** \name Draw Call (DRW_calls)
 * \{ */

static void drw_call_calc_orco(Object *ob, float (*r_orcofacs)[3])
{
	ID *ob_data = (ob) ? ob->data : NULL;
	float *texcoloc = NULL;
	float *texcosize = NULL;
	if (ob_data != NULL) {
		switch (GS(ob_data->name)) {
			case ID_ME:
				BKE_mesh_texspace_get_reference((Mesh *)ob_data, NULL, &texcoloc, NULL, &texcosize);
				break;
			case ID_CU:
			{
				Curve *cu = (Curve *)ob_data;
				if (cu->bb == NULL || (cu->bb->flag & BOUNDBOX_DIRTY)) {
					BKE_curve_texspace_calc(cu);
				}
				texcoloc = cu->loc;
				texcosize = cu->size;
				break;
			}
			case ID_MB:
			{
				MetaBall *mb = (MetaBall *)ob_data;
				texcoloc = mb->loc;
				texcosize = mb->size;
				break;
			}
			default:
				break;
		}
	}

	if ((texcoloc != NULL) && (texcosize != NULL)) {
		mul_v3_v3fl(r_orcofacs[1], texcosize, 2.0f);
		invert_v3(r_orcofacs[1]);
		sub_v3_v3v3(r_orcofacs[0], texcoloc, texcosize);
		negate_v3(r_orcofacs[0]);
		mul_v3_v3(r_orcofacs[0], r_orcofacs[1]); /* result in a nice MADD in the shader */
	}
	else {
		copy_v3_fl(r_orcofacs[0], 0.0f);
		copy_v3_fl(r_orcofacs[1], 1.0f);
	}
}

static DRWCallState *drw_call_state_create(DRWShadingGroup *shgroup, float (*obmat)[4], Object *ob)
{
	DRWCallState *state = BLI_mempool_alloc(DST.vmempool->states);
	state->flag = 0;
	state->cache_id = 0;
	state->visibility_cb = NULL;
	state->matflag = shgroup->matflag;

	/* Matrices */
	if (obmat != NULL) {
		copy_m4_m4(state->model, obmat);

		if (is_negative_m4(state->model)) {
			state->flag |= DRW_CALL_NEGSCALE;
		}
	}
	else {
		unit_m4(state->model);
	}

	if (ob != NULL) {
		float corner[3];
		BoundBox *bbox = BKE_object_boundbox_get(ob);
		/* Get BoundSphere center and radius from the BoundBox. */
		mid_v3_v3v3(state->bsphere.center, bbox->vec[0], bbox->vec[6]);
		mul_v3_m4v3(corner, obmat, bbox->vec[0]);
		mul_m4_v3(obmat, state->bsphere.center);
		state->bsphere.radius = len_v3v3(state->bsphere.center, corner);
	}
	else {
		/* Bypass test. */
		state->bsphere.radius = -1.0f;
	}

	/* Orco factors: We compute this at creation to not have to save the *ob_data */
	if ((state->matflag & DRW_CALL_ORCOTEXFAC) != 0) {
		drw_call_calc_orco(ob, state->orcotexfac);
		state->matflag &= ~DRW_CALL_ORCOTEXFAC;
	}

	return state;
}

static DRWCallState *drw_call_state_object(DRWShadingGroup *shgroup, float (*obmat)[4], Object *ob)
{
	if (DST.ob_state == NULL) {
		DST.ob_state = drw_call_state_create(shgroup, obmat, ob);
	}
	else {
		/* If the DRWCallState is reused, add necessary matrices. */
		DST.ob_state->matflag |= shgroup->matflag;
	}

	return DST.ob_state;
}

void DRW_shgroup_call_add(DRWShadingGroup *shgroup, GPUBatch *geom, float (*obmat)[4])
{
	BLI_assert(geom != NULL);
	BLI_assert(ELEM(shgroup->type, DRW_SHG_NORMAL, DRW_SHG_FEEDBACK_TRANSFORM));

	DRWCall *call = BLI_mempool_alloc(DST.vmempool->calls);
	call->state = drw_call_state_create(shgroup, obmat, NULL);
	call->type = DRW_CALL_SINGLE;
	call->single.geometry = geom;
#ifdef USE_GPU_SELECT
	call->select_id = DST.select_id;
#endif

	BLI_LINKS_APPEND(&shgroup->calls, call);
}

void DRW_shgroup_call_range_add(DRWShadingGroup *shgroup, GPUBatch *geom, float (*obmat)[4], uint v_sta, uint v_count)
{
	BLI_assert(geom != NULL);
	BLI_assert(ELEM(shgroup->type, DRW_SHG_NORMAL, DRW_SHG_FEEDBACK_TRANSFORM));
	BLI_assert(v_count);

	DRWCall *call = BLI_mempool_alloc(DST.vmempool->calls);
	call->state = drw_call_state_create(shgroup, obmat, NULL);
	call->type = DRW_CALL_RANGE;
	call->range.geometry = geom;
	call->range.start = v_sta;
	call->range.count = v_count;
#ifdef USE_GPU_SELECT
	call->select_id = DST.select_id;
#endif

	BLI_LINKS_APPEND(&shgroup->calls, call);
}

static void drw_shgroup_call_procedural_add_ex(
        DRWShadingGroup *shgroup, GPUPrimType prim_type, uint vert_count, float (*obmat)[4], Object *ob)
{
	BLI_assert(ELEM(shgroup->type, DRW_SHG_NORMAL, DRW_SHG_FEEDBACK_TRANSFORM));

	DRWCall *call = BLI_mempool_alloc(DST.vmempool->calls);
	if (ob) {
		call->state = drw_call_state_object(shgroup, ob->obmat, ob);
	}
	else {
		call->state = drw_call_state_create(shgroup, obmat, NULL);
	}
	call->type = DRW_CALL_PROCEDURAL;
	call->procedural.prim_type = prim_type;
	call->procedural.vert_count = vert_count;
#ifdef USE_GPU_SELECT
	call->select_id = DST.select_id;
#endif

	BLI_LINKS_APPEND(&shgroup->calls, call);
}

void DRW_shgroup_call_procedural_points_add(DRWShadingGroup *shgroup, uint point_len, float (*obmat)[4])
{
	drw_shgroup_call_procedural_add_ex(shgroup, GPU_PRIM_POINTS, point_len, obmat, NULL);
}

void DRW_shgroup_call_procedural_lines_add(DRWShadingGroup *shgroup, uint line_count, float (*obmat)[4])
{
	drw_shgroup_call_procedural_add_ex(shgroup, GPU_PRIM_LINES, line_count * 2, obmat, NULL);
}

void DRW_shgroup_call_procedural_triangles_add(DRWShadingGroup *shgroup, uint tria_count, float (*obmat)[4])
{
	drw_shgroup_call_procedural_add_ex(shgroup, GPU_PRIM_TRIS, tria_count * 3, obmat, NULL);
}

/* TODO (fclem): this is a sign that the api is starting to be limiting.
 * Maybe add special function that general purpose for special cases. */
void DRW_shgroup_call_object_procedural_triangles_culled_add(DRWShadingGroup *shgroup, uint tria_count, Object *ob)
{
	drw_shgroup_call_procedural_add_ex(shgroup, GPU_PRIM_TRIS, tria_count * 3, NULL, ob);
}

/* These calls can be culled and are optimized for redraw */
void DRW_shgroup_call_object_add_ex(DRWShadingGroup *shgroup, GPUBatch *geom, Object *ob, bool bypass_culling)
{
	BLI_assert(geom != NULL);
	BLI_assert(ELEM(shgroup->type, DRW_SHG_NORMAL, DRW_SHG_FEEDBACK_TRANSFORM));

	DRWCall *call = BLI_mempool_alloc(DST.vmempool->calls);
	call->state = drw_call_state_object(shgroup, ob->obmat, ob);
	call->type = DRW_CALL_SINGLE;
	call->single.geometry = geom;
#ifdef USE_GPU_SELECT
	call->select_id = DST.select_id;
#endif

	/* NOTE this will disable culling for the whole object. */
	call->state->flag |= (bypass_culling) ? DRW_CALL_BYPASS_CULLING : 0;

	BLI_LINKS_APPEND(&shgroup->calls, call);
}

void DRW_shgroup_call_object_add_with_callback(
        DRWShadingGroup *shgroup, GPUBatch *geom, Object *ob,
        DRWCallVisibilityFn *callback, void *user_data)
{
	BLI_assert(geom != NULL);
	BLI_assert(ELEM(shgroup->type, DRW_SHG_NORMAL, DRW_SHG_FEEDBACK_TRANSFORM));

	DRWCall *call = BLI_mempool_alloc(DST.vmempool->calls);
	call->state = drw_call_state_object(shgroup, ob->obmat, ob);
	call->state->visibility_cb = callback;
	call->state->user_data = user_data;
	call->type = DRW_CALL_SINGLE;
	call->single.geometry = geom;
#ifdef USE_GPU_SELECT
	call->select_id = DST.select_id;
#endif

	BLI_LINKS_APPEND(&shgroup->calls, call);
}

void DRW_shgroup_call_instances_add(DRWShadingGroup *shgroup, GPUBatch *geom, float (*obmat)[4], uint *count)
{
	BLI_assert(geom != NULL);
	BLI_assert(ELEM(shgroup->type, DRW_SHG_NORMAL, DRW_SHG_FEEDBACK_TRANSFORM));

	DRWCall *call = BLI_mempool_alloc(DST.vmempool->calls);
	call->state = drw_call_state_create(shgroup, obmat, NULL);
	call->type = DRW_CALL_INSTANCES;
	call->instances.geometry = geom;
	call->instances.count = count;
#ifdef USE_GPU_SELECT
	call->select_id = DST.select_id;
#endif

	BLI_LINKS_APPEND(&shgroup->calls, call);
}

/* These calls can be culled and are optimized for redraw */
void DRW_shgroup_call_object_instances_add(DRWShadingGroup *shgroup, GPUBatch *geom, Object *ob, uint *count)
{
	BLI_assert(geom != NULL);
	BLI_assert(ELEM(shgroup->type, DRW_SHG_NORMAL, DRW_SHG_FEEDBACK_TRANSFORM));

	DRWCall *call = BLI_mempool_alloc(DST.vmempool->calls);
	call->state = drw_call_state_object(shgroup, ob->obmat, ob);
	call->type = DRW_CALL_INSTANCES;
	call->instances.geometry = geom;
	call->instances.count = count;
#ifdef USE_GPU_SELECT
	call->select_id = DST.select_id;
#endif

	BLI_LINKS_APPEND(&shgroup->calls, call);
}

void DRW_shgroup_call_generate_add(
        DRWShadingGroup *shgroup,
        DRWCallGenerateFn *geometry_fn, void *user_data,
        float (*obmat)[4])
{
	BLI_assert(geometry_fn != NULL);
	BLI_assert(ELEM(shgroup->type, DRW_SHG_NORMAL, DRW_SHG_FEEDBACK_TRANSFORM));

	DRWCall *call = BLI_mempool_alloc(DST.vmempool->calls);
	call->state = drw_call_state_create(shgroup, obmat, NULL);
	call->type = DRW_CALL_GENERATE;
	call->generate.geometry_fn = geometry_fn;
	call->generate.user_data = user_data;
#ifdef USE_GPU_SELECT
	call->select_id = DST.select_id;
#endif

	BLI_LINKS_APPEND(&shgroup->calls, call);
}

static void sculpt_draw_cb(
        DRWShadingGroup *shgroup,
        void (*draw_fn)(DRWShadingGroup *shgroup, GPUBatch *geom),
        void *user_data)
{
	Object *ob = user_data;
	PBVH *pbvh = ob->sculpt->pbvh;

	if (pbvh) {
		BKE_pbvh_draw_cb(
		        pbvh, NULL, NULL, false,
		        (void (*)(void *, GPUBatch *))draw_fn, shgroup);
	}
}

void DRW_shgroup_call_sculpt_add(DRWShadingGroup *shgroup, Object *ob, float (*obmat)[4])
{
	DRW_shgroup_call_generate_add(shgroup, sculpt_draw_cb, ob, obmat);
}

void DRW_shgroup_call_dynamic_add_array(DRWShadingGroup *shgroup, const void *attr[], uint attr_len)
{
#ifdef USE_GPU_SELECT
	if (G.f & G_PICKSEL) {
		if (shgroup->instance_count == shgroup->inst_selectid->vertex_len) {
			GPU_vertbuf_data_resize(shgroup->inst_selectid, shgroup->instance_count + 32);
		}
		GPU_vertbuf_attr_set(shgroup->inst_selectid, 0, shgroup->instance_count, &DST.select_id);
	}
#endif

	BLI_assert(attr_len == shgroup->attribs_count);
	UNUSED_VARS_NDEBUG(attr_len);

	for (int i = 0; i < attr_len; ++i) {
		if (shgroup->instance_count == shgroup->instance_vbo->vertex_len) {
			GPU_vertbuf_data_resize(shgroup->instance_vbo, shgroup->instance_count + 32);
		}
		GPU_vertbuf_attr_set(shgroup->instance_vbo, i, shgroup->instance_count, attr[i]);
	}

	shgroup->instance_count += 1;
}

/** \} */

/* -------------------------------------------------------------------- */

/** \name Shading Groups (DRW_shgroup)
 * \{ */

static void drw_shgroup_init(DRWShadingGroup *shgroup, GPUShader *shader)
{
	shgroup->instance_geom = NULL;
	shgroup->instance_vbo = NULL;
	shgroup->instance_count = 0;
	shgroup->uniforms = NULL;
#ifdef USE_GPU_SELECT
	shgroup->inst_selectid = NULL;
	shgroup->override_selectid = -1;
#endif
#ifndef NDEBUG
	shgroup->attribs_count = 0;
#endif

	int view_ubo_location = GPU_shader_get_uniform_block(shader, "viewBlock");

	if (view_ubo_location != -1) {
		drw_shgroup_uniform_create_ex(shgroup, view_ubo_location, DRW_UNIFORM_BLOCK_PERSIST, view_ubo, 0, 1);
	}
	else {
		/* Only here to support builtin shaders. This should not be used by engines. */
		drw_shgroup_builtin_uniform(shgroup, GPU_UNIFORM_VIEW, DST.view_data.matstate.mat[DRW_MAT_VIEW], 16, 1);
		drw_shgroup_builtin_uniform(shgroup, GPU_UNIFORM_VIEW_INV, DST.view_data.matstate.mat[DRW_MAT_VIEWINV], 16, 1);
		drw_shgroup_builtin_uniform(shgroup, GPU_UNIFORM_VIEWPROJECTION, DST.view_data.matstate.mat[DRW_MAT_PERS], 16, 1);
		drw_shgroup_builtin_uniform(shgroup, GPU_UNIFORM_VIEWPROJECTION_INV, DST.view_data.matstate.mat[DRW_MAT_PERSINV], 16, 1);
		drw_shgroup_builtin_uniform(shgroup, GPU_UNIFORM_PROJECTION, DST.view_data.matstate.mat[DRW_MAT_WIN], 16, 1);
		drw_shgroup_builtin_uniform(shgroup, GPU_UNIFORM_PROJECTION_INV, DST.view_data.matstate.mat[DRW_MAT_WININV], 16, 1);
		drw_shgroup_builtin_uniform(shgroup, GPU_UNIFORM_CAMERATEXCO, DST.view_data.viewcamtexcofac, 3, 2);
	}

	shgroup->model = GPU_shader_get_builtin_uniform(shader, GPU_UNIFORM_MODEL);
	shgroup->modelinverse = GPU_shader_get_builtin_uniform(shader, GPU_UNIFORM_MODEL_INV);
	shgroup->modelview = GPU_shader_get_builtin_uniform(shader, GPU_UNIFORM_MODELVIEW);
	shgroup->modelviewinverse = GPU_shader_get_builtin_uniform(shader, GPU_UNIFORM_MODELVIEW_INV);
	shgroup->modelviewprojection = GPU_shader_get_builtin_uniform(shader, GPU_UNIFORM_MVP);
	shgroup->normalview = GPU_shader_get_builtin_uniform(shader, GPU_UNIFORM_NORMAL);
	shgroup->normalworld = GPU_shader_get_builtin_uniform(shader, GPU_UNIFORM_WORLDNORMAL);
	shgroup->orcotexfac = GPU_shader_get_builtin_uniform(shader, GPU_UNIFORM_ORCO);
	shgroup->eye = GPU_shader_get_builtin_uniform(shader, GPU_UNIFORM_EYE);
	shgroup->callid = GPU_shader_get_builtin_uniform(shader, GPU_UNIFORM_CALLID);

	shgroup->matflag = 0;
	if (shgroup->modelinverse > -1)
		shgroup->matflag |= DRW_CALL_MODELINVERSE;
	if (shgroup->modelview > -1)
		shgroup->matflag |= DRW_CALL_MODELVIEW;
	if (shgroup->modelviewinverse > -1)
		shgroup->matflag |= DRW_CALL_MODELVIEWINVERSE;
	if (shgroup->modelviewprojection > -1)
		shgroup->matflag |= DRW_CALL_MODELVIEWPROJECTION;
	if (shgroup->normalview > -1)
		shgroup->matflag |= DRW_CALL_NORMALVIEW;
	if (shgroup->normalworld > -1)
		shgroup->matflag |= DRW_CALL_NORMALWORLD;
	if (shgroup->orcotexfac > -1)
		shgroup->matflag |= DRW_CALL_ORCOTEXFAC;
	if (shgroup->eye > -1)
		shgroup->matflag |= DRW_CALL_EYEVEC;
}

static void drw_shgroup_instance_init(
        DRWShadingGroup *shgroup, GPUShader *shader, GPUBatch *batch, GPUVertFormat *format)
{
	BLI_assert(shgroup->type == DRW_SHG_INSTANCE);
	BLI_assert(batch != NULL);
	BLI_assert(format != NULL);

	drw_shgroup_init(shgroup, shader);

	shgroup->instance_geom = batch;
#ifndef NDEBUG
	shgroup->attribs_count = format->attr_len;
#endif

	DRW_instancing_buffer_request(DST.idatalist, format, batch, shgroup,
	                              &shgroup->instance_geom, &shgroup->instance_vbo);

#ifdef USE_GPU_SELECT
	if (G.f & G_PICKSEL) {
		/* Not actually used for rendering but alloced in one chunk.
		 * Plus we don't have to care about ownership. */
		static GPUVertFormat inst_select_format = {0};
		if (inst_select_format.attr_len == 0) {
			GPU_vertformat_attr_add(&inst_select_format, "selectId", GPU_COMP_I32, 1, GPU_FETCH_INT);
		}
		GPUBatch *batch_dummy; /* Not used */
		DRW_batching_buffer_request(DST.idatalist, &inst_select_format,
		                            GPU_PRIM_POINTS, shgroup,
		                            &batch_dummy, &shgroup->inst_selectid);
	}
#endif
}

static void drw_shgroup_batching_init(
        DRWShadingGroup *shgroup, GPUShader *shader, GPUVertFormat *format)
{
	drw_shgroup_init(shgroup, shader);

#ifndef NDEBUG
	shgroup->attribs_count = (format != NULL) ? format->attr_len : 0;
#endif
	BLI_assert(format != NULL);

	GPUPrimType type;
	switch (shgroup->type) {
		case DRW_SHG_POINT_BATCH: type = GPU_PRIM_POINTS; break;
		case DRW_SHG_LINE_BATCH: type = GPU_PRIM_LINES; break;
		case DRW_SHG_TRIANGLE_BATCH: type = GPU_PRIM_TRIS; break;
		default: type = GPU_PRIM_NONE; BLI_assert(0); break;
	}

	DRW_batching_buffer_request(DST.idatalist, format, type, shgroup,
	                            &shgroup->batch_geom, &shgroup->batch_vbo);

#ifdef USE_GPU_SELECT
	if (G.f & G_PICKSEL) {
		/* Not actually used for rendering but alloced in one chunk. */
		static GPUVertFormat inst_select_format = {0};
		if (inst_select_format.attr_len == 0) {
			GPU_vertformat_attr_add(&inst_select_format, "selectId", GPU_COMP_I32, 1, GPU_FETCH_INT);
		}
		GPUBatch *batch; /* Not used */
		DRW_batching_buffer_request(DST.idatalist, &inst_select_format,
		                            GPU_PRIM_POINTS, shgroup,
		                            &batch, &shgroup->inst_selectid);
	}
#endif
}

static DRWShadingGroup *drw_shgroup_create_ex(struct GPUShader *shader, DRWPass *pass)
{
	DRWShadingGroup *shgroup = BLI_mempool_alloc(DST.vmempool->shgroups);

	BLI_LINKS_APPEND(&pass->shgroups, shgroup);

	shgroup->type = DRW_SHG_NORMAL;
	shgroup->shader = shader;
	shgroup->state_extra = 0;
	shgroup->state_extra_disable = ~0x0;
	shgroup->stencil_mask = 0;
	shgroup->calls.first = NULL;
	shgroup->calls.last = NULL;
#if 0 /* All the same in the union! */
	shgroup->batch_geom = NULL;
	shgroup->batch_vbo = NULL;

	shgroup->instance_geom = NULL;
	shgroup->instance_vbo = NULL;
#endif

#ifdef USE_GPU_SELECT
	shgroup->pass_parent = pass;
#endif

	return shgroup;
}

static DRWShadingGroup *drw_shgroup_material_create_ex(GPUPass *gpupass, DRWPass *pass)
{
	if (!gpupass) {
		/* Shader compilation error */
		return NULL;
	}

	GPUShader *sh = GPU_pass_shader_get(gpupass);

	if (!sh) {
		/* Shader not yet compiled */
		return NULL;
	}

	DRWShadingGroup *grp = drw_shgroup_create_ex(sh, pass);
	return grp;
}

static DRWShadingGroup *drw_shgroup_material_inputs(DRWShadingGroup *grp, struct GPUMaterial *material)
{
	/* TODO : Ideally we should not convert. But since the whole codegen
	 * is relying on GPUPass we keep it as is for now. */

	ListBase *inputs = GPU_material_get_inputs(material);

	/* Converting dynamic GPUInput to DRWUniform */
	for (GPUInput *input = inputs->first; input; input = input->next) {
		/* Textures */
		if (input->ima) {
			double time = 0.0; /* TODO make time variable */
			GPUTexture *tex = GPU_texture_from_blender(
			        input->ima, input->iuser, input->textarget, input->image_isdata, time);

			if (input->bindtex) {
				DRW_shgroup_uniform_texture(grp, input->shadername, tex);
			}
		}
		/* Color Ramps */
		else if (input->tex) {
			DRW_shgroup_uniform_texture(grp, input->shadername, input->tex);
		}
		/* Floats */
		else {
			switch (input->type) {
				case GPU_FLOAT:
				case GPU_VEC2:
				case GPU_VEC3:
				case GPU_VEC4:
					/* Should already be in the material ubo. */
					break;
				case GPU_MAT3:
					DRW_shgroup_uniform_mat3(grp, input->shadername, (float (*)[3])input->dynamicvec);
					break;
				case GPU_MAT4:
					DRW_shgroup_uniform_mat4(grp, input->shadername, (float (*)[4])input->dynamicvec);
					break;
				default:
					break;
			}
		}
	}

	GPUUniformBuffer *ubo = GPU_material_uniform_buffer_get(material);
	if (ubo != NULL) {
		DRW_shgroup_uniform_block(grp, GPU_UBO_BLOCK_NAME, ubo);
	}

	return grp;
}

GPUVertFormat *DRW_shgroup_instance_format_array(const DRWInstanceAttribFormat attribs[], int arraysize)
{
	GPUVertFormat *format = MEM_callocN(sizeof(GPUVertFormat), "GPUVertFormat");

	for (int i = 0; i < arraysize; ++i) {
		GPU_vertformat_attr_add(format, attribs[i].name,
		                        (attribs[i].type == DRW_ATTRIB_INT) ? GPU_COMP_I32 : GPU_COMP_F32,
		                        attribs[i].components,
		                        (attribs[i].type == DRW_ATTRIB_INT) ? GPU_FETCH_INT : GPU_FETCH_FLOAT);
	}
	return format;
}

DRWShadingGroup *DRW_shgroup_material_create(
        struct GPUMaterial *material, DRWPass *pass)
{
	GPUPass *gpupass = GPU_material_get_pass(material);
	DRWShadingGroup *shgroup = drw_shgroup_material_create_ex(gpupass, pass);

	if (shgroup) {
		drw_shgroup_init(shgroup, GPU_pass_shader_get(gpupass));
		drw_shgroup_material_inputs(shgroup, material);
	}

	return shgroup;
}

DRWShadingGroup *DRW_shgroup_material_instance_create(
        struct GPUMaterial *material, DRWPass *pass, GPUBatch *geom, Object *ob, GPUVertFormat *format)
{
	GPUPass *gpupass = GPU_material_get_pass(material);
	DRWShadingGroup *shgroup = drw_shgroup_material_create_ex(gpupass, pass);

	if (shgroup) {
		shgroup->type = DRW_SHG_INSTANCE;
		shgroup->instance_geom = geom;
		drw_call_calc_orco(ob, shgroup->instance_orcofac);
		drw_shgroup_instance_init(shgroup, GPU_pass_shader_get(gpupass), geom, format);
		drw_shgroup_material_inputs(shgroup, material);
	}

	return shgroup;
}

DRWShadingGroup *DRW_shgroup_material_empty_tri_batch_create(
        struct GPUMaterial *material, DRWPass *pass, int tri_count)
{
#ifdef USE_GPU_SELECT
	BLI_assert((G.f & G_PICKSEL) == 0);
#endif
	GPUPass *gpupass = GPU_material_get_pass(material);
	DRWShadingGroup *shgroup = drw_shgroup_material_create_ex(gpupass, pass);

	if (shgroup) {
		/* Calling drw_shgroup_init will cause it to call GPU_draw_primitive(). */
		drw_shgroup_init(shgroup, GPU_pass_shader_get(gpupass));
		shgroup->type = DRW_SHG_TRIANGLE_BATCH;
		shgroup->instance_count = tri_count * 3;
		drw_shgroup_material_inputs(shgroup, material);
	}

	return shgroup;
}

DRWShadingGroup *DRW_shgroup_create(struct GPUShader *shader, DRWPass *pass)
{
	DRWShadingGroup *shgroup = drw_shgroup_create_ex(shader, pass);
	drw_shgroup_init(shgroup, shader);
	return shgroup;
}

DRWShadingGroup *DRW_shgroup_instance_create(
        struct GPUShader *shader, DRWPass *pass, GPUBatch *geom, GPUVertFormat *format)
{
	DRWShadingGroup *shgroup = drw_shgroup_create_ex(shader, pass);
	shgroup->type = DRW_SHG_INSTANCE;
	shgroup->instance_geom = geom;
	drw_call_calc_orco(NULL, shgroup->instance_orcofac);
	drw_shgroup_instance_init(shgroup, shader, geom, format);

	return shgroup;
}

DRWShadingGroup *DRW_shgroup_point_batch_create(struct GPUShader *shader, DRWPass *pass)
{
	DRW_shgroup_instance_format(g_pos_format, {{"pos", DRW_ATTRIB_FLOAT, 3}});

	DRWShadingGroup *shgroup = drw_shgroup_create_ex(shader, pass);
	shgroup->type = DRW_SHG_POINT_BATCH;

	drw_shgroup_batching_init(shgroup, shader, g_pos_format);

	return shgroup;
}

DRWShadingGroup *DRW_shgroup_line_batch_create_with_format(
        struct GPUShader *shader, DRWPass *pass, GPUVertFormat *format)
{
	DRWShadingGroup *shgroup = drw_shgroup_create_ex(shader, pass);
	shgroup->type = DRW_SHG_LINE_BATCH;

	drw_shgroup_batching_init(shgroup, shader, format);

	return shgroup;
}

DRWShadingGroup *DRW_shgroup_line_batch_create(struct GPUShader *shader, DRWPass *pass)
{
	DRW_shgroup_instance_format(g_pos_format, {{"pos", DRW_ATTRIB_FLOAT, 3}});

	return DRW_shgroup_line_batch_create_with_format(shader, pass, g_pos_format);
}

/* Very special batch. Use this if you position
 * your vertices with the vertex shader
 * and dont need any VBO attrib */
DRWShadingGroup *DRW_shgroup_empty_tri_batch_create(struct GPUShader *shader, DRWPass *pass, int tri_count)
{
#ifdef USE_GPU_SELECT
	BLI_assert((G.f & G_PICKSEL) == 0);
#endif
	DRWShadingGroup *shgroup = drw_shgroup_create_ex(shader, pass);

	/* Calling drw_shgroup_init will cause it to call GPU_draw_primitive(). */
	drw_shgroup_init(shgroup, shader);

	shgroup->type = DRW_SHG_TRIANGLE_BATCH;
	shgroup->instance_count = tri_count * 3;

	return shgroup;
}

DRWShadingGroup *DRW_shgroup_transform_feedback_create(struct GPUShader *shader, DRWPass *pass, GPUVertBuf *tf_target)
{
	BLI_assert(tf_target != NULL);
	DRWShadingGroup *shgroup = drw_shgroup_create_ex(shader, pass);
	shgroup->type = DRW_SHG_FEEDBACK_TRANSFORM;

	drw_shgroup_init(shgroup, shader);

	shgroup->tfeedback_target = tf_target;

	return shgroup;
}

/* Specify an external batch instead of adding each attrib one by one. */
void DRW_shgroup_instance_batch(DRWShadingGroup *shgroup, struct GPUBatch *batch)
{
	BLI_assert(shgroup->type == DRW_SHG_INSTANCE);
	BLI_assert(shgroup->instance_count == 0);
	/* You cannot use external instancing batch without a dummy format. */
	BLI_assert(shgroup->attribs_count != 0);

	shgroup->type = DRW_SHG_INSTANCE_EXTERNAL;
	drw_call_calc_orco(NULL, shgroup->instance_orcofac);
	/* PERF : This destroys the vaos cache so better check if it's necessary. */
	/* Note: This WILL break if batch->verts[0] is destroyed and reallocated
	 * at the same adress. Bindings/VAOs would remain obsolete. */
	//if (shgroup->instancing_geom->inst != batch->verts[0])
	GPU_batch_instbuf_set(shgroup->instance_geom, batch->verts[0], false);

#ifdef USE_GPU_SELECT
	shgroup->override_selectid = DST.select_id;
#endif
}

uint DRW_shgroup_get_instance_count(const DRWShadingGroup *shgroup)
{
	return shgroup->instance_count;
}

/**
 * State is added to #Pass.state while drawing.
 * Use to temporarily enable draw options.
 */
void DRW_shgroup_state_enable(DRWShadingGroup *shgroup, DRWState state)
{
	shgroup->state_extra |= state;
}

void DRW_shgroup_state_disable(DRWShadingGroup *shgroup, DRWState state)
{
	shgroup->state_extra_disable &= ~state;
}

void DRW_shgroup_stencil_mask(DRWShadingGroup *shgroup, uint mask)
{
	BLI_assert(mask <= 255);
	shgroup->stencil_mask = mask;
}

/** \} */

/* -------------------------------------------------------------------- */

/** \name Passes (DRW_pass)
 * \{ */

DRWPass *DRW_pass_create(const char *name, DRWState state)
{
	DRWPass *pass = BLI_mempool_alloc(DST.vmempool->passes);
	pass->state = state;
	if (G.debug_value > 20) {
		BLI_strncpy(pass->name, name, MAX_PASS_NAME);
	}

	pass->shgroups.first = NULL;
	pass->shgroups.last = NULL;

	return pass;
}

void DRW_pass_state_set(DRWPass *pass, DRWState state)
{
	pass->state = state;
}

void DRW_pass_state_add(DRWPass *pass, DRWState state)
{
	pass->state |= state;
}

void DRW_pass_state_remove(DRWPass *pass, DRWState state)
{
	pass->state &= ~state;
}

void DRW_pass_free(DRWPass *pass)
{
	pass->shgroups.first = NULL;
	pass->shgroups.last = NULL;
}

void DRW_pass_foreach_shgroup(DRWPass *pass, void (*callback)(void *userData, DRWShadingGroup *shgrp), void *userData)
{
	for (DRWShadingGroup *shgroup = pass->shgroups.first; shgroup; shgroup = shgroup->next) {
		callback(userData, shgroup);
	}
}

typedef struct ZSortData {
	float *axis;
	float *origin;
} ZSortData;

static int pass_shgroup_dist_sort(void *thunk, const void *a, const void *b)
{
	const ZSortData *zsortdata = (ZSortData *)thunk;
	const DRWShadingGroup *shgrp_a = (const DRWShadingGroup *)a;
	const DRWShadingGroup *shgrp_b = (const DRWShadingGroup *)b;

	const DRWCall *call_a = (DRWCall *)shgrp_a->calls.first;
	const DRWCall *call_b = (DRWCall *)shgrp_b->calls.first;

	if (call_a == NULL) return -1;
	if (call_b == NULL) return -1;

	float tmp[3];
	sub_v3_v3v3(tmp, zsortdata->origin, call_a->state->model[3]);
	const float a_sq = dot_v3v3(zsortdata->axis, tmp);
	sub_v3_v3v3(tmp, zsortdata->origin, call_b->state->model[3]);
	const float b_sq = dot_v3v3(zsortdata->axis, tmp);

	if      (a_sq < b_sq) return  1;
	else if (a_sq > b_sq) return -1;
	else {
		/* If there is a depth prepass put it before */
		if ((shgrp_a->state_extra & DRW_STATE_WRITE_DEPTH) != 0) {
			return -1;
		}
		else if ((shgrp_b->state_extra & DRW_STATE_WRITE_DEPTH) != 0) {
			return  1;
		}
		else return  0;
	}
}

/* ------------------ Shading group sorting --------------------- */

#define SORT_IMPL_LINKTYPE DRWShadingGroup

#define SORT_IMPL_USE_THUNK
#define SORT_IMPL_FUNC shgroup_sort_fn_r
#include "../../blenlib/intern/list_sort_impl.h"
#undef SORT_IMPL_FUNC
#undef SORT_IMPL_USE_THUNK

#undef SORT_IMPL_LINKTYPE

/**
 * Sort Shading groups by decreasing Z of their first draw call.
 * This is usefull for order dependant effect such as transparency.
 **/
void DRW_pass_sort_shgroup_z(DRWPass *pass)
{
	float (*viewinv)[4];
	viewinv = DST.view_data.matstate.mat[DRW_MAT_VIEWINV];

	ZSortData zsortdata = {viewinv[2], viewinv[3]};

	if (pass->shgroups.first && pass->shgroups.first->next) {
		pass->shgroups.first = shgroup_sort_fn_r(pass->shgroups.first, pass_shgroup_dist_sort, &zsortdata);

		/* Find the next last */
		DRWShadingGroup *last = pass->shgroups.first;
		while ((last = last->next)) {
			/* Do nothing */
		}
		pass->shgroups.last = last;
	}
}

/** \} */