face2a1d43fbbcfef951ba3ed3010ef4384abadb
[blender.git] / source / blender / depsgraph / intern / builder / deg_builder_relations.h
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) 2013 Blender Foundation.
19  * All rights reserved.
20  *
21  * Original Author: Lukas Toenne
22  * Contributor(s): None Yet
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/depsgraph/intern/builder/deg_builder_relations.h
28  *  \ingroup depsgraph
29  */
30
31 #pragma once
32
33 #include <cstdio>
34 #include <cstring>
35
36 #include "intern/depsgraph_types.h"
37
38 #include "DNA_ID.h"
39
40 #include "RNA_access.h"
41 #include "RNA_types.h"
42
43 #include "BLI_utildefines.h"
44 #include "BLI_string.h"
45
46 #include "intern/builder/deg_builder_map.h"
47 #include "intern/depsgraph.h"
48 #include "intern/nodes/deg_node.h"
49 #include "intern/nodes/deg_node_component.h"
50 #include "intern/nodes/deg_node_operation.h"
51
52 struct Base;
53 struct bArmature;
54 struct bAction;
55 struct bGPdata;
56 struct CacheFile;
57 struct Camera;
58 struct ListBase;
59 struct GHash;
60 struct ID;
61 struct FCurve;
62 struct Collection;
63 struct Key;
64 struct Lamp;
65 struct LayerCollection;
66 struct LightProbe;
67 struct Main;
68 struct Mask;
69 struct Material;
70 struct MTex;
71 struct ModifierData;
72 struct MovieClip;
73 struct bNodeTree;
74 struct Object;
75 struct bPoseChannel;
76 struct bConstraint;
77 struct ParticleSystem;
78 struct ParticleSettings;
79 struct Scene;
80 struct Speaker;
81 struct ViewLayer;
82 struct Tex;
83 struct World;
84 struct EffectorWeights;
85
86 struct PropertyRNA;
87
88 namespace DEG {
89
90 struct Depsgraph;
91 struct DepsNode;
92 struct DepsNodeHandle;
93 struct DepsRelation;
94 struct RootDepsNode;
95 struct IDDepsNode;
96 struct TimeSourceDepsNode;
97 struct ComponentDepsNode;
98 struct OperationDepsNode;
99 struct RootPChanMap;
100
101 struct TimeSourceKey
102 {
103         TimeSourceKey();
104         TimeSourceKey(ID *id);
105
106         string identifier() const;
107
108         ID *id;
109 };
110
111 struct ComponentKey
112 {
113         ComponentKey();
114         ComponentKey(ID *id, eDepsNode_Type type, const char *name = "");
115
116         string identifier() const;
117
118         ID *id;
119         eDepsNode_Type type;
120         const char *name;
121 };
122
123 struct OperationKey
124 {
125         OperationKey();
126         OperationKey(ID *id,
127                      eDepsNode_Type component_type,
128                      const char *name,
129                      int name_tag = -1);
130         OperationKey(ID *id,
131                      eDepsNode_Type component_type,
132                      const char *component_name,
133                      const char *name,
134                      int name_tag);
135
136         OperationKey(ID *id,
137                      eDepsNode_Type component_type,
138                      eDepsOperation_Code opcode);
139         OperationKey(ID *id,
140                      eDepsNode_Type component_type,
141                      const char *component_name,
142                      eDepsOperation_Code opcode);
143
144         OperationKey(ID *id,
145                      eDepsNode_Type component_type,
146                      eDepsOperation_Code opcode,
147                      const char *name,
148                      int name_tag = -1);
149         OperationKey(ID *id,
150                      eDepsNode_Type component_type,
151                      const char *component_name,
152                      eDepsOperation_Code opcode,
153                      const char *name,
154                      int name_tag = -1);
155
156         string identifier() const;
157
158         ID *id;
159         eDepsNode_Type component_type;
160         const char *component_name;
161         eDepsOperation_Code opcode;
162         const char *name;
163         int name_tag;
164 };
165
166 struct RNAPathKey
167 {
168         /* NOTE: see depsgraph_build.cpp for implementation */
169         RNAPathKey(ID *id, const char *path);
170
171         RNAPathKey(ID *id, const PointerRNA &ptr, PropertyRNA *prop);
172
173         string identifier() const;
174
175         ID *id;
176         PointerRNA ptr;
177         PropertyRNA *prop;
178 };
179
180 struct DepsgraphRelationBuilder
181 {
182         DepsgraphRelationBuilder(Main *bmain, Depsgraph *graph);
183
184         void begin_build();
185
186         template <typename KeyFrom, typename KeyTo>
187         DepsRelation *add_relation(const KeyFrom& key_from,
188                                    const KeyTo& key_to,
189                                    const char *description,
190                                    bool check_unique = false,
191                                    int flags = 0);
192
193         template <typename KeyFrom, typename KeyTo>
194         DepsRelation *add_relation(const KeyFrom& key_from,
195                                    const KeyTo& key_to,
196                                    const char *description,
197                                    eDepsRelation_Flag flag);
198
199         template <typename KeyTo>
200         DepsRelation *add_relation(const TimeSourceKey& key_from,
201                                    const KeyTo& key_to,
202                                    const char *description,
203                                    bool check_unique = false,
204                                    int flags = 0);
205
206         template <typename KeyType>
207         DepsRelation *add_node_handle_relation(const KeyType& key_from,
208                                                const DepsNodeHandle *handle,
209                                                const char *description,
210                                                bool check_unique = false,
211                                                int flags = 0);
212
213         void add_customdata_mask(Object *object, uint64_t mask);
214         void add_special_eval_flag(ID *object, uint32_t flag);
215
216         void build_id(ID *id);
217         void build_layer_collections(ListBase *lb);
218         void build_view_layer(Scene *scene, ViewLayer *view_layer);
219         void build_collection(LayerCollection *from_layer_collection,
220                               Object *object,
221                               Collection *collection);
222         void build_object(Base *base, Object *object);
223         void build_object_flags(Base *base, Object *object);
224         void build_object_data(Object *object);
225         void build_object_data_camera(Object *object);
226         void build_object_data_geometry(Object *object);
227         void build_object_data_geometry_datablock(ID *obdata);
228         void build_object_data_lamp(Object *object);
229         void build_object_data_lightprobe(Object *object);
230         void build_object_data_speaker(Object *object);
231         void build_object_parent(Object *object);
232         void build_object_pointcache(Object *object);
233         void build_constraints(ID *id,
234                                eDepsNode_Type component_type,
235                                const char *component_subdata,
236                                ListBase *constraints,
237                                RootPChanMap *root_map);
238         void build_animdata(ID *id);
239         void build_animdata_curves(ID *id);
240         void build_animdata_curves_targets(ID *id,
241                                            ComponentKey &adt_key,
242                                            OperationDepsNode *operation_from,
243                                            ListBase *curves);
244         void build_animdata_nlastrip_targets(ID *id,
245                                              ComponentKey &adt_key,
246                                              OperationDepsNode *operation_from,
247                                              ListBase *strips);
248         void build_animdata_drivers(ID *id);
249         void build_action(bAction *action);
250         void build_driver(ID *id, FCurve *fcurve);
251         void build_driver_data(ID *id, FCurve *fcurve);
252         void build_driver_variables(ID *id, FCurve *fcurve);
253         void build_world(World *world);
254         void build_rigidbody(Scene *scene);
255         void build_particles(Object *object);
256         void build_particle_settings(ParticleSettings *part);
257         void build_particles_visualization_object(Object *object,
258                                                   ParticleSystem *psys,
259                                                   Object *draw_object);
260         void build_ik_pose(Object *object,
261                            bPoseChannel *pchan,
262                            bConstraint *con,
263                            RootPChanMap *root_map);
264         void build_splineik_pose(Object *object,
265                                  bPoseChannel *pchan,
266                                  bConstraint *con,
267                                  RootPChanMap *root_map);
268         void build_rig(Object *object);
269         void build_proxy_rig(Object *object);
270         void build_shapekeys(Key *key);
271         void build_armature(bArmature *armature);
272         void build_camera(Camera *camera);
273         void build_lamp(Lamp *lamp);
274         void build_nodetree(bNodeTree *ntree);
275         void build_material(Material *ma);
276         void build_texture(Tex *tex);
277         void build_compositor(Scene *scene);
278         void build_gpencil(bGPdata *gpd);
279         void build_cachefile(CacheFile *cache_file);
280         void build_mask(Mask *mask);
281         void build_movieclip(MovieClip *clip);
282         void build_lightprobe(LightProbe *probe);
283         void build_speaker(Speaker *speaker);
284
285         void build_nested_datablock(ID *owner, ID *id);
286         void build_nested_nodetree(ID *owner, bNodeTree *ntree);
287         void build_nested_shapekey(ID *owner, Key *key);
288
289         void add_collision_relations(const OperationKey &key,
290                                      Object *object,
291                                      Collection *collection,
292                                      const char *name);
293         void add_forcefield_relations(const OperationKey &key,
294                                       Object *object,
295                                       ParticleSystem *psys,
296                                       EffectorWeights *eff,
297                                       bool add_absorption, const char *name);
298
299         void build_copy_on_write_relations();
300         void build_copy_on_write_relations(IDDepsNode *id_node);
301
302         template <typename KeyType>
303         OperationDepsNode *find_operation_node(const KeyType &key);
304
305         Depsgraph *getGraph();
306
307 protected:
308         TimeSourceDepsNode *get_node(const TimeSourceKey &key) const;
309         ComponentDepsNode *get_node(const ComponentKey &key) const;
310         OperationDepsNode *get_node(const OperationKey &key) const;
311         DepsNode *get_node(const RNAPathKey &key) const;
312
313         OperationDepsNode *find_node(const OperationKey &key) const;
314         bool has_node(const OperationKey &key) const;
315
316         DepsRelation *add_time_relation(TimeSourceDepsNode *timesrc,
317                                         DepsNode *node_to,
318                                         const char *description,
319                                         bool check_unique = false,
320                                         int flags = 0);
321         DepsRelation *add_operation_relation(OperationDepsNode *node_from,
322                                              OperationDepsNode *node_to,
323                                              const char *description,
324                                              bool check_unique = false,
325                                              int flags = 0);
326
327         template <typename KeyType>
328         DepsNodeHandle create_node_handle(const KeyType& key,
329                                           const char *default_name = "");
330
331         /* TODO(sergey): All those is_same* functions are to be generalized. */
332
333         /* Check whether two keys corresponds to the same bone from same armature.
334          *
335          * This is used by drivers relations builder to avoid possible fake
336          * dependency cycle when one bone property drives another property of the
337          * same bone.
338          */
339         template <typename KeyFrom, typename KeyTo>
340         bool is_same_bone_dependency(const KeyFrom& key_from, const KeyTo& key_to);
341
342         /* Similar to above, but used to check whether driver is using node from
343          * the same node tree as a driver variable.
344          */
345         template <typename KeyFrom, typename KeyTo>
346         bool is_same_nodetree_node_dependency(const KeyFrom& key_from,
347                                               const KeyTo& key_to);
348
349         /* Similar to above, but used to check whether driver is using key from
350          * the same key datablock as a driver variable.
351          */
352         template <typename KeyFrom, typename KeyTo>
353         bool is_same_shapekey_dependency(const KeyFrom& key_from,
354                                          const KeyTo& key_to);
355
356 private:
357         struct BuilderWalkUserData {
358                 DepsgraphRelationBuilder *builder;
359         };
360
361         static void modifier_walk(void *user_data,
362                                   struct Object *object,
363                                   struct ID **idpoin,
364                                   int cb_flag);
365
366         static void constraint_walk(bConstraint *con,
367                                     ID **idpoin,
368                                     bool is_reference,
369                                     void *user_data);
370
371         /* State which never changes, same for the whole builder time. */
372         Main *bmain_;
373         Depsgraph *graph_;
374
375         /* State which demotes currently built entities. */
376         Scene *scene_;
377
378         BuilderMap built_map_;
379 };
380
381 struct DepsNodeHandle
382 {
383         DepsNodeHandle(DepsgraphRelationBuilder *builder,
384                        OperationDepsNode *node,
385                        const char *default_name = "")
386                 : builder(builder),
387                   node(node),
388                   default_name(default_name)
389         {
390                 BLI_assert(node != NULL);
391         }
392
393         DepsgraphRelationBuilder *builder;
394         OperationDepsNode *node;
395         const char *default_name;
396 };
397
398 }  // namespace DEG
399
400
401 #include "intern/builder/deg_builder_relations_impl.h"