Blender V4.3
bvh/node.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2009-2010 NVIDIA Corporation
2 * SPDX-FileCopyrightText: 2011-2022 Blender Foundation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Adapted code from NVIDIA Corporation. */
7
8#include "bvh/node.h"
9
10#include "bvh/build.h"
11#include "bvh/bvh.h"
12
13#include "util/vector.h"
14
16
17/* BVH Node */
18
20{
21 int cnt = 0;
22
23 switch (stat) {
25 cnt = 1;
26 break;
28 cnt = is_leaf() ? 1 : 0;
29 break;
31 cnt = is_leaf() ? 0 : 1;
32 break;
34 cnt = is_leaf() ? reinterpret_cast<const LeafNode *>(this)->num_triangles() : 0;
35 break;
37 cnt = num_children();
38 break;
40 if (!is_unaligned) {
41 cnt = 1;
42 }
43 break;
45 if (is_unaligned) {
46 cnt = 1;
47 }
48 break;
50 if (!is_leaf()) {
51 bool has_unaligned = false;
52 for (int j = 0; j < num_children(); j++) {
54 }
55 cnt += has_unaligned ? 0 : 1;
56 }
57 break;
59 if (!is_leaf()) {
60 bool has_unaligned = false;
61 for (int j = 0; j < num_children(); j++) {
63 }
64 cnt += has_unaligned ? 1 : 0;
65 }
66 break;
68 cnt = (is_leaf() && !is_unaligned) ? 1 : 0;
69 break;
71 cnt = (is_leaf() && is_unaligned) ? 1 : 0;
72 break;
73 case BVH_STAT_DEPTH:
74 if (is_leaf()) {
75 cnt = 1;
76 }
77 else {
78 for (int i = 0; i < num_children(); i++) {
79 cnt = max(cnt, get_child(i)->getSubtreeSize(stat));
80 }
81 cnt += 1;
82 }
83 return cnt;
84 default:
85 assert(0); /* unknown mode */
86 }
87
88 if (!is_leaf()) {
89 for (int i = 0; i < num_children(); i++) {
90 cnt += get_child(i)->getSubtreeSize(stat);
91 }
92 }
93
94 return cnt;
95}
96
98{
99 for (int i = 0; i < num_children(); i++) {
100 if (get_child(i)) {
102 }
103 }
104
105 delete this;
106}
107
108float BVHNode::computeSubtreeSAHCost(const BVHParams &p, float probability) const
109{
110 float SAH = probability * p.cost(num_children(), num_triangles());
111
112 for (int i = 0; i < num_children(); i++) {
113 BVHNode *child = get_child(i);
114 SAH += child->computeSubtreeSAHCost(
115 p, probability * child->bounds.safe_area() / bounds.safe_area());
116 }
117
118 return SAH;
119}
120
122{
123 if (!is_leaf() && visibility == 0) {
124 InnerNode *inner = (InnerNode *)this;
125 BVHNode *child0 = inner->children[0];
126 BVHNode *child1 = inner->children[1];
127
128 visibility = child0->update_visibility() | child1->update_visibility();
129 }
130
131 return visibility;
132}
133
135{
136 if (!is_leaf()) {
137 InnerNode *inner = (InnerNode *)this;
138 BVHNode *child0 = inner->children[0];
139 BVHNode *child1 = inner->children[1];
140 child0->update_time();
141 child1->update_time();
142 time_from = min(child0->time_from, child1->time_from);
143 time_to = max(child0->time_to, child1->time_to);
144 }
145}
146
147namespace {
148
149struct DumpTraversalContext {
150 /* Descriptor of while where writing is happening. */
151 FILE *stream;
152 /* Unique identifier of the node current. */
153 int id;
154};
155
156void dump_subtree(DumpTraversalContext *context, const BVHNode *node, const BVHNode *parent = NULL)
157{
158 if (node->is_leaf()) {
159 fprintf(context->stream,
160 " node_%p [label=\"%d\",fillcolor=\"#ccccee\",style=filled]\n",
161 node,
162 context->id);
163 }
164 else {
165 fprintf(context->stream,
166 " node_%p [label=\"%d\",fillcolor=\"#cceecc\",style=filled]\n",
167 node,
168 context->id);
169 }
170 if (parent != NULL) {
171 fprintf(context->stream, " node_%p -> node_%p;\n", parent, node);
172 }
173 context->id += 1;
174 for (int i = 0; i < node->num_children(); ++i) {
175 dump_subtree(context, node->get_child(i), node);
176 }
177}
178
179} // namespace
180
181void BVHNode::dump_graph(const char *filename)
182{
183 DumpTraversalContext context;
184 context.stream = fopen(filename, "w");
185 if (context.stream == NULL) {
186 return;
187 }
188 context.id = 0;
189 fprintf(context.stream, "digraph BVH {\n");
190 dump_subtree(&context, this);
191 fprintf(context.stream, "}\n");
192 fclose(context.stream);
193}
194
195/* Inner Node */
196
197void InnerNode::print(int depth) const
198{
199 for (int i = 0; i < depth; i++) {
200 printf(" ");
201 }
202
203 printf("inner node %p\n", (void *)this);
204
205 if (children[0]) {
206 children[0]->print(depth + 1);
207 }
208 if (children[1]) {
209 children[1]->print(depth + 1);
210 }
211}
212
213void LeafNode::print(int depth) const
214{
215 for (int i = 0; i < depth; i++) {
216 printf(" ");
217 }
218
219 printf("leaf node %d to %d\n", lo, hi);
220}
221
unsigned int uint
BVH_STAT
Definition bvh/node.h:16
@ BVH_STAT_TRIANGLE_COUNT
Definition bvh/node.h:20
@ BVH_STAT_NODE_COUNT
Definition bvh/node.h:17
@ BVH_STAT_CHILDNODE_COUNT
Definition bvh/node.h:21
@ BVH_STAT_ALIGNED_COUNT
Definition bvh/node.h:22
@ BVH_STAT_ALIGNED_INNER_COUNT
Definition bvh/node.h:24
@ BVH_STAT_ALIGNED_LEAF_COUNT
Definition bvh/node.h:26
@ BVH_STAT_DEPTH
Definition bvh/node.h:28
@ BVH_STAT_UNALIGNED_COUNT
Definition bvh/node.h:23
@ BVH_STAT_UNALIGNED_LEAF_COUNT
Definition bvh/node.h:27
@ BVH_STAT_UNALIGNED_INNER_COUNT
Definition bvh/node.h:25
@ BVH_STAT_INNER_COUNT
Definition bvh/node.h:18
@ BVH_STAT_LEAF_COUNT
Definition bvh/node.h:19
__forceinline float cost(int num_nodes, int num_primitives) const
Definition params.h:149
void print(int depth) const
Definition bvh/node.cpp:197
BVHNode * children[kNumMaxChildren]
Definition bvh/node.h:197
void print(int depth) const
Definition bvh/node.cpp:213
#define printf
#define CCL_NAMESPACE_END
#define NULL
#define min(a, b)
Definition sort.c:32
int getSubtreeSize(BVH_STAT stat=BVH_STAT_NODE_COUNT) const
Definition bvh/node.cpp:19
void update_time()
Definition bvh/node.cpp:134
uint visibility
Definition bvh/node.h:94
uint update_visibility()
Definition bvh/node.cpp:121
virtual int num_children() const =0
float time_to
Definition bvh/node.h:103
float computeSubtreeSAHCost(const BVHParams &p, float probability=1.0f) const
Definition bvh/node.cpp:108
bool is_unaligned
Definition bvh/node.h:96
virtual bool is_leaf() const =0
void deleteSubtree()
Definition bvh/node.cpp:97
float time_from
Definition bvh/node.h:103
bool has_unaligned() const
Definition bvh/node.h:68
virtual int num_triangles() const
Definition bvh/node.h:43
virtual void print(int depth=0) const =0
virtual BVHNode * get_child(int i) const =0
BoundBox bounds
Definition bvh/node.h:93
void dump_graph(const char *filename)
Definition bvh/node.cpp:181
__forceinline float safe_area() const
Definition boundbox.h:93
float max