goat3dgfx

view src/shader.cc @ 21:7c593721547f

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