⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 programcg.cpp

📁 SiftGPU is an implementation of SIFT [1] for GPU. SiftGPU processes pixels parallely to build Gaussi
💻 CPP
📖 第 1 页 / 共 5 页
字号:
	{
		out<<"\n"
	"	float Orientations = 0;				";
		testpeak_code ="\n"
	"		if(npeaks==0){								\n"
	"		test = (bins[i] >= maxh)	;";
		savepeak_code="\n"
	"				npeaks++;								\n"
	"				Orientations = th.x;";

	}

	//find the peaks
	//the following loop will be unrolled

	out<<"\n"
	"	const float4 hh = maxh * ORIENTATION_THRESHOLD;	bool4 test;	\n"
	"	bins[9] = bins[0];								\n"
	"	float npeaks = 0, k = 0;			\n"
	"	float prevb	= bins[8].w;						\n"
	"	for (int i = 0; i <9 ; i++)						\n"
	"	{"
		<<testpeak_code<<"									\n"
	"		if( any ( test.xy || test.zw) )							\n"
	"		{											\n"
	"			if(test.r && bins[i].x > prevb && bins[i].x > bins[i].y )	\n"
	"			{											\n"
	"			    float	di = 0.5 * (bins[i].y-prevb) / (bins[i].x *2.0 -bins[i].y -prevb) ; \n"
	"				float	th = (k+di+0.5);	float weight = bins[i].x;"
					<<savepeak_code<<"\n"
	"			}\n"
	"			else if(test.g && all( bins[i].yy > bins[i].xz) )	\n"
	"			{											\n"
	"			    float	di = 0.5 * (bins[i].z-bins[i].x) / (bins[i].y * 2.0 - bins[i].z - bins[i].x) ; \n"
	"				float	th = (k+di+1.5);	float weight = bins[i].y;				"
					<<savepeak_code<<"	\n"
	"			}"
		<<"\n"
	"			if(test.b && all( bins[i].zz > bins[i].yw) )	\n"
	"			{											\n"
	"			    float	di = 0.5 * (bins[i].w-bins[i].y) / (bins[i].z * 2.0-bins[i].w-bins[i].y) ; \n"
	"				float	th = (k+di+2.5);	float weight = bins[i].z;				"
					<<savepeak_code<<"	\n"
	"			}\n"
	"			else if(test.a && bins[i].w > bins[i].z && bins[i].w > bins[i+1].x )	\n"
	"			{											\n"
	"			    float	di = 0.5 * (bins[i+1].x-bins[i].z) / (bins[i].w * 2.0- bins[i+1].x-bins[i].z) ; \n"
	"				float	th = (k+di+3.5);	float weight = bins[i].w;				"
					<<savepeak_code<<"	\n"
	"			}\n"
	"		}}\n"
	"		k = k + 4.0;						\n"
	"		prevb = bins[i].w;\n"
	"	}";
	//WRITE output
	if(GlobalUtil::_OrientationPack2)
	{
		//pack two orientations in one float
	out<<"\n"
	"	 if(orientation_mode){\n"
	"		Orientations.xy = frac(Orientations.xy / 36.0 + 1.0);\n"
	"		if(weights.x <= 0) Orientations.x = 1.0;\n"
	"		if(weights.y <= 0) Orientations.y = 1.0;\n"
	"		float packed_orientation = pack_2ushort(Orientations.xy); \n"
	"		FeatureData = float4(pos, packed_orientation, sigma);\n"
	"	}else{\n"
	"		FeatureData = float4(pos, radians((Orientations.x)*10.0), sigma);\n"
	"	}\n";		
	}else if(GlobalUtil::_MaxOrientation>1)
	{
	out<<"\n"
	"	 if(orientation_mode){\n"
	"	 npeaks = dot(float4(1,1,"
			<<(GlobalUtil::_MaxOrientation>2 ? 1 : 0)<<","
			<<(GlobalUtil::_MaxOrientation >3? 1 : 0)<<"), float4(weights>hh));\n"
	"		OrientationData = radians((Orientations )*10.0);\n"
	"		FeatureData = float4(pos, npeaks, sigma);\n"
	"	}else{\n"
	"		FeatureData = float4(pos, radians((Orientations.x)*10.0), sigma);\n"
	"	}\n";
	}else
	{
	out<<"\n"
	"	 FeatureData = float4(pos, radians((Orientations.x)*10.0), sigma);";
	}
	//end
	out<<"\n"
	"}\n"<<'\0';


}

