dungeon_crawler

annotate prototype/src/sdr.c @ 61:4472ef41a209

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