-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathBvhBuilder.h
More file actions
173 lines (158 loc) · 5.49 KB
/
BvhBuilder.h
File metadata and controls
173 lines (158 loc) · 5.49 KB
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
//
// Copyright (c) 2021-2023 Advanced Micro Devices, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#pragma once
#include <cassert>
#include <cstdint>
#include <queue>
#include <tutorials/common/Aabb.h>
struct QueueEntry
{
int m_nodeIndex;
int m_begin;
int m_end;
Aabb m_box;
QueueEntry( int nodeIndex, int begin, int end, const Aabb& box )
: m_nodeIndex( nodeIndex ), m_begin( begin ), m_end( end ), m_box( box )
{
}
};
class BvhBuilder
{
public:
BvhBuilder( void ) = delete;
BvhBuilder& operator=( const BvhBuilder& ) = delete;
static void build( uint32_t nPrims, const std::vector<Aabb>& primBoxes, std::vector<hiprtInternalNode>& nodes )
{
assert( nPrims >= 2 );
std::vector<Aabb> rightBoxes( nPrims );
std::vector<int> tmpIndices( nPrims );
std::vector<int> leftIndices( nPrims );
std::vector<int> indices[3];
for ( int k = 0; k < 3; ++k )
{
indices[k].resize( nPrims );
for ( int i = 0; i < nPrims; ++i )
indices[k][i] = i;
std::sort( indices[k].begin(), indices[k].end(), [&]( int a, int b ) {
hiprtFloat3 ca = primBoxes[a].center();
hiprtFloat3 cb = primBoxes[b].center();
return reinterpret_cast<float*>( &ca )[k] > reinterpret_cast<float*>( &cb )[k];
} );
}
Aabb box;
for ( int i = 0; i < nPrims; ++i )
box.grow( primBoxes[i] );
std::queue<QueueEntry> queue;
queue.push( QueueEntry( 0, 0, nPrims, box ) );
nodes.push_back( hiprtInternalNode() );
while ( !queue.empty() )
{
int nodeIndex = queue.front().m_nodeIndex;
int begin = queue.front().m_begin;
int end = queue.front().m_end;
Aabb box = queue.front().m_box;
queue.pop();
float minCost = FLT_MAX;
int minAxis = 0;
int minIndex = 0;
Aabb minLeftBox, minRightBox;
for ( int k = 0; k < 3; ++k )
{
rightBoxes[end - 1] = primBoxes[indices[k][end - 1]];
for ( int i = end - 2; i >= begin; --i )
rightBoxes[i] = Aabb( primBoxes[indices[k][i]], rightBoxes[i + 1] );
Aabb leftBox, rightBox;
for ( int i = begin; i < end - 1; ++i )
{
int leftCount = ( i + 1 ) - begin;
int rightCount = end - ( i + 1 );
leftBox.grow( primBoxes[indices[k][i]] );
rightBox = rightBoxes[i + 1];
float cost = leftBox.area() * leftCount + rightBox.area() * rightCount;
if ( cost < minCost )
{
minCost = cost;
minIndex = i + 1;
minAxis = k;
minLeftBox = leftBox;
minRightBox = rightBox;
}
assert( leftBox.area() <= box.area() );
assert( rightBox.area() <= box.area() );
}
}
assert( minIndex > begin );
assert( end > minIndex );
memset( leftIndices.data(), 0, nPrims * sizeof( int ) );
for ( int i = begin; i < minIndex; ++i )
{
int index = indices[minAxis][i];
leftIndices[index] = 1;
}
for ( int j = 0; j < 3; ++j )
{
if ( j != minAxis )
{
int k = begin;
int l = minIndex;
for ( int i = begin; i < end; ++i )
{
int index = indices[j][i];
if ( leftIndices[indices[j][i]] )
tmpIndices[k++] = index;
else
tmpIndices[l++] = index;
}
assert( k == minIndex );
assert( l == end );
memcpy( &indices[j][begin], &tmpIndices[begin], ( end - begin ) * sizeof( int ) );
}
}
nodes[nodeIndex].aabbMin = min( minLeftBox.m_min, minRightBox.m_min );
nodes[nodeIndex].aabbMax = max( minLeftBox.m_max, minRightBox.m_max );
if ( minIndex - begin == 1 )
{
nodes[nodeIndex].childIndices[0] = indices[minAxis][begin];
nodes[nodeIndex].childNodeTypes[0] = hiprtBvhNodeTypeLeaf;
}
else
{
nodes[nodeIndex].childIndices[0] = static_cast<int>( nodes.size() );
nodes[nodeIndex].childNodeTypes[0] = hiprtBvhNodeTypeInternal;
queue.push( QueueEntry( nodes[nodeIndex].childIndices[0], begin, minIndex, minLeftBox ) );
nodes.push_back( hiprtInternalNode() );
}
if ( end - minIndex == 1 )
{
nodes[nodeIndex].childIndices[1] = indices[minAxis][minIndex];
nodes[nodeIndex].childNodeTypes[1] = hiprtBvhNodeTypeLeaf;
}
else
{
nodes[nodeIndex].childIndices[1] = static_cast<int>( nodes.size() );
nodes[nodeIndex].childNodeTypes[1] = hiprtBvhNodeTypeInternal;
queue.push( QueueEntry( nodes[nodeIndex].childIndices[1], minIndex, end, minRightBox ) );
nodes.push_back( hiprtInternalNode() );
}
}
}
};