void ShaderBagCG::SetSimpleOrientationInput(int oTex, float sigma, float sigma_step)
{
	cgGLSetTextureParameter(_param_orientation_gtex, oTex);
	cgGLEnableTextureParameter(_param_orientation_gtex);
	cgGLSetParameter1f(_param_orientation_size, sigma);
}

void ShaderBagCG::SetFeatureOrientationParam(int gtex, int width, int height, float sigma, int stex, float step)
{
	///
	cgGLSetTextureParameter(_param_orientation_gtex, gtex);	
	cgGLEnableTextureParameter(_param_orientation_gtex);

	if((GlobalUtil::_SubpixelLocalization || GlobalUtil::_KeepExtremumSign)&& stex)
	{
		//specify texutre for subpixel subscale localization
		cgGLSetTextureParameter(_param_orientation_stex, stex);
		cgGLEnableTextureParameter(_param_orientation_stex);
	}

	float size[4];
	size[0] = (float)width;
	size[1] = (float)height;
	size[2] = sigma;
	size[3] = step;
	cgGLSetParameter4fv(_param_orientation_size, size);

}

void ShaderBagCG::SetFeatureDescirptorParam(int gtex, int otex, float dwidth, float fwidth,  float width, float height, float sigma)
{
	///
	cgGLSetTextureParameter(_param_descriptor_gtex, gtex);	
	cgGLEnableTextureParameter(_param_descriptor_gtex);

	float dsize[4] ={dwidth, 1.0f/dwidth, fwidth, 1.0f/fwidth};
	cgGLSetParameter4fv(_param_descriptor_dsize, dsize);
	float size[3];
	size[0] = width;
	size[1] = height;
	size[2] = GlobalUtil::_DescriptorWindowFactor;	
	cgGLSetParameter3fv(_param_descriptor_size, size);
}


///////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////PACKED VERSION?///////////////////////////////////

ShaderBagPKCG::ShaderBagPKCG()
{
	ProgramCG::InitContext();
}

void ShaderBagPKCG::UnloadProgram()
{

	cgGLUnbindProgram(ProgramCG::_FProfile);
	cgGLDisableProfile(ProgramCG::_FProfile);
}

