erebus

annotate src/sdr.c @ 10:506e114b7ca2

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