Statistics
| Branch: | Revision:

root / rgbdslam / external / siftgpu / src / SiftGPU / SiftMatch.cpp @ 9240aaa3

History | View | Annotate | Download (20.8 KB)

1
////////////////////////////////////////////////////////////////////////////
2
//        File:                SiftMatch.cpp
3
//        Author:                Changchang Wu
4
//        Description :        implementation of SiftMatchGPU and SiftMatchGL
5
//
6
//
7
//        Copyright (c) 2007 University of North Carolina at Chapel Hill
8
//        All Rights Reserved
9
//
10
//        Permission to use, copy, modify and distribute this software and its
11
//        documentation for educational, research and non-profit purposes, without
12
//        fee, and without a written agreement is hereby granted, provided that the
13
//        above copyright notice and the following paragraph appear in all copies.
14
//        
15
//        The University of North Carolina at Chapel Hill make no representations
16
//        about the suitability of this software for any purpose. It is provided
17
//        'as is' without express or implied warranty. 
18
//
19
//        Please send BUG REPORTS to ccwu@cs.unc.edu
20
//
21
////////////////////////////////////////////////////////////////////////////
22

    
23

    
24
#include "GL/glew.h"
25
#include <iostream>
26
#include <iomanip>
27
#include <vector>
28
#include <strstream>
29
#include <algorithm>
30
using namespace std;
31
#include <string.h>
32
#include "GlobalUtil.h"
33

    
34
#include "ProgramGLSL.h"
35
#include "GLTexImage.h"
36
#include "SiftGPU.h"
37
#include "SiftMatch.h"
38
#include "FrameBufferObject.h"
39

    
40
#if defined(CUDA_SIFTGPU_ENABLED)
41
#include "CuTexImage.h"
42
#include "SiftMatchCU.h"
43
#endif
44

    
45

    
46
SiftMatchGL::SiftMatchGL(int max_sift, int use_glsl): SiftMatchGPU()
47
{
48
        s_multiply = s_col_max = s_row_max = s_guided_mult = NULL;
49
        _num_sift[0] = _num_sift[1] = 0;
50
        _id_sift[0] = _id_sift[1] = 0;
51
        _have_loc[0] = _have_loc[1] = 0;
52
        _max_sift = max_sift <=0 ? 4096 : ((max_sift + 31)/ 32 * 32) ; 
53
        _pixel_per_sift = 32; //must be 32
54
        _sift_num_stripe = 1; 
55
        _sift_per_stripe = 1;
56
        _sift_per_row = _sift_per_stripe * _sift_num_stripe;
57
        _initialized = 0;
58
}
59

    
60
SiftMatchGL::~SiftMatchGL()
61
{
62
        if(s_multiply) delete s_multiply;
63
        if(s_guided_mult) delete s_guided_mult;
64
        if(s_col_max) delete s_col_max;
65
        if(s_row_max) delete s_row_max;
66
}
67

    
68
void SiftMatchGL::SetMaxSift(int max_sift)
69
{
70
        
71
        max_sift = ((max_sift + 31)/32)*32;
72
        if(max_sift > GlobalUtil::_texMaxDimGL) max_sift = GlobalUtil::_texMaxDimGL;
73
        if(max_sift > _max_sift)
74
        {
75
                _max_sift = max_sift;
76
                AllocateSiftMatch();
77
                _have_loc[0] = _have_loc[1] = 0;
78
                _id_sift[0] = _id_sift[1] = -1;
79
                _num_sift[0] = _num_sift[1] = 1;
80
        }else
81
        {
82
                _max_sift = max_sift;
83
        }
84

    
85
}
86

    
87
void SiftMatchGL::AllocateSiftMatch()
88
{
89
        //parameters, number of sift is limited by the texture size
90
        if(_max_sift > GlobalUtil::_texMaxDimGL) _max_sift = GlobalUtil::_texMaxDimGL;        
91
        ///
92
        int h = _max_sift / _sift_per_row; 
93
        int n = (GlobalUtil::_texMaxDimGL + h - 1) / GlobalUtil::_texMaxDimGL; 
94
        if ( n > 1) {_sift_num_stripe *= n; _sift_per_row *= n; }
95

    
96
        //initialize
97

    
98
        _texDes[0].InitTexture(_sift_per_row * _pixel_per_sift, _max_sift / _sift_per_row, 0,GL_RGBA8);
99
        _texDes[1].InitTexture(_sift_per_row * _pixel_per_sift, _max_sift / _sift_per_row, 0, GL_RGBA8);
100
        _texLoc[0].InitTexture(_sift_per_row , _max_sift / _sift_per_row, 0);
101
        _texLoc[1].InitTexture(_sift_per_row , _max_sift / _sift_per_row, 0);
102

    
103
        if(GlobalUtil::_SupportNVFloat || GlobalUtil::_SupportTextureRG)
104
        {
105
                //use single-component texture to save memory
106
#ifndef GL_R32F
107
#define GL_R32F 0x822E
108
#endif
109
                GLuint format = GlobalUtil::_SupportNVFloat ? GL_FLOAT_R_NV : GL_R32F;
110
                _texDot.InitTexture(_max_sift, _max_sift, 0, format);
111
                _texMatch[0].InitTexture(16, _max_sift / 16, 0, format);
112
                _texMatch[1].InitTexture(16, _max_sift / 16, 0, format);
113
        }else
114
        {
115
                _texDot.InitTexture(_max_sift, _max_sift, 0);
116
                _texMatch[0].InitTexture(16, _max_sift / 16, 0);
117
                _texMatch[1].InitTexture(16, _max_sift / 16, 0);
118
        }
119

    
120
}
121
void SiftMatchGL::InitSiftMatch()
122
{
123
        if(_initialized) return;
124
        GlobalUtil::InitGLParam(0);
125
        if(GlobalUtil::_GoodOpenGL == 0) return;
126
        AllocateSiftMatch();
127
        LoadSiftMatchShadersGLSL();
128
        _initialized = 1; 
129
}
130

    
131

    
132
void SiftMatchGL::SetDescriptors(int index, int num, const unsigned char* descriptors, int id)
133
{        
134
        if(_initialized == 0) return;
135
        if (index > 1) index = 1;
136
        if (index < 0) index = 0;
137
        _have_loc[index] = 0;
138

    
139
        //the same feature is already set
140
        if(id !=-1 && id == _id_sift[index]) return ;
141
        _id_sift[index] = id;
142

    
143
        if(num > _max_sift) num = _max_sift;
144

    
145
        sift_buffer.resize(num * 128 /4);
146
        memcpy(&sift_buffer[0], descriptors, 128 * num);
147
        _num_sift[index] = num; 
148
        int w = _sift_per_row * _pixel_per_sift;
149
        int h = (num + _sift_per_row  - 1)/ _sift_per_row; 
150
        sift_buffer.resize(w * h * 4, 0);
151
        _texDes[index].SetImageSize(w , h);
152
        _texDes[index].BindTex(); 
153
        if(_sift_num_stripe == 1)
154
        {
155
                glTexSubImage2D(GlobalUtil::_texTarget, 0, 0, 0, w, h, GL_RGBA,  GL_UNSIGNED_BYTE, &sift_buffer[0]);
156
        }else
157
        {
158
                for(int i = 0; i < _sift_num_stripe; ++i)
159
                {
160
                        int ws = _sift_per_stripe * _pixel_per_sift;
161
                        int x = i * ws;
162
                        int pos = i * ws * h * 4; 
163
                        glTexSubImage2D(GlobalUtil::_texTarget, 0, x, 0, ws, h, GL_RGBA, GL_UNSIGNED_BYTE, &sift_buffer[pos]);
164
                }
165
        }
166
        _texDes[index].UnbindTex();
167

    
168
}
169

    
170
void SiftMatchGL::SetFeautreLocation(int index, const float* locations, int gap)
171
{
172
        if(_num_sift[index] <=0) return;
173
        int w = _sift_per_row ;
174
        int h = (_num_sift[index] + _sift_per_row  - 1)/ _sift_per_row; 
175
        sift_buffer.resize(_num_sift[index] * 2);
176
        if(gap == 0)
177
        {
178
                memcpy(&sift_buffer[0], locations, _num_sift[index] * 2 * sizeof(float));
179
        }else
180
        {
181
                for(int i = 0; i < _num_sift[index]; ++i)
182
                {
183
                        sift_buffer[i*2] = *locations++;
184
                        sift_buffer[i*2+1]= *locations ++;
185
                        locations += gap;
186
                }
187
        }
188
        sift_buffer.resize(w * h * 2, 0);
189
        _texLoc[index].SetImageSize(w , h);
190
        _texLoc[index].BindTex(); 
191
        if(_sift_num_stripe == 1)
192
        {
193
                glTexSubImage2D(GlobalUtil::_texTarget, 0, 0, 0, w, h, GL_LUMINANCE_ALPHA , GL_FLOAT , &sift_buffer[0]);
194
        }else
195
        {
196
                for(int i = 0; i < _sift_num_stripe; ++i)
197
                {
198
                        int ws = _sift_per_stripe;
199
                        int x = i * ws;
200
                        int pos = i * ws * h * 2; 
201
                        glTexSubImage2D(GlobalUtil::_texTarget, 0, x, 0, ws, h, GL_LUMINANCE_ALPHA , GL_FLOAT, &sift_buffer[pos]);
202
                }
203
        }
204
        _texLoc[index].UnbindTex();
205
        _have_loc[index] = 1;
206
}
207

    
208
void SiftMatchGL::SetDescriptors(int index, int num, const float* descriptors, int id)
209
{        
210
        if(_initialized == 0) return;
211
        if (index > 1) index = 1;
212
        if (index < 0) index = 0;
213
        _have_loc[index] = 0;
214

    
215
        //the same feature is already set
216
        if(id !=-1 && id == _id_sift[index]) return ;
217
        _id_sift[index] = id; 
218

    
219
        if(num > _max_sift) num = _max_sift;
220

    
221
        sift_buffer.resize(num * 128 /4);
222
        unsigned char * pub = (unsigned char*) &sift_buffer[0];
223
        for(int i = 0; i < 128 * num; ++i)
224
        {
225
                pub[i] = int(512 * descriptors[i] + 0.5);
226
        }
227
        _num_sift[index] = num; 
228
        int w = _sift_per_row * _pixel_per_sift;
229
        int h = (num + _sift_per_row  - 1)/ _sift_per_row; 
230
        sift_buffer.resize(w * h * 4, 0);
231
        _texDes[index].SetImageSize(w, h);
232
        _texDes[index].BindTex();
233
        if(_sift_num_stripe == 1)
234
        {
235
                glTexSubImage2D(GlobalUtil::_texTarget, 0, 0, 0, w, h, GL_RGBA,  GL_UNSIGNED_BYTE, &sift_buffer[0]);
236
        }else
237
        {
238
                for(int i = 0; i < _sift_num_stripe; ++i)
239
                {
240
                        int ws = _sift_per_stripe * _pixel_per_sift;
241
                        int x = i * ws;
242
                        int pos = i * ws * h * 4; 
243
                        glTexSubImage2D(GlobalUtil::_texTarget, 0, x, 0, ws, h, GL_RGBA, GL_UNSIGNED_BYTE, &sift_buffer[pos]);
244
                }
245
        }
246
        _texDes[index].UnbindTex();
247
}
248

    
249

    
250
void SiftMatchGL::LoadSiftMatchShadersGLSL()
251
{
252
        ProgramGLSL * program;
253
        char buffer[10240];
254
        ostrstream out(buffer, 10240);
255
        if(GlobalUtil::_IsNvidia)
256
        out <<  "#pragma optionNV(ifcvt none)\n"
257
                        "#pragma optionNV(unroll all)\n";
258

    
259
    out <<  "#define SIFT_PER_STRIPE " << _sift_per_stripe << ".0\n" 
260
                        "#define PIXEL_PER_SIFT " << _pixel_per_sift << "\n"
261
                        "uniform sampler2DRect tex1, tex2; uniform vec2        size;\n"
262
                        "void main()                \n"
263
                    "{\n"
264
                <<        "   vec4 val = vec4(0, 0, 0, 0), data1, buf;\n"
265
                        "   vec2 index = gl_FragCoord.yx; \n"
266
                        "   vec2 stripe_size = size.xy * SIFT_PER_STRIPE;\n"
267
                        "        vec2 temp_div1 = index / stripe_size;\n"
268
                        "   vec2 stripe_index = floor(temp_div1);\n"
269
                        "   index = floor(stripe_size * (temp_div1 - stripe_index));\n"
270
                        "        vec2 temp_div2 = index * vec2(1.0 / float(SIFT_PER_STRIPE));\n"
271
                        "        vec2 temp_floor2 = floor(temp_div2);\n"
272
                        "   vec2 index_v = temp_floor2 + vec2(0.5);\n "
273
                        "   vec2 index_h = vec2(SIFT_PER_STRIPE)* (temp_div2 - temp_floor2);\n"
274
                        "   vec2 tx = (index_h + stripe_index * vec2(SIFT_PER_STRIPE))* vec2(PIXEL_PER_SIFT) + 0.5;\n"
275
                        "   vec2 tpos1, tpos2; \n"
276
                        "        vec4 tpos = vec4(tx, index_v);\n"
277
                        //////////////////////////////////////////////////////
278
                        "   for(int i = 0; i < PIXEL_PER_SIFT; ++i){\n"
279
                        "                buf = texture2DRect(tex2, tpos.yw);\n"
280
                        "                data1 = texture2DRect(tex1, tpos.xz);\n"
281
                        "                val += (data1 * buf);\n"
282
                        "                tpos.xy = tpos.xy + vec2(1.0, 1.0);\n"
283
                        "        }\n"
284
                        "        const float factor = 0.248050689697265625; \n"
285
                        "        gl_FragColor =vec4(dot(val, vec4(factor)), index,  0);\n"
286
                        "}"
287
                <<        '\0';
288

    
289
        s_multiply = program= new ProgramGLSL(buffer); 
290

    
291
        _param_multiply_tex1 = glGetUniformLocation(*program, "tex1");
292
        _param_multiply_tex2 = glGetUniformLocation(*program, "tex2");
293
        _param_multiply_size = glGetUniformLocation(*program, "size");
294

    
295
        out.seekp(ios::beg);
296
    if(GlobalUtil::_IsNvidia)
297
    out <<  "#pragma optionNV(ifcvt none)\n"
298
                        "#pragma optionNV(unroll all)\n";
299

    
300
    out <<  "#define SIFT_PER_STRIPE " << _sift_per_stripe << ".0\n" 
301
                        "#define PIXEL_PER_SIFT " << _pixel_per_sift << "\n"
302
                        "uniform sampler2DRect tex1, tex2;\n"
303
                        "uniform sampler2DRect texL1;\n"
304
                        "uniform sampler2DRect texL2; \n"
305
                        "uniform mat3 H; \n"
306
                        "uniform mat3 F; \n"
307
                        "uniform vec4        size; \n"
308
                        "void main()                \n"
309
                    "{\n"
310
                <<        "   vec4 val = vec4(0, 0, 0, 0), data1, buf;\n"
311
                        "   vec2 index = gl_FragCoord.yx; \n"
312
                        "   vec2 stripe_size = size.xy * SIFT_PER_STRIPE;\n"
313
                        "        vec2 temp_div1 = index / stripe_size;\n"
314
                        "   vec2 stripe_index = floor(temp_div1);\n"
315
                        "   index = floor(stripe_size * (temp_div1 - stripe_index));\n"
316
                        "        vec2 temp_div2 = index  * vec2(1.0/ float(SIFT_PER_STRIPE));\n"
317
                        "        vec2 temp_floor2 = floor(temp_div2);\n"
318
                        "   vec2 index_v = temp_floor2 + vec2(0.5);\n "
319
                        "   vec2 index_h = vec2(SIFT_PER_STRIPE)* (temp_div2 - temp_floor2);\n"
320
                        
321
                        //read feature location data
322
                        "   vec4 tlpos = vec4((index_h + stripe_index * vec2(SIFT_PER_STRIPE)) + 0.5, index_v);\n"
323
                        "   vec3 loc1 = vec3(texture2DRect(texL1, tlpos.xz).xw, 1.0);\n"
324
                        "   vec3 loc2 = vec3(texture2DRect(texL2, tlpos.yw).xw, 1.0);\n"
325
                        
326
                        //check the guiding homography
327
                        "   vec3 hxloc1 = H* loc1;\n"
328
                        "   vec2 diff = abs(loc2.xy- (hxloc1.xy/hxloc1.z));\n"
329
                        "   float disth = max(diff.x, diff.y);\n"
330
                        "   if(disth > size.z ) {gl_FragColor = vec4(0, index, 0); return;}\n"
331

    
332
                        //check the guiding fundamental 
333
                        "   vec3 fx1 = (F * loc1), ftx2 = (loc2 * F);\n"
334
                        "   float x2tfx1 = dot(loc2, fx1);\n"
335
                        "   vec4 temp = vec4(fx1.xy, ftx2.xy); \n"
336
                        "   float sampson_error = (x2tfx1 * x2tfx1) / dot(temp, temp);\n"
337
                        "   if(sampson_error > size.w) {gl_FragColor = vec4(0, index, 0); return;}\n"
338

    
339
                        //compare feature descriptor
340
                        "   vec2 tx = (index_h + stripe_index * SIFT_PER_STRIPE)* vec2(PIXEL_PER_SIFT) + 0.5;\n"
341
                        "   vec2 tpos1, tpos2; \n"
342
                        "        vec4 tpos = vec4(tx, index_v);\n"
343
                        "   for(int i = 0; i < PIXEL_PER_SIFT; ++i){\n"
344
                        "                buf = texture2DRect(tex2, tpos.yw);\n"
345
                        "                data1 = texture2DRect(tex1, tpos.xz);\n"
346
                        "                val += data1 * buf;\n"
347
                        "                tpos.xy = tpos.xy + vec2(1.0, 1.0);\n"
348
                        "        }\n"
349
                        "        const float factor = 0.248050689697265625; \n"
350
                        "        gl_FragColor =vec4(dot(val, vec4(factor)), index,  0);\n"
351
                        "}"
352
                <<        '\0';
353

    
354
        s_guided_mult = program= new ProgramGLSL(buffer);
355

    
356
        _param_guided_mult_tex1 = glGetUniformLocation(*program, "tex1");
357
        _param_guided_mult_tex2= glGetUniformLocation(*program, "tex2");
358
        _param_guided_mult_texl1 = glGetUniformLocation(*program, "texL1");
359
        _param_guided_mult_texl2 = glGetUniformLocation(*program, "texL2");
360
        _param_guided_mult_h = glGetUniformLocation(*program, "H");
361
        _param_guided_mult_f = glGetUniformLocation(*program, "F");
362
        _param_guided_mult_param = glGetUniformLocation(*program, "size");
363

    
364
        //row max
365
        out.seekp(ios::beg);
366
        out <<        "#define BLOCK_WIDTH 16.0\n"
367
                        "uniform sampler2DRect tex;        uniform vec3 param;\n"
368
                        "void main ()\n"
369
                        "{\n"
370
                        "        float index = gl_FragCoord.x + floor(gl_FragCoord.y) * BLOCK_WIDTH; \n"
371
                        "        vec2 bestv = vec2(-1.0); float imax = -1.0;\n"
372
                        "        for(float i = 0.0; i < param.x; i ++){\n "
373
                        "                float v = texture2DRect(tex, vec2(i + 0.5, index)).r; \n"
374
                        "                imax = v > bestv.r ? i : imax; \n "
375
                        "                bestv  = v > bestv.r? vec2(v, bestv.r) : max(bestv, vec2(v));\n "
376
                        "        }\n"
377
                        "        bestv = acos(min(bestv, 1.0));\n"
378
                        "        if(bestv.x >= param.y || bestv.x >= param.z * bestv.y) imax = -1.0;\n"
379
                        "        gl_FragColor = vec4(imax, bestv, index);\n"
380
                        "}"
381
                <<  '\0';
382
        s_row_max = program= new ProgramGLSL(buffer); 
383
        _param_rowmax_param = glGetUniformLocation(*program, "param");
384

    
385
        out.seekp(ios::beg);
386
        out <<        "#define BLOCK_WIDTH 16.0\n"
387
                        "uniform sampler2DRect tex; uniform vec3 param;\n"
388
                        "void main ()\n"
389
                        "{\n"
390
                        "        float index = gl_FragCoord.x + floor(gl_FragCoord.y) * BLOCK_WIDTH; \n"
391
                        "        vec2 bestv = vec2(-1.0); float imax = -1.0;\n"
392
                        "        for(float i = 0.0; i < param.x; i ++){\n "
393
                        "                float v = texture2DRect(tex, vec2(index, i + 0.5)).r; \n"
394
                        "                imax = (v > bestv.r)? i : imax; \n "
395
                        "                bestv  = v > bestv.r? vec2(v, bestv.r) : max(bestv, vec2(v));\n "
396
                        "        }\n"
397
                        "        bestv = acos(min(bestv, 1.0));\n"
398
                        "        if(bestv.x >= param.y || bestv.x >= param.z * bestv.y) imax = -1.0;\n"
399
                        "        gl_FragColor = vec4(imax, bestv, index);\n"
400
                        "}"
401
                <<  '\0';
402
        s_col_max = program =new ProgramGLSL(buffer); 
403
        _param_colmax_param = glGetUniformLocation(*program, "param");
404

    
405

    
406
}
407

    
408
int  SiftMatchGL::GetGuidedSiftMatch(int max_match, int match_buffer[][2], float H[3][3], float F[3][3],
409
                                                                         float distmax, float ratiomax, float hdistmax, float fdistmax, int mbm)
