[r36929]: trunk / contrib / brl / bseg / boxm2 / ocl / cl / bit / compute_vis.cl Maximize Restore History

Download this file

compute_vis.cl    317 lines (267 with data), 11.6 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#pragma OPENCL EXTENSION cl_khr_global_int32_base_atomics: enable
#if NVIDIA
#pragma OPENCL EXTENSION cl_khr_gl_sharing : enable
#endif
#ifdef COMPVIS
typedef struct
{
__global float* alpha;
float* visibility;
bool* start;
__constant RenderSceneInfo * linfo;
} AuxArgs;
//create 4 rays (u_i) around given ray d such that (u_i).d=cos(5)
void create_aux_rays(float4 d, float4* u)
{
d = normalize(d);
float4 m1;
if(d.z != 0.0f)
m1 = (float4)(1, 1, -(d.x+d.y)/d.z, 0);
else if(d.y != 0.0f)
m1 = (float4)(1, -(d.x+d.z)/d.y, 1, 0);
else
m1 = (float4)(-(d.y+d.z)/d.x , 1, 1, 0);
m1 = normalize(m1);
float4 m2 = cross(d,m1);
float c = tan(radians(5.0f));
u[0] = d + m1 * c;
u[1] = d - m1 * c;
u[2] = d + m2 * c;
u[3] = d - m2 * c;
}
//forward declare cast ray (so you can use it)
void cast_ray(int,int,float,float,float,float,float,float,__constant RenderSceneInfo*,
__global int4*,local uchar16*,constant uchar *,local uchar *,float*,AuxArgs);
__kernel
void
compute_vis(__constant uint * datasize_points,
__constant RenderSceneInfo * linfo,
__constant float4 * directions,
__global int4 * tree_array, // tree structure for each block
__constant uchar * bit_lookup, // used to get data_index
__global float * alpha_array,
__global float4 * points,
__global float4 * normals,
__global float16 * vis_sphere,
__constant bool * contain_point,
__local uchar16 * local_tree, // cache current tree into local memory
__local uchar * cumsum)
{
int gid=get_global_id(0);
if (gid<datasize_points[0]) {
//get normal and point from global mem
float4 ray_o = points[ gid ];
float4 ray_d = normals[ gid ];
//check if there is a normal here
if ( (ray_d.x == 0 && ray_d.y == 0 && ray_d.z ==0) || (ray_o.x == 0 && ray_o.y == 0 && ray_o.z == 0)) {
vis_sphere[gid].sf = -1.0f; //flag to indicate there is no normal in this location.
return;
}
else {
//declare ray
float ray_ox, ray_oy, ray_oz, ray_dx, ray_dy, ray_dz, normal_x,normal_y,normal_z;
calc_scene_ray_generic_cam(linfo, ray_o, ray_d, &ray_ox, &ray_oy, &ray_oz, &ray_dx, &ray_dy, &ray_dz);
normal_x = ray_dx; normal_y = ray_dy; normal_z = ray_dz;
AuxArgs aux_args;
aux_args.linfo = linfo;
aux_args.alpha = alpha_array;
//get visibilities from global mem to private mem
float private_vis[12];
private_vis[0] = vis_sphere[gid].s0;
private_vis[1] = vis_sphere[gid].s1;
private_vis[2] = vis_sphere[gid].s2;
private_vis[3] = vis_sphere[gid].s3;
private_vis[4] = vis_sphere[gid].s4;
private_vis[5] = vis_sphere[gid].s5;
private_vis[6] = vis_sphere[gid].s6;
private_vis[7] = vis_sphere[gid].s7;
private_vis[8] = vis_sphere[gid].s8;
private_vis[9] = vis_sphere[gid].s9;
private_vis[10] = vis_sphere[gid].sa;
private_vis[11] = vis_sphere[gid].sb;
float4 aux_rays[4];
float vis_of_aux_rays[5];
//loop thru directions
bool start;
float vis;
for (unsigned int i = 0; i < 12; i++)
{
//setup ray
start = !contain_point[0];
vis = private_vis[i];
aux_args.visibility = &(private_vis[i]);
aux_args.start = &start;
calc_scene_ray_generic_cam(linfo, ray_o, directions[i], &ray_ox, &ray_oy, &ray_oz, &ray_dx, &ray_dy, &ray_dz);
//shoot ray
cast_ray( 1, 1,
ray_ox, ray_oy, ray_oz,
ray_dx, ray_dy, ray_dz,
linfo, tree_array, //scene info
local_tree, bit_lookup, cumsum, &vis, aux_args); //utility info
//zip thru aux rays
create_aux_rays(directions[i],aux_rays);
for(unsigned j = 0; j < 4; j++)
{
start = !contain_point[0];
vis_of_aux_rays[j] = vis;
aux_args.visibility = &(vis_of_aux_rays[j]);
aux_args.start = &start;
calc_scene_ray_generic_cam(linfo, ray_o, aux_rays[j], &ray_ox, &ray_oy, &ray_oz, &ray_dx, &ray_dy, &ray_dz);
//shoot ray
cast_ray( 1, 1,
ray_ox, ray_oy, ray_oz,
ray_dx, ray_dy, ray_dz,
linfo, tree_array, //scene info
local_tree, bit_lookup, cumsum, &vis, aux_args); //utility info
}
//pick the median of sorted rays
vis_of_aux_rays[4] = private_vis[i];
sort_vector( vis_of_aux_rays, 5);
private_vis[i] = vis_of_aux_rays[2];
}
//transfer from private mem to global mem
vis_sphere[gid].s0 = private_vis[0];
vis_sphere[gid].s1 = private_vis[1];
vis_sphere[gid].s2 = private_vis[2];
vis_sphere[gid].s3 = private_vis[3];
vis_sphere[gid].s4 = private_vis[4];
vis_sphere[gid].s5 = private_vis[5];
vis_sphere[gid].s6 = private_vis[6];
vis_sphere[gid].s7 = private_vis[7];
vis_sphere[gid].s8 = private_vis[8];
vis_sphere[gid].s9 = private_vis[9];
vis_sphere[gid].sa = private_vis[10];
vis_sphere[gid].sb = private_vis[11];
}
}
}
//compute_vis step cell functor
void step_cell_computevis(AuxArgs aux_args, int data_ptr, uchar llid, float d)
{
//start computing visibility after ray has left the original cell
if ((*aux_args.start)) {
float alpha = aux_args.alpha[data_ptr];
float seg_len = d * aux_args.linfo->block_len;
(*aux_args.visibility) *= exp(-alpha * seg_len);
}
else
(*aux_args.start) = true;
}
__kernel
void
decide_normal_dir( __constant RenderSceneInfo * linfo,
__constant float4 * directions,
__global float4 * normals,
__global float * vis,
__global float16 * vis_sphere )
{
int gid=get_global_id(0);
int datasize = linfo->data_len ;//* info->num_buffer;
if (gid<datasize) {
vis[gid] = -1.0f;
//check if there is meaningful data here
if (vis_sphere[gid].sf != -1.0f) {
//declare ray
float4 dummy = (float4) (1.0,1.0,1.0,1.0);
float4 ray_d = normals[ gid ];
float ray_ox, ray_oy, ray_oz, ray_dx, ray_dy, ray_dz, normal_x,normal_y,normal_z;
calc_scene_ray_generic_cam(linfo, dummy, ray_d, &ray_ox, &ray_oy, &ray_oz, &ray_dx, &ray_dy, &ray_dz);
normal_x = ray_dx; normal_y = ray_dy; normal_z = ray_dz;
float max_vis = 0.0f;
float max_vis_flipped = 0.0f;
float sum_vis = 0.0f;
float sum_vis_flipped = 0.0f;
float private_vis[12];
private_vis[0] = vis_sphere[gid].s0;
private_vis[1] = vis_sphere[gid].s1;
private_vis[2] = vis_sphere[gid].s2;
private_vis[3] = vis_sphere[gid].s3;
private_vis[4] = vis_sphere[gid].s4;
private_vis[5] = vis_sphere[gid].s5;
private_vis[6] = vis_sphere[gid].s6;
private_vis[7] = vis_sphere[gid].s7;
private_vis[8] = vis_sphere[gid].s8;
private_vis[9] = vis_sphere[gid].s9;
private_vis[10] = vis_sphere[gid].sa;
private_vis[11] = vis_sphere[gid].sb;
//compute max visibility in normal and opposite hemisphere
for (unsigned int i = 0; i < 12; i++)
{
calc_scene_ray_generic_cam(linfo, dummy, directions[i], &ray_ox, &ray_oy, &ray_oz, &ray_dx, &ray_dy, &ray_dz);
if (dot((float4)(ray_dx,ray_dy,ray_dz,0), (float4)(normal_x,normal_y,normal_z,0)) > 0.0){
max_vis = (max_vis < private_vis[i]) ? private_vis[i] : max_vis;
sum_vis+=private_vis[i];
}
else {
max_vis_flipped = (max_vis_flipped < private_vis[i]) ? private_vis[i] : max_vis_flipped;
sum_vis_flipped+=private_vis[i];
}
}
#ifdef USESUM //use the sum of visibilities for the given hemisphere
//flip if necessary
if(sum_vis_flipped > sum_vis){
normals[ gid ] = (float4)(-normal_x,-normal_y,-normal_z,normals[gid].w);
vis[gid] = sum_vis_flipped;
}
else {
normals[ gid ] = (float4)(normal_x,normal_y,normal_z,normals[gid].w);
vis[gid] = sum_vis;
}
#else //use the max visibility for the given hemisphere
//flip if necessary
if(max_vis_flipped > max_vis)
normals[ gid ] = (float4)(-normal_x,-normal_y,-normal_z,normals[gid].w);
else
normals[ gid ] = (float4)(normal_x,normal_y,normal_z,normals[gid].w);
//store max visibility
vis[gid] = (max_vis_flipped > max_vis) ? max_vis_flipped : max_vis;
#endif //USESUM
}
}
}
__kernel
void
decide_inside_cell( __constant RenderSceneInfo * linfo,
__global float * alpha,
__global float * vis,
__global float16 * vis_sphere )
{
int gid=get_global_id(0);
int datasize = linfo->data_len ;
if (gid<datasize) {
vis[gid] = -1.0f;
//check if there is meaningful data here
if (vis_sphere[gid].sf != -1.0f) {
//declare ray
float max_vis = 0.0f;
float sum_vis = 0.0f;
float private_vis[12];
private_vis[0] = vis_sphere[gid].s0;
private_vis[1] = vis_sphere[gid].s1;
private_vis[2] = vis_sphere[gid].s2;
private_vis[3] = vis_sphere[gid].s3;
private_vis[4] = vis_sphere[gid].s4;
private_vis[5] = vis_sphere[gid].s5;
private_vis[6] = vis_sphere[gid].s6;
private_vis[7] = vis_sphere[gid].s7;
private_vis[8] = vis_sphere[gid].s8;
private_vis[9] = vis_sphere[gid].s9;
private_vis[10] = vis_sphere[gid].sa;
private_vis[11] = vis_sphere[gid].sb;
//compute max visibility in normal and opposite hemisphere
for (unsigned int i = 0; i < 12; i++)
{
max_vis = (max_vis < private_vis[i]) ? private_vis[i] : max_vis;
sum_vis+=private_vis[i];
}
#ifdef USESUM //use the sum of visibilities for the given hemisphere
vis[gid] = sum_vis;
#else //use the max visibility for the given hemisphere
vis[gid] = max_vis;
#endif //USESUM
if(vis[gid] < 0.75 )
alpha[gid] =0.0;
}
}
}
#endif //COMPVIS