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