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

node_geometry_exec.cc « intern « nodes « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a3bbca90731ab5326f6f52d550d4d1810f45422f (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
/*
 * 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 "DNA_modifier_types.h"

#include "DEG_depsgraph_query.h"

#include "NOD_geometry_exec.hh"
#include "NOD_type_conversions.hh"

#include "node_geometry_util.hh"

using blender::nodes::geometry_nodes_eval_log::LocalGeoLogger;

namespace blender::nodes {

void GeoNodeExecParams::error_message_add(const NodeWarningType type, std::string message) const
{
  if (provider_->logger == nullptr) {
    return;
  }
  LocalGeoLogger &local_logger = provider_->logger->local();
  local_logger.log_node_warning(provider_->dnode, type, std::move(message));
}

const bNodeSocket *GeoNodeExecParams::find_available_socket(const StringRef name) const
{
  for (const InputSocketRef *socket : provider_->dnode->inputs()) {
    if (socket->is_available() && socket->name() == name) {
      return socket->bsocket();
    }
  }

  return nullptr;
}

GVArrayPtr GeoNodeExecParams::get_input_attribute(const StringRef name,
                                                  const GeometryComponent &component,
                                                  const AttributeDomain domain,
                                                  const CustomDataType type,
                                                  const void *default_value) const
{
  const bNodeSocket *found_socket = this->find_available_socket(name);
  BLI_assert(found_socket != nullptr); /* There should always be available socket for the name. */
  const CPPType *cpp_type = bke::custom_data_type_to_cpp_type(type);
  const int64_t domain_size = component.attribute_domain_size(domain);

  if (default_value == nullptr) {
    default_value = cpp_type->default_value();
  }

  if (found_socket == nullptr) {
    return std::make_unique<fn::GVArray_For_SingleValue>(*cpp_type, domain_size, default_value);
  }

  if (found_socket->type == SOCK_STRING) {
    const std::string name = this->get_input<std::string>(found_socket->identifier);
    /* Try getting the attribute without the default value. */
    GVArrayPtr attribute = component.attribute_try_get_for_read(name, domain, type);
    if (attribute) {
      return attribute;
    }

    /* If the attribute doesn't exist, use the default value and output an error message
     * (except when the field is empty, to avoid spamming error messages, and not when
     * the domain is empty and we don't expect an attribute anyway). */
    if (!name.empty() && component.attribute_domain_size(domain) != 0) {
      this->error_message_add(NodeWarningType::Error,
                              TIP_("No attribute with name \"") + name + "\"");
    }
    return std::make_unique<fn::GVArray_For_SingleValue>(*cpp_type, domain_size, default_value);
  }
  const DataTypeConversions &conversions = get_implicit_type_conversions();
  if (found_socket->type == SOCK_FLOAT) {
    const float value = this->get_input<float>(found_socket->identifier);
    BUFFER_FOR_CPP_TYPE_VALUE(*cpp_type, buffer);
    conversions.convert_to_uninitialized(CPPType::get<float>(), *cpp_type, &value, buffer);
    return std::make_unique<fn::GVArray_For_SingleValue>(*cpp_type, domain_size, buffer);
  }
  if (found_socket->type == SOCK_INT) {
    const int value = this->get_input<int>(found_socket->identifier);
    BUFFER_FOR_CPP_TYPE_VALUE(*cpp_type, buffer);
    conversions.convert_to_uninitialized(CPPType::get<int>(), *cpp_type, &value, buffer);
    return std::make_unique<fn::GVArray_For_SingleValue>(*cpp_type, domain_size, buffer);
  }
  if (found_socket->type == SOCK_VECTOR) {
    const float3 value = this->get_input<float3>(found_socket->identifier);
    BUFFER_FOR_CPP_TYPE_VALUE(*cpp_type, buffer);
    conversions.convert_to_uninitialized(CPPType::get<float3>(), *cpp_type, &value, buffer);
    return std::make_unique<fn::GVArray_For_SingleValue>(*cpp_type, domain_size, buffer);
  }
  if (found_socket->type == SOCK_RGBA) {
    const ColorGeometry4f value = this->get_input<ColorGeometry4f>(found_socket->identifier);
    BUFFER_FOR_CPP_TYPE_VALUE(*cpp_type, buffer);
    conversions.convert_to_uninitialized(
        CPPType::get<ColorGeometry4f>(), *cpp_type, &value, buffer);
    return std::make_unique<fn::GVArray_For_SingleValue>(*cpp_type, domain_size, buffer);
  }
  BLI_assert(false);
  return std::make_unique<fn::GVArray_For_SingleValue>(*cpp_type, domain_size, default_value);
}

CustomDataType GeoNodeExecParams::get_input_attribute_data_type(
    const StringRef name,
    const GeometryComponent &component,
    const CustomDataType default_type) const
{
  const bNodeSocket *found_socket = this->find_available_socket(name);
  BLI_assert(found_socket != nullptr); /* There should always be available socket for the name. */
  if (found_socket == nullptr) {
    return default_type;
  }

  if (found_socket->type == SOCK_STRING) {
    const std::string name = this->get_input<std::string>(found_socket->identifier);
    std::optional<AttributeMetaData> info = component.attribute_get_meta_data(name);
    if (info) {
      return info->data_type;
    }
    return default_type;
  }
  if (found_socket->type == SOCK_FLOAT) {
    return CD_PROP_FLOAT;
  }
  if (found_socket->type == SOCK_VECTOR) {
    return CD_PROP_FLOAT3;
  }
  if (found_socket->type == SOCK_RGBA) {
    return CD_PROP_COLOR;
  }
  if (found_socket->type == SOCK_BOOLEAN) {
    return CD_PROP_BOOL;
  }

  BLI_assert(false);
  return default_type;
}

/**
 * If any of the corresponding input sockets are attributes instead of single values,
 * use the highest priority attribute domain from among them.
 * Otherwise return the default domain.
 */
AttributeDomain GeoNodeExecParams::get_highest_priority_input_domain(
    Span<std::string> names,
    const GeometryComponent &component,
    const AttributeDomain default_domain) const
{
  Vector<AttributeDomain, 8> input_domains;
  for (const std::string &name : names) {
    const bNodeSocket *found_socket = this->find_available_socket(name);
    BLI_assert(found_socket != nullptr); /* A socket should be available socket for the name. */
    if (found_socket == nullptr) {
      continue;
    }

    if (found_socket->type == SOCK_STRING) {
      const std::string name = this->get_input<std::string>(found_socket->identifier);
      std::optional<AttributeMetaData> info = component.attribute_get_meta_data(name);
      if (info) {
        input_domains.append(info->domain);
      }
    }
  }

  if (input_domains.size() > 0) {
    return bke::attribute_domain_highest_priority(input_domains);
  }

  return default_domain;
}

void GeoNodeExecParams::check_input_access(StringRef identifier,
                                           const CPPType *requested_type) const
{
  bNodeSocket *found_socket = nullptr;
  for (const InputSocketRef *socket : provider_->dnode->inputs()) {
    if (socket->identifier() == identifier) {
      found_socket = socket->bsocket();
      break;
    }
  }

  if (found_socket == nullptr) {
    std::cout << "Did not find an input socket with the identifier '" << identifier << "'.\n";
    std::cout << "Possible identifiers are: ";
    for (const InputSocketRef *socket : provider_->dnode->inputs()) {
      if (socket->is_available()) {
        std::cout << "'" << socket->identifier() << "', ";
      }
    }
    std::cout << "\n";
    BLI_assert_unreachable();
  }
  else if (found_socket->flag & SOCK_UNAVAIL) {
    std::cout << "The socket corresponding to the identifier '" << identifier
              << "' is disabled.\n";
    BLI_assert_unreachable();
  }
  else if (!provider_->can_get_input(identifier)) {
    std::cout << "The identifier '" << identifier
              << "' is valid, but there is no value for it anymore.\n";
    std::cout << "Most likely it has been extracted before.\n";
    BLI_assert_unreachable();
  }
  else if (requested_type != nullptr) {
    const CPPType &expected_type = *found_socket->typeinfo->get_geometry_nodes_cpp_type();
    if (*requested_type != expected_type) {
      std::cout << "The requested type '" << requested_type->name() << "' is incorrect. Expected '"
                << expected_type.name() << "'.\n";
      BLI_assert_unreachable();
    }
  }
}

void GeoNodeExecParams::check_output_access(StringRef identifier, const CPPType &value_type) const
{
  bNodeSocket *found_socket = nullptr;
  for (const OutputSocketRef *socket : provider_->dnode->outputs()) {
    if (socket->identifier() == identifier) {
      found_socket = socket->bsocket();
      break;
    }
  }

  if (found_socket == nullptr) {
    std::cout << "Did not find an output socket with the identifier '" << identifier << "'.\n";
    std::cout << "Possible identifiers are: ";
    for (const OutputSocketRef *socket : provider_->dnode->outputs()) {
      if (socket->is_available()) {
        std::cout << "'" << socket->identifier() << "', ";
      }
    }
    std::cout << "\n";
    BLI_assert_unreachable();
  }
  else if (found_socket->flag & SOCK_UNAVAIL) {
    std::cout << "The socket corresponding to the identifier '" << identifier
              << "' is disabled.\n";
    BLI_assert_unreachable();
  }
  else if (!provider_->can_set_output(identifier)) {
    std::cout << "The identifier '" << identifier << "' has been set already.\n";
    BLI_assert_unreachable();
  }
  else {
    const CPPType &expected_type = *found_socket->typeinfo->get_geometry_nodes_cpp_type();
    if (value_type != expected_type) {
      std::cout << "The value type '" << value_type.name() << "' is incorrect. Expected '"
                << expected_type.name() << "'.\n";
      BLI_assert_unreachable();
    }
  }
}

}  // namespace blender::nodes