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

multires_bake.c « intern « render « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3f37b80f320c1a5ca45eb5b11665453d7cf33a43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
/* SPDX-License-Identifier: GPL-2.0-or-later
 * Copyright 2012 Blender Foundation. All rights reserved. */

/** \file
 * \ingroup render
 */

#include <string.h>

#include "MEM_guardedalloc.h"

#include "DNA_mesh_types.h"
#include "DNA_meshdata_types.h"
#include "DNA_object_types.h"
#include "DNA_scene_types.h"

#include "BLI_listbase.h"
#include "BLI_math.h"
#include "BLI_threads.h"

#include "BKE_DerivedMesh.h"
#include "BKE_ccg.h"
#include "BKE_global.h"
#include "BKE_image.h"
#include "BKE_lib_id.h"
#include "BKE_material.h"
#include "BKE_mesh.h"
#include "BKE_mesh_tangent.h"
#include "BKE_modifier.h"
#include "BKE_multires.h"
#include "BKE_subsurf.h"

#include "DEG_depsgraph.h"

#include "RE_multires_bake.h"
#include "RE_pipeline.h"
#include "RE_texture.h"
#include "RE_texture_margin.h"

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

typedef void (*MPassKnownData)(DerivedMesh *lores_dm,
                               DerivedMesh *hires_dm,
                               void *thread_data,
                               void *bake_data,
                               ImBuf *ibuf,
                               const int face_index,
                               const int lvl,
                               const float st[2],
                               float tangmat[3][3],
                               const int x,
                               const int y);

typedef void *(*MInitBakeData)(MultiresBakeRender *bkr, ImBuf *ibuf);
typedef void (*MFreeBakeData)(void *bake_data);

typedef struct MultiresBakeResult {
  float height_min, height_max;
} MultiresBakeResult;

typedef struct {
  const float (*positions)[3];
  const float (*vert_normals)[3];
  MPoly *mpoly;
  const int *material_indices;
  MLoop *mloop;
  MLoopUV *mloopuv;
  float uv_offset[2];
  const MLoopTri *mlooptri;
  float *pvtangent;
  const float (*precomputed_normals)[3];
  int w, h;
  int tri_index;
  DerivedMesh *lores_dm, *hires_dm;
  int lvl;
  void *thread_data;
  void *bake_data;
  ImBuf *ibuf;
  MPassKnownData pass_data;
  /* material aligned UV array */
  Image **image_array;
} MResolvePixelData;

typedef void (*MFlushPixel)(const MResolvePixelData *data, const int x, const int y);

typedef struct {
  int w, h;
  char *texels;
  const MResolvePixelData *data;
  MFlushPixel flush_pixel;
  short *do_update;
} MBakeRast;

typedef struct {
  float *heights;
  DerivedMesh *ssdm;
  const int *orig_index_mp_to_orig;
} MHeightBakeData;

typedef struct {
  const int *orig_index_mp_to_orig;
} MNormalBakeData;

typedef struct BakeImBufuserData {
  float *displacement_buffer;
  char *mask_buffer;
} BakeImBufuserData;

static void multiresbake_get_normal(const MResolvePixelData *data,
                                    const int tri_num,
                                    const int vert_index,
                                    float r_normal[3])
{
  const int poly_index = data->mlooptri[tri_num].poly;
  const MPoly *mp = &data->mpoly[poly_index];
  const bool smoothnormal = (mp->flag & ME_SMOOTH) != 0;

  if (smoothnormal) {
    const int vi = data->mloop[data->mlooptri[tri_num].tri[vert_index]].v;
    copy_v3_v3(r_normal, data->vert_normals[vi]);
  }
  else {
    if (data->precomputed_normals) {
      copy_v3_v3(r_normal, data->precomputed_normals[poly_index]);
    }
    else {
      BKE_mesh_calc_poly_normal(mp, &data->mloop[mp->loopstart], data->positions, r_normal);
    }
  }
}

static void init_bake_rast(MBakeRast *bake_rast,
                           const ImBuf *ibuf,
                           const MResolvePixelData *data,
                           MFlushPixel flush_pixel,
                           short *do_update)
{
  BakeImBufuserData *userdata = (BakeImBufuserData *)ibuf->userdata;

  memset(bake_rast, 0, sizeof(MBakeRast));

  bake_rast->texels = userdata->mask_buffer;
  bake_rast->w = ibuf->x;
  bake_rast->h = ibuf->y;
  bake_rast->data = data;
  bake_rast->flush_pixel = flush_pixel;
  bake_rast->do_update = do_update;
}

static void flush_pixel(const MResolvePixelData *data, const int x, const int y)
{
  const float st[2] = {(x + 0.5f) / data->w + data->uv_offset[0],
                       (y + 0.5f) / data->h + data->uv_offset[1]};
  const float *st0, *st1, *st2;
  const float *tang0, *tang1, *tang2;
  float no0[3], no1[3], no2[3];
  float fUV[2], from_tang[3][3], to_tang[3][3];
  float u, v, w, sign;
  int r;

  st0 = data->mloopuv[data->mlooptri[data->tri_index].tri[0]].uv;
  st1 = data->mloopuv[data->mlooptri[data->tri_index].tri[1]].uv;
  st2 = data->mloopuv[data->mlooptri[data->tri_index].tri[2]].uv;

  multiresbake_get_normal(data, data->tri_index, 0, no0); /* can optimize these 3 into one call */
  multiresbake_get_normal(data, data->tri_index, 1, no1);
  multiresbake_get_normal(data, data->tri_index, 2, no2);

  resolve_tri_uv_v2(fUV, st, st0, st1, st2);

  u = fUV[0];
  v = fUV[1];
  w = 1 - u - v;

  if (data->pvtangent) {
    tang0 = data->pvtangent + data->mlooptri[data->tri_index].tri[0] * 4;
    tang1 = data->pvtangent + data->mlooptri[data->tri_index].tri[1] * 4;
    tang2 = data->pvtangent + data->mlooptri[data->tri_index].tri[2] * 4;

    /* the sign is the same at all face vertices for any non degenerate face.
     * Just in case we clamp the interpolated value though. */
    sign = (tang0[3] * u + tang1[3] * v + tang2[3] * w) < 0 ? (-1.0f) : 1.0f;

    /* this sequence of math is designed specifically as is with great care
     * to be compatible with our shader. Please don't change without good reason. */
    for (r = 0; r < 3; r++) {
      from_tang[0][r] = tang0[r] * u + tang1[r] * v + tang2[r] * w;
      from_tang[2][r] = no0[r] * u + no1[r] * v + no2[r] * w;
    }

    cross_v3_v3v3(from_tang[1], from_tang[2], from_tang[0]); /* `B = sign * cross(N, T)` */
    mul_v3_fl(from_tang[1], sign);
    invert_m3_m3(to_tang, from_tang);
  }
  else {
    zero_m3(to_tang);
  }

  data->pass_data(data->lores_dm,
                  data->hires_dm,
                  data->thread_data,
                  data->bake_data,
                  data->ibuf,
                  data->tri_index,
                  data->lvl,
                  st,
                  to_tang,
                  x,
                  y);
}

static void set_rast_triangle(const MBakeRast *bake_rast, const int x, const int y)
{
  const int w = bake_rast->w;
  const int h = bake_rast->h;

  if (x >= 0 && x < w && y >= 0 && y < h) {
    if ((bake_rast->texels[y * w + x]) == 0) {
      bake_rast->texels[y * w + x] = FILTER_MASK_USED;
      flush_pixel(bake_rast->data, x, y);
      if (bake_rast->do_update) {
        *bake_rast->do_update = true;
      }
    }
  }
}

static void rasterize_half(const MBakeRast *bake_rast,
                           const float s0_s,
                           const float t0_s,
                           const float s1_s,
                           const float t1_s,
                           const float s0_l,
                           const float t0_l,
                           const float s1_l,
                           const float t1_l,
                           const int y0_in,
                           const int y1_in,
                           const int is_mid_right)
{
  const int s_stable = fabsf(t1_s - t0_s) > FLT_EPSILON ? 1 : 0;
  const int l_stable = fabsf(t1_l - t0_l) > FLT_EPSILON ? 1 : 0;
  const int w = bake_rast->w;
  const int h = bake_rast->h;
  int y, y0, y1;

  if (y1_in <= 0 || y0_in >= h) {
    return;
  }

  y0 = y0_in < 0 ? 0 : y0_in;
  y1 = y1_in >= h ? h : y1_in;

  for (y = y0; y < y1; y++) {
    /*-b(x-x0) + a(y-y0) = 0 */
    int iXl, iXr, x;
    float x_l = s_stable != 0 ? (s0_s + (((s1_s - s0_s) * (y - t0_s)) / (t1_s - t0_s))) : s0_s;
    float x_r = l_stable != 0 ? (s0_l + (((s1_l - s0_l) * (y - t0_l)) / (t1_l - t0_l))) : s0_l;

    if (is_mid_right != 0) {
      SWAP(float, x_l, x_r);
    }

    iXl = (int)ceilf(x_l);
    iXr = (int)ceilf(x_r);

    if (iXr > 0 && iXl < w) {
      iXl = iXl < 0 ? 0 : iXl;
      iXr = iXr >= w ? w : iXr;

      for (x = iXl; x < iXr; x++) {
        set_rast_triangle(bake_rast, x, y);
      }
    }
  }
}

static void bake_rasterize(const MBakeRast *bake_rast,
                           const float st0_in[2],
                           const float st1_in[2],
                           const float st2_in[2])
{
  const int w = bake_rast->w;
  const int h = bake_rast->h;
  float slo = st0_in[0] * w - 0.5f;
  float tlo = st0_in[1] * h - 0.5f;
  float smi = st1_in[0] * w - 0.5f;
  float tmi = st1_in[1] * h - 0.5f;
  float shi = st2_in[0] * w - 0.5f;
  float thi = st2_in[1] * h - 0.5f;
  int is_mid_right = 0, ylo, yhi, yhi_beg;

  /* skip degenerates */
  if ((slo == smi && tlo == tmi) || (slo == shi && tlo == thi) || (smi == shi && tmi == thi)) {
    return;
  }

  /* sort by T */
  if (tlo > tmi && tlo > thi) {
    SWAP(float, shi, slo);
    SWAP(float, thi, tlo);
  }
  else if (tmi > thi) {
    SWAP(float, shi, smi);
    SWAP(float, thi, tmi);
  }

  if (tlo > tmi) {
    SWAP(float, slo, smi);
    SWAP(float, tlo, tmi);
  }

  /* check if mid point is to the left or to the right of the lo-hi edge */
  is_mid_right = (-(shi - slo) * (tmi - thi) + (thi - tlo) * (smi - shi)) > 0 ? 1 : 0;
  ylo = (int)ceilf(tlo);
  yhi_beg = (int)ceilf(tmi);
  yhi = (int)ceilf(thi);

  // if (fTmi>ceilf(fTlo))
  rasterize_half(bake_rast, slo, tlo, smi, tmi, slo, tlo, shi, thi, ylo, yhi_beg, is_mid_right);
  rasterize_half(bake_rast, smi, tmi, shi, thi, slo, tlo, shi, thi, yhi_beg, yhi, is_mid_right);
}

static int multiresbake_test_break(MultiresBakeRender *bkr)
{
  if (!bkr->stop) {
    /* this means baker is executed outside from job system */
    return 0;
  }

  return *bkr->stop || G.is_break;
}

/* **** Threading routines **** */

typedef struct MultiresBakeQueue {
  int cur_tri;
  int tot_tri;
  SpinLock spin;
} MultiresBakeQueue;

typedef struct MultiresBakeThread {
  /* this data is actually shared between all the threads */
  MultiresBakeQueue *queue;
  MultiresBakeRender *bkr;
  Image *image;
  void *bake_data;

  /* thread-specific data */
  MBakeRast bake_rast;
  MResolvePixelData data;

  /* displacement-specific data */
  float height_min, height_max;
} MultiresBakeThread;

static int multires_bake_queue_next_tri(MultiresBakeQueue *queue)
{
  int face = -1;

  /* TODO: it could worth making it so thread will handle neighbor faces
   *       for better memory cache utilization
   */

  BLI_spin_lock(&queue->spin);
  if (queue->cur_tri < queue->tot_tri) {
    face = queue->cur_tri;
    queue->cur_tri++;
  }
  BLI_spin_unlock(&queue->spin);

  return face;
}

static void *do_multires_bake_thread(void *data_v)
{
  MultiresBakeThread *handle = (MultiresBakeThread *)data_v;
  MResolvePixelData *data = &handle->data;
  MBakeRast *bake_rast = &handle->bake_rast;
  MultiresBakeRender *bkr = handle->bkr;
  int tri_index;

  while ((tri_index = multires_bake_queue_next_tri(handle->queue)) >= 0) {
    const MLoopTri *lt = &data->mlooptri[tri_index];
    const short mat_nr = data->material_indices == NULL ? 0 : data->material_indices[lt->poly];
    const MLoopUV *mloopuv = data->mloopuv;

    if (multiresbake_test_break(bkr)) {
      break;
    }

    Image *tri_image = mat_nr < bkr->ob_image.len ? bkr->ob_image.array[mat_nr] : NULL;
    if (tri_image != handle->image) {
      continue;
    }

    data->tri_index = tri_index;

    float uv[3][2];
    sub_v2_v2v2(uv[0], mloopuv[lt->tri[0]].uv, data->uv_offset);
    sub_v2_v2v2(uv[1], mloopuv[lt->tri[1]].uv, data->uv_offset);
    sub_v2_v2v2(uv[2], mloopuv[lt->tri[2]].uv, data->uv_offset);

    bake_rasterize(bake_rast, uv[0], uv[1], uv[2]);

    /* tag image buffer for refresh */
    if (data->ibuf->rect_float) {
      data->ibuf->userflags |= IB_RECT_INVALID;
    }

    data->ibuf->userflags |= IB_DISPLAY_BUFFER_INVALID;

    /* update progress */
    BLI_spin_lock(&handle->queue->spin);
    bkr->baked_faces++;

    if (bkr->do_update) {
      *bkr->do_update = true;
    }

    if (bkr->progress) {
      *bkr->progress = ((float)bkr->baked_objects +
                        (float)bkr->baked_faces / handle->queue->tot_tri) /
                       bkr->tot_obj;
    }
    BLI_spin_unlock(&handle->queue->spin);
  }

  return NULL;
}

/* some of arrays inside ccgdm are lazy-initialized, which will generally
 * require lock around accessing such data
 * this function will ensure all arrays are allocated before threading started
 */
static void init_ccgdm_arrays(DerivedMesh *dm)
{
  CCGElem **grid_data;
  CCGKey key;
  int grid_size;
  const int *grid_offset;

  grid_size = dm->getGridSize(dm);
  grid_data = dm->getGridData(dm);
  grid_offset = dm->getGridOffset(dm);
  dm->getGridKey(dm, &key);

  (void)grid_size;
  (void)grid_data;
  (void)grid_offset;
}

static void do_multires_bake(MultiresBakeRender *bkr,
                             Image *ima,
                             ImageTile *tile,
                             ImBuf *ibuf,
                             bool require_tangent,
                             MPassKnownData passKnownData,
                             MInitBakeData initBakeData,
                             MFreeBakeData freeBakeData,
                             MultiresBakeResult *result)
{
  DerivedMesh *dm = bkr->lores_dm;
  const MLoopTri *mlooptri = dm->getLoopTriArray(dm);
  const int lvl = bkr->lvl;
  int tot_tri = dm->getNumLoopTri(dm);
  if (tot_tri < 1) {
    return;
  }

  MultiresBakeThread *handles;
  MultiresBakeQueue queue;

  const float(*positions)[3] = (float(*)[3])dm->getVertArray(dm);
  MPoly *mpoly = dm->getPolyArray(dm);
  MLoop *mloop = dm->getLoopArray(dm);
  MLoopUV *mloopuv = dm->getLoopDataArray(dm, CD_MLOOPUV);
  float *pvtangent = NULL;

  ListBase threads;
  int i, tot_thread = bkr->threads > 0 ? bkr->threads : BLI_system_thread_count();

  void *bake_data = NULL;

  Mesh *temp_mesh = BKE_mesh_new_nomain(
      dm->getNumVerts(dm), dm->getNumEdges(dm), 0, dm->getNumLoops(dm), dm->getNumPolys(dm));
  memcpy(
      BKE_mesh_positions_for_write(temp_mesh), positions, temp_mesh->totvert * sizeof(float[3]));
  memcpy(BKE_mesh_edges_for_write(temp_mesh),
         dm->getEdgeArray(dm),
         temp_mesh->totedge * sizeof(MEdge));
  memcpy(BKE_mesh_polys_for_write(temp_mesh),
         dm->getPolyArray(dm),
         temp_mesh->totpoly * sizeof(MPoly));
  memcpy(BKE_mesh_loops_for_write(temp_mesh),
         dm->getLoopArray(dm),
         temp_mesh->totloop * sizeof(MLoop));
  const float(*vert_normals)[3] = BKE_mesh_vertex_normals_ensure(temp_mesh);
  const float(*poly_normals)[3] = BKE_mesh_poly_normals_ensure(temp_mesh);

  if (require_tangent) {
    if (CustomData_get_layer_index(&dm->loopData, CD_TANGENT) == -1) {
      BKE_mesh_calc_loop_tangent_ex(
          positions,
          dm->getPolyArray(dm),
          dm->getNumPolys(dm),
          dm->getLoopArray(dm),
          dm->getLoopTriArray(dm),
          dm->getNumLoopTri(dm),
          &dm->loopData,
          true,
          NULL,
          0,
          vert_normals,
          poly_normals,
          (const float(*)[3])dm->getLoopDataArray(dm, CD_NORMAL),
          (const float(*)[3])dm->getVertDataArray(dm, CD_ORCO), /* may be nullptr */
          /* result */
          &dm->loopData,
          dm->getNumLoops(dm),
          &dm->tangent_mask);
    }

    pvtangent = DM_get_loop_data_layer(dm, CD_TANGENT);
  }

  /* all threads shares the same custom bake data */
  if (initBakeData) {
    bake_data = initBakeData(bkr, ibuf);
  }

  if (tot_thread > 1) {
    BLI_threadpool_init(&threads, do_multires_bake_thread, tot_thread);
  }

  handles = MEM_callocN(tot_thread * sizeof(MultiresBakeThread), "do_multires_bake handles");

  init_ccgdm_arrays(bkr->hires_dm);

  /* faces queue */
  queue.cur_tri = 0;
  queue.tot_tri = tot_tri;
  BLI_spin_init(&queue.spin);

  /* fill in threads handles */
  for (i = 0; i < tot_thread; i++) {
    MultiresBakeThread *handle = &handles[i];

    handle->bkr = bkr;
    handle->image = ima;
    handle->queue = &queue;

    handle->data.mpoly = mpoly;
    handle->data.material_indices = CustomData_get_layer_named(
        &dm->polyData, CD_PROP_INT32, "material_index");
    handle->data.positions = positions;
    handle->data.vert_normals = vert_normals;
    handle->data.mloopuv = mloopuv;
    BKE_image_get_tile_uv(ima, tile->tile_number, handle->data.uv_offset);
    handle->data.mlooptri = mlooptri;
    handle->data.mloop = mloop;
    handle->data.pvtangent = pvtangent;
    handle->data.precomputed_normals = poly_normals; /* don't strictly need this */
    handle->data.w = ibuf->x;
    handle->data.h = ibuf->y;
    handle->data.lores_dm = dm;
    handle->data.hires_dm = bkr->hires_dm;
    handle->data.lvl = lvl;
    handle->data.pass_data = passKnownData;
    handle->data.thread_data = handle;
    handle->data.bake_data = bake_data;
    handle->data.ibuf = ibuf;

    handle->height_min = FLT_MAX;
    handle->height_max = -FLT_MAX;

    init_bake_rast(&handle->bake_rast, ibuf, &handle->data, flush_pixel, bkr->do_update);

    if (tot_thread > 1) {
      BLI_threadpool_insert(&threads, handle);
    }
  }

  /* run threads */
  if (tot_thread > 1) {
    BLI_threadpool_end(&threads);
  }
  else {
    do_multires_bake_thread(&handles[0]);
  }

  /* construct bake result */
  result->height_min = handles[0].height_min;
  result->height_max = handles[0].height_max;

  for (i = 1; i < tot_thread; i++) {
    result->height_min = min_ff(result->height_min, handles[i].height_min);
    result->height_max = max_ff(result->height_max, handles[i].height_max);
  }

  BLI_spin_end(&queue.spin);

  /* finalize baking */
  if (freeBakeData) {
    freeBakeData(bake_data);
  }

  MEM_freeN(handles);

  BKE_id_free(NULL, temp_mesh);
}

/* mode = 0: interpolate normals,
 * mode = 1: interpolate coord */
