1 ////////////////////////////////////////////////////////////////////////////
2 //	File:		ProgramGLSL.h
3 //	Author:		Changchang Wu
4 //	Description : Interface for ProgramGLSL classes
5 //		ProgramGLSL:	Glsl Program
6 //		FilterGLSL:		Glsl Gaussian Filters
7 //		ShaderBag:	    base class of ShaderBagPKSL and ShaderBagGLSL
8 //
9 //	Copyright (c) 2007 University of North Carolina at Chapel Hill
10 //	All Rights Reserved
11 //
12 //	Permission to use, copy, modify and distribute this software and its
13 //	documentation for educational, research and non-profit purposes, without
14 //	fee, and without a written agreement is hereby granted, provided that the
15 //	above copyright notice and the following paragraph appear in all copies.
16 //
17 //	The University of North Carolina at Chapel Hill make no representations
18 //	about the suitability of this software for any purpose. It is provided
19 //	'as is' without express or implied warranty.
20 //
21 //	Please send BUG REPORTS to ccwu@cs.unc.edu
22 //
23 ////////////////////////////////////////////////////////////////////////////
24 
25 
26 #ifndef _PROGRAM_GLSL_H
27 #define _PROGRAM_GLSL_H
28 
29 
30 #include "ProgramGPU.h"
31 
32 class ProgramGLSL:public ProgramGPU
33 {
34 	class ShaderObject
35 	{
36 		GLuint		_shaderID;
37 		int			_type;
38 		int			_compiled;
39 		static int ReadShaderFile(const char * source,  char *& code);
40 		void CheckCompileLog();
41 	public:
42 		void PrintCompileLog(ostream & os  );
IsValidShaderObject()43 		int inline IsValidShaderObject(){	return _shaderID && _compiled;}
44 		int IsValidVertexShader();
45 		int IsValidFragmentShader();
GetShaderID()46 		GLuint GetShaderID(){return _shaderID;}
47 		~ShaderObject();
48 		ShaderObject(int shadertype,  const char * source, int filesource =0);
49 	};
50 
51 protected:
52 	int			_linked;
53 	GLint		_TextureParam0;
54 	GLuint		_programID;
55 private:
56 	void AttachShaderObject(ShaderObject& shader);
57 	void DetachShaderObject(ShaderObject& shader);
58 
59 public:
60 	void ReLink();
61 	int IsNative();
62 	int	UseProgram();
63 	void PrintLinkLog(std::ostream&os);
64 	int ValidateProgram();
65 	void CheckLinkLog();
66 	int LinkProgram();
GLuint()67 	operator GLuint (){return _programID;}
GetProgramID()68     virtual void * GetProgramID() { return (void*) _programID; }
69 public:
70 	ProgramGLSL();
71 	~ProgramGLSL();
72 	ProgramGLSL(const char* frag_source);
73 };
74 
75 
76 class GLTexImage;
77 class FilterGLSL : public FilterProgram
78 {
79 private:
80 	ProgramGPU* CreateFilterH(float kernel[], int width);
81 	ProgramGPU* CreateFilterV(float kernel[], int height);
82 	ProgramGPU* CreateFilterHPK(float kernel[], int width);
83 	ProgramGPU* CreateFilterVPK(float kernel[], int height);
84 public:
85     void MakeFilterProgram(float kernel[],  int width);
86 public:
87     FilterGLSL(float sigma) ;
88 };
89 
90 class SiftParam;
91 
92 /////////////////////////////////////////////////////////////////////////////////
93 //class ShaderBag
94 //desciption:	pure virtual class
95 //				provides storage and usage interface of all the shaders for SIFT
96 //				two implementations are  ShaderBagPKSL and ShaderBagGLSL
97 /////////////////////////////////////////////////////////////////////////////////
98 class ShaderBag
99 {
100 public:
101 	//shader:	rgb to gray
102 	ProgramGPU  * s_gray;
103 	//shader:	copy keypoint to PBO
104 	ProgramGPU  * s_copy_key;
105 	//shader:	debug view
106 	ProgramGPU  * s_debug;
107 	//shader:	orientation
108 	//shader:	assign simple orientation to keypoints if hardware is low
109 	ProgramGPU  * s_orientation;
110 	//shader:	display gaussian levels
111 	ProgramGPU  * s_display_gaussian;
112 	//shader:	display difference of gassian
113 	ProgramGPU  * s_display_dog;
114 	//shader:	display  gradient
115 	ProgramGPU  * s_display_grad;
116 	//shader:	display keypoints as red(maximum) and blue (minimum)
117 	ProgramGPU  * s_display_keys;
118 	//shader:	up/down-sample
119 	ProgramGPU  * s_sampling;
120 	//shader:	compute gradient/dog
121 	ProgramGPU  * s_grad_pass;
122 	ProgramGPU  * s_dog_pass;
123 	//shader:   keypoint detection in one pass
124 	ProgramGPU  * s_keypoint;
125 	ProgramGPU  * s_seperate_sp;
126 	//shader:   feature list generations..
127 	ProgramGPU	* s_genlist_init_tight;
128 	ProgramGPU	* s_genlist_init_ex;
129 	ProgramGPU	* s_genlist_histo;
130 	ProgramGPU	* s_genlist_start;
131 	ProgramGPU	* s_genlist_step;
132 	ProgramGPU	* s_genlist_end;
133 	ProgramGPU	* s_zero_pass;
134 	//shader:	generate vertex to display SIFT as a square
135 	ProgramGPU  * s_vertex_list;
136 	//shader:	descriptor
137 	ProgramGPU  * s_descriptor_fp;
138 	//shader:	copy pixels to margin
139 	ProgramGPU	* s_margin_copy;
140 public:
141 	FilterProgram  *         f_gaussian_skip0;
142 	vector<FilterProgram*>   f_gaussian_skip0_v;
143 	FilterProgram  *         f_gaussian_skip1;
144 	FilterProgram  **        f_gaussian_step;
145     int                     _gaussian_step_num;
146 public:
SetGenListInitParam(int w,int h)147 	virtual void SetGenListInitParam(int w, int h){};
SetGenListEndParam(int ktex)148 	virtual void SetGenListEndParam(int ktex){};
SetMarginCopyParam(int xmax,int ymax)149 	virtual void SetMarginCopyParam(int xmax, int ymax){};
LoadDescriptorShader()150 	virtual void LoadDescriptorShader(){};
SetFeatureDescirptorParam(int gtex,int otex,float dwidth,float fwidth,float width,float height,float sigma)151 	virtual void SetFeatureDescirptorParam(int gtex, int otex, float dwidth, float fwidth, float width, float height, float sigma){};
SetFeatureOrientationParam(int gtex,int width,int height,float sigma,int stex,float step)152 	virtual void SetFeatureOrientationParam(int gtex, int width, int height, float sigma, int stex, float step){};
SetSimpleOrientationInput(int oTex,float sigma,float sigma_step)153 	virtual void SetSimpleOrientationInput(int oTex, float sigma, float sigma_step){};
154 	virtual void LoadOrientationShader() =0;
155 	virtual void SetGenListStartParam(float width, int tex0) =0;
156 	virtual void LoadGenListShader(int ndoglev, int nlev)=0;
157 	virtual void UnloadProgram()=0;
158 	virtual void LoadKeypointShader(float threshold, float edgeTrheshold) = 0;
159 	virtual void LoadFixedShaders()=0;
160 	virtual void LoadDisplayShaders() = 0;
161 	virtual void SetDogTexParam(int texU, int texD)=0;
162 	virtual void SetGradPassParam(int texP=0){}
163 	virtual void SetGenListStepParam(int tex, int tex0) = 0;
164 	virtual void SetGenVBOParam( float width, float fwidth, float size)=0;
165 public:
166     void CreateGaussianFilters(SiftParam&param);
167     void SelectInitialSmoothingFilter(int octave_min, SiftParam&param);
168     void LoadDynamicShaders(SiftParam& param);
169 	ShaderBag();
170 	virtual ~ShaderBag();
171 };
172 
173 
174 class ShaderBagGLSL:public ShaderBag
175 {
176 	GLint _param_dog_texu;
177 	GLint _param_dog_texd;
178 	GLint _param_ftex_width;
179 	GLint _param_genlist_start_tex0;
180 	GLint _param_genlist_step_tex0;
181 	GLint _param_genvbo_size;
182 	GLint _param_orientation_gtex;
183 	GLint _param_orientation_size;
184 	GLint _param_orientation_stex;
185 	GLint _param_margin_copy_truncate;
186 	GLint _param_genlist_init_bbox;
187 	GLint _param_descriptor_gtex;
188 	GLint _param_descriptor_size;
189 	GLint _param_descriptor_dsize;
190 public:
191 	virtual void SetMarginCopyParam(int xmax, int ymax);
192 	void SetSimpleOrientationInput(int oTex, float sigma, float sigma_step);
193 	void LoadOrientationShader();
194 	void LoadDescriptorShaderF2();
195 	virtual void LoadDescriptorShader();
196 	virtual void SetFeatureOrientationParam(int gtex, int width, int height, float sigma, int stex = 0, float step = 1.0f);
197 	virtual void SetFeatureDescirptorParam(int gtex, int otex, float dwidth, float fwidth, float width, float height, float sigma);
198 	static void  WriteOrientationCodeToStream(ostream& out);
199 	static ProgramGLSL* LoadGenListStepShader(int start, int step);
200 	virtual void SetGenListInitParam(int w, int h);
201 	virtual void SetGenListStartParam(float width, int tex0);
202 	virtual void LoadGenListShader(int ndoglev, int nlev);
203 	virtual void UnloadProgram();
204 	virtual void LoadKeypointShader(float threshold, float edgeTrheshold);
205 	virtual void LoadFixedShaders();
206 	virtual void LoadDisplayShaders();
207 	virtual void SetDogTexParam(int texU, int texD);
208 	virtual void SetGenListStepParam(int tex, int tex0);
209 	virtual void SetGenVBOParam( float width, float fwidth, float size);
~ShaderBagGLSL()210 	virtual ~ShaderBagGLSL(){}
211 };
212 
213 
214 class ShaderBagPKSL:public ShaderBag
215 {
216 private:
217 	GLint	_param_dog_texu;
218 	GLint	_param_dog_texd;
219 	GLint	_param_dog_texi;
220 	GLint	_param_margin_copy_truncate;
221 	GLint	_param_grad_pass_texp;
222 	GLint	_param_genlist_init_bbox;
223 	GLint	_param_genlist_start_tex0;
224 	GLint	_param_ftex_width;
225 	GLint	_param_genlist_step_tex0;
226 	GLint	_param_genlist_end_ktex;
227 	GLint	_param_genvbo_size;
228 	GLint	_param_orientation_gtex;
229 	GLint	_param_orientation_otex;
230 	GLint	_param_orientation_size;
231 	GLint	_param_descriptor_gtex;
232 	GLint	_param_descriptor_otex;
233 	GLint	_param_descriptor_size;
234 	GLint	_param_descriptor_dsize;
235 
236     //
237     ProgramGLSL* s_rect_description;
238 public:
ShaderBagPKSL()239     ShaderBagPKSL () {s_rect_description = NULL; }
~ShaderBagPKSL()240 	virtual ~ShaderBagPKSL() {if(s_rect_description) delete s_rect_description; }
241 	virtual void LoadFixedShaders();
242 	virtual void LoadDisplayShaders();
243 	virtual void LoadOrientationShader() ;
244 	virtual void SetGenListStartParam(float width, int tex0) ;
245 	virtual void LoadGenListShader(int ndoglev, int nlev);
246 	virtual void UnloadProgram();
247 	virtual void LoadKeypointShader(float threshold, float edgeTrheshold) ;
248 	virtual void LoadDescriptorShader();
249 	virtual void LoadDescriptorShaderF2();
250     static ProgramGLSL* LoadDescriptorProgramRECT();
251 	static ProgramGLSL* LoadDescriptorProgramPKSL();
252 /////////////////
253 	virtual void SetDogTexParam(int texU, int texD);
254 	virtual void SetGradPassParam(int texP);
255 	virtual void SetGenListStepParam(int tex, int tex0);
256 	virtual void SetGenVBOParam( float width, float fwidth, float size);
257 	virtual void SetFeatureDescirptorParam(int gtex, int otex, float dwidth, float fwidth, float width, float height, float sigma);
258 	virtual void SetFeatureOrientationParam(int gtex, int width, int height, float sigma, int stex, float step);
259 	virtual void SetSimpleOrientationInput(int oTex, float sigma, float sigma_step);
260 	virtual void SetGenListEndParam(int ktex);
261 	virtual void SetGenListInitParam(int w, int h);
262 	virtual void SetMarginCopyParam(int xmax, int ymax);
263 };
264 
265 
266 #endif
267 
268