bloboland

annotate src/sdr.c @ 3:a39c301cdcce

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