1 | #include "ShaderProgram.h"
|
---|
2 | #include "Matrix4x4.h"
|
---|
3 | #include "Timer/PerfTimer.h"
|
---|
4 | #include "Vector3.h"
|
---|
5 | #include "Camera.h"
|
---|
6 |
|
---|
7 |
|
---|
8 | using namespace std;
|
---|
9 |
|
---|
10 | namespace CHCDemoEngine
|
---|
11 | {
|
---|
12 |
|
---|
13 | //////////
|
---|
14 | //-- changing shader parameters
|
---|
15 | static float sCurrentTimer = 0;
|
---|
16 | static Matrix4x4 sCurrentViewMatrix = IdentityMatrix();
|
---|
17 | static Vector3 sCurrentViewVector = Vector3::UNIT_Y();
|
---|
18 |
|
---|
19 |
|
---|
20 | GPUProgramParameters::GPUProgramParameters(ShaderProgram *p):
|
---|
21 | mProgram(p),
|
---|
22 | mTimerParam(-1),
|
---|
23 | mViewVectorParam(-1),
|
---|
24 | mViewMatrixParam(-1)
|
---|
25 | {
|
---|
26 | }
|
---|
27 |
|
---|
28 |
|
---|
29 | GPUProgramParameters::GPUProgramParameters():
|
---|
30 | mProgram(NULL),
|
---|
31 | mTimerParam(-1),
|
---|
32 | mViewVectorParam(-1),
|
---|
33 | mViewMatrixParam(-1)
|
---|
34 | {
|
---|
35 | }
|
---|
36 |
|
---|
37 |
|
---|
38 | void GPUProgramParameters::Reset()
|
---|
39 | {
|
---|
40 | mFloats.clear();
|
---|
41 | mTextures.clear();
|
---|
42 | mMatrices.clear();
|
---|
43 | mArrays.clear();
|
---|
44 |
|
---|
45 | mTimerParam = -1;
|
---|
46 | mViewVectorParam = -1;
|
---|
47 | mViewMatrixParam = -1;
|
---|
48 | }
|
---|
49 |
|
---|
50 |
|
---|
51 | void GPUProgramParameters::SetValue1f(int idx, float value)
|
---|
52 | {
|
---|
53 | if (mFloats.size() < idx + 1)
|
---|
54 | mFloats.resize(idx + 1);
|
---|
55 |
|
---|
56 | mFloats[idx] = FloatParam(&value, 1);
|
---|
57 | }
|
---|
58 |
|
---|
59 |
|
---|
60 | void GPUProgramParameters::SetValue2f(int idx, float val1, float val2)
|
---|
61 | {
|
---|
62 | if (mFloats.size() < idx + 1)
|
---|
63 | mFloats.resize(idx + 1);
|
---|
64 |
|
---|
65 | float vals[] = {val1, val2};
|
---|
66 |
|
---|
67 | mFloats[idx] = FloatParam(vals, 2);
|
---|
68 | }
|
---|
69 |
|
---|
70 |
|
---|
71 | void GPUProgramParameters::SetValue3f(int idx, float val1, float val2, float val3)
|
---|
72 | {
|
---|
73 | if (mFloats.size() < idx + 1)
|
---|
74 | mFloats.resize(idx + 1);
|
---|
75 |
|
---|
76 | float vals[] = {val1, val2, val3};
|
---|
77 |
|
---|
78 | mFloats[idx] = FloatParam(vals, 3);
|
---|
79 | }
|
---|
80 |
|
---|
81 |
|
---|
82 | void GPUProgramParameters::SetArray1f(int idx, float *vals, int numElements)
|
---|
83 | {
|
---|
84 | if (mArrays.size() < idx + 1)
|
---|
85 | mArrays.resize(idx + 1);
|
---|
86 |
|
---|
87 | mArrays[idx] = ArrayParam(vals, 1, numElements);
|
---|
88 | }
|
---|
89 |
|
---|
90 |
|
---|
91 | void GPUProgramParameters::SetArray2f(int idx, float *vals, int numElements)
|
---|
92 | {
|
---|
93 | if (mArrays.size() < idx + 1)
|
---|
94 | mArrays.resize(idx + 1);
|
---|
95 |
|
---|
96 | mArrays[idx] = ArrayParam(vals, 2, numElements);
|
---|
97 | }
|
---|
98 |
|
---|
99 |
|
---|
100 | void GPUProgramParameters::SetArray3f(int idx, float *vals, int numElements)
|
---|
101 | {
|
---|
102 | if (mArrays.size() < idx + 1)
|
---|
103 | mArrays.resize(idx + 1);
|
---|
104 |
|
---|
105 | mArrays[idx] = ArrayParam(vals, 3, numElements);
|
---|
106 | }
|
---|
107 |
|
---|
108 |
|
---|
109 | void GPUProgramParameters::SetTexture(int idx, unsigned int tex)
|
---|
110 | {
|
---|
111 | if (mTextures.size() < idx + 1)
|
---|
112 | mTextures.resize(idx + 1);
|
---|
113 |
|
---|
114 | mTextures[idx] = IntParam(tex);
|
---|
115 | }
|
---|
116 |
|
---|
117 |
|
---|
118 | void GPUProgramParameters::SetMatrix(int idx, Matrix4x4 *mat)
|
---|
119 | {
|
---|
120 | if (mMatrices.size() < idx + 1)
|
---|
121 | mMatrices.resize(idx + 1);
|
---|
122 |
|
---|
123 | mMatrices[idx] = MatrixParam(mat);
|
---|
124 | }
|
---|
125 |
|
---|
126 |
|
---|
127 | void GPUProgramParameters::SetTimerParam(int idx)
|
---|
128 | {
|
---|
129 | mTimerParam = idx;
|
---|
130 | }
|
---|
131 |
|
---|
132 |
|
---|
133 | void GPUProgramParameters::SetViewMatrixParam(int idx)
|
---|
134 | {
|
---|
135 | mViewMatrixParam = idx;
|
---|
136 | }
|
---|
137 |
|
---|
138 |
|
---|
139 | void GPUProgramParameters::SetViewVectorParam(int idx)
|
---|
140 | {
|
---|
141 | mViewVectorParam = idx;
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|
145 | void GPUProgramParameters::SetValue1f(const string &name, float val)
|
---|
146 | {
|
---|
147 | SetValue1f(mProgram->GetParamIdxByName(name), val);
|
---|
148 | }
|
---|
149 |
|
---|
150 |
|
---|
151 | void GPUProgramParameters::SetValue2f(const string &name, float val1, float val2)
|
---|
152 | {
|
---|
153 | SetValue2f(mProgram->GetParamIdxByName(name), val1, val2);
|
---|
154 | }
|
---|
155 |
|
---|
156 |
|
---|
157 | void GPUProgramParameters::SetValue3f(const string &name, float val1, float val2, float val3)
|
---|
158 | {
|
---|
159 | SetValue3f(mProgram->GetParamIdxByName(name), val1, val2, val3);
|
---|
160 | }
|
---|
161 |
|
---|
162 |
|
---|
163 | void GPUProgramParameters::SetArray1f(const string &name, float *vals, int numElements)
|
---|
164 | {
|
---|
165 | SetArray1f(mProgram->GetParamIdxByName(name), vals, numElements);
|
---|
166 | }
|
---|
167 |
|
---|
168 |
|
---|
169 | void GPUProgramParameters::SetArray2f(const string &name, float *vals, int numElements)
|
---|
170 | {
|
---|
171 | SetArray2f(mProgram->GetParamIdxByName(name), vals, numElements);
|
---|
172 | }
|
---|
173 |
|
---|
174 |
|
---|
175 | void GPUProgramParameters::SetArray3f(const string &name, float *vals, int numElements)
|
---|
176 | {
|
---|
177 | SetArray3f(mProgram->GetParamIdxByName(name), vals, numElements);
|
---|
178 | }
|
---|
179 |
|
---|
180 |
|
---|
181 | void GPUProgramParameters::SetMatrix(const string &name, Matrix4x4 *mat)
|
---|
182 | {
|
---|
183 | SetMatrix(mProgram->GetParamIdxByName(name), mat);
|
---|
184 | }
|
---|
185 |
|
---|
186 |
|
---|
187 | void GPUProgramParameters::SetTexture(const string &name, unsigned int tex)
|
---|
188 | {
|
---|
189 | SetTexture(mProgram->GetParamIdxByName(name), tex);
|
---|
190 | }
|
---|
191 |
|
---|
192 |
|
---|
193 | void GPUProgramParameters::UpdateParameters()
|
---|
194 | {
|
---|
195 | if (!mProgram) return;
|
---|
196 |
|
---|
197 | for (int i = 0; i < (int)mFloats.size(); ++ i)
|
---|
198 | {
|
---|
199 | const FloatParam &p = mFloats[i];
|
---|
200 |
|
---|
201 | if (!p.mValid) continue;
|
---|
202 |
|
---|
203 | switch (p.mNumComponents)
|
---|
204 | {
|
---|
205 | case 1:
|
---|
206 | mProgram->SetValue1f(i, p.mValues[0]);
|
---|
207 | break;
|
---|
208 | case 2:
|
---|
209 | mProgram->SetValue2f(i, p.mValues[0], p.mValues[1]);
|
---|
210 | break;
|
---|
211 | case 3:
|
---|
212 | mProgram->SetValue3f(i, p.mValues[0], p.mValues[1], p.mValues[2]);
|
---|
213 | break;
|
---|
214 | default:
|
---|
215 | mProgram->SetValue1f(i, p.mValues[0]);
|
---|
216 | }
|
---|
217 | }
|
---|
218 |
|
---|
219 | for (int i = 0; i < (int)mTextures.size(); ++ i)
|
---|
220 | {
|
---|
221 | const IntParam &p = mTextures[i];
|
---|
222 |
|
---|
223 | if (p.mValid)
|
---|
224 | mProgram->SetTexture(i, p.mValue);
|
---|
225 | }
|
---|
226 |
|
---|
227 | for (int i = 0; i < (int)mMatrices.size(); ++ i)
|
---|
228 | {
|
---|
229 | const MatrixParam &p = mMatrices[i];
|
---|
230 |
|
---|
231 | if (p.mValid)
|
---|
232 | mProgram->SetMatrix(i, *p.mValue);
|
---|
233 | }
|
---|
234 |
|
---|
235 | for (int i = 0; i < (int)mArrays.size(); ++ i)
|
---|
236 | {
|
---|
237 | const ArrayParam &p = mArrays[i];
|
---|
238 |
|
---|
239 | if (!p.mValid) continue;
|
---|
240 |
|
---|
241 | switch (p.mNumComponents)
|
---|
242 | {
|
---|
243 | case 1:
|
---|
244 | mProgram->SetArray1f(i, p.mValues, p.mNumEntries);
|
---|
245 | break;
|
---|
246 | case 2:
|
---|
247 | mProgram->SetArray2f(i, p.mValues, p.mNumEntries);
|
---|
248 | break;
|
---|
249 | case 3:
|
---|
250 | mProgram->SetArray3f(i, p.mValues, p.mNumEntries);
|
---|
251 | break;
|
---|
252 | default:
|
---|
253 | mProgram->SetArray1f(i, p.mValues, p.mNumEntries);
|
---|
254 | }
|
---|
255 | }
|
---|
256 |
|
---|
257 | if (mTimerParam >= 0)
|
---|
258 | mProgram->SetValue1f(mTimerParam, sCurrentTimer);
|
---|
259 |
|
---|
260 | if (mViewVectorParam >= 0)
|
---|
261 | mProgram->SetValue3f(mViewVectorParam, sCurrentViewVector.x, sCurrentViewVector.y, sCurrentViewVector.z);
|
---|
262 |
|
---|
263 | if (mViewMatrixParam >= 0)
|
---|
264 | mProgram->SetMatrix(mViewMatrixParam, sCurrentViewMatrix);
|
---|
265 | }
|
---|
266 |
|
---|
267 |
|
---|
268 | void GPUProgramParameters::InitFrame(Camera *cam)
|
---|
269 | {
|
---|
270 | static PerfTimer mytimer;
|
---|
271 | sCurrentTimer = mytimer.Elapsedms(false) * M_PI / 180.0f;
|
---|
272 |
|
---|
273 | cam->GetModelViewMatrix(sCurrentViewMatrix);
|
---|
274 | sCurrentViewVector = cam->GetDirection();
|
---|
275 | }
|
---|
276 |
|
---|
277 |
|
---|
278 |
|
---|
279 | /********************************************************/
|
---|
280 | /* ShaderProgram implementation */
|
---|
281 | /********************************************************/
|
---|
282 |
|
---|
283 |
|
---|
284 | ShaderProgram::ShaderProgram(CGcontext context,
|
---|
285 | const std::string &filename,
|
---|
286 | CGprofile profile,
|
---|
287 | const std::string &functionName)
|
---|
288 | {
|
---|
289 | mProgram = cgCreateProgramFromFile(context,
|
---|
290 | CG_SOURCE,
|
---|
291 | filename.c_str(),
|
---|
292 | profile,
|
---|
293 | functionName.c_str(),
|
---|
294 | NULL);
|
---|
295 |
|
---|
296 | if (mProgram) cgGLLoadProgram(mProgram);
|
---|
297 |
|
---|
298 | const int maxParams = 64;
|
---|
299 | mParameters.resize(maxParams);
|
---|
300 |
|
---|
301 | for (int i = 0; i < maxParams; ++ i)
|
---|
302 | {
|
---|
303 | mParameters[i] = NULL;
|
---|
304 | }
|
---|
305 | }
|
---|
306 |
|
---|
307 |
|
---|
308 | void ShaderProgram::Bind()
|
---|
309 | {
|
---|
310 | cgGLBindProgram(mProgram);
|
---|
311 |
|
---|
312 | // enable all texture parameters
|
---|
313 | CGParameterArray::const_iterator it, it_end = mTextureParams.end();
|
---|
314 |
|
---|
315 | for (it = mTextureParams.begin(); it != it_end; ++ it)
|
---|
316 | cgGLEnableTextureParameter(*it);
|
---|
317 | }
|
---|
318 |
|
---|
319 |
|
---|
320 | /*void ShaderProgram::Release()
|
---|
321 | {
|
---|
322 | CGParameterArray::const_iterator it, it_end = mTextureParams.end();
|
---|
323 |
|
---|
324 | // disable all texture parameters
|
---|
325 | for (it = mTextureParams.begin(); it != it_end; ++ it)
|
---|
326 | cgGLDisableTextureParameter(*it);
|
---|
327 | }*/
|
---|
328 |
|
---|
329 |
|
---|
330 | CGparameter ShaderProgram::AddParameter(const string &name, int idx)
|
---|
331 | {
|
---|
332 | CGparameter p = GetOrCreateParameter(name);
|
---|
333 |
|
---|
334 | mParamHash[name] = idx;
|
---|
335 | mParameters[idx] = p;
|
---|
336 |
|
---|
337 | return p;
|
---|
338 | }
|
---|
339 |
|
---|
340 |
|
---|
341 | CGparameter ShaderProgram::GetOrCreateParameter(const string &name)
|
---|
342 | {
|
---|
343 | CGparameter p;
|
---|
344 | CGParameterMap::const_iterator it = mParamHash.find(name);
|
---|
345 |
|
---|
346 | if (it == mParamHash.end())
|
---|
347 | {
|
---|
348 | p = cgGetNamedParameter(mProgram, name.c_str());
|
---|
349 | }
|
---|
350 | else
|
---|
351 | {
|
---|
352 | p = mParameters[(*it).second];
|
---|
353 | }
|
---|
354 |
|
---|
355 | //cout << "name: " << name << " " << p << " " << mProgram << endl;
|
---|
356 | return p;
|
---|
357 | }
|
---|
358 |
|
---|
359 |
|
---|
360 | CGparameter ShaderProgram::GetParameter(int idx) const
|
---|
361 | {
|
---|
362 | return mParameters[idx];
|
---|
363 | }
|
---|
364 |
|
---|
365 |
|
---|
366 | void ShaderProgram::SetValue1f(int idx, float val)
|
---|
367 | {
|
---|
368 | cgGLSetParameter1f(GetParameter(idx), val);
|
---|
369 | }
|
---|
370 |
|
---|
371 |
|
---|
372 | void ShaderProgram::SetValue2f(int idx, float val1, float val2)
|
---|
373 | {
|
---|
374 | cgGLSetParameter2f(GetParameter(idx), val1, val2);
|
---|
375 | }
|
---|
376 |
|
---|
377 |
|
---|
378 | void ShaderProgram::SetValue3f(int idx, float val1, float val2, float val3)
|
---|
379 | {
|
---|
380 | cgGLSetParameter3f(GetParameter(idx), val1, val2, val3);
|
---|
381 | }
|
---|
382 |
|
---|
383 |
|
---|
384 | void ShaderProgram::SetArray1f(int idx, float *vals, int numElements)
|
---|
385 | {
|
---|
386 | cgGLSetParameterArray1f(GetParameter(idx), 0, numElements, (const float *)vals);
|
---|
387 | }
|
---|
388 |
|
---|
389 |
|
---|
390 | void ShaderProgram::SetArray2f(int idx, float *vals, int numElements)
|
---|
391 | {
|
---|
392 | cgGLSetParameterArray2f(GetParameter(idx), 0, numElements, (const float *)vals);
|
---|
393 | }
|
---|
394 |
|
---|
395 |
|
---|
396 | void ShaderProgram::SetArray3f(int idx, float *vals, int numElements)
|
---|
397 | {
|
---|
398 | cgGLSetParameterArray3f(GetParameter(idx), 0, numElements, (const float *)vals);
|
---|
399 | }
|
---|
400 |
|
---|
401 |
|
---|
402 | void ShaderProgram::SetMatrix(int idx, const Matrix4x4 &mat)
|
---|
403 | {
|
---|
404 | cgGLSetMatrixParameterfc(GetParameter(idx), (const float *)mat.x);
|
---|
405 | }
|
---|
406 |
|
---|
407 |
|
---|
408 | void ShaderProgram::SetTexture(int idx, unsigned int tex)
|
---|
409 | {
|
---|
410 | CGparameter p = GetParameter(idx);
|
---|
411 |
|
---|
412 | cgGLSetTextureParameter(p, tex);
|
---|
413 | cgGLEnableTextureParameter(p);
|
---|
414 | }
|
---|
415 |
|
---|
416 |
|
---|
417 | void ShaderProgram::SetValue1f(const string &name, float val)
|
---|
418 | {
|
---|
419 | cgGLSetParameter1f(GetOrCreateParameter(name), val);
|
---|
420 | }
|
---|
421 |
|
---|
422 |
|
---|
423 | void ShaderProgram::SetValue2f(const string &name, float val1, float val2)
|
---|
424 | {
|
---|
425 | cgGLSetParameter2f(GetOrCreateParameter(name), val1, val2);
|
---|
426 | }
|
---|
427 |
|
---|
428 |
|
---|
429 | void ShaderProgram::SetValue3f(const string &name, float val1, float val2, float val3)
|
---|
430 | {
|
---|
431 | cgGLSetParameter3f(GetOrCreateParameter(name), val1, val2, val3);
|
---|
432 | }
|
---|
433 |
|
---|
434 |
|
---|
435 | void ShaderProgram::SetArray1f(const string &name, float *vals, int numElements)
|
---|
436 | {
|
---|
437 | cgGLSetParameterArray1f(GetOrCreateParameter(name), 0, numElements, (const float *)vals);
|
---|
438 | }
|
---|
439 |
|
---|
440 |
|
---|
441 | void ShaderProgram::SetArray2f(const string &name, float *vals, int numElements)
|
---|
442 | {
|
---|
443 | cgGLSetParameterArray2f(GetOrCreateParameter(name), 0, numElements, (const float *)vals);
|
---|
444 | }
|
---|
445 |
|
---|
446 |
|
---|
447 | void ShaderProgram::SetArray3f(const string &name, float *vals, int numElements)
|
---|
448 | {
|
---|
449 | cgGLSetParameterArray3f(GetOrCreateParameter(name), 0, numElements, (const float *)vals);
|
---|
450 | }
|
---|
451 |
|
---|
452 |
|
---|
453 | void ShaderProgram::SetMatrix(const string &name, const Matrix4x4 &mat)
|
---|
454 | {
|
---|
455 | cgGLSetMatrixParameterfc(GetOrCreateParameter(name), (const float *)mat.x);
|
---|
456 | }
|
---|
457 |
|
---|
458 |
|
---|
459 | void ShaderProgram::SetTexture(const string &name, unsigned int tex)
|
---|
460 | {
|
---|
461 | CGparameter p = GetOrCreateParameter(name);
|
---|
462 |
|
---|
463 | cgGLSetTextureParameter(p, tex);
|
---|
464 | cgGLEnableTextureParameter(p);
|
---|
465 |
|
---|
466 | mTextureParams.push_back(p);
|
---|
467 | }
|
---|
468 |
|
---|
469 |
|
---|
470 | int ShaderProgram::GetParamIdxByName(const std::string &name) const
|
---|
471 | {
|
---|
472 | CGParameterMap::const_iterator it = mParamHash.find(name);
|
---|
473 | return (*it).second;
|
---|
474 | }
|
---|
475 |
|
---|
476 |
|
---|
477 | } // namespace |
---|