goat3dgfx

view src/shader.cc @ 33:253542d715f4

default texture
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 02 Mar 2014 06:32:37 +0200
parents dc5918c62a64
children
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 "mesh.h"
8 #include "logger.h"
9 #include "unistate.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 std::string read_source(const char *fname);
25 static void bind_standard_attr(const ShaderProg *prog);
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 }
46 Shader::Shader()
47 {
48 sdr = type = 0;
49 }
51 Shader::~Shader()
52 {
53 destroy();
54 }
56 unsigned int Shader::get_id() const
57 {
58 return sdr;
59 }
61 void Shader::set_name(const char *name)
62 {
63 this->name = std::string(name);
64 }
66 const char *Shader::get_name() const
67 {
68 return name.c_str();
69 }
71 bool Shader::create(const char *src, unsigned int type)
72 {
73 #if !GL_ES_VERSION_2_0
74 const char *src_arr[] = {src};
75 #else
76 const char *src_arr[] = { "precision mediump float; ", src };
77 #endif
79 // keep a copy of the source code
80 this->src = std::string(src);
82 if(!sdr) {
83 sdr = glCreateShader(type);
84 }
86 info_log("compiling shader: %s... ", name.c_str());
88 glShaderSource(sdr, sizeof src_arr / sizeof *src_arr, src_arr, 0);
89 glCompileShader(sdr);
91 int status;
92 glGetShaderiv(sdr, GL_COMPILE_STATUS, &status);
94 info_log(status ? "success\n" : "failed\n");
96 int info_len;
97 glGetShaderiv(sdr, GL_INFO_LOG_LENGTH, &info_len);
98 if(info_len > 1) {
99 char *buf = (char*)alloca(info_len);
100 glGetShaderInfoLog(sdr, info_len, 0, buf);
101 buf[info_len - 1] = 0;
103 if(status) {
104 info_log("%s\n", buf);
105 } else {
106 error_log("%s\n", buf);
107 }
108 }
110 return status == GL_TRUE;
111 }
113 void Shader::destroy()
114 {
115 if(sdr) {
116 glDeleteShader(sdr);
117 }
118 sdr = type = 0;
120 src.clear();
121 name.clear();
122 }
124 static std::string read_source(const char *fname)
125 {
126 FILE *fp;
128 if(!(fp = fopen(datafile_path(fname).c_str(), "rb"))) {
129 error_log("failed to load shader: %s: %s\n", fname, strerror(errno));
130 return std::string();
131 }
133 fseek(fp, 0, SEEK_END);
134 long sz = ftell(fp);
135 rewind(fp);
137 char *src = (char*)alloca(sz + 1);
138 if(fread(src, 1, sz, fp) < (size_t)sz) {
139 error_log("failed to load shader: %s: %s\n", fname, strerror(errno));
140 fclose(fp);
141 delete [] src;
142 return std::string();
143 }
144 src[sz] = 0;
145 fclose(fp);
147 return std::string(src);
148 }
150 bool Shader::load(const char *fname, unsigned int type)
151 {
152 std::string src = read_source(fname);
153 if(src.empty()) {
154 return false;
155 }
156 set_name(fname);
157 return create(src.c_str(), type);
158 }
160 // ---- shader program ----
161 ShaderProg::ShaderProg()
162 {
163 prog = 0;
164 must_link = true;
165 }
167 ShaderProg::~ShaderProg()
168 {
169 destroy();
170 }
172 unsigned int ShaderProg::get_id() const
173 {
174 return prog;
175 }
177 bool ShaderProg::create(const char *src, unsigned int type, ...)
178 {
179 va_list ap;
181 va_start(ap, type);
182 bool res = create(src, type, ap);
183 va_end(ap);
185 return res;
186 }
188 bool ShaderProg::create(const char *src, unsigned int type, va_list ap)
189 {
190 destroy();
191 prog = glCreateProgram();
193 while(src) {
194 Shader *sdr = new Shader;
195 if(!sdr->create(src, type)) {
196 va_end(ap);
197 return false;
198 }
199 add_shader(sdr);
200 src = va_arg(ap, const char*);
201 type = va_arg(ap, unsigned int);
202 }
204 return link();
205 }
207 bool ShaderProg::create(const char *vsrc, const char *psrc)
208 {
209 return create(VSDR(vsrc), PSDR(psrc), 0);
210 }
212 bool ShaderProg::create(Shader *sdr, ...)
213 {
214 va_list ap;
216 va_start(ap, sdr);
217 bool res = create(sdr, ap);
218 va_end(ap);
220 return res;
221 }
223 bool ShaderProg::create(Shader *sdr, va_list ap)
224 {
225 destroy();
226 prog = glCreateProgram();
228 while(sdr) {
229 add_shader(sdr);
230 sdr = va_arg(ap, Shader*);
231 }
232 return link();
233 }
235 bool ShaderProg::create(Shader *vsdr, Shader *psdr)
236 {
237 return create(vsdr, psdr, 0);
238 }
240 void ShaderProg::destroy()
241 {
242 if(prog) {
243 glDeleteProgram(prog);
244 }
245 prog = 0;
247 shaders.clear();
248 // don't actually destroy the shaders, let the ShaderSet own them
249 }
251 bool ShaderProg::load(const char *fname, unsigned int type, ...)
252 {
253 va_list ap;
254 va_start(ap, type);
255 bool res = load(fname, type, ap);
256 va_end(ap);
258 return res;
259 }
261 bool ShaderProg::load(const char *fname, unsigned int type, va_list ap)
262 {
263 destroy();
264 prog = glCreateProgram();
266 while(fname) {
267 Shader *sdr = new Shader;
268 if(!sdr->load(fname, type)) {
269 delete sdr;
270 return false;
271 }
272 add_shader(sdr);
274 if((fname = va_arg(ap, const char*))) {
275 type = va_arg(ap, unsigned int);
276 }
277 }
279 return link();
280 }
282 bool ShaderProg::load(const char *vfname, const char *pfname)
283 {
284 return load(VSDR(vfname), PSDR(pfname), 0);
285 }
287 void ShaderProg::add_shader(Shader *sdr)
288 {
289 glAttachShader(prog, sdr->get_id());
290 }
292 bool ShaderProg::link() const
293 {
294 bind_standard_attr(this);
296 CHECKGLERR;
297 info_log("linking program ... ");
298 glLinkProgram(prog);
300 int status;
301 glGetProgramiv(prog, GL_LINK_STATUS, &status);
303 info_log(status ? "success\n" : "failed\n");
305 int info_len;
306 glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &info_len);
307 if(info_len > 1) {
308 char *buf = (char*)alloca(info_len);
309 glGetProgramInfoLog(prog, info_len, 0, buf);
310 buf[info_len - 1] = 0;
312 if(status) {
313 info_log("%s\n", buf);
314 } else {
315 error_log("%s\n", buf);
316 }
317 }
319 if(status) {
320 must_link = false;
321 cache_state_uniforms();
322 return true;
323 }
324 return false;
325 }
327 void ShaderProg::bind() const
328 {
329 CHECKGLERR;
330 if(must_link) {
331 if(!link()) {
332 return;
333 }
334 }
335 CHECKGLERR;
336 glUseProgram(prog);
337 ShaderProg::current = (ShaderProg*)this;
339 setup_state_uniforms();
340 }
343 int ShaderProg::get_attrib_location(const char *name) const
344 {
345 glUseProgram(prog);
346 return glGetAttribLocation(prog, name);
347 }
349 void ShaderProg::set_attrib_location(const char *name, int loc) const
350 {
351 glBindAttribLocation(prog, loc, name);
352 must_link = true;
353 }
355 int ShaderProg::get_uniform_location(const char *name) const
356 {
357 glUseProgram(prog);
358 return glGetUniformLocation(prog, name);
359 }
361 bool ShaderProg::set_uniform(int loc, int val) const
362 {
363 glUseProgram(prog);
364 if(loc >= 0) {
365 glUniform1i(loc, val);
366 return true;
367 }
368 return false;
369 }
371 bool ShaderProg::set_uniform(int loc, float val) const
372 {
373 glUseProgram(prog);
374 if(loc >= 0) {
375 glUniform1f(loc, val);
376 return true;
377 }
378 return false;
379 }
381 bool ShaderProg::set_uniform(int loc, const Vector2 &v) const
382 {
383 glUseProgram(prog);
384 if(loc >= 0) {
385 glUniform2f(loc, v.x, v.y);
386 return true;
387 }
388 return false;
389 }
391 bool ShaderProg::set_uniform(int loc, const Vector3 &v) const
392 {
393 glUseProgram(prog);
394 if(loc >= 0) {
395 glUniform3f(loc, v.x, v.y, v.z);
396 return true;
397 }
398 return false;
399 }
401 bool ShaderProg::set_uniform(int loc, const Vector4 &v) const
402 {
403 glUseProgram(prog);
404 if(loc >= 0) {
405 glUniform4f(loc, v.x, v.y, v.z, v.w);
406 return true;
407 }
408 return false;
409 }
411 bool ShaderProg::set_uniform(int loc, const Matrix3x3 &m) const
412 {
413 glUseProgram(prog);
414 if(loc >= 0) {
415 glUniformMatrix3fv(loc, 1, GL_TRUE, m[0]);
416 return true;
417 }
418 return false;
419 }
421 bool ShaderProg::set_uniform(int loc, const Matrix4x4 &m) const
422 {
423 glUseProgram(prog);
424 if(loc >= 0) {
425 glUniformMatrix4fv(loc, 1, GL_TRUE, m[0]);
426 return true;
427 }
428 return false;
429 }
432 bool ShaderProg::set_uniform(const char *name, int val) const
433 {
434 return set_uniform(get_uniform_location(name), val);
435 }
437 bool ShaderProg::set_uniform(const char *name, float val) const
438 {
439 return set_uniform(get_uniform_location(name), val);
440 }
442 bool ShaderProg::set_uniform(const char *name, const Vector2 &v) const
443 {
444 return set_uniform(get_uniform_location(name), v);
445 }
447 bool ShaderProg::set_uniform(const char *name, const Vector3 &v) const
448 {
449 return set_uniform(get_uniform_location(name), v);
450 }
452 bool ShaderProg::set_uniform(const char *name, const Vector4 &v) const
453 {
454 return set_uniform(get_uniform_location(name), v);
455 }
457 bool ShaderProg::set_uniform(const char *name, const Matrix3x3 &m) const
458 {
459 return set_uniform(get_uniform_location(name), m);
460 }
462 bool ShaderProg::set_uniform(const char *name, const Matrix4x4 &m) const
463 {
464 return set_uniform(get_uniform_location(name), m);
465 }
467 static StType unist_type(GLenum type)
468 {
469 switch(type) {
470 case GL_FLOAT:
471 return ST_FLOAT;
472 case GL_FLOAT_VEC2:
473 return ST_FLOAT2;
474 case GL_FLOAT_VEC3:
475 return ST_FLOAT3;
476 case GL_FLOAT_VEC4:
477 return ST_FLOAT4;
478 case GL_INT:
479 case GL_SAMPLER_2D:
480 case GL_SAMPLER_CUBE:
481 #if !GL_ES_VERSION_2_0
482 case GL_SAMPLER_1D:
483 case GL_SAMPLER_3D:
484 case GL_SAMPLER_1D_SHADOW:
485 case GL_SAMPLER_2D_SHADOW:
486 #endif
487 return ST_INT;
488 case GL_INT_VEC2:
489 return ST_INT2;
490 case GL_INT_VEC3:
491 return ST_INT3;
492 case GL_INT_VEC4:
493 return ST_INT4;
494 case GL_FLOAT_MAT3:
495 return ST_MATRIX3;
496 case GL_FLOAT_MAT4:
497 return ST_MATRIX4;
498 default:
499 break;
500 }
501 return ST_UNKNOWN;
502 }
504 void ShaderProg::cache_state_uniforms() const
505 {
506 if(!glIsProgram(prog)) {
507 return;
508 }
510 int num_uni;
511 glGetProgramiv(prog, GL_ACTIVE_UNIFORMS, &num_uni);
513 char name[256];
514 for(int i=0; i<num_uni; i++) {
515 GLint sz;
516 GLenum type;
517 glGetActiveUniform(prog, i, sizeof name - 1, 0, &sz, &type, name);
519 if(strstr(name, "st_") == name) {
520 StateLocCache s;
521 s.sidx = add_unistate(name, unist_type(type));
522 s.loc = glGetUniformLocation(prog, name);
523 stloc_cache.push_back(s);
524 }
525 }
526 }
528 void ShaderProg::setup_state_uniforms() const
529 {
530 for(size_t i=0; i<stloc_cache.size(); i++) {
531 setup_unistate(stloc_cache[i].sidx, this, stloc_cache[i].loc);
532 CHECKGLERR;
533 }
534 }
536 // Static functions to use with ShaderSet
537 Shader *Shader::create_shader()
538 {
539 return new Shader;
540 }
542 bool Shader::load_shader(Shader *sdr, const char *fname)
543 {
544 std::string src = read_source(fname);
545 if(src.empty()) {
546 return false;
547 }
549 sdr->src = src;
550 return true;
551 }
553 bool Shader::done_shader(Shader *sdr, unsigned int type)
554 {
555 return sdr->create(sdr->src.c_str(), type);
556 }
558 static bool done_vertex_shader(Shader *sdr)
559 {
560 return Shader::done_shader(sdr, GL_VERTEX_SHADER);
561 }
563 static bool done_pixel_shader(Shader *sdr)
564 {
565 return Shader::done_shader(sdr, GL_FRAGMENT_SHADER);
566 }
568 #ifdef HAVE_GEOMETRY_SHADER
569 static bool done_geom_shader(Shader *sdr)
570 {
571 return Shader::done_shader(sdr, GL_GEOMETRY_SHADER);
572 }
573 #endif
575 #ifdef HAVE_TESSELATION_SHADER
576 static bool done_tc_shader(Shader *sdr)
577 {
578 return Shader::done_shader(sdr, GL_TESS_CONTROL_SHADER);
579 }
581 static bool done_te_shader(Shader *sdr)
582 {
583 return Shader::done_shader(sdr, GL_TESS_EVALUATION_SHADER);
584 }
585 #endif
587 void Shader::destroy_shader(Shader *sdr)
588 {
589 delete sdr;
590 }
592 // ---- ShaderSet ----
594 ShaderSet::ShaderSet(unsigned int type)
595 : DataSet<Shader*>(Shader::create_shader, Shader::load_shader, 0, Shader::destroy_shader)
596 {
597 switch(type) {
598 case GL_VERTEX_SHADER:
599 done = done_vertex_shader;
600 break;
602 case GL_FRAGMENT_SHADER:
603 done = done_pixel_shader;
604 break;
606 #ifdef HAVE_GEOMETRY_SHADER
607 case GL_GEOMETRY_SHADER:
608 done = done_geom_shader;
609 break;
610 #endif
612 #ifdef HAVE_TESSELATION_SHADER
613 case GL_TESS_CONTROL_SHADER:
614 done = done_tc_shader;
615 break;
617 case GL_TESS_EVALUATION_SHADER:
618 done = done_te_shader;
619 break;
620 #endif
622 default:
623 error_log("ShaderSet constructed with invalid shader type!\n");
624 }
625 }
627 static struct { const char *name; int loc; } attr_loc[] = {
628 {"attr_vertex", MESH_ATTR_VERTEX},
629 {"attr_normal", MESH_ATTR_NORMAL},
630 {"attr_tangent", MESH_ATTR_TANGENT},
631 {"attr_texcoord", MESH_ATTR_TEXCOORD},
632 {"attr_color", MESH_ATTR_COLOR},
633 {"attr_boneweights", MESH_ATTR_BONEWEIGHTS},
634 {"attr_boneidx", MESH_ATTR_BONEIDX}
635 };
637 static void bind_standard_attr(const ShaderProg *prog)
638 {
639 // we must link once to find out which are the active attributes
640 glLinkProgram(prog->get_id());
642 int num_attr;
643 glGetProgramiv(prog->get_id(), GL_ACTIVE_ATTRIBUTES, &num_attr);
645 char name[256];
646 for(int i=0; i<num_attr; i++) {
647 GLint sz;
648 GLenum type;
649 glGetActiveAttrib(prog->get_id(), i, sizeof name - 1, 0, &sz, &type, name);
651 for(int j=0; j<(int)(sizeof attr_loc / sizeof *attr_loc); j++) {
652 if(strcmp(name, attr_loc[j].name) == 0) {
653 prog->set_attrib_location(name, attr_loc[j].loc);
654 }
655 }
656 }
657 }
660 /*
661 static const char *strtype(unsigned int type)
662 {
663 switch(type) {
664 case GL_VERTEX_SHADER:
665 return "vertex";
666 case GL_FRAGMENT_SHADER:
667 return "fragment";
668 #ifdef HAVE_GEOMETRY_SHADER
669 case GL_GEOMETRY_SHADER:
670 return "geometry";
671 #endif
672 #ifdef HAVE_TESSELATION_SHADER
673 case GL_TESS_CONTROL_SHADER:
674 return "tesselation control";
675 case GL_TESS_EVALUATION_SHADER:
676 return "tesselation evaluation";
677 #endif
678 default:
679 break;
680 }
681 return "<unknown>";
682 }
683 */