-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils_kernel.cu
171 lines (156 loc) · 5.91 KB
/
utils_kernel.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
// Copyright (c) Meta Platforms, Inc. and affiliates.
// All rights reserved.
//
// This source code is licensed under the license found in the
// LICENSE file in the root directory of this source tree.
#include "helper_math.h"
using namespace math;
__global__ void compute_raydirs_forward_kernel(
int N, int H, int W,
float3 * viewposim,
float3 * viewrotim,
float2 * focalim,
float2 * princptim,
float2 * pixelcoordsim,
float volradius,
float3 * rayposim,
float3 * raydirim,
float2 * tminmaxim
) {
bool validthread = false;
int w, h, n;
w = blockIdx.x * blockDim.x + threadIdx.x;
h = (blockIdx.y * blockDim.y + threadIdx.y)%H;
n = (blockIdx.y * blockDim.y + threadIdx.y)/H;
validthread = (w < W) && (h < H) && (n<N);
if (validthread) {
float3 raypos = viewposim[n] / volradius;
float3 viewrot0 = viewrotim[n * 3 + 0];
float3 viewrot1 = viewrotim[n * 3 + 1];
float3 viewrot2 = viewrotim[n * 3 + 2];
float2 pixelcoord = pixelcoordsim ? pixelcoordsim[n * H * W + h * W + w] : make_float2(w, h);
pixelcoord = (pixelcoord - princptim[n]) / focalim[n];
float3 raydir = make_float3(pixelcoord, 1.f);
raydir = viewrot0 * raydir.x + viewrot1 * raydir.y + viewrot2 * raydir.z;
raydir = normalize(raydir);
float3 t1 = (-1.f - raypos) / raydir;
float3 t2 = ( 1.f - raypos) / raydir;
float tmin = fmaxf(fminf(t1.x, t2.x), fmaxf(fminf(t1.y, t2.y), fminf(t1.z, t2.z)));
float tmax = fminf(fmaxf(t1.x, t2.x), fminf(fmaxf(t1.y, t2.y), fmaxf(t1.z, t2.z)));
float2 tminmax = make_float2(fmaxf(tmin, 0.f), tmax);
rayposim[n * H * W + h * W + w] = raypos;
raydirim[n * H * W + h * W + w] = raydir;
tminmaxim[n * H * W + h * W + w] = tminmax;
}
}
__global__ void compute_raydirs_backward_kernel(
int N, int H, int W,
float3 * viewposim,
float3 * viewrotim,
float2 * focalim,
float2 * princptim,
float2 * pixelcoordsim,
float volradius,
float3 * rayposim,
float3 * raydirim,
float2 * tminmaxim,
float3 * grad_viewposim,
float3 * grad_viewrotim,
float2 * grad_focalim,
float2 * grad_princptim
) {
bool validthread = false;
int w, h, n;
w = blockIdx.x * blockDim.x + threadIdx.x;
h = (blockIdx.y * blockDim.y + threadIdx.y)%H;
n = (blockIdx.y * blockDim.y + threadIdx.y)/H;
validthread = (w < W) && (h < H) && (n<N);
if (validthread) {
float3 raypos = viewposim[n] / volradius;
float3 viewrot0 = viewrotim[n * 3 + 0];
float3 viewrot1 = viewrotim[n * 3 + 1];
float3 viewrot2 = viewrotim[n * 3 + 2];
float2 pixelcoord = pixelcoordsim ? pixelcoordsim[n * H * W + h * W + w] : make_float2(w, h);
pixelcoord = (pixelcoord - princptim[n]) / focalim[n];
float3 raydir = make_float3(pixelcoord, 1.f);
raydir = viewrot0 * raydir.x + viewrot1 * raydir.y + viewrot2 * raydir.z;
raydir = normalize(raydir);
float3 t1 = (-1.f - raypos) / raydir;
float3 t2 = ( 1.f - raypos) / raydir;
float tmin = fmaxf(fminf(t1.x, t2.x), fmaxf(fminf(t1.y, t2.y), fminf(t1.z, t2.z)));
float tmax = fminf(fmaxf(t1.x, t2.x), fminf(fmaxf(t1.y, t2.y), fmaxf(t1.z, t2.z)));
float2 tminmax = make_float2(fmaxf(tmin, 0.f), tmax);
}
}
void compute_raydirs_forward_cuda(
int N, int H, int W,
float * viewposim,
float * viewrotim,
float * focalim,
float * princptim,
float * pixelcoordsim,
float volradius,
float * rayposim,
float * raydirim,
float * tminmaxim,
cudaStream_t stream) {
int blocksizex = 16;
int blocksizey = 16;
dim3 blocksize(blocksizex, blocksizey);
dim3 gridsize;
gridsize = dim3(
(W + blocksize.x - 1) / blocksize.x,
(N*H + blocksize.y - 1) / blocksize.y);
auto fn = compute_raydirs_forward_kernel;
fn<<<gridsize, blocksize, 0, stream>>>(
N, H, W,
reinterpret_cast<float3 *>(viewposim),
reinterpret_cast<float3 *>(viewrotim),
reinterpret_cast<float2 *>(focalim),
reinterpret_cast<float2 *>(princptim),
reinterpret_cast<float2 *>(pixelcoordsim),
volradius,
reinterpret_cast<float3 *>(rayposim),
reinterpret_cast<float3 *>(raydirim),
reinterpret_cast<float2 *>(tminmaxim));
}
void compute_raydirs_backward_cuda(
int N, int H, int W,
float * viewposim,
float * viewrotim,
float * focalim,
float * princptim,
float * pixelcoordsim,
float volradius,
float * rayposim,
float * raydirim,
float * tminmaxim,
float * grad_viewposim,
float * grad_viewrotim,
float * grad_focalim,
float * grad_princptim,
cudaStream_t stream) {
int blocksizex = 16;
int blocksizey = 16;
dim3 blocksize(blocksizex, blocksizey);
dim3 gridsize;
gridsize = dim3(
(W + blocksize.x - 1) / blocksize.x,
(N*H + blocksize.y - 1) / blocksize.y);
auto fn = compute_raydirs_backward_kernel;
fn<<<gridsize, blocksize, 0, stream>>>(
N, H, W,
reinterpret_cast<float3 *>(viewposim),
reinterpret_cast<float3 *>(viewrotim),
reinterpret_cast<float2 *>(focalim),
reinterpret_cast<float2 *>(princptim),
reinterpret_cast<float2 *>(pixelcoordsim),
volradius,
reinterpret_cast<float3 *>(rayposim),
reinterpret_cast<float3 *>(raydirim),
reinterpret_cast<float2 *>(tminmaxim),
reinterpret_cast<float3 *>(grad_viewposim),
reinterpret_cast<float3 *>(grad_viewrotim),
reinterpret_cast<float2 *>(grad_focalim),
reinterpret_cast<float2 *>(grad_princptim));
}