-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgramCG.cpp
More file actions
2765 lines (2439 loc) · 92.7 KB
/
ProgramCG.cpp
File metadata and controls
2765 lines (2439 loc) · 92.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//////////////////////////////////////////////////////////////////////////////
// File: ProgramCG.cpp
// Author: Changchang Wu
// Description : implementation of cg related class.
// class ProgramCG A simple wrapper of Cg programs
// class ShaderBagCG cg shaders for SIFT
// class FilterCGGL cg gaussian filters for SIFT
//
// Copyright (c) 2007 University of North Carolina at Chapel Hill
// All Rights Reserved
//
// Permission to use, copy, modify and distribute this software and its
// documentation for educational, research and non-profit purposes, without
// fee, and without a written agreement is hereby granted, provided that the
// above copyright notice and the following paragraph appear in all copies.
//
// The University of North Carolina at Chapel Hill make no representations
// about the suitability of this software for any purpose. It is provided
// 'as is' without express or implied warranty.
//
// Please send BUG REPORTS to ccwu@cs.unc.edu
//
////////////////////////////////////////////////////////////////////////////
#if defined(CG_SIFTGPU_ENABLED)
#include "GL/glew.h"
#include <iostream>
#include <iomanip>
#include <vector>
#include <strstream>
#include <algorithm>
#include <stdlib.h>
#include <math.h>
#include <string.h>
using namespace std;
#include "GlobalUtil.h"
#include "ProgramCG.h"
#include "GLTexImage.h"
#include "ShaderMan.h"
#include "FrameBufferObject.h"
#if defined(_WIN32)
#pragma comment (lib, "../../lib/cg.lib")
#pragma comment (lib, "../../lib/cggl.lib")
#endif
CGcontext ProgramCG::_Context =0;
CGprofile ProgramCG::_FProfile;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
ProgramCG::ProgramCG()
{
_programID = NULL;
}
ProgramCG::~ProgramCG()
{
if(_programID) cgDestroyProgram(_programID);
}
ProgramCG::ProgramCG(const char *code, const char** cg_compile_args, CGprofile profile)
{
_valid = 0;
_profile = profile;
GLint epos;
const char* ati_args[] = {"-po", "ATI_draw_buffers",0};
const char* fp40_args[] = {"-ifcvt", "none","-unroll", "all", GlobalUtil::_UseFastMath? "-fastmath" : 0, 0};
if(cg_compile_args == NULL) cg_compile_args = GlobalUtil::_IsNvidia? (GlobalUtil::_SupportFP40? fp40_args:NULL) : ati_args;
_programID = ::cgCreateProgram(_Context, CG_SOURCE, code, profile, NULL, cg_compile_args);
if(_programID)
{
cgGLLoadProgram(_programID );
//_texParamID = cgGetNamedParameter(_programID, "tex");
glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &epos);
if(epos >=0)
{
std::cout<<cgGetProgramString(_programID, CG_COMPILED_PROGRAM)<<endl;
std::cerr<<glGetString(GL_PROGRAM_ERROR_STRING_ARB)<<endl;
}else
{
_valid = 1;
}
}else
{
std::cerr<<code<<endl;
glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &epos);
if(epos >=0)
{
std::cout<<cgGetProgramString(_programID, CG_COMPILED_PROGRAM)<<endl;
std::cerr<<glGetString(GL_PROGRAM_ERROR_STRING_ARB)<<endl;
}else
{
std::cout<<glGetString(GL_PROGRAM_ERROR_STRING_ARB)<<endl;
}
}
}
void ProgramCG::ErrorCallback()
{
CGerror err = cgGetError();
if(err)
{
std::cerr<< cgGetErrorString(err)<<endl;
}
}
void ProgramCG::InitContext()
{
if(_Context == 0)
{
_Context = cgCreateContext();
/////////////
_FProfile = cgGLGetLatestProfile(CG_GL_FRAGMENT);
cgGLSetOptimalOptions(_FProfile);
if(GlobalUtil::_verbose) std::cout<<"Shader Profile: "<<cgGetProfileString(_FProfile)<<endl;
cgSetErrorCallback(ErrorCallback);
}
}
void ProgramCG::DestroyContext()
{
cgDestroyContext(_Context);
}
ShaderBagCG::ShaderBagCG()
{
ProgramCG::InitContext();
}
int ProgramCG::UseProgram()
{
if(_programID)
{
cgGLEnableProfile(_profile);
cgGLBindProgram(_programID);
return 1;
}else
{
return 0;
}
}
void ShaderBagCG::UnloadProgram()
{
cgGLUnbindProgram(ProgramCG::_FProfile);
cgGLDisableProfile(ProgramCG::_FProfile);
}
void ShaderBagCG::LoadFixedShaders()
{
// s_debug = new ProgramCG( "void main(float4 TexCoord0:TEXCOORD0, out float4 FragColor:COLOR0,"
// "uniform samplerRECT tex){ gl_FragColor.rg = gl_TexCoord[0].st;}");
s_gray = new ProgramCG(
"void main(float4 TexCoord0 : TEXCOORD0, out float4 FragColor : COLOR0, uniform samplerRECT tex){\n"
"float intensity = dot(float3(0.299, 0.587, 0.114), texRECT(tex,TexCoord0.xy ).rgb);\n"
"FragColor= float4(intensity, intensity, intensity, 1.0);}" );
s_sampling = new ProgramCG(
"void main(float4 TexCoord0 : TEXCOORD0, out float4 FragColor : COLOR0, uniform samplerRECT tex){\n"
"float4 cc = texRECT(tex, TexCoord0.xy); FragColor = float4(cc.rg, 0.0, 0.0); }" );
s_zero_pass = new ProgramCG("void main(out float4 FragColor : COLOR0){FragColor = 0;}");
ProgramCG * program;
s_margin_copy = program = new ProgramCG(
"void main(float4 texCoord0: TEXCOORD0, out float4 FragColor: COLOR0, \n"
"uniform samplerRECT tex, uniform float2 truncate){\n"
"FragColor = texRECT(tex, min(texCoord0.xy, truncate)); }");
_param_margin_copy_truncate = cgGetNamedParameter(*program, "truncate");
s_grad_pass = new ProgramCG(
"void main (\n"
"float4 TexCC : TEXCOORD0, float4 TexLC : TEXCOORD1,\n"
"float4 TexRC : TEXCOORD2, float4 TexCD : TEXCOORD3, float4 TexCU : TEXCOORD4,\n"
"out float4 FragData0 : COLOR0, uniform samplerRECT tex)\n"
"{\n"
" float4 v1, v2, gg;\n"
" float4 cc = texRECT(tex, TexCC.xy);\n"
" gg.x = texRECT(tex, TexLC.xy).r;\n"
" gg.y = texRECT(tex, TexRC.xy).r;\n"
" gg.z = texRECT(tex, TexCD.xy).r;\n"
" gg.w = texRECT(tex, TexCU.xy).r;\n"
" float2 dxdy = (gg.yw - gg.xz); \n"
" float grad = 0.5*length(dxdy);\n"
" float theta = grad==0? 0: atan2(dxdy.y, dxdy.x);\n"
" FragData0 = float4(cc.rg, grad, theta);\n"
"}\n\0");
if(GlobalUtil::_SupportFP40)
{
//use the packing mode for cpu list reshape and two orientations
if(GlobalUtil::_MaxOrientation != 2) GlobalUtil::_OrientationPack2 = 0;
LoadOrientationShader();
if(GlobalUtil::_DescriptorPPT) LoadDescriptorShader();
}else
{
s_orientation = program = new ProgramCG(
"void main(out float4 FragColor : COLOR0, \n"
" uniform samplerRECT fTex, uniform samplerRECT oTex, \n"
" uniform float size, \n"
" in float2 tpos : TEXCOORD0){\n"
" float4 cc = texRECT(fTex, tpos);\n"
" float4 oo = texRECT(oTex, cc.rg);\n"
" FragColor = float4(cc.rg, oo.a, size);}");
_param_orientation_gtex= cgGetNamedParameter(*program, "oTex");
_param_orientation_size= cgGetNamedParameter(*program, "size");
///
GlobalUtil::_FullSupported = 0;
GlobalUtil::_MaxOrientation = 0; //0 for simplified version
GlobalUtil::_DescriptorPPT = 0;
std::cerr<<"Orientation simplified on this hardware"<<endl;
std::cerr<<"Descriptor ignored on this hardware"<<endl;
}
}
void ShaderBagCG::LoadDisplayShaders()
{
s_copy_key = new ProgramCG(
"void main(float4 TexCoord0 : TEXCOORD0, out float4 FragColor : COLOR0, uniform samplerRECT tex){\n"
"FragColor.rg= texRECT(tex, TexCoord0.xy).rg; FragColor.ba = float2(0,1); }");
//shader used to write a vertex buffer object
//which is used to draw the quads of each feature
ProgramCG * program;
s_vertex_list = program = new ProgramCG(
"void main(in float4 TexCoord0: TEXCOORD0,\n"
"uniform float4 sizes, \n"
"uniform samplerRECT tex, \n"
"out float4 FragColor: COLOR0){\n"
"float fwidth = sizes.y; \n"
"float twidth = sizes.z; \n"
"float rwidth = sizes.w; \n"
"float index = 0.1*(fwidth*floor(TexCoord0.y) + TexCoord0.x);\n"
"float px = fmod(index, twidth);\n"
"float2 tpos= floor(float2(px, index*rwidth))+0.5;\n"
"float4 cc = texRECT(tex, tpos );\n"
"float size = cc.a * 3.0f;//sizes.x;// \n"
"FragColor.zw = float2(0.0, 1.0);\n"
"if(any(cc.xy <=0)) {FragColor.xy = cc.xy;}else \n"
"{\n"
" float type = frac(px);\n"
" float2 dxy; float s, c;\n"
" dxy.x = type < 0.1 ? 0 : ((type <0.5 || type > 0.9)? size : -size);\n"
" dxy.y = type < 0.2 ? 0 : ((type < 0.3 || type > 0.7 )? -size :size); \n"
" sincos(cc.b, s, c);\n"
" FragColor.x = cc.x + c*dxy.x-s*dxy.y;\n"
" FragColor.y = cc.y + c*dxy.y+s*dxy.x;}\n"
"}\n\0");
/*FragColor = float4(tpos, 0.0, 1.0);}\n\0");*/
_param_genvbo_size = cgGetNamedParameter(*program, "sizes");
s_display_gaussian = new ProgramCG(
"void main(float4 TexCoord0 : TEXCOORD0, out float4 FragColor : COLOR0, uniform samplerRECT tex){\n"
"float r = texRECT(tex, TexCoord0.xy).r;\n"
"FragColor = float4(r, r, r, 1.0);}");
s_display_dog = new ProgramCG(
"void main(float4 TexCoord0 : TEXCOORD0, out float4 FragColor : COLOR0, uniform samplerRECT tex){\n"
"float g = (0.5+20.0*texRECT(tex, TexCoord0.xy).g);\n"
"FragColor = float4(g, g, g, 1.0);}" );
s_display_grad = new ProgramCG(
"void main(float4 TexCoord0 : TEXCOORD0, out float4 FragColor : COLOR0, uniform samplerRECT tex){\n"
"float4 cc = texRECT(tex, TexCoord0.xy); FragColor = float4(5.0 * cc.bbb, 1.0); }");
s_display_keys= new ProgramCG(
"void main(float4 TexCoord0 : TEXCOORD0, out float4 FragColor : COLOR0, uniform samplerRECT tex){\n"
"float4 cc = texRECT(tex, TexCoord0.xy);\n"
"if(cc.r ==1.0) FragColor = float4(1.0, 0, 0,1.0); \n"
"else {if (cc.r ==0.5) FragColor = float4(0.0,1.0,0.0,1.0); else discard;}}");
}
void ShaderBagCG::SetMarginCopyParam(int xmax, int ymax)
{
float truncate[2] = {xmax - 0.5f , ymax - 0.5f};
cgGLSetParameter2fv(_param_margin_copy_truncate, truncate);
}
int ShaderBagCG::LoadKeypointShaderMR(float threshold, float edge_threshold)
{
char buffer[10240];
float threshold0 = threshold * 0.8f;
float threshold1 = threshold;
float threshold2 = (edge_threshold+1)*(edge_threshold+1)/edge_threshold;
int max_refine = max(2, GlobalUtil::_SubpixelLocalization);
ostrstream out(buffer, 10240);
out << "#define THRESHOLD0 " << threshold0 << "\n"
"#define THRESHOLD1 " << threshold1 << "\n"
"#define THRESHOLD2 " << threshold2 << "\n"
"#define MAX_REFINE " << max_refine << "\n";
out<<
"void main (\n"
"float4 TexCC : TEXCOORD0, float4 TexLC : TEXCOORD1,\n"
"float4 TexRC : TEXCOORD2, float4 TexCD : TEXCOORD3, \n"
"float4 TexCU : TEXCOORD4, float4 TexLD : TEXCOORD5, \n"
"float4 TexLU : TEXCOORD6, float4 TexRD : TEXCOORD7,\n"
"out float4 FragData0 : COLOR0, out float4 FragData1 : COLOR1, \n"
"uniform samplerRECT tex, uniform samplerRECT texU, uniform samplerRECT texD)\n"
"{\n"
" float4 v1, v2, gg;\n"
" float2 TexRU = float2(TexRC.x, TexCU.y); \n"
" float4 cc = texRECT(tex, TexCC.xy);\n"
" v1.x = texRECT(tex, TexLC.xy).g;\n"
" gg.x = texRECT(tex, TexLC.xy).r;\n"
" v1.y = texRECT(tex, TexRC.xy).g;\n"
" gg.y = texRECT(tex, TexRC.xy).r;\n"
" v1.z = texRECT(tex, TexCD.xy).g;\n"
" gg.z = texRECT(tex, TexCD.xy).r;\n"
" v1.w = texRECT(tex, TexCU.xy).g;\n"
" gg.w = texRECT(tex, TexCU.xy).r;\n"
" v2.x = texRECT(tex, TexLD.xy).g;\n"
" v2.y = texRECT(tex, TexLU.xy).g;\n"
" v2.z = texRECT(tex, TexRD.xy).g;\n"
" v2.w = texRECT(tex, TexRU.xy).g;\n"
" float2 dxdy = 0.5*(gg.yw - gg.xz); \n"
" float grad = length(dxdy);\n"
" float theta = grad==0? 0: atan2(dxdy.y, dxdy.x);\n"
" FragData0 = float4(cc.rg, grad, theta);\n"
<<
" float dog = 0.0; \n"
" FragData1 = float4(0, 0, 0, 0); \n"
" float2 v3; float4 v4, v5, v6;\n"
<<
" if( cc.g > THRESHOLD0 && all(cc.gggg > max(v1, v2)))\n"
" {\n"
" v3.x = texRECT(texU, TexCC.xy).g;\n"
" v4.x = texRECT(texU, TexLC.xy).g;\n"
" v4.y = texRECT(texU, TexRC.xy).g;\n"
" v4.z = texRECT(texU, TexCD.xy).g;\n"
" v4.w = texRECT(texU, TexCU.xy).g;\n"
" v6.x = texRECT(texU, TexLD.xy).g;\n"
" v6.y = texRECT(texU, TexLU.xy).g;\n"
" v6.z = texRECT(texU, TexRD.xy).g;\n"
" v6.w = texRECT(texU, TexRU.xy).g;\n"
" if(cc.g < v3.x || any(cc.gggg<v4.xyzw || cc.gggg<v6.xyzw))return; \n"
" v3.y = texRECT(texD, TexCC.xy).g;\n"
" v5.x = texRECT(texD, TexLC.xy).g;\n"
" v5.y = texRECT(texD, TexRC.xy).g;\n"
" v5.z = texRECT(texD, TexCD.xy).g;\n"
" v5.w = texRECT(texD, TexCU.xy).g;\n"
" v6.x = texRECT(texD, TexLD.xy).g;\n"
" v6.y = texRECT(texD, TexLU.xy).g;\n"
" v6.z = texRECT(texD, TexRD.xy).g;\n"
" v6.w = texRECT(texD, TexRU.xy).g;\n"
" if(cc.g < v3.y || any(cc.gggg<v5.xyzw || cc.gggg<v6.xyzw))return; \n"
" dog = 1.0; \n"
" }\n"
//the minimum case
<<
" else if(cc.g < -THRESHOLD0 && all(cc.gggg < min(v1, v2)))\n"
" {\n"
" v3.x = texRECT(texU, TexCC.xy).g;\n"
" v4.x = texRECT(texU, TexLC.xy).g;\n"
" v4.y = texRECT(texU, TexRC.xy).g;\n"
" v4.z = texRECT(texU, TexCD.xy).g;\n"
" v4.w = texRECT(texU, TexCU.xy).g;\n"
" v6.x = texRECT(texU, TexLD.xy).g;\n"
" v6.y = texRECT(texU, TexLU.xy).g;\n"
" v6.z = texRECT(texU, TexRD.xy).g;\n"
" v6.w = texRECT(texU, TexRU.xy).g;\n"
" if(cc.g > v3.x || any(cc.gggg>v4.xyzw || cc.gggg>v6.xyzw))return; \n"
" v3.y = texRECT(texD, TexCC.xy).g;\n"
" v5.x = texRECT(texD, TexLC.xy).g;\n"
" v5.y = texRECT(texD, TexRC.xy).g;\n"
" v5.z = texRECT(texD, TexCD.xy).g;\n"
" v5.w = texRECT(texD, TexCU.xy).g;\n"
" v6.x = texRECT(texD, TexLD.xy).g;\n"
" v6.y = texRECT(texD, TexLU.xy).g;\n"
" v6.z = texRECT(texD, TexRD.xy).g;\n"
" v6.w = texRECT(texD, TexRU.xy).g;\n"
" if(cc.g > v3.y || any(cc.gggg>v5.xyzw || cc.gggg>v6.xyzw))return; \n"
" dog = 0.5 ; \n"
" }\n"
" else\n"
" return;\n"
<<
" int i = 0; \n"
" float2 offset = float2(0, 0);\n"
" float2 offsets = float2(0, 0);\n"
" float3 dxys; bool key_moved; \n"
" float fx, fy, fs; \n"
" float fxx, fyy, fxy; \n"
" float fxs, fys, fss; \n"
" do\n"
" {\n"
" dxys = float3(0, 0, 0);\n"
" offset = float2(0, 0);\n"
" float4 D2 = v1.xyzw - cc.gggg;\n"
" fxx = D2.x + D2.y;\n"
" fyy = D2.z + D2.w;\n"
" float2 D4 = v2.xw - v2.yz;\n"
" fxy = 0.25*(D4.x + D4.y);\n"
" float2 D5 = 0.5*(v1.yw-v1.xz); \n"
" fx = D5.x;\n"
" fy = D5.y ; \n"
" fs = 0.5*( v3.x - v3.y ); \n"
" fss = v3.x + v3.y - cc.g - cc.g;\n"
" fxs = 0.25 * ( v4.y + v5.x - v4.x - v5.y);\n"
" fys = 0.25 * ( v4.w + v5.z - v4.z - v5.w);\n"
" float4 A0, A1, A2 ; \n"
" A0 = float4(fxx, fxy, fxs, -fx); \n"
" A1 = float4(fxy, fyy, fys, -fy); \n"
" A2 = float4(fxs, fys, fss, -fs); \n"
" float3 x3 = abs(float3(fxx, fxy, fxs)); \n"
" float maxa = max(max(x3.x, x3.y), x3.z); \n"
" if(maxa > 1e-10 ) \n"
" {\n"
" if(x3.y ==maxa ) \n"
" { \n"
" float4 TEMP = A1; A1 = A0; A0 = TEMP; \n"
" }else if( x3.z == maxa ) \n"
" { \n"
" float4 TEMP = A2; A2 = A0; A0 = TEMP; \n"
" } \n"
" A0 /= A0.x; \n"
" A1 -= A1.x * A0; \n"
" A2 -= A2.x * A0; \n"
" float2 x2 = abs(float2(A1.y, A2.y)); \n"
" if( x2.y > x2.x ) \n"
" { \n"
" float3 TEMP = A2.yzw; \n"
" A2.yzw = A1.yzw; \n"
" A1.yzw = TEMP; \n"
" x2.x = x2.y; \n"
" } \n"
" if(x2.x > 1e-10) \n"
" {\n"
" A1.yzw /= A1.y; \n"
" A2.yzw -= A2.y * A1.yzw; \n"
" if(abs(A2.z) > 1e-10) \n"
" {\n"
// compute dx, dy, ds:
<<
" dxys.z = A2.w /A2.z; \n"
" dxys.y = A1.w - dxys.z*A1.z; \n"
" dxys.x = A0.w - dxys.z*A0.z - dxys.y*A0.y; \n"
" }\n"
" }\n"
" }\n"
" offset.x = dxys.x > 0.6 ? 1 : 0 + dxys.x < -0.6 ? -1 : 0;\n"
" offset.y = dxys.y > 0.6 ? 1 : 0 + dxys.y < - 0.6? -1 : 0;\n"
" i++; key_moved = i < MAX_REFINE && any(abs(offset)>0) ; \n"
" if(key_moved)\n"
" {\n"
" offsets += offset; \n"
" cc = texRECT(tex, TexCC.xy + offsets);\n"
" v1.x = texRECT(tex , TexLC.xy + offsets).g;\n"
" v1.y = texRECT(tex , TexRC.xy + offsets).g;\n"
" v1.z = texRECT(tex , TexCD.xy + offsets).g;\n"
" v1.w = texRECT(tex , TexCU.xy + offsets).g;\n"
" v2.x = texRECT(tex , TexLD.xy + offsets).g;\n"
" v2.y = texRECT(tex , TexLU.xy + offsets).g;\n"
" v2.z = texRECT(tex , TexRD.xy + offsets).g;\n"
" v2.w = texRECT(tex , TexRU.xy + offsets).g;\n"
" v3.x = texRECT(texU, TexCC.xy + offsets).g;\n"
" v4.x = texRECT(texU, TexLC.xy + offsets).g;\n"
" v4.y = texRECT(texU, TexRC.xy + offsets).g;\n"
" v4.z = texRECT(texU, TexCD.xy + offsets).g;\n"
" v4.w = texRECT(texU, TexCU.xy + offsets).g;\n"
" v3.y = texRECT(texD, TexCC.xy + offsets).g;\n"
" v5.x = texRECT(texD, TexLC.xy + offsets).g;\n"
" v5.y = texRECT(texD, TexRC.xy + offsets).g;\n"
" v5.z = texRECT(texD, TexCD.xy + offsets).g;\n"
" v5.w = texRECT(texD, TexCU.xy + offsets).g;\n"
" }\n"
" }while(key_moved);\n"
<<
" bool test1 = (abs(cc.g + 0.5*dot(float3(fx, fy, fs), dxys ))> THRESHOLD1) ;\n"
" float test2_v1= fxx*fyy - fxy *fxy; \n"
" float test2_v2 = (fxx+fyy); \n"
" test2_v2 = test2_v2*test2_v2;\n"
" bool test2 = test2_v1>0 && test2_v2 < THRESHOLD2 * test2_v1; \n "
//keep the point when the offset is less than 1
<<
" FragData1 = test1 && test2 && all( abs(dxys) < 1)? float4( dog, dxys.xy+offsets, dxys.z) : float4(0, 0, 0, 0); \n"
"}\n"
<<'\0';
ProgramCG * program;
s_keypoint = program = new ProgramCG(buffer);
//parameter
_param_dog_texu = cgGetNamedParameter(*program, "texU");
_param_dog_texd = cgGetNamedParameter(*program, "texD");
return 1;
}
//keypoint detection shader
//1. compare with 26 neighbours
//2. sub-pixel sub-scale localization
//3. output: [dog, offset(x,y,s)]
void ShaderBagCG:: LoadKeypointShader(float threshold, float edge_threshold)
{
char buffer[10240];
float threshold0 = threshold* (GlobalUtil::_SubpixelLocalization?0.8f:1.0f);
float threshold1 = threshold;
float threshold2 = (edge_threshold+1)*(edge_threshold+1)/edge_threshold;
ostrstream out(buffer, 10240);
out<<setprecision(8);
streampos pos;
//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<<
"void main (\n"
"float4 TexCC : TEXCOORD0, float4 TexLC : TEXCOORD1,\n"
"float4 TexRC : TEXCOORD2, float4 TexCD : TEXCOORD3, \n"
"float4 TexCU : TEXCOORD4, float4 TexLD : TEXCOORD5, \n"
"float4 TexLU : TEXCOORD6, float4 TexRD : TEXCOORD7,\n"
"out float4 FragData0 : COLOR0, out float4 FragData1 : COLOR1, \n"
"uniform samplerRECT tex, uniform samplerRECT texU, uniform samplerRECT texD)\n"
"{\n"
" float4 v1, v2, gg;\n"
" float2 TexRU = float2(TexRC.x, TexCU.y); \n"
" float4 cc = texRECT(tex, TexCC.xy);\n"
" v1.x = texRECT(tex, TexLC.xy).g;\n"
" gg.x = texRECT(tex, TexLC.xy).r;\n"
" v1.y = texRECT(tex, TexRC.xy).g;\n"
" gg.y = texRECT(tex, TexRC.xy).r;\n"
" v1.z = texRECT(tex, TexCD.xy).g;\n"
" gg.z = texRECT(tex, TexCD.xy).r;\n"
" v1.w = texRECT(tex, TexCU.xy).g;\n"
" gg.w = texRECT(tex, TexCU.xy).r;\n"
" v2.x = texRECT(tex, TexLD.xy).g;\n"
" v2.y = texRECT(tex, TexLU.xy).g;\n"
" v2.z = texRECT(tex, TexRD.xy).g;\n"
" v2.w = texRECT(tex, TexRU.xy).g;\n"
" float2 dxdy = (gg.yw - gg.xz); \n"
" float grad = 0.5*length(dxdy);\n"
" float theta = grad==0? 0: atan2(dxdy.y, dxdy.x);\n"
" FragData0 = float4(cc.rg, grad, theta);\n"
//test against 8 neighbours
//use variable to identify type of extremum
//1.0 for local maximum and 0.5 for minimum
<<
" float dog = 0.0; \n"
" FragData1 = float4(0, 0, 0, 0); \n"
" dog = cc.g > THRESHOLD0 && all(cc.gggg > max(v1, v2))?1.0: 0.0;\n"
" dog = cc.g < -THRESHOLD0 && all(cc.gggg < min(v1, v2))?0.5: dog;\n";
pos = out.tellp();
//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<<
" if(dog == 0.0) return;\n"
" float fxx, fyy, fxy; \n"
" float4 D2 = v1.xyzw - cc.gggg;\n"
" float2 D4 = v2.xw - v2.yz;\n"
" fxx = D2.x + D2.y;\n"
" fyy = D2.z + D2.w;\n"
" fxy = 0.25*(D4.x + D4.y);\n"
" float fxx_plus_fyy = fxx + fyy;\n"
" float score_up = fxx_plus_fyy*fxx_plus_fyy; \n"
" float score_down = (fxx*fyy - fxy*fxy);\n"
" if( score_down <= 0 || score_up > THRESHOLD2 * score_down)return;\n"
//...
<<
" float2 D5 = 0.5*(v1.yw-v1.xz); \n"
" float fx = D5.x, fy = D5.y ; \n"
" float fs, fss , fxs, fys ; \n"
" float2 v3; float4 v4, v5, v6;\n"
//read 9 pixels of upper level
<<
" v3.x = texRECT(texU, TexCC.xy).g;\n"
" v4.x = texRECT(texU, TexLC.xy).g;\n"
" v4.y = texRECT(texU, TexRC.xy).g;\n"
" v4.z = texRECT(texU, TexCD.xy).g;\n"
" v4.w = texRECT(texU, TexCU.xy).g;\n"
" v6.x = texRECT(texU, TexLD.xy).g;\n"
" v6.y = texRECT(texU, TexLU.xy).g;\n"
" v6.z = texRECT(texU, TexRD.xy).g;\n"
" v6.w = texRECT(texU, TexRU.xy).g;\n"
//compare with 9 pixels of upper level
//read and compare with 9 pixels of lower level
//the maximum case
<<
" if(dog == 1.0)\n"
" {\n"
" bool4 test = cc.gggg < max(v4, v6); \n"
" if(cc.g < v3.x || any(test.xy||test.zw))return; \n"
" v3.y = texRECT(texD, TexCC.xy).g;\n"
" v5.x = texRECT(texD, TexLC.xy).g;\n"
" v5.y = texRECT(texD, TexRC.xy).g;\n"
" v5.z = texRECT(texD, TexCD.xy).g;\n"
" v5.w = texRECT(texD, TexCU.xy).g;\n"
" v6.x = texRECT(texD, TexLD.xy).g;\n"
" v6.y = texRECT(texD, TexLU.xy).g;\n"
" v6.z = texRECT(texD, TexRD.xy).g;\n"
" v6.w = texRECT(texD, TexRU.xy).g;\n"
" test = cc.gggg<max(v5, v6); \n"
" if(cc.g < v3.y || any(test.xy||test.zw))return; \n"
" }\n"
//the minimum case
<<
" else{\n"
" bool4 test = cc.gggg>min(v4, v6); \n"
" if(cc.g > v3.x || any(test.xy||test.zw))return; \n"
" v3.y = texRECT(texD, TexCC.xy).g;\n"
" v5.x = texRECT(texD, TexLC.xy).g;\n"
" v5.y = texRECT(texD, TexRC.xy).g;\n"
" v5.z = texRECT(texD, TexCD.xy).g;\n"
" v5.w = texRECT(texD, TexCU.xy).g;\n"
" v6.x = texRECT(texD, TexLD.xy).g;\n"
" v6.y = texRECT(texD, TexLU.xy).g;\n"
" v6.z = texRECT(texD, TexRD.xy).g;\n"
" v6.w = texRECT(texD, TexRU.xy).g;\n"
" test = cc.gggg>min(v5, v6); \n"
" if(cc.g > v3.y || any(test.xy||test.zw))return; \n"
" }\n";
if(GlobalUtil::_SubpixelLocalization)
// sub-pixel localization FragData1 = float4(dog, 0, 0, 0); return;
out <<
" fs = 0.5*( v3.x - v3.y ); //bug fix 9/12/2007 \n"
" fss = v3.x + v3.y - cc.g - cc.g;\n"
" fxs = 0.25 * ( v4.y + v5.x - v4.x - v5.y);\n"
" fys = 0.25 * ( v4.w + v5.z - v4.z - v5.w);\n"
/////////////////////////////////////////////////////////////////
// let dog difference be quatratic function of dx, dy, ds;
// df(dx, dy, ds) = fx * dx + fy*dy + fs * ds +
// + 0.5 * ( fxx * dx * dx + fyy * dy * dy + fss * ds * ds)
// + (fxy * dx * dy + fxs * dx * ds + fys * dy * ds)
// (fx, fy, fs, fxx, fyy, fss, fxy, fxs, fys are the derivatives)
//the local extremum satisfies
// df/dx = 0, df/dy = 0, df/dz = 0
//that is
// |-fx| | fxx fxy fxs | |dx|
// |-fy| = | fxy fyy fys | * |dy|
// |-fs| | fxs fys fss | |ds|
// need to solve dx, dy, ds
// Use Gauss elimination to solve the linear system
<<
" float3 dxys = float3(0.0); \n"
" float4 A0, A1, A2 ; \n"
" A0 = float4(fxx, fxy, fxs, -fx); \n"
" A1 = float4(fxy, fyy, fys, -fy); \n"
" A2 = float4(fxs, fys, fss, -fs); \n"
" float3 x3 = abs(float3(fxx, fxy, fxs)); \n"
" float maxa = max(max(x3.x, x3.y), x3.z); \n"
" if(maxa >= 1e-10 ) { \n"
" if(x3.y ==maxa ) \n"
" { \n"
" float4 TEMP = A1; A1 = A0; A0 = TEMP; \n"
" }else if( x3.z == maxa ) \n"
" { \n"
" float4 TEMP = A2; A2 = A0; A0 = TEMP; \n"
" } \n"
" A0 /= A0.x; \n"
" A1 -= A1.x * A0; \n"
" A2 -= A2.x * A0; \n"
" float2 x2 = abs(float2(A1.y, A2.y)); \n"
" if( x2.y > x2.x ) \n"
" { \n"
" float3 TEMP = A2.yzw; \n"
" A2.yzw = A1.yzw; \n"
" A1.yzw = TEMP; \n"
" x2.x = x2.y; \n"
" } \n"
" if(x2.x >= 1e-10) { \n"
" A1.yzw /= A1.y; \n"
" A2.yzw -= A2.y * A1.yzw; \n"
" if(abs(A2.z) >= 1e-10) { \n"
// compute dx, dy, ds:
<<
" dxys.z = A2.w /A2.z; \n"
" dxys.y = A1.w - dxys.z*A1.z; \n"
" dxys.x = A0.w - dxys.z*A0.z - dxys.y*A0.y; \n"
//one more threshold which I forgot in versions prior to 286
<<
" bool bugfix_test = (abs(cc.g + 0.5*dot(float3(fx, fy, fs), dxys )) < THRESHOLD1) ;\n"
" if(bugfix_test || any(abs(dxys) >= 1.0)) dog = 0; \n"
" }}}\n"
//keep the point when the offset is less than 1
<<
" FragData1 = float4( dog, dxys); \n"
"}\n" <<'\0';
else out<<
" FragData1 = float4( dog, 0, 0, 0) ; \n"
"}\n" <<'\0';
ProgramCG * program;
s_keypoint = program = new ProgramCG(buffer);
if(!program->IsValidProgram())
{
delete program;
out.seekp(pos);
out <<
" FragData1 = float4( fabs(cc.g) > 2.0 * THRESHOLD0? dog : 0, 0, 0, 0) ; \n"
"}\n" <<'\0';
s_keypoint = program = new ProgramCG(buffer);
GlobalUtil::_SubpixelLocalization = 0;
std::cerr<<"Detection simplified on this hardware"<<endl;
}
//parameter
_param_dog_texu = cgGetNamedParameter(*program, "texU");
_param_dog_texd = cgGetNamedParameter(*program, "texD");
}
void ShaderBagCG::SetDogTexParam(int texU, int texD)
{
cgGLSetTextureParameter(_param_dog_texu, texU);
cgGLEnableTextureParameter(_param_dog_texu);
cgGLSetTextureParameter(_param_dog_texd, texD);
cgGLEnableTextureParameter(_param_dog_texd);
}
void ShaderBagCG::SetGenListStepParam(int tex, int tex0)
{
cgGLSetTextureParameter(_param_genlist_step_tex, tex);
cgGLEnableTextureParameter(_param_genlist_step_tex);
cgGLSetTextureParameter(_param_genlist_step_tex0, tex0);
cgGLEnableTextureParameter(_param_genlist_step_tex0);
}
void ShaderBagCG::SetGenVBOParam(float width, float fwidth, float size)
{
float sizes[4] = {size*3.0f, fwidth, width, 1.0f/width};
cgGLSetParameter4fv(_param_genvbo_size, sizes);
}
ProgramGPU* FilterGLCG::CreateFilterH(float kernel[], float offset[], int width)
{
char buffer[10240];
ostrstream out(buffer, 10240);
out<<setprecision(8);
if(GlobalUtil::_BetaFilter)
{
out<< "void main(uniform samplerRECT tex,";
out<<"\n\tin float4 TexCoord0: TEXCOORD0,";
out<<"\n\tout float4 FragColor : COLOR0 )";
out<<"\n{\n\tfloat4 intensity4 = float4(0, 0, 0, 0), data;\n";
out<<"float or = texRECT(tex, TexCoord0.xy).r, intensity;\n";
for(int i = 0; i< width; i+=4)
{
out <<"data = float4(";
for(int j = i; j < i + 4; j++)
{
if(j != i) out <<", \n";
if(j >= width)
{
out<<"0";
}else if(offset[j]==0.0)
{
out<<"or";
}else
{
out<<"texRECT(tex, TexCoord0.xy + float2(float("<<offset[j] <<") , 0)).r";
}
}
out << ");\n";
out << "intensity4 += data * float4(";
for(int k = i; k < i + 4; k++)
{
if(k != i) out <<", ";
if(k >= width) out<<"0";
else out<<kernel[k];
}
out << ");\n";
}
out << "intensity4.xy += intensity4.zw;\n";
out << "intensity = intensity4.x + intensity4.y;\n";
}else
{
out<< "void main(uniform samplerRECT tex,";
out<<"\n\tin float4 TexCoord0: TEXCOORD0,";
out<<"\n\tout float4 FragColor : COLOR0 )";
out<<"\n{\n\tfloat intensity = 0.0 ; float2 pos;\n";
for(int i = 0; i< width; i++)
{
if(offset[i]==0.0)
{
out<<"float or = texRECT(tex, TexCoord0.xy).r;\n";
out<<"intensity+= or * "<<kernel[i]<<";\n";
}else
{
out<<"pos = TexCoord0.xy + float2(float("<<offset[i] <<") , 0);\n";
out<<"intensity+= "<<kernel[i]<<"*texRECT(tex, pos).r;\n";
}
}
}
//copy original data to red channel
out<<"FragColor.r = or;\n";
out<<"FragColor.b = intensity;}\n"<<'\0';
return new ProgramCG( buffer);
}
ProgramGPU* FilterGLCG::CreateFilterV(float kernel[], float offset[], int height)
{
char buffer[10240];
ostrstream out(buffer, 10240);
out<<setprecision(8);
if(GlobalUtil::_BetaFilter)
{
out<< "void main(uniform samplerRECT tex,";
out<<"\n\tin float4 TexCoord0: TEXCOORD0,";
out<<"\n\tout float4 FragColor : COLOR0 )";
out<<"\n{\n\tfloat4 intensity4 = float4(0, 0, 0, 0), data;\n";
out<<"float2 orb = texRECT(tex, TexCoord0.xy).rb; float intensity;\n";
for(int i = 0; i< height; i+=4)
{
out <<"data = float4(";
for(int j = i; j < i + 4; j++)
{
if(j != i) out <<", \n";
if(j >= height)
{
out<<"0";
}else if(offset[j]==0.0)
{
out<<"orb.y";
}else
{
out<<"texRECT(tex, TexCoord0.xy + float2(0, float("<<offset[j] <<"))).b";
}
}
out << ");\n";
out << "intensity4 += data * float4(";
for(int k = i; k < i + 4; k++)
{
if(k != i) out <<", ";
if(k >= height) out<<"0";
else out<<kernel[k];
}
out << ");\n";
}
out << "intensity4.xy += intensity4.zw;\n";
out << "intensity = intensity4.x + intensity4.y;\n";
}else
{
out<< "void main(uniform samplerRECT tex,";
out<<"\n\tin float4 TexCoord0: TEXCOORD0,";
out<<"\n\tout float4 FragColor : COLOR0 )";
out<<"\n{\n\tfloat intensity = 0.0 ; float2 pos;\n";
for(int i = 0; i< height; i++)
{
if(offset[i]==0.0)
{
out<<"float2 orb = texRECT(tex, TexCoord0.xy).rb;\n";
out<<"intensity+= orb.y * "<<kernel[i]<<";\n";
}else
{
out<<"pos = TexCoord0.xy + float2(0, float("<<offset[i] <<"));\n";
out<<"intensity+= "<<kernel[i]<<"*texRECT(tex, pos).b;\n";
}
}
}
out<<"FragColor.b = orb.y;\n";
out<<"FragColor.g = intensity - orb.x;\n"; // difference of gaussian..
out<<"FragColor.r = intensity;}\n"<<'\0';
return new ProgramCG( buffer);
}
ProgramGPU* FilterGLCG::CreateFilterHPK(float kernel[], float offset[], int width)
{
//both h and v are packed...
int i, j , xw, xwn;
int halfwidth = width >>1;
float * pf = kernel + halfwidth;
int nhpixel = (halfwidth+1)>>1; //how many neighbour pixels need to be looked up
int npixel = (nhpixel<<1)+1;//
char buffer[10240];
float weight[3];
ostrstream out(buffer, 10240);
out<<setprecision(8);
out<< "void main(uniform samplerRECT tex, float4 TexCoord0 : TEXCOORD0, out float4 FragColor : COLOR0 ){\n";
out<< "float4 result = float4(0, 0, 0, 0); \nfloat4 pc; float2 coord; \n";
///use multi texture coordinate because nhpixels can be at most 3
for( i = 0 ; i < npixel ; i++)
{
out<<"coord = TexCoord0.xy + float2(float("<<i-nhpixel<<"),0);\n";
out<<"pc=texRECT(tex, coord);\n";
if(GlobalUtil::_PreciseBorder) out<<"if(coord.x < 0) pc = pc.rrbb;\n";
//for each sub-pixel j in center, the weight of sub-pixel k
xw = (i - nhpixel)*2;
for( j = 0; j < 3; j++)
{
xwn = xw + j -1;
weight[j] = xwn < -halfwidth || xwn > halfwidth? 0 : pf[xwn];
}
//if(weight[1]!=0.0) out<<"FragColor += "<<weight[1]<<"*pc;\n";
//out<<"FragColor += float4("<<weight[2]<<","<<weight[0]<<","<<weight[2]<<","<<weight[0]<<")*pc.grab;\n";
if(weight[1] == 0.0)
{
out<<"result += float4("<<weight[2]<<","<<weight[0]<<","<<weight[2]<<","<<weight[0]<<")*pc.grab;\n";
}
else
{
out<<"result += float4("<<weight[1]<<", "<<weight[0]<<", "<<weight[1]<<", "<<weight[0]<<")*pc.rrbb;\n";
out<<"result += float4("<<weight[2]<<", "<<weight[1]<<", "<<weight[2]<<", "<<weight[1]<<")*pc.ggaa;\n";
}
}
out<<
" FragColor = result; }\n"<<'\0';
return new ProgramCG( buffer);
}
ProgramGPU* FilterGLCG::CreateFilterVPK(float kernel[], float offset[], int height)
{
//both h and v are packed...
int i, j , yw, ywn;
int halfh = height >>1;
float * pf = kernel + halfh;
int nhpixel = (halfh+1)>>1; //how many neighbour pixels need to be looked up
int npixel = (nhpixel<<1)+1;//
char buffer[10240];
float weight[3];
ostrstream out(buffer, 10240);
out<<setprecision(8);
out<< "void main(uniform samplerRECT tex, float4 TexCoord0 : TEXCOORD0, out float4 FragColor : COLOR0 ){\n";
out<< "float4 result = float4(0, 0, 0, 0);\nfloat4 pc; float2 coord;\n";
///use multi texture coordinate because nhpixels can be at most 3
for( i = 0 ; i < npixel ; i++)