static void interp_bilinear_grid(
    CCGKey *key, CCGElem *grid, float crn_x, float crn_y, int mode, float res[3])
{
  int x0, x1, y0, y1;
  float u, v;
  float data[4][3];

  x0 = (int)crn_x;
  x1 = x0 >= (key->grid_size - 1) ? (key->grid_size - 1) : (x0 + 1);

  y0 = (int)crn_y;
  y1 = y0 >= (key->grid_size - 1) ? (key->grid_size - 1) : (y0 + 1);

  u = crn_x - x0;
  v = crn_y - y0;

  if (mode == 0) {
    copy_v3_v3(data[0], CCG_grid_elem_no(key, grid, x0, y0));
    copy_v3_v3(data[1], CCG_grid_elem_no(key, grid, x1, y0));
    copy_v3_v3(data[2], CCG_grid_elem_no(key, grid, x1, y1));
    copy_v3_v3(data[3], CCG_grid_elem_no(key, grid, x0, y1));
  }
  else {
    copy_v3_v3(data[0], CCG_grid_elem_co(key, grid, x0, y0));
    copy_v3_v3(data[1], CCG_grid_elem_co(key, grid, x1, y0));
    copy_v3_v3(data[2], CCG_grid_elem_co(key, grid, x1, y1));
    copy_v3_v3(data[3], CCG_grid_elem_co(key, grid, x0, y1));
  }

  interp_bilinear_quad_v3(data, u, v, res);
}

static void get_ccgdm_data(DerivedMesh *lodm,
                           DerivedMesh *hidm,
                           const int *index_mp_to_orig,
                           const int lvl,
                           const MLoopTri *lt,
                           const float u,
                           const float v,
                           float co[3],
                           float n[3])
{
  CCGElem **grid_data;
  CCGKey key;
  float crn_x, crn_y;
  int grid_size, S, face_side;
  int *grid_offset, g_index;
  int poly_index = lt->poly;

  grid_size = hidm->getGridSize(hidm);
  grid_data = hidm->getGridData(hidm);
  grid_offset = hidm->getGridOffset(hidm);
  hidm->getGridKey(hidm, &key);

  if (lvl == 0) {
    MPoly *mpoly;
    face_side = (grid_size << 1) - 1;

    mpoly = lodm->getPolyArray(lodm) + poly_index;
    g_index = grid_offset[poly_index];
    S = mdisp_rot_face_to_crn(mpoly,
                              lodm->getLoopArray(lodm),
                              lt,
                              face_side,
                              u * (face_side - 1),
                              v * (face_side - 1),
                              &crn_x,
                              &crn_y);
  }
  else {
    /* number of faces per grid side */
    int polys_per_grid_side = (1 << (lvl - 1));
    /* get the original cage face index */
    int cage_face_index = index_mp_to_orig ? index_mp_to_orig[poly_index] : poly_index;
    /* local offset in total cage face grids
     * `(1 << (2 * lvl))` is number of all polys for one cage face */
    int loc_cage_poly_ofs = poly_index % (1 << (2 * lvl));
    /* local offset in the vertex grid itself */
    int cell_index = loc_cage_poly_ofs % (polys_per_grid_side * polys_per_grid_side);
    int cell_side = (grid_size - 1) / polys_per_grid_side;
    /* row and column based on grid side */
    int row = cell_index / polys_per_grid_side;
    int col = cell_index % polys_per_grid_side;

    /* S is the vertex whose grid we are examining */
    S = poly_index / (1 << (2 * (lvl - 1))) - grid_offset[cage_face_index];
    /* get offset of grid data for original cage face */
    g_index = grid_offset[cage_face_index];

    crn_y = (row * cell_side) + u * cell_side;
    crn_x = (col * cell_side) + v * cell_side;
  }

  CLAMP(crn_x, 0.0f, grid_size);
  CLAMP(crn_y, 0.0f, grid_size);

  if (n != NULL) {
    interp_bilinear_grid(&key, grid_data[g_index + S], crn_x, crn_y, 0, n);
  }

  if (co != NULL) {
    interp_bilinear_grid(&key, grid_data[g_index + S], crn_x, crn_y, 1, co);
  }
}

/* mode = 0: interpolate normals,
 * mode = 1: interpolate coord */

static void interp_bilinear_mpoly(DerivedMesh *dm,
                                  MLoop *mloop,
                                  MPoly *mpoly,
                                  const float u,
                                  const float v,
                                  const int mode,
                                  float res[3])
{
  float data[4][3];

  if (mode == 0) {
    dm->getVertNo(dm, mloop[mpoly->loopstart].v, data[0]);
    dm->getVertNo(dm, mloop[mpoly->loopstart + 1].v, data[1]);
    dm->getVertNo(dm, mloop[mpoly->loopstart + 2].v, data[2]);
    dm->getVertNo(dm, mloop[mpoly->loopstart + 3].v, data[3]);
  }
  else {
    dm->getVertCo(dm, mloop[mpoly->loopstart].v, data[0]);
    dm->getVertCo(dm, mloop[mpoly->loopstart + 1].v, data[1]);
    dm->getVertCo(dm, mloop[mpoly->loopstart + 2].v, data[2]);
    dm->getVertCo(dm, mloop[mpoly->loopstart + 3].v, data[3]);
  }

  interp_bilinear_quad_v3(data, u, v, res);
}

static void interp_barycentric_mlooptri(DerivedMesh *dm,
                                        MLoop *mloop,
                                        const MLoopTri *lt,
                                        const float u,
                                        const float v,
                                        const int mode,
                                        float res[3])
{
  float data[3][3];

  if (mode == 0) {
    dm->getVertNo(dm, mloop[lt->tri[0]].v, data[0]);
    dm->getVertNo(dm, mloop[lt->tri[1]].v, data[1]);
    dm->getVertNo(dm, mloop[lt->tri[2]].v, data[2]);
  }
  else {
    dm->getVertCo(dm, mloop[lt->tri[0]].v, data[0]);
    dm->getVertCo(dm, mloop[lt->tri[1]].v, data[1]);
    dm->getVertCo(dm, mloop[lt->tri[2]].v, data[2]);
  }

  interp_barycentric_tri_v3(data, u, v, res);
}

/* **************** Displacement Baker **************** */

static void *init_heights_data(MultiresBakeRender *bkr, ImBuf *ibuf)
{
  MHeightBakeData *height_data;
  DerivedMesh *lodm = bkr->lores_dm;
  BakeImBufuserData *userdata = ibuf->userdata;

  if (userdata->displacement_buffer == NULL) {
    userdata->displacement_buffer = MEM_callocN(sizeof(float) * ibuf->x * ibuf->y,
                                                "MultiresBake heights");
  }

  height_data = MEM_callocN(sizeof(MHeightBakeData), "MultiresBake heightData");

  height_data->heights = userdata->displacement_buffer;

  if (!bkr->use_lores_mesh) {
    SubsurfModifierData smd = {{NULL}};
    int ss_lvl = bkr->tot_lvl - bkr->lvl;

    CLAMP(ss_lvl, 0, 6);

    if (ss_lvl > 0) {
      smd.levels = smd.renderLevels = ss_lvl;
      smd.uv_smooth = SUBSURF_UV_SMOOTH_PRESERVE_BOUNDARIES;
      smd.quality = 3;

      height_data->ssdm = subsurf_make_derived_from_derived(
          bkr->lores_dm, &smd, bkr->scene, NULL, 0);
      init_ccgdm_arrays(height_data->ssdm);
    }
  }

  height_data->orig_index_mp_to_orig = lodm->getPolyDataArray(lodm, CD_ORIGINDEX);

  return (void *)height_data;
}

static void free_heights_data(void *bake_data)
{
  MHeightBakeData *height_data = (MHeightBakeData *)bake_data;

  if (height_data->ssdm) {
    height_data->ssdm->release(height_data->ssdm);
  }

  MEM_freeN(height_data);
}

/* MultiresBake callback for heights baking
 * general idea:
 *   - find coord of point with specified UV in hi-res mesh (let's call it p1)
 *   - find coord of point and normal with specified UV in lo-res mesh (or subdivided lo-res
 *     mesh to make texture smoother) let's call this point p0 and n.
 *   - height wound be dot(n, p1-p0) */
static void apply_heights_callback(DerivedMesh *lores_dm,
                                   DerivedMesh *hires_dm,
                                   void *thread_data_v,
                                   void *bake_data,
                                   ImBuf *ibuf,
                                   const int tri_index,
                                   const int lvl,
                                   const float st[2],
                                   float UNUSED(tangmat[3][3]),
                                   const int x,
                                   const int y)
{
  const MLoopTri *lt = lores_dm->getLoopTriArray(lores_dm) + tri_index;
  MLoop *mloop = lores_dm->getLoopArray(lores_dm);
  MPoly *mpoly = lores_dm->getPolyArray(lores_dm) + lt->poly;
  MLoopUV *mloopuv = lores_dm->getLoopDataArray(lores_dm, CD_MLOOPUV);
  MHeightBakeData *height_data = (MHeightBakeData *)bake_data;
  MultiresBakeThread *thread_data = (MultiresBakeThread *)thread_data_v;
  float uv[2], *st0, *st1, *st2, *st3;
  int pixel = ibuf->x * y + x;
  float vec[3], p0[3], p1[3], n[3], len;

  /* ideally we would work on triangles only, however, we rely on quads to get orthogonal
   * coordinates for use in grid space (triangle barycentric is not orthogonal) */
  if (mpoly->totloop == 4) {
    st0 = mloopuv[mpoly->loopstart].uv;
    st1 = mloopuv[mpoly->loopstart + 1].uv;
    st2 = mloopuv[mpoly->loopstart + 2].uv;
    st3 = mloopuv[mpoly->loopstart + 3].uv;
    resolve_quad_uv_v2(uv, st, st0, st1, st2, st3);
  }
  else {
    st0 = mloopuv[lt->tri[0]].uv;
    st1 = mloopuv[lt->tri[1]].uv;
    st2 = mloopuv[lt->tri[2]].uv;
    resolve_tri_uv_v2(uv, st, st0, st1, st2);
  }

  clamp_v2(uv, 0.0f, 1.0f);

  get_ccgdm_data(
      lores_dm, hires_dm, height_data->orig_index_mp_to_orig, lvl, lt, uv[0], uv[1], p1, NULL);

  if (height_data->ssdm) {
    get_ccgdm_data(lores_dm,
                   height_data->ssdm,
                   height_data->orig_index_mp_to_orig,
                   0,
                   lt,
                   uv[0],
                   uv[1],
                   p0,
                   n);
  }
  else {
    if (mpoly->totloop == 4) {
      interp_bilinear_mpoly(lores_dm, mloop, mpoly, uv[0], uv[1], 1, p0);
      interp_bilinear_mpoly(lores_dm, mloop, mpoly, uv[0], uv[1], 0, n);
    }
    else {
      interp_barycentric_mlooptri(lores_dm, mloop, lt, uv[0], uv[1], 1, p0);
      interp_barycentric_mlooptri(lores_dm, mloop, lt, uv[0], uv[1], 0, n);
    }
  }

  sub_v3_v3v3(vec, p1, p0);
  len = dot_v3v3(n, vec);

  height_data->heights[pixel] = len;

  thread_data->height_min = min_ff(thread_data->height_min, len);
  thread_data->height_max = max_ff(thread_data->height_max, len);

  if (ibuf->rect_float) {
    float *rrgbf = ibuf->rect_float + pixel * 4;
    rrgbf[0] = rrgbf[1] = rrgbf[2] = len;
    rrgbf[3] = 1.0f;
  }
  else {
    char *rrgb = (char *)ibuf->rect + pixel * 4;
    rrgb[0] = rrgb[1] = rrgb[2] = unit_float_to_uchar_clamp(len);
    rrgb[3] = 255;
  }
}

/* **************** Normal Maps Baker **************** */

static void *init_normal_data(MultiresBakeRender *bkr, ImBuf *UNUSED(ibuf))
{
  MNormalBakeData *normal_data;
  DerivedMesh *lodm = bkr->lores_dm;

  normal_data = MEM_callocN(sizeof(MNormalBakeData), "MultiresBake normalData");

  normal_data->orig_index_mp_to_orig = lodm->getPolyDataArray(lodm, CD_ORIGINDEX);

  return (void *)normal_data;
}

static void free_normal_data(void *bake_data)
{
  MNormalBakeData *normal_data = (MNormalBakeData *)bake_data;

  MEM_freeN(normal_data);
}

/**
 * MultiresBake callback for normals' baking.
 *
 * General idea:
 * - Find coord and normal of point with specified UV in hi-res mesh.
 * - Multiply it by tangmat.
 * - Vector in color space would be `norm(vec) / 2 + (0.5, 0.5, 0.5)`.
 */
static void apply_tangmat_callback(DerivedMesh *lores_dm,
                                   DerivedMesh *hires_dm,
                                   void *UNUSED(thread_data),
                                   void *bake_data,
                                   ImBuf *ibuf,
                                   const int tri_index,
                                   const int lvl,
                                   const float st[2],
                                   float tangmat[3][3],
                                   const int x,
                                   const int y)
{
  const MLoopTri *lt = lores_dm->getLoopTriArray(lores_dm) + tri_index;
  MPoly *mpoly = lores_dm->getPolyArray(lores_dm) + lt->poly;
  MLoopUV *mloopuv = lores_dm->getLoopDataArray(lores_dm, CD_MLOOPUV);
  MNormalBakeData *normal_data = (MNormalBakeData *)bake_data;
  float uv[2], *st0, *st1, *st2, *st3;
  int pixel = ibuf->x * y + x;
  float n[3], vec[3], tmp[3] = {0.5, 0.5, 0.5};

  /* ideally we would work on triangles only, however, we rely on quads to get orthogonal
   * coordinates for use in grid space (triangle barycentric is not orthogonal) */
  if (mpoly->totloop == 4) {
    st0 = mloopuv[mpoly->loopstart].uv;
    st1 = mloopuv[mpoly->loopstart + 1].uv;
    st2 = mloopuv[mpoly->loopstart + 2].uv;
    st3 = mloopuv[mpoly->loopstart + 3].uv;
    resolve_quad_uv_v2(uv, st, st0, st1, st2, st3);
  }
  else {
    st0 = mloopuv[lt->tri[0]].uv;
    st1 = mloopuv[lt->tri[1]].uv;
    st2 = mloopuv[lt->tri[2]].uv;
    resolve_tri_uv_v2(uv, st, st0, st1, st2);
  }

  clamp_v2(uv, 0.0f, 1.0f);

  get_ccgdm_data(
      lores_dm, hires_dm, normal_data->orig_index_mp_to_orig, lvl, lt, uv[0], uv[1], NULL, n);

  mul_v3_m3v3(vec, tangmat, n);
  normalize_v3_length(vec, 0.5);
  add_v3_v3(vec, tmp);

  if (ibuf->rect_float) {
    float *rrgbf = ibuf->rect_float + pixel * 4;
    rrgbf[0] = vec[0];
    rrgbf[1] = vec[1];
    rrgbf[2] = vec[2];
    rrgbf[3] = 1.0f;
  }
  else {
    uchar *rrgb = (uchar *)ibuf->rect + pixel * 4;
    rgb_float_to_uchar(rrgb, vec);
    rrgb[3] = 255;
  }
}

