istereo2

annotate src/sdr.c @ 9:64e15874f3bd

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