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

field.cc « intern « functions « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 87cfc60d502217dd47c4c87f20de5953bafc616c (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
/*
 * 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.
 */

#include "BLI_map.hh"
#include "BLI_set.hh"
#include "BLI_stack.hh"

#include "FN_field.hh"

namespace blender::fn {

/**
 * A map to hold the output variables for each function output or input so they can be reused.
 */
using VariableMap = Map<const FieldSource *, Vector<MFVariable *>>;

/**
 * A map of the computed inputs for all of a field system's inputs, to avoid creating duplicates.
 * Usually virtual arrays are just references, but sometimes they can be heavier as well.
 */
using ComputedInputMap = Map<const MFVariable *, GVArrayPtr>;

static MFVariable &get_field_variable(const GField &field, VariableMap &unique_variables)
{
  if (field.is_input()) {
    const FieldInput &input = dynamic_cast<const FieldInput &>(field.source());
    return *unique_variables.lookup(&input).first();
  }
  const FieldOperation &operation = dynamic_cast<const FieldOperation &>(field.source());
  MutableSpan<MFVariable *> operation_outputs = unique_variables.lookup(&operation);
  return *operation_outputs[field.source_output_index()];
}

static const MFVariable &get_field_variable(const GField &field,
                                            const VariableMap &unique_variables)
{
  if (field.is_input()) {
    const FieldInput &input = dynamic_cast<const FieldInput &>(field.source());
    return *unique_variables.lookup(&input).first();
  }
  const FieldOperation &operation = dynamic_cast<const FieldOperation &>(field.source());
  Span<MFVariable *> operation_outputs = unique_variables.lookup(&operation);
  return *operation_outputs[field.source_output_index()];
}

/**
 * TODO: Merge duplicate input nodes, not just fields pointing to the same FieldInput.
 */
static void add_variables_for_input(const GField &field,
                                    Stack<const GField *> &fields_to_visit,
                                    MFProcedureBuilder &builder,
                                    VariableMap &unique_variables)
{
  fields_to_visit.pop();
  const FieldInput &input = dynamic_cast<const FieldInput &>(field.source());
  MFVariable &variable = builder.add_input_parameter(MFDataType::ForSingle(field.cpp_type()),
                                                     input.debug_name());
  unique_variables.add(&input, {&variable});
}

static void add_variables_for_operation(const GField &field,
                                        Stack<const GField *> &fields_to_visit,
                                        MFProcedureBuilder &builder,
                                        VariableMap &unique_variables)
{
  const FieldOperation &operation = dynamic_cast<const FieldOperation &>(field.source());
  for (const GField &input_field : operation.inputs()) {
    if (!unique_variables.contains(&input_field.source())) {
      /* The field for this input hasn't been handled yet. Handle it now, so that we know all
       * of this field's function inputs already have variables. TODO: Verify that this is the
       * best way to do a depth first traversal. These extra lookups don't seem ideal. */
      fields_to_visit.push(&input_field);
      return;
    }
  }

  fields_to_visit.pop();

  Vector<MFVariable *> inputs;
  Set<MFVariable *> unique_inputs;
  for (const GField &input_field : operation.inputs()) {
    MFVariable &input = get_field_variable(input_field, unique_variables);
    unique_inputs.add(&input);
    inputs.append(&input);
  }

  Vector<MFVariable *> outputs = builder.add_call(operation.multi_function(), inputs);
  Vector<MFVariable *> &unique_outputs = unique_variables.lookup_or_add(&operation, {});
  for (MFVariable *output : outputs) {
    unique_outputs.append(output);
  }
}

static void add_unique_variables(const Span<GField> fields,
                                 MFProcedureBuilder &builder,
                                 VariableMap &unique_variables)
{
  Stack<const GField *> fields_to_visit;
  for (const GField &field : fields) {
    fields_to_visit.push(&field);
  }

  while (!fields_to_visit.is_empty()) {
    const GField &field = *fields_to_visit.peek();
    if (unique_variables.contains(&field.source())) {
      fields_to_visit.pop();
      continue;
    }

    if (field.is_input()) {
      add_variables_for_input(field, fields_to_visit, builder, unique_variables);
    }
    else {
      add_variables_for_operation(field, fields_to_visit, builder, unique_variables);
    }
  }
}

/**
 * Add destruct calls to the procedure so that internal variables and inputs are destructed before
 * the procedure finishes. Currently this just adds all of the destructs at the end. That is not
 * optimal, but properly ordering destructs should be combined with reordering function calls to
 * use variables more optimally.
 */
static void add_destructs(const Span<GField> fields,
                          MFProcedureBuilder &builder,
                          VariableMap &unique_variables)
{
  Set<MFVariable *> destructed_variables;
  Set<MFVariable *> outputs;
  for (const GField &field : fields) {
    /* Currently input fields are handled separately in the evaluator. */
    BLI_assert(!field.is_input());
    outputs.add(&get_field_variable(field, unique_variables));
  }

  for (MutableSpan<MFVariable *> variables : unique_variables.values()) {
    for (MFVariable *variable : variables) {
      /* Don't destruct the variable if it is used as an output parameter. */
      if (!outputs.contains(variable)) {
        builder.add_destruct(*variable);
      }
    }
  }
}

static void build_procedure(const Span<GField> fields,
                            MFProcedure &procedure,
                            VariableMap &unique_variables)
{
  MFProcedureBuilder builder{procedure};

  add_unique_variables(fields, builder, unique_variables);

  add_destructs(fields, builder, unique_variables);

  builder.add_return();

  for (const GField &field : fields) {
    MFVariable &input = get_field_variable(field, unique_variables);
    builder.add_output_parameter(input);
  }

  // std::cout << procedure.to_dot();

  BLI_assert(procedure.validate());
}

/**
 * TODO: Maybe this doesn't add inputs in the same order as the the unique
 * variable traversal. Add a test for that and fix it if it doesn't work.
 */
static void gather_inputs(const Span<GField> fields,
                          const VariableMap &unique_variables,
                          const IndexMask mask,
                          MFParamsBuilder &params,
                          Vector<GVArrayPtr> &r_inputs)
{
  Set<const MFVariable *> computed_inputs;
  Stack<const GField *> fields_to_visit;
  for (const GField &field : fields) {
    fields_to_visit.push(&field);
  }

  while (!fields_to_visit.is_empty()) {
    const GField &field = *fields_to_visit.pop();
    if (field.is_input()) {
      const FieldInput &input = dynamic_cast<const FieldInput &>(field.source());
      const MFVariable &variable = get_field_variable(field, unique_variables);
      if (!computed_inputs.contains(&variable)) {
        GVArrayPtr data = input.get_varray_generic_context(mask);
        computed_inputs.add_new(&variable);
        params.add_readonly_single_input(*data, input.debug_name());
        r_inputs.append(std::move(data));
      }
    }
    else {
      const FieldOperation &operation = dynamic_cast<const FieldOperation &>(field.source());
      for (const GField &input_field : operation.inputs()) {
        fields_to_visit.push(&input_field);
      }
    }
  }
}

static void add_outputs(MFParamsBuilder &params, Span<GMutableSpan> outputs)
{
  for (const int i : outputs.index_range()) {
    params.add_uninitialized_single_output(outputs[i]);
  }
}

static void evaluate_non_input_fields(const Span<GField> fields,
                                      const IndexMask mask,
                                      const Span<GMutableSpan> outputs)
{
  MFProcedure procedure;
  VariableMap unique_variables;
  build_procedure(fields, procedure, unique_variables);

  MFProcedureExecutor executor{"Evaluate Field", procedure};
  MFParamsBuilder params{executor, mask.min_array_size()};
  MFContextBuilder context;

  Vector<GVArrayPtr> inputs;
  gather_inputs(fields, unique_variables, mask, params, inputs);

  add_outputs(params, outputs);

  executor.call(mask, params, context);
}

/**
 * Evaluate more than one prodecure at a time, since often intermediate results will be shared
 * between multiple final results, and the procedure evaluator can optimize for this case.
 */
void evaluate_fields(const Span<GField> fields,
                     const IndexMask mask,
                     const Span<GMutableSpan> outputs)
{
  BLI_assert(fields.size() == outputs.size());

  /* Process fields that just connect to inputs separately, since otherwise we need a special
   * case to avoid sharing the same variable for an input and output parameters elsewhere.
   * TODO: It would be nice if there were a more elegant way to handle this, rather than a
   * separate step here, but I haven't thought of anything yet. */
  Vector<GField> non_input_fields{fields};
  Vector<GMutableSpan> non_input_outputs{outputs};
  for (int i = fields.size() - 1; i >= 0; i--) {
    if (non_input_fields[i].is_input()) {
      dynamic_cast<const FieldInput &>(non_input_fields[i].source())
          .get_varray_generic_context(mask)
          ->materialize(mask, outputs[i].data());

      non_input_fields.remove_and_reorder(i);
      non_input_outputs.remove_and_reorder(i);
    }
  }

  if (!non_input_fields.is_empty()) {
    evaluate_non_input_fields(non_input_fields, mask, non_input_outputs);
  }
}

/**
 * #r_value is expected to be uninitialized.
 */
void evaluate_constant_field(const GField &field, void *r_value)
{
  GMutableSpan value_span{field.cpp_type(), r_value, 1};
  evaluate_fields({field}, IndexRange(1), {value_span});
}

}  // namespace blender::fn