Depsgraph: Move evaluation debug prints to depsgraph
[blender-staging.git] / source / blender / blenkernel / intern / object_update.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 20014 by Blender Foundation.
19  * All rights reserved.
20  *
21  * Contributor(s): Sergey Sharybin.
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/blenkernel/intern/object_update.c
27  *  \ingroup bke
28  */
29
30 #include "DNA_anim_types.h"
31 #include "DNA_constraint_types.h"
32 #include "DNA_group_types.h"
33 #include "DNA_key_types.h"
34 #include "DNA_material_types.h"
35 #include "DNA_scene_types.h"
36
37 #include "BLI_blenlib.h"
38 #include "BLI_utildefines.h"
39 #include "BLI_math.h"
40 #include "BLI_threads.h"
41
42 #include "BKE_global.h"
43 #include "BKE_armature.h"
44 #include "BKE_action.h"
45 #include "BKE_constraint.h"
46 #include "BKE_depsgraph.h"
47 #include "BKE_DerivedMesh.h"
48 #include "BKE_animsys.h"
49 #include "BKE_displist.h"
50 #include "BKE_effect.h"
51 #include "BKE_key.h"
52 #include "BKE_lamp.h"
53 #include "BKE_lattice.h"
54 #include "BKE_editmesh.h"
55 #include "BKE_object.h"
56 #include "BKE_particle.h"
57 #include "BKE_pointcache.h"
58 #include "BKE_scene.h"
59 #include "BKE_material.h"
60 #include "BKE_image.h"
61
62 #include "DEG_depsgraph.h"
63
64 static ThreadMutex material_lock = BLI_MUTEX_INITIALIZER;
65
66 void BKE_object_eval_local_transform(EvaluationContext *UNUSED(eval_ctx),
67                                      Object *ob)
68 {
69         DEG_debug_print_eval(__func__, ob->id.name, ob);
70
71         /* calculate local matrix */
72         BKE_object_to_mat4(ob, ob->obmat);
73 }
74
75 /* Evaluate parent */
76 /* NOTE: based on solve_parenting(), but with the cruft stripped out */
77 void BKE_object_eval_parent(EvaluationContext *UNUSED(eval_ctx),
78                             Scene *scene,
79                             Object *ob)
80 {
81         Object *par = ob->parent;
82
83         float totmat[4][4];
84         float tmat[4][4];
85         float locmat[4][4];
86
87         DEG_debug_print_eval(__func__, ob->id.name, ob);
88
89         /* get local matrix (but don't calculate it, as that was done already!) */
90         // XXX: redundant?
91         copy_m4_m4(locmat, ob->obmat);
92
93         /* get parent effect matrix */
94         BKE_object_get_parent_matrix(scene, ob, par, totmat);
95
96         /* total */
97         mul_m4_m4m4(tmat, totmat, ob->parentinv);
98         mul_m4_m4m4(ob->obmat, tmat, locmat);
99
100         /* origin, for help line */
101         if ((ob->partype & PARTYPE) == PARSKEL) {
102                 copy_v3_v3(ob->orig, par->obmat[3]);
103         }
104         else {
105                 copy_v3_v3(ob->orig, totmat[3]);
106         }
107 }
108
109 void BKE_object_eval_constraints(EvaluationContext *UNUSED(eval_ctx),
110                                  Scene *scene,
111                                  Object *ob)
112 {
113         bConstraintOb *cob;
114         float ctime = BKE_scene_frame_get(scene);
115
116         DEG_debug_print_eval(__func__, ob->id.name, ob);
117
118         /* evaluate constraints stack */
119         /* TODO: split this into:
120          * - pre (i.e. BKE_constraints_make_evalob, per-constraint (i.e.
121          * - inner body of BKE_constraints_solve),
122          * - post (i.e. BKE_constraints_clear_evalob)
123          *
124          * Not sure why, this is from Joshua - sergey
125          *
126          */
127         cob = BKE_constraints_make_evalob(scene, ob, NULL, CONSTRAINT_OBTYPE_OBJECT);
128         BKE_constraints_solve(&ob->constraints, cob, ctime);
129         BKE_constraints_clear_evalob(cob);
130 }
131
132 void BKE_object_eval_done(EvaluationContext *UNUSED(eval_ctx), Object *ob)
133 {
134         DEG_debug_print_eval(__func__, ob->id.name, ob);
135
136         /* Set negative scale flag in object. */
137         if (is_negative_m4(ob->obmat)) ob->transflag |= OB_NEG_SCALE;
138         else ob->transflag &= ~OB_NEG_SCALE;
139 }
140
141 void BKE_object_handle_data_update(EvaluationContext *eval_ctx,
142                                    Scene *scene,
143                                    Object *ob)
144 {
145         ID *data_id = (ID *)ob->data;
146         AnimData *adt = BKE_animdata_from_id(data_id);
147         Key *key;
148         float ctime = BKE_scene_frame_get(scene);
149
150         if (G.debug & G_DEBUG_DEPSGRAPH_EVAL)
151                 printf("recalcdata %s\n", ob->id.name + 2);
152
153         /* TODO(sergey): Only used by legacy depsgraph. */
154         if (adt) {
155                 /* evaluate drivers - datalevel */
156                 /* XXX: for mesh types, should we push this to derivedmesh instead? */
157                 BKE_animsys_evaluate_animdata(scene, data_id, adt, ctime, ADT_RECALC_DRIVERS);
158         }
159
160         /* TODO(sergey): Only used by legacy depsgraph. */
161         key = BKE_key_from_object(ob);
162         if (key && key->block.first) {
163                 if (!(ob->shapeflag & OB_SHAPE_LOCK))
164                         BKE_animsys_evaluate_animdata(scene, &key->id, key->adt, ctime, ADT_RECALC_DRIVERS);
165         }
166
167         /* includes all keys and modifiers */
168         switch (ob->type) {
169                 case OB_MESH:
170                 {
171                         BMEditMesh *em = (ob == scene->obedit) ? BKE_editmesh_from_object(ob) : NULL;
172                         uint64_t data_mask = scene->customdata_mask | CD_MASK_BAREMESH;
173 #ifdef WITH_FREESTYLE
174                         /* make sure Freestyle edge/face marks appear in DM for render (see T40315) */
175                         if (eval_ctx->mode != DAG_EVAL_VIEWPORT) {
176                                 data_mask |= CD_MASK_FREESTYLE_EDGE | CD_MASK_FREESTYLE_FACE;
177                         }
178 #endif
179                         if (em) {
180                                 makeDerivedMesh(scene, ob, em,  data_mask, false); /* was CD_MASK_BAREMESH */
181                         }
182                         else {
183                                 makeDerivedMesh(scene, ob, NULL, data_mask, false);
184                         }
185                         break;
186                 }
187                 case OB_ARMATURE:
188                         if (ID_IS_LINKED(ob) && ob->proxy_from) {
189                                 if (BKE_pose_copy_result(ob->pose, ob->proxy_from->pose) == false) {
190                                         printf("Proxy copy error, lib Object: %s proxy Object: %s\n",
191                                                ob->id.name + 2, ob->proxy_from->id.name + 2);
192                                 }
193                         }
194                         else {
195                                 BKE_pose_where_is(scene, ob);
196                         }
197                         break;
198
199                 case OB_MBALL:
200                         BKE_displist_make_mball(eval_ctx, scene, ob);
201                         break;
202
203                 case OB_CURVE:
204                 case OB_SURF:
205                 case OB_FONT:
206                         BKE_displist_make_curveTypes(scene, ob, 0);
207                         break;
208
209                 case OB_LATTICE:
210                         BKE_lattice_modifiers_calc(scene, ob);
211                         break;
212
213                 case OB_EMPTY:
214                         if (ob->empty_drawtype == OB_EMPTY_IMAGE && ob->data)
215                                 if (BKE_image_is_animated(ob->data))
216                                         BKE_image_user_check_frame_calc(ob->iuser, (int)ctime, 0);
217                         break;
218         }
219
220         /* related materials */
221         /* XXX: without depsgraph tagging, this will always need to be run, which will be slow!
222          * However, not doing anything (or trying to hack around this lack) is not an option
223          * anymore, especially due to Cycles [#31834]
224          */
225         if (ob->totcol) {
226                 int a;
227                 if (ob->totcol != 0) {
228                         BLI_mutex_lock(&material_lock);
229                         for (a = 1; a <= ob->totcol; a++) {
230                                 Material *ma = give_current_material(ob, a);
231                                 if (ma) {
232                                         /* recursively update drivers for this material */
233                                         material_drivers_update(scene, ma, ctime);
234                                 }
235                         }
236                         BLI_mutex_unlock(&material_lock);
237                 }
238         }
239         else if (ob->type == OB_LAMP)
240                 lamp_drivers_update(scene, ob->data, ctime);
241
242         /* particles */
243         if (ob != scene->obedit && ob->particlesystem.first) {
244                 ParticleSystem *tpsys, *psys;
245                 DerivedMesh *dm;
246                 ob->transflag &= ~OB_DUPLIPARTS;
247                 psys = ob->particlesystem.first;
248                 while (psys) {
249                         /* ensure this update always happens even if psys is disabled */
250                         if (psys->recalc & PSYS_RECALC_TYPE) {
251                                 psys_changed_type(ob, psys);
252                         }
253
254                         if (psys_check_enabled(ob, psys, eval_ctx->mode == DAG_EVAL_RENDER)) {
255                                 /* check use of dupli objects here */
256                                 if (psys->part && (psys->part->draw_as == PART_DRAW_REND || eval_ctx->mode == DAG_EVAL_RENDER) &&
257                                     ((psys->part->ren_as == PART_DRAW_OB && psys->part->dup_ob) ||
258                                      (psys->part->ren_as == PART_DRAW_GR && psys->part->dup_group)))
259                                 {
260                                         ob->transflag |= OB_DUPLIPARTS;
261                                 }
262
263                                 particle_system_update(scene, ob, psys, (eval_ctx->mode == DAG_EVAL_RENDER));
264                                 psys = psys->next;
265                         }
266                         else if (psys->flag & PSYS_DELETE) {
267                                 tpsys = psys->next;
268                                 BLI_remlink(&ob->particlesystem, psys);
269                                 psys_free(ob, psys);
270                                 psys = tpsys;
271                         }
272                         else
273                                 psys = psys->next;
274                 }
275
276                 if (eval_ctx->mode == DAG_EVAL_RENDER && ob->transflag & OB_DUPLIPARTS) {
277                         /* this is to make sure we get render level duplis in groups:
278                          * the derivedmesh must be created before init_render_mesh,
279                          * since object_duplilist does dupliparticles before that */
280                         CustomDataMask data_mask = CD_MASK_BAREMESH | CD_MASK_MFACE | CD_MASK_MTFACE | CD_MASK_MCOL;
281                         dm = mesh_create_derived_render(scene, ob, data_mask);
282                         dm->release(dm);
283
284                         for (psys = ob->particlesystem.first; psys; psys = psys->next)
285                                 psys_get_modifier(ob, psys)->flag &= ~eParticleSystemFlag_psys_updated;
286                 }
287         }
288
289         /* quick cache removed */
290 }
291
292 bool BKE_object_eval_proxy_copy(EvaluationContext *UNUSED(eval_ctx),
293                                 Object *object)
294 {
295         /* Handle proxy copy for target, */
296         if (ID_IS_LINKED(object) && object->proxy_from) {
297                 if (object->proxy_from->proxy_group) {
298                         /* Transform proxy into group space. */
299                         Object *obg = object->proxy_from->proxy_group;
300                         float imat[4][4];
301                         invert_m4_m4(imat, obg->obmat);
302                         mul_m4_m4m4(object->obmat, imat, object->proxy_from->obmat);
303                         /* Should always be true. */
304                         if (obg->dup_group) {
305                                 add_v3_v3(object->obmat[3], obg->dup_group->dupli_ofs);
306                         }
307                 }
308                 else {
309                         copy_m4_m4(object->obmat, object->proxy_from->obmat);
310                 }
311                 return true;
312         }
313         return false;
314 }
315
316 void BKE_object_eval_uber_transform(EvaluationContext *eval_ctx, Object *object)
317 {
318         BKE_object_eval_proxy_copy(eval_ctx, object);
319         object->recalc &= ~(OB_RECALC_OB | OB_RECALC_TIME);
320         if (object->data == NULL) {
321                 object->recalc &= ~OB_RECALC_DATA;
322         }
323 }
324
325 void BKE_object_eval_uber_data(EvaluationContext *eval_ctx,
326                                Scene *scene,
327                                Object *ob)
328 {
329         DEG_debug_print_eval(__func__, ob->id.name, ob);
330         BLI_assert(ob->type != OB_ARMATURE);
331         BKE_object_handle_data_update(eval_ctx, scene, ob);
332
333         ob->recalc &= ~(OB_RECALC_DATA | OB_RECALC_TIME);
334 }
335
336 void BKE_object_eval_cloth(EvaluationContext *UNUSED(eval_ctx),
337                            Scene *scene,
338                            Object *object)
339 {
340         DEG_debug_print_eval(__func__, object->id.name, object);
341         BKE_ptcache_object_reset(scene, object, PTCACHE_RESET_DEPSGRAPH);
342 }
343
344 void BKE_object_eval_transform_all(EvaluationContext *eval_ctx,
345                                    Scene *scene,
346                                    Object *object)
347 {
348         /* This mimics full transform update chain from new depsgraph. */
349         BKE_object_eval_local_transform(eval_ctx, object);
350         if (object->parent != NULL) {
351                 BKE_object_eval_parent(eval_ctx, scene, object);
352         }
353         if (!BLI_listbase_is_empty(&object->constraints)) {
354                 BKE_object_eval_constraints(eval_ctx, scene, object);
355         }
356         BKE_object_eval_uber_transform(eval_ctx, object);
357         BKE_object_eval_done(eval_ctx, object);
358 }