3dphotoshoot
view src/sdr.c @ 10:c71c477521ca
converting to GLES2 and C++
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 31 May 2015 00:40:26 +0300 |
parents | |
children | aef7f51f6397 |
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 if(prog && sdr) {
237 glAttachShader(prog, sdr);
238 if(glGetError() != GL_NO_ERROR) {
239 fprintf(stderr, "failed to attach shader %u to program %u\n", sdr, prog);
240 abort();
241 }
242 }
243 }
245 int link_program(unsigned int prog)
246 {
247 int linked, info_len, retval = 0;
248 char *info_str = 0;
250 glLinkProgram(prog);
251 assert(glGetError() == GL_NO_ERROR);
252 glGetProgramiv(prog, GL_LINK_STATUS, &linked);
253 assert(glGetError() == GL_NO_ERROR);
254 glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &info_len);
255 assert(glGetError() == GL_NO_ERROR);
257 if(info_len) {
258 if((info_str = malloc(info_len + 1))) {
259 glGetProgramInfoLog(prog, info_len, 0, info_str);
260 assert(glGetError() == GL_NO_ERROR);
261 info_str[info_len] = 0;
262 }
263 }
265 if(linked) {
266 fprintf(stderr, info_str ? "linking done: %s\n" : "linking done\n", info_str);
267 } else {
268 fprintf(stderr, info_str ? "linking failed: %s\n" : "linking failed\n", info_str);
269 retval = -1;
270 }
272 free(info_str);
273 return retval;
274 }
276 int bind_program(unsigned int prog)
277 {
278 GLenum err;
280 glUseProgram(prog);
281 if(prog && (err = glGetError()) != GL_NO_ERROR) {
282 /* maybe the program is not linked, try linking first */
283 if(err == GL_INVALID_OPERATION) {
284 if(link_program(prog) == -1) {
285 return -1;
286 }
287 glUseProgram(prog);
288 return glGetError() == GL_NO_ERROR ? 0 : -1;
289 }
290 return -1;
291 }
292 return 0;
293 }
295 /* ugly but I'm not going to write the same bloody code over and over */
296 #define BEGIN_UNIFORM_CODE \
297 int loc, curr_prog; \
298 glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog); \
299 if((unsigned int)curr_prog != prog && bind_program(prog) == -1) { \
300 return -1; \
301 } \
302 if((loc = glGetUniformLocation(prog, name)) != -1)
304 #define END_UNIFORM_CODE \
305 if((unsigned int)curr_prog != prog) { \
306 bind_program(curr_prog); \
307 } \
308 return loc == -1 ? -1 : 0
310 int set_uniform_int(unsigned int prog, const char *name, int val)
311 {
312 BEGIN_UNIFORM_CODE {
313 glUniform1i(loc, val);
314 }
315 END_UNIFORM_CODE;
316 }
318 int set_uniform_float(unsigned int prog, const char *name, float val)
319 {
320 BEGIN_UNIFORM_CODE {
321 glUniform1f(loc, val);
322 }
323 END_UNIFORM_CODE;
324 }
326 int set_uniform_float2(unsigned int prog, const char *name, float x, float y)
327 {
328 BEGIN_UNIFORM_CODE {
329 glUniform2f(loc, x, y);
330 }
331 END_UNIFORM_CODE;
332 }
334 int set_uniform_float3(unsigned int prog, const char *name, float x, float y, float z)
335 {
336 BEGIN_UNIFORM_CODE {
337 glUniform3f(loc, x, y, z);
338 }
339 END_UNIFORM_CODE;
340 }
342 int set_uniform_float4(unsigned int prog, const char *name, float x, float y, float z, float w)
343 {
344 BEGIN_UNIFORM_CODE {
345 glUniform4f(loc, x, y, z, w);
346 }
347 END_UNIFORM_CODE;
348 }
350 int set_uniform_matrix4(unsigned int prog, const char *name, float *mat)
351 {
352 BEGIN_UNIFORM_CODE {
353 glUniformMatrix4fv(loc, 1, GL_FALSE, mat);
354 }
355 END_UNIFORM_CODE;
356 }
358 int set_uniform_matrix4_transposed(unsigned int prog, const char *name, float *mat)
359 {
360 BEGIN_UNIFORM_CODE {
361 glUniformMatrix4fv(loc, 1, GL_TRUE, mat);
362 }
363 END_UNIFORM_CODE;
364 }
366 int get_attrib_loc(unsigned int prog, const char *name)
367 {
368 int loc, curr_prog;
370 glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog);
371 if((unsigned int)curr_prog != prog && bind_program(prog) == -1) {
372 return -1;
373 }
375 loc = glGetAttribLocation(prog, (char*)name);
377 if((unsigned int)curr_prog != prog) {
378 bind_program(curr_prog);
379 }
380 return loc;
381 }
383 void set_attrib_float3(int attr_loc, float x, float y, float z)
384 {
385 glVertexAttrib3f(attr_loc, x, y, z);
386 }
388 static const char *sdrtypestr(unsigned int sdrtype)
389 {
390 switch(sdrtype) {
391 case GL_VERTEX_SHADER:
392 return "vertex";
393 case GL_FRAGMENT_SHADER:
394 return "pixel";
395 #ifdef GL_TESS_CONTROL_SHADER
396 case GL_TESS_CONTROL_SHADER:
397 return "tessellation control";
398 #endif
399 #ifdef GL_TESS_EVALUATION_SHADER
400 case GL_TESS_EVALUATION_SHADER:
401 return "tessellation evaluation";
402 #endif
403 #ifdef GL_GEOMETRY_SHADER
404 case GL_GEOMETRY_SHADER:
405 return "geometry";
406 #endif
408 default:
409 break;
410 }
411 return "<unknown>";
412 }