goat3dgfx

view src/shader.cc @ 15:7d6b667821cf

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