void ShaderBagPKCG::LoadFixedShaders()
{
	ProgramCG * program;

	/*
	char *rgb2gray_packing_code =
		"void main(uniform samplerRECT rgbTex, in float4 TexCoord0 : TEXCOORD0, \n"
	"		in float4 TexCoord1 : TEXCOORD1, in float4 TexCoord2 : TEXCOORD2, \n"
	"		in float4 TexCoord3 : TEXCOORD3, out float4 FragData : COLOR0){\n"
	"		const float3 weight = vec3(0.299, 0.587, 0.114);\n"
	"		FragData.r = dot(weight, texRECT(rgbTex,TexCoord0.st ).rgb);\n"
	"		FragData.g = dot(weight, texRECT(rgbTex,TexCoord1.st ).rgb);\n"
	"		FragData.b = dot(weight, texRECT(rgbTex,TexCoord2.st ).rgb);\n"
	"		FragData.a = dot(weight, texRECT(rgbTex,TexCoord3.st ).rgb);}";//
	s_gray = new ProgramCG( rgb2gray_packing_code);
	*/

	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(uniform samplerRECT tex, in float4 TexCoord0 : TEXCOORD0, \n"
	"		in float4 TexCoord1 : TEXCOORD1, in float4 TexCoord2 : TEXCOORD2, \n"
	"		in float4 TexCoord3 : TEXCOORD3, out float4 FragData : COLOR0 ){\n"
	"		FragData= float4(	texRECT(tex,TexCoord0.st ).r,texRECT(tex,TexCoord1.st ).r,\n"
	"							texRECT(tex,TexCoord2.st ).r,texRECT(tex,TexCoord3.st ).r);}"	);


	s_margin_copy = program = new ProgramCG(
	"void main(in float4 texCoord0: TEXCOORD0, out float4 FragColor: COLOR0, \n"
	"uniform samplerRECT tex, uniform float4 truncate){\n"
	"float4 cc = texRECT(tex, min(texCoord0.xy, truncate.xy)); \n"
	"bool2 ob = texCoord0.xy < truncate.xy;\n"
	"if(ob.y) { FragColor = (truncate.z ==0 ? cc.rrbb : cc.ggaa); } \n"
	"else if(ob.x) {FragColor = (truncate.w <1.5 ? cc.rgrg : cc.baba);} \n"
	"else {	float4 weights = float4(float4(0, 1, 2, 3) == truncate.w);\n"
	"float v = dot(weights, cc); FragColor = v.xxxx;}}");

	_param_margin_copy_truncate = cgGetNamedParameter(*program, "truncate");


	s_zero_pass = new ProgramCG("void main(out float4 FragColor : COLOR0){FragColor = 0;}");

	s_grad_pass = program = 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, out float4 FragData1 : COLOR1, \n"
	"out float4 FragData2 : COLOR2, uniform samplerRECT tex, uniform samplerRECT texp)\n"
	"{\n"
	"	float4 v1, v2, gg;\n"
	"	float4 cc = texRECT(tex, TexCC.xy);\n"
	"	float4 cp = texRECT(texp, TexCC.xy);\n"
	"	FragData0 = cc - cp; \n"
	"	float4 cl = texRECT(tex, TexLC.xy);	float4 cr = texRECT(tex, TexRC.xy);\n"
	"	float4 cd = texRECT(tex, TexCD.xy); float4 cu = texRECT(tex, TexCU.xy);\n"
	"	float4 dx = (float4(cr.rb, cc.ga) - float4(cc.rb, cl.ga)).zxwy;\n"
	"	float4 dy = (float4(cu.rg, cc.ba) - float4(cc.rg, cd.ba)).zwxy;\n"
	"	FragData1 = 0.5 * sqrt(dx*dx + dy * dy);\n"
	"	FragData2 = FragData1 > 0?  atan2(dy, dx) : float4(0);\n"
	"}\n\0");

	_param_grad_pass_texp = cgGetNamedParameter(*program, "texp");


	s_dog_pass = program = new ProgramCG(
	"void main (float4 TexCC : TEXCOORD0, out float4 FragData0 : COLOR0, \n"
	"	uniform samplerRECT tex, uniform samplerRECT texp)\n"
	"{\n"
	"	float4 cc = texRECT(tex, TexCC.xy);\n"
	"	float4 cp = texRECT(texp, TexCC.xy);\n"
	"	FragData0 = cc - cp; \n"
	"}\n\0");

	////
	if(GlobalUtil::_SupportFP40)
	{
		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 float2 size, \n"
	"	in float2 tpos : TEXCOORD0){\n"
	"	float4 cc = texRECT(fTex, tpos);\n"
	"	float2 co = cc.xy * 0.5; \n"
	"	float4 oo = texRECT(oTex, co);\n"
	"	bool2 bo = frac(co) < 0.5; \n"
	"	float o = bo.y? (bo.x? oo.r : oo.g) : (bo.x? oo.b : oo.a); \n"
	"	FragColor = float4(cc.rg, o, size.x * pow(size.y, cc.a));}");  
		_param_orientation_gtex= cgGetNamedParameter(*program, "oTex");
		_param_orientation_size= cgGetNamedParameter(*program, "size");

		GlobalUtil::_FullSupported = 0;
		GlobalUtil::_MaxOrientation = 0;
		GlobalUtil::_DescriptorPPT = 0;
		std::cerr<<"Orientation simplified on this hardware"<<endl;
		std::cerr<<"Descriptor ignored on this hardware"<<endl;
	}
}

void ShaderBagPKCG::LoadDisplayShaders()
{
	ProgramCG * program;

	s_copy_key = new ProgramCG(
	"void main(in 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
	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 = 3.0f * cc.a;// 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(uniform samplerRECT tex, in float4 TexCoord0:TEXCOORD0, out float4 FragData: COLOR0 ){\n"
		"float4 pc = texRECT(tex, TexCoord0.xy);	 bool2 ff = (frac(TexCoord0.xy) < 0.5);\n"
    "float v = ff.y?(ff.x? pc.r : pc.g):(ff.x?pc.b:pc.a); FragData = float4(v.xxx, 1.0);}");

	s_display_dog =  new ProgramCG(
	"void main(in float4 TexCoord0 : TEXCOORD0, out float4 FragColor : COLOR0, uniform samplerRECT tex){\n"
	"float4 pc = texRECT(tex, TexCoord0.xy); bool2 ff = (frac(TexCoord0.xy) < 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"
	"FragColor = float4(g, g, g, 1.0);}" );


	s_display_grad = new ProgramCG(
	"void main(in float4 TexCoord0 : TEXCOORD0, out float4 FragColor : COLOR0, uniform samplerRECT tex){\n"
	"float4 pc = texRECT(tex, TexCoord0.xy); bool2 ff = (frac(TexCoord0.xy) < 0.5);\n"
	"float v = ff.y ?(ff.x ? pc.r : pc.g):(ff.x ? pc.b : pc.a); FragColor = float4(5.0 *v.xxx, 1.0); }");

	s_display_keys= new ProgramCG(
	"void main(in float4 TexCoord0 : TEXCOORD0, out float4 FragColor : COLOR0, uniform samplerRECT tex){\n"
	"float4 oc = texRECT(tex, TexCoord0.xy); \n"
	"float4 cc = float4(abs(oc.r) == float4(1.0, 2.0, 3.0, 4.0));\n"
	"bool2 ff = (frac(TexCoord0.xy) < 0.5);\n"
	"float v = ff.y ?(ff.x ? cc.r : cc.g):(ff.x ? cc.b : cc.a);\n"
	"if(oc.r == 0) discard;\n"
	"else if(oc.r > 0) FragColor = float4(1.0, 0, 0,1.0); \n"
	"else FragColor = float4(0.0,1.0,0.0,1.0); }" );		
}

