📄 programglsl.cpp
字号:
"uniform sampler2DRect tex; void main(){\n"
"vec4 pc = texture2DRect(tex, gl_TexCoord[0].xy); bvec2 ff = lessThan(fract(gl_TexCoord[0].xy), vec2(0.5));\n"
"float v = ff.y ?(ff.x ? pc.r : pc.g):(ff.x ? pc.b : pc.a);float g = (0.5+20.0*v);\n"
"gl_FragColor = vec4(g, g, g, 1.0);}" );
s_display_grad = new ProgramGLSL(
"uniform sampler2DRect tex; void main(){\n"
"vec4 pc = texture2DRect(tex, gl_TexCoord[0].xy); bvec2 ff = lessThan(fract(gl_TexCoord[0].xy), vec2(0.5));\n"
"float v = ff.y ?(ff.x ? pc.r : pc.g):(ff.x ? pc.b : pc.a); gl_FragColor = vec4(5.0 *vec3(v), 1.0); }");
s_display_keys= new ProgramGLSL(
"uniform sampler2DRect tex; void main(){\n"
"vec4 oc = texture2DRect(tex, gl_TexCoord[0].xy); \n"
"vec4 cc = vec4(equal(abs(oc.rrrr), vec4(1.0, 2.0, 3.0, 4.0))); \n"
"bvec2 ff = lessThan(fract(gl_TexCoord[0].xy) , vec2(0.5));\n"
"float v = ff.y ?(ff.x ? cc.r : cc.g):(ff.x ? cc.b : cc.a);\n"
"if(v == 0) discard; \n"
"else if(oc.r > 0) gl_FragColor = vec4(1.0, 0, 0,1.0); \n"
"else gl_FragColor = vec4(0.0,1.0,0.0,1.0); }" );
}
void ShaderBagPKSL::LoadOrientationShader(void)
{
char buffer[10240];
ostrstream out(buffer,10240);
if(GlobalUtil::_IsNvidia)
{
out << "#pragma optionNV(ifcvt none)\n"
"#pragma optionNV(unroll all)\n";
}
out<<"\n"
"#define GAUSSIAN_WF "<<GlobalUtil::_OrientationGaussianFactor<<" \n"
"#define SAMPLE_WF ("<<GlobalUtil::_OrientationWindowFactor<< " )\n"
"#define ORIENTATION_THRESHOLD "<< GlobalUtil::_MulitiOrientationThreshold << "\n"
"uniform sampler2DRect tex; uniform sampler2DRect gtex;\n"
"uniform sampler2DRect otex; uniform vec4 size;\n"
"void main() \n"
"{ \n"
" vec4 bins[10]; \n"
" bins[0] = vec4(0.0);bins[1] = vec4(0.0);bins[2] = vec4(0.0); \n"
" bins[3] = vec4(0.0);bins[4] = vec4(0.0);bins[5] = vec4(0.0); \n"
" bins[6] = vec4(0.0);bins[7] = vec4(0.0);bins[8] = vec4(0.0); \n"
" vec4 sift = texture2DRect(tex, gl_TexCoord[0].xy); \n"
" vec2 pos = sift.xy; \n"
" bool orientation_mode = (size.z != 0); \n"
" float sigma = orientation_mode? (abs(size.z) * pow(size.w, sift.w) * sift.z) : (sift.w); \n"
" //bool fixed_orientation = (size.z < 0); \n"
" if(size.z < 0) {gl_FragData[0] = vec4(pos, 0, sigma); return;}"
" float gsigma = sigma * GAUSSIAN_WF; \n"
" vec2 win = abs(vec2(sigma)) * (SAMPLE_WF * GAUSSIAN_WF); \n"
" vec2 dim = size.xy; \n"
" vec4 dist_threshold = vec4(win.x*win.x+0.5); \n"
" float factor = -0.5/(gsigma*gsigma); \n"
" vec4 sz; vec2 spos; \n"
" //if(any(pos.xy <= 1)) discard; \n"
" sz.xy = max( pos - win, vec2(2,2)); \n"
" sz.zw = min( pos + win, dim-3); \n"
" sz = floor(sz*0.5) + 0.5; ";
//loop to get the histogram
out<<"\n"
" for(spos.y = sz.y; spos.y <= sz.w; spos.y+=1.0) \n"
" { \n"
" for(spos.x = sz.x; spos.x <= sz.z; spos.x+=1.0) \n"
" { \n"
" vec2 offset = 2* spos - pos - 0.5; \n"
" vec4 off = vec4(offset, offset + 1); \n"
" vec4 distsq = off.xzxz * off.xzxz + off.yyww * off.yyww; \n"
" bvec4 inside = lessThan(distsq, dist_threshold); \n"
" if(any(inside)) \n"
" { \n"
" vec4 gg = texture2DRect(gtex, spos); \n"
" vec4 oo = texture2DRect(otex, spos); \n"
" vec4 weight = gg * exp(distsq * factor); \n"
" vec4 idxv = floor(degrees(oo)*0.1); \n"
" idxv+= (vec4(lessThan(idxv, vec4(0.0)))*36.0); \n"
" vec4 vidx = fract(idxv * 0.25) * 4.0;//mod(idxv, 4.0); \n";
//
if(GlobalUtil::_UseDynamicIndexing && GlobalUtil::_IsNvidia)
{
// it might be slow on some GPUs
out<<"\n"
" for(int i = 0 ; i < 4; i++)\n"
" {\n"
" if(inside[i])\n"
" {\n"
" float idx = idxv[i]; \n"
" vec4 inc = weight[i] * vec4(equal(vec4(vidx[i]), vec4(0,1,2,3))); \n"
" int iidx = int(floor(idx*0.25)); \n"
" bins[iidx]+=inc; \n"
" } \n"
" } \n"
" } \n"
" } \n"
" }";
}else
{
//nvfp40 still does not support dynamic array indexing
//unrolled binary search
//it seems to be faster than the dyanmic indexing version on some GPUs
out<<"\n"
" for(int i = 0 ; i < 4; i++)\n"
" {\n"
" if(inside[i])\n"
" {\n"
" float idx = idxv[i]; \n"
" vec4 inc = weight[i] * vec4(equal(vec4(vidx[i]), vec4(0,1,2,3))); \n"
" if(idx < 16) \n"
" { \n"
" if(idx < 8) \n"
" { \n"
" if(idx < 4) { bins[0]+=inc;} \n"
" else { bins[1]+=inc;} \n"
" }else \n"
" { \n"
" if(idx < 12){ bins[2]+=inc;} \n"
" else { bins[3]+=inc;} \n"
" } \n"
" }else if(idx < 32) \n"
" { \n"
" if(idx < 24) \n"
" { \n"
" if(idx <20) { bins[4]+=inc;} \n"
" else { bins[5]+=inc;} \n"
" }else \n"
" { \n"
" if(idx < 28){ bins[6]+=inc;} \n"
" else { bins[7]+=inc;} \n"
" } \n"
" }else \n"
" { \n"
" bins[8]+=inc; \n"
" } \n"
" } \n"
" } \n"
" } \n"
" } \n"
" }";
}
//reuse the code from the unpacked version..
ShaderBagGLSL::WriteOrientationCodeToStream(out);
ProgramGLSL * program = new ProgramGLSL(buffer);
if(program->IsNative())
{
s_orientation = program ;
_param_orientation_gtex = glGetUniformLocation(*program, "gtex");
_param_orientation_otex = glGetUniformLocation(*program, "otex");
_param_orientation_size = glGetUniformLocation(*program, "size");
}else
{
delete program;
}
}
void ShaderBagPKSL::SetGenListStartParam(float width, int tex0)
{
glUniform1f(_param_ftex_width, width);
}
void ShaderBagPKSL::LoadGenListShader(int ndoglev,int nlev)
{
ProgramGLSL * program;
s_genlist_init_tight = new ProgramGLSL(
"uniform sampler2DRect tex; void main ()\n"
"{\n"
" vec4 key = vec4(texture2DRect(tex, gl_TexCoord[0].xy).r, \n"
" texture2DRect(tex, gl_TexCoord[1].xy).r, \n"
" texture2DRect(tex, gl_TexCoord[2].xy).r, \n"
" texture2DRect(tex, gl_TexCoord[3].xy).r); \n"
" gl_FragColor = vec4(notEqual(key, vec4(0.0))); \n"
"}");
s_genlist_init_ex = program = new ProgramGLSL(
"uniform sampler2DRect tex; uniform vec4 bbox; void main ()\n"
"{\n"
" vec4 helper1 = vec4(equal(vec4(abs(texture2DRect(tex, gl_TexCoord[0].xy).r)), vec4(1.0, 2.0, 3.0, 4.0)));\n"
" vec4 helper2 = vec4(equal(vec4(abs(texture2DRect(tex, gl_TexCoord[1].xy).r)), vec4(1.0, 2.0, 3.0, 4.0)));\n"
" vec4 helper3 = vec4(equal(vec4(abs(texture2DRect(tex, gl_TexCoord[2].xy).r)), vec4(1.0, 2.0, 3.0, 4.0)));\n"
" vec4 helper4 = vec4(equal(vec4(abs(texture2DRect(tex, gl_TexCoord[3].xy).r)), vec4(1.0, 2.0, 3.0, 4.0)));\n"
" vec4 bx1 = vec4(lessThan(gl_TexCoord[0].xxyy, bbox)); \n"
" vec4 bx4 = vec4(lessThan(gl_TexCoord[3].xxyy, bbox)); \n"
" vec4 bx2 = vec4(bx4.xy, bx1.zw); \n"
" vec4 bx3 = vec4(bx1.xy, bx4.zw);\n"
" helper1 = min(min(bx1.xyxy, bx1.zzww), helper1);\n"
" helper2 = min(min(bx2.xyxy, bx2.zzww), helper2);\n"
" helper3 = min(min(bx3.xyxy, bx3.zzww), helper3);\n"
" helper4 = min(min(bx4.xyxy, bx4.zzww), helper4);\n"
" gl_FragColor.r = float(any(greaterThan(max(helper1.xy, helper1.zw), vec2(0.0)))); \n"
" gl_FragColor.g = float(any(greaterThan(max(helper2.xy, helper2.zw), vec2(0.0)))); \n"
" gl_FragColor.b = float(any(greaterThan(max(helper3.xy, helper3.zw), vec2(0.0)))); \n"
" gl_FragColor.a = float(any(greaterThan(max(helper4.xy, helper4.zw), vec2(0.0)))); \n"
"}");
_param_genlist_init_bbox = glGetUniformLocation( *program, "bbox");
s_genlist_end = program = new ProgramGLSL(
GlobalUtil::_KeepExtremumSign == 0 ?
"uniform sampler2DRect tex; uniform sampler2DRect ktex; void main()\n"
"{\n"
" vec4 tc = texture2DRect( tex, gl_TexCoord[0].xy);\n"
" vec2 pos = tc.rg; float index = tc.b;\n"
" vec4 tk = texture2DRect( ktex, pos); \n"
" vec4 keys = vec4(equal(abs(tk.rrrr), vec4(1.0, 2.0, 3.0, 4.0))); \n"
" vec2 opos; \n"
" opos.x = dot(keys, vec4(-0.5, 0.5, -0.5, 0.5));\n"
" opos.y = dot(keys, vec4(-0.5, -0.5, 0.5, 0.5));\n"
" gl_FragColor = vec4(opos + pos * 2.0 + tk.yz, 1.0, tk.w);\n"
"}" :
"uniform sampler2DRect tex; uniform sampler2DRect ktex; void main()\n"
"{\n"
" vec4 tc = texture2DRect( tex, gl_TexCoord[0].xy);\n"
" vec2 pos = tc.rg; float index = tc.b;\n"
" vec4 tk = texture2DRect( ktex, pos); \n"
" vec4 keys = vec4(equal(abs(tk.rrrr), vec4(1.0, 2.0, 3.0, 4.0))) \n"
" vec2 opos; \n"
" opos.x = dot(keys, vec4(-0.5, 0.5, -0.5, 0.5));\n"
" opos.y = dot(keys, vec4(-0.5, -0.5, 0.5, 0.5));\n"
" gl_FragColor = vec4(opos + pos * 2.0 + tk.yz, sign(tk.r), tk.w);\n"
"}"
);
_param_genlist_end_ktex = glGetUniformLocation(*program, "ktex");
//reduction ...
s_genlist_histo = new ProgramGLSL(
"uniform sampler2DRect tex; void main ()\n"
"{\n"
" vec4 helper; vec4 helper2; \n"
" helper = texture2DRect(tex, gl_TexCoord[0].xy); helper2.xy = helper.xy + helper.zw; \n"
" helper = texture2DRect(tex, gl_TexCoord[1].xy); helper2.zw = helper.xy + helper.zw; \n"
" gl_FragColor.rg = helper2.xz + helper2.yw;\n"
" helper = texture2DRect(tex, gl_TexCoord[2].xy); helper2.xy = helper.xy + helper.zw; \n"
" helper = texture2DRect(tex, gl_TexCoord[3].xy); helper2.zw = helper.xy + helper.zw; \n"
" gl_FragColor.ba= helper2.xz+helper2.yw;\n"
"}");
//read of the first part, which generates tex coordinates
s_genlist_start= program = ShaderBagGLSL::LoadGenListStepShader(1, 1);
_param_ftex_width= glGetUniformLocation(*program, "width");
_param_genlist_start_tex0 = glGetUniformLocation(*program, "tex0");
//stepping
s_genlist_step = program = ShaderBagGLSL::LoadGenListStepShader(0, 1);
_param_genlist_step_tex= glGetUniformLocation(*program, "tex");
_param_genlist_step_tex0= glGetUniformLocation(*program, "tex0");
}
void ShaderBagPKSL::UnloadProgram(void)
{
glUseProgram(0);
}
void ShaderBagPKSL::LoadKeypointShader(float dog_threshold, float edge_threshold)
{
//
char buffer[10240];
float threshold0 = dog_threshold* (GlobalUtil::_SubpixelLocalization?0.8f:1.0f);
float threshold1 = dog_threshold;
float threshold2 = (edge_threshold+1)*(edge_threshold+1)/edge_threshold;
ostrstream out(buffer, 10240);
out<<setprecision(8);
if(GlobalUtil::_IsNvidia)
{
out << "#pragma optionNV(ifcvt none)\n"
"#pragma optionNV(unroll all)\n"
"#define REPEAT4(FUNCTION)\\\n"
"for(int i = 0; i < 4; ++i)\\\n"
"{\\\n"
" FUNCTION(i);\\\n"
"}\n";
}else
{
//loop unroll
out << "#define REPEAT4(FUNCTION)\\\n"
"FUNCTION(0);\\\n"
"FUNCTION(1);\\\n"
"FUNCTION(2);\\\n"
"FUNCTION(3);\n";
}
//tex(X)(Y)
//X: (CLR) (CENTER 0, LEFT -1, RIGHT +1)
//Y: (CDU) (CENTER 0, DOWN -1, UP +1)
out << "#define THRESHOLD0 " << threshold0 << "\n"
"#define THRESHOLD1 " << threshold1 << "\n"
"#define THRESHOLD2 " << threshold2 << "\n";
out<<
"uniform sampler2DRect tex; uniform sampler2DRect texU;\n"
"uniform sampler2DRect texD; void main ()\n"
"{\n"
" vec2 TexRU = vec2(gl_TexCoord[2].x, gl_TexCoord[4].y); \n"
" vec4 ccc = texture2DRect(tex, gl_TexCoord[0].xy);\n"
" vec4 clc = texture2DRect(tex, gl_TexCoord[1].xy);\n"
" vec4 crc = texture2DRect(tex, gl_TexCoord[2].xy);\n"
" vec4 ccd = texture2DRect(tex, gl_TexCoord[3].xy);\n"
" vec4 ccu = texture2DRect(tex, gl_TexCoord[4].xy);\n"
" vec4 cld = texture2DRect(tex, gl_TexCoord[5].xy);\n"
" vec4 clu = texture2DRect(tex, gl_TexCoord[6].xy);\n"
" vec4 crd = texture2DRect(tex, gl_TexCoord[7].xy);\n"
" vec4 cru = texture2DRect(tex, TexRU.xy);\n"
" vec4 cc = ccc;\n"
" vec4 v1[4], v2[4];\n"
" v1[0] = vec4(clc.g, ccc.g, ccd.b, ccc.b);\n"
" v1[1] = vec4(ccc.r, crc.r, ccd.a, ccc.a);\n"
" v1[2] = vec4(clc.a, ccc.a, ccc.r, ccu.r);\n"
" v1[3] = vec4(ccc.b, crc.b, ccc.g, ccu.g);\n"
" v2[0] = vec4(cld.a, clc.a, ccd.a, ccc.a);\n"
" v2[1] = vec4(ccd.b, ccc.b, crd.b, crc.b);\n"
" v2[2] = vec4(clc.g, clu.g, ccc.g, ccu.g);\n"
" v2[3] = vec4(ccc.r, ccu.r, crc.r, cru.r);\n"
//test against 8 neighbours
//use variable to identify type of extremum
//1.0 for local maximum and -1.0 for minimum
<<
" vec4 key = vec4(0.0); \n"
" #define KEYTEST_STEP0(i) \\\n"
" {\\\n"
" bvec4 test1 = greaterThan(vec4(cc[i]), max(v1[i], v2[i])), test2 = lessThan(vec4(cc[i]), min(v1[i], v2[i]));\\\n"
" key[i] = cc[i] > THRESHOLD0 && all(test1)?1.0: 0.0;\\\n"
" key[i] = cc[i] < -THRESHOLD0 && all(test2)? -1.0: key[i];\\\n"
" }\n"
" REPEAT4(KEYTEST_STEP0);\n"
" if(gl_TexCoord[0].x < 1.0) {key.rb = vec2(0.0);}\n"
" if(gl_TexCoord[0].y < 1.0) {key.rg = vec2(0.0);}\n"
" gl_FragColor = vec4(0.0);\n"
" if(any(notEqual(key, vec4(0.0)))) {\n";
//do edge supression first..
//vector v1 is < (-1, 0), (1, 0), (0,-1), (0, 1)>
//vector v2 is < (-1,-1), (-1,1), (1,-1), (1, 1)>
out<<
" float fxx[4], fyy[4], fxy[4], fx[4], fy[4];\n"
" #define EDGE_SUPPRESION(i) \\\n"
" if(key[i] != 0)\\\n"
" {\\\n"
" vec4 D2 = v1[i].xyzw - cc[i];\\\n"
" vec2 D4 = v2[i].xw - v2[i].yz;\\\n"
" vec2 D5 = 0.5*(v1[i].yw-v1[i].xz); \\\n"
" fx[i] = D5.x; fy[i] = D5.y ;\\\n"
" fxx[i] = D2.x + D2.y;\\\n"
" fyy[i] = D2.z + D2.w;\\\n"
" fxy[i] = 0.25*(D4.x + D4.y);\\\n"
" float fxx_plus_fyy = fxx[i] + fyy[i];\\\n"
" float score_up = fxx_plus_fyy*fxx_plus_fyy; \\\n"
" float score_down = (fxx[i]*fyy[i] - fxy[i]*fxy[i]);\\\n"
" if( score_down <= 0 || score_up > THRESHOLD2 * score_down)key[i] = 0;\\\n"
" }\n"
" REPEAT4(EDGE_SUPPRESION);\n"
" if(any(notEqual(key, vec4(0.0)))) {\n";
////////////////////////////////////////////////
//read 9 pixels of upper/lower level
out<<
" vec4 v4[4], v5[4], v6[4];\n"
" ccc = texture2DRect(texU, gl_TexCoord[0].xy);\n"
" clc = texture2DRect(texU, gl_TexCoord[1].xy);\n"
" crc = texture2DRect(texU, gl_TexCoord[2].xy);\n"
" ccd = texture2DRect(texU, gl_TexCoord[3].xy);\n"
" ccu = texture2DRect(texU, gl_TexCoord[4].xy);\n"
" cld = texture2DRect(texU, gl_TexCoord[5].xy);\n"
" clu = texture2DRect(texU, gl_TexCoord[6].xy);\n"
" crd = texture2DRect(texU, gl_TexCoord[7].xy);\n"
" cru = texture2DRect(texU, TexRU.xy);\n"
" vec4 cu = ccc;\n"
" v4[0] = vec4(clc.g, ccc.g, ccd.b, ccc.b);\n"
" v4[1] = vec4(ccc.r, crc.r, ccd.a, ccc.a);\n"
" v4[2] = vec4(clc.a, ccc.a, ccc.r, ccu.r);\n"
" v4[3] = vec4(ccc.b, crc.b, ccc.g, ccu.g);\n"
" v6[0] = vec4(cld.a, clc.a, ccd.a, ccc.a);\n"
" v6[1] = vec4(ccd.b, ccc.b, crd.b, crc.b);\n"
" v6[2] = vec4(clc.g, clu.g, ccc.g, ccu.g);\n"
" v6[3] = vec4(ccc.r, ccu.r, crc.r, cru.r);\n"
<<
" #define KEYTEST_STEP1(i)\\\n"
" if(key[i] == 1.0)\\\n"
" {\\\n"
" bvec4 test = lessThan(vec4(cc[i]), max(v4[i], v6[i])); \\\n"
" if(cc[i] < cu[i] || any(test))key[i] = 0.0; \\\n"
" }else if(key[i] == -1.0)\\\n"
" {\\\n"
" bvec4 test = greaterThan(vec4(cc[i]), min(v4[i], v6[i])); \\\n"
" if(cc[i] > cu[i] || any(test) )key[i] = 0.0; \\\n"
" }\n"
" REPEAT4(KEYTEST_STEP1);\n"
" if(any(notEqual(key, vec4(0.0)))) { \n"
<<
" ccc = texture2DRect(texD, gl_TexCoord[0].xy);\n"
" clc = texture2DRect(texD, gl_TexCoord[1].xy);\n"
" crc = texture2DRect(texD, gl_TexCoord[2].xy);\n"
" ccd = texture2D
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -