tesspot

annotate src/sdr.c @ 0:72b7f9f2eead

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 02 Dec 2012 08:23:51 +0200
parents
children
rev   line source
nuclear@0 1 #include <stdio.h>
nuclear@0 2 #include <stdlib.h>
nuclear@0 3 #include <string.h>
nuclear@0 4 #include <errno.h>
nuclear@0 5 #include <stdarg.h>
nuclear@0 6 #include <assert.h>
nuclear@0 7 #include <GL/glew.h>
nuclear@0 8
nuclear@0 9 #if defined(unix) || defined(__unix__)
nuclear@0 10 #include <unistd.h>
nuclear@0 11 #include <sys/stat.h>
nuclear@0 12 #endif /* unix */
nuclear@0 13
nuclear@0 14 #include "sdr.h"
nuclear@0 15
nuclear@0 16 static const char *sdrtypestr(unsigned int sdrtype);
nuclear@0 17
nuclear@0 18 unsigned int create_vertex_shader(const char *src)
nuclear@0 19 {
nuclear@0 20 return create_shader(src, GL_VERTEX_SHADER);
nuclear@0 21 }
nuclear@0 22
nuclear@0 23 unsigned int create_pixel_shader(const char *src)
nuclear@0 24 {
nuclear@0 25 return create_shader(src, GL_FRAGMENT_SHADER);
nuclear@0 26 }
nuclear@0 27
nuclear@0 28 unsigned int create_tessctl_shader(const char *src)
nuclear@0 29 {
nuclear@0 30 return create_shader(src, GL_TESS_CONTROL_SHADER);
nuclear@0 31 }
nuclear@0 32
nuclear@0 33 unsigned int create_tesseval_shader(const char *src)
nuclear@0 34 {
nuclear@0 35 return create_shader(src, GL_TESS_EVALUATION_SHADER);
nuclear@0 36 }
nuclear@0 37
nuclear@0 38 unsigned int create_geometry_shader(const char *src)
nuclear@0 39 {
nuclear@0 40 return create_shader(src, GL_GEOMETRY_SHADER);
nuclear@0 41 }
nuclear@0 42
nuclear@0 43 unsigned int create_shader(const char *src, unsigned int sdr_type)
nuclear@0 44 {
nuclear@0 45 unsigned int sdr;
nuclear@0 46 int success, info_len;
nuclear@0 47 char *info_str = 0;
nuclear@0 48 GLenum err;
nuclear@0 49
nuclear@0 50 sdr = glCreateShader(sdr_type);
nuclear@0 51 assert(glGetError() == GL_NO_ERROR);
nuclear@0 52 glShaderSource(sdr, 1, &src, 0);
nuclear@0 53 err = glGetError();
nuclear@0 54 assert(err == GL_NO_ERROR);
nuclear@0 55 glCompileShader(sdr);
nuclear@0 56 assert(glGetError() == GL_NO_ERROR);
nuclear@0 57
nuclear@0 58 glGetShaderiv(sdr, GL_COMPILE_STATUS, &success);
nuclear@0 59 assert(glGetError() == GL_NO_ERROR);
nuclear@0 60 glGetShaderiv(sdr, GL_INFO_LOG_LENGTH, &info_len);
nuclear@0 61 assert(glGetError() == GL_NO_ERROR);
nuclear@0 62
nuclear@0 63 if(info_len) {
nuclear@0 64 if((info_str = malloc(info_len + 1))) {
nuclear@0 65 glGetShaderInfoLog(sdr, info_len, 0, info_str);
nuclear@0 66 assert(glGetError() == GL_NO_ERROR);
nuclear@0 67 }
nuclear@0 68 }
nuclear@0 69
nuclear@0 70 if(success) {
nuclear@0 71 fprintf(stderr, info_str ? "done: %s\n" : "done\n", info_str);
nuclear@0 72 } else {
nuclear@0 73 fprintf(stderr, info_str ? "failed: %s\n" : "failed\n", info_str);
nuclear@0 74 glDeleteShader(sdr);
nuclear@0 75 sdr = 0;
nuclear@0 76 }
nuclear@0 77
nuclear@0 78 free(info_str);
nuclear@0 79 return sdr;
nuclear@0 80 }
nuclear@0 81
nuclear@0 82 void free_shader(unsigned int sdr)
nuclear@0 83 {
nuclear@0 84 glDeleteShader(sdr);
nuclear@0 85 }
nuclear@0 86
nuclear@0 87 unsigned int load_vertex_shader(const char *fname)
nuclear@0 88 {
nuclear@0 89 return load_shader(fname, GL_VERTEX_SHADER);
nuclear@0 90 }
nuclear@0 91
nuclear@0 92 unsigned int load_pixel_shader(const char *fname)
nuclear@0 93 {
nuclear@0 94 return load_shader(fname, GL_FRAGMENT_SHADER);
nuclear@0 95 }
nuclear@0 96
nuclear@0 97 unsigned int load_tessctl_shader(const char *fname)
nuclear@0 98 {
nuclear@0 99 return load_shader(fname, GL_TESS_CONTROL_SHADER);
nuclear@0 100 }
nuclear@0 101
nuclear@0 102 unsigned int load_tesseval_shader(const char *fname)
nuclear@0 103 {
nuclear@0 104 return load_shader(fname, GL_TESS_EVALUATION_SHADER);
nuclear@0 105 }
nuclear@0 106
nuclear@0 107 unsigned int load_geometry_shader(const char *fname)
nuclear@0 108 {
nuclear@0 109 return load_shader(fname, GL_GEOMETRY_SHADER);
nuclear@0 110 }
nuclear@0 111
nuclear@0 112 unsigned int load_shader(const char *fname, unsigned int sdr_type)
nuclear@0 113 {
nuclear@0 114 #if defined(unix) || defined(__unix__)
nuclear@0 115 struct stat st;
nuclear@0 116 #endif
nuclear@0 117 unsigned int sdr;
nuclear@0 118 size_t filesize;
nuclear@0 119 FILE *fp;
nuclear@0 120 char *src;
nuclear@0 121
nuclear@0 122 if(!(fp = fopen(fname, "r"))) {
nuclear@0 123 fprintf(stderr, "failed to open shader %s: %s\n", fname, strerror(errno));
nuclear@0 124 return 0;
nuclear@0 125 }
nuclear@0 126
nuclear@0 127 #if defined(unix) || defined(__unix__)
nuclear@0 128 fstat(fileno(fp), &st);
nuclear@0 129 filesize = st.st_size;
nuclear@0 130 #else
nuclear@0 131 fseek(fp, 0, SEEK_END);
nuclear@0 132 filesize = ftell(fp);
nuclear@0 133 fseek(fp, 0, SEEK_SET);
nuclear@0 134 #endif /* unix */
nuclear@0 135
nuclear@0 136 if(!(src = malloc(filesize + 1))) {
nuclear@0 137 fclose(fp);
nuclear@0 138 return 0;
nuclear@0 139 }
nuclear@0 140 fread(src, 1, filesize, fp);
nuclear@0 141 src[filesize] = 0;
nuclear@0 142 fclose(fp);
nuclear@0 143
nuclear@0 144 fprintf(stderr, "compiling %s shader: %s... ", sdrtypestr(sdr_type), fname);
nuclear@0 145 sdr = create_shader(src, sdr_type);
nuclear@0 146
nuclear@0 147 free(src);
nuclear@0 148 return sdr;
nuclear@0 149 }
nuclear@0 150
nuclear@0 151
nuclear@0 152 unsigned int get_vertex_shader(const char *fname)
nuclear@0 153 {
nuclear@0 154 return get_shader(fname, GL_VERTEX_SHADER);
nuclear@0 155 }
nuclear@0 156
nuclear@0 157 unsigned int get_pixel_shader(const char *fname)
nuclear@0 158 {
nuclear@0 159 return get_shader(fname, GL_FRAGMENT_SHADER);
nuclear@0 160 }
nuclear@0 161
nuclear@0 162 unsigned int get_tessctl_shader(const char *fname)
nuclear@0 163 {
nuclear@0 164 return get_shader(fname, GL_TESS_CONTROL_SHADER);
nuclear@0 165 }
nuclear@0 166
nuclear@0 167 unsigned int get_tesseval_shader(const char *fname)
nuclear@0 168 {
nuclear@0 169 return get_shader(fname, GL_TESS_EVALUATION_SHADER);
nuclear@0 170 }
nuclear@0 171
nuclear@0 172 unsigned int get_geometry_shader(const char *fname)
nuclear@0 173 {
nuclear@0 174 return get_shader(fname, GL_GEOMETRY_SHADER);
nuclear@0 175 }
nuclear@0 176
nuclear@0 177 unsigned int get_shader(const char *fname, unsigned int sdr_type)
nuclear@0 178 {
nuclear@0 179 unsigned int sdr;
nuclear@0 180 if(!(sdr = load_shader(fname, sdr_type))) {
nuclear@0 181 return 0;
nuclear@0 182 }
nuclear@0 183 return sdr;
nuclear@0 184 }
nuclear@0 185
nuclear@0 186
nuclear@0 187 /* ---- gpu programs ---- */
nuclear@0 188
nuclear@0 189 unsigned int create_program(void)
nuclear@0 190 {
nuclear@0 191 unsigned int prog = glCreateProgram();
nuclear@0 192 assert(glGetError() == GL_NO_ERROR);
nuclear@0 193 return prog;
nuclear@0 194 }
nuclear@0 195
nuclear@0 196 unsigned int create_program_link(unsigned int sdr0, ...)
nuclear@0 197 {
nuclear@0 198 unsigned int prog, sdr;
nuclear@0 199 va_list ap;
nuclear@0 200
nuclear@0 201 if(!(prog = create_program())) {
nuclear@0 202 return 0;
nuclear@0 203 }
nuclear@0 204
nuclear@0 205 attach_shader(prog, sdr0);
nuclear@0 206 if(glGetError()) {
nuclear@0 207 return 0;
nuclear@0 208 }
nuclear@0 209
nuclear@0 210 va_start(ap, sdr0);
nuclear@0 211 while((sdr = va_arg(ap, unsigned int))) {
nuclear@0 212 attach_shader(prog, sdr);
nuclear@0 213 if(glGetError()) {
nuclear@0 214 return 0;
nuclear@0 215 }
nuclear@0 216 }
nuclear@0 217 va_end(ap);
nuclear@0 218
nuclear@0 219 if(link_program(prog) == -1) {
nuclear@0 220 free_program(prog);
nuclear@0 221 return 0;
nuclear@0 222 }
nuclear@0 223 return prog;
nuclear@0 224 }
nuclear@0 225
nuclear@0 226 unsigned int create_program_load(const char *vfile, const char *pfile)
nuclear@0 227 {
nuclear@0 228 unsigned int vs = 0, ps = 0;
nuclear@0 229
nuclear@0 230 if(vfile && *vfile && !(vs = get_vertex_shader(vfile))) {
nuclear@0 231 return 0;
nuclear@0 232 }
nuclear@0 233 if(pfile && *pfile && !(ps = get_pixel_shader(pfile))) {
nuclear@0 234 return 0;
nuclear@0 235 }
nuclear@0 236 return create_program_link(vs, ps, 0);
nuclear@0 237 }
nuclear@0 238
nuclear@0 239 void free_program(unsigned int sdr)
nuclear@0 240 {
nuclear@0 241 glDeleteProgram(sdr);
nuclear@0 242 }
nuclear@0 243
nuclear@0 244 void attach_shader(unsigned int prog, unsigned int sdr)
nuclear@0 245 {
nuclear@0 246 glAttachShader(prog, sdr);
nuclear@0 247 assert(glGetError() == GL_NO_ERROR);
nuclear@0 248 }
nuclear@0 249
nuclear@0 250 int link_program(unsigned int prog)
nuclear@0 251 {
nuclear@0 252 int linked, info_len, retval = 0;
nuclear@0 253 char *info_str = 0;
nuclear@0 254
nuclear@0 255 glLinkProgram(prog);
nuclear@0 256 assert(glGetError() == GL_NO_ERROR);
nuclear@0 257 glGetProgramiv(prog, GL_LINK_STATUS, &linked);
nuclear@0 258 assert(glGetError() == GL_NO_ERROR);
nuclear@0 259 glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &info_len);
nuclear@0 260 assert(glGetError() == GL_NO_ERROR);
nuclear@0 261
nuclear@0 262 if(info_len) {
nuclear@0 263 if((info_str = malloc(info_len + 1))) {
nuclear@0 264 glGetProgramInfoLog(prog, info_len, 0, info_str);
nuclear@0 265 assert(glGetError() == GL_NO_ERROR);
nuclear@0 266 }
nuclear@0 267 }
nuclear@0 268
nuclear@0 269 if(linked) {
nuclear@0 270 fprintf(stderr, info_str ? "linking done: %s\n" : "linking done\n", info_str);
nuclear@0 271 } else {
nuclear@0 272 fprintf(stderr, info_str ? "linking failed: %s\n" : "linking failed\n", info_str);
nuclear@0 273 retval = -1;
nuclear@0 274 }
nuclear@0 275
nuclear@0 276 free(info_str);
nuclear@0 277 return retval;
nuclear@0 278 }
nuclear@0 279
nuclear@0 280 int bind_program(unsigned int prog)
nuclear@0 281 {
nuclear@0 282 GLenum err;
nuclear@0 283
nuclear@0 284 glUseProgram(prog);
nuclear@0 285 if(prog && (err = glGetError()) != GL_NO_ERROR) {
nuclear@0 286 /* maybe the program is not linked, try linking first */
nuclear@0 287 if(err == GL_INVALID_OPERATION) {
nuclear@0 288 if(link_program(prog) == -1) {
nuclear@0 289 return -1;
nuclear@0 290 }
nuclear@0 291 glUseProgram(prog);
nuclear@0 292 return glGetError() == GL_NO_ERROR ? 0 : -1;
nuclear@0 293 }
nuclear@0 294 return -1;
nuclear@0 295 }
nuclear@0 296 return 0;
nuclear@0 297 }
nuclear@0 298
nuclear@0 299 /* ugly but I'm not going to write the same bloody code over and over */
nuclear@0 300 #define BEGIN_UNIFORM_CODE \
nuclear@0 301 int loc, curr_prog; \
nuclear@0 302 glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog); \
nuclear@0 303 if((unsigned int)curr_prog != prog && bind_program(prog) == -1) { \
nuclear@0 304 return -1; \
nuclear@0 305 } \
nuclear@0 306 if((loc = glGetUniformLocation(prog, name)) != -1)
nuclear@0 307
nuclear@0 308 #define END_UNIFORM_CODE \
nuclear@0 309 if((unsigned int)curr_prog != prog) { \
nuclear@0 310 bind_program(curr_prog); \
nuclear@0 311 } \
nuclear@0 312 return loc == -1 ? -1 : 0
nuclear@0 313
nuclear@0 314 int set_uniform_int(unsigned int prog, const char *name, int val)
nuclear@0 315 {
nuclear@0 316 BEGIN_UNIFORM_CODE {
nuclear@0 317 glUniform1i(loc, val);
nuclear@0 318 }
nuclear@0 319 END_UNIFORM_CODE;
nuclear@0 320 }
nuclear@0 321
nuclear@0 322 int set_uniform_float(unsigned int prog, const char *name, float val)
nuclear@0 323 {
nuclear@0 324 BEGIN_UNIFORM_CODE {
nuclear@0 325 glUniform1f(loc, val);
nuclear@0 326 }
nuclear@0 327 END_UNIFORM_CODE;
nuclear@0 328 }
nuclear@0 329
nuclear@0 330 int set_uniform_float2(unsigned int prog, const char *name, float x, float y)
nuclear@0 331 {
nuclear@0 332 BEGIN_UNIFORM_CODE {
nuclear@0 333 glUniform2f(loc, x, y);
nuclear@0 334 }
nuclear@0 335 END_UNIFORM_CODE;
nuclear@0 336 }
nuclear@0 337
nuclear@0 338 int set_uniform_float3(unsigned int prog, const char *name, float x, float y, float z)
nuclear@0 339 {
nuclear@0 340 BEGIN_UNIFORM_CODE {
nuclear@0 341 glUniform3f(loc, x, y, z);
nuclear@0 342 }
nuclear@0 343 END_UNIFORM_CODE;
nuclear@0 344 }
nuclear@0 345
nuclear@0 346 int set_uniform_float4(unsigned int prog, const char *name, float x, float y, float z, float w)
nuclear@0 347 {
nuclear@0 348 BEGIN_UNIFORM_CODE {
nuclear@0 349 glUniform4f(loc, x, y, z, w);
nuclear@0 350 }
nuclear@0 351 END_UNIFORM_CODE;
nuclear@0 352 }
nuclear@0 353
nuclear@0 354 int set_uniform_matrix4(unsigned int prog, const char *name, float *mat)
nuclear@0 355 {
nuclear@0 356 BEGIN_UNIFORM_CODE {
nuclear@0 357 glUniformMatrix4fv(loc, 1, GL_FALSE, mat);
nuclear@0 358 }
nuclear@0 359 END_UNIFORM_CODE;
nuclear@0 360 }
nuclear@0 361
nuclear@0 362 int set_uniform_matrix4_transposed(unsigned int prog, const char *name, float *mat)
nuclear@0 363 {
nuclear@0 364 BEGIN_UNIFORM_CODE {
nuclear@0 365 glUniformMatrix4fv(loc, 1, GL_TRUE, mat);
nuclear@0 366 }
nuclear@0 367 END_UNIFORM_CODE;
nuclear@0 368 }
nuclear@0 369
nuclear@0 370 int get_attrib_loc(unsigned int prog, const char *name)
nuclear@0 371 {
nuclear@0 372 int loc, curr_prog;
nuclear@0 373
nuclear@0 374 glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog);
nuclear@0 375 if((unsigned int)curr_prog != prog && bind_program(prog) == -1) {
nuclear@0 376 return -1;
nuclear@0 377 }
nuclear@0 378
nuclear@0 379 loc = glGetAttribLocation(prog, (char*)name);
nuclear@0 380
nuclear@0 381 if((unsigned int)curr_prog != prog) {
nuclear@0 382 bind_program(curr_prog);
nuclear@0 383 }
nuclear@0 384 return loc;
nuclear@0 385 }
nuclear@0 386
nuclear@0 387 void set_attrib_float3(int attr_loc, float x, float y, float z)
nuclear@0 388 {
nuclear@0 389 glVertexAttrib3f(attr_loc, x, y, z);
nuclear@0 390 }
nuclear@0 391
nuclear@0 392 static const char *sdrtypestr(unsigned int sdrtype)
nuclear@0 393 {
nuclear@0 394 switch(sdrtype) {
nuclear@0 395 case GL_VERTEX_SHADER:
nuclear@0 396 return "vertex";
nuclear@0 397 case GL_FRAGMENT_SHADER:
nuclear@0 398 return "pixel";
nuclear@0 399 case GL_TESS_CONTROL_SHADER:
nuclear@0 400 return "tessellation control";
nuclear@0 401 case GL_TESS_EVALUATION_SHADER:
nuclear@0 402 return "tessellation evaluation";
nuclear@0 403 case GL_GEOMETRY_SHADER:
nuclear@0 404 return "geometry";
nuclear@0 405
nuclear@0 406 default:
nuclear@0 407 break;
nuclear@0 408 }
nuclear@0 409 return "<unknown>";
nuclear@0 410 }