rayfract
diff src/sdr.c @ 0:09bb67c000bc
ray-fract repository
author | John Tsiombikas <nuclear@siggraph.org> |
---|---|
date | Thu, 21 Oct 2010 23:39:26 +0300 |
parents | |
children | 1496aae2e7d4 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/sdr.c Thu Oct 21 23:39:26 2010 +0300 1.3 @@ -0,0 +1,324 @@ 1.4 +#include <stdio.h> 1.5 +#include <stdlib.h> 1.6 +#include <string.h> 1.7 +#include <errno.h> 1.8 +#include <assert.h> 1.9 +#include <GL/glew.h> 1.10 + 1.11 +#if defined(unix) || defined(__unix__) 1.12 +#include <unistd.h> 1.13 +#include <sys/stat.h> 1.14 +#endif /* unix */ 1.15 + 1.16 +#include "sdr.h" 1.17 + 1.18 +unsigned int create_vertex_shader(const char *src) 1.19 +{ 1.20 + return create_shader(src, GL_VERTEX_SHADER); 1.21 +} 1.22 + 1.23 +unsigned int create_pixel_shader(const char *src) 1.24 +{ 1.25 + return create_shader(src, GL_FRAGMENT_SHADER); 1.26 +} 1.27 + 1.28 +unsigned int create_shader(const char *src, unsigned int sdr_type) 1.29 +{ 1.30 + unsigned int sdr; 1.31 + int success, info_len; 1.32 + char *info_str = 0; 1.33 + GLenum err; 1.34 + 1.35 + sdr = glCreateShader(sdr_type); 1.36 + assert(glGetError() == GL_NO_ERROR); 1.37 + glShaderSource(sdr, 1, &src, 0); 1.38 + err = glGetError(); 1.39 + assert(err == GL_NO_ERROR); 1.40 + glCompileShader(sdr); 1.41 + assert(glGetError() == GL_NO_ERROR); 1.42 + 1.43 + glGetShaderiv(sdr, GL_COMPILE_STATUS, &success); 1.44 + assert(glGetError() == GL_NO_ERROR); 1.45 + glGetShaderiv(sdr, GL_INFO_LOG_LENGTH, &info_len); 1.46 + assert(glGetError() == GL_NO_ERROR); 1.47 + 1.48 + if(info_len) { 1.49 + if((info_str = malloc(info_len + 1))) { 1.50 + glGetShaderInfoLog(sdr, info_len, 0, info_str); 1.51 + assert(glGetError() == GL_NO_ERROR); 1.52 + } 1.53 + } 1.54 + 1.55 + if(success) { 1.56 + fprintf(stderr, info_str ? "done: %s\n" : "done\n", info_str); 1.57 + } else { 1.58 + fprintf(stderr, info_str ? "failed: %s\n" : "failed\n", info_str); 1.59 + glDeleteShader(sdr); 1.60 + sdr = 0; 1.61 + } 1.62 + 1.63 + free(info_str); 1.64 + return sdr; 1.65 +} 1.66 + 1.67 +void free_shader(unsigned int sdr) 1.68 +{ 1.69 + glDeleteShader(sdr); 1.70 +} 1.71 + 1.72 +unsigned int load_vertex_shader(const char *fname) 1.73 +{ 1.74 + return load_shader(fname, GL_VERTEX_SHADER); 1.75 +} 1.76 + 1.77 +unsigned int load_pixel_shader(const char *fname) 1.78 +{ 1.79 + return load_shader(fname, GL_FRAGMENT_SHADER); 1.80 +} 1.81 + 1.82 +unsigned int load_shader(const char *fname, unsigned int sdr_type) 1.83 +{ 1.84 +#if defined(unix) || defined(__unix__) 1.85 + struct stat st; 1.86 +#endif 1.87 + unsigned int sdr; 1.88 + size_t filesize; 1.89 + FILE *fp; 1.90 + char *src; 1.91 + 1.92 + if(!(fp = fopen(fname, "r"))) { 1.93 + fprintf(stderr, "failed to open shader %s: %s\n", fname, strerror(errno)); 1.94 + return 0; 1.95 + } 1.96 + 1.97 +#if defined(unix) || defined(__unix__) 1.98 + fstat(fileno(fp), &st); 1.99 + filesize = st.st_size; 1.100 +#else 1.101 + fseek(fp, 0, SEEK_END); 1.102 + filesize = ftell(fp); 1.103 + fseek(fp, 0, SEEK_SET); 1.104 +#endif /* unix */ 1.105 + 1.106 + if(!(src = malloc(filesize + 1))) { 1.107 + fclose(fp); 1.108 + return 0; 1.109 + } 1.110 + fread(src, 1, filesize, fp); 1.111 + src[filesize] = 0; 1.112 + fclose(fp); 1.113 + 1.114 + fprintf(stderr, "compiling %s shader: %s... ", (sdr_type == GL_VERTEX_SHADER ? "vertex" : "pixel"), fname); 1.115 + sdr = create_shader(src, sdr_type); 1.116 + 1.117 + free(src); 1.118 + return sdr; 1.119 +} 1.120 + 1.121 + 1.122 +unsigned int get_vertex_shader(const char *fname) 1.123 +{ 1.124 + return get_shader(fname, GL_VERTEX_SHADER); 1.125 +} 1.126 + 1.127 +unsigned int get_pixel_shader(const char *fname) 1.128 +{ 1.129 + return get_shader(fname, GL_FRAGMENT_SHADER); 1.130 +} 1.131 + 1.132 +unsigned int get_shader(const char *fname, unsigned int sdr_type) 1.133 +{ 1.134 + unsigned int sdr; 1.135 +#if 0 1.136 + if((res = get_resource(sdrman, fname))) { 1.137 + /* TODO: validate that the shader matches sdr_type? */ 1.138 + return (uintptr_t)res; 1.139 + } 1.140 +#endif 1.141 + 1.142 + if(!(sdr = load_shader(fname, sdr_type))) { 1.143 + return 0; 1.144 + } 1.145 + add_shader(fname, sdr); 1.146 + return sdr; 1.147 +} 1.148 + 1.149 +int add_shader(const char *fname, unsigned int sdr) 1.150 +{ 1.151 + return 0;/*add_resource(sdrman, fname, (void*)(uintptr_t)sdr);*/ 1.152 +} 1.153 + 1.154 +int remove_shader(const char *fname) 1.155 +{ 1.156 + return 0;/*remove_resource(sdrman, fname);*/ 1.157 +} 1.158 + 1.159 + 1.160 +/* ---- gpu programs ---- */ 1.161 + 1.162 +unsigned int create_program(void) 1.163 +{ 1.164 + unsigned int prog = glCreateProgram(); 1.165 + assert(glGetError() == GL_NO_ERROR); 1.166 + return prog; 1.167 +} 1.168 + 1.169 +unsigned int create_program_link(unsigned int vs, unsigned int ps) 1.170 +{ 1.171 + unsigned int prog; 1.172 + 1.173 + if(!(prog = create_program())) { 1.174 + return 0; 1.175 + } 1.176 + 1.177 + attach_shader(prog, vs); 1.178 + assert(glGetError() == GL_NO_ERROR); 1.179 + attach_shader(prog, ps); 1.180 + assert(glGetError() == GL_NO_ERROR); 1.181 + 1.182 + if(link_program(prog) == -1) { 1.183 + free_program(prog); 1.184 + return 0; 1.185 + } 1.186 + return prog; 1.187 +} 1.188 + 1.189 +unsigned int create_program_load(const char *vfile, const char *pfile) 1.190 +{ 1.191 + unsigned int vs, ps; 1.192 + 1.193 + if(!(vs = get_vertex_shader(vfile)) || !(ps = get_pixel_shader(pfile))) { 1.194 + return 0; 1.195 + } 1.196 + return create_program_link(vs, ps); 1.197 +} 1.198 + 1.199 +void free_program(unsigned int sdr) 1.200 +{ 1.201 + glDeleteProgram(sdr); 1.202 +} 1.203 + 1.204 +void attach_shader(unsigned int prog, unsigned int sdr) 1.205 +{ 1.206 + glAttachShader(prog, sdr); 1.207 + assert(glGetError() == GL_NO_ERROR); 1.208 +} 1.209 + 1.210 +int link_program(unsigned int prog) 1.211 +{ 1.212 + int linked, info_len, retval = 0; 1.213 + char *info_str = 0; 1.214 + 1.215 + glLinkProgram(prog); 1.216 + assert(glGetError() == GL_NO_ERROR); 1.217 + glGetProgramiv(prog, GL_LINK_STATUS, &linked); 1.218 + assert(glGetError() == GL_NO_ERROR); 1.219 + glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &info_len); 1.220 + assert(glGetError() == GL_NO_ERROR); 1.221 + 1.222 + if(info_len) { 1.223 + if((info_str = malloc(info_len + 1))) { 1.224 + glGetProgramInfoLog(prog, info_len, 0, info_str); 1.225 + assert(glGetError() == GL_NO_ERROR); 1.226 + } 1.227 + } 1.228 + 1.229 + if(linked) { 1.230 + fprintf(stderr, info_str ? "linking done: %s\n" : "linking done\n", info_str); 1.231 + } else { 1.232 + fprintf(stderr, info_str ? "linking failed: %s\n" : "linking failed\n", info_str); 1.233 + retval = -1; 1.234 + } 1.235 + 1.236 + free(info_str); 1.237 + return retval; 1.238 +} 1.239 + 1.240 +int bind_program(unsigned int prog) 1.241 +{ 1.242 + GLenum err; 1.243 + 1.244 + glUseProgram(prog); 1.245 + if(prog && (err = glGetError()) != GL_NO_ERROR) { 1.246 + /* maybe the program is not linked, try to link first */ 1.247 + if(err == GL_INVALID_OPERATION) { 1.248 + if(link_program(prog) == -1) { 1.249 + return -1; 1.250 + } 1.251 + glUseProgram(prog); 1.252 + return glGetError() == GL_NO_ERROR ? 0 : -1; 1.253 + } 1.254 + return -1; 1.255 + } 1.256 + return 0; 1.257 +} 1.258 + 1.259 +/* ugly but I'm not going to write the same bloody code over and over */ 1.260 +#define BEGIN_UNIFORM_CODE \ 1.261 + int loc, curr_prog; \ 1.262 + glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog); \ 1.263 + if(curr_prog != prog && bind_program(prog) == -1) { \ 1.264 + return -1; \ 1.265 + } \ 1.266 + if((loc = glGetUniformLocation(prog, name)) != -1) 1.267 + 1.268 +#define END_UNIFORM_CODE \ 1.269 + if(curr_prog != prog) { \ 1.270 + bind_program(curr_prog); \ 1.271 + } \ 1.272 + return loc == -1 ? -1 : 0 1.273 + 1.274 +int set_uniform_int(unsigned int prog, const char *name, int val) 1.275 +{ 1.276 + BEGIN_UNIFORM_CODE { 1.277 + glUniform1i(loc, val); 1.278 + } 1.279 + END_UNIFORM_CODE; 1.280 +} 1.281 + 1.282 +int set_uniform_float(unsigned int prog, const char *name, float val) 1.283 +{ 1.284 + BEGIN_UNIFORM_CODE { 1.285 + glUniform1f(loc, val); 1.286 + } 1.287 + END_UNIFORM_CODE; 1.288 +} 1.289 + 1.290 +int set_uniform_float3(unsigned int prog, const char *name, float x, float y, float z) 1.291 +{ 1.292 + BEGIN_UNIFORM_CODE { 1.293 + glUniform3f(loc, x, y, z); 1.294 + } 1.295 + END_UNIFORM_CODE; 1.296 +} 1.297 + 1.298 +int set_uniform_float4(unsigned int prog, const char *name, float x, float y, float z, float w) 1.299 +{ 1.300 + BEGIN_UNIFORM_CODE { 1.301 + glUniform4f(loc, x, y, z, w); 1.302 + } 1.303 + END_UNIFORM_CODE; 1.304 +} 1.305 + 1.306 + 1.307 +int get_attrib_loc(unsigned int prog, const char *name) 1.308 +{ 1.309 + int loc, curr_prog; 1.310 + 1.311 + glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog); 1.312 + if(curr_prog != prog && bind_program(prog) == -1) { 1.313 + return -1; 1.314 + } 1.315 + 1.316 + loc = glGetAttribLocation(prog, (char*)name); 1.317 + 1.318 + if(curr_prog != prog) { 1.319 + bind_program(curr_prog); 1.320 + } 1.321 + return loc; 1.322 +} 1.323 + 1.324 +void set_attrib_float3(int attr_loc, float x, float y, float z) 1.325 +{ 1.326 + glVertexAttrib3f(attr_loc, x, y, z); 1.327 +}