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

mesh_tangent.c « intern « blenkernel « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c0b2b33c47c4a6f1c860dee243e2a4dfca1581ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
/* SPDX-License-Identifier: GPL-2.0-or-later
 * Copyright 2001-2002 NaN Holding BV. All rights reserved. */

/** \file
 * \ingroup bke
 *
 * Functions to evaluate mesh tangents.
 */

#include <limits.h>

#include "MEM_guardedalloc.h"

#include "DNA_mesh_types.h"
#include "DNA_meshdata_types.h"

#include "BLI_math.h"
#include "BLI_task.h"
#include "BLI_utildefines.h"

#include "BKE_customdata.h"
#include "BKE_mesh.h"
#include "BKE_mesh_runtime.h"
#include "BKE_mesh_tangent.h"
#include "BKE_report.h"

#include "BLI_strict_flags.h"

#include "atomic_ops.h"
#include "mikktspace.h"

/* -------------------------------------------------------------------- */
/** \name Mesh Tangent Calculations (Single Layer)
 * \{ */

/* Tangent space utils. */

/* User data. */
typedef struct {
  const MPoly *mpolys;     /* faces */
  const MLoop *mloops;     /* faces's vertices */
  const MVert *mverts;     /* vertices */
  const MLoopUV *luvs;     /* texture coordinates */
  const float (*lnors)[3]; /* loops' normals */
  float (*tangents)[4];    /* output tangents */
  int num_polys;           /* number of polygons */
} BKEMeshToTangent;

/* Mikktspace's API */
static int get_num_faces(const SMikkTSpaceContext *pContext)
{
  BKEMeshToTangent *p_mesh = (BKEMeshToTangent *)pContext->m_pUserData;
  return p_mesh->num_polys;
}

static int get_num_verts_of_face(const SMikkTSpaceContext *pContext, const int face_idx)
{
  BKEMeshToTangent *p_mesh = (BKEMeshToTangent *)pContext->m_pUserData;
  return p_mesh->mpolys[face_idx].totloop;
}

static void get_position(const SMikkTSpaceContext *pContext,
                         float r_co[3],
                         const int face_idx,
                         const int vert_idx)
{
  BKEMeshToTangent *p_mesh = (BKEMeshToTangent *)pContext->m_pUserData;
  const int loop_idx = p_mesh->mpolys[face_idx].loopstart + vert_idx;
  copy_v3_v3(r_co, p_mesh->mverts[p_mesh->mloops[loop_idx].v].co);
}

static void get_texture_coordinate(const SMikkTSpaceContext *pContext,
                                   float r_uv[2],
                                   const int face_idx,
                                   const int vert_idx)
{
  BKEMeshToTangent *p_mesh = (BKEMeshToTangent *)pContext->m_pUserData;
  copy_v2_v2(r_uv, p_mesh->luvs[p_mesh->mpolys[face_idx].loopstart + vert_idx].uv);
}

static void get_normal(const SMikkTSpaceContext *pContext,
                       float r_no[3],
                       const int face_idx,
                       const int vert_idx)
{
  BKEMeshToTangent *p_mesh = (BKEMeshToTangent *)pContext->m_pUserData;
  copy_v3_v3(r_no, p_mesh->lnors[p_mesh->mpolys[face_idx].loopstart + vert_idx]);
}

static void set_tspace(const SMikkTSpaceContext *pContext,
                       const float fv_tangent[3],
                       const float face_sign,
                       const int face_idx,
                       const int vert_idx)
{
  BKEMeshToTangent *p_mesh = (BKEMeshToTangent *)pContext->m_pUserData;
  float *p_res = p_mesh->tangents[p_mesh->mpolys[face_idx].loopstart + vert_idx];
  copy_v3_v3(p_res, fv_tangent);
  p_res[3] = face_sign;
}