/* TODO: restore ambient occlusion baking support, using BLI BVH? */
#if 0
/* **************** Ambient Occlusion Baker **************** */

/* Must be a power of two. */
#  define MAX_NUMBER_OF_AO_RAYS 1024

static ushort ao_random_table_1[MAX_NUMBER_OF_AO_RAYS];
static ushort ao_random_table_2[MAX_NUMBER_OF_AO_RAYS];

static void init_ao_random(void)
{
  int i;

  for (i = 0; i < MAX_NUMBER_OF_AO_RAYS; i++) {
    ao_random_table_1[i] = rand() & 0xffff;
    ao_random_table_2[i] = rand() & 0xffff;
  }
}

static ushort get_ao_random1(const int i)
{
  return ao_random_table_1[i & (MAX_NUMBER_OF_AO_RAYS - 1)];
}

static ushort get_ao_random2(const int i)
{
  return ao_random_table_2[i & (MAX_NUMBER_OF_AO_RAYS - 1)];
}

static void build_permutation_table(ushort permutation[],
                                    ushort temp_permutation[],
                                    const int number_of_rays,
                                    const int is_first_perm_table)
{
  int i, k;

  for (i = 0; i < number_of_rays; i++) {
    temp_permutation[i] = i;
  }

  for (i = 0; i < number_of_rays; i++) {
    const uint nr_entries_left = number_of_rays - i;
    ushort rnd = is_first_perm_table != false ? get_ao_random1(i) : get_ao_random2(i);
    const ushort entry = rnd % nr_entries_left;

    /* pull entry */
    permutation[i] = temp_permutation[entry];

    /* delete entry */
    for (k = entry; k < nr_entries_left - 1; k++) {
      temp_permutation[k] = temp_permutation[k + 1];
    }
  }

  /* verify permutation table
   * every entry must appear exactly once
   */
#  if 0
  for (i = 0; i < number_of_rays; i++) temp_permutation[i] = 0;
  for (i = 0; i < number_of_rays; i++) ++temp_permutation[permutation[i]];
  for (i = 0; i < number_of_rays; i++) BLI_assert(temp_permutation[i] == 1);
#  endif
}

static void create_ao_raytree(MultiresBakeRender *bkr, MAOBakeData *ao_data)
{
  DerivedMesh *hidm = bkr->hires_dm;
  RayObject *raytree;
  RayFace *face;
  CCGElem **grid_data;
  CCGKey key;
  int grids_num, grid_size /*, face_side */, faces_num;
  int i;

  grids_num = hidm->getNumGrids(hidm);
  grid_size = hidm->getGridSize(hidm);
  grid_data = hidm->getGridData(hidm);
  hidm->getGridKey(hidm, &key);

  /* face_side = (grid_size << 1) - 1; */ /* UNUSED */
  faces_num = grids_num * (grid_size - 1) * (grid_size - 1);

  raytree = ao_data->raytree = RE_rayobject_create(
      bkr->raytrace_structure, faces_num, bkr->octree_resolution);
  face = ao_data->rayfaces = (RayFace *)MEM_callocN(faces_num * sizeof(RayFace),
                                                    "ObjectRen faces");

  for (i = 0; i < grids_num; i++) {
    int x, y;
    for (x = 0; x < grid_size - 1; x++) {
      for (y = 0; y < grid_size - 1; y++) {
        float co[4][3];

        copy_v3_v3(co[0], CCG_grid_elem_co(&key, grid_data[i], x, y));
        copy_v3_v3(co[1], CCG_grid_elem_co(&key, grid_data[i], x, y + 1));
        copy_v3_v3(co[2], CCG_grid_elem_co(&key, grid_data[i], x + 1, y + 1));
        copy_v3_v3(co[3], CCG_grid_elem_co(&key, grid_data[i], x + 1, y));

        RE_rayface_from_coords(face, ao_data, face, co[0], co[1], co[2], co[3]);
        RE_rayobject_add(raytree, RE_rayobject_unalignRayFace(face));

        face++;
      }
    }
  }

  RE_rayobject_done(raytree);
}

static void *init_ao_data(MultiresBakeRender *bkr, ImBuf *UNUSED(ibuf))
{
  MAOBakeData *ao_data;
  DerivedMesh *lodm = bkr->lores_dm;
  ushort *temp_permutation_table;
  size_t permutation_size;

  init_ao_random();

  ao_data = MEM_callocN(sizeof(MAOBakeData), "MultiresBake aoData");

  ao_data->number_of_rays = bkr->number_of_rays;
  ao_data->bias = bkr->bias;

  ao_data->orig_index_mp_to_orig = lodm->getPolyDataArray(lodm, CD_ORIGINDEX);

  create_ao_raytree(bkr, ao_data);

  /* initialize permutation tables */
  permutation_size = sizeof(ushort) * bkr->number_of_rays;
  ao_data->permutation_table_1 = MEM_callocN(permutation_size, "multires AO baker perm1");
  ao_data->permutation_table_2 = MEM_callocN(permutation_size, "multires AO baker perm2");
  temp_permutation_table = MEM_callocN(permutation_size, "multires AO baker temp perm");

  build_permutation_table(
      ao_data->permutation_table_1, temp_permutation_table, bkr->number_of_rays, 1);
  build_permutation_table(
      ao_data->permutation_table_2, temp_permutation_table, bkr->number_of_rays, 0);

  MEM_freeN(temp_permutation_table);

  return (void *)ao_data;
}

static void free_ao_data(void *bake_data)
{
  MAOBakeData *ao_data = (MAOBakeData *)bake_data;

  RE_rayobject_free(ao_data->raytree);
  MEM_freeN(ao_data->rayfaces);

  MEM_freeN(ao_data->permutation_table_1);
  MEM_freeN(ao_data->permutation_table_2);

  MEM_freeN(ao_data);
}

