forked from nvpro-samples/optix_prime_baking
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bake_kernels.cu
178 lines (149 loc) · 6.28 KB
/
bake_kernels.cu
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
/*-----------------------------------------------------------------------
Copyright (c) 2015-2016, NVIDIA. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Neither the name of its contributors may be used to endorse
or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-----------------------------------------------------------------------*/
#include "bake_kernels.h"
#include "bake_api.h"
#include "random.h"
#include <optixu/optixu_math_namespace.h>
using optix::float3;
inline int idivCeil( int x, int y )
{
return (x + y-1)/y;
}
//------------------------------------------------------------------------------
//
// Ray generation kernel
//
//------------------------------------------------------------------------------
__global__
void generateRaysKernel(
const unsigned int base_seed,
const int px,
const int py,
const int sqrt_passes,
const float scene_offset,
const float scene_maxdistance,
const int num_samples,
const float3* sample_normals,
const float3* sample_face_normals,
const float3* sample_positions,
Ray* rays
)
{
int idx = threadIdx.x + blockIdx.x*blockDim.x;
if( idx >= num_samples )
return;
const unsigned int tea_seed = (base_seed << 16) | (px*sqrt_passes+py);
unsigned seed = tea<2>( tea_seed, idx );
const float3 sample_norm = sample_normals[idx];
const float3 sample_face_norm = sample_face_normals[idx];
const float3 sample_pos = sample_positions[idx];
const float3 ray_origin = sample_pos;
optix::Onb onb( sample_norm );
float3 ray_dir;
float u0 = ( static_cast<float>( px ) + rnd( seed ) ) / static_cast<float>( sqrt_passes );
float u1 = ( static_cast<float>( py ) + rnd( seed ) ) / static_cast<float>( sqrt_passes );
int j = 0;
do
{
optix::cosine_sample_hemisphere( u0, u1, ray_dir );
onb.inverse_transform( ray_dir );
++j;
u0 = rnd( seed );
u1 = rnd( seed );
}
while( j < 5 && optix::dot( ray_dir, sample_face_norm ) <= 0.0f );
#if 1
// Reverse shadow rays for better performance
rays[idx].origin = ray_origin + scene_maxdistance * ray_dir;
rays[idx].tmin = 0.0f;
rays[idx].direction = -ray_dir;
rays[idx].tmax = scene_maxdistance - scene_offset; // possible loss of precision here (bignum - smallnum)
#else
// Forward shadow rays for better precision
rays[idx].origin = ray_origin;
rays[idx].tmin = scene_offset;
rays[idx].direction = ray_dir;
rays[idx].tmax = scene_maxdistance;
#endif
}
__host__
void bake::generateRaysDevice(unsigned int seed, int px, int py, int sqrt_passes, float scene_offset, float scene_maxdistance, const bake::AOSamples& ao_samples, Ray* rays )
{
const int block_size = 512;
const int block_count = idivCeil( (int)ao_samples.num_samples, block_size );
generateRaysKernel<<<block_count,block_size>>>(
seed,
px,
py,
sqrt_passes,
scene_offset,
scene_maxdistance,
(int)ao_samples.num_samples,
(float3*)ao_samples.sample_normals,
(float3*)ao_samples.sample_face_normals,
(float3*)ao_samples.sample_positions,
rays
);
}
//------------------------------------------------------------------------------
//
// AO update kernel
//
//------------------------------------------------------------------------------
__global__
void updateAOKernel(int num_samples, const float* hit_data, float* ao_data)
{
int idx = threadIdx.x + blockIdx.x*blockDim.x;
if( idx >= num_samples )
return;
float distance = hit_data[idx];
ao_data[idx] += distance > 0.0 ? 1.0f : 0.0f;
}
// Precondition: ao output initialized to 0 before first pass
__host__
void bake::updateAODevice( int num_samples, const float* hits, float* ao )
{
int block_size = 512;
int block_count = idivCeil( num_samples, block_size );
updateAOKernel <<<block_count, block_size >>>(num_samples, hits, ao);
}
//------------------------------------------------------------------------------
//
// AO normalize-and-invert kernel
//
//------------------------------------------------------------------------------
__global__
void normalizeAOKernel(int num_samples, float* ao_data, int rays_per_sample)
{
int idx = threadIdx.x + blockIdx.x*blockDim.x;
if( idx >= num_samples )
return;
ao_data[idx] = 1.0f - ao_data[idx] / rays_per_sample;
}
__host__
void bake::normalizeAODevice( int num_samples, float* ao, int rays_per_sample )
{
int block_size = 512;
int block_count = idivCeil( num_samples, block_size );
normalizeAOKernel <<<block_count, block_size >>>(num_samples, ao, rays_per_sample);
}