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

drawobject.c « space_view3d « editors « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3649c6f6dbb18983c8e0c8c54e5c0d11e4e830c3 (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
/*
 * ***** 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) 2001-2002 by NaN Holding BV.
 * All rights reserved.
 *
 * Contributor(s): Blender Foundation, full recode and added functions
 *
 * ***** END GPL LICENSE BLOCK *****
 */

/** \file blender/editors/space_view3d/drawobject.c
 *  \ingroup spview3d
 */

#include "MEM_guardedalloc.h"

#include "DNA_camera_types.h"
#include "DNA_curve_types.h"
#include "DNA_constraint_types.h"  /* for drawing constraint */
#include "DNA_lamp_types.h"
#include "DNA_lattice_types.h"
#include "DNA_material_types.h"
#include "DNA_mesh_types.h"
#include "DNA_meta_types.h"
#include "DNA_rigidbody_types.h"
#include "DNA_scene_types.h"
#include "DNA_smoke_types.h"
#include "DNA_world_types.h"
#include "DNA_object_types.h"

#include "BLI_listbase.h"
#include "BLI_link_utils.h"
#include "BLI_string.h"
#include "BLI_math.h"
#include "BLI_memarena.h"

#include "BKE_anim.h"  /* for the where_on_path function */
#include "BKE_armature.h"
#include "BKE_camera.h"
#include "BKE_colortools.h"
#include "BKE_constraint.h"  /* for the get_constraint_target function */
#include "BKE_context.h"
#include "BKE_curve.h"
#include "BKE_DerivedMesh.h"
#include "BKE_deform.h"
#include "BKE_displist.h"
#include "BKE_font.h"
#include "BKE_global.h"
#include "BKE_image.h"
#include "BKE_key.h"
#include "BKE_layer.h"
#include "BKE_lattice.h"
#include "BKE_main.h"
#include "BKE_mesh.h"
#include "BKE_mesh_runtime.h"
#include "BKE_material.h"
#include "BKE_mball.h"
#include "BKE_modifier.h"
#include "BKE_movieclip.h"
#include "BKE_object.h"
#include "BKE_paint.h"
#include "BKE_particle.h"
#include "BKE_pointcache.h"
#include "BKE_scene.h"
#include "BKE_subsurf.h"
#include "BKE_unit.h"
#include "BKE_tracking.h"
#include "BKE_gpencil.h"

#include "BKE_editmesh.h"

#include "DEG_depsgraph.h"
#include "DEG_depsgraph_query.h"

#include "IMB_imbuf.h"
#include "IMB_imbuf_types.h"

#include "BIF_gl.h"
#include "BIF_glutil.h"

#include "GPU_draw.h"
#include "GPU_select.h"
#include "GPU_basic_shader.h"
#include "GPU_shader.h"
#include "GPU_immediate.h"
#include "GPU_immediate_util.h"
#include "GPU_batch.h"
#include "GPU_matrix.h"
#include "GPU_state.h"
#include "GPU_framebuffer.h"

#include "ED_mesh.h"
#include "ED_particle.h"
#include "ED_screen.h"
#include "ED_sculpt.h"
#include "ED_types.h"

#include "UI_resources.h"
#include "UI_interface_icons.h"

#include "WM_api.h"
#include "BLF_api.h"

#include "view3d_intern.h"  /* bad level include */

#include "../../draw/intern/draw_cache_impl.h"  /* bad level include (temporary) */

int view3d_effective_drawtype(const struct View3D *v3d)
{
	if (v3d->shading.type == OB_RENDER) {
		return v3d->shading.prev_type;
	}
	return v3d->shading.type;
}

static bool check_ob_drawface_dot(Scene *sce, View3D *vd, char dt)
{
	if ((sce->toolsettings->selectmode & SCE_SELECT_FACE) == 0)
		return false;

	if (G.f & G_BACKBUFSEL)
		return false;

	if ((vd->flag & V3D_ZBUF_SELECT) == 0)
		return true;

	/* if its drawing textures with zbuf sel, then don't draw dots */
	if (dt == OB_TEXTURE && vd->shading.type == OB_TEXTURE)
		return false;

	if ((vd->shading.type >= OB_SOLID) && (vd->flag2 & V3D_SOLID_TEX))
		return false;

	return true;
}

/* ----------------- OpenGL Circle Drawing - Tables for Optimized Drawing Speed ------------------ */
/* 32 values of sin function (still same result!) */
#define CIRCLE_RESOL 32

static const float sinval[CIRCLE_RESOL] = {
	0.00000000,
	0.20129852,
	0.39435585,
	0.57126821,
	0.72479278,
	0.84864425,
	0.93775213,
	0.98846832,
	0.99871650,
	0.96807711,
	0.89780453,
	0.79077573,
	0.65137248,
	0.48530196,
	0.29936312,
	0.10116832,
	-0.10116832,
	-0.29936312,
	-0.48530196,
	-0.65137248,
	-0.79077573,
	-0.89780453,
	-0.96807711,
	-0.99871650,
	-0.98846832,
	-0.93775213,
	-0.84864425,
	-0.72479278,
	-0.57126821,
	-0.39435585,
	-0.20129852,
	0.00000000
};

/* 32 values of cos function (still same result!) */
static const float cosval[CIRCLE_RESOL] = {
	1.00000000,
	0.97952994,
	0.91895781,
	0.82076344,
	0.68896691,
	0.52896401,
	0.34730525,
	0.15142777,
	-0.05064916,
	-0.25065253,
	-0.44039415,
	-0.61210598,
	-0.75875812,
	-0.87434661,
	-0.95413925,
	-0.99486932,
	-0.99486932,
	-0.95413925,
	-0.87434661,
	-0.75875812,
	-0.61210598,
	-0.44039415,
	-0.25065253,
	-0.05064916,
	0.15142777,
	0.34730525,
	0.52896401,
	0.68896691,
	0.82076344,
	0.91895781,
	0.97952994,
	1.00000000
};

static void circball_array_fill(float verts[CIRCLE_RESOL][3], const float cent[3], float rad, const float tmat[4][4])
{
	float vx[3], vy[3];
	float *viter = (float *)verts;

	mul_v3_v3fl(vx, tmat[0], rad);
	mul_v3_v3fl(vy, tmat[1], rad);

	for (unsigned int a = 0; a < CIRCLE_RESOL; a++, viter += 3) {
		viter[0] = cent[0] + sinval[a] * vx[0] + cosval[a] * vy[0];
		viter[1] = cent[1] + sinval[a] * vx[1] + cosval[a] * vy[1];
		viter[2] = cent[2] + sinval[a] * vx[2] + cosval[a] * vy[2];
	}
}

void imm_drawcircball(const float cent[3], float rad, const float tmat[4][4], unsigned pos)
{
	float verts[CIRCLE_RESOL][3];

	circball_array_fill(verts, cent, rad, tmat);

	immBegin(GPU_PRIM_LINE_LOOP, CIRCLE_RESOL);
	for (int i = 0; i < CIRCLE_RESOL; ++i) {
		immVertex3fv(pos, verts[i]);
	}
	immEnd();
}

#ifdef VIEW3D_CAMERA_BORDER_HACK
unsigned char view3d_camera_border_hack_col[3];
bool view3d_camera_border_hack_test = false;
#endif

/* ***************** BACKBUF SEL (BBS) ********* */

#ifdef USE_MESH_DM_SELECT
static void bbs_obmode_mesh_verts__mapFunc(void *userData, int index, const float co[3],
                                           const float UNUSED(no_f[3]), const short UNUSED(no_s[3]))
{
	drawMVertOffset_userData *data = userData;
	MVert *mv = &data->mvert[index];

	if (!(mv->flag & ME_HIDE)) {
		int selcol;
		GPU_select_index_get(data->offset + index, &selcol);
		immAttrib1u(data->col, selcol);
		immVertex3fv(data->pos, co);
	}
}

static void bbs_obmode_mesh_verts(Object *ob, DerivedMesh *dm, int offset)
{
	drawMVertOffset_userData data;
	Mesh *me = ob->data;
	MVert *mvert = me->mvert;
	data.mvert = mvert;
	data.offset = offset;

	const int imm_len = dm->getNumVerts(dm);

	if (imm_len == 0) return;

	GPUVertFormat *format = immVertexFormat();
	data.pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
	data.col = GPU_vertformat_attr_add(format, "color", GPU_COMP_U32, 1, GPU_FETCH_INT);

	immBindBuiltinProgram(GPU_SHADER_3D_FLAT_COLOR_U32);

	GPU_point_size(UI_GetThemeValuef(TH_VERTEX_SIZE));

	immBeginAtMost(GPU_PRIM_POINTS, imm_len);
	dm->foreachMappedVert(dm, bbs_obmode_mesh_verts__mapFunc, &data, DM_FOREACH_NOP);
	immEnd();

	immUnbindProgram();
}
#else
static void bbs_obmode_mesh_verts(Object *ob, DerivedMesh *UNUSED(dm), int offset)
{
	Mesh *me = ob->data;
	GPUBatch *batch = DRW_mesh_batch_cache_get_verts_with_select_id(me, offset);
	GPU_batch_program_set_builtin(batch, GPU_SHADER_3D_FLAT_COLOR_U32);
	GPU_batch_draw(batch);
}
#endif

#ifdef USE_MESH_DM_SELECT
static void bbs_mesh_verts__mapFunc(void *userData, int index, const float co[3],
                                    const float UNUSED(no_f[3]), const short UNUSED(no_s[3]))
{
	drawBMOffset_userData *data = userData;
	BMVert *eve = BM_vert_at_index(data->bm, index);

	if (!BM_elem_flag_test(eve, BM_ELEM_HIDDEN)) {
		int selcol;
		GPU_select_index_get(data->offset + index, &selcol);
		immAttrib1u(data->col, selcol);
		immVertex3fv(data->pos, co);
	}
}
static void bbs_mesh_verts(BMEditMesh *em, DerivedMesh *dm, int offset)
{
	drawBMOffset_userData data;
	data.bm = em->bm;
	data.offset = offset;
	GPUVertFormat *format = immVertexFormat();
	data.pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
	data.col = GPU_vertformat_attr_add(format, "color", GPU_COMP_U32, 1, GPU_FETCH_INT);

	immBindBuiltinProgram(GPU_SHADER_3D_FLAT_COLOR_U32);

	GPU_point_size(UI_GetThemeValuef(TH_VERTEX_SIZE));

	immBeginAtMost(GPU_PRIM_POINTS, em->bm->totvert);
	dm->foreachMappedVert(dm, bbs_mesh_verts__mapFunc, &data, DM_FOREACH_NOP);
	immEnd();

	immUnbindProgram();
}
#else
static void bbs_mesh_verts(BMEditMesh *em, DerivedMesh *UNUSED(dm), int offset)
{
	GPU_point_size(UI_GetThemeValuef(TH_VERTEX_SIZE));

	Mesh *me = em->ob->data;
	GPUBatch *batch = DRW_mesh_batch_cache_get_verts_with_select_id(me, offset);
	GPU_batch_program_set_builtin(batch, GPU_SHADER_3D_FLAT_COLOR_U32);
	GPU_batch_draw(batch);
}
#endif

#ifdef USE_MESH_DM_SELECT
static void bbs_mesh_wire__mapFunc(void *userData, int index, const float v0co[3], const float v1co[3])
{
	drawBMOffset_userData *data = userData;
	BMEdge *eed = BM_edge_at_index(data->bm, index);

	if (!BM_elem_flag_test(eed, BM_ELEM_HIDDEN)) {
		int selcol;
		GPU_select_index_get(data->offset + index, &selcol);
		immAttrib1u(data->col, selcol);
		immVertex3fv(data->pos, v0co);
		immVertex3fv(data->pos, v1co);
	}
}

static void bbs_mesh_wire(BMEditMesh *em, DerivedMesh *dm, int offset)
{
	drawBMOffset_userData data;
	data.bm = em->bm;
	data.offset = offset;

	GPUVertFormat *format = immVertexFormat();

	const int imm_len = dm->getNumEdges(dm) * 2;

	if (imm_len == 0) return;

	data.pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
	data.col = GPU_vertformat_attr_add(format, "color", GPU_COMP_U32, 1, GPU_FETCH_INT);

	immBindBuiltinProgram(GPU_SHADER_3D_FLAT_COLOR_U32);

	GPU_line_width(1.0f);

	immBeginAtMost(GPU_PRIM_LINES, imm_len);
	dm->foreachMappedEdge(dm, bbs_mesh_wire__mapFunc, &data);
	immEnd();

	immUnbindProgram();
}
#else
static void bbs_mesh_wire(BMEditMesh *em, DerivedMesh *UNUSED(dm), int offset)
{
	GPU_line_width(1.0f);

	Mesh *me = em->ob->data;
	GPUBatch *batch = DRW_mesh_batch_cache_get_edges_with_select_id(me, offset);
	GPU_batch_program_set_builtin(batch, GPU_SHADER_3D_FLAT_COLOR_U32);
	GPU_batch_draw(batch);
}
#endif

#ifdef USE_MESH_DM_SELECT
static void bbs_mesh_face(BMEditMesh *em, DerivedMesh *dm, const bool use_select)
{
	UNUSED_VARS(dm);

	drawBMOffset_userData data;
	data.bm = em->bm;

	const int tri_len = em->tottri;
	const int imm_len = tri_len * 3;
	const char hflag_skip = use_select ? BM_ELEM_HIDDEN : (BM_ELEM_HIDDEN | BM_ELEM_SELECT);

	if (imm_len == 0) return;

	GPUVertFormat *format = immVertexFormat();
	data.pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
	data.col = GPU_vertformat_attr_add(format, "color", GPU_COMP_U32, 1, GPU_FETCH_INT);

	immBindBuiltinProgram(GPU_SHADER_3D_FLAT_COLOR_U32);

	immBeginAtMost(GPU_PRIM_TRIS, imm_len);

	if (use_select == false) {
		int selcol;
		GPU_select_index_get(0, &selcol);
		immAttrib1u(data.col, selcol);
	}

	int index = 0;
	while (index < tri_len) {
		const BMFace *f = em->looptris[index][0]->f;
		const int ntris = f->len - 2;
		if (!BM_elem_flag_test(f, hflag_skip)) {
			if (use_select) {
				int selcol;
				GPU_select_index_get(BM_elem_index_get(f) + 1, &selcol);
				immAttrib1u(data.col, selcol);
			}
			for (int t = 0; t < ntris; t++) {
				immVertex3fv(data.pos, em->looptris[index][0]->v->co);
				immVertex3fv(data.pos, em->looptris[index][1]->v->co);
				immVertex3fv(data.pos, em->looptris[index][2]->v->co);
				index++;
			}
		}
		else {
			index += ntris;
		}
	}
	immEnd();

	immUnbindProgram();
}
#else
static void bbs_mesh_face(BMEditMesh *em, DerivedMesh *UNUSED(dm), const bool use_select)
{
	Mesh *me = em->ob->data;
	GPUBatch *batch;

	if (use_select) {
		batch = DRW_mesh_batch_cache_get_triangles_with_select_id(me, true, 1);
		GPU_batch_program_set_builtin(batch, GPU_SHADER_3D_FLAT_COLOR_U32);
		GPU_batch_draw(batch);
	}
	else {
		int selcol;
		GPU_select_index_get(0, &selcol);
		batch = DRW_mesh_batch_cache_get_triangles_with_select_mask(me, true);
		GPU_batch_program_set_builtin(batch, GPU_SHADER_3D_UNIFORM_COLOR_U32);
		GPU_batch_uniform_1ui(batch, "color", selcol);
		GPU_batch_draw(batch);
	}
}
#endif

#ifdef USE_MESH_DM_SELECT
static void bbs_mesh_solid__drawCenter(void *userData, int index, const float cent[3], const float UNUSED(no[3]))
{
	drawBMOffset_userData *data = (drawBMOffset_userData *)userData;
	BMFace *efa = BM_face_at_index(userData, index);

	if (!BM_elem_flag_test(efa, BM_ELEM_HIDDEN)) {
		int selcol;
		GPU_select_index_get(index + 1, &selcol);
		immAttrib1u(data->col, selcol);
		immVertex3fv(data->pos, cent);
	}
}

static void bbs_mesh_face_dot(BMEditMesh *em, DerivedMesh *dm)
{
	drawBMOffset_userData data; /* don't use offset */
	data.bm = em->bm;
	GPUVertFormat *format = immVertexFormat();
	data.pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
	data.col = GPU_vertformat_attr_add(format, "color", GPU_COMP_U32, 1, GPU_FETCH_INT);

	immBindBuiltinProgram(GPU_SHADER_3D_FLAT_COLOR_U32);

	GPU_point_size(UI_GetThemeValuef(TH_FACEDOT_SIZE));

	immBeginAtMost(GPU_PRIM_POINTS, em->bm->totface);
	dm->foreachMappedFaceCenter(dm, bbs_mesh_solid__drawCenter, &data, DM_FOREACH_NOP);
	immEnd();

	immUnbindProgram();
}
#else
static void bbs_mesh_face_dot(BMEditMesh *em, DerivedMesh *UNUSED(dm))
{
	Mesh *me = em->ob->data;
	GPUBatch *batch = DRW_mesh_batch_cache_get_facedots_with_select_id(me, 1);
	GPU_batch_program_set_builtin(batch, GPU_SHADER_3D_FLAT_COLOR_U32);
	GPU_batch_draw(batch);
}
#endif

/* two options, facecolors or black */
static void bbs_mesh_solid_EM(BMEditMesh *em, Scene *scene, View3D *v3d,
                              Object *ob, DerivedMesh *dm, bool use_faceselect)
{
	if (use_faceselect) {
		bbs_mesh_face(em, dm, true);

		if (check_ob_drawface_dot(scene, v3d, ob->dt)) {
			bbs_mesh_face_dot(em, dm);
		}
	}
	else {
		bbs_mesh_face(em, dm, false);
	}
}

#ifdef USE_MESH_DM_SELECT
/* must have called GPU_framebuffer_index_set beforehand */
static DMDrawOption bbs_mesh_solid_hide2__setDrawOpts(void *userData, int index)
{
	Mesh *me = userData;

	if (!(me->mpoly[index].flag & ME_HIDE)) {
		return DM_DRAW_OPTION_NORMAL;
	}
	else {
		return DM_DRAW_OPTION_SKIP;
	}
}

static void bbs_mesh_solid_verts(Depsgraph *depsgraph, Scene *scene, Object *ob)
{
	Mesh *me = ob->data;
	DerivedMesh *dm = mesh_get_derived_final(depsgraph, scene, ob, scene->customdata_mask);

	DM_update_materials(dm, ob);

	/* Only draw faces to mask out verts, we don't want their selection ID's. */
	const int G_f_orig = G.f;
	G.f &= ~G_BACKBUFSEL;

	dm->drawMappedFaces(dm, bbs_mesh_solid_hide2__setDrawOpts, NULL, NULL, me, DM_DRAW_SKIP_HIDDEN);

	G.f |= (G_f_orig & G_BACKBUFSEL);

	bbs_obmode_mesh_verts(ob, dm, 1);
	bm_vertoffs = me->totvert + 1;
	dm->release(dm);
}
#else
static void bbs_mesh_solid_verts(Depsgraph *UNUSED(depsgraph), Scene *UNUSED(scene), Object *ob)
{
	Mesh *me = ob->data;

	/* Only draw faces to mask out verts, we don't want their selection ID's. */
	const int G_f_orig = G.f;
	G.f &= ~G_BACKBUFSEL;

	{
		int selcol;
		GPUBatch *batch;
		GPU_select_index_get(0, &selcol);
		batch = DRW_mesh_batch_cache_get_triangles_with_select_mask(me, true);
		GPU_batch_program_set_builtin(batch, GPU_SHADER_3D_UNIFORM_COLOR_U32);
		GPU_batch_uniform_1ui(batch, "color", selcol);
		GPU_batch_draw(batch);
	}

	G.f |= (G_f_orig & G_BACKBUFSEL);

	bbs_obmode_mesh_verts(ob, NULL, 1);
	bm_vertoffs = me->totvert + 1;
}
#endif

static void bbs_mesh_solid_faces(Scene *UNUSED(scene), Object *ob)
{
	Mesh *me = ob->data;
	GPUBatch *batch;
	if ((me->editflag & ME_EDIT_PAINT_FACE_SEL)) {
		batch = DRW_mesh_batch_cache_get_triangles_with_select_id(me, true, 1);
	}
	else {
		batch = DRW_mesh_batch_cache_get_triangles_with_select_id(me, false, 1);
	}
	GPU_batch_program_set_builtin(batch, GPU_SHADER_3D_FLAT_COLOR_U32);
	GPU_batch_draw(batch);
}

void draw_object_backbufsel(
        Depsgraph *depsgraph, Scene *scene, View3D *v3d, RegionView3D *rv3d, Object *ob,
        short select_mode)
{
	ToolSettings *ts = scene->toolsettings;
	if (select_mode == -1) {
		select_mode = ts->selectmode;
	}

	GPU_matrix_mul(ob->obmat);

	glClearDepth(1.0); GPU_clear(GPU_DEPTH_BIT);
	GPU_depth_test(true);

	switch (ob->type) {
		case OB_MESH:
			if (ob->mode & OB_MODE_EDIT) {
				Mesh *me = ob->data;
				BMEditMesh *em = me->edit_btmesh;

				DerivedMesh *dm = editbmesh_get_derived_cage(depsgraph, scene, ob, em, CD_MASK_BAREMESH);

				BM_mesh_elem_table_ensure(em->bm, BM_VERT | BM_EDGE | BM_FACE);

				DM_update_materials(dm, ob);

				bbs_mesh_solid_EM(em, scene, v3d, ob, dm, (select_mode & SCE_SELECT_FACE) != 0);
				if (select_mode & SCE_SELECT_FACE)
					bm_solidoffs = 1 + em->bm->totface;
				else {
					bm_solidoffs = 1;
				}

				ED_view3d_polygon_offset(rv3d, 1.0);

				/* we draw edges if edge select mode */
				if (select_mode & SCE_SELECT_EDGE) {
					bbs_mesh_wire(em, dm, bm_solidoffs);
					bm_wireoffs = bm_solidoffs + em->bm->totedge;
				}
				else {
					/* `bm_vertoffs` is calculated from `bm_wireoffs`. (otherwise see T53512) */
					bm_wireoffs = bm_solidoffs;
				}

				/* we draw verts if vert select mode. */
				if (select_mode & SCE_SELECT_VERTEX) {
					bbs_mesh_verts(em, dm, bm_wireoffs);
					bm_vertoffs = bm_wireoffs + em->bm->totvert;
				}
				else {
					bm_vertoffs = bm_wireoffs;
				}

				ED_view3d_polygon_offset(rv3d, 0.0);

				dm->release(dm);
			}
			else {
				Mesh *me = ob->data;
				if ((me->editflag & ME_EDIT_PAINT_VERT_SEL) &&
				    /* currently vertex select supports weight paint and vertex paint*/
				    ((ob->mode & OB_MODE_WEIGHT_PAINT) || (ob->mode & OB_MODE_VERTEX_PAINT)))
				{
					bbs_mesh_solid_verts(depsgraph, scene, ob);
				}
				else {
					bbs_mesh_solid_faces(scene, ob);
				}
			}
			break;
		case OB_CURVE:
		case OB_SURF:
			break;
	}

	GPU_matrix_set(rv3d->viewmat);
}


void ED_draw_object_facemap(
        Depsgraph *depsgraph, Scene *scene, Object *ob, const float col[4], const int facemap)
{
	DerivedMesh *dm = NULL;

	/* happens on undo */
	if (ob->type != OB_MESH || !ob->data)
		return;

	/* Temporary, happens on undo, would resolve but will eventually move away from DM. */
	if (ob->derivedFinal == NULL) {
		return;
	}

	dm = mesh_get_derived_final(depsgraph, scene, ob, CD_MASK_BAREMESH);
	if (!dm || !CustomData_has_layer(&dm->polyData, CD_FACEMAP))
		return;


	glFrontFace((ob->transflag & OB_NEG_SCALE) ? GL_CW : GL_CCW);

#if 0
	DM_update_materials(dm, ob);

	/* add polygon offset so we draw above the original surface */
	glPolygonOffset(1.0, 1.0);

	GPU_facemap_setup(dm);

	glColor4fv(col);

	gpuPushAttrib(GL_ENABLE_BIT);
	GPU_blend(true);
	glDisable(GL_LIGHTING);

	/* always draw using backface culling */
	glEnable(GL_CULL_FACE);
	glCullFace(GL_BACK);

	if (dm->drawObject->facemapindices) {
		glDrawElements(GL_TRIANGLES, dm->drawObject->facemap_count[facemap] * 3, GL_UNSIGNED_INT,
		               (int *)NULL + dm->drawObject->facemap_start[facemap] * 3);
	}
	gpuPopAttrib();

	GPU_buffers_unbind();

	glPolygonOffset(0.0, 0.0);

#else

	/* Just to create the data to pass to immediate mode, grr! */
	Mesh *me = ob->data;
	const int *facemap_data = CustomData_get_layer(&me->pdata, CD_FACEMAP);
	if (facemap_data) {
		GPUVertFormat *format = immVertexFormat();
		uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);

		immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
		immUniformColor4fv(col);

		/* XXX, alpha isn't working yet, not sure why. */
		GPU_blend_set_func_separate(GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA, GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA);
		GPU_blend(true);

		MVert *mvert;

		MPoly *mpoly;
		int    mpoly_len;

		MLoop *mloop;
		int    mloop_len;

		if (dm && CustomData_has_layer(&dm->polyData, CD_FACEMAP)) {
			mvert = dm->getVertArray(dm);
			mpoly = dm->getPolyArray(dm);
			mloop = dm->getLoopArray(dm);

			mpoly_len = dm->getNumPolys(dm);
			mloop_len = dm->getNumLoops(dm);

			facemap_data = CustomData_get_layer(&dm->polyData, CD_FACEMAP);
		}
		else {
			mvert = me->mvert;
			mpoly = me->mpoly;
			mloop = me->mloop;

			mpoly_len = me->totpoly;
			mloop_len = me->totloop;

			facemap_data = CustomData_get_layer(&me->pdata, CD_FACEMAP);
		}

		/* use gawain immediate mode fore now */
		const int looptris_len = poly_to_tri_count(mpoly_len, mloop_len);
		immBeginAtMost(GPU_PRIM_TRIS, looptris_len * 3);

		MPoly *mp;
		int i;
		for (mp = mpoly, i = 0; i < mpoly_len; i++, mp++) {
			if (facemap_data[i] == facemap) {
				/* Weak, fan-fill, use until we have derived-mesh replaced. */
				const MLoop *ml_start = &mloop[mp->loopstart];
				const MLoop *ml_a = ml_start + 1;
				const MLoop *ml_b = ml_start + 2;
				for (int j = 2; j < mp->totloop; j++) {
					immVertex3fv(pos, mvert[ml_start->v].co);
					immVertex3fv(pos, mvert[ml_a->v].co);
					immVertex3fv(pos, mvert[ml_b->v].co);

					ml_a++;
					ml_b++;
				}
			}
		}
		immEnd();

		immUnbindProgram();

		GPU_blend(false);
	}
#endif

	dm->release(dm);
}