goat3dgfx

annotate src/shader.cc @ 29:9d581abd0bfb

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