void BKE_mesh_calc_loop_tangent_single_ex(const MVert *mverts,
                                          const int UNUSED(numVerts),
                                          const MLoop *mloops,
                                          float (*r_looptangent)[4],
                                          const float (*loopnors)[3],
                                          const MLoopUV *loopuvs,
                                          const int UNUSED(numLoops),
                                          const MPoly *mpolys,
                                          const int numPolys,
                                          ReportList *reports)
{
  BKEMeshToTangent mesh_to_tangent = {NULL};
  SMikkTSpaceContext s_context = {NULL};
  SMikkTSpaceInterface s_interface = {NULL};

  const MPoly *mp;
  int mp_index;

  /* First check we do have a tris/quads only mesh. */
  for (mp = mpolys, mp_index = 0; mp_index < numPolys; mp++, mp_index++) {
    if (mp->totloop > 4) {
      BKE_report(
          reports, RPT_ERROR, "Tangent space can only be computed for tris/quads, aborting");
      return;
    }
  }

  /* Compute Mikktspace's tangent normals. */
  mesh_to_tangent.mpolys = mpolys;
  mesh_to_tangent.mloops = mloops;
  mesh_to_tangent.mverts = mverts;
  mesh_to_tangent.luvs = loopuvs;
  mesh_to_tangent.lnors = loopnors;
  mesh_to_tangent.tangents = r_looptangent;
  mesh_to_tangent.num_polys = numPolys;

  s_context.m_pUserData = &mesh_to_tangent;
  s_context.m_pInterface = &s_interface;
  s_interface.m_getNumFaces = get_num_faces;
  s_interface.m_getNumVerticesOfFace = get_num_verts_of_face;
  s_interface.m_getPosition = get_position;
  s_interface.m_getTexCoord = get_texture_coordinate;
  s_interface.m_getNormal = get_normal;
  s_interface.m_setTSpaceBasic = set_tspace;

  /* 0 if failed */
  if (genTangSpaceDefault(&s_context) == false) {
    BKE_report(reports, RPT_ERROR, "Mikktspace failed to generate tangents for this mesh!");
  }
}

void BKE_mesh_calc_loop_tangent_single(Mesh *mesh,
                                       const char *uvmap,
                                       float (*r_looptangents)[4],
                                       ReportList *reports)
{
  const MLoopUV *loopuvs;

  /* Check we have valid texture coordinates first! */
  if (uvmap) {
    loopuvs = CustomData_get_layer_named(&mesh->ldata, CD_MLOOPUV, uvmap);
  }
  else {
    loopuvs = CustomData_get_layer(&mesh->ldata, CD_MLOOPUV);
  }
  if (!loopuvs) {
    BKE_reportf(reports,
                RPT_ERROR,
                "Tangent space computation needs an UVMap, \"%s\" not found, aborting",
                uvmap);
    return;
  }

  const float(*loopnors)[3] = CustomData_get_layer(&mesh->ldata, CD_NORMAL);
  if (!loopnors) {
    BKE_report(
        reports, RPT_ERROR, "Tangent space computation needs loop normals, none found, aborting");
    return;
  }

  BKE_mesh_calc_loop_tangent_single_ex(mesh->mvert,
                                       mesh->totvert,
                                       mesh->mloop,
                                       r_looptangents,
                                       loopnors,
                                       loopuvs,
                                       mesh->totloop,
                                       mesh->mpoly,
                                       mesh->totpoly,
                                       reports);
}

/** \} */

/* -------------------------------------------------------------------- */
/** \name Mesh Tangent Calculations (All Layers)
 * \{ */

/* Necessary complexity to handle looptri's as quads for correct tangents */
#define USE_LOOPTRI_DETECT_QUADS

typedef struct {
  const float (*precomputedFaceNormals)[3];
  const float (*precomputedLoopNormals)[3];
  const MLoopTri *looptri;
  const MLoopUV *mloopuv; /* texture coordinates */
  const MPoly *mpoly;     /* indices */
  const MLoop *mloop;     /* indices */
  const MVert *mvert;     /* vertex coordinates */
  const float (*vert_normals)[3];
  const float (*orco)[3];
  float (*tangent)[4]; /* destination */
  int numTessFaces;

#ifdef USE_LOOPTRI_DETECT_QUADS
  /* map from 'fake' face index to looptri,
   * quads will point to the first looptri of the quad */
  const int *face_as_quad_map;
  int num_face_as_quad_map;
#endif

} SGLSLMeshToTangent;

