dbf-halloween2015
diff src/sdr.c @ 0:50683c78264e
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 01 Nov 2015 00:09:12 +0200 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/sdr.c Sun Nov 01 00:09:12 2015 +0200 1.3 @@ -0,0 +1,407 @@ 1.4 +#include <stdio.h> 1.5 +#include <stdlib.h> 1.6 +#include <string.h> 1.7 +#include <errno.h> 1.8 +#include <stdarg.h> 1.9 +#include <assert.h> 1.10 +#include "opengl.h" 1.11 + 1.12 +#if defined(unix) || defined(__unix__) 1.13 +#include <unistd.h> 1.14 +#include <sys/stat.h> 1.15 +#endif /* unix */ 1.16 + 1.17 +#include "sdr.h" 1.18 + 1.19 +static const char *sdrtypestr(unsigned int sdrtype); 1.20 + 1.21 +unsigned int create_vertex_shader(const char *src) 1.22 +{ 1.23 + return create_shader(src, GL_VERTEX_SHADER); 1.24 +} 1.25 + 1.26 +unsigned int create_pixel_shader(const char *src) 1.27 +{ 1.28 + return create_shader(src, GL_FRAGMENT_SHADER); 1.29 +} 1.30 + 1.31 +unsigned int create_tessctl_shader(const char *src) 1.32 +{ 1.33 +#ifdef GL_TESS_CONTROL_SHADER 1.34 + return create_shader(src, GL_TESS_CONTROL_SHADER); 1.35 +#else 1.36 + return 0; 1.37 +#endif 1.38 +} 1.39 + 1.40 +unsigned int create_tesseval_shader(const char *src) 1.41 +{ 1.42 +#ifdef GL_TESS_EVALUATION_SHADER 1.43 + return create_shader(src, GL_TESS_EVALUATION_SHADER); 1.44 +#else 1.45 + return 0; 1.46 +#endif 1.47 +} 1.48 + 1.49 +unsigned int create_geometry_shader(const char *src) 1.50 +{ 1.51 +#ifdef GL_GEOMETRY_SHADER 1.52 + return create_shader(src, GL_GEOMETRY_SHADER); 1.53 +#else 1.54 + return 0; 1.55 +#endif 1.56 +} 1.57 + 1.58 +unsigned int create_shader(const char *src, unsigned int sdr_type) 1.59 +{ 1.60 + unsigned int sdr; 1.61 + int success, info_len; 1.62 + char *info_str = 0; 1.63 + GLenum err; 1.64 + 1.65 + sdr = glCreateShader(sdr_type); 1.66 + assert(glGetError() == GL_NO_ERROR); 1.67 + glShaderSource(sdr, 1, &src, 0); 1.68 + err = glGetError(); 1.69 + assert(err == GL_NO_ERROR); 1.70 + glCompileShader(sdr); 1.71 + assert(glGetError() == GL_NO_ERROR); 1.72 + 1.73 + glGetShaderiv(sdr, GL_COMPILE_STATUS, &success); 1.74 + assert(glGetError() == GL_NO_ERROR); 1.75 + glGetShaderiv(sdr, GL_INFO_LOG_LENGTH, &info_len); 1.76 + assert(glGetError() == GL_NO_ERROR); 1.77 + 1.78 + if(info_len) { 1.79 + if((info_str = malloc(info_len + 1))) { 1.80 + glGetShaderInfoLog(sdr, info_len, 0, info_str); 1.81 + assert(glGetError() == GL_NO_ERROR); 1.82 + info_str[info_len] = 0; 1.83 + } 1.84 + } 1.85 + 1.86 + if(success) { 1.87 + fprintf(stderr, info_str ? "done: %s\n" : "done\n", info_str); 1.88 + } else { 1.89 + fprintf(stderr, info_str ? "failed: %s\n" : "failed\n", info_str); 1.90 + glDeleteShader(sdr); 1.91 + sdr = 0; 1.92 + } 1.93 + 1.94 + free(info_str); 1.95 + return sdr; 1.96 +} 1.97 + 1.98 +void free_shader(unsigned int sdr) 1.99 +{ 1.100 + glDeleteShader(sdr); 1.101 +} 1.102 + 1.103 +unsigned int load_vertex_shader(const char *fname) 1.104 +{ 1.105 + return load_shader(fname, GL_VERTEX_SHADER); 1.106 +} 1.107 + 1.108 +unsigned int load_pixel_shader(const char *fname) 1.109 +{ 1.110 + return load_shader(fname, GL_FRAGMENT_SHADER); 1.111 +} 1.112 + 1.113 +unsigned int load_tessctl_shader(const char *fname) 1.114 +{ 1.115 +#ifdef GL_TESS_CONTROL_SHADER 1.116 + return load_shader(fname, GL_TESS_CONTROL_SHADER); 1.117 +#else 1.118 + return 0; 1.119 +#endif 1.120 +} 1.121 + 1.122 +unsigned int load_tesseval_shader(const char *fname) 1.123 +{ 1.124 +#ifdef GL_TESS_EVALUATION_SHADER 1.125 + return load_shader(fname, GL_TESS_EVALUATION_SHADER); 1.126 +#else 1.127 + return 0; 1.128 +#endif 1.129 +} 1.130 + 1.131 +unsigned int load_geometry_shader(const char *fname) 1.132 +{ 1.133 +#ifdef GL_GEOMETRY_SHADER 1.134 + return load_shader(fname, GL_GEOMETRY_SHADER); 1.135 +#else 1.136 + return 0; 1.137 +#endif 1.138 +} 1.139 + 1.140 +unsigned int load_shader(const char *fname, unsigned int sdr_type) 1.141 +{ 1.142 + unsigned int sdr; 1.143 + size_t filesize; 1.144 + FILE *fp; 1.145 + char *src; 1.146 + 1.147 + if(!(fp = fopen(fname, "rb"))) { 1.148 + fprintf(stderr, "failed to open shader %s: %s\n", fname, strerror(errno)); 1.149 + return 0; 1.150 + } 1.151 + 1.152 + fseek(fp, 0, SEEK_END); 1.153 + filesize = ftell(fp); 1.154 + fseek(fp, 0, SEEK_SET); 1.155 + 1.156 + if(!(src = malloc(filesize + 1))) { 1.157 + fclose(fp); 1.158 + return 0; 1.159 + } 1.160 + fread(src, 1, filesize, fp); 1.161 + src[filesize] = 0; 1.162 + fclose(fp); 1.163 + 1.164 + fprintf(stderr, "compiling %s shader: %s... ", sdrtypestr(sdr_type), fname); 1.165 + sdr = create_shader(src, sdr_type); 1.166 + 1.167 + free(src); 1.168 + return sdr; 1.169 +} 1.170 + 1.171 + 1.172 +/* ---- gpu programs ---- */ 1.173 + 1.174 +unsigned int create_program(void) 1.175 +{ 1.176 + unsigned int prog = glCreateProgram(); 1.177 + assert(glGetError() == GL_NO_ERROR); 1.178 + return prog; 1.179 +} 1.180 + 1.181 +unsigned int create_program_link(unsigned int sdr0, ...) 1.182 +{ 1.183 + unsigned int prog, sdr; 1.184 + va_list ap; 1.185 + 1.186 + if(!(prog = create_program())) { 1.187 + return 0; 1.188 + } 1.189 + 1.190 + attach_shader(prog, sdr0); 1.191 + if(glGetError()) { 1.192 + return 0; 1.193 + } 1.194 + 1.195 + va_start(ap, sdr0); 1.196 + while((sdr = va_arg(ap, unsigned int))) { 1.197 + attach_shader(prog, sdr); 1.198 + if(glGetError()) { 1.199 + return 0; 1.200 + } 1.201 + } 1.202 + va_end(ap); 1.203 + 1.204 + if(link_program(prog) == -1) { 1.205 + free_program(prog); 1.206 + return 0; 1.207 + } 1.208 + return prog; 1.209 +} 1.210 + 1.211 +unsigned int create_program_load(const char *vfile, const char *pfile) 1.212 +{ 1.213 + unsigned int vs = 0, ps = 0; 1.214 + 1.215 + if(vfile && *vfile && !(vs = load_vertex_shader(vfile))) { 1.216 + return 0; 1.217 + } 1.218 + if(pfile && *pfile && !(ps = load_pixel_shader(pfile))) { 1.219 + return 0; 1.220 + } 1.221 + return create_program_link(vs, ps, 0); 1.222 +} 1.223 + 1.224 +void free_program(unsigned int sdr) 1.225 +{ 1.226 + glDeleteProgram(sdr); 1.227 +} 1.228 + 1.229 +void attach_shader(unsigned int prog, unsigned int sdr) 1.230 +{ 1.231 + int err; 1.232 + 1.233 + if(prog && sdr) { 1.234 + assert(glGetError() == GL_NO_ERROR); 1.235 + glAttachShader(prog, sdr); 1.236 + if((err = glGetError()) != GL_NO_ERROR) { 1.237 + fprintf(stderr, "failed to attach shader %u to program %u (err: 0x%x)\n", sdr, prog, err); 1.238 + abort(); 1.239 + } 1.240 + } 1.241 +} 1.242 + 1.243 +int link_program(unsigned int prog) 1.244 +{ 1.245 + int linked, info_len, retval = 0; 1.246 + char *info_str = 0; 1.247 + 1.248 + glLinkProgram(prog); 1.249 + assert(glGetError() == GL_NO_ERROR); 1.250 + glGetProgramiv(prog, GL_LINK_STATUS, &linked); 1.251 + assert(glGetError() == GL_NO_ERROR); 1.252 + glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &info_len); 1.253 + assert(glGetError() == GL_NO_ERROR); 1.254 + 1.255 + if(info_len) { 1.256 + if((info_str = malloc(info_len + 1))) { 1.257 + glGetProgramInfoLog(prog, info_len, 0, info_str); 1.258 + assert(glGetError() == GL_NO_ERROR); 1.259 + info_str[info_len] = 0; 1.260 + } 1.261 + } 1.262 + 1.263 + if(linked) { 1.264 + fprintf(stderr, info_str ? "linking done: %s\n" : "linking done\n", info_str); 1.265 + } else { 1.266 + fprintf(stderr, info_str ? "linking failed: %s\n" : "linking failed\n", info_str); 1.267 + retval = -1; 1.268 + } 1.269 + 1.270 + free(info_str); 1.271 + return retval; 1.272 +} 1.273 + 1.274 +int bind_program(unsigned int prog) 1.275 +{ 1.276 + GLenum err; 1.277 + 1.278 + glUseProgram(prog); 1.279 + if(prog && (err = glGetError()) != GL_NO_ERROR) { 1.280 + /* maybe the program is not linked, try linking first */ 1.281 + if(err == GL_INVALID_OPERATION) { 1.282 + if(link_program(prog) == -1) { 1.283 + return -1; 1.284 + } 1.285 + glUseProgram(prog); 1.286 + return glGetError() == GL_NO_ERROR ? 0 : -1; 1.287 + } 1.288 + return -1; 1.289 + } 1.290 + return 0; 1.291 +} 1.292 + 1.293 +/* ugly but I'm not going to write the same bloody code over and over */ 1.294 +#define BEGIN_UNIFORM_CODE \ 1.295 + int loc, curr_prog; \ 1.296 + glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog); \ 1.297 + if((unsigned int)curr_prog != prog && bind_program(prog) == -1) { \ 1.298 + return -1; \ 1.299 + } \ 1.300 + if((loc = glGetUniformLocation(prog, name)) != -1) 1.301 + 1.302 +#define END_UNIFORM_CODE \ 1.303 + if((unsigned int)curr_prog != prog) { \ 1.304 + bind_program(curr_prog); \ 1.305 + } \ 1.306 + return loc == -1 ? -1 : 0 1.307 + 1.308 +int set_uniform_int(unsigned int prog, const char *name, int val) 1.309 +{ 1.310 + BEGIN_UNIFORM_CODE { 1.311 + glUniform1i(loc, val); 1.312 + } 1.313 + END_UNIFORM_CODE; 1.314 +} 1.315 + 1.316 +int set_uniform_float(unsigned int prog, const char *name, float val) 1.317 +{ 1.318 + BEGIN_UNIFORM_CODE { 1.319 + glUniform1f(loc, val); 1.320 + } 1.321 + END_UNIFORM_CODE; 1.322 +} 1.323 + 1.324 +int set_uniform_float2(unsigned int prog, const char *name, float x, float y) 1.325 +{ 1.326 + BEGIN_UNIFORM_CODE { 1.327 + glUniform2f(loc, x, y); 1.328 + } 1.329 + END_UNIFORM_CODE; 1.330 +} 1.331 + 1.332 +int set_uniform_float3(unsigned int prog, const char *name, float x, float y, float z) 1.333 +{ 1.334 + BEGIN_UNIFORM_CODE { 1.335 + glUniform3f(loc, x, y, z); 1.336 + } 1.337 + END_UNIFORM_CODE; 1.338 +} 1.339 + 1.340 +int set_uniform_float4(unsigned int prog, const char *name, float x, float y, float z, float w) 1.341 +{ 1.342 + BEGIN_UNIFORM_CODE { 1.343 + glUniform4f(loc, x, y, z, w); 1.344 + } 1.345 + END_UNIFORM_CODE; 1.346 +} 1.347 + 1.348 +int set_uniform_matrix4(unsigned int prog, const char *name, float *mat) 1.349 +{ 1.350 + BEGIN_UNIFORM_CODE { 1.351 + glUniformMatrix4fv(loc, 1, GL_FALSE, mat); 1.352 + } 1.353 + END_UNIFORM_CODE; 1.354 +} 1.355 + 1.356 +int set_uniform_matrix4_transposed(unsigned int prog, const char *name, float *mat) 1.357 +{ 1.358 + BEGIN_UNIFORM_CODE { 1.359 + glUniformMatrix4fv(loc, 1, GL_TRUE, mat); 1.360 + } 1.361 + END_UNIFORM_CODE; 1.362 +} 1.363 + 1.364 +int get_attrib_loc(unsigned int prog, const char *name) 1.365 +{ 1.366 + int loc, curr_prog; 1.367 + 1.368 + glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog); 1.369 + if((unsigned int)curr_prog != prog && bind_program(prog) == -1) { 1.370 + return -1; 1.371 + } 1.372 + 1.373 + loc = glGetAttribLocation(prog, (char*)name); 1.374 + 1.375 + if((unsigned int)curr_prog != prog) { 1.376 + bind_program(curr_prog); 1.377 + } 1.378 + return loc; 1.379 +} 1.380 + 1.381 +void set_attrib_float3(int attr_loc, float x, float y, float z) 1.382 +{ 1.383 + glVertexAttrib3f(attr_loc, x, y, z); 1.384 +} 1.385 + 1.386 +static const char *sdrtypestr(unsigned int sdrtype) 1.387 +{ 1.388 + switch(sdrtype) { 1.389 + case GL_VERTEX_SHADER: 1.390 + return "vertex"; 1.391 + case GL_FRAGMENT_SHADER: 1.392 + return "pixel"; 1.393 +#ifdef GL_TESS_CONTROL_SHADER 1.394 + case GL_TESS_CONTROL_SHADER: 1.395 + return "tessellation control"; 1.396 +#endif 1.397 +#ifdef GL_TESS_EVALUATION_SHADER 1.398 + case GL_TESS_EVALUATION_SHADER: 1.399 + return "tessellation evaluation"; 1.400 +#endif 1.401 +#ifdef GL_GEOMETRY_SHADER 1.402 + case GL_GEOMETRY_SHADER: 1.403 + return "geometry"; 1.404 +#endif 1.405 + 1.406 + default: 1.407 + break; 1.408 + } 1.409 + return "<unknown>"; 1.410 +}