-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscanline_optimize.cu
480 lines (472 loc) · 22 KB
/
scanline_optimize.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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
#include "scanline_optimize.cuh"
#define FULL_MASK 0xffffffff
__global__ void scanline_optimize_left2right(uint8_t* img_left, uint8_t* img_right,float* cost_init, float* cost_aggr, ADCensus_Option option)
{
//allocate d size thread and d*2 size shared memory
//one for last aggr row and one for current init row and one for minimum reduction
extern volatile __shared__ float smem[];
int direction = 1;
const int width = option.width; //xdim
const int height = option.height; //ydim
const int min_disp = option.min_disparity;
const int max_disp = option.max_disparity;
const float p1 = option.so_p1;
const float p2 = option.so_p2;
const float tso = option.so_tso;
const int disp_range = max_disp - min_disp;
float* last_aggr_row_buffer = (float*)smem; //shared mem array with disp range
float* cost_init_row_buffer = (float*)&smem[disp_range];//shared mem array with disp range
const int row_idx = blockIdx.x;
int d_idx = threadIdx.x;
int tid = d_idx;
int wrap_id = tid / 32; //location of current wrap
int wrap_num = max_disp / 32;
//if (row_idx < height)
//{
//first to handle first col first col all disparity of cost aggr do no need to compute
int col_idx = 0;
last_aggr_row_buffer[d_idx] = cost_init[Get3dIdx(row_idx, col_idx, d_idx, height, width, disp_range)];
__syncthreads();
//becuase first column dont need to do any aggregation
cost_aggr[Get3dIdx(row_idx, col_idx, d_idx, height, width, disp_range)] = last_aggr_row_buffer[d_idx];
uchar3 color = FETCH_UCHAR3(img_left[Get2dIdxRGB(row_idx, col_idx, height, width)]);
uchar3 color_last = color;
///caculate minimum cost on the shared memory
__syncthreads();
float min_cost_last_path = last_aggr_row_buffer[d_idx];
for (int offset = 16; offset > 0; offset /= 2)
min_cost_last_path = min(__shfl_down_sync(FULL_MASK, min_cost_last_path, offset),min_cost_last_path);
if (tid % 32 == 0)
cost_init_row_buffer[wrap_id] = min_cost_last_path; //save wrap reduce result to corresponding wrap id
__syncthreads();
if (tid == 0) {
for (int w = 1; w < wrap_num; w++)
cost_init_row_buffer[0] = min(cost_init_row_buffer[0], cost_init_row_buffer[w]); //use thread 0 to caculate final answer
}
__syncthreads();
min_cost_last_path = cost_init_row_buffer[0]; ///boardcast to every thread in the block
col_idx += direction;
__syncthreads();
///copy 1st row as buffer of current row start of iteration
cost_init_row_buffer[d_idx] = cost_init[Get3dIdx(row_idx, col_idx, d_idx, height, width, disp_range)];
__syncthreads();
//seems ok till here
for (int j = 0; j < width - 1; j++)
{
color = FETCH_UCHAR3(img_left[Get2dIdxRGB(row_idx, col_idx, height, width)]);
uint8_t d1 = ColorDist(color, color_last);
uint8_t d2 = d1;
int yr = col_idx - d_idx - min_disp;
if (yr > 0 && yr < width - 1)
{
uchar3 color_r= FETCH_UCHAR3(img_right[Get2dIdxRGB(row_idx, yr, height, width)]);
uchar3 color_last_r = FETCH_UCHAR3(img_right[Get2dIdxRGB(row_idx, yr-direction, height, width)]);
d2 = ColorDist(color_r, color_last_r);
}
float P1(0.0f), P2(0.0f);
if (d1 < tso && d2 < tso) {
P1 = p1;
P2 = p2;
}
else if (d1 < tso && d2 >= tso) {
P1 = p1 / 4.0f;
P2 = p2 / 4.0f;
}
else if (d1 >= tso && d2 < tso) {
P1 = p1 / 4.0f;
P2 = p2 / 4.0f;
}
else if (d1 >= tso && d2 >= tso) {
P1 = p1 / 10.0f;
P2 = p2 / 10.0f;
}
const float cost = cost_init_row_buffer[d_idx];
//l1 is current pixel
const float l1 = last_aggr_row_buffer[d_idx];//d_idx + 1 < max_disp ? last_aggr_row_buffer[d_idx + 1] : 99999.0f;
//l2 is pixel on the left
const float l2 = d_idx-1>=0?last_aggr_row_buffer[d_idx-1]+P1:99999.0f+P1;
//l3 is pixel on the right
const float l3 = d_idx + 1 < max_disp ? last_aggr_row_buffer[d_idx + 1] + P1 : 99999.0f + P1;//d_idx + 2 < max_disp ? last_aggr_row_buffer[d_idx + 2] : 99999.0f;
const float l4 = min_cost_last_path + P2;
float cost_s = cost + float(min(min(l1, l2), min(l3, l4)));
cost_s /= 2.0f;
__syncthreads();
cost_aggr[Get3dIdx(row_idx, col_idx, d_idx, height, width, disp_range)] = cost_s; //update cost aggr
last_aggr_row_buffer[d_idx] = cost_s; //IMPORTANT:update cost aggr last aggr row buffer for next column
//__syncthreads();
//caculate the best minimum cost this round,re-use cost_init_row_buffer
float min_cost_this_path = cost_s;
for (int offset = 16; offset > 0; offset /= 2)
min_cost_this_path = min(__shfl_down_sync(FULL_MASK, min_cost_this_path, offset), min_cost_this_path);
if (tid % 32 == 0)
cost_init_row_buffer[wrap_id] = min_cost_this_path; //save wrap reduce result to corresponding wrap id
__syncthreads();
if (tid == 0) {
for (int w = 0; w < wrap_num; w++)
cost_init_row_buffer[0] = min(cost_init_row_buffer[0], cost_init_row_buffer[w]); //use thread 0 to caculate final answer
}
__syncthreads();
min_cost_last_path = cost_init_row_buffer[0]; //boardcast to every thread in the block to replace the cost of min_cost_last_path
col_idx += direction; //prepare to start next column
__syncthreads();
//dont need to update in last row iteration
if (col_idx<width)
{
cost_init_row_buffer[d_idx] = cost_init[Get3dIdx(row_idx, col_idx, d_idx, height, width, disp_range)];
color_last = color;
}
__syncthreads();
} //end for
//}
//if (row_idx == 100 && d_idx == 0)
// printf("scanline val left 2 right of %d , %d %d is %f \n", row_idx,100,d_idx,cost_aggr[Get3dIdx(row_idx, 100, d_idx, height, width, disp_range)]);
//if (row_idx == 100 && d_idx == 100)
// printf("scanline val left 2 right of %d , %d %d is %f \n", row_idx, 100, d_idx, cost_aggr[Get3dIdx(row_idx, 100, d_idx, height, width, disp_range)]);
}
__global__ void scanline_optimize_right2left(uint8_t* img_left, uint8_t* img_right, float* cost_init, float* cost_aggr, ADCensus_Option option)
{
//allocate d size thread and d*2 size shared memory
//one for last aggr row and one for current init row and one for minimum reduction
extern volatile __shared__ float smem[];
int direction = -1;
const int width = option.width; //xdim
const int height = option.height; //ydim
const int min_disp = option.min_disparity;
const int max_disp = option.max_disparity;
const float p1 = option.so_p1;
const float p2 = option.so_p2;
const float tso = option.so_tso;
const int disp_range = max_disp - min_disp;
float* last_aggr_row_buffer = (float*)smem; //shared mem array with disp range
float* cost_init_row_buffer = (float*)&smem[disp_range];;//shared mem array with disp range
int row_idx = blockIdx.x;
int d_idx = threadIdx.x;
int tid = d_idx;
int wrap_id = tid / 32; //location of current wrap
int wrap_num = max_disp / 32;
//if (row_idx < height)
//{
//first to handle first col first col all disparity of cost aggr do no need to compute
int col_idx = width-1;
last_aggr_row_buffer[d_idx] = cost_init[Get3dIdx(row_idx, col_idx, d_idx, height, width, disp_range)];
__syncthreads();
cost_aggr[Get3dIdx(row_idx, col_idx, d_idx, height, width, disp_range)] = last_aggr_row_buffer[d_idx];
uchar3 color = FETCH_UCHAR3(img_left[Get2dIdxRGB(row_idx, col_idx, height, width)]);
uchar3 color_last = color;
//caculate minimum cost on the shared memory
__syncthreads();
float min_cost_last_path = last_aggr_row_buffer[d_idx];
for (int offset = 16; offset > 0; offset /= 2)
min_cost_last_path = min(__shfl_down_sync(FULL_MASK, min_cost_last_path, offset), min_cost_last_path);
if (tid % 32 == 0)
cost_init_row_buffer[wrap_id] = min_cost_last_path; //save wrap reduce result to corresponding wrap id
__syncthreads();
if (tid == 0) {
for (int w = 0; w < wrap_num; w++)
cost_init_row_buffer[0] = min(cost_init_row_buffer[0], cost_init_row_buffer[w]); //use thread 0 to caculate final answer
}
__syncthreads();
min_cost_last_path = cost_init_row_buffer[0]; //boardcast to every thread in the block
col_idx += direction;
__syncthreads();
//copy 1st row as buffer of current row start of iteration
cost_init_row_buffer[d_idx] = cost_init[Get3dIdx(row_idx, col_idx, d_idx, height, width, disp_range)];
__syncthreads();
for (int j = 0; j < width - 1; j++)
{
color = FETCH_UCHAR3(img_left[Get2dIdxRGB(row_idx, col_idx, height, width)]);
uint8_t d1 = ColorDist(color, color_last);
uint8_t d2 = d1;
int yr = col_idx - d_idx - min_disp;
if (yr > 0 && yr < width - 1)
{
uchar3 color_r = FETCH_UCHAR3(img_right[Get2dIdxRGB(row_idx, yr, height, width)]);
uchar3 color_last_r = FETCH_UCHAR3(img_right[Get2dIdxRGB(row_idx, yr - direction, height, width)]);
d2 = ColorDist(color_r, color_last_r);
}
float P1(0.0f), P2(0.0f);
if (d1 < tso && d2 < tso) {
P1 = p1;
P2 = p2;
}
else if (d1 < tso && d2 >= tso) {
P1 = p1 / 4.0f;
P2 = p2 / 4.0f;
}
else if (d1 >= tso && d2 < tso) {
P1 = p1 / 4.0f;
P2 = p2 / 4.0f;
}
else if (d1 >= tso && d2 >= tso) {
P1 = p1 / 10.0f;
P2 = p2 / 10.0f;
}
const float cost = cost_init_row_buffer[d_idx];
const float l1 = last_aggr_row_buffer[d_idx];//d_idx + 1 < max_disp ? last_aggr_row_buffer[d_idx + 1] : 99999.0f;
//l2 is pixel on the left
const float l2 = d_idx - 1 >= 0 ? last_aggr_row_buffer[d_idx - 1] + P1 : 99999.0f + P1;
//l3 is pixel on the right
const float l3 = d_idx + 1 < max_disp ? last_aggr_row_buffer[d_idx + 1] + P1 : 99999.0f + P1;//d_idx + 2 < max_disp ? last_aggr_row_buffer[d_idx + 2] : 99999.0f;
const float l4 = min_cost_last_path + P2;
float cost_s = cost + float(min(min(l1, l2), min(l3, l4)));
cost_s /= 2.0f;
__syncthreads();
cost_aggr[Get3dIdx(row_idx, col_idx, d_idx, height, width, disp_range)] = cost_s; //update cost aggr
last_aggr_row_buffer[d_idx] = cost_s; //IMPORTANT:update cost aggr last aggr row buffer for next column
//caculate the best minimum cost this round,re-use cost_init_row_buffer
float min_cost_this_path = cost_s;
for (int offset = 16; offset > 0; offset /= 2)
min_cost_this_path = min(__shfl_down_sync(FULL_MASK, min_cost_this_path, offset), min_cost_this_path);
if (tid % 32 == 0)
cost_init_row_buffer[wrap_id] = min_cost_this_path; //save wrap reduce result to corresponding wrap id
__syncthreads();
if (tid == 0) {
for (int w = 0; w < wrap_num; w++)
cost_init_row_buffer[0] = min(cost_init_row_buffer[0], cost_init_row_buffer[w]); //use thread 0 to caculate final answer
}
__syncthreads();
min_cost_last_path = cost_init_row_buffer[0]; //boardcast to every thread in the block to replace the cost of min_cost_last_path
col_idx += direction; //prepare to start next column
__syncthreads();
//dont need to do this in last iteration
if (col_idx >= 0) {
cost_init_row_buffer[d_idx] = cost_init[Get3dIdx(row_idx, col_idx, d_idx, height, width, disp_range)];
color_last = color;
}
__syncthreads();
} //end for loop
//if (row_idx == 100 && d_idx == 0)
// printf("scanline val left 2 right of %d , %d %d is %f \n", row_idx, 100, d_idx, cost_aggr[Get3dIdx(row_idx, 100, d_idx, height, width, disp_range)]);
//if (row_idx == 100 && d_idx == 100)
// printf("scanline val left 2 right of %d , %d %d is %f \n", row_idx, 100, d_idx, cost_aggr[Get3dIdx(row_idx, 100, d_idx, height, width, disp_range)]);
}
__global__ void scanline_optimize_top2bottom(uint8_t* img_left, uint8_t* img_right, float* cost_init, float* cost_aggr, ADCensus_Option option)
{
//allocate d size thread and d*2 size shared memory
//one for last aggr row and one for current init row and one for minimum reduction
extern volatile __shared__ float smem[];
const int direction = 1; //from top 2 bottom index increase, increase row_idx
const int width = option.width; //xdim
const int height = option.height; //ydim
const int min_disp = option.min_disparity;
const int max_disp = option.max_disparity;
const float p1 = option.so_p1;
const float p2 = option.so_p2;
const float tso = option.so_tso;
const int disp_range = max_disp - min_disp;
float* last_aggr_row_buffer = (float*)smem; //shared mem array with disp range
float* cost_init_row_buffer = (float*)&last_aggr_row_buffer[disp_range];//shared mem array with disp range
const int col_idx = blockIdx.x; //move in row order, col stay cool
int d_idx = threadIdx.x;
int tid = d_idx;
int wrap_id = tid / 32; //location of current wrap
int wrap_num = max_disp / 32;
//first to handle first col first col all disparity of cost aggr do no need to compute
int row_idx = 0;
last_aggr_row_buffer[d_idx] = cost_init[Get3dIdx(row_idx, col_idx, d_idx, height, width, disp_range)];
__syncthreads();
cost_aggr[Get3dIdx(row_idx, col_idx, d_idx, height, width, disp_range)] = last_aggr_row_buffer[d_idx];
uchar3 color = FETCH_UCHAR3(img_left[Get2dIdxRGB(row_idx, col_idx, height, width)]);
uchar3 color_last = color;
//caculate minimum cost on the shared memory
__syncthreads();
float min_cost_last_path = last_aggr_row_buffer[d_idx];
for (int offset = 16; offset > 0; offset /= 2)
min_cost_last_path = min(__shfl_down_sync(FULL_MASK, min_cost_last_path, offset), min_cost_last_path);
if (tid % 32 == 0)
cost_init_row_buffer[wrap_id] = min_cost_last_path; //save wrap reduce result to corresponding wrap id
__syncthreads();
if (tid == 0) {
for (int w = 1; w < wrap_num; w++)
cost_init_row_buffer[0] = min(cost_init_row_buffer[0], cost_init_row_buffer[w]); //use thread 0 to caculate final answer
}
__syncthreads();
min_cost_last_path = cost_init_row_buffer[0]; //boardcast to every thread in the block
row_idx += direction;
__syncthreads();
//copy 1st row as buffer of current row start of iteration
cost_init_row_buffer[d_idx] = cost_init[Get3dIdx(row_idx, col_idx, d_idx, height, width, disp_range)];
__syncthreads();
for (int j = 0; j < height - 1; j++)
{
color = FETCH_UCHAR3(img_left[Get2dIdxRGB(row_idx, col_idx, height, width)]);
uint8_t d1 = ColorDist(color, color_last);
uint8_t d2 = d1;
int yr = col_idx - d_idx - min_disp;
if (yr > 0 && yr < width - 1)
{
uchar3 color_r = FETCH_UCHAR3(img_right[Get2dIdxRGB(row_idx, yr, height, width)]);
uchar3 color_last_r = FETCH_UCHAR3(img_right[Get2dIdxRGB(row_idx-direction, yr, height, width)]);
d2 = ColorDist(color_r, color_last_r);
}
float P1(0.0f), P2(0.0f);
if (d1 < tso && d2 < tso) {
P1 = p1;
P2 = p2;
}
else if (d1 < tso && d2 >= tso) {
P1 = p1 / 4.0f;
P2 = p2 / 4.0f;
}
else if (d1 >= tso && d2 < tso) {
P1 = p1 / 4.0f;
P2 = p2 / 4.0f;
}
else if (d1 >= tso && d2 >= tso) {
P1 = p1 / 10.0f;
P2 = p2 / 10.0f;
}
const float cost = cost_init_row_buffer[d_idx];
const float l1 = last_aggr_row_buffer[d_idx];//d_idx + 1 < max_disp ? last_aggr_row_buffer[d_idx + 1] : 99999.0f;
//l2 is pixel on the left
const float l2 = d_idx - 1 >= 0 ? last_aggr_row_buffer[d_idx - 1] + P1 : 99999.0f + P1;
//l3 is pixel on the right
const float l3 = d_idx + 1 < max_disp ? last_aggr_row_buffer[d_idx + 1] + P1 : 99999.0f + P1;//d_idx + 2 < max_disp ? last_aggr_row_buffer[d_idx + 2] : 99999.0f;
const float l4 = min_cost_last_path + P2;
float cost_s = cost + float(min(min(l1, l2), min(l3, l4)));
cost_s /= 2.0f;
__syncthreads();
cost_aggr[Get3dIdx(row_idx, col_idx, d_idx, height, width, disp_range)] = cost_s; //update cost aggr
last_aggr_row_buffer[d_idx] = cost_s; //IMPORTANT:update cost aggr last aggr row buffer for next column
//caculate the best minimum cost this round,re-use cost_init_row_buffer
float min_cost_this_path = cost_s;
for (int offset = 16; offset > 0; offset /= 2)
min_cost_this_path = min(__shfl_down_sync(FULL_MASK, min_cost_this_path, offset), min_cost_this_path);
if (tid % 32 == 0)
cost_init_row_buffer[wrap_id] = min_cost_this_path; //save wrap reduce result to corresponding wrap id
__syncthreads();
if (tid == 0) {
for (int w = 1; w < wrap_num; w++)
cost_init_row_buffer[0] = min(cost_init_row_buffer[0], cost_init_row_buffer[w]); //use thread 0 to caculate final answer
}
__syncthreads();
min_cost_last_path = cost_init_row_buffer[0]; //boardcast to every thread in the block to replace the cost of min_cost_last_path
row_idx += direction; //prepare to start next row
__syncthreads();
//dont need to do this in last iteration
if (row_idx < height) {
cost_init_row_buffer[d_idx] = cost_init[Get3dIdx(row_idx, col_idx, d_idx, height, width, disp_range)];
color_last = color;
}
__syncthreads();
} //for loop ends
//if (col_idx == 100 && d_idx == 0)
// printf("scanline val left 2 right of %d , %d %d is %f \n", 100, col_idx, d_idx, cost_aggr[Get3dIdx(100, col_idx, d_idx, height, width, disp_range)]);
//if (col_idx == 100 && d_idx == 100)
// printf("scanline val left 2 right of %d , %d %d is %f \n", 100, col_idx, d_idx, cost_aggr[Get3dIdx(100, col_idx, d_idx, height, width, disp_range)]);
}
__global__ void scanline_optimize_bottom2top(uint8_t* img_left, uint8_t* img_right, float* cost_init, float* cost_aggr, ADCensus_Option option)
{
//allocate d size thread and d*2 size shared memory
//one for last aggr row and one for current init row and one for minimum reduction
extern volatile __shared__ float smem[];
int direction = -1; //from top 2 bottom index increase, increase row_idx
const int width = option.width; //xdim
const int height = option.height; //ydim
const int min_disp = option.min_disparity;
const int max_disp = option.max_disparity;
const float p1 = option.so_p1;
const float p2 = option.so_p2;
const float tso = option.so_tso;
const int disp_range = max_disp - min_disp;
float* last_aggr_row_buffer = (float*)smem; //shared mem array with disp range
float* cost_init_row_buffer = (float*)&last_aggr_row_buffer[disp_range];//shared mem array with disp range
const int col_idx = blockIdx.x; //move in row order, col stay cool
int d_idx = threadIdx.x;
int tid = d_idx;
int wrap_id = tid / 32; //location of current wrap
int wrap_num = max_disp / 32;
//first to handle first row first col all disparity of cost aggr do no need to compute
int row_idx = height-1;
last_aggr_row_buffer[d_idx] = cost_init[Get3dIdx(row_idx, col_idx, d_idx, height, width, disp_range)];
__syncthreads();
cost_aggr[Get3dIdx(row_idx, col_idx, d_idx, height, width, disp_range)] = last_aggr_row_buffer[d_idx];
uchar3 color = FETCH_UCHAR3(img_left[Get2dIdxRGB(row_idx, col_idx, height, width)]);
uchar3 color_last = color;
//caculate minimum cost on the shared memory
__syncthreads();
float min_cost_last_path = last_aggr_row_buffer[d_idx];
for (int offset = 16; offset > 0; offset /= 2)
min_cost_last_path = min(__shfl_down_sync(FULL_MASK, min_cost_last_path, offset), min_cost_last_path);
if (tid % 32 == 0)
cost_init_row_buffer[wrap_id] = min_cost_last_path; //save wrap reduce result to corresponding wrap id
__syncthreads();
if (tid == 0) {
for (int w = 1; w < wrap_num; w++)
cost_init_row_buffer[0] = min(cost_init_row_buffer[0], cost_init_row_buffer[w]); //use thread 0 to caculate final answer
}
__syncthreads();
min_cost_last_path = cost_init_row_buffer[0]; //boardcast to every thread in the block
row_idx += direction;
__syncthreads();
//copy 1st row as buffer of current row start of iteration
cost_init_row_buffer[d_idx] = cost_init[Get3dIdx(row_idx, col_idx, d_idx, height, width, disp_range)];
__syncthreads();
for (int j = 0; j < height - 1; j++)
{
color = FETCH_UCHAR3(img_left[Get2dIdxRGB(row_idx, col_idx, height, width)]);
uint8_t d1 = ColorDist(color, color_last);
uint8_t d2 = d1;
int yr = col_idx - d_idx - min_disp;
if (yr > 0 && yr < width - 1)
{
uchar3 color_r = FETCH_UCHAR3(img_right[Get2dIdxRGB(row_idx, yr, height, width)]);
uchar3 color_last_r = FETCH_UCHAR3(img_right[Get2dIdxRGB(row_idx - direction, yr, height, width)]);
d2 = ColorDist(color_r, color_last_r);
}
float P1(0.0f), P2(0.0f);
if (d1 < tso && d2 < tso) {
P1 = p1;
P2 = p2;
}
else if (d1 < tso && d2 >= tso) {
P1 = p1 / 4;
P2 = p2 / 4;
}
else if (d1 >= tso && d2 < tso) {
P1 = p1 / 4;
P2 = p2 / 4;
}
else if (d1 >= tso && d2 >= tso) {
P1 = p1 / 10;
P2 = p2 / 10;
}
const float cost = cost_init_row_buffer[d_idx];
const float l1 = last_aggr_row_buffer[d_idx];
//l2 is pixel on the left
const float l2 = d_idx - 1 >= 0 ? last_aggr_row_buffer[d_idx - 1] + P1 : 99999.0f + P1;
//l3 is pixel on the right
const float l3 = d_idx + 1 < max_disp ? last_aggr_row_buffer[d_idx + 1] + P1 : 99999.0f + P1;
const float l4 = min_cost_last_path + P2;
float cost_s = cost + float(min(min(l1, l2), min(l3, l4)));
cost_s /= 2.0f;
__syncthreads();
cost_aggr[Get3dIdx(row_idx, col_idx, d_idx, height, width, disp_range)] = cost_s; //update cost aggr
last_aggr_row_buffer[d_idx] = cost_s; //IMPORTANT:update cost aggr last aggr row buffer for next column
//caculate the best minimum cost this round,re-use cost_init_row_buffer
float min_cost_this_path = cost_s;
for (int offset = 16; offset > 0; offset /= 2)
min_cost_this_path = min(__shfl_down_sync(FULL_MASK, min_cost_this_path, offset), min_cost_this_path);
if (tid % 32 == 0)
cost_init_row_buffer[wrap_id] = min_cost_this_path; //save wrap reduce result to corresponding wrap id
__syncthreads();
if (tid == 0) {
for (int w = 1; w < wrap_num; w++)
cost_init_row_buffer[0] = min(cost_init_row_buffer[0], cost_init_row_buffer[w]); //use thread 0 to caculate final answer
}
__syncthreads();
min_cost_last_path = cost_init_row_buffer[0]; //boardcast to every thread in the block to replace the cost of min_cost_last_path
row_idx += direction; //prepare to start next column
__syncthreads();
//dont need to do this in last iteration
if (row_idx >= 0) {
cost_init_row_buffer[d_idx] = cost_init[Get3dIdx(row_idx, col_idx, d_idx, height, width, disp_range)];
color_last = color;
}
__syncthreads();
}
//if (col_idx == 100 && d_idx == 0)
// printf("scanline val left 2 right of %d , %d %d is %f \n", 100, col_idx, d_idx, cost_aggr[Get3dIdx(100, col_idx, d_idx, height, width, disp_range)]);
//if (col_idx == 100 && d_idx == 100)
// printf("scanline val left 2 right of %d , %d %d is %f \n", 100, col_idx, d_idx, cost_aggr[Get3dIdx(100, col_idx, d_idx, height, width, disp_range)]);
}