-
Notifications
You must be signed in to change notification settings - Fork 0
/
cudaSiftD.cu
656 lines (614 loc) · 22.9 KB
/
cudaSiftD.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
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
//********************************************************//
// CUDA SIFT extractor by Marten Bjorkman aka Celebrandil //
//********************************************************//
#include "cudautils.h"
#include "cudaSiftD.h"
#include "cudaSift.h"
///////////////////////////////////////////////////////////////////////////////
// Kernel configuration
///////////////////////////////////////////////////////////////////////////////
__constant__ float d_Threshold[2];
__constant__ float d_Scales[8], d_Factor;
__constant__ float d_EdgeLimit;
__constant__ int d_MaxNumPoints;
__device__ unsigned int d_PointCounter[1];
__constant__ float d_Kernel1[5];
__constant__ float d_Kernel2[12*16];
///////////////////////////////////////////////////////////////////////////////
// Lowpass filter and subsample image
///////////////////////////////////////////////////////////////////////////////
__global__ void ScaleDown(float *d_Result, float *d_Data, int width, int pitch, int height, int newpitch)
{
__shared__ float inrow[SCALEDOWN_W+4];
__shared__ float brow[5*(SCALEDOWN_W/2)];
__shared__ int yRead[SCALEDOWN_H+4];
__shared__ int yWrite[SCALEDOWN_H+4];
#define dx2 (SCALEDOWN_W/2)
const int tx = threadIdx.x;
const int tx0 = tx + 0*dx2;
const int tx1 = tx + 1*dx2;
const int tx2 = tx + 2*dx2;
const int tx3 = tx + 3*dx2;
const int tx4 = tx + 4*dx2;
const int xStart = blockIdx.x*SCALEDOWN_W;
const int yStart = blockIdx.y*SCALEDOWN_H;
const int xWrite = xStart/2 + tx;
const float *k = d_Kernel1;
if (tx<SCALEDOWN_H+4) {
int y = yStart + tx - 1;
y = (y<0 ? 0 : y);
y = (y>=height ? height-1 : y);
yRead[tx] = y*pitch;
yWrite[tx] = (yStart + tx - 4)/2 * newpitch;
}
__syncthreads();
int xRead = xStart + tx - 2;
xRead = (xRead<0 ? 0 : xRead);
xRead = (xRead>=width ? width-1 : xRead);
for (int dy=0;dy<SCALEDOWN_H+4;dy+=5) {
inrow[tx] = d_Data[yRead[dy+0] + xRead];
__syncthreads();
if (tx<dx2)
brow[tx0] = k[0]*(inrow[2*tx]+inrow[2*tx+4]) + k[1]*(inrow[2*tx+1]+inrow[2*tx+3]) + k[2]*inrow[2*tx+2];
__syncthreads();
if (tx<dx2 && dy>=4 && !(dy&1))
d_Result[yWrite[dy+0] + xWrite] = k[2]*brow[tx2] + k[0]*(brow[tx0]+brow[tx4]) + k[1]*(brow[tx1]+brow[tx3]);
if (dy<(SCALEDOWN_H+3)) {
inrow[tx] = d_Data[yRead[dy+1] + xRead];
__syncthreads();
if (tx<dx2)
brow[tx1] = k[0]*(inrow[2*tx]+inrow[2*tx+4]) + k[1]*(inrow[2*tx+1]+inrow[2*tx+3]) + k[2]*inrow[2*tx+2];
__syncthreads();
if (tx<dx2 && dy>=3 && (dy&1))
d_Result[yWrite[dy+1] + xWrite] = k[2]*brow[tx3] + k[0]*(brow[tx1]+brow[tx0]) + k[1]*(brow[tx2]+brow[tx4]);
}
if (dy<(SCALEDOWN_H+2)) {
inrow[tx] = d_Data[yRead[dy+2] + xRead];
__syncthreads();
if (tx<dx2)
brow[tx2] = k[0]*(inrow[2*tx]+inrow[2*tx+4]) + k[1]*(inrow[2*tx+1]+inrow[2*tx+3]) + k[2]*inrow[2*tx+2];
__syncthreads();
if (tx<dx2 && dy>=2 && !(dy&1))
d_Result[yWrite[dy+2] + xWrite] = k[2]*brow[tx4] + k[0]*(brow[tx2]+brow[tx1]) + k[1]*(brow[tx3]+brow[tx0]);
}
if (dy<(SCALEDOWN_H+1)) {
inrow[tx] = d_Data[yRead[dy+3] + xRead];
__syncthreads();
if (tx<dx2)
brow[tx3] = k[0]*(inrow[2*tx]+inrow[2*tx+4]) + k[1]*(inrow[2*tx+1]+inrow[2*tx+3]) + k[2]*inrow[2*tx+2];
__syncthreads();
if (tx<dx2 && dy>=1 && (dy&1))
d_Result[yWrite[dy+3] + xWrite] = k[2]*brow[tx0] + k[0]*(brow[tx3]+brow[tx2]) + k[1]*(brow[tx4]+brow[tx1]);
}
if (dy<SCALEDOWN_H) {
inrow[tx] = d_Data[yRead[dy+4] + xRead];
__syncthreads();
if (tx<dx2)
brow[tx4] = k[0]*(inrow[2*tx]+inrow[2*tx+4]) + k[1]*(inrow[2*tx+1]+inrow[2*tx+3]) + k[2]*inrow[2*tx+2];
__syncthreads();
if (tx<dx2 && !(dy&1))
d_Result[yWrite[dy+4] + xWrite] = k[2]*brow[tx1] + k[0]*(brow[tx4]+brow[tx3]) + k[1]*(brow[tx0]+brow[tx2]);
}
__syncthreads();
}
}
__global__ void ScaleUp(float *d_Result, float *d_Data, int width, int pitch, int height, int newpitch)
{
#define BW (SCALEUP_W/2 + 2)
#define BH (SCALEUP_H/2 + 2)
__shared__ float buffer[BW*BH];
const int tx = threadIdx.x;
const int ty = threadIdx.y;
if (tx<BW && ty<BH) {
int x = min(max(blockIdx.x*(SCALEUP_W/2) + tx - 1, 0), width-1);
int y = min(max(blockIdx.y*(SCALEUP_H/2) + ty - 1, 0), height-1);
buffer[ty*BW + tx] = d_Data[y*pitch + x];
}
__syncthreads();
int x = blockIdx.x*SCALEUP_W + tx;
int y = blockIdx.y*SCALEUP_H + ty;
if (x<2*width && y<2*height) {
int bx = (tx + 1)/2;
int by = (ty + 1)/2;
int bp = by*BW + bx;
float wx = 0.25f + (tx&1)*0.50f;
float wy = 0.25f + (ty&1)*0.50f;
d_Result[y*newpitch + x] = wy*(wx*buffer[bp] + (1.0f-wx)*buffer[bp+1]) +
(1.0f-wy)*(wx*buffer[bp+BW] + (1.0f-wx)*buffer[bp+BW+1]);
}
}
__global__ void ExtractSiftDescriptors(cudaTextureObject_t texObj, SiftPoint *d_sift, int fstPts, float subsampling)
{
__shared__ float gauss[16];
__shared__ float buffer[128];
__shared__ float sums[4];
const int tx = threadIdx.x; // 0 -> 16
const int ty = threadIdx.y; // 0 -> 8
const int idx = ty*16 + tx;
const int bx = blockIdx.x + fstPts; // 0 -> numPts
if (ty==0)
gauss[tx] = exp(-(tx-7.5f)*(tx-7.5f)/128.0f);
buffer[idx] = 0.0f;
__syncthreads();
// Compute angles and gradients
float theta = 2.0f*3.1415f/360.0f*d_sift[bx].orientation;
float sina = sinf(theta); // cosa -sina
float cosa = cosf(theta); // sina cosa
float scale = 12.0f/16.0f*d_sift[bx].scale;
float ssina = scale*sina;
float scosa = scale*cosa;
for (int y=ty;y<16;y+=8) {
float xpos = d_sift[bx].xpos + (tx-7.5f)*scosa - (y-7.5f)*ssina;
float ypos = d_sift[bx].ypos + (tx-7.5f)*ssina + (y-7.5f)*scosa;
float dx = tex2D<float>(texObj, xpos+cosa, ypos+sina) -
tex2D<float>(texObj, xpos-cosa, ypos-sina);
float dy = tex2D<float>(texObj, xpos-sina, ypos+cosa) -
tex2D<float>(texObj, xpos+sina, ypos-cosa);
float grad = gauss[y]*gauss[tx] * sqrtf(dx*dx + dy*dy);
float angf = 4.0f/3.1415f*atan2f(dy, dx) + 4.0f;
int hori = (tx + 2)/4 - 1; // Convert from (tx,y,angle) to bins
float horf = (tx - 1.5f)/4.0f - hori;
float ihorf = 1.0f - horf;
int veri = (y + 2)/4 - 1;
float verf = (y - 1.5f)/4.0f - veri;
float iverf = 1.0f - verf;
int angi = angf;
int angp = (angi<7 ? angi+1 : 0);
angf -= angi;
float iangf = 1.0f - angf;
int hist = 8*(4*veri + hori); // Each gradient measure is interpolated
int p1 = angi + hist; // in angles, xpos and ypos -> 8 stores
int p2 = angp + hist;
if (tx>=2) {
float grad1 = ihorf*grad;
if (y>=2) { // Upper left
float grad2 = iverf*grad1;
atomicAdd(buffer + p1, iangf*grad2);
atomicAdd(buffer + p2, angf*grad2);
}
if (y<=13) { // Lower left
float grad2 = verf*grad1;
atomicAdd(buffer + p1+32, iangf*grad2);
atomicAdd(buffer + p2+32, angf*grad2);
}
}
if (tx<=13) {
float grad1 = horf*grad;
if (y>=2) { // Upper right
float grad2 = iverf*grad1;
atomicAdd(buffer + p1+8, iangf*grad2);
atomicAdd(buffer + p2+8, angf*grad2);
}
if (y<=13) { // Lower right
float grad2 = verf*grad1;
atomicAdd(buffer + p1+40, iangf*grad2);
atomicAdd(buffer + p2+40, angf*grad2);
}
}
}
__syncthreads();
// Normalize twice and suppress peaks first time
float sum = buffer[idx]*buffer[idx];
for (int i=1;i<=16;i*=2)
sum += __shfl_xor(sum, i);
if ((idx&31)==0)
sums[idx/32] = sum;
__syncthreads();
float tsum1 = sums[0] + sums[1] + sums[2] + sums[3];
tsum1 = min(buffer[idx] * rsqrtf(tsum1), 0.2f);
sum = tsum1*tsum1;
for (int i=1;i<=16;i*=2)
sum += __shfl_xor(sum, i);
if ((idx&31)==0)
sums[idx/32] = sum;
__syncthreads();
float tsum2 = sums[0] + sums[1] + sums[2] + sums[3];
float *desc = d_sift[bx].data;
desc[idx] = tsum1 * rsqrtf(tsum2);
if (idx==0) {
d_sift[bx].xpos *= subsampling;
d_sift[bx].ypos *= subsampling;
d_sift[bx].scale *= subsampling;
}
}
__global__ void ExtractSiftDescriptorsOld(cudaTextureObject_t texObj, SiftPoint *d_sift, int fstPts, float subsampling)
{
__shared__ float gauss[16];
__shared__ float buffer[128];
__shared__ float sums[128];
const int tx = threadIdx.x; // 0 -> 16
const int ty = threadIdx.y; // 0 -> 8
const int idx = ty*16 + tx;
const int bx = blockIdx.x + fstPts; // 0 -> numPts
if (ty==0)
gauss[tx] = exp(-(tx-7.5f)*(tx-7.5f)/128.0f);
buffer[idx] = 0.0f;
__syncthreads();
// Compute angles and gradients
float theta = 2.0f*3.1415f/360.0f*d_sift[bx].orientation;
float sina = sinf(theta); // cosa -sina
float cosa = cosf(theta); // sina cosa
float scale = 12.0f/16.0f*d_sift[bx].scale;
float ssina = scale*sina;
float scosa = scale*cosa;
for (int y=ty;y<16;y+=8) {
float xpos = d_sift[bx].xpos + (tx-7.5f)*scosa - (y-7.5f)*ssina;
float ypos = d_sift[bx].ypos + (tx-7.5f)*ssina + (y-7.5f)*scosa;
float dx = tex2D<float>(texObj, xpos+cosa, ypos+sina) -
tex2D<float>(texObj, xpos-cosa, ypos-sina);
float dy = tex2D<float>(texObj, xpos-sina, ypos+cosa) -
tex2D<float>(texObj, xpos+sina, ypos-cosa);
float grad = gauss[y]*gauss[tx] * sqrtf(dx*dx + dy*dy);
float angf = 4.0f/3.1415f*atan2f(dy, dx) + 4.0f;
int hori = (tx + 2)/4 - 1; // Convert from (tx,y,angle) to bins
float horf = (tx - 1.5f)/4.0f - hori;
float ihorf = 1.0f - horf;
int veri = (y + 2)/4 - 1;
float verf = (y - 1.5f)/4.0f - veri;
float iverf = 1.0f - verf;
int angi = angf;
int angp = (angi<7 ? angi+1 : 0);
angf -= angi;
float iangf = 1.0f - angf;
int hist = 8*(4*veri + hori); // Each gradient measure is interpolated
int p1 = angi + hist; // in angles, xpos and ypos -> 8 stores
int p2 = angp + hist;
if (tx>=2) {
float grad1 = ihorf*grad;
if (y>=2) { // Upper left
float grad2 = iverf*grad1;
atomicAdd(buffer + p1, iangf*grad2);
atomicAdd(buffer + p2, angf*grad2);
}
if (y<=13) { // Lower left
float grad2 = verf*grad1;
atomicAdd(buffer + p1+32, iangf*grad2);
atomicAdd(buffer + p2+32, angf*grad2);
}
}
if (tx<=13) {
float grad1 = horf*grad;
if (y>=2) { // Upper right
float grad2 = iverf*grad1;
atomicAdd(buffer + p1+8, iangf*grad2);
atomicAdd(buffer + p2+8, angf*grad2);
}
if (y<=13) { // Lower right
float grad2 = verf*grad1;
atomicAdd(buffer + p1+40, iangf*grad2);
atomicAdd(buffer + p2+40, angf*grad2);
}
}
}
__syncthreads();
// Normalize twice and suppress peaks first time
if (idx<64)
sums[idx] = buffer[idx]*buffer[idx] + buffer[idx+64]*buffer[idx+64];
__syncthreads();
if (idx<32) sums[idx] = sums[idx] + sums[idx+32];
__syncthreads();
if (idx<16) sums[idx] = sums[idx] + sums[idx+16];
__syncthreads();
if (idx<8) sums[idx] = sums[idx] + sums[idx+8];
__syncthreads();
if (idx<4) sums[idx] = sums[idx] + sums[idx+4];
__syncthreads();
float tsum1 = sums[0] + sums[1] + sums[2] + sums[3];
buffer[idx] = buffer[idx] * rsqrtf(tsum1);
if (buffer[idx]>0.2f)
buffer[idx] = 0.2f;
__syncthreads();
if (idx<64)
sums[idx] = buffer[idx]*buffer[idx] + buffer[idx+64]*buffer[idx+64];
__syncthreads();
if (idx<32) sums[idx] = sums[idx] + sums[idx+32];
__syncthreads();
if (idx<16) sums[idx] = sums[idx] + sums[idx+16];
__syncthreads();
if (idx<8) sums[idx] = sums[idx] + sums[idx+8];
__syncthreads();
if (idx<4) sums[idx] = sums[idx] + sums[idx+4];
__syncthreads();
float tsum2 = sums[0] + sums[1] + sums[2] + sums[3];
float *desc = d_sift[bx].data;
desc[idx] = buffer[idx] * rsqrtf(tsum2);
if (idx==0) {
d_sift[bx].xpos *= subsampling;
d_sift[bx].ypos *= subsampling;
d_sift[bx].scale *= subsampling;
}
}
__global__ void RescalePositions(SiftPoint *d_sift, int numPts, float scale)
{
int num = blockIdx.x*blockDim.x + threadIdx.x;
if (num<numPts) {
d_sift[num].xpos *= scale;
d_sift[num].ypos *= scale;
d_sift[num].scale *= scale;
}
}
__global__ void ComputeOrientations(cudaTextureObject_t texObj, SiftPoint *d_Sift, int fstPts)
{
__shared__ float hist[64];
__shared__ float gauss[11];
const int tx = threadIdx.x;
const int bx = blockIdx.x + fstPts;
float i2sigma2 = -1.0f/(4.5f*d_Sift[bx].scale*d_Sift[bx].scale);
if (tx<11)
gauss[tx] = exp(i2sigma2*(tx-5)*(tx-5));
if (tx<64)
hist[tx] = 0.0f;
__syncthreads();
float xp = d_Sift[bx].xpos - 5.0f;
float yp = d_Sift[bx].ypos - 5.0f;
int yd = tx/11;
int xd = tx - yd*11;
float xf = xp + xd;
float yf = yp + yd;
if (yd<11) {
float dx = tex2D<float>(texObj, xf+1.0, yf) - tex2D<float>(texObj, xf-1.0, yf);
float dy = tex2D<float>(texObj, xf, yf+1.0) - tex2D<float>(texObj, xf, yf-1.0);
int bin = 16.0f*atan2f(dy, dx)/3.1416f + 16.5f;
if (bin>31)
bin = 0;
float grad = sqrtf(dx*dx + dy*dy);
atomicAdd(&hist[bin], grad*gauss[xd]*gauss[yd]);
}
__syncthreads();
int x1m = (tx>=1 ? tx-1 : tx+31);
int x1p = (tx<=30 ? tx+1 : tx-31);
if (tx<32) {
int x2m = (tx>=2 ? tx-2 : tx+30);
int x2p = (tx<=29 ? tx+2 : tx-30);
hist[tx+32] = 6.0f*hist[tx] + 4.0f*(hist[x1m] + hist[x1p]) + (hist[x2m] + hist[x2p]);
}
__syncthreads();
if (tx<32) {
float v = hist[32+tx];
hist[tx] = (v>hist[32+x1m] && v>=hist[32+x1p] ? v : 0.0f);
}
__syncthreads();
if (tx==0) {
float maxval1 = 0.0;
float maxval2 = 0.0;
int i1 = -1;
int i2 = -1;
for (int i=0;i<32;i++) {
float v = hist[i];
if (v>maxval1) {
maxval2 = maxval1;
maxval1 = v;
i2 = i1;
i1 = i;
} else if (v>maxval2) {
maxval2 = v;
i2 = i;
}
}
float val1 = hist[32+((i1+1)&31)];
float val2 = hist[32+((i1+31)&31)];
float peak = i1 + 0.5f*(val1-val2) / (2.0f*maxval1-val1-val2);
d_Sift[bx].orientation = 11.25f*(peak<0.0f ? peak+32.0f : peak);
if (maxval2>0.8f*maxval1 && false) {
float val1 = hist[32+((i2+1)&31)];
float val2 = hist[32+((i2+31)&31)];
float peak = i2 + 0.5f*(val1-val2) / (2.0f*maxval2-val1-val2);
unsigned int idx = atomicInc(d_PointCounter, 0x7fffffff);
if (idx<d_MaxNumPoints) {
d_Sift[idx].xpos = d_Sift[bx].xpos;
d_Sift[idx].ypos = d_Sift[bx].ypos;
d_Sift[idx].scale = d_Sift[bx].scale;
d_Sift[idx].sharpness = d_Sift[bx].sharpness;
d_Sift[idx].edgeness = d_Sift[bx].edgeness;
d_Sift[idx].orientation = 11.25f*(peak<0.0f ? peak+32.0f : peak);;
d_Sift[idx].subsampling = d_Sift[bx].subsampling;
}
}
}
}
///////////////////////////////////////////////////////////////////////////////
// Subtract two images (multi-scale version)
///////////////////////////////////////////////////////////////////////////////
__global__ void FindPointsMulti(float *d_Data0, SiftPoint *d_Sift, int width, int pitch, int height, int nScales, float subsampling, float lowestScale)
{
#define MEMWID (MINMAX_W + 2)
__shared__ float ymin1[MEMWID], ymin2[MEMWID], ymin3[MEMWID];
__shared__ float ymax1[MEMWID], ymax2[MEMWID], ymax3[MEMWID];
__shared__ unsigned int cnt;
__shared__ unsigned short points[96];
int tx = threadIdx.x;
int block = blockIdx.x/nScales;
int scale = blockIdx.x - nScales*block;
int minx = block*MINMAX_W;
int maxx = min(minx + MINMAX_W, width);
int xpos = minx + tx;
int size = pitch*height;
int ptr = size*scale + max(min(xpos-1, width-1), 0);
if (tx==0)
cnt = 0;
__syncthreads();
int yloops = min(height - MINMAX_H*blockIdx.y, MINMAX_H);
for (int y=0;y<yloops;y++) {
int ypos = MINMAX_H*blockIdx.y + y;
int yptr0 = ptr + max(0,ypos-1)*pitch;
int yptr1 = ptr + ypos*pitch;
int yptr2 = ptr + min(height-1,ypos+1)*pitch;
{
float d10 = d_Data0[yptr0];
float d11 = d_Data0[yptr1];
float d12 = d_Data0[yptr2];
ymin1[tx] = fminf(fminf(d10, d11), d12);
ymax1[tx] = fmaxf(fmaxf(d10, d11), d12);
}
{
float d30 = d_Data0[yptr0 + 2*size];
float d31 = d_Data0[yptr1 + 2*size];
float d32 = d_Data0[yptr2 + 2*size];
ymin3[tx] = fminf(fminf(d30, d31), d32);
ymax3[tx] = fmaxf(fmaxf(d30, d31), d32);
}
float d20 = d_Data0[yptr0 + 1*size];
float d21 = d_Data0[yptr1 + 1*size];
float d22 = d_Data0[yptr2 + 1*size];
ymin2[tx] = fminf(fminf(ymin1[tx], fminf(fminf(d20, d21), d22)), ymin3[tx]);
ymax2[tx] = fmaxf(fmaxf(ymax1[tx], fmaxf(fmaxf(d20, d21), d22)), ymax3[tx]);
__syncthreads();
if (tx>0 && tx<MINMAX_W+1 && xpos<=maxx) {
if (d21<d_Threshold[1]) {
float minv = fminf(fminf(fminf(ymin2[tx-1], ymin2[tx+1]), ymin1[tx]), ymin3[tx]);
minv = fminf(fminf(minv, d20), d22);
if (d21<minv) {
int pos = atomicInc(&cnt, 31);
points[3*pos+0] = xpos - 1;
points[3*pos+1] = ypos;
points[3*pos+2] = scale;
}
}
if (d21>d_Threshold[0]) {
float maxv = fmaxf(fmaxf(fmaxf(ymax2[tx-1], ymax2[tx+1]), ymax1[tx]), ymax3[tx]);
maxv = fmaxf(fmaxf(maxv, d20), d22);
if (d21>maxv) {
int pos = atomicInc(&cnt, 31);
points[3*pos+0] = xpos - 1;
points[3*pos+1] = ypos;
points[3*pos+2] = scale;
}
}
}
__syncthreads();
}
if (tx<cnt) {
int xpos = points[3*tx+0];
int ypos = points[3*tx+1];
int scale = points[3*tx+2];
int ptr = xpos + (ypos + (scale+1)*height)*pitch;
float val = d_Data0[ptr];
float *data1 = &d_Data0[ptr];
float dxx = 2.0f*val - data1[-1] - data1[1];
float dyy = 2.0f*val - data1[-pitch] - data1[pitch];
float dxy = 0.25f*(data1[+pitch+1] + data1[-pitch-1] - data1[-pitch+1] - data1[+pitch-1]);
float tra = dxx + dyy;
float det = dxx*dyy - dxy*dxy;
if (tra*tra<d_EdgeLimit*det) {
float edge = __fdividef(tra*tra, det);
float dx = 0.5f*(data1[1] - data1[-1]);
float dy = 0.5f*(data1[pitch] - data1[-pitch]);
float *data0 = d_Data0 + ptr - height*pitch;
float *data2 = d_Data0 + ptr + height*pitch;
float ds = 0.5f*(data0[0] - data2[0]);
float dss = 2.0f*val - data2[0] - data0[0];
float dxs = 0.25f*(data2[1] + data0[-1] - data0[1] - data2[-1]);
float dys = 0.25f*(data2[pitch] + data0[-pitch] - data2[-pitch] - data0[pitch]);
float idxx = dyy*dss - dys*dys;
float idxy = dys*dxs - dxy*dss;
float idxs = dxy*dys - dyy*dxs;
float idet = __fdividef(1.0f, idxx*dxx + idxy*dxy + idxs*dxs);
float idyy = dxx*dss - dxs*dxs;
float idys = dxy*dxs - dxx*dys;
float idss = dxx*dyy - dxy*dxy;
float pdx = idet*(idxx*dx + idxy*dy + idxs*ds);
float pdy = idet*(idxy*dx + idyy*dy + idys*ds);
float pds = idet*(idxs*dx + idys*dy + idss*ds);
if (pdx<-0.5f || pdx>0.5f || pdy<-0.5f || pdy>0.5f || pds<-0.5f || pds>0.5f) {
pdx = __fdividef(dx, dxx);
pdy = __fdividef(dy, dyy);
pds = __fdividef(ds, dss);
}
float dval = 0.5f*(dx*pdx + dy*pdy + ds*pds);
int maxPts = d_MaxNumPoints;
float sc = d_Scales[scale] * exp2f(pds*d_Factor);
if (sc>=lowestScale) {
unsigned int idx = atomicInc(d_PointCounter, 0x7fffffff);
idx = (idx>=maxPts ? maxPts-1 : idx);
d_Sift[idx].xpos = xpos + pdx;
d_Sift[idx].ypos = ypos + pdy;
d_Sift[idx].scale = sc;
d_Sift[idx].sharpness = val + dval;
d_Sift[idx].edgeness = edge;
d_Sift[idx].subsampling = subsampling;
}
}
}
}
__global__ void LaplaceMultiTex(cudaTextureObject_t texObj, float *d_Result, int width, int pitch, int height)
{
__shared__ float data1[(LAPLACE_W + 2*LAPLACE_R)*LAPLACE_S];
__shared__ float data2[LAPLACE_W*LAPLACE_S];
const int tx = threadIdx.x;
const int xp = blockIdx.x*LAPLACE_W + tx;
const int yp = blockIdx.y;
const int scale = threadIdx.y;
float *kernel = d_Kernel2 + scale*16;
float *sdata1 = data1 + (LAPLACE_W + 2*LAPLACE_R)*scale;
float x = xp-3.5;
float y = yp+0.5;
sdata1[tx] = kernel[4]*tex2D<float>(texObj, x, y) +
kernel[3]*(tex2D<float>(texObj, x, y-1.0) + tex2D<float>(texObj, x, y+1.0)) +
kernel[2]*(tex2D<float>(texObj, x, y-2.0) + tex2D<float>(texObj, x, y+2.0)) +
kernel[1]*(tex2D<float>(texObj, x, y-3.0) + tex2D<float>(texObj, x, y+3.0)) +
kernel[0]*(tex2D<float>(texObj, x, y-4.0) + tex2D<float>(texObj, x, y+4.0));
__syncthreads();
float *sdata2 = data2 + LAPLACE_W*scale;
if (tx<LAPLACE_W) {
sdata2[tx] = kernel[4]*sdata1[tx+4] +
kernel[3]*(sdata1[tx+3] + sdata1[tx+5]) +
kernel[2]*(sdata1[tx+2] + sdata1[tx+6]) +
kernel[1]*(sdata1[tx+1] + sdata1[tx+7]) +
kernel[0]*(sdata1[tx+0] + sdata1[tx+8]);
}
__syncthreads();
if (tx<LAPLACE_W && scale<LAPLACE_S-1 && xp<width)
d_Result[scale*height*pitch + yp*pitch + xp] = sdata2[tx] - sdata2[tx+LAPLACE_W];
}
__global__ void LaplaceMultiMem(float *d_Image, float *d_Result, int width, int pitch, int height)
{
__shared__ float data1[(LAPLACE_W + 2*LAPLACE_R)*LAPLACE_S];
__shared__ float data2[LAPLACE_W*LAPLACE_S];
const int tx = threadIdx.x;
const int xp = blockIdx.x*LAPLACE_W + tx;
const int yp = blockIdx.y;
const int scale = threadIdx.y;
float *kernel = d_Kernel2 + scale*16;
float *sdata1 = data1 + (LAPLACE_W + 2*LAPLACE_R)*scale;
float *data = d_Image + max(min(xp - 4, width-1), 0);
int h = height-1;
sdata1[tx] = kernel[4]*data[min(yp, h)*pitch] +
kernel[3]*(data[max(0, min(yp-1, h))*pitch] + data[min(yp+1, h)*pitch]) +
kernel[2]*(data[max(0, min(yp-2, h))*pitch] + data[min(yp+2, h)*pitch]) +
kernel[1]*(data[max(0, min(yp-3, h))*pitch] + data[min(yp+3, h)*pitch]) +
kernel[0]*(data[max(0, min(yp-4, h))*pitch] + data[min(yp+4, h)*pitch]);
__syncthreads();
float *sdata2 = data2 + LAPLACE_W*scale;
if (tx<LAPLACE_W) {
sdata2[tx] = kernel[4]*sdata1[tx+4] +
kernel[3]*(sdata1[tx+3] + sdata1[tx+5]) + kernel[2]*(sdata1[tx+2] + sdata1[tx+6]) +
kernel[1]*(sdata1[tx+1] + sdata1[tx+7]) + kernel[0]*(sdata1[tx+0] + sdata1[tx+8]);
}
__syncthreads();
if (tx<LAPLACE_W && scale<LAPLACE_S-1 && xp<width)
d_Result[scale*height*pitch + yp*pitch + xp] = sdata2[tx] - sdata2[tx+LAPLACE_W];
}
__global__ void LowPass(float *d_Image, float *d_Result, int width, int pitch, int height)
{
__shared__ float buffer[(LOWPASS_W + 2*LOWPASS_R)*LOWPASS_H];
const int tx = threadIdx.x;
const int ty = threadIdx.y;
const int xp = blockIdx.x*LOWPASS_W + tx;
const int yp = blockIdx.y*LOWPASS_H + ty;
float *kernel = d_Kernel2;
float *data = d_Image + max(min(xp - 4, width-1), 0);
float *buff = buffer + ty*(LOWPASS_W + 2*LOWPASS_R);
int h = height-1;
if (yp<height)
buff[tx] = kernel[4]*data[min(yp, h)*pitch] +
kernel[3]*(data[max(0, min(yp-1, h))*pitch] + data[min(yp+1, h)*pitch]) +
kernel[2]*(data[max(0, min(yp-2, h))*pitch] + data[min(yp+2, h)*pitch]) +
kernel[1]*(data[max(0, min(yp-3, h))*pitch] + data[min(yp+3, h)*pitch]) +
kernel[0]*(data[max(0, min(yp-4, h))*pitch] + data[min(yp+4, h)*pitch]);
__syncthreads();
if (tx<LOWPASS_W && xp<width && yp<height) {
d_Result[yp*pitch + xp] = kernel[4]*buff[tx+4] +
kernel[3]*(buff[tx+3] + buff[tx+5]) + kernel[2]*(buff[tx+2] + buff[tx+6]) +
kernel[1]*(buff[tx+1] + buff[tx+7]) + kernel[0]*(buff[tx+0] + buff[tx+8]);
}
}