vrshoot

view src/shader.cc @ 3:c179c72369be

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