Blender  V2.93
deg_builder_remove_noop.cc
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2020 Blender Foundation.
17  * All rights reserved.
18  */
19 
25 
26 #include "MEM_guardedalloc.h"
27 
28 #include "intern/node/deg_node.h"
30 
31 #include "intern/debug/deg_debug.h"
32 #include "intern/depsgraph.h"
34 #include "intern/depsgraph_type.h"
35 
36 namespace blender::deg {
37 
38 static inline bool is_unused_noop(OperationNode *op_node)
39 {
40  if (op_node == nullptr) {
41  return false;
42  }
43  if (op_node->flag & OperationFlag::DEPSOP_FLAG_PINNED) {
44  return false;
45  }
46  return op_node->is_noop() && op_node->outlinks.is_empty();
47 }
48 
50 {
51  int num_removed_relations = 0;
52  deque<OperationNode *> queue;
53 
55  if (is_unused_noop(node)) {
56  queue.push_back(node);
57  }
58  }
59 
60  while (!queue.empty()) {
61  OperationNode *to_remove = queue.front();
62  queue.pop_front();
63 
64  while (!to_remove->inlinks.is_empty()) {
65  Relation *rel_in = to_remove->inlinks[0];
66  Node *dependency = rel_in->from;
67 
68  /* Remove the relation. */
69  rel_in->unlink();
70  delete rel_in;
71  num_removed_relations++;
72 
73  /* Queue parent no-op node that has now become unused. */
74  OperationNode *operation = dependency->get_exit_operation();
75  if (is_unused_noop(operation)) {
76  queue.push_back(operation);
77  }
78  }
79 
80  /* TODO(Sybren): Remove the node itself. */
81  }
82 
84  (::Depsgraph *)graph, BUILD, "Removed %d relations to no-op nodes\n", num_removed_relations);
85 }
86 
87 } // namespace blender::deg
Read Guarded memory(de)allocation.
bool is_empty() const
Definition: BLI_vector.hh:674
OperationNode * node
Depsgraph * graph
#define DEG_DEBUG_PRINTF(depsgraph, type,...)
Definition: deg_debug.h:68
ThreadQueue * queue
all scheduled work for the cpu
void deg_graph_remove_unused_noops(Depsgraph *graph)
static bool is_unused_noop(OperationNode *op_node)
OperationNodes operations
Definition: depsgraph.h:126
Relations inlinks
Definition: deg_node.h:175
virtual OperationNode * get_exit_operation()
Definition: deg_node.h:203
Relations outlinks
Definition: deg_node.h:176