dbf-halloween2015

annotate src/sdr.c @ 4:4316c0c879e9

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