Depsgraph: Comb code to a better state all over
[blender.git] / source / blender / depsgraph / intern / builder / deg_builder_nodes.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_nodes.h
28  *  \ingroup depsgraph
29  */
30
31 #pragma once
32
33 #include "intern/builder/deg_builder_map.h"
34 #include "intern/depsgraph_type.h"
35
36 #include "DEG_depsgraph.h"
37
38 #include "intern/node/deg_node_id.h"
39 #include "intern/node/deg_node_operation.h"
40
41 struct Base;
42 struct CacheFile;
43 struct Camera;
44 struct Collection;
45 struct FCurve;
46 struct GHash;
47 struct ID;
48 struct Image;
49 struct Key;
50 struct Lamp;
51 struct LayerCollection;
52 struct LightProbe;
53 struct ListBase;
54 struct MTex;
55 struct Main;
56 struct Mask;
57 struct Material;
58 struct MovieClip;
59 struct Object;
60 struct ParticleSettings;
61 struct Probe;
62 struct Scene;
63 struct Speaker;
64 struct Tex;
65 struct World;
66 struct bAction;
67 struct bArmature;
68 struct bConstraint;
69 struct bGPdata;
70 struct bNodeTree;
71 struct bPoseChannel;
72
73 struct PropertyRNA;
74
75 namespace DEG {
76
77 struct ComponentNode;
78 struct Node;
79 struct Depsgraph;
80 struct IDNode;
81 struct OperationNode;
82 struct TimeSourceNode;
83
84 struct DepsgraphNodeBuilder {
85         DepsgraphNodeBuilder(Main *bmain, Depsgraph *graph);
86         ~DepsgraphNodeBuilder();
87
88         /* For given original ID get ID which is created by CoW system. */
89         ID *get_cow_id(const ID *id_orig) const;
90         /* Similar to above, but for the cases when there is no ID node we create
91          * one. */
92         ID *ensure_cow_id(ID *id_orig);
93
94         /* Helper wrapper function which wraps get_cow_id with a needed type cast. */
95         template<typename T>
96         T *get_cow_datablock(const T *orig) const {
97                 return (T *)get_cow_id(&orig->id);
98         }
99
100         /* For a given COW datablock get corresponding original one. */
101         template<typename T>
102         T *get_orig_datablock(const T *cow) const {
103                 return (T *)cow->id.orig_id;
104         }
105
106         void begin_build();
107         void end_build();
108
109         IDNode *add_id_node(ID *id);
110         IDNode *find_id_node(ID *id);
111         TimeSourceNode *add_time_source();
112
113         ComponentNode *add_component_node(ID *id,
114                                           NodeType comp_type,
115                                           const char *comp_name = "");
116
117         OperationNode *add_operation_node(ComponentNode *comp_node,
118                                           const DepsEvalOperationCb& op,
119                                           OperationCode opcode,
120                                           const char *name = "",
121                                           int name_tag = -1);
122         OperationNode *add_operation_node(ID *id,
123                                           NodeType comp_type,
124                                           const char *comp_name,
125                                           const DepsEvalOperationCb& op,
126                                           OperationCode opcode,
127                                           const char *name = "",
128                                           int name_tag = -1);
129         OperationNode *add_operation_node(ID *id,
130                                           NodeType comp_type,
131                                           const DepsEvalOperationCb& op,
132                                           OperationCode opcode,
133                                           const char *name = "",
134                                           int name_tag = -1);
135
136         OperationNode *ensure_operation_node(ID *id,
137                                              NodeType comp_type,
138                                              const DepsEvalOperationCb& op,
139                                              OperationCode opcode,
140                                              const char *name = "",
141                                              int name_tag = -1);
142
143         bool has_operation_node(ID *id,
144                                 NodeType comp_type,
145                                 const char *comp_name,
146                                 OperationCode opcode,
147                                 const char *name = "",
148                                 int name_tag = -1);
149
150         OperationNode *find_operation_node(ID *id,
151                                            NodeType comp_type,
152                                            const char *comp_name,
153                                            OperationCode opcode,
154                                            const char *name = "",
155                                                int name_tag = -1);
156
157         OperationNode *find_operation_node(ID *id,
158                                            NodeType comp_type,
159                                            OperationCode opcode,
160                                            const char *name = "",
161                                            int name_tag = -1);
162
163         void build_id(ID *id);
164         void build_layer_collections(ListBase *lb);
165         void build_view_layer(Scene *scene,
166                               ViewLayer *view_layer,
167                               eDepsNode_LinkedState_Type linked_state);
168         void build_collection(LayerCollection *from_layer_collection,
169                               Collection *collection);
170         void build_object(int base_index,
171                           Object *object,
172                           eDepsNode_LinkedState_Type linked_state,
173                           bool is_visible);
174         void build_object_flags(int base_index,
175                                 Object *object,
176                                 eDepsNode_LinkedState_Type linked_state);
177         void build_object_data(Object *object, bool is_object_visible);
178         void build_object_data_camera(Object *object);
179         void build_object_data_geometry(Object *object, bool is_object_visible);
180         void build_object_data_geometry_datablock(ID *obdata,
181                                                   bool is_object_visible);
182         void build_object_data_lamp(Object *object);
183         void build_object_data_lightprobe(Object *object);
184         void build_object_data_speaker(Object *object);
185         void build_object_transform(Object *object);
186         void build_object_constraints(Object *object);
187         void build_object_pointcache(Object *object);
188         void build_pose_constraints(Object *object,
189                                     bPoseChannel *pchan,
190                                     int pchan_index,
191                                     bool is_object_visible);
192         void build_rigidbody(Scene *scene);
193         void build_particle_systems(Object *object, bool is_object_visible);
194         void build_particle_settings(ParticleSettings *part);
195         void build_animdata(ID *id);
196         void build_animdata_nlastrip_targets(ListBase *strips);
197         void build_action(bAction *action);
198         void build_driver(ID *id, FCurve *fcurve, int driver_index);
199         void build_driver_variables(ID *id, FCurve *fcurve);
200         void build_driver_id_property(ID *id, const char *rna_path);
201         void build_ik_pose(Object *object,
202                            bPoseChannel *pchan,
203                            bConstraint *con);
204         void build_splineik_pose(Object *object,
205                                  bPoseChannel *pchan,
206                                  bConstraint *con);
207         void build_rig(Object *object, bool is_object_visible);
208         void build_proxy_rig(Object *object);
209         void build_armature(bArmature *armature);
210         void build_shapekeys(Key *key);
211         void build_camera(Camera *camera);
212         void build_lamp(Lamp *lamp);
213         void build_nodetree(bNodeTree *ntree);
214         void build_material(Material *ma);
215         void build_texture(Tex *tex);
216         void build_image(Image *image);
217         void build_world(World *world);
218         void build_compositor(Scene *scene);
219         void build_gpencil(bGPdata *gpd);
220         void build_cachefile(CacheFile *cache_file);
221         void build_mask(Mask *mask);
222         void build_movieclip(MovieClip *clip);
223         void build_lightprobe(LightProbe *probe);
224         void build_speaker(Speaker *speaker);
225
226         /* Per-ID information about what was already in the dependency graph.
227          * Allows to re-use certain values, to speed up following evaluation. */
228         struct IDInfo {
229                 /* Copy-on-written pointer of the corresponding ID. */
230                 ID *id_cow;
231                 /* Mask of visible components from previous state of the
232                  * dependency graph. */
233                 IDComponentsMask previously_visible_components_mask;
234                 /* Special evaluation flag mask from the previous depsgraph. */
235                 uint32_t previous_eval_flags;
236                 /* Mesh CustomData mask from the previous depsgraph. */
237                 uint64_t previous_customdata_mask;
238         };
239
240 protected:
241         /* Allows to identify an operation which was tagged for update at the time
242          * relations are being updated. We can not reuse operation node pointer
243          * since it will change during dependency graph construction. */
244         struct SavedEntryTag {
245                 ID *id_orig;
246                 NodeType component_type;
247                 OperationCode opcode;
248                 const char *name;
249                 int name_tag;
250         };
251         vector<SavedEntryTag> saved_entry_tags_;
252
253         struct BuilderWalkUserData {
254                 DepsgraphNodeBuilder *builder;
255                 /* Denotes whether object the walk is invoked from is visible. */
256                 bool is_parent_visible;
257         };
258         static void modifier_walk(void *user_data,
259                                   struct Object *object,
260                                   struct ID **idpoin,
261                                   int cb_flag);
262         static void constraint_walk(bConstraint *constraint,
263                                     ID **idpoin,
264                                     bool is_reference,
265                                     void *user_data);
266
267         /* State which never changes, same for the whole builder time. */
268         Main *bmain_;
269         Depsgraph *graph_;
270
271         /* State which demotes currently built entities. */
272         Scene *scene_;
273         ViewLayer *view_layer_;
274         int view_layer_index_;
275         /* NOTE: Collection are possibly built recursively, so be careful when
276          * setting the current state. */
277         Collection *collection_;
278         /* Accumulated flag over the hierarchy opf currently building collections.
279          * Denotes whether all the hierarchy from parent of collection_ to the
280          * very root is visible (aka not restricted.). */
281         bool is_parent_collection_visible_;
282
283         /* Indexed by original ID, values are IDInfo. */
284         GHash *id_info_hash_;
285
286         /* Set of IDs which were already build. Makes it easier to keep track of
287          * what was already built and what was not. */
288         BuilderMap built_map_;
289 };
290
291 }  // namespace DEG