-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbvh_tree.h
245 lines (204 loc) · 6.83 KB
/
bvh_tree.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
/*
* Copyright (C) 2015-2018, Nils Moehrle
* All rights reserved.
*
* This software may be modified and distributed under the terms
* of the BSD 3-Clause license. See the LICENSE.txt file for details.
*/
#ifndef CACC_BVHTREE_HEADER
#define CACC_BVHTREE_HEADER
#include "acc/bvh_tree.h"
#include "defines.h"
#include "vector.h"
#include "primitives.h"
#include "buffer_texture.h"
CACC_NAMESPACE_BEGIN
template <Location L>
class BVHTree {
public:
typedef std::shared_ptr<BVHTree> Ptr;
const uint NAI = std::numeric_limits<uint>::max();
typedef cacc::Tri Tri;
typedef cacc::AABB AABB;
#pragma __align__(64)
struct Node {
union {
struct {
uint first;
uint last;
uint left;
uint right;
};
uint4 rllf;
};
};
struct Accessor {
BufferTexture<uint4>::Accessor nodes;
BufferTexture<float4>::Accessor aabbs;
BufferTexture<float4>::Accessor tris;
uint *indices_ptr;
__device__ __forceinline__
Node load_node(uint idx) const {
BVHTree<DEVICE>::Node node;
node.rllf = nodes[idx];
return node;
}
__device__ __forceinline__
AABB load_aabb(uint idx) const {
AABB aabb;
float4 min = aabbs[2 * idx + 0];
aabb.min = Vec3f(min.x, min.y, min.z);
float4 max = aabbs[2 * idx + 1];
aabb.max = Vec3f(max.x, max.y, max.z);
return aabb;
}
__device__ __forceinline__
Tri load_tri(uint idx) const {
Tri tri;
float4 a = tris[3 * idx + 0];
tri.a = Vec3f(a.x, a.y, a.z);
float4 b = tris[3 * idx + 1];
tri.b = Vec3f(b.x, b.y, b.z);
float4 c = tris[3 * idx + 2];
tri.c = Vec3f(c.x, c.y, c.z);
return tri;
}
};
private:
struct Data {
uint num_nodes;
uint num_faces;
uint *indices_ptr;
Tri *tris_ptr;
AABB *aabbs_ptr;
Node *nodes_ptr;
};
Data data;
struct Textures {
BufferTexture<uint4> *nodes;
BufferTexture<float4> *aabbs;
BufferTexture<float4> *tris;
};
Textures textures;
void init(uint num_nodes, uint num_faces) {
data.num_nodes = num_nodes;
data.num_faces = num_faces;
if (L == HOST) {
data.nodes_ptr = new Node[num_nodes];
data.aabbs_ptr = new AABB[num_nodes];
data.tris_ptr = new Tri[num_faces];
data.indices_ptr = new uint[num_faces];
} else {
CHECK(cudaMalloc(&data.nodes_ptr, num_nodes * sizeof(Node)));
CHECK(cudaMalloc(&data.aabbs_ptr, num_nodes * sizeof(AABB)));
CHECK(cudaMalloc(&data.tris_ptr, num_faces * sizeof(Tri)));
CHECK(cudaMalloc(&data.indices_ptr, num_faces * sizeof(uint)));
}
if (L == DEVICE) initialize_textures();
}
template <typename T>
void cleanup(T * ptr) {
if (ptr == nullptr) return;
if (L == HOST) delete[] ptr;
if (L == DEVICE) CHECK(cudaFree(ptr));
ptr = nullptr;
}
void cleanup(void) {
cleanup(data.nodes_ptr);
cleanup(data.aabbs_ptr);
cleanup(data.tris_ptr);
cleanup(data.indices_ptr);
if (L == DEVICE) cleanup_textures();
}
template <Location O>
bool meta_equal(Data data, typename BVHTree<O>::Data odata) {
return data.num_nodes == odata.num_nodes
&& data.num_faces == odata.num_faces;
}
template <Location O>
void copy(typename BVHTree<O>::Data const & odata, cudaMemcpyKind src_to_dst) {
CHECK(cudaMemcpy(data.nodes_ptr, odata.nodes_ptr,
data.num_nodes * sizeof(Node), src_to_dst));
CHECK(cudaMemcpy(data.aabbs_ptr, odata.aabbs_ptr,
data.num_nodes * sizeof(AABB), src_to_dst));
CHECK(cudaMemcpy(data.tris_ptr, odata.tris_ptr,
data.num_faces * sizeof(Tri), src_to_dst));
CHECK(cudaMemcpy(data.indices_ptr, odata.indices_ptr,
data.num_faces * sizeof(uint), src_to_dst));
}
void initialize_textures(void) {
static_assert(sizeof(BVHTree<DEVICE>::Node) == sizeof(uint4), "");
static_assert(sizeof(AABB) == 2 * sizeof(float4), "");
static_assert(sizeof(Tri) == 3 * sizeof(float4), "");
textures.nodes = new BufferTexture<uint4>(
(uint4 *) data.nodes_ptr, data.num_nodes);
textures.aabbs = new BufferTexture<float4>(
(float4 *) data.aabbs_ptr, 2 * data.num_nodes);
textures.tris = new BufferTexture<float4>(
(float4 *) data.tris_ptr, 3 * data.num_faces);
}
template <typename T>
void cleanup(BufferTexture<T> * ptr) {
if (ptr == nullptr) return;
delete ptr;
ptr = nullptr;
}
void cleanup_textures(void) {
cleanup(textures.nodes);
cleanup(textures.aabbs);
cleanup(textures.tris);
}
BVHTree() {
data = {0, 0, nullptr, nullptr, nullptr, nullptr};
textures = {nullptr, nullptr, nullptr};
}
BVHTree(uint num_nodes, uint num_faces) : BVHTree() {
init(num_nodes, num_faces);
}
template <Location O> friend class BVHTree;
public:
template <typename IdxType, typename Vec3fType>
static
Ptr create(typename acc::BVHTree<IdxType, Vec3fType>::ConstPtr bvh_tree) {
return Ptr(new BVHTree(*bvh_tree));
}
template<typename IdxType, typename Vec3fType>
BVHTree(typename acc::BVHTree<IdxType, Vec3fType> const & bvh_tree);
template <Location O>
BVHTree& operator=(BVHTree<O> const & other) {
typename BVHTree<O>::Data const & odata = other.data;
if (!meta_equal<O>(data, odata)) {
cleanup();
init(odata.num_nodes, odata.num_faces);
}
if (L == HOST && O == HOST) copy<O>(odata, cudaMemcpyHostToHost);
if (L == HOST && O == DEVICE) copy<O>(odata, cudaMemcpyDeviceToHost);
if (L == DEVICE && O == HOST) copy<O>(odata, cudaMemcpyHostToDevice);
if (L == DEVICE && O == DEVICE) copy<O>(odata, cudaMemcpyDeviceToDevice);
return *this;
}
template<Location O>
BVHTree(BVHTree<O> const & other) : BVHTree() {
*this = other;
}
~BVHTree() {
cleanup();
}
Accessor accessor(void) const;
template <typename IdxType, typename Vec3fType>
template <class C>
friend C acc::BVHTree<IdxType, Vec3fType>::convert(
acc::BVHTree<IdxType, Vec3fType> const & bvh_tree);
};
template<> inline
BVHTree<DEVICE>::Accessor
BVHTree<DEVICE>::accessor(void) const {
return {
textures.nodes->accessor(),
textures.aabbs->accessor(),
textures.tris->accessor(),
data.indices_ptr
};
}
CACC_NAMESPACE_END
#endif /* CACC_BVHTREE_HEADER */