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

FN_field.hh « functions « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 65220b6a4a85615577f0fc0c4c0a24ae4ddd1176 (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
/*
 * 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.
 */

#pragma once

/** \file
 * \ingroup fn
 *
 * Field serve as an intermediate representation for a calculation of a group of functions. Having
 * an intermediate representation is helpful mainly to separate the execution system from the
 * system that describes the necessary computations. Fields can be executed in different contexts,
 * and optimization might mean executing the fields differently based on some factors like the
 * number of elements.
 *
 * For now, fields are very tied to the multi-function system, but in the future the #FieldFunction
 * class could be extended to use different descriptions of its outputs and computation besides
 * the embedded multi-function.
 */

#include "BLI_string_ref.hh"
#include "BLI_vector.hh"

#include "FN_multi_function_procedure.hh"
#include "FN_multi_function_procedure_builder.hh"
#include "FN_multi_function_procedure_executor.hh"

namespace blender::fn {

class FieldInput;
class FieldFunction;

/**
 * Descibes the output of a function. Generally corresponds to the combination of an output socket
 * and link combination in a node graph.
 */
class Field {
  /**
   * The type of this field's result.
   */
  const fn::CPPType *type_;

  /**
   * The function that calculates this field's values. Many fields can share the same function,
   * since a function can have many outputs, just like a node graph, where a single output can be
   * used as multiple inputs. This avoids calling the same function many times, only using one of
   * its results.
   */
  std::shared_ptr<FieldFunction> function_;
  /**
   * Which output of the function this field corresponds to.
   */
  int output_index_;

  std::shared_ptr<FieldInput> input_;

  StringRef name_;

 public:
  Field(const fn::CPPType &type,
        std::shared_ptr<FieldFunction> function,
        const int output_index,
        StringRef name = "")
      : type_(&type), function_(function), output_index_(output_index), name_(name)
  {
  }

  Field(const fn::CPPType &type, std::shared_ptr<FieldInput> input, StringRef name = "")
      : type_(&type), input_(input), name_(name)
  {
  }

  const fn::CPPType &type() const
  {
    BLI_assert(type_ != nullptr);
    return *type_;
  }

  bool is_input() const
  {
    return input_ != nullptr;
  }
  const FieldInput &input() const
  {
    BLI_assert(function_ == nullptr);
    BLI_assert(input_ != nullptr);
    return *input_;
  }

  bool is_function() const
  {
    return function_ != nullptr;
  }
  const FieldFunction &function() const
  {
    BLI_assert(function_ != nullptr);
    BLI_assert(input_ == nullptr);
    return *function_;
  }

  int function_output_index() const
  {
    BLI_assert(function_ != nullptr);
    BLI_assert(input_ == nullptr);
    return output_index_;
  }

  blender::StringRef name() const
  {
    return name_;
  }
};

/**
 * An operation acting on data described by fields. Generally corresponds
 * to a node or a subset of a node in a node graph.
 */
class FieldFunction {
  /**
   * The function used to calculate the
   */
  std::unique_ptr<MultiFunction> function_;

  /**
   * References to descriptions of the results from the functions this function depends on.
   */
  blender::Vector<Field> inputs_;

 public:
  FieldFunction(std::unique_ptr<MultiFunction> function, Vector<Field> &&inputs)
      : function_(std::move(function)), inputs_(std::move(inputs))
  {
  }

  Span<Field> inputs() const
  {
    return inputs_;
  }

  const MultiFunction &multi_function() const
  {
    return *function_;
  }
};

class FieldInput {
 public:
  virtual GVArrayPtr retrieve_data(IndexMask mask) const = 0;
};

/**
 * Evaluate more than one field at a time, as an optimization
 * in case they share inputs or various intermediate values.
 */
void evaluate_fields(blender::Span<Field> fields,
                     blender::IndexMask mask,
                     blender::Span<GMutableSpan> outputs);

}  // namespace blender::fn