410
{
411

    
412
        int dw = _num_sift[1];
413
        int dh = _num_sift[0]; 
414
        if(_initialized ==0) return 0;
415
        if(dw <= 0 || dh <=0) return 0;
416
        if(_have_loc[0] == 0 || _have_loc[1] == 0) return 0;
417

    
418
        FrameBufferObject fbo;
419
        glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
420
        _texDot.SetImageSize(dw, dh);
421

    
422

    
423
        //data
424
        _texDot.AttachToFBO(0);
425
        _texDot.FitTexViewPort();
426
        glActiveTexture(GL_TEXTURE0);
427
        _texDes[0].BindTex();
428
        glActiveTexture(GL_TEXTURE1);
429
        _texDes[1].BindTex();
430
        glActiveTexture(GL_TEXTURE2);
431
        _texLoc[0].BindTex();
432
        glActiveTexture(GL_TEXTURE3);
433
        _texLoc[1].BindTex();
434

    
435
        //multiply the descriptor matrices
436
        s_guided_mult->UseProgram();
437

    
438

    
439
        //set parameters glsl
440
        float dot_param[4] = {(float)_texDes[0].GetDrawHeight(), (float) _texDes[1].GetDrawHeight(), hdistmax, fdistmax};
441
        glUniform1i(_param_guided_mult_tex1, 0);
442
        glUniform1i(_param_guided_mult_tex2, 1);
443
        glUniform1i(_param_guided_mult_texl1, 2);
444
        glUniform1i(_param_guided_mult_texl2, 3);
445
        glUniformMatrix3fv(_param_guided_mult_h, 1, GL_TRUE, H[0]);
446
        glUniformMatrix3fv(_param_guided_mult_f, 1, GL_TRUE, F[0]);
447
        glUniform4fv(_param_guided_mult_param, 1, dot_param);
448

    
449
        _texDot.DrawQuad();
450

    
451
        GLTexImage::UnbindMultiTex(4);
452

    
453
        return GetBestMatch(max_match, match_buffer, distmax, ratiomax, mbm);
454
}
455

    
456
int SiftMatchGL::GetBestMatch(int max_match, int match_buffer[][2], float distmax, float ratiomax, int mbm)
457
{
458

    
459
        glActiveTexture(GL_TEXTURE0);
460
        _texDot.BindTex();
461

    
462
        //readback buffer
463
        sift_buffer.resize(_num_sift[0] + _num_sift[1] + 16);
464
        float * buffer1 = &sift_buffer[0], * buffer2 = &sift_buffer[_num_sift[0]];
465

    
466
        //row max
467
        _texMatch[0].AttachToFBO(0);
468
        _texMatch[0].SetImageSize(16, ( _num_sift[0] + 15) / 16);
469
        _texMatch[0].FitTexViewPort();
470

    
471
        ///set parameter glsl
472
        s_row_max->UseProgram();
473
        glUniform3f(_param_rowmax_param, (float)_num_sift[1], distmax, ratiomax);
474

    
475
        _texMatch[0].DrawQuad();
476
        glReadPixels(0, 0, 16, (_num_sift[0] + 15)/16, GL_RED, GL_FLOAT, buffer1);
477

    
478
        //col max
479
        if(mbm)
480
        {
481
                _texMatch[1].AttachToFBO(0);
482
                _texMatch[1].SetImageSize(16, (_num_sift[1] + 15) / 16);
483
                _texMatch[1].FitTexViewPort();
484
                //set parameter glsl
485
                s_col_max->UseProgram();
486
                glUniform3f(_param_rowmax_param, (float)_num_sift[0], distmax, ratiomax);
487
                _texMatch[1].DrawQuad();
488
                glReadPixels(0, 0, 16, (_num_sift[1] + 15) / 16, GL_RED, GL_FLOAT, buffer2);
489
        }
490

    
491

    
492
        //unload
493
        glUseProgram(0);
494

    
495
        GLTexImage::UnbindMultiTex(2);
496
        GlobalUtil::CleanupOpenGL();
497

    
498
        //write back the matches
499
        int nmatch = 0, j ;
500
        for(int i = 0; i < _num_sift[0] && nmatch < max_match; ++i)
501
        {
502
                j = int(buffer1[i]);
503
                if( j>= 0 && (!mbm ||int(buffer2[j]) == i))
504
                {
505
                        match_buffer[nmatch][0] = i;
506
                        match_buffer[nmatch][1] = j;
507
                        nmatch++;
508
                }
509
        }
510
        return nmatch;
511
}
512

    
513
int  SiftMatchGL::GetSiftMatch(int max_match, int match_buffer[][2], float distmax, float ratiomax, int mbm)
514
{
515
        int dw = _num_sift[1];
516
        int dh =  _num_sift[0]; 
517
        if(_initialized ==0) return 0;
518
        if(dw <= 0 || dh <=0) return 0;
519

    
520
        FrameBufferObject fbo;
521
        glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
522
        _texDot.SetImageSize(dw, dh);
523

    
524
        //data
525
        _texDot.AttachToFBO(0);
526
        _texDot.FitTexViewPort();
527
        glActiveTexture(GL_TEXTURE0);
528
        _texDes[0].BindTex();
529
        glActiveTexture(GL_TEXTURE1);
530
        _texDes[1].BindTex();
531

    
532
        //////////////////        
533
        //multiply the descriptor matrices
534
        s_multiply->UseProgram();
535
        //set parameters
536
        float heights[2] = {(float)_texDes[0].GetDrawHeight(), (float)_texDes[1].GetDrawHeight()};
537

    
538
        glUniform1i(_param_multiply_tex1, 0);
539
        glUniform1i(_param_multiply_tex2 , 1);
540
        glUniform2fv(_param_multiply_size, 1, heights);
541

    
542
        _texDot.DrawQuad();
543

    
544
        glActiveTexture(GL_TEXTURE1);
545
        glBindTexture(GlobalUtil::_texTarget, 0);
546

    
547
        return GetBestMatch(max_match, match_buffer, distmax, ratiomax, mbm);
548
}
549

    
550

    
551
int SiftMatchGPU::_CreateContextGL()
552
{
553
        //Create an OpenGL Context?
554
    if (__language >= SIFTMATCH_CUDA) {}
555
        else if(!GlobalUtil::CreateWindowEZ())
556
        {
557
#if CUDA_SIFTGPU_ENABLED
558
                __language = SIFTMATCH_CUDA;
559
#else
560
                return 0;
561
#endif
562
        }
563
        return VerifyContextGL();
564
}
565

    
566

    
567
int SiftMatchGPU::_VerifyContextGL()
568
{
569
        if(__matcher) return GlobalUtil::_GoodOpenGL;
570
        
571
#ifdef CUDA_SIFTGPU_ENABLED
572

    
573
    if(__language >= SIFTMATCH_CUDA) {}
574
    else if(__language == SIFTMATCH_SAME_AS_SIFTGPU && GlobalUtil::_UseCUDA){}
575
    else  GlobalUtil::InitGLParam(0); 
576
    if(GlobalUtil::_GoodOpenGL == 0) __language = SIFTMATCH_CUDA;
577

    
578
    if(((__language == SIFTMATCH_SAME_AS_SIFTGPU && GlobalUtil::_UseCUDA) || __language >= SIFTMATCH_CUDA) 
579
        && SiftMatchCU::CheckCudaDevice (GlobalUtil::_DeviceIndex))
580
    {
581
                __language = SIFTMATCH_CUDA;
582
                __matcher = new SiftMatchCU(__max_sift);
583
        }else
584
#else
585
    if((__language == SIFTMATCH_SAME_AS_SIFTGPU && GlobalUtil::_UseCUDA) || __language >= SIFTMATCH_CUDA) 
586
    {
587
            std::cerr        << "---------------------------------------------------------------------------\n"
588
                                    << "CUDA not supported in this binary! To enable it, please use SiftGPU_CUDA_Enable\n" 
589
                                    << "Project for VS2005+ or set siftgpu_enable_cuda to 1 in makefile\n"
590
                                    << "----------------------------------------------------------------------------\n";
591
    }
592
#endif
593
        {
594
                __language = SIFTMATCH_GLSL;
595
                __matcher = new SiftMatchGL(__max_sift, 1);
596
        }
597

    
598
        if(GlobalUtil::_verbose)
599
        std::cout   << "[SiftMatchGPU]: " << (__language == SIFTMATCH_CUDA? "CUDA" : "GLSL") <<"\n\n";
600

    
601
        __matcher->InitSiftMatch();
602
        return GlobalUtil::_GoodOpenGL;
603
}
604

    
605
void* SiftMatchGPU::operator new (size_t  size){
606
  void * p = malloc(size);
607
  if (p == 0)  
608
  {
609
          const std::bad_alloc ba;
610
          throw ba; 
611
  }
612
  return p; 
613
}
614

    
615

    
616
SiftMatchGPU::SiftMatchGPU(int max_sift)
617
{
618
        __max_sift = max(max_sift, 1024);
619
        __language = 0;
620
        __matcher = NULL;
621
}
622

    
623
void SiftMatchGPU::SetLanguage(int language)
624
{
625
        if(__matcher) return;
626
    ////////////////////////
627
#ifdef CUDA_SIFTGPU_ENABLED
628
        if(language >= SIFTMATCH_CUDA) GlobalUtil::_DeviceIndex = language - SIFTMATCH_CUDA; 
629
#endif
630
    __language = language > SIFTMATCH_CUDA ? SIFTMATCH_CUDA : language;
631
}
632

    
633
void SiftMatchGPU::SetDeviceParam(int argc, char**argv)
634
{
635
    if(__matcher) return;
636
    GlobalUtil::SetDeviceParam(argc, argv);
637
}
638

    
639
void SiftMatchGPU::SetMaxSift(int max_sift)
640
{
641
        if(__matcher)        __matcher->SetMaxSift(max(128, max_sift));
642
        else __max_sift = max(128, max_sift);
643
}
644

    
645
SiftMatchGPU::~SiftMatchGPU()
646
{
647
        if(__matcher) delete __matcher;
648
}
649

    
650
void SiftMatchGPU::SetDescriptors(int index, int num, const unsigned char* descriptors, int id)
651
{
652
        __matcher->SetDescriptors(index, num,  descriptors, id);
653
}
654

    
655
void SiftMatchGPU::SetDescriptors(int index, int num, const float* descriptors, int id)
656
{
657
        __matcher->SetDescriptors(index, num, descriptors, id);
658
}
659

    
660
void SiftMatchGPU::SetFeautreLocation(int index, const float* locations, int gap)
661
{
662
        __matcher->SetFeautreLocation(index, locations, gap);
663

    
664
}
665
int  SiftMatchGPU::GetGuidedSiftMatch(int max_match, int match_buffer[][2], float H[3][3], float F[3][3], 
666
                                float distmax, float ratiomax, float hdistmax, float fdistmax, int mutual_best_match)
667
{
668
        if(H == NULL && F == NULL)
669
        {
670
                return __matcher->GetSiftMatch(max_match, match_buffer, distmax, ratiomax, mutual_best_match);
671
        }else
672
        {
673
                float Z[3][3] = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}, ti = (1.0e+20F);
674

    
675
                return __matcher->GetGuidedSiftMatch(max_match, match_buffer, H? H : Z, F? F : Z,
676
                        distmax, ratiomax, H? hdistmax: ti,  F? fdistmax: ti, mutual_best_match);
677
        }
678
}
679

    
680
int  SiftMatchGPU::GetSiftMatch(int max_match, int match_buffer[][2], float distmax, float ratiomax, int mutual_best_match)
681
{
682
        return __matcher->GetSiftMatch(max_match, match_buffer, distmax, ratiomax, mutual_best_match);
683
}
684

    
685
SiftMatchGPU* CreateNewSiftMatchGPU(int max_sift)
686
{
687
        return new SiftMatchGPU(max_sift);
688
}
689