Pass EvaluationContext argument everywhere
[blender.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_mesh_types.h"
36 #include "DNA_scene_types.h"
37
38 #include "BLI_blenlib.h"
39 #include "BLI_utildefines.h"
40 #include "BLI_math.h"
41 #include "BLI_threads.h"
42
43 #include "BKE_global.h"
44 #include "BKE_armature.h"
45 #include "BKE_action.h"
46 #include "BKE_constraint.h"
47 #include "BKE_curve.h"
48 #include "BKE_DerivedMesh.h"
49 #include "BKE_animsys.h"
50 #include "BKE_displist.h"
51 #include "BKE_effect.h"
52 #include "BKE_key.h"
53 #include "BKE_lamp.h"
54 #include "BKE_lattice.h"
55 #include "BKE_library.h"
56 #include "BKE_editmesh.h"
57 #include "BKE_object.h"
58 #include "BKE_particle.h"
59 #include "BKE_pointcache.h"
60 #include "BKE_scene.h"
61 #include "BKE_material.h"
62 #include "BKE_mesh.h"
63 #include "BKE_image.h"
64
65 #include "MEM_guardedalloc.h"
66 #include "DEG_depsgraph.h"
67
68 #define DEBUG_PRINT if (G.debug & G_DEBUG_DEPSGRAPH) printf
69
70 static ThreadMutex material_lock = BLI_MUTEX_INITIALIZER;
71
72 void BKE_object_eval_local_transform(EvaluationContext *UNUSED(eval_ctx),
73                                      Scene *UNUSED(scene),
74                                      Object *ob)
75 {
76         DEBUG_PRINT("%s on %s\n", __func__, ob->id.name);
77
78         /* calculate local matrix */
79         BKE_object_to_mat4(ob, ob->obmat);
80 }
81
82 /* Evaluate parent */
83 /* NOTE: based on solve_parenting(), but with the cruft stripped out */
84 void BKE_object_eval_parent(EvaluationContext *UNUSED(eval_ctx),
85                             Scene *scene,
86                             Object *ob)
87 {
88         Object *par = ob->parent;
89
90         float totmat[4][4];
91         float tmat[4][4];
92         float locmat[4][4];
93
94         DEBUG_PRINT("%s on %s\n", __func__, ob->id.name);
95
96         /* get local matrix (but don't calculate it, as that was done already!) */
97         // XXX: redundant?
98         copy_m4_m4(locmat, ob->obmat);
99
100         /* get parent effect matrix */
101         BKE_object_get_parent_matrix(scene, ob, par, totmat);
102
103         /* total */
104         mul_m4_m4m4(tmat, totmat, ob->parentinv);
105         mul_m4_m4m4(ob->obmat, tmat, locmat);
106
107         /* origin, for help line */
108         if ((ob->partype & PARTYPE) == PARSKEL) {
109                 copy_v3_v3(ob->orig, par->obmat[3]);
110         }
111         else {
112                 copy_v3_v3(ob->orig, totmat[3]);
113         }
114 }
115
116 void BKE_object_eval_constraints(EvaluationContext *eval_ctx,
117                                  Scene *scene,
118                                  Object *ob)
119 {
120         bConstraintOb *cob;
121         float ctime = BKE_scene_frame_get(scene);
122
123         DEBUG_PRINT("%s on %s\n", __func__, ob->id.name);
124
125         /* evaluate constraints stack */
126         /* TODO: split this into:
127          * - pre (i.e. BKE_constraints_make_evalob, per-constraint (i.e.
128          * - inner body of BKE_constraints_solve),
129          * - post (i.e. BKE_constraints_clear_evalob)
130          *
131          * Not sure why, this is from Joshua - sergey
132          *
133          */
134         cob = BKE_constraints_make_evalob(scene, ob, NULL, CONSTRAINT_OBTYPE_OBJECT);
135         BKE_constraints_solve(eval_ctx, &ob->constraints, cob, ctime);
136         BKE_constraints_clear_evalob(cob);
137 }
138
139 void BKE_object_eval_done(EvaluationContext *UNUSED(eval_ctx), Object *ob)
140 {
141         DEBUG_PRINT("%s on %s\n", __func__, ob->id.name);
142
143         /* Set negative scale flag in object. */
144         if (is_negative_m4(ob->obmat)) ob->transflag |= OB_NEG_SCALE;
145         else ob->transflag &= ~OB_NEG_SCALE;
146 }
147
148 void BKE_object_handle_data_update(EvaluationContext *eval_ctx,
149                                    Scene *scene,
150                                    Object *ob)
151 {
152         ID *data_id = (ID *)ob->data;
153         AnimData *adt = BKE_animdata_from_id(data_id);
154         Key *key;
155         float ctime = BKE_scene_frame_get(scene);
156
157         if (G.debug & G_DEBUG_DEPSGRAPH)
158                 printf("recalcdata %s\n", ob->id.name + 2);
159
160         /* TODO(sergey): Only used by legacy depsgraph. */
161         if (adt) {
162                 /* evaluate drivers - datalevel */
163                 /* XXX: for mesh types, should we push this to derivedmesh instead? */
164                 BKE_animsys_evaluate_animdata(scene, data_id, adt, ctime, ADT_RECALC_DRIVERS);
165         }
166
167         /* TODO(sergey): Only used by legacy depsgraph. */
168         key = BKE_key_from_object(ob);
169         if (key && key->block.first) {
170                 if (!(ob->shapeflag & OB_SHAPE_LOCK))
171                         BKE_animsys_evaluate_animdata(scene, &key->id, key->adt, ctime, ADT_RECALC_DRIVERS);
172         }
173
174         /* includes all keys and modifiers */
175         switch (ob->type) {
176                 case OB_MESH:
177                 {
178                         BMEditMesh *em = (ob == scene->obedit) ? BKE_editmesh_from_object(ob) : NULL;
179                         uint64_t data_mask = scene->customdata_mask | CD_MASK_BAREMESH;
180 #ifdef WITH_FREESTYLE
181                         /* make sure Freestyle edge/face marks appear in DM for render (see T40315) */
182                         if (eval_ctx->mode != DAG_EVAL_VIEWPORT) {
183                                 data_mask |= CD_MASK_FREESTYLE_EDGE | CD_MASK_FREESTYLE_FACE;
184                         }
185 #endif
186                         if (em) {
187                                 makeDerivedMesh(eval_ctx, scene, ob, em,  data_mask, false); /* was CD_MASK_BAREMESH */
188                         }
189                         else {
190                                 makeDerivedMesh(eval_ctx, scene, ob, NULL, data_mask, false);
191                         }
192                         break;
193                 }
194                 case OB_ARMATURE:
195                         if (ID_IS_LINKED_DATABLOCK(ob) && ob->proxy_from) {
196                                 if (BKE_pose_copy_result(ob->pose, ob->proxy_from->pose) == false) {
197                                         printf("Proxy copy error, lib Object: %s proxy Object: %s\n",
198                                                ob->id.name + 2, ob->proxy_from->id.name + 2);
199                                 }
200                         }
201                         else {
202                                 BKE_pose_where_is(eval_ctx, scene, ob);
203                         }
204                         break;
205
206                 case OB_MBALL:
207                         BKE_displist_make_mball(eval_ctx, scene, ob);
208                         break;
209
210                 case OB_CURVE:
211                 case OB_SURF:
212                 case OB_FONT:
213                         BKE_displist_make_curveTypes(eval_ctx, scene, ob, 0);
214                         break;
215
216                 case OB_LATTICE:
217                         BKE_lattice_modifiers_calc(eval_ctx, scene, ob);
218                         break;
219
220                 case OB_EMPTY:
221                         if (ob->empty_drawtype == OB_EMPTY_IMAGE && ob->data)
222                                 if (BKE_image_is_animated(ob->data))
223                                         BKE_image_user_check_frame_calc(ob->iuser, (int)ctime, 0);
224                         break;
225         }
226
227         /* related materials */
228         /* XXX: without depsgraph tagging, this will always need to be run, which will be slow!
229          * However, not doing anything (or trying to hack around this lack) is not an option
230          * anymore, especially due to Cycles [#31834]
231          */
232         if (ob->totcol) {
233                 int a;
234                 if (ob->totcol != 0) {
235                         BLI_mutex_lock(&material_lock);
236                         for (a = 1; a <= ob->totcol; a++) {
237                                 Material *ma = give_current_material(ob, a);
238                                 if (ma) {
239                                         /* recursively update drivers for this material */
240                                         material_drivers_update(scene, ma, ctime);
241                                 }
242                         }
243                         BLI_mutex_unlock(&material_lock);
244                 }
245         }
246         else if (ob->type == OB_LAMP)
247                 lamp_drivers_update(scene, ob->data, ctime);
248
249         /* particles */
250         if (ob != scene->obedit && ob->particlesystem.first) {
251                 ParticleSystem *tpsys, *psys;
252                 DerivedMesh *dm;
253                 ob->transflag &= ~OB_DUPLIPARTS;
254                 psys = ob->particlesystem.first;
255                 while (psys) {
256                         /* ensure this update always happens even if psys is disabled */
257                         if (psys->recalc & PSYS_RECALC_TYPE) {
258                                 psys_changed_type(ob, psys);
259                         }
260
261                         if (psys_check_enabled(ob, psys, eval_ctx->mode == DAG_EVAL_RENDER)) {
262                                 /* check use of dupli objects here */
263                                 if (psys->part && (psys->part->draw_as == PART_DRAW_REND || eval_ctx->mode == DAG_EVAL_RENDER) &&
264                                     ((psys->part->ren_as == PART_DRAW_OB && psys->part->dup_ob) ||
265                                      (psys->part->ren_as == PART_DRAW_GR && psys->part->dup_group)))
266                                 {
267                                         ob->transflag |= OB_DUPLIPARTS;
268                                 }
269
270                                 particle_system_update(eval_ctx, scene, ob, psys, (eval_ctx->mode == DAG_EVAL_RENDER));
271                                 psys = psys->next;
272                         }
273                         else if (psys->flag & PSYS_DELETE) {
274                                 tpsys = psys->next;
275                                 BLI_remlink(&ob->particlesystem, psys);
276                                 psys_free(ob, psys);
277                                 psys = tpsys;
278                         }
279                         else
280                                 psys = psys->next;
281                 }
282
283                 if (eval_ctx->mode == DAG_EVAL_RENDER && ob->transflag & OB_DUPLIPARTS) {
284                         /* this is to make sure we get render level duplis in groups:
285                          * the derivedmesh must be created before init_render_mesh,
286                          * since object_duplilist does dupliparticles before that */
287                         CustomDataMask data_mask = CD_MASK_BAREMESH | CD_MASK_MFACE | CD_MASK_MTFACE | CD_MASK_MCOL;
288                         dm = mesh_create_derived_render(eval_ctx, scene, ob, data_mask);
289                         dm->release(dm);
290
291                         for (psys = ob->particlesystem.first; psys; psys = psys->next)
292                                 psys_get_modifier(ob, psys)->flag &= ~eParticleSystemFlag_psys_updated;
293                 }
294         }
295
296         /* quick cache removed */
297 }
298
299 void BKE_object_eval_uber_transform(EvaluationContext *UNUSED(eval_ctx),
300                                     Scene *UNUSED(scene),
301                                     Object *ob)
302 {
303         /* TODO(sergey): Currently it's a duplicate of logic in BKE_object_handle_update_ex(). */
304         // XXX: it's almost redundant now...
305
306         /* Handle proxy copy for target, */
307         if (ID_IS_LINKED_DATABLOCK(ob) && ob->proxy_from) {
308                 if (ob->proxy_from->proxy_group) {
309                         /* Transform proxy into group space. */
310                         Object *obg = ob->proxy_from->proxy_group;
311                         float imat[4][4];
312                         invert_m4_m4(imat, obg->obmat);
313                         mul_m4_m4m4(ob->obmat, imat, ob->proxy_from->obmat);
314                         /* Should always be true. */
315                         if (obg->dup_group) {
316                                 add_v3_v3(ob->obmat[3], obg->dup_group->dupli_ofs);
317                         }
318                 }
319                 else
320                         copy_m4_m4(ob->obmat, ob->proxy_from->obmat);
321         }
322
323         ob->recalc &= ~(OB_RECALC_OB | OB_RECALC_TIME);
324         if (ob->data == NULL) {
325                 ob->recalc &= ~OB_RECALC_DATA;
326         }
327 }
328
329 void BKE_object_eval_uber_data(EvaluationContext *eval_ctx,
330                                Scene *scene,
331                                Object *ob)
332 {
333         DEBUG_PRINT("%s on %s\n", __func__, ob->id.name);
334         BLI_assert(ob->type != OB_ARMATURE);
335         BKE_object_handle_data_update(eval_ctx, scene, ob);
336
337         switch (ob->type) {
338                 case OB_MESH:
339                         BKE_mesh_batch_cache_dirty(ob->data, BKE_MESH_BATCH_DIRTY_ALL);
340                         break;
341                 case OB_LATTICE:
342                         BKE_lattice_batch_cache_dirty(ob->data, BKE_LATTICE_BATCH_DIRTY_ALL);
343                         break;
344                 case OB_CURVE:
345                 case OB_FONT:
346                 case OB_SURF:
347                         BKE_curve_batch_cache_dirty(ob->data, BKE_CURVE_BATCH_DIRTY_ALL);
348                         break;
349         }
350
351 #ifdef WITH_COPY_ON_WRITE
352         if (ob->type == OB_MESH) {
353                 /* Quick hack to convert evaluated derivedMesh to Mesh. */
354                 DerivedMesh *dm = ob->derivedFinal;
355                 if (dm != NULL) {
356                         Mesh *mesh = (Mesh *)ob->data;
357                         Mesh *new_mesh = BKE_libblock_alloc_notest(ID_ME);
358                         BKE_mesh_init(new_mesh);
359                         /* Copy ID name so GS(new_mesh->id) works correct later on. */
360                         BLI_strncpy(new_mesh->id.name, mesh->id.name, sizeof(new_mesh->id.name));
361                         /* Copy materials so render engines can access them. */
362                         new_mesh->mat = MEM_dupallocN(mesh->mat);
363                         new_mesh->totcol = mesh->totcol;
364                         DM_to_mesh(dm, new_mesh, ob, CD_MASK_MESH, true);
365                         new_mesh->edit_btmesh = mesh->edit_btmesh;
366                         /* Store result mesh as derived_mesh of object. This way we have
367                          * explicit  way to query final object evaluated data and know for sure
368                          * who owns the newly created mesh datablock.
369                          */
370                         ob->mesh_evaluated = new_mesh;
371                         /* TODO(sergey): This is kind of compatibility thing, so all render
372                          * engines can use object->data for mesh data for display. This is
373                          * something what we might want to change in the future.
374                          */
375                         ob->data = new_mesh;
376                         /* Save some memory by throwing DerivedMesh away. */
377                         /* NOTE: Watch out, some tools might need it!
378                          * So keep around for now..
379                          */
380                 }
381 #if 0
382                 if (ob->derivedFinal != NULL) {
383                         ob->derivedFinal->needsFree = 1;
384                         ob->derivedFinal->release(ob->derivedFinal);
385                         ob->derivedFinal = NULL;
386                 }
387                 if (ob->derivedDeform != NULL) {
388                         ob->derivedDeform->needsFree = 1;
389                         ob->derivedDeform->release(ob->derivedDeform);
390                         ob->derivedDeform = NULL;
391                 }
392 #endif
393         }
394 #endif
395
396         ob->recalc &= ~(OB_RECALC_DATA | OB_RECALC_TIME);
397 }
398
399 void BKE_object_eval_cloth(EvaluationContext *UNUSED(eval_ctx), Scene *scene, Object *object)
400 {
401         DEBUG_PRINT("%s on %s\n", __func__, object->id.name);
402         BKE_ptcache_object_reset(scene, object, PTCACHE_RESET_DEPSGRAPH);
403 }
404
405 void BKE_object_eval_update_shading(EvaluationContext *UNUSED(eval_ctx), Object *object)
406 {
407         DEBUG_PRINT("%s on %s\n", __func__, object->id.name);
408         if (object->type == OB_MESH) {
409                 BKE_mesh_batch_cache_dirty(object->data, BKE_MESH_BATCH_DIRTY_SHADING);
410         }
411 }