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