3dphotoshoot

view src/sdr.c @ 18:dccbd7d65517

moved to android-19 to make it work with android 4.x
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 10 Jun 2015 18:53:28 +0300
parents c71c477521ca
children 4ca4e3c5a754
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <stdarg.h>
6 #include <assert.h>
7 #include "opengl.h"
9 #if defined(unix) || defined(__unix__)
10 #include <unistd.h>
11 #include <sys/stat.h>
12 #endif /* unix */
14 #include "sdr.h"
16 static const char *sdrtypestr(unsigned int sdrtype);
18 unsigned int create_vertex_shader(const char *src)
19 {
20 return create_shader(src, GL_VERTEX_SHADER);
21 }
23 unsigned int create_pixel_shader(const char *src)
24 {
25 return create_shader(src, GL_FRAGMENT_SHADER);
26 }
28 unsigned int create_tessctl_shader(const char *src)
29 {
30 #ifdef GL_TESS_CONTROL_SHADER
31 return create_shader(src, GL_TESS_CONTROL_SHADER);
32 #else
33 return 0;
34 #endif
35 }
37 unsigned int create_tesseval_shader(const char *src)
38 {
39 #ifdef GL_TESS_EVALUATION_SHADER
40 return create_shader(src, GL_TESS_EVALUATION_SHADER);
41 #else
42 return 0;
43 #endif
44 }
46 unsigned int create_geometry_shader(const char *src)
47 {
48 #ifdef GL_GEOMETRY_SHADER
49 return create_shader(src, GL_GEOMETRY_SHADER);
50 #else
51 return 0;
52 #endif
53 }
55 unsigned int create_shader(const char *src, unsigned int sdr_type)
56 {
57 unsigned int sdr;
58 int success, info_len;
59 char *info_str = 0;
60 GLenum err;
62 sdr = glCreateShader(sdr_type);
63 assert(glGetError() == GL_NO_ERROR);
64 glShaderSource(sdr, 1, &src, 0);
65 err = glGetError();
66 assert(err == GL_NO_ERROR);
67 glCompileShader(sdr);
68 assert(glGetError() == GL_NO_ERROR);
70 glGetShaderiv(sdr, GL_COMPILE_STATUS, &success);
71 assert(glGetError() == GL_NO_ERROR);
72 glGetShaderiv(sdr, GL_INFO_LOG_LENGTH, &info_len);
73 assert(glGetError() == GL_NO_ERROR);
75 if(info_len) {
76 if((info_str = malloc(info_len + 1))) {
77 glGetShaderInfoLog(sdr, info_len, 0, info_str);
78 assert(glGetError() == GL_NO_ERROR);
79 info_str[info_len] = 0;
80 }
81 }
83 if(success) {
84 fprintf(stderr, info_str ? "done: %s\n" : "done\n", info_str);
85 } else {
86 fprintf(stderr, info_str ? "failed: %s\n" : "failed\n", info_str);
87 glDeleteShader(sdr);
88 sdr = 0;
89 }
91 free(info_str);
92 return sdr;
93 }
95 void free_shader(unsigned int sdr)
96 {
97 glDeleteShader(sdr);
98 }
100 unsigned int load_vertex_shader(const char *fname)
101 {
102 return load_shader(fname, GL_VERTEX_SHADER);
103 }
105 unsigned int load_pixel_shader(const char *fname)
106 {
107 return load_shader(fname, GL_FRAGMENT_SHADER);
108 }
110 unsigned int load_tessctl_shader(const char *fname)
111 {
112 #ifdef GL_TESS_CONTROL_SHADER
113 return load_shader(fname, GL_TESS_CONTROL_SHADER);
114 #else
115 return 0;
116 #endif
117 }
119 unsigned int load_tesseval_shader(const char *fname)
120 {
121 #ifdef GL_TESS_EVALUATION_SHADER
122 return load_shader(fname, GL_TESS_EVALUATION_SHADER);
123 #else
124 return 0;
125 #endif
126 }
128 unsigned int load_geometry_shader(const char *fname)
129 {
130 #ifdef GL_GEOMETRY_SHADER
131 return load_shader(fname, GL_GEOMETRY_SHADER);
132 #else
133 return 0;
134 #endif
135 }
137 unsigned int load_shader(const char *fname, unsigned int sdr_type)
138 {
139 #if defined(unix) || defined(__unix__)
140 struct stat st;
141 #endif
142 unsigned int sdr;
143 size_t filesize;
144 FILE *fp;
145 char *src;
147 if(!(fp = fopen(fname, "rb"))) {
148 fprintf(stderr, "failed to open shader %s: %s\n", fname, strerror(errno));
149 return 0;
150 }
152 #if defined(unix) || defined(__unix__)
153 fstat(fileno(fp), &st);
154 filesize = st.st_size;
155 #else
156 fseek(fp, 0, SEEK_END);
157 filesize = ftell(fp);
158 fseek(fp, 0, SEEK_SET);
159 #endif /* unix */
161 if(!(src = malloc(filesize + 1))) {
162 fclose(fp);
163 return 0;
164 }
165 fread(src, 1, filesize, fp);
166 src[filesize] = 0;
167 fclose(fp);
169 fprintf(stderr, "compiling %s shader: %s... ", sdrtypestr(sdr_type), fname);
170 sdr = create_shader(src, sdr_type);
172 free(src);
173 return sdr;
174 }
177 /* ---- gpu programs ---- */
179 unsigned int create_program(void)
180 {
181 unsigned int prog = glCreateProgram();
182 assert(glGetError() == GL_NO_ERROR);
183 return prog;
184 }
186 unsigned int create_program_link(unsigned int sdr0, ...)
187 {
188 unsigned int prog, sdr;
189 va_list ap;
191 if(!(prog = create_program())) {
192 return 0;
193 }
195 attach_shader(prog, sdr0);
196 if(glGetError()) {
197 return 0;
198 }
200 va_start(ap, sdr0);
201 while((sdr = va_arg(ap, unsigned int))) {
202 attach_shader(prog, sdr);
203 if(glGetError()) {
204 return 0;
205 }
206 }
207 va_end(ap);
209 if(link_program(prog) == -1) {
210 free_program(prog);
211 return 0;
212 }
213 return prog;
214 }
216 unsigned int create_program_load(const char *vfile, const char *pfile)
217 {
218 unsigned int vs = 0, ps = 0;
220 if(vfile && *vfile && !(vs = load_vertex_shader(vfile))) {
221 return 0;
222 }
223 if(pfile && *pfile && !(ps = load_pixel_shader(pfile))) {
224 return 0;
225 }
226 return create_program_link(vs, ps, 0);
227 }
229 void free_program(unsigned int sdr)
230 {
231 glDeleteProgram(sdr);
232 }
234 void attach_shader(unsigned int prog, unsigned int sdr)
235 {
236 int err;
238 if(prog && sdr) {
239 assert(glGetError() == GL_NO_ERROR);
240 glAttachShader(prog, sdr);
241 if((err = glGetError()) != GL_NO_ERROR) {
242 fprintf(stderr, "failed to attach shader %u to program %u (err: 0x%x)\n", sdr, prog, err);
243 abort();
244 }
245 }
246 }
248 int link_program(unsigned int prog)
249 {
250 int linked, info_len, retval = 0;
251 char *info_str = 0;
253 glLinkProgram(prog);
254 assert(glGetError() == GL_NO_ERROR);
255 glGetProgramiv(prog, GL_LINK_STATUS, &linked);
256 assert(glGetError() == GL_NO_ERROR);
257 glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &info_len);
258 assert(glGetError() == GL_NO_ERROR);
260 if(info_len) {
261 if((info_str = malloc(info_len + 1))) {
262 glGetProgramInfoLog(prog, info_len, 0, info_str);
263 assert(glGetError() == GL_NO_ERROR);
264 info_str[info_len] = 0;
265 }
266 }
268 if(linked) {
269 fprintf(stderr, info_str ? "linking done: %s\n" : "linking done\n", info_str);
270 } else {
271 fprintf(stderr, info_str ? "linking failed: %s\n" : "linking failed\n", info_str);
272 retval = -1;
273 }
275 free(info_str);
276 return retval;
277 }
279 int bind_program(unsigned int prog)
280 {
281 GLenum err;
283 glUseProgram(prog);
284 if(prog && (err = glGetError()) != GL_NO_ERROR) {
285 /* maybe the program is not linked, try linking first */
286 if(err == GL_INVALID_OPERATION) {
287 if(link_program(prog) == -1) {
288 return -1;
289 }
290 glUseProgram(prog);
291 return glGetError() == GL_NO_ERROR ? 0 : -1;
292 }
293 return -1;
294 }
295 return 0;
296 }
298 /* ugly but I'm not going to write the same bloody code over and over */
299 #define BEGIN_UNIFORM_CODE \
300 int loc, curr_prog; \
301 glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog); \
302 if((unsigned int)curr_prog != prog && bind_program(prog) == -1) { \
303 return -1; \
304 } \
305 if((loc = glGetUniformLocation(prog, name)) != -1)
307 #define END_UNIFORM_CODE \
308 if((unsigned int)curr_prog != prog) { \
309 bind_program(curr_prog); \
310 } \
311 return loc == -1 ? -1 : 0
313 int set_uniform_int(unsigned int prog, const char *name, int val)
314 {
315 BEGIN_UNIFORM_CODE {
316 glUniform1i(loc, val);
317 }
318 END_UNIFORM_CODE;
319 }
321 int set_uniform_float(unsigned int prog, const char *name, float val)
322 {
323 BEGIN_UNIFORM_CODE {
324 glUniform1f(loc, val);
325 }
326 END_UNIFORM_CODE;
327 }
329 int set_uniform_float2(unsigned int prog, const char *name, float x, float y)
330 {
331 BEGIN_UNIFORM_CODE {
332 glUniform2f(loc, x, y);
333 }
334 END_UNIFORM_CODE;
335 }
337 int set_uniform_float3(unsigned int prog, const char *name, float x, float y, float z)
338 {
339 BEGIN_UNIFORM_CODE {
340 glUniform3f(loc, x, y, z);
341 }
342 END_UNIFORM_CODE;
343 }
345 int set_uniform_float4(unsigned int prog, const char *name, float x, float y, float z, float w)
346 {
347 BEGIN_UNIFORM_CODE {
348 glUniform4f(loc, x, y, z, w);
349 }
350 END_UNIFORM_CODE;
351 }
353 int set_uniform_matrix4(unsigned int prog, const char *name, float *mat)
354 {
355 BEGIN_UNIFORM_CODE {
356 glUniformMatrix4fv(loc, 1, GL_FALSE, mat);
357 }
358 END_UNIFORM_CODE;
359 }
361 int set_uniform_matrix4_transposed(unsigned int prog, const char *name, float *mat)
362 {
363 BEGIN_UNIFORM_CODE {
364 glUniformMatrix4fv(loc, 1, GL_TRUE, mat);
365 }
366 END_UNIFORM_CODE;
367 }
369 int get_attrib_loc(unsigned int prog, const char *name)
370 {
371 int loc, curr_prog;
373 glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog);
374 if((unsigned int)curr_prog != prog && bind_program(prog) == -1) {
375 return -1;
376 }
378 loc = glGetAttribLocation(prog, (char*)name);
380 if((unsigned int)curr_prog != prog) {
381 bind_program(curr_prog);
382 }
383 return loc;
384 }
386 void set_attrib_float3(int attr_loc, float x, float y, float z)
387 {
388 glVertexAttrib3f(attr_loc, x, y, z);
389 }
391 static const char *sdrtypestr(unsigned int sdrtype)
392 {
393 switch(sdrtype) {
394 case GL_VERTEX_SHADER:
395 return "vertex";
396 case GL_FRAGMENT_SHADER:
397 return "pixel";
398 #ifdef GL_TESS_CONTROL_SHADER
399 case GL_TESS_CONTROL_SHADER:
400 return "tessellation control";
401 #endif
402 #ifdef GL_TESS_EVALUATION_SHADER
403 case GL_TESS_EVALUATION_SHADER:
404 return "tessellation evaluation";
405 #endif
406 #ifdef GL_GEOMETRY_SHADER
407 case GL_GEOMETRY_SHADER:
408 return "geometry";
409 #endif
411 default:
412 break;
413 }
414 return "<unknown>";
415 }