| 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 [email protected] |
| 20 |
// |
| 21 |
//////////////////////////////////////////////////////////////////////////// |
| 22 |
|
| 23 |
#include "GL/glew.h" |
| 24 |
#include <iostream> |
| 25 |
#include <iomanip> |
| 26 |
#include <vector> |
| 27 |
#include <sstream> |
| 28 |
#include <algorithm> |
| 29 |
using namespace std; |
| 30 |
#include <string.h> |
| 31 |
#include <cfloat> |
| 32 |
#include "GlobalUtil.h" |
| 33 |
|
| 34 |
#include "ProgramGLSL.h" |
| 35 |
#include "GLTexImage.h" |
| 36 |
#include "SiftGPU.h" |
| 37 |
#include "SiftMatch.h" |
| 38 |
|
| 39 |
#include "FrameBufferObject.h" |
| 40 |
|
| 41 |
#if defined(SIFTGPU_CUDA_ENABLED) |
| 42 |
#include "CuTexImage.h" |
| 43 |
#include "SiftMatchCU.h" |
| 44 |
#endif |
| 45 |
|
| 46 |
|
| 47 |
SiftMatchGL::SiftMatchGL(int max_sift, int use_glsl): SiftMatchGPU() |
| 48 |
{ |
| 49 |
s_multiply = s_col_max = s_row_max = s_guided_mult = NULL; |
| 50 |
s_guided_mult_ray = NULL; |
| 51 |
_num_sift[0] = _num_sift[1] = 0; |
| 52 |
_id_sift[0] = _id_sift[1] = 0; |
| 53 |
_have_loc[0] = _have_loc[1] = 0; |
| 54 |
_loc_ncomp[0] = _loc_ncomp[1] = 2; |
| 55 |
__max_sift = max_sift <=0 ? 4096 : ((max_sift + 31)/ 32 * 32) ; |
| 56 |
_pixel_per_sift = 32; //must be 32 |
| 57 |
_sift_num_stripe = 1; |
| 58 |
_sift_per_stripe = 1; |
| 59 |
_sift_per_row = _sift_per_stripe * _sift_num_stripe; |
| 60 |
_initialized = 0; |
| 61 |
} |
| 62 |
|
| 63 |
SiftMatchGL::~SiftMatchGL() |
| 64 |
{ |
| 65 |
if(s_multiply) delete s_multiply; |
| 66 |
if(s_guided_mult) delete s_guided_mult; |
| 67 |
if(s_guided_mult_ray) delete s_guided_mult_ray; |
| 68 |
if(s_col_max) delete s_col_max; |
| 69 |
if(s_row_max) delete s_row_max; |
| 70 |
} |
| 71 |
|
| 72 |
bool SiftMatchGL::Allocate(int max_sift, int mbm) { |
| 73 |
SetMaxSift(max_sift); |
| 74 |
return glGetError() == GL_NO_ERROR; |
| 75 |
} |
| 76 |
|
| 77 |
void SiftMatchGL::SetMaxSift(int max_sift) |
| 78 |
{ |
| 79 |
|
| 80 |
max_sift = ((max_sift + 31)/32)*32; |
| 81 |
if(max_sift > GlobalUtil::_texMaxDimGL) max_sift = GlobalUtil::_texMaxDimGL; |
| 82 |
if(max_sift > __max_sift) |
| 83 |
{ |
| 84 |
__max_sift = max_sift; |
| 85 |
AllocateSiftMatch(); |
| 86 |
_have_loc[0] = _have_loc[1] = 0; |
| 87 |
_loc_ncomp[0] = _loc_ncomp[1] = 2; |
| 88 |
_id_sift[0] = _id_sift[1] = -1; |
| 89 |
_num_sift[0] = _num_sift[1] = 1; |
| 90 |
}else |
| 91 |
{ |
| 92 |
__max_sift = max_sift; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
void SiftMatchGL::AllocateSiftMatch() |
| 97 |
{ |
| 98 |
//parameters, number of sift is limited by the texture size |
| 99 |
if(__max_sift > GlobalUtil::_texMaxDimGL) __max_sift = GlobalUtil::_texMaxDimGL; |
| 100 |
/// |
| 101 |
int h = __max_sift / _sift_per_row; |
| 102 |
int n = (GlobalUtil::_texMaxDimGL + h - 1) / GlobalUtil::_texMaxDimGL; |
| 103 |
if ( n > 1) {_sift_num_stripe *= n; _sift_per_row *= n; } |
| 104 |
|
| 105 |
//initialize |
| 106 |
|
| 107 |
_texDes[0].InitTexture(_sift_per_row * _pixel_per_sift, __max_sift / _sift_per_row, 0,GL_RGBA8); |
| 108 |
_texDes[1].InitTexture(_sift_per_row * _pixel_per_sift, __max_sift / _sift_per_row, 0, GL_RGBA8); |
| 109 |
// Three texels per feature so that the tangent Sampson shader can store a |
| 110 |
// bearing vector plus its unprojection Jacobian. The two-component mode |
| 111 |
// uses only the first texel of each triple. |
| 112 |
_texLoc[0].InitTexture(_sift_per_row * 3 , __max_sift / _sift_per_row, 0); |
| 113 |
_texLoc[1].InitTexture(_sift_per_row * 3 , __max_sift / _sift_per_row, 0); |
| 114 |
|
| 115 |
if(GlobalUtil::_SupportNVFloat || GlobalUtil::_SupportTextureRG) |
| 116 |
{ |
| 117 |
//use single-component texture to save memory |
| 118 |
#ifndef GL_R32F |
| 119 |
#define GL_R32F 0x822E |
| 120 |
#endif |
| 121 |
GLuint format = GlobalUtil::_SupportNVFloat ? GL_FLOAT_R_NV : GL_R32F; |
| 122 |
_texDot.InitTexture(__max_sift, __max_sift, 0, format); |
| 123 |
_texMatch[0].InitTexture(16, __max_sift / 16, 0, format); |
| 124 |
_texMatch[1].InitTexture(16, __max_sift / 16, 0, format); |
| 125 |
}else |
| 126 |
{ |
| 127 |
_texDot.InitTexture(__max_sift, __max_sift, 0); |
| 128 |
_texMatch[0].InitTexture(16, __max_sift / 16, 0); |
| 129 |
_texMatch[1].InitTexture(16, __max_sift / 16, 0); |
| 130 |
} |
| 131 |
|
| 132 |
} |
| 133 |
void SiftMatchGL::InitSiftMatch() |
| 134 |
{ |
| 135 |
if(_initialized) return; |
| 136 |
GlobalUtil::InitGLParam(0); |
| 137 |
if(GlobalUtil::_GoodOpenGL == 0) return; |
| 138 |
AllocateSiftMatch(); |
| 139 |
LoadSiftMatchShadersGLSL(); |
| 140 |
_initialized = 1; |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
void SiftMatchGL::SetDescriptors(int index, int num, const unsigned char* descriptors, int id) |
| 145 |
{ |
| 146 |
if(_initialized == 0) return; |
| 147 |
if (index > 1) index = 1; |
| 148 |
if (index < 0) index = 0; |
| 149 |
_have_loc[index] = 0; |
| 150 |
|
| 151 |
//the same feature is already set |
| 152 |
if(id !=-1 && id == _id_sift[index]) return ; |
| 153 |
_id_sift[index] = id; |
| 154 |
|
| 155 |
if(num > __max_sift) num = __max_sift; |
| 156 |
|
| 157 |
sift_buffer.resize(num * 128 /4); |
| 158 |
memcpy(&sift_buffer[0], descriptors, 128 * num); |
| 159 |
_num_sift[index] = num; |
| 160 |
int w = _sift_per_row * _pixel_per_sift; |
| 161 |
int h = (num + _sift_per_row - 1)/ _sift_per_row; |
| 162 |
sift_buffer.resize(w * h * 4, 0); |
| 163 |
_texDes[index].SetImageSize(w , h); |
| 164 |
_texDes[index].BindTex(); |
| 165 |
if(_sift_num_stripe == 1) |
| 166 |
{ |
| 167 |
glTexSubImage2D(GlobalUtil::_texTarget, 0, 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, &sift_buffer[0]); |
| 168 |
}else |
| 169 |
{ |
| 170 |
for(int i = 0; i < _sift_num_stripe; ++i) |
| 171 |
{ |
| 172 |
int ws = _sift_per_stripe * _pixel_per_sift; |
| 173 |
int x = i * ws; |
| 174 |
int pos = i * ws * h * 4; |
| 175 |
glTexSubImage2D(GlobalUtil::_texTarget, 0, x, 0, ws, h, GL_RGBA, GL_UNSIGNED_BYTE, &sift_buffer[pos]); |
| 176 |
} |
| 177 |
} |
| 178 |
_texDes[index].UnbindTex(); |
| 179 |
|
| 180 |
} |
| 181 |
|
| 182 |
void SiftMatchGL::SetFeatureLocation(int index, const float* locations, int gap, int ncomp) |
| 183 |
{ |
| 184 |
if(_num_sift[index] <=0) return; |
| 185 |
if(ncomp != 2 && ncomp != 3 && ncomp != 9) ncomp = 2; |
| 186 |
_loc_ncomp[index] = ncomp; |
| 187 |
int w = _sift_per_row ; |
| 188 |
int h = (_num_sift[index] + _sift_per_row - 1)/ _sift_per_row; |
| 189 |
// Every feature occupies three RGBA texels. Mode 2 writes (x, y, 0, 0) into |
| 190 |
// the first and leaves the rest zero; mode 9 fills all nine components with |
| 191 |
// the bearing vector followed by the two Jacobian columns. |
| 192 |
const int w3 = w * 3; |
| 193 |
sift_buffer.resize(_num_sift[index] * 12, 0.0f); |
| 194 |
std::fill(sift_buffer.begin(), sift_buffer.begin() + _num_sift[index] * 12, 0.0f); |
| 195 |
for(int i = 0; i < _num_sift[index]; ++i) |
| 196 |
{ |
| 197 |
float* out_ptr = &sift_buffer[i * 12]; |
| 198 |
for(int k = 0; k < ncomp; ++k) out_ptr[k] = *locations++; |
| 199 |
locations += gap; |
| 200 |
} |
| 201 |
sift_buffer.resize(w3 * h * 4, 0); |
| 202 |
_texLoc[index].SetImageSize(w3 , h); |
| 203 |
_texLoc[index].BindTex(); |
| 204 |
if(_sift_num_stripe == 1) |
| 205 |
{ |
| 206 |
glTexSubImage2D(GlobalUtil::_texTarget, 0, 0, 0, w3, h, GL_RGBA, GL_FLOAT, &sift_buffer[0]); |
| 207 |
}else |
| 208 |
{ |
| 209 |
for(int i = 0; i < _sift_num_stripe; ++i) |
| 210 |
{ |
| 211 |
int ws = _sift_per_stripe * 3; |
| 212 |
int x = i * ws; |
| 213 |
int pos = i * ws * h * 4; |
| 214 |
glTexSubImage2D(GlobalUtil::_texTarget, 0, x, 0, ws, h, GL_RGBA, GL_FLOAT, &sift_buffer[pos]); |
| 215 |
} |
| 216 |
} |
| 217 |
_texLoc[index].UnbindTex(); |
| 218 |
_have_loc[index] = 1; |
| 219 |
} |
| 220 |
|
| 221 |
void SiftMatchGL::SetDescriptors(int index, int num, const float* descriptors, int id) |
| 222 |
{ |
| 223 |
if(_initialized == 0) return; |
| 224 |
if (index > 1) index = 1; |
| 225 |
if (index < 0) index = 0; |
| 226 |
_have_loc[index] = 0; |
| 227 |
|
| 228 |
//the same feature is already set |
| 229 |
if(id !=-1 && id == _id_sift[index]) return ; |
| 230 |
_id_sift[index] = id; |
| 231 |
|
| 232 |
if(num > __max_sift) num = __max_sift; |
| 233 |
|
| 234 |
sift_buffer.resize(num * 128 /4); |
| 235 |
unsigned char * pub = (unsigned char*) &sift_buffer[0]; |
| 236 |
for(int i = 0; i < 128 * num; ++i) |
| 237 |
{ |
| 238 |
pub[i] = int(512 * descriptors[i] + 0.5); |
| 239 |
} |
| 240 |
_num_sift[index] = num; |
| 241 |
int w = _sift_per_row * _pixel_per_sift; |
| 242 |
int h = (num + _sift_per_row - 1)/ _sift_per_row; |
| 243 |
sift_buffer.resize(w * h * 4, 0); |
| 244 |
_texDes[index].SetImageSize(w, h); |
| 245 |
_texDes[index].BindTex(); |
| 246 |
if(_sift_num_stripe == 1) |
| 247 |
{ |
| 248 |
glTexSubImage2D(GlobalUtil::_texTarget, 0, 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, &sift_buffer[0]); |
| 249 |
}else |
| 250 |
{ |
| 251 |
for(int i = 0; i < _sift_num_stripe; ++i) |
| 252 |
{ |
| 253 |
int ws = _sift_per_stripe * _pixel_per_sift; |
| 254 |
int x = i * ws; |
| 255 |
int pos = i * ws * h * 4; |
| 256 |
glTexSubImage2D(GlobalUtil::_texTarget, 0, x, 0, ws, h, GL_RGBA, GL_UNSIGNED_BYTE, &sift_buffer[pos]); |
| 257 |
} |
| 258 |
} |
| 259 |
_texDes[index].UnbindTex(); |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
void SiftMatchGL::LoadSiftMatchShadersGLSL() |
| 264 |
{ |
| 265 |
ProgramGLSL * program; |
| 266 |
ostringstream out; |
| 267 |
if(GlobalUtil::_IsNvidia) |
| 268 |
out << "#pragma optionNV(ifcvt none)\n" |
| 269 |
"#pragma optionNV(unroll all)\n"; |
| 270 |
|
| 271 |
out << "#define SIFT_PER_STRIPE " << _sift_per_stripe << ".0\n" |
| 272 |
"#define PIXEL_PER_SIFT " << _pixel_per_sift << "\n" |
| 273 |
"uniform sampler2DRect tex1, tex2; uniform vec2 size;\n" |
| 274 |
"void main() \n" |
| 275 |
"{\n" |
| 276 |
<< " vec4 val = vec4(0.0, 0.0, 0.0, 0.0), data1, buf;\n" |
| 277 |
" vec2 index = gl_FragCoord.yx; \n" |
| 278 |
" vec2 stripe_size = size.xy * SIFT_PER_STRIPE;\n" |
| 279 |
" vec2 temp_div1 = index / stripe_size;\n" |
| 280 |
" vec2 stripe_index = floor(temp_div1);\n" |
| 281 |
" index = floor(stripe_size * (temp_div1 - stripe_index));\n" |
| 282 |
" vec2 temp_div2 = index * vec2(1.0 / float(SIFT_PER_STRIPE));\n" |
| 283 |
" vec2 temp_floor2 = floor(temp_div2);\n" |
| 284 |
" vec2 index_v = temp_floor2 + vec2(0.5);\n " |
| 285 |
" vec2 index_h = vec2(SIFT_PER_STRIPE)* (temp_div2 - temp_floor2);\n" |
| 286 |
" vec2 tx = (index_h + stripe_index * vec2(SIFT_PER_STRIPE))* vec2(PIXEL_PER_SIFT) + 0.5;\n" |
| 287 |
" vec2 tpos1, tpos2; \n" |
| 288 |
" vec4 tpos = vec4(tx, index_v);\n" |
| 289 |
////////////////////////////////////////////////////// |
| 290 |
" for(int i = 0; i < PIXEL_PER_SIFT; ++i){\n" |
| 291 |
" buf = texture2DRect(tex2, tpos.yw);\n" |
| 292 |
" data1 = texture2DRect(tex1, tpos.xz);\n" |
| 293 |
" val += (data1 * buf);\n" |
| 294 |
" tpos.xy = tpos.xy + vec2(1.0, 1.0);\n" |
| 295 |
" }\n" |
| 296 |
" const float factor = 0.248050689697265625; \n" |
| 297 |
" gl_FragColor =vec4(dot(val, vec4(factor)), index, 0);\n" |
| 298 |
"}" |
| 299 |
<< '\0'; |
| 300 |
|
| 301 |
s_multiply = program= new ProgramGLSL(out.str().c_str()); |
| 302 |
|
| 303 |
_param_multiply_tex1 = glGetUniformLocation(*program, "tex1"); |
| 304 |
_param_multiply_tex2 = glGetUniformLocation(*program, "tex2"); |
| 305 |
_param_multiply_size = glGetUniformLocation(*program, "size"); |
| 306 |
|
| 307 |
out.seekp(ios::beg); |
| 308 |
if(GlobalUtil::_IsNvidia) |
| 309 |
out << "#pragma optionNV(ifcvt none)\n" |
| 310 |
"#pragma optionNV(unroll all)\n"; |
| 311 |
|
| 312 |
out << "#define SIFT_PER_STRIPE " << _sift_per_stripe << ".0\n" |
| 313 |
"#define PIXEL_PER_SIFT " << _pixel_per_sift << "\n" |
| 314 |
"uniform sampler2DRect tex1, tex2;\n" |
| 315 |
"uniform sampler2DRect texL1;\n" |
| 316 |
"uniform sampler2DRect texL2; \n" |
| 317 |
"uniform mat3 H; \n" |
| 318 |
"uniform mat3 F; \n" |
| 319 |
"uniform vec4 size; \n" |
| 320 |
"void main() \n" |
| 321 |
"{\n" |
| 322 |
<< " vec4 val = vec4(0.0, 0.0, 0.0, 0.0), data1, buf;\n" |
| 323 |
" vec2 index = gl_FragCoord.yx; \n" |
| 324 |
" vec2 stripe_size = size.xy * SIFT_PER_STRIPE;\n" |
| 325 |
" vec2 temp_div1 = index / stripe_size;\n" |
| 326 |
" vec2 stripe_index = floor(temp_div1);\n" |
| 327 |
" index = floor(stripe_size * (temp_div1 - stripe_index));\n" |
| 328 |
" vec2 temp_div2 = index * vec2(1.0/ float(SIFT_PER_STRIPE));\n" |
| 329 |
" vec2 temp_floor2 = floor(temp_div2);\n" |
| 330 |
" vec2 index_v = temp_floor2 + vec2(0.5);\n " |
| 331 |
" vec2 index_h = vec2(SIFT_PER_STRIPE)* (temp_div2 - temp_floor2);\n" |
| 332 |
|
| 333 |
//read feature location data; three texels per feature, the image |
| 334 |
//plane point lives in .xy of the first one |
| 335 |
" vec4 tlpos = vec4((index_h + stripe_index * vec2(SIFT_PER_STRIPE)) * 3.0 + 0.5, index_v);\n" |
| 336 |
" vec3 loc1 = vec3(texture2DRect(texL1, tlpos.xz).xy, 1.0);\n" |
| 337 |
" vec3 loc2 = vec3(texture2DRect(texL2, tlpos.yw).xy, 1.0);\n" |
| 338 |
|
| 339 |
//check the guiding homography |
| 340 |
" vec3 hxloc1 = H* loc1;\n" |
| 341 |
" vec2 diff = loc2.xy- (hxloc1.xy/hxloc1.z);\n" |
| 342 |
" float disth = diff.x * diff.x + diff.y * diff.y;\n" |
| 343 |
" if(size.z >= 0.0 && disth > size.z) {gl_FragColor = vec4(0.0, index, 0.0); return;}\n" |
| 344 |
|
| 345 |
//check the guiding fundamental |
| 346 |
" vec3 fx1 = (F * loc1), ftx2 = (loc2 * F);\n" |
| 347 |
" float x2tfx1 = dot(loc2, fx1);\n" |
| 348 |
" vec4 temp = vec4(fx1.xy, ftx2.xy); \n" |
| 349 |
" float sampson_error = (x2tfx1 * x2tfx1) / dot(temp, temp);\n" |
| 350 |
" if(size.w >= 0.0 && sampson_error > size.w) {gl_FragColor = vec4(0.0, index, 0.0); return;}\n" |
| 351 |
|
| 352 |
//compare feature descriptor |
| 353 |
" vec2 tx = (index_h + stripe_index * SIFT_PER_STRIPE)* vec2(PIXEL_PER_SIFT) + 0.5;\n" |
| 354 |
" vec2 tpos1, tpos2; \n" |
| 355 |
" vec4 tpos = vec4(tx, index_v);\n" |
| 356 |
" for(int i = 0; i < PIXEL_PER_SIFT; ++i){\n" |
| 357 |
" buf = texture2DRect(tex2, tpos.yw);\n" |
| 358 |
" data1 = texture2DRect(tex1, tpos.xz);\n" |
| 359 |
" val += data1 * buf;\n" |
| 360 |
" tpos.xy = tpos.xy + vec2(1.0, 1.0);\n" |
| 361 |
" }\n" |
| 362 |
" const float factor = 0.248050689697265625; \n" |
| 363 |
" gl_FragColor =vec4(dot(val, vec4(factor)), index, 0.0);\n" |
| 364 |
"}" |
| 365 |
<< '\0'; |
| 366 |
|
| 367 |
s_guided_mult = program= new ProgramGLSL(out.str().c_str()); |
| 368 |
|
| 369 |
_param_guided_mult_tex1 = glGetUniformLocation(*program, "tex1"); |
| 370 |
_param_guided_mult_tex2= glGetUniformLocation(*program, "tex2"); |
| 371 |
_param_guided_mult_texl1 = glGetUniformLocation(*program, "texL1"); |
| 372 |
_param_guided_mult_texl2 = glGetUniformLocation(*program, "texL2"); |
| 373 |
_param_guided_mult_h = glGetUniformLocation(*program, "H"); |
| 374 |
_param_guided_mult_f = glGetUniformLocation(*program, "F"); |
| 375 |
_param_guided_mult_param = glGetUniformLocation(*program, "size"); |
| 376 |
|
| 377 |
out.seekp(ios::beg); |
| 378 |
if(GlobalUtil::_IsNvidia) |
| 379 |
out << "#pragma optionNV(ifcvt none)\n" |
| 380 |
"#pragma optionNV(unroll all)\n"; |
| 381 |
|
| 382 |
out << "#define SIFT_PER_STRIPE " << _sift_per_stripe << ".0\n" |
| 383 |
"#define PIXEL_PER_SIFT " << _pixel_per_sift << "\n" |
| 384 |
"uniform sampler2DRect tex1, tex2;\n" |
| 385 |
"uniform sampler2DRect texL1;\n" |
| 386 |
"uniform sampler2DRect texL2; \n" |
| 387 |
"uniform mat3 E; \n" |
| 388 |
"uniform vec4 size; \n" |
| 389 |
"void main() \n" |
| 390 |
"{\n" |
| 391 |
<< " vec4 val = vec4(0.0, 0.0, 0.0, 0.0), data1, buf;\n" |
| 392 |
" vec2 index = gl_FragCoord.yx; \n" |
| 393 |
" vec2 stripe_size = size.xy * SIFT_PER_STRIPE;\n" |
| 394 |
" vec2 temp_div1 = index / stripe_size;\n" |
| 395 |
" vec2 stripe_index = floor(temp_div1);\n" |
| 396 |
" index = floor(stripe_size * (temp_div1 - stripe_index));\n" |
| 397 |
" vec2 temp_div2 = index * vec2(1.0/ float(SIFT_PER_STRIPE));\n" |
| 398 |
" vec2 temp_floor2 = floor(temp_div2);\n" |
| 399 |
" vec2 index_v = temp_floor2 + vec2(0.5);\n " |
| 400 |
" vec2 index_h = vec2(SIFT_PER_STRIPE)* (temp_div2 - temp_floor2);\n" |
| 401 |
|
| 402 |
//three texels per feature: bearing, then the two Jacobian columns |
| 403 |
" vec4 tlpos = vec4((index_h + stripe_index * vec2(SIFT_PER_STRIPE)) * 3.0 + 0.5, index_v);\n" |
| 404 |
" vec4 a1 = texture2DRect(texL1, tlpos.xz);\n" |
| 405 |
" vec4 b1 = texture2DRect(texL1, vec2(tlpos.x + 1.0, tlpos.z));\n" |
| 406 |
" vec4 c1 = texture2DRect(texL1, vec2(tlpos.x + 2.0, tlpos.z));\n" |
| 407 |
" vec4 a2 = texture2DRect(texL2, tlpos.yw);\n" |
| 408 |
" vec4 b2 = texture2DRect(texL2, vec2(tlpos.y + 1.0, tlpos.w));\n" |
| 409 |
" vec4 c2 = texture2DRect(texL2, vec2(tlpos.y + 2.0, tlpos.w));\n" |
| 410 |
" vec3 r1 = a1.xyz, r2 = a2.xyz;\n" |
| 411 |
" vec3 J1c0 = vec3(a1.w, b1.xy), J1c1 = vec3(b1.zw, c1.x);\n" |
| 412 |
" vec3 J2c0 = vec3(a2.w, b2.xy), J2c1 = vec3(b2.zw, c2.x);\n" |
| 413 |
|
| 414 |
//tangent Sampson error, in pixels |
| 415 |
" vec3 er1 = E * r1;\n" |
| 416 |
" vec3 etr2 = r2 * E;\n" |
| 417 |
" float num = dot(r2, er1);\n" |
| 418 |
" vec4 grad = vec4(dot(J1c0, etr2), dot(J1c1, etr2),\n" |
| 419 |
" dot(J2c0, er1), dot(J2c1, er1));\n" |
| 420 |
" float denom = dot(grad, grad);\n" |
| 421 |
" if(denom <= 0.0 || num * num > size.w * denom)\n" |
| 422 |
" {gl_FragColor = vec4(0.0, index, 0.0); return;}\n" |
| 423 |
|
| 424 |
//compare feature descriptor |
| 425 |
" vec2 tx = (index_h + stripe_index * SIFT_PER_STRIPE)* vec2(PIXEL_PER_SIFT) + 0.5;\n" |
| 426 |
" vec2 tpos1, tpos2; \n" |
| 427 |
" vec4 tpos = vec4(tx, index_v);\n" |
| 428 |
" for(int i = 0; i < PIXEL_PER_SIFT; ++i){\n" |
| 429 |
" buf = texture2DRect(tex2, tpos.yw);\n" |
| 430 |
" data1 = texture2DRect(tex1, tpos.xz);\n" |
| 431 |
" val += data1 * buf;\n" |
| 432 |
" tpos.xy = tpos.xy + vec2(1.0, 1.0);\n" |
| 433 |
" }\n" |
| 434 |
" const float factor = 0.248050689697265625; \n" |
| 435 |
" gl_FragColor =vec4(dot(val, vec4(factor)), index, 0.0);\n" |
| 436 |
"}" |
| 437 |
<< '\0'; |
| 438 |
|
| 439 |
s_guided_mult_ray = program= new ProgramGLSL(out.str().c_str()); |
| 440 |
|
| 441 |
_param_guided_mult_ray_tex1 = glGetUniformLocation(*program, "tex1"); |
| 442 |
_param_guided_mult_ray_tex2= glGetUniformLocation(*program, "tex2"); |
| 443 |
_param_guided_mult_ray_texl1 = glGetUniformLocation(*program, "texL1"); |
| 444 |
_param_guided_mult_ray_texl2 = glGetUniformLocation(*program, "texL2"); |
| 445 |
_param_guided_mult_ray_e = glGetUniformLocation(*program, "E"); |
| 446 |
_param_guided_mult_ray_param = glGetUniformLocation(*program, "size"); |
| 447 |
|
| 448 |
//row max |
| 449 |
out.seekp(ios::beg); |
| 450 |
out << "#define BLOCK_WIDTH 16.0\n" |
| 451 |
"uniform sampler2DRect tex; uniform vec3 param;\n" |
| 452 |
"void main ()\n" |
| 453 |
"{\n" |
| 454 |
" float index = gl_FragCoord.x + floor(gl_FragCoord.y) * BLOCK_WIDTH; \n" |
| 455 |
" vec2 bestv = vec2(-1.0); float imax = -1.0;\n" |
| 456 |
" for(float i = 0.0; i < param.x; i ++){\n " |
| 457 |
" float v = texture2DRect(tex, vec2(i + 0.5, index)).r; \n" |
| 458 |
" imax = v > bestv.r ? i : imax; \n " |
| 459 |
" bestv = v > bestv.r? vec2(v, bestv.r) : max(bestv, vec2(v));\n " |
| 460 |
" }\n" |
| 461 |
" bestv = acos(min(bestv, 1.0));\n" |
| 462 |
" if(bestv.x >= param.y || bestv.x >= param.z * bestv.y) imax = -1.0;\n" |
| 463 |
" gl_FragColor = vec4(imax, bestv, index);\n" |
| 464 |
"}" |
| 465 |
<< '\0'; |
| 466 |
s_row_max = program= new ProgramGLSL(out.str().c_str()); |
| 467 |
_param_rowmax_param = glGetUniformLocation(*program, "param"); |
| 468 |
|
| 469 |
out.seekp(ios::beg); |
| 470 |
out << "#define BLOCK_WIDTH 16.0\n" |
| 471 |
"uniform sampler2DRect tex; uniform vec3 param;\n" |
| 472 |
"void main ()\n" |
| 473 |
"{\n" |
| 474 |
" float index = gl_FragCoord.x + floor(gl_FragCoord.y) * BLOCK_WIDTH; \n" |
| 475 |
" vec2 bestv = vec2(-1.0); float imax = -1.0;\n" |
| 476 |
" for(float i = 0.0; i < param.x; i ++){\n " |
| 477 |
" float v = texture2DRect(tex, vec2(index, i + 0.5)).r; \n" |
| 478 |
" imax = (v > bestv.r)? i : imax; \n " |
| 479 |
" bestv = v > bestv.r? vec2(v, bestv.r) : max(bestv, vec2(v));\n " |
| 480 |
" }\n" |
| 481 |
" bestv = acos(min(bestv, 1.0));\n" |
| 482 |
" if(bestv.x >= param.y || bestv.x >= param.z * bestv.y) imax = -1.0;\n" |
| 483 |
" gl_FragColor = vec4(imax, bestv, index);\n" |
| 484 |
"}" |
| 485 |
<< '\0'; |
| 486 |
s_col_max = program =new ProgramGLSL(out.str().c_str()); |
| 487 |
_param_colmax_param = glGetUniformLocation(*program, "param"); |
| 488 |
|
| 489 |
|
| 490 |
} |
| 491 |
|
| 492 |
int SiftMatchGL::GetGuidedSiftMatch(int max_match, uint32_t match_buffer[][2], float* H, float* F, |
| 493 |
float distmax, float ratiomax, float hdistmax, float fdistmax, int mbm, |
| 494 |
int use_h, int use_f) |
| 495 |
{ |
| 496 |
|
| 497 |
int dw = _num_sift[1]; |
| 498 |
int dh = _num_sift[0]; |
| 499 |
if(_initialized ==0) return 0; |
| 500 |
if(dw <= 0 || dh <=0) return 0; |
| 501 |
if(_have_loc[0] == 0 || _have_loc[1] == 0) return 0; |
| 502 |
|
| 503 |
FrameBufferObject fbo; |
| 504 |
glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT); |
| 505 |
_texDot.SetImageSize(dw, dh); |
| 506 |
|
| 507 |
|
| 508 |
//data |
| 509 |
_texDot.AttachToFBO(0); |
| 510 |
_texDot.FitTexViewPort(); |
| 511 |
glActiveTexture(GL_TEXTURE0); |
| 512 |
_texDes[0].BindTex(); |
| 513 |
glActiveTexture(GL_TEXTURE1); |
| 514 |
_texDes[1].BindTex(); |
| 515 |
glActiveTexture(GL_TEXTURE2); |
| 516 |
_texLoc[0].BindTex(); |
| 517 |
glActiveTexture(GL_TEXTURE3); |
| 518 |
_texLoc[1].BindTex(); |
| 519 |
|
| 520 |
//multiply the descriptor matrices |
| 521 |
if(_loc_ncomp[0] == 9 && _loc_ncomp[1] == 9) |
| 522 |
{ |
| 523 |
// Bearing vectors with Jacobians: score the essential matrix in pixels. |
| 524 |
// This layout carries no image plane points, so there is no homography |
| 525 |
// branch and F is interpreted as E. |
| 526 |
s_guided_mult_ray->UseProgram(); |
| 527 |
float dot_param[4] = {(float)_texDes[0].GetDrawHeight(), (float) _texDes[1].GetDrawHeight(), 0.0f, fdistmax}; |
| 528 |
glUniform1i(_param_guided_mult_ray_tex1, 0); |
| 529 |
glUniform1i(_param_guided_mult_ray_tex2, 1); |
| 530 |
glUniform1i(_param_guided_mult_ray_texl1, 2); |
| 531 |
glUniform1i(_param_guided_mult_ray_texl2, 3); |
| 532 |
glUniformMatrix3fv(_param_guided_mult_ray_e, 1, GL_TRUE, F); |
| 533 |
glUniform4fv(_param_guided_mult_ray_param, 1, dot_param); |
| 534 |
} |
| 535 |
else |
| 536 |
{ |
| 537 |
s_guided_mult->UseProgram(); |
| 538 |
|
| 539 |
|
| 540 |
//set parameters glsl |
| 541 |
// A negative threshold disables the model (the shader guards with size >= 0), |
| 542 |
// matching the CUDA use_h/use_f short-circuit instead of an inf sentinel that |
| 543 |
// would spuriously reject on an inf/NaN residual. |
| 544 |
if(!use_h) hdistmax = -1.0f; |
| 545 |
if(!use_f) fdistmax = -1.0f; |
| 546 |
float dot_param[4] = {(float)_texDes[0].GetDrawHeight(), (float) _texDes[1].GetDrawHeight(), hdistmax, fdistmax}; |
| 547 |
glUniform1i(_param_guided_mult_tex1, 0); |
| 548 |
glUniform1i(_param_guided_mult_tex2, 1); |
| 549 |
glUniform1i(_param_guided_mult_texl1, 2); |
| 550 |
glUniform1i(_param_guided_mult_texl2, 3); |
| 551 |
glUniformMatrix3fv(_param_guided_mult_h, 1, GL_TRUE, H); |
| 552 |
glUniformMatrix3fv(_param_guided_mult_f, 1, GL_TRUE, F); |
| 553 |
glUniform4fv(_param_guided_mult_param, 1, dot_param); |
| 554 |
} |
| 555 |
|
| 556 |
_texDot.DrawQuad(); |
| 557 |
|
| 558 |
GLTexImage::UnbindMultiTex(4); |
| 559 |
|
| 560 |
return GetBestMatch(max_match, match_buffer, distmax, ratiomax, mbm); |
| 561 |
} |
| 562 |
|
| 563 |
int SiftMatchGL::GetBestMatch(int max_match, uint32_t match_buffer[][2], float distmax, float ratiomax, int mbm) |
| 564 |
{ |
| 565 |
|
| 566 |
glActiveTexture(GL_TEXTURE0); |
| 567 |
_texDot.BindTex(); |
| 568 |
|
| 569 |
//readback buffer |
| 570 |
sift_buffer.resize(_num_sift[0] + _num_sift[1] + 16); |
| 571 |
float * buffer1 = &sift_buffer[0], * buffer2 = &sift_buffer[_num_sift[0]]; |
| 572 |
|
| 573 |
//row max |
| 574 |
_texMatch[0].AttachToFBO(0); |
| 575 |
_texMatch[0].SetImageSize(16, ( _num_sift[0] + 15) / 16); |
| 576 |
_texMatch[0].FitTexViewPort(); |
| 577 |
|
| 578 |
///set parameter glsl |
| 579 |
s_row_max->UseProgram(); |
| 580 |
glUniform3f(_param_rowmax_param, (float)_num_sift[1], distmax, ratiomax); |
| 581 |
|
| 582 |
_texMatch[0].DrawQuad(); |
| 583 |
glReadPixels(0, 0, 16, (_num_sift[0] + 15)/16, GL_RED, GL_FLOAT, buffer1); |
| 584 |
|
| 585 |
//col max |
| 586 |
if(mbm) |
| 587 |
{ |
| 588 |
_texMatch[1].AttachToFBO(0); |
| 589 |
_texMatch[1].SetImageSize(16, (_num_sift[1] + 15) / 16); |
| 590 |
_texMatch[1].FitTexViewPort(); |
| 591 |
//set parameter glsl |
| 592 |
s_col_max->UseProgram(); |
| 593 |
glUniform3f(_param_rowmax_param, (float)_num_sift[0], distmax, ratiomax); |
| 594 |
_texMatch[1].DrawQuad(); |
| 595 |
glReadPixels(0, 0, 16, (_num_sift[1] + 15) / 16, GL_RED, GL_FLOAT, buffer2); |
| 596 |
} |
| 597 |
|
| 598 |
|
| 599 |
//unload |
| 600 |
glUseProgram(0); |
| 601 |
|
| 602 |
GLTexImage::UnbindMultiTex(2); |
| 603 |
GlobalUtil::CleanupOpenGL(); |
| 604 |
|
| 605 |
//write back the matches |
| 606 |
int nmatch = 0, j ; |
| 607 |
for(int i = 0; i < _num_sift[0] && nmatch < max_match; ++i) |
| 608 |
{ |
| 609 |
j = int(buffer1[i]); |
| 610 |
if( j>= 0 && (!mbm ||int(buffer2[j]) == i)) |
| 611 |
{ |
| 612 |
match_buffer[nmatch][0] = i; |
| 613 |
match_buffer[nmatch][1] = j; |
| 614 |
nmatch++; |
| 615 |
} |
| 616 |
} |
| 617 |
|
| 618 |
const GLenum error_code(glGetError()); |
| 619 |
if (error_code != GL_NO_ERROR) { |
| 620 |
return -1; |
| 621 |
} |
| 622 |
|
| 623 |
return nmatch; |
| 624 |
} |
| 625 |
|
| 626 |
int SiftMatchGL::GetSiftMatch(int max_match, uint32_t match_buffer[][2], float distmax, float ratiomax, int mbm) |
| 627 |
{ |
| 628 |
int dw = _num_sift[1]; |
| 629 |
int dh = _num_sift[0]; |
| 630 |
if(_initialized ==0) return 0; |
| 631 |
if(dw <= 0 || dh <=0) return 0; |
| 632 |
|
| 633 |
FrameBufferObject fbo; |
| 634 |
glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT); |
| 635 |
_texDot.SetImageSize(dw, dh); |
| 636 |
|
| 637 |
//data |
| 638 |
_texDot.AttachToFBO(0); |
| 639 |
_texDot.FitTexViewPort(); |
| 640 |
glActiveTexture(GL_TEXTURE0); |
| 641 |
_texDes[0].BindTex(); |
| 642 |
glActiveTexture(GL_TEXTURE1); |
| 643 |
_texDes[1].BindTex(); |
| 644 |
|
| 645 |
////////////////// |
| 646 |
//multiply the descriptor matrices |
| 647 |
s_multiply->UseProgram(); |
| 648 |
//set parameters |
| 649 |
float heights[2] = {(float)_texDes[0].GetDrawHeight(), (float)_texDes[1].GetDrawHeight()}; |
| 650 |
|
| 651 |
glUniform1i(_param_multiply_tex1, 0); |
| 652 |
glUniform1i(_param_multiply_tex2 , 1); |
| 653 |
glUniform2fv(_param_multiply_size, 1, heights); |
| 654 |
|
| 655 |
_texDot.DrawQuad(); |
| 656 |
|
| 657 |
glActiveTexture(GL_TEXTURE1); |
| 658 |
glBindTexture(GlobalUtil::_texTarget, 0); |
| 659 |
|
| 660 |
return GetBestMatch(max_match, match_buffer, distmax, ratiomax, mbm); |
| 661 |
} |
| 662 |
|
| 663 |
|
| 664 |
int SiftMatchGPU::_CreateContextGL() |
| 665 |
{ |
| 666 |
//Create an OpenGL Context? |
| 667 |
if (__language >= SIFTMATCH_CUDA) {} |
| 668 |
else if(!GlobalUtil::CreateWindowEZ()) |
| 669 |
{ |
| 670 |
#if SIFTGPU_CUDA_ENABLED |
| 671 |
__language = SIFTMATCH_CUDA; |
| 672 |
#else |
| 673 |
return 0; |
| 674 |
#endif |
| 675 |
} |
| 676 |
return VerifyContextGL(); |
| 677 |
} |
| 678 |
|
| 679 |
|
| 680 |
int SiftMatchGPU::_VerifyContextGL() |
| 681 |
{ |
| 682 |
if(__matcher) return GlobalUtil::_GoodOpenGL; |
| 683 |
|
| 684 |
#ifdef SIFTGPU_CUDA_ENABLED |
| 685 |
|
| 686 |
if(__language >= SIFTMATCH_CUDA) {} |
| 687 |
else if(__language == SIFTMATCH_SAME_AS_SIFTGPU && GlobalUtil::_UseCUDA){} |
| 688 |
else GlobalUtil::InitGLParam(0); |
| 689 |
if(GlobalUtil::_GoodOpenGL == 0) __language = SIFTMATCH_CUDA; |
| 690 |
|
| 691 |
if(((__language == SIFTMATCH_SAME_AS_SIFTGPU && GlobalUtil::_UseCUDA) || __language >= SIFTMATCH_CUDA) |
| 692 |
&& SiftMatchCU::CheckCudaDevice (GlobalUtil::_DeviceIndex)) |
| 693 |
{ |
| 694 |
__language = SIFTMATCH_CUDA; |
| 695 |
__matcher = ::new SiftMatchCU(__max_sift); |
| 696 |
}else |
| 697 |
#else |
| 698 |
if((__language == SIFTMATCH_SAME_AS_SIFTGPU && GlobalUtil::_UseCUDA) || __language >= SIFTMATCH_CUDA) |
| 699 |
{ |
| 700 |
std::cerr << "---------------------------------------------------------------------------\n" |
| 701 |
<< "CUDA not supported in this binary! To enable it, please use SiftGPU_CUDA_Enable\n" |
| 702 |
<< "Project for VS2005+ or set siftgpu_enable_cuda to 1 in makefile\n" |
| 703 |
<< "----------------------------------------------------------------------------\n"; |
| 704 |
} |
| 705 |
#endif |
| 706 |
{ |
| 707 |
__language = SIFTMATCH_GLSL; |
| 708 |
__matcher = ::new SiftMatchGL(__max_sift, 1); |
| 709 |
} |
| 710 |
|
| 711 |
if(GlobalUtil::_verbose) |
| 712 |
std::cout << "[SiftMatchGPU]: " << (__language == SIFTMATCH_CUDA? "CUDA" : "GLSL") <<"\n\n"; |
| 713 |
|
| 714 |
__matcher->InitSiftMatch(); |
| 715 |
return GlobalUtil::_GoodOpenGL; |
| 716 |
} |
| 717 |
|
| 718 |
SiftMatchGPU::SiftMatchGPU(int max_sift) |
| 719 |
{ |
| 720 |
__max_sift = max(max_sift, 1024); |
| 721 |
__language = 0; |
| 722 |
__matcher = NULL; |
| 723 |
} |
| 724 |
|
| 725 |
void SiftMatchGPU::SetLanguage(int language) |
| 726 |
{ |
| 727 |
if(__matcher) return; |
| 728 |
//////////////////////// |
| 729 |
#ifdef SIFTGPU_CUDA_ENABLED |
| 730 |
if(language >= SIFTMATCH_CUDA) GlobalUtil::_DeviceIndex = language - SIFTMATCH_CUDA; |
| 731 |
#endif |
| 732 |
__language = language > SIFTMATCH_CUDA ? SIFTMATCH_CUDA : language; |
| 733 |
} |
| 734 |
|
| 735 |
void SiftMatchGPU::SetDeviceParam(int argc, char**argv) |
| 736 |
{ |
| 737 |
if(__matcher) return; |
| 738 |
GlobalUtil::SetDeviceParam(argc, argv); |
| 739 |
} |
| 740 |
|
| 741 |
bool SiftMatchGPU::Allocate(int max_sift, int mbm) { |
| 742 |
if(__matcher) { |
| 743 |
const bool success = __matcher->Allocate(max_sift, mbm); |
| 744 |
__max_sift = __matcher->__max_sift; |
| 745 |
return success; |
| 746 |
} |
| 747 |
|
| 748 |
return false; |
| 749 |
} |
| 750 |
|
| 751 |
void SiftMatchGPU::SetMaxSift(int max_sift) |
| 752 |
{ |
| 753 |
if(__matcher) { |
| 754 |
__matcher->SetMaxSift(max(128, max_sift)); |
| 755 |
__max_sift = __matcher->__max_sift; |
| 756 |
} else { |
| 757 |
__max_sift = max(128, max_sift); |
| 758 |
} |
| 759 |
} |
| 760 |
|
| 761 |
SiftMatchGPU::~SiftMatchGPU() |
| 762 |
{ |
| 763 |
if(__matcher) delete __matcher; |
| 764 |
} |
| 765 |
|
| 766 |
void SiftMatchGPU::SetDescriptors(int index, int num, const unsigned char* descriptors, int id) |
| 767 |
{ |
| 768 |
__matcher->SetDescriptors(index, num, descriptors, id); |
| 769 |
} |
| 770 |
|
| 771 |
void SiftMatchGPU::SetDescriptors(int index, int num, const float* descriptors, int id) |
| 772 |
{ |
| 773 |
__matcher->SetDescriptors(index, num, descriptors, id); |
| 774 |
} |
| 775 |
|
| 776 |
void SiftMatchGPU::SetFeatureLocation(int index, const float* locations, int gap, int ncomp) |
| 777 |
{ |
| 778 |
__matcher->SetFeatureLocation(index, locations, gap, ncomp); |
| 779 |
|
| 780 |
} |
| 781 |
int SiftMatchGPU::GetGuidedSiftMatch(int max_match, uint32_t match_buffer[][2], float* H, float* F, |
| 782 |
float distmax, float ratiomax, float hdistmax, float fdistmax, int mutual_best_match, |
| 783 |
int use_h, int use_f) |
| 784 |
{ |
| 785 |
if(H == NULL && F == NULL) |
| 786 |
{ |
| 787 |
return __matcher->GetSiftMatch(max_match, match_buffer, distmax, ratiomax, mutual_best_match); |
| 788 |
}else |
| 789 |
{ |
| 790 |
float Z[9] = {1, 0, 0, 0, 1, 0, 0, 0, 1}, ti = (1.0e+20F); |
| 791 |
|
| 792 |
// The unused model is still handed an identity matrix so the kernel has |
| 793 |
// well-defined inputs, but it is disabled through an explicit flag |
| 794 |
// rather than through a threshold large enough to swallow it. |
| 795 |
return __matcher->GetGuidedSiftMatch(max_match, match_buffer, H? H : Z, F? F : Z, |
| 796 |
distmax, ratiomax, H? hdistmax: ti, F? fdistmax: ti, mutual_best_match, |
| 797 |
H != NULL, F != NULL); |
| 798 |
} |
| 799 |
} |
| 800 |
|
| 801 |
int SiftMatchGPU::GetSiftMatch(int max_match, uint32_t match_buffer[][2], float distmax, float ratiomax, int mutual_best_match) |
| 802 |
{ |
| 803 |
return __matcher->GetSiftMatch(max_match, match_buffer, distmax, ratiomax, mutual_best_match); |
| 804 |
} |
| 805 |
|
| 806 |
SiftMatchGPU* CreateNewSiftMatchGPU(int max_sift) |
| 807 |
{ |
| 808 |
return new SiftMatchGPU(max_sift); |
| 809 |
} |
| 810 |
|