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
|
/*
* 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/modes/edit_mesh_mode.c
* \ingroup draw
*/
#include "DRW_engine.h"
#include "DRW_render.h"
#include "GPU_shader.h"
#include "DNA_mesh_types.h"
#include "DNA_view3d_types.h"
#include "draw_common.h"
#include "draw_cache_impl.h"
#include "draw_mode_engines.h"
#include "edit_mesh_mode_intern.h" /* own include */
#include "BKE_editmesh.h"
#include "BKE_object.h"
#include "BLI_dynstr.h"
#include "BLI_string_utils.h"
extern char datatoc_common_world_clip_lib_glsl[];
extern char datatoc_paint_weight_vert_glsl[];
extern char datatoc_paint_weight_frag_glsl[];
extern char datatoc_edit_mesh_overlay_common_lib_glsl[];
extern char datatoc_edit_mesh_overlay_frag_glsl[];
extern char datatoc_edit_mesh_overlay_vert_glsl[];
extern char datatoc_edit_mesh_overlay_geom_tri_glsl[];
extern char datatoc_edit_mesh_overlay_geom_edge_glsl[];
extern char datatoc_edit_mesh_overlay_points_vert_glsl[];
extern char datatoc_edit_mesh_overlay_facedot_frag_glsl[];
extern char datatoc_edit_mesh_overlay_facedot_vert_glsl[];
extern char datatoc_edit_mesh_overlay_mix_frag_glsl[];
extern char datatoc_edit_mesh_overlay_facefill_vert_glsl[];
extern char datatoc_edit_mesh_overlay_facefill_frag_glsl[];
extern char datatoc_edit_normals_vert_glsl[];
extern char datatoc_edit_normals_geom_glsl[];
extern char datatoc_common_globals_lib_glsl[];
extern char datatoc_gpu_shader_uniform_color_frag_glsl[];
extern char datatoc_gpu_shader_flat_color_frag_glsl[];
extern char datatoc_gpu_shader_point_varying_color_frag_glsl[];
extern char datatoc_gpu_shader_depth_only_frag_glsl[];
/* *********** LISTS *********** */
typedef struct EDIT_MESH_PassList {
struct DRWPass *weight_faces;
struct DRWPass *depth_hidden_wire;
struct DRWPass *ghost_clear_depth;
struct DRWPass *edit_face_overlay;
struct DRWPass *edit_face_occluded;
struct DRWPass *mix_occlude;
struct DRWPass *facefill_occlude;
struct DRWPass *normals;
} EDIT_MESH_PassList;
typedef struct EDIT_MESH_FramebufferList {
struct GPUFrameBuffer *occlude_wire_fb;
struct GPUFrameBuffer *ghost_wire_fb;
} EDIT_MESH_FramebufferList;
typedef struct EDIT_MESH_StorageList {
struct EDIT_MESH_PrivateData *g_data;
} EDIT_MESH_StorageList;
typedef struct EDIT_MESH_Data {
void *engine_type;
EDIT_MESH_FramebufferList *fbl;
DRWViewportEmptyList *txl;
EDIT_MESH_PassList *psl;
EDIT_MESH_StorageList *stl;
} EDIT_MESH_Data;
#define MAX_SHADERS 16
/** Can only contain shaders (freed as array). */
typedef struct EDIT_MESH_Shaders {
/* weight */
GPUShader *weight_face;
/* Geometry */
GPUShader *overlay_tri_cache[MAX_SHADERS];
GPUShader *overlay_loose_edge_cache[MAX_SHADERS];
GPUShader *overlay_vert;
GPUShader *overlay_lvert;
GPUShader *overlay_facedot;
GPUShader *overlay_mix;
GPUShader *overlay_facefill;
GPUShader *normals_face;
GPUShader *normals_loop;
GPUShader *normals;
GPUShader *depth;
GPUShader *ghost_clear_depth;
} EDIT_MESH_Shaders;
/* *********** STATIC *********** */
static struct {
/* 0: normal, 1: clipped. */
EDIT_MESH_Shaders sh_data[2];
/* temp buffer texture */
struct GPUTexture *occlude_wire_depth_tx;
struct GPUTexture *occlude_wire_color_tx;
} e_data = {NULL}; /* Engine data */
typedef struct EDIT_MESH_PrivateData {
/* weight */
DRWShadingGroup *fweights_shgrp;
DRWShadingGroup *depth_shgrp_hidden_wire;
DRWShadingGroup *fnormals_shgrp;
DRWShadingGroup *vnormals_shgrp;
DRWShadingGroup *lnormals_shgrp;
DRWShadingGroup *face_shgrp;
DRWShadingGroup *face_cage_shgrp;
DRWShadingGroup *verts_shgrp;
DRWShadingGroup *ledges_shgrp;
DRWShadingGroup *lverts_shgrp;
DRWShadingGroup *facedot_shgrp;
DRWShadingGroup *facefill_occluded_shgrp;
int data_mask[4];
int ghost_ob;
int edit_ob;
bool do_zbufclip;
bool do_faces;
bool do_edges;
float edge_width_scale;
} EDIT_MESH_PrivateData; /* Transient data */
/* *********** FUNCTIONS *********** */
static int EDIT_MESH_sh_data_index_from_rv3d(const RegionView3D *rv3d)
{
if (rv3d->rflag & RV3D_CLIPPING) {
return 1;
}
return 0;
}
static int EDIT_MESH_sh_index(ToolSettings *tsettings, RegionView3D *rv3d, bool supports_fast_mode)
{
int result = tsettings->selectmode << 1;
if (supports_fast_mode) {
SET_FLAG_FROM_TEST(result, (rv3d->rflag & RV3D_NAVIGATING), 1 << 0);
}
return result;
}
static char *EDIT_MESH_sh_defines(ToolSettings *tsettings, RegionView3D *rv3d, bool anti_alias, bool looseedge)
{
const int selectmode = tsettings->selectmode;
const int fast_mode = rv3d->rflag & RV3D_NAVIGATING;
char *str = NULL;
DynStr *ds = BLI_dynstr_new();
if (selectmode & SCE_SELECT_VERTEX) {
BLI_dynstr_append(ds, "#define VERTEX_SELECTION\n");
}
if (selectmode & SCE_SELECT_EDGE) {
BLI_dynstr_append(ds, "#define EDGE_SELECTION\n");
}
if (selectmode & SCE_SELECT_FACE) {
BLI_dynstr_append(ds, "#define FACE_SELECTION\n");
}
if (!fast_mode || looseedge) {
BLI_dynstr_append(ds, "#define EDGE_FIX\n");
}
if (anti_alias) {
BLI_dynstr_append(ds, "#define ANTI_ALIASING\n");
}
if (!looseedge) {
BLI_dynstr_append(ds, "#define VERTEX_FACING\n");
}
str = BLI_dynstr_get_cstring(ds);
BLI_dynstr_free(ds);
return str;
}
static GPUShader *EDIT_MESH_ensure_shader(
EDIT_MESH_Shaders *sh_data,
ToolSettings *tsettings, RegionView3D *rv3d, bool supports_fast_mode, bool looseedge)
{
const int index = EDIT_MESH_sh_index(tsettings, rv3d, supports_fast_mode);
const bool fast_mode = (rv3d->rflag & RV3D_NAVIGATING) != 0;
const bool is_clip = (rv3d->rflag & RV3D_CLIPPING) != 0;
const char *world_clip_lib_or_empty = is_clip ? datatoc_common_world_clip_lib_glsl : "";
const char *world_clip_def_or_empty = is_clip ? "#define USE_WORLD_CLIP_PLANES\n" : "";
if (looseedge) {
if (!sh_data->overlay_loose_edge_cache[index]) {
char *lib = BLI_string_joinN(world_clip_lib_or_empty, datatoc_common_globals_lib_glsl, datatoc_edit_mesh_overlay_common_lib_glsl);
char *defines = EDIT_MESH_sh_defines(tsettings, rv3d, true, true);
sh_data->overlay_loose_edge_cache[index] = DRW_shader_create_from_arrays({
.vert = (const char *[]){lib, datatoc_edit_mesh_overlay_vert_glsl, NULL},
.geom = (const char *[]){lib, datatoc_edit_mesh_overlay_geom_edge_glsl, NULL},
.frag = (const char *[]){lib, datatoc_edit_mesh_overlay_frag_glsl, NULL},
.defs = (const char *[]){world_clip_def_or_empty, defines, NULL},
});
MEM_freeN(lib);
MEM_freeN(defines);
}
return sh_data->overlay_loose_edge_cache[index];
}
else {
if (!sh_data->overlay_tri_cache[index]) {
char *lib = BLI_string_joinN(world_clip_lib_or_empty, datatoc_common_globals_lib_glsl, datatoc_edit_mesh_overlay_common_lib_glsl);
char *defines = EDIT_MESH_sh_defines(tsettings, rv3d, true, false);
sh_data->overlay_tri_cache[index] = DRW_shader_create_from_arrays({
.vert = (const char *[]){lib, datatoc_edit_mesh_overlay_vert_glsl, NULL},
.geom = fast_mode ? NULL : (const char *[]){lib, datatoc_edit_mesh_overlay_geom_tri_glsl, NULL},
.frag = (const char *[]){lib, datatoc_edit_mesh_overlay_frag_glsl, NULL},
.defs = (const char *[]){world_clip_def_or_empty, defines, NULL},
});
MEM_freeN(lib);
MEM_freeN(defines);
}
return sh_data->overlay_tri_cache[index];
}
}
static void EDIT_MESH_engine_init(void *vedata)
{
EDIT_MESH_FramebufferList *fbl = ((EDIT_MESH_Data *)vedata)->fbl;
const DRWContextState *draw_ctx = DRW_context_state_get();
EDIT_MESH_Shaders *sh_data = &e_data.sh_data[EDIT_MESH_sh_data_index_from_rv3d(draw_ctx->rv3d)];
const bool is_clip = (draw_ctx->rv3d->rflag & RV3D_CLIPPING) != 0;
const float *viewport_size = DRW_viewport_size_get();
const int size[2] = {(int)viewport_size[0], (int)viewport_size[1]};
e_data.occlude_wire_depth_tx = DRW_texture_pool_query_2D(size[0], size[1], GPU_DEPTH_COMPONENT24,
&draw_engine_edit_mesh_type);
e_data.occlude_wire_color_tx = DRW_texture_pool_query_2D(size[0], size[1], GPU_RGBA8,
&draw_engine_edit_mesh_type);
GPU_framebuffer_ensure_config(&fbl->occlude_wire_fb, {
GPU_ATTACHMENT_TEXTURE(e_data.occlude_wire_depth_tx),
GPU_ATTACHMENT_TEXTURE(e_data.occlude_wire_color_tx)
});
if (is_clip) {
DRW_state_clip_planes_set_from_rv3d(draw_ctx->rv3d);
}
const char *world_clip_lib_or_empty = is_clip ? datatoc_common_world_clip_lib_glsl : "";
const char *world_clip_def_or_empty = is_clip ? "#define USE_WORLD_CLIP_PLANES\n" : "";
if (!sh_data->weight_face) {
sh_data->weight_face = DRW_shader_create_from_arrays({
.vert = (const char *[]){world_clip_lib_or_empty, datatoc_common_globals_lib_glsl, datatoc_paint_weight_vert_glsl, NULL},
.frag = (const char *[]){datatoc_common_globals_lib_glsl, datatoc_paint_weight_frag_glsl, NULL},
.defs = (const char *[]){world_clip_def_or_empty, NULL}});
}
if (!sh_data->overlay_vert) {
char *lib = BLI_string_joinN(world_clip_lib_or_empty, datatoc_common_globals_lib_glsl, datatoc_edit_mesh_overlay_common_lib_glsl);
sh_data->overlay_vert = DRW_shader_create_from_arrays({
.vert = (const char *[]){lib, datatoc_edit_mesh_overlay_points_vert_glsl, NULL},
.frag = (const char *[]){datatoc_gpu_shader_point_varying_color_frag_glsl, NULL},
.defs = (const char *[]){world_clip_def_or_empty, "#define VERTEX_FACING\n", NULL}});
sh_data->overlay_lvert = DRW_shader_create_from_arrays({
.vert = (const char *[]){lib, datatoc_edit_mesh_overlay_points_vert_glsl, NULL},
.frag = (const char *[]){datatoc_gpu_shader_point_varying_color_frag_glsl, NULL},
.defs = (const char *[]){world_clip_def_or_empty, NULL}});
MEM_freeN(lib);
}
if (!sh_data->overlay_facedot) {
sh_data->overlay_facedot = DRW_shader_create_from_arrays({
.vert = (const char *[]){world_clip_lib_or_empty, datatoc_common_globals_lib_glsl, datatoc_edit_mesh_overlay_facedot_vert_glsl, NULL},
.frag = (const char *[]){datatoc_common_globals_lib_glsl, datatoc_edit_mesh_overlay_facedot_frag_glsl, NULL},
.defs = (const char *[]){world_clip_def_or_empty, "#define VERTEX_FACING\n", NULL}});
}
if (!sh_data->overlay_mix) {
sh_data->overlay_mix = DRW_shader_create_fullscreen(datatoc_edit_mesh_overlay_mix_frag_glsl, NULL);
}
if (!sh_data->overlay_facefill) {
sh_data->overlay_facefill = DRW_shader_create_from_arrays({
.vert = (const char *[]){world_clip_lib_or_empty, datatoc_common_globals_lib_glsl, datatoc_edit_mesh_overlay_facefill_vert_glsl, NULL},
.frag = (const char *[]){datatoc_common_globals_lib_glsl, datatoc_edit_mesh_overlay_facefill_frag_glsl, NULL},
.defs = (const char *[]){world_clip_def_or_empty, NULL}});
}
if (!sh_data->normals_face) {
sh_data->normals_face = DRW_shader_create_from_arrays({
.vert = (const char *[]){world_clip_lib_or_empty, datatoc_edit_normals_vert_glsl, NULL},
.geom = (const char *[]){world_clip_lib_or_empty, datatoc_edit_normals_geom_glsl, NULL},
.frag = (const char *[]){datatoc_gpu_shader_uniform_color_frag_glsl, NULL},
.defs = (const char *[]){world_clip_def_or_empty, "#define FACE_NORMALS\n", NULL}});
}
if (!sh_data->normals_loop) {
sh_data->normals_loop = DRW_shader_create_from_arrays({
.vert = (const char *[]){world_clip_lib_or_empty, datatoc_edit_normals_vert_glsl, NULL},
.geom = (const char *[]){world_clip_lib_or_empty, datatoc_edit_normals_geom_glsl, NULL},
.frag = (const char *[]){datatoc_gpu_shader_uniform_color_frag_glsl, NULL},
.defs = (const char *[]){world_clip_def_or_empty, "#define LOOP_NORMALS\n", NULL}});
}
if (!sh_data->normals) {
sh_data->normals = DRW_shader_create_from_arrays({
.vert = (const char *[]){world_clip_lib_or_empty, datatoc_edit_normals_vert_glsl, NULL},
.geom = (const char *[]){world_clip_lib_or_empty, datatoc_edit_normals_geom_glsl, NULL},
.frag = (const char *[]){datatoc_gpu_shader_uniform_color_frag_glsl, NULL},
.defs = (const char *[]){world_clip_def_or_empty, NULL}});
}
if (!sh_data->depth) {
sh_data->depth = DRW_shader_create_3D_depth_only();
}
if (!sh_data->ghost_clear_depth) {
sh_data->ghost_clear_depth = DRW_shader_create_fullscreen(datatoc_gpu_shader_depth_only_frag_glsl, NULL);
}
}
static DRWPass *edit_mesh_create_overlay_pass(
float *face_alpha, float *edge_width_scale, int *data_mask, bool do_edges, bool xray,
DRWState statemod,
DRWShadingGroup **r_face_shgrp, DRWShadingGroup **r_face_cage_shgrp,
DRWShadingGroup **r_verts_shgrp, DRWShadingGroup **r_ledges_shgrp,
DRWShadingGroup **r_lverts_shgrp, DRWShadingGroup **r_facedot_shgrp)
{
GPUShader *tri_sh, *ledge_sh;
const DRWContextState *draw_ctx = DRW_context_state_get();
RegionView3D *rv3d = draw_ctx->rv3d;
Scene *scene = draw_ctx->scene;
ToolSettings *tsettings = scene->toolsettings;
const int fast_mode = rv3d->rflag & RV3D_NAVIGATING;
EDIT_MESH_Shaders *sh_data = &e_data.sh_data[EDIT_MESH_sh_data_index_from_rv3d(draw_ctx->rv3d)];
ledge_sh = EDIT_MESH_ensure_shader(sh_data, tsettings, rv3d, false, true);
tri_sh = EDIT_MESH_ensure_shader(sh_data, tsettings, rv3d, true, false);
DRWPass *pass = DRW_pass_create(
"Edit Mesh Face Overlay Pass",
DRW_STATE_WRITE_COLOR | DRW_STATE_POINT | statemod);
if ((tsettings->selectmode & SCE_SELECT_VERTEX) != 0) {
*r_lverts_shgrp = DRW_shgroup_create(sh_data->overlay_lvert, pass);
DRW_shgroup_uniform_block(*r_lverts_shgrp, "globalsBlock", G_draw.block_ubo);
DRW_shgroup_uniform_vec2(*r_lverts_shgrp, "viewportSize", DRW_viewport_size_get(), 1);
DRW_shgroup_uniform_float(*r_lverts_shgrp, "edgeScale", edge_width_scale, 1);
DRW_shgroup_state_enable(*r_lverts_shgrp, DRW_STATE_WRITE_DEPTH);
DRW_shgroup_state_disable(*r_lverts_shgrp, DRW_STATE_BLEND);
if (rv3d->rflag & RV3D_CLIPPING) {
DRW_shgroup_world_clip_planes_from_rv3d(*r_lverts_shgrp, rv3d);
}
*r_verts_shgrp = DRW_shgroup_create(sh_data->overlay_vert, pass);
DRW_shgroup_uniform_block(*r_verts_shgrp, "globalsBlock", G_draw.block_ubo);
DRW_shgroup_uniform_vec2(*r_verts_shgrp, "viewportSize", DRW_viewport_size_get(), 1);
DRW_shgroup_uniform_float(*r_verts_shgrp, "edgeScale", edge_width_scale, 1);
DRW_shgroup_state_enable(*r_verts_shgrp, DRW_STATE_WRITE_DEPTH);
DRW_shgroup_state_disable(*r_verts_shgrp, DRW_STATE_BLEND);
if (rv3d->rflag & RV3D_CLIPPING) {
DRW_shgroup_world_clip_planes_from_rv3d(*r_verts_shgrp, rv3d);
}
}
if ((tsettings->selectmode & SCE_SELECT_FACE) != 0) {
*r_facedot_shgrp = DRW_shgroup_create(sh_data->overlay_facedot, pass);
DRW_shgroup_uniform_block(*r_facedot_shgrp, "globalsBlock", G_draw.block_ubo);
DRW_shgroup_uniform_float(*r_facedot_shgrp, "edgeScale", edge_width_scale, 1);
DRW_shgroup_state_enable(*r_facedot_shgrp, DRW_STATE_WRITE_DEPTH);
if (rv3d->rflag & RV3D_CLIPPING) {
DRW_shgroup_world_clip_planes_from_rv3d(*r_facedot_shgrp, rv3d);
}
}
*r_face_shgrp = DRW_shgroup_create(tri_sh, pass);
DRW_shgroup_uniform_block(*r_face_shgrp, "globalsBlock", G_draw.block_ubo);
DRW_shgroup_uniform_vec2(*r_face_shgrp, "viewportSize", DRW_viewport_size_get(), 1);
DRW_shgroup_uniform_float(*r_face_shgrp, "faceAlphaMod", face_alpha, 1);
DRW_shgroup_uniform_float(*r_face_shgrp, "edgeScale", edge_width_scale, 1);
DRW_shgroup_uniform_ivec4(*r_face_shgrp, "dataMask", data_mask, 1);
DRW_shgroup_uniform_bool_copy(*r_face_shgrp, "doEdges", do_edges);
if (!fast_mode) {
DRW_shgroup_uniform_bool_copy(*r_face_shgrp, "isXray", xray);
}
else {
/* To be able to use triple load. */
DRW_shgroup_state_enable(*r_face_shgrp, DRW_STATE_FIRST_VERTEX_CONVENTION);
}
if (rv3d->rflag & RV3D_CLIPPING) {
DRW_shgroup_world_clip_planes_from_rv3d(*r_face_shgrp, rv3d);
}
/* Cage geom needs to be offseted to avoid Z-fighting. */
*r_face_cage_shgrp = DRW_shgroup_create_sub(*r_face_shgrp);
DRW_shgroup_state_enable(*r_face_cage_shgrp, DRW_STATE_OFFSET_NEGATIVE);
*r_ledges_shgrp = DRW_shgroup_create(ledge_sh, pass);
DRW_shgroup_uniform_block(*r_ledges_shgrp, "globalsBlock", G_draw.block_ubo);
DRW_shgroup_uniform_vec2(*r_ledges_shgrp, "viewportSize", DRW_viewport_size_get(), 1);
DRW_shgroup_uniform_float(*r_ledges_shgrp, "edgeScale", edge_width_scale, 1);
DRW_shgroup_uniform_ivec4(*r_ledges_shgrp, "dataMask", data_mask, 1);
DRW_shgroup_uniform_bool_copy(*r_ledges_shgrp, "doEdges", do_edges);
if (rv3d->rflag & RV3D_CLIPPING) {
DRW_shgroup_world_clip_planes_from_rv3d(*r_ledges_shgrp, rv3d);
}
return pass;
}
static float backwire_opacity;
static float face_mod;
static float size_normal;
static void EDIT_MESH_cache_init(void *vedata)
{
EDIT_MESH_PassList *psl = ((EDIT_MESH_Data *)vedata)->psl;
EDIT_MESH_StorageList *stl = ((EDIT_MESH_Data *)vedata)->stl;
DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
const DRWContextState *draw_ctx = DRW_context_state_get();
View3D *v3d = draw_ctx->v3d;
RegionView3D *rv3d = draw_ctx->rv3d;
Scene *scene = draw_ctx->scene;
ToolSettings *tsettings = scene->toolsettings;
EDIT_MESH_Shaders *sh_data = &e_data.sh_data[EDIT_MESH_sh_data_index_from_rv3d(draw_ctx->rv3d)];
static float zero = 0.0f;
if (!stl->g_data) {
/* Alloc transient pointers */
stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
}
stl->g_data->ghost_ob = 0;
stl->g_data->edit_ob = 0;
stl->g_data->do_faces = true;
stl->g_data->do_edges = true;
stl->g_data->do_zbufclip = ((v3d)->shading.flag & XRAY_FLAG(v3d)) != 0;
/* Applies on top of the theme edge width, so edge-mode can have thick edges. */
stl->g_data->edge_width_scale = (tsettings->selectmode & (SCE_SELECT_EDGE)) ? 1.75f : 1.0f;
stl->g_data->data_mask[0] = 0xFF; /* Face Flag */
stl->g_data->data_mask[1] = 0xFF; /* Edge Flag */
stl->g_data->data_mask[2] = 0xFF; /* Crease */
stl->g_data->data_mask[3] = 0xFF; /* BWeight */
if (draw_ctx->object_edit->type == OB_MESH) {
if (BKE_object_is_in_editmode(draw_ctx->object_edit)) {
if ((v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_FREESTYLE_FACE) == 0) {
stl->g_data->data_mask[0] &= ~VFLAG_FACE_FREESTYLE;
}
if ((v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_FACES) == 0) {
stl->g_data->data_mask[0] &= ~(VFLAG_FACE_SELECTED & VFLAG_FACE_FREESTYLE);
stl->g_data->do_faces = false;
}
if ((v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_SEAMS) == 0) {
stl->g_data->data_mask[1] &= ~VFLAG_EDGE_SEAM;
}
if ((v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_SHARP) == 0) {
stl->g_data->data_mask[1] &= ~VFLAG_EDGE_SHARP;
}
if ((v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_FREESTYLE_EDGE) == 0) {
stl->g_data->data_mask[1] &= ~VFLAG_EDGE_FREESTYLE;
}
if ((v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_EDGES) == 0) {
if ((tsettings->selectmode & SCE_SELECT_EDGE) == 0) {
stl->g_data->data_mask[1] &= ~(VFLAG_EDGE_ACTIVE & VFLAG_EDGE_SELECTED);
stl->g_data->do_edges = false;
}
}
if ((v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_CREASES) == 0) {
stl->g_data->data_mask[2] = 0x0;
}
if ((v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_BWEIGHTS) == 0) {
stl->g_data->data_mask[3] = 0x0;
}
}
}
{
psl->weight_faces = DRW_pass_create(
"Weight Pass",
DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL);
stl->g_data->fweights_shgrp = DRW_shgroup_create(sh_data->weight_face, psl->weight_faces);
static float alpha = 1.0f;
DRW_shgroup_uniform_float(stl->g_data->fweights_shgrp, "opacity", &alpha, 1);
DRW_shgroup_uniform_texture(stl->g_data->fweights_shgrp, "colorramp", G_draw.weight_ramp);
DRW_shgroup_uniform_block(stl->g_data->fweights_shgrp, "globalsBlock", G_draw.block_ubo);
if (rv3d->rflag & RV3D_CLIPPING) {
DRW_shgroup_world_clip_planes_from_rv3d(stl->g_data->fweights_shgrp, rv3d);
}
}
{
/* Complementary Depth Pass */
psl->depth_hidden_wire = DRW_pass_create(
"Depth Pass Hidden Wire",
DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_CULL_BACK);
stl->g_data->depth_shgrp_hidden_wire = DRW_shgroup_create(sh_data->depth, psl->depth_hidden_wire);
}
{
/* Depth clearing for ghosting. */
psl->ghost_clear_depth = DRW_pass_create(
"Ghost Depth Clear",
DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_ALWAYS | DRW_STATE_STENCIL_NEQUAL);
DRWShadingGroup *shgrp = DRW_shgroup_create(sh_data->ghost_clear_depth, psl->ghost_clear_depth);
DRW_shgroup_stencil_mask(shgrp, 0x00);
DRW_shgroup_call_add(shgrp, DRW_cache_fullscreen_quad_get(), NULL);
}
{
/* Normals */
psl->normals = DRW_pass_create(
"Edit Mesh Normals Pass",
DRW_STATE_WRITE_DEPTH | DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_LESS_EQUAL);
stl->g_data->fnormals_shgrp = DRW_shgroup_create(sh_data->normals_face, psl->normals);
DRW_shgroup_uniform_float(stl->g_data->fnormals_shgrp, "normalSize", &size_normal, 1);
DRW_shgroup_uniform_vec4(stl->g_data->fnormals_shgrp, "color", G_draw.block.colorNormal, 1);
if (rv3d->rflag & RV3D_CLIPPING) {
DRW_shgroup_world_clip_planes_from_rv3d(stl->g_data->fnormals_shgrp, rv3d);
}
stl->g_data->vnormals_shgrp = DRW_shgroup_create(sh_data->normals, psl->normals);
DRW_shgroup_uniform_float(stl->g_data->vnormals_shgrp, "normalSize", &size_normal, 1);
DRW_shgroup_uniform_vec4(stl->g_data->vnormals_shgrp, "color", G_draw.block.colorVNormal, 1);
if (rv3d->rflag & RV3D_CLIPPING) {
DRW_shgroup_world_clip_planes_from_rv3d(stl->g_data->vnormals_shgrp, rv3d);
}
stl->g_data->lnormals_shgrp = DRW_shgroup_create(sh_data->normals_loop, psl->normals);
DRW_shgroup_uniform_float(stl->g_data->lnormals_shgrp, "normalSize", &size_normal, 1);
DRW_shgroup_uniform_vec4(stl->g_data->lnormals_shgrp, "color", G_draw.block.colorLNormal, 1);
if (rv3d->rflag & RV3D_CLIPPING) {
DRW_shgroup_world_clip_planes_from_rv3d(stl->g_data->lnormals_shgrp, rv3d);
}
}
if (!stl->g_data->do_zbufclip) {
psl->edit_face_overlay = edit_mesh_create_overlay_pass(
&face_mod, &stl->g_data->edge_width_scale, stl->g_data->data_mask, stl->g_data->do_edges, false,
DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_BLEND,
&stl->g_data->face_shgrp,
&stl->g_data->face_cage_shgrp,
&stl->g_data->verts_shgrp,
&stl->g_data->ledges_shgrp,
&stl->g_data->lverts_shgrp,
&stl->g_data->facedot_shgrp);
}
else {
/* We render all wires with depth and opaque to a new fbo and blend the result based on depth values */
psl->edit_face_occluded = edit_mesh_create_overlay_pass(
&zero, &stl->g_data->edge_width_scale, stl->g_data->data_mask, stl->g_data->do_edges, true,
DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_WRITE_DEPTH,
&stl->g_data->face_shgrp,
&stl->g_data->face_cage_shgrp,
&stl->g_data->verts_shgrp,
&stl->g_data->ledges_shgrp,
&stl->g_data->lverts_shgrp,
&stl->g_data->facedot_shgrp);
/* however we loose the front faces value (because we need the depth of occluded wires and
* faces are alpha blended ) so we recover them in a new pass. */
psl->facefill_occlude = DRW_pass_create(
"Front Face Color",
DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_BLEND);
stl->g_data->facefill_occluded_shgrp = DRW_shgroup_create(sh_data->overlay_facefill, psl->facefill_occlude);
DRW_shgroup_uniform_block(stl->g_data->facefill_occluded_shgrp, "globalsBlock", G_draw.block_ubo);
DRW_shgroup_uniform_ivec4(stl->g_data->facefill_occluded_shgrp, "dataMask", stl->g_data->data_mask, 1);
if (rv3d->rflag & RV3D_CLIPPING) {
DRW_shgroup_world_clip_planes_from_rv3d(stl->g_data->facefill_occluded_shgrp, rv3d);
}
/* we need a full screen pass to combine the result */
struct GPUBatch *quad = DRW_cache_fullscreen_quad_get();
psl->mix_occlude = DRW_pass_create(
"Mix Occluded Wires",
DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND);
DRWShadingGroup *mix_shgrp = DRW_shgroup_create(sh_data->overlay_mix, psl->mix_occlude);
DRW_shgroup_call_add(mix_shgrp, quad, NULL);
DRW_shgroup_uniform_float(mix_shgrp, "alpha", &backwire_opacity, 1);
DRW_shgroup_uniform_texture_ref(mix_shgrp, "wireColor", &e_data.occlude_wire_color_tx);
DRW_shgroup_uniform_texture_ref(mix_shgrp, "wireDepth", &e_data.occlude_wire_depth_tx);
DRW_shgroup_uniform_texture_ref(mix_shgrp, "sceneDepth", &dtxl->depth);
}
}
static void edit_mesh_add_ob_to_pass(
Scene *scene, Object *ob,
EDIT_MESH_PrivateData *g_data,
DRWShadingGroup *facedot_shgrp,
DRWShadingGroup *facefill_shgrp)
{
struct GPUBatch *geom_tris, *geom_verts, *geom_ledges, *geom_ledges_nor, *geom_lverts, *geom_fcenter;
ToolSettings *tsettings = scene->toolsettings;
bool has_edit_mesh_cage = false;
/* TODO: Should be its own function. */
Mesh *me = (Mesh *)ob->data;
BMEditMesh *embm = me->edit_btmesh;
if (embm) {
has_edit_mesh_cage = embm->mesh_eval_cage && (embm->mesh_eval_cage != embm->mesh_eval_final);
}
DRWShadingGroup *face_shgrp = (has_edit_mesh_cage) ? g_data->face_cage_shgrp : g_data->face_shgrp;
DRWShadingGroup *verts_shgrp = g_data->verts_shgrp;
DRWShadingGroup *ledges_shgrp = g_data->ledges_shgrp;
DRWShadingGroup *lverts_shgrp = g_data->lverts_shgrp;
geom_tris = DRW_mesh_batch_cache_get_edit_triangles(ob->data);
geom_ledges = DRW_mesh_batch_cache_get_edit_loose_edges(ob->data);
DRW_shgroup_call_add(face_shgrp, geom_tris, ob->obmat);
DRW_shgroup_call_add(ledges_shgrp, geom_ledges, ob->obmat);
if (facefill_shgrp) {
DRW_shgroup_call_add(facefill_shgrp, geom_tris, ob->obmat);
}
if ((tsettings->selectmode & SCE_SELECT_VERTEX) != 0) {
/* Thoses are point batches. */
geom_verts = DRW_mesh_batch_cache_get_edit_vertices(ob->data);
geom_ledges_nor = DRW_mesh_batch_cache_get_edit_loose_edges_nor(ob->data);
geom_lverts = DRW_mesh_batch_cache_get_edit_loose_verts(ob->data);
DRW_shgroup_call_add(verts_shgrp, geom_verts, ob->obmat);
DRW_shgroup_call_add(lverts_shgrp, geom_ledges_nor, ob->obmat);
DRW_shgroup_call_add(lverts_shgrp, geom_lverts, ob->obmat);
}
if (facedot_shgrp && (tsettings->selectmode & SCE_SELECT_FACE) != 0 ) {
geom_fcenter = DRW_mesh_batch_cache_get_edit_facedots(ob->data);
DRW_shgroup_call_add(facedot_shgrp, geom_fcenter, ob->obmat);
}
}
static void EDIT_MESH_cache_populate(void *vedata, Object *ob)
{
EDIT_MESH_StorageList *stl = ((EDIT_MESH_Data *)vedata)->stl;
const DRWContextState *draw_ctx = DRW_context_state_get();
View3D *v3d = draw_ctx->v3d;
Scene *scene = draw_ctx->scene;
ToolSettings *tsettings = scene->toolsettings;
struct GPUBatch *geom;
if (ob->type == OB_MESH) {
if ((ob == draw_ctx->object_edit) || BKE_object_is_in_editmode(ob)) {
bool do_occlude_wire = (v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_OCCLUDE_WIRE) != 0;
bool do_show_weight = (v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_WEIGHT) != 0;
bool fnormals_do = (v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_FACE_NORMALS) != 0;
bool vnormals_do = (v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_VERT_NORMALS) != 0;
bool lnormals_do = (v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_LOOP_NORMALS) != 0;
bool show_face_dots = (v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_FACE_DOT) != 0;
if (stl->g_data->do_faces == false &&
stl->g_data->do_edges == false &&
(tsettings->selectmode & SCE_SELECT_FACE))
{
/* Force display of face centers in this case because that's
* the only way to see if a face is selected. */
show_face_dots = true;
}
/* Updating uniform */
backwire_opacity = v3d->overlay.backwire_opacity;
size_normal = v3d->overlay.normals_length;
face_mod = (do_occlude_wire) ? 0.0f : 1.0f;
if (!stl->g_data->do_faces) {
face_mod = 0.0f;
}
if (do_show_weight) {
geom = DRW_cache_mesh_surface_weights_get(ob);
DRW_shgroup_call_add(stl->g_data->fweights_shgrp, geom, ob->obmat);
}
if (do_occlude_wire) {
geom = DRW_cache_mesh_surface_get(ob);
DRW_shgroup_call_add(stl->g_data->depth_shgrp_hidden_wire, geom, ob->obmat);
}
if (vnormals_do) {
geom = DRW_mesh_batch_cache_get_edit_triangles_nor(ob->data);
DRW_shgroup_call_add(stl->g_data->vnormals_shgrp, geom, ob->obmat);
geom = DRW_mesh_batch_cache_get_edit_loose_verts(ob->data);
DRW_shgroup_call_add(stl->g_data->vnormals_shgrp, geom, ob->obmat);
geom = DRW_mesh_batch_cache_get_edit_loose_edges_nor(ob->data);
DRW_shgroup_call_add(stl->g_data->vnormals_shgrp, geom, ob->obmat);
}
if (lnormals_do) {
geom = DRW_mesh_batch_cache_get_edit_triangles_lnor(ob->data);
DRW_shgroup_call_add(stl->g_data->lnormals_shgrp, geom, ob->obmat);
}
if (fnormals_do) {
geom = DRW_mesh_batch_cache_get_edit_facedots(ob->data);
DRW_shgroup_call_add(stl->g_data->fnormals_shgrp, geom, ob->obmat);
}
if (stl->g_data->do_zbufclip) {
edit_mesh_add_ob_to_pass(
scene, ob, stl->g_data,
stl->g_data->facedot_shgrp,
(stl->g_data->do_faces) ? stl->g_data->facefill_occluded_shgrp : NULL);
}
else {
edit_mesh_add_ob_to_pass(
scene, ob, stl->g_data,
(show_face_dots) ? stl->g_data->facedot_shgrp : NULL,
NULL);
}
stl->g_data->ghost_ob += (ob->dtx & OB_DRAWXRAY) ? 1 : 0;
stl->g_data->edit_ob += 1;
/* 3D text overlay */
if (v3d->overlay.edit_flag & (V3D_OVERLAY_EDIT_EDGE_LEN |
V3D_OVERLAY_EDIT_FACE_AREA |
V3D_OVERLAY_EDIT_FACE_ANG |
V3D_OVERLAY_EDIT_EDGE_ANG |
V3D_OVERLAY_EDIT_INDICES))
{
if (DRW_state_show_text()) {
DRW_edit_mesh_mode_text_measure_stats(
draw_ctx->ar, v3d, ob, &scene->unit);
}
}
}
}
}
static void EDIT_MESH_draw_scene(void *vedata)
{
EDIT_MESH_PassList *psl = ((EDIT_MESH_Data *)vedata)->psl;
EDIT_MESH_StorageList *stl = ((EDIT_MESH_Data *)vedata)->stl;
EDIT_MESH_FramebufferList *fbl = ((EDIT_MESH_Data *)vedata)->fbl;
DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
DRW_draw_pass(psl->weight_faces);
DRW_draw_pass(psl->depth_hidden_wire);
if (stl->g_data->do_zbufclip) {
float clearcol[4] = {0.0f, 0.0f, 0.0f, 0.0f};
/* render facefill */
DRW_draw_pass(psl->facefill_occlude);
/* Render wires on a separate framebuffer */
GPU_framebuffer_bind(fbl->occlude_wire_fb);
GPU_framebuffer_clear_color_depth(fbl->occlude_wire_fb, clearcol, 1.0f);
DRW_draw_pass(psl->normals);
DRW_draw_pass(psl->edit_face_occluded);
/* Combine with scene buffer */
GPU_framebuffer_bind(dfbl->color_only_fb);
DRW_draw_pass(psl->mix_occlude);
}
else {
DRW_draw_pass(psl->normals);
const DRWContextState *draw_ctx = DRW_context_state_get();
View3D *v3d = draw_ctx->v3d;
if (v3d->shading.type == OB_SOLID && (v3d->shading.flag & XRAY_FLAG(v3d)) == 0) {
if (stl->g_data->ghost_ob == 1 && stl->g_data->edit_ob == 1) {
/* In the case of single ghost object edit (common case for retopology):
* we duplicate the depht+stencil buffer and clear all depth to 1.0f where
* the stencil buffer is no 0x00. */
const float *viewport_size = DRW_viewport_size_get();
const int size[2] = {(int)viewport_size[0], (int)viewport_size[1]};
struct GPUTexture *ghost_depth_tx = DRW_texture_pool_query_2D(size[0], size[1], GPU_DEPTH24_STENCIL8, &draw_engine_edit_mesh_type);
GPU_framebuffer_ensure_config(&fbl->ghost_wire_fb, {
GPU_ATTACHMENT_TEXTURE(ghost_depth_tx),
GPU_ATTACHMENT_TEXTURE(dtxl->color),
});
GPU_framebuffer_blit(dfbl->depth_only_fb, 0, fbl->ghost_wire_fb, 0, GPU_DEPTH_BIT | GPU_STENCIL_BIT);
GPU_framebuffer_bind(fbl->ghost_wire_fb);
DRW_draw_pass(psl->ghost_clear_depth);
}
}
DRW_draw_pass(psl->edit_face_overlay);
}
DRW_state_clip_planes_reset();
}
static void EDIT_MESH_engine_free(void)
{
for (int sh_data_index = 0; sh_data_index < ARRAY_SIZE(e_data.sh_data); sh_data_index++) {
EDIT_MESH_Shaders *sh_data = &e_data.sh_data[sh_data_index];
/* Don't free builtins. */
sh_data->depth = NULL;
GPUShader **sh_data_as_array = (GPUShader **)sh_data;
for (int i = 0; i < (sizeof(EDIT_MESH_Shaders) / sizeof(GPUShader *)); i++) {
DRW_SHADER_FREE_SAFE(sh_data_as_array[i]);
}
}
}
static const DrawEngineDataSize EDIT_MESH_data_size = DRW_VIEWPORT_DATA_SIZE(EDIT_MESH_Data);
DrawEngineType draw_engine_edit_mesh_type = {
NULL, NULL,
N_("EditMeshMode"),
&EDIT_MESH_data_size,
&EDIT_MESH_engine_init,
&EDIT_MESH_engine_free,
&EDIT_MESH_cache_init,
&EDIT_MESH_cache_populate,
NULL,
NULL,
&EDIT_MESH_draw_scene,
NULL,
NULL,
};
|