1 ////////////////////////////////////////////////////////////////////////////
2 //	File:		ProgramCL.h
3 //	Author:		Changchang Wu
4 //	Description :	interface for the ProgramCL classes.
5 //		ProgramCL:		Cg programs
6 //		ShaderBagCG:	All Cg shaders for Sift in a bag
7 //		FilterCL:		Cg Gaussian Filters
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 #if defined(CL_SIFTGPU_ENABLED)
27 
28 #ifndef _PROGRAM_CL_H
29 #define _PROGRAM_CL_H
30 
31 #include "ProgramGPU.h"
32 
33 class ProgramCL: public ProgramGPU
34 {
35 	cl_program		  _program;
36 	cl_kernel		  _kernel;
37 	int				  _valid;
38 public:
IsValidProgram()39 	int IsValidProgram(){return _program && _valid;}
40 	ProgramCL(const char* name, const char * code, cl_context contex, cl_device_id device);
41 	ProgramCL();
42     void PrintBuildLog(cl_device_id device, int all);
43 	virtual ~ProgramCL();
UseProgram()44     virtual int UseProgram(){return 1;}
GetProgramID()45     virtual void * GetProgramID() {return _kernel;}
46     friend class ProgramBagCL;
47     friend class ProgramBagCLN;
48 };
49 
50 class  CLTexImage;
51 class FilterCL
52 {
53 public:
54 	ProgramCL*  s_shader_h;
55 	ProgramCL*  s_shader_v;
56 	int			 _size;
57 	int			 _id;
58     CLTexImage * _weight;
59 public:
FilterCL()60     FilterCL() : s_shader_h(NULL), s_shader_v(NULL), _size(0), _id(0), _weight(NULL) {}
~FilterCL()61     ~FilterCL() {if(s_shader_h) delete s_shader_h; if(s_shader_v) delete s_shader_v; if(_weight) delete _weight; }
62 };
63 
64 class  SiftParam;
65 
66 class ProgramBagCL
67 {
68 protected:
69     cl_platform_id      _platform;
70     cl_device_id        _device;
71     cl_context          _context;
72     cl_command_queue    _queue;
73 protected:
74     ProgramCL  * s_gray;
75 	ProgramCL  * s_sampling;
76     ProgramCL  * s_sampling_k;
77     ProgramCL  * s_sampling_u;
78 	ProgramCL  * s_zero_pass;
79     ProgramCL  * s_packup;
80     ProgramCL  * s_unpack;
81     ProgramCL  * s_unpack_dog;
82     ProgramCL  * s_unpack_grd;
83     ProgramCL  * s_unpack_key;
84     ProgramCL  * s_dog_pass;
85     ProgramCL  * s_grad_pass;
86     ProgramCL  * s_grad_pass2;
87     ProgramCL  * s_gray_pack;
88     ProgramCL  * s_keypoint;
89 public:
90 	FilterCL  *         f_gaussian_skip0;
91 	vector<FilterCL*>   f_gaussian_skip0_v;
92 	FilterCL  *         f_gaussian_skip1;
93 	FilterCL  **        f_gaussian_step;
94     int                     _gaussian_step_num;
95 public:
96 	ProgramBagCL();
97     bool InitializeContext();
98 	virtual ~ProgramBagCL();
99     void FinishCL();
GetContextCL()100     cl_context          GetContextCL() {return _context;}
GetCommandQueue()101     cl_command_queue    GetCommandQueue() {return _queue;}
102     static const char* GetErrorString(cl_int error);
103     static bool  CheckErrorCL(cl_int error, const char* location = NULL);
104 public:
105     FilterCL * CreateGaussianFilter(float sigma);
106     void CreateGaussianFilters(SiftParam&param);
107     void SelectInitialSmoothingFilter(int octave_min, SiftParam&param);
108     void FilterInitialImage(CLTexImage* tex, CLTexImage* buf);
109     void FilterSampledImage(CLTexImage* tex, CLTexImage* buf);
110     void UnpackImage(CLTexImage*src, CLTexImage* dst);
111     void UnpackImageDOG(CLTexImage*src, CLTexImage* dst);
112     void UnpackImageGRD(CLTexImage*src, CLTexImage* dst);
113     void UnpackImageKEY(CLTexImage*src, CLTexImage* dog, CLTexImage* dst);
114     void ComputeDOG(CLTexImage*tex, CLTexImage* texp, CLTexImage* dog, CLTexImage* grad, CLTexImage* rot);
115     void ComputeKEY(CLTexImage*dog, CLTexImage* key, float Tdog, float Tedge);
116 public:
117 	virtual void SampleImageU(CLTexImage *dst, CLTexImage *src, int log_scale);
118 	virtual void SampleImageD(CLTexImage *dst, CLTexImage *src, int log_scale = 1);
119     virtual void FilterImage(FilterCL* filter, CLTexImage *dst, CLTexImage *src, CLTexImage*tmp);
120     virtual ProgramCL* CreateFilterH(float kernel[], int width);
121     virtual ProgramCL* CreateFilterV(float kernel[], int width);
122     virtual FilterCL*  CreateFilter(float kernel[], int width);
123 public:
124     virtual void InitProgramBag(SiftParam&param);
125 	virtual void LoadDescriptorShader();
126 	virtual void LoadDescriptorShaderF2();
127 	virtual void LoadOrientationShader();
128 	virtual void LoadGenListShader(int ndoglev, int nlev);
129 	virtual void UnloadProgram() ;
130 	virtual void LoadKeypointShader();
131 	virtual void LoadFixedShaders();
132 	virtual void LoadDisplayShaders();
133     virtual void LoadDynamicShaders(SiftParam& param);
134 public:
135 	//parameters
136 	virtual void SetGradPassParam(int texP);
137 	virtual void SetGenListEndParam(int ktex);
138 	virtual void SetGenListStartParam(float width, int tex0);
139 	virtual void SetGenListInitParam(int w, int h);
140 	virtual void SetMarginCopyParam(int xmax, int ymax);
141 	virtual void SetDogTexParam(int texU, int texD);
142 	virtual void SetGenListStepParam(int tex, int tex0);
143 	virtual void SetGenVBOParam( float width, float fwidth, float size);
144 	virtual void SetFeatureDescirptorParam(int gtex, int otex, float dwidth, float fwidth, float width, float height, float sigma);
145 	virtual void SetFeatureOrientationParam(int gtex, int width, int height, float sigma, int stex, float step);
146 	virtual void SetSimpleOrientationInput(int oTex, float sigma, float sigma_step);
147 
148 };
149 
150 class CLTexImage ;
151 class ProgramBagCLN: public ProgramBagCL
152 {
153 public:
154 	virtual void SampleImageD(CLTexImage *dst, CLTexImage *src, int log_scale = 1);
155     virtual FilterCL*  CreateFilter(float kernel[], int width);
156     virtual ProgramCL* CreateFilterH(float kernel[], int width);
157     virtual ProgramCL* CreateFilterV(float kernel[], int width);
158     virtual void FilterImage(FilterCL* filter, CLTexImage *dst, CLTexImage *src, CLTexImage*tmp);
159     virtual void LoadFixedShaders();
160 	virtual void LoadDisplayShaders();
161 };
162 #endif
163 #endif
164 
165