Blender V5.3
deg_eval_runtime_backup_object.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2019 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_session_uid.hh"
10
12
13#include <cstring>
14
15#include "DNA_mesh_types.h"
16
17#include "BLI_listbase.hh"
18
19#include "BKE_action.hh"
20#include "BKE_light_linking.h"
21#include "BKE_mesh_types.hh"
22#include "BKE_modifier.hh"
23#include "BKE_object.hh"
24#include "BKE_object_types.hh"
25
26namespace blender::deg {
27
30{
31 /* TODO(sergey): Use something like BKE_object_runtime_reset(). */
32 runtime = {};
33}
34
36{
37 /* Store evaluated mesh and curve_cache, and make sure we don't free it. */
38 runtime = *object->runtime;
39 if (object->light_linking) {
40 light_linking_runtime = object->light_linking->runtime;
41 }
43 /* Keep bounding-box (for now at least). */
44 object->runtime->bounds_eval = runtime.bounds_eval;
45 /* Object update will override actual object->data to an evaluated version.
46 * Need to make sure we don't have data set to evaluated one before free
47 * anything. */
48 object->data = runtime.data_orig;
49 /* Make a backup of base flags. */
50 base_flag = object->base_flag;
51 base_local_view_bits = object->base_local_view_bits;
52 /* Backup runtime data of all modifiers. */
54 /* Backup runtime data of all pose channels. */
56}
57
59{
60 for (ModifierData &modifier_data : object->modifiers) {
61 if (modifier_data.runtime == nullptr) {
62 continue;
63 }
64
65 modifier_runtime_data.add(modifier_data.persistent_uid, ModifierDataBackup(&modifier_data));
66 modifier_data.runtime = nullptr;
67 }
68}
69
71{
72 if (object->pose != nullptr) {
73 for (bPoseChannel &pchan : object->pose->chanbase) {
74 const SessionUID &session_uid = pchan.runtime.session_uid;
75 BLI_assert(session_uid.is_generated());
76
77 pose_channel_runtime_data.add(session_uid, pchan.runtime);
79 }
80 }
81}
82
84{
85 ID *data_orig = object->runtime->data_orig;
86 ID *data_eval = runtime.data_eval;
87 std::optional<Bounds<float3>> bounds = object->runtime->bounds_eval;
88 SculptSession *sculpt_session = object->runtime->sculpt_session;
89 *object->runtime = runtime;
90 object->runtime->data_orig = data_orig;
91 object->runtime->bounds_eval = bounds;
92 object->runtime->sculpt_session = sculpt_session;
93 if (ELEM(object->type, OB_MESH, OB_LATTICE, OB_CURVES_LEGACY, OB_FONT) && data_eval != nullptr) {
94 if (object->id.recalc & ID_RECALC_GEOMETRY) {
95 /* If geometry is tagged for update it means, that part of
96 * evaluated mesh are not valid anymore. In this case we can not
97 * have any "persistent" pointers to point to an invalid data.
98 *
99 * We restore object's data datablock to an original copy of
100 * that datablock. */
101 object->data = data_orig;
102
103 /* After that, immediately free the invalidated caches. */
105 }
106 else {
107 /* Do same thing as object update: override actual object data pointer with evaluated
108 * datablock, but only if the evaluated data has the same type as the original data. */
109 if (GS(((ID *)object->data)->name) == GS(data_eval->name)) {
110 object->data = data_eval;
111 }
112
113 /* Evaluated mesh simply copied edit_mesh pointer from
114 * original mesh during update, need to make sure no dead
115 * pointers are left behind. */
116 if (object->type == OB_MESH) {
117 Mesh *mesh_eval = id_cast<Mesh *>(data_eval);
118 Mesh *mesh_orig = id_cast<Mesh *>(data_orig);
119 mesh_eval->runtime->edit_mesh = mesh_orig->runtime->edit_mesh;
120 }
121 }
122 }
124 if (object->id.recalc & ID_RECALC_GEOMETRY) {
125 /* Free evaluated caches. */
126 object->data = data_orig;
128 }
129 else {
130 object->data = object->runtime->data_eval;
131 }
132 }
133
135 /* Lazily allocate light linking on the evaluated object for the cases when the object is only
136 * a receiver or a blocker and does not need its own LightLinking on the original object. */
138 object->light_linking->runtime = *light_linking_runtime;
139 }
140
141 object->base_flag = base_flag;
142 object->base_local_view_bits = base_local_view_bits;
143
144 /* Restore modifier's runtime data.
145 * NOTE: Data of unused modifiers will be freed there. */
148}
149
151{
152 for (ModifierData &modifier_data : object->modifiers) {
153 std::optional<ModifierDataBackup> backup = modifier_runtime_data.pop_try(
154 modifier_data.persistent_uid);
155 if (backup.has_value()) {
156 modifier_data.runtime = backup->runtime;
157 }
158 }
159
160 for (ModifierDataBackup &backup : modifier_runtime_data.values()) {
161 const ModifierTypeInfo *modifier_type_info = BKE_modifier_get_info(backup.type);
162 BLI_assert(modifier_type_info != nullptr);
163 modifier_type_info->free_runtime_data(backup.runtime);
164
165 if (backup.type == eModifierType_Subsurf) {
166 if (object->type == OB_MESH) {
167 Mesh *mesh = id_cast<Mesh *>(object->data);
168 if (mesh->runtime->subsurf_runtime_data == backup.runtime) {
169 mesh->runtime->subsurf_runtime_data = nullptr;
170 }
171 }
172 }
173 }
174}
175
177{
178 if (object->pose != nullptr) {
179 for (bPoseChannel &pchan : object->pose->chanbase) {
180 const SessionUID &session_uid = pchan.runtime.session_uid;
181 std::optional<bPoseChannel_Runtime> runtime = pose_channel_runtime_data.pop_try(session_uid);
182 if (runtime.has_value()) {
183 pchan.runtime = *runtime;
184 }
185 }
186 }
189 }
190}
191
192} // namespace blender::deg
Blender kernel action and pose functionality.
General operations, lookup, etc. for blender objects.
#define ELEM(...)
Map< SessionUID, bPoseChannel_Runtime > pose_channel_runtime_data
Map< int, ModifierDataBackup > modifier_runtime_data
std::optional< LightLinkingRuntime > light_linking_runtime
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void BKE_object_runtime_reset(Object *object)
Definition object.cc:5202
void BKE_pose_channel_runtime_reset(bPoseChannel_Runtime *runtime) ATTR_NONNULL(1)
void BKE_object_free_derived_caches(Object *ob)
Definition object.cc:1810
void BKE_light_linking_ensure(struct Object *object)
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
ID_Type GS(const char *name)
Definition DNA_ID.h:410
void BKE_pose_channel_runtime_free(bPoseChannel_Runtime *runtime) ATTR_NONNULL(1)
const ModifierTypeInfo * BKE_modifier_get_info(ModifierType type)
char name[258]
Definition DNA_ID.h:433
bke::id::ID_Runtime * runtime
Definition DNA_ID.h:541
bke::MeshRuntime * runtime
void(*) free_runtime_data(void *runtime_data)
struct bPoseChannel_Runtime runtime
std::shared_ptr< BMEditMesh > edit_mesh
std::optional< Bounds< float3 > > bounds_eval