/* interface */
static int dm_ts_GetNumFaces(const SMikkTSpaceContext *pContext)
{
  SGLSLMeshToTangent *pMesh = pContext->m_pUserData;

#ifdef USE_LOOPTRI_DETECT_QUADS
  return pMesh->num_face_as_quad_map;
#else
  return pMesh->numTessFaces;
#endif
}

static int dm_ts_GetNumVertsOfFace(const SMikkTSpaceContext *pContext, const int face_num)
{
#ifdef USE_LOOPTRI_DETECT_QUADS
  SGLSLMeshToTangent *pMesh = pContext->m_pUserData;
  if (pMesh->face_as_quad_map) {
    const MLoopTri *lt = &pMesh->looptri[pMesh->face_as_quad_map[face_num]];
    const MPoly *mp = &pMesh->mpoly[lt->poly];
    if (mp->totloop == 4) {
      return 4;
    }
  }
  return 3;
#else
  UNUSED_VARS(pContext, face_num);
  return 3;
#endif
}

static void dm_ts_GetPosition(const SMikkTSpaceContext *pContext,
                              float r_co[3],
                              const int face_num,
                              const int vert_index)
{
  // assert(vert_index >= 0 && vert_index < 4);
  SGLSLMeshToTangent *pMesh = pContext->m_pUserData;
  const MLoopTri *lt;
  uint loop_index;
  const float *co;

#ifdef USE_LOOPTRI_DETECT_QUADS
  if (pMesh->face_as_quad_map) {
    lt = &pMesh->looptri[pMesh->face_as_quad_map[face_num]];
    const MPoly *mp = &pMesh->mpoly[lt->poly];
    if (mp->totloop == 4) {
      loop_index = (uint)(mp->loopstart + vert_index);
      goto finally;
    }
    /* fall through to regular triangle */
  }
  else {
    lt = &pMesh->looptri[face_num];
  }
#else
  lt = &pMesh->looptri[face_num];
#endif
  loop_index = lt->tri[vert_index];

finally:
  co = pMesh->mvert[pMesh->mloop[loop_index].v].co;
  copy_v3_v3(r_co, co);
}

static void dm_ts_GetTextureCoordinate(const SMikkTSpaceContext *pContext,
                                       float r_uv[2],
                                       const int face_num,
                                       const int vert_index)
{
  // assert(vert_index >= 0 && vert_index < 4);
  SGLSLMeshToTangent *pMesh = pContext->m_pUserData;
  const MLoopTri *lt;
  uint loop_index;

#ifdef USE_LOOPTRI_DETECT_QUADS
  if (pMesh->face_as_quad_map) {
    lt = &pMesh->looptri[pMesh->face_as_quad_map[face_num]];
    const MPoly *mp = &pMesh->mpoly[lt->poly];
    if (mp->totloop == 4) {
      loop_index = (uint)(mp->loopstart + vert_index);
      goto finally;
    }
    /* fall through to regular triangle */
  }
  else {
    lt = &pMesh->looptri[face_num];
  }
#else
  lt = &pMesh->looptri[face_num];
#endif
  loop_index = lt->tri[vert_index];

finally:
  if (pMesh->mloopuv != NULL) {
    const float *uv = pMesh->mloopuv[loop_index].uv;
    copy_v2_v2(r_uv, uv);
  }
  else {
    const float *orco = pMesh->orco[pMesh->mloop[loop_index].v];
    map_to_sphere(&r_uv[0], &r_uv[1], orco[0], orco[1], orco[2]);
  }
}

