-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphcut.cl
1169 lines (918 loc) · 24.5 KB
/
graphcut.cl
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define TRUE 1
#define FALSE 0
#define imin(a, b) (a < b) ? a : b
float weight(uint c1, uint c2);
#define WAVE_BREDTH 32
#define WAVE_LENGTH 8
#define WAVES_PER_WORKGROUP 4
#define WORKGROUP_LENGTH (WAVES_PER_WORKGROUP * WAVE_LENGTH)
#define BETA 1.0f/10
#define IMAGEW 800
#define IMAGEH 608
#define NWAVES (IMAGEH/WAVE_LENGTH)
__kernel void check_completion(
__global int* tiles_list,
__global float* excess,
__global int* bfs,
__local int* tile_has_active_nodes,
__global int* isCompleted
)
{
//get tile x,y offset
int txy = tiles_list[get_group_id(0)];
int tx = txy%TILESW;
int ty = txy/TILESW;
int lx = get_local_id(0);
int ly = get_local_id(1);
//image coordinates
int ix = tx*WAVE_BREDTH + lx;
int iy = (WAVES_PER_WORKGROUP*ty + ly)*WAVE_LENGTH;
int ixy = iy*IMAGEW + ix;
if (lx == 0 && ly == 0)
tile_has_active_nodes[0] = FALSE;
barrier(CLK_LOCAL_MEM_FENCE);
for (int i=0; i<WAVE_LENGTH; i++) {
if (excess[ixy] > 0 && bfs[ixy] < MAX_HEIGHT) { //looking for active nodes
tile_has_active_nodes[0] = TRUE;
}
ixy += IMAGEW;
}
barrier(CLK_LOCAL_MEM_FENCE);
if (lx == 0 && ly == 0) {
if (tile_has_active_nodes[0]) //tile has active nodes
isCompleted[0] = FALSE;
}
}
//work straight from the excess image - this is just a place holder for now
__kernel void init_gc(
__global float* up,
__global float* down,
__global float* left,
__global float* right,
__global float* excesses,
__local int* tile_flags, //tile has nodes with excess >= 0
__global int* tilesLoad,
int iteration
) {
int gx = get_global_id(0);
int gy = get_global_id(1);
int gxy = gy*IMAGEW + gx;
int lx = get_local_id(0);
int ly = get_local_id(0);
if (ly == 0) tile_flags[0] = FALSE;
barrier(CLK_LOCAL_MEM_FENCE);
int ixy = WAVE_LENGTH*gy*IMAGEW + gx;
float excess;
for (int i=0; i<WAVE_LENGTH; i++) {
up[ixy] = 0;
down[ixy] = 0;
left[ixy] = 0;
right[ixy] = 0;
//bfs needs all tiles with excess >= 0
if (excesses[ixy] >= 0)
tile_flags[0] = TRUE;
ixy += IMAGEW;
}
barrier(CLK_LOCAL_MEM_FENCE);
if (lx == 0 && ly == 0) {
int tx = get_group_id(0);
int ty = get_group_id(1);
int txy = ty*TILESW + tx;
if (tile_flags[0]) {
tilesLoad[txy] = iteration;
}
}
}
__kernel void add_border(
__global int* tiles_list,
__global float* border,
__global float* excess,
int direction
)
{
//get tile x,y offset
int txy = tiles_list[get_group_id(0)];
int tx = txy%TILESW;
int ty = txy/TILESW;
int lx = get_local_id(0);
int ix = tx*WAVE_BREDTH + lx;
int iy = ty;
int ixy = iy*IMAGEW + ix;
int ix2, iy2, ixy2;
switch(direction) {
case 0:
ix2 = ix;
iy2 = ty*WORKGROUP_LENGTH;
break;
case 1:
ix2 = ix;
iy2 = (ty+1)*WORKGROUP_LENGTH - 1;
break;
case 2:
ix2 = tx*WORKGROUP_LENGTH;
iy2 = ty*WAVE_BREDTH + lx;
break;
case 3: //flow received from tile to right
ix2 = (tx+1)*WORKGROUP_LENGTH - 1;
iy2 = ty*WAVE_BREDTH + lx;
break;
}
ixy2 = iy2*IMAGEW + ix2;
excess[ixy2] += border[ixy];
}
__kernel void push_left(
__global int* tiles_list,
__global float* excess,
__local float* excess_s,
__global int* height,
__local int* height_s,
__global float* right,
__global float* left,
__global float* border,
__local int* flags,
__global int* border_tiles,
int iteration
)
{
//get tile x,y offset
int txy = tiles_list[get_group_id(0)];
int tx = txy%TILESW;
int ty = txy/TILESW;
int lx = get_local_id(0);
int ly = get_local_id(1);
int lw = get_local_size(0);
int lh = get_local_size(1);
//image coordinates
int ix = tx*WAVE_BREDTH + lx;
int iy = (WAVES_PER_WORKGROUP*ty + ly)*WAVE_LENGTH;
int ixy = iy*IMAGEW + ix;
int iwS = lw + 1;
int ixS = lx;
int iyS = ly*WAVE_LENGTH;
int ixyS = iyS*iwS + ixS;
if (lx == 0 && ly == 0)
flags[0] = FALSE;
for (int i=0; i<WAVE_LENGTH; i++) {
excess_s[ixyS] = excess[ixy];
height_s[ixyS + 1] = height[ixy];
ixy += IMAGEW;
ixyS += iwS; //padding to prevent bank conflicts
}
//load first row of heights from next tile
if (ly == lh-1) {
int ix2 = tx*WAVE_BREDTH - 1;
int iy2 = ty*WORKGROUP_LENGTH + lx;
ixy = iy2*IMAGEW + ix2;
height_s[lx*iwS + 0] = height[ixy];
}
barrier(CLK_LOCAL_MEM_FENCE);
iy = (WAVES_PER_WORKGROUP*ty + ly+1)*WAVE_LENGTH - 1;
ixy = iy*IMAGEW + ix;
ixS = (ly+1)*WAVE_LENGTH - 1;
iyS = lx;
ixyS = iyS*iwS + ixS;
float ef = 0;
float flow = 0;
int h = height_s[ixyS + 1];
int hNext;
flow = 0;
for (int i=0; i<WAVE_LENGTH; i++) {
ef = excess_s[ixyS] + flow;
hNext = height_s[ixyS +1 - 1]; //+1 for offset, -1 for going left
if (ef > 0 && h < MAX_HEIGHT && h == hNext + 1) {
flow = min(ef, left[ixy]);
}
else
flow = 0;
left[ixy] -= flow;
excess_s[ixyS] = ef-flow;
if (ly == 0 && i == WAVE_LENGTH-1)
right[ixy - 32 + 31*IMAGEW] += flow;
else
right[ixy-IMAGEW] += flow;
h = hNext;
ixy -= IMAGEW;
ixyS -= 1;
}
barrier(CLK_LOCAL_MEM_FENCE);
if (ly != 0)
excess_s[ixyS] += flow;
else if (flow > 0) // does the tile-overspilling wave have excess flow?
flags[0] = TRUE;
barrier(CLK_LOCAL_MEM_FENCE);
iy = (WAVES_PER_WORKGROUP*ty + ly)*WAVE_LENGTH;
ixy = iy*IMAGEW + ix;
iwS = lw + 1;
ixS = lx;
iyS = ly*WAVE_LENGTH;
ixyS = iyS*iwS + ixS;
for (int i=0; i<WAVE_LENGTH; i++) {
excess[ixy] = excess_s[ixyS];
ixy += IMAGEW;
ixyS += iwS;
}
if (ly == 0 && flags[0] == TRUE) {
border[ty*IMAGEW + ix - WAVE_BREDTH] = flow;
border_tiles[txy - 1] = iteration;
}
}
__kernel void push_right(
__global int* tiles_list,
__global float* excess,
__local float* excess_s,
__global int* height,
__local int* height_s,
__global float* right,
__global float* left,
__global float* border,
__local int* flags,
__global int* border_tiles,
int iteration
)
{
//get tile x,y offset
int txy = tiles_list[get_group_id(0)];
int tx = txy%TILESW;
int ty = txy/TILESW;
int lx = get_local_id(0);
int ly = get_local_id(1);
int lw = get_local_size(0);
int lh = get_local_size(1);
//image coordinates
int ix = tx*WAVE_BREDTH + lx;
int iy = (WAVES_PER_WORKGROUP*ty + ly)*WAVE_LENGTH;
int ixy = iy*IMAGEW + ix;
int iwS = lw + 1;
int ixS = lx;
int iyS = ly*WAVE_LENGTH;
int ixyS = iyS*iwS + ixS;
if (lx == 0 && ly == 0)
flags[0] = FALSE;
for (int i=0; i<WAVE_LENGTH; i++) {
excess_s[ixyS] = excess[ixy];
height_s[ixyS] = height[ixy];
ixy += IMAGEW;
ixyS += iwS; //padding to prevent bank conflicts
}
//load first row of heights from next tile
if (ly == lh-1) {
int ix2 = (tx+1)*WAVE_BREDTH;
int iy2 = ty*WORKGROUP_LENGTH + lx;
ixy = iy2*IMAGEW + ix2;
height_s[lx*iwS + lw] = height[ixy];
}
barrier(CLK_LOCAL_MEM_FENCE);
ixy = iy*IMAGEW + ix;
ixS = ly*WAVE_LENGTH;
iyS = lx;
ixyS = iyS*iwS + ixS;
float ef = 0;
float flow = 0;
int h = height_s[ixyS];
int hNext;
flow = 0;
for (int i=0; i<WAVE_LENGTH; i++) {
ef = excess_s[ixyS] + flow;
hNext = height_s[ixyS+1];
if (ef > 0 && h < MAX_HEIGHT && h == hNext + 1) {
flow = min(ef, right[ixy]);
}
else
flow = 0;
right[ixy] -= flow;
excess_s[ixyS] = ef-flow;
if (ly == 3 && i == WAVE_LENGTH-1)
left[ixy + 32 - 31*IMAGEW] += flow;
else
left[ixy+IMAGEW] += flow;
h = hNext;
ixy += IMAGEW;
ixyS += 1;
}
barrier(CLK_LOCAL_MEM_FENCE);
if (ly != lh-1)
excess_s[ixyS] += flow;
else if (flow > 0) // does the tile-overspilling wave have excess flow?
flags[0] = TRUE;
barrier(CLK_LOCAL_MEM_FENCE);
ixy = iy*IMAGEW + ix;
iwS = lw + 1;
ixS = lx;
iyS = ly*WAVE_LENGTH;
ixyS = iyS*iwS + ixS;
for (int i=0; i<WAVE_LENGTH; i++) {
excess[ixy] = excess_s[ixyS];
ixy += IMAGEW;
ixyS += iwS;
}
if (ly == lh-1 && flags[0] == TRUE) {
border[ty*IMAGEW + ix + WAVE_BREDTH] = flow;
border_tiles[txy + 1] = iteration;
}
}
__kernel void push_down(
__global int* tiles_list,
__global float* down,
__global float* up,
__global int* height,
__global float* excess,
__global float* border,
__local int* flags,
__global int* border_tiles,
int iteration
)
{
//get tile x,y offset
int txy = tiles_list[get_group_id(0)];
int tx = txy%TILESW;
int ty = txy/TILESW;
int lx = get_local_id(0);
int ly = get_local_id(1);
int lh = get_local_size(1);
//image coordinates
int ix = tx*WAVE_BREDTH + lx;
int iy = (WAVES_PER_WORKGROUP*ty + ly)*WAVE_LENGTH;
int ixy = iy*IMAGEW + ix;
if (lx == 0 && ly == 0)
flags[0] = FALSE;
barrier(CLK_LOCAL_MEM_FENCE);
float ef = 0;
float flow = 0;
int h = height[ixy];
int hNext;
for (int i=0; i<WAVE_LENGTH; i++) {
ef = excess[ixy] + flow;
hNext = height[ixy+IMAGEW];
if (ef > 0 && h < MAX_HEIGHT && h == hNext + 1) {
flow = min(ef, down[ixy]);
}
else
flow = 0;
down[ixy] -= flow;
excess[ixy] = ef-flow;
up[ixy+IMAGEW] += flow;
h = hNext;
ixy += IMAGEW;
}
barrier(CLK_LOCAL_MEM_FENCE);
if (ly != lh-1)
excess[ixy] += flow;
else if (flow > 0) // does the tile-overspilling wave have excess flow?
flags[0] = TRUE;
barrier(CLK_LOCAL_MEM_FENCE);
if (ly == lh-1 && flags[0] == TRUE) {
border[ty*IMAGEW + ix + IMAGEW] = flow;
border_tiles[txy + TILESW] = iteration;
}
}
__kernel void push_up(
__global int* tiles_list,
__global float* down,
__global float* up,
__global int* height,
__global float* excess,
__global float* border,
__local int* flags,
__global int* border_tiles,
int iteration
)
{
int txy = tiles_list[get_group_id(0)];
int tx = txy%TILESW;
int ty = txy/TILESW;
int lx = get_local_id(0);
int ly = get_local_id(1);
//image coordinates
int ix = tx*WAVE_BREDTH + lx;
int iy = (WAVES_PER_WORKGROUP*ty + ly+1)*WAVE_LENGTH - 1;
int ixy = iy*IMAGEW + ix;
if (lx == 0 && ly == 0)
flags[0] = FALSE;
barrier(CLK_LOCAL_MEM_FENCE);
float ef = 0;
float flow = 0;
int h = height[ixy];
int hNext;
for (int i=WAVE_LENGTH-1; i>=0; i--) {
ef = excess[ixy] + flow;
hNext = height[ixy-IMAGEW];
if (ef > 0 && h < MAX_HEIGHT && h == hNext + 1)
flow = min(ef, up[ixy]);
else
flow = 0;
up[ixy] -= flow;
excess[ixy] = ef-flow;
down[ixy-IMAGEW] += flow;
h = hNext;
ixy -= IMAGEW;
}
barrier(CLK_LOCAL_MEM_FENCE);
if (ly != 0)
excess[ixy] += flow;
else if (flow > 0) // does the tile-overspilling wave have excess flow?
flags[0] = TRUE;
barrier(CLK_LOCAL_MEM_FENCE);
if (ly == 0 && flags[0] == TRUE) {
border[ty*IMAGEW + ix - IMAGEW] = flow;
border_tiles[txy - TILESW] = iteration;
}
}
__kernel void relabel(
__global float* down,
__global float* right,
__global float* up,
__global float* left,
__global float* excess,
__global int* height,
__global int* height2
)
{
int gx = get_global_id(0);
int gy = get_global_id(1);
int gxy = gy*IMAGEW + gx;
int gxT = (get_group_id(0)/2)*WAVE_BREDTH + gy%32;
int gyT = (get_group_id(1)/2)*WORKGROUP_LENGTH + gx%32;
int gxyT = gyT*IMAGEW + gxT;
int h = height[gxy];
int newHeight = h;
if (excess[gxy] > 0 && h < MAX_HEIGHT) {
newHeight = MAX_HEIGHT;
if (gy < IMAGEH-1 && down[gxy] > 0)
newHeight = imin(newHeight, height[gxy+IMAGEW]+1);
if (gy > 0 && up[gxy] > 0)
newHeight = imin(newHeight, height[gxy-IMAGEW]+1);
if (gx < IMAGEW-1 && right[gxyT] > 0)
newHeight = imin(newHeight, height[gxy+1]+1);
if (gx > 0 && left[gxyT] > 0)
newHeight = imin(newHeight, height[gxy-1]+1);
}
height2[gxy] = newHeight;
return;
}
//compact up,down,left,right residual weights
//handle intertile gaps, flag tiles with discrepancies
//flag tiles with intratile gaps
__kernel void init_bfs(
__global int* tiles_list,
__global float* excess,
__local float* excess_s,
__global int* bfs,
__local int* tile_flags, //0:has nodes with excess < 0, 1: nodes with excess >= 0
__global float* down,
__global float* up,
__global float* right,
__global float* left,
__global uint* can_downs,
__global uint* can_ups,
__global uint* can_rights,
__global uint* can_lefts,
__local uchar* can_downs_s,
__local uchar* can_ups_s,
__local uchar* can_rights_s,
__local uchar* can_lefts_s,
__global int* bfs_tiles,
int iteration
)
{
//get tile x,y offset
int txy = tiles_list[get_group_id(0)];
int tx = txy%TILESW;
int ty = txy/TILESW;
int lx = get_local_id(0);
int ly = get_local_id(1);
int lw = get_local_size(0);
//image coordinates
int ix = tx*WAVE_BREDTH + lx;
int iy = (WAVES_PER_WORKGROUP*ty + ly)*WAVE_LENGTH;
int ixy = iy*IMAGEW + ix;
int iwS = lw;
int ixS = lx;
int iyS = ly*WAVE_LENGTH;
int ixyS = iyS*iwS + ixS;
for (int i=0; i<WAVE_LENGTH; i++) {
excess_s[ixyS] = excess[ixy];
ixy += IMAGEW;
ixyS += iwS;
}
int ix2, iy2, ixy2;
if (lx < 6) tile_flags[lx] = FALSE;
barrier(CLK_LOCAL_MEM_FENCE);
uchar can_down = 0;
uchar can_up = 0;
uchar can_right = 0;
uchar can_left = 0;
int dist;
ixy = iy*IMAGEW + ix;
ixyS = iyS*iwS + ixS;
for (int i=0; i<WAVE_LENGTH; i++) {
if (excess_s[ixyS] >= 0) {
dist = MAX_HEIGHT;
tile_flags[1] = TRUE;
}
else {
dist = 0;
tile_flags[0] = TRUE;
}
bfs[ixy] = dist;
if (down[ixy] > 0) can_down |= 128; //10000000 in binary
if (up[ixy] > 0) can_up |= 128;
if (right[ixy] > 0) can_right |= 128;
if (left[ixy] > 0) can_left |= 128;
if (i != WAVE_LENGTH-1) {
can_down >>= 1;
can_up >>= 1;
can_right >>= 1;
can_left >>= 1;
}
ixy += IMAGEW;
ixyS += iwS;
}
can_downs_s[ly*WAVE_BREDTH + lx] = can_down;
can_ups_s[ly*WAVE_BREDTH + lx] = can_up;
can_rights_s[ly*WAVE_BREDTH + lx] = can_right;
can_lefts_s[ly*WAVE_BREDTH + lx] = can_left;
barrier(CLK_LOCAL_MEM_FENCE);
float e1, e2;
switch(ly) {
case 0: //up
if (ty == 0) break;
ix2 = ix;
iy2 = ty*WORKGROUP_LENGTH;
ixy2 = iy2*IMAGEW + ix2;
ixS = lx;
iyS = 0;
ixyS = iyS*iwS + ixS;
e1 = excess_s[ixyS];
e2 = excess[ixy2-IMAGEW];
// no need to check the reverse i.e e2 >= 0 and e1 < 0
// if e2 >= 0 was true the tile above would be processed
if ((can_ups_s[lx] & 1) && e1 >= 0 && e2 < 0) { // & 1 -> check first bit to see if possible to push up
bfs[ixy2] = 1; // dist of 1 away from sink
bfs_tiles[txy] = iteration;
}
break;
case 1: //down
if (ty == TILESH-1) break;
ix2 = ix;
iy2 = (ty+1)*WORKGROUP_LENGTH - 1;
ixy2 = iy2*IMAGEW + ix2;
ixS = lx;
iyS = WORKGROUP_LENGTH-1;
ixyS = iyS*iwS + ixS;
e1 = excess_s[ixyS];
e2 = excess[ixy2+IMAGEW];
if (((can_downs_s[3*WAVE_BREDTH + lx] & 128) == 128) && e1 >= 0 && e2 < 0) {
bfs[ixy2] = 1;
bfs_tiles[txy] = iteration;
}
break;
case 2: //right
if (tx == TILESW-1) break;
ix2 = (tx+1)*WAVE_BREDTH - 1;
iy2 = ty*WORKGROUP_LENGTH + lx;
ixy2 = iy2*IMAGEW + ix2;
ixS = WAVE_BREDTH-1;
iyS = lx;
ixyS = iyS*iwS + ixS;
e1 = excess_s[ixyS];
e2 = excess[ixy2 + 1];
if (((can_rights_s[3*WAVE_BREDTH + lx] & 128) == 128) && e1 >= 0 && e2 < 0) {
bfs[ixy2] = 1;
bfs_tiles[txy] = iteration;
}
break;
case 3: //left
if (ty == 0) break;
ix2 = tx*WAVE_BREDTH;
iy2 = ty*WORKGROUP_LENGTH + lx;
ixy2 = iy2*IMAGEW + ix2;
ixS = 0;
iyS = lx;
ixyS = iyS*iwS + ixS;
e1 = excess_s[ixyS];
e2 = excess[ixy2 - 1];
if ((can_lefts_s[lx] & 1) && e1 >= 0 && e2 < 0) {
bfs[ixy2] = 1;
bfs_tiles[txy] = iteration;
}
break;
}
if (lx == 0 && ly == 0) {
if (tile_flags[0] && tile_flags[1]) //has intratile gaps
bfs_tiles[txy] = iteration;
}
if (ly == 0) {
can_downs[ty*IMAGEW + ix] =
(can_downs_s[3*WAVE_BREDTH + lx] << 24) |
(can_downs_s[2*WAVE_BREDTH + lx] << 16) |
(can_downs_s[1*WAVE_BREDTH + lx] << 8 ) |
can_downs_s[lx];
}
if (ly == 1) {
can_ups[ty*IMAGEW + ix] =
(can_ups_s[3*WAVE_BREDTH + lx] << 24) |
(can_ups_s[2*WAVE_BREDTH + lx] << 16) |
(can_ups_s[1*WAVE_BREDTH + lx] << 8 ) |
can_ups_s[lx];
}
if (ly == 2) {
can_rights[ty*IMAGEW + ix] =
(can_rights_s[3*WAVE_BREDTH + lx] << 24) |
(can_rights_s[2*WAVE_BREDTH + lx] << 16) |
(can_rights_s[1*WAVE_BREDTH + lx] << 8 ) |
can_rights_s[lx];
}
if (ly == 3) {
can_lefts[ty*IMAGEW + ix] =
(can_lefts_s[3*WAVE_BREDTH + lx] << 24) |
(can_lefts_s[2*WAVE_BREDTH + lx] << 16) |
(can_lefts_s[1*WAVE_BREDTH + lx] << 8 ) |
can_lefts_s[lx];
}
}
__kernel void bfs_intertile(
__global int* tiles_list,
__local int* flags,
__global int* bfs,
__global uint* can_downs,
__global uint* can_ups,
__global uint* can_rights,
__global uint* can_lefts,
int iteration,
__global int* bfs_tiles
) {
int txy = tiles_list[get_group_id(0)];
int tx = txy%TILESW;
int ty = txy/TILESW;
int lx = get_local_id(0);
int lw = get_local_size(0);
if (lx < 3) flags[lx] = FALSE;
barrier(CLK_LOCAL_MEM_FENCE);
//intertile gap with tile above
int ixy = (ty*WORKGROUP_LENGTH)*IMAGEW + (tx*WAVE_BREDTH + lx); //index into image
int ixy2 = ty*IMAGEW + (tx*WAVE_BREDTH + lx); //index into compressed edges
bool can = can_ups[ixy2] & 0x1;
bool can2 = (can_downs[ixy2-IMAGEW] >> WORKGROUP_LENGTH-1) & 0x1;
int dist = bfs[ixy];
int dist2 = bfs[ixy-IMAGEW];
if(can && dist2+1 < dist) {
bfs[ixy] = dist2+1;
flags[0] = TRUE;
}
if(can2 && dist+1 < dist2) {
bfs[ixy-IMAGEW] = dist+1;
flags[1] = TRUE;
}
//intertile gap with tile to left
can = can_lefts[ixy2] & 0x1;
can2 = (can_rights[ixy2-WAVE_BREDTH] >> WORKGROUP_LENGTH-1) & 0x1;
ixy = (ty*WORKGROUP_LENGTH + lx)*IMAGEW + tx*WAVE_BREDTH;
dist = bfs[ixy];
dist2 = bfs[ixy-1];
if(can && dist2+1 < dist) { //up and left checked by same block
bfs[ixy] = dist2+1;
flags[0] = TRUE;
}
if(can2 && dist+1 < dist2) {
bfs[ixy-1] = dist+1;
flags[2] = TRUE;
}
barrier(CLK_LOCAL_MEM_FENCE);
if (lx == 0 && flags[0] == TRUE) bfs_tiles[txy] = iteration;
else if (lx == 1 && flags[1] == TRUE) bfs_tiles[txy-TILESW] = iteration;
else if (lx == 2 && flags[2] == TRUE) bfs_tiles[txy-1] = iteration;
}
__kernel void bfs_intratile(
__global int* active_tiles,
__global int* bfs,
__local int* bfs_s,
__global uint* can_downs,
__global uint* can_ups,
__global uint* can_rights,
__global uint* can_lefts,
__local int* _l_didChange,
__global int* bfs_edges,
int iteration
)
{
int txy = active_tiles[get_group_id(0)];
int tx = txy%TILESW;
int ty = txy/TILESW;
int lx = get_local_id(0);
int ly = get_local_id(1);
int lw = get_local_size(0);
int sw = lw + 2;
int ixy = (ty*WORKGROUP_LENGTH + ly*WAVE_LENGTH)*IMAGEW + tx*WAVE_BREDTH + lx;
int sxy = (ly*WAVE_LENGTH + 1)*sw + lx + 1;
//load bfs distances into shared mem
//MAX_HEIGHT filled padding used to not worry about boundary issues later on
//and eliminate bank conflicts
for (int i=0; i<WAVE_LENGTH; i++) {
bfs_s[sxy] = bfs[ixy]; //height is offeset by 1 in shared mem
ixy += IMAGEW;
sxy += sw;
}
switch(ly) {
case 0: bfs_s[1+lx] = MAX_HEIGHT; break; //fill top padding
case 1: bfs_s[(1+WORKGROUP_LENGTH)*sw + 1+lx] = MAX_HEIGHT; break; //bottom
case 2: bfs_s[(1+lx)*sw] = MAX_HEIGHT; break; //left
case 3: bfs_s[(1+lx)*sw + sw-1] = MAX_HEIGHT; break; //right
//dont need corners - only using 4 neightbourhood
}
barrier(CLK_LOCAL_MEM_FENCE);
int bxy = ty*IMAGEW + tx*WAVE_BREDTH + lx;
int bxyT = tx*IMAGEH + ty*WAVE_BREDTH + lx;
uint can_down = (can_downs[bxy] >> ly*WAVE_LENGTH);
uint can_up = (can_ups[bxy] >> ly*WAVE_LENGTH);
uint can_right = (can_rights[bxy] >> ly*WAVE_LENGTH);
uint can_left = (can_lefts[bxy] >> ly*WAVE_LENGTH);
int _p_didChange = FALSE;
_l_didChange[0] = TRUE;
int i = 0;
while(_l_didChange[0] && i < 30) {
_p_didChange = FALSE;
_l_didChange[0] = FALSE;
sxy = (1 + ly*WAVE_LENGTH)*sw + lx + 1;
uint mask = 1; //00000001 in binary
for (int i=0; i<WAVE_LENGTH; i++) {
if ((can_up & mask) == mask && bfs_s[sxy-sw]+1 < bfs_s[sxy]) {
bfs_s[sxy] = bfs_s[sxy-sw]+1;
_p_didChange = TRUE;
}
mask <<= 1;
sxy += sw;
}
sxy -= sw;
//note that the bit sequence is in reverse for down and right
mask = 128; //10000000 in binary
for (int i=0; i<WAVE_LENGTH; i++) {
if ((can_down & mask) == mask && bfs_s[sxy+sw]+1 < bfs_s[sxy]) {
bfs_s[sxy] = bfs_s[sxy+sw]+1;
_p_didChange = TRUE;
}
mask >>= 1;
sxy -= sw;
}
sxy = (1 + lx)*sw + (1 + ly*WAVE_LENGTH);
mask = 1;
for (int i=0; i<WAVE_LENGTH; i++) {
if ((can_left & mask) == mask && bfs_s[sxy-1]+1 < bfs_s[sxy]) {
bfs_s[sxy] = bfs_s[sxy-1]+1;
_p_didChange = TRUE;
}
mask <<= 1;
sxy += 1;
}
sxy -= 1;
mask = 128;
for (int i=0; i<WAVE_LENGTH; i++) {
if ((can_right & mask) == mask && bfs_s[sxy+1]+1 < bfs_s[sxy]) {
bfs_s[sxy] = bfs_s[sxy+1]+1;
_p_didChange = TRUE;
}
mask >>= 1;
sxy -= 1;
}
if (_p_didChange) _l_didChange[0] = TRUE;
barrier(CLK_LOCAL_MEM_FENCE);
i++;
}
i--;
if (lx ==0 && ly == 0) {
if (i>0) {