rev |
line source |
nuclear@0
|
1 #include <stdio.h>
|
nuclear@0
|
2 #include <string.h>
|
nuclear@0
|
3 #include <stdarg.h>
|
nuclear@0
|
4 #include <errno.h>
|
nuclear@0
|
5 #include "opengl.h"
|
nuclear@0
|
6 #include "shader.h"
|
nuclear@0
|
7 #include "logger.h"
|
nuclear@0
|
8 #include "unistate.h"
|
nuclear@0
|
9 #include "mesh.h"
|
nuclear@0
|
10
|
nuclear@0
|
11 #ifdef _MSC_VER
|
nuclear@0
|
12 #include <malloc.h>
|
nuclear@0
|
13 #else
|
nuclear@0
|
14 #include <alloca.h>
|
nuclear@0
|
15 #endif
|
nuclear@0
|
16
|
nuclear@15
|
17 using namespace goatgfx;
|
nuclear@15
|
18
|
nuclear@0
|
19 #ifdef __GLEW_H__
|
nuclear@0
|
20 #define HAVE_GEOMETRY_SHADER
|
nuclear@0
|
21 #define HAVE_TESSELATION_SHADER
|
nuclear@0
|
22 #endif
|
nuclear@0
|
23
|
nuclear@0
|
24 static void bind_standard_attr(const ShaderProg *prog);
|
nuclear@0
|
25 static const char *strtype(unsigned int type);
|
nuclear@0
|
26
|
nuclear@0
|
27 ShaderProg *ShaderProg::current;
|
nuclear@0
|
28
|
nuclear@0
|
29 Shader::Shader()
|
nuclear@0
|
30 {
|
nuclear@0
|
31 sdr = type = 0;
|
nuclear@0
|
32 name = 0;
|
nuclear@0
|
33 }
|
nuclear@0
|
34
|
nuclear@0
|
35 Shader::~Shader()
|
nuclear@0
|
36 {
|
nuclear@0
|
37 destroy();
|
nuclear@0
|
38 }
|
nuclear@0
|
39
|
nuclear@0
|
40 unsigned int Shader::get_id() const
|
nuclear@0
|
41 {
|
nuclear@0
|
42 return sdr;
|
nuclear@0
|
43 }
|
nuclear@0
|
44
|
nuclear@0
|
45 void Shader::set_name(const char *name)
|
nuclear@0
|
46 {
|
nuclear@0
|
47 delete [] this->name;
|
nuclear@0
|
48 this->name = new char[strlen(name) + 1];
|
nuclear@0
|
49 strcpy(this->name, name);
|
nuclear@0
|
50 }
|
nuclear@0
|
51
|
nuclear@0
|
52 const char *Shader::get_name() const
|
nuclear@0
|
53 {
|
nuclear@0
|
54 return name;
|
nuclear@0
|
55 }
|
nuclear@0
|
56
|
nuclear@0
|
57 bool Shader::create(const char *src, unsigned int type)
|
nuclear@0
|
58 {
|
nuclear@0
|
59 #if !GL_ES_VERSION_2_0
|
nuclear@0
|
60 const char *src_arr[] = {src};
|
nuclear@0
|
61 #else
|
nuclear@0
|
62 const char *src_arr[] = { "precision mediump float; ", src };
|
nuclear@0
|
63 #endif
|
nuclear@0
|
64
|
nuclear@0
|
65 if(!sdr) {
|
nuclear@0
|
66 sdr = glCreateShader(type);
|
nuclear@0
|
67 }
|
nuclear@0
|
68
|
nuclear@0
|
69 info_log("compiling shader: %s... ", name ? name : "");
|
nuclear@0
|
70
|
nuclear@0
|
71 glShaderSource(sdr, sizeof src_arr / sizeof *src_arr, src_arr, 0);
|
nuclear@0
|
72 glCompileShader(sdr);
|
nuclear@0
|
73
|
nuclear@0
|
74 int status;
|
nuclear@0
|
75 glGetShaderiv(sdr, GL_COMPILE_STATUS, &status);
|
nuclear@0
|
76
|
nuclear@0
|
77 info_log(status ? "success\n" : "failed\n");
|
nuclear@0
|
78
|
nuclear@0
|
79 int info_len;
|
nuclear@0
|
80 glGetShaderiv(sdr, GL_INFO_LOG_LENGTH, &info_len);
|
nuclear@0
|
81 if(info_len > 1) {
|
nuclear@0
|
82 char *buf = (char*)alloca(info_len);
|
nuclear@0
|
83 glGetShaderInfoLog(sdr, info_len, 0, buf);
|
nuclear@0
|
84 buf[info_len - 1] = 0;
|
nuclear@0
|
85
|
nuclear@0
|
86 if(status) {
|
nuclear@0
|
87 info_log("%s\n", buf);
|
nuclear@0
|
88 } else {
|
nuclear@0
|
89 error_log("%s\n", buf);
|
nuclear@0
|
90 }
|
nuclear@0
|
91 }
|
nuclear@0
|
92
|
nuclear@0
|
93 return status == GL_TRUE;
|
nuclear@0
|
94 }
|
nuclear@0
|
95
|
nuclear@0
|
96 void Shader::destroy()
|
nuclear@0
|
97 {
|
nuclear@0
|
98 if(sdr) {
|
nuclear@0
|
99 glDeleteShader(sdr);
|
nuclear@0
|
100 }
|
nuclear@0
|
101 sdr = type = 0;
|
nuclear@0
|
102
|
nuclear@0
|
103 delete [] name;
|
nuclear@0
|
104 name = 0;
|
nuclear@0
|
105 }
|
nuclear@0
|
106
|
nuclear@0
|
107 bool Shader::load(const char *fname, unsigned int type)
|
nuclear@0
|
108 {
|
nuclear@0
|
109 FILE *fp;
|
nuclear@0
|
110
|
nuclear@0
|
111 if(!(fp = fopen(fname, "rb"))) {
|
nuclear@0
|
112 error_log("failed to load %s shader: %s: %s\n", strtype(type), fname, strerror(errno));
|
nuclear@0
|
113 return false;
|
nuclear@0
|
114 }
|
nuclear@0
|
115
|
nuclear@0
|
116 fseek(fp, 0, SEEK_END);
|
nuclear@0
|
117 long sz = ftell(fp);
|
nuclear@0
|
118 rewind(fp);
|
nuclear@0
|
119
|
nuclear@0
|
120 char *src = (char*)alloca(sz + 1);
|
nuclear@0
|
121 if(fread(src, 1, sz, fp) < (size_t)sz) {
|
nuclear@0
|
122 error_log("failed to load %s shader: %s: %s\n", strtype(type), fname, strerror(errno));
|
nuclear@0
|
123 fclose(fp);
|
nuclear@0
|
124 return false;
|
nuclear@0
|
125 }
|
nuclear@0
|
126 src[sz] = 0;
|
nuclear@0
|
127 fclose(fp);
|
nuclear@0
|
128
|
nuclear@0
|
129 set_name(fname);
|
nuclear@0
|
130 return create(src, type);
|
nuclear@0
|
131 }
|
nuclear@0
|
132
|
nuclear@0
|
133 // ---- shader program ----
|
nuclear@0
|
134 ShaderProg::ShaderProg()
|
nuclear@0
|
135 {
|
nuclear@0
|
136 prog = 0;
|
nuclear@0
|
137 must_link = true;
|
nuclear@0
|
138 }
|
nuclear@0
|
139
|
nuclear@0
|
140 ShaderProg::~ShaderProg()
|
nuclear@0
|
141 {
|
nuclear@0
|
142 destroy();
|
nuclear@0
|
143 }
|
nuclear@0
|
144
|
nuclear@0
|
145 unsigned int ShaderProg::get_id() const
|
nuclear@0
|
146 {
|
nuclear@0
|
147 return prog;
|
nuclear@0
|
148 }
|
nuclear@0
|
149
|
nuclear@0
|
150 bool ShaderProg::create(const char *src, unsigned int type, ...)
|
nuclear@0
|
151 {
|
nuclear@0
|
152 va_list ap;
|
nuclear@0
|
153
|
nuclear@0
|
154 va_start(ap, type);
|
nuclear@0
|
155 bool res = create(src, type, ap);
|
nuclear@0
|
156 va_end(ap);
|
nuclear@0
|
157
|
nuclear@0
|
158 return res;
|
nuclear@0
|
159 }
|
nuclear@0
|
160
|
nuclear@0
|
161 bool ShaderProg::create(const char *src, unsigned int type, va_list ap)
|
nuclear@0
|
162 {
|
nuclear@0
|
163 destroy();
|
nuclear@0
|
164 prog = glCreateProgram();
|
nuclear@0
|
165
|
nuclear@0
|
166 while(src) {
|
nuclear@0
|
167 Shader *sdr = new Shader;
|
nuclear@0
|
168 if(!sdr->create(src, type)) {
|
nuclear@0
|
169 va_end(ap);
|
nuclear@0
|
170 return false;
|
nuclear@0
|
171 }
|
nuclear@0
|
172 add_shader(sdr);
|
nuclear@0
|
173 src = va_arg(ap, const char*);
|
nuclear@0
|
174 type = va_arg(ap, unsigned int);
|
nuclear@0
|
175 }
|
nuclear@0
|
176 return link();
|
nuclear@0
|
177 }
|
nuclear@0
|
178
|
nuclear@0
|
179 bool ShaderProg::create(const char *vsrc, const char *psrc)
|
nuclear@0
|
180 {
|
nuclear@0
|
181 return create(VSDR(vsrc), PSDR(psrc), 0);
|
nuclear@0
|
182 }
|
nuclear@0
|
183
|
nuclear@0
|
184 bool ShaderProg::create(Shader *sdr, ...)
|
nuclear@0
|
185 {
|
nuclear@0
|
186 va_list ap;
|
nuclear@0
|
187
|
nuclear@0
|
188 va_start(ap, sdr);
|
nuclear@0
|
189 bool res = create(sdr, ap);
|
nuclear@0
|
190 va_end(ap);
|
nuclear@0
|
191
|
nuclear@0
|
192 return res;
|
nuclear@0
|
193 }
|
nuclear@0
|
194
|
nuclear@0
|
195 bool ShaderProg::create(Shader *sdr, va_list ap)
|
nuclear@0
|
196 {
|
nuclear@0
|
197 destroy();
|
nuclear@0
|
198 prog = glCreateProgram();
|
nuclear@0
|
199
|
nuclear@0
|
200 while(sdr) {
|
nuclear@0
|
201 add_shader(sdr);
|
nuclear@0
|
202 sdr = va_arg(ap, Shader*);
|
nuclear@0
|
203 }
|
nuclear@0
|
204 return link();
|
nuclear@0
|
205 }
|
nuclear@0
|
206
|
nuclear@0
|
207 bool ShaderProg::create(Shader *vsdr, Shader *psdr)
|
nuclear@0
|
208 {
|
nuclear@0
|
209 return create(vsdr, psdr, 0);
|
nuclear@0
|
210 }
|
nuclear@0
|
211
|
nuclear@0
|
212 void ShaderProg::destroy()
|
nuclear@0
|
213 {
|
nuclear@0
|
214 if(prog) {
|
nuclear@0
|
215 glDeleteProgram(prog);
|
nuclear@0
|
216 }
|
nuclear@0
|
217 prog = 0;
|
nuclear@0
|
218
|
nuclear@0
|
219 shaders.clear();
|
nuclear@0
|
220 // don't actually destroy the shaders, let the ShaderSet own them
|
nuclear@0
|
221 }
|
nuclear@0
|
222
|
nuclear@0
|
223 bool ShaderProg::load(const char *fname, unsigned int type, ...)
|
nuclear@0
|
224 {
|
nuclear@0
|
225 va_list ap;
|
nuclear@0
|
226 va_start(ap, type);
|
nuclear@0
|
227 bool res = load(fname, type, ap);
|
nuclear@0
|
228 va_end(ap);
|
nuclear@0
|
229
|
nuclear@0
|
230 return res;
|
nuclear@0
|
231 }
|
nuclear@0
|
232
|
nuclear@0
|
233 bool ShaderProg::load(const char *fname, unsigned int type, va_list ap)
|
nuclear@0
|
234 {
|
nuclear@0
|
235 destroy();
|
nuclear@0
|
236 prog = glCreateProgram();
|
nuclear@0
|
237
|
nuclear@0
|
238 while(fname) {
|
nuclear@0
|
239 Shader *sdr = new Shader;
|
nuclear@0
|
240 if(!sdr->load(fname, type)) {
|
nuclear@0
|
241 delete sdr;
|
nuclear@0
|
242 return false;
|
nuclear@0
|
243 }
|
nuclear@0
|
244 add_shader(sdr);
|
nuclear@0
|
245
|
nuclear@0
|
246 if((fname = va_arg(ap, const char*))) {
|
nuclear@0
|
247 type = va_arg(ap, unsigned int);
|
nuclear@0
|
248 }
|
nuclear@0
|
249 }
|
nuclear@0
|
250
|
nuclear@0
|
251 return link();
|
nuclear@0
|
252 }
|
nuclear@0
|
253
|
nuclear@0
|
254 bool ShaderProg::load(const char *vfname, const char *pfname)
|
nuclear@0
|
255 {
|
nuclear@0
|
256 return load(VSDR(vfname), PSDR(pfname), 0);
|
nuclear@0
|
257 }
|
nuclear@0
|
258
|
nuclear@0
|
259 void ShaderProg::add_shader(Shader *sdr)
|
nuclear@0
|
260 {
|
nuclear@0
|
261 glAttachShader(prog, sdr->get_id());
|
nuclear@0
|
262 }
|
nuclear@0
|
263
|
nuclear@0
|
264 bool ShaderProg::link() const
|
nuclear@0
|
265 {
|
nuclear@0
|
266 bind_standard_attr(this);
|
nuclear@0
|
267
|
nuclear@0
|
268 CHECKGLERR;
|
nuclear@0
|
269 info_log("linking program ... ");
|
nuclear@0
|
270 glLinkProgram(prog);
|
nuclear@0
|
271
|
nuclear@0
|
272 int status;
|
nuclear@0
|
273 glGetProgramiv(prog, GL_LINK_STATUS, &status);
|
nuclear@0
|
274
|
nuclear@0
|
275 info_log(status ? "success\n" : "failed\n");
|
nuclear@0
|
276
|
nuclear@0
|
277 int info_len;
|
nuclear@0
|
278 glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &info_len);
|
nuclear@0
|
279 if(info_len > 1) {
|
nuclear@0
|
280 char *buf = (char*)alloca(info_len);
|
nuclear@0
|
281 glGetProgramInfoLog(prog, info_len, 0, buf);
|
nuclear@0
|
282 buf[info_len - 1] = 0;
|
nuclear@0
|
283
|
nuclear@0
|
284 if(status) {
|
nuclear@0
|
285 info_log("%s\n", buf);
|
nuclear@0
|
286 } else {
|
nuclear@0
|
287 error_log("%s\n", buf);
|
nuclear@0
|
288 }
|
nuclear@0
|
289 }
|
nuclear@0
|
290
|
nuclear@0
|
291 if(status) {
|
nuclear@0
|
292 must_link = false;
|
nuclear@0
|
293 cache_state_uniforms();
|
nuclear@0
|
294
|
nuclear@0
|
295 return true;
|
nuclear@0
|
296 }
|
nuclear@0
|
297 return false;
|
nuclear@0
|
298 }
|
nuclear@0
|
299
|
nuclear@0
|
300 void ShaderProg::bind() const
|
nuclear@0
|
301 {
|
nuclear@0
|
302 CHECKGLERR;
|
nuclear@0
|
303 if(must_link) {
|
nuclear@0
|
304 if(!link()) {
|
nuclear@0
|
305 return;
|
nuclear@0
|
306 }
|
nuclear@0
|
307 }
|
nuclear@0
|
308 CHECKGLERR;
|
nuclear@0
|
309 glUseProgram(prog);
|
nuclear@0
|
310 ShaderProg::current = (ShaderProg*)this;
|
nuclear@0
|
311
|
nuclear@0
|
312 setup_state_uniforms();
|
nuclear@0
|
313 }
|
nuclear@0
|
314
|
nuclear@0
|
315
|
nuclear@0
|
316 int ShaderProg::get_attrib_location(const char *name) const
|
nuclear@0
|
317 {
|
nuclear@0
|
318 glUseProgram(prog);
|
nuclear@0
|
319 return glGetAttribLocation(prog, name);
|
nuclear@0
|
320 }
|
nuclear@0
|
321
|
nuclear@0
|
322 void ShaderProg::set_attrib_location(const char *name, int loc) const
|
nuclear@0
|
323 {
|
nuclear@0
|
324 glBindAttribLocation(prog, loc, name);
|
nuclear@0
|
325 must_link = true;
|
nuclear@0
|
326 }
|
nuclear@0
|
327
|
nuclear@0
|
328 int ShaderProg::get_uniform_location(const char *name) const
|
nuclear@0
|
329 {
|
nuclear@0
|
330 glUseProgram(prog);
|
nuclear@0
|
331 return glGetUniformLocation(prog, name);
|
nuclear@0
|
332 }
|
nuclear@0
|
333
|
nuclear@0
|
334 bool ShaderProg::set_uniform(int loc, int val) const
|
nuclear@0
|
335 {
|
nuclear@0
|
336 glUseProgram(prog);
|
nuclear@0
|
337 if(loc >= 0) {
|
nuclear@0
|
338 glUniform1i(loc, val);
|
nuclear@0
|
339 return true;
|
nuclear@0
|
340 }
|
nuclear@0
|
341 return false;
|
nuclear@0
|
342 }
|
nuclear@0
|
343
|
nuclear@0
|
344 bool ShaderProg::set_uniform(int loc, float val) const
|
nuclear@0
|
345 {
|
nuclear@0
|
346 glUseProgram(prog);
|
nuclear@0
|
347 if(loc >= 0) {
|
nuclear@0
|
348 glUniform1f(loc, val);
|
nuclear@0
|
349 return true;
|
nuclear@0
|
350 }
|
nuclear@0
|
351 return false;
|
nuclear@0
|
352 }
|
nuclear@0
|
353
|
nuclear@0
|
354 bool ShaderProg::set_uniform(int loc, const Vector2 &v) const
|
nuclear@0
|
355 {
|
nuclear@0
|
356 glUseProgram(prog);
|
nuclear@0
|
357 if(loc >= 0) {
|
nuclear@0
|
358 glUniform2f(loc, v.x, v.y);
|
nuclear@0
|
359 return true;
|
nuclear@0
|
360 }
|
nuclear@0
|
361 return false;
|
nuclear@0
|
362 }
|
nuclear@0
|
363
|
nuclear@0
|
364 bool ShaderProg::set_uniform(int loc, const Vector3 &v) const
|
nuclear@0
|
365 {
|
nuclear@0
|
366 glUseProgram(prog);
|
nuclear@0
|
367 if(loc >= 0) {
|
nuclear@0
|
368 glUniform3f(loc, v.x, v.y, v.z);
|
nuclear@0
|
369 return true;
|
nuclear@0
|
370 }
|
nuclear@0
|
371 return false;
|
nuclear@0
|
372 }
|
nuclear@0
|
373
|
nuclear@0
|
374 bool ShaderProg::set_uniform(int loc, const Vector4 &v) const
|
nuclear@0
|
375 {
|
nuclear@0
|
376 glUseProgram(prog);
|
nuclear@0
|
377 if(loc >= 0) {
|
nuclear@0
|
378 glUniform4f(loc, v.x, v.y, v.z, v.w);
|
nuclear@0
|
379 return true;
|
nuclear@0
|
380 }
|
nuclear@0
|
381 return false;
|
nuclear@0
|
382 }
|
nuclear@0
|
383
|
nuclear@0
|
384 bool ShaderProg::set_uniform(int loc, const Matrix3x3 &m) const
|
nuclear@0
|
385 {
|
nuclear@0
|
386 glUseProgram(prog);
|
nuclear@0
|
387 if(loc >= 0) {
|
nuclear@0
|
388 glUniformMatrix3fv(loc, 1, GL_TRUE, m[0]);
|
nuclear@0
|
389 return true;
|
nuclear@0
|
390 }
|
nuclear@0
|
391 return false;
|
nuclear@0
|
392 }
|
nuclear@0
|
393
|
nuclear@0
|
394 bool ShaderProg::set_uniform(int loc, const Matrix4x4 &m) const
|
nuclear@0
|
395 {
|
nuclear@0
|
396 glUseProgram(prog);
|
nuclear@0
|
397 if(loc >= 0) {
|
nuclear@0
|
398 glUniformMatrix4fv(loc, 1, GL_TRUE, m[0]);
|
nuclear@0
|
399 return true;
|
nuclear@0
|
400 }
|
nuclear@0
|
401 return false;
|
nuclear@0
|
402 }
|
nuclear@0
|
403
|
nuclear@0
|
404
|
nuclear@0
|
405 bool ShaderProg::set_uniform(const char *name, int val) const
|
nuclear@0
|
406 {
|
nuclear@0
|
407 return set_uniform(get_uniform_location(name), val);
|
nuclear@0
|
408 }
|
nuclear@0
|
409
|
nuclear@0
|
410 bool ShaderProg::set_uniform(const char *name, float val) const
|
nuclear@0
|
411 {
|
nuclear@0
|
412 return set_uniform(get_uniform_location(name), val);
|
nuclear@0
|
413 }
|
nuclear@0
|
414
|
nuclear@0
|
415 bool ShaderProg::set_uniform(const char *name, const Vector2 &v) const
|
nuclear@0
|
416 {
|
nuclear@0
|
417 return set_uniform(get_uniform_location(name), v);
|
nuclear@0
|
418 }
|
nuclear@0
|
419
|
nuclear@0
|
420 bool ShaderProg::set_uniform(const char *name, const Vector3 &v) const
|
nuclear@0
|
421 {
|
nuclear@0
|
422 return set_uniform(get_uniform_location(name), v);
|
nuclear@0
|
423 }
|
nuclear@0
|
424
|
nuclear@0
|
425 bool ShaderProg::set_uniform(const char *name, const Vector4 &v) const
|
nuclear@0
|
426 {
|
nuclear@0
|
427 return set_uniform(get_uniform_location(name), v);
|
nuclear@0
|
428 }
|
nuclear@0
|
429
|
nuclear@0
|
430 bool ShaderProg::set_uniform(const char *name, const Matrix3x3 &m) const
|
nuclear@0
|
431 {
|
nuclear@0
|
432 return set_uniform(get_uniform_location(name), m);
|
nuclear@0
|
433 }
|
nuclear@0
|
434
|
nuclear@0
|
435 bool ShaderProg::set_uniform(const char *name, const Matrix4x4 &m) const
|
nuclear@0
|
436 {
|
nuclear@0
|
437 return set_uniform(get_uniform_location(name), m);
|
nuclear@0
|
438 }
|
nuclear@0
|
439
|
nuclear@0
|
440 static StType unist_type(GLenum type)
|
nuclear@0
|
441 {
|
nuclear@0
|
442 switch(type) {
|
nuclear@0
|
443 case GL_FLOAT:
|
nuclear@0
|
444 return ST_FLOAT;
|
nuclear@0
|
445 case GL_FLOAT_VEC2:
|
nuclear@0
|
446 return ST_FLOAT2;
|
nuclear@0
|
447 case GL_FLOAT_VEC3:
|
nuclear@0
|
448 return ST_FLOAT3;
|
nuclear@0
|
449 case GL_FLOAT_VEC4:
|
nuclear@0
|
450 return ST_FLOAT4;
|
nuclear@0
|
451 case GL_INT:
|
nuclear@0
|
452 case GL_SAMPLER_2D:
|
nuclear@0
|
453 case GL_SAMPLER_CUBE:
|
nuclear@0
|
454 #if !GL_ES_VERSION_2_0
|
nuclear@0
|
455 case GL_SAMPLER_1D:
|
nuclear@0
|
456 case GL_SAMPLER_3D:
|
nuclear@0
|
457 case GL_SAMPLER_1D_SHADOW:
|
nuclear@0
|
458 case GL_SAMPLER_2D_SHADOW:
|
nuclear@0
|
459 #endif
|
nuclear@0
|
460 return ST_INT;
|
nuclear@0
|
461 case GL_INT_VEC2:
|
nuclear@0
|
462 return ST_INT2;
|
nuclear@0
|
463 case GL_INT_VEC3:
|
nuclear@0
|
464 return ST_INT3;
|
nuclear@0
|
465 case GL_INT_VEC4:
|
nuclear@0
|
466 return ST_INT4;
|
nuclear@0
|
467 case GL_FLOAT_MAT3:
|
nuclear@0
|
468 return ST_MATRIX3;
|
nuclear@0
|
469 case GL_FLOAT_MAT4:
|
nuclear@0
|
470 return ST_MATRIX4;
|
nuclear@0
|
471 default:
|
nuclear@0
|
472 break;
|
nuclear@0
|
473 }
|
nuclear@0
|
474 return ST_UNKNOWN;
|
nuclear@0
|
475 }
|
nuclear@0
|
476
|
nuclear@0
|
477 void ShaderProg::cache_state_uniforms() const
|
nuclear@0
|
478 {
|
nuclear@0
|
479 if(!glIsProgram(prog)) {
|
nuclear@0
|
480 return;
|
nuclear@0
|
481 }
|
nuclear@0
|
482
|
nuclear@0
|
483 int num_uni;
|
nuclear@0
|
484 glGetProgramiv(prog, GL_ACTIVE_UNIFORMS, &num_uni);
|
nuclear@0
|
485
|
nuclear@0
|
486 char name[256];
|
nuclear@0
|
487 for(int i=0; i<num_uni; i++) {
|
nuclear@0
|
488 GLint sz;
|
nuclear@0
|
489 GLenum type;
|
nuclear@0
|
490 glGetActiveUniform(prog, i, sizeof name - 1, 0, &sz, &type, name);
|
nuclear@0
|
491
|
nuclear@0
|
492 if(strstr(name, "st_") == name) {
|
nuclear@0
|
493 StateLocCache s;
|
nuclear@0
|
494 s.sidx = add_unistate(name, unist_type(type));
|
nuclear@0
|
495 s.loc = glGetUniformLocation(prog, name);
|
nuclear@0
|
496 stloc_cache.push_back(s);
|
nuclear@0
|
497 }
|
nuclear@0
|
498 }
|
nuclear@0
|
499 }
|
nuclear@0
|
500
|
nuclear@0
|
501 void ShaderProg::setup_state_uniforms() const
|
nuclear@0
|
502 {
|
nuclear@0
|
503 for(size_t i=0; i<stloc_cache.size(); i++) {
|
nuclear@0
|
504 setup_unistate(stloc_cache[i].sidx, this, stloc_cache[i].loc);
|
nuclear@0
|
505 CHECKGLERR;
|
nuclear@0
|
506 }
|
nuclear@0
|
507 }
|
nuclear@0
|
508
|
nuclear@0
|
509 // ---- ShaderSet ----
|
nuclear@0
|
510 static Shader *load_shader(const char *fname, unsigned int type)
|
nuclear@0
|
511 {
|
nuclear@0
|
512 Shader *sdr = new Shader;
|
nuclear@0
|
513 if(!sdr->load(fname, type)) {
|
nuclear@0
|
514 delete sdr;
|
nuclear@0
|
515 return 0;
|
nuclear@0
|
516 }
|
nuclear@0
|
517 return sdr;
|
nuclear@0
|
518 }
|
nuclear@0
|
519
|
nuclear@0
|
520 static Shader *load_vertex_shader(const char *fname)
|
nuclear@0
|
521 {
|
nuclear@0
|
522 return load_shader(fname, GL_VERTEX_SHADER);
|
nuclear@0
|
523 }
|
nuclear@0
|
524
|
nuclear@0
|
525 static Shader *load_pixel_shader(const char *fname)
|
nuclear@0
|
526 {
|
nuclear@0
|
527 return load_shader(fname, GL_FRAGMENT_SHADER);
|
nuclear@0
|
528 }
|
nuclear@0
|
529
|
nuclear@0
|
530 #ifdef HAVE_GEOMETRY_SHADER
|
nuclear@0
|
531 static Shader *load_geom_shader(const char *fname)
|
nuclear@0
|
532 {
|
nuclear@0
|
533 return load_shader(fname, GL_GEOMETRY_SHADER);
|
nuclear@0
|
534 }
|
nuclear@0
|
535 #endif
|
nuclear@0
|
536
|
nuclear@0
|
537 #ifdef HAVE_TESSELATION_SHADER
|
nuclear@0
|
538 static Shader *load_tc_shader(const char *fname)
|
nuclear@0
|
539 {
|
nuclear@0
|
540 return load_shader(fname, GL_TESS_CONTROL_SHADER);
|
nuclear@0
|
541 }
|
nuclear@0
|
542
|
nuclear@0
|
543 static Shader *load_te_shader(const char *fname)
|
nuclear@0
|
544 {
|
nuclear@0
|
545 return load_shader(fname, GL_TESS_EVALUATION_SHADER);
|
nuclear@0
|
546 }
|
nuclear@0
|
547 #endif
|
nuclear@0
|
548
|
nuclear@0
|
549 static void destroy_shader(Shader *sdr)
|
nuclear@0
|
550 {
|
nuclear@0
|
551 delete sdr;
|
nuclear@0
|
552 }
|
nuclear@0
|
553
|
nuclear@0
|
554 ShaderSet::ShaderSet(unsigned int type)
|
nuclear@0
|
555 : DataSet<Shader*>(0, destroy_shader)
|
nuclear@0
|
556 {
|
nuclear@0
|
557 this->type = type;
|
nuclear@0
|
558
|
nuclear@0
|
559 switch(type) {
|
nuclear@0
|
560 case GL_VERTEX_SHADER:
|
nuclear@0
|
561 load = load_vertex_shader;
|
nuclear@0
|
562 break;
|
nuclear@0
|
563
|
nuclear@0
|
564 case GL_FRAGMENT_SHADER:
|
nuclear@0
|
565 load = load_pixel_shader;
|
nuclear@0
|
566 break;
|
nuclear@0
|
567
|
nuclear@0
|
568 #ifdef HAVE_GEOMETRY_SHADER
|
nuclear@0
|
569 case GL_GEOMETRY_SHADER:
|
nuclear@0
|
570 load = load_geom_shader;
|
nuclear@0
|
571 break;
|
nuclear@0
|
572 #endif
|
nuclear@0
|
573
|
nuclear@0
|
574 #ifdef HAVE_TESSELATION_SHADER
|
nuclear@0
|
575 case GL_TESS_CONTROL_SHADER:
|
nuclear@0
|
576 load = load_tc_shader;
|
nuclear@0
|
577 break;
|
nuclear@0
|
578
|
nuclear@0
|
579 case GL_TESS_EVALUATION_SHADER:
|
nuclear@0
|
580 load = load_te_shader;
|
nuclear@0
|
581 break;
|
nuclear@0
|
582 #endif
|
nuclear@0
|
583
|
nuclear@0
|
584 default:
|
nuclear@0
|
585 error_log("ShaderSet constructed with invalid shader type!\n");
|
nuclear@0
|
586 }
|
nuclear@0
|
587 }
|
nuclear@0
|
588
|
nuclear@0
|
589 static struct { const char *name; int loc; } attr_loc[] = {
|
nuclear@0
|
590 {"attr_vertex", MESH_ATTR_VERTEX},
|
nuclear@0
|
591 {"attr_normal", MESH_ATTR_NORMAL},
|
nuclear@0
|
592 {"attr_tangent", MESH_ATTR_TANGENT},
|
nuclear@0
|
593 {"attr_texcoord", MESH_ATTR_TEXCOORD},
|
nuclear@0
|
594 {"attr_color", MESH_ATTR_COLOR},
|
nuclear@0
|
595 {"attr_boneweights", MESH_ATTR_BONEWEIGHTS},
|
nuclear@0
|
596 {"attr_boneidx", MESH_ATTR_BONEIDX}
|
nuclear@0
|
597 };
|
nuclear@0
|
598
|
nuclear@0
|
599 static void bind_standard_attr(const ShaderProg *prog)
|
nuclear@0
|
600 {
|
nuclear@0
|
601 // we must link once to find out which are the active attributes
|
nuclear@0
|
602 glLinkProgram(prog->get_id());
|
nuclear@0
|
603
|
nuclear@0
|
604 int num_attr;
|
nuclear@0
|
605 glGetProgramiv(prog->get_id(), GL_ACTIVE_ATTRIBUTES, &num_attr);
|
nuclear@0
|
606
|
nuclear@0
|
607 char name[256];
|
nuclear@0
|
608 for(int i=0; i<num_attr; i++) {
|
nuclear@0
|
609 GLint sz;
|
nuclear@0
|
610 GLenum type;
|
nuclear@0
|
611 glGetActiveAttrib(prog->get_id(), i, sizeof name - 1, 0, &sz, &type, name);
|
nuclear@0
|
612
|
nuclear@8
|
613 for(int j=0; j<(int)(sizeof attr_loc / sizeof *attr_loc); j++) {
|
nuclear@0
|
614 if(strcmp(name, attr_loc[j].name) == 0) {
|
nuclear@0
|
615 prog->set_attrib_location(name, attr_loc[j].loc);
|
nuclear@0
|
616 }
|
nuclear@0
|
617 }
|
nuclear@0
|
618 }
|
nuclear@0
|
619 }
|
nuclear@0
|
620
|
nuclear@0
|
621
|
nuclear@0
|
622 static const char *strtype(unsigned int type)
|
nuclear@0
|
623 {
|
nuclear@0
|
624 switch(type) {
|
nuclear@0
|
625 case GL_VERTEX_SHADER:
|
nuclear@0
|
626 return "vertex";
|
nuclear@0
|
627 case GL_FRAGMENT_SHADER:
|
nuclear@0
|
628 return "fragment";
|
nuclear@0
|
629 #ifdef HAVE_GEOMETRY_SHADER
|
nuclear@0
|
630 case GL_GEOMETRY_SHADER:
|
nuclear@0
|
631 return "geometry";
|
nuclear@0
|
632 #endif
|
nuclear@0
|
633 #ifdef HAVE_TESSELATION_SHADER
|
nuclear@0
|
634 case GL_TESS_CONTROL_SHADER:
|
nuclear@0
|
635 return "tesselation control";
|
nuclear@0
|
636 case GL_TESS_EVALUATION_SHADER:
|
nuclear@0
|
637 return "tesselation evaluation";
|
nuclear@0
|
638 #endif
|
nuclear@0
|
639 default:
|
nuclear@0
|
640 break;
|
nuclear@0
|
641 }
|
nuclear@0
|
642 return "<unknown>";
|
nuclear@0
|
643 }
|