File: ShaderMan.cpp

package info (click to toggle)
colmap 3.5-1
  • links: PTS
  • area: main
  • in suites: buster
  • size: 20,564 kB
  • sloc: ansic: 170,595; cpp: 95,339; python: 2,335; makefile: 183; sh: 51
file content (349 lines) | stat: -rwxr-xr-x 7,457 bytes parent folder | download | duplicates (5)
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
////////////////////////////////////////////////////////////////////////////
//	File:		ShaderMan.cpp
//	Author:		Changchang Wu
//	Description :	implementation of the ShaderMan class.
//				A Shader Manager that calls different implementation of shaders
//
//
//	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
//
////////////////////////////////////////////////////////////////////////////


#include "GL/glew.h"
#include <vector>
#include <iostream>
#include <stdlib.h>
#include <math.h>
using std::vector;
using std::ostream;
using std::endl;


#include "ProgramGLSL.h"
#include "GlobalUtil.h"
#include "GLTexImage.h"
#include "SiftGPU.h"
#include "ShaderMan.h"

///
ShaderBag   * ShaderMan::s_bag = NULL;

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

void ShaderMan::InitShaderMan(SiftParam&param)
{
	if(s_bag) return;

	if(GlobalUtil::_usePackedTex )	s_bag = new ShaderBagPKSL;
	else		                	s_bag =new ShaderBagGLSL;	

    GlobalUtil::StartTimer("Load Programs");
	s_bag->LoadFixedShaders();
    s_bag->LoadDynamicShaders(param);
	if(GlobalUtil::_UseSiftGPUEX) 	s_bag->LoadDisplayShaders();
    GlobalUtil::StopTimer();

	GlobalUtil::CheckErrorsGL("InitShaderMan");
}


void ShaderMan::DestroyShaders()
{
	if(s_bag) delete s_bag;
    s_bag   =   NULL; 
}

void ShaderMan::UnloadProgram()
{
	if(s_bag) s_bag->UnloadProgram();
}

void ShaderMan::FilterImage(FilterProgram* filter, GLTexImage *dst, GLTexImage *src, GLTexImage*tmp)
{
    if(filter == NULL) return; 

    //////////////////////////////
    src->FillMargin(filter->_size, 0);

	//output parameter
	if(tmp) tmp->AttachToFBO(0);
	else dst->AttachToFBO(0);


	//input parameter
	src->BindTex();
	dst->FitTexViewPort();

	//horizontal filter
	filter->s_shader_h->UseProgram();
	dst->DrawQuad();

	//parameters
	if(tmp)
	{
		// fill margin for out-of-boundary lookup
		tmp->DetachFBO(0);
		tmp->AttachToFBO(0);
		tmp->FillMargin(0, filter->_size);
		tmp->DetachFBO(0);
		dst->AttachToFBO(0);
		tmp->BindTex();
	}
	else
	{
		glFinish();
		// fill margin for out-of-boundary lookup
		dst->FillMargin(0, filter->_size);
		dst->BindTex();
	}

	//vertical filter
	filter->s_shader_v->UseProgram();
	dst->DrawQuad();


	//clean up
	dst->UnbindTex();
	dst->DetachFBO(0);

	//
	ShaderMan::UnloadProgram();
}


void ShaderMan::FilterInitialImage(GLTexImage* tex, GLTexImage* buf)
{
    if(s_bag->f_gaussian_skip0) FilterImage(s_bag->f_gaussian_skip0, tex, tex, buf);
}

void ShaderMan::FilterSampledImage(GLTexImage* tex, GLTexImage* buf)
{
    if(s_bag->f_gaussian_skip1) FilterImage(s_bag->f_gaussian_skip1, tex, tex, buf);
}

void ShaderMan::TextureCopy(GLTexImage*dst, GLTexImage*src)
{

	dst->AttachToFBO(0);

	src->BindTex();

	dst->FitTexViewPort();

	dst->DrawQuad();

	dst->UnbindTex();
//	ShaderMan::UnloadProgram();
	dst->DetachFBO(0);
	return;
}
void ShaderMan::TextureDownSample(GLTexImage *dst, GLTexImage *src, int scale)
{
	//output parameter
	
	dst->AttachToFBO(0);

	//input parameter
	src->BindTex();

	//
	dst->FitTexViewPort();

	s_bag->s_sampling->UseProgram();

	dst->DrawQuadDS(scale);
	src->UnbindTex();

	UnloadProgram();

	dst->DetachFBO(0); 
}

void ShaderMan::TextureUpSample(GLTexImage *dst, GLTexImage *src, int scale)
{

	//output parameter
	dst->AttachToFBO(0);
	//input parameter
	src->BindTex();

	dst->FitTexViewPort();

	GlobalUtil::SetTextureParameterUS();

	if(GlobalUtil::_usePackedTex)
	{
		s_bag->s_sampling->UseProgram();
	}

	dst->DrawQuadUS(scale);
	src->UnbindTex();

	UnloadProgram();

	dst->DetachFBO(0);

	GlobalUtil::SetTextureParameter();
}



void ShaderMan::UseShaderDisplayGaussian()
{
	if(s_bag && s_bag->s_display_gaussian) s_bag->s_display_gaussian->UseProgram();
}

void ShaderMan::UseShaderDisplayDOG()
{
	if(s_bag && s_bag->s_display_dog) s_bag->s_display_dog->UseProgram();
}



void ShaderMan::UseShaderRGB2Gray()
{
	if(s_bag && s_bag->s_gray)s_bag->s_gray->UseProgram();
}


void ShaderMan::UseShaderDisplayGrad()
{
	if(s_bag && s_bag->s_display_grad) s_bag->s_display_grad->UseProgram();
}


void ShaderMan::UseShaderDisplayKeypoints()
{
	if(s_bag && s_bag->s_display_keys) s_bag->s_display_keys->UseProgram();
}





void ShaderMan::UseShaderGradientPass(int texP)
{
	s_bag->s_grad_pass->UseProgram();
	s_bag->SetGradPassParam(texP);	
}


void ShaderMan::UseShaderKeypoint(int texU, int texD)
{
	s_bag->s_keypoint->UseProgram();
	s_bag->SetDogTexParam(texU, texD);
}



void ShaderMan::UseShaderGenListInit(int w, int h, int tight)
{
	if(tight)
	{
		s_bag->s_genlist_init_tight->UseProgram();
	}else
	{
		s_bag->s_genlist_init_ex->UseProgram();
		s_bag->SetGenListInitParam(w, h);
	}
}

void ShaderMan::UseShaderGenListHisto()
{
	s_bag->s_genlist_histo->UseProgram();

}




void ShaderMan::UseShaderGenListStart(float fw, int tex0)
{
	s_bag->s_genlist_start->UseProgram();
	s_bag->SetGenListStartParam(fw, tex0);
}

void ShaderMan::UseShaderGenListStep(int tex, int tex0)
{
	s_bag->s_genlist_step->UseProgram();
	s_bag->SetGenListStepParam( tex,  tex0);
}

void ShaderMan::UseShaderGenListEnd(int ktex)
{
	s_bag->s_genlist_end->UseProgram();
	s_bag->SetGenListEndParam(ktex);
}

void ShaderMan::UseShaderDebug()
{
	if(s_bag->s_debug)	s_bag->s_debug->UseProgram();
}

void ShaderMan::UseShaderZeroPass()
{
	if(s_bag->s_zero_pass) s_bag->s_zero_pass->UseProgram();
}

void ShaderMan::UseShaderGenVBO( float width, float fwidth, float size)
{
	s_bag->s_vertex_list->UseProgram();
	s_bag->SetGenVBOParam(width, fwidth, size);
}
void ShaderMan::UseShaderMarginCopy(int xmax, int ymax)
{
	s_bag->s_margin_copy->UseProgram();
	s_bag->SetMarginCopyParam(xmax, ymax);
	
}
void ShaderMan::UseShaderCopyKeypoint()
{
	s_bag->s_copy_key->UseProgram();
}

void ShaderMan::UseShaderSimpleOrientation(int oTex, float sigma, float sigma_step)
{
	s_bag->s_orientation->UseProgram();
	s_bag->SetSimpleOrientationInput(oTex, sigma, sigma_step);
}



void ShaderMan::UseShaderOrientation(int gtex, int width, int height, float sigma, int auxtex, float step, int keypoint_list)
{
	s_bag->s_orientation->UseProgram();

	//changes in v345. 
	//set sigma to 0 to identify keypoit list mode
	//set sigma to negative to identify fixed_orientation
	if(keypoint_list) sigma = 0.0f;
	else if(GlobalUtil::_FixedOrientation) sigma = - sigma;

	s_bag->SetFeatureOrientationParam(gtex, width, height, sigma, auxtex, step);
}

void ShaderMan::UseShaderDescriptor(int gtex, int otex, int dwidth, int fwidth,  int width, int height, float sigma)
{
	s_bag->s_descriptor_fp->UseProgram();
	s_bag->SetFeatureDescirptorParam(gtex, otex, (float)dwidth,  (float)fwidth, (float)width, (float)height, sigma);
}

void ShaderMan::SelectInitialSmoothingFilter(int octave_min, SiftParam&param)
{
    s_bag->SelectInitialSmoothingFilter(octave_min, param);
}