Fermat
clustered_rl.h
1 /*
2  * Fermat
3  *
4  * Copyright (c) 2017-2019, NVIDIA CORPORATION. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the NVIDIA CORPORATION nor the
14  * names of its contributors may be used to endorse or promote products
15  * derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #pragma once
30 
31 #include <hashmap.h>
32 #include <cugar/bvh/bvh_node.h>
33 #include <cugar/basic/algorithms.h>
34 #include <cugar/basic/cuda/hash.h>
35 
38 
47 
51 {
53 
54  FERMAT_HOST_DEVICE
55  ClusteredRLView() {}
56 
59  FERMAT_DEVICE
60  uint32 find_slot(const uint64 key);
61 
64  FERMAT_DEVICE
65  uint32 sample(const uint32 cell_slot, const float z, float* pdf, uint32* cluster_idx) const;
66 
69  FERMAT_DEVICE
70  float pdf(const uint32 cell_slot, const uint32 index) const;
71 
74  FERMAT_DEVICE
75  void update(const uint32 cell_slot, const uint32 cluster_idx, const float val, const float alpha = 0.05f);
76 
77  HashMap hashmap;
78  float* pdfs;
79  float* cdfs;
80  const uint32* cluster_offsets;
81  uint32 cluster_count;
82  uint32 hash_size;
83 };
84 
88 {
89  typedef ClusteredRLView view_type;
90 
91  ClusteredRLStorage() : hash_size(0), n_clusters(0), old_hash_size(0) {}
92 
93  uint64 needed_bytes(const uint32 _hash_size, const uint32 _n_clusters) const;
94 
95  void init(
96  const uint32 _hash_size,
97  const uint32 _n_clusters,
98  const uint32* _cluster_offsets);
99 
100  void resize(
101  const uint32 _hash_size,
102  const uint32 _n_clusters,
103  const uint32* _cluster_offsets);
104 
105  void update(const float bias = 0.1f);
106 
107  void clear();
108 
109  uint32 size() const;
110 
111  DeviceHashTable hashmap;
113  uint32 hash_size;
114  uint32 n_clusters;
115  const uint32* cluster_offsets;
116  uint32 old_hash_size;
117 };
118 
119 ClusteredRLView view(ClusteredRLStorage& storage);
120 
124 {
126 
127  FERMAT_HOST_DEVICE
129 
132  FERMAT_DEVICE
133  uint32 find_slot(const uint64 key);
134 
137  FERMAT_DEVICE
138  uint32 sample(const uint32 cell_slot, const float z, float* pdf, uint32* cluster_idx) const;
139 
142  FERMAT_DEVICE
143  float pdf(const uint32 cell_slot, const uint32 index) const;
144 
147  FERMAT_DEVICE
148  void update(const uint32 cell_slot, const uint32 cluster_idx, const float val, const float alpha = 0.05f);
149 
150  HashMap hashmap;
151  float* pdfs;
152  float* cdfs;
153  const uint32* cluster_ends;
154  const uint32* cluster_counts;
155  uint32 init_cluster_count;
156  uint32 hash_size;
157 };
158 
162 {
164 
166 
167  uint64 needed_bytes(const uint32 _hash_size, const uint32 _n_clusters) const;
168 
169  void init(
170  const uint32 _hash_size,
171  const cugar::Bvh_node_3d* _nodes,
172  const uint32* _parents,
173  const uint2* _ranges,
174  const uint32 _n_clusters,
175  const uint32* _cluster_indices,
176  const uint32* _cluster_offsets);
177 
178  void update(bool adaptive = true);
179 
180  void clear();
181 
182  uint32 size() const;
183 
184  DeviceHashTable hashmap;
186  const cugar::Bvh_node_3d* nodes;
187  const uint32* parents;
188  const uint2* ranges;
189  uint32 hash_size;
190  DomainBuffer<CUDA_BUFFER, uint32> cluster_counts;
191  DomainBuffer<CUDA_BUFFER, uint32> cluster_indices;
193  uint32 init_cluster_count;
194  const uint32* init_cluster_indices;
195  const uint32* init_cluster_offsets;
196 };
197 
199 
202 
203 #include <clustered_rl_inline.h>
Entry point to the generic Bounding Volume Hierarchy library.
Definition: bvh_node.h:80
Definition: clustered_rl.h:161
FERMAT_DEVICE float pdf(const uint32 cell_slot, const uint32 index) const
Definition: clustered_rl_inline.h:79
Definition: hashmap.h:32
FERMAT_DEVICE void update(const uint32 cell_slot, const uint32 cluster_idx, const float val, const float alpha=0.05f)
Definition: clustered_rl_inline.h:99
Defines some general purpose algorithms.
Definition: clustered_rl.h:123
Definition: clustered_rl.h:87
FERMAT_DEVICE uint32 sample(const uint32 cell_slot, const float z, float *pdf, uint32 *cluster_idx) const
Definition: clustered_rl_inline.h:46
Definition: clustered_rl.h:50
FERMAT_DEVICE uint32 find_slot(const uint64 key)
Definition: clustered_rl_inline.h:36