/* builds an X and a Y axis from the given Z axis */
static void build_coordinate_frame(float axisX[3], float axisY[3], const float axisZ[3])
{
  const float faX = fabsf(axisZ[0]);
  const float faY = fabsf(axisZ[1]);
  const float faZ = fabsf(axisZ[2]);

  if (faX <= faY && faX <= faZ) {
    const float len = sqrtf(axisZ[1] * axisZ[1] + axisZ[2] * axisZ[2]);
    axisY[0] = 0;
    axisY[1] = axisZ[2] / len;
    axisY[2] = -axisZ[1] / len;
    cross_v3_v3v3(axisX, axisY, axisZ);
  }
  else if (faY <= faZ) {
    const float len = sqrtf(axisZ[0] * axisZ[0] + axisZ[2] * axisZ[2]);
    axisX[0] = axisZ[2] / len;
    axisX[1] = 0;
    axisX[2] = -axisZ[0] / len;
    cross_v3_v3v3(axisY, axisZ, axisX);
  }
  else {
    const float len = sqrtf(axisZ[0] * axisZ[0] + axisZ[1] * axisZ[1]);
    axisX[0] = axisZ[1] / len;
    axisX[1] = -axisZ[0] / len;
    axisX[2] = 0;
    cross_v3_v3v3(axisY, axisZ, axisX);
  }
}

/* return false if nothing was hit and true otherwise */
static int trace_ao_ray(MAOBakeData *ao_data, float ray_start[3], float ray_direction[3])
{
  Isect isect = {{0}};

  isect.dist = RE_RAYTRACE_MAXDIST;
  copy_v3_v3(isect.start, ray_start);
  copy_v3_v3(isect.dir, ray_direction);
  isect.lay = -1;

  normalize_v3(isect.dir);

  return RE_rayobject_raycast(ao_data->raytree, &isect);
}

static void apply_ao_callback(DerivedMesh *lores_dm,
                              DerivedMesh *hires_dm,
                              void *UNUSED(thread_data),
                              void *bake_data,
                              ImBuf *ibuf,
                              const int tri_index,
                              const int lvl,
                              const float st[2],
                              float UNUSED(tangmat[3][3]),
                              const int x,
                              const int y)
{
  const MLoopTri *lt = lores_dm->getLoopTriArray(lores_dm) + tri_index;
  MPoly *mpoly = lores_dm->getPolyArray(lores_dm) + lt->poly;
  MLoopUV *mloopuv = lores_dm->getLoopDataArray(lores_dm, CD_MLOOPUV);
  MAOBakeData *ao_data = (MAOBakeData *)bake_data;

  int i, k, perm_ofs;
  float pos[3], nrm[3];
  float cen[3];
  float axisX[3], axisY[3], axisZ[3];
  float shadow = 0;
  float value;
  int pixel = ibuf->x * y + x;
  float uv[2], *st0, *st1, *st2, *st3;

  /* ideally we would work on triangles only, however, we rely on quads to get orthogonal
   * coordinates for use in grid space (triangle barycentric is not orthogonal) */
  if (mpoly->totloop == 4) {
    st0 = mloopuv[mpoly->loopstart].uv;
    st1 = mloopuv[mpoly->loopstart + 1].uv;
    st2 = mloopuv[mpoly->loopstart + 2].uv;
    st3 = mloopuv[mpoly->loopstart + 3].uv;
    resolve_quad_uv_v2(uv, st, st0, st1, st2, st3);
  }
  else {
    st0 = mloopuv[lt->tri[0]].uv;
    st1 = mloopuv[lt->tri[1]].uv;
    st2 = mloopuv[lt->tri[2]].uv;
    resolve_tri_uv_v2(uv, st, st0, st1, st2);
  }

  clamp_v2(uv, 0.0f, 1.0f);

  get_ccgdm_data(
      lores_dm, hires_dm, ao_data->orig_index_mp_to_orig, lvl, lt, uv[0], uv[1], pos, nrm);

  /* offset ray origin by user bias along normal */
  for (i = 0; i < 3; i++) {
    cen[i] = pos[i] + ao_data->bias * nrm[i];
  }

  /* build tangent frame */
  for (i = 0; i < 3; i++) {
    axisZ[i] = nrm[i];
  }

  build_coordinate_frame(axisX, axisY, axisZ);

  /* static noise */
  perm_ofs = (get_ao_random2(get_ao_random1(x) + y)) & (MAX_NUMBER_OF_AO_RAYS - 1);

  /* importance sample shadow rays (cosine weighted) */
  for (i = 0; i < ao_data->number_of_rays; i++) {
    int hit_something;

    /* use N-Rooks to distribute our N ray samples across
     * a multi-dimensional domain (2D)
     */
    const ushort I =
        ao_data->permutation_table_1[(i + perm_ofs) % ao_data->number_of_rays];
    const ushort J = ao_data->permutation_table_2[i];

    const float JitPh = (get_ao_random2(I + perm_ofs) & (MAX_NUMBER_OF_AO_RAYS - 1)) /
                        ((float)MAX_NUMBER_OF_AO_RAYS);
    const float JitTh = (get_ao_random1(J + perm_ofs) & (MAX_NUMBER_OF_AO_RAYS - 1)) /
                        ((float)MAX_NUMBER_OF_AO_RAYS);
    const float SiSqPhi = (I + JitPh) / ao_data->number_of_rays;
    const float Theta = (float)(2 * M_PI) * ((J + JitTh) / ao_data->number_of_rays);

    /* this gives results identical to the so-called cosine
     * weighted distribution relative to the north pole.
     */
    float SiPhi = sqrtf(SiSqPhi);
    float CoPhi = SiSqPhi < 1.0f ? sqrtf(1.0f - SiSqPhi) : 0;
    float CoThe = cosf(Theta);
    float SiThe = sinf(Theta);

    const float dx = CoThe * CoPhi;
    const float dy = SiThe * CoPhi;
    const float dz = SiPhi;

    /* transform ray direction out of tangent frame */
    float dv[3];
    for (k = 0; k < 3; k++) {
      dv[k] = axisX[k] * dx + axisY[k] * dy + axisZ[k] * dz;
    }

    hit_something = trace_ao_ray(ao_data, cen, dv);

    if (hit_something != 0) {
      shadow += 1;
    }
  }

  value = 1.0f - (shadow / ao_data->number_of_rays);

  if (ibuf->rect_float) {
    float *rrgbf = ibuf->rect_float + pixel * 4;
    rrgbf[0] = rrgbf[1] = rrgbf[2] = value;
    rrgbf[3] = 1.0f;
  }
  else {
    uchar *rrgb = (uchar *)ibuf->rect + pixel * 4;
    rrgb[0] = rrgb[1] = rrgb[2] = unit_float_to_uchar_clamp(value);
    rrgb[3] = 255;
  }
}
#endif

/* ******$***************** Post processing ************************* */