void ShaderBagPKCG::LoadGenListShader(int ndoglev, int nlev)
{

	//the V2 algorithms are only slightly faster, but way more complicated
	//LoadGenListShaderV2(ndoglev, nlev); return; 
	ProgramCG * program;

	s_genlist_init_tight = new ProgramCG(
	"void main (uniform samplerRECT tex, in float4 TexCoord0 : TEXCOORD0,\n"
	"in float4 TexCoord1 : TEXCOORD1, in float4 TexCoord2 : TEXCOORD2, \n"
	"in float4 TexCoord3 : TEXCOORD3, out float4 FragColor : COLOR0)\n"
	"{\n"
	"	float4 data = float4(	texRECT(tex, TexCoord0.xy).r,\n"
	"							texRECT(tex, TexCoord1.xy).r,\n"
	"							texRECT(tex, TexCoord2.xy).r,\n"
	"							texRECT(tex, TexCoord3.xy).r);\n"
	"	FragColor = float4(data != 0);\n"
	"}");

	s_genlist_init_ex = program = new ProgramCG(
	"void main (uniform float4 bbox, uniform samplerRECT tex, \n"
	"in float4 TexCoord0 : TEXCOORD0, in float4 TexCoord1 : TEXCOORD1, \n"
	"in float4 TexCoord2 : TEXCOORD2, in float4 TexCoord3 : TEXCOORD3,\n"
	"out float4 FragColor : COLOR0)\n"
	"{\n"
	"	bool4 helper1 = abs(texRECT(tex, TexCoord0.xy).r)== float4(1.0, 2.0, 3.0, 4.0); \n"
	"	bool4 helper2 = abs(texRECT(tex, TexCoord1.xy).r)== float4(1.0, 2.0, 3.0, 4.0);\n"
	"	bool4 helper3 = abs(texRECT(tex, TexCoord2.xy).r)== float4(1.0, 2.0, 3.0, 4.0);\n"
	"	bool4 helper4 = abs(texRECT(tex, TexCoord3.xy).r)== float4(1.0, 2.0, 3.0, 4.0);\n"
	"	bool4 bx1 = TexCoord0.xxyy < bbox; \n"
	"	bool4 bx4 = TexCoord3.xxyy < bbox; \n"
	"	bool4 bx2 = bool4(bx4.xy, bx1.zw); \n"
	"	bool4 bx3 = bool4(bx1.xy, bx4.zw);\n"
	"	helper1 = (bx1.xyxy && bx1.zzww && helper1);\n"
	"	helper2 = (bx2.xyxy && bx2.zzww && helper2);\n"
	"	helper3 = (bx3.xyxy && bx3.zzww && helper3);\n"
	"	helper4 = (bx4.xyxy && bx4.zzww && helper4);\n"
	"	FragColor.r = any(helper1.xy || helper1.zw);	\n"
	"	FragColor.g = any(helper2.xy || helper2.zw);	\n"
	"	FragColor.b = any(helper3.xy || helper3.zw);	\n"
	"	FragColor.a = any(helper4.xy || helper4.zw);	\n"
	"}");
	_param_genlist_init_bbox = cgGetNamedParameter( *program, "bbox");

	s_genlist_end = program = new ProgramCG(
		GlobalUtil::_KeepExtremumSign == 0 ? 
	
	"void main(	uniform samplerRECT tex, uniform samplerRECT ktex,\n"
	"			in float4 tpos : TEXCOORD0,	out float4 FragColor : COLOR0)\n"
	"{\n"
	"	float4 tc = texRECT( tex, tpos.xy);\n"
	"	float2 pos = tc.rg; float index = tc.b;\n"
	"	flo

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -