1 | #include "ShaderProgram.h"
|
---|
2 | #include "Matrix4x4.h"
|
---|
3 |
|
---|
4 |
|
---|
5 | using namespace std;
|
---|
6 |
|
---|
7 | namespace CHCDemoEngine
|
---|
8 | {
|
---|
9 |
|
---|
10 |
|
---|
11 | GPUProgramParameters::GPUProgramParameters(ShaderProgram *p)
|
---|
12 | : mProgram(p)
|
---|
13 | {
|
---|
14 | }
|
---|
15 |
|
---|
16 | void GPUProgramParameters::SetValue1f(int idx, float value)
|
---|
17 | {
|
---|
18 | }
|
---|
19 |
|
---|
20 |
|
---|
21 | void GPUProgramParameters::SetValue2f(int idx, float val1, float val2)
|
---|
22 | {
|
---|
23 | }
|
---|
24 |
|
---|
25 |
|
---|
26 | void GPUProgramParameters::SetValue3f(int idx, float val1, float val, float val3)
|
---|
27 | {
|
---|
28 | }
|
---|
29 |
|
---|
30 |
|
---|
31 | void GPUProgramParameters::SetArray1f(int idx, float *vals, int numElements)
|
---|
32 | {
|
---|
33 | }
|
---|
34 |
|
---|
35 |
|
---|
36 | void GPUProgramParameters::SetArray2f(int idx, float *vals, int numElements)
|
---|
37 | {
|
---|
38 | }
|
---|
39 |
|
---|
40 |
|
---|
41 | void GPUProgramParameters::SetArray3f(int idx, float *vals, int numElements)
|
---|
42 | {
|
---|
43 | }
|
---|
44 |
|
---|
45 |
|
---|
46 | void GPUProgramParameters::SetTexture(int idx, unsigned int tex)
|
---|
47 | {
|
---|
48 | mTextures.push_back(IntParam(idx, tex));
|
---|
49 | }
|
---|
50 |
|
---|
51 |
|
---|
52 | void GPUProgramParameters::SetMatrix(int idx, Matrix4x4 *mat)
|
---|
53 | {
|
---|
54 | }
|
---|
55 |
|
---|
56 |
|
---|
57 | void GPUProgramParameters::SetValue1f(const string &name, float val)
|
---|
58 | {
|
---|
59 | SetValue1f(mProgram->GetParamIdxByName(name), val);
|
---|
60 | }
|
---|
61 |
|
---|
62 |
|
---|
63 | void GPUProgramParameters::SetValue2f(const string &name, float val1, float val2)
|
---|
64 | {
|
---|
65 | SetValue2f(mProgram->GetParamIdxByName(name), val1, val2);
|
---|
66 | }
|
---|
67 |
|
---|
68 |
|
---|
69 | void GPUProgramParameters::SetValue3f(const string &name, float val1, float val2, float val3)
|
---|
70 | {
|
---|
71 | SetValue3f(mProgram->GetParamIdxByName(name), val1, val2, val3);
|
---|
72 | }
|
---|
73 |
|
---|
74 |
|
---|
75 | void GPUProgramParameters::SetArray1f(const string &name, float *vals, int numElements)
|
---|
76 | {
|
---|
77 | SetArray1f(mProgram->GetParamIdxByName(name), vals, numElements);
|
---|
78 | }
|
---|
79 |
|
---|
80 |
|
---|
81 | void GPUProgramParameters::SetArray2f(const string &name, float *vals, int numElements)
|
---|
82 | {
|
---|
83 | SetArray2f(mProgram->GetParamIdxByName(name), vals, numElements);
|
---|
84 | }
|
---|
85 |
|
---|
86 |
|
---|
87 | void GPUProgramParameters::SetArray3f(const string &name, float *vals, int numElements)
|
---|
88 | {
|
---|
89 | SetArray3f(mProgram->GetParamIdxByName(name), vals, numElements);
|
---|
90 | }
|
---|
91 |
|
---|
92 |
|
---|
93 | void GPUProgramParameters::SetMatrix(const string &name, Matrix4x4 *mat)
|
---|
94 | {
|
---|
95 | SetMatrix(mProgram->GetParamIdxByName(name), mat);
|
---|
96 | }
|
---|
97 |
|
---|
98 |
|
---|
99 | void GPUProgramParameters::SetTexture(const string &name, unsigned int tex)
|
---|
100 | {
|
---|
101 | SetTexture(mProgram->GetParamIdxByName(name), tex);
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 | void GPUProgramParameters::UpdateParameters()
|
---|
106 | {
|
---|
107 | for (size_t i = 0; i < mFloats.size(); ++ i)
|
---|
108 | {
|
---|
109 | const FloatParam &p = mFloats[i];
|
---|
110 |
|
---|
111 | switch (p.mNumComponents)
|
---|
112 | {
|
---|
113 | case 1:
|
---|
114 | mProgram->SetValue1f(p.mIndex, p.mValues[0]);
|
---|
115 | break;
|
---|
116 | case 2:
|
---|
117 | mProgram->SetValue2f(p.mIndex, p.mValues[0], p.mValues[1]);
|
---|
118 | break;
|
---|
119 | case 3:
|
---|
120 | mProgram->SetValue3f(p.mIndex, p.mValues[0], p.mValues[1], p.mValues[2]);
|
---|
121 | break;
|
---|
122 | default:
|
---|
123 | mProgram->SetValue1f(p.mIndex, p.mValues[0]);
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | for (size_t i = 0; i < mTextures.size(); ++ i)
|
---|
128 | {
|
---|
129 | const IntParam &p = mTextures[i];
|
---|
130 | mProgram->SetTexture(p.mIndex, p.mValue);
|
---|
131 | }
|
---|
132 |
|
---|
133 | for (size_t i = 0; i < mMatrices.size(); ++ i)
|
---|
134 | {
|
---|
135 | const MatrixParam &p = mMatrices[i];
|
---|
136 | mProgram->SetMatrix(p.mIndex, *p.mValue);
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 |
|
---|
141 |
|
---|
142 | /********************************************************/
|
---|
143 | /* ShaderProgram implementation */
|
---|
144 | /********************************************************/
|
---|
145 |
|
---|
146 |
|
---|
147 | ShaderProgram::ShaderProgram(CGcontext context,
|
---|
148 | const std::string &filename,
|
---|
149 | CGprofile profile,
|
---|
150 | const std::string &functionName)
|
---|
151 | {
|
---|
152 | mProgram = cgCreateProgramFromFile(context,
|
---|
153 | CG_SOURCE,
|
---|
154 | filename.c_str(),
|
---|
155 | profile,
|
---|
156 | functionName.c_str(),
|
---|
157 | NULL);
|
---|
158 |
|
---|
159 | if (mProgram) cgGLLoadProgram(mProgram);
|
---|
160 |
|
---|
161 | const int maxParams = 64;
|
---|
162 | mParameters.resize(maxParams);
|
---|
163 |
|
---|
164 | for (int i = 0; i < maxParams; ++ i)
|
---|
165 | {
|
---|
166 | mParameters[i] = NULL;
|
---|
167 | }
|
---|
168 | }
|
---|
169 |
|
---|
170 |
|
---|
171 | void ShaderProgram::Bind()
|
---|
172 | {
|
---|
173 | cgGLBindProgram(mProgram);
|
---|
174 |
|
---|
175 | // enable all texture parameters
|
---|
176 | CGParameterArray::const_iterator it, it_end = mTextureParams.end();
|
---|
177 |
|
---|
178 | for (it = mTextureParams.begin(); it != it_end; ++ it)
|
---|
179 | cgGLEnableTextureParameter(*it);
|
---|
180 | }
|
---|
181 |
|
---|
182 |
|
---|
183 | /*void ShaderProgram::Release()
|
---|
184 | {
|
---|
185 | CGParameterArray::const_iterator it, it_end = mTextureParams.end();
|
---|
186 |
|
---|
187 | // disable all texture parameters
|
---|
188 | for (it = mTextureParams.begin(); it != it_end; ++ it)
|
---|
189 | cgGLDisableTextureParameter(*it);
|
---|
190 | }*/
|
---|
191 |
|
---|
192 |
|
---|
193 | CGparameter ShaderProgram::AddParameter(const string &name, int idx)
|
---|
194 | {
|
---|
195 | CGparameter p = GetOrCreateParameter(name);
|
---|
196 |
|
---|
197 | mParamHash[name] = idx;
|
---|
198 | mParameters[idx] = p;
|
---|
199 |
|
---|
200 | return p;
|
---|
201 | }
|
---|
202 |
|
---|
203 |
|
---|
204 | CGparameter ShaderProgram::GetOrCreateParameter(const string &name)
|
---|
205 | {
|
---|
206 | CGparameter p;
|
---|
207 | CGParameterMap::const_iterator it = mParamHash.find(name);
|
---|
208 |
|
---|
209 | if (it == mParamHash.end())
|
---|
210 | {
|
---|
211 | p = cgGetNamedParameter(mProgram, name.c_str());
|
---|
212 | }
|
---|
213 | else
|
---|
214 | {
|
---|
215 | p = mParameters[(*it).second];
|
---|
216 | }
|
---|
217 |
|
---|
218 | //cout << "name: " << name << " " << p << " " << mProgram << endl;
|
---|
219 | return p;
|
---|
220 | }
|
---|
221 |
|
---|
222 |
|
---|
223 | CGparameter ShaderProgram::GetParameter(int idx) const
|
---|
224 | {
|
---|
225 | return mParameters[idx];
|
---|
226 | }
|
---|
227 |
|
---|
228 |
|
---|
229 | void ShaderProgram::SetValue1f(int idx, float val)
|
---|
230 | {
|
---|
231 | cgGLSetParameter1f(GetParameter(idx), val);
|
---|
232 | }
|
---|
233 |
|
---|
234 |
|
---|
235 | void ShaderProgram::SetValue2f(int idx, float val1, float val2)
|
---|
236 | {
|
---|
237 | cgGLSetParameter2f(GetParameter(idx), val1, val2);
|
---|
238 | }
|
---|
239 |
|
---|
240 |
|
---|
241 | void ShaderProgram::SetValue3f(int idx, float val1, float val2, float val3)
|
---|
242 | {
|
---|
243 | cgGLSetParameter3f(GetParameter(idx), val1, val2, val3);
|
---|
244 | }
|
---|
245 |
|
---|
246 |
|
---|
247 | void ShaderProgram::SetArray1f(int idx, float *vals, int numElements)
|
---|
248 | {
|
---|
249 | cgGLSetParameterArray1f(GetParameter(idx), 0, numElements, (const float *)vals);
|
---|
250 | }
|
---|
251 |
|
---|
252 |
|
---|
253 | void ShaderProgram::SetArray2f(int idx, float *vals, int numElements)
|
---|
254 | {
|
---|
255 | cgGLSetParameterArray2f(GetParameter(idx), 0, numElements, (const float *)vals);
|
---|
256 | }
|
---|
257 |
|
---|
258 |
|
---|
259 | void ShaderProgram::SetArray3f(int idx, float *vals, int numElements)
|
---|
260 | {
|
---|
261 | cgGLSetParameterArray3f(GetParameter(idx), 0, numElements, (const float *)vals);
|
---|
262 | }
|
---|
263 |
|
---|
264 |
|
---|
265 | void ShaderProgram::SetMatrix(int idx, const Matrix4x4 &mat)
|
---|
266 | {
|
---|
267 | cgGLSetMatrixParameterfc(GetParameter(idx), (const float *)mat.x);
|
---|
268 | }
|
---|
269 |
|
---|
270 |
|
---|
271 | void ShaderProgram::SetTexture(int idx, unsigned int tex)
|
---|
272 | {
|
---|
273 | CGparameter p = GetParameter(idx);
|
---|
274 |
|
---|
275 | cgGLSetTextureParameter(p, tex);
|
---|
276 | cgGLEnableTextureParameter(p);
|
---|
277 | }
|
---|
278 |
|
---|
279 |
|
---|
280 | void ShaderProgram::SetValue1f(const string &name, float val)
|
---|
281 | {
|
---|
282 | cgGLSetParameter1f(GetOrCreateParameter(name), val);
|
---|
283 | }
|
---|
284 |
|
---|
285 |
|
---|
286 | void ShaderProgram::SetValue2f(const string &name, float val1, float val2)
|
---|
287 | {
|
---|
288 | cgGLSetParameter2f(GetOrCreateParameter(name), val1, val2);
|
---|
289 | }
|
---|
290 |
|
---|
291 |
|
---|
292 | void ShaderProgram::SetValue3f(const string &name, float val1, float val2, float val3)
|
---|
293 | {
|
---|
294 | cgGLSetParameter3f(GetOrCreateParameter(name), val1, val2, val3);
|
---|
295 | }
|
---|
296 |
|
---|
297 |
|
---|
298 | void ShaderProgram::SetArray1f(const string &name, float *vals, int numElements)
|
---|
299 | {
|
---|
300 | cgGLSetParameterArray1f(GetOrCreateParameter(name), 0, numElements, (const float *)vals);
|
---|
301 | }
|
---|
302 |
|
---|
303 |
|
---|
304 | void ShaderProgram::SetArray2f(const string &name, float *vals, int numElements)
|
---|
305 | {
|
---|
306 | cgGLSetParameterArray2f(GetOrCreateParameter(name), 0, numElements, (const float *)vals);
|
---|
307 | }
|
---|
308 |
|
---|
309 |
|
---|
310 | void ShaderProgram::SetArray3f(const string &name, float *vals, int numElements)
|
---|
311 | {
|
---|
312 | cgGLSetParameterArray3f(GetOrCreateParameter(name), 0, numElements, (const float *)vals);
|
---|
313 | }
|
---|
314 |
|
---|
315 |
|
---|
316 | void ShaderProgram::SetMatrix(const string &name, const Matrix4x4 &mat)
|
---|
317 | {
|
---|
318 | cgGLSetMatrixParameterfc(GetOrCreateParameter(name), (const float *)mat.x);
|
---|
319 | }
|
---|
320 |
|
---|
321 |
|
---|
322 | void ShaderProgram::SetTexture(const string &name, unsigned int tex)
|
---|
323 | {
|
---|
324 | CGparameter p = GetOrCreateParameter(name);
|
---|
325 |
|
---|
326 | cgGLSetTextureParameter(p, tex);
|
---|
327 | cgGLEnableTextureParameter(p);
|
---|
328 |
|
---|
329 | mTextureParams.push_back(p);
|
---|
330 | }
|
---|
331 |
|
---|
332 |
|
---|
333 | int ShaderProgram::GetParamIdxByName(const std::string &name) const
|
---|
334 | {
|
---|
335 | CGParameterMap::const_iterator it = mParamHash.find(name);
|
---|
336 | return (*it).second;
|
---|
337 | }
|
---|
338 |
|
---|
339 |
|
---|
340 | } // namespace |
---|