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 bGPdata;
49 struct CacheFile;
50 struct ListBase;
51 struct GHash;
52 struct ID;
53 struct FCurve;
54 struct Group;
55 struct Key;
56 struct LayerCollection;
57 struct Main;
58 struct Mask;
59 struct Material;
60 struct MTex;
61 struct ModifierData;
62 struct MovieClip;
63 struct bNodeTree;
64 struct Object;
65 struct bPoseChannel;
66 struct bConstraint;
67 struct Scene;
68 struct ViewLayer;
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_view_layer(Scene *scene, ViewLayer *view_layer);
190         void build_group(Object *object, Group *group);
191         void build_object(Object *object);
192         void build_object_data(Object *object);
193         void build_object_flags(Object *object);
194         void build_object_parent(Object *object);
195         void build_constraints(ID *id,
196                                eDepsNode_Type component_type,
197                                const char *component_subdata,
198                                ListBase *constraints,
199                                RootPChanMap *root_map);
200         void build_animdata(ID *id);
201         void build_driver(ID *id, FCurve *fcurve);
202         void build_world(World *world);
203         void build_rigidbody(Scene *scene);
204         void build_particles(Object *object);
205         void build_particle_settings(ParticleSettings *part);
206         void build_cloth(Object *object, ModifierData *md);
207         void build_ik_pose(Object *object,
208                            bPoseChannel *pchan,
209                            bConstraint *con,
210                            RootPChanMap *root_map);
211         void build_splineik_pose(Object *object,
212                                  bPoseChannel *pchan,
213                                  bConstraint *con,
214                                  RootPChanMap *root_map);
215         void build_rig(Object *object);
216         void build_proxy_rig(Object *object);
217         void build_shapekeys(ID *obdata, Key *key);
218         void build_obdata_geom(Object *object);
219         void build_camera(Object *object);
220         void build_lamp(Object *object);
221         void build_nodetree(bNodeTree *ntree);
222         void build_material(Material *ma);
223         void build_texture(Tex *tex);
224         void build_texture_stack(MTex **texture_stack);
225         void build_compositor(Scene *scene);
226         void build_gpencil(bGPdata *gpd);
227         void build_cachefile(CacheFile *cache_file);
228         void build_mask(Mask *mask);
229         void build_movieclip(MovieClip *clip);
230         void build_lightprobe(Object *object);
231
232         void add_collision_relations(const OperationKey &key,
233                                      Scene *scene,
234                                      Object *object,
235                                      Group *group,
236                                      bool dupli,
237                                      const char *name);
238         void add_forcefield_relations(const OperationKey &key,
239                                       Scene *scene,
240                                       Object *object,
241                                       ParticleSystem *psys,
242                                       EffectorWeights *eff,
243                                       bool add_absorption, const char *name);
244
245         struct LayerCollectionState {
246                 int index;
247                 OperationKey init_key;
248                 OperationKey done_key;
249                 OperationKey prev_key;
250         };
251         void build_layer_collection(LayerCollection *layer_collection,
252                                     LayerCollectionState *state);
253         void build_layer_collections(ListBase *layer_collections,
254                                      LayerCollectionState *state);
255         void build_view_layer_collections(ViewLayer *view_layer);
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 *get_node(const TimeSourceKey &key) const;
267         ComponentDepsNode *get_node(const ComponentKey &key) const;
268         OperationDepsNode *get_node(const OperationKey &key) const;
269         DepsNode *get_node(const RNAPathKey &key) const;
270
271         OperationDepsNode *find_node(const OperationKey &key) const;
272         bool has_node(const OperationKey &key) const;
273
274         void add_time_relation(TimeSourceDepsNode *timesrc,
275                                DepsNode *node_to,
276                                const char *description);
277         void add_operation_relation(OperationDepsNode *node_from,
278                                     OperationDepsNode *node_to,
279                                     const char *description);
280
281         template <typename KeyType>
282         DepsNodeHandle create_node_handle(const KeyType& key,
283                                           const char *default_name = "");
284
285         bool needs_animdata_node(ID *id);
286
287 private:
288         /* State which never changes, same for the whole builder time. */
289         Main *bmain_;
290         Depsgraph *graph_;
291
292         /* State which demotes currently built entities. */
293         Scene *scene_;
294 };
295
296 struct DepsNodeHandle
297 {
298         DepsNodeHandle(DepsgraphRelationBuilder *builder, OperationDepsNode *node, const char *default_name = "") :
299             builder(builder),
300             node(node),
301             default_name(default_name)
302         {
303                 BLI_assert(node != NULL);
304         }
305
306         DepsgraphRelationBuilder *builder;
307         OperationDepsNode *node;
308         const char *default_name;
309 };
310
311 /* Utilities for Builders ----------------------------------------------------- */
312
313 template <typename KeyType>
314 OperationDepsNode *DepsgraphRelationBuilder::find_operation_node(const KeyType& key)
315 {
316         DepsNode *node = get_node(key);
317         return node != NULL ? node->get_exit_operation() : NULL;
318 }
319
320 template <typename KeyFrom, typename KeyTo>
321 void DepsgraphRelationBuilder::add_relation(const KeyFrom &key_from,
322                                             const KeyTo &key_to,
323                                             const char *description)
324 {
325         DepsNode *node_from = get_node(key_from);
326         DepsNode *node_to = get_node(key_to);
327         OperationDepsNode *op_from = node_from ? node_from->get_exit_operation() : NULL;
328         OperationDepsNode *op_to = node_to ? node_to->get_entry_operation() : NULL;
329         if (op_from && op_to) {
330                 add_operation_relation(op_from, op_to, description);
331         }
332         else {
333                 if (!op_from) {
334                         /* XXX TODO handle as error or report if needed */
335                         fprintf(stderr, "add_relation(%s) - Could not find op_from (%s)\n",
336                                 description, key_from.identifier().c_str());
337                 }
338                 else {
339                         fprintf(stderr, "add_relation(%s) - Failed, but op_from (%s) was ok\n",
340                                 description, key_from.identifier().c_str());
341                 }
342                 if (!op_to) {
343                         /* XXX TODO handle as error or report if needed */
344                         fprintf(stderr, "add_relation(%s) - Could not find op_to (%s)\n",
345                                 description, key_to.identifier().c_str());
346                 }
347                 else {
348                         fprintf(stderr, "add_relation(%s) - Failed, but op_to (%s) was ok\n",
349                                 description, key_to.identifier().c_str());
350                 }
351         }
352 }
353
354 template <typename KeyTo>
355 void DepsgraphRelationBuilder::add_relation(const TimeSourceKey &key_from,
356                                             const KeyTo &key_to,
357                                             const char *description)
358 {
359         TimeSourceDepsNode *time_from = get_node(key_from);
360         DepsNode *node_to = get_node(key_to);
361         OperationDepsNode *op_to = node_to ? node_to->get_entry_operation() : NULL;
362         if (time_from != NULL && op_to != NULL) {
363                 add_time_relation(time_from, op_to, description);
364         }
365 }
366
367 template <typename KeyType>
368 void DepsgraphRelationBuilder::add_node_handle_relation(
369         const KeyType &key_from,
370         const DepsNodeHandle *handle,
371         const char *description)
372 {
373         DepsNode *node_from = get_node(key_from);
374         OperationDepsNode *op_from = node_from ? node_from->get_exit_operation() : NULL;
375         OperationDepsNode *op_to = handle->node->get_entry_operation();
376         if (op_from != NULL && op_to != NULL) {
377                 add_operation_relation(op_from, op_to, description);
378         }
379         else {
380                 if (!op_from) {
381                         fprintf(stderr, "add_node_handle_relation(%s) - Could not find op_from (%s)\n",
382                                 description, key_from.identifier().c_str());
383                 }
384                 if (!op_to) {
385                         fprintf(stderr, "add_node_handle_relation(%s) - Could not find op_to (%s)\n",
386                                 description, key_from.identifier().c_str());
387                 }
388         }
389 }
390
391 template <typename KeyType>
392 DepsNodeHandle DepsgraphRelationBuilder::create_node_handle(
393         const KeyType &key,
394         const char *default_name)
395 {
396         return DepsNodeHandle(this, get_node(key), default_name);
397 }
398
399 }  // namespace DEG