Blender V5.3
sculpt_undo.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2006 by Nicholas Bishop. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
25#include "sculpt_undo.hh"
26
27#include <mutex>
28#include <zstd.h>
29
30#include "CLG_log.h"
31
32#include "BLI_array.hh"
34#include "BLI_compression.hh"
36#include "BLI_listbase.hh"
37#include "BLI_map.hh"
38#include "BLI_memory_counter.hh"
39#include "BLI_string_utf8.hh"
40#include "BLI_task_c.hh"
41#include "BLI_utildefines.hh"
42#include "BLI_vector.hh"
43
44#include "DNA_key_types.h"
45#include "DNA_object_types.h"
46#include "DNA_scene_types.h"
47#include "DNA_screen_types.h"
48
49#include "BKE_attribute.hh"
51#include "BKE_ccg.hh"
52#include "BKE_context.hh"
53#include "BKE_customdata.hh"
54#include "BKE_global.hh"
55#include "BKE_key.hh"
56#include "BKE_layer.hh"
57#include "BKE_main.hh"
58#include "BKE_mesh.hh"
59#include "BKE_multires.hh"
60#include "BKE_object.hh"
61#include "BKE_object_types.hh"
62#include "BKE_paint.hh"
63#include "BKE_paint_types.hh"
64#include "BKE_scene.hh"
65#include "BKE_subdiv_ccg.hh"
66#include "BKE_undo_system.hh"
67
68/* TODO(sergey): Ideally should be no direct call to such low level things. */
69#include "BKE_subdiv_eval.hh"
70
71#include "DEG_depsgraph.hh"
72
73#include "WM_api.hh"
74#include "WM_types.hh"
75
76#include "ED_geometry.hh"
77#include "ED_object.hh"
78#include "ED_sculpt.hh"
79#include "ED_undo.hh"
80
81#include "bmesh.hh"
82#include "mesh_brush_common.hh"
83#include "paint_hide.hh"
84#include "sculpt_color.hh"
85#include "sculpt_dyntopo.hh"
86#include "sculpt_face_set.hh"
87#include "sculpt_intern.hh"
88
89namespace blender {
90
91// #define DEBUG_TIME
92
93#ifdef DEBUG_TIME
94# include "BLI_timeit.hh"
95#endif
96
97static CLG_LogRef LOG = {"undo.sculpt"};
98
99namespace ed::sculpt_paint::undo {
100
101/* Implementation of undo system for objects in sculpt mode.
102 *
103 * Each undo step in sculpt mode consists of list of nodes, each node contains a flat array of data
104 * related to the step type.
105 *
106 * Node type used for undo depends on specific operation and active sculpt mode ("regular" or
107 * dynamic topology).
108 *
109 * Regular sculpt brushes will use Position, HideVert, HideFace, Mask, FaceSet nodes. These
110 * nodes are created for every BVH node which is affected by the brush. The undo push for the node
111 * happens BEFORE modifications. This makes the operation undo to work in the following way: for
112 * every node in the undo step swap happens between node in the undo stack and the corresponding
113 * value in the BVH. This is how redo is possible after undo.
114 *
115 * The COORDS, HIDDEN or MASK type of nodes contains arrays of the corresponding values.
116 *
117 * Operations like Symmetrize are using GEOMETRY type of nodes which pushes the entire state of the
118 * mesh to the undo stack. This node contains all CustomData layers.
119 *
120 * The tricky aspect of this undo node type is that it stores mesh before and after modification.
121 * This allows the undo system to both undo and redo the symmetrize operation within the
122 * pre-modified-push of other node type behavior, but it uses more memory that it seems it should
123 * be.
124 *
125 * The dynamic topology undo nodes are handled somewhat separately from all other ones and the idea
126 * there is to store log of operations: which vertices and faces have been added or removed.
127 *
128 * Begin of dynamic topology sculpting mode have own node type. It contains an entire copy of mesh
129 * since just enabling the dynamic topology mode already does modifications on it.
130 *
131 * End of dynamic topology and symmetrize in this mode are handled in a special manner as well. */
132
166
168 std::optional<bke::AttrDomain> domain;
170 std::string name;
172};
173
174/* Storage of geometry for the undo node.
175 * Is used as a storage for either original or modified geometry. */
177 /* Is used for sanity check, helping with ensuring that two and only two
178 * geometry pushes happened in the undo stack. */
180
192};
193
194struct Node;
196
197struct StepData {
198 private:
199 bool applied_ = true;
200
201 public:
207
209 std::string object_name;
210
213
214 /* TODO: Combine the three structs into a variant, since they specify data that is only valid
215 * within a single mode. */
216 struct {
217 /* The number of vertices in the entire mesh. */
218 int verts_num;
219 /* The number of face corners in the entire mesh. */
220 int corners_num;
222
223 struct {
225 int grids_num;
227 int grid_size;
229
230 struct {
242
243 /* Geometry at the bmesh enter moment. */
246
249
250 /* Geometry modification operations. */
251 /* Original geometry is stored before the modification and is restored from when undoing. */
253 /* Modified geometry is stored after the modification and is restored from when redoing. */
255
257
268
271 std::unique_ptr<PositionUndoStorage> position_step_storage;
272 size_t undo_size;
273
275 bool needs_undo() const
276 {
277 return applied_;
278 }
279
281 {
282 applied_ = true;
283 }
284
286 {
287 applied_ = false;
288 }
289};
290
291namespace compression {
292
297template<typename T>
299 Vector<std::byte> &filter_buffer,
300 Vector<std::byte> &compress_buffer)
301{
303 filter_buffer.resize(src.size_in_bytes());
304 filter_transpose_delta(reinterpret_cast<const uint8_t *>(src.data()),
305 reinterpret_cast<uint8_t *>(filter_buffer.data()),
306 src.size(),
307 sizeof(T));
308
309 /* Level 3 gives a good balance of compression performance and ratio, and is also used elsewhere
310 * across Blender for calls to #ZSTD_compress. */
311 constexpr int zstd_level = 3;
312 compress_buffer.resize(ZSTD_compressBound(src.size_in_bytes()));
313 const size_t dst_size = ZSTD_compress(compress_buffer.data(),
314 compress_buffer.size(),
315 filter_buffer.data(),
316 filter_buffer.size(),
317 zstd_level);
318 if (ZSTD_isError(dst_size)) {
319 compress_buffer.clear();
320 return;
321 }
322
323 compress_buffer.resize(dst_size);
324}
325
326template<typename T>
328{
330 const unsigned long long dst_size_in_bytes = ZSTD_getFrameContentSize(src.data(), src.size());
331 if (ELEM(dst_size_in_bytes, ZSTD_CONTENTSIZE_ERROR, ZSTD_CONTENTSIZE_UNKNOWN)) {
332 dst.clear();
333 return;
334 }
335
336 buffer.resize(dst_size_in_bytes);
337 const size_t result = ZSTD_decompress(buffer.data(), buffer.size(), src.data(), src.size());
338 if (ZSTD_isError(result)) {
339 dst.clear();
340 return;
341 }
342
343 dst.resize(buffer.size() / sizeof(T));
344 unfilter_transpose_delta(reinterpret_cast<const uint8_t *>(buffer.data()),
345 reinterpret_cast<uint8_t *>(dst.data()),
346 dst.size(),
347 sizeof(T));
348}
349
352
355
356} // namespace compression
357
361
363
364 /* As undo and redo happen, the data in these arrays is swapped (an undo step becomes a redo
365 * step, and vice versa). */
367
369
371 std::atomic<bool> compression_ready = false;
372 std::atomic<bool> compression_started = false;
374
375 explicit PositionUndoStorage(StepData &step_data)
376 : nodes_to_compress(std::move(step_data.nodes)), owner_step_data(&step_data)
377 {
378 this->multires_undo = step_data.grids.grids_num != 0;
379 if (!multires_undo) {
380 this->unique_verts_nums.reinitialize(this->nodes_to_compress.size());
381 for (const int i : this->nodes_to_compress.index_range()) {
382 this->unique_verts_nums[i] = this->nodes_to_compress[i]->unique_verts_num;
383 }
384 }
385
386 this->compression_task_pool = BLI_task_pool_create_background(this, TASK_PRIORITY_LOW);
387 this->compression_started = true;
388
389 BLI_task_pool_push(this->compression_task_pool, compress_fn, this, false, nullptr);
390 }
391
399
401 {
402 if (!compression_ready.load(std::memory_order_acquire)) {
404 }
405 }
406
407 static void compress_fn(TaskPool * /*pool*/, void *task_data)
408 {
409#ifdef DEBUG_TIME
410 SCOPED_TIMER_AVERAGED(__func__);
411#endif
412 auto *data = static_cast<PositionUndoStorage *>(task_data);
413 MutableSpan<std::unique_ptr<Node>> nodes = data->nodes_to_compress;
414 const int nodes_num = nodes.size();
415
417 Array<Array<std::byte>> compressed_data(nodes.size(), NoInitialization());
418 struct CompressLocalData {
419 Vector<std::byte> filtered;
420 Vector<std::byte> compressed;
421 };
424 threading::parallel_for(IndexRange(nodes_num), 1, [&](const IndexRange range) {
425 CompressLocalData &local_data = all_tls.local();
426 for (const int i : range) {
427 const Span<int> indices = data->multires_undo ? nodes[i]->grids : nodes[i]->vert_indices;
428 const Span<float3> positions = !nodes[i]->orig_position.is_empty() ?
429 nodes[i]->orig_position :
430 nodes[i]->position;
431 compression::filter_compress(indices, local_data.filtered, local_data.compressed);
432 new (&compressed_indices[i]) Array<std::byte>(local_data.compressed.as_span());
433 compression::filter_compress(positions, local_data.filtered, local_data.compressed);
434 new (&compressed_data[i]) Array<std::byte>(local_data.compressed.as_span());
435 nodes[i].reset();
436 }
437 });
438 });
439 data->nodes_to_compress.clear_and_shrink();
440
441 size_t memory_size = 0;
442 for (const int i : IndexRange(nodes_num)) {
443 memory_size += compressed_indices[i].as_span().size_in_bytes();
444 memory_size += compressed_data[i].as_span().size_in_bytes();
445 }
446
447 data->compressed_indices = std::move(compressed_indices);
448 data->compressed_positions = std::move(compressed_data);
449 data->owner_step_data->undo_size += memory_size;
450
451 data->compression_ready.store(true, std::memory_order_release);
452 }
453};
454
457 /* NOTE: will split out into list for multi-object-sculpt-mode. */
459
460 /* Active color attribute at the start of this undo step. */
462
463 /* Active color attribute at the end of this undo step. */
465};
466
468{
469 if (step->type != BKE_UNDOSYS_TYPE_SCULPT) {
470 return 0;
471 }
472
473 SculptUndoStep *sculpt_step = reinterpret_cast<SculptUndoStep *>(step);
474
475 if (sculpt_step->data.position_step_storage) {
476 sculpt_step->data.position_step_storage->ensure_compression_complete();
477 }
478
479 return sculpt_step->data.undo_size;
480}
481
483{
484 UndoStack *ustack = ED_undo_stack_get();
486 return reinterpret_cast<SculptUndoStep *>(us);
487}
488
490{
491 if (SculptUndoStep *us = get_active_step()) {
492 return &us->data;
493 }
494 return nullptr;
495}
496
497static bool use_multires_undo(const StepData &step_data, const SculptSession &ss)
498{
499 return step_data.grids.grids_num != 0 && ss.subdiv_ccg != nullptr;
500}
501
502static bool topology_matches(const StepData &step_data, const Object &object)
503{
504 const SculptSession &ss = *object.runtime->sculpt_session;
505 if (use_multires_undo(step_data, ss)) {
506 const SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
507 return subdiv_ccg.grids_num == step_data.grids.grids_num &&
508 subdiv_ccg.grid_size == step_data.grids.grid_size;
509 }
510 const Mesh &mesh = *id_cast<Mesh *>(object.data);
511 return mesh.verts_num == step_data.mesh.verts_num;
512}
513
514static bool indices_contain_true(const Span<bool> data, const Span<int> indices)
515{
516 return std::any_of(indices.begin(), indices.end(), [&](const int i) { return data[i]; });
517}
518
520 Depsgraph &depsgraph,
521 const StepData &step_data,
522 Object &object)
523{
524 const SculptSession &ss = *object.runtime->sculpt_session;
525 if (ss.shapekey_active && ss.shapekey_active->name != step_data.active_shape_key_name) {
526 /* Shape key has been changed before calling undo operator. */
527
528 Key *key = BKE_key_from_object(&object);
529 const KeyBlock *kb = key ?
530 BKE_keyblock_find_name(key, step_data.active_shape_key_name.c_str()) :
531 nullptr;
532
533 if (kb) {
534 object.shapenr = BLI_findindex(&key->block, kb) + 1;
535
536 BKE_sculptsession_update_for_edit(&depsgraph, &object, false);
538 }
539 else {
540 /* Key has been removed -- skip this undo node. */
541 return false;
542 }
543 }
544 return true;
545}
546
547template<typename T>
548static void swap_indexed_data(MutableSpan<T> full, const Span<int> indices, MutableSpan<T> indexed)
549{
551 BLI_assert(full.size() == indices.size());
552 for (const int i : indices.index_range()) {
553 std::swap(full[i], indexed[indices[i]]);
554 }
555}
556
557static void restore_position_mesh(Object &object,
558 PositionUndoStorage &undo_data,
559 const MutableSpan<bool> modified_verts)
560{
562 SculptSession &ss = *object.runtime->sculpt_session;
563 Mesh &mesh = *id_cast<Mesh *>(object.data);
564 MutableSpan<float3> positions = mesh.vert_positions_for_write();
565 std::optional<ShapeKeyData> shape_key_data = ShapeKeyData::from_object(object);
566
567 undo_data.ensure_compression_complete();
568
569 const int nodes_num = undo_data.unique_verts_nums.size();
570
571 struct LocalData {
572 Vector<std::byte> compress_buffer;
573 Vector<std::byte> filter_buffer;
574 Vector<int> indices;
575 Vector<float3> positions;
576 };
578 threading::parallel_for(IndexRange(nodes_num), 1, [&](const IndexRange range) {
579 LocalData &tls = all_tls.local();
580 for (const int i : range) {
582 undo_data.compressed_indices[i], tls.compress_buffer, tls.indices);
583 const int unique_verts_num = undo_data.unique_verts_nums[i];
584 const Span<int> verts = tls.indices.as_span().take_front(unique_verts_num);
585
587 undo_data.compressed_positions[i], tls.compress_buffer, tls.positions);
588 MutableSpan undo_positions = tls.positions.as_mutable_span();
589
590 if (!ss.deform_modifiers_active && !shape_key_data) {
591 /* When original positions aren't written separately in the undo step, there are no
592 * deform modifiers. Therefore the original and evaluated deform positions will be the
593 * same, and modifying the positions from the original mesh is enough. */
594 swap_indexed_data(undo_positions.take_front(unique_verts_num), verts, positions);
595 }
596 else {
597 /* When original positions are stored in the undo step, undo/redo will cause a reevaluation
598 * of the object. The evaluation will recompute the evaluated positions, so dealing with
599 * them here is unnecessary. */
600 if (shape_key_data) {
601 MutableSpan<float3> active_data = shape_key_data->active_key_data;
602
603 if (!shape_key_data->dependent_keys.is_empty()) {
604 Array<float3, 1024> translations(verts.size());
606 undo_positions.take_front(unique_verts_num), verts, active_data, translations);
607 for (MutableSpan<float3> data : shape_key_data->dependent_keys) {
608 apply_translations(translations, verts, data);
609 }
610 }
611
612 if (shape_key_data->basis_key_active) {
613 /* The basis key positions and the mesh positions are always kept in sync. */
614 scatter_data_mesh(undo_positions.as_span(), verts, positions);
615 }
616 swap_indexed_data(undo_positions.take_front(unique_verts_num), verts, active_data);
617 }
618 else {
619 /* There is a deform modifier, but no shape keys. */
620 swap_indexed_data(undo_positions.take_front(unique_verts_num), verts, positions);
621 }
622 }
623
624 modified_verts.fill_indices(verts, true);
625
626 compression::filter_compress<float3>(undo_positions, tls.filter_buffer, tls.compress_buffer);
627 undo_data.compressed_positions[i] = tls.compress_buffer.as_span();
628 }
629 });
630}
631
632static void restore_position_grids(const MutableSpan<float3> positions,
633 const CCGKey &key,
634 PositionUndoStorage &undo_data,
635 const MutableSpan<bool> modified_grids)
636{
638 const int nodes_num = undo_data.compressed_indices.size();
639
640 struct LocalData {
641 Vector<std::byte> compress_buffer;
642 Vector<std::byte> filter_buffer;
643 Vector<int> indices;
644 Vector<float3> positions;
645 };
647 threading::parallel_for(IndexRange(nodes_num), 1, [&](const IndexRange range) {
648 LocalData &tls = all_tls.local();
649 for (const int i : range) {
651 undo_data.compressed_indices[i], tls.compress_buffer, tls.indices);
652 const Span<int> grids = tls.indices.as_span();
653
655 undo_data.compressed_positions[i], tls.compress_buffer, tls.positions);
656 MutableSpan node_positions = tls.positions.as_mutable_span();
657
658 for (const int i : grids.index_range()) {
659 MutableSpan data = positions.slice(bke::ccg::grid_range(key, grids[i]));
660 MutableSpan undo_data = node_positions.slice(bke::ccg::grid_range(key, i));
661 for (const int offset : data.index_range()) {
662 std::swap(data[offset], undo_data[offset]);
663 }
664 }
665
666 modified_grids.fill_indices(grids, true);
667
668 compression::filter_compress<float3>(node_positions, tls.filter_buffer, tls.compress_buffer);
669 undo_data.compressed_positions[i] = tls.compress_buffer.as_span();
670 }
671 });
672}
673
675 Node &unode,
676 const MutableSpan<bool> modified_verts)
677{
679 Mesh &mesh = *id_cast<Mesh *>(object.data);
680 bke::MutableAttributeAccessor attributes = mesh.attributes_for_write();
682 ".hide_vert", bke::AttrDomain::Point);
683 for (const int i : unode.vert_indices.index_range().take_front(unode.unique_verts_num)) {
684 const int vert = unode.vert_indices[i];
685 if (unode.vert_hidden[i].test() != hide_vert.span[vert]) {
686 unode.vert_hidden[i].set(!unode.vert_hidden[i].test());
687 hide_vert.span[vert] = !hide_vert.span[vert];
688 modified_verts[vert] = true;
689 }
690 }
691 hide_vert.finish();
692}
693
695 Node &unode,
696 const MutableSpan<bool> modified_grids)
697{
699 if (unode.grid_hidden.is_empty()) {
701 return;
702 }
703
704 BitGroupVector<> &grid_hidden = BKE_subdiv_ccg_grid_hidden_ensure(subdiv_ccg);
705 const Span<int> grids = unode.grids;
706 for (const int i : grids.index_range()) {
707 /* Swap the two bit spans. */
709 MutableBoundedBitSpan b = grid_hidden[grids[i]];
710 for (const int j : a.index_range()) {
711 const bool value_a = a[j];
712 const bool value_b = b[j];
713 a[j].set(value_b);
714 b[j].set(value_a);
715 }
716 }
717
718 modified_grids.fill_indices(grids, true);
719}
720
721static void restore_hidden_face(Object &object,
722 Node &unode,
723 const MutableSpan<bool> modified_faces)
724{
726 Mesh &mesh = *id_cast<Mesh *>(object.data);
727 bke::MutableAttributeAccessor attributes = mesh.attributes_for_write();
728 bke::SpanAttributeWriter hide_poly = attributes.lookup_or_add_for_write_span<bool>(
729 ".hide_poly", bke::AttrDomain::Face);
730
731 const Span<int> face_indices = unode.face_indices;
732
733 for (const int i : face_indices.index_range()) {
734 const int face = face_indices[i];
735 if (unode.face_hidden[i].test() != hide_poly.span[face]) {
736 unode.face_hidden[i].set(!unode.face_hidden[i].test());
737 hide_poly.span[face] = !hide_poly.span[face];
738 modified_faces[face] = true;
739 }
740 }
741 hide_poly.finish();
742}
743
744static void restore_color(Object &object,
745 StepData &step_data,
746 const MutableSpan<bool> modified_verts)
747{
749 Mesh &mesh = *id_cast<Mesh *>(object.data);
751
752 for (std::unique_ptr<Node> &unode : step_data.nodes) {
753 if (color_attribute.domain == bke::AttrDomain::Point && !unode->col.is_empty()) {
755 unode->vert_indices.as_span().take_front(unode->unique_verts_num),
756 color_attribute.span,
757 unode->col);
758 }
759 else if (color_attribute.domain == bke::AttrDomain::Corner && !unode->loop_col.is_empty()) {
760 color::swap_gathered_colors(unode->corner_indices, color_attribute.span, unode->loop_col);
761 }
762
763 modified_verts.fill_indices(unode->vert_indices.as_span(), true);
764 }
765
766 color_attribute.finish();
767}
768
769static void restore_mask_mesh(Object &object, Node &unode, const MutableSpan<bool> modified_verts)
770{
773
774 bke::MutableAttributeAccessor attributes = mesh->attributes_for_write();
776 ".sculpt_mask", bke::AttrDomain::Point);
777
778 const Span<int> index = unode.vert_indices.as_span().take_front(unode.unique_verts_num);
779
780 for (const int i : index.index_range()) {
781 const int vert = index[i];
782 if (mask.span[vert] != unode.mask[i]) {
783 std::swap(mask.span[vert], unode.mask[i]);
784 modified_verts[vert] = true;
785 }
786 }
787
788 mask.finish();
789}
790
791static void restore_mask_grids(Object &object, Node &unode, const MutableSpan<bool> modified_grids)
792{
794 SculptSession &ss = *object.runtime->sculpt_session;
795 SubdivCCG *subdiv_ccg = ss.subdiv_ccg;
796 MutableSpan<float> masks = subdiv_ccg->masks;
797
798 const CCGKey key = BKE_subdiv_ccg_key_top_level(*subdiv_ccg);
799
800 const Span<int> grids = unode.grids;
801 MutableSpan<float> undo_mask = unode.mask;
802
803 for (const int i : grids.index_range()) {
804 MutableSpan data = masks.slice(bke::ccg::grid_range(key, grids[i]));
805 MutableSpan undo_data = undo_mask.slice(bke::ccg::grid_range(key, i));
806 for (const int offset : data.index_range()) {
807 std::swap(data[offset], undo_data[offset]);
808 }
809 }
810
811 modified_grids.fill_indices(unode.grids.as_span(), true);
812}
813
814static bool restore_face_sets(Object &object,
815 Node &unode,
816 const MutableSpan<bool> modified_face_set_faces)
817{
819 const Span<int> face_indices = unode.face_indices;
820
822 *id_cast<Mesh *>(object.data));
823 bool modified = false;
824 for (const int i : face_indices.index_range()) {
825 const int face = face_indices[i];
826 if (unode.face_sets[i] == face_sets.span[face]) {
827 continue;
828 }
829 std::swap(unode.face_sets[i], face_sets.span[face]);
830 modified_face_set_faces[face] = true;
831 modified = true;
832 }
833 face_sets.finish();
834 return modified;
835}
836
837static void bmesh_restore_generic(StepData &step_data, Object &object)
838{
840 SculptSession &ss = *object.runtime->sculpt_session;
841 if (step_data.needs_undo()) {
842 BM_log_undo(ss.bm, ss.bm_log);
843 step_data.tag_needs_redo();
844 }
845 else {
846 BM_log_redo(ss.bm, ss.bm_log);
847 step_data.tag_needs_undo();
848 }
849
850 if (step_data.type == Type::Mask) {
852 IndexMaskMemory memory;
853 const IndexMask node_mask = bke::pbvh::all_leaf_nodes(pbvh, memory);
854 pbvh.tag_masks_changed(node_mask);
855 bke::pbvh::update_mask_bmesh(*ss.bm, node_mask, pbvh);
856 }
857 else {
861 }
862}
863
864/* Create empty sculpt BMesh and enable logging. */
865static void bmesh_enable(Object &object, const StepData &step_data)
866{
867 SculptSession &ss = *object.runtime->sculpt_session;
868 Mesh *mesh = id_cast<Mesh *>(object.data);
869
872
873 /* Create empty BMesh and enable logging. */
874 BMeshCreateParams bmesh_create_params{};
875 bmesh_create_params.use_toolflags = false;
876
877 ss.bm = BM_mesh_create(&bm_mesh_allocsize_default, &bmesh_create_params);
878 BM_data_layer_add_named(ss.bm, &ss.bm->vdata, CD_PROP_FLOAT, ".sculpt_mask");
879
881
882 /* Restore the BMLog using saved entries. */
883 ss.bm_log = BM_log_from_existing_entries_create(ss.bm, step_data.bmesh.bm_entry);
884}
885
886static void bmesh_handle_dyntopo_begin(bContext *C, StepData &step_data, Object &object)
887{
888 if (step_data.needs_undo()) {
889 dyntopo::disable(C, &step_data);
890 step_data.tag_needs_redo();
891 }
892 else /* needs_redo */ {
893 SculptSession &ss = *object.runtime->sculpt_session;
894 bmesh_enable(object, step_data);
895
896 /* Restore the mesh from the first log entry. */
897 BM_log_redo(ss.bm, ss.bm_log);
898
899 step_data.tag_needs_undo();
900 }
901}
902
903static void bmesh_handle_dyntopo_end(bContext *C, StepData &step_data, Object &object)
904{
905 if (step_data.needs_undo()) {
906 SculptSession &ss = *object.runtime->sculpt_session;
907 bmesh_enable(object, step_data);
908
909 /* Restore the mesh from the last log entry. */
910 BM_log_undo(ss.bm, ss.bm_log);
911
912 step_data.tag_needs_redo();
913 }
914 else /* needs_redo */ {
915 /* Disable dynamic topology sculpting. */
916 dyntopo::disable(C, nullptr);
917 step_data.tag_needs_undo();
918 }
919}
920
922{
924 const Mesh *mesh = id_cast<const Mesh *>(object.data);
925
926 BLI_assert(!geometry->is_initialized);
927 geometry->is_initialized = true;
928
929 geometry->attribute_storage = mesh->attribute_storage.wrap();
931 &mesh->vert_data, &geometry->vert_data, CD_MASK_MESH.vmask, mesh->verts_num);
933 &mesh->edge_data, &geometry->edge_data, CD_MASK_MESH.emask, mesh->edges_num);
935 &mesh->corner_data, &geometry->corner_data, CD_MASK_MESH.lmask, mesh->corners_num);
937 &mesh->face_data, &geometry->face_data, CD_MASK_MESH.pmask, mesh->faces_num);
938 implicit_sharing::copy_shared_pointer(mesh->face_offset_indices,
939 mesh->runtime->face_offsets_sharing_info,
940 &geometry->face_offset_indices,
941 &geometry->face_offsets_sharing_info);
942
943 geometry->verts_num = mesh->verts_num;
944 geometry->edges_num = mesh->edges_num;
945 geometry->corners_num = mesh->corners_num;
946 geometry->faces_num = mesh->faces_num;
947}
948
950{
952 BLI_assert(geometry->is_initialized);
953
955
956 mesh->verts_num = geometry->verts_num;
957 mesh->edges_num = geometry->edges_num;
958 mesh->corners_num = geometry->corners_num;
959 mesh->faces_num = geometry->faces_num;
960 mesh->totface_legacy = 0;
961
962 mesh->attribute_storage.wrap() = geometry->attribute_storage.wrap();
964 &geometry->vert_data, &mesh->vert_data, CD_MASK_MESH.vmask, geometry->verts_num);
966 &geometry->edge_data, &mesh->edge_data, CD_MASK_MESH.emask, geometry->edges_num);
968 &geometry->corner_data, &mesh->corner_data, CD_MASK_MESH.lmask, geometry->corners_num);
970 &geometry->face_data, &mesh->face_data, CD_MASK_MESH.pmask, geometry->faces_num);
972 geometry->face_offsets_sharing_info,
973 &mesh->face_offset_indices,
974 &mesh->runtime->face_offsets_sharing_info);
975}
976
978{
980 CustomData_free(&geometry->vert_data);
981 CustomData_free(&geometry->edge_data);
982 CustomData_free(&geometry->corner_data);
983 CustomData_free(&geometry->face_data);
984 implicit_sharing::free_shared_data(&geometry->face_offset_indices,
985 &geometry->face_offsets_sharing_info);
986}
987
988static void restore_geometry(StepData &step_data, Object &object)
989{
993
994 Mesh *mesh = id_cast<Mesh *>(object.data);
995
996 if (step_data.needs_undo()) {
998 step_data.tag_needs_redo();
999 }
1000 else {
1002 step_data.tag_needs_undo();
1003 }
1004}
1005
1006/* Handle all dynamic-topology updates
1007 *
1008 * Returns true if this was a dynamic-topology undo step, otherwise
1009 * returns false to indicate the non-dyntopo code should run. */
1010static int bmesh_restore(bContext *C, Depsgraph &depsgraph, StepData &step_data, Object &object)
1011{
1012 SculptSession &ss = *object.runtime->sculpt_session;
1013 switch (step_data.type) {
1014 case Type::DyntopoBegin:
1015 BKE_sculptsession_update_for_edit(&depsgraph, &object, false);
1016 bmesh_handle_dyntopo_begin(C, step_data, object);
1017 return true;
1018
1019 case Type::DyntopoEnd:
1020 BKE_sculptsession_update_for_edit(&depsgraph, &object, false);
1021 bmesh_handle_dyntopo_end(C, step_data, object);
1022 return true;
1023 default:
1024 if (ss.bm_log) {
1025 BKE_sculptsession_update_for_edit(&depsgraph, &object, false);
1026 bmesh_restore_generic(step_data, object);
1027 return true;
1028 }
1029 break;
1030 }
1031
1032 return false;
1033}
1034
1036{
1037 restore_geometry_data(&step_data.bmesh.geometry_enter, &mesh);
1038}
1039
1041{
1042 return get_step_data()->bmesh.bm_entry;
1043}
1044
1045/* Geometry updates (such as Apply Base, for example) will re-evaluate the object and refine its
1046 * Subdiv descriptor. Upon undo it is required that mesh, grids, and subdiv all stay consistent
1047 * with each other. This means that when geometry coordinate changes the undo should refine the
1048 * subdiv to the new coarse mesh coordinates. Tricky part is: this needs to happen without using
1049 * dependency graph tag: tagging object for geometry update will either loose sculpted data from
1050 * the sculpt grids, or will wrongly "commit" them to the CD_MDISPS.
1051 *
1052 * So what we do instead is do minimum object evaluation to get base mesh coordinates for the
1053 * multires modifier input. While this is expensive, it is less expensive than dependency graph
1054 * evaluation and is only happening when geometry coordinates changes on undo.
1055 *
1056 * Note that the dependency graph is ensured to be evaluated prior to the undo step is decoded,
1057 * so if the object's modifier stack references other object it is all fine. */
1058static void refine_subdiv(Depsgraph *depsgraph,
1059 const SculptSession &ss,
1060 Object &object,
1062{
1064 depsgraph, &object, ss.multires_modifier);
1065
1066 bke::subdiv::eval_refine_from_mesh(subdiv, id_cast<const Mesh *>(object.data), deformed_verts);
1067}
1068
1069static void restore_list(bContext *C, Depsgraph *depsgraph, StepData &step_data)
1070{
1072 const Main *bmain = CTX_data_main(C);
1073 Scene *scene = CTX_data_scene(C);
1074 ViewLayer *view_layer = CTX_data_view_layer(C);
1076 BKE_view_layer_synced_ensure(*bmain, scene, view_layer);
1077 Object &object = *BKE_view_layer_active_object_get(view_layer);
1078 if (step_data.object_name != object.id.name) {
1079 return;
1080 }
1081 SculptSession &ss = *object.runtime->sculpt_session;
1082 bke::pbvh::Tree &pbvh = bke::object::pbvh_ensure(*depsgraph, object);
1083
1084 /* Restore pivot. */
1085 ss.pivot_pos = step_data.pivot_pos;
1086 ss.pivot_rot = step_data.pivot_rot;
1087
1088 if (bmesh_restore(C, *depsgraph, step_data, object)) {
1089 return;
1090 }
1091
1092 /* Switching to sculpt mode does not push a particular type.
1093 * See #124484. */
1094 /* TODO: Add explicit type for switching into Sculpt Mode. */
1095 if (step_data.type == Type::None && step_data.nodes.is_empty()) {
1096 return;
1097 }
1098
1099 /* Adding multires via the `subdivision_set` operator results in the subsequent undo step
1100 * not correctly performing a global undo step; we exit early here to avoid crashing.
1101 * See: #131478 */
1102 const bool multires_undo_step = use_multires_undo(step_data, ss);
1103 if ((multires_undo_step && pbvh.type() != bke::pbvh::Type::Grids) ||
1104 (!multires_undo_step && pbvh.type() != bke::pbvh::Type::Mesh))
1105 {
1106 CLOG_WARN(&LOG,
1107 "Undo step type and sculpt geometry type do not match: skipping undo state restore");
1108 return;
1109 }
1110
1111 const bool tag_update = ID_REAL_USERS(object.data) > 1 ||
1112 !BKE_sculptsession_use_pbvh_draw(&object, rv3d) || ss.shapekey_active ||
1114
1115 switch (step_data.type) {
1116 case Type::None: {
1118 break;
1119 }
1120 case Type::Position: {
1121 IndexMaskMemory memory;
1122 const IndexMask node_mask = bke::pbvh::all_leaf_nodes(pbvh, memory);
1123
1124 BKE_sculptsession_update_for_edit(depsgraph, &object, false);
1125 if (!topology_matches(step_data, object)) {
1126 return;
1127 }
1128
1129 if (use_multires_undo(step_data, ss)) {
1131 SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
1132 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
1133
1134 Array<bool> modified_grids(subdiv_ccg.grids_num, false);
1136 subdiv_ccg.positions, key, *step_data.position_step_storage, modified_grids);
1137
1138 const IndexMask changed_nodes = IndexMask::from_predicate(
1139 node_mask,
1140 memory,
1141 [&](const int i) { return indices_contain_true(modified_grids, nodes[i].grids()); },
1143 pbvh.tag_positions_changed(changed_nodes);
1145 }
1146 else {
1148 if (!restore_active_shape_key(*C, *depsgraph, step_data, object)) {
1149 return;
1150 }
1151 const Mesh &mesh = *id_cast<const Mesh *>(object.data);
1152 Array<bool> modified_verts(mesh.verts_num, false);
1153 restore_position_mesh(object, *step_data.position_step_storage, modified_verts);
1154
1155 const IndexMask changed_nodes = IndexMask::from_predicate(
1156 node_mask,
1157 memory,
1158 [&](const int i) {
1159 return indices_contain_true(modified_verts, nodes[i].all_verts());
1160 },
1162 pbvh.tag_positions_changed(changed_nodes);
1163 }
1164
1165 if (tag_update) {
1166 Mesh &mesh = *id_cast<Mesh *>(object.data);
1167 mesh.tag_positions_changed();
1169 }
1170 else {
1171 Mesh &mesh = *id_cast<Mesh *>(object.data);
1172 /* The BVH normals recalculation that will happen later (caused by
1173 * `pbvh.tag_positions_changed`) won't recalculate the face corner normals.
1174 * We need to manually clear that cache. */
1175 mesh.runtime->corner_normals_cache.tag_dirty();
1176 }
1177 pbvh.update_bounds(*depsgraph, object);
1179 break;
1180 }
1181 case Type::HideVert: {
1182 IndexMaskMemory memory;
1183 const IndexMask node_mask = bke::pbvh::all_leaf_nodes(pbvh, memory);
1184
1185 BKE_sculptsession_update_for_edit(depsgraph, &object, false);
1186 if (!topology_matches(step_data, object)) {
1187 return;
1188 }
1189
1190 if (use_multires_undo(step_data, ss)) {
1192 SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
1193 Array<bool> modified_grids(subdiv_ccg.grids_num, false);
1194 for (std::unique_ptr<Node> &unode : step_data.nodes) {
1195 restore_vert_visibility_grids(subdiv_ccg, *unode, modified_grids);
1196 }
1197 const IndexMask changed_nodes = IndexMask::from_predicate(
1198 node_mask,
1199 memory,
1200 [&](const int i) { return indices_contain_true(modified_grids, nodes[i].grids()); },
1202 pbvh.tag_visibility_changed(changed_nodes);
1203 }
1204 else {
1206 const Mesh &mesh = *id_cast<const Mesh *>(object.data);
1207 Array<bool> modified_verts(mesh.verts_num, false);
1208 for (std::unique_ptr<Node> &unode : step_data.nodes) {
1209 restore_vert_visibility_mesh(object, *unode, modified_verts);
1210 }
1211 const IndexMask changed_nodes = IndexMask::from_predicate(
1212 node_mask,
1213 memory,
1214 [&](const int i) {
1215 return indices_contain_true(modified_verts, nodes[i].all_verts());
1216 },
1218 pbvh.tag_visibility_changed(changed_nodes);
1219 }
1220
1222 pbvh.update_visibility(object);
1223 if (BKE_sculpt_multires_active(scene, &object)) {
1225 }
1226 break;
1227 }
1228 case Type::HideFace: {
1229 IndexMaskMemory memory;
1230 const IndexMask node_mask = bke::pbvh::all_leaf_nodes(pbvh, memory);
1231
1232 BKE_sculptsession_update_for_edit(depsgraph, &object, false);
1233 if (!topology_matches(step_data, object)) {
1234 return;
1235 }
1236
1237 const Mesh &mesh = *id_cast<const Mesh *>(object.data);
1238 Array<bool> modified_faces(mesh.faces_num, false);
1239 for (std::unique_ptr<Node> &unode : step_data.nodes) {
1240 restore_hidden_face(object, *unode, modified_faces);
1241 }
1242
1243 if (use_multires_undo(step_data, ss)) {
1245 const SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
1246 const IndexMask changed_nodes = IndexMask::from_predicate(
1247 node_mask,
1248 memory,
1249 [&](const int i) {
1250 Vector<int> faces_vector;
1252 subdiv_ccg, nodes[i], faces_vector);
1253 return indices_contain_true(modified_faces, faces);
1254 },
1256 pbvh.tag_visibility_changed(changed_nodes);
1257 }
1258 else {
1260 const IndexMask changed_nodes = IndexMask::from_predicate(
1261 node_mask,
1262 memory,
1263 [&](const int i) { return indices_contain_true(modified_faces, nodes[i].faces()); },
1265 pbvh.tag_visibility_changed(changed_nodes);
1266 }
1267
1269 pbvh.update_visibility(object);
1270 break;
1271 }
1272 case Type::Mask: {
1273 IndexMaskMemory memory;
1274 const IndexMask node_mask = bke::pbvh::all_leaf_nodes(pbvh, memory);
1275
1276 BKE_sculptsession_update_for_edit(depsgraph, &object, false);
1277 if (!topology_matches(step_data, object)) {
1278 return;
1279 }
1280
1281 if (use_multires_undo(step_data, ss)) {
1283 Array<bool> modified_grids(ss.subdiv_ccg->grids_num, false);
1284 for (std::unique_ptr<Node> &unode : step_data.nodes) {
1285 restore_mask_grids(object, *unode, modified_grids);
1286 }
1287 const IndexMask changed_nodes = IndexMask::from_predicate(
1288 node_mask,
1289 memory,
1290 [&](const int i) { return indices_contain_true(modified_grids, nodes[i].grids()); },
1292 bke::pbvh::update_mask_grids(*ss.subdiv_ccg, changed_nodes, pbvh);
1293 pbvh.tag_masks_changed(changed_nodes);
1294 }
1295 else {
1297 const Mesh &mesh = *id_cast<const Mesh *>(object.data);
1298 Array<bool> modified_verts(mesh.verts_num, false);
1299 for (std::unique_ptr<Node> &unode : step_data.nodes) {
1300 restore_mask_mesh(object, *unode, modified_verts);
1301 }
1302 const IndexMask changed_nodes = IndexMask::from_predicate(
1303 node_mask,
1304 memory,
1305 [&](const int i) {
1306 return indices_contain_true(modified_verts, nodes[i].all_verts());
1307 },
1309 bke::pbvh::update_mask_mesh(mesh, changed_nodes, pbvh);
1310 pbvh.tag_masks_changed(changed_nodes);
1311 }
1312 break;
1313 }
1314 case Type::FaceSet: {
1315 IndexMaskMemory memory;
1316 const IndexMask node_mask = bke::pbvh::all_leaf_nodes(pbvh, memory);
1317
1318 BKE_sculptsession_update_for_edit(depsgraph, &object, false);
1319 if (!topology_matches(step_data, object)) {
1320 return;
1321 }
1322
1323 const Mesh &mesh = *id_cast<const Mesh *>(object.data);
1324 Array<bool> modified_faces(mesh.faces_num, false);
1325 for (std::unique_ptr<Node> &unode : step_data.nodes) {
1326 restore_face_sets(object, *unode, modified_faces);
1327 }
1328 if (use_multires_undo(step_data, ss)) {
1330 const SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
1331 const IndexMask changed_nodes = IndexMask::from_predicate(
1332 node_mask,
1333 memory,
1334 [&](const int i) {
1335 Vector<int> faces_vector;
1337 subdiv_ccg, nodes[i], faces_vector);
1338 return indices_contain_true(modified_faces, faces);
1339 },
1341 pbvh.tag_face_sets_changed(changed_nodes);
1342 }
1343 else {
1345 const IndexMask changed_nodes = IndexMask::from_predicate(
1346 node_mask,
1347 memory,
1348 [&](const int i) { return indices_contain_true(modified_faces, nodes[i].faces()); },
1350 pbvh.tag_face_sets_changed(changed_nodes);
1351 }
1352 break;
1353 }
1354 case Type::Color: {
1355 IndexMaskMemory memory;
1356 const IndexMask node_mask = bke::pbvh::all_leaf_nodes(pbvh, memory);
1357
1358 BKE_sculptsession_update_for_edit(depsgraph, &object, false);
1359 if (!topology_matches(step_data, object)) {
1360 return;
1361 }
1362
1364
1365 const Mesh &mesh = *id_cast<const Mesh *>(object.data);
1366 Array<bool> modified_verts(mesh.verts_num, false);
1367 restore_color(object, step_data, modified_verts);
1368 const IndexMask changed_nodes = IndexMask::from_predicate(
1369 node_mask,
1370 memory,
1371 [&](const int i) { return indices_contain_true(modified_verts, nodes[i].all_verts()); },
1373 pbvh.tag_attribute_changed(changed_nodes, mesh.active_color_attribute);
1374 break;
1375 }
1376 case Type::Geometry: {
1377 BLI_assert(!ss.bm);
1378
1379 restore_geometry(step_data, object);
1381 if (SubdivCCG *subdiv_ccg = ss.subdiv_ccg) {
1382 refine_subdiv(depsgraph, ss, object, subdiv_ccg->subdiv);
1383 }
1384 break;
1385 }
1386 case Type::DyntopoBegin:
1387 case Type::DyntopoEnd:
1388 /* Handled elsewhere. */
1390 break;
1391 }
1392
1394 if (tag_update) {
1396 }
1397}
1398
1399static void free_step_data(StepData &step_data)
1400{
1403 geometry_free_data(&step_data.bmesh.geometry_enter);
1404 if (step_data.bmesh.bm_entry) {
1405 BM_log_entry_drop(step_data.bmesh.bm_entry);
1406 }
1407 step_data.~StepData();
1408}
1409
1416static const Node *get_node(const bke::pbvh::Node *node, const Type type)
1417{
1418 StepData *step_data = get_step_data();
1419 if (!step_data) {
1420 return nullptr;
1421 }
1422 if (step_data->type != type) {
1423 return nullptr;
1424 }
1425 /* This access does not need to be locked because this function is not expected to be called
1426 * while the per-node undo data is being pushed. In other words, this must not be called
1427 * concurrently with #push_node. */
1428 std::unique_ptr<Node> *node_ptr = step_data->undo_nodes_by_pbvh_node.lookup_ptr(node);
1429 if (!node_ptr) {
1430 return nullptr;
1431 }
1432 return node_ptr->get();
1433}
1434
1435static void store_vert_visibility_grids(const SubdivCCG &subdiv_ccg,
1437 Node &unode)
1438{
1440 const BitGroupVector<> &grid_hidden = subdiv_ccg.grid_hidden;
1441 if (grid_hidden.is_empty()) {
1442 return;
1443 }
1444
1445 const Span<int> grid_indices = node.grids();
1446 unode.grid_hidden = BitGroupVector<0>(grid_indices.size(), grid_hidden.group_size());
1447 for (const int i : grid_indices.index_range()) {
1448 unode.grid_hidden[i].copy_from(grid_hidden[grid_indices[i]]);
1449 }
1450}
1451
1452static void store_positions_mesh(const Depsgraph &depsgraph, const Object &object, Node &unode)
1453{
1455 const SculptSession &ss = *object.runtime->sculpt_session;
1457 unode.vert_indices.as_span(),
1458 unode.position.as_mutable_span());
1460 unode.vert_indices.as_span(),
1461 unode.normal.as_mutable_span());
1462
1463 if (ss.deform_modifiers_active) {
1464 const Mesh &mesh = *id_cast<const Mesh *>(object.data);
1465 const Span<float3> orig_positions = ss.shapekey_active ? Span(static_cast<const float3 *>(
1466 ss.shapekey_active->data),
1467 mesh.verts_num) :
1468 mesh.vert_positions();
1469
1471 orig_positions, unode.vert_indices.as_span(), unode.orig_position.as_mutable_span());
1472 }
1473}
1474
1475static void store_positions_grids(const SubdivCCG &subdiv_ccg, Node &unode)
1476{
1479 subdiv_ccg, subdiv_ccg.positions.as_span(), unode.grids, unode.position.as_mutable_span());
1481 subdiv_ccg, subdiv_ccg.normals.as_span(), unode.grids, unode.normal.as_mutable_span());
1482}
1483
1485{
1487 const bke::AttributeAccessor attributes = mesh.attributes();
1488 const VArraySpan<bool> hide_vert = *attributes.lookup<bool>(".hide_vert",
1490 if (hide_vert.is_empty()) {
1491 return;
1492 }
1493
1494 const Span<int> verts = static_cast<const bke::pbvh::MeshNode &>(node).all_verts();
1495 for (const int i : verts.index_range()) {
1496 unode.vert_hidden[i].set(hide_vert[verts[i]]);
1497 }
1498}
1499
1500static void store_face_visibility(const Mesh &mesh, Node &unode)
1501{
1503 const bke::AttributeAccessor attributes = mesh.attributes();
1504 const VArraySpan<bool> hide_poly = *attributes.lookup<bool>(".hide_poly", bke::AttrDomain::Face);
1505 if (hide_poly.is_empty()) {
1506 unode.face_hidden.fill(false);
1507 return;
1508 }
1509 const Span<int> faces = unode.face_indices;
1510 for (const int i : faces.index_range()) {
1511 unode.face_hidden[i].set(hide_poly[faces[i]]);
1512 }
1513}
1514
1515static void store_mask_mesh(const Mesh &mesh, Node &unode)
1516{
1518 const bke::AttributeAccessor attributes = mesh.attributes();
1519 const VArraySpan mask = *attributes.lookup<float>(".sculpt_mask", bke::AttrDomain::Point);
1520 if (mask.is_empty()) {
1521 unode.mask.fill(0.0f);
1522 }
1523 else {
1525 }
1526}
1527
1528static void store_mask_grids(const SubdivCCG &subdiv_ccg, Node &unode)
1529{
1531 if (!subdiv_ccg.masks.is_empty()) {
1533 subdiv_ccg, subdiv_ccg.masks.as_span(), unode.grids, unode.mask.as_mutable_span());
1534 }
1535 else {
1536 unode.mask.fill(0.0f);
1537 }
1538}
1539
1540static void store_color(const Mesh &mesh, const bke::pbvh::MeshNode &node, Node &unode)
1541{
1543 const OffsetIndices<int> faces = mesh.faces();
1544 const Span<int> corner_verts = mesh.corner_verts();
1545 const GroupedSpan<int> vert_to_face_map = mesh.vert_to_face_map();
1547 const GVArraySpan colors(*color_attribute);
1548
1549 /* NOTE: even with loop colors we still store (derived)
1550 * vertex colors for original data lookup. */
1551 const Span<int> verts = node.verts();
1552 unode.col.reinitialize(verts.size());
1554 faces, corner_verts, vert_to_face_map, colors, color_attribute.domain, verts, unode.col);
1555
1556 if (color_attribute.domain == bke::AttrDomain::Corner) {
1557 for (const int face : node.faces()) {
1558 for (const int corner : faces[face]) {
1559 unode.corner_indices.append(corner);
1560 }
1561 }
1562 unode.loop_col.reinitialize(unode.corner_indices.size());
1563 color::gather_colors(colors, unode.corner_indices, unode.loop_col);
1564 }
1565}
1566
1568{
1569 if (!step_data.geometry_original.is_initialized) {
1570 return &step_data.geometry_original;
1571 }
1572
1574
1575 return &step_data.geometry_modified;
1576}
1577
1578static void geometry_push(const Object &object)
1579{
1580 StepData *step_data = get_step_data();
1581
1582 step_data->type = Type::Geometry;
1583
1584 NodeGeometry *geometry = geometry_get(*step_data);
1586}
1587
1588static void store_face_sets(const Mesh &mesh, Node &unode)
1589{
1591 const bke::AttributeAccessor attributes = mesh.attributes();
1592 const VArraySpan face_sets = *attributes.lookup<int>(".sculpt_face_set", bke::AttrDomain::Face);
1593 if (face_sets.is_empty()) {
1594 unode.face_sets.fill(1);
1595 }
1596 else {
1597 gather_data_mesh(face_sets, unode.face_indices.as_span(), unode.face_sets.as_mutable_span());
1598 }
1599}
1600
1601static void fill_node_data_mesh(const Depsgraph &depsgraph,
1602 const Object &object,
1604 const Type type,
1605 Node &unode)
1606{
1607 const SculptSession &ss = *object.runtime->sculpt_session;
1608 const Mesh &mesh = *id_cast<Mesh *>(object.data);
1609
1610 unode.vert_indices = node.all_verts();
1611 unode.unique_verts_num = node.verts().size();
1612
1613 const int verts_num = unode.vert_indices.size();
1614
1615 if (ELEM(type, Type::FaceSet, Type::HideFace)) {
1616 unode.face_indices = node.faces();
1617 }
1618
1619 switch (type) {
1620 case Type::None:
1622 break;
1623 case Type::Position: {
1624 unode.position.reinitialize(verts_num);
1625 /* Needed for original data lookup. */
1626 unode.normal.reinitialize(verts_num);
1627 if (ss.deform_modifiers_active) {
1628 unode.orig_position.reinitialize(verts_num);
1629 }
1630 store_positions_mesh(depsgraph, object, unode);
1631 break;
1632 }
1633 case Type::HideVert: {
1634 unode.vert_hidden.resize(unode.vert_indices.size());
1636 break;
1637 }
1638 case Type::HideFace: {
1639 unode.face_hidden.resize(unode.face_indices.size());
1641 break;
1642 }
1643 case Type::Mask: {
1644 unode.mask.reinitialize(verts_num);
1645 store_mask_mesh(mesh, unode);
1646 break;
1647 }
1648 case Type::Color: {
1649 store_color(mesh, node, unode);
1650 break;
1651 }
1652 case Type::DyntopoBegin:
1653 case Type::DyntopoEnd:
1654 /* Dyntopo should be handled elsewhere. */
1656 break;
1657 case Type::Geometry:
1658 /* See #geometry_push. */
1660 break;
1661 case Type::FaceSet: {
1662 unode.face_sets.reinitialize(unode.face_indices.size());
1663 store_face_sets(mesh, unode);
1664 break;
1665 }
1666 }
1667}
1668
1669static void fill_node_data_grids(const Object &object,
1671 const Type type,
1672 Node &unode)
1673{
1674 const SculptSession &ss = *object.runtime->sculpt_session;
1675 const Mesh &base_mesh = *id_cast<const Mesh *>(object.data);
1676 const SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
1677
1678 unode.grids = node.grids();
1679
1680 const int grid_area = subdiv_ccg.grid_size * subdiv_ccg.grid_size;
1681 const int verts_num = unode.grids.size() * grid_area;
1682
1683 if (ELEM(type, Type::FaceSet, Type::HideFace)) {
1685 }
1686
1687 switch (type) {
1688 case Type::None:
1690 break;
1691 case Type::Position: {
1692 unode.position.reinitialize(verts_num);
1693 /* Needed for original data lookup. */
1694 unode.normal.reinitialize(verts_num);
1695 store_positions_grids(subdiv_ccg, unode);
1696 break;
1697 }
1698 case Type::HideVert: {
1699 store_vert_visibility_grids(subdiv_ccg, node, unode);
1700 break;
1701 }
1702 case Type::HideFace: {
1703 unode.face_hidden.resize(unode.face_indices.size());
1704 store_face_visibility(base_mesh, unode);
1705 break;
1706 }
1707 case Type::Mask: {
1708 unode.mask.reinitialize(verts_num);
1709 store_mask_grids(subdiv_ccg, unode);
1710 break;
1711 }
1712 case Type::Color: {
1714 break;
1715 }
1716 case Type::DyntopoBegin:
1717 case Type::DyntopoEnd:
1718 /* Dyntopo should be handled elsewhere. */
1720 break;
1721 case Type::Geometry:
1722 /* See #geometry_push. */
1724 break;
1725 case Type::FaceSet: {
1726 unode.face_sets.reinitialize(unode.face_indices.size());
1727 store_face_sets(base_mesh, unode);
1728 break;
1729 }
1730 }
1731}
1732
1737BLI_NOINLINE static void bmesh_push(const Object &object,
1739 Type type)
1740{
1741 StepData *step_data = get_step_data();
1742 const SculptSession &ss = *object.runtime->sculpt_session;
1743
1744 std::scoped_lock lock(step_data->nodes_mutex);
1745
1746 if (step_data->nodes.is_empty()) {
1747 /* We currently need to append data here so that the overall undo system knows to indicate that
1748 * data should be flushed to the memfile */
1749 /* TODO: Once we store entering Sculpt Mode as a specific type of action, we can remove this
1750 * call. */
1751 step_data->nodes.append(std::make_unique<Node>());
1752
1753 step_data->type = type;
1754
1755 if (type == Type::DyntopoEnd) {
1756 step_data->bmesh.bm_entry = BM_log_entry_add(ss.bm_log);
1758 }
1759 else if (type == Type::DyntopoBegin) {
1760 /* Store a copy of the mesh's current vertices, loops, and
1761 * faces. A full copy like this is needed because entering
1762 * dynamic-topology immediately does topological edits
1763 * (converting faces to triangles) that the BMLog can't
1764 * fully restore from. */
1765 NodeGeometry *geometry = &step_data->bmesh.geometry_enter;
1767
1768 step_data->bmesh.bm_entry = BM_log_entry_add(ss.bm_log);
1769 BM_log_all_added(ss.bm, ss.bm_log);
1770 }
1771 else {
1772 step_data->bmesh.bm_entry = BM_log_entry_add(ss.bm_log);
1773 }
1774 }
1775
1776 if (node) {
1777 const int cd_vert_mask_offset = CustomData_get_offset_named(
1778 &ss.bm->vdata, CD_PROP_FLOAT, ".sculpt_mask");
1779
1780 /* The vertices and node aren't changed, though pointers to them are stored in the log. */
1781 bke::pbvh::BMeshNode *node_mut = const_cast<bke::pbvh::BMeshNode *>(node);
1782
1783 switch (type) {
1784 case Type::None:
1786 break;
1787 case Type::Position:
1788 case Type::Mask:
1789 /* Before any vertex values get modified, ensure their
1790 * original positions are logged. */
1791 for (BMVert *vert : BKE_pbvh_bmesh_node_unique_verts(node_mut)) {
1792 BM_log_vert_before_modified(ss.bm_log, vert, cd_vert_mask_offset);
1793 }
1794 for (BMVert *vert : BKE_pbvh_bmesh_node_other_verts(node_mut)) {
1795 BM_log_vert_before_modified(ss.bm_log, vert, cd_vert_mask_offset);
1796 }
1797 break;
1798
1799 case Type::HideFace:
1800 case Type::HideVert: {
1801 for (BMVert *vert : BKE_pbvh_bmesh_node_unique_verts(node_mut)) {
1802 BM_log_vert_before_modified(ss.bm_log, vert, cd_vert_mask_offset);
1803 }
1804 for (BMVert *vert : BKE_pbvh_bmesh_node_other_verts(node_mut)) {
1805 BM_log_vert_before_modified(ss.bm_log, vert, cd_vert_mask_offset);
1806 }
1807
1808 for (BMFace *f : BKE_pbvh_bmesh_node_faces(node_mut)) {
1810 }
1811 break;
1812 }
1813
1814 case Type::DyntopoBegin:
1815 case Type::DyntopoEnd:
1816 case Type::Geometry:
1817 case Type::FaceSet:
1818 case Type::Color:
1819 break;
1820 }
1821 }
1822}
1823
1828static Node *ensure_node(StepData &step_data, const bke::pbvh::Node &node, bool &r_new)
1829{
1830 std::scoped_lock lock(step_data.nodes_mutex);
1831 r_new = false;
1832 std::unique_ptr<Node> &unode = step_data.undo_nodes_by_pbvh_node.lookup_or_add_cb(&node, [&]() {
1833 std::unique_ptr<Node> new_unode = std::make_unique<Node>();
1834 r_new = true;
1835 return new_unode;
1836 });
1837 return unode.get();
1838}
1839
1840void push_node(const Depsgraph &depsgraph,
1841 const Object &object,
1842 const bke::pbvh::Node *node,
1843 const Type type)
1844{
1845 SculptSession &ss = *object.runtime->sculpt_session;
1846 const bke::pbvh::Tree &pbvh = *bke::object::pbvh_get(object);
1847 if (ss.bm || ELEM(type, Type::DyntopoBegin, Type::DyntopoEnd)) {
1848 bmesh_push(object, static_cast<const bke::pbvh::BMeshNode *>(node), type);
1849 return;
1850 }
1851
1852 StepData *step_data = get_step_data();
1853 BLI_assert(ELEM(step_data->type, Type::None, type));
1854 step_data->type = type;
1855
1856 bool newly_added;
1857 Node *unode = ensure_node(*step_data, *node, newly_added);
1858 if (!newly_added) {
1859 /* The node was already filled with data for this undo step. */
1860 return;
1861 }
1862
1863 ss.needs_flush_to_id = true;
1864
1865 switch (pbvh.type()) {
1868 depsgraph, object, static_cast<const bke::pbvh::MeshNode &>(*node), type, *unode);
1869 break;
1871 fill_node_data_grids(object, static_cast<const bke::pbvh::GridsNode &>(*node), type, *unode);
1872 break;
1875 break;
1876 }
1877}
1878
1879void push_nodes(const Depsgraph &depsgraph,
1880 Object &object,
1881 const IndexMask &node_mask,
1882 const Type type)
1883{
1885 SculptSession &ss = *object.runtime->sculpt_session;
1886
1887 ss.needs_flush_to_id = true;
1888
1889 const bke::pbvh::Tree &pbvh = *bke::object::pbvh_get(object);
1890 if (ss.bm || ELEM(type, Type::DyntopoBegin, Type::DyntopoEnd)) {
1892 node_mask.foreach_index([&](const int i) { bmesh_push(object, &nodes[i], type); });
1893 return;
1894 }
1895
1896 StepData *step_data = get_step_data();
1897 BLI_assert(ELEM(step_data->type, Type::None, type));
1898 step_data->type = type;
1899
1900 switch (pbvh.type()) {
1901 case bke::pbvh::Type::Mesh: {
1904 node_mask.foreach_index([&](const int i) {
1905 bool newly_added;
1906 Node *unode = ensure_node(*step_data, nodes[i], newly_added);
1907 if (newly_added) {
1908 nodes_to_fill.append({&nodes[i], unode});
1909 }
1910 });
1911 threading::parallel_for(nodes_to_fill.index_range(), 1, [&](const IndexRange range) {
1912 for (const auto &[node, unode] : nodes_to_fill.as_span().slice(range)) {
1913 fill_node_data_mesh(depsgraph, object, *node, type, *unode);
1914 }
1915 });
1916 break;
1917 }
1921 node_mask.foreach_index([&](const int i) {
1922 bool newly_added;
1923 Node *unode = ensure_node(*step_data, nodes[i], newly_added);
1924 if (newly_added) {
1925 nodes_to_fill.append({&nodes[i], unode});
1926 }
1927 });
1928 threading::parallel_for(nodes_to_fill.index_range(), 1, [&](const IndexRange range) {
1929 for (const auto &[node, unode] : nodes_to_fill.as_span().slice(range)) {
1930 fill_node_data_grids(object, *node, type, *unode);
1931 }
1932 });
1933 break;
1934 }
1937 break;
1938 }
1939 }
1940}
1941
1942static void save_active_attribute(Object &object, SculptAttrRef *attr)
1943{
1945 attr->was_set = true;
1946 attr->domain = std::nullopt;
1947 attr->name = "";
1948 if (!mesh) {
1949 return;
1950 }
1951 const char *name = mesh->active_color_attribute;
1952 if (!name) {
1953 return;
1954 }
1955 const bke::AttributeAccessor attributes = mesh->attributes();
1956 const std::optional<bke::AttributeMetaData> meta_data = attributes.lookup_meta_data(name);
1957 if (!bke::mesh::is_color_attribute(meta_data)) {
1958 return;
1959 }
1960 attr->domain = meta_data->domain;
1961 attr->name = name;
1962 attr->type = meta_data->data_type;
1963}
1964
1970{
1971 us->data.object_name = ob.id.name;
1972
1973 if (!us->active_color_start.was_set) {
1975 }
1976
1977 /* Set end attribute in case push_end is not called,
1978 * so we don't end up with corrupted state.
1979 */
1980 if (!us->active_color_end.was_set) {
1982 us->active_color_end.was_set = false;
1983 }
1984
1985 const SculptSession &ss = *ob.runtime->sculpt_session;
1986
1987 us->data.pivot_pos = ss.pivot_pos;
1988 us->data.pivot_rot = ss.pivot_rot;
1989
1990 if (const KeyBlock *key = BKE_keyblock_from_object(&ob)) {
1991 us->data.active_shape_key_name = key->name;
1992 }
1993}
1994
1995void push_begin_ex(const Scene & /*scene*/, Object &ob, const char *name)
1996{
1997 UndoStack *ustack = ED_undo_stack_get();
1998
1999 /* If possible, we need to tag the object and its geometry data as 'changed in the future' in
2000 * the previous undo step if it's a memfile one. */
2003
2004 /* Special case, we never read from this. */
2005 bContext *C = nullptr;
2006
2007 SculptUndoStep *us = reinterpret_cast<SculptUndoStep *>(
2009
2010 const SculptSession &ss = *ob.runtime->sculpt_session;
2012
2013 save_common_data(ob, us);
2014
2015 switch (pbvh.type()) {
2016 case bke::pbvh::Type::Mesh: {
2017 const Mesh &mesh = *id_cast<const Mesh *>(ob.data);
2018 us->data.mesh.verts_num = mesh.verts_num;
2019 us->data.mesh.corners_num = mesh.corners_num;
2020 break;
2021 }
2023 us->data.grids.grids_num = ss.subdiv_ccg->grids_num;
2024 us->data.grids.grid_size = ss.subdiv_ccg->grid_size;
2025 break;
2026 }
2028 break;
2029 }
2030 }
2031}
2032
2033void push_begin(const Scene &scene, Object &ob, const wmOperator *op)
2034{
2035 push_begin_ex(scene, ob, op->type->name);
2036}
2037
2038void push_enter_sculpt_mode(const Scene & /*scene*/, Object &ob, const wmOperator *op)
2039{
2040 UndoStack *ustack = ED_undo_stack_get();
2041
2042 /* If possible, we need to tag the object and its geometry data as 'changed in the future' in
2043 * the previous undo step if it's a memfile one. */
2046
2047 /* Special case, we never read from this. */
2048 bContext *C = nullptr;
2049
2050 SculptUndoStep *us = reinterpret_cast<SculptUndoStep *>(
2052 save_common_data(ob, us);
2053}
2054
2055static size_t node_size_in_bytes(const Node &node)
2056{
2057 size_t size = sizeof(Node);
2058 size += node.position.as_span().size_in_bytes();
2059 size += node.orig_position.as_span().size_in_bytes();
2060 size += node.normal.as_span().size_in_bytes();
2061 size += node.col.as_span().size_in_bytes();
2062 size += node.mask.as_span().size_in_bytes();
2063 size += node.loop_col.as_span().size_in_bytes();
2064 size += node.vert_indices.as_span().size_in_bytes();
2065 size += node.corner_indices.as_span().size_in_bytes();
2066 size += node.vert_hidden.size() / 8;
2067 size += node.face_hidden.size() / 8;
2068 size += node.grids.as_span().size_in_bytes();
2069 size += node.grid_hidden.all_bits().size() / 8;
2070 size += node.face_sets.as_span().size_in_bytes();
2071 size += node.face_indices.as_span().size_in_bytes();
2072 return size;
2073}
2074
2075void push_end_ex(Object &ob, const bool use_nested_undo)
2076{
2077 StepData *step_data = get_step_data();
2078
2079 /* Move undo node storage from map to vector. */
2080 step_data->nodes.reserve(step_data->undo_nodes_by_pbvh_node.size());
2081 for (std::unique_ptr<Node> &node : step_data->undo_nodes_by_pbvh_node.values()) {
2082 step_data->nodes.append(std::move(node));
2083 }
2084 step_data->undo_nodes_by_pbvh_node.clear();
2085
2086 /* We don't need normals in the undo stack. */
2087 for (std::unique_ptr<Node> &unode : step_data->nodes) {
2088 unode->normal = {};
2089 }
2090 /* TODO: When #Node.orig_positions is stored, #Node.positions is unnecessary, don't keep it in
2091 * the stored undo step. In the future the stored undo step should use a different format with
2092 * just one positions array that has a different semantic meaning depending on whether there are
2093 * deform modifiers. */
2094
2095 if (step_data->type == Type::Position) {
2096 step_data->position_step_storage = std::make_unique<PositionUndoStorage>(*step_data);
2097 }
2098 else {
2100 step_data->nodes.index_range(),
2101 16,
2102 0,
2103 [&](const IndexRange range, size_t size) {
2104 for (const int i : range) {
2105 size += node_size_in_bytes(*step_data->nodes[i]);
2106 }
2107 return size;
2108 },
2109 std::plus<size_t>());
2110 }
2111
2112 /* We could remove this and enforce all callers run in an operator using 'OPTYPE_UNDO'. */
2113 wmWindowManager *wm = G_MAIN->wm.first();
2114 if (wm->op_undo_depth == 0 || use_nested_undo) {
2115 UndoStack *ustack = ED_undo_stack_get();
2116 BKE_undosys_step_push(ustack, nullptr, nullptr, UndoEncodeHints::None);
2117 if (wm->op_undo_depth == 0) {
2119 }
2121 }
2122
2123 UndoStack *ustack = ED_undo_stack_get();
2124 SculptUndoStep *us = reinterpret_cast<SculptUndoStep *>(
2126
2127 save_active_attribute(ob, &us->active_color_end);
2128}
2129
2131{
2132 push_end_ex(ob, false);
2133}
2134
2135/* -------------------------------------------------------------------- */
2138
2139static void set_active_layer(bContext *C, const SculptAttrRef *attr_ref)
2140{
2141 if (!attr_ref->domain) {
2142 return;
2143 }
2144
2147
2148 SculptAttrRef existing;
2149 save_active_attribute(*ob, &existing);
2150
2151 bke::MutableAttributeAccessor attributes = mesh->attributes_for_write();
2152
2153 /* Temporary fix for #97408. This is a fundamental
2154 * bug in the undo stack; the operator code needs to push
2155 * an extra undo step before running an operator if a
2156 * non-memfile undo system is active.
2157 *
2158 * For now, detect if the layer does exist but with a different
2159 * domain and just unconvert it.
2160 */
2161 if (const bke::GAttributeReader attr = attributes.lookup(attr_ref->name)) {
2162 if (attr.domain != attr_ref->domain ||
2163 bke::cpp_type_to_attribute_type(attr.varray.type()) != attr_ref->type)
2164 {
2167 mesh->attributes_for_write(),
2168 attr_ref->name,
2169 *attr_ref->domain,
2170 attr_ref->type,
2171 nullptr))
2172 {
2173 }
2174 }
2175 }
2176
2177 if (!attributes.contains(attr_ref->name)) {
2178 /* Memfile undo killed the layer; re-create it. */
2179 mesh->attributes_for_write().add(
2180 attr_ref->name, *attr_ref->domain, attr_ref->type, bke::AttributeInitDefaultValue());
2182 }
2183
2184 if (attributes.contains(attr_ref->name)) {
2186 }
2187}
2188
2189static void step_encode_init(bContext * /*C*/, UndoStep *us_p)
2190{
2191 SculptUndoStep *us = reinterpret_cast<SculptUndoStep *>(us_p);
2192 new (&us->data) StepData();
2193 new (&us->active_color_start) SculptAttrRef();
2194 new (&us->active_color_end) SculptAttrRef();
2195}
2196
2197static bool step_encode(bContext * /*C*/, Main *bmain, UndoStep *us_p)
2198{
2199 /* Dummy, encoding is done along the way by adding tiles
2200 * to the current 'SculptUndoStep' added by encode_init. */
2201 SculptUndoStep *us = reinterpret_cast<SculptUndoStep *>(us_p);
2202 us->step.data_size = us->data.undo_size;
2203
2204 if (us->data.type == Type::DyntopoEnd) {
2205 us->step.use_memfile_step = true;
2206 }
2207 us->step.is_applied = true;
2208
2209 /* We do not flush data when entering sculpt mode - this is currently indicated by Type::None */
2210 if (us->data.type != Type::None) {
2211 bmain->is_memfile_undo_flush_needed = true;
2212 }
2213
2214 return true;
2215}
2216
2217static void step_decode_undo_impl(bContext *C, Depsgraph *depsgraph, SculptUndoStep *us)
2218{
2219 BLI_assert(us->step.is_applied == true);
2220
2221 restore_list(C, depsgraph, us->data);
2222 us->step.is_applied = false;
2223}
2224
2225static void step_decode_redo_impl(bContext *C, Depsgraph *depsgraph, SculptUndoStep *us)
2226{
2227 BLI_assert(us->step.is_applied == false);
2228
2229 restore_list(C, depsgraph, us->data);
2230 us->step.is_applied = true;
2231}
2232
2234 Depsgraph *depsgraph,
2235 SculptUndoStep *us,
2236 const bool is_final)
2237{
2238 /* Walk forward over any applied steps of same type,
2239 * then walk back in the next loop, un-applying them. */
2240 SculptUndoStep *us_iter = us;
2241 while (us_iter->step.next && (us_iter->step.next->type == us_iter->step.type)) {
2242 if (us_iter->step.next->is_applied == false) {
2243 break;
2244 }
2245 us_iter = reinterpret_cast<SculptUndoStep *>(us_iter->step.next);
2246 }
2247
2248 while ((us_iter != us) || (!is_final && us_iter == us)) {
2249 BLI_assert(us_iter->step.type == us->step.type); /* Previous loop ensures this. */
2250
2252 step_decode_undo_impl(C, depsgraph, us_iter);
2253
2254 if (us_iter == us) {
2255 if (us_iter->step.prev && us_iter->step.prev->type == BKE_UNDOSYS_TYPE_SCULPT) {
2257 C, &reinterpret_cast<SculptUndoStep *>(us_iter->step.prev)->active_color_end);
2258 }
2259 break;
2260 }
2261
2262 us_iter = reinterpret_cast<SculptUndoStep *>(us_iter->step.prev);
2263 }
2264}
2265
2266static void step_decode_redo(bContext *C, Depsgraph *depsgraph, SculptUndoStep *us)
2267{
2268 SculptUndoStep *us_iter = us;
2269 while (us_iter->step.prev && (us_iter->step.prev->type == us_iter->step.type)) {
2270 if (us_iter->step.prev->is_applied == true) {
2271 break;
2272 }
2273 us_iter = reinterpret_cast<SculptUndoStep *>(us_iter->step.prev);
2274 }
2275 while (us_iter && (us_iter->step.is_applied == false)) {
2277 step_decode_redo_impl(C, depsgraph, us_iter);
2278
2279 if (us_iter == us) {
2281 break;
2282 }
2283 us_iter = reinterpret_cast<SculptUndoStep *>(us_iter->step.next);
2284 }
2285}
2286
2287static void step_decode(
2288 bContext *C, Main *bmain, UndoStep *us_p, const eUndoStepDir dir, const bool is_final)
2289{
2290 /* NOTE: behavior for undo/redo closely matches image undo. */
2291 BLI_assert(dir != STEP_INVALID);
2292
2293 Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
2294
2295 /* Ensure sculpt mode. */
2296 {
2297 Scene *scene = CTX_data_scene(C);
2298 ViewLayer *view_layer = CTX_data_view_layer(C);
2299 BKE_view_layer_synced_ensure(*bmain, scene, view_layer);
2300 Object *ob = BKE_view_layer_active_object_get(view_layer);
2301 if (ob && (ob->type == OB_MESH)) {
2302 if (ob->mode & (OB_MODE_SCULPT)) {
2303 /* Pass. */
2304 }
2305 else {
2306 object::mode_generic_exit(bmain, depsgraph, scene, ob);
2307
2308 /* Sculpt needs evaluated state.
2309 * NOTE: needs to be done here, as #object::mode_generic_exit will usually invalidate
2310 * (some) evaluated data. */
2311 BKE_scene_graph_evaluated_ensure(depsgraph, bmain);
2312
2313 Mesh *mesh = id_cast<Mesh *>(ob->data);
2314 /* Don't add sculpt topology undo steps when reading back undo state.
2315 * The undo steps must enter/exit for us. */
2317 object_sculpt_mode_enter(*bmain, *depsgraph, *scene, *ob, true, nullptr);
2318 }
2319
2320 if (ob->runtime->sculpt_session) {
2322 }
2323 bmain->is_memfile_undo_flush_needed = true;
2324 }
2325 else {
2326 BLI_assert(0);
2327 return;
2328 }
2329 }
2330
2331 SculptUndoStep *us = reinterpret_cast<SculptUndoStep *>(us_p);
2332 if (dir == STEP_UNDO) {
2333 step_decode_undo(C, depsgraph, us, is_final);
2334 }
2335 else if (dir == STEP_REDO) {
2336 step_decode_redo(C, depsgraph, us);
2337 }
2338}
2339
2340static void step_free(UndoStep *us_p)
2341{
2342 SculptUndoStep *us = reinterpret_cast<SculptUndoStep *>(us_p);
2343 free_step_data(us->data);
2344 us->active_color_start.~SculptAttrRef();
2345 us->active_color_end.~SculptAttrRef();
2346}
2347
2348void geometry_begin(const Scene &scene, Object &ob, const wmOperator *op)
2349{
2350 geometry_begin_ex(scene, ob, op->type->name);
2351}
2352
2353void geometry_begin_ex(const Scene & /*scene*/, Object &ob, const char *name)
2354{
2355 UndoStack *ustack = ED_undo_stack_get();
2356
2357 /* If possible, we need to tag the object and its geometry data as 'changed in the future' in
2358 * the previous undo step if it's a memfile one. */
2361
2362 /* Special case, we never read from this. */
2363 bContext *C = nullptr;
2364
2365 SculptUndoStep *us = reinterpret_cast<SculptUndoStep *>(
2367 save_common_data(ob, us);
2368 geometry_push(ob);
2369}
2370
2372{
2373 BLI_assert(node_geometry.is_initialized);
2374
2375 MemoryCount memory;
2377
2378 memory_counter.add_shared(node_geometry.face_offsets_sharing_info,
2379 sizeof(int) * (node_geometry.faces_num + 1));
2380
2385 node_geometry.attribute_storage.wrap().count_memory(memory_counter);
2386
2387 return memory.total_bytes;
2388}
2389
2390static size_t estimate_geometry_step_size(const StepData &step_data)
2391{
2392 size_t step_size = 0;
2393
2394 /* TODO: This calculation is not entirely accurate, as the current amount of memory consumed by
2395 * Sculpt Undo is not updated when elements are evicted. Further changes to the overall undo
2396 * system would be needed to measure this accurately. */
2399
2400 return step_size;
2401}
2402
2404{
2405 geometry_push(ob);
2406
2407 StepData *step_data = get_step_data();
2408 step_data->undo_size = estimate_geometry_step_size(*step_data);
2409
2410 /* We could remove this and enforce all callers run in an operator using 'OPTYPE_UNDO'. */
2411 wmWindowManager *wm = G_MAIN->wm.first();
2412 if (wm->op_undo_depth == 0) {
2413 UndoStack *ustack = ED_undo_stack_get();
2414 BKE_undosys_step_push(ustack, nullptr, nullptr, UndoEncodeHints::None);
2415 if (wm->op_undo_depth == 0) {
2417 }
2419 }
2420}
2421
2423{
2424 ut->identifier = "SCULPT";
2425 ut->poll = nullptr; /* No poll from context for now. */
2429 ut->step_free = step_free;
2430
2432
2433 ut->step_size = sizeof(SculptUndoStep);
2434}
2435
2437
2438/* -------------------------------------------------------------------- */
2457
2459{
2461 return false;
2462 }
2463
2464 const Object *object = CTX_data_active_object(C);
2465 const SculptSession *sculpt_session = object->runtime->sculpt_session;
2466
2467 return sculpt_session->multires_modifier;
2468}
2469
2471{
2472 if (!use_multires_mesh(C)) {
2473 return;
2474 }
2475
2476 const Scene &scene = *CTX_data_scene(C);
2477 Object *object = CTX_data_active_object(C);
2478
2480
2481 push_begin_ex(scene, *object, str);
2482
2483 geometry_push(*object);
2484}
2485
2487{
2488 if (!use_multires_mesh(C)) {
2489 ED_undo_push(C, str);
2490 return;
2491 }
2492
2493 Object *object = CTX_data_active_object(C);
2494
2495 geometry_push(*object);
2496
2497 push_end(*object);
2498}
2499
2501
2502} // namespace ed::sculpt_paint::undo
2503
2504namespace ed::sculpt_paint {
2505
2506std::optional<OrigPositionData> orig_position_data_lookup_mesh_all_verts(
2507 const Object & /*object*/, const bke::pbvh::MeshNode &node)
2508{
2510 if (!unode) {
2511 return std::nullopt;
2512 }
2513 return OrigPositionData{unode->position.as_span(), unode->normal.as_span()};
2514}
2515
2516std::optional<OrigPositionData> orig_position_data_lookup_mesh(const Object &object,
2518{
2519 const std::optional<OrigPositionData> result = orig_position_data_lookup_mesh_all_verts(object,
2520 node);
2521 if (!result) {
2522 return std::nullopt;
2523 }
2524 return OrigPositionData{result->positions.take_front(node.verts().size()),
2525 result->normals.take_front(node.verts().size())};
2526}
2527
2528std::optional<OrigPositionData> orig_position_data_lookup_grids(const Object & /*object*/,
2530{
2532 if (!unode) {
2533 return std::nullopt;
2534 }
2535 return OrigPositionData{unode->position.as_span(), unode->normal.as_span()};
2536}
2537
2539 const Set<BMVert *, 0> &verts,
2540 const MutableSpan<float3> positions,
2541 const MutableSpan<float3> normals)
2542{
2543 int i = 0;
2544 for (const BMVert *vert : verts) {
2545 const float *co;
2546 const float *no;
2547 BM_log_original_vert_data(&const_cast<BMLog &>(bm_log), const_cast<BMVert *>(vert), &co, &no);
2548 if (!positions.is_empty()) {
2549 positions[i] = co;
2550 }
2551 if (!normals.is_empty()) {
2552 normals[i] = no;
2553 }
2554 i++;
2555 }
2556}
2557
2558std::optional<Span<float4>> orig_color_data_lookup_mesh(const Object & /*object*/,
2560{
2562 if (!unode) {
2563 return std::nullopt;
2564 }
2565 return unode->col.as_span();
2566}
2567
2568std::optional<Span<int>> orig_face_set_data_lookup_mesh(const Object & /*object*/,
2570{
2572 if (!unode) {
2573 return std::nullopt;
2574 }
2575 return unode->face_sets.as_span();
2576}
2577
2578std::optional<Span<int>> orig_face_set_data_lookup_grids(const Object & /*object*/,
2580{
2582 if (!unode) {
2583 return std::nullopt;
2584 }
2585 return unode->face_sets.as_span();
2586}
2587
2588std::optional<Span<float>> orig_mask_data_lookup_mesh(const Object & /*object*/,
2590{
2592 if (!unode) {
2593 return std::nullopt;
2594 }
2595 return unode->mask.as_span();
2596}
2597
2598std::optional<Span<float>> orig_mask_data_lookup_grids(const Object & /*object*/,
2600{
2602 if (!unode) {
2603 return std::nullopt;
2604 }
2605 return unode->mask.as_span();
2606}
2607
2608} // namespace ed::sculpt_paint
2609
2610} // namespace blender
CustomData interface, see also DNA_customdata_types.h.
#define G_MAIN
General operations, lookup, etc. for blender objects.
#define BKE_undosys_stack_limit_steps_and_memory_defaults(ustack)
#define SCOPED_TIMER_AVERAGED(name)
Definition BLI_timeit.hh:70
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
#define ID_REAL_USERS(id)
Definition DNA_ID.h:679
Object is a sort of wrapper for general info.
#define PRF_scope(category)
#define C
Definition RandGen.cpp:29
#define ND_DATA
Definition WM_types.hh:552
#define NC_OBJECT
Definition WM_types.hh:392
volatile int lock
#define BLI_assert_unreachable()
#define BLI_NOINLINE
#define ELEM(...)
int64_t size() const
Definition BLI_array.hh:267
Span< T > as_span() const
Definition BLI_array.hh:254
MutableSpan< T > as_mutable_span()
Definition BLI_array.hh:259
IndexRange index_range() const
Definition BLI_array.hh:371
void fill(const T &value) const
Definition BLI_array.hh:283
void reinitialize(const int64_t new_size)
Definition BLI_array.hh:430
bool is_empty() const
Definition BLI_array.hh:275
static AttributeOwner from_id(ID *id)
static IndexMask from_predicate(const IndexMask &universe, LinearAllocator<> &memory, Fn &&predicate, exec_mode::Mode mode=exec_mode::parallel)
constexpr IndexRange()=default
constexpr IndexRange take_front(int64_t n) const
constexpr int64_t size() const
Definition BLI_span.hh:499
constexpr MutableSpan slice(const int64_t start, const int64_t size) const
Definition BLI_span.hh:579
constexpr bool is_empty() const
Definition BLI_span.hh:515
constexpr Span< T > as_span() const
Definition BLI_span.hh:667
constexpr void fill_indices(Span< IndexT > indices, const T &value) const
Definition BLI_span.hh:532
constexpr MutableSpan take_front(const int64_t n) const
Definition BLI_span.hh:635
constexpr int64_t size_in_bytes() const
Definition BLI_span.hh:272
constexpr const T * data() const
Definition BLI_span.hh:219
constexpr Span()=default
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr const T * end() const
Definition BLI_span.hh:228
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
constexpr const T * begin() const
Definition BLI_span.hh:224
int64_t size() const
IndexRange index_range() const
void resize(const int64_t new_size)
Span< T > as_span() const
IndexRange index_range() const
bool contains(StringRef name) const
std::optional< AttributeMetaData > lookup_meta_data(StringRef name) const
GAttributeReader lookup(const StringRef name) const
GSpanAttributeWriter lookup_or_add_for_write_span(StringRef name, AttrDomain domain, AttrType data_type, const AttributeInit &initializer=AttributeInitDefaultValue())
NonMovable(NonMovable &&other)=delete
constexpr bool is_empty() const
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
#define str(s)
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
void node_geometry(float3 orco_attr, KernelGlobals &kg, ShadingData &sd, float3 &position, float3 &normal, float3 &tangent, float3 &true_normal, float3 &incoming, float3 &parametric, float &backfacing, float &pointiness, float &random_per_island)
#define T
IndexRange grid_range(const int grid_area, const int grid)
bool is_color_attribute(const AttributeMetaData &meta_data)
pbvh::Tree & pbvh_ensure(Depsgraph &depsgraph, Object &object)
pbvh::Tree * pbvh_get(Object &object)
void update_mask_bmesh(const BMesh &bm, const IndexMask &node_mask, Tree &pbvh)
Definition pbvh.cc:1421
void update_mask_mesh(const Mesh &mesh, const IndexMask &node_mask, Tree &pbvh)
Definition pbvh.cc:1356
IndexMask all_leaf_nodes(const Tree &pbvh, IndexMaskMemory &memory)
Definition pbvh.cc:2522
Span< float3 > vert_normals_eval(const Depsgraph &depsgraph, const Object &object_orig)
Definition pbvh.cc:952
void update_mask_grids(const SubdivCCG &subdiv_ccg, const IndexMask &node_mask, Tree &pbvh)
Definition pbvh.cc:1387
void store_bounds_orig(Tree &pbvh)
Definition pbvh.cc:1332
Span< int > node_face_indices_calc_grids(const SubdivCCG &subdiv_ccg, const GridsNode &node, Vector< int > &faces)
Definition pbvh.cc:1666
Span< float3 > vert_positions_eval(const Depsgraph &depsgraph, const Object &object_orig)
Definition pbvh.cc:933
bool eval_refine_from_mesh(Subdiv *subdiv, const Mesh *mesh, Span< float3 > coarse_vert_positions)
AttrType cpp_type_to_attribute_type(const CPPType &type)
bool convert_attribute(AttributeOwner &owner, bke::MutableAttributeAccessor attributes, const StringRef name, const bke::AttrDomain dst_domain, const bke::AttrType dst_type, ReportList *reports)
void mode_generic_exit(Main *bmain, Depsgraph *depsgraph, Scene *scene, Object *ob)
bke::GSpanAttributeWriter active_color_attribute_for_write(Mesh &mesh)
void gather_colors_vert(OffsetIndices< int > faces, Span< int > corner_verts, GroupedSpan< int > vert_to_face_map, GSpan color_attribute, bke::AttrDomain color_domain, Span< int > verts, MutableSpan< float4 > r_colors)
void gather_colors(GSpan color_attribute, Span< int > indices, MutableSpan< float4 > r_colors)
void swap_gathered_colors(Span< int > indices, GMutableSpan color_attribute, MutableSpan< float4 > r_colors)
bke::GAttributeReader active_color_attribute(const Mesh &mesh)
void disable(bContext *C, undo::StepData *undo_step)
bke::SpanAttributeWriter< int > ensure_face_sets_mesh(Mesh &mesh)
void sync_all_from_faces(Object &object)
Definition paint_hide.cc:56
void filter_decompress(const Span< std::byte > src, Vector< std::byte > &buffer, Vector< T > &dst)
template void filter_decompress< float3 >(Span< std::byte >, Vector< std::byte > &, Vector< float3 > &)
template void filter_decompress< int >(Span< std::byte >, Vector< std::byte > &, Vector< int > &)
template void filter_compress< float3 >(Span< float3 >, Vector< std::byte > &, Vector< std::byte > &)
void filter_compress(const Span< T > src, Vector< std::byte > &filter_buffer, Vector< std::byte > &compress_buffer)
static void save_active_attribute(Object &object, SculptAttrRef *attr)
static void bmesh_enable(Object &object, const StepData &step_data)
void push_nodes(const Depsgraph &depsgraph, Object &object, const IndexMask &node_mask, const Type type)
static void store_vert_visibility_grids(const SubdivCCG &subdiv_ccg, const bke::pbvh::GridsNode &node, Node &unode)
static bool use_multires_mesh(bContext *C)
static void step_decode(bContext *C, Main *bmain, UndoStep *us_p, const eUndoStepDir dir, const bool is_final)
static void geometry_push(const Object &object)
static void store_positions_grids(const SubdivCCG &subdiv_ccg, Node &unode)
static BLI_NOINLINE void bmesh_push(const Object &object, const bke::pbvh::BMeshNode *node, Type type)
static void free_step_data(StepData &step_data)
void push_multires_mesh_begin(bContext *C, const char *str)
void push_multires_mesh_end(bContext *C, const char *str)
static void fill_node_data_mesh(const Depsgraph &depsgraph, const Object &object, const bke::pbvh::MeshNode &node, const Type type, Node &unode)
static size_t node_size_in_bytes(const Node &node)
static bool topology_matches(const StepData &step_data, const Object &object)
static SculptUndoStep * get_active_step()
static void store_face_visibility(const Mesh &mesh, Node &unode)
size_t step_memory_size_get(UndoStep *step)
static void refine_subdiv(Depsgraph *depsgraph, const SculptSession &ss, Object &object, bke::subdiv::Subdiv *subdiv)
static void bmesh_handle_dyntopo_end(bContext *C, StepData &step_data, Object &object)
static void restore_vert_visibility_mesh(Object &object, Node &unode, const MutableSpan< bool > modified_verts)
static void step_encode_init(bContext *, UndoStep *us_p)
static size_t estimate_geometry_step_size(const StepData &step_data)
static bool use_multires_undo(const StepData &step_data, const SculptSession &ss)
void push_begin_ex(const Scene &, Object &ob, const char *name)
static void step_decode_redo_impl(bContext *C, Depsgraph *depsgraph, SculptUndoStep *us)
static void restore_geometry_data(const NodeGeometry *geometry, Mesh *mesh)
void push_enter_sculpt_mode(const Scene &, Object &ob, const wmOperator *op)
static StepData * get_step_data()
static void store_vert_visibility_mesh(const Mesh &mesh, const bke::pbvh::Node &node, Node &unode)
static void store_mask_grids(const SubdivCCG &subdiv_ccg, Node &unode)
static NodeGeometry * geometry_get(StepData &step_data)
static void store_positions_mesh(const Depsgraph &depsgraph, const Object &object, Node &unode)
static void fill_node_data_grids(const Object &object, const bke::pbvh::GridsNode &node, const Type type, Node &unode)
static void geometry_free_data(NodeGeometry *geometry)
static void step_decode_undo(bContext *C, Depsgraph *depsgraph, SculptUndoStep *us, const bool is_final)
static void restore_hidden_face(Object &object, Node &unode, const MutableSpan< bool > modified_faces)
void geometry_begin_ex(const Scene &scene, Object &ob, const char *name)
static void restore_position_grids(const MutableSpan< float3 > positions, const CCGKey &key, PositionUndoStorage &undo_data, const MutableSpan< bool > modified_grids)
static void step_free(UndoStep *us_p)
static void restore_position_mesh(Object &object, PositionUndoStorage &undo_data, const MutableSpan< bool > modified_verts)
static const Node * get_node(const bke::pbvh::Node *node, const Type type)
static bool step_encode(bContext *, Main *bmain, UndoStep *us_p)
static void restore_vert_visibility_grids(SubdivCCG &subdiv_ccg, Node &unode, const MutableSpan< bool > modified_grids)
static bool indices_contain_true(const Span< bool > data, const Span< int > indices)
static void set_active_layer(bContext *C, const SculptAttrRef *attr_ref)
static Node * ensure_node(StepData &step_data, const bke::pbvh::Node &node, bool &r_new)
static void save_common_data(Object &ob, SculptUndoStep *us)
void geometry_begin(const Scene &scene, Object &ob, const wmOperator *op)
static void restore_mask_grids(Object &object, Node &unode, const MutableSpan< bool > modified_grids)
static void step_decode_undo_impl(bContext *C, Depsgraph *depsgraph, SculptUndoStep *us)
static void restore_list(bContext *C, Depsgraph *depsgraph, StepData &step_data)
static void step_decode_redo(bContext *C, Depsgraph *depsgraph, SculptUndoStep *us)
static void restore_color(Object &object, StepData &step_data, const MutableSpan< bool > modified_verts)
static void store_geometry_data(NodeGeometry *geometry, const Object &object)
static void store_mask_mesh(const Mesh &mesh, Node &unode)
static void bmesh_restore_generic(StepData &step_data, Object &object)
void push_node(const Depsgraph &depsgraph, const Object &object, const bke::pbvh::Node *node, const Type type)
static bool restore_active_shape_key(bContext &C, Depsgraph &depsgraph, const StepData &step_data, Object &object)
static void restore_mask_mesh(Object &object, Node &unode, const MutableSpan< bool > modified_verts)
static void store_color(const Mesh &mesh, const bke::pbvh::MeshNode &node, Node &unode)
void push_begin(const Scene &scene, Object &ob, const wmOperator *op)
void restore_from_bmesh_enter_geometry(const StepData &step_data, Mesh &mesh)
static void store_face_sets(const Mesh &mesh, Node &unode)
static bool restore_face_sets(Object &object, Node &unode, const MutableSpan< bool > modified_face_set_faces)
static int bmesh_restore(bContext *C, Depsgraph &depsgraph, StepData &step_data, Object &object)
static size_t calculate_node_geometry_allocated_size(const NodeGeometry &node_geometry)
void push_end_ex(Object &ob, const bool use_nested_undo)
static void swap_indexed_data(MutableSpan< T > full, const Span< int > indices, MutableSpan< T > indexed)
static void restore_geometry(StepData &step_data, Object &object)
static void bmesh_handle_dyntopo_begin(bContext *C, StepData &step_data, Object &object)
void object_sculpt_mode_enter(Main &bmain, Depsgraph &depsgraph, Scene &scene, Object &ob, bool force_dyntopo, ReportList *reports)
std::optional< Span< float > > orig_mask_data_lookup_grids(const Object &object, const bke::pbvh::GridsNode &node)
std::optional< Span< int > > orig_face_set_data_lookup_mesh(const Object &object, const bke::pbvh::MeshNode &node)
void gather_data_grids(const SubdivCCG &subdiv_ccg, Span< T > src, Span< int > grids, MutableSpan< T > node_data)
Definition sculpt.cc:6528
std::optional< OrigPositionData > orig_position_data_lookup_grids(const Object &object, const bke::pbvh::GridsNode &node)
void orig_position_data_gather_bmesh(const BMLog &bm_log, const Set< BMVert *, 0 > &verts, MutableSpan< float3 > positions, MutableSpan< float3 > normals)
void translations_from_new_positions(Span< float3 > new_positions, Span< int > verts, Span< float3 > old_positions, MutableSpan< float3 > translations)
Definition sculpt.cc:7889
std::optional< Span< float4 > > orig_color_data_lookup_mesh(const Object &object, const bke::pbvh::MeshNode &node)
void scatter_data_mesh(Span< T > src, Span< int > indices, MutableSpan< T > dst)
void apply_translations(Span< float3 > translations, Span< int > verts, MutableSpan< float3 > positions)
Definition sculpt.cc:7592
void gather_data_mesh(Span< T > src, Span< int > indices, MutableSpan< T > dst)
std::optional< Span< float > > orig_mask_data_lookup_mesh(const Object &object, const bke::pbvh::MeshNode &node)
std::optional< OrigPositionData > orig_position_data_lookup_mesh_all_verts(const Object &object, const bke::pbvh::MeshNode &node)
std::optional< OrigPositionData > orig_position_data_lookup_mesh(const Object &object, const bke::pbvh::MeshNode &node)
std::optional< Span< int > > orig_face_set_data_lookup_grids(const Object &object, const bke::pbvh::GridsNode &node)
constexpr ParallelGrainSize grain_size(int grain_size)
void copy_shared_pointer(T *src_ptr, const ImplicitSharingInfo *src_sharing_info, T **r_dst_ptr, const ImplicitSharingInfo **r_dst_sharing_info)
void free_shared_data(T **data, const ImplicitSharingInfo **sharing_info)
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:69
auto isolate_task(const Function &function)
Definition BLI_task.hh:224
Value parallel_reduce(IndexRange range, int64_t grain_size, const Value &identity, const Function &function, const Reduction &reduction)
Definition BLI_task.hh:127
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
Object * BKE_view_layer_active_object_get(const ViewLayer *view_layer)
void multires_mark_as_modified(Depsgraph *depsgraph, Object *object, MultiresModifiedFlags flags)
Definition multires.cc:254
const Set< BMFace *, 0 > & BKE_pbvh_bmesh_node_faces(bke::pbvh::BMeshNode *node)
MultiresModifierData * BKE_sculpt_multires_active(const Scene *scene, Object *ob)
UndoStack * ED_undo_stack_get()
Definition ed_undo.cc:690
@ UNDOTYPE_FLAG_DECODE_ACTIVE_STEP
void BM_log_original_vert_data(BMLog *log, BMVert *v, const float **r_co, const float **r_no)
Definition bmesh_log.cc:810
@ TASK_PRIORITY_LOW
Definition BLI_task_c.hh:60
KeyBlock * BKE_keyblock_from_object(Object *ob)
Definition key.cc:1407
KeyBlock * BKE_keyblock_find_name(Key *key, const char name[])
Definition key.cc:1434
void BM_log_redo(BMesh *bm, BMLog *log)
Definition bmesh_log.cc:622
void BM_log_all_added(BMesh *bm, BMLog *log)
Definition bmesh_log.cc:741
void BKE_sculptsession_free_pbvh(Object &object)
ViewLayer * CTX_data_view_layer(const bContext *C)
BitGroupVector & BKE_subdiv_ccg_grid_hidden_ensure(SubdivCCG &subdiv_ccg)
void BKE_scene_graph_evaluated_ensure(Depsgraph *depsgraph, Main *bmain)
Definition scene.cc:2872
CCGKey BKE_subdiv_ccg_key_top_level(const SubdivCCG &subdiv_ccg)
void BKE_subdiv_ccg_grid_hidden_free(SubdivCCG &subdiv_ccg)
void filter_transpose_delta(const uint8_t *src, uint8_t *dst, size_t items_num, size_t item_size)
void multires_flush_sculpt_updates(Object *object)
Definition multires.cc:277
Array< float3 > BKE_multires_create_deformed_base_mesh_vert_coords(Depsgraph *depsgraph, Object *object, MultiresModifierData *mmd)
Definition multires.cc:125
void BLI_task_pool_work_and_wait(TaskPool *pool)
Definition task_pool.cc:455
VecBase< float, 4 > float4
void BKE_pbvh_sync_visibility_from_verts(Object &object)
Definition pbvh.cc:2441
int BLI_findindex(const ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:593
void BM_log_before_all_removed(BMesh *bm, BMLog *log)
Definition bmesh_log.cc:769
void DEG_id_tag_update(ID *id, unsigned int flags)
BMesh const char void * data
@ MULTIRES_COORDS_MODIFIED
@ MULTIRES_HIDDEN_MODIFIED
Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C, bool rna_write_check=false)
void ED_undo_push(bContext *C, const char *str, UndoEncodeHints hints=UndoEncodeHints(0))
Definition ed_undo.cc:103
UndoStep * BKE_undosys_stack_init_or_active_with_type(UndoStack *ustack, const UndoType *ut)
int CustomData_get_offset_named(const CustomData *data, eCustomDataType type, StringRef name)
unsigned char uint8_t
Definition BKE_text.h:79
BMLog * BM_log_from_existing_entries_create(BMesh *bm, BMLogEntry *entry)
Definition bmesh_log.cc:448
const UndoType * BKE_UNDOSYS_TYPE_SCULPT
bool BKE_view_layer_synced_ensure(const Main &bmain, const Scene *scene, ViewLayer *view_layer)
const Set< BMVert *, 0 > & BKE_pbvh_bmesh_node_other_verts(bke::pbvh::BMeshNode *node)
const BMAllocTemplate bm_mesh_allocsize_default
Definition bmesh_mesh.cc:26
RegionView3D * CTX_wm_region_view3d(const bContext *C)
@ ME_SCULPT_DYNAMIC_TOPOLOGY
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
void BLI_task_pool_free(TaskPool *pool)
Definition task_pool.cc:441
void BKE_sculptsession_free_deformMats(SculptSession *ss)
void BM_log_face_modified(BMLog *log, BMFace *f)
Definition bmesh_log.cc:680
void CustomData_count_memory(const CustomData &data, int totelem, MemoryCounter &memory)
BMLogEntry * BM_log_entry_add(BMLog *log)
Definition bmesh_log.cc:516
bool BKE_sculptsession_use_pbvh_draw(const Object *ob, const RegionView3D *rv3d)
void BM_log_undo(BMesh *bm, BMLog *log)
Definition bmesh_log.cc:601
void WM_file_tag_modified()
Definition wm_files.cc:188
Main * CTX_data_main(const bContext *C)
const CustomData_MeshMasks CD_MASK_MESH
void CustomData_free(CustomData *data)
std::mutex Mutex
Definition BLI_mutex.hh:51
void BKE_id_attributes_active_color_set(struct ID *id, std::optional< StringRef > name)
@ ID_RECALC_SHADING
Definition DNA_ID.h:1094
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
void BKE_sculptsession_update_for_edit(Depsgraph *depsgraph, Object *ob_orig, bool is_paint_tool)
static CLG_LogRef LOG
Definition logging.cpp:12
void BM_data_layer_add_named(BMesh *bm, CustomData *data, int type, const StringRef name)
Scene * CTX_data_scene(const bContext *C)
Mesh * BKE_object_get_original_mesh(const Object *object)
Definition object.cc:4461
VecBase< float, 3 > float3
Key * BKE_key_from_object(Object *ob)
Definition key.cc:1311
BMesh * BM_mesh_create(const BMAllocTemplate *allocsize, const BMeshCreateParams *params)
BMesh Make Mesh.
Object * CTX_data_active_object(const bContext *C)
void unfilter_transpose_delta(const uint8_t *src, uint8_t *dst, size_t items_num, size_t item_size)
void BM_log_vert_before_modified(BMLog *log, BMVert *v, const int cd_vert_mask_offset)
Definition bmesh_log.cc:656
void BLI_task_pool_push(TaskPool *pool, TaskRunFunction run, void *taskdata, bool free_taskdata, TaskFreeFunction freedata)
Definition task_pool.cc:446
const Set< BMVert *, 0 > & BKE_pbvh_bmesh_node_unique_verts(bke::pbvh::BMeshNode *node)
void CustomData_init_from(const CustomData *source, CustomData *dest, eCustomDataMask mask, int totelem)
PaintMode BKE_paintmode_get_active_from_context(const bContext *C)
void BKE_mesh_clear_geometry(Mesh *mesh)
eUndoPushReturn BKE_undosys_step_push(UndoStack *ustack, bContext *C, const char *name, UndoEncodeHints hints)
void BM_mesh_normals_update(BMesh *bm)
void BM_log_entry_drop(BMLogEntry *entry)
Definition bmesh_log.cc:530
void ED_undosys_stack_memfile_id_changed_tag(UndoStack *ustack, ID *id)
TaskPool * BLI_task_pool_create_background(void *userdata, eTaskPriority priority)
Definition task_pool.cc:409
UndoStep * BKE_undosys_step_push_init_with_type(UndoStack *ustack, bContext *C, const char *name, const UndoType *ut)
CustomData vdata
char name[258]
Definition DNA_ID.h:433
ListBaseT< KeyBlock > block
bool is_memfile_undo_flush_needed
Definition BKE_main.hh:302
bke::ObjectRuntime * runtime
KeyBlock * shapekey_active
Definition BKE_paint.hh:368
MultiresModifierData * multires_modifier
Definition BKE_paint.hh:385
Array< float3 > positions
Array< float3 > normals
BitGroupVector grid_hidden
Array< float > masks
const UndoType * type
bool(*) poll(bContext *C)
void(*) step_free(UndoStep *us)
void(*) step_decode(bContext *C, Main *bmain, UndoStep *us, eUndoStepDir dir, bool is_final)
bool(*) step_encode(bContext *C, Main *bmain, UndoStep *us)
const char * identifier
void(*) step_encode_init(bContext *C, UndoStep *us)
static std::optional< ShapeKeyData > from_object(Object &object)
Definition sculpt.cc:7747
const ImplicitSharingInfo * face_offsets_sharing_info
static void compress_fn(TaskPool *, void *task_data)
Vector< std::unique_ptr< Node > > nodes_to_compress
std::optional< bke::AttrDomain > domain
Vector< std::unique_ptr< Node > > nodes
struct blender::ed::sculpt_paint::undo::StepData::@215211107015304361034301322047137170060312277124 grids
struct blender::ed::sculpt_paint::undo::StepData::@122226043223236050374162026040070012345131255174 bmesh
struct blender::ed::sculpt_paint::undo::StepData::@304076261154065047305032143163302111343365002361 mesh
std::unique_ptr< PositionUndoStorage > position_step_storage
Map< const bke::pbvh::Node *, std::unique_ptr< Node > > undo_nodes_by_pbvh_node
struct wmOperatorType * type