static void bake_ibuf_filter(ImBuf *ibuf,
                             char *mask,
                             const int margin,
                             const char margin_type,
                             DerivedMesh *dm,
                             const float uv_offset[2])
{
  /* must check before filtering */
  const bool is_new_alpha = (ibuf->planes != R_IMF_PLANES_RGBA) && BKE_imbuf_alpha_test(ibuf);

  if (margin) {
    switch (margin_type) {
      case R_BAKE_ADJACENT_FACES:
        RE_generate_texturemargin_adjacentfaces_dm(ibuf, mask, margin, dm, uv_offset);
        break;
      default:
      /* fall through */
      case R_BAKE_EXTEND:
        IMB_filter_extend(ibuf, mask, margin);
        break;
    }
  }

  /* if the bake results in new alpha then change the image setting */
  if (is_new_alpha) {
    ibuf->planes = R_IMF_PLANES_RGBA;
  }
  else {
    if (margin && ibuf->planes != R_IMF_PLANES_RGBA) {
      /* clear alpha added by filtering */
      IMB_rectfill_alpha(ibuf, 1.0f);
    }
  }
}

static void bake_ibuf_normalize_displacement(ImBuf *ibuf,
                                             const float *displacement,
                                             const char *mask,
                                             float displacement_min,
                                             float displacement_max)
{
  int i;
  const float *current_displacement = displacement;
  const char *current_mask = mask;
  float max_distance;

  max_distance = max_ff(fabsf(displacement_min), fabsf(displacement_max));

  for (i = 0; i < ibuf->x * ibuf->y; i++) {
    if (*current_mask == FILTER_MASK_USED) {
      float normalized_displacement;

      if (max_distance > 1e-5f) {
        normalized_displacement = (*current_displacement + max_distance) / (max_distance * 2);
      }
      else {
        normalized_displacement = 0.5f;
      }

      if (ibuf->rect_float) {
        /* currently baking happens to RGBA only */
        float *fp = ibuf->rect_float + i * 4;
        fp[0] = fp[1] = fp[2] = normalized_displacement;
        fp[3] = 1.0f;
      }

      if (ibuf->rect) {
        uchar *cp = (uchar *)(ibuf->rect + i);
        cp[0] = cp[1] = cp[2] = unit_float_to_uchar_clamp(normalized_displacement);
        cp[3] = 255;
      }
    }

    current_displacement++;
    current_mask++;
  }
}

/* **************** Common functions public API relates on **************** */

static void count_images(MultiresBakeRender *bkr)
{
  BLI_listbase_clear(&bkr->image);
  bkr->tot_image = 0;

  for (int i = 0; i < bkr->ob_image.len; i++) {
    Image *ima = bkr->ob_image.array[i];
    if (ima) {
      ima->id.tag &= ~LIB_TAG_DOIT;
    }
  }

  for (int i = 0; i < bkr->ob_image.len; i++) {
    Image *ima = bkr->ob_image.array[i];
    if (ima) {
      if ((ima->id.tag & LIB_TAG_DOIT) == 0) {
        LinkData *data = BLI_genericNodeN(ima);
        BLI_addtail(&bkr->image, data);
        bkr->tot_image++;
        ima->id.tag |= LIB_TAG_DOIT;
      }
    }
  }

  for (int i = 0; i < bkr->ob_image.len; i++) {
    Image *ima = bkr->ob_image.array[i];
    if (ima) {
      ima->id.tag &= ~LIB_TAG_DOIT;
    }
  }
}

static void bake_images(MultiresBakeRender *bkr, MultiresBakeResult *result)
{
  LinkData *link;

  for (link = bkr->image.first; link; link = link->next) {
    Image *ima = (Image *)link->data;

    LISTBASE_FOREACH (ImageTile *, tile, &ima->tiles) {
      ImageUser iuser;
      BKE_imageuser_default(&iuser);
      iuser.tile = tile->tile_number;

      ImBuf *ibuf = BKE_image_acquire_ibuf(ima, &iuser, NULL);

      if (ibuf->x > 0 && ibuf->y > 0) {
        BakeImBufuserData *userdata = MEM_callocN(sizeof(BakeImBufuserData),
                                                  "MultiresBake userdata");
        userdata->mask_buffer = MEM_callocN(ibuf->y * ibuf->x, "MultiresBake imbuf mask");
        ibuf->userdata = userdata;

        switch (bkr->mode) {
          case RE_BAKE_NORMALS:
            do_multires_bake(bkr,
                             ima,
                             tile,
                             ibuf,
                             true,
                             apply_tangmat_callback,
                             init_normal_data,
                             free_normal_data,
                             result);
            break;
          case RE_BAKE_DISPLACEMENT:
            do_multires_bake(bkr,
                             ima,
                             tile,
                             ibuf,
                             false,
                             apply_heights_callback,
                             init_heights_data,
                             free_heights_data,
                             result);
            break;
            /* TODO: restore ambient occlusion baking support. */
#if 0
          case RE_BAKE_AO:
            do_multires_bake(bkr, ima, tile, ibuf, false, apply_ao_callback, init_ao_data, free_ao_data, result);
            break;
#endif
        }
      }

      BKE_image_release_ibuf(ima, ibuf, NULL);
    }

    ima->id.tag |= LIB_TAG_DOIT;
  }
}

static void finish_images(MultiresBakeRender *bkr, MultiresBakeResult *result)
{
  LinkData *link;
  bool use_displacement_buffer = bkr->mode == RE_BAKE_DISPLACEMENT;

  for (link = bkr->image.first; link; link = link->next) {
    Image *ima = (Image *)link->data;

    LISTBASE_FOREACH (ImageTile *, tile, &ima->tiles) {
      ImageUser iuser;
      BKE_imageuser_default(&iuser);
      iuser.tile = tile->tile_number;

      ImBuf *ibuf = BKE_image_acquire_ibuf(ima, &iuser, NULL);
      BakeImBufuserData *userdata = (BakeImBufuserData *)ibuf->userdata;

      if (ibuf->x <= 0 || ibuf->y <= 0) {
        continue;
      }

      if (use_displacement_buffer) {
        bake_ibuf_normalize_displacement(ibuf,
                                         userdata->displacement_buffer,
                                         userdata->mask_buffer,
                                         result->height_min,
                                         result->height_max);
      }

      float uv_offset[2];
      BKE_image_get_tile_uv(ima, tile->tile_number, uv_offset);

      bake_ibuf_filter(ibuf,
                       userdata->mask_buffer,
                       bkr->bake_margin,
                       bkr->bake_margin_type,
                       bkr->lores_dm,
                       uv_offset);

      ibuf->userflags |= IB_DISPLAY_BUFFER_INVALID;
      BKE_image_mark_dirty(ima, ibuf);

      if (ibuf->rect_float) {
        ibuf->userflags |= IB_RECT_INVALID;
      }

      if (ibuf->mipmap[0]) {
        ibuf->userflags |= IB_MIPMAP_INVALID;
        imb_freemipmapImBuf(ibuf);
      }

      if (ibuf->userdata) {
        if (userdata->displacement_buffer) {
          MEM_freeN(userdata->displacement_buffer);
        }

        MEM_freeN(userdata->mask_buffer);
        MEM_freeN(userdata);
        ibuf->userdata = NULL;
      }

      BKE_image_release_ibuf(ima, ibuf, NULL);
      DEG_id_tag_update(&ima->id, 0);
    }
  }
}

void RE_multires_bake_images(MultiresBakeRender *bkr)
{
  MultiresBakeResult result;

  count_images(bkr);
  bake_images(bkr, &result);
  finish_images(bkr, &result);
}