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

multi_function_builder.cc « intern « functions « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 020d6ba01397cefc89818136aae65a97d8ad3b2e (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
/* SPDX-License-Identifier: GPL-2.0-or-later */

#include "FN_multi_function_builder.hh"

#include "BLI_hash.hh"

namespace blender::fn {

CustomMF_GenericConstant::CustomMF_GenericConstant(const CPPType &type,
                                                   const void *value,
                                                   bool make_value_copy)
    : type_(type), owns_value_(make_value_copy)
{
  if (make_value_copy) {
    void *copied_value = MEM_mallocN_aligned(type.size(), type.alignment(), __func__);
    type.copy_construct(value, copied_value);
    value = copied_value;
  }
  value_ = value;

  MFSignatureBuilder signature{"Constant"};
  signature.single_output("Value", type);
  signature_ = signature.build();
  this->set_signature(&signature_);
}

CustomMF_GenericConstant::~CustomMF_GenericConstant()
{
  if (owns_value_) {
    signature_.param_types[0].data_type().single_type().destruct((void *)value_);
    MEM_freeN((void *)value_);
  }
}

void CustomMF_GenericConstant::call(IndexMask mask,
                                    MFParams params,
                                    MFContext UNUSED(context)) const
{
  GMutableSpan output = params.uninitialized_single_output(0);
  type_.fill_construct_indices(value_, output.data(), mask);
}

uint64_t CustomMF_GenericConstant::hash() const
{
  return type_.hash_or_fallback(value_, (uintptr_t)this);
}

bool CustomMF_GenericConstant::equals(const MultiFunction &other) const
{
  const CustomMF_GenericConstant *_other = dynamic_cast<const CustomMF_GenericConstant *>(&other);
  if (_other == nullptr) {
    return false;
  }
  if (type_ != _other->type_) {
    return false;
  }
  return type_.is_equal(value_, _other->value_);
}

CustomMF_GenericConstantArray::CustomMF_GenericConstantArray(GSpan array) : array_(array)
{
  const CPPType &type = array.type();
  MFSignatureBuilder signature{"Constant Vector"};
  signature.vector_output("Value", type);
  signature_ = signature.build();
  this->set_signature(&signature_);
}

void CustomMF_GenericConstantArray::call(IndexMask mask,
                                         MFParams params,
                                         MFContext UNUSED(context)) const
{
  GVectorArray &vectors = params.vector_output(0);
  for (int64_t i : mask) {
    vectors.extend(i, array_);
  }
}

CustomMF_DefaultOutput::CustomMF_DefaultOutput(Span<MFDataType> input_types,
                                               Span<MFDataType> output_types)
    : output_amount_(output_types.size())
{
  MFSignatureBuilder signature{"Default Output"};
  for (MFDataType data_type : input_types) {
    signature.input("Input", data_type);
  }
  for (MFDataType data_type : output_types) {
    signature.output("Output", data_type);
  }
  signature_ = signature.build();
  this->set_signature(&signature_);
}
void CustomMF_DefaultOutput::call(IndexMask mask, MFParams params, MFContext UNUSED(context)) const
{
  for (int param_index : this->param_indices()) {
    MFParamType param_type = this->param_type(param_index);
    if (!param_type.is_output()) {
      continue;
    }

    if (param_type.data_type().is_single()) {
      GMutableSpan span = params.uninitialized_single_output(param_index);
      const CPPType &type = span.type();
      type.fill_construct_indices(type.default_value(), span.data(), mask);
    }
  }
}

CustomMF_GenericCopy::CustomMF_GenericCopy(MFDataType data_type)
{
  MFSignatureBuilder signature{"Copy"};
  signature.input("Input", data_type);
  signature.output("Output", data_type);
  signature_ = signature.build();
  this->set_signature(&signature_);
}

void CustomMF_GenericCopy::call(IndexMask mask, MFParams params, MFContext UNUSED(context)) const
{
  const MFDataType data_type = this->param_type(0).data_type();
  switch (data_type.category()) {
    case MFDataType::Single: {
      const GVArray &inputs = params.readonly_single_input(0, "Input");
      GMutableSpan outputs = params.uninitialized_single_output(1, "Output");
      inputs.materialize_to_uninitialized(mask, outputs.data());
      break;
    }
    case MFDataType::Vector: {
      const GVVectorArray &inputs = params.readonly_vector_input(0, "Input");
      GVectorArray &outputs = params.vector_output(1, "Output");
      outputs.extend(mask, inputs);
      break;
    }
  }
}

}  // namespace blender::fn