Edit Mesh: Only draw vertices once
[blender.git] / source / blender / draw / intern / draw_cache_impl_mesh.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2017 by Blender Foundation.
17  * All rights reserved.
18  */
19
20 /** \file \ingroup draw
21  *
22  * \brief Mesh API for render engines
23  */
24
25 #include "MEM_guardedalloc.h"
26
27 #include "BLI_buffer.h"
28 #include "BLI_utildefines.h"
29 #include "BLI_math_vector.h"
30 #include "BLI_math_bits.h"
31 #include "BLI_string.h"
32 #include "BLI_alloca.h"
33 #include "BLI_edgehash.h"
34
35 #include "DNA_mesh_types.h"
36 #include "DNA_meshdata_types.h"
37 #include "DNA_object_types.h"
38 #include "DNA_scene_types.h"
39
40 #include "BKE_customdata.h"
41 #include "BKE_deform.h"
42 #include "BKE_editmesh.h"
43 #include "BKE_editmesh_cache.h"
44 #include "BKE_editmesh_tangent.h"
45 #include "BKE_mesh.h"
46 #include "BKE_mesh_tangent.h"
47 #include "BKE_mesh_runtime.h"
48 #include "BKE_object_deform.h"
49
50
51 #include "bmesh.h"
52
53 #include "GPU_batch.h"
54 #include "GPU_material.h"
55
56 #include "DRW_render.h"
57
58 #include "ED_mesh.h"
59 #include "ED_uvedit.h"
60
61 #include "draw_cache_impl.h"  /* own include */
62
63
64 static void mesh_batch_cache_clear(Mesh *me);
65
66 /* Vertex Group Selection and display options */
67 typedef struct DRW_MeshWeightState {
68         int defgroup_active;
69         int defgroup_len;
70
71         short flags;
72         char alert_mode;
73
74         /* Set of all selected bones for Multipaint. */
75         bool *defgroup_sel; /* [defgroup_len] */
76         int   defgroup_sel_count;
77 } DRW_MeshWeightState;
78
79 /* DRW_MeshWeightState.flags */
80 enum {
81         DRW_MESH_WEIGHT_STATE_MULTIPAINT          = (1 << 0),
82         DRW_MESH_WEIGHT_STATE_AUTO_NORMALIZE      = (1 << 1),
83 };
84
85 /* ---------------------------------------------------------------------- */
86 /** \name Mesh/BMesh Interface (direct access to basic data).
87  * \{ */
88
89 static int mesh_render_verts_len_get(Mesh *me)
90 {
91         return me->edit_btmesh ? me->edit_btmesh->bm->totvert : me->totvert;
92 }
93
94 static int mesh_render_edges_len_get(Mesh *me)
95 {
96         return me->edit_btmesh ? me->edit_btmesh->bm->totedge : me->totedge;
97 }
98
99 static int mesh_render_looptri_len_get(Mesh *me)
100 {
101         return me->edit_btmesh ? me->edit_btmesh->tottri : poly_to_tri_count(me->totpoly, me->totloop);
102 }
103
104 static int mesh_render_polys_len_get(Mesh *me)
105 {
106         return me->edit_btmesh ? me->edit_btmesh->bm->totface : me->totpoly;
107 }
108
109 static int mesh_render_mat_len_get(Mesh *me)
110 {
111         return MAX2(1, me->totcol);
112 }
113
114 static int UNUSED_FUNCTION(mesh_render_loops_len_get)(Mesh *me)
115 {
116         return me->edit_btmesh ? me->edit_btmesh->bm->totloop : me->totloop;
117 }
118
119 /** \} */
120
121
122 /* ---------------------------------------------------------------------- */
123 /** \name Mesh/BMesh Interface (indirect, partially cached access to complex data).
124  * \{ */
125
126 typedef struct EdgeAdjacentPolys {
127         int count;
128         int face_index[2];
129 } EdgeAdjacentPolys;
130
131 typedef struct EdgeAdjacentVerts {
132         int vert_index[2]; /* -1 if none */
133 } EdgeAdjacentVerts;
134
135 typedef struct EdgeDrawAttr {
136         uchar v_flag;
137         uchar e_flag;
138         uchar crease;
139         uchar bweight;
140 } EdgeDrawAttr;
141
142 typedef struct MeshRenderData {
143         int types;
144
145         int vert_len;
146         int edge_len;
147         int tri_len;
148         int loop_len;
149         int poly_len;
150         int mat_len;
151         int loose_vert_len;
152         int loose_edge_len;
153
154         /* Support for mapped mesh data. */
155         struct {
156                 /* Must be set if we want to get mapped data. */
157                 bool use;
158                 bool supported;
159
160                 Mesh *me_cage;
161
162                 int vert_len;
163                 int edge_len;
164                 int tri_len;
165                 int loop_len;
166                 int poly_len;
167
168                 int *loose_verts;
169                 int  loose_vert_len;
170
171                 int *loose_edges;
172                 int  loose_edge_len;
173
174                 /* origindex layers */
175                 int *v_origindex;
176                 int *e_origindex;
177                 int *l_origindex;
178                 int *p_origindex;
179         } mapped;
180
181         BMEditMesh *edit_bmesh;
182         struct EditMeshData *edit_data;
183         const ToolSettings *toolsettings;
184
185         Mesh *me;
186
187         MVert *mvert;
188         const MEdge *medge;
189         const MLoop *mloop;
190         const MPoly *mpoly;
191         float (*orco)[3];  /* vertex coordinates normalized to bounding box */
192         bool is_orco_allocated;
193         MDeformVert *dvert;
194         MLoopUV *mloopuv;
195         MLoopCol *mloopcol;
196         float (*loop_normals)[3];
197
198         /* CustomData 'cd' cache for efficient access. */
199         struct {
200                 struct {
201                         MLoopUV **uv;
202                         int       uv_len;
203                         int       uv_active;
204
205                         MLoopCol **vcol;
206                         int        vcol_len;
207                         int        vcol_active;
208
209                         float (**tangent)[4];
210                         int      tangent_len;
211                         int      tangent_active;
212
213                         bool *auto_vcol;
214                 } layers;
215
216                 /* Custom-data offsets (only needed for BMesh access) */
217                 struct {
218                         int crease;
219                         int bweight;
220                         int *uv;
221                         int *vcol;
222 #ifdef WITH_FREESTYLE
223                         int freestyle_edge;
224                         int freestyle_face;
225 #endif
226                 } offset;
227
228                 struct {
229                         char (*auto_mix)[32];
230                         char (*uv)[32];
231                         char (*vcol)[32];
232                         char (*tangent)[32];
233                 } uuid;
234
235                 /* for certain cases we need an output loop-data storage (bmesh tangents) */
236                 struct {
237                         CustomData ldata;
238                         /* grr, special case variable (use in place of 'dm->tangent_mask') */
239                         short tangent_mask;
240                 } output;
241         } cd;
242
243         BMVert *eve_act;
244         BMEdge *eed_act;
245         BMFace *efa_act;
246         BMFace *efa_act_uv;
247
248         /* Data created on-demand (usually not for bmesh-based data). */
249         EdgeAdjacentPolys *edges_adjacent_polys;
250         MLoopTri *mlooptri;
251         int *loose_edges;
252         int *loose_verts;
253
254         float (*poly_normals)[3];
255         float *vert_weight;
256         char (*vert_color)[3];
257         GPUPackedNormal *poly_normals_pack;
258         GPUPackedNormal *vert_normals_pack;
259         bool *edge_select_bool;
260         bool *edge_visible_bool;
261 } MeshRenderData;
262
263 enum {
264         MR_DATATYPE_VERT       = 1 << 0,
265         MR_DATATYPE_EDGE       = 1 << 1,
266         MR_DATATYPE_LOOPTRI    = 1 << 2,
267         MR_DATATYPE_LOOP       = 1 << 3,
268         MR_DATATYPE_POLY       = 1 << 4,
269         MR_DATATYPE_OVERLAY    = 1 << 5,
270         MR_DATATYPE_SHADING    = 1 << 6,
271         MR_DATATYPE_DVERT      = 1 << 7,
272         MR_DATATYPE_LOOPCOL    = 1 << 8,
273         MR_DATATYPE_LOOPUV     = 1 << 9,
274         MR_DATATYPE_LOOSE_VERT = 1 << 10,
275         MR_DATATYPE_LOOSE_EDGE = 1 << 11,
276 };
277
278 /**
279  * These functions look like they would be slow but they will typically return true on the first iteration.
280  * Only false when all attached elements are hidden.
281  */
282 static bool bm_vert_has_visible_edge(const BMVert *v)
283 {
284         const BMEdge *e_iter, *e_first;
285
286         e_iter = e_first = v->e;
287         do {
288                 if (!BM_elem_flag_test(e_iter, BM_ELEM_HIDDEN)) {
289                         return true;
290                 }
291         } while ((e_iter = BM_DISK_EDGE_NEXT(e_iter, v)) != e_first);
292         return false;
293 }
294
295 static bool bm_edge_has_visible_face(const BMEdge *e)
296 {
297         const BMLoop *l_iter, *l_first;
298         l_iter = l_first = e->l;
299         do {
300                 if (!BM_elem_flag_test(l_iter->f, BM_ELEM_HIDDEN)) {
301                         return true;
302                 }
303         } while ((l_iter = l_iter->radial_next) != l_first);
304         return false;
305 }
306
307 BLI_INLINE bool bm_vert_is_loose_and_visible(const BMVert *v)
308 {
309         return (!BM_elem_flag_test(v, BM_ELEM_HIDDEN) &&
310                 (v->e == NULL || !bm_vert_has_visible_edge(v)));
311 }
312
313 BLI_INLINE bool bm_edge_is_loose_and_visible(const BMEdge *e)
314 {
315         return (!BM_elem_flag_test(e, BM_ELEM_HIDDEN) &&
316                 (e->l == NULL || !bm_edge_has_visible_face(e)));
317 }
318
319 /* Return true is all layers in _b_ are inside _a_. */
320 static bool mesh_cd_layers_type_overlap(
321         const uchar av[CD_NUMTYPES], const ushort al[CD_NUMTYPES],
322         const uchar bv[CD_NUMTYPES], const ushort bl[CD_NUMTYPES])
323 {
324         for (int i = 0; i < CD_NUMTYPES; ++i) {
325                 if ((av[i] & bv[i]) != bv[i]) {
326                         return false;
327                 }
328                 if ((al[i] & bl[i]) != bl[i]) {
329                         return false;
330                 }
331         }
332         return true;
333 }
334
335 static void mesh_cd_layers_type_merge(
336         uchar av[CD_NUMTYPES], ushort al[CD_NUMTYPES],
337         uchar bv[CD_NUMTYPES], ushort bl[CD_NUMTYPES])
338 {
339         for (int i = 0; i < CD_NUMTYPES; ++i) {
340                 av[i] |= bv[i];
341                 al[i] |= bl[i];
342         }
343 }
344
345 static void mesh_cd_calc_active_uv_layer(
346         const Mesh *me, ushort cd_lused[CD_NUMTYPES])
347 {
348         const CustomData *cd_ldata = (me->edit_btmesh) ? &me->edit_btmesh->bm->ldata : &me->ldata;
349
350         int layer = CustomData_get_active_layer(cd_ldata, CD_MLOOPUV);
351         if (layer != -1) {
352                 cd_lused[CD_MLOOPUV] |= (1 << layer);
353         }
354 }
355
356 static void mesh_cd_calc_active_vcol_layer(
357         const Mesh *me, ushort cd_lused[CD_NUMTYPES])
358 {
359         const CustomData *cd_ldata = (me->edit_btmesh) ? &me->edit_btmesh->bm->ldata : &me->ldata;
360
361         int layer = CustomData_get_active_layer(cd_ldata, CD_MLOOPCOL);
362         if (layer != -1) {
363                 cd_lused[CD_MLOOPCOL] |= (1 << layer);
364         }
365 }
366
367 static void mesh_cd_calc_used_gpu_layers(
368         const Mesh *me, uchar cd_vused[CD_NUMTYPES], ushort cd_lused[CD_NUMTYPES],
369         struct GPUMaterial **gpumat_array, int gpumat_array_len)
370 {
371         const CustomData *cd_ldata = (me->edit_btmesh) ? &me->edit_btmesh->bm->ldata : &me->ldata;
372
373         /* See: DM_vertex_attributes_from_gpu for similar logic */
374         GPUVertAttrLayers gpu_attrs = {{{0}}};
375
376         for (int i = 0; i < gpumat_array_len; i++) {
377                 GPUMaterial *gpumat = gpumat_array[i];
378                 if (gpumat) {
379                         GPU_material_vertex_attrs(gpumat, &gpu_attrs);
380                         for (int j = 0; j < gpu_attrs.totlayer; j++) {
381                                 const char *name = gpu_attrs.layer[j].name;
382                                 int type = gpu_attrs.layer[j].type;
383                                 int layer = -1;
384
385                                 if (type == CD_AUTO_FROM_NAME) {
386                                         /* We need to deduct what exact layer is used.
387                                          *
388                                          * We do it based on the specified name.
389                                          */
390                                         if (name[0] != '\0') {
391                                                 layer = CustomData_get_named_layer(cd_ldata, CD_MLOOPUV, name);
392                                                 type = CD_MTFACE;
393
394                                                 if (layer == -1) {
395                                                         layer = CustomData_get_named_layer(cd_ldata, CD_MLOOPCOL, name);
396                                                         type = CD_MCOL;
397                                                 }
398 #if 0                                   /* Tangents are always from UV's - this will never happen. */
399                                                 if (layer == -1) {
400                                                         layer = CustomData_get_named_layer(cd_ldata, CD_TANGENT, name);
401                                                         type = CD_TANGENT;
402                                                 }
403 #endif
404                                                 if (layer == -1) {
405                                                         continue;
406                                                 }
407                                         }
408                                         else {
409                                                 /* Fall back to the UV layer, which matches old behavior. */
410                                                 type = CD_MTFACE;
411                                         }
412                                 }
413
414                                 switch (type) {
415                                         case CD_MTFACE:
416                                         {
417                                                 if (layer == -1) {
418                                                         layer = (name[0] != '\0') ?
419                                                                 CustomData_get_named_layer(cd_ldata, CD_MLOOPUV, name) :
420                                                                 CustomData_get_active_layer(cd_ldata, CD_MLOOPUV);
421                                                 }
422                                                 if (layer != -1) {
423                                                         cd_lused[CD_MLOOPUV] |= (1 << layer);
424                                                 }
425                                                 break;
426                                         }
427                                         case CD_TANGENT:
428                                         {
429                                                 if (layer == -1) {
430                                                         layer = (name[0] != '\0') ?
431                                                                 CustomData_get_named_layer(cd_ldata, CD_MLOOPUV, name) :
432                                                                 CustomData_get_active_layer(cd_ldata, CD_MLOOPUV);
433
434                                                         /* Only fallback to orco (below) when we have no UV layers, see: T56545 */
435                                                         if (layer == -1 && name[0] != '\0') {
436                                                                 layer = CustomData_get_active_layer(cd_ldata, CD_MLOOPUV);
437                                                         }
438                                                 }
439                                                 if (layer != -1) {
440                                                         cd_lused[CD_TANGENT] |= (1 << layer);
441                                                 }
442                                                 else {
443                                                         /* no UV layers at all => requesting orco */
444                                                         cd_lused[CD_TANGENT] |= DM_TANGENT_MASK_ORCO;
445                                                         cd_vused[CD_ORCO] |= 1;
446                                                 }
447                                                 break;
448                                         }
449                                         case CD_MCOL:
450                                         {
451                                                 if (layer == -1) {
452                                                         layer = (name[0] != '\0') ?
453                                                                 CustomData_get_named_layer(cd_ldata, CD_MLOOPCOL, name) :
454                                                                 CustomData_get_active_layer(cd_ldata, CD_MLOOPCOL);
455                                                 }
456                                                 if (layer != -1) {
457                                                         cd_lused[CD_MLOOPCOL] |= (1 << layer);
458                                                 }
459                                                 break;
460                                         }
461                                         case CD_ORCO:
462                                         {
463                                                 cd_vused[CD_ORCO] |= 1;
464                                                 break;
465                                         }
466                                 }
467                         }
468                 }
469         }
470 }
471
472
473 static void mesh_render_calc_normals_loop_and_poly(const Mesh *me, const float split_angle, MeshRenderData *rdata)
474 {
475         BLI_assert((me->flag & ME_AUTOSMOOTH) != 0);
476
477         int totloop = me->totloop;
478         int totpoly = me->totpoly;
479         float (*loop_normals)[3] = MEM_mallocN(sizeof(*loop_normals) * totloop, __func__);
480         float (*poly_normals)[3] = MEM_mallocN(sizeof(*poly_normals) * totpoly, __func__);
481         short (*clnors)[2] = CustomData_get_layer(&me->ldata, CD_CUSTOMLOOPNORMAL);
482
483         BKE_mesh_calc_normals_poly(
484                 me->mvert, NULL, me->totvert,
485                 me->mloop, me->mpoly, totloop, totpoly, poly_normals, false);
486
487         BKE_mesh_normals_loop_split(
488                 me->mvert, me->totvert, me->medge, me->totedge,
489                 me->mloop, loop_normals, totloop, me->mpoly, poly_normals, totpoly,
490                 true, split_angle, NULL, clnors, NULL);
491
492         rdata->loop_len = totloop;
493         rdata->poly_len = totpoly;
494         rdata->loop_normals = loop_normals;
495         rdata->poly_normals = poly_normals;
496 }
497
498 static void mesh_cd_extract_auto_layers_names_and_srgb(
499         Mesh *me, const ushort cd_lused[CD_NUMTYPES],
500         char **r_auto_layers_names, int **r_auto_layers_srgb, int *r_auto_layers_len)
501 {
502         const CustomData *cd_ldata = (me->edit_btmesh) ? &me->edit_btmesh->bm->ldata : &me->ldata;
503
504         int uv_len_used = count_bits_i(cd_lused[CD_MLOOPUV]);
505         int vcol_len_used = count_bits_i(cd_lused[CD_MLOOPCOL]);
506         int uv_len = CustomData_number_of_layers(cd_ldata, CD_MLOOPUV);
507         int vcol_len = CustomData_number_of_layers(cd_ldata, CD_MLOOPCOL);
508
509         uint auto_names_len = 32 * (uv_len_used + vcol_len_used);
510         uint auto_ofs = 0;
511         /* Allocate max, resize later. */
512         char *auto_names = MEM_callocN(sizeof(char) * auto_names_len, __func__);
513         int *auto_is_srgb = MEM_callocN(sizeof(int) * (uv_len_used + vcol_len_used), __func__);
514
515         for (int i = 0; i < uv_len; i++) {
516                 if ((cd_lused[CD_MLOOPUV] & (1 << i)) != 0) {
517                         const char *name = CustomData_get_layer_name(cd_ldata, CD_MLOOPUV, i);
518                         uint hash = BLI_ghashutil_strhash_p(name);
519                         /* +1 to include '\0' terminator. */
520                         auto_ofs += 1 + BLI_snprintf_rlen(auto_names + auto_ofs, auto_names_len - auto_ofs, "ba%u", hash);
521                 }
522         }
523
524         uint auto_is_srgb_ofs = uv_len_used;
525         for (int i = 0; i < vcol_len; i++) {
526                 if ((cd_lused[CD_MLOOPCOL] & (1 << i)) != 0) {
527                         const char *name = CustomData_get_layer_name(cd_ldata, CD_MLOOPCOL, i);
528                         /* We only do vcols that are not overridden by a uv layer with same name. */
529                         if (CustomData_get_named_layer_index(cd_ldata, CD_MLOOPUV, name) == -1) {
530                                 uint hash = BLI_ghashutil_strhash_p(name);
531                                 /* +1 to include '\0' terminator. */
532                                 auto_ofs += 1 + BLI_snprintf_rlen(auto_names + auto_ofs, auto_names_len - auto_ofs, "ba%u", hash);
533                                 auto_is_srgb[auto_is_srgb_ofs] = true;
534                                 auto_is_srgb_ofs++;
535                         }
536                 }
537         }
538
539         auto_names = MEM_reallocN(auto_names, sizeof(char) * auto_ofs);
540         auto_is_srgb = MEM_reallocN(auto_is_srgb, sizeof(int) * auto_is_srgb_ofs);
541
542         *r_auto_layers_names = auto_names;
543         *r_auto_layers_srgb = auto_is_srgb;
544         *r_auto_layers_len = auto_is_srgb_ofs;
545 }
546
547 /**
548  * TODO(campbell): 'gpumat_array' may include materials linked to the object.
549  * While not default, object materials should be supported.
550  * Although this only impacts the data that's generated, not the materials that display.
551  */
552 static MeshRenderData *mesh_render_data_create_ex(
553         Mesh *me, const int types, const uchar cd_vused[CD_NUMTYPES], const ushort cd_lused[CD_NUMTYPES],
554         const ToolSettings *ts)
555 {
556         MeshRenderData *rdata = MEM_callocN(sizeof(*rdata), __func__);
557         rdata->types = types;
558         rdata->toolsettings = ts;
559         rdata->mat_len = mesh_render_mat_len_get(me);
560
561         CustomData_reset(&rdata->cd.output.ldata);
562
563         const bool is_auto_smooth = (me->flag & ME_AUTOSMOOTH) != 0;
564         const float split_angle = is_auto_smooth ? me->smoothresh : (float)M_PI;
565
566         if (me->edit_btmesh) {
567                 BMEditMesh *embm = me->edit_btmesh;
568                 BMesh *bm = embm->bm;
569
570                 rdata->edit_bmesh = embm;
571                 rdata->edit_data = me->runtime.edit_data;
572
573                 if (embm->mesh_eval_cage && (embm->mesh_eval_cage->runtime.is_original == false)) {
574                         Mesh *me_cage = embm->mesh_eval_cage;
575
576                         rdata->mapped.me_cage = me_cage;
577                         if (types & MR_DATATYPE_VERT) {
578                                 rdata->mapped.vert_len = me_cage->totvert;
579                         }
580                         if (types & MR_DATATYPE_EDGE) {
581                                 rdata->mapped.edge_len = me_cage->totedge;
582                         }
583                         if (types & MR_DATATYPE_LOOP) {
584                                 rdata->mapped.loop_len = me_cage->totloop;
585                         }
586                         if (types & MR_DATATYPE_POLY) {
587                                 rdata->mapped.poly_len = me_cage->totpoly;
588                         }
589                         if (types & MR_DATATYPE_LOOPTRI) {
590                                 rdata->mapped.tri_len = poly_to_tri_count(me_cage->totpoly, me_cage->totloop);
591                         }
592                         if (types & MR_DATATYPE_LOOPUV) {
593                                 rdata->mloopuv = CustomData_get_layer(&me_cage->ldata, CD_MLOOPUV);
594                         }
595
596                         rdata->mapped.v_origindex = CustomData_get_layer(&me_cage->vdata, CD_ORIGINDEX);
597                         rdata->mapped.e_origindex = CustomData_get_layer(&me_cage->edata, CD_ORIGINDEX);
598                         rdata->mapped.l_origindex = CustomData_get_layer(&me_cage->ldata, CD_ORIGINDEX);
599                         rdata->mapped.p_origindex = CustomData_get_layer(&me_cage->pdata, CD_ORIGINDEX);
600                         rdata->mapped.supported = (
601                                 rdata->mapped.v_origindex &&
602                                 rdata->mapped.e_origindex &&
603                                 rdata->mapped.p_origindex);
604                 }
605
606                 int bm_ensure_types = 0;
607                 if (types & MR_DATATYPE_VERT) {
608                         rdata->vert_len = bm->totvert;
609                         bm_ensure_types |= BM_VERT;
610                 }
611                 if (types & MR_DATATYPE_EDGE) {
612                         rdata->edge_len = bm->totedge;
613                         bm_ensure_types |= BM_EDGE;
614                 }
615                 if (types & MR_DATATYPE_LOOPTRI) {
616                         bm_ensure_types |= BM_LOOP;
617                 }
618                 if (types & MR_DATATYPE_LOOP) {
619                         int totloop = bm->totloop;
620                         if (is_auto_smooth) {
621                                 rdata->loop_normals = MEM_mallocN(sizeof(*rdata->loop_normals) * totloop, __func__);
622                                 int cd_loop_clnors_offset = CustomData_get_offset(&bm->ldata, CD_CUSTOMLOOPNORMAL);
623                                 BM_loops_calc_normal_vcos(
624                                         bm, NULL, NULL, NULL, true, split_angle, rdata->loop_normals, NULL, NULL,
625                                         cd_loop_clnors_offset, false);
626                         }
627                         rdata->loop_len = totloop;
628                         bm_ensure_types |= BM_LOOP;
629                 }
630                 if (types & MR_DATATYPE_POLY) {
631                         rdata->poly_len = bm->totface;
632                         bm_ensure_types |= BM_FACE;
633                 }
634                 if (types & MR_DATATYPE_OVERLAY) {
635                         rdata->efa_act_uv = EDBM_uv_active_face_get(embm, false, false);
636                         rdata->efa_act = BM_mesh_active_face_get(bm, false, true);
637                         rdata->eed_act = BM_mesh_active_edge_get(bm);
638                         rdata->eve_act = BM_mesh_active_vert_get(bm);
639                         rdata->cd.offset.crease = CustomData_get_offset(&bm->edata, CD_CREASE);
640                         rdata->cd.offset.bweight = CustomData_get_offset(&bm->edata, CD_BWEIGHT);
641
642 #ifdef WITH_FREESTYLE
643                         rdata->cd.offset.freestyle_edge = CustomData_get_offset(&bm->edata, CD_FREESTYLE_EDGE);
644                         rdata->cd.offset.freestyle_face = CustomData_get_offset(&bm->pdata, CD_FREESTYLE_FACE);
645 #endif
646                 }
647                 if (types & (MR_DATATYPE_DVERT)) {
648                         bm_ensure_types |= BM_VERT;
649                 }
650                 if (rdata->edit_data != NULL) {
651                         bm_ensure_types |= BM_VERT;
652                 }
653
654                 BM_mesh_elem_index_ensure(bm, bm_ensure_types);
655                 BM_mesh_elem_table_ensure(bm, bm_ensure_types & ~BM_LOOP);
656
657                 if (types & MR_DATATYPE_LOOPTRI) {
658                         /* Edit mode ensures this is valid, no need to calculate. */
659                         BLI_assert((bm->totloop == 0) || (embm->looptris != NULL));
660                         int tottri = embm->tottri;
661                         MLoopTri *mlooptri = MEM_mallocN(sizeof(*rdata->mlooptri) * embm->tottri, __func__);
662                         for (int index = 0; index < tottri ; index ++ ) {
663                                 BMLoop **bmtri = embm->looptris[index];
664                                 MLoopTri *mtri = &mlooptri[index];
665                                 mtri->tri[0] = BM_elem_index_get(bmtri[0]);
666                                 mtri->tri[1] = BM_elem_index_get(bmtri[1]);
667                                 mtri->tri[2] = BM_elem_index_get(bmtri[2]);
668                         }
669                         rdata->mlooptri = mlooptri;
670                         rdata->tri_len = tottri;
671                 }
672
673                 if (types & MR_DATATYPE_LOOSE_VERT) {
674                         BLI_assert(types & MR_DATATYPE_VERT);
675                         rdata->loose_vert_len = 0;
676
677                         {
678                                 int *lverts = MEM_mallocN(rdata->vert_len * sizeof(int), __func__);
679                                 BLI_assert((bm->elem_table_dirty & BM_VERT) == 0);
680                                 for (int i = 0; i < bm->totvert; i++) {
681                                         const BMVert *eve = BM_vert_at_index(bm, i);
682                                         if (bm_vert_is_loose_and_visible(eve)) {
683                                                 lverts[rdata->loose_vert_len++] = i;
684                                         }
685                                 }
686                                 rdata->loose_verts = MEM_reallocN(lverts, rdata->loose_vert_len * sizeof(int));
687                         }
688
689                         if (rdata->mapped.supported) {
690                                 Mesh *me_cage = embm->mesh_eval_cage;
691                                 rdata->mapped.loose_vert_len = 0;
692
693                                 if (rdata->loose_vert_len) {
694                                         int *lverts = MEM_mallocN(me_cage->totvert * sizeof(int), __func__);
695                                         const int *v_origindex = rdata->mapped.v_origindex;
696                                         for (int i = 0; i < me_cage->totvert; i++) {
697                                                 const int v_orig = v_origindex[i];
698                                                 if (v_orig != ORIGINDEX_NONE) {
699                                                         BMVert *eve = BM_vert_at_index(bm, v_orig);
700                                                         if (bm_vert_is_loose_and_visible(eve)) {
701                                                                 lverts[rdata->mapped.loose_vert_len++] = i;
702                                                         }
703                                                 }
704                                         }
705                                         rdata->mapped.loose_verts = MEM_reallocN(lverts, rdata->mapped.loose_vert_len * sizeof(int));
706                                 }
707                         }
708                 }
709
710                 if (types & MR_DATATYPE_LOOSE_EDGE) {
711                         BLI_assert(types & MR_DATATYPE_EDGE);
712                         rdata->loose_edge_len = 0;
713
714                         {
715                                 int *ledges = MEM_mallocN(rdata->edge_len * sizeof(int), __func__);
716                                 BLI_assert((bm->elem_table_dirty & BM_EDGE) == 0);
717                                 for (int i = 0; i < bm->totedge; i++) {
718                                         const BMEdge *eed = BM_edge_at_index(bm, i);
719                                         if (bm_edge_is_loose_and_visible(eed)) {
720                                                 ledges[rdata->loose_edge_len++] = i;
721                                         }
722                                 }
723                                 rdata->loose_edges = MEM_reallocN(ledges, rdata->loose_edge_len * sizeof(int));
724                         }
725
726                         if (rdata->mapped.supported) {
727                                 Mesh *me_cage = embm->mesh_eval_cage;
728                                 rdata->mapped.loose_edge_len = 0;
729
730                                 if (rdata->loose_edge_len) {
731                                         int *ledges = MEM_mallocN(me_cage->totedge * sizeof(int), __func__);
732                                         const int *e_origindex = rdata->mapped.e_origindex;
733                                         for (int i = 0; i < me_cage->totedge; i++) {
734                                                 const int e_orig = e_origindex[i];
735                                                 if (e_orig != ORIGINDEX_NONE) {
736                                                         BMEdge *eed = BM_edge_at_index(bm, e_orig);
737                                                         if (bm_edge_is_loose_and_visible(eed)) {
738                                                                 ledges[rdata->mapped.loose_edge_len++] = i;
739                                                         }
740                                                 }
741                                         }
742                                         rdata->mapped.loose_edges = MEM_reallocN(ledges, rdata->mapped.loose_edge_len * sizeof(int));
743                                 }
744                         }
745                 }
746         }
747         else {
748                 rdata->me = me;
749
750                 if (types & (MR_DATATYPE_VERT)) {
751                         rdata->vert_len = me->totvert;
752                         rdata->mvert = CustomData_get_layer(&me->vdata, CD_MVERT);
753                 }
754                 if (types & (MR_DATATYPE_EDGE)) {
755                         rdata->edge_len = me->totedge;
756                         rdata->medge = CustomData_get_layer(&me->edata, CD_MEDGE);
757                 }
758                 if (types & MR_DATATYPE_LOOPTRI) {
759                         const int tri_len = rdata->tri_len = poly_to_tri_count(me->totpoly, me->totloop);
760                         MLoopTri *mlooptri = MEM_mallocN(sizeof(*mlooptri) * tri_len, __func__);
761                         BKE_mesh_recalc_looptri(me->mloop, me->mpoly, me->mvert, me->totloop, me->totpoly, mlooptri);
762                         rdata->mlooptri = mlooptri;
763                 }
764                 if (types & MR_DATATYPE_LOOP) {
765                         rdata->loop_len = me->totloop;
766                         rdata->mloop = CustomData_get_layer(&me->ldata, CD_MLOOP);
767
768                         if (is_auto_smooth) {
769                                 mesh_render_calc_normals_loop_and_poly(me, split_angle, rdata);
770                         }
771                 }
772                 if (types & MR_DATATYPE_POLY) {
773                         rdata->poly_len = me->totpoly;
774                         rdata->mpoly = CustomData_get_layer(&me->pdata, CD_MPOLY);
775                 }
776                 if (types & MR_DATATYPE_DVERT) {
777                         rdata->vert_len = me->totvert;
778                         rdata->dvert = CustomData_get_layer(&me->vdata, CD_MDEFORMVERT);
779                 }
780                 if (types & MR_DATATYPE_LOOPCOL) {
781                         rdata->loop_len = me->totloop;
782                         rdata->mloopcol = CustomData_get_layer(&me->ldata, CD_MLOOPCOL);
783                 }
784                 if (types & MR_DATATYPE_LOOPUV) {
785                         rdata->loop_len = me->totloop;
786                         rdata->mloopuv = CustomData_get_layer(&me->ldata, CD_MLOOPUV);
787                 }
788         }
789
790         if (types & MR_DATATYPE_SHADING) {
791                 CustomData *cd_vdata, *cd_ldata;
792
793                 BLI_assert(cd_vused != NULL && cd_lused != NULL);
794
795                 if (me->edit_btmesh) {
796                         BMesh *bm = me->edit_btmesh->bm;
797                         cd_vdata = &bm->vdata;
798                         cd_ldata = &bm->ldata;
799                 }
800                 else {
801                         cd_vdata = &me->vdata;
802                         cd_ldata = &me->ldata;
803                 }
804
805                 rdata->cd.layers.uv_active = CustomData_get_active_layer(cd_ldata, CD_MLOOPUV);
806                 rdata->cd.layers.vcol_active = CustomData_get_active_layer(cd_ldata, CD_MLOOPCOL);
807                 rdata->cd.layers.tangent_active = rdata->cd.layers.uv_active;
808
809 #define CD_VALIDATE_ACTIVE_LAYER(active_index, used) \
810                 if ((active_index != -1) && (used & (1 << active_index)) == 0) { \
811                         active_index = -1; \
812                 } ((void)0)
813
814                 CD_VALIDATE_ACTIVE_LAYER(rdata->cd.layers.uv_active, cd_lused[CD_MLOOPUV]);
815                 CD_VALIDATE_ACTIVE_LAYER(rdata->cd.layers.tangent_active, cd_lused[CD_TANGENT]);
816                 CD_VALIDATE_ACTIVE_LAYER(rdata->cd.layers.vcol_active, cd_lused[CD_MLOOPCOL]);
817
818 #undef CD_VALIDATE_ACTIVE_LAYER
819
820                 rdata->is_orco_allocated = false;
821                 if (cd_vused[CD_ORCO] & 1) {
822                         rdata->orco = CustomData_get_layer(cd_vdata, CD_ORCO);
823                         /* If orco is not available compute it ourselves */
824                         if (!rdata->orco) {
825                                 rdata->is_orco_allocated = true;
826                                 if (me->edit_btmesh) {
827                                         BMesh *bm = me->edit_btmesh->bm;
828                                         rdata->orco = MEM_mallocN(sizeof(*rdata->orco) * rdata->vert_len, "orco mesh");
829                                         BLI_assert((bm->elem_table_dirty & BM_VERT) == 0);
830                                         for (int i = 0; i < bm->totvert; i++) {
831                                                 copy_v3_v3(rdata->orco[i], BM_vert_at_index(bm, i)->co);
832                                         }
833                                         BKE_mesh_orco_verts_transform(me, rdata->orco, rdata->vert_len, 0);
834                                 }
835                                 else {
836                                         rdata->orco = MEM_mallocN(sizeof(*rdata->orco) * rdata->vert_len, "orco mesh");
837                                         MVert *mvert = rdata->mvert;
838                                         for (int a = 0; a < rdata->vert_len; a++, mvert++) {
839                                                 copy_v3_v3(rdata->orco[a], mvert->co);
840                                         }
841                                         BKE_mesh_orco_verts_transform(me, rdata->orco, rdata->vert_len, 0);
842                                 }
843                         }
844                 }
845                 else {
846                         rdata->orco = NULL;
847                 }
848
849                 /* don't access mesh directly, instead use vars taken from BMesh or Mesh */
850 #define me DONT_USE_THIS
851 #ifdef  me /* quiet warning */
852 #endif
853                 struct {
854                         uint uv_len;
855                         uint vcol_len;
856                 } cd_layers_src = {
857                         .uv_len = CustomData_number_of_layers(cd_ldata, CD_MLOOPUV),
858                         .vcol_len = CustomData_number_of_layers(cd_ldata, CD_MLOOPCOL),
859                 };
860
861                 rdata->cd.layers.uv_len = min_ii(cd_layers_src.uv_len, count_bits_i(cd_lused[CD_MLOOPUV]));
862                 rdata->cd.layers.tangent_len = count_bits_i(cd_lused[CD_TANGENT]);
863                 rdata->cd.layers.vcol_len = min_ii(cd_layers_src.vcol_len, count_bits_i(cd_lused[CD_MLOOPCOL]));
864
865                 rdata->cd.layers.uv = MEM_mallocN(sizeof(*rdata->cd.layers.uv) * rdata->cd.layers.uv_len, __func__);
866                 rdata->cd.layers.vcol = MEM_mallocN(sizeof(*rdata->cd.layers.vcol) * rdata->cd.layers.vcol_len, __func__);
867                 rdata->cd.layers.tangent = MEM_mallocN(sizeof(*rdata->cd.layers.tangent) * rdata->cd.layers.tangent_len, __func__);
868
869                 rdata->cd.uuid.uv = MEM_mallocN(sizeof(*rdata->cd.uuid.uv) * rdata->cd.layers.uv_len, __func__);
870                 rdata->cd.uuid.vcol = MEM_mallocN(sizeof(*rdata->cd.uuid.vcol) * rdata->cd.layers.vcol_len, __func__);
871                 rdata->cd.uuid.tangent = MEM_mallocN(sizeof(*rdata->cd.uuid.tangent) * rdata->cd.layers.tangent_len, __func__);
872
873                 rdata->cd.offset.uv = MEM_mallocN(sizeof(*rdata->cd.offset.uv) * rdata->cd.layers.uv_len, __func__);
874                 rdata->cd.offset.vcol = MEM_mallocN(sizeof(*rdata->cd.offset.vcol) * rdata->cd.layers.vcol_len, __func__);
875
876                 /* Allocate max */
877                 rdata->cd.layers.auto_vcol = MEM_callocN(
878                         sizeof(*rdata->cd.layers.auto_vcol) * rdata->cd.layers.vcol_len, __func__);
879                 rdata->cd.uuid.auto_mix = MEM_mallocN(
880                         sizeof(*rdata->cd.uuid.auto_mix) * (rdata->cd.layers.vcol_len + rdata->cd.layers.uv_len), __func__);
881
882                 /* XXX FIXME XXX */
883                 /* We use a hash to identify each data layer based on its name.
884                  * Gawain then search for this name in the current shader and bind if it exists.
885                  * NOTE : This is prone to hash collision.
886                  * One solution to hash collision would be to format the cd layer name
887                  * to a safe glsl var name, but without name clash.
888                  * NOTE 2 : Replicate changes to code_generate_vertex_new() in gpu_codegen.c */
889                 if (rdata->cd.layers.vcol_len != 0) {
890                         for (int i_src = 0, i_dst = 0; i_src < cd_layers_src.vcol_len; i_src++, i_dst++) {
891                                 if ((cd_lused[CD_MLOOPCOL] & (1 << i_src)) == 0) {
892                                         i_dst--;
893                                         if (rdata->cd.layers.vcol_active >= i_src) {
894                                                 rdata->cd.layers.vcol_active--;
895                                         }
896                                 }
897                                 else {
898                                         const char *name = CustomData_get_layer_name(cd_ldata, CD_MLOOPCOL, i_src);
899                                         uint hash = BLI_ghashutil_strhash_p(name);
900                                         BLI_snprintf(rdata->cd.uuid.vcol[i_dst], sizeof(*rdata->cd.uuid.vcol), "c%u", hash);
901                                         rdata->cd.layers.vcol[i_dst] = CustomData_get_layer_n(cd_ldata, CD_MLOOPCOL, i_src);
902                                         if (rdata->edit_bmesh) {
903                                                 rdata->cd.offset.vcol[i_dst] = CustomData_get_n_offset(
904                                                         &rdata->edit_bmesh->bm->ldata, CD_MLOOPCOL, i_src);
905                                         }
906
907                                         /* Gather number of auto layers. */
908                                         /* We only do vcols that are not overridden by uvs */
909                                         if (CustomData_get_named_layer_index(cd_ldata, CD_MLOOPUV, name) == -1) {
910                                                 BLI_snprintf(
911                                                         rdata->cd.uuid.auto_mix[rdata->cd.layers.uv_len + i_dst],
912                                                         sizeof(*rdata->cd.uuid.auto_mix), "a%u", hash);
913                                                 rdata->cd.layers.auto_vcol[i_dst] = true;
914                                         }
915                                 }
916                         }
917                 }
918
919                 /* Start Fresh */
920                 CustomData_free_layers(cd_ldata, CD_TANGENT, rdata->loop_len);
921                 CustomData_free_layers(cd_ldata, CD_MLOOPTANGENT, rdata->loop_len);
922
923                 if (rdata->cd.layers.uv_len != 0) {
924                         for (int i_src = 0, i_dst = 0; i_src < cd_layers_src.uv_len; i_src++, i_dst++) {
925                                 if ((cd_lused[CD_MLOOPUV] & (1 << i_src)) == 0) {
926                                         i_dst--;
927                                         if (rdata->cd.layers.uv_active >= i_src) {
928                                                 rdata->cd.layers.uv_active--;
929                                         }
930                                 }
931                                 else {
932                                         const char *name = CustomData_get_layer_name(cd_ldata, CD_MLOOPUV, i_src);
933                                         uint hash = BLI_ghashutil_strhash_p(name);
934
935                                         BLI_snprintf(rdata->cd.uuid.uv[i_dst], sizeof(*rdata->cd.uuid.uv), "u%u", hash);
936                                         rdata->cd.layers.uv[i_dst] = CustomData_get_layer_n(cd_ldata, CD_MLOOPUV, i_src);
937                                         if (rdata->edit_bmesh) {
938                                                 rdata->cd.offset.uv[i_dst] = CustomData_get_n_offset(
939                                                         &rdata->edit_bmesh->bm->ldata, CD_MLOOPUV, i_src);
940                                         }
941                                         BLI_snprintf(rdata->cd.uuid.auto_mix[i_dst], sizeof(*rdata->cd.uuid.auto_mix), "a%u", hash);
942                                 }
943                         }
944                 }
945
946                 if (rdata->cd.layers.tangent_len != 0) {
947
948                         /* -------------------------------------------------------------------- */
949                         /* Pre-calculate tangents into 'rdata->cd.output.ldata' */
950
951                         BLI_assert(!CustomData_has_layer(&rdata->cd.output.ldata, CD_TANGENT));
952
953                         /* Tangent Names */
954                         char tangent_names[MAX_MTFACE][MAX_NAME];
955                         for (int i_src = 0, i_dst = 0; i_src < cd_layers_src.uv_len; i_src++, i_dst++) {
956                                 if ((cd_lused[CD_TANGENT] & (1 << i_src)) == 0) {
957                                         i_dst--;
958                                 }
959                                 else {
960                                         BLI_strncpy(
961                                                 tangent_names[i_dst],
962                                                 CustomData_get_layer_name(cd_ldata, CD_MLOOPUV, i_src), MAX_NAME);
963                                 }
964                         }
965
966                         /* If tangent from orco is requested, decrement tangent_len */
967                         int actual_tangent_len = (cd_lused[CD_TANGENT] & DM_TANGENT_MASK_ORCO) ?
968                                 rdata->cd.layers.tangent_len - 1 : rdata->cd.layers.tangent_len;
969                         if (rdata->edit_bmesh) {
970                                 BMEditMesh *em = rdata->edit_bmesh;
971                                 BMesh *bm = em->bm;
972
973                                 if (is_auto_smooth && rdata->loop_normals == NULL) {
974                                         /* Should we store the previous array of `loop_normals` in somewhere? */
975                                         rdata->loop_len = bm->totloop;
976                                         rdata->loop_normals = MEM_mallocN(sizeof(*rdata->loop_normals) * rdata->loop_len, __func__);
977                                         BM_loops_calc_normal_vcos(bm, NULL, NULL, NULL, true, split_angle, rdata->loop_normals, NULL, NULL, -1, false);
978                                 }
979
980                                 bool calc_active_tangent = false;
981
982                                 BKE_editmesh_loop_tangent_calc(
983                                         em, calc_active_tangent,
984                                         tangent_names, actual_tangent_len,
985                                         rdata->poly_normals, rdata->loop_normals,
986                                         rdata->orco,
987                                         &rdata->cd.output.ldata, bm->totloop,
988                                         &rdata->cd.output.tangent_mask);
989                         }
990                         else {
991 #undef me
992
993                                 if (is_auto_smooth && rdata->loop_normals == NULL) {
994                                         /* Should we store the previous array of `loop_normals` in CustomData? */
995                                         mesh_render_calc_normals_loop_and_poly(me, split_angle, rdata);
996                                 }
997
998                                 bool calc_active_tangent = false;
999
1000                                 BKE_mesh_calc_loop_tangent_ex(
1001                                         me->mvert,
1002                                         me->mpoly, me->totpoly,
1003                                         me->mloop,
1004                                         rdata->mlooptri, rdata->tri_len,
1005                                         cd_ldata,
1006                                         calc_active_tangent,
1007                                         tangent_names, actual_tangent_len,
1008                                         rdata->poly_normals, rdata->loop_normals,
1009                                         rdata->orco,
1010                                         &rdata->cd.output.ldata, me->totloop,
1011                                         &rdata->cd.output.tangent_mask);
1012
1013                                 /* If we store tangents in the mesh, set temporary. */
1014 #if 0
1015                                 CustomData_set_layer_flag(cd_ldata, CD_TANGENT, CD_FLAG_TEMPORARY);
1016 #endif
1017
1018 #define me DONT_USE_THIS
1019 #ifdef  me /* quiet warning */
1020 #endif
1021                         }
1022
1023                         /* End tangent calculation */
1024                         /* -------------------------------------------------------------------- */
1025
1026                         BLI_assert(CustomData_number_of_layers(&rdata->cd.output.ldata, CD_TANGENT) == rdata->cd.layers.tangent_len);
1027
1028                         int i_dst = 0;
1029                         for (int i_src = 0; i_src < cd_layers_src.uv_len; i_src++, i_dst++) {
1030                                 if ((cd_lused[CD_TANGENT] & (1 << i_src)) == 0) {
1031                                         i_dst--;
1032                                         if (rdata->cd.layers.tangent_active >= i_src) {
1033                                                 rdata->cd.layers.tangent_active--;
1034                                         }
1035                                 }
1036                                 else {
1037                                         const char *name = CustomData_get_layer_name(cd_ldata, CD_MLOOPUV, i_src);
1038                                         uint hash = BLI_ghashutil_strhash_p(name);
1039
1040                                         BLI_snprintf(rdata->cd.uuid.tangent[i_dst], sizeof(*rdata->cd.uuid.tangent), "t%u", hash);
1041
1042                                         /* Done adding tangents. */
1043
1044                                         /* note: BKE_editmesh_loop_tangent_calc calculates 'CD_TANGENT',
1045                                          * not 'CD_MLOOPTANGENT' (as done below). It's OK, they're compatible. */
1046
1047                                         /* note: normally we'd use 'i_src' here, but 'i_dst' is in sync with 'rdata->cd.output' */
1048                                         rdata->cd.layers.tangent[i_dst] = CustomData_get_layer_n(&rdata->cd.output.ldata, CD_TANGENT, i_dst);
1049                                         if (rdata->tri_len != 0) {
1050                                                 BLI_assert(rdata->cd.layers.tangent[i_dst] != NULL);
1051                                         }
1052                                 }
1053                         }
1054                         if (cd_lused[CD_TANGENT] & DM_TANGENT_MASK_ORCO) {
1055                                 const char *name = CustomData_get_layer_name(&rdata->cd.output.ldata, CD_TANGENT, i_dst);
1056                                 uint hash = BLI_ghashutil_strhash_p(name);
1057                                 BLI_snprintf(rdata->cd.uuid.tangent[i_dst], sizeof(*rdata->cd.uuid.tangent), "t%u", hash);
1058
1059                                 rdata->cd.layers.tangent[i_dst] = CustomData_get_layer_n(&rdata->cd.output.ldata, CD_TANGENT, i_dst);
1060                         }
1061                 }
1062
1063 #undef me
1064         }
1065
1066         return rdata;
1067 }
1068
1069 /* Warning replace mesh pointer. */
1070 #define MBC_GET_FINAL_MESH(me) \
1071         /* Hack to show the final result. */ \
1072         const bool _use_em_final = ( \
1073                 (me)->edit_btmesh && \
1074                 (me)->edit_btmesh->mesh_eval_final && \
1075                 ((me)->edit_btmesh->mesh_eval_final->runtime.is_original == false)); \
1076         Mesh _me_fake; \
1077         if (_use_em_final) { \
1078                 _me_fake = *(me)->edit_btmesh->mesh_eval_final; \
1079                 _me_fake.mat = (me)->mat; \
1080                 _me_fake.totcol = (me)->totcol; \
1081                 (me) = &_me_fake; \
1082         } ((void)0)
1083
1084 static void mesh_render_data_free(MeshRenderData *rdata)
1085 {
1086         if (rdata->is_orco_allocated) {
1087                 MEM_SAFE_FREE(rdata->orco);
1088         }
1089         MEM_SAFE_FREE(rdata->cd.offset.uv);
1090         MEM_SAFE_FREE(rdata->cd.offset.vcol);
1091         MEM_SAFE_FREE(rdata->cd.uuid.auto_mix);
1092         MEM_SAFE_FREE(rdata->cd.uuid.uv);
1093         MEM_SAFE_FREE(rdata->cd.uuid.vcol);
1094         MEM_SAFE_FREE(rdata->cd.uuid.tangent);
1095         MEM_SAFE_FREE(rdata->cd.layers.uv);
1096         MEM_SAFE_FREE(rdata->cd.layers.vcol);
1097         MEM_SAFE_FREE(rdata->cd.layers.tangent);
1098         MEM_SAFE_FREE(rdata->cd.layers.auto_vcol);
1099         MEM_SAFE_FREE(rdata->loose_verts);
1100         MEM_SAFE_FREE(rdata->loose_edges);
1101         MEM_SAFE_FREE(rdata->edges_adjacent_polys);
1102         MEM_SAFE_FREE(rdata->mlooptri);
1103         MEM_SAFE_FREE(rdata->loop_normals);
1104         MEM_SAFE_FREE(rdata->poly_normals);
1105         MEM_SAFE_FREE(rdata->poly_normals_pack);
1106         MEM_SAFE_FREE(rdata->vert_normals_pack);
1107         MEM_SAFE_FREE(rdata->vert_weight);
1108         MEM_SAFE_FREE(rdata->edge_select_bool);
1109         MEM_SAFE_FREE(rdata->edge_visible_bool);
1110         MEM_SAFE_FREE(rdata->vert_color);
1111
1112         MEM_SAFE_FREE(rdata->mapped.loose_verts);
1113         MEM_SAFE_FREE(rdata->mapped.loose_edges);
1114
1115         CustomData_free(&rdata->cd.output.ldata, rdata->loop_len);
1116
1117         MEM_freeN(rdata);
1118 }
1119
1120 /** \} */
1121
1122 /* ---------------------------------------------------------------------- */
1123 /** \name Accessor Functions
1124  * \{ */
1125
1126 static const char *mesh_render_data_uv_auto_layer_uuid_get(const MeshRenderData *rdata, int layer)
1127 {
1128         BLI_assert(rdata->types & MR_DATATYPE_SHADING);
1129         return rdata->cd.uuid.auto_mix[layer];
1130 }
1131
1132 static const char *mesh_render_data_vcol_auto_layer_uuid_get(const MeshRenderData *rdata, int layer)
1133 {
1134         BLI_assert(rdata->types & MR_DATATYPE_SHADING);
1135         return rdata->cd.uuid.auto_mix[rdata->cd.layers.uv_len + layer];
1136 }
1137
1138 static const char *mesh_render_data_uv_layer_uuid_get(const MeshRenderData *rdata, int layer)
1139 {
1140         BLI_assert(rdata->types & MR_DATATYPE_SHADING);
1141         return rdata->cd.uuid.uv[layer];
1142 }
1143
1144 static const char *mesh_render_data_vcol_layer_uuid_get(const MeshRenderData *rdata, int layer)
1145 {
1146         BLI_assert(rdata->types & MR_DATATYPE_SHADING);
1147         return rdata->cd.uuid.vcol[layer];
1148 }
1149
1150 static const char *mesh_render_data_tangent_layer_uuid_get(const MeshRenderData *rdata, int layer)
1151 {
1152         BLI_assert(rdata->types & MR_DATATYPE_SHADING);
1153         return rdata->cd.uuid.tangent[layer];
1154 }
1155
1156 static int UNUSED_FUNCTION(mesh_render_data_verts_len_get)(const MeshRenderData *rdata)
1157 {
1158         BLI_assert(rdata->types & MR_DATATYPE_VERT);
1159         return rdata->vert_len;
1160 }
1161 static int mesh_render_data_verts_len_get_maybe_mapped(const MeshRenderData *rdata)
1162 {
1163         BLI_assert(rdata->types & MR_DATATYPE_VERT);
1164         return ((rdata->mapped.use == false) ? rdata->vert_len : rdata->mapped.vert_len);
1165 }
1166
1167 static int UNUSED_FUNCTION(mesh_render_data_loose_verts_len_get)(const MeshRenderData *rdata)
1168 {
1169         BLI_assert(rdata->types & MR_DATATYPE_LOOSE_VERT);
1170         return rdata->loose_vert_len;
1171 }
1172 static int mesh_render_data_loose_verts_len_get_maybe_mapped(const MeshRenderData *rdata)
1173 {
1174         BLI_assert(rdata->types & MR_DATATYPE_LOOSE_VERT);
1175         return ((rdata->mapped.use == false) ? rdata->loose_vert_len : rdata->mapped.loose_vert_len);
1176 }
1177
1178 static int UNUSED_FUNCTION(mesh_render_data_edges_len_get)(const MeshRenderData *rdata)
1179 {
1180         BLI_assert(rdata->types & MR_DATATYPE_EDGE);
1181         return rdata->edge_len;
1182 }
1183 static int mesh_render_data_edges_len_get_maybe_mapped(const MeshRenderData *rdata)
1184 {
1185         BLI_assert(rdata->types & MR_DATATYPE_EDGE);
1186         return ((rdata->mapped.use == false) ? rdata->edge_len : rdata->mapped.edge_len);
1187 }
1188
1189 static int UNUSED_FUNCTION(mesh_render_data_loose_edges_len_get)(const MeshRenderData *rdata)
1190 {
1191         BLI_assert(rdata->types & MR_DATATYPE_LOOSE_EDGE);
1192         return rdata->loose_edge_len;
1193 }
1194 static int mesh_render_data_loose_edges_len_get_maybe_mapped(const MeshRenderData *rdata)
1195 {
1196         BLI_assert(rdata->types & MR_DATATYPE_LOOSE_EDGE);
1197         return ((rdata->mapped.use == false) ? rdata->loose_edge_len : rdata->mapped.loose_edge_len);
1198 }
1199
1200 static int mesh_render_data_looptri_len_get(const MeshRenderData *rdata)
1201 {
1202         BLI_assert(rdata->types & MR_DATATYPE_LOOPTRI);
1203         return rdata->tri_len;
1204 }
1205 static int mesh_render_data_looptri_len_get_maybe_mapped(const MeshRenderData *rdata)
1206 {
1207         BLI_assert(rdata->types & MR_DATATYPE_LOOPTRI);
1208         return ((rdata->mapped.use == false) ? rdata->tri_len : rdata->mapped.tri_len);
1209 }
1210
1211 static int UNUSED_FUNCTION(mesh_render_data_mat_len_get)(const MeshRenderData *rdata)
1212 {
1213         BLI_assert(rdata->types & MR_DATATYPE_POLY);
1214         return rdata->mat_len;
1215 }
1216
1217 static int mesh_render_data_loops_len_get(const MeshRenderData *rdata)
1218 {
1219         BLI_assert(rdata->types & MR_DATATYPE_LOOP);
1220         return rdata->loop_len;
1221 }
1222
1223 static int mesh_render_data_loops_len_get_maybe_mapped(const MeshRenderData *rdata)
1224 {
1225         BLI_assert(rdata->types & MR_DATATYPE_LOOP);
1226         return ((rdata->mapped.use == false) ? rdata->loop_len : rdata->mapped.loop_len);
1227 }
1228
1229 static int mesh_render_data_polys_len_get(const MeshRenderData *rdata)
1230 {
1231         BLI_assert(rdata->types & MR_DATATYPE_POLY);
1232         return rdata->poly_len;
1233 }
1234 static int mesh_render_data_polys_len_get_maybe_mapped(const MeshRenderData *rdata)
1235 {
1236         BLI_assert(rdata->types & MR_DATATYPE_POLY);
1237         return ((rdata->mapped.use == false) ? rdata->poly_len : rdata->mapped.poly_len);
1238 }
1239
1240 /** \} */
1241
1242
1243 /* ---------------------------------------------------------------------- */
1244
1245 /* TODO remove prototype. */
1246 static void mesh_create_edit_facedots(MeshRenderData *rdata, GPUVertBuf *vbo_facedots_pos_nor_data);
1247
1248 /** \name Internal Cache (Lazy Initialization)
1249  * \{ */
1250
1251 /** Ensure #MeshRenderData.poly_normals_pack */
1252 static void mesh_render_data_ensure_poly_normals_pack(MeshRenderData *rdata)
1253 {
1254         GPUPackedNormal *pnors_pack = rdata->poly_normals_pack;
1255         if (pnors_pack == NULL) {
1256                 if (rdata->edit_bmesh) {
1257                         BMesh *bm = rdata->edit_bmesh->bm;
1258                         BMIter fiter;
1259                         BMFace *efa;
1260                         int i;
1261
1262                         pnors_pack = rdata->poly_normals_pack = MEM_mallocN(sizeof(*pnors_pack) * rdata->poly_len, __func__);
1263                         if (rdata->edit_data && rdata->edit_data->vertexCos != NULL) {
1264                                 BKE_editmesh_cache_ensure_poly_normals(rdata->edit_bmesh, rdata->edit_data);
1265                                 const float (*pnors)[3] = rdata->edit_data->polyNos;
1266                                 for (i = 0; i < bm->totface; i++) {
1267                                         pnors_pack[i] = GPU_normal_convert_i10_v3(pnors[i]);
1268                                 }
1269                         }
1270                         else {
1271                                 BM_ITER_MESH_INDEX(efa, &fiter, bm, BM_FACES_OF_MESH, i) {
1272                                         pnors_pack[i] = GPU_normal_convert_i10_v3(efa->no);
1273                                 }
1274                         }
1275                 }
1276                 else {
1277                         float (*pnors)[3] = rdata->poly_normals;
1278
1279                         if (!pnors) {
1280                                 pnors = rdata->poly_normals = MEM_mallocN(sizeof(*pnors) * rdata->poly_len, __func__);
1281                                 BKE_mesh_calc_normals_poly(
1282                                         rdata->mvert, NULL, rdata->vert_len,
1283                                         rdata->mloop, rdata->mpoly, rdata->loop_len, rdata->poly_len, pnors, true);
1284                         }
1285
1286                         pnors_pack = rdata->poly_normals_pack = MEM_mallocN(sizeof(*pnors_pack) * rdata->poly_len, __func__);
1287                         for (int i = 0; i < rdata->poly_len; i++) {
1288                                 pnors_pack[i] = GPU_normal_convert_i10_v3(pnors[i]);
1289                         }
1290                 }
1291         }
1292 }
1293
1294 /** Ensure #MeshRenderData.vert_normals_pack */
1295 static void mesh_render_data_ensure_vert_normals_pack(MeshRenderData *rdata)
1296 {
1297         GPUPackedNormal *vnors_pack = rdata->vert_normals_pack;
1298         if (vnors_pack == NULL) {
1299                 if (rdata->edit_bmesh) {
1300                         BMesh *bm = rdata->edit_bmesh->bm;
1301                         BMIter viter;
1302                         BMVert *eve;
1303                         int i;
1304
1305                         vnors_pack = rdata->vert_normals_pack = MEM_mallocN(sizeof(*vnors_pack) * rdata->vert_len, __func__);
1306                         BM_ITER_MESH_INDEX(eve, &viter, bm, BM_VERT, i) {
1307                                 vnors_pack[i] = GPU_normal_convert_i10_v3(eve->no);
1308                         }
1309                 }
1310                 else {
1311                         /* data from mesh used directly */
1312                         BLI_assert(0);
1313                 }
1314         }
1315 }
1316
1317
1318 /** Ensure #MeshRenderData.vert_color */
1319 static void UNUSED_FUNCTION(mesh_render_data_ensure_vert_color)(MeshRenderData *rdata)
1320 {
1321         char (*vcol)[3] = rdata->vert_color;
1322         if (vcol == NULL) {
1323                 if (rdata->edit_bmesh) {
1324                         BMesh *bm = rdata->edit_bmesh->bm;
1325                         const int cd_loop_color_offset = CustomData_get_offset(&bm->ldata, CD_MLOOPCOL);
1326                         if (cd_loop_color_offset == -1) {
1327                                 goto fallback;
1328                         }
1329
1330                         vcol = rdata->vert_color = MEM_mallocN(sizeof(*vcol) * rdata->loop_len, __func__);
1331
1332                         BMIter fiter;
1333                         BMFace *efa;
1334                         int i = 0;
1335
1336                         BM_ITER_MESH(efa, &fiter, bm, BM_FACES_OF_MESH) {
1337                                 BMLoop *l_iter, *l_first;
1338                                 l_iter = l_first = BM_FACE_FIRST_LOOP(efa);
1339                                 do {
1340                                         const MLoopCol *lcol = BM_ELEM_CD_GET_VOID_P(l_iter, cd_loop_color_offset);
1341                                         vcol[i][0] = lcol->r;
1342                                         vcol[i][1] = lcol->g;
1343                                         vcol[i][2] = lcol->b;
1344                                         i += 1;
1345                                 } while ((l_iter = l_iter->next) != l_first);
1346                         }
1347                         BLI_assert(i == rdata->loop_len);
1348                 }
1349                 else {
1350                         if (rdata->mloopcol == NULL) {
1351                                 goto fallback;
1352                         }
1353
1354                         vcol = rdata->vert_color = MEM_mallocN(sizeof(*vcol) * rdata->loop_len, __func__);
1355
1356                         for (int i = 0; i < rdata->loop_len; i++) {
1357                                 vcol[i][0] = rdata->mloopcol[i].r;
1358                                 vcol[i][1] = rdata->mloopcol[i].g;
1359                                 vcol[i][2] = rdata->mloopcol[i].b;
1360                         }
1361                 }
1362         }
1363         return;
1364
1365 fallback:
1366         vcol = rdata->vert_color = MEM_mallocN(sizeof(*vcol) * rdata->loop_len, __func__);
1367
1368         for (int i = 0; i < rdata->loop_len; i++) {
1369                 vcol[i][0] = 255;
1370                 vcol[i][1] = 255;
1371                 vcol[i][2] = 255;
1372         }
1373 }
1374
1375 static float evaluate_vertex_weight(const MDeformVert *dvert, const DRW_MeshWeightState *wstate)
1376 {
1377         float input = 0.0f;
1378         bool show_alert_color = false;
1379
1380         if (wstate->flags & DRW_MESH_WEIGHT_STATE_MULTIPAINT) {
1381                 /* Multi-Paint feature */
1382                 input = BKE_defvert_multipaint_collective_weight(
1383                         dvert, wstate->defgroup_len, wstate->defgroup_sel, wstate->defgroup_sel_count,
1384                         (wstate->flags & DRW_MESH_WEIGHT_STATE_AUTO_NORMALIZE) != 0);
1385
1386                 /* make it black if the selected groups have no weight on a vertex */
1387                 if (input == 0.0f) {
1388                         show_alert_color = true;
1389                 }
1390         }
1391         else {
1392                 /* default, non tricky behavior */
1393                 input = defvert_find_weight(dvert, wstate->defgroup_active);
1394
1395                 if (input == 0.0f) {
1396                         switch (wstate->alert_mode) {
1397                                 case OB_DRAW_GROUPUSER_ACTIVE:
1398                                         show_alert_color = true;
1399                                         break;
1400
1401                                 case OB_DRAW_GROUPUSER_ALL:
1402                                         show_alert_color = defvert_is_weight_zero(dvert, wstate->defgroup_len);
1403                                         break;
1404                         }
1405                 }
1406         }
1407
1408         if (show_alert_color) {
1409                 return -1.0f;
1410         }
1411         else {
1412                 CLAMP(input, 0.0f, 1.0f);
1413                 return input;
1414         }
1415 }
1416
1417 /** Ensure #MeshRenderData.vert_weight */
1418 static void mesh_render_data_ensure_vert_weight(MeshRenderData *rdata, const struct DRW_MeshWeightState *wstate)
1419 {
1420         float *vweight = rdata->vert_weight;
1421         if (vweight == NULL) {
1422                 if (wstate->defgroup_active == -1) {
1423                         goto fallback;
1424                 }
1425
1426                 if (rdata->edit_bmesh) {
1427                         BMesh *bm = rdata->edit_bmesh->bm;
1428                         const int cd_dvert_offset = CustomData_get_offset(&bm->vdata, CD_MDEFORMVERT);
1429                         if (cd_dvert_offset == -1) {
1430                                 goto fallback;
1431                         }
1432
1433                         BMIter viter;
1434                         BMVert *eve;
1435                         int i;
1436
1437                         vweight = rdata->vert_weight = MEM_mallocN(sizeof(*vweight) * rdata->vert_len, __func__);
1438                         BM_ITER_MESH_INDEX(eve, &viter, bm, BM_VERT, i) {
1439                                 const MDeformVert *dvert = BM_ELEM_CD_GET_VOID_P(eve, cd_dvert_offset);
1440                                 vweight[i] = evaluate_vertex_weight(dvert, wstate);
1441                         }
1442                 }
1443                 else {
1444                         if (rdata->dvert == NULL) {
1445                                 goto fallback;
1446                         }
1447
1448                         vweight = rdata->vert_weight = MEM_mallocN(sizeof(*vweight) * rdata->vert_len, __func__);
1449                         for (int i = 0; i < rdata->vert_len; i++) {
1450                                 vweight[i] = evaluate_vertex_weight(&rdata->dvert[i], wstate);
1451                         }
1452                 }
1453         }
1454         return;
1455
1456 fallback:
1457         vweight = rdata->vert_weight = MEM_callocN(sizeof(*vweight) * rdata->vert_len, __func__);
1458
1459         if ((wstate->defgroup_active < 0) && (wstate->defgroup_len > 0)) {
1460                 copy_vn_fl(vweight, rdata->vert_len, -2.0f);
1461         }
1462         else if (wstate->alert_mode != OB_DRAW_GROUPUSER_NONE) {
1463                 copy_vn_fl(vweight, rdata->vert_len, -1.0f);
1464         }
1465 }
1466
1467 /** \} */
1468
1469 /* ---------------------------------------------------------------------- */
1470 /** \name Internal Cache Generation
1471  * \{ */
1472
1473 static uchar mesh_render_data_face_flag(MeshRenderData *rdata, const BMFace *efa, const int cd_ofs)
1474 {
1475         uchar fflag = 0;
1476
1477         if (efa == rdata->efa_act) {
1478                 fflag |= VFLAG_FACE_ACTIVE;
1479         }
1480         if (BM_elem_flag_test(efa, BM_ELEM_SELECT)) {
1481                 fflag |= VFLAG_FACE_SELECTED;
1482         }
1483
1484         if (efa == rdata->efa_act_uv) {
1485                 fflag |= VFLAG_FACE_UV_ACTIVE;
1486         }
1487         if ((cd_ofs != -1) && uvedit_face_select_test_ex(rdata->toolsettings, (BMFace *)efa, cd_ofs)) {
1488                 fflag |= VFLAG_FACE_UV_SELECT;
1489         }
1490
1491 #ifdef WITH_FREESTYLE
1492         if (rdata->cd.offset.freestyle_face != -1) {
1493                 const FreestyleFace *ffa = BM_ELEM_CD_GET_VOID_P(efa, rdata->cd.offset.freestyle_face);
1494                 if (ffa->flag & FREESTYLE_FACE_MARK) {
1495                         fflag |= VFLAG_FACE_FREESTYLE;
1496                 }
1497         }
1498 #endif
1499
1500         return fflag;
1501 }
1502
1503 static void mesh_render_data_edge_flag(
1504         const MeshRenderData *rdata, const BMEdge *eed,
1505         EdgeDrawAttr *eattr)
1506 {
1507         const ToolSettings *ts = rdata->toolsettings;
1508         const bool is_vertex_select_mode = (ts != NULL) && (ts->selectmode & SCE_SELECT_VERTEX) != 0;
1509
1510         if (eed == rdata->eed_act) {
1511                 eattr->e_flag |= VFLAG_EDGE_ACTIVE;
1512         }
1513         if (!is_vertex_select_mode &&
1514             BM_elem_flag_test(eed, BM_ELEM_SELECT))
1515         {
1516                 eattr->e_flag |= VFLAG_EDGE_SELECTED;
1517         }
1518         if (is_vertex_select_mode &&
1519             BM_elem_flag_test(eed->v1, BM_ELEM_SELECT) &&
1520             BM_elem_flag_test(eed->v2, BM_ELEM_SELECT))
1521         {
1522                 eattr->e_flag |= VFLAG_EDGE_SELECTED;
1523                 eattr->e_flag |= VFLAG_VERT_SELECTED;
1524         }
1525         if (BM_elem_flag_test(eed, BM_ELEM_SEAM)) {
1526                 eattr->e_flag |= VFLAG_EDGE_SEAM;
1527         }
1528         if (!BM_elem_flag_test(eed, BM_ELEM_SMOOTH)) {
1529                 eattr->e_flag |= VFLAG_EDGE_SHARP;
1530         }
1531         /* Use a byte for value range */
1532         if (rdata->cd.offset.crease != -1) {
1533                 float crease = BM_ELEM_CD_GET_FLOAT(eed, rdata->cd.offset.crease);
1534                 if (crease > 0) {
1535                         eattr->crease = (uchar)(crease * 255.0f);
1536                 }
1537         }
1538         /* Use a byte for value range */
1539         if (rdata->cd.offset.bweight != -1) {
1540                 float bweight = BM_ELEM_CD_GET_FLOAT(eed, rdata->cd.offset.bweight);
1541                 if (bweight > 0) {
1542                         eattr->bweight = (uchar)(bweight * 255.0f);
1543                 }
1544         }
1545 #ifdef WITH_FREESTYLE
1546         if (rdata->cd.offset.freestyle_edge != -1) {
1547                 const FreestyleEdge *fed = BM_ELEM_CD_GET_VOID_P(eed, rdata->cd.offset.freestyle_edge);
1548                 if (fed->flag & FREESTYLE_EDGE_MARK) {
1549                         eattr->e_flag |= VFLAG_EDGE_FREESTYLE;
1550                 }
1551         }
1552 #endif
1553 }
1554
1555 static void mesh_render_data_loop_flag(MeshRenderData *rdata, BMLoop *loop, const int cd_ofs, EdgeDrawAttr *eattr)
1556 {
1557         if (cd_ofs == -1) {
1558                 return;
1559         }
1560         MLoopUV *luv = BM_ELEM_CD_GET_VOID_P(loop, cd_ofs);
1561         if (luv != NULL && (luv->flag & MLOOPUV_PINNED)) {
1562                 eattr->v_flag |= VFLAG_VERT_UV_PINNED;
1563         }
1564         if (uvedit_uv_select_test_ex(rdata->toolsettings, loop, cd_ofs)) {
1565                 eattr->v_flag |= VFLAG_VERT_UV_SELECT;
1566         }
1567         if (uvedit_edge_select_test_ex(rdata->toolsettings, loop, cd_ofs)) {
1568                 eattr->v_flag |= VFLAG_EDGE_UV_SELECT;
1569         }
1570 }
1571
1572 static void mesh_render_data_vert_flag(MeshRenderData *rdata, const BMVert *eve, EdgeDrawAttr *eattr)
1573 {
1574         if (eve == rdata->eve_act) {
1575                 eattr->e_flag |= VFLAG_VERT_ACTIVE;
1576         }
1577         if (BM_elem_flag_test(eve, BM_ELEM_SELECT)) {
1578                 eattr->e_flag |= VFLAG_VERT_SELECTED;
1579         }
1580 }
1581
1582 static bool add_edit_facedot(
1583         MeshRenderData *rdata, GPUVertBuf *vbo,
1584         const uint fdot_pos_id, const uint fdot_nor_flag_id,
1585         const int poly, const int base_vert_idx)
1586 {
1587         BLI_assert(rdata->types & (MR_DATATYPE_VERT | MR_DATATYPE_LOOP | MR_DATATYPE_POLY));
1588         float pnor[3], center[3];
1589         bool selected;
1590         if (rdata->edit_bmesh) {
1591                 const BMFace *efa = BM_face_at_index(rdata->edit_bmesh->bm, poly);
1592                 if (BM_elem_flag_test(efa, BM_ELEM_HIDDEN)) {
1593                         return false;
1594                 }
1595                 if (rdata->edit_data && rdata->edit_data->vertexCos) {
1596                         copy_v3_v3(center, rdata->edit_data->polyCos[poly]);
1597                         copy_v3_v3(pnor, rdata->edit_data->polyNos[poly]);
1598                 }
1599                 else {
1600                         BM_face_calc_center_median(efa, center);
1601                         copy_v3_v3(pnor, efa->no);
1602                 }
1603                 selected = (BM_elem_flag_test(efa, BM_ELEM_SELECT) != 0) ? true : false;
1604         }
1605         else {
1606                 MVert *mvert = rdata->mvert;
1607                 const MPoly *mpoly = rdata->mpoly + poly;
1608                 const MLoop *mloop = rdata->mloop + mpoly->loopstart;
1609
1610                 BKE_mesh_calc_poly_center(mpoly, mloop, mvert, center);
1611                 BKE_mesh_calc_poly_normal(mpoly, mloop, mvert, pnor);
1612
1613                 selected = false; /* No selection if not in edit mode */
1614         }
1615
1616         GPUPackedNormal nor = GPU_normal_convert_i10_v3(pnor);
1617         nor.w = (selected) ? 1 : 0;
1618         GPU_vertbuf_attr_set(vbo, fdot_nor_flag_id, base_vert_idx, &nor);
1619         GPU_vertbuf_attr_set(vbo, fdot_pos_id, base_vert_idx, center);
1620
1621         return true;
1622 }
1623 static bool add_edit_facedot_mapped(
1624         MeshRenderData *rdata, GPUVertBuf *vbo,
1625         const uint fdot_pos_id, const uint fdot_nor_flag_id,
1626         const int poly, const int base_vert_idx)
1627 {
1628         BLI_assert(rdata->types & (MR_DATATYPE_VERT | MR_DATATYPE_LOOP | MR_DATATYPE_POLY));
1629         float pnor[3], center[3];
1630         const int *p_origindex = rdata->mapped.p_origindex;
1631         const int p_orig = p_origindex[poly];
1632         if (p_orig == ORIGINDEX_NONE) {
1633                 return false;
1634         }
1635         BMEditMesh *em = rdata->edit_bmesh;
1636         const BMFace *efa = BM_face_at_index(rdata->edit_bmesh->bm, p_orig);
1637         if (BM_elem_flag_test(efa, BM_ELEM_HIDDEN)) {
1638                 return false;
1639         }
1640
1641         Mesh *me_cage = em->mesh_eval_cage;
1642         const MVert *mvert = me_cage->mvert;
1643         const MLoop *mloop = me_cage->mloop;
1644         const MPoly *mpoly = me_cage->mpoly;
1645
1646         const MPoly *mp = mpoly + poly;
1647         const MLoop *ml = mloop + mp->loopstart;
1648
1649         BKE_mesh_calc_poly_center(mp, ml, mvert, center);
1650         BKE_mesh_calc_poly_normal(mp, ml, mvert, pnor);
1651
1652         GPUPackedNormal nor = GPU_normal_convert_i10_v3(pnor);
1653         nor.w = (BM_elem_flag_test(efa, BM_ELEM_SELECT) != 0) ? 1 : 0;
1654         GPU_vertbuf_attr_set(vbo, fdot_nor_flag_id, base_vert_idx, &nor);
1655         GPU_vertbuf_attr_set(vbo, fdot_pos_id, base_vert_idx, center);
1656
1657         return true;
1658 }
1659
1660 /** \} */
1661
1662 /* ---------------------------------------------------------------------- */
1663 /** \name Vertex Group Selection
1664  * \{ */
1665
1666 /** Reset the selection structure, deallocating heap memory as appropriate. */
1667 static void drw_mesh_weight_state_clear(struct DRW_MeshWeightState *wstate)
1668 {
1669         MEM_SAFE_FREE(wstate->defgroup_sel);
1670
1671         memset(wstate, 0, sizeof(*wstate));
1672
1673         wstate->defgroup_active = -1;
1674 }
1675
1676 /** Copy selection data from one structure to another, including heap memory. */
1677 static void drw_mesh_weight_state_copy(
1678         struct DRW_MeshWeightState *wstate_dst, const struct DRW_MeshWeightState *wstate_src)
1679 {
1680         MEM_SAFE_FREE(wstate_dst->defgroup_sel);
1681
1682         memcpy(wstate_dst, wstate_src, sizeof(*wstate_dst));
1683
1684         if (wstate_src->defgroup_sel) {
1685                 wstate_dst->defgroup_sel = MEM_dupallocN(wstate_src->defgroup_sel);
1686         }
1687 }
1688
1689 /** Compare two selection structures. */
1690 static bool drw_mesh_weight_state_compare(const struct DRW_MeshWeightState *a, const struct DRW_MeshWeightState *b)
1691 {
1692         return a->defgroup_active == b->defgroup_active &&
1693                a->defgroup_len == b->defgroup_len &&
1694                a->flags == b->flags &&
1695                a->alert_mode == b->alert_mode &&
1696                a->defgroup_sel_count == b->defgroup_sel_count &&
1697                ((!a->defgroup_sel && !b->defgroup_sel) ||
1698                 (a->defgroup_sel && b->defgroup_sel &&
1699                  memcmp(a->defgroup_sel, b->defgroup_sel, a->defgroup_len * sizeof(bool)) == 0));
1700 }
1701
1702 static void drw_mesh_weight_state_extract(
1703         Object *ob, Mesh *me, const ToolSettings *ts, bool paint_mode,
1704         struct DRW_MeshWeightState *wstate)
1705 {
1706         /* Extract complete vertex weight group selection state and mode flags. */
1707         memset(wstate, 0, sizeof(*wstate));
1708
1709         wstate->defgroup_active = ob->actdef - 1;
1710         wstate->defgroup_len = BLI_listbase_count(&ob->defbase);
1711
1712         wstate->alert_mode = ts->weightuser;
1713
1714         if (paint_mode && ts->multipaint) {
1715                 /* Multipaint needs to know all selected bones, not just the active group.
1716                  * This is actually a relatively expensive operation, but caching would be difficult. */
1717                 wstate->defgroup_sel = BKE_object_defgroup_selected_get(ob, wstate->defgroup_len, &wstate->defgroup_sel_count);
1718
1719                 if (wstate->defgroup_sel_count > 1) {
1720                         wstate->flags |= DRW_MESH_WEIGHT_STATE_MULTIPAINT | (ts->auto_normalize ? DRW_MESH_WEIGHT_STATE_AUTO_NORMALIZE : 0);
1721
1722                         if (me->editflag & ME_EDIT_MIRROR_X) {
1723                                 BKE_object_defgroup_mirror_selection(
1724                                         ob, wstate->defgroup_len, wstate->defgroup_sel, wstate->defgroup_sel, &wstate->defgroup_sel_count);
1725                         }
1726                 }
1727                 /* With only one selected bone Multipaint reverts to regular mode. */
1728                 else {
1729                         wstate->defgroup_sel_count = 0;
1730                         MEM_SAFE_FREE(wstate->defgroup_sel);
1731                 }
1732         }
1733 }
1734
1735 /** \} */
1736
1737 /* ---------------------------------------------------------------------- */
1738 /** \name Mesh GPUBatch Cache
1739  * \{ */
1740
1741 typedef struct MeshBatchCache {
1742         /* In order buffers: All verts only specified once.
1743          * To be used with a GPUIndexBuf. */
1744         struct {
1745                 /* Vertex data. */
1746                 GPUVertBuf *pos_nor;
1747                 GPUVertBuf *weights;
1748                 /* Loop data. */
1749                 GPUVertBuf *loop_pos_nor;
1750                 GPUVertBuf *loop_uv_tan;
1751                 GPUVertBuf *loop_vcol;
1752         } ordered;
1753
1754         /* Tesselated: (all verts specified for each triangles).
1755          * Indices does not match the CPU data structure's. */
1756         struct {
1757                 GPUVertBuf *pos_nor;
1758                 GPUVertBuf *wireframe_data;
1759         } tess;
1760
1761         /* Edit Mesh Data:
1762          * Edit cage can be different from final mesh so vertex count
1763          * might differ. */
1764         struct {
1765                 /* TODO(fclem): Reuse ordered.loop_pos_nor and maybe even
1766                  * ordered.loop_uv_tan when cage match final mesh. */
1767                 GPUVertBuf *loop_pos_nor;
1768                 GPUVertBuf *loop_data;
1769                 GPUVertBuf *loop_lnor;
1770                 GPUVertBuf *facedots_pos_nor_data;
1771                 /* UV data without modifier applied.
1772                  * Vertex count is always the one of the cage. */
1773                 GPUVertBuf *loop_uv;
1774                 GPUVertBuf *loop_uv_data;
1775                 GPUVertBuf *loop_stretch_angle;
1776                 GPUVertBuf *loop_stretch_area;
1777                 GPUVertBuf *facedots_uv;
1778                 GPUVertBuf *facedots_uv_data;
1779                 /* Selection */
1780                 GPUVertBuf *loop_vert_idx;
1781                 GPUVertBuf *loop_edge_idx;
1782                 GPUVertBuf *loop_face_idx;
1783                 GPUVertBuf *facedots_idx;
1784         } edit;
1785
1786         /* Index Buffers:
1787          * Only need to be updated when topology changes. */
1788         struct {
1789                 /* Indices to verts. */
1790                 GPUIndexBuf *surf_tris;
1791                 GPUIndexBuf *edges_lines;
1792                 GPUIndexBuf *edges_adj_lines;
1793                 GPUIndexBuf *loose_edges_lines;
1794                 /* Indices to vloops. */
1795                 GPUIndexBuf *loops_tris;
1796                 GPUIndexBuf *loops_lines;
1797                 /* Edit mode. */
1798                 GPUIndexBuf *edit_loops_points; /* verts */
1799                 GPUIndexBuf *edit_loops_lines; /* edges */
1800                 GPUIndexBuf *edit_loops_tris; /* faces */
1801                 /* Edit UVs */
1802                 GPUIndexBuf *edituv_loops_points; /* verts */
1803                 GPUIndexBuf *edituv_loops_lines; /* edges */
1804                 GPUIndexBuf *edituv_loops_tri_fans; /* faces */
1805         } ibo;
1806
1807         struct {
1808                 /* Surfaces / Render */
1809                 GPUBatch *surface;
1810                 GPUBatch *surface_weights;
1811                 /* Edit mode */
1812                 GPUBatch *edit_triangles;
1813                 GPUBatch *edit_vertices;
1814                 GPUBatch *edit_edges;
1815                 GPUBatch *edit_lnor;
1816                 GPUBatch *edit_facedots;
1817                 /* Edit UVs */
1818                 GPUBatch *edituv_faces_strech_area;
1819                 GPUBatch *edituv_faces_strech_angle;
1820                 GPUBatch *edituv_faces;
1821                 GPUBatch *edituv_edges;
1822                 GPUBatch *edituv_verts;
1823                 GPUBatch *edituv_facedots;
1824                 /* Edit selection */
1825                 GPUBatch *edit_selection_verts;
1826                 GPUBatch *edit_selection_edges;
1827                 GPUBatch *edit_selection_faces;
1828                 GPUBatch *edit_selection_facedots;
1829                 /* Common display / Other */
1830                 GPUBatch *all_verts;
1831                 GPUBatch *all_edges;
1832                 GPUBatch *loose_edges;
1833                 GPUBatch *edge_detection;
1834                 GPUBatch *wire_loops; /* Loops around faces. */
1835                 GPUBatch *wire_loops_uvs; /* Same as wire_loops but only has uvs. */
1836                 GPUBatch *wire_triangles; /* Triangles for object mode wireframe. */
1837         } batch;
1838
1839         GPUIndexBuf **surf_per_mat_tris;
1840         GPUBatch **surf_per_mat;
1841
1842         /* arrays of bool uniform names (and value) that will be use to
1843          * set srgb conversion for auto attributes.*/
1844         char *auto_layer_names;
1845         int *auto_layer_is_srgb;
1846         int auto_layer_len;
1847
1848         /* settings to determine if cache is invalid */
1849         bool is_maybe_dirty;
1850         bool is_dirty; /* Instantly invalidates cache, skipping mesh check */
1851         int edge_len;
1852         int tri_len;
1853         int poly_len;
1854         int vert_len;
1855         int mat_len;
1856         bool is_editmode;
1857         bool is_uvsyncsel;
1858
1859         struct DRW_MeshWeightState weight_state;
1860
1861         uchar cd_vused[CD_NUMTYPES];
1862         uchar cd_vneeded[CD_NUMTYPES];
1863         ushort cd_lused[CD_NUMTYPES];
1864         ushort cd_lneeded[CD_NUMTYPES];
1865
1866         /* XXX, only keep for as long as sculpt mode uses shaded drawing. */
1867         bool is_sculpt_points_tag;
1868
1869         /* Valid only if edge_detection is up to date. */
1870         bool is_manifold;
1871 } MeshBatchCache;
1872
1873 /* GPUBatch cache management. */
1874
1875 static bool mesh_batch_cache_valid(Mesh *me)
1876 {
1877         MeshBatchCache *cache = me->runtime.batch_cache;
1878
1879         if (cache == NULL) {
1880                 return false;
1881         }
1882
1883         if (cache->mat_len != mesh_render_mat_len_get(me)) {
1884                 return false;
1885         }
1886
1887         if (cache->is_editmode != (me->edit_btmesh != NULL)) {
1888                 return false;
1889         }
1890
1891         if (cache->is_dirty) {
1892                 return false;
1893         }
1894
1895         if (cache->is_maybe_dirty == false) {
1896                 return true;
1897         }
1898         else {
1899                 if (cache->is_editmode) {
1900                         return false;
1901                 }
1902                 else if ((cache->vert_len != mesh_render_verts_len_get(me)) ||
1903                          (cache->edge_len != mesh_render_edges_len_get(me)) ||
1904                          (cache->tri_len  != mesh_render_looptri_len_get(me)) ||
1905                          (cache->poly_len != mesh_render_polys_len_get(me)) ||
1906                          (cache->mat_len   != mesh_render_mat_len_get(me)))
1907                 {
1908                         return false;
1909                 }
1910         }
1911
1912         return true;
1913 }
1914
1915 static void mesh_batch_cache_init(Mesh *me)
1916 {
1917         MeshBatchCache *cache = me->runtime.batch_cache;
1918
1919         if (!cache) {
1920                 cache = me->runtime.batch_cache = MEM_callocN(sizeof(*cache), __func__);
1921         }
1922         else {
1923                 memset(cache, 0, sizeof(*cache));
1924         }
1925
1926         cache->is_editmode = me->edit_btmesh != NULL;
1927
1928         if (cache->is_editmode == false) {
1929                 cache->edge_len = mesh_render_edges_len_get(me);
1930                 cache->tri_len = mesh_render_looptri_len_get(me);
1931                 cache->poly_len = mesh_render_polys_len_get(me);
1932                 cache->vert_len = mesh_render_verts_len_get(me);
1933         }
1934
1935         cache->mat_len = mesh_render_mat_len_get(me);
1936         cache->surf_per_mat_tris = MEM_callocN(sizeof(*cache->surf_per_mat_tris) * cache->mat_len, __func__);
1937         cache->surf_per_mat = MEM_callocN(sizeof(*cache->surf_per_mat) * cache->mat_len, __func__);
1938
1939         cache->is_maybe_dirty = false;
1940         cache->is_dirty = false;
1941
1942         drw_mesh_weight_state_clear(&cache->weight_state);
1943 }
1944
1945 static MeshBatchCache *mesh_batch_cache_get(Mesh *me)
1946 {
1947         if (!mesh_batch_cache_valid(me)) {
1948                 mesh_batch_cache_clear(me);
1949                 mesh_batch_cache_init(me);
1950         }
1951         return me->runtime.batch_cache;
1952 }
1953
1954 static void mesh_batch_cache_check_vertex_group(MeshBatchCache *cache, const struct DRW_MeshWeightState *wstate)
1955 {
1956         if (!drw_mesh_weight_state_compare(&cache->weight_state, wstate)) {
1957                 GPU_BATCH_CLEAR_SAFE(cache->batch.surface_weights);
1958                 GPU_VERTBUF_DISCARD_SAFE(cache->ordered.weights);
1959
1960                 drw_mesh_weight_state_clear(&cache->weight_state);
1961         }
1962 }
1963
1964 static void mesh_batch_cache_discard_shaded_tri(MeshBatchCache *cache)
1965 {
1966         GPU_VERTBUF_DISCARD_SAFE(cache->ordered.loop_pos_nor);
1967         GPU_VERTBUF_DISCARD_SAFE(cache->ordered.loop_uv_tan);
1968         GPU_VERTBUF_DISCARD_SAFE(cache->ordered.loop_vcol);
1969         /* TODO */
1970         // GPU_VERTBUF_DISCARD_SAFE(cache->ordered.loop_orco);
1971
1972         if (cache->surf_per_mat_tris) {
1973                 for (int i = 0; i < cache->mat_len; i++) {
1974                         GPU_INDEXBUF_DISCARD_SAFE(cache->surf_per_mat_tris[i]);
1975                 }
1976         }
1977         MEM_SAFE_FREE(cache->surf_per_mat_tris);
1978         if (cache->surf_per_mat) {
1979                 for (int i = 0; i < cache->mat_len; i++) {
1980                         GPU_BATCH_DISCARD_SAFE(cache->surf_per_mat[i]);
1981                 }
1982         }
1983         MEM_SAFE_FREE(cache->surf_per_mat);
1984
1985         MEM_SAFE_FREE(cache->auto_layer_names);
1986         MEM_SAFE_FREE(cache->auto_layer_is_srgb);
1987
1988         cache->mat_len = 0;
1989 }
1990
1991 static void mesh_batch_cache_discard_uvedit(MeshBatchCache *cache)
1992 {
1993         GPU_VERTBUF_DISCARD_SAFE(cache->edit.loop_stretch_angle);
1994         GPU_VERTBUF_DISCARD_SAFE(cache->edit.loop_stretch_area);
1995         GPU_VERTBUF_DISCARD_SAFE(cache->edit.loop_uv);
1996         GPU_VERTBUF_DISCARD_SAFE(cache->edit.loop_uv_data);
1997         GPU_VERTBUF_DISCARD_SAFE(cache->edit.facedots_uv);
1998         GPU_VERTBUF_DISCARD_SAFE(cache->edit.facedots_uv_data);
1999         GPU_INDEXBUF_DISCARD_SAFE(cache->ibo.edituv_loops_tri_fans);
2000         GPU_INDEXBUF_DISCARD_SAFE(cache->ibo.edituv_loops_lines);
2001         GPU_INDEXBUF_DISCARD_SAFE(cache->ibo.edituv_loops_points);
2002         GPU_BATCH_DISCARD_SAFE(cache->batch.edituv_faces_strech_area);
2003         GPU_BATCH_DISCARD_SAFE(cache->batch.edituv_faces_strech_angle);
2004         GPU_BATCH_DISCARD_SAFE(cache->batch.edituv_faces);
2005         GPU_BATCH_DISCARD_SAFE(cache->batch.edituv_edges);
2006         GPU_BATCH_DISCARD_SAFE(cache->batch.edituv_verts);
2007         GPU_BATCH_DISCARD_SAFE(cache->batch.edituv_facedots);
2008 }
2009
2010 void DRW_mesh_batch_cache_dirty_tag(Mesh *me, int mode)
2011 {
2012         MeshBatchCache *cache = me->runtime.batch_cache;
2013         if (cache == NULL) {
2014                 return;
2015         }
2016         switch (mode) {
2017                 case BKE_MESH_BATCH_DIRTY_MAYBE_ALL:
2018                         cache->is_maybe_dirty = true;
2019                         break;
2020                 case BKE_MESH_BATCH_DIRTY_SELECT:
2021                         GPU_VERTBUF_DISCARD_SAFE(cache->edit.loop_data);
2022                         GPU_VERTBUF_DISCARD_SAFE(cache->edit.facedots_pos_nor_data);
2023                         GPU_BATCH_DISCARD_SAFE(cache->batch.edit_triangles);
2024                         GPU_BATCH_DISCARD_SAFE(cache->batch.edit_vertices);
2025                         GPU_BATCH_DISCARD_SAFE(cache->batch.edit_edges);
2026                         GPU_BATCH_DISCARD_SAFE(cache->batch.edit_facedots);
2027                         /* Paint mode selection */
2028                         /* TODO only do that in paint mode. */
2029                         GPU_VERTBUF_DISCARD_SAFE(cache->ordered.loop_pos_nor);
2030                         GPU_BATCH_DISCARD_SAFE(cache->batch.surface);
2031                         GPU_BATCH_DISCARD_SAFE(cache->batch.wire_loops);
2032                         if (cache->surf_per_mat) {
2033                                 for (int i = 0; i < cache->mat_len; i++) {
2034                                         GPU_BATCH_DISCARD_SAFE(cache->surf_per_mat[i]);
2035                                 }
2036                         }
2037                         /* Because visible UVs depends on edit mode selection, discard everything. */
2038                         mesh_batch_cache_discard_uvedit(cache);
2039                         break;
2040                 case BKE_MESH_BATCH_DIRTY_ALL:
2041                         cache->is_dirty = true;
2042                         break;
2043                 case BKE_MESH_BATCH_DIRTY_SHADING:
2044                         mesh_batch_cache_discard_shaded_tri(cache);
2045                         mesh_batch_cache_discard_uvedit(cache);
2046                         break;
2047                 case BKE_MESH_BATCH_DIRTY_SCULPT_COORDS:
2048                         cache->is_sculpt_points_tag = true;
2049                         break;
2050                 case BKE_MESH_BATCH_DIRTY_UVEDIT_ALL:
2051                         mesh_batch_cache_discard_uvedit(cache);
2052                         break;
2053                 case BKE_MESH_BATCH_DIRTY_UVEDIT_SELECT:
2054                         GPU_VERTBUF_DISCARD_SAFE(cache->edit.loop_uv_data);
2055                         GPU_VERTBUF_DISCARD_SAFE(cache->edit.facedots_uv_data);
2056                         GPU_BATCH_DISCARD_SAFE(cache->batch.edituv_faces_strech_area);
2057                         GPU_BATCH_DISCARD_SAFE(cache->batch.edituv_faces_strech_angle);
2058                         GPU_BATCH_DISCARD_SAFE(cache->batch.edituv_faces);
2059                         GPU_BATCH_DISCARD_SAFE(cache->batch.edituv_edges);
2060                         GPU_BATCH_DISCARD_SAFE(cache->batch.edituv_verts);
2061                         GPU_BATCH_DISCARD_SAFE(cache->batch.edituv_facedots);
2062                         break;
2063                 default:
2064                         BLI_assert(0);
2065         }
2066 }
2067
2068 static void mesh_batch_cache_clear(Mesh *me)
2069 {
2070         MeshBatchCache *cache = me->runtime.batch_cache;
2071         if (!cache) {
2072                 return;
2073         }
2074
2075         for (int i = 0; i < sizeof(cache->ordered) / sizeof(void *); ++i) {
2076                 GPUVertBuf **vbo = (GPUVertBuf **)&cache->ordered;
2077                 GPU_VERTBUF_DISCARD_SAFE(vbo[i]);
2078         }
2079         for (int i = 0; i < sizeof(cache->tess) / sizeof(void *); ++i) {
2080                 GPUVertBuf **vbo = (GPUVertBuf **)&cache->tess;
2081                 GPU_VERTBUF_DISCARD_SAFE(vbo[i]);
2082         }
2083         for (int i = 0; i < sizeof(cache->edit) / sizeof(void *); ++i) {
2084                 GPUVertBuf **vbo = (GPUVertBuf **)&cache->edit;
2085                 GPU_VERTBUF_DISCARD_SAFE(vbo[i]);
2086         }
2087         for (int i = 0; i < sizeof(cache->ibo) / sizeof(void *); ++i) {
2088                 GPUIndexBuf **ibo = (GPUIndexBuf **)&cache->ibo;
2089                 GPU_INDEXBUF_DISCARD_SAFE(ibo[i]);
2090         }
2091         for (int i = 0; i < sizeof(cache->batch) / sizeof(void *); ++i) {
2092                 GPUBatch **batch = (GPUBatch **)&cache->batch;
2093                 GPU_BATCH_DISCARD_SAFE(batch[i]);
2094         }
2095
2096         mesh_batch_cache_discard_shaded_tri(cache);
2097
2098         mesh_batch_cache_discard_uvedit(cache);
2099
2100         drw_mesh_weight_state_clear(&cache->weight_state);
2101 }
2102
2103 void DRW_mesh_batch_cache_free(Mesh *me)
2104 {
2105         mesh_batch_cache_clear(me);
2106         MEM_SAFE_FREE(me->runtime.batch_cache);
2107 }
2108
2109 /* GPUBatch cache usage. */
2110
2111 static void mesh_create_pos_and_nor_tess(MeshRenderData *rdata, GPUVertBuf *vbo, bool use_hide)
2112 {
2113         static GPUVertFormat format = { 0 };
2114         static struct { uint pos, nor; } attr_id;
2115         if (format.attr_len == 0) {
2116                 attr_id.pos = GPU_vertformat_attr_add(&format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
2117                 attr_id.nor = GPU_vertformat_attr_add(&format, "nor", GPU_COMP_I10, 3, GPU_FETCH_INT_TO_FLOAT_UNIT);
2118                 GPU_vertformat_triple_load(&format);
2119         }
2120
2121         GPU_vertbuf_init_with_format(vbo, &format);
2122
2123         const int tri_len = mesh_render_data_looptri_len_get_maybe_mapped(rdata);
2124         const int vbo_len_capacity = tri_len * 3;
2125         int vbo_len_used = 0;
2126         GPU_vertbuf_data_alloc(vbo, vbo_len_capacity);
2127
2128         GPUVertBufRaw pos_step, nor_step;
2129         GPU_vertbuf_attr_get_raw_data(vbo, attr_id.pos, &pos_step);
2130         GPU_vertbuf_attr_get_raw_data(vbo, attr_id.nor, &nor_step);
2131
2132         if (rdata->mapped.use == false) {
2133                 float (*lnors)[3] = rdata->loop_normals;
2134                 if (rdata->edit_bmesh) {
2135                         GPUPackedNormal *pnors_pack, *vnors_pack;
2136
2137                         if (lnors == NULL) {
2138                                 mesh_render_data_ensure_poly_normals_pack(rdata);
2139                                 mesh_render_data_ensure_vert_normals_pack(rdata);
2140
2141                                 pnors_pack = rdata->poly_normals_pack;
2142                                 vnors_pack = rdata->vert_normals_pack;
2143                         }
2144
2145                         for (int i = 0; i < tri_len; i++) {
2146                                 const BMLoop **bm_looptri = (const BMLoop **)rdata->edit_bmesh->looptris[i];
2147                                 const BMFace *bm_face = bm_looptri[0]->f;
2148
2149                                 /* use_hide always for edit-mode */
2150                                 if (BM_elem_flag_test(bm_face, BM_ELEM_HIDDEN)) {
2151                                         continue;
2152                                 }
2153
2154                                 if (lnors) {
2155                                         for (uint t = 0; t < 3; t++) {
2156                                                 const float *nor = lnors[BM_elem_index_get(bm_looptri[t])];
2157                                                 *((GPUPackedNormal *)GPU_vertbuf_raw_step(&nor_step)) = GPU_normal_convert_i10_v3(nor);
2158                                         }
2159                                 }
2160                                 else if (BM_elem_flag_test(bm_face, BM_ELEM_SMOOTH)) {
2161                                         for (uint t = 0; t < 3; t++) {
2162                                                 *((GPUPackedNormal *)GPU_vertbuf_raw_step(&nor_step)) = vnors_pack[BM_elem_index_get(bm_looptri[t]->v)];
2163                                         }
2164                                 }
2165                                 else {
2166                                         const GPUPackedNormal *snor_pack = &pnors_pack[BM_elem_index_get(bm_face)];
2167                                         for (uint t = 0; t < 3; t++) {
2168                                                 *((GPUPackedNormal *)GPU_vertbuf_raw_step(&nor_step)) = *snor_pack;
2169                                         }
2170                                 }
2171
2172                                 /* TODO(sybren): deduplicate this and all the other places it's pasted to in this file. */
2173                                 if (rdata->edit_data && rdata->edit_data->vertexCos) {
2174                                         for (uint t = 0; t < 3; t++) {
2175                                                 int vidx = BM_elem_index_get(bm_looptri[t]->v);
2176                                                 const float *pos = rdata->edit_data->vertexCos[vidx];
2177                                                 copy_v3_v3(GPU_vertbuf_raw_step(&pos_step), pos);
2178                                         }
2179                                 }
2180                                 else {
2181                                         for (uint t = 0; t < 3; t++) {
2182                                                 copy_v3_v3(GPU_vertbuf_raw_step(&pos_step), bm_looptri[t]->v->co);
2183                                         }
2184                                 }
2185                         }
2186                 }
2187                 else {
2188                         if (lnors == NULL) {
2189                                 /* Use normals from vertex. */
2190                                 mesh_render_data_ensure_poly_normals_pack(rdata);
2191                         }
2192
2193                         for (int i = 0; i < tri_len; i++) {
2194                                 const MLoopTri *mlt = &rdata->mlooptri[i];
2195                                 const MPoly *mp = &rdata->mpoly[mlt->poly];
2196
2197                                 if (use_hide && (mp->flag & ME_HIDE)) {
2198                                         continue;
2199                                 }
2200
2201                                 const uint vtri[3] = {
2202                                         rdata->mloop[mlt->tri[0]].v,
2203                                         rdata->mloop[mlt->tri[1]].v,
2204                                         rdata->mloop[mlt->tri[2]].v,
2205                                 };
2206
2207                                 if (lnors) {
2208                                         for (uint t = 0; t < 3; t++) {
2209                                                 const float *nor = lnors[mlt->tri[t]];
2210                                                 *((GPUPackedNormal *)GPU_vertbuf_raw_step(&nor_step)) = GPU_normal_convert_i10_v3(nor);
2211                                         }
2212                                 }
2213                                 else if (mp->flag & ME_SMOOTH) {
2214                                         for (uint t = 0; t < 3; t++) {
2215                                                 const MVert *mv = &rdata->mvert[vtri[t]];
2216                                                 *((GPUPackedNormal *)GPU_vertbuf_raw_step(&nor_step)) = GPU_normal_convert_i10_s3(mv->no);
2217                                         }
2218                                 }
2219                                 else {
2220                                         const GPUPackedNormal *pnors_pack = &rdata->poly_normals_pack[mlt->poly];
2221                                         for (uint t = 0; t < 3; t++) {
2222                                                 *((GPUPackedNormal *)GPU_vertbuf_raw_step(&nor_step)) = *pnors_pack;
2223                                         }
2224                                 }
2225
2226                                 for (uint t = 0; t < 3; t++) {
2227                                         const MVert *mv = &rdata->mvert[vtri[t]];
2228                                         copy_v3_v3(GPU_vertbuf_raw_step(&pos_step), mv->co);
2229                                 }
2230                         }
2231                 }
2232         }
2233         else {
2234                 /* Note: mapped doesn't support lnors yet. */
2235                 BMesh *bm = rdata->edit_bmesh->bm;
2236                 Mesh *me_cage = rdata->mapped.me_cage;
2237
2238                 /* TODO(campbell): unlike non-mapped modes we don't generate these on demand, just use if they exist.
2239                  * this seems like a low priority TODO since mapped meshes typically
2240                  * use the final mesh evaluated mesh for showing faces. */
2241                 const float (*lnors)[3] = CustomData_get_layer(&me_cage->ldata, CD_NORMAL);
2242
2243                 /* TODO(campbell): this is quite an expensive operation for something
2244                  * that's not used unless 'normal' display option is enabled. */
2245                 if (!CustomData_has_layer(&me_cage->pdata, CD_NORMAL)) {
2246                         /* TODO(campbell): this is quite an expensive operation for something
2247                          * that's not used unless 'normal' display option is enabled. */
2248                         BKE_mesh_ensure_normals_for_display(me_cage);
2249                 }
2250                 const float (*polynors)[3] = CustomData_get_layer(&me_cage->pdata, CD_NORMAL);
2251
2252                 const MVert *mvert = rdata->mapped.me_cage->mvert;
2253                 const MLoop *mloop = rdata->mapped.me_cage->mloop;
2254                 const MPoly *mpoly = rdata->mapped.me_cage->mpoly;
2255
2256                 const MLoopTri *mlooptri = BKE_mesh_runtime_looptri_ensure(me_cage);
2257                 for (int i = 0; i < tri_len; i++) {
2258                         const MLoopTri *mlt = &mlooptri[i];
2259                         const int p_orig = rdata->mapped.p_origindex[mlt->poly];
2260                         if (p_orig != ORIGINDEX_NONE) {
2261                                 /* Assume 'use_hide' */
2262                                 BMFace *efa = BM_face_at_index(bm, p_orig);
2263                                 if (!BM_elem_flag_test(efa, BM_ELEM_HIDDEN)) {
2264                                         const MPoly *mp = &mpoly[mlt->poly];
2265                                         const uint vtri[3] = {
2266                                                 mloop[mlt->tri[0]].v,
2267                                                 mloop[mlt->tri[1]].v,
2268                                                 mloop[mlt->tri[2]].v,
2269                                         };
2270
2271                                         if (lnors) {
2272                                                 for (uint t = 0; t < 3; t++) {
2273                                                         const float *nor = lnors[mlt->tri[t]];
2274                                                         *((GPUPackedNormal *)GPU_vertbuf_raw_step(&nor_step)) = GPU_normal_convert_i10_v3(nor);
2275                                                 }
2276                                         }
2277                                         else if (mp->flag & ME_SMOOTH) {
2278                                                 for (uint t = 0; t < 3; t++) {
2279                                                         const MVert *mv = &mvert[vtri[t]];
2280                                                         *((GPUPackedNormal *)GPU_vertbuf_raw_step(&nor_step)) = GPU_normal_convert_i10_s3(mv->no);
2281                                                 }
2282                                         }
2283                                         else {
2284                                                 /* we don't have cached 'rdata->poly_normals_pack'. */
2285                                                 const GPUPackedNormal pnor = GPU_normal_convert_i10_v3(polynors[mlt->poly]);
2286                                                 for (uint t = 0; t < 3; t++) {
2287                                                         *((GPUPackedNormal *)GPU_vertbuf_raw_step(&nor_step)) = pnor;
2288                                                 }
2289                                         }
2290
2291                                         for (uint t = 0; t < 3; t++) {
2292                                                 const MVert *mv = &mvert[vtri[t]];
2293                                                 copy_v3_v3(GPU_vertbuf_raw_step(&pos_step), mv->co);
2294                                         }
2295                                 }
2296                         }
2297                 }
2298         }
2299
2300         vbo_len_used = GPU_vertbuf_raw_used(&pos_step);
2301         BLI_assert(vbo_len_used == GPU_vertbuf_raw_used(&nor_step));
2302
2303         if (vbo_len_capacity != vbo_len_used) {
2304                 GPU_vertbuf_data_resize(vbo, vbo_len_used);
2305         }
2306 }
2307
2308 BLI_INLINE void mesh_edit_add_select_index(GPUVertBufRaw *buf, GPUVertCompType comp, uint index)
2309 {
2310         /* We don't need to sanitize the value because the elements
2311          * with these undefined values will never get drawn. */
2312         switch (comp) {
2313                 case GPU_COMP_U32:
2314                         *((uint *)GPU_vertbuf_raw_step(buf)) = index;
2315                         break;
2316                 case GPU_COMP_U16:
2317                         *((ushort *)GPU_vertbuf_raw_step(buf)) = (ushort)index;
2318                         break;
2319                 case GPU_COMP_U8:
2320                         *((uchar *)GPU_vertbuf_raw_step(buf)) = (uchar)index;
2321                         break;
2322                 default:
2323                         BLI_assert(0);
2324                         break;
2325         }
2326 }
2327
2328 #define SELECT_COMP_FORMAT(el_len) (el_len > 0xFF ? (el_len > 0xFFFF ? GPU_COMP_U32 : GPU_COMP_U16) : GPU_COMP_U8)
2329
2330 static void mesh_create_edit_vertex_loops(
2331         MeshRenderData *rdata,
2332         GPUVertBuf *vbo_pos_nor,
2333         GPUVertBuf *vbo_lnor,
2334         GPUVertBuf *vbo_uv,
2335         GPUVertBuf *vbo_data,
2336         GPUVertBuf *vbo_verts,
2337         GPUVertBuf *vbo_edges,
2338         GPUVertBuf *vbo_faces)
2339 {
2340         const int vert_len = mesh_render_data_verts_len_get_maybe_mapped(rdata);
2341         const int edge_len = mesh_render_data_edges_len_get_maybe_mapped(rdata);
2342         const int poly_len = mesh_render_data_polys_len_get_maybe_mapped(rdata);
2343         const int lvert_len = mesh_render_data_loose_verts_len_get_maybe_mapped(rdata);
2344         const int ledge_len = mesh_render_data_loose_edges_len_get_maybe_mapped(rdata);
2345         const int loop_len = mesh_render_data_loops_len_get_maybe_mapped(rdata);
2346         const int tot_loop_len = loop_len + ledge_len * 2 + lvert_len;
2347         static struct { uint vert, edge, face, pos, nor, lnor, data, uvs; } attr_id;
2348         float (*lnors)[3] = rdata->loop_normals;
2349         uchar fflag;
2350
2351         /* Choose the most compact vertex format. */
2352         GPUVertCompType vert_comp = SELECT_COMP_FORMAT(vert_len);
2353         GPUVertCompType edge_comp = SELECT_COMP_FORMAT(edge_len);
2354         GPUVertCompType face_comp = SELECT_COMP_FORMAT(poly_len);
2355
2356         GPUVertFormat format_vert_idx = { 0 }, format_edge_idx = { 0 }, format_face_idx = { 0 };
2357         attr_id.vert = GPU_vertformat_attr_add(&format_vert_idx, "color", vert_comp, 1, GPU_FETCH_INT);
2358         attr_id.edge = GPU_vertformat_attr_add(&format_edge_idx, "color", edge_comp, 1, GPU_FETCH_INT);
2359         attr_id.face = GPU_vertformat_attr_add(&format_face_idx, "color", face_comp, 1, GPU_FETCH_INT);
2360
2361         /* Static formats */
2362         static GPUVertFormat format_pos_nor = { 0 }, format_lnor = { 0 }, format_flag = { 0 }, format_uv = { 0 };
2363         if (format_pos_nor.attr_len == 0) {
2364                 attr_id.pos = GPU_vertformat_attr_add(&format_pos_nor, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
2365                 attr_id.nor = GPU_vertformat_attr_add(&format_pos_nor, "vnor", GPU_COMP_I10, 3, GPU_FETCH_INT_TO_FLOAT_UNIT);
2366                 attr_id.lnor = GPU_vertformat_attr_add(&format_lnor, "lnor", GPU_COMP_I10, 3, GPU_FETCH_INT_TO_FLOAT_UNIT);
2367                 attr_id.data = GPU_vertformat_attr_add(&format_flag, "data", GPU_COMP_U8, 4, GPU_FETCH_INT);
2368                 attr_id.uvs = GPU_vertformat_attr_add(&format_uv, "u", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
2369                 GPU_vertformat_alias_add(&format_uv, "pos");
2370                 GPU_vertformat_alias_add(&format_flag, "flag");
2371         }
2372
2373         GPUVertBufRaw raw_verts, raw_edges, raw_faces, raw_pos, raw_nor, raw_lnor, raw_uv, raw_data;
2374         if (DRW_TEST_ASSIGN_VBO(vbo_pos_nor)) {
2375                 GPU_vertbuf_init_with_format(vbo_pos_nor, &format_pos_nor);
2376                 GPU_vertbuf_data_alloc(vbo_pos_nor, tot_loop_len);
2377                 GPU_vertbuf_attr_get_raw_data(vbo_pos_nor, attr_id.pos, &raw_pos);
2378                 GPU_vertbuf_attr_get_raw_data(vbo_pos_nor, attr_id.nor, &raw_nor);
2379         }
2380         if (DRW_TEST_ASSIGN_VBO(vbo_lnor)) {
2381                 GPU_vertbuf_init_with_format(vbo_lnor, &format_lnor);
2382                 GPU_vertbuf_data_alloc(vbo_lnor, tot_loop_len);
2383                 GPU_vertbuf_attr_get_raw_data(vbo_lnor, attr_id.lnor, &raw_lnor);
2384         }
2385         if (DRW_TEST_ASSIGN_VBO(vbo_data)) {
2386                 GPU_vertbuf_init_with_format(vbo_data, &format_flag);
2387                 GPU_vertbuf_data_alloc(vbo_data, tot_loop_len);
2388                 GPU_vertbuf_attr_get_raw_data(vbo_data, attr_id.data, &raw_data);
2389         }
2390         if (DRW_TEST_ASSIGN_VBO(vbo_uv)) {
2391                 GPU_vertbuf_init_with_format(vbo_uv, &format_uv);
2392                 GPU_vertbuf_data_alloc(vbo_uv, tot_loop_len);
2393                 GPU_vertbuf_attr_get_raw_data(vbo_uv, attr_id.uvs, &raw_uv);
2394         }
2395         /* Select Idx */
2396         if (DRW_TEST_ASSIGN_VBO(vbo_verts)) {
2397                 GPU_vertbuf_init_with_format(vbo_verts, &format_vert_idx);
2398                 GPU_vertbuf_data_alloc(vbo_verts, tot_loop_len);
2399                 GPU_vertbuf_attr_get_raw_data(vbo_verts, attr_id.vert, &raw_verts);
2400         }
2401         if (DRW_TEST_ASSIGN_VBO(vbo_edges)) {
2402                 GPU_vertbuf_init_with_format(vbo_edges, &format_edge_idx);
2403                 GPU_vertbuf_data_alloc(vbo_edges, tot_loop_len);
2404                 GPU_vertbuf_attr_get_raw_data(vbo_edges, attr_id.edge, &raw_edges);
2405         }
2406         if (DRW_TEST_ASSIGN_VBO(vbo_faces)) {
2407                 GPU_vertbuf_init_with_format(vbo_faces, &format_face_idx);
2408                 GPU_vertbuf_data_alloc(vbo_faces, tot_loop_len);
2409                 GPU_vertbuf_attr_get_raw_data(vbo_faces, attr_id.face, &raw_faces);
2410         }
2411
2412         if (rdata->edit_bmesh && rdata->mapped.use == false) {
2413                 BMesh *bm = rdata->edit_bmesh->bm;
2414                 BMIter iter_efa, iter_loop, iter_vert;
2415                 BMFace *efa;
2416                 BMEdge *eed;
2417                 BMVert *eve;
2418                 BMLoop *loop;
2419                 const int cd_loop_uv_offset = CustomData_get_offset(&bm->ldata, CD_MLOOPUV);
2420
2421                 /* Face Loops */
2422                 BM_ITER_MESH (efa, &iter_efa, bm, BM_FACES_OF_MESH) {
2423                         int fidx = BM_elem_index_get(efa);
2424                         if (vbo_data) {
2425                                 fflag = mesh_render_data_face_flag(rdata, efa, cd_loop_uv_offset);
2426                         }
2427                         BM_ITER_ELEM (loop, &iter_loop, efa, BM_LOOPS_OF_FACE) {
2428                                 if (vbo_pos_nor) {
2429                                         GPUPackedNormal *vnor = (GPUPackedNormal *)GPU_vertbuf_raw_step(&raw_nor);
2430                                         *vnor = GPU_normal_convert_i10_v3(loop->v->no);
2431                                         copy_v3_v3(GPU_vertbuf_raw_step(&raw_pos), loop->v->co);
2432                                 }
2433                                 if (vbo_lnor) {
2434                                         const float *nor = (lnors) ? lnors[BM_elem_index_get(loop)] : efa->no;
2435                                         GPUPackedNormal *lnor = (GPUPackedNormal *)GPU_vertbuf_raw_step(&raw_lnor);
2436                                         *lnor = GPU_normal_convert_i10_v3(nor);
2437                                 }
2438                                 if (vbo_data) {
2439                                         EdgeDrawAttr eattr = { .v_flag = fflag };
2440                                         mesh_render_data_edge_flag(rdata, loop->e, &eattr);
2441                                         mesh_render_data_vert_flag(rdata, loop->v, &eattr);
2442                                         mesh_render_data_loop_flag(rdata, loop, cd_loop_uv_offset, &eattr);
2443                                         memcpy(GPU_vertbuf_raw_step(&raw_data), &eattr, sizeof(EdgeDrawAttr));
2444                                 }
2445                                 if (vbo_uv) {
2446                                         MLoopUV *luv = BM_ELEM_CD_GET_VOID_P(loop, cd_loop_uv_offset);
2447                                         copy_v2_v2(GPU_vertbuf_raw_step(&raw_uv), luv->uv);
2448                                 }
2449                                 /* Select Idx */
2450                                 if (vbo_verts) {
2451                                         int vidx = BM_elem_index_get(loop->v);
2452                                         mesh_edit_add_select_index(&raw_verts, vert_comp, vidx);
2453                                 }
2454                                 if (vbo_edges) {
2455                                         int eidx = BM_elem_index_get(loop->e);
2456                                         mesh_edit_add_select_index(&raw_edges, edge_comp, eidx);
2457                                 }
2458                                 if (vbo_faces) {
2459                                         mesh_edit_add_select_index(&raw_faces, face_comp, fidx);
2460                                 }
2461                         }
2462                 }
2463                 /* Loose edges */
2464                 for (int e = 0; e < ledge_len; e++) {
2465                         eed = BM_edge_at_index(bm, rdata->loose_edges[e]);
2466                         BM_ITER_ELEM (eve, &iter_vert, eed, BM_VERTS_OF_EDGE) {
2467                                 if (vbo_pos_nor) {
2468                                         GPUPackedNormal *vnor = (GPUPackedNormal *)GPU_vertbuf_raw_step(&raw_nor);
2469                                         *vnor = GPU_normal_convert_i10_v3(eve->no);
2470                                         copy_v3_v3(GPU_vertbuf_raw_step(&raw_pos), eve->co);
2471                                 }
2472                                 if (vbo_data) {
2473                                         EdgeDrawAttr eattr = { 0 };
2474                                         mesh_render_data_edge_flag(rdata, eed, &eattr);
2475                                         mesh_render_data_vert_flag(rdata, eve, &eattr);
2476                                         memcpy(GPU_vertbuf_raw_step(&raw_data), &eattr, sizeof(EdgeDrawAttr));
2477                                 }
2478                                 if (vbo_lnor) {
2479                                         memset(GPU_vertbuf_raw_step(&raw_lnor), 0, sizeof(GPUPackedNormal));
2480                                 }
2481                                 /* Select Idx */
2482                                 if (vbo_verts) {
2483                                         int vidx = BM_elem_index_get(eve);
2484                                         mesh_edit_add_select_index(&raw_verts, vert_comp, vidx);
2485                                 }
2486                                 if (vbo_edges) {
2487                                         int eidx = BM_elem_index_get(eed);
2488                                         mesh_edit_add_select_index(&raw_edges, edge_comp, eidx);
2489                                 }
2490                         }
2491                 }
2492                 /* Loose verts */
2493                 for (int e = 0; e < lvert_len; e++) {
2494                         eve = BM_vert_at_index(bm, rdata->loose_verts[e]);
2495                         if (vbo_pos_nor) {
2496                                 GPUPackedNormal *vnor = (GPUPackedNormal *)GPU_vertbuf_raw_step(&raw_nor);
2497                                 *vnor = GPU_normal_convert_i10_v3(eve->no);
2498                                 copy_v3_v3(GPU_vertbuf_raw_step(&raw_pos), eve->co);
2499                         }
2500                         if (vbo_lnor) {
2501                                 memset(GPU_vertbuf_raw_step(&raw_lnor), 0, sizeof(GPUPackedNormal));
2502                         }
2503                         if (vbo_data) {
2504                                 EdgeDrawAttr eattr = { 0 };
2505                                 mesh_render_data_vert_flag(rdata, eve, &eattr);
2506                                 memcpy(GPU_vertbuf_raw_step(&raw_data), &eattr, sizeof(EdgeDrawAttr));
2507                         }
2508                         /* Select Idx */
2509                         if (vbo_verts) {
2510                                 int vidx = BM_elem_index_get(eve);
2511                                 mesh_edit_add_select_index(&raw_verts, vert_comp, vidx);
2512                         }
2513                 }
2514         }
2515         else if (rdata->mapped.use == true) {
2516                 BMesh *bm = rdata->edit_bmesh->bm;
2517                 const int cd_loop_uv_offset = CustomData_get_offset(&bm->ldata, CD_MLOOPUV);
2518
2519                 const MPoly *mpoly = rdata->mapped.me_cage->mpoly;
2520                 const MEdge *medge = rdata->mapped.me_cage->medge;
2521                 const MVert *mvert = rdata->mapped.me_cage->mvert;
2522                 const MLoop *mloop = rdata->mapped.me_cage->mloop;
2523
2524                 const int *v_origindex = rdata->mapped.v_origindex;
2525                 const int *e_origindex = rdata->mapped.e_origindex;
2526                 const int *p_origindex = rdata->mapped.p_origindex;
2527
2528                 /* Face Loops */
2529                 for (int poly = 0; poly < poly_len; poly++, mpoly++) {
2530                         const MLoop *l = &mloop[mpoly->loopstart];
2531                         int fidx = p_origindex[poly];
2532                         BMFace *efa = NULL;
2533                         if (vbo_data) {
2534                                 fflag = 0;
2535                                 if (fidx != ORIGINDEX_NONE) {
2536                                         efa = BM_face_at_index(bm, fidx);
2537                                         fflag = mesh_render_data_face_flag(rdata, efa, cd_loop_uv_offset);
2538                                 }
2539                         }
2540                         for (int i = 0; i < mpoly->totloop; i++, l++) {
2541                                 if (vbo_pos_nor) {
2542                                         copy_v3_v3(GPU_vertbuf_raw_step(&raw_pos), mvert[l->v].co);
2543                                 }
2544                                 if (vbo_lnor || vbo_pos_nor) {
2545                                         GPUPackedNormal vnor = GPU_normal_convert_i10_s3(mvert[l->v].no);
2546                                         if (vbo_pos_nor) {
2547                                                 *(GPUPackedNormal *)GPU_vertbuf_raw_step(&raw_nor) = vnor;
2548                                         }
2549                                         if (vbo_lnor) {
2550                                                 /* Mapped does not support lnors yet. */
2551                                                 *(GPUPackedNormal *)GPU_vertbuf_raw_step(&raw_lnor) = vnor;
2552                                         }
2553                                 }
2554                                 if (vbo_data) {
2555                                         EdgeDrawAttr eattr = { .v_flag = fflag };
2556                                         int vidx = v_origindex[l->v];
2557                                         int eidx = e_origindex[l->e];
2558                                         if (vidx != ORIGINDEX_NONE) {
2559                                                 BMVert *eve = BM_vert_at_index(bm, vidx);
2560                                                 mesh_render_data_vert_flag(rdata, eve, &eattr);
2561                                         }
2562                                         if (eidx != ORIGINDEX_NONE) {
2563                                                 BMEdge *eed = BM_edge_at_index(bm, eidx);
2564                                                 mesh_render_data_edge_flag(rdata, eed, &eattr);
2565                                                 /* TODO find a more efficient way to do that. */
2566                                                 BMLoop *loop;
2567                                                 BMIter iter_loop;
2568                                                 BM_ITER_ELEM (loop, &iter_loop, efa, BM_LOOPS_OF_FACE) {
2569                                                         if (loop->e == eed) {
2570                                                                 mesh_render_data_loop_flag(rdata, loop, cd_loop_uv_offset, &eattr);
2571                                                                 break;
2572                                                         }
2573                                                 }
2574                                         }
2575                                         memcpy(GPU_vertbuf_raw_step(&raw_data), &eattr, sizeof(EdgeDrawAttr));
2576                                 }
2577                                 if (vbo_uv) {
2578                                         MLoopUV *luv = &rdata->mloopuv[mpoly->loopstart + i];
2579                                         copy_v2_v2(GPU_vertbuf_raw_step(&raw_uv), luv->uv);
2580                                 }
2581                                 /* Select Idx */
2582                                 if (vbo_verts) {
2583                                         int vidx = v_origindex[l->v];
2584                                         mesh_edit_add_select_index(&raw_verts, vert_comp, vidx);
2585                                 }
2586                                 if (vbo_edges) {
2587                                         int eidx = e_origindex[l->e];
2588                                         mesh_edit_add_select_index(&raw_edges, edge_comp, eidx);
2589                                 }
2590                                 if (vbo_faces) {
2591                                         mesh_edit_add_select_index(&raw_faces, face_comp, fidx);
2592                                 }
2593                         }
2594                 }
2595                 /* Loose edges */
2596                 for (int j = 0; j < ledge_len; j++) {
2597                         const int e = rdata->mapped.loose_edges[j];
2598                         for (int i = 0; i < 2; ++i) {
2599                                 int v = (i == 0) ? medge[e].v1 : medge[e].v2;
2600                                 if (vbo_pos_nor) {
2601                                         GPUPackedNormal vnor = GPU_normal_convert_i10_s3(mvert[v].no);
2602                                         *(GPUPackedNormal *)GPU_vertbuf_raw_step(&raw_nor) = vnor;
2603                                         copy_v3_v3(GPU_vertbuf_raw_step(&raw_pos), mvert[v].co);
2604                                 }
2605                                 if (vbo_lnor) {
2606                                         memset(GPU_vertbuf_raw_step(&raw_lnor), 0, sizeof(GPUPackedNormal));
2607                                 }
2608                                 if (vbo_data) {
2609                                         EdgeDrawAttr eattr = { 0 };
2610                                         int vidx = v_origindex[v];
2611                                         int eidx = e_origindex[e];
2612                                         if (vidx != ORIGINDEX_NONE) {
2613                                                 BMVert *eve = BM_vert_at_index(bm, vidx);
2614                                                 mesh_render_data_vert_flag(rdata, eve, &eattr);
2615                                         }
2616                                         if (eidx != ORIGINDEX_NONE) {
2617                                                 BMEdge *eed = BM_edge_at_index(bm, eidx);
2618                                                 mesh_render_data_edge_flag(rdata, eed, &eattr);
2619                                         }
2620                                         memcpy(GPU_vertbuf_raw_step(&raw_data), &eattr, sizeof(EdgeDrawAttr));
2621                                 }
2622                                 /* Select Idx */
2623                                 if (vbo_verts) {
2624                                         int vidx = v_origindex[v];
2625                                         mesh_edit_add_select_index(&raw_verts, vert_comp, vidx);
2626                                 }
2627                                 if (vbo_edges) {
2628                                         int eidx = e_origindex[e];
2629                                         mesh_edit_add_select_index(&raw_edges, edge_comp, eidx);
2630                                 }
2631                         }
2632                 }
2633                 /* Loose verts */
2634                 for (int i = 0; i < lvert_len; i++) {
2635                         const int v = rdata->mapped.loose_verts[i];
2636                         if (vbo_pos_nor) {
2637                                 GPUPackedNormal vnor = GPU_normal_convert_i10_s3(mvert[v].no);
2638                                 *(GPUPackedNormal *)GPU_vertbuf_raw_step(&raw_nor) = vnor;
2639                                 copy_v3_v3(GPU_vertbuf_raw_step(&raw_pos), mvert[v].co);
2640                         }
2641                         if (vbo_lnor) {
2642                                 memset(GPU_vertbuf_raw_step(&raw_lnor), 0, sizeof(GPUPackedNormal));
2643                         }
2644                         if (vbo_data) {
2645                                 EdgeDrawAttr eattr = { 0 };
2646                                 int vidx = v_origindex[v];
2647                                 if (vidx != ORIGINDEX_NONE) {
2648                                         BMVert *eve = BM_vert_at_index(bm, vidx);
2649                                         mesh_render_data_vert_flag(rdata, eve, &eattr);
2650                                 }
2651                                 memcpy(GPU_vertbuf_raw_step(&raw_data), &eattr, sizeof(EdgeDrawAttr));
2652                         }
2653                         /* Select Idx */
2654                         if (vbo_verts) {
2655                                 int vidx = v_origindex[v];
2656                                 mesh_edit_add_select_index(&raw_verts, vert_comp, vidx);
2657                         }
2658                 }
2659         }
2660         else {
2661                 const MPoly *mpoly = rdata->mpoly;
2662                 const MVert *mvert = rdata->mvert;
2663                 const MLoop *mloop = rdata->mloop;
2664
2665                 const int *v_origindex = CustomData_get_layer(&rdata->me->vdata, CD_ORIGINDEX);
2666                 const int *e_origindex = CustomData_get_layer(&rdata->me->edata, CD_ORIGINDEX);
2667                 const int *p_origindex = CustomData_get_layer(&rdata->me->pdata, CD_ORIGINDEX);
2668
2669                 /* Face Loops */
2670                 for (int poly = 0; poly < poly_len; poly++, mpoly++) {
2671                         const MLoop *l = &mloop[mpoly->loopstart];
2672                         int fidx = p_origindex ? p_origindex[poly] : poly;
2673                         for (int i = 0; i < mpoly->totloop; i++, l++) {
2674                                 if (vbo_pos_nor) {
2675                                         copy_v3_v3(GPU_vertbuf_raw_step(&raw_pos), mvert[l->v].co);
2676                                 }
2677                                 if (vbo_lnor || vbo_pos_nor) {
2678                                         GPUPackedNormal vnor = GPU_normal_convert_i10_s3(mvert[l->v].no);
2679                                         if (vbo_pos_nor) {
2680                                                 *(GPUPackedNormal *)GPU_vertbuf_raw_step(&raw_nor) = vnor;
2681                                         }
2682                                         if (vbo_lnor) {
2683                                                 /* Mapped does not support lnors yet. */
2684                                                 *(GPUPackedNormal *)GPU_vertbuf_raw_step(&raw_lnor) = vnor;
2685                                         }
2686                                 }
2687                                 if (vbo_uv) {
2688                                         MLoopUV *luv = &rdata->mloopuv[mpoly->loopstart + i];
2689                                         copy_v2_v2(GPU_vertbuf_raw_step(&raw_uv), luv->uv);
2690                                 }
2691                                 /* Select Idx */
2692                                 if (vbo_verts) {
2693                                         int vidx = v_origindex ? v_origindex[l->v] : l->v;
2694                                         mesh_edit_add_select_index(&raw_verts, vert_comp, vidx);
2695                                 }
2696                                 if (vbo_edges) {
2697                                         int eidx = e_origindex ? e_origindex[l->e] : l->e;
2698                                         mesh_edit_add_select_index(&raw_edges, edge_comp, eidx);
2699                                 }
2700                                 if (vbo_faces) {
2701                                         mesh_edit_add_select_index(&raw_faces, face_comp, fidx);
2702                                 }
2703                         }
2704                 }
2705                 /* TODO(fclem): Until we find a way to detect
2706                  * loose verts easily outside of edit mode, this
2707                  * will remain disabled. */
2708 #if 0
2709                 /* Loose edges */
2710                 for (int e = 0; e < edge_len; e++, medge++) {
2711                         int eidx = e_origindex[e];
2712                         if (eidx != ORIGINDEX_NONE && (medge->flag & ME_LOOSEEDGE)) {
2713                                 for (int i = 0; i < 2; ++i) {
2714                                         int vidx = (i == 0) ? medge->v1 : medge->v2;
2715                                         if (vbo_pos) {
2716                                                 copy_v3_v3(GPU_vertbuf_raw_step(&raw_pos), mvert[vidx].co);
2717                                         }
2718                                         if (vbo_verts) {
2719                                                 mesh_edit_add_select_index(&raw_verts, vert_comp, vidx);
2720                                         }
2721                                         if (vbo_edges) {
2722                                                 mesh_edit_add_select_index(&raw_edges, edge_comp, eidx);
2723                                         }
2724                                 }
2725                         }
2726                 }
2727                 /* Loose verts */
2728                 for (int v = 0; v < vert_len; v++, mvert++) {
2729                         int vidx = v_origindex[v];
2730                         if (vidx != ORIGINDEX_NONE) {
2731                                 MVert *eve = BM_vert_at_index(bm, vidx);
2732                                 if (eve->e == NULL) {
2733                                         if (vbo_pos) {
2734                                                 copy_v3_v3(GPU_vertbuf_raw_step(&raw_pos), mvert->co);
2735                                         }
2736                                         if (vbo_verts) {
2737                                                 mesh_edit_add_select_index(&raw_verts, vert_comp, vidx);
2738                                         }
2739                                 }
2740                         }
2741                 }
2742 #endif
2743         }
2744         /* Don't resize */
2745 }
2746
2747 /* TODO: We could use gl_PrimitiveID as index instead of using another VBO. */
2748 static void mesh_create_edit_facedots_select_id(
2749         MeshRenderData *rdata,
2750         GPUVertBuf *vbo)
2751 {
2752         const int poly_len = mesh_render_data_polys_len_get_maybe_mapped(rdata);
2753
2754         GPUVertCompType comp = SELECT_COMP_FORMAT(poly_len);
2755
2756         GPUVertFormat format = { 0 };
2757         struct { uint idx; } attr_id;
2758         attr_id.idx = GPU_vertformat_attr_add(&format, "color", comp, 1, GPU_FETCH_INT);
2759
2760         GPUVertBufRaw idx_step;
2761         GPU_vertbuf_init_with_format(vbo, &format);
2762         GPU_vertbuf_data_alloc(vbo, poly_len);
2763         GPU_vertbuf_attr_get_raw_data(vbo, attr_id.idx, &idx_step);
2764
2765         /* Keep in sync with mesh_create_edit_facedots(). */
2766         if (rdata->mapped.use == false) {
2767                 if (rdata->edit_bmesh) {
2768                         for (int poly = 0; poly < poly_len; poly++) {
2769                                 const BMFace *efa = BM_face_at_index(rdata->edit_bmesh->bm, poly);
2770                                 if (!BM_elem_flag_test(efa, BM_ELEM_HIDDEN)) {
2771                                         mesh_edit_add_select_index(&idx_step, comp, poly);
2772                                 }
2773                         }
2774                 }
2775                 else {
2776                         for (int poly = 0; poly < poly_len; poly++) {
2777                                 mesh_edit_add_select_index(&idx_step, comp, poly);
2778                         }
2779                 }
2780         }
2781         else {
2782                 const int *p_origindex = rdata->mapped.p_origindex;
2783                 for (int poly = 0; poly < poly_len; poly++) {
2784                         const int p_orig = p_origindex[poly];
2785                         if (p_orig != ORIGINDEX_NONE) {
2786                                 const BMFace *efa = BM_face_at_index(rdata->edit_bmesh->bm, p_orig);
2787                                 if (!BM_elem_flag_test(efa, BM_ELEM_HIDDEN)) {
2788                                         mesh_edit_add_select_index(&idx_step, comp, poly);
2789                                 }
2790                         }
2791                 }
2792         }
2793
2794         /* Resize & Finish */
2795         int facedot_len_used = GPU_vertbuf_raw_used(&idx_step);
2796         if (facedot_len_used != poly_len) {
2797                 GPU_vertbuf_data_resize(vbo, facedot_len_used);
2798         }
2799 }
2800 #undef SELECT_COMP_FORMAT
2801
2802 static void mesh_create_pos_and_nor(MeshRenderData *rdata, GPUVertBuf *vbo)
2803 {
2804         static GPUVertFormat format = { 0 };
2805         static struct { uint pos, nor; } attr_id;
2806         if (format.attr_len == 0) {
2807                 attr_id.pos = GPU_vertformat_attr_add(&format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
2808                 attr_id.nor = GPU_vertformat_attr_add(&format, "nor", GPU_COMP_I10, 4, GPU_FETCH_INT_TO_FLOAT_UNIT);
2809         }
2810
2811         GPU_vertbuf_init_with_format(vbo, &format);
2812         const int vbo_len_capacity = mesh_render_data_verts_len_get_maybe_mapped(rdata);
2813         GPU_vertbuf_data_alloc(vbo, vbo_len_capacity);
2814
2815         if (rdata->mapped.use == false) {
2816                 if (rdata->edit_bmesh) {
2817                         BMesh *bm = rdata->edit_bmesh->bm;
2818                         BMIter iter;
2819                         BMVert *eve;
2820                         uint i;
2821
2822                         mesh_render_data_ensure_vert_normals_pack(rdata);
2823                         GPUPackedNormal *vnor = rdata->vert_normals_pack;
2824
2825                         BM_ITER_MESH_INDEX (eve, &iter, bm, BM_VERTS_OF_MESH, i) {
2826                                 GPU_vertbuf_attr_set(vbo, attr_id.pos, i, eve->co);
2827                                 GPU_vertbuf_attr_set(vbo, attr_id.nor, i, &vnor[i]);
2828                         }
2829                         BLI_assert(i == vbo_len_capacity);
2830                 }
2831                 else {