forked from smistad/Tube-Segmentation-Framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kernels_no_3d_write.cl
2354 lines (2076 loc) · 74.4 KB
/
kernels_no_3d_write.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
__constant sampler_t sampler = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_NEAREST;
__constant sampler_t interpolationSampler = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_LINEAR;
__constant sampler_t hpSampler = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP | CLK_FILTER_NEAREST;
#define LPOS(pos) pos.x+pos.y*get_global_size(0)+pos.z*get_global_size(0)*get_global_size(1)
#define NLPOS(pos) ((pos).x) + ((pos).y)*size.x + ((pos).z)*size.x*size.y
#ifdef VECTORS_16BIT
#define FLOAT_TO_SNORM16_4(vector) convert_short4_sat_rte(vector * 32767.0f)
#define SNORM16_TO_FLOAT_4(vector) max(-1.0f, convert_float4(vector) / 32767.0f)
#define FLOAT_TO_SNORM16_3(vector) convert_short3_sat_rte(vector * 32767.0f)
#define SNORM16_TO_FLOAT_3(vector) max(-1.0f, convert_float3(vector) / 32767.0f)
#define FLOAT_TO_SNORM16_2(vector) convert_short2_sat_rte(vector * 32767.0f)
#define SNORM16_TO_FLOAT_2(vector) max(-1.0f, convert_float2(vector) / 32767.0f)
#define FLOAT_TO_SNORM16(vector) convert_short_sat_rte(vector * 32767.0f)
#define SNORM16_TO_FLOAT(vector) max(-1.0f, convert_float(vector) / 32767.0f)
#define VECTOR_FIELD_TYPE short
#define UNORM16_TO_FLOAT(v) (float)v / 65535.0f
#define FLOAT_TO_UNORM16(v) convert_ushort_sat_rte(v * 65535.0f)
#define TDF_TYPE ushort
#else
#define FLOAT_TO_SNORM16_4(vector) vector
#define SNORM16_TO_FLOAT_4(vector) vector
#define FLOAT_TO_SNORM16_3(vector) vector
#define SNORM16_TO_FLOAT_3(vector) vector
#define FLOAT_TO_SNORM16_2(vector) vector
#define SNORM16_TO_FLOAT_2(vector) vector
#define FLOAT_TO_SNORM16(vector) vector
#define SNORM16_TO_FLOAT(vector) vector
#define VECTOR_FIELD_TYPE float
#define UNORM16_TO_FLOAT(v) v
#define FLOAT_TO_UNORM16(v) v
#define TDF_TYPE float
#endif
// Intialize 2D image to 0
__kernel void init2DImage(
__write_only image2d_t image
) {
write_imageui(image, (int2)(get_global_id(0), get_global_id(1)), (uint4)(0,0,0,0));
}
// Intialize int buffer to 0
__kernel void initIntBuffer(
__global int * buffer
) {
buffer[get_global_id(0)] = 0;
}
// Intialize char buffer to 0
__kernel void initCharBuffer(
__global char * buffer
) {
buffer[get_global_id(0)] = 0;
}
// Intialize int buffer to its ID
__kernel void initIntBufferID(
__global int * buffer,
__private int sum
) {
int id = get_global_id(0);
if(id >= sum)
id = 0;
buffer[id] = id;
}
// Forward declaration of eigen_decomp function
void eigen_decomposition(float M[3][3], float V[3][3], float e[3]);
__constant int4 cubeOffsets2D[4] = {
{0, 0, 0, 0},
{0, 1, 0, 0},
{1, 0, 0, 0},
{1, 1, 0, 0},
};
__constant int4 cubeOffsets[8] = {
{0, 0, 0, 0},
{1, 0, 0, 0},
{0, 0, 1, 0},
{1, 0, 1, 0},
{0, 1, 0, 0},
{1, 1, 0, 0},
{0, 1, 1, 0},
{1, 1, 1, 0},
};
float3 gradientNormalized(
__read_only image3d_t volume, // Volume to perform gradient on
int4 pos, // Position to perform gradient on
int volumeComponent, // The volume component to perform gradient on: 0, 1 or 2
int dimensions // The number of dimensions to perform gradient in: 1, 2 or 3
) {
float f100, f_100, f010, f0_10, f001, f00_1;
switch(volumeComponent) {
case 0:
f100 = read_imagef(volume, sampler, pos + (int4)(1,0,0,0)).x;
f_100 = read_imagef(volume, sampler, pos - (int4)(1,0,0,0)).x;
if(dimensions > 1) {
f010 = read_imagef(volume, sampler, pos + (int4)(0,1,0,0)).x;
f0_10 = read_imagef(volume, sampler, pos - (int4)(0,1,0,0)).x;
}
if(dimensions > 2) {
f001 = read_imagef(volume, sampler, pos + (int4)(0,0,1,0)).x;
f00_1 = read_imagef(volume, sampler, pos - (int4)(0,0,1,0)).x;
}
break;
case 1:
f100 = read_imagef(volume, sampler, pos + (int4)(1,0,0,0)).y;
f_100 = read_imagef(volume, sampler, pos - (int4)(1,0,0,0)).y;
if(dimensions > 1) {
f010 = read_imagef(volume, sampler, pos + (int4)(0,1,0,0)).y;
f0_10 = read_imagef(volume, sampler, pos - (int4)(0,1,0,0)).y;
}
if(dimensions > 2) {
f001 = read_imagef(volume, sampler, pos + (int4)(0,0,1,0)).y;
f00_1 = read_imagef(volume, sampler, pos - (int4)(0,0,1,0)).y;
}
break;
case 2:
f100 = read_imagef(volume, sampler, pos + (int4)(1,0,0,0)).z;
f_100 = read_imagef(volume, sampler, pos - (int4)(1,0,0,0)).z;
if(dimensions > 1) {
f010 = read_imagef(volume, sampler, pos + (int4)(0,1,0,0)).z;
f0_10 = read_imagef(volume, sampler, pos - (int4)(0,1,0,0)).z;
}
if(dimensions > 2) {
f001 = read_imagef(volume, sampler, pos + (int4)(0,0,1,0)).z;
f00_1 = read_imagef(volume, sampler, pos - (int4)(0,0,1,0)).z;
}
break;
}
float3 grad = {
0.5f*(f100/read_imagef(volume, sampler, pos+(int4)(1,0,0,0)).w-f_100/read_imagef(volume, sampler, pos-(int4)(1,0,0,0)).w),
0.5f*(f010/read_imagef(volume, sampler, pos+(int4)(0,1,0,0)).w-f0_10/read_imagef(volume, sampler, pos-(int4)(0,1,0,0)).w),
0.5f*(f001/read_imagef(volume, sampler, pos+(int4)(0,0,1,0)).w-f00_1/read_imagef(volume, sampler, pos-(int4)(0,0,1,0)).w)
};
return grad;
}
float3 gradient(
__read_only image3d_t volume, // Volume to perform gradient on
int4 pos, // Position to perform gradient on
int volumeComponent, // The volume component to perform gradient on: 0, 1 or 2
int dimensions // The number of dimensions to perform gradient in: 1, 2 or 3
) {
float f100, f_100, f010, f0_10, f001, f00_1;
switch(volumeComponent) {
case 0:
f100 = read_imagef(volume, sampler, pos + (int4)(1,0,0,0)).x;
f_100 = read_imagef(volume, sampler, pos - (int4)(1,0,0,0)).x;
if(dimensions > 1) {
f010 = read_imagef(volume, sampler, pos + (int4)(0,1,0,0)).x;
f0_10 = read_imagef(volume, sampler, pos - (int4)(0,1,0,0)).x;
}
if(dimensions > 2) {
f001 = read_imagef(volume, sampler, pos + (int4)(0,0,1,0)).x;
f00_1 = read_imagef(volume, sampler, pos - (int4)(0,0,1,0)).x;
}
break;
case 1:
f100 = read_imagef(volume, sampler, pos + (int4)(1,0,0,0)).y;
f_100 = read_imagef(volume, sampler, pos - (int4)(1,0,0,0)).y;
if(dimensions > 1) {
f010 = read_imagef(volume, sampler, pos + (int4)(0,1,0,0)).y;
f0_10 = read_imagef(volume, sampler, pos - (int4)(0,1,0,0)).y;
}
if(dimensions > 2) {
f001 = read_imagef(volume, sampler, pos + (int4)(0,0,1,0)).y;
f00_1 = read_imagef(volume, sampler, pos - (int4)(0,0,1,0)).y;
}
break;
case 2:
f100 = read_imagef(volume, sampler, pos + (int4)(1,0,0,0)).z;
f_100 = read_imagef(volume, sampler, pos - (int4)(1,0,0,0)).z;
if(dimensions > 1) {
f010 = read_imagef(volume, sampler, pos + (int4)(0,1,0,0)).z;
f0_10 = read_imagef(volume, sampler, pos - (int4)(0,1,0,0)).z;
}
if(dimensions > 2) {
f001 = read_imagef(volume, sampler, pos + (int4)(0,0,1,0)).z;
f00_1 = read_imagef(volume, sampler, pos - (int4)(0,0,1,0)).z;
}
break;
}
float3 grad = {
0.5f*(f100-f_100),
0.5f*(f010-f0_10),
0.5f*(f001-f00_1)
};
return grad;
}
/* Morton Code Functions - Kudos to http://fgiesen.wordpress.com/2009/12/13/decoding-morton-codes/ */
// "Insert" two 0 bits after each of the 10 low bits of x
uint Part1By2(uint x) {
x &= 0x000003ff; // x = ---- ---- ---- ---- ---- --98 7654 3210
x = (x ^ (x << 16)) & 0xff0000ff; // x = ---- --98 ---- ---- ---- ---- 7654 3210
x = (x ^ (x << 8)) & 0x0300f00f; // x = ---- --98 ---- ---- 7654 ---- ---- 3210
x = (x ^ (x << 4)) & 0x030c30c3; // x = ---- --98 ---- 76-- --54 ---- 32-- --10
x = (x ^ (x << 2)) & 0x09249249; // x = ---- 9--8 --7- -6-- 5--4 --3- -2-- 1--0
return x;
}
uint EncodeMorton3(uint x, uint y, uint z) {
return (Part1By2(z) << 2) + (Part1By2(y) << 1) + Part1By2(x);
}
uint EncodeMorton(int4 v) {
return EncodeMorton3(v.x,v.y,v.z);
}
// Inverse of Part1By2 - "delete" all bits not at positions divisible by 3
uint Compact1By2(uint x) {
x &= 0x09249249; // x = ---- 9--8 --7- -6-- 5--4 --3- -2-- 1--0
x = (x ^ (x >> 2)) & 0x030c30c3; // x = ---- --98 ---- 76-- --54 ---- 32-- --10
x = (x ^ (x >> 4)) & 0x0300f00f; // x = ---- --98 ---- ---- 7654 ---- ---- 3210
x = (x ^ (x >> 8)) & 0xff0000ff; // x = ---- --98 ---- ---- ---- ---- 7654 3210
x = (x ^ (x >> 16)) & 0x000003ff; // x = ---- ---- ---- ---- ---- --98 7654 3210
return x;
}
uint DecodeMorton3X(uint code) {
return Compact1By2(code >> 0);
}
uint DecodeMorton3Y(uint code) {
return Compact1By2(code >> 1);
}
uint DecodeMorton3Z(uint code) {
return Compact1By2(code >> 2);
}
__kernel void constructHPLevelCharChar(
__global uchar * readHistoPyramid,
__global uchar * writeHistoPyramid,
__private int sizeX,
__private int sizeY,
__private int sizeZ
) {
uint3 size = {sizeX,sizeY,sizeZ};
uint writePos = EncodeMorton3(get_global_id(0), get_global_id(1), get_global_id(2));
int4 readPos = (int4)(get_global_id(0)*2, get_global_id(1)*2, get_global_id(2)*2,0);
uchar writeValue;
if(readPos.x >= size.x || readPos.y >= size.y || readPos.z >= size.z) {
writeValue = 0;
} else {
writeValue = readHistoPyramid[NLPOS(readPos)] +
readHistoPyramid[NLPOS(readPos+cubeOffsets[1])] +
readHistoPyramid[NLPOS(readPos+cubeOffsets[2])] +
readHistoPyramid[NLPOS(readPos+cubeOffsets[3])] +
readHistoPyramid[NLPOS(readPos+cubeOffsets[4])] +
readHistoPyramid[NLPOS(readPos+cubeOffsets[5])] +
readHistoPyramid[NLPOS(readPos+cubeOffsets[6])] +
readHistoPyramid[NLPOS(readPos+cubeOffsets[7])];
}
writeHistoPyramid[writePos] = writeValue;
}
__kernel void constructHPLevelCharShort(
__global uchar * readHistoPyramid,
__global ushort * writeHistoPyramid
) {
uint writePos = EncodeMorton3(get_global_id(0), get_global_id(1), get_global_id(2));
uint readPos = EncodeMorton3(get_global_id(0)*2, get_global_id(1)*2, get_global_id(2)*2);
ushort writeValue = readHistoPyramid[readPos] +
readHistoPyramid[readPos + 1] +
readHistoPyramid[readPos + 2] +
readHistoPyramid[readPos + 3] +
readHistoPyramid[readPos + 4] +
readHistoPyramid[readPos + 5] +
readHistoPyramid[readPos + 6] +
readHistoPyramid[readPos + 7];
writeHistoPyramid[writePos] = writeValue;
}
__kernel void constructHPLevelShortShort(
__global ushort * readHistoPyramid,
__global ushort * writeHistoPyramid
) {
uint writePos = EncodeMorton3(get_global_id(0), get_global_id(1), get_global_id(2));
uint readPos = EncodeMorton3(get_global_id(0)*2, get_global_id(1)*2, get_global_id(2)*2);
ushort writeValue = readHistoPyramid[readPos] +
readHistoPyramid[readPos + 1] +
readHistoPyramid[readPos + 2] +
readHistoPyramid[readPos + 3] +
readHistoPyramid[readPos + 4] +
readHistoPyramid[readPos + 5] +
readHistoPyramid[readPos + 6] +
readHistoPyramid[readPos + 7];
writeHistoPyramid[writePos] = writeValue;
}
__kernel void constructHPLevelShortInt(
__global ushort * readHistoPyramid,
__global int * writeHistoPyramid
) {
uint writePos = EncodeMorton3(get_global_id(0), get_global_id(1), get_global_id(2));
uint readPos = EncodeMorton3(get_global_id(0)*2, get_global_id(1)*2, get_global_id(2)*2);
int writeValue = readHistoPyramid[readPos] +
readHistoPyramid[readPos + 1] +
readHistoPyramid[readPos + 2] +
readHistoPyramid[readPos + 3] +
readHistoPyramid[readPos + 4] +
readHistoPyramid[readPos + 5] +
readHistoPyramid[readPos + 6] +
readHistoPyramid[readPos + 7];
writeHistoPyramid[writePos] = writeValue;
}
__kernel void constructHPLevelBuffer(
__global int * readHistoPyramid,
__global int * writeHistoPyramid
) {
uint writePos = EncodeMorton3(get_global_id(0), get_global_id(1), get_global_id(2));
uint readPos = EncodeMorton3(get_global_id(0)*2, get_global_id(1)*2, get_global_id(2)*2);
int writeValue = readHistoPyramid[readPos] +
readHistoPyramid[readPos + 1] +
readHistoPyramid[readPos + 2] +
readHistoPyramid[readPos + 3] +
readHistoPyramid[readPos + 4] +
readHistoPyramid[readPos + 5] +
readHistoPyramid[readPos + 6] +
readHistoPyramid[readPos + 7];
writeHistoPyramid[writePos] = writeValue;
}
__kernel void constructHPLevel2D(
__read_only image2d_t readHistoPyramid,
__write_only image2d_t writeHistoPyramid
) {
int2 writePos = {get_global_id(0), get_global_id(1)};
int2 readPos = writePos*2;
uint writeValue =
read_imageui(readHistoPyramid, hpSampler, readPos).x +
read_imageui(readHistoPyramid, hpSampler, readPos+(int2)(1,0)).x +
read_imageui(readHistoPyramid, hpSampler, readPos+(int2)(0,1)).x +
read_imageui(readHistoPyramid, hpSampler, readPos+(int2)(1,1)).x;
write_imageui(writeHistoPyramid, writePos, (uint4)(writeValue,0,0,0));
}
int3 scanHPLevel2D(int target, __read_only image2d_t hp, int3 current) {
int4 neighbors = {
read_imagei(hp, hpSampler, current.xy).x,
read_imagei(hp, hpSampler, current.xy + (int2)(0,1)).x,
read_imagei(hp, hpSampler, current.xy + (int2)(1,0)).x,
0
};
int acc = current.z + neighbors.s0;
int4 cmp;
cmp.s0 = acc <= target;
acc += neighbors.s1;
cmp.s1 = acc <= target;
acc += neighbors.s2;
cmp.s2 = acc <= target;
current += cubeOffsets2D[(cmp.s0+cmp.s1+cmp.s2)].xyz;
current.x = current.x*2;
current.y = current.y*2;
current.z = current.z +
cmp.s0*neighbors.s0 +
cmp.s1*neighbors.s1 +
cmp.s2*neighbors.s2;
return current;
}
int4 scanHPLevelShort(int target, __global ushort * hp, int4 current) {
int8 neighbors = {
hp[EncodeMorton(current)],
hp[EncodeMorton(current + cubeOffsets[1])],
hp[EncodeMorton(current + cubeOffsets[2])],
hp[EncodeMorton(current + cubeOffsets[3])],
hp[EncodeMorton(current + cubeOffsets[4])],
hp[EncodeMorton(current + cubeOffsets[5])],
hp[EncodeMorton(current + cubeOffsets[6])],
hp[EncodeMorton(current + cubeOffsets[7])],
};
int acc = current.s3 + neighbors.s0;
int8 cmp;
cmp.s0 = acc <= target;
acc += neighbors.s1;
cmp.s1 = acc <= target;
acc += neighbors.s2;
cmp.s2 = acc <= target;
acc += neighbors.s3;
cmp.s3 = acc <= target;
acc += neighbors.s4;
cmp.s4 = acc <= target;
acc += neighbors.s5;
cmp.s5 = acc <= target;
acc += neighbors.s6;
cmp.s6 = acc <= target;
cmp.s7 = 0;
current += cubeOffsets[(cmp.s0+cmp.s1+cmp.s2+cmp.s3+cmp.s4+cmp.s5+cmp.s6+cmp.s7)];
current.s0 = current.s0*2;
current.s1 = current.s1*2;
current.s2 = current.s2*2;
current.s3 = current.s3 +
cmp.s0*neighbors.s0 +
cmp.s1*neighbors.s1 +
cmp.s2*neighbors.s2 +
cmp.s3*neighbors.s3 +
cmp.s4*neighbors.s4 +
cmp.s5*neighbors.s5 +
cmp.s6*neighbors.s6 +
cmp.s7*neighbors.s7;
return current;
}
int4 scanHPLevelChar(int target, __global uchar * hp, int4 current) {
int8 neighbors = {
hp[EncodeMorton(current)],
hp[EncodeMorton(current + cubeOffsets[1])],
hp[EncodeMorton(current + cubeOffsets[2])],
hp[EncodeMorton(current + cubeOffsets[3])],
hp[EncodeMorton(current + cubeOffsets[4])],
hp[EncodeMorton(current + cubeOffsets[5])],
hp[EncodeMorton(current + cubeOffsets[6])],
hp[EncodeMorton(current + cubeOffsets[7])],
};
int acc = current.s3 + neighbors.s0;
int8 cmp;
cmp.s0 = acc <= target;
acc += neighbors.s1;
cmp.s1 = acc <= target;
acc += neighbors.s2;
cmp.s2 = acc <= target;
acc += neighbors.s3;
cmp.s3 = acc <= target;
acc += neighbors.s4;
cmp.s4 = acc <= target;
acc += neighbors.s5;
cmp.s5 = acc <= target;
acc += neighbors.s6;
cmp.s6 = acc <= target;
cmp.s7 = 0;
current += cubeOffsets[(cmp.s0+cmp.s1+cmp.s2+cmp.s3+cmp.s4+cmp.s5+cmp.s6+cmp.s7)];
current.s0 = current.s0*2;
current.s1 = current.s1*2;
current.s2 = current.s2*2;
current.s3 = current.s3 +
cmp.s0*neighbors.s0 +
cmp.s1*neighbors.s1 +
cmp.s2*neighbors.s2 +
cmp.s3*neighbors.s3 +
cmp.s4*neighbors.s4 +
cmp.s5*neighbors.s5 +
cmp.s6*neighbors.s6 +
cmp.s7*neighbors.s7;
return current;
}
int4 scanHPLevelCharNoMorton(int target, __global uchar * hp, int4 current, uint3 size) {
int8 neighbors = {
hp[NLPOS(current)],
hp[NLPOS(current + cubeOffsets[1])],
hp[NLPOS(current + cubeOffsets[2])],
hp[NLPOS(current + cubeOffsets[3])],
hp[NLPOS(current + cubeOffsets[4])],
hp[NLPOS(current + cubeOffsets[5])],
hp[NLPOS(current + cubeOffsets[6])],
hp[NLPOS(current + cubeOffsets[7])],
};
int acc = current.s3 + neighbors.s0;
int8 cmp;
cmp.s0 = acc <= target;
acc += neighbors.s1;
cmp.s1 = acc <= target;
acc += neighbors.s2;
cmp.s2 = acc <= target;
acc += neighbors.s3;
cmp.s3 = acc <= target;
acc += neighbors.s4;
cmp.s4 = acc <= target;
acc += neighbors.s5;
cmp.s5 = acc <= target;
acc += neighbors.s6;
cmp.s6 = acc <= target;
cmp.s7 = 0;
current += cubeOffsets[(cmp.s0+cmp.s1+cmp.s2+cmp.s3+cmp.s4+cmp.s5+cmp.s6+cmp.s7)];
current.s0 = current.s0*2;
current.s1 = current.s1*2;
current.s2 = current.s2*2;
current.s3 = current.s3 +
cmp.s0*neighbors.s0 +
cmp.s1*neighbors.s1 +
cmp.s2*neighbors.s2 +
cmp.s3*neighbors.s3 +
cmp.s4*neighbors.s4 +
cmp.s5*neighbors.s5 +
cmp.s6*neighbors.s6 +
cmp.s7*neighbors.s7;
return current;
}
int4 scanHPLevel(int target, __global int * hp, int4 current) {
int8 neighbors = {
hp[EncodeMorton(current)],
hp[EncodeMorton(current + cubeOffsets[1])],
hp[EncodeMorton(current + cubeOffsets[2])],
hp[EncodeMorton(current + cubeOffsets[3])],
hp[EncodeMorton(current + cubeOffsets[4])],
hp[EncodeMorton(current + cubeOffsets[5])],
hp[EncodeMorton(current + cubeOffsets[6])],
hp[EncodeMorton(current + cubeOffsets[7])],
};
int acc = current.s3 + neighbors.s0;
int8 cmp;
cmp.s0 = acc <= target;
acc += neighbors.s1;
cmp.s1 = acc <= target;
acc += neighbors.s2;
cmp.s2 = acc <= target;
acc += neighbors.s3;
cmp.s3 = acc <= target;
acc += neighbors.s4;
cmp.s4 = acc <= target;
acc += neighbors.s5;
cmp.s5 = acc <= target;
acc += neighbors.s6;
cmp.s6 = acc <= target;
cmp.s7 = 0;
current += cubeOffsets[(cmp.s0+cmp.s1+cmp.s2+cmp.s3+cmp.s4+cmp.s5+cmp.s6+cmp.s7)];
current.s0 = current.s0*2;
current.s1 = current.s1*2;
current.s2 = current.s2*2;
current.s3 = current.s3 +
cmp.s0*neighbors.s0 +
cmp.s1*neighbors.s1 +
cmp.s2*neighbors.s2 +
cmp.s3*neighbors.s3 +
cmp.s4*neighbors.s4 +
cmp.s5*neighbors.s5 +
cmp.s6*neighbors.s6 +
cmp.s7*neighbors.s7;
return current;
}
int4 traverseHP3DBuffer(
uint3 size,
int target,
int HP_SIZE,
__global uchar * hp0,
__global uchar * hp1,
__global ushort * hp2,
__global ushort * hp3,
__global ushort * hp4,
__global int * hp5,
__global int * hp6,
__global int * hp7,
__global int * hp8,
__global int * hp9
) {
int4 position = {0,0,0,0}; // x,y,z,sum
if(HP_SIZE > 512)
position = scanHPLevel(target, hp9, position);
if(HP_SIZE > 256)
position = scanHPLevel(target, hp8, position);
if(HP_SIZE > 128)
position = scanHPLevel(target, hp7, position);
if(HP_SIZE > 64)
position = scanHPLevel(target, hp6, position);
if(HP_SIZE > 32)
position = scanHPLevel(target, hp5, position);
if(HP_SIZE > 16)
position = scanHPLevelShort(target, hp4, position);
if(HP_SIZE > 8)
position = scanHPLevelShort(target, hp3, position);
position = scanHPLevelShort(target, hp2, position);
position = scanHPLevelChar(target, hp1, position);
position = scanHPLevelCharNoMorton(target, hp0, position,size);
position.x = position.x / 2;
position.y = position.y / 2;
position.z = position.z / 2;
return position;
}
int2 traverseHP2D(
int target,
int HP_SIZE,
image2d_t hp0,
image2d_t hp1,
image2d_t hp2,
image2d_t hp3,
image2d_t hp4,
image2d_t hp5,
image2d_t hp6,
image2d_t hp7,
image2d_t hp8,
image2d_t hp9,
image2d_t hp10,
image2d_t hp11,
image2d_t hp12,
image2d_t hp13
) {
int3 position = {0,0,0};
if(HP_SIZE > 8192)
position = scanHPLevel2D(target, hp13, position);
if(HP_SIZE > 4096)
position = scanHPLevel2D(target, hp12, position);
if(HP_SIZE > 2048)
position = scanHPLevel2D(target, hp11, position);
if(HP_SIZE > 1024)
position = scanHPLevel2D(target, hp10, position);
if(HP_SIZE > 512)
position = scanHPLevel2D(target, hp9, position);
if(HP_SIZE > 256)
position = scanHPLevel2D(target, hp8, position);
if(HP_SIZE > 128)
position = scanHPLevel2D(target, hp7, position);
if(HP_SIZE > 64)
position = scanHPLevel2D(target, hp6, position);
if(HP_SIZE > 32)
position = scanHPLevel2D(target, hp5, position);
if(HP_SIZE > 16)
position = scanHPLevel2D(target, hp4, position);
if(HP_SIZE > 8)
position = scanHPLevel2D(target, hp3, position);
position = scanHPLevel2D(target, hp2, position);
position = scanHPLevel2D(target, hp1, position);
position = scanHPLevel2D(target, hp0, position);
position.x = position.x / 2;
position.y = position.y / 2;
return position.xy;
}
__kernel void createPositions3DBuffer(
__private int sizeX,
__private int sizeY,
__private int sizeZ,
__global int * positions,
__private int HP_SIZE,
__private int sum,
__global uchar * hp0, // Largest HP
__global uchar * hp1,
__global ushort * hp2,
__global ushort * hp3,
__global ushort * hp4,
__global int * hp5,
__global int * hp6,
__global int * hp7,
__global int * hp8,
__global int * hp9
) {
int target = get_global_id(0);
if(target >= sum)
target = 0;
uint3 size = {sizeX,sizeY,sizeZ};
int4 pos = traverseHP3DBuffer(size,target,HP_SIZE,hp0,hp1,hp2,hp3,hp4,hp5,hp6,hp7,hp8,hp9);
vstore3(pos.xyz, target, positions);
}
__kernel void createPositions2D(
__global int * positions,
__private int HP_SIZE,
__private int sum,
__read_only image2d_t hp0, // Largest HP
__read_only image2d_t hp1,
__read_only image2d_t hp2,
__read_only image2d_t hp3,
__read_only image2d_t hp4,
__read_only image2d_t hp5
,__read_only image2d_t hp6
,__read_only image2d_t hp7
,__read_only image2d_t hp8
,__read_only image2d_t hp9
,__read_only image2d_t hp10
,__read_only image2d_t hp11
,__read_only image2d_t hp12
,__read_only image2d_t hp13
) {
int target = get_global_id(0);
if(target >= sum)
target = 0;
int2 pos = traverseHP2D(target,HP_SIZE,hp0,hp1,hp2,hp3,hp4,hp5,hp6,hp7,hp8,hp9,hp10,hp11,hp12,hp13);
vstore2(pos, target, positions);
}
__kernel void linkLengths(
__global int const * restrict positions,
__write_only image2d_t lengths
) {
const float3 xa = convert_float3(vload3(get_global_id(0), positions));
const float3 xb = convert_float3(vload3(get_global_id(1), positions));
write_imagef(lengths, (int2)(get_global_id(0), get_global_id(1)), (float4)(distance(xa,xb),0.0f,0.0f,0.0f));
}
__kernel void compact(
__read_only image2d_t lengths,
volatile __global int * incs,
__write_only image2d_t compacted_lengths,
__private float maxDistance
) {
const int i = get_global_id(0);
const int j = get_global_id(1);
float length = read_imagef(lengths, sampler, (int2)(i,j)).x;
if(length < maxDistance && length > 0.0f) {
volatile int nr = atomic_inc(&(incs[i]));
write_imagef(compacted_lengths, (int2)(i,nr), (float4)(length, j, 0, 0));
}
}
__kernel void linkCenterpoints(
__read_only image3d_t TDF,
__global int const * restrict positions,
__write_only image2d_t edges,
__read_only image2d_t compacted_lengths,
__private int sum,
__private float minAvgTDF,
__private float maxDistance
) {
int id = get_global_id(0);
if(id >= sum)
id = 0;
float3 xa = convert_float3(vload3(id, positions));
//printf("%f %f %f\n",xa.x,xa.y,xa.z);
int2 bestPair;
float shortestDistance = maxDistance*2;
bool validPairFound = false;
for(int i = 0; i < sum; i++) {
float2 cl = read_imagef(compacted_lengths, sampler, (int2)(id,i)).xy;
// reached the end?
if(cl.x == 0.0f)
break;
float3 xb = convert_float3(vload3(cl.y, positions));
int db = round(cl.x);
if(db >= shortestDistance)
continue;
for(int j = 0; j < i; j++) {
float2 cl2 = read_imagef(compacted_lengths, sampler, (int2)(id,j)).xy;
if(cl2.y == cl.y)
continue;
// reached the end?
if(cl2.x == 0.0f)
break;
float3 xc = convert_float3(vload3(cl2.y, positions));
// Check distance between xa and xb
int dc = round(cl2.x);
float minTDF = 0.0f;
float maxVarTDF = 1.005f;
float maxIntensity = 1.3f;
float maxAvgIntensity = 1.2f;
float maxVarIntensity = 1.005f;
if(db+dc < shortestDistance) {
// Check angle
float3 ab = (xb-xa);
float3 ac = (xc-xa);
float angle = acos(dot(normalize(ab), normalize(ac)));
if(angle < 2.0f) // 120 degrees
continue;
// Check TDF
float avgTDF = 0.0f;
float avgIntensity = 0.0f;
for(int k = 0; k <= db; k++) {
float alpha = (float)k/db;
float3 p = xa+ab*alpha;
float t = read_imagef(TDF, interpolationSampler, p.xyzz).x;
avgTDF += t;
}
avgTDF /= db+1;
if(avgTDF < minAvgTDF)
continue;
/*
float varTDF = 0.0f;
for(int k = 0; k <= db; k++) {
float alpha = (float)k/db;
float3 p = xa+ab*alpha;
float t = read_imagef(TDF, interpolationSampler, p.xyzz).x;
float i = read_imagef(intensity, interpolationSampler, p.xyzz).x;
varIntensity += (i-avgIntensity)*(i-avgIntensity);
varTDF += (t-avgTDF)*(t-avgTDF);
if(i > maxIntensity || t < minTDF) {
invalid = true;
break;
}
}
if(invalid)
continue;
if(db > 4 && varTDF / (db+1) > maxVarTDF)
continue;
*/
avgTDF = 0.0f;
for(int k = 0; k <= dc; k++) {
float alpha = (float)k/dc;
float3 p = xa+ac*alpha;
float t = read_imagef(TDF, interpolationSampler, p.xyzz).x;
avgTDF += t;
}
avgTDF /= dc+1;
if(avgTDF < minAvgTDF)
continue;
/*
for(int k = 0; k <= dc; k++) {
float alpha = (float)k/dc;
float3 p = xa+ac*alpha;
float t = read_imagef(TDF, interpolationSampler, p.xyzz).x;
varTDF += (t-avgTDF)*(t-avgTDF);
}
if(dc > 4 && varIntensity / (dc+1) > maxVarIntensity)
continue;
if(dc > 4 && varTDF / (dc+1) > maxVarTDF)
continue;
*/
validPairFound = true;
bestPair.x = cl.y;
bestPair.y = cl2.y;
shortestDistance = db+dc;
}
}}
if(validPairFound) {
// Store edges
int2 edge = {id, bestPair.x};
int2 edge2 = {id, bestPair.y};
write_imageui(edges, edge, (uint4)(1,0,0,0));
write_imageui(edges, edge2, (uint4)(1,0,0,0));
}
}
__kernel void graphComponentLabeling(
__global int const * restrict edges,
volatile __global int * C,
__global int * m,
__private int sum
) {
int id = get_global_id(0);
if(id >= sum)
id = 0;
int2 edge = vload2(id, edges);
const int ca = C[edge.x];
const int cb = C[edge.y];
// Find the smallest C value and store in C in the others
if(ca == cb) {
return;
} else {
if(ca < cb) {
// ca is smallest
atomic_min(&C[edge.y], ca);
} else {
// cb is smallest
atomic_min(&C[edge.x], cb);
}
m[0] = 1; // register change
}
}
__kernel void calculateTreeLength(
__global int const * restrict C,
volatile __global int * S
) {
const int id = get_global_id(0);
atomic_inc(&S[C[id]]);
}
__kernel void removeSmallTrees(
__global int const * restrict edges,
__global int const * restrict vertices,
__global int const * restrict C,
__global int const * restrict S,
__private int minTreeLength,
__global char * centerlines,
__private int width,
__private int height
) {
// Find the edges that are part of the large trees
const int id = get_global_id(0);
int2 edge = vload2(id, edges);
const int ca = C[edge.x];
if(S[ca] >= minTreeLength) {
const float3 xa = convert_float3(vload3(edge.x, vertices));
const float3 xb = convert_float3(vload3(edge.y, vertices));
int l = round(length(xb-xa));
for(int i = 0; i < l; i++) {
const float alpha = (float)i/l;
//write_imagei(centerlines, convert_int3(round(xa+(xb-xa)*alpha)).xyzz, 1);
const int3 pos = convert_int3(round(xa+(xb-xa)*alpha));
centerlines[pos.x+pos.y*width+pos.z*width*height] = 1;
}
}
}
__kernel void removeDuplicateEdges(
__read_only image2d_t edgeTuples,
__write_only image2d_t edgeTuples2
) {
const int2 pos = {get_global_id(0), get_global_id(1)};
if(read_imageui(edgeTuples, sampler, pos).x == 0) {
write_imageui(edgeTuples2,pos, (uint4)(0,0,0,0));
} else if(pos.x > pos.y) {
// Check if opposite is an edge
if(read_imageui(edgeTuples, sampler, (int2)(pos.y,pos.x)).x == 1) {
// opposite exists => remove this one
write_imageui(edgeTuples2,pos, (uint4)(0,0,0,0));
} else {
// opposite doesn't exist => save this one
write_imageui(edgeTuples2,pos, (uint4)(1,0,0,0));
}
} else {
write_imageui(edgeTuples2,pos, (uint4)(1,0,0,0));
}
}
#define SQR_MAG(pos) read_imagef(vectorField, sampler, pos).w
__kernel void dd(
__read_only image3d_t TDF,
__read_only image3d_t centerpointCandidates,
__global uchar * centerpoints,
__private int cubeSize
) {
int4 bestPos;
float bestTDF = 0.0f;
int4 readPos = {
get_global_id(0)*cubeSize,
get_global_id(1)*cubeSize,
get_global_id(2)*cubeSize,
0
};
bool found = false;
for(int a = 0; a < cubeSize; a++) {
for(int b = 0; b < cubeSize; b++) {
for(int c = 0; c < cubeSize; c++) {
int4 pos = readPos + (int4)(a,b,c,0);
if(read_imagei(centerpointCandidates, sampler, pos).x == 1) {
float tdf = read_imagef(TDF, sampler, pos).x;
if(tdf > bestTDF) {
found = true;
bestTDF = tdf;
bestPos = pos;
}
}
}}}
if(found) {
centerpoints[bestPos.x+bestPos.y*get_image_width(TDF)+bestPos.z*get_image_width(TDF)*get_image_height(TDF)] = 1;