-
Notifications
You must be signed in to change notification settings - Fork 0
/
bvh.h
269 lines (221 loc) · 5.84 KB
/
bvh.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#ifndef BVH_H
#define BVH_H
#include "types.h"
#include "math_3d.h"
#include "memcache.h"
#include "list.h"
#include "rbtree.h"
enum {
NR_PLANE_NORMALS = 7,
};
static __constant const vec3_t plane_set_normals[] = {
[0] = {1.0f, 0.0f, 0.0f},
[1] = {0.0f, 1.0f, 0.0f},
[2] = {0.0f, 0.0f, 1.0f},
/* sqrt(3)/3.0f */
[3] = { 0.577350, 0.577350, 0.577350},
[4] = {-0.577350, 0.577350, 0.577350},
[5] = {-0.577350, -0.577350, 0.577350},
[6] = { 0.577350, -0.577350, 0.577350},
};
struct slab {
float near;
float far;
};
struct extent {
/**
* 'd' components for near and far plane-sets of plane equation:
* Ax + By + Cz - d = 0
* N.P - d = 0 ; where P = O + t*Dir
* N.(O + t*Dir) = d
* N.O + t*N.Dir = d
* t = (d - N.O) / (N.Dir); where 't' is a distance
*/
struct slab d[NR_PLANE_NORMALS];
};
struct extent_leaf {
struct extent e;
struct list_head entry; /* entry in octant->leaves */
__global struct triangle_mesh
*mesh;
uint32_t index; /* index of first triangle vertex in mesh */
};
enum {
OCTANT_CACHE_SIZE = 1024,
OCTANT_MAX_LEAVES = 128,
OCTANT_MAX_DEPTH = 32,
};
struct octant {
union {
__global struct octant *octants[8];
struct list_head entry; /* entry in bvh->stat.octants_leaves */
};
struct list_head leaves; /* list of extent_leaf */
struct extent extent;
uint32_t __leaves_cnt; /* see octant_is/set_leaf() */
};
struct octant_cache {
__global struct octant *octants;
};
enum {
LESS_10 = 0,
LESS_100,
LESS_1000,
LESS_10000,
UPPER_BOUND,
};
struct bvhtree_stat {
uint32_t max_depth;
uint32_t max_leaves;
uint32_t num_octants[UPPER_BOUND+1];
struct list_head octants_leaves;
};
struct bvhtree {
struct octant root;
__global struct extent_leaf *leaves;
__global struct allocator *alloc;
struct octant_cache *octant_caches;
struct bvhtree_stat stat;
uint32_t num_caches;
uint32_t num_octants;
};
struct scene;
/* BVH host public API */
void bvhtree_init(struct bvhtree *bvh);
void bvhtree_deinit(struct bvhtree *bvh);
int bvhtree_build(struct bvhtree *bvh, struct scene *scene);
/*
* The following API can be called from any CPU and GPU
*/
static inline bool octant_is_leaf(const __global struct octant *octant)
{
return octant->__leaves_cnt;
}
static inline bool extent_intersect(const __global struct extent *e,
float numerators[NR_PLANE_NORMALS],
float denominators[NR_PLANE_NORMALS],
float *t_near, float *t_far)
{
int i;
for (i = 0; i < ARRAY_SIZE(e->d); i++) {
float t_near_ext = (e->d[i].near - numerators[i]) / denominators[i];
float t_far_ext = (e->d[i].far - numerators[i]) / denominators[i];
if (denominators[i] < 0.0f) {
SWAP(t_near_ext, t_far_ext);
}
if (t_near_ext > *t_near) {
*t_near = t_near_ext;
}
if (t_far_ext < *t_far) {
*t_far = t_far_ext;
}
if (*t_near > *t_far) {
return false;
}
}
return true;
}
struct octant_elem {
struct rb_node entry; /* entry in octant_queue->root */
__global struct octant *octant;
float t;
};
struct octant_queue {
struct memcache mc;
__global struct rb_root *root;
};
static inline int octant_queue_init(struct octant_queue *queue,
__global struct allocator *a)
{
int ret;
ret = memcache_init(&queue->mc, a, sizeof(struct octant_elem));
if (ret)
return ret;
queue->root = NULL;
return 0;
}
static inline int octant_queue_deinit(struct octant_queue *queue)
{
__global struct octant_elem *elem;
__global struct rb_node *node;
if (!queue->root)
return 0;
/* Free queue node by node */
/*
* Optimization hint: don't free node by node, but simply
* free chunks, we don't care about proper queue tree elements
* destruction, we care only about freeing allocator chunks.
*/
while ((node = rb_first(queue->root)) != NULL) {
rb_erase(node, queue->root);
elem = container_of(node, typeof(*elem), entry);
(void)memcache_free(&queue->mc, elem);
}
(void)memcache_free(&queue->mc, queue->root);
return memcache_deinit(&queue->mc);
}
static inline __global struct octant *
octant_queue_pop_first(struct octant_queue *queue, float *t)
{
__global struct octant_elem *elem;
__global struct octant *octant;
__global struct rb_node *node;
if (!queue->root || RB_EMPTY_ROOT(queue->root))
return NULL;
node = rb_first(queue->root);
elem = container_of(node, typeof(*elem), entry);
*t = elem->t;
rb_erase(node, queue->root);
octant = elem->octant;
memcache_free(&queue->mc, elem);
return octant;
}
static float cmp_octants(__global const struct rb_node *a_,
__global const struct rb_node *b_)
{
__global struct octant_elem *a;
__global struct octant_elem *b;
a = container_of(a_, typeof(*a), entry);
b = container_of(b_, typeof(*b), entry);
return a->t - b->t;
}
static inline int octant_queue_insert(struct octant_queue *queue,
__global struct octant *octant,
float t)
{
__global struct rb_node * __global * this;
__global struct rb_node *parent = NULL;
__global struct octant_elem *elem;
__global struct rb_node *new;
float cmp;
if (!queue->root) {
queue->root = memcache_alloc(&queue->mc);
if (!queue->root)
return -ENOMEM;
*queue->root = RB_ROOT;
}
this = &queue->root->rb_node;
elem = memcache_alloc(&queue->mc);
if (!elem)
return -ENOMEM;
elem->octant = octant;
elem->t = t;
new = &elem->entry;
while (*this) {
parent = *this;
cmp = cmp_octants(new, *this);
/*
* Equal elements go to the right, i.e. FIFO order, thus '<',
* if LIFO is needed then use '<='
*/
if (cmp < 0.0f)
this = &(*this)->rb_left;
else
this = &(*this)->rb_right;
}
/* Add new node to the tree and rebalance it */
rb_link_node(new, parent, this);
rb_insert_color(new, queue->root);
return 0;
}
#endif /* BVH_H */