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

pbvh_pixels.cc « intern « blenkernel « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cbbd25d3f4b803a29c0740b5f442c2d70ae63219 (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
/* SPDX-License-Identifier: GPL-2.0-or-later
 * Copyright 2022 Blender Foundation. All rights reserved. */

#include "BKE_customdata.h"
#include "BKE_mesh.h"
#include "BKE_mesh_mapping.h"
#include "BKE_pbvh.h"
#include "BKE_pbvh_pixels.hh"

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

#include "BLI_math.h"
#include "BLI_task.h"
#include "PIL_time.h"

#include "BKE_global.h"
#include "BKE_image_wrappers.hh"

#include "bmesh.h"

#include "pbvh_intern.h"

#include <atomic>

namespace blender::bke::pbvh::pixels {

/**
 * Calculate the delta of two neighbor UV coordinates in the given image buffer.
 */
static float2 calc_barycentric_delta(const float2 uvs[3],
                                     const float2 start_uv,
                                     const float2 end_uv)
{

  float3 start_barycentric;
  barycentric_weights_v2(uvs[0], uvs[1], uvs[2], start_uv, start_barycentric);
  float3 end_barycentric;
  barycentric_weights_v2(uvs[0], uvs[1], uvs[2], end_uv, end_barycentric);
  float3 barycentric = end_barycentric - start_barycentric;
  return float2(barycentric.x, barycentric.y);
}

static float2 calc_barycentric_delta_x(const ImBuf *image_buffer,
                                       const float2 uvs[3],
                                       const int x,
                                       const int y)
{
  const float2 start_uv(float(x) / image_buffer->x, float(y) / image_buffer->y);
  const float2 end_uv(float(x + 1) / image_buffer->x, float(y) / image_buffer->y);
  return calc_barycentric_delta(uvs, start_uv, end_uv);
}

int count_node_pixels(PBVHNode &node)
{
  if (!node.pixels.node_data) {
    return 0;
  }

  NodeData &data = BKE_pbvh_pixels_node_data_get(node);

  int totpixel = 0;

  for (UDIMTilePixels &tile : data.tiles) {
    for (PackedPixelRow &row : tile.pixel_rows) {
      totpixel += row.num_pixels;
    }
  }

  return totpixel;
}

struct SplitQueueData {
  ThreadQueue *new_nodes;
  TaskPool *pool;

  PBVH *pbvh;
  Mesh *mesh;
  Image *image;
  ImageUser *image_user;
};

struct SplitNodePair {
  SplitNodePair *parent;
  PBVHNode node;
  int children_offset = 0;
  int depth = 0;
  int source_index = -1;
  bool is_old = false;
  SplitQueueData *tdata;

  SplitNodePair(SplitNodePair *node_parent = nullptr) : parent(node_parent)
  {
    memset(static_cast<void *>(&node), 0, sizeof(PBVHNode));
  }
};

static void split_thread_job(TaskPool *__restrict pool, void *taskdata);

static void split_pixel_node(PBVH *pbvh,
                             SplitNodePair *split,
                             Mesh *mesh,
                             Image *image,
                             ImageUser *image_user,
                             SplitQueueData *tdata)
{
  BB cb;
  PBVHNode *node = &split->node;

  cb = node->vb;

  if (count_node_pixels(*node) <= pbvh->pixel_leaf_limit || split->depth >= pbvh->depth_limit) {
    BKE_pbvh_pixels_node_data_get(split->node).rebuild_undo_regions();
    return;
  }

  /* Find widest axis and its midpoint */
  const int axis = BB_widest_axis(&cb);
  const float mid = (cb.bmax[axis] + cb.bmin[axis]) * 0.5f;

  node->flag = (PBVHNodeFlags)((int)node->flag & (int)~PBVH_TexLeaf);

  SplitNodePair *split1 = MEM_new<SplitNodePair>("split_pixel_node split1", split);
  SplitNodePair *split2 = MEM_new<SplitNodePair>("split_pixel_node split1", split);

  split1->depth = split->depth + 1;
  split2->depth = split->depth + 1;

  PBVHNode *child1 = &split1->node;
  PBVHNode *child2 = &split2->node;

  child1->flag = PBVH_TexLeaf;
  child2->flag = PBVH_TexLeaf;

  child1->vb = cb;
  child1->vb.bmax[axis] = mid;

  child2->vb = cb;
  child2->vb.bmin[axis] = mid;

  NodeData &data = BKE_pbvh_pixels_node_data_get(split->node);

  NodeData *data1 = MEM_new<NodeData>(__func__);
  NodeData *data2 = MEM_new<NodeData>(__func__);
  child1->pixels.node_data = static_cast<void *>(data1);
  child2->pixels.node_data = static_cast<void *>(data2);

  data1->triangles = data.triangles;
  data2->triangles = data.triangles;

  data1->tiles.resize(data.tiles.size());
  data2->tiles.resize(data.tiles.size());

  for (int i : IndexRange(data.tiles.size())) {
    UDIMTilePixels &tile = data.tiles[i];
    UDIMTilePixels &tile1 = data1->tiles[i];
    UDIMTilePixels &tile2 = data2->tiles[i];

    tile1.tile_number = tile2.tile_number = tile.tile_number;
    tile1.flags.dirty = tile2.flags.dirty = 0;
  }

  ImageUser image_user2 = *image_user;

  for (int i : IndexRange(data.tiles.size())) {
    const UDIMTilePixels &tile = data.tiles[i];

    image_user2.tile = tile.tile_number;

    ImBuf *image_buffer = BKE_image_acquire_ibuf(image, &image_user2, nullptr);
    if (image_buffer == nullptr) {
      continue;
    }

    const MVert *mvert = BKE_pbvh_get_verts(pbvh);

    for (const PackedPixelRow &row : tile.pixel_rows) {
      UDIMTilePixels *tile1 = &data1->tiles[i];
      UDIMTilePixels *tile2 = &data2->tiles[i];

      TrianglePaintInput &tri = data.triangles->paint_input[row.triangle_index];

      float verts[3][3];

      copy_v3_v3(verts[0], mvert[tri.vert_indices[0]].co);
      copy_v3_v3(verts[1], mvert[tri.vert_indices[1]].co);
      copy_v3_v3(verts[2], mvert[tri.vert_indices[2]].co);

      float2 delta = tri.delta_barycentric_coord_u;
      float2 uv1 = row.start_barycentric_coord;
      float2 uv2 = row.start_barycentric_coord + delta * (float)row.num_pixels;

      float co1[3];
      float co2[3];

      interp_barycentric_tri_v3(verts, uv1[0], uv1[1], co1);
      interp_barycentric_tri_v3(verts, uv2[0], uv2[1], co2);

      /* Are we spanning the midpoint? */
      if ((co1[axis] <= mid) != (co2[axis] <= mid)) {
        PackedPixelRow row1 = row;
        float t;

        if (mid < co1[axis]) {
          t = 1.0f - (mid - co2[axis]) / (co1[axis] - co2[axis]);

          SWAP(UDIMTilePixels *, tile1, tile2);
        }
        else {
          t = (mid - co1[axis]) / (co2[axis] - co1[axis]);
        }

        int num_pixels = (int)floorf((float)row.num_pixels * t);

        if (num_pixels) {
          row1.num_pixels = num_pixels;
          tile1->pixel_rows.append(row1);
        }

        if (num_pixels != row.num_pixels) {
          PackedPixelRow row2 = row;

          row2.num_pixels = row.num_pixels - num_pixels;

          row2.start_barycentric_coord = row.start_barycentric_coord +
                                         tri.delta_barycentric_coord_u * (float)num_pixels;
          row2.start_image_coordinate = row.start_image_coordinate;
          row2.start_image_coordinate[0] += num_pixels;

          tile2->pixel_rows.append(row2);
        }
      }
      else if (co1[axis] <= mid && co2[axis] <= mid) {
        tile1->pixel_rows.append(row);
      }
      else {
        tile2->pixel_rows.append(row);
      }
    }

    BKE_image_release_ibuf(image, image_buffer, nullptr);
  }

  if (node->flag & PBVH_Leaf) {
    data.clear_data();
  }
  else {
    pbvh_pixels_free(node);
  }

  data.undo_regions.clear();

  BLI_thread_queue_push(tdata->new_nodes, static_cast<void *>(split1));
  BLI_thread_queue_push(tdata->new_nodes, static_cast<void *>(split2));

  BLI_task_pool_push(tdata->pool, split_thread_job, static_cast<void *>(split1), false, nullptr);
  BLI_task_pool_push(tdata->pool, split_thread_job, static_cast<void *>(split2), false, nullptr);
}

static void split_flush_final_nodes(SplitQueueData *tdata)
{
  PBVH *pbvh = tdata->pbvh;
  Vector<SplitNodePair *> splits;

  while (!BLI_thread_queue_is_empty(tdata->new_nodes)) {
    SplitNodePair *newsplit = static_cast<SplitNodePair *>(BLI_thread_queue_pop(tdata->new_nodes));

    splits.append(newsplit);

    if (newsplit->is_old) {
      continue;
    }

    if (!newsplit->parent->children_offset) {
      newsplit->parent->children_offset = pbvh->totnode;

      pbvh_grow_nodes(pbvh, pbvh->totnode + 2);
      newsplit->source_index = newsplit->parent->children_offset;
    }
    else {
      newsplit->source_index = newsplit->parent->children_offset + 1;
    }
  }

  for (SplitNodePair *split : splits) {
    BLI_assert(split->source_index != -1);

    split->node.children_offset = split->children_offset;
    pbvh->nodes[split->source_index] = split->node;
  }

  for (SplitNodePair *split : splits) {
    MEM_delete<SplitNodePair>(split);
  }
}

static void split_thread_job(TaskPool *__restrict pool, void *taskdata)
{

  SplitQueueData *tdata = static_cast<SplitQueueData *>(BLI_task_pool_user_data(pool));
  SplitNodePair *split = static_cast<SplitNodePair *>(taskdata);

  split_pixel_node(tdata->pbvh, split, tdata->mesh, tdata->image, tdata->image_user, tdata);
}

static void split_pixel_nodes(PBVH *pbvh, Mesh *mesh, Image *image, ImageUser *image_user)
{
  if (G.debug_value == 891) {
    return;
  }

  if (!pbvh->depth_limit) {
    pbvh->depth_limit = 40; /* TODO: move into a constant */
  }

  if (!pbvh->pixel_leaf_limit) {
    pbvh->pixel_leaf_limit = 256 * 256; /* TODO: move into a constant */
  }

  SplitQueueData tdata;
  TaskPool *pool = BLI_task_pool_create_suspended(&tdata, TASK_PRIORITY_HIGH);

  tdata.pool = pool;
  tdata.pbvh = pbvh;
  tdata.mesh = mesh;
  tdata.image = image;
  tdata.image_user = image_user;

  tdata.new_nodes = BLI_thread_queue_init();

  /* Set up initial jobs before initializing threads. */
  for (int i : IndexRange(pbvh->totnode)) {
    if (pbvh->nodes[i].flag & PBVH_TexLeaf) {
      SplitNodePair *split = MEM_new<SplitNodePair>("split_pixel_nodes split");

      split->source_index = i;
      split->is_old = true;
      split->node = pbvh->nodes[i];
      split->tdata = &tdata;

      BLI_task_pool_push(pool, split_thread_job, static_cast<void *>(split), false, nullptr);

      BLI_thread_queue_push(tdata.new_nodes, static_cast<void *>(split));
    }
  }

  BLI_task_pool_work_and_wait(pool);
  BLI_task_pool_free(pool);

  split_flush_final_nodes(&tdata);

  BLI_thread_queue_free(tdata.new_nodes);
}

/**
 * During debugging this check could be enabled.
 * It will write to each image pixel that is covered by the PBVH.
 */
constexpr bool USE_WATERTIGHT_CHECK = false;

static void extract_barycentric_pixels(UDIMTilePixels &tile_data,
                                       const ImBuf *image_buffer,
                                       const int triangle_index,
                                       const float2 uvs[3],
                                       const int minx,
                                       const int miny,
                                       const int maxx,
                                       const int maxy)
{
  for (int y = miny; y < maxy; y++) {
    bool start_detected = false;
    PackedPixelRow pixel_row;
    pixel_row.triangle_index = triangle_index;
    pixel_row.num_pixels = 0;
    int x;

    for (x = minx; x < maxx; x++) {
      float2 uv((float(x) + 0.5f) / image_buffer->x, (float(y) + 0.5f) / image_buffer->y);
      float3 barycentric_weights;
      barycentric_weights_v2(uvs[0], uvs[1], uvs[2], uv, barycentric_weights);

      const bool is_inside = barycentric_inside_triangle_v2(barycentric_weights);
      if (!start_detected && is_inside) {
        start_detected = true;
        pixel_row.start_image_coordinate = ushort2(x, y);
        pixel_row.start_barycentric_coord = float2(barycentric_weights.x, barycentric_weights.y);
      }
      else if (start_detected && !is_inside) {
        break;
      }
    }

    if (!start_detected) {
      continue;
    }
    pixel_row.num_pixels = x - pixel_row.start_image_coordinate.x;
    tile_data.pixel_rows.append(pixel_row);
  }
}

static void init_triangles(PBVH *pbvh, PBVHNode *node, NodeData *node_data, const MLoop *mloop)
{
  if (node_data->triangles) {
    MEM_delete<Triangles>(node_data->triangles);
  }

  node_data->triangles = MEM_new<Triangles>("triangles");

  for (int i = 0; i < node->totprim; i++) {
    const MLoopTri *lt = &pbvh->looptri[node->prim_indices[i]];
    node_data->triangles->append(
        int3(mloop[lt->tri[0]].v, mloop[lt->tri[1]].v, mloop[lt->tri[2]].v));
  }
}

struct EncodePixelsUserData {
  Image *image;
  ImageUser *image_user;
  PBVH *pbvh;
  Vector<PBVHNode *> *nodes;
  const MLoopUV *ldata_uv;
};

static void do_encode_pixels(void *__restrict userdata,
                             const int n,
                             const TaskParallelTLS *__restrict /*tls*/)
{
  EncodePixelsUserData *data = static_cast<EncodePixelsUserData *>(userdata);
  Image *image = data->image;
  ImageUser image_user = *data->image_user;
  PBVH *pbvh = data->pbvh;
  PBVHNode *node = (*data->nodes)[n];
  NodeData *node_data = static_cast<NodeData *>(node->pixels.node_data);
  LISTBASE_FOREACH (ImageTile *, tile, &data->image->tiles) {
    image::ImageTileWrapper image_tile(tile);
    image_user.tile = image_tile.get_tile_number();
    ImBuf *image_buffer = BKE_image_acquire_ibuf(image, &image_user, nullptr);
    if (image_buffer == nullptr) {
      continue;
    }

    float2 tile_offset = float2(image_tile.get_tile_offset());
    UDIMTilePixels tile_data;

    Triangles &triangles = *node_data->triangles;
    for (int triangle_index = 0; triangle_index < triangles.size(); triangle_index++) {
      const MLoopTri *lt = &pbvh->looptri[node->prim_indices[triangle_index]];
      float2 uvs[3] = {
          float2(data->ldata_uv[lt->tri[0]].uv) - tile_offset,
          float2(data->ldata_uv[lt->tri[1]].uv) - tile_offset,
          float2(data->ldata_uv[lt->tri[2]].uv) - tile_offset,
      };

      const float minv = clamp_f(min_fff(uvs[0].y, uvs[1].y, uvs[2].y), 0.0f, 1.0f);
      const int miny = floor(minv * image_buffer->y);
      const float maxv = clamp_f(max_fff(uvs[0].y, uvs[1].y, uvs[2].y), 0.0f, 1.0f);
      const int maxy = min_ii(ceil(maxv * image_buffer->y), image_buffer->y);
      const float minu = clamp_f(min_fff(uvs[0].x, uvs[1].x, uvs[2].x), 0.0f, 1.0f);
      const int minx = floor(minu * image_buffer->x);
      const float maxu = clamp_f(max_fff(uvs[0].x, uvs[1].x, uvs[2].x), 0.0f, 1.0f);
      const int maxx = min_ii(ceil(maxu * image_buffer->x), image_buffer->x);

      TrianglePaintInput &triangle = triangles.get_paint_input(triangle_index);
      triangle.delta_barycentric_coord_u = calc_barycentric_delta_x(image_buffer, uvs, minx, miny);
      extract_barycentric_pixels(
          tile_data, image_buffer, triangle_index, uvs, minx, miny, maxx, maxy);
    }

    BKE_image_release_ibuf(image, image_buffer, nullptr);

    if (tile_data.pixel_rows.is_empty()) {
      continue;
    }

    tile_data.tile_number = image_tile.get_tile_number();
    node_data->tiles.append(tile_data);
  }
}

static bool should_pixels_be_updated(PBVHNode *node)
{
  if ((node->flag & PBVH_Leaf) == 0) {
    return false;
  }
  if (node->children_offset != 0) {
    return false;
  }
  if ((node->flag & PBVH_RebuildPixels) != 0) {
    return true;
  }
  NodeData *node_data = static_cast<NodeData *>(node->pixels.node_data);
  if (node_data != nullptr) {
    return false;
  }
  return true;
}

static int64_t count_nodes_to_update(PBVH *pbvh)
{
  int64_t result = 0;
  for (int n = 0; n < pbvh->totnode; n++) {
    PBVHNode *node = &pbvh->nodes[n];
    if (should_pixels_be_updated(node)) {
      result++;
    }
  }
  return result;
}

/**
 * Find the nodes that needs to be updated.
 *
 * The nodes that require updated are added to the r_nodes_to_update parameter.
 * Will fill in r_visited_polygons with polygons that are owned by nodes that do not require
 * updates.
 *
 * returns if there were any nodes found (true).
 */
static bool find_nodes_to_update(PBVH *pbvh, Vector<PBVHNode *> &r_nodes_to_update)
{
  int64_t nodes_to_update_len = count_nodes_to_update(pbvh);
  if (nodes_to_update_len == 0) {
    return false;
  }

  r_nodes_to_update.reserve(nodes_to_update_len);

  for (int n = 0; n < pbvh->totnode; n++) {
    PBVHNode *node = &pbvh->nodes[n];
    if (!should_pixels_be_updated(node)) {
      continue;
    }
    r_nodes_to_update.append(node);
    node->flag = static_cast<PBVHNodeFlags>(node->flag | PBVH_RebuildPixels);

    if (node->pixels.node_data == nullptr) {
      NodeData *node_data = MEM_new<NodeData>(__func__);
      node->pixels.node_data = node_data;
    }
    else {
      NodeData *node_data = static_cast<NodeData *>(node->pixels.node_data);
      node_data->clear_data();

      if (node_data->triangles && (node->flag & PBVH_Leaf)) {
        MEM_delete<Triangles>(node_data->triangles);
        node_data->triangles = nullptr;
      }
    }
  }

  return true;
}

static void apply_watertight_check(PBVH *pbvh, Image *image, ImageUser *image_user)
{
  ImageUser watertight = *image_user;
  LISTBASE_FOREACH (ImageTile *, tile_data, &image->tiles) {
    image::ImageTileWrapper image_tile(tile_data);
    watertight.tile = image_tile.get_tile_number();
    ImBuf *image_buffer = BKE_image_acquire_ibuf(image, &watertight, nullptr);
    if (image_buffer == nullptr) {
      continue;
    }
    for (int n = 0; n < pbvh->totnode; n++) {
      PBVHNode *node = &pbvh->nodes[n];
      if ((node->flag & PBVH_Leaf) == 0) {
        continue;
      }
      NodeData *node_data = static_cast<NodeData *>(node->pixels.node_data);
      UDIMTilePixels *tile_node_data = node_data->find_tile_data(image_tile);
      if (tile_node_data == nullptr) {
        continue;
      }

      for (PackedPixelRow &pixel_row : tile_node_data->pixel_rows) {
        int pixel_offset = pixel_row.start_image_coordinate.y * image_buffer->x +
                           pixel_row.start_image_coordinate.x;
        for (int x = 0; x < pixel_row.num_pixels; x++) {
          if (image_buffer->rect_float) {
            copy_v4_fl(&image_buffer->rect_float[pixel_offset * 4], 1.0);
          }
          if (image_buffer->rect) {
            uint8_t *dest = static_cast<uint8_t *>(
                static_cast<void *>(&image_buffer->rect[pixel_offset]));
            copy_v4_uchar(dest, 255);
          }
          pixel_offset += 1;
        }
      }
    }
    BKE_image_release_ibuf(image, image_buffer, nullptr);
  }
  BKE_image_partial_update_mark_full_update(image);
}

static bool update_pixels(PBVH *pbvh, Mesh *mesh, Image *image, ImageUser *image_user)
{
  Vector<PBVHNode *> nodes_to_update;

  if (!find_nodes_to_update(pbvh, nodes_to_update)) {
    return false;
  }

  const MLoopUV *ldata_uv = static_cast<const MLoopUV *>(
      CustomData_get_layer(&mesh->ldata, CD_MLOOPUV));
  if (ldata_uv == nullptr) {
    return false;
  }

  for (PBVHNode *node : nodes_to_update) {
    NodeData *node_data = static_cast<NodeData *>(node->pixels.node_data);
    const Span<MLoop> loops = mesh->loops();
    init_triangles(pbvh, node, node_data, loops.data());
  }

  EncodePixelsUserData user_data;
  user_data.pbvh = pbvh;
  user_data.image = image;
  user_data.image_user = image_user;
  user_data.ldata_uv = ldata_uv;
  user_data.nodes = &nodes_to_update;

  TaskParallelSettings settings;
  BKE_pbvh_parallel_range_settings(&settings, true, nodes_to_update.size());
  BLI_task_parallel_range(0, nodes_to_update.size(), &user_data, do_encode_pixels, &settings);
  if (USE_WATERTIGHT_CHECK) {
    apply_watertight_check(pbvh, image, image_user);
  }

  /* Rebuild the undo regions. */
  for (PBVHNode *node : nodes_to_update) {
    NodeData *node_data = static_cast<NodeData *>(node->pixels.node_data);
    node_data->rebuild_undo_regions();
  }

  /* Clear the UpdatePixels flag. */
  for (PBVHNode *node : nodes_to_update) {
    node->flag = static_cast<PBVHNodeFlags>(node->flag & ~PBVH_RebuildPixels);
  }

  /* Add PBVH_TexLeaf flag */
  for (int i : IndexRange(pbvh->totnode)) {
    PBVHNode &node = pbvh->nodes[i];

    if (node.flag & PBVH_Leaf) {
      node.flag = (PBVHNodeFlags)((int)node.flag | (int)PBVH_TexLeaf);
    }
  }

//#define DO_PRINT_STATISTICS
#ifdef DO_PRINT_STATISTICS
  /* Print some statistics about compression ratio. */
  {
    int64_t compressed_data_len = 0;
    int64_t num_pixels = 0;
    for (int n = 0; n < pbvh->totnode; n++) {
      PBVHNode *node = &pbvh->nodes[n];
      if ((node->flag & PBVH_Leaf) == 0) {
        continue;
      }
      NodeData *node_data = static_cast<NodeData *>(node->pixels.node_data);
      compressed_data_len += node_data->triangles.mem_size();
      for (const UDIMTilePixels &tile_data : node_data->tiles) {
        compressed_data_len += tile_data.encoded_pixels.size() * sizeof(PackedPixelRow);
        for (const PackedPixelRow &encoded_pixels : tile_data.encoded_pixels) {
          num_pixels += encoded_pixels.num_pixels;
        }
      }
    }
    printf("Encoded %lld pixels in %lld bytes (%f bytes per pixel)\n",
           num_pixels,
           compressed_data_len,
           float(compressed_data_len) / num_pixels);
  }
#endif

  return true;
}

NodeData &BKE_pbvh_pixels_node_data_get(PBVHNode &node)
{
  BLI_assert(node.pixels.node_data != nullptr);
  NodeData *node_data = static_cast<NodeData *>(node.pixels.node_data);
  return *node_data;
}

void BKE_pbvh_pixels_mark_image_dirty(PBVHNode &node, Image &image, ImageUser &image_user)
{
  BLI_assert(node.pixels.node_data != nullptr);
  NodeData *node_data = static_cast<NodeData *>(node.pixels.node_data);
  if (node_data->flags.dirty) {
    ImageUser local_image_user = image_user;
    LISTBASE_FOREACH (ImageTile *, tile, &image.tiles) {
      image::ImageTileWrapper image_tile(tile);
      local_image_user.tile = image_tile.get_tile_number();
      ImBuf *image_buffer = BKE_image_acquire_ibuf(&image, &local_image_user, nullptr);
      if (image_buffer == nullptr) {
        continue;
      }

      node_data->mark_region(image, image_tile, *image_buffer);
      BKE_image_release_ibuf(&image, image_buffer, nullptr);
    }
    node_data->flags.dirty = false;
  }
}
}  // namespace blender::bke::pbvh::pixels

extern "C" {
using namespace blender::bke::pbvh::pixels;

void BKE_pbvh_build_pixels(PBVH *pbvh, Mesh *mesh, Image *image, ImageUser *image_user)
{
  if (update_pixels(pbvh, mesh, image, image_user)) {
    double time_ms = PIL_check_seconds_timer();

    split_pixel_nodes(pbvh, mesh, image, image_user);

    time_ms = PIL_check_seconds_timer() - time_ms;

    printf("Nodes split time: %.2fms\n", time_ms * 1000.0);
  }
}

void pbvh_pixels_free(PBVHNode *node)
{
  NodeData *node_data = static_cast<NodeData *>(node->pixels.node_data);

  if (!node_data) {
    return;
  }

  if (node_data->triangles && (node->flag & PBVH_Leaf)) {
    MEM_delete<Triangles>(node_data->triangles);
  }

  MEM_delete(node_data);
  node->pixels.node_data = nullptr;
}
}