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
|
/*
* 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.
*/
/** \file
* \ingroup fn
*
* The `MFNetworkEvaluator` class is a multi-function that consists of potentially many smaller
* multi-functions. When called, it traverses the underlying MFNetwork and executes the required
* function nodes.
*
* There are many possible approaches to evaluate a function network. The approach implemented
* below has the following features:
* - It does not use recursion. Those could become problematic with long node chains.
* - It can handle all existing parameter types (including mutable parameters).
* - Avoids data copies in many cases.
* - Every node is executed at most once.
* - Can compute sub-functions on a single element, when the result is the same for all elements.
*
* Possible improvements:
* - Cache and reuse buffers.
* - Use "deepest depth first" heuristic to decide which order the inputs of a node should be
* computed. This reduces the number of required temporary buffers when they are reused.
*/
#include "FN_multi_function_network_evaluation.hh"
#include "BLI_stack.hh"
namespace blender::fn {
struct Value;
/**
* This keeps track of all the values that flow through the multi-function network. Therefore it
* maintains a mapping between output sockets and their corresponding values. Every `value`
* references some memory, that is owned either by the caller or this storage.
*
* A value can be owned by different sockets over time to avoid unnecessary copies.
*/
class MFNetworkEvaluationStorage {
private:
LinearAllocator<> allocator_;
IndexMask mask_;
Array<Value *> value_per_output_id_;
uint min_array_size_;
public:
MFNetworkEvaluationStorage(IndexMask mask, uint socket_id_amount);
~MFNetworkEvaluationStorage();
/* Add the values that have been provided by the caller of the multi-function network. */
void add_single_input_from_caller(const MFOutputSocket &socket, GVSpan virtual_span);
void add_vector_input_from_caller(const MFOutputSocket &socket, GVArraySpan virtual_array_span);
void add_single_output_from_caller(const MFOutputSocket &socket, GMutableSpan span);
void add_vector_output_from_caller(const MFOutputSocket &socket, GVectorArray &vector_array);
/* Get input buffers for function node evaluations. */
GVSpan get_single_input__full(const MFInputSocket &socket);
GVSpan get_single_input__single(const MFInputSocket &socket);
GVArraySpan get_vector_input__full(const MFInputSocket &socket);
GVArraySpan get_vector_input__single(const MFInputSocket &socket);
/* Get output buffers for function node evaluations. */
GMutableSpan get_single_output__full(const MFOutputSocket &socket);
GMutableSpan get_single_output__single(const MFOutputSocket &socket);
GVectorArray &get_vector_output__full(const MFOutputSocket &socket);
GVectorArray &get_vector_output__single(const MFOutputSocket &socket);
/* Get mutable buffers for function node evaluations. */
GMutableSpan get_mutable_single__full(const MFInputSocket &input, const MFOutputSocket &output);
GMutableSpan get_mutable_single__single(const MFInputSocket &input,
const MFOutputSocket &output);
GVectorArray &get_mutable_vector__full(const MFInputSocket &input, const MFOutputSocket &output);
GVectorArray &get_mutable_vector__single(const MFInputSocket &input,
const MFOutputSocket &output);
/* Mark a node as being done with evaluation. This might free temporary buffers that are no
* longer needed. */
void finish_node(const MFFunctionNode &node);
void finish_output_socket(const MFOutputSocket &socket);
void finish_input_socket(const MFInputSocket &socket);
IndexMask mask() const;
bool socket_is_computed(const MFOutputSocket &socket);
bool is_same_value_for_every_index(const MFOutputSocket &socket);
bool socket_has_buffer_for_output(const MFOutputSocket &socket);
};
MFNetworkEvaluator::MFNetworkEvaluator(Vector<const MFOutputSocket *> inputs,
Vector<const MFInputSocket *> outputs)
: inputs_(std::move(inputs)), outputs_(std::move(outputs))
{
BLI_assert(outputs_.size() > 0);
MFSignatureBuilder signature = this->get_builder("Function Tree");
for (auto socket : inputs_) {
BLI_assert(socket->node().is_dummy());
MFDataType type = socket->data_type();
switch (type.category()) {
case MFDataType::Single:
signature.single_input("Input", type.single_type());
break;
case MFDataType::Vector:
signature.vector_input("Input", type.vector_base_type());
break;
}
}
for (auto socket : outputs_) {
BLI_assert(socket->node().is_dummy());
MFDataType type = socket->data_type();
switch (type.category()) {
case MFDataType::Single:
signature.single_output("Output", type.single_type());
break;
case MFDataType::Vector:
signature.vector_output("Output", type.vector_base_type());
break;
}
}
}
void MFNetworkEvaluator::call(IndexMask mask, MFParams params, MFContext context) const
{
if (mask.size() == 0) {
return;
}
const MFNetwork &network = outputs_[0]->node().network();
Storage storage(mask, network.socket_id_amount());
Vector<const MFInputSocket *> outputs_to_initialize_in_the_end;
this->copy_inputs_to_storage(params, storage);
this->copy_outputs_to_storage(params, storage, outputs_to_initialize_in_the_end);
this->evaluate_network_to_compute_outputs(context, storage);
this->initialize_remaining_outputs(params, storage, outputs_to_initialize_in_the_end);
}
BLI_NOINLINE void MFNetworkEvaluator::copy_inputs_to_storage(MFParams params,
Storage &storage) const
{
for (uint input_index : inputs_.index_range()) {
uint param_index = input_index + 0;
const MFOutputSocket &socket = *inputs_[input_index];
switch (socket.data_type().category()) {
case MFDataType::Single: {
GVSpan input_list = params.readonly_single_input(param_index);
storage.add_single_input_from_caller(socket, input_list);
break;
}
case MFDataType::Vector: {
GVArraySpan input_list_list = params.readonly_vector_input(param_index);
storage.add_vector_input_from_caller(socket, input_list_list);
break;
}
}
}
}
BLI_NOINLINE void MFNetworkEvaluator::copy_outputs_to_storage(
MFParams params,
Storage &storage,
Vector<const MFInputSocket *> &outputs_to_initialize_in_the_end) const
{
for (uint output_index : outputs_.index_range()) {
uint param_index = output_index + inputs_.size();
const MFInputSocket &socket = *outputs_[output_index];
const MFOutputSocket &origin = *socket.origin();
if (origin.node().is_dummy()) {
BLI_assert(inputs_.contains(&origin));
/* Don't overwrite input buffers. */
outputs_to_initialize_in_the_end.append(&socket);
continue;
}
if (storage.socket_has_buffer_for_output(origin)) {
/* When two outputs will be initialized to the same values. */
outputs_to_initialize_in_the_end.append(&socket);
continue;
}
switch (socket.data_type().category()) {
case MFDataType::Single: {
GMutableSpan span = params.uninitialized_single_output(param_index);
storage.add_single_output_from_caller(origin, span);
break;
}
case MFDataType::Vector: {
GVectorArray &vector_array = params.vector_output(param_index);
storage.add_vector_output_from_caller(origin, vector_array);
break;
}
}
}
}
BLI_NOINLINE void MFNetworkEvaluator::evaluate_network_to_compute_outputs(
MFContext &global_context, Storage &storage) const
{
Stack<const MFOutputSocket *, 32> sockets_to_compute;
for (const MFInputSocket *socket : outputs_) {
sockets_to_compute.push(socket->origin());
}
/* This is the main loop that traverses the MFNetwork. */
while (!sockets_to_compute.is_empty()) {
const MFOutputSocket &socket = *sockets_to_compute.peek();
const MFNode &node = socket.node();
if (storage.socket_is_computed(socket)) {
sockets_to_compute.pop();
continue;
}
BLI_assert(node.is_function());
BLI_assert(node.all_inputs_have_origin());
const MFFunctionNode &function_node = node.as_function();
bool all_origins_are_computed = true;
for (const MFInputSocket *input_socket : function_node.inputs()) {
const MFOutputSocket *origin = input_socket->origin();
if (origin != nullptr) {
if (!storage.socket_is_computed(*origin)) {
sockets_to_compute.push(origin);
all_origins_are_computed = false;
}
}
}
if (all_origins_are_computed) {
this->evaluate_function(global_context, function_node, storage);
sockets_to_compute.pop();
}
}
}
BLI_NOINLINE void MFNetworkEvaluator::evaluate_function(MFContext &global_context,
const MFFunctionNode &function_node,
Storage &storage) const
{
const MultiFunction &function = function_node.function();
// std::cout << "Function: " << function.name() << "\n";
if (this->can_do_single_value_evaluation(function_node, storage)) {
/* The function output would be the same for all elements. Therefore, it is enough to call the
* function only on a single element. This can avoid many duplicate computations. */
MFParamsBuilder params{function, 1};
for (uint param_index : function.param_indices()) {
MFParamType param_type = function.param_type(param_index);
switch (param_type.category()) {
case MFParamType::SingleInput: {
const MFInputSocket &socket = function_node.input_for_param(param_index);
GVSpan values = storage.get_single_input__single(socket);
params.add_readonly_single_input(values);
break;
}
case MFParamType::VectorInput: {
const MFInputSocket &socket = function_node.input_for_param(param_index);
GVArraySpan values = storage.get_vector_input__single(socket);
params.add_readonly_vector_input(values);
break;
}
case MFParamType::SingleOutput: {
const MFOutputSocket &socket = function_node.output_for_param(param_index);
GMutableSpan values = storage.get_single_output__single(socket);
params.add_uninitialized_single_output(values);
break;
}
case MFParamType::VectorOutput: {
const MFOutputSocket &socket = function_node.output_for_param(param_index);
GVectorArray &values = storage.get_vector_output__single(socket);
params.add_vector_output(values);
break;
}
case MFParamType::SingleMutable: {
const MFInputSocket &input = function_node.input_for_param(param_index);
const MFOutputSocket &output = function_node.output_for_param(param_index);
GMutableSpan values = storage.get_mutable_single__single(input, output);
params.add_single_mutable(values);
break;
}
case MFParamType::VectorMutable: {
const MFInputSocket &input = function_node.input_for_param(param_index);
const MFOutputSocket &output = function_node.output_for_param(param_index);
GVectorArray &values = storage.get_mutable_vector__single(input, output);
params.add_vector_mutable(values);
break;
}
}
}
function.call(IndexRange(1), params, global_context);
}
else {
MFParamsBuilder params{function, storage.mask().min_array_size()};
for (uint param_index : function.param_indices()) {
MFParamType param_type = function.param_type(param_index);
switch (param_type.category()) {
case MFParamType::SingleInput: {
const MFInputSocket &socket = function_node.input_for_param(param_index);
GVSpan values = storage.get_single_input__full(socket);
params.add_readonly_single_input(values);
break;
}
case MFParamType::VectorInput: {
const MFInputSocket &socket = function_node.input_for_param(param_index);
GVArraySpan values = storage.get_vector_input__full(socket);
params.add_readonly_vector_input(values);
break;
}
case MFParamType::SingleOutput: {
const MFOutputSocket &socket = function_node.output_for_param(param_index);
GMutableSpan values = storage.get_single_output__full(socket);
params.add_uninitialized_single_output(values);
break;
}
case MFParamType::VectorOutput: {
const MFOutputSocket &socket = function_node.output_for_param(param_index);
GVectorArray &values = storage.get_vector_output__full(socket);
params.add_vector_output(values);
break;
}
case MFParamType::SingleMutable: {
const MFInputSocket &input = function_node.input_for_param(param_index);
const MFOutputSocket &output = function_node.output_for_param(param_index);
GMutableSpan values = storage.get_mutable_single__full(input, output);
params.add_single_mutable(values);
break;
}
case MFParamType::VectorMutable: {
const MFInputSocket &input = function_node.input_for_param(param_index);
const MFOutputSocket &output = function_node.output_for_param(param_index);
GVectorArray &values = storage.get_mutable_vector__full(input, output);
params.add_vector_mutable(values);
break;
}
}
}
function.call(storage.mask(), params, global_context);
}
storage.finish_node(function_node);
}
bool MFNetworkEvaluator::can_do_single_value_evaluation(const MFFunctionNode &function_node,
Storage &storage) const
{
for (const MFInputSocket *socket : function_node.inputs()) {
if (!storage.is_same_value_for_every_index(*socket->origin())) {
return false;
}
}
if (storage.mask().min_array_size() >= 1) {
for (const MFOutputSocket *socket : function_node.outputs()) {
if (storage.socket_has_buffer_for_output(*socket)) {
return false;
}
}
}
return true;
}
BLI_NOINLINE void MFNetworkEvaluator::initialize_remaining_outputs(
MFParams params, Storage &storage, Span<const MFInputSocket *> remaining_outputs) const
{
for (const MFInputSocket *socket : remaining_outputs) {
uint param_index = inputs_.size() + outputs_.first_index_of(socket);
switch (socket->data_type().category()) {
case MFDataType::Single: {
GVSpan values = storage.get_single_input__full(*socket);
GMutableSpan output_values = params.uninitialized_single_output(param_index);
values.materialize_to_uninitialized(storage.mask(), output_values.buffer());
break;
}
case MFDataType::Vector: {
GVArraySpan values = storage.get_vector_input__full(*socket);
GVectorArray &output_values = params.vector_output(param_index);
output_values.extend(storage.mask(), values);
break;
}
}
}
}
/* -------------------------------------------------------------------- */
/** \name Value Types
* \{ */
enum class ValueType {
InputSingle,
InputVector,
OutputSingle,
OutputVector,
OwnSingle,
OwnVector,
};
struct Value {
ValueType type;
Value(ValueType type) : type(type)
{
}
};
struct InputSingleValue : public Value {
/** This span has been provided by the code that called the multi-function network. */
GVSpan virtual_span;
InputSingleValue(GVSpan virtual_span) : Value(ValueType::InputSingle), virtual_span(virtual_span)
{
}
};
struct InputVectorValue : public Value {
/** This span has been provided by the code that called the multi-function network. */
GVArraySpan virtual_array_span;
InputVectorValue(GVArraySpan virtual_array_span)
: Value(ValueType::InputVector), virtual_array_span(virtual_array_span)
{
}
};
struct OutputValue : public Value {
bool is_computed = false;
OutputValue(ValueType type) : Value(type)
{
}
};
struct OutputSingleValue : public OutputValue {
/** This span has been provided by the code that called the multi-function network. */
GMutableSpan span;
OutputSingleValue(GMutableSpan span) : OutputValue(ValueType::OutputSingle), span(span)
{
}
};
struct OutputVectorValue : public OutputValue {
/** This vector array has been provided by the code that called the multi-function network. */
GVectorArray *vector_array;
OutputVectorValue(GVectorArray &vector_array)
: OutputValue(ValueType::OutputVector), vector_array(&vector_array)
{
}
};
struct OwnSingleValue : public Value {
/** This span has been allocated during the evaluation of the multi-function network and contains
* intermediate data. It has to be freed once the network evaluation is finished. */
GMutableSpan span;
int max_remaining_users;
bool is_single_allocated;
OwnSingleValue(GMutableSpan span, int max_remaining_users, bool is_single_allocated)
: Value(ValueType::OwnSingle),
span(span),
max_remaining_users(max_remaining_users),
is_single_allocated(is_single_allocated)
{
}
};
struct OwnVectorValue : public Value {
/** This vector array has been allocated during the evaluation of the multi-function network and
* contains intermediate data. It has to be freed once the network evaluation is finished. */
GVectorArray *vector_array;
int max_remaining_users;
OwnVectorValue(GVectorArray &vector_array, int max_remaining_users)
: Value(ValueType::OwnVector),
vector_array(&vector_array),
max_remaining_users(max_remaining_users)
{
}
};
/** \} */
/* -------------------------------------------------------------------- */
/** \name Storage methods
* \{ */
MFNetworkEvaluationStorage::MFNetworkEvaluationStorage(IndexMask mask, uint socket_id_amount)
: mask_(mask),
value_per_output_id_(socket_id_amount, nullptr),
min_array_size_(mask.min_array_size())
{
}
MFNetworkEvaluationStorage::~MFNetworkEvaluationStorage()
{
for (Value *any_value : value_per_output_id_) {
if (any_value == nullptr) {
continue;
}
else if (any_value->type == ValueType::OwnSingle) {
OwnSingleValue *value = (OwnSingleValue *)any_value;
GMutableSpan span = value->span;
const CPPType &type = span.type();
if (value->is_single_allocated) {
type.destruct(span.buffer());
}
else {
type.destruct_indices(span.buffer(), mask_);
MEM_freeN(span.buffer());
}
}
else if (any_value->type == ValueType::OwnVector) {
OwnVectorValue *value = (OwnVectorValue *)any_value;
delete value->vector_array;
}
}
}
IndexMask MFNetworkEvaluationStorage::mask() const
{
return mask_;
}
bool MFNetworkEvaluationStorage::socket_is_computed(const MFOutputSocket &socket)
{
Value *any_value = value_per_output_id_[socket.id()];
if (any_value == nullptr) {
return false;
}
if (ELEM(any_value->type, ValueType::OutputSingle, ValueType::OutputVector)) {
return ((OutputValue *)any_value)->is_computed;
}
return true;
}
bool MFNetworkEvaluationStorage::is_same_value_for_every_index(const MFOutputSocket &socket)
{
Value *any_value = value_per_output_id_[socket.id()];
switch (any_value->type) {
case ValueType::OwnSingle:
return ((OwnSingleValue *)any_value)->span.size() == 1;
case ValueType::OwnVector:
return ((OwnVectorValue *)any_value)->vector_array->size() == 1;
case ValueType::InputSingle:
return ((InputSingleValue *)any_value)->virtual_span.is_single_element();
case ValueType::InputVector:
return ((InputVectorValue *)any_value)->virtual_array_span.is_single_array();
case ValueType::OutputSingle:
return ((OutputSingleValue *)any_value)->span.size() == 1;
case ValueType::OutputVector:
return ((OutputVectorValue *)any_value)->vector_array->size() == 1;
}
BLI_assert(false);
return false;
}
bool MFNetworkEvaluationStorage::socket_has_buffer_for_output(const MFOutputSocket &socket)
{
Value *any_value = value_per_output_id_[socket.id()];
if (any_value == nullptr) {
return false;
}
BLI_assert(ELEM(any_value->type, ValueType::OutputSingle, ValueType::OutputVector));
return true;
}
void MFNetworkEvaluationStorage::finish_node(const MFFunctionNode &node)
{
for (const MFInputSocket *socket : node.inputs()) {
this->finish_input_socket(*socket);
}
for (const MFOutputSocket *socket : node.outputs()) {
this->finish_output_socket(*socket);
}
}
void MFNetworkEvaluationStorage::finish_output_socket(const MFOutputSocket &socket)
{
Value *any_value = value_per_output_id_[socket.id()];
if (any_value == nullptr) {
return;
}
if (ELEM(any_value->type, ValueType::OutputSingle, ValueType::OutputVector)) {
((OutputValue *)any_value)->is_computed = true;
}
}
void MFNetworkEvaluationStorage::finish_input_socket(const MFInputSocket &socket)
{
const MFOutputSocket &origin = *socket.origin();
Value *any_value = value_per_output_id_[origin.id()];
if (any_value == nullptr) {
/* Can happen when a value has been forward to the next node. */
return;
}
switch (any_value->type) {
case ValueType::InputSingle:
case ValueType::OutputSingle:
case ValueType::InputVector:
case ValueType::OutputVector: {
break;
}
case ValueType::OwnSingle: {
OwnSingleValue *value = (OwnSingleValue *)any_value;
BLI_assert(value->max_remaining_users >= 1);
value->max_remaining_users--;
if (value->max_remaining_users == 0) {
GMutableSpan span = value->span;
const CPPType &type = span.type();
if (value->is_single_allocated) {
type.destruct(span.buffer());
}
else {
type.destruct_indices(span.buffer(), mask_);
MEM_freeN(span.buffer());
}
value_per_output_id_[origin.id()] = nullptr;
}
break;
}
case ValueType::OwnVector: {
OwnVectorValue *value = (OwnVectorValue *)any_value;
BLI_assert(value->max_remaining_users >= 1);
value->max_remaining_users--;
if (value->max_remaining_users == 0) {
delete value->vector_array;
value_per_output_id_[origin.id()] = nullptr;
}
break;
}
}
}
void MFNetworkEvaluationStorage::add_single_input_from_caller(const MFOutputSocket &socket,
GVSpan virtual_span)
{
BLI_assert(value_per_output_id_[socket.id()] == nullptr);
BLI_assert(virtual_span.size() >= min_array_size_);
auto *value = allocator_.construct<InputSingleValue>(virtual_span);
value_per_output_id_[socket.id()] = value;
}
void MFNetworkEvaluationStorage::add_vector_input_from_caller(const MFOutputSocket &socket,
GVArraySpan virtual_array_span)
{
BLI_assert(value_per_output_id_[socket.id()] == nullptr);
BLI_assert(virtual_array_span.size() >= min_array_size_);
auto *value = allocator_.construct<InputVectorValue>(virtual_array_span);
value_per_output_id_[socket.id()] = value;
}
void MFNetworkEvaluationStorage::add_single_output_from_caller(const MFOutputSocket &socket,
GMutableSpan span)
{
BLI_assert(value_per_output_id_[socket.id()] == nullptr);
BLI_assert(span.size() >= min_array_size_);
auto *value = allocator_.construct<OutputSingleValue>(span);
value_per_output_id_[socket.id()] = value;
}
void MFNetworkEvaluationStorage::add_vector_output_from_caller(const MFOutputSocket &socket,
GVectorArray &vector_array)
{
BLI_assert(value_per_output_id_[socket.id()] == nullptr);
BLI_assert(vector_array.size() >= min_array_size_);
auto *value = allocator_.construct<OutputVectorValue>(vector_array);
value_per_output_id_[socket.id()] = value;
}
GMutableSpan MFNetworkEvaluationStorage::get_single_output__full(const MFOutputSocket &socket)
{
Value *any_value = value_per_output_id_[socket.id()];
if (any_value == nullptr) {
const CPPType &type = socket.data_type().single_type();
void *buffer = MEM_mallocN_aligned(min_array_size_ * type.size(), type.alignment(), AT);
GMutableSpan span(type, buffer, min_array_size_);
auto *value = allocator_.construct<OwnSingleValue>(span, socket.targets().size(), false);
value_per_output_id_[socket.id()] = value;
return span;
}
else {
BLI_assert(any_value->type == ValueType::OutputSingle);
return ((OutputSingleValue *)any_value)->span;
}
}
GMutableSpan MFNetworkEvaluationStorage::get_single_output__single(const MFOutputSocket &socket)
{
Value *any_value = value_per_output_id_[socket.id()];
if (any_value == nullptr) {
const CPPType &type = socket.data_type().single_type();
void *buffer = allocator_.allocate(type.size(), type.alignment());
GMutableSpan span(type, buffer, 1);
auto *value = allocator_.construct<OwnSingleValue>(span, socket.targets().size(), true);
value_per_output_id_[socket.id()] = value;
return value->span;
}
else {
BLI_assert(any_value->type == ValueType::OutputSingle);
GMutableSpan span = ((OutputSingleValue *)any_value)->span;
BLI_assert(span.size() == 1);
return span;
}
}
GVectorArray &MFNetworkEvaluationStorage::get_vector_output__full(const MFOutputSocket &socket)
{
Value *any_value = value_per_output_id_[socket.id()];
if (any_value == nullptr) {
const CPPType &type = socket.data_type().vector_base_type();
GVectorArray *vector_array = new GVectorArray(type, min_array_size_);
auto *value = allocator_.construct<OwnVectorValue>(*vector_array, socket.targets().size());
value_per_output_id_[socket.id()] = value;
return *value->vector_array;
}
else {
BLI_assert(any_value->type == ValueType::OutputVector);
return *((OutputVectorValue *)any_value)->vector_array;
}
}
GVectorArray &MFNetworkEvaluationStorage::get_vector_output__single(const MFOutputSocket &socket)
{
Value *any_value = value_per_output_id_[socket.id()];
if (any_value == nullptr) {
const CPPType &type = socket.data_type().vector_base_type();
GVectorArray *vector_array = new GVectorArray(type, 1);
auto *value = allocator_.construct<OwnVectorValue>(*vector_array, socket.targets().size());
value_per_output_id_[socket.id()] = value;
return *value->vector_array;
}
else {
BLI_assert(any_value->type == ValueType::OutputVector);
GVectorArray &vector_array = *((OutputVectorValue *)any_value)->vector_array;
BLI_assert(vector_array.size() == 1);
return vector_array;
}
}
GMutableSpan MFNetworkEvaluationStorage::get_mutable_single__full(const MFInputSocket &input,
const MFOutputSocket &output)
{
const MFOutputSocket &from = *input.origin();
const MFOutputSocket &to = output;
const CPPType &type = from.data_type().single_type();
Value *from_any_value = value_per_output_id_[from.id()];
Value *to_any_value = value_per_output_id_[to.id()];
BLI_assert(from_any_value != nullptr);
BLI_assert(type == to.data_type().single_type());
if (to_any_value != nullptr) {
BLI_assert(to_any_value->type == ValueType::OutputSingle);
GMutableSpan span = ((OutputSingleValue *)to_any_value)->span;
GVSpan virtual_span = this->get_single_input__full(input);
virtual_span.materialize_to_uninitialized(mask_, span.buffer());
return span;
}
if (from_any_value->type == ValueType::OwnSingle) {
OwnSingleValue *value = (OwnSingleValue *)from_any_value;
if (value->max_remaining_users == 1 && !value->is_single_allocated) {
value_per_output_id_[to.id()] = value;
value_per_output_id_[from.id()] = nullptr;
value->max_remaining_users = to.targets().size();
return value->span;
}
}
GVSpan virtual_span = this->get_single_input__full(input);
void *new_buffer = MEM_mallocN_aligned(min_array_size_ * type.size(), type.alignment(), AT);
GMutableSpan new_array_ref(type, new_buffer, min_array_size_);
virtual_span.materialize_to_uninitialized(mask_, new_array_ref.buffer());
OwnSingleValue *new_value = allocator_.construct<OwnSingleValue>(
new_array_ref, to.targets().size(), false);
value_per_output_id_[to.id()] = new_value;
return new_array_ref;
}
GMutableSpan MFNetworkEvaluationStorage::get_mutable_single__single(const MFInputSocket &input,
const MFOutputSocket &output)
{
const MFOutputSocket &from = *input.origin();
const MFOutputSocket &to = output;
const CPPType &type = from.data_type().single_type();
Value *from_any_value = value_per_output_id_[from.id()];
Value *to_any_value = value_per_output_id_[to.id()];
BLI_assert(from_any_value != nullptr);
BLI_assert(type == to.data_type().single_type());
if (to_any_value != nullptr) {
BLI_assert(to_any_value->type == ValueType::OutputSingle);
GMutableSpan span = ((OutputSingleValue *)to_any_value)->span;
BLI_assert(span.size() == 1);
GVSpan virtual_span = this->get_single_input__single(input);
type.copy_to_uninitialized(virtual_span.as_single_element(), span[0]);
return span;
}
if (from_any_value->type == ValueType::OwnSingle) {
OwnSingleValue *value = (OwnSingleValue *)from_any_value;
if (value->max_remaining_users == 1) {
value_per_output_id_[to.id()] = value;
value_per_output_id_[from.id()] = nullptr;
value->max_remaining_users = to.targets().size();
BLI_assert(value->span.size() == 1);
return value->span;
}
}
GVSpan virtual_span = this->get_single_input__single(input);
void *new_buffer = allocator_.allocate(type.size(), type.alignment());
type.copy_to_uninitialized(virtual_span.as_single_element(), new_buffer);
GMutableSpan new_array_ref(type, new_buffer, 1);
OwnSingleValue *new_value = allocator_.construct<OwnSingleValue>(
new_array_ref, to.targets().size(), true);
value_per_output_id_[to.id()] = new_value;
return new_array_ref;
}
GVectorArray &MFNetworkEvaluationStorage::get_mutable_vector__full(const MFInputSocket &input,
const MFOutputSocket &output)
{
const MFOutputSocket &from = *input.origin();
const MFOutputSocket &to = output;
const CPPType &base_type = from.data_type().vector_base_type();
Value *from_any_value = value_per_output_id_[from.id()];
Value *to_any_value = value_per_output_id_[to.id()];
BLI_assert(from_any_value != nullptr);
BLI_assert(base_type == to.data_type().vector_base_type());
if (to_any_value != nullptr) {
BLI_assert(to_any_value->type == ValueType::OutputVector);
GVectorArray &vector_array = *((OutputVectorValue *)to_any_value)->vector_array;
GVArraySpan virtual_array_span = this->get_vector_input__full(input);
vector_array.extend(mask_, virtual_array_span);
return vector_array;
}
if (from_any_value->type == ValueType::OwnVector) {
OwnVectorValue *value = (OwnVectorValue *)from_any_value;
if (value->max_remaining_users == 1) {
value_per_output_id_[to.id()] = value;
value_per_output_id_[from.id()] = nullptr;
value->max_remaining_users = to.targets().size();
return *value->vector_array;
}
}
GVArraySpan virtual_array_span = this->get_vector_input__full(input);
GVectorArray *new_vector_array = new GVectorArray(base_type, min_array_size_);
new_vector_array->extend(mask_, virtual_array_span);
OwnVectorValue *new_value = allocator_.construct<OwnVectorValue>(*new_vector_array,
to.targets().size());
value_per_output_id_[to.id()] = new_value;
return *new_vector_array;
}
GVectorArray &MFNetworkEvaluationStorage::get_mutable_vector__single(const MFInputSocket &input,
const MFOutputSocket &output)
{
const MFOutputSocket &from = *input.origin();
const MFOutputSocket &to = output;
const CPPType &base_type = from.data_type().vector_base_type();
Value *from_any_value = value_per_output_id_[from.id()];
Value *to_any_value = value_per_output_id_[to.id()];
BLI_assert(from_any_value != nullptr);
BLI_assert(base_type == to.data_type().vector_base_type());
if (to_any_value != nullptr) {
BLI_assert(to_any_value->type == ValueType::OutputVector);
GVectorArray &vector_array = *((OutputVectorValue *)to_any_value)->vector_array;
BLI_assert(vector_array.size() == 1);
GVArraySpan virtual_array_span = this->get_vector_input__single(input);
vector_array.extend(0, virtual_array_span[0]);
return vector_array;
}
if (from_any_value->type == ValueType::OwnVector) {
OwnVectorValue *value = (OwnVectorValue *)from_any_value;
if (value->max_remaining_users == 1) {
value_per_output_id_[to.id()] = value;
value_per_output_id_[from.id()] = nullptr;
value->max_remaining_users = to.targets().size();
return *value->vector_array;
}
}
GVArraySpan virtual_array_span = this->get_vector_input__single(input);
GVectorArray *new_vector_array = new GVectorArray(base_type, 1);
new_vector_array->extend(0, virtual_array_span[0]);
OwnVectorValue *new_value = allocator_.construct<OwnVectorValue>(*new_vector_array,
to.targets().size());
value_per_output_id_[to.id()] = new_value;
return *new_vector_array;
}
GVSpan MFNetworkEvaluationStorage::get_single_input__full(const MFInputSocket &socket)
{
const MFOutputSocket &origin = *socket.origin();
Value *any_value = value_per_output_id_[origin.id()];
BLI_assert(any_value != nullptr);
if (any_value->type == ValueType::OwnSingle) {
OwnSingleValue *value = (OwnSingleValue *)any_value;
if (value->is_single_allocated) {
return GVSpan::FromSingle(value->span.type(), value->span.buffer(), min_array_size_);
}
else {
return value->span;
}
}
else if (any_value->type == ValueType::InputSingle) {
InputSingleValue *value = (InputSingleValue *)any_value;
return value->virtual_span;
}
else if (any_value->type == ValueType::OutputSingle) {
OutputSingleValue *value = (OutputSingleValue *)any_value;
BLI_assert(value->is_computed);
return value->span;
}
BLI_assert(false);
return GVSpan(CPPType::get<float>());
}
GVSpan MFNetworkEvaluationStorage::get_single_input__single(const MFInputSocket &socket)
{
const MFOutputSocket &origin = *socket.origin();
Value *any_value = value_per_output_id_[origin.id()];
BLI_assert(any_value != nullptr);
if (any_value->type == ValueType::OwnSingle) {
OwnSingleValue *value = (OwnSingleValue *)any_value;
BLI_assert(value->span.size() == 1);
return value->span;
}
else if (any_value->type == ValueType::InputSingle) {
InputSingleValue *value = (InputSingleValue *)any_value;
BLI_assert(value->virtual_span.is_single_element());
return value->virtual_span;
}
else if (any_value->type == ValueType::OutputSingle) {
OutputSingleValue *value = (OutputSingleValue *)any_value;
BLI_assert(value->is_computed);
BLI_assert(value->span.size() == 1);
return value->span;
}
BLI_assert(false);
return GVSpan(CPPType::get<float>());
}
GVArraySpan MFNetworkEvaluationStorage::get_vector_input__full(const MFInputSocket &socket)
{
const MFOutputSocket &origin = *socket.origin();
Value *any_value = value_per_output_id_[origin.id()];
BLI_assert(any_value != nullptr);
if (any_value->type == ValueType::OwnVector) {
OwnVectorValue *value = (OwnVectorValue *)any_value;
if (value->vector_array->size() == 1) {
GSpan span = (*value->vector_array)[0];
return GVArraySpan(span, min_array_size_);
}
else {
return *value->vector_array;
}
}
else if (any_value->type == ValueType::InputVector) {
InputVectorValue *value = (InputVectorValue *)any_value;
return value->virtual_array_span;
}
else if (any_value->type == ValueType::OutputVector) {
OutputVectorValue *value = (OutputVectorValue *)any_value;
return *value->vector_array;
}
BLI_assert(false);
return GVArraySpan(CPPType::get<float>());
}
GVArraySpan MFNetworkEvaluationStorage::get_vector_input__single(const MFInputSocket &socket)
{
const MFOutputSocket &origin = *socket.origin();
Value *any_value = value_per_output_id_[origin.id()];
BLI_assert(any_value != nullptr);
if (any_value->type == ValueType::OwnVector) {
OwnVectorValue *value = (OwnVectorValue *)any_value;
BLI_assert(value->vector_array->size() == 1);
return *value->vector_array;
}
else if (any_value->type == ValueType::InputVector) {
InputVectorValue *value = (InputVectorValue *)any_value;
BLI_assert(value->virtual_array_span.is_single_array());
return value->virtual_array_span;
}
else if (any_value->type == ValueType::OutputVector) {
OutputVectorValue *value = (OutputVectorValue *)any_value;
BLI_assert(value->vector_array->size() == 1);
return *value->vector_array;
}
BLI_assert(false);
return GVArraySpan(CPPType::get<float>());
}
/** \} */
} // namespace blender::fn
|