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
|
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The Original Code is Copyright (C) 2017 by Blender Foundation.
* All rights reserved.
*/
/** \file
* \ingroup draw
*
* \brief Extraction of Mesh data into VBO to feed to GPU.
*/
#include "MEM_guardedalloc.h"
#include "atomic_ops.h"
#include "DNA_mesh_types.h"
#include "DNA_scene_types.h"
#include "BLI_task.h"
#include "BLI_vector.hh"
#include "BKE_editmesh.h"
#include "GPU_capabilities.h"
#include "draw_cache_extract.h"
#include "draw_cache_extract_mesh_private.h"
#include "draw_cache_inline.h"
// #define DEBUG_TIME
#ifdef DEBUG_TIME
# include "PIL_time_utildefines.h"
#endif
#define CHUNK_SIZE 8192
namespace blender::draw {
/* ---------------------------------------------------------------------- */
/** \name Mesh Elements Extract Struct
* \{ */
struct MeshExtractRunData {
const MeshExtract *extractor;
void *buffer;
void *user_data;
};
using MeshExtractRunDataArray = blender::Vector<MeshExtractRunData>;
static void mesh_extract_run_data_array_add(MeshExtractRunDataArray &array,
const MeshExtract *extractor)
{
MeshExtractRunData run_data;
run_data.extractor = extractor;
run_data.buffer = NULL;
run_data.user_data = NULL;
array.append(run_data);
}
static void mesh_extract_run_data_array_filter_iter_type(const MeshExtractRunDataArray &src,
MeshExtractRunDataArray &dst,
eMRIterType iter_type)
{
for (const MeshExtractRunData &data : src) {
const MeshExtract *extractor = data.extractor;
if ((iter_type & MR_ITER_LOOPTRI) && extractor->iter_looptri_bm) {
BLI_assert(extractor->iter_looptri_mesh);
dst.append(data);
continue;
}
if ((iter_type & MR_ITER_POLY) && extractor->iter_poly_bm) {
BLI_assert(extractor->iter_poly_mesh);
dst.append(data);
continue;
}
if ((iter_type & MR_ITER_LEDGE) && extractor->iter_ledge_bm) {
BLI_assert(extractor->iter_ledge_mesh);
dst.append(data);
continue;
}
if ((iter_type & MR_ITER_LVERT) && extractor->iter_lvert_bm) {
BLI_assert(extractor->iter_lvert_mesh);
dst.append(data);
continue;
}
}
}
static void mesh_extract_run_data_array_filter_threading(
const MeshExtractRunDataArray &src, MeshExtractRunDataArray &dst_multi_threaded)
{
for (const MeshExtractRunData &data : src) {
const MeshExtract *extractor = data.extractor;
if (extractor->use_threading) {
mesh_extract_run_data_array_add(dst_multi_threaded, extractor);
}
}
}
/** \} */
/* ---------------------------------------------------------------------- */
/** \name Extract
* \{ */
static void extracts_flags_get(const MeshExtractRunDataArray &extractors,
eMRIterType *r_iter_type,
eMRDataType *r_data_flag)
{
eMRIterType iter_type = static_cast<eMRIterType>(0);
eMRDataType data_flag = static_cast<eMRDataType>(0);
for (const MeshExtractRunData &data : extractors) {
const MeshExtract *extractor = data.extractor;
iter_type |= mesh_extract_iter_type(extractor);
data_flag |= extractor->data_flag;
}
if (r_iter_type) {
*r_iter_type = iter_type;
}
if (r_data_flag) {
*r_data_flag = data_flag;
}
}
BLI_INLINE void extract_init(const MeshRenderData *mr,
struct MeshBatchCache *cache,
MeshExtractRunDataArray &extractors,
MeshBufferCache *mbc)
{
/* Multi thread. */
for (MeshExtractRunData &run_data : extractors) {
const MeshExtract *extractor = run_data.extractor;
run_data.buffer = mesh_extract_buffer_get(extractor, mbc);
run_data.user_data = extractor->init(mr, cache, run_data.buffer);
}
}
BLI_INLINE void extract_iter_looptri_bm(const MeshRenderData *mr,
const ExtractTriBMesh_Params *params,
const MeshExtractRunDataArray &all_extractors)
{
MeshExtractRunDataArray extractors;
mesh_extract_run_data_array_filter_iter_type(all_extractors, extractors, MR_ITER_LOOPTRI);
EXTRACT_TRIS_LOOPTRI_FOREACH_BM_BEGIN(elt, elt_index, params)
{
for (MeshExtractRunData &run_data : extractors) {
run_data.extractor->iter_looptri_bm(mr, elt, elt_index, run_data.user_data);
}
}
EXTRACT_TRIS_LOOPTRI_FOREACH_BM_END;
}
BLI_INLINE void extract_iter_looptri_mesh(const MeshRenderData *mr,
const ExtractTriMesh_Params *params,
const MeshExtractRunDataArray &all_extractors)
{
MeshExtractRunDataArray extractors;
mesh_extract_run_data_array_filter_iter_type(all_extractors, extractors, MR_ITER_LOOPTRI);
EXTRACT_TRIS_LOOPTRI_FOREACH_MESH_BEGIN(mlt, mlt_index, params)
{
for (MeshExtractRunData &run_data : extractors) {
run_data.extractor->iter_looptri_mesh(mr, mlt, mlt_index, run_data.user_data);
}
}
EXTRACT_TRIS_LOOPTRI_FOREACH_MESH_END;
}
BLI_INLINE void extract_iter_poly_bm(const MeshRenderData *mr,
const ExtractPolyBMesh_Params *params,
const MeshExtractRunDataArray &all_extractors)
{
MeshExtractRunDataArray extractors;
mesh_extract_run_data_array_filter_iter_type(all_extractors, extractors, MR_ITER_POLY);
EXTRACT_POLY_FOREACH_BM_BEGIN(f, f_index, params, mr)
{
for (MeshExtractRunData &run_data : extractors) {
run_data.extractor->iter_poly_bm(mr, f, f_index, run_data.user_data);
}
}
EXTRACT_POLY_FOREACH_BM_END;
}
BLI_INLINE void extract_iter_poly_mesh(const MeshRenderData *mr,
const ExtractPolyMesh_Params *params,
const MeshExtractRunDataArray &all_extractors)
{
MeshExtractRunDataArray extractors;
mesh_extract_run_data_array_filter_iter_type(all_extractors, extractors, MR_ITER_POLY);
EXTRACT_POLY_FOREACH_MESH_BEGIN(mp, mp_index, params, mr)
{
for (MeshExtractRunData &run_data : extractors) {
run_data.extractor->iter_poly_mesh(mr, mp, mp_index, run_data.user_data);
}
}
EXTRACT_POLY_FOREACH_MESH_END;
}
BLI_INLINE void extract_iter_ledge_bm(const MeshRenderData *mr,
const ExtractLEdgeBMesh_Params *params,
const MeshExtractRunDataArray &all_extractors)
{
MeshExtractRunDataArray extractors;
mesh_extract_run_data_array_filter_iter_type(all_extractors, extractors, MR_ITER_LEDGE);
EXTRACT_LEDGE_FOREACH_BM_BEGIN(eed, ledge_index, params)
{
for (MeshExtractRunData &run_data : extractors) {
run_data.extractor->iter_ledge_bm(mr, eed, ledge_index, run_data.user_data);
}
}
EXTRACT_LEDGE_FOREACH_BM_END;
}
BLI_INLINE void extract_iter_ledge_mesh(const MeshRenderData *mr,
const ExtractLEdgeMesh_Params *params,
const MeshExtractRunDataArray &all_extractors)
{
MeshExtractRunDataArray extractors;
mesh_extract_run_data_array_filter_iter_type(all_extractors, extractors, MR_ITER_LEDGE);
EXTRACT_LEDGE_FOREACH_MESH_BEGIN(med, ledge_index, params, mr)
{
for (MeshExtractRunData &run_data : extractors) {
run_data.extractor->iter_ledge_mesh(mr, med, ledge_index, run_data.user_data);
}
}
EXTRACT_LEDGE_FOREACH_MESH_END;
}
BLI_INLINE void extract_iter_lvert_bm(const MeshRenderData *mr,
const ExtractLVertBMesh_Params *params,
const MeshExtractRunDataArray &all_extractors)
{
MeshExtractRunDataArray extractors;
mesh_extract_run_data_array_filter_iter_type(all_extractors, extractors, MR_ITER_LVERT);
EXTRACT_LVERT_FOREACH_BM_BEGIN(eve, lvert_index, params)
{
for (MeshExtractRunData &run_data : extractors) {
run_data.extractor->iter_lvert_bm(mr, eve, lvert_index, run_data.user_data);
}
}
EXTRACT_LVERT_FOREACH_BM_END;
}
BLI_INLINE void extract_iter_lvert_mesh(const MeshRenderData *mr,
const ExtractLVertMesh_Params *params,
const MeshExtractRunDataArray &all_extractors)
{
MeshExtractRunDataArray extractors;
mesh_extract_run_data_array_filter_iter_type(all_extractors, extractors, MR_ITER_LVERT);
EXTRACT_LVERT_FOREACH_MESH_BEGIN(mv, lvert_index, params, mr)
{
for (MeshExtractRunData &run_data : extractors) {
run_data.extractor->iter_lvert_mesh(mr, mv, lvert_index, run_data.user_data);
}
}
EXTRACT_LVERT_FOREACH_MESH_END;
}
BLI_INLINE void extract_finish(const MeshRenderData *mr,
struct MeshBatchCache *cache,
const MeshExtractRunDataArray &extractors)
{
for (const MeshExtractRunData &run_data : extractors) {
const MeshExtract *extractor = run_data.extractor;
if (extractor->finish) {
extractor->finish(mr, cache, run_data.buffer, run_data.user_data);
}
}
}
/* Single Thread. */
BLI_INLINE void extract_run_and_finish_init(const MeshRenderData *mr,
struct MeshBatchCache *cache,
MeshExtractRunDataArray &extractors,
eMRIterType iter_type,
MeshBufferCache *mbc)
{
extract_init(mr, cache, extractors, mbc);
bool is_mesh = mr->extract_type != MR_EXTRACT_BMESH;
if (iter_type & MR_ITER_LOOPTRI) {
if (is_mesh) {
ExtractTriMesh_Params params;
params.mlooptri = mr->mlooptri;
params.tri_range[0] = 0;
params.tri_range[1] = mr->tri_len;
extract_iter_looptri_mesh(mr, ¶ms, extractors);
}
else {
ExtractTriBMesh_Params params;
params.looptris = mr->edit_bmesh->looptris;
params.tri_range[0] = 0;
params.tri_range[1] = mr->tri_len;
extract_iter_looptri_bm(mr, ¶ms, extractors);
}
}
if (iter_type & MR_ITER_POLY) {
if (is_mesh) {
ExtractPolyMesh_Params params;
params.poly_range[0] = 0;
params.poly_range[1] = mr->poly_len;
extract_iter_poly_mesh(mr, ¶ms, extractors);
}
else {
ExtractPolyBMesh_Params params;
params.poly_range[0] = 0;
params.poly_range[1] = mr->poly_len;
extract_iter_poly_bm(mr, ¶ms, extractors);
}
}
if (iter_type & MR_ITER_LEDGE) {
if (is_mesh) {
ExtractLEdgeMesh_Params params;
params.ledge = mr->ledges;
params.ledge_range[0] = 0;
params.ledge_range[1] = mr->edge_loose_len;
extract_iter_ledge_mesh(mr, ¶ms, extractors);
}
else {
ExtractLEdgeBMesh_Params params;
params.ledge = mr->ledges;
params.ledge_range[0] = 0;
params.ledge_range[1] = mr->edge_loose_len;
extract_iter_ledge_bm(mr, ¶ms, extractors);
}
}
if (iter_type & MR_ITER_LVERT) {
if (is_mesh) {
ExtractLVertMesh_Params params;
params.lvert = mr->lverts;
params.lvert_range[0] = 0;
params.lvert_range[1] = mr->vert_loose_len;
extract_iter_lvert_mesh(mr, ¶ms, extractors);
}
else {
ExtractLVertBMesh_Params params;
params.lvert = mr->lverts;
params.lvert_range[0] = 0;
params.lvert_range[1] = mr->vert_loose_len;
extract_iter_lvert_bm(mr, ¶ms, extractors);
}
}
extract_finish(mr, cache, extractors);
}
/** \} */
/* ---------------------------------------------------------------------- */
/** \name ExtractTaskData
* \{ */
struct ExtractTaskData {
void *next = nullptr;
void *prev = nullptr;
const MeshRenderData *mr = nullptr;
MeshBatchCache *cache = nullptr;
/* #UserData is shared between the iterations as it holds counters to detect if the
* extraction is finished. To make sure the duplication of the user_data does not create a new
* instance of the counters we allocate the user_data in its own container.
*
* This structure makes sure that when extract_init is called, that the user data of all
* iterations are updated. */
MeshExtractRunDataArray *extractors = nullptr;
MeshBufferCache *mbc = nullptr;
int32_t *task_counter = nullptr;
eMRIterType iter_type;
int start = 0;
int end = INT_MAX;
/** Decremented each time a task is finished. */
ExtractTaskData(const MeshRenderData *mr,
struct MeshBatchCache *cache,
MeshExtractRunDataArray *extractors,
MeshBufferCache *mbc,
int32_t *task_counter)
: mr(mr), cache(cache), extractors(extractors), mbc(mbc), task_counter(task_counter)
{
extracts_flags_get(*extractors, &iter_type, NULL);
};
ExtractTaskData(const ExtractTaskData &src) = default;
~ExtractTaskData()
{
delete extractors;
}
#ifdef WITH_CXX_GUARDEDALLOC
MEM_CXX_CLASS_ALLOC_FUNCS("DRW:ExtractTaskData")
#endif
};
static ExtractTaskData *extract_extract_iter_task_data_create_mesh(
const MeshRenderData *mr,
MeshBatchCache *cache,
MeshExtractRunDataArray *extractors,
MeshBufferCache *mbc,
int32_t *task_counter)
{
ExtractTaskData *taskdata = new ExtractTaskData(mr, cache, extractors, mbc, task_counter);
return taskdata;
}
static void extract_task_data_free(void *data)
{
ExtractTaskData *task_data = static_cast<ExtractTaskData *>(data);
delete task_data;
}
static void extract_task_data_free_ex(void *data)
{
ExtractTaskData *task_data = static_cast<ExtractTaskData *>(data);
task_data->extractors = nullptr;
delete task_data;
}
BLI_INLINE void mesh_extract_iter(const MeshRenderData *mr,
const eMRIterType iter_type,
int start,
int end,
MeshExtractRunDataArray &extractors)
{
switch (mr->extract_type) {
case MR_EXTRACT_BMESH:
if (iter_type & MR_ITER_LOOPTRI) {
ExtractTriBMesh_Params params;
params.looptris = mr->edit_bmesh->looptris;
params.tri_range[0] = start;
params.tri_range[1] = min_ii(mr->tri_len, end);
extract_iter_looptri_bm(mr, ¶ms, extractors);
}
if (iter_type & MR_ITER_POLY) {
ExtractPolyBMesh_Params params;
params.poly_range[0] = start;
params.poly_range[1] = min_ii(mr->poly_len, end);
extract_iter_poly_bm(mr, ¶ms, extractors);
}
if (iter_type & MR_ITER_LEDGE) {
ExtractLEdgeBMesh_Params params;
params.ledge = mr->ledges;
params.ledge_range[0] = start;
params.ledge_range[1] = min_ii(mr->edge_loose_len, end);
extract_iter_ledge_bm(mr, ¶ms, extractors);
}
if (iter_type & MR_ITER_LVERT) {
ExtractLVertBMesh_Params params;
params.lvert = mr->lverts;
params.lvert_range[0] = start;
params.lvert_range[1] = min_ii(mr->vert_loose_len, end);
extract_iter_lvert_bm(mr, ¶ms, extractors);
}
break;
case MR_EXTRACT_MAPPED:
case MR_EXTRACT_MESH:
if (iter_type & MR_ITER_LOOPTRI) {
ExtractTriMesh_Params params;
params.mlooptri = mr->mlooptri;
params.tri_range[0] = start;
params.tri_range[1] = min_ii(mr->tri_len, end);
extract_iter_looptri_mesh(mr, ¶ms, extractors);
}
if (iter_type & MR_ITER_POLY) {
ExtractPolyMesh_Params params;
params.poly_range[0] = start;
params.poly_range[1] = min_ii(mr->poly_len, end);
extract_iter_poly_mesh(mr, ¶ms, extractors);
}
if (iter_type & MR_ITER_LEDGE) {
ExtractLEdgeMesh_Params params;
params.ledge = mr->ledges;
params.ledge_range[0] = start;
params.ledge_range[1] = min_ii(mr->edge_loose_len, end);
extract_iter_ledge_mesh(mr, ¶ms, extractors);
}
if (iter_type & MR_ITER_LVERT) {
ExtractLVertMesh_Params params;
params.lvert = mr->lverts;
params.lvert_range[0] = start;
params.lvert_range[1] = min_ii(mr->vert_loose_len, end);
extract_iter_lvert_mesh(mr, ¶ms, extractors);
}
break;
}
}
static void extract_task_init(ExtractTaskData *data)
{
extract_init(data->mr, data->cache, *data->extractors, data->mbc);
}
static void extract_task_run(void *__restrict taskdata)
{
ExtractTaskData *data = (ExtractTaskData *)taskdata;
mesh_extract_iter(data->mr, data->iter_type, data->start, data->end, *data->extractors);
/* If this is the last task, we do the finish function. */
int remainin_tasks = atomic_sub_and_fetch_int32(data->task_counter, 1);
if (remainin_tasks == 0) {
extract_finish(data->mr, data->cache, *data->extractors);
}
}
static void extract_task_init_and_run(void *__restrict taskdata)
{
ExtractTaskData *data = (ExtractTaskData *)taskdata;
extract_run_and_finish_init(
data->mr, data->cache, *data->extractors, data->iter_type, data->mbc);
}
/** \} */
/* ---------------------------------------------------------------------- */
/** \name Task Node - Update Mesh Render Data
* \{ */
struct MeshRenderDataUpdateTaskData {
MeshRenderData *mr = nullptr;
eMRIterType iter_type;
eMRDataType data_flag;
~MeshRenderDataUpdateTaskData()
{
mesh_render_data_free(mr);
}
#ifdef WITH_CXX_GUARDEDALLOC
MEM_CXX_CLASS_ALLOC_FUNCS("DRW:MeshRenderDataUpdateTaskData")
#endif
};
static void mesh_render_data_update_task_data_free(MeshRenderDataUpdateTaskData *taskdata)
{
BLI_assert(taskdata);
delete taskdata;
}
static void mesh_extract_render_data_node_exec(void *__restrict task_data)
{
MeshRenderDataUpdateTaskData *update_task_data = static_cast<MeshRenderDataUpdateTaskData *>(
task_data);
MeshRenderData *mr = update_task_data->mr;
const eMRIterType iter_type = update_task_data->iter_type;
const eMRDataType data_flag = update_task_data->data_flag;
mesh_render_data_update_normals(mr, iter_type, data_flag);
mesh_render_data_update_looptris(mr, iter_type, data_flag);
}
static struct TaskNode *mesh_extract_render_data_node_create(struct TaskGraph *task_graph,
MeshRenderData *mr,
const eMRIterType iter_type,
const eMRDataType data_flag)
{
MeshRenderDataUpdateTaskData *task_data = new (MeshRenderDataUpdateTaskData);
task_data->mr = mr;
task_data->iter_type = iter_type;
task_data->data_flag = data_flag;
struct TaskNode *task_node = BLI_task_graph_node_create(
task_graph,
mesh_extract_render_data_node_exec,
task_data,
(TaskGraphNodeFreeFunction)mesh_render_data_update_task_data_free);
return task_node;
}
/** \} */
/* ---------------------------------------------------------------------- */
/** \name Task Node - Extract Single Threaded
* \{ */
static struct TaskNode *extract_single_threaded_task_node_create(struct TaskGraph *task_graph,
ExtractTaskData *task_data)
{
struct TaskNode *task_node = BLI_task_graph_node_create(
task_graph,
extract_task_init_and_run,
task_data,
(TaskGraphNodeFreeFunction)extract_task_data_free);
return task_node;
}
/** \} */
/* ---------------------------------------------------------------------- */
/** \name Task Node - UserData Initializer
* \{ */
struct UserDataInitTaskData {
ExtractTaskData *td;
int32_t task_counter = 0;
~UserDataInitTaskData()
{
extract_task_data_free(td);
}
#ifdef WITH_CXX_GUARDEDALLOC
MEM_CXX_CLASS_ALLOC_FUNCS("DRW:UserDataInitTaskData")
#endif
};
static void user_data_init_task_data_free(UserDataInitTaskData *taskdata)
{
delete taskdata;
}
static void user_data_init_task_data_exec(void *__restrict task_data)
{
UserDataInitTaskData *extract_task_data = static_cast<UserDataInitTaskData *>(task_data);
ExtractTaskData *taskdata_base = extract_task_data->td;
extract_task_init(taskdata_base);
}
static struct TaskNode *user_data_init_task_node_create(struct TaskGraph *task_graph,
UserDataInitTaskData *task_data)
{
struct TaskNode *task_node = BLI_task_graph_node_create(
task_graph,
user_data_init_task_data_exec,
task_data,
(TaskGraphNodeFreeFunction)user_data_init_task_data_free);
return task_node;
}
/** \} */
/* ---------------------------------------------------------------------- */
/** \name Extract Loop
* \{ */
static void extract_range_task_create(struct TaskGraph *task_graph,
struct TaskNode *task_node_user_data_init,
ExtractTaskData *taskdata,
const eMRIterType type,
int start,
int length)
{
taskdata = new ExtractTaskData(*taskdata);
atomic_add_and_fetch_int32(taskdata->task_counter, 1);
taskdata->iter_type = type;
taskdata->start = start;
taskdata->end = start + length;
struct TaskNode *task_node = BLI_task_graph_node_create(
task_graph, extract_task_run, taskdata, extract_task_data_free_ex);
BLI_task_graph_edge_create(task_node_user_data_init, task_node);
}
static int extract_range_task_num_elements_get(const MeshRenderData *mr,
const eMRIterType iter_type)
{
/* Divide task into sensible chunks. */
int iter_len = 0;
if (iter_type & MR_ITER_LOOPTRI) {
iter_len += mr->tri_len;
}
if (iter_type & MR_ITER_POLY) {
iter_len += mr->poly_len;
}
if (iter_type & MR_ITER_LEDGE) {
iter_len += mr->edge_loose_len;
}
if (iter_type & MR_ITER_LVERT) {
iter_len += mr->vert_loose_len;
}
return iter_len;
}
static int extract_range_task_chunk_size_get(const MeshRenderData *mr,
const eMRIterType iter_type,
const int num_threads)
{
/* Divide task into sensible chunks. */
const int num_elements = extract_range_task_num_elements_get(mr, iter_type);
int range_len = (num_elements + num_threads) / num_threads;
CLAMP_MIN(range_len, CHUNK_SIZE);
return range_len;
}
static void extract_task_in_ranges_create(struct TaskGraph *task_graph,
struct TaskNode *task_node_user_data_init,
ExtractTaskData *taskdata_base,
const int num_threads)
{
const MeshRenderData *mr = taskdata_base->mr;
const int range_len = extract_range_task_chunk_size_get(
mr, taskdata_base->iter_type, num_threads);
if (taskdata_base->iter_type & MR_ITER_LOOPTRI) {
for (int i = 0; i < mr->tri_len; i += range_len) {
extract_range_task_create(
task_graph, task_node_user_data_init, taskdata_base, MR_ITER_LOOPTRI, i, range_len);
}
}
if (taskdata_base->iter_type & MR_ITER_POLY) {
for (int i = 0; i < mr->poly_len; i += range_len) {
extract_range_task_create(
task_graph, task_node_user_data_init, taskdata_base, MR_ITER_POLY, i, range_len);
}
}
if (taskdata_base->iter_type & MR_ITER_LEDGE) {
for (int i = 0; i < mr->edge_loose_len; i += range_len) {
extract_range_task_create(
task_graph, task_node_user_data_init, taskdata_base, MR_ITER_LEDGE, i, range_len);
}
}
if (taskdata_base->iter_type & MR_ITER_LVERT) {
for (int i = 0; i < mr->vert_loose_len; i += range_len) {
extract_range_task_create(
task_graph, task_node_user_data_init, taskdata_base, MR_ITER_LVERT, i, range_len);
}
}
}
static void mesh_buffer_cache_create_requested(struct TaskGraph *task_graph,
MeshBatchCache *cache,
MeshBufferCache *mbc,
MeshBufferExtractionCache *extraction_cache,
Mesh *me,
const bool is_editmode,
const bool is_paint_mode,
const bool is_mode_active,
const float obmat[4][4],
const bool do_final,
const bool do_uvedit,
const bool use_subsurf_fdots,
const DRW_MeshCDMask *cd_layer_used,
const Scene *scene,
const ToolSettings *ts,
const bool use_hide)
{
/* For each mesh where batches needs to be updated a sub-graph will be added to the task_graph.
* This sub-graph starts with an extract_render_data_node. This fills/converts the required
* data from Mesh.
*
* Small extractions and extractions that can't be multi-threaded are grouped in a single
* `extract_single_threaded_task_node`.
*
* Other extractions will create a node for each loop exceeding 8192 items. these nodes are
* linked to the `user_data_init_task_node`. the `user_data_init_task_node` prepares the
* user_data needed for the extraction based on the data extracted from the mesh.
* counters are used to check if the finalize of a task has to be called.
*
* Mesh extraction sub graph
*
* +----------------------+
* +-----> | extract_task1_loop_1 |
* | +----------------------+
* +------------------+ +----------------------+ +----------------------+
* | mesh_render_data | --> | | --> | extract_task1_loop_2 |
* +------------------+ | | +----------------------+
* | | | +----------------------+
* | | user_data_init | --> | extract_task2_loop_1 |
* v | | +----------------------+
* +------------------+ | | +----------------------+
* | single_threaded | | | --> | extract_task2_loop_2 |
* +------------------+ +----------------------+ +----------------------+
* | +----------------------+
* +-----> | extract_task2_loop_3 |
* +----------------------+
*/
const bool do_lines_loose_subbuffer = mbc->ibo.lines_loose != NULL;
const bool do_hq_normals = (scene->r.perf_flag & SCE_PERF_HQ_NORMALS) != 0 ||
GPU_use_hq_normals_workaround();
/* Create an array containing all the extractors that needs to be executed. */
MeshExtractRunDataArray extractors;
#define EXTRACT_ADD_REQUESTED(type, type_lowercase, name) \
do { \
if (DRW_##type_lowercase##_requested(mbc->type_lowercase.name)) { \
const MeshExtract *extractor = mesh_extract_override_get( \
&extract_##name, do_hq_normals, do_lines_loose_subbuffer); \
mesh_extract_run_data_array_add(extractors, extractor); \
} \
} while (0)
EXTRACT_ADD_REQUESTED(VBO, vbo, pos_nor);
EXTRACT_ADD_REQUESTED(VBO, vbo, lnor);
EXTRACT_ADD_REQUESTED(VBO, vbo, uv);
EXTRACT_ADD_REQUESTED(VBO, vbo, tan);
EXTRACT_ADD_REQUESTED(VBO, vbo, vcol);
EXTRACT_ADD_REQUESTED(VBO, vbo, sculpt_data);
EXTRACT_ADD_REQUESTED(VBO, vbo, orco);
EXTRACT_ADD_REQUESTED(VBO, vbo, edge_fac);
EXTRACT_ADD_REQUESTED(VBO, vbo, weights);
EXTRACT_ADD_REQUESTED(VBO, vbo, edit_data);
EXTRACT_ADD_REQUESTED(VBO, vbo, edituv_data);
EXTRACT_ADD_REQUESTED(VBO, vbo, edituv_stretch_area);
EXTRACT_ADD_REQUESTED(VBO, vbo, edituv_stretch_angle);
EXTRACT_ADD_REQUESTED(VBO, vbo, mesh_analysis);
EXTRACT_ADD_REQUESTED(VBO, vbo, fdots_pos);
EXTRACT_ADD_REQUESTED(VBO, vbo, fdots_nor);
EXTRACT_ADD_REQUESTED(VBO, vbo, fdots_uv);
EXTRACT_ADD_REQUESTED(VBO, vbo, fdots_edituv_data);
EXTRACT_ADD_REQUESTED(VBO, vbo, poly_idx);
EXTRACT_ADD_REQUESTED(VBO, vbo, edge_idx);
EXTRACT_ADD_REQUESTED(VBO, vbo, vert_idx);
EXTRACT_ADD_REQUESTED(VBO, vbo, fdot_idx);
EXTRACT_ADD_REQUESTED(VBO, vbo, skin_roots);
EXTRACT_ADD_REQUESTED(IBO, ibo, tris);
EXTRACT_ADD_REQUESTED(IBO, ibo, lines);
EXTRACT_ADD_REQUESTED(IBO, ibo, points);
EXTRACT_ADD_REQUESTED(IBO, ibo, fdots);
EXTRACT_ADD_REQUESTED(IBO, ibo, lines_paint_mask);
EXTRACT_ADD_REQUESTED(IBO, ibo, lines_adjacency);
EXTRACT_ADD_REQUESTED(IBO, ibo, edituv_tris);
EXTRACT_ADD_REQUESTED(IBO, ibo, edituv_lines);
EXTRACT_ADD_REQUESTED(IBO, ibo, edituv_points);
EXTRACT_ADD_REQUESTED(IBO, ibo, edituv_fdots);
#undef EXTRACT_ADD_REQUESTED
if (extractors.is_empty()) {
return;
}
#ifdef DEBUG_TIME
double rdata_start = PIL_check_seconds_timer();
#endif
eMRIterType iter_type;
eMRDataType data_flag;
extracts_flags_get(extractors, &iter_type, &data_flag);
MeshRenderData *mr = mesh_render_data_create(me,
extraction_cache,
is_editmode,
is_paint_mode,
is_mode_active,
obmat,
do_final,
do_uvedit,
cd_layer_used,
ts,
iter_type);
mr->use_hide = use_hide;
mr->use_subsurf_fdots = use_subsurf_fdots;
mr->use_final_mesh = do_final;
#ifdef DEBUG_TIME
double rdata_end = PIL_check_seconds_timer();
#endif
struct TaskNode *task_node_mesh_render_data = mesh_extract_render_data_node_create(
task_graph, mr, iter_type, data_flag);
/* Simple heuristic. */
const bool use_thread = (mr->loop_len + mr->loop_loose_len) > CHUNK_SIZE;
if (use_thread) {
uint threads_to_use = 0;
/* First run the requested extractors that do not support asynchronous ranges. */
for (const MeshExtractRunData &run_data : extractors) {
const MeshExtract *extractor = run_data.extractor;
if (!extractor->use_threading) {
MeshExtractRunDataArray *single_threaded_extractors = new MeshExtractRunDataArray();
mesh_extract_run_data_array_add(*single_threaded_extractors, extractor);
ExtractTaskData *taskdata = extract_extract_iter_task_data_create_mesh(
mr, cache, single_threaded_extractors, mbc, NULL);
struct TaskNode *task_node = extract_single_threaded_task_node_create(task_graph,
taskdata);
BLI_task_graph_edge_create(task_node_mesh_render_data, task_node);
}
threads_to_use++;
}
/* Distribute the remaining extractors into ranges per core. */
MeshExtractRunDataArray *multi_threaded_extractors = new MeshExtractRunDataArray();
mesh_extract_run_data_array_filter_threading(extractors, *multi_threaded_extractors);
if (!multi_threaded_extractors->is_empty()) {
/*
* Determine the number of thread to use for multithreading.
* Thread can be used for single threaded tasks. These typically take longer to execute so
* fill the rest of the threads for range operations.
*/
int num_threads = BLI_task_scheduler_num_threads();
if (threads_to_use < num_threads) {
num_threads -= threads_to_use;
}
UserDataInitTaskData *user_data_init_task_data = new UserDataInitTaskData();
struct TaskNode *task_node_user_data_init = user_data_init_task_node_create(
task_graph, user_data_init_task_data);
user_data_init_task_data->td = extract_extract_iter_task_data_create_mesh(
mr, cache, multi_threaded_extractors, mbc, &user_data_init_task_data->task_counter);
extract_task_in_ranges_create(
task_graph, task_node_user_data_init, user_data_init_task_data->td, num_threads);
BLI_task_graph_edge_create(task_node_mesh_render_data, task_node_user_data_init);
}
else {
/* No tasks created freeing extractors list. */
delete multi_threaded_extractors;
}
}
else {
/* Run all requests on the same thread. */
MeshExtractRunDataArray *extractors_copy = new MeshExtractRunDataArray(extractors);
ExtractTaskData *taskdata = extract_extract_iter_task_data_create_mesh(
mr, cache, extractors_copy, mbc, NULL);
struct TaskNode *task_node = extract_single_threaded_task_node_create(task_graph, taskdata);
BLI_task_graph_edge_create(task_node_mesh_render_data, task_node);
}
/* Trigger the sub-graph for this mesh. */
BLI_task_graph_node_push_work(task_node_mesh_render_data);
#ifdef DEBUG_TIME
BLI_task_graph_work_and_wait(task_graph);
double end = PIL_check_seconds_timer();
static double avg = 0;
static double avg_fps = 0;
static double avg_rdata = 0;
static double end_prev = 0;
if (end_prev == 0) {
end_prev = end;
}
avg = avg * 0.95 + (end - rdata_end) * 0.05;
avg_fps = avg_fps * 0.95 + (end - end_prev) * 0.05;
avg_rdata = avg_rdata * 0.95 + (rdata_end - rdata_start) * 0.05;
printf(
"rdata %.0fms iter %.0fms (frame %.0fms)\n", avg_rdata * 1000, avg * 1000, avg_fps * 1000);
end_prev = end;
#endif
}
} // namespace blender::draw
extern "C" {
void mesh_buffer_cache_create_requested(struct TaskGraph *task_graph,
MeshBatchCache *cache,
MeshBufferCache *mbc,
MeshBufferExtractionCache *extraction_cache,
Mesh *me,
const bool is_editmode,
const bool is_paint_mode,
const bool is_mode_active,
const float obmat[4][4],
const bool do_final,
const bool do_uvedit,
const bool use_subsurf_fdots,
const DRW_MeshCDMask *cd_layer_used,
const Scene *scene,
const ToolSettings *ts,
const bool use_hide)
{
blender::draw::mesh_buffer_cache_create_requested(task_graph,
cache,
mbc,
extraction_cache,
me,
is_editmode,
is_paint_mode,
is_mode_active,
obmat,
do_final,
do_uvedit,
use_subsurf_fdots,
cd_layer_used,
scene,
ts,
use_hide);
}
} // extern "C"
/** \} */
|