Blender V5.3
bake_values.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "MEM_guardedalloc.h"
10
11#include "BLT_translation.hh"
12
16#include "BKE_bake_values.hh"
17#include "BKE_curves.hh"
19#include "BKE_grease_pencil.hh"
20#include "BKE_instances.hh"
21#include "BKE_mesh_types.hh"
22#include "BKE_node.hh"
24#include "BKE_pointcloud.hh"
25#include "BKE_volume.hh"
26#include "BKE_volume_grid.hh"
27
28#include "DNA_curves_types.h"
30#include "DNA_mesh_types.h"
32#include "DNA_volume_types.h"
33
34#include "FN_field.hh"
35
41
42namespace blender::bke::bake {
43
48static constexpr StringRef anonymous_bake_attribute_prefix = ".bake_";
49
50static std::unique_ptr<BakeMaterialsList> materials_to_weak_references(
51 Material ***materials, short *materials_num, BakeDataBlockMap *data_block_map)
52{
53 if (*materials_num == 0) {
54 return {};
55 }
56 auto materials_list = std::make_unique<BakeMaterialsList>();
57 materials_list->resize(*materials_num);
58 for (const int i : materials_list->index_range()) {
59 Material *material = (*materials)[i];
60 if (material) {
61 (*materials_list)[i] = BakeDataBlockID(material->id);
62 if (data_block_map) {
63 data_block_map->try_add(material->id);
64 }
65 }
66 }
67
68 MEM_delete(*materials);
69 *materials = nullptr;
70 *materials_num = 0;
71
72 return materials_list;
73}
74
75static void restore_materials(Material ***materials,
76 short *materials_num,
77 std::unique_ptr<BakeMaterialsList> materials_list,
78 BakeDataBlockMap *data_block_map)
79{
80 if (!materials_list) {
81 return;
82 }
83 BLI_assert(*materials == nullptr);
84 *materials_num = materials_list->size();
85 *materials = MEM_new_array_zeroed<Material *>(materials_list->size(), __func__);
86 if (!data_block_map) {
87 return;
88 }
89
90 for (const int i : materials_list->index_range()) {
91 const std::optional<BakeDataBlockID> &data_block_id = (*materials_list)[i];
92 if (data_block_id) {
93 (*materials)[i] = reinterpret_cast<Material *>(
94 data_block_map->lookup_or_remember_missing(*data_block_id));
95 }
96 }
97}
98
103 private:
104 Vector<BakeValues::InputValue> &root_values_;
105 Map<std::string, std::string> referenced_anonymous_attributes_;
106 int attribute_field_count_ = 0;
107 BakeDataBlockMap *data_block_map_ = nullptr;
108 bool is_for_cache_;
109
110 public:
112 BakeDataBlockMap *data_block_map,
113 const bool is_for_cache)
114 : root_values_(root_values), data_block_map_(data_block_map), is_for_cache_(is_for_cache)
115 {
116 }
117
118 void convert()
119 {
120 PRF_scope_with_name("RuntimeToBakeValue::convert", ProfileCategory::Default);
121 for (BakeValues::InputValue &input_value : root_values_) {
122 input_value.value.ensure_owns_direct_data();
123 }
124
125 this->top_level_fields_to_attributes();
126
127 /* As a pre-pass, gather all directly referenced anonymous attributes, because those will be
128 * kept on the geometries. */
129 {
130 using namespace socket_value_visitor;
131 auto scan_field = [&](const fn::GField &field) {
132 if (const auto *attribute_field = field.get_input_if<AttributeFieldInput>()) {
133 const StringRef attribute_name = attribute_field->attribute_name();
134 if (attribute_name_is_anonymous(attribute_name)) {
135 referenced_anonymous_attributes_.lookup_or_add_cb_as(
136 attribute_name, [&]() { return this->get_next_bake_attribute_name(); });
137 }
138 }
139 return VisitParams::continue_check(true);
140 };
141 VisitParams visit_params;
142 visit_params.check_GField = scan_field;
143 for (const BakeValues::InputValue &input_value : root_values_) {
144 check_recursive(input_value.value, visit_params);
145 }
146 }
147
148 /* Now process all data to be stored in a bake. This involves removing data that can't be
149 * baked. */
150 Vector<int> inputs_to_remove;
151 for (const int i : root_values_.index_range()) {
152 if (!this->runtime_to_bake__SocketValueVariant(root_values_[i].value)) {
153 inputs_to_remove.append(i);
154 }
155 }
156 /* Remove invalid values. */
157 for (int i = inputs_to_remove.size() - 1; i >= 0; i--) {
158 root_values_.remove_and_reorder(inputs_to_remove[i]);
159 }
160 }
161
162 private:
164 void top_level_fields_to_attributes()
165 {
166 GeometrySet *prev_geo = nullptr;
167 for (const int value_i : root_values_.index_range()) {
168 BakeValues::InputValue &input_value = root_values_[value_i];
169 if (input_value.value.is_single()) {
170 const GMutablePointer value_ptr = input_value.value.get();
171 if (value_ptr.is_type<GeometrySet>()) {
172 prev_geo = value_ptr.get<GeometrySet>();
173 continue;
174 }
175 }
176 if (prev_geo && input_value.field_domain.has_value() &&
177 input_value.value.is_context_dependent_field())
178 {
179 const fn::GField &field = input_value.value.ensure_type<fn::GField>();
180 if (field.get_input_if<AttributeFieldInput>()) {
181 continue;
182 }
183 std::string attribute_name = this->get_next_bake_attribute_name();
184 bool any_success = false;
185 for (const GeometryComponent::Type type :
186 {
192 })
193 {
194 if (!prev_geo->has(type)) {
195 continue;
196 }
197 GeometryComponent &component = prev_geo->get_component_for_write(type);
198 any_success |= try_capture_field_on_geometry(
199 component, attribute_name, *input_value.field_domain, field);
200 }
201 if (any_success) {
202 /* Replace the field with the one that was just captured. */
203 input_value.value.emplace<fn::GField>(
204 AttributeFieldInput::from(attribute_name, field.cpp_type()));
205 }
206 }
207 }
208 }
209
210 std::string get_next_bake_attribute_name()
211 {
212 return fmt::format("{}{}", anonymous_bake_attribute_prefix, attribute_field_count_++);
213 }
214
215 [[nodiscard]] bool runtime_to_bake__SocketValueVariant(SocketValueVariant &value_variant)
216 {
217 if (value_variant.is_context_dependent_field()) {
218 const fn::GField &field = *value_variant.get_if<fn::GField>();
219 if (const auto *attribute_field = field.get_input_if<AttributeFieldInput>()) {
220 if (const std::string *new_name = referenced_anonymous_attributes_.lookup_ptr(
221 attribute_field->attribute_name()))
222 {
223 value_variant.emplace<fn::GField>(
224 AttributeFieldInput::from(*new_name, field.cpp_type()));
225 }
226 }
227 else {
228 /* Only attribute fields can be baked. Other fields are discarded. */
229 value_variant.ensure_type(field.cpp_type());
230 }
231 return true;
232 }
233 if (value_variant.is_single()) {
234 GMutablePointer value_ptr = value_variant.get();
235 return this->runtime_to_bake__GMutablePointer(value_ptr);
236 }
237 if (value_variant.is_list()) {
238 nodes::GListPtr list_ptr = value_variant.extract<nodes::GListPtr>();
239 if (list_ptr) {
240 nodes::GList &list = list_ptr.get_for_write();
241 if (!this->runtime_to_bake__List(list)) {
242 return false;
243 }
244 }
245 value_variant.emplace<nodes::GListPtr>(std::move(list_ptr));
246 return true;
247 }
248 if (value_variant.is_volume_grid()) {
249 return true;
250 }
251 return false;
252 }
253
254 [[nodiscard]] bool runtime_to_bake__List(nodes::GList &list)
255 {
256 const CPPType &list_cpp_type = list.cpp_type();
257 if (list_cpp_type.is<SocketValueVariant>()) {
258 bool found_invalid = false;
259 list.typed<SocketValueVariant>().foreach_for_write([&](SocketValueVariant &value_variant) {
260 if (!this->runtime_to_bake__SocketValueVariant(value_variant)) {
261 found_invalid = true;
262 }
263 });
264 return !found_invalid;
265 }
266 if (list_cpp_type.is<GeometrySet>()) {
267 list.typed<GeometrySet>().foreach_for_write(
268 [&](GeometrySet &geometry) { this->runtime_to_bake__GeometrySet(geometry); });
269 return true;
270 }
271 if (list_cpp_type.is<nodes::BundlePtr>()) {
272 list.typed<nodes::BundlePtr>().foreach_for_write([&](nodes::BundlePtr &bundle_ptr) {
273 if (bundle_ptr) {
274 this->runtime_to_bake__Bundle(bundle_ptr.ensure_mutable_inplace());
275 }
276 });
277 return true;
278 }
279 if (this->is_bakeable_single_value_type(list_cpp_type)) {
280 return true;
281 }
282 return false;
283 }
284
285 [[nodiscard]] bool runtime_to_bake__GMutablePointer(GMutablePointer value_ptr)
286 {
287 const CPPType &type = *value_ptr.type();
288 if (type.is<GeometrySet>()) {
289 GeometrySet &geometry = *value_ptr.get<GeometrySet>();
290 this->runtime_to_bake__GeometrySet(geometry);
291 return true;
292 }
293 if (type.is<nodes::BundlePtr>()) {
294 nodes::BundlePtr &bundle_ptr = *value_ptr.get<nodes::BundlePtr>();
295 if (bundle_ptr) {
296 nodes::Bundle &bundle = bundle_ptr.ensure_mutable_inplace();
297 this->runtime_to_bake__Bundle(bundle);
298 }
299 return true;
300 }
301 if (this->is_bakeable_single_value_type(type)) {
302 return true;
303 }
304 return false;
305 }
306
307 void runtime_to_bake__GeometrySet(GeometrySet &geometry)
308 {
309 geometry.ensure_owns_all_data();
310 if (geometry.has_bundle()) {
311 nodes::BundlePtr &bundle_ptr = geometry.bundle_ptr();
312 nodes::Bundle &bundle = bundle_ptr.ensure_mutable_inplace();
313 this->runtime_to_bake__Bundle(bundle);
314 }
315 if (geometry.has_instances()) {
316 Instances &instances = *geometry.get_instances_for_write();
317 instances.ensure_geometry_instances();
318 this->runtime_to_bake__AttributeStorage(instances.attribute_storage());
319 for (bke::InstanceReference &reference : instances.references_for_write()) {
320 if (reference.type() == InstanceReference::Type::GeometrySet) {
321 GeometrySet &geometry = reference.geometry_set();
322 this->runtime_to_bake__GeometrySet(geometry);
323 }
324 else {
325 /* Don't leave dangling pointers for non-geometry object instances (e.g. lights). */
326 reference = bke::InstanceReference();
327 }
328 }
329 }
330 if (geometry.has_mesh()) {
331 Mesh &mesh = *geometry.get_mesh_for_write();
332 if (is_for_cache_) {
333 /* This contains a weak raw pointer to a #BMesh which may become dangling. */
334 mesh.runtime->edit_mesh.reset();
335 }
336 this->runtime_to_bake__AttributeStorage(mesh.attribute_storage.wrap());
337 mesh.runtime->bake_materials = materials_to_weak_references(
338 &mesh.mat, &mesh.totcol, data_block_map_);
339 }
340 if (geometry.has_curves()) {
341 Curves &curves = *geometry.get_curves_for_write();
342 this->runtime_to_bake__AttributeStorage(curves.geometry.attribute_storage.wrap());
343 curves.geometry.runtime->bake_materials = materials_to_weak_references(
344 &curves.mat, &curves.totcol, data_block_map_);
345 }
346 if (geometry.has_pointcloud()) {
347 PointCloud &pointcloud = *geometry.get_pointcloud_for_write();
348 this->runtime_to_bake__AttributeStorage(pointcloud.attribute_storage.wrap());
349 pointcloud.runtime->bake_materials = materials_to_weak_references(
350 &pointcloud.mat, &pointcloud.totcol, data_block_map_);
351 }
352 if (geometry.has_grease_pencil()) {
353 GreasePencil &grease_pencil = *geometry.get_grease_pencil_for_write();
354 this->runtime_to_bake__AttributeStorage(grease_pencil.attribute_storage.wrap());
355 for (GreasePencilDrawingBase *base : grease_pencil.drawings()) {
356 if (base->type != GP_DRAWING) {
357 continue;
358 }
359 greasepencil::Drawing &drawing = reinterpret_cast<GreasePencilDrawing *>(base)->wrap();
360 this->runtime_to_bake__AttributeStorage(
361 drawing.strokes_for_write().attribute_storage.wrap());
362 }
363 grease_pencil.runtime->bake_materials = materials_to_weak_references(
364 &grease_pencil.material_array, &grease_pencil.material_array_num, data_block_map_);
365 }
366 if (geometry.has_volume()) {
367 Volume &volume = *geometry.get_volume_for_write();
368 volume.runtime->bake_materials = materials_to_weak_references(
369 &volume.mat, &volume.totcol, data_block_map_);
370 }
371 }
372
373 void runtime_to_bake__AttributeStorage(AttributeStorage &attributes)
374 {
375 Set<const Attribute *> attributes_to_remove;
376 Map<Attribute *, StringRef> attributes_to_rename;
377 for (Attribute &attribute : attributes) {
378 const StringRef attribute_name = attribute.name();
379 if (attribute_name_is_anonymous(attribute_name) &&
380 !attribute_name.startswith(anonymous_bake_attribute_prefix))
381 {
382 const std::string *new_name = referenced_anonymous_attributes_.lookup_ptr(attribute_name);
383 if (new_name) {
384 attributes_to_rename.add_new(&attribute, *new_name);
385 }
386 else {
387 attributes_to_remove.add_new(&attribute);
388 }
389 }
390 }
391 attributes.remove(attributes_to_remove);
392 attributes.rename(attributes_to_rename);
393 }
394
395 void runtime_to_bake__Bundle(nodes::Bundle &bundle)
396 {
397 Vector<nodes::BundleKey> values_to_remove;
398 for (const auto &item : bundle.items()) {
399 if (auto *socket_value = std::get_if<nodes::BundleItemSocketValue>(&item.value.value)) {
400 if (!this->runtime_to_bake__SocketValueVariant(socket_value->value)) {
401 values_to_remove.append(item.key);
402 }
403 }
404 }
405 for (const nodes::BundleKey &value_to_remove : values_to_remove) {
406 bundle.remove(value_to_remove);
407 }
408 }
409
410 bool is_bakeable_single_value_type(const CPPType &type) const
411 {
412 if (CPPType::get<std::string>() == type) {
413 return true;
414 }
415 return cpp_type_to_custom_data_type(type).has_value();
416 }
417};
418
423 private:
425 std::string anonymous_attribute_name_mixin_;
427 Map<std::string, std::string> runtime_name_by_bake_attribute_;
429 Map<std::string, const CPPType *> attribute_field_types_;
431 BakeDataBlockMap *data_block_map_ = nullptr;
432
433 public:
434 BakeToRuntimeValue(std::string anonymous_attribute_name_mixin, BakeDataBlockMap *data_block_map)
435 : anonymous_attribute_name_mixin_(std::move(anonymous_attribute_name_mixin)),
436 data_block_map_(data_block_map)
437 {
438 }
439
440 void scan(const SocketValueVariant &root_value)
441 {
442 using namespace socket_value_visitor;
443 auto scan_attributes = [&](const AttributeAccessor &attributes) {
444 attributes.foreach_attribute([&](const AttributeIter &iter) {
446 const AttrType attr_type = iter.data_type;
447 const CPPType &attr_cpp_type = attribute_type_to_cpp_type(attr_type);
448 this->attribute_field_types_.add(iter.name, &attr_cpp_type);
449 }
450 });
451 return VisitParams::continue_check(true);
452 };
453 VisitParams visit_params;
454 visit_params.check_AttributeAccessor = scan_attributes;
455 check_recursive(root_value, visit_params);
456 }
457
458 void bake_to_runtime(SocketValueVariant &root_value, const StringRef name)
459 {
460 this->bake_to_runtime__SocketValueVariant(root_value, name);
461 }
462
463 private:
464 void bake_to_runtime__SocketValueVariant(SocketValueVariant &value_variant, const StringRef name)
465 {
466 if (value_variant.is_context_dependent_field()) {
467 const fn::GField &field = *value_variant.get_if<fn::GField>();
468 std::string socket_inspection = nodes::make_anonymous_attribute_socket_inspection_string(
469 TIP_("Bake"), name);
470 if (const auto *attribute_field = field.get_input_if<AttributeFieldInput>()) {
471 const StringRef bake_attribute_name = attribute_field->attribute_name();
472 if (bake_attribute_name.startswith(anonymous_bake_attribute_prefix)) {
473 std::string anonymous_attribute_name = this->get_anonymous_attribute_name(
474 bake_attribute_name);
475 value_variant.emplace<fn::GField>(
476 AttributeFieldInput::from(std::move(anonymous_attribute_name),
477 attribute_field->cpp_type(),
478 std::move(socket_inspection)));
479 }
480 }
481 else if (const auto *attribute_field = field.get_input_if<DeferredTypeAttributeFieldInput>())
482 {
483 const StringRef bake_attribute_name = attribute_field->attribute_name;
484 if (bake_attribute_name.startswith(anonymous_bake_attribute_prefix)) {
485 if (const CPPType *cpp_type = attribute_field_types_.lookup_default(bake_attribute_name,
486 nullptr))
487 {
488 std::string anonymous_attribute_name = this->get_anonymous_attribute_name(
489 bake_attribute_name);
491 std::move(anonymous_attribute_name), *cpp_type, std::move(socket_inspection)));
492 }
493 }
494 }
495 return;
496 }
497 if (value_variant.is_single()) {
498 GMutablePointer value_ptr = value_variant.get();
499 this->bake_to_runtime__GMutablePointer(value_ptr);
500 return;
501 }
502 if (value_variant.is_list()) {
503 nodes::GListPtr list_ptr = value_variant.extract<nodes::GListPtr>();
504 if (list_ptr) {
505 nodes::GList &list = list_ptr.get_for_write();
506 this->bake_to_runtime__GList(list);
507 }
508 value_variant.emplace<nodes::GListPtr>(std::move(list_ptr));
509 }
510 }
511
512 void bake_to_runtime__GMutablePointer(GMutablePointer value_ptr)
513 {
514 const CPPType &type = *value_ptr.type();
515 if (type.is<GeometrySet>()) {
516 GeometrySet &geometry = *value_ptr.get<GeometrySet>();
517 this->bake_to_runtime__GeometrySet(geometry);
518 }
519 if (type.is<nodes::BundlePtr>()) {
520 nodes::BundlePtr &bundle_ptr = *value_ptr.get<nodes::BundlePtr>();
521 if (bundle_ptr) {
522 nodes::Bundle &bundle = bundle_ptr.ensure_mutable_inplace();
523 this->bake_to_runtime__Bundle(bundle);
524 }
525 }
526 }
527
528 void bake_to_runtime__GeometrySet(GeometrySet &geometry)
529 {
530 if (geometry.has_bundle()) {
531 nodes::BundlePtr &bundle_ptr = geometry.bundle_ptr();
532 nodes::Bundle &bundle = bundle_ptr.ensure_mutable_inplace();
533 this->bake_to_runtime__Bundle(bundle);
534 }
535 if (geometry.has_instances()) {
536 Instances &instances = *geometry.get_instances_for_write();
537 instances.ensure_geometry_instances();
538 for (bke::InstanceReference &reference : instances.references_for_write()) {
539 if (reference.type() == InstanceReference::Type::GeometrySet) {
540 GeometrySet &geometry = reference.geometry_set();
541 this->bake_to_runtime__GeometrySet(geometry);
542 }
543 }
544 this->bake_to_runtime__AttributeStorage(instances.attribute_storage());
545 }
546 if (geometry.has_mesh()) {
547 Mesh &mesh = *geometry.get_mesh_for_write();
548 this->bake_to_runtime__AttributeStorage(mesh.attribute_storage.wrap());
550 &mesh.mat, &mesh.totcol, std::move(mesh.runtime->bake_materials), data_block_map_);
551 }
552 if (geometry.has_curves()) {
553 Curves &curves = *geometry.get_curves_for_write();
554 this->bake_to_runtime__AttributeStorage(curves.geometry.attribute_storage.wrap());
555 restore_materials(&curves.mat,
556 &curves.totcol,
557 std::move(curves.geometry.runtime->bake_materials),
558 data_block_map_);
559 }
560 if (geometry.has_pointcloud()) {
561 PointCloud &pointcloud = *geometry.get_pointcloud_for_write();
562 this->bake_to_runtime__AttributeStorage(pointcloud.attribute_storage.wrap());
563 restore_materials(&pointcloud.mat,
564 &pointcloud.totcol,
565 std::move(pointcloud.runtime->bake_materials),
566 data_block_map_);
567 }
568 if (geometry.has_grease_pencil()) {
569 GreasePencil &grease_pencil = *geometry.get_grease_pencil_for_write();
570 this->bake_to_runtime__AttributeStorage(grease_pencil.attribute_storage.wrap());
571 restore_materials(&grease_pencil.material_array,
572 &grease_pencil.material_array_num,
573 std::move(grease_pencil.runtime->bake_materials),
574 data_block_map_);
575 for (GreasePencilDrawingBase *base : grease_pencil.drawings()) {
576 if (base->type != GP_DRAWING) {
577 continue;
578 }
579 greasepencil::Drawing &drawing = reinterpret_cast<GreasePencilDrawing *>(base)->wrap();
580 this->bake_to_runtime__AttributeStorage(
581 drawing.strokes_for_write().attribute_storage.wrap());
582 }
583 }
584 if (geometry.has_volume()) {
585 Volume &volume = *geometry.get_volume_for_write();
587 &volume.mat, &volume.totcol, std::move(volume.runtime->bake_materials), data_block_map_);
588 }
589 }
590
591 void bake_to_runtime__AttributeStorage(AttributeStorage &attributes)
592 {
593 Vector<std::pair<std::string, std::string>> attributes_to_rename;
594 for (const Attribute &attribute : attributes) {
595 const StringRef attribute_name = attribute.name();
596 if (attribute_name.startswith(anonymous_bake_attribute_prefix)) {
597 attributes_to_rename.append(
598 {attribute_name, this->get_anonymous_attribute_name(attribute_name)});
599 }
600 }
601 for (const std::pair<std::string, std::string> &attribute_to_rename : attributes_to_rename) {
602 attributes.rename(attribute_to_rename.first, attribute_to_rename.second);
603 }
604 }
605
606 void bake_to_runtime__Bundle(nodes::Bundle &bundle)
607 {
608 for (auto &&item : bundle.items()) {
609 if (auto *socket_value = std::get_if<nodes::BundleItemSocketValue>(&item.value.value)) {
610 this->bake_to_runtime__SocketValueVariant(socket_value->value, item.key.ustr().ref());
611 }
612 }
613 }
614
615 void bake_to_runtime__GList(nodes::GList &list)
616 {
617 const CPPType &list_cpp_type = list.cpp_type();
618 if (list_cpp_type.is<SocketValueVariant>()) {
619 list.typed<SocketValueVariant>().foreach_for_write([&](SocketValueVariant &value_variant) {
620 this->bake_to_runtime__SocketValueVariant(value_variant, TIP_("List Item"));
621 });
622 }
623 else if (list_cpp_type.is<GeometrySet>()) {
624 list.typed<GeometrySet>().foreach_for_write(
625 [&](GeometrySet &geometry) { this->bake_to_runtime__GeometrySet(geometry); });
626 }
627 else if (list_cpp_type.is<nodes::BundlePtr>()) {
628 list.typed<nodes::BundlePtr>().foreach_for_write([&](nodes::BundlePtr &bundle_ptr) {
629 if (bundle_ptr) {
630 this->bake_to_runtime__Bundle(bundle_ptr.ensure_mutable_inplace());
631 }
632 });
633 }
634 }
635
636 std::string get_anonymous_attribute_name(const StringRef bake_attribute_name)
637 {
638 return runtime_name_by_bake_attribute_.lookup_or_add_cb(bake_attribute_name, [&]() {
639 return hash_to_anonymous_attribute_name(anonymous_attribute_name_mixin_,
640 bake_attribute_name);
641 });
642 }
643};
644
646 BakeDataBlockMap *data_block_map,
647 const bool is_for_cache)
648{
649 RuntimeToBakeValue preparation{runtime_values, data_block_map, is_for_cache};
650 /* This may also remove runtime values that can't be baked. */
651 preparation.convert();
652
653 BakeValues bake_values;
654 for (InputValue &input_value : runtime_values) {
655 bake_values.values_by_id_.add(input_value.id,
656 Item{std::move(input_value.value), std::move(input_value.name)});
657 }
658 return bake_values;
659}
660
662 const CPPType &base_cpp_type)
663{
664 if (value.is_field()) {
665 return value.get_if<fn::GField>()->cpp_type() == base_cpp_type;
666 }
667 if (value.is_list()) {
668 const nodes::GListPtr &list = *value.get_if<nodes::GListPtr>();
669 if (list && list->cpp_type().is<SocketValueVariant>()) {
670 bool found_invalid = false;
671 list->typed<SocketValueVariant>().foreach([&](const SocketValueVariant &item) {
672 if (!value_variant_valid_for_socket(item, base_cpp_type)) {
673 found_invalid = true;
674 }
675 });
676 return !found_invalid;
677 }
678 return !list || list->cpp_type() == base_cpp_type;
679 }
680#ifdef WITH_OPENVDB
681 if (value.is_volume_grid()) {
682 const volume_grid::GVolumeGrid &grid = *value.get_if<volume_grid::GVolumeGrid>();
683 return !grid || grid->cpp_type() == &base_cpp_type;
684 }
685#endif
686 if (value.is_single()) {
687 return value.get().type() == &base_cpp_type;
688 }
689 return false;
690}
691
693 const ComputeContext &compute_context,
694 BakeDataBlockMap *data_block_map) const
695{
696 Vector<SocketValueVariant> output_values(keys.size());
697 std::stringstream ss;
698 ss << compute_context.hash();
699 BakeToRuntimeValue bake_to_runtime_op(ss.str(), data_block_map);
700 for (const Item &item : values_by_id_.values()) {
701 bake_to_runtime_op.scan(item.value);
702 }
703 for (const int i : keys.index_range()) {
704 const OutputKey &key = keys[i];
706 if (!stype || !stype->geometry_nodes_default_value) {
707 continue;
708 }
709 SocketValueVariant &output_value = output_values[i];
710 const Item *item = values_by_id_.lookup_ptr(key.id);
711 if (!item) {
712 output_value = *stype->geometry_nodes_default_value;
713 continue;
714 }
715 output_value = item->value;
716 bake_to_runtime_op.bake_to_runtime(output_value, item->name.value_or(""));
717 if (!value_variant_valid_for_socket(output_value, *stype->base_cpp_type)) {
718 output_value = *stype->geometry_nodes_default_value;
719 }
720 }
721 return output_values;
722}
723
724} // namespace blender::bke::bake
Low-level operations for curves.
Low-level operations for grease pencil.
General operations for point clouds.
Volume data-block.
#define TIP_(msgid)
Read Blender memory allocation and freeing.
#define PRF_scope_with_name(ui_name, category)
const ComputeContextHash & hash() const
const CPPType * type() const
bool add(const Key &key, const Value &value)
Definition BLI_map.hh:295
Value lookup_default(const Key &key, const Value &default_value) const
Definition BLI_map.hh:570
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
int64_t size() const
void append(const T &value)
IndexRange index_range() const
static fn::GField from(std::string name, const CPPType &type, std::optional< std::string > socket_inspection_name=std::nullopt)
void bake_to_runtime(SocketValueVariant &root_value, const StringRef name)
BakeToRuntimeValue(std::string anonymous_attribute_name_mixin, BakeDataBlockMap *data_block_map)
void scan(const SocketValueVariant &root_value)
static BakeValues from_runtime_values(Vector< InputValue > runtime_values, BakeDataBlockMap *data_block_map, bool is_for_cache=true)
Vector< SocketValueVariant > to_runtime_values(const Span< OutputKey > keys, const ComputeContext &compute_context, BakeDataBlockMap *data_block_map) const
RuntimeToBakeValue(Vector< BakeValues::InputValue > &root_values, BakeDataBlockMap *data_block_map, const bool is_for_cache)
constexpr bool startswith(StringRef prefix) const
const InputT * get_input_if() const
Definition FN_field.hh:623
const CPPType & cpp_type() const
Definition FN_field.hh:518
#define attribute(slot)
static constexpr StringRef anonymous_bake_attribute_prefix
static std::unique_ptr< BakeMaterialsList > materials_to_weak_references(Material ***materials, short *materials_num, BakeDataBlockMap *data_block_map)
static void restore_materials(Material ***materials, short *materials_num, std::unique_ptr< BakeMaterialsList > materials_list, BakeDataBlockMap *data_block_map)
static bool value_variant_valid_for_socket(const SocketValueVariant &value, const CPPType &base_cpp_type)
bool attribute_name_is_anonymous(const StringRef name)
std::optional< eCustomDataType > cpp_type_to_custom_data_type(const CPPType &type)
bool try_capture_field_on_geometry(MutableAttributeAccessor attributes, const fn::FieldContext &field_context, const StringRef name, AttrDomain domain, const fn::Field< bool > &selection, const fn::GField &field)
bNodeSocketType * node_socket_type_find_static(int type, int subtype=0)
Definition node.cc:2975
std::string hash_to_anonymous_attribute_name(Args &&...args)
const CPPType & attribute_type_to_cpp_type(AttrType type)
BLI_INLINE Texture * wrap(MTLTexture *texture)
std::string make_anonymous_attribute_socket_inspection_string(const bNodeSocket &socket)
ImplicitSharingPtr< Bundle > BundlePtr
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
GeometryComponent & get_component_for_write(GeometryComponent::Type component_type)
bool has(const GeometryComponent::Type component_type) const
Defines a socket type.
Definition BKE_node.hh:163
const CPPType * base_cpp_type
Definition BKE_node.hh:210
const SocketValueVariant * geometry_nodes_default_value
Definition BKE_node.hh:216
BakeDataBlockID(ID_Type type, std::string id_name, std::string lib_name)
virtual void try_add(ID &id)=0
virtual ID * lookup_or_remember_missing(const BakeDataBlockID &key)=0
std::optional< std::string > name