istereo

view src/sdr.c @ 0:1bb950d0976b

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