Cleanup: remove redundant doxygen \file argument
[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         }
1524         if (BM_elem_flag_test(eed, BM_ELEM_SEAM)) {
1525                 eattr->e_flag |= VFLAG_EDGE_SEAM;
1526         }
1527         if (!BM_elem_flag_test(eed, BM_ELEM_SMOOTH)) {
1528                 eattr->e_flag |= VFLAG_EDGE_SHARP;
1529         }
1530         /* Use a byte for value range */
1531         if (rdata->cd.offset.crease != -1) {
1532                 float crease = BM_ELEM_CD_GET_FLOAT(eed, rdata->cd.offset.crease);
1533                 if (crease > 0) {
1534                         eattr->crease = (uchar)(crease * 255.0f);
1535                 }
1536         }
1537         /* Use a byte for value range */
1538         if (rdata->cd.offset.bweight != -1) {
1539                 float bweight = BM_ELEM_CD_GET_FLOAT(eed, rdata->cd.offset.bweight);
1540                 if (bweight > 0) {
1541                         eattr->bweight = (uchar)(bweight * 255.0f);
1542                 }
1543         }
1544 #ifdef WITH_FREESTYLE
1545         if (rdata->cd.offset.freestyle_edge != -1) {
1546                 const FreestyleEdge *fed = BM_ELEM_CD_GET_VOID_P(eed, rdata->cd.offset.freestyle_edge);
1547                 if (fed->flag & FREESTYLE_EDGE_MARK) {
1548                         eattr->e_flag |= VFLAG_EDGE_FREESTYLE;
1549                 }
1550         }
1551 #endif
1552 }
1553
1554 static void mesh_render_data_loop_flag(MeshRenderData *rdata, BMLoop *loop, const int cd_ofs, EdgeDrawAttr *eattr)
1555 {
1556         if (cd_ofs == -1) {
1557                 return;
1558         }
1559         MLoopUV *luv = BM_ELEM_CD_GET_VOID_P(loop, cd_ofs);
1560         if (luv != NULL && (luv->flag & MLOOPUV_PINNED)) {
1561                 eattr->v_flag |= VFLAG_VERT_UV_PINNED;
1562         }
1563         if (uvedit_uv_select_test_ex(rdata->toolsettings, loop, cd_ofs)) {
1564                 eattr->v_flag |= VFLAG_VERT_UV_SELECT;
1565         }
1566         if (uvedit_edge_select_test_ex(rdata->toolsettings, loop, cd_ofs)) {
1567                 eattr->v_flag |= VFLAG_EDGE_UV_SELECT;
1568         }
1569 }
1570
1571 static void mesh_render_data_vert_flag(MeshRenderData *rdata, const BMVert *eve, EdgeDrawAttr *eattr)
1572 {
1573         if (eve == rdata->eve_act) {
1574                 eattr->e_flag |= VFLAG_VERT_ACTIVE;
1575         }
1576         if (BM_elem_flag_test(eve, BM_ELEM_SELECT)) {
1577                 eattr->e_flag |= VFLAG_VERT_SELECTED;
1578         }
1579 }
1580
1581 static bool add_edit_facedot(
1582         MeshRenderData *rdata, GPUVertBuf *vbo,
1583         const uint fdot_pos_id, const uint fdot_nor_flag_id,
1584         const int poly, const int base_vert_idx)
1585 {
1586         BLI_assert(rdata->types & (MR_DATATYPE_VERT | MR_DATATYPE_LOOP | MR_DATATYPE_POLY));
1587         float pnor[3], center[3];
1588         bool selected;
1589         if (rdata->edit_bmesh) {
1590                 const BMFace *efa = BM_face_at_index(rdata->edit_bmesh->bm, poly);
1591                 if (BM_elem_flag_test(efa, BM_ELEM_HIDDEN)) {
1592                         return false;
1593                 }
1594                 if (rdata->edit_data && rdata->edit_data->vertexCos) {
1595                         copy_v3_v3(center, rdata->edit_data->polyCos[poly]);
1596                         copy_v3_v3(pnor, rdata->edit_data->polyNos[poly]);
1597                 }
1598                 else {
1599                         BM_face_calc_center_median(efa, center);
1600                         copy_v3_v3(pnor, efa->no);
1601                 }
1602                 selected = (BM_elem_flag_test(efa, BM_ELEM_SELECT) != 0) ? true : false;
1603         }
1604         else {
1605                 MVert *mvert = rdata->mvert;
1606                 const MPoly *mpoly = rdata->mpoly + poly;
1607                 const MLoop *mloop = rdata->mloop + mpoly->loopstart;
1608
1609                 BKE_mesh_calc_poly_center(mpoly, mloop, mvert, center);
1610                 BKE_mesh_calc_poly_normal(mpoly, mloop, mvert, pnor);
1611
1612                 selected = false; /* No selection if not in edit mode */
1613         }
1614
1615         GPUPackedNormal nor = GPU_normal_convert_i10_v3(pnor);
1616         nor.w = (selected) ? 1 : 0;
1617         GPU_vertbuf_attr_set(vbo, fdot_nor_flag_id, base_vert_idx, &nor);
1618         GPU_vertbuf_attr_set(vbo, fdot_pos_id, base_vert_idx, center);
1619
1620         return true;
1621 }
1622 static bool add_edit_facedot_mapped(
1623         MeshRenderData *rdata, GPUVertBuf *vbo,
1624         const uint fdot_pos_id, const uint fdot_nor_flag_id,
1625         const int poly, const int base_vert_idx)
1626 {
1627         BLI_assert(rdata->types & (MR_DATATYPE_VERT | MR_DATATYPE_LOOP | MR_DATATYPE_POLY));
1628         float pnor[3], center[3];
1629         const int *p_origindex = rdata->mapped.p_origindex;
1630         const int p_orig = p_origindex[poly];
1631         if (p_orig == ORIGINDEX_NONE) {
1632                 return false;
1633         }
1634         BMEditMesh *em = rdata->edit_bmesh;
1635         const BMFace *efa = BM_face_at_index(rdata->edit_bmesh->bm, p_orig);
1636         if (BM_elem_flag_test(efa, BM_ELEM_HIDDEN)) {
1637                 return false;
1638         }
1639
1640         Mesh *me_cage = em->mesh_eval_cage;
1641         const MVert *mvert = me_cage->mvert;
1642         const MLoop *mloop = me_cage->mloop;
1643         const MPoly *mpoly = me_cage->mpoly;
1644
1645         const MPoly *mp = mpoly + poly;
1646         const MLoop *ml = mloop + mp->loopstart;
1647
1648         BKE_mesh_calc_poly_center(mp, ml, mvert, center);
1649         BKE_mesh_calc_poly_normal(mp, ml, mvert, pnor);
1650
1651         GPUPackedNormal nor = GPU_normal_convert_i10_v3(pnor);
1652         nor.w = (BM_elem_flag_test(efa, BM_ELEM_SELECT) != 0) ? 1 : 0;
1653         GPU_vertbuf_attr_set(vbo, fdot_nor_flag_id, base_vert_idx, &nor);
1654         GPU_vertbuf_attr_set(vbo, fdot_pos_id, base_vert_idx, center);
1655
1656         return true;
1657 }
1658
1659 /** \} */
1660
1661 /* ---------------------------------------------------------------------- */
1662 /** \name Vertex Group Selection
1663  * \{ */
1664
1665 /** Reset the selection structure, deallocating heap memory as appropriate. */
1666 static void drw_mesh_weight_state_clear(struct DRW_MeshWeightState *wstate)
1667 {
1668         MEM_SAFE_FREE(wstate->defgroup_sel);
1669
1670         memset(wstate, 0, sizeof(*wstate));
1671
1672         wstate->defgroup_active = -1;
1673 }
1674
1675 /** Copy selection data from one structure to another, including heap memory. */
1676 static void drw_mesh_weight_state_copy(
1677         struct DRW_MeshWeightState *wstate_dst, const struct DRW_MeshWeightState *wstate_src)
1678 {
1679         MEM_SAFE_FREE(wstate_dst->defgroup_sel);
1680
1681         memcpy(wstate_dst, wstate_src, sizeof(*wstate_dst));
1682
1683         if (wstate_src->defgroup_sel) {
1684                 wstate_dst->defgroup_sel = MEM_dupallocN(wstate_src->defgroup_sel);
1685         }
1686 }
1687
1688 /** Compare two selection structures. */
1689 static bool drw_mesh_weight_state_compare(const struct DRW_MeshWeightState *a, const struct DRW_MeshWeightState *b)
1690 {
1691         return a->defgroup_active == b->defgroup_active &&
1692                a->defgroup_len == b->defgroup_len &&
1693                a->flags == b->flags &&
1694                a->alert_mode == b->alert_mode &&
1695                a->defgroup_sel_count == b->defgroup_sel_count &&
1696                ((!a->defgroup_sel && !b->defgroup_sel) ||
1697                 (a->defgroup_sel && b->defgroup_sel &&
1698                  memcmp(a->defgroup_sel, b->defgroup_sel, a->defgroup_len * sizeof(bool)) == 0));
1699 }
1700
1701 static void drw_mesh_weight_state_extract(
1702         Object *ob, Mesh *me, const ToolSettings *ts, bool paint_mode,
1703         struct DRW_MeshWeightState *wstate)
1704 {
1705         /* Extract complete vertex weight group selection state and mode flags. */
1706         memset(wstate, 0, sizeof(*wstate));
1707
1708         wstate->defgroup_active = ob->actdef - 1;
1709         wstate->defgroup_len = BLI_listbase_count(&ob->defbase);
1710
1711         wstate->alert_mode = ts->weightuser;
1712
1713         if (paint_mode && ts->multipaint) {
1714                 /* Multipaint needs to know all selected bones, not just the active group.
1715                  * This is actually a relatively expensive operation, but caching would be difficult. */
1716                 wstate->defgroup_sel = BKE_object_defgroup_selected_get(ob, wstate->defgroup_len, &wstate->defgroup_sel_count);
1717
1718                 if (wstate->defgroup_sel_count > 1) {
1719                         wstate->flags |= DRW_MESH_WEIGHT_STATE_MULTIPAINT | (ts->auto_normalize ? DRW_MESH_WEIGHT_STATE_AUTO_NORMALIZE : 0);
1720
1721                         if (me->editflag & ME_EDIT_MIRROR_X) {
1722                                 BKE_object_defgroup_mirror_selection(
1723                                         ob, wstate->defgroup_len, wstate->defgroup_sel, wstate->defgroup_sel, &wstate->defgroup_sel_count);
1724                         }
1725                 }
1726                 /* With only one selected bone Multipaint reverts to regular mode. */
1727                 else {
1728                         wstate->defgroup_sel_count = 0;
1729                         MEM_SAFE_FREE(wstate->defgroup_sel);
1730                 }
1731         }
1732 }
1733
1734 /** \} */
1735
1736 /* ---------------------------------------------------------------------- */
1737 /** \name Mesh GPUBatch Cache
1738  * \{ */
1739
1740 typedef struct MeshBatchCache {
1741         /* In order buffers: All verts only specified once.
1742          * To be used with a GPUIndexBuf. */
1743         struct {
1744                 /* Vertex data. */
1745                 GPUVertBuf *pos_nor;
1746                 GPUVertBuf *weights;
1747                 /* Loop data. */
1748                 GPUVertBuf *loop_pos_nor;
1749                 GPUVertBuf *loop_uv_tan;
1750                 GPUVertBuf *loop_vcol;
1751         } ordered;
1752
1753         /* Tesselated: (all verts specified for each triangles).
1754          * Indices does not match the CPU data structure's. */
1755         struct {
1756                 GPUVertBuf *pos_nor;
1757                 GPUVertBuf *wireframe_data;
1758         } tess;
1759
1760         /* Edit Mesh Data:
1761          * Edit cage can be different from final mesh so vertex count
1762          * might differ. */
1763         struct {
1764                 /* TODO(fclem): Reuse ordered.loop_pos_nor and maybe even
1765                  * ordered.loop_uv_tan when cage match final mesh. */
1766                 GPUVertBuf *loop_pos_nor;
1767                 GPUVertBuf *loop_data;
1768                 GPUVertBuf *loop_lnor;
1769                 GPUVertBuf *facedots_pos_nor_data;
1770                 /* UV data without modifier applied.
1771                  * Vertex count is always the one of the cage. */
1772                 GPUVertBuf *loop_uv;
1773                 GPUVertBuf *loop_uv_data;
1774                 GPUVertBuf *loop_stretch_angle;
1775                 GPUVertBuf *loop_stretch_area;
1776                 GPUVertBuf *facedots_uv;
1777                 GPUVertBuf *facedots_uv_data;
1778                 /* Selection */
1779                 GPUVertBuf *loop_vert_idx;
1780                 GPUVertBuf *loop_edge_idx;
1781                 GPUVertBuf *loop_face_idx;
1782                 GPUVertBuf *facedots_idx;
1783         } edit;
1784
1785         /* Index Buffers:
1786          * Only need to be updated when topology changes. */
1787         struct {
1788                 /* Indices to verts. */
1789                 GPUIndexBuf *surf_tris;
1790                 GPUIndexBuf *edges_lines;
1791                 GPUIndexBuf *edges_adj_lines;
1792                 GPUIndexBuf *loose_edges_lines;
1793                 /* Indices to vloops. */
1794                 GPUIndexBuf *loops_tris;
1795                 GPUIndexBuf *loops_lines;
1796                 /* Edit mode. */
1797                 GPUIndexBuf *edit_loops_points; /* verts */
1798                 GPUIndexBuf *edit_loops_lines; /* edges */
1799                 GPUIndexBuf *edit_loops_tris; /* faces */
1800                 /* Edit UVs */
1801                 GPUIndexBuf *edituv_loops_points; /* verts */
1802                 GPUIndexBuf *edituv_loops_lines; /* edges */
1803                 GPUIndexBuf *edituv_loops_tri_fans; /* faces */
1804         } ibo;
1805
1806         struct {
1807                 /* Surfaces / Render */
1808                 GPUBatch *surface;
1809                 GPUBatch *surface_weights;
1810                 /* Edit mode */
1811                 GPUBatch *edit_triangles;
1812                 GPUBatch *edit_vertices;
1813                 GPUBatch *edit_edges;
1814                 GPUBatch *edit_lnor;
1815                 GPUBatch *edit_facedots;
1816                 /* Edit UVs */
1817                 GPUBatch *edituv_faces_strech_area;
1818                 GPUBatch *edituv_faces_strech_angle;
1819                 GPUBatch *edituv_faces;
1820                 GPUBatch *edituv_edges;
1821                 GPUBatch *edituv_verts;
1822                 GPUBatch *edituv_facedots;
1823                 /* Edit selection */
1824                 GPUBatch *edit_selection_verts;
1825                 GPUBatch *edit_selection_edges;
1826                 GPUBatch *edit_selection_faces;
1827                 GPUBatch *edit_selection_facedots;
1828                 /* Common display / Other */
1829                 GPUBatch *all_verts;
1830                 GPUBatch *all_edges;
1831                 GPUBatch *loose_edges;
1832                 GPUBatch *edge_detection;
1833                 GPUBatch *wire_loops; /* Loops around faces. */
1834                 GPUBatch *wire_loops_uvs; /* Same as wire_loops but only has uvs. */
1835                 GPUBatch *wire_triangles; /* Triangles for object mode wireframe. */
1836         } batch;
1837
1838         GPUIndexBuf **surf_per_mat_tris;
1839         GPUBatch **surf_per_mat;
1840
1841         /* arrays of bool uniform names (and value) that will be use to
1842          * set srgb conversion for auto attributes.*/
1843         char *auto_layer_names;
1844         int *auto_layer_is_srgb;
1845         int auto_layer_len;
1846
1847         /* settings to determine if cache is invalid */
1848         bool is_maybe_dirty;
1849         bool is_dirty; /* Instantly invalidates cache, skipping mesh check */
1850         int edge_len;
1851         int tri_len;
1852         int poly_len;
1853         int vert_len;
1854         int mat_len;
1855         bool is_editmode;
1856         bool is_uvsyncsel;
1857
1858         struct DRW_MeshWeightState weight_state;
1859
1860         uchar cd_vused[CD_NUMTYPES];
1861         uchar cd_vneeded[CD_NUMTYPES];
1862         ushort cd_lused[CD_NUMTYPES];
1863         ushort cd_lneeded[CD_NUMTYPES];
1864
1865         /* XXX, only keep for as long as sculpt mode uses shaded drawing. */
1866         bool is_sculpt_points_tag;
1867
1868         /* Valid only if edge_detection is up to date. */
1869         bool is_manifold;
1870 } MeshBatchCache;
1871
1872 /* GPUBatch cache management. */
1873
1874 static bool mesh_batch_cache_valid(Mesh *me)
1875 {
1876         MeshBatchCache *cache = me->runtime.batch_cache;
1877
1878         if (cache == NULL) {
1879                 return false;
1880         }
1881
1882         if (cache->mat_len != mesh_render_mat_len_get(me)) {
1883                 return false;
1884         }
1885
1886         if (cache->is_editmode != (me->edit_btmesh != NULL)) {
1887                 return false;
1888         }
1889
1890         if (cache->is_dirty) {
1891                 return false;
1892         }
1893
1894         if (cache->is_maybe_dirty == false) {
1895                 return true;
1896         }
1897         else {
1898                 if (cache->is_editmode) {
1899                         return false;
1900                 }
1901                 else if ((cache->vert_len != mesh_render_verts_len_get(me)) ||
1902                          (cache->edge_len != mesh_render_edges_len_get(me)) ||
1903                          (cache->tri_len  != mesh_render_looptri_len_get(me)) ||
1904                          (cache->poly_len != mesh_render_polys_len_get(me)) ||
1905                          (cache->mat_len   != mesh_render_mat_len_get(me)))
1906                 {
1907                         return false;
1908                 }
1909         }
1910
1911         return true;
1912 }
1913
1914 static void mesh_batch_cache_init(Mesh *me)
1915 {
1916         MeshBatchCache *cache = me->runtime.batch_cache;
1917
1918         if (!cache) {
1919                 cache = me->runtime.batch_cache = MEM_callocN(sizeof(*cache), __func__);
1920         }
1921         else {
1922                 memset(cache, 0, sizeof(*cache));
1923         }
1924
1925         cache->is_editmode = me->edit_btmesh != NULL;
1926
1927         if (cache->is_editmode == false) {
1928                 cache->edge_len = mesh_render_edges_len_get(me);
1929                 cache->tri_len = mesh_render_looptri_len_get(me);
1930                 cache->poly_len = mesh_render_polys_len_get(me);
1931                 cache->vert_len = mesh_render_verts_len_get(me);
1932         }
1933
1934         cache->mat_len = mesh_render_mat_len_get(me);
1935         cache->surf_per_mat_tris = MEM_callocN(sizeof(*cache->surf_per_mat_tris) * cache->mat_len, __func__);
1936         cache->surf_per_mat = MEM_callocN(sizeof(*cache->surf_per_mat) * cache->mat_len, __func__);
1937
1938         cache->is_maybe_dirty = false;
1939         cache->is_dirty = false;
1940
1941         drw_mesh_weight_state_clear(&cache->weight_state);
1942 }
1943
1944 static MeshBatchCache *mesh_batch_cache_get(Mesh *me)
1945 {
1946         if (!mesh_batch_cache_valid(me)) {
1947                 mesh_batch_cache_clear(me);
1948                 mesh_batch_cache_init(me);
1949         }
1950         return me->runtime.batch_cache;
1951 }
1952
1953 static void mesh_batch_cache_check_vertex_group(MeshBatchCache *cache, const struct DRW_MeshWeightState *wstate)
1954 {
1955         if (!drw_mesh_weight_state_compare(&cache->weight_state, wstate)) {
1956                 GPU_BATCH_CLEAR_SAFE(cache->batch.surface_weights);
1957                 GPU_VERTBUF_DISCARD_SAFE(cache->ordered.weights);
1958
1959                 drw_mesh_weight_state_clear(&cache->weight_state);
1960         }
1961 }
1962
1963 static void mesh_batch_cache_discard_shaded_tri(MeshBatchCache *cache)
1964 {
1965         GPU_VERTBUF_DISCARD_SAFE(cache->ordered.loop_pos_nor);
1966         GPU_VERTBUF_DISCARD_SAFE(cache->ordered.loop_uv_tan);
1967         GPU_VERTBUF_DISCARD_SAFE(cache->ordered.loop_vcol);
1968         /* TODO */
1969         // GPU_VERTBUF_DISCARD_SAFE(cache->ordered.loop_orco);
1970
1971         if (cache->surf_per_mat_tris) {
1972                 for (int i = 0; i < cache->mat_len; i++) {
1973                         GPU_INDEXBUF_DISCARD_SAFE(cache->surf_per_mat_tris[i]);
1974                 }
1975         }
1976         MEM_SAFE_FREE(cache->surf_per_mat_tris);
1977         if (cache->surf_per_mat) {
1978                 for (int i = 0; i < cache->mat_len; i++) {
1979                         GPU_BATCH_DISCARD_SAFE(cache->surf_per_mat[i]);
1980                 }
1981         }
1982         MEM_SAFE_FREE(cache->surf_per_mat);
1983
1984         MEM_SAFE_FREE(cache->auto_layer_names);
1985         MEM_SAFE_FREE(cache->auto_layer_is_srgb);
1986
1987         cache->mat_len = 0;
1988 }
1989
1990 static void mesh_batch_cache_discard_uvedit(MeshBatchCache *cache)
1991 {
1992         GPU_VERTBUF_DISCARD_SAFE(cache->edit.loop_stretch_angle);
1993         GPU_VERTBUF_DISCARD_SAFE(cache->edit.loop_stretch_area);
1994         GPU_VERTBUF_DISCARD_SAFE(cache->edit.loop_uv);
1995         GPU_VERTBUF_DISCARD_SAFE(cache->edit.loop_uv_data);
1996         GPU_VERTBUF_DISCARD_SAFE(cache->edit.facedots_uv);
1997         GPU_VERTBUF_DISCARD_SAFE(cache->edit.facedots_uv_data);
1998         GPU_INDEXBUF_DISCARD_SAFE(cache->ibo.edituv_loops_tri_fans);
1999         GPU_INDEXBUF_DISCARD_SAFE(cache->ibo.edituv_loops_lines);
2000         GPU_INDEXBUF_DISCARD_SAFE(cache->ibo.edituv_loops_points);
2001         GPU_BATCH_DISCARD_SAFE(cache->batch.edituv_faces_strech_area);
2002         GPU_BATCH_DISCARD_SAFE(cache->batch.edituv_faces_strech_angle);
2003         GPU_BATCH_DISCARD_SAFE(cache->batch.edituv_faces);
2004         GPU_BATCH_DISCARD_SAFE(cache->batch.edituv_edges);
2005         GPU_BATCH_DISCARD_SAFE(cache->batch.edituv_verts);
2006         GPU_BATCH_DISCARD_SAFE(cache->batch.edituv_facedots);
2007 }
2008
2009 void DRW_mesh_batch_cache_dirty_tag(Mesh *me, int mode)
2010 {
2011         MeshBatchCache *cache = me->runtime.batch_cache;
2012         if (cache == NULL) {
2013                 return;
2014         }
2015         switch (mode) {
2016                 case BKE_MESH_BATCH_DIRTY_MAYBE_ALL:
2017                         cache->is_maybe_dirty = true;
2018                         break;
2019                 case BKE_MESH_BATCH_DIRTY_SELECT:
2020                         GPU_VERTBUF_DISCARD_SAFE(cache->edit.loop_data);
2021                         GPU_VERTBUF_DISCARD_SAFE(cache->edit.facedots_pos_nor_data);
2022                         GPU_BATCH_DISCARD_SAFE(cache->batch.edit_triangles);
2023                         GPU_BATCH_DISCARD_SAFE(cache->batch.edit_vertices);
2024                         GPU_BATCH_DISCARD_SAFE(cache->batch.edit_edges);
2025                         GPU_BATCH_DISCARD_SAFE(cache->batch.edit_facedots);
2026                         /* Paint mode selection */
2027                         /* TODO only do that in paint mode. */
2028                         GPU_VERTBUF_DISCARD_SAFE(cache->ordered.loop_pos_nor);
2029                         GPU_BATCH_DISCARD_SAFE(cache->batch.surface);
2030                         GPU_BATCH_DISCARD_SAFE(cache->batch.wire_loops);
2031                         if (cache->surf_per_mat) {
2032                                 for (int i = 0; i < cache->mat_len; i++) {
2033                                         GPU_BATCH_DISCARD_SAFE(cache->surf_per_mat[i]);
2034                                 }
2035                         }
2036                         /* Because visible UVs depends on edit mode selection, discard everything. */
2037                         mesh_batch_cache_discard_uvedit(cache);
2038                         break;
2039                 case BKE_MESH_BATCH_DIRTY_ALL:
2040                         cache->is_dirty = true;
2041                         break;
2042                 case BKE_MESH_BATCH_DIRTY_SHADING:
2043                         mesh_batch_cache_discard_shaded_tri(cache);
2044                         mesh_batch_cache_discard_uvedit(cache);
2045                         break;
2046                 case BKE_MESH_BATCH_DIRTY_SCULPT_COORDS:
2047                         cache->is_sculpt_points_tag = true;
2048                         break;
2049                 case BKE_MESH_BATCH_DIRTY_UVEDIT_ALL:
2050                         mesh_batch_cache_discard_uvedit(cache);
2051                         break;
2052                 case BKE_MESH_BATCH_DIRTY_UVEDIT_SELECT:
2053                         GPU_VERTBUF_DISCARD_SAFE(cache->edit.loop_uv_data);
2054                         GPU_VERTBUF_DISCARD_SAFE(cache->edit.facedots_uv_data);
2055                         GPU_BATCH_DISCARD_SAFE(cache->batch.edituv_faces_strech_area);
2056                         GPU_BATCH_DISCARD_SAFE(cache->batch.edituv_faces_strech_angle);
2057                         GPU_BATCH_DISCARD_SAFE(cache->batch.edituv_faces);
2058                         GPU_BATCH_DISCARD_SAFE(cache->batch.edituv_edges);
2059                         GPU_BATCH_DISCARD_SAFE(cache->batch.edituv_verts);
2060                         GPU_BATCH_DISCARD_SAFE(cache->batch.edituv_facedots);
2061                         break;
2062                 default:
2063                         BLI_assert(0);
2064         }
2065 }
2066
2067 static void mesh_batch_cache_clear(Mesh *me)
2068 {
2069         MeshBatchCache *cache = me->runtime.batch_cache;
2070         if (!cache) {
2071                 return;
2072         }
2073
2074         for (int i = 0; i < sizeof(cache->ordered) / sizeof(void *); ++i) {
2075                 GPUVertBuf **vbo = (GPUVertBuf **)&cache->ordered;
2076                 GPU_VERTBUF_DISCARD_SAFE(vbo[i]);
2077         }
2078         for (int i = 0; i < sizeof(cache->tess) / sizeof(void *); ++i) {
2079                 GPUVertBuf **vbo = (GPUVertBuf **)&cache->tess;
2080                 GPU_VERTBUF_DISCARD_SAFE(vbo[i]);
2081         }
2082         for (int i = 0; i < sizeof(cache->edit) / sizeof(void *); ++i) {
2083                 GPUVertBuf **vbo = (GPUVertBuf **)&cache->edit;
2084                 GPU_VERTBUF_DISCARD_SAFE(vbo[i]);
2085         }
2086         for (int i = 0; i < sizeof(cache->ibo) / sizeof(void *); ++i) {
2087                 GPUIndexBuf **ibo = (GPUIndexBuf **)&cache->ibo;
2088                 GPU_INDEXBUF_DISCARD_SAFE(ibo[i]);
2089         }
2090         for (int i = 0; i < sizeof(cache->batch) / sizeof(void *); ++i) {
2091                 GPUBatch **batch = (GPUBatch **)&cache->batch;
2092                 GPU_BATCH_DISCARD_SAFE(batch[i]);
2093         }
2094
2095         mesh_batch_cache_discard_shaded_tri(cache);
2096
2097         mesh_batch_cache_discard_uvedit(cache);
2098
2099         drw_mesh_weight_state_clear(&cache->weight_state);
2100 }
2101
2102 void DRW_mesh_batch_cache_free(Mesh *me)
2103 {
2104         mesh_batch_cache_clear(me);
2105         MEM_SAFE_FREE(me->runtime.batch_cache);
2106 }
2107
2108 /* GPUBatch cache usage. */
2109
2110 static void mesh_create_pos_and_nor_tess(MeshRenderData *rdata, GPUVertBuf *vbo, bool use_hide)
2111 {
2112         static GPUVertFormat format = { 0 };
2113         static struct { uint pos, nor; } attr_id;
2114         if (format.attr_len == 0) {
2115                 attr_id.pos = GPU_vertformat_attr_add(&format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
2116                 attr_id.nor = GPU_vertformat_attr_add(&format, "nor", GPU_COMP_I10, 3, GPU_FETCH_INT_TO_FLOAT_UNIT);
2117                 GPU_vertformat_triple_load(&format);
2118         }
2119
2120         GPU_vertbuf_init_with_format(vbo, &format);
2121
2122         const int tri_len = mesh_render_data_looptri_len_get_maybe_mapped(rdata);
2123         const int vbo_len_capacity = tri_len * 3;
2124         int vbo_len_used = 0;
2125         GPU_vertbuf_data_alloc(vbo, vbo_len_capacity);
2126
2127         GPUVertBufRaw pos_step, nor_step;
2128         GPU_vertbuf_attr_get_raw_data(vbo, attr_id.pos, &pos_step);
2129         GPU_vertbuf_attr_get_raw_data(vbo, attr_id.nor, &nor_step);
2130
2131         if (rdata->mapped.use == false) {
2132                 float (*lnors)[3] = rdata->loop_normals;
2133                 if (rdata->edit_bmesh) {
2134                         GPUPackedNormal *pnors_pack, *vnors_pack;
2135
2136                         if (lnors == NULL) {
2137                                 mesh_render_data_ensure_poly_normals_pack(rdata);
2138                                 mesh_render_data_ensure_vert_normals_pack(rdata);
2139
2140                                 pnors_pack = rdata->poly_normals_pack;
2141                                 vnors_pack = rdata->vert_normals_pack;
2142                         }
2143
2144                         for (int i = 0; i < tri_len; i++) {
2145                                 const BMLoop **bm_looptri = (const BMLoop **)rdata->edit_bmesh->looptris[i];
2146                                 const BMFace *bm_face = bm_looptri[0]->f;
2147
2148                                 /* use_hide always for edit-mode */
2149                                 if (BM_elem_flag_test(bm_face, BM_ELEM_HIDDEN)) {
2150                                         continue;
2151                                 }
2152
2153                                 if (lnors) {
2154                                         for (uint t = 0; t < 3; t++) {
2155                                                 const float *nor = lnors[BM_elem_index_get(bm_looptri[t])];
2156                                                 *((GPUPackedNormal *)GPU_vertbuf_raw_step(&nor_step)) = GPU_normal_convert_i10_v3(nor);
2157                                         }
2158                                 }
2159                                 else if (BM_elem_flag_test(bm_face, BM_ELEM_SMOOTH)) {
2160                                         for (uint t = 0; t < 3; t++) {
2161                                                 *((GPUPackedNormal *)GPU_vertbuf_raw_step(&nor_step)) = vnors_pack[BM_elem_index_get(bm_looptri[t]->v)];
2162                                         }
2163                                 }
2164                                 else {
2165                                         const GPUPackedNormal *snor_pack = &pnors_pack[BM_elem_index_get(bm_face)];
2166                                         for (uint t = 0; t < 3; t++) {
2167                                                 *((GPUPackedNormal *)GPU_vertbuf_raw_step(&nor_step)) = *snor_pack;
2168                                         }
2169                                 }
2170
2171                                 /* TODO(sybren): deduplicate this and all the other places it's pasted to in this file. */
2172                                 if (rdata->edit_data && rdata->edit_data->vertexCos) {
2173                                         for (uint t = 0; t < 3; t++) {
2174                                                 int vidx = BM_elem_index_get(bm_looptri[t]->v);
2175                                                 const float *pos = rdata->edit_data->vertexCos[vidx];
2176                                                 copy_v3_v3(GPU_vertbuf_raw_step(&pos_step), pos);
2177                                         }
2178                                 }
2179                                 else {
2180                                         for (uint t = 0; t < 3; t++) {
2181                                                 copy_v3_v3(GPU_vertbuf_raw_step(&pos_step), bm_looptri[t]->v->co);
2182                                         }
2183                                 }
2184                         }
2185                 }
2186                 else {
2187                         if (lnors == NULL) {
2188                                 /* Use normals from vertex. */
2189                                 mesh_render_data_ensure_poly_normals_pack(rdata);
2190                         }
2191
2192                         for (int i = 0; i < tri_len; i++) {
2193                                 const MLoopTri *mlt = &rdata->mlooptri[i];
2194                                 const MPoly *mp = &rdata->mpoly[mlt->poly];
2195
2196                                 if (use_hide && (mp->flag & ME_HIDE)) {
2197                                         continue;
2198                                 }
2199
2200                                 const uint vtri[3] = {
2201                                         rdata->mloop[mlt->tri[0]].v,
2202                                         rdata->mloop[mlt->tri[1]].v,
2203                                         rdata->mloop[mlt->tri[2]].v,
2204                                 };
2205
2206                                 if (lnors) {
2207                                         for (uint t = 0; t < 3; t++) {
2208                                                 const float *nor = lnors[mlt->tri[t]];
2209                                                 *((GPUPackedNormal *)GPU_vertbuf_raw_step(&nor_step)) = GPU_normal_convert_i10_v3(nor);
2210                                         }
2211                                 }
2212                                 else if (mp->flag & ME_SMOOTH) {
2213                                         for (uint t = 0; t < 3; t++) {
2214                                                 const MVert *mv = &rdata->mvert[vtri[t]];
2215                                                 *((GPUPackedNormal *)GPU_vertbuf_raw_step(&nor_step)) = GPU_normal_convert_i10_s3(mv->no);
2216                                         }
2217                                 }
2218                                 else {
2219                                         const GPUPackedNormal *pnors_pack = &rdata->poly_normals_pack[mlt->poly];
2220                                         for (uint t = 0; t < 3; t++) {
2221                                                 *((GPUPackedNormal *)GPU_vertbuf_raw_step(&nor_step)) = *pnors_pack;
2222                                         }
2223                                 }
2224
2225                                 for (uint t = 0; t < 3; t++) {
2226                                         const MVert *mv = &rdata->mvert[vtri[t]];
2227                                         copy_v3_v3(GPU_vertbuf_raw_step(&pos_step), mv->co);
2228                                 }
2229                         }
2230                 }
2231         }
2232         else {
2233                 /* Note: mapped doesn't support lnors yet. */
2234                 BMesh *bm = rdata->edit_bmesh->bm;
2235                 Mesh *me_cage = rdata->mapped.me_cage;
2236
2237                 /* TODO(campbell): unlike non-mapped modes we don't generate these on demand, just use if they exist.
2238                  * this seems like a low priority TODO since mapped meshes typically
2239                  * use the final mesh evaluated mesh for showing faces. */
2240                 const float (*lnors)[3] = CustomData_get_layer(&me_cage->ldata, CD_NORMAL);
2241
2242                 /* TODO(campbell): this is quite an expensive operation for something
2243                  * that's not used unless 'normal' display option is enabled. */
2244                 if (!CustomData_has_layer(&me_cage->pdata, CD_NORMAL)) {
2245                         /* TODO(campbell): this is quite an expensive operation for something
2246                          * that's not used unless 'normal' display option is enabled. */
2247                         BKE_mesh_ensure_normals_for_display(me_cage);
2248                 }
2249                 const float (*polynors)[3] = CustomData_get_layer(&me_cage->pdata, CD_NORMAL);
2250
2251                 const MVert *mvert = rdata->mapped.me_cage->mvert;
2252                 const MLoop *mloop = rdata->mapped.me_cage->mloop;
2253                 const MPoly *mpoly = rdata->mapped.me_cage->mpoly;
2254
2255                 const MLoopTri *mlooptri = BKE_mesh_runtime_looptri_ensure(me_cage);
2256                 for (int i = 0; i < tri_len; i++) {
2257                         const MLoopTri *mlt = &mlooptri[i];
2258                         const int p_orig = rdata->mapped.p_origindex[mlt->poly];
2259                         if (p_orig != ORIGINDEX_NONE) {
2260                                 /* Assume 'use_hide' */
2261                                 BMFace *efa = BM_face_at_index(bm, p_orig);
2262                                 if (!BM_elem_flag_test(efa, BM_ELEM_HIDDEN)) {
2263                                         const MPoly *mp = &mpoly[mlt->poly];
2264                                         const uint vtri[3] = {
2265                                                 mloop[mlt->tri[0]].v,
2266                                                 mloop[mlt->tri[1]].v,
2267                                                 mloop[mlt->tri[2]].v,
2268                                         };
2269
2270                                         if (lnors) {
2271                                                 for (uint t = 0; t < 3; t++) {
2272                                                         const float *nor = lnors[mlt->tri[t]];
2273                                                         *((GPUPackedNormal *)GPU_vertbuf_raw_step(&nor_step)) = GPU_normal_convert_i10_v3(nor);
2274                                                 }
2275                                         }
2276                                         else if (mp->flag & ME_SMOOTH) {
2277                                                 for (uint t = 0; t < 3; t++) {
2278                                                         const MVert *mv = &mvert[vtri[t]];
2279                                                         *((GPUPackedNormal *)GPU_vertbuf_raw_step(&nor_step)) = GPU_normal_convert_i10_s3(mv->no);
2280                                                 }
2281                                         }
2282                                         else {
2283                                                 /* we don't have cached 'rdata->poly_normals_pack'. */
2284                                                 const GPUPackedNormal pnor = GPU_normal_convert_i10_v3(polynors[mlt->poly]);
2285                                                 for (uint t = 0; t < 3; t++) {
2286                                                         *((GPUPackedNormal *)GPU_vertbuf_raw_step(&nor_step)) = pnor;
2287                                                 }
2288                                         }
2289
2290                                         for (uint t = 0; t < 3; t++) {
2291                                                 const MVert *mv = &mvert[vtri[t]];
2292                                                 copy_v3_v3(GPU_vertbuf_raw_step(&pos_step), mv->co);
2293                                         }
2294                                 }
2295                         }
2296                 }
2297         }
2298
2299         vbo_len_used = GPU_vertbuf_raw_used(&pos_step);
2300         BLI_assert(vbo_len_used == GPU_vertbuf_raw_used(&nor_step));
2301
2302         if (vbo_len_capacity != vbo_len_used) {
2303                 GPU_vertbuf_data_resize(vbo, vbo_len_used);
2304         }
2305 }
2306
2307 BLI_INLINE void mesh_edit_add_select_index(GPUVertBufRaw *buf, GPUVertCompType comp, uint index)
2308 {
2309         /* We don't need to sanitize the value because the elements
2310          * with these undefined values will never get drawn. */
2311         switch (comp) {
2312                 case GPU_COMP_U32:
2313                         *((uint *)GPU_vertbuf_raw_step(buf)) = index;
2314                         break;
2315                 case GPU_COMP_U16:
2316                         *((ushort *)GPU_vertbuf_raw_step(buf)) = (ushort)index;
2317                         break;
2318                 case GPU_COMP_U8:
2319                         *((uchar *)GPU_vertbuf_raw_step(buf)) = (uchar)index;
2320                         break;
2321                 default:
2322                         BLI_assert(0);
2323                         break;
2324         }
2325 }
2326
2327 #define SELECT_COMP_FORMAT(el_len) (el_len > 0xFF ? (el_len > 0xFFFF ? GPU_COMP_U32 : GPU_COMP_U16) : GPU_COMP_U8)
2328
2329 static void mesh_create_edit_vertex_loops(
2330         MeshRenderData *rdata,
2331         GPUVertBuf *vbo_pos_nor,
2332         GPUVertBuf *vbo_lnor,
2333         GPUVertBuf *vbo_uv,
2334         GPUVertBuf *vbo_data,
2335         GPUVertBuf *vbo_verts,
2336         GPUVertBuf *vbo_edges,
2337         GPUVertBuf *vbo_faces)
2338 {
2339         const int vert_len = mesh_render_data_verts_len_get_maybe_mapped(rdata);
2340         const int edge_len = mesh_render_data_edges_len_get_maybe_mapped(rdata);
2341         const int poly_len = mesh_render_data_polys_len_get_maybe_mapped(rdata);
2342         const int lvert_len = mesh_render_data_loose_verts_len_get_maybe_mapped(rdata);
2343         const int ledge_len = mesh_render_data_loose_edges_len_get_maybe_mapped(rdata);
2344         const int loop_len = mesh_render_data_loops_len_get_maybe_mapped(rdata);
2345         const int tot_loop_len = loop_len + ledge_len * 2 + lvert_len;
2346         static struct { uint vert, edge, face, pos, nor, lnor, data, uvs; } attr_id;
2347         float (*lnors)[3] = rdata->loop_normals;
2348         uchar fflag;
2349
2350         /* Choose the most compact vertex format. */
2351         GPUVertCompType vert_comp = SELECT_COMP_FORMAT(vert_len);
2352         GPUVertCompType edge_comp = SELECT_COMP_FORMAT(edge_len);
2353         GPUVertCompType face_comp = SELECT_COMP_FORMAT(poly_len);
2354
2355         GPUVertFormat format_vert_idx = { 0 }, format_edge_idx = { 0 }, format_face_idx = { 0 };
2356         attr_id.vert = GPU_vertformat_attr_add(&format_vert_idx, "color", vert_comp, 1, GPU_FETCH_INT);
2357         attr_id.edge = GPU_vertformat_attr_add(&format_edge_idx, "color", edge_comp, 1, GPU_FETCH_INT);
2358         attr_id.face = GPU_vertformat_attr_add(&format_face_idx, "color", face_comp, 1, GPU_FETCH_INT);
2359
2360         /* Static formats */
2361         static GPUVertFormat format_pos_nor = { 0 }, format_lnor = { 0 }, format_flag = { 0 }, format_uv = { 0 };
2362         if (format_pos_nor.attr_len == 0) {
2363                 attr_id.pos = GPU_vertformat_attr_add(&format_pos_nor, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
2364                 attr_id.nor = GPU_vertformat_attr_add(&format_pos_nor, "vnor", GPU_COMP_I10, 3, GPU_FETCH_INT_TO_FLOAT_UNIT);
2365                 attr_id.lnor = GPU_vertformat_attr_add(&format_lnor, "lnor", GPU_COMP_I10, 3, GPU_FETCH_INT_TO_FLOAT_UNIT);
2366                 attr_id.data = GPU_vertformat_attr_add(&format_flag, "data", GPU_COMP_U8, 4, GPU_FETCH_INT);
2367                 attr_id.uvs = GPU_vertformat_attr_add(&format_uv, "u", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
2368                 GPU_vertformat_alias_add(&format_uv, "pos");
2369                 GPU_vertformat_alias_add(&format_flag, "flag");
2370         }
2371
2372         GPUVertBufRaw raw_verts, raw_edges, raw_faces, raw_pos, raw_nor, raw_lnor, raw_uv, raw_data;
2373         if (DRW_TEST_ASSIGN_VBO(vbo_pos_nor)) {
2374                 GPU_vertbuf_init_with_format(vbo_pos_nor, &format_pos_nor);
2375                 GPU_vertbuf_data_alloc(vbo_pos_nor, tot_loop_len);
2376                 GPU_vertbuf_attr_get_raw_data(vbo_pos_nor, attr_id.pos, &raw_pos);
2377                 GPU_vertbuf_attr_get_raw_data(vbo_pos_nor, attr_id.nor, &raw_nor);
2378         }
2379         if (DRW_TEST_ASSIGN_VBO(vbo_lnor)) {
2380                 GPU_vertbuf_init_with_format(vbo_lnor, &format_lnor);
2381                 GPU_vertbuf_data_alloc(vbo_lnor, tot_loop_len);
2382                 GPU_vertbuf_attr_get_raw_data(vbo_lnor, attr_id.lnor, &raw_lnor);
2383         }
2384         if (DRW_TEST_ASSIGN_VBO(vbo_data)) {
2385                 GPU_vertbuf_init_with_format(vbo_data, &format_flag);
2386                 GPU_vertbuf_data_alloc(vbo_data, tot_loop_len);
2387                 GPU_vertbuf_attr_get_raw_data(vbo_data, attr_id.data, &raw_data);
2388         }
2389         if (DRW_TEST_ASSIGN_VBO(vbo_uv)) {
2390                 GPU_vertbuf_init_with_format(vbo_uv, &format_uv);
2391                 GPU_vertbuf_data_alloc(vbo_uv, tot_loop_len);
2392                 GPU_vertbuf_attr_get_raw_data(vbo_uv, attr_id.uvs, &raw_uv);
2393         }
2394         /* Select Idx */
2395         if (DRW_TEST_ASSIGN_VBO(vbo_verts)) {
2396                 GPU_vertbuf_init_with_format(vbo_verts, &format_vert_idx);
2397                 GPU_vertbuf_data_alloc(vbo_verts, tot_loop_len);
2398                 GPU_vertbuf_attr_get_raw_data(vbo_verts, attr_id.vert, &raw_verts);
2399         }
2400         if (DRW_TEST_ASSIGN_VBO(vbo_edges)) {
2401                 GPU_vertbuf_init_with_format(vbo_edges, &format_edge_idx);
2402                 GPU_vertbuf_data_alloc(vbo_edges, tot_loop_len);
2403                 GPU_vertbuf_attr_get_raw_data(vbo_edges, attr_id.edge, &raw_edges);
2404         }
2405         if (DRW_TEST_ASSIGN_VBO(vbo_faces)) {
2406                 GPU_vertbuf_init_with_format(vbo_faces, &format_face_idx);
2407                 GPU_vertbuf_data_alloc(vbo_faces, tot_loop_len);
2408                 GPU_vertbuf_attr_get_raw_data(vbo_faces, attr_id.face, &raw_faces);
2409         }
2410
2411         if (rdata->edit_bmesh && rdata->mapped.use == false) {
2412                 BMesh *bm = rdata->edit_bmesh->bm;
2413                 BMIter iter_efa, iter_loop, iter_vert;
2414                 BMFace *efa;
2415                 BMEdge *eed;
2416                 BMVert *eve;
2417                 BMLoop *loop;
2418                 const int cd_loop_uv_offset = CustomData_get_offset(&bm->ldata, CD_MLOOPUV);
2419
2420                 /* Face Loops */
2421                 BM_ITER_MESH (efa, &iter_efa, bm, BM_FACES_OF_MESH) {
2422                         int fidx = BM_elem_index_get(efa);
2423                         if (vbo_data) {
2424                                 fflag = mesh_render_data_face_flag(rdata, efa, cd_loop_uv_offset);
2425                         }
2426                         BM_ITER_ELEM (loop, &iter_loop, efa, BM_LOOPS_OF_FACE) {
2427                                 if (vbo_pos_nor) {
2428                                         GPUPackedNormal *vnor = (GPUPackedNormal *)GPU_vertbuf_raw_step(&raw_nor);
2429                                         *vnor = GPU_normal_convert_i10_v3(loop->v->no);
2430                                         copy_v3_v3(GPU_vertbuf_raw_step(&raw_pos), loop->v->co);
2431                                 }
2432                                 if (vbo_lnor) {
2433                                         const float *nor = (lnors) ? lnors[BM_elem_index_get(loop)] : efa->no;
2434                                         GPUPackedNormal *lnor = (GPUPackedNormal *)GPU_vertbuf_raw_step(&raw_lnor);
2435                                         *lnor = GPU_normal_convert_i10_v3(nor);
2436                                 }
2437                                 if (vbo_data) {
2438                                         EdgeDrawAttr eattr = { .v_flag = fflag };
2439                                         mesh_render_data_edge_flag(rdata, loop->e, &eattr);
2440                                         mesh_render_data_vert_flag(rdata, loop->v, &eattr);
2441                                         mesh_render_data_loop_flag(rdata, loop, cd_loop_uv_offset, &eattr);
2442                                         memcpy(GPU_vertbuf_raw_step(&raw_data), &eattr, sizeof(EdgeDrawAttr));
2443                                 }
2444                                 if (vbo_uv) {
2445                                         MLoopUV *luv = BM_ELEM_CD_GET_VOID_P(loop, cd_loop_uv_offset);
2446                                         copy_v2_v2(GPU_vertbuf_raw_step(&raw_uv), luv->uv);
2447                                 }
2448                                 /* Select Idx */
2449                                 if (vbo_verts) {
2450                                         int vidx = BM_elem_index_get(loop->v);
2451                                         mesh_edit_add_select_index(&raw_verts, vert_comp, vidx);
2452                                 }
2453                                 if (vbo_edges) {
2454                                         int eidx = BM_elem_index_get(loop->e);
2455                                         mesh_edit_add_select_index(&raw_edges, edge_comp, eidx);
2456                                 }
2457                                 if (vbo_faces) {
2458                                         mesh_edit_add_select_index(&raw_faces, face_comp, fidx);
2459                                 }
2460                         }
2461                 }
2462                 /* Loose edges */
2463                 for (int e = 0; e < ledge_len; e++) {
2464                         eed = BM_edge_at_index(bm, rdata->loose_edges[e]);
2465                         BM_ITER_ELEM (eve, &iter_vert, eed, BM_VERTS_OF_EDGE) {
2466                                 if (vbo_pos_nor) {
2467                                         GPUPackedNormal *vnor = (GPUPackedNormal *)GPU_vertbuf_raw_step(&raw_nor);
2468                                         *vnor = GPU_normal_convert_i10_v3(eve->no);
2469                                         copy_v3_v3(GPU_vertbuf_raw_step(&raw_pos), eve->co);
2470                                 }
2471                                 if (vbo_data) {
2472                                         EdgeDrawAttr eattr = { 0 };
2473                                         mesh_render_data_edge_flag(rdata, eed, &eattr);
2474                                         mesh_render_data_vert_flag(rdata, eve, &eattr);
2475                                         memcpy(GPU_vertbuf_raw_step(&raw_data), &eattr, sizeof(EdgeDrawAttr));
2476                                 }
2477                                 if (vbo_lnor) {
2478                                         memset(GPU_vertbuf_raw_step(&raw_lnor), 0, sizeof(GPUPackedNormal));
2479                                 }
2480                                 /* Select Idx */
2481                                 if (vbo_verts) {
2482                                         int vidx = BM_elem_index_get(eve);
2483                                         mesh_edit_add_select_index(&raw_verts, vert_comp, vidx);
2484                                 }
2485                                 if (vbo_edges) {
2486                                         int eidx = BM_elem_index_get(eed);
2487                                         mesh_edit_add_select_index(&raw_edges, edge_comp, eidx);
2488                                 }
2489                         }
2490                 }
2491                 /* Loose verts */
2492                 for (int e = 0; e < lvert_len; e++) {
2493                         eve = BM_vert_at_index(bm, rdata->loose_verts[e]);
2494                         if (vbo_pos_nor) {
2495                                 GPUPackedNormal *vnor = (GPUPackedNormal *)GPU_vertbuf_raw_step(&raw_nor);
2496                                 *vnor = GPU_normal_convert_i10_v3(eve->no);
2497                                 copy_v3_v3(GPU_vertbuf_raw_step(&raw_pos), eve->co);
2498                         }
2499                         if (vbo_lnor) {
2500                                 memset(GPU_vertbuf_raw_step(&raw_lnor), 0, sizeof(GPUPackedNormal));
2501                         }
2502                         if (vbo_data) {
2503                                 EdgeDrawAttr eattr = { 0 };
2504                                 mesh_render_data_vert_flag(rdata, eve, &eattr);
2505                                 memcpy(GPU_vertbuf_raw_step(&raw_data), &eattr, sizeof(EdgeDrawAttr));
2506                         }
2507                         /* Select Idx */
2508                         if (vbo_verts) {
2509                                 int vidx = BM_elem_index_get(eve);
2510                                 mesh_edit_add_select_index(&raw_verts, vert_comp, vidx);
2511                         }
2512                 }
2513         }
2514         else if (rdata->mapped.use == true) {
2515                 BMesh *bm = rdata->edit_bmesh->bm;
2516                 const int cd_loop_uv_offset = CustomData_get_offset(&bm->ldata, CD_MLOOPUV);
2517
2518                 const MPoly *mpoly = rdata->mapped.me_cage->mpoly;
2519                 const MEdge *medge = rdata->mapped.me_cage->medge;
2520                 const MVert *mvert = rdata->mapped.me_cage->mvert;
2521                 const MLoop *mloop = rdata->mapped.me_cage->mloop;
2522
2523                 const int *v_origindex = rdata->mapped.v_origindex;
2524                 const int *e_origindex = rdata->mapped.e_origindex;
2525                 const int *p_origindex = rdata->mapped.p_origindex;
2526
2527                 /* Face Loops */
2528                 for (int poly = 0; poly < poly_len; poly++, mpoly++) {
2529                         const MLoop *l = &mloop[mpoly->loopstart];
2530                         int fidx = p_origindex[poly];
2531                         BMFace *efa = NULL;
2532                         if (vbo_data) {
2533                                 fflag = 0;
2534                                 if (fidx != ORIGINDEX_NONE) {
2535                                         efa = BM_face_at_index(bm, fidx);
2536                                         fflag = mesh_render_data_face_flag(rdata, efa, cd_loop_uv_offset);
2537                                 }
2538                         }
2539                         for (int i = 0; i < mpoly->totloop; i++, l++) {
2540                                 if (vbo_pos_nor) {
2541                                         copy_v3_v3(GPU_vertbuf_raw_step(&raw_pos), mvert[l->v].co);
2542                                 }
2543                                 if (vbo_lnor || vbo_pos_nor) {
2544                                         GPUPackedNormal vnor = GPU_normal_convert_i10_s3(mvert[l->v].no);
2545                                         if (vbo_pos_nor) {
2546                                                 *(GPUPackedNormal *)GPU_vertbuf_raw_step(&raw_nor) = vnor;
2547                                         }
2548                                         if (vbo_lnor) {
2549                                                 /* Mapped does not support lnors yet. */
2550                                                 *(GPUPackedNormal *)GPU_vertbuf_raw_step(&raw_lnor) = vnor;
2551                                         }
2552                                 }
2553                                 if (vbo_data) {
2554                                         EdgeDrawAttr eattr = { .v_flag = fflag };
2555                                         int vidx = v_origindex[l->v];
2556                                         int eidx = e_origindex[l->e];
2557                                         if (vidx != ORIGINDEX_NONE) {
2558                                                 BMVert *eve = BM_vert_at_index(bm, vidx);
2559                                                 mesh_render_data_vert_flag(rdata, eve, &eattr);
2560                                         }
2561                                         if (eidx != ORIGINDEX_NONE) {
2562                                                 BMEdge *eed = BM_edge_at_index(bm, eidx);
2563                                                 mesh_render_data_edge_flag(rdata, eed, &eattr);
2564                                                 /* TODO find a more efficient way to do that. */
2565                                                 BMLoop *loop;
2566                                                 BMIter iter_loop;
2567                                                 BM_ITER_ELEM (loop, &iter_loop, efa, BM_LOOPS_OF_FACE) {
2568                                                         if (loop->e == eed) {
2569                                                                 mesh_render_data_loop_flag(rdata, loop, cd_loop_uv_offset, &eattr);
2570                                                                 break;
2571                                                         }
2572                                                 }
2573                                         }
2574                                         memcpy(GPU_vertbuf_raw_step(&raw_data), &eattr, sizeof(EdgeDrawAttr));
2575                                 }
2576                                 if (vbo_uv) {
2577                                         MLoopUV *luv = &rdata->mloopuv[mpoly->loopstart + i];
2578                                         copy_v2_v2(GPU_vertbuf_raw_step(&raw_uv), luv->uv);
2579                                 }
2580                                 /* Select Idx */
2581                                 if (vbo_verts) {
2582                                         int vidx = v_origindex[l->v];
2583                                         mesh_edit_add_select_index(&raw_verts, vert_comp, vidx);
2584                                 }
2585                                 if (vbo_edges) {
2586                                         int eidx = e_origindex[l->e];
2587                                         mesh_edit_add_select_index(&raw_edges, edge_comp, eidx);
2588                                 }
2589                                 if (vbo_faces) {
2590                                         mesh_edit_add_select_index(&raw_faces, face_comp, fidx);
2591                                 }
2592                         }
2593                 }
2594                 /* Loose edges */
2595                 for (int j = 0; j < ledge_len; j++) {
2596                         const int e = rdata->mapped.loose_edges[j];
2597                         for (int i = 0; i < 2; ++i) {
2598                                 int v = (i == 0) ? medge[e].v1 : medge[e].v2;
2599                                 if (vbo_pos_nor) {
2600                                         GPUPackedNormal vnor = GPU_normal_convert_i10_s3(mvert[v].no);
2601                                         *(GPUPackedNormal *)GPU_vertbuf_raw_step(&raw_nor) = vnor;
2602                                         copy_v3_v3(GPU_vertbuf_raw_step(&raw_pos), mvert[v].co);
2603                                 }
2604                                 if (vbo_lnor) {
2605                                         memset(GPU_vertbuf_raw_step(&raw_lnor), 0, sizeof(GPUPackedNormal));
2606                                 }
2607                                 if (vbo_data) {
2608                                         EdgeDrawAttr eattr = { 0 };
2609                                         int vidx = v_origindex[v];
2610                                         int eidx = e_origindex[e];
2611                                         if (vidx != ORIGINDEX_NONE) {
2612                                                 BMVert *eve = BM_vert_at_index(bm, vidx);
2613                                                 mesh_render_data_vert_flag(rdata, eve, &eattr);
2614                                         }
2615                                         if (eidx != ORIGINDEX_NONE) {
2616                                                 BMEdge *eed = BM_edge_at_index(bm, eidx);
2617                                                 mesh_render_data_edge_flag(rdata, eed, &eattr);
2618                                         }
2619                                         memcpy(GPU_vertbuf_raw_step(&raw_data), &eattr, sizeof(EdgeDrawAttr));
2620                                 }
2621                                 /* Select Idx */
2622                                 if (vbo_verts) {
2623                                         int vidx = v_origindex[v];
2624                                         mesh_edit_add_select_index(&raw_verts, vert_comp, vidx);
2625                                 }
2626                                 if (vbo_edges) {
2627                                         int eidx = e_origindex[e];
2628                                         mesh_edit_add_select_index(&raw_edges, edge_comp, eidx);
2629                                 }
2630                         }
2631                 }
2632                 /* Loose verts */
2633                 for (int i = 0; i < lvert_len; i++) {
2634                         const int v = rdata->mapped.loose_verts[i];
2635                         if (vbo_pos_nor) {
2636                                 GPUPackedNormal vnor = GPU_normal_convert_i10_s3(mvert[v].no);
2637                                 *(GPUPackedNormal *)GPU_vertbuf_raw_step(&raw_nor) = vnor;
2638                                 copy_v3_v3(GPU_vertbuf_raw_step(&raw_pos), mvert[v].co);
2639                         }
2640                         if (vbo_lnor) {
2641                                 memset(GPU_vertbuf_raw_step(&raw_lnor), 0, sizeof(GPUPackedNormal));
2642                         }
2643                         if (vbo_data) {
2644                                 EdgeDrawAttr eattr = { 0 };
2645                                 int vidx = v_origindex[v];
2646                                 if (vidx != ORIGINDEX_NONE) {
2647                                         BMVert *eve = BM_vert_at_index(bm, vidx);
2648                                         mesh_render_data_vert_flag(rdata, eve, &eattr);
2649                                 }
2650                                 memcpy(GPU_vertbuf_raw_step(&raw_data), &eattr, sizeof(EdgeDrawAttr));
2651                         }
2652                         /* Select Idx */
2653                         if (vbo_verts) {
2654                                 int vidx = v_origindex[v];
2655                                 mesh_edit_add_select_index(&raw_verts, vert_comp, vidx);
2656                         }
2657                 }
2658         }
2659         else {
2660                 const MPoly *mpoly = rdata->mpoly;
2661                 const MVert *mvert = rdata->mvert;
2662                 const MLoop *mloop = rdata->mloop;
2663
2664                 const int *v_origindex = CustomData_get_layer(&rdata->me->vdata, CD_ORIGINDEX);
2665                 const int *e_origindex = CustomData_get_layer(&rdata->me->edata, CD_ORIGINDEX);
2666                 const int *p_origindex = CustomData_get_layer(&rdata->me->pdata, CD_ORIGINDEX);
2667
2668                 /* Face Loops */
2669                 for (int poly = 0; poly < poly_len; poly++, mpoly++) {
2670                         const MLoop *l = &mloop[mpoly->loopstart];
2671                         int fidx = p_origindex ? p_origindex[poly] : poly;
2672                         for (int i = 0; i < mpoly->totloop; i++, l++) {
2673                                 if (vbo_pos_nor) {
2674                                         copy_v3_v3(GPU_vertbuf_raw_step(&raw_pos), mvert[l->v].co);
2675                                 }
2676                                 if (vbo_lnor || vbo_pos_nor) {
2677                                         GPUPackedNormal vnor = GPU_normal_convert_i10_s3(mvert[l->v].no);
2678                                         if (vbo_pos_nor) {
2679                                                 *(GPUPackedNormal *)GPU_vertbuf_raw_step(&raw_nor) = vnor;
2680                                         }
2681                                         if (vbo_lnor) {
2682                                                 /* Mapped does not support lnors yet. */
2683                                                 *(GPUPackedNormal *)GPU_vertbuf_raw_step(&raw_lnor) = vnor;
2684                                         }
2685                                 }
2686                                 if (vbo_uv) {
2687                                         MLoopUV *luv = &rdata->mloopuv[mpoly->loopstart + i];
2688                                         copy_v2_v2(GPU_vertbuf_raw_step(&raw_uv), luv->uv);
2689                                 }
2690                                 /* Select Idx */
2691                                 if (vbo_verts) {
2692                                         int vidx = v_origindex ? v_origindex[l->v] : l->v;
2693                                         mesh_edit_add_select_index(&raw_verts, vert_comp, vidx);
2694                                 }
2695                                 if (vbo_edges) {
2696                                         int eidx = e_origindex ? e_origindex[l->e] : l->e;
2697                                         mesh_edit_add_select_index(&raw_edges, edge_comp, eidx);
2698                                 }
2699                                 if (vbo_faces) {
2700                                         mesh_edit_add_select_index(&raw_faces, face_comp, fidx);
2701                                 }
2702                         }
2703                 }
2704                 /* TODO(fclem): Until we find a way to detect
2705                  * loose verts easily outside of edit mode, this
2706                  * will remain disabled. */
2707 #if 0
2708                 /* Loose edges */
2709                 for (int e = 0; e < edge_len; e++, medge++) {
2710                         int eidx = e_origindex[e];
2711                         if (eidx != ORIGINDEX_NONE && (medge->flag & ME_LOOSEEDGE)) {
2712                                 for (int i = 0; i < 2; ++i) {
2713                                         int vidx = (i == 0) ? medge->v1 : medge->v2;
2714                                         if (vbo_pos) {
2715                                                 copy_v3_v3(GPU_vertbuf_raw_step(&raw_pos), mvert[vidx].co);
2716                                         }
2717                                         if (vbo_verts) {
2718                                                 mesh_edit_add_select_index(&raw_verts, vert_comp, vidx);
2719                                         }
2720                                         if (vbo_edges) {
2721                                                 mesh_edit_add_select_index(&raw_edges, edge_comp, eidx);
2722                                         }
2723                                 }
2724                         }
2725                 }
2726                 /* Loose verts */
2727                 for (int v = 0; v < vert_len; v++, mvert++) {
2728                         int vidx = v_origindex[v];
2729                         if (vidx != ORIGINDEX_NONE) {
2730                                 MVert *eve = BM_vert_at_index(bm, vidx);
2731                                 if (eve->e == NULL) {
2732                                         if (vbo_pos) {
2733                                                 copy_v3_v3(GPU_vertbuf_raw_step(&raw_pos), mvert->co);
2734                                         }
2735                                         if (vbo_verts) {
2736                                                 mesh_edit_add_select_index(&raw_verts, vert_comp, vidx);
2737                                         }
2738                                 }
2739                         }
2740                 }
2741 #endif
2742         }
2743         /* Don't resize */
2744 }
2745
2746 /* TODO: We could use gl_PrimitiveID as index instead of using another VBO. */
2747 static void mesh_create_edit_facedots_select_id(
2748         MeshRenderData *rdata,
2749         GPUVertBuf *vbo)
2750 {
2751         const int poly_len = mesh_render_data_polys_len_get_maybe_mapped(rdata);
2752
2753         GPUVertCompType comp = SELECT_COMP_FORMAT(poly_len);
2754
2755         GPUVertFormat format = { 0 };
2756         struct { uint idx; } attr_id;
2757         attr_id.idx = GPU_vertformat_attr_add(&format, "color", comp, 1, GPU_FETCH_INT);
2758
2759         GPUVertBufRaw idx_step;
2760         GPU_vertbuf_init_with_format(vbo, &format);
2761         GPU_vertbuf_data_alloc(vbo, poly_len);
2762         GPU_vertbuf_attr_get_raw_data(vbo, attr_id.idx, &idx_step);
2763
2764         /* Keep in sync with mesh_create_edit_facedots(). */
2765         if (rdata->mapped.use == false) {
2766                 if (rdata->edit_bmesh) {
2767                         for (int poly = 0; poly < poly_len; poly++) {
2768                                 const BMFace *efa = BM_face_at_index(rdata->edit_bmesh->bm, poly);
2769                                 if (!BM_elem_flag_test(efa, BM_ELEM_HIDDEN)) {
2770                                         mesh_edit_add_select_index(&idx_step, comp, poly);
2771                                 }
2772                         }
2773                 }
2774                 else {
2775                         for (int poly = 0; poly < poly_len; poly++) {
2776                                 mesh_edit_add_select_index(&idx_step, comp, poly);
2777                         }
2778                 }
2779         }
2780         else {
2781                 const int *p_origindex = rdata->mapped.p_origindex;
2782                 for (int poly = 0; poly < poly_len; poly++) {
2783                         const int p_orig = p_origindex[poly];
2784                         if (p_orig != ORIGINDEX_NONE) {
2785                                 const BMFace *efa = BM_face_at_index(rdata->edit_bmesh->bm, p_orig);
2786                                 if (!BM_elem_flag_test(efa, BM_ELEM_HIDDEN)) {
2787                                         mesh_edit_add_select_index(&idx_step, comp, poly);
2788                                 }
2789                         }
2790                 }
2791         }
2792
2793         /* Resize & Finish */
2794         int facedot_len_used = GPU_vertbuf_raw_used(&idx_step);
2795         if (facedot_len_used != poly_len) {
2796                 GPU_vertbuf_data_resize(vbo, facedot_len_used);
2797         }
2798 }
2799 #undef SELECT_COMP_FORMAT
2800
2801 static void mesh_create_pos_and_nor(MeshRenderData *rdata, GPUVertBuf *vbo)
2802 {
2803         static GPUVertFormat format = { 0 };
2804         static struct { uint pos, nor; } attr_id;
2805         if (format.attr_len == 0) {
2806                 attr_id.pos = GPU_vertformat_attr_add(&format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
2807                 attr_id.nor = GPU_vertformat_attr_add(&format, "nor", GPU_COMP_I10, 4, GPU_FETCH_INT_TO_FLOAT_UNIT);
2808         }
2809
2810         GPU_vertbuf_init_with_format(vbo, &format);
2811         const int vbo_len_capacity = mesh_render_data_verts_len_get_maybe_mapped(rdata);
2812         GPU_vertbuf_data_alloc(vbo, vbo_len_capacity);
2813
2814         if (rdata->mapped.use == false) {
2815                 if (rdata->edit_bmesh) {
2816                         BMesh *bm = rdata->edit_bmesh->bm;
2817                         BMIter iter;
2818                         BMVert *eve;
2819                         uint i;
2820
2821                         mesh_render_data_ensure_vert_normals_pack(rdata);
2822                         GPUPackedNormal *vnor = rdata->vert_normals_pack;
2823
2824                         BM_ITER_MESH_INDEX (eve, &iter, bm, BM_VERTS_OF_MESH, i) {
2825                                 GPU_vertbuf_attr_set(vbo, attr_id.pos, i, eve->co);
2826                                 GPU_vertbuf_attr_set(vbo, attr_id.nor, i, &vnor[i]);
2827                         }
2828                         BLI_assert(i == vbo_len_capacity);
2829                 }
2830                 else {
2831                         for (int i =