static void dm_ts_GetNormal(const SMikkTSpaceContext *pContext,
                            float r_no[3],
                            const int face_num,
                            const int vert_index)
{
  // assert(vert_index >= 0 && vert_index < 4);
  SGLSLMeshToTangent *pMesh = (SGLSLMeshToTangent *)pContext->m_pUserData;
  const MLoopTri *lt;
  uint loop_index;

#ifdef USE_LOOPTRI_DETECT_QUADS
  if (pMesh->face_as_quad_map) {
    lt = &pMesh->looptri[pMesh->face_as_quad_map[face_num]];
    const MPoly *mp = &pMesh->mpoly[lt->poly];
    if (mp->totloop == 4) {
      loop_index = (uint)(mp->loopstart + vert_index);
      goto finally;
    }
    /* fall through to regular triangle */
  }
  else {
    lt = &pMesh->looptri[face_num];
  }
#else
  lt = &pMesh->looptri[face_num];
#endif
  loop_index = lt->tri[vert_index];

finally:
  if (pMesh->precomputedLoopNormals) {
    copy_v3_v3(r_no, pMesh->precomputedLoopNormals[loop_index]);
  }
  else if ((pMesh->mpoly[lt->poly].flag & ME_SMOOTH) == 0) { /* flat */
    if (pMesh->precomputedFaceNormals) {
      copy_v3_v3(r_no, pMesh->precomputedFaceNormals[lt->poly]);
    }
    else {
#ifdef USE_LOOPTRI_DETECT_QUADS
      const MPoly *mp = &pMesh->mpoly[lt->poly];
      if (mp->totloop == 4) {
        normal_quad_v3(r_no,
                       pMesh->mvert[pMesh->mloop[mp->loopstart + 0].v].co,
                       pMesh->mvert[pMesh->mloop[mp->loopstart + 1].v].co,
                       pMesh->mvert[pMesh->mloop[mp->loopstart + 2].v].co,
                       pMesh->mvert[pMesh->mloop[mp->loopstart + 3].v].co);
      }
      else
#endif
      {
        normal_tri_v3(r_no,
                      pMesh->mvert[pMesh->mloop[lt->tri[0]].v].co,
                      pMesh->mvert[pMesh->mloop[lt->tri[1]].v].co,
                      pMesh->mvert[pMesh->mloop[lt->tri[2]].v].co);
      }
    }
  }
  else {
    copy_v3_v3(r_no, pMesh->vert_normals[pMesh->mloop[loop_index].v]);
  }
}

static void dm_ts_SetTSpace(const SMikkTSpaceContext *pContext,
                            const float fvTangent[3],
                            const float fSign,
                            const int face_num,
                            const int vert_index)
{
  // assert(vert_index >= 0 && vert_index < 4);
  SGLSLMeshToTangent *pMesh = (SGLSLMeshToTangent *)pContext->m_pUserData;
  const MLoopTri *lt;
  uint loop_index;

#ifdef USE_LOOPTRI_DETECT_QUADS
  if (pMesh->face_as_quad_map) {
    lt = &pMesh->looptri[pMesh->face_as_quad_map[face_num]];
    const MPoly *mp = &pMesh->mpoly[lt->poly];
    if (mp->totloop == 4) {
      loop_index = (uint)(mp->loopstart + vert_index);
      goto finally;
    }
    /* fall through to regular triangle */
  }
  else {
    lt = &pMesh->looptri[face_num];
  }
#else
  lt = &pMesh->looptri[face_num];
#endif
  loop_index = lt->tri[vert_index];

  float *pRes;

finally:
  pRes = pMesh->tangent[loop_index];
  copy_v3_v3(pRes, fvTangent);
  pRes[3] = fSign;
}

static void DM_calc_loop_tangents_thread(TaskPool *__restrict UNUSED(pool), void *taskdata)
{
  struct SGLSLMeshToTangent *mesh2tangent = taskdata;
  /* new computation method */
  {
    SMikkTSpaceContext sContext = {NULL};
    SMikkTSpaceInterface sInterface = {NULL};

    sContext.m_pUserData = mesh2tangent;
    sContext.m_pInterface = &sInterface;
    sInterface.m_getNumFaces = dm_ts_GetNumFaces;
    sInterface.m_getNumVerticesOfFace = dm_ts_GetNumVertsOfFace;
    sInterface.m_getPosition = dm_ts_GetPosition;
    sInterface.m_getTexCoord = dm_ts_GetTextureCoordinate;
    sInterface.m_getNormal = dm_ts_GetNormal;
    sInterface.m_setTSpaceBasic = dm_ts_SetTSpace;

    /* 0 if failed */
    genTangSpaceDefault(&sContext);
  }
}

