Merge branch 'master' into blender2.8
[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
35 #include "intern/depsgraph_types.h"
36
37 #include "DNA_ID.h"
38
39 #include "RNA_access.h"
40 #include "RNA_types.h"
41
42 #include "BLI_utildefines.h"
43 #include "BLI_string.h"
44
45 #include "intern/nodes/deg_node.h"
46 #include "intern/nodes/deg_node_operation.h"
47
48 struct BaseLegacy;
49 struct bGPdata;
50 struct CacheFile;
51 struct ListBase;
52 struct GHash;
53 struct ID;
54 struct FCurve;
55 struct Group;
56 struct Key;
57 struct LayerCollection;
58 struct Main;
59 struct Mask;
60 struct Material;
61 struct MTex;
62 struct ModifierData;
63 struct MovieClip;
64 struct bNodeTree;
65 struct Object;
66 struct bPoseChannel;
67 struct bConstraint;
68 struct Scene;
69 struct Tex;
70 struct World;
71 struct EffectorWeights;
72 struct ParticleSystem;
73 struct ParticleSettings;
74
75 struct PropertyRNA;
76
77 namespace DEG {
78
79 struct Depsgraph;
80 struct DepsNode;
81 struct DepsNodeHandle;
82 struct RootDepsNode;
83 struct IDDepsNode;
84 struct TimeSourceDepsNode;
85 struct ComponentDepsNode;
86 struct OperationDepsNode;
87 struct RootPChanMap;
88
89 struct TimeSourceKey
90 {
91         TimeSourceKey();
92         TimeSourceKey(ID *id);
93
94         string identifier() const;
95
96         ID *id;
97 };
98
99 struct ComponentKey
100 {
101         ComponentKey();
102         ComponentKey(ID *id, eDepsNode_Type type, const char *name = "");
103
104         string identifier() const;
105
106         ID *id;
107         eDepsNode_Type type;
108         const char *name;
109 };
110
111 struct OperationKey
112 {
113         OperationKey();
114         OperationKey(ID *id,
115                      eDepsNode_Type component_type,
116                      const char *name,
117                      int name_tag = -1);
118         OperationKey(ID *id,
119                      eDepsNode_Type component_type,
120                      const char *component_name,
121                      const char *name,
122                      int name_tag);
123
124         OperationKey(ID *id,
125                      eDepsNode_Type component_type,
126                      eDepsOperation_Code opcode);
127         OperationKey(ID *id,
128                      eDepsNode_Type component_type,
129                      const char *component_name,
130                      eDepsOperation_Code opcode);
131
132         OperationKey(ID *id,
133                      eDepsNode_Type component_type,
134                      eDepsOperation_Code opcode,
135                      const char *name,
136                      int name_tag = -1);
137         OperationKey(ID *id,
138                      eDepsNode_Type component_type,
139                      const char *component_name,
140                      eDepsOperation_Code opcode,
141                      const char *name,
142                      int name_tag = -1);
143
144         string identifier() const;
145
146         ID *id;
147         eDepsNode_Type component_type;
148         const char *component_name;
149         eDepsOperation_Code opcode;
150         const char *name;
151         int name_tag;
152 };
153
154 struct RNAPathKey
155 {
156         /* NOTE: see depsgraph_build.cpp for implementation */
157         RNAPathKey(ID *id, const char *path);
158
159         RNAPathKey(ID *id, const PointerRNA &ptr, PropertyRNA *prop);
160
161         string identifier() const;
162
163         ID *id;
164         PointerRNA ptr;
165         PropertyRNA *prop;
166 };
167
168 struct DepsgraphRelationBuilder
169 {
170         DepsgraphRelationBuilder(Main *bmain, Depsgraph *graph);
171
172         void begin_build();
173
174         template <typename KeyFrom, typename KeyTo>
175         void add_relation(const KeyFrom& key_from,
176                           const KeyTo& key_to,
177                           const char *description);
178
179         template <typename KeyTo>
180         void add_relation(const TimeSourceKey& key_from,
181                           const KeyTo& key_to,
182                           const char *description);
183
184         template <typename KeyType>
185         void add_node_handle_relation(const KeyType& key_from,
186                                       const DepsNodeHandle *handle,
187                                       const char *description);
188
189         void build_scene(Scene *scene);
190         void build_group(Object *object, Group *group);
191         void build_object(Object *ob);
192         void build_object_parent(Object *ob);
193         void build_constraints(ID *id,
194                                eDepsNode_Type component_type,
195                                const char *component_subdata,
196                                ListBase *constraints,
197                                RootPChanMap *root_map);
198         void build_animdata(ID *id);
199         void build_driver(ID *id, FCurve *fcurve);
200         void build_world(World *world);
201         void build_rigidbody(Scene *scene);
202         void build_particles(Object *ob);
203         void build_particle_settings(ParticleSettings *part);
204         void build_cloth(Object *object, ModifierData *md);
205         void build_ik_pose(Object *ob,
206                            bPoseChannel *pchan,
207                            bConstraint *con,
208                            RootPChanMap *root_map);
209         void build_splineik_pose(Object *ob,
210                                  bPoseChannel *pchan,
211                                  bConstraint *con,
212                                  RootPChanMap *root_map);
213         void build_rig(Object *ob);
214         void build_proxy_rig(Object *ob);
215         void build_shapekeys(ID *obdata, Key *key);
216         void build_obdata_geom(Object *ob);
217         void build_camera(Object *ob);
218         void build_lamp(Object *ob);
219         void build_nodetree(bNodeTree *ntree);
220         void build_material(Material *ma);
221         void build_texture(Tex *tex);
222         void build_texture_stack(MTex **texture_stack);
223         void build_compositor(Scene *scene);
224         void build_gpencil(bGPdata *gpd);
225         void build_cachefile(CacheFile *cache_file);
226         void build_mask(Mask *mask);
227         void build_movieclip(MovieClip *clip);
228         void build_lightprobe(Object *object);
229
230         void add_collision_relations(const OperationKey &key,
231                                      Scene *scene,
232                                      Object *ob,
233                                      Group *group,
234                                      bool dupli,
235                                      const char *name);
236         void add_forcefield_relations(const OperationKey &key,
237                                       Scene *scene,
238                                       Object *ob,
239                                       ParticleSystem *psys,
240                                       EffectorWeights *eff,
241                                       bool add_absorption, const char *name);
242
243         struct LayerCollectionState {
244                 int index;
245                 OperationKey init_key;
246                 OperationKey done_key;
247                 OperationKey prev_key;
248         };
249         void build_layer_collection(Scene *scene,
250                                     LayerCollection *layer_collection,
251                                     LayerCollectionState *state);
252         void build_layer_collections(Scene *scene,
253                                      ListBase *layer_collections,
254                                      LayerCollectionState *state);
255         void build_scene_layer_collections(Scene *scene);
256
257         void build_copy_on_write_relations();
258         void build_copy_on_write_relations(IDDepsNode *id_node);
259
260         template <typename KeyType>
261         OperationDepsNode *find_operation_node(const KeyType &key);
262
263         Depsgraph *getGraph();
264
265 protected:
266         TimeSourceDepsNode *find_node(const TimeSourceKey &key) const;
267         ComponentDepsNode *find_node(const ComponentKey &key) const;
268         OperationDepsNode *find_node(const OperationKey &key) const;
269         DepsNode *find_node(const RNAPathKey &key) const;
270         OperationDepsNode *has_node(const OperationKey &key) const;
271
272         void add_time_relation(TimeSourceDepsNode *timesrc,
273                                DepsNode *node_to,
274                                const char *description);
275         void add_operation_relation(OperationDepsNode *node_from,
276                                     OperationDepsNode *node_to,
277                                     const char *description);
278
279         template <typename KeyType>
280         DepsNodeHandle create_node_handle(const KeyType& key,
281                                           const char *default_name = "");
282
283         bool needs_animdata_node(ID *id);
284
285 private:
286         /* State which never changes, same for the whole builder time. */
287         Main *bmain_;
288         Depsgraph *graph_;
289
290         /* State which demotes currently built entities. */
291         Scene *scene_;
292 };
293
294 struct DepsNodeHandle
295 {
296         DepsNodeHandle(DepsgraphRelationBuilder *builder, OperationDepsNode *node, const char *default_name = "") :
297             builder(builder),
298             node(node),
299             default_name(default_name)
300         {
301                 BLI_assert(node != NULL);
302         }
303
304         DepsgraphRelationBuilder *builder;
305         OperationDepsNode *node;
306         const char *default_name;
307 };
308
309 /* Utilities for Builders ----------------------------------------------------- */
310
311 template <typename KeyType>
312 OperationDepsNode *DepsgraphRelationBuilder::find_operation_node(const KeyType& key)
313 {
314         DepsNode *node = find_node(key);
315         return node != NULL ? node->get_exit_operation() : NULL;
316 }
317
318 template <typename KeyFrom, typename KeyTo>
319 void DepsgraphRelationBuilder::add_relation(const KeyFrom &key_from,
320                                             const KeyTo &key_to,
321                                             const char *description)
322 {
323         DepsNode *node_from = find_node(key_from);
324         DepsNode *node_to = find_node(key_to);
325         OperationDepsNode *op_from = node_from ? node_from->get_exit_operation() : NULL;
326         OperationDepsNode *op_to = node_to ? node_to->get_entry_operation() : NULL;
327         if (op_from && op_to) {
328                 add_operation_relation(op_from, op_to, description);
329         }
330         else {
331                 if (!op_from) {
332                         /* XXX TODO handle as error or report if needed */
333                         node_from = find_node(key_from);
334                         fprintf(stderr, "add_relation(%s) - Could not find op_from (%s)\n",
335                                 description, key_from.identifier().c_str());
336                 }
337                 else {
338                         fprintf(stderr, "add_relation(%s) - Failed, but op_from (%s) was ok\n",
339                                 description, key_from.identifier().c_str());
340                 }
341                 if (!op_to) {
342                         /* XXX TODO handle as error or report if needed */
343                         fprintf(stderr, "add_relation(%s) - Could not find op_to (%s)\n",
344                                 description, key_to.identifier().c_str());
345                 }
346                 else {
347                         fprintf(stderr, "add_relation(%s) - Failed, but op_to (%s) was ok\n",
348                                 description, key_to.identifier().c_str());
349                 }
350         }
351 }
352
353 template <typename KeyTo>
354 void DepsgraphRelationBuilder::add_relation(const TimeSourceKey &key_from,
355                                             const KeyTo &key_to,
356                                             const char *description)
357 {
358         TimeSourceDepsNode *time_from = find_node(key_from);
359         DepsNode *node_to = find_node(key_to);
360         OperationDepsNode *op_to = node_to ? node_to->get_entry_operation() : NULL;
361         if (time_from && op_to) {
362                 add_time_relation(time_from, op_to, description);
363         }
364         else {
365         }
366 }
367
368 template <typename KeyType>
369 void DepsgraphRelationBuilder::add_node_handle_relation(
370         const KeyType &key_from,
371         const DepsNodeHandle *handle,
372         const char *description)
373 {
374         DepsNode *node_from = find_node(key_from);
375         OperationDepsNode *op_from = node_from ? node_from->get_exit_operation() : NULL;
376         OperationDepsNode *op_to = handle->node->get_entry_operation();
377         if (op_from && op_to) {
378                 add_operation_relation(op_from, op_to, description);
379         }
380         else {
381                 if (!op_from) {
382                         fprintf(stderr, "add_node_handle_relation(%s) - Could not find op_from (%s)\n",
383                                 description, key_from.identifier().c_str());
384                 }
385                 if (!op_to) {
386                         fprintf(stderr, "add_node_handle_relation(%s) - Could not find op_to (%s)\n",
387                                 description, key_from.identifier().c_str());
388                 }
389         }
390 }
391
392 template <typename KeyType>
393 DepsNodeHandle DepsgraphRelationBuilder::create_node_handle(
394         const KeyType &key,
395         const char *default_name)
396 {
397         return DepsNodeHandle(this, find_node(key), default_name);
398 }
399
400 }  // namespace DEG