Fermat
sah_builder.h
1 /*
2  * Copyright (c) 2010-2011, NVIDIA Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of NVIDIA Corporation nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #pragma once
29 
30 #include <nih/bvh/bvh.h>
31 #include <nih/linalg/vector.h>
32 #include <nih/linalg/bbox.h>
33 #include <nih/bintree/bintree_node.h>
34 #include <nih/bintree/cuda/bintree_gen_context.h>
35 #include <thrust/device_vector.h>
36 
37 namespace nih {
38 namespace cuda {
39 
42 {
45  thrust::device_vector<Bvh_node>& nodes,
46  thrust::device_vector<uint2>& leaves,
47  thrust::device_vector<uint32>& index) :
48  m_nodes( &nodes ), m_leaves( &leaves ), m_index( &index )
49  {
50  m_init_bins_time = 0.0f;
51  m_update_bins_time = 0.0f;
52  m_sah_split_time = 0.0f;
53  m_distribute_objects_time = 0.0f;
54  }
55 
57  template <typename Iterator>
58  void build(
59  const uint32 BINS,
60  const Bbox3f bbox,
61  const Iterator bbox_begin,
62  const Iterator bbox_end,
63  const uint32 max_leaf_size,
64  const float max_cost = 1.8f);
65 
66  thrust::device_vector<Bvh_node>* m_nodes;
67  thrust::device_vector<uint2>* m_leaves;
68  thrust::device_vector<uint32>* m_index;
69  uint32 m_levels[128];
70  uint32 m_level_count;
71  Bbox3f m_bbox;
72  uint32 m_node_count;
73  uint32 m_leaf_count;
74 
75  float m_init_bins_time;
76  float m_update_bins_time;
77  float m_sah_split_time;
78  float m_distribute_objects_time;
79 
80  struct Bin
81  {
82  float4 bmin;
83  float4 bmax;
84 
85  FORCE_INLINE NIH_HOST_DEVICE int32 get_size() const { return binary_cast<int32>( bmin.w ); }
86  FORCE_INLINE NIH_HOST_DEVICE void set_size(const int32 x) { bmin.w = binary_cast<float>(x); }
87  };
88  struct Split
89  {
90  NIH_HOST_DEVICE Split() {}
91  NIH_HOST_DEVICE Split(int32 id, int32 plane) : task_id(id), best_plane(plane) {}
92 
93  int32 task_id;
94  int32 best_plane;
95  };
96  struct Queue
97  {
98  Bin* bins;
99  Split* splits;
100  uint32* offsets;
101  int32 size;
102  };
103  struct Objects
104  {
105  int4* bin_ids;
106  int32* split_ids;
107  int32* node_ids;
108  uint32* index;
109  };
110  struct Bbox
111  {
112  float3 bmin;
113  float3 bmax;
114  };
115  struct Bins
116  {
117  float3* bmin;
118  float3* bmax;
119  volatile int32* size;
120  };
121 
122  thrust::device_vector<float3> m_bin_bmin;
123  thrust::device_vector<float3> m_bin_bmax;
124  thrust::device_vector<int32> m_bin_size;
125  thrust::device_vector<Bin> m_queue_bins;
126  thrust::device_vector<Split> m_queue_splits;
127  thrust::device_vector<uint32> m_queue_offsets;
128 
129  thrust::device_vector<int4> m_bin_ids;
130  thrust::device_vector<int32> m_split_ids;
131  thrust::device_vector<int32> m_node_ids;
132  thrust::device_vector<uint32> m_new_pos;
133  thrust::device_vector<uint32> m_counters;
134 };
135 
136 } // namespace cuda
137 } // namespace nih
138 
139 #include <nih/bvh/cuda/sah_builder_inline.h>
Sah_builder(thrust::device_vector< Bvh_node > &nodes, thrust::device_vector< uint2 > &leaves, thrust::device_vector< uint32 > &index)
constructor
Definition: sah_builder.h:44
Definition: sah_builder.h:37
Definition: sah_builder.h:115
Definition: sah_builder.h:103
Definition: sah_builder.h:110
void build(const uint32 BINS, const Bbox3f bbox, const Iterator bbox_begin, const Iterator bbox_end, const uint32 max_leaf_size, const float max_cost=1.8f)
build a bvh given a set of bboxes
Definition: sah_builder_inline.h:217
Definition: sah_builder.h:96
GPU-based SAH BVH builder.
Definition: sah_builder.h:41
Definition: sah_builder.h:80
Definition: sah_builder.h:88
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE Out binary_cast(const In in)
Definition: types.h:288