void BKE_mesh_add_loop_tangent_named_layer_for_uv(CustomData *uv_data,
                                                  CustomData *tan_data,
                                                  int numLoopData,
                                                  const char *layer_name)
{
  if (CustomData_get_named_layer_index(tan_data, CD_TANGENT, layer_name) == -1 &&
      CustomData_get_named_layer_index(uv_data, CD_MLOOPUV, layer_name) != -1) {
    CustomData_add_layer_named(tan_data, CD_TANGENT, CD_CALLOC, NULL, numLoopData, layer_name);
  }
}

void BKE_mesh_calc_loop_tangent_step_0(const CustomData *loopData,
                                       bool calc_active_tangent,
                                       const char (*tangent_names)[MAX_NAME],
                                       int tangent_names_count,
                                       bool *rcalc_act,
                                       bool *rcalc_ren,
                                       int *ract_uv_n,
                                       int *rren_uv_n,
                                       char *ract_uv_name,
                                       char *rren_uv_name,
                                       short *rtangent_mask)
{
  /* Active uv in viewport */
  int layer_index = CustomData_get_layer_index(loopData, CD_MLOOPUV);
  *ract_uv_n = CustomData_get_active_layer(loopData, CD_MLOOPUV);
  ract_uv_name[0] = 0;
  if (*ract_uv_n != -1) {
    strcpy(ract_uv_name, loopData->layers[*ract_uv_n + layer_index].name);
  }

  /* Active tangent in render */
  *rren_uv_n = CustomData_get_render_layer(loopData, CD_MLOOPUV);
  rren_uv_name[0] = 0;
  if (*rren_uv_n != -1) {
    strcpy(rren_uv_name, loopData->layers[*rren_uv_n + layer_index].name);
  }

  /* If active tangent not in tangent_names we take it into account */
  *rcalc_act = false;
  *rcalc_ren = false;
  for (int i = 0; i < tangent_names_count; i++) {
    if (tangent_names[i][0] == 0) {
      calc_active_tangent = true;
    }
  }
  if (calc_active_tangent) {
    *rcalc_act = true;
    *rcalc_ren = true;
    for (int i = 0; i < tangent_names_count; i++) {
      if (STREQ(ract_uv_name, tangent_names[i])) {
        *rcalc_act = false;
      }
      if (STREQ(rren_uv_name, tangent_names[i])) {
        *rcalc_ren = false;
      }
    }
  }
  *rtangent_mask = 0;

  const int uv_layer_num = CustomData_number_of_layers(loopData, CD_MLOOPUV);
  for (int n = 0; n < uv_layer_num; n++) {
    const char *name = CustomData_get_layer_name(loopData, CD_MLOOPUV, n);
    bool add = false;
    for (int i = 0; i < tangent_names_count; i++) {
      if (tangent_names[i][0] && STREQ(tangent_names[i], name)) {
        add = true;
        break;
      }
    }
    if (!add && ((*rcalc_act && ract_uv_name[0] && STREQ(ract_uv_name, name)) ||
                 (*rcalc_ren && rren_uv_name[0] && STREQ(rren_uv_name, name)))) {
      add = true;
    }
    if (add) {
      *rtangent_mask |= (short)(1 << n);
    }
  }

  if (uv_layer_num == 0) {
    *rtangent_mask |= DM_TANGENT_MASK_ORCO;
  }
}

void BKE_mesh_calc_loop_tangent_ex(const MVert *mvert,
                                   const MPoly *mpoly,
                                   const uint mpoly_len,
                                   const MLoop *mloop,
                                   const MLoopTri *looptri,
                                   const uint looptri_len,

                                   CustomData *loopdata,
                                   bool calc_active_tangent,
                                   const char (*tangent_names)[MAX_NAME],
                                   int tangent_names_len,
                                   const float (*vert_normals)[3],
                                   const float (*poly_normals)[3],
                                   const float (*loop_normals)[3],
                                   const float (*vert_orco)[3],
                                   /* result */
                                   CustomData *loopdata_out,
                                   const uint loopdata_out_len,
                                   short *tangent_mask_curr_p)
{
  int act_uv_n = -1;
  int ren_uv_n = -1;
  bool calc_act = false;
  bool calc_ren = false;
  char act_uv_name[MAX_NAME];
  char ren_uv_name[MAX_NAME];
  short tangent_mask = 0;
  short tangent_mask_curr = *tangent_mask_curr_p;

  BKE_mesh_calc_loop_tangent_step_0(loopdata,
                                    calc_active_tangent,
                                    tangent_names,
                                    tangent_names_len,
                                    &calc_act,
                                    &calc_ren,
                                    &act_uv_n,
                                    &ren_uv_n,
                                    act_uv_name,
                                    ren_uv_name,
                                    &tangent_mask);
  if ((tangent_mask_curr | tangent_mask) != tangent_mask_curr) {
    /* Check we have all the needed layers */
    /* Allocate needed tangent layers */
    for (int i = 0; i < tangent_names_len; i++) {
      if (tangent_names[i][0]) {
        BKE_mesh_add_loop_tangent_named_layer_for_uv(
            loopdata, loopdata_out, (int)loopdata_out_len, tangent_names[i]);
      }
    }
    if ((tangent_mask & DM_TANGENT_MASK_ORCO) &&
        CustomData_get_named_layer_index(loopdata, CD_TANGENT, "") == -1) {
      CustomData_add_layer_named(
          loopdata_out, CD_TANGENT, CD_CALLOC, NULL, (int)loopdata_out_len, "");
    }
    if (calc_act && act_uv_name[0]) {
      BKE_mesh_add_loop_tangent_named_layer_for_uv(
          loopdata, loopdata_out, (int)loopdata_out_len, act_uv_name);
    }
    if (calc_ren && ren_uv_name[0]) {
      BKE_mesh_add_loop_tangent_named_layer_for_uv(
          loopdata, loopdata_out, (int)loopdata_out_len, ren_uv_name);
    }

#ifdef USE_LOOPTRI_DETECT_QUADS
    int num_face_as_quad_map;
    int *face_as_quad_map = NULL;

    /* map faces to quads */
    if (looptri_len != mpoly_len) {
      /* Over allocate, since we don't know how many ngon or quads we have. */

      /* map fake face index to looptri */
      face_as_quad_map = MEM_mallocN(sizeof(int) * looptri_len, __func__);
      int k, j;
      for (k = 0, j = 0; j < (int)looptri_len; k++, j++) {
        face_as_quad_map[k] = j;
        /* step over all quads */
        if (mpoly[looptri[j].poly].totloop == 4) {
          j++; /* skips the nest looptri */
        }
      }
      num_face_as_quad_map = k;
    }
    else {
      num_face_as_quad_map = (int)looptri_len;
    }
#endif

    /* Calculation */
    if (looptri_len != 0) {
      TaskPool *task_pool = BLI_task_pool_create(NULL, TASK_PRIORITY_HIGH);

      tangent_mask_curr = 0;
      /* Calculate tangent layers */
      SGLSLMeshToTangent data_array[MAX_MTFACE];
      const int tangent_layer_num = CustomData_number_of_layers(loopdata_out, CD_TANGENT);
      for (int n = 0; n < tangent_layer_num; n++) {
        int index = CustomData_get_layer_index_n(loopdata_out, CD_TANGENT, n);
        BLI_assert(n < MAX_MTFACE);
        SGLSLMeshToTangent *mesh2tangent = &data_array[n];
        mesh2tangent->numTessFaces = (int)looptri_len;
#ifdef USE_LOOPTRI_DETECT_QUADS
        mesh2tangent->face_as_quad_map = face_as_quad_map;
        mesh2tangent->num_face_as_quad_map = num_face_as_quad_map;
#endif
        mesh2tangent->mvert = mvert;
        mesh2tangent->vert_normals = vert_normals;
        mesh2tangent->mpoly = mpoly;
        mesh2tangent->mloop = mloop;
        mesh2tangent->looptri = looptri;
        /* NOTE: we assume we do have tessellated loop normals at this point
         * (in case it is object-enabled), have to check this is valid. */
        mesh2tangent->precomputedLoopNormals = loop_normals;
        mesh2tangent->precomputedFaceNormals = poly_normals;

        mesh2tangent->orco = NULL;
        mesh2tangent->mloopuv = CustomData_get_layer_named(
            loopdata, CD_MLOOPUV, loopdata_out->layers[index].name);

        /* Fill the resulting tangent_mask */
        if (!mesh2tangent->mloopuv) {
          mesh2tangent->orco = vert_orco;
          if (!mesh2tangent->orco) {
            continue;
          }

          tangent_mask_curr |= DM_TANGENT_MASK_ORCO;
        }
        else {
          int uv_ind = CustomData_get_named_layer_index(
              loopdata, CD_MLOOPUV, loopdata_out->layers[index].name);
          int uv_start = CustomData_get_layer_index(loopdata, CD_MLOOPUV);
          BLI_assert(uv_ind != -1 && uv_start != -1);
          BLI_assert(uv_ind - uv_start < MAX_MTFACE);
          tangent_mask_curr |= (short)(1 << (uv_ind - uv_start));
        }

        mesh2tangent->tangent = loopdata_out->layers[index].data;
        BLI_task_pool_push(task_pool, DM_calc_loop_tangents_thread, mesh2tangent, false, NULL);
      }

      BLI_assert(tangent_mask_curr == tangent_mask);
      BLI_task_pool_work_and_wait(task_pool);
      BLI_task_pool_free(task_pool);
    }
    else {
      tangent_mask_curr = tangent_mask;
    }
#ifdef USE_LOOPTRI_DETECT_QUADS
    if (face_as_quad_map) {
      MEM_freeN(face_as_quad_map);
    }
#  undef USE_LOOPTRI_DETECT_QUADS

#endif

    *tangent_mask_curr_p = tangent_mask_curr;

    /* Update active layer index */
    int act_uv_index = (act_uv_n != -1) ?
                           CustomData_get_layer_index_n(loopdata, CD_MLOOPUV, act_uv_n) :
                           -1;
    if (act_uv_index != -1) {
      int tan_index = CustomData_get_named_layer_index(
          loopdata, CD_TANGENT, loopdata->layers[act_uv_index].name);
      CustomData_set_layer_active_index(loopdata, CD_TANGENT, tan_index);
    } /* else tangent has been built from orco */

    /* Update render layer index */
    int ren_uv_index = (ren_uv_n != -1) ?
                           CustomData_get_layer_index_n(loopdata, CD_MLOOPUV, ren_uv_n) :
                           -1;
    if (ren_uv_index != -1) {
      int tan_index = CustomData_get_named_layer_index(
          loopdata, CD_TANGENT, loopdata->layers[ren_uv_index].name);
      CustomData_set_layer_render_index(loopdata, CD_TANGENT, tan_index);
    } /* else tangent has been built from orco */
  }
}

void BKE_mesh_calc_loop_tangents(Mesh *me_eval,
                                 bool calc_active_tangent,
                                 const char (*tangent_names)[MAX_NAME],
                                 int tangent_names_len)
{
  BKE_mesh_runtime_looptri_ensure(me_eval);

  /* TODO(campbell): store in Mesh.runtime to avoid recalculation. */
  short tangent_mask = 0;
  BKE_mesh_calc_loop_tangent_ex(me_eval->mvert,
                                me_eval->mpoly,
                                (uint)me_eval->totpoly,
                                me_eval->mloop,
                                me_eval->runtime.looptris.array,
                                (uint)me_eval->runtime.looptris.len,
                                &me_eval->ldata,
                                calc_active_tangent,
                                tangent_names,
                                tangent_names_len,
                                BKE_mesh_vertex_normals_ensure(me_eval),
                                BKE_mesh_poly_normals_ensure(me_eval),
                                CustomData_get_layer(&me_eval->ldata, CD_NORMAL),
                                CustomData_get_layer(&me_eval->vdata, CD_ORCO), /* may be NULL */
                                /* result */
                                &me_eval->ldata,
                                (uint)me_eval->totloop,
                                &tangent_mask);
}

/** \} */