fractorb

view src/sdr.c @ 0:6e849d7377ff

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 18 Nov 2017 20:04:16 +0200
parents
children
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);
17 static int sdrtypeidx(unsigned int sdrtype);
20 unsigned int create_vertex_shader(const char *src)
21 {
22 return create_shader(src, GL_VERTEX_SHADER);
23 }
25 unsigned int create_pixel_shader(const char *src)
26 {
27 return create_shader(src, GL_FRAGMENT_SHADER);
28 }
30 unsigned int create_tessctl_shader(const char *src)
31 {
32 #ifdef GL_TESS_CONTROL_SHADER
33 return create_shader(src, GL_TESS_CONTROL_SHADER);
34 #else
35 return 0;
36 #endif
37 }
39 unsigned int create_tesseval_shader(const char *src)
40 {
41 #ifdef GL_TESS_EVALUATION_SHADER
42 return create_shader(src, GL_TESS_EVALUATION_SHADER);
43 #else
44 return 0;
45 #endif
46 }
48 unsigned int create_geometry_shader(const char *src)
49 {
50 #ifdef GL_GEOMETRY_SHADER
51 return create_shader(src, GL_GEOMETRY_SHADER);
52 #else
53 return 0;
54 #endif
55 }
57 unsigned int create_shader(const char *src, unsigned int sdr_type)
58 {
59 unsigned int sdr;
60 int success, info_len;
61 char *info_str = 0;
62 const char *src_str[3], *header, *footer;
63 int src_str_count = 0;
64 GLenum err;
66 if((header = get_shader_header(sdr_type))) {
67 src_str[src_str_count++] = header;
68 }
69 src_str[src_str_count++] = src;
70 if((footer = get_shader_footer(sdr_type))) {
71 src_str[src_str_count++] = footer;
72 }
74 sdr = glCreateShader(sdr_type);
75 assert(glGetError() == GL_NO_ERROR);
76 glShaderSource(sdr, src_str_count, src_str, 0);
77 err = glGetError();
78 assert(err == GL_NO_ERROR);
79 glCompileShader(sdr);
80 assert(glGetError() == GL_NO_ERROR);
82 glGetShaderiv(sdr, GL_COMPILE_STATUS, &success);
83 assert(glGetError() == GL_NO_ERROR);
84 glGetShaderiv(sdr, GL_INFO_LOG_LENGTH, &info_len);
85 assert(glGetError() == GL_NO_ERROR);
87 if(info_len) {
88 if((info_str = malloc(info_len + 1))) {
89 glGetShaderInfoLog(sdr, info_len, 0, info_str);
90 assert(glGetError() == GL_NO_ERROR);
91 info_str[info_len] = 0;
92 }
93 }
95 if(success) {
96 fprintf(stderr, info_str ? "done: %s\n" : "done\n", info_str);
97 } else {
98 fprintf(stderr, info_str ? "failed: %s\n" : "failed\n", info_str);
99 glDeleteShader(sdr);
100 sdr = 0;
101 }
103 free(info_str);
104 return sdr;
105 }
107 void free_shader(unsigned int sdr)
108 {
109 glDeleteShader(sdr);
110 }
112 unsigned int load_vertex_shader(const char *fname)
113 {
114 return load_shader(fname, GL_VERTEX_SHADER);
115 }
117 unsigned int load_pixel_shader(const char *fname)
118 {
119 return load_shader(fname, GL_FRAGMENT_SHADER);
120 }
122 unsigned int load_tessctl_shader(const char *fname)
123 {
124 #ifdef GL_TESS_CONTROL_SHADER
125 return load_shader(fname, GL_TESS_CONTROL_SHADER);
126 #else
127 return 0;
128 #endif
129 }
131 unsigned int load_tesseval_shader(const char *fname)
132 {
133 #ifdef GL_TESS_EVALUATION_SHADER
134 return load_shader(fname, GL_TESS_EVALUATION_SHADER);
135 #else
136 return 0;
137 #endif
138 }
140 unsigned int load_geometry_shader(const char *fname)
141 {
142 #ifdef GL_GEOMETRY_SHADER
143 return load_shader(fname, GL_GEOMETRY_SHADER);
144 #else
145 return 0;
146 #endif
147 }
149 unsigned int load_shader(const char *fname, unsigned int sdr_type)
150 {
151 unsigned int sdr;
152 size_t filesize;
153 FILE *fp;
154 char *src;
156 if(!(fp = fopen(fname, "rb"))) {
157 fprintf(stderr, "failed to open shader %s: %s\n", fname, strerror(errno));
158 return 0;
159 }
161 fseek(fp, 0, SEEK_END);
162 filesize = ftell(fp);
163 fseek(fp, 0, SEEK_SET);
165 if(!(src = malloc(filesize + 1))) {
166 fclose(fp);
167 return 0;
168 }
169 fread(src, 1, filesize, fp);
170 src[filesize] = 0;
171 fclose(fp);
173 fprintf(stderr, "compiling %s shader: %s... ", sdrtypestr(sdr_type), fname);
174 sdr = create_shader(src, sdr_type);
176 free(src);
177 return sdr;
178 }
181 /* ---- gpu programs ---- */
183 unsigned int create_program(void)
184 {
185 unsigned int prog = glCreateProgram();
186 assert(glGetError() == GL_NO_ERROR);
187 return prog;
188 }
190 unsigned int create_program_link(unsigned int sdr0, ...)
191 {
192 unsigned int prog, sdr;
193 va_list ap;
195 if(!(prog = create_program())) {
196 return 0;
197 }
199 attach_shader(prog, sdr0);
200 if(glGetError()) {
201 return 0;
202 }
204 va_start(ap, sdr0);
205 while((sdr = va_arg(ap, unsigned int))) {
206 attach_shader(prog, sdr);
207 if(glGetError()) {
208 return 0;
209 }
210 }
211 va_end(ap);
213 if(link_program(prog) == -1) {
214 free_program(prog);
215 return 0;
216 }
217 return prog;
218 }
220 unsigned int create_program_load(const char *vfile, const char *pfile)
221 {
222 unsigned int vs = 0, ps = 0;
224 if(vfile && *vfile && !(vs = load_vertex_shader(vfile))) {
225 return 0;
226 }
227 if(pfile && *pfile && !(ps = load_pixel_shader(pfile))) {
228 return 0;
229 }
230 return create_program_link(vs, ps, 0);
231 }
233 void free_program(unsigned int sdr)
234 {
235 glDeleteProgram(sdr);
236 }
238 void attach_shader(unsigned int prog, unsigned int sdr)
239 {
240 int err;
242 if(prog && sdr) {
243 assert(glGetError() == GL_NO_ERROR);
244 glAttachShader(prog, sdr);
245 if((err = glGetError()) != GL_NO_ERROR) {
246 fprintf(stderr, "failed to attach shader %u to program %u (err: 0x%x)\n", sdr, prog, err);
247 abort();
248 }
249 }
250 }
252 int link_program(unsigned int prog)
253 {
254 int linked, info_len, retval = 0;
255 char *info_str = 0;
257 glLinkProgram(prog);
258 assert(glGetError() == GL_NO_ERROR);
259 glGetProgramiv(prog, GL_LINK_STATUS, &linked);
260 assert(glGetError() == GL_NO_ERROR);
261 glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &info_len);
262 assert(glGetError() == GL_NO_ERROR);
264 if(info_len) {
265 if((info_str = malloc(info_len + 1))) {
266 glGetProgramInfoLog(prog, info_len, 0, info_str);
267 assert(glGetError() == GL_NO_ERROR);
268 info_str[info_len] = 0;
269 }
270 }
272 if(linked) {
273 fprintf(stderr, info_str ? "linking done: %s\n" : "linking done\n", info_str);
274 } else {
275 fprintf(stderr, info_str ? "linking failed: %s\n" : "linking failed\n", info_str);
276 retval = -1;
277 }
279 free(info_str);
280 return retval;
281 }
283 int bind_program(unsigned int prog)
284 {
285 GLenum err;
287 glUseProgram(prog);
288 if(prog && (err = glGetError()) != GL_NO_ERROR) {
289 /* maybe the program is not linked, try linking first */
290 if(err == GL_INVALID_OPERATION) {
291 if(link_program(prog) == -1) {
292 return -1;
293 }
294 glUseProgram(prog);
295 return glGetError() == GL_NO_ERROR ? 0 : -1;
296 }
297 return -1;
298 }
299 return 0;
300 }
302 /* ugly but I'm not going to write the same bloody code over and over */
303 #define BEGIN_UNIFORM_CODE \
304 int loc, curr_prog; \
305 glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog); \
306 if((unsigned int)curr_prog != prog && bind_program(prog) == -1) { \
307 return -1; \
308 } \
309 if((loc = glGetUniformLocation(prog, name)) != -1)
311 #define END_UNIFORM_CODE \
312 if((unsigned int)curr_prog != prog) { \
313 bind_program(curr_prog); \
314 } \
315 return loc == -1 ? -1 : 0
317 int get_uniform_loc(unsigned int prog, const char *name)
318 {
319 int loc, curr_prog;
320 glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog);
321 if((unsigned int)curr_prog != prog && bind_program(prog) == -1) {
322 return -1;
323 }
324 loc = glGetUniformLocation(prog, name);
325 if((unsigned int)curr_prog != prog) {
326 bind_program(curr_prog);
327 }
328 return loc;
329 }
331 int set_uniform_int(unsigned int prog, const char *name, int val)
332 {
333 BEGIN_UNIFORM_CODE {
334 glUniform1i(loc, val);
335 }
336 END_UNIFORM_CODE;
337 }
339 int set_uniform_float(unsigned int prog, const char *name, float val)
340 {
341 BEGIN_UNIFORM_CODE {
342 glUniform1f(loc, val);
343 }
344 END_UNIFORM_CODE;
345 }
347 int set_uniform_float2(unsigned int prog, const char *name, float x, float y)
348 {
349 BEGIN_UNIFORM_CODE {
350 glUniform2f(loc, x, y);
351 }
352 END_UNIFORM_CODE;
353 }
355 int set_uniform_float3(unsigned int prog, const char *name, float x, float y, float z)
356 {
357 BEGIN_UNIFORM_CODE {
358 glUniform3f(loc, x, y, z);
359 }
360 END_UNIFORM_CODE;
361 }
363 int set_uniform_float4(unsigned int prog, const char *name, float x, float y, float z, float w)
364 {
365 BEGIN_UNIFORM_CODE {
366 glUniform4f(loc, x, y, z, w);
367 }
368 END_UNIFORM_CODE;
369 }
371 int set_uniform_matrix4(unsigned int prog, const char *name, const float *mat)
372 {
373 BEGIN_UNIFORM_CODE {
374 glUniformMatrix4fv(loc, 1, GL_FALSE, mat);
375 }
376 END_UNIFORM_CODE;
377 }
379 int set_uniform_matrix4_transposed(unsigned int prog, const char *name, const float *mat)
380 {
381 BEGIN_UNIFORM_CODE {
382 glUniformMatrix4fv(loc, 1, GL_TRUE, mat);
383 }
384 END_UNIFORM_CODE;
385 }
387 int get_attrib_loc(unsigned int prog, const char *name)
388 {
389 int loc, curr_prog;
391 glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog);
392 if((unsigned int)curr_prog != prog && bind_program(prog) == -1) {
393 return -1;
394 }
396 loc = glGetAttribLocation(prog, (char*)name);
398 if((unsigned int)curr_prog != prog) {
399 bind_program(curr_prog);
400 }
401 return loc;
402 }
404 void set_attrib_float3(int attr_loc, float x, float y, float z)
405 {
406 glVertexAttrib3f(attr_loc, x, y, z);
407 }
409 /* ---- shader composition ---- */
410 struct string {
411 char *text;
412 int len;
413 };
415 #define NUM_SHADER_TYPES 5
416 static struct string header[NUM_SHADER_TYPES];
417 static struct string footer[NUM_SHADER_TYPES];
419 static void clear_string(struct string *str)
420 {
421 free(str->text);
422 str->text = 0;
423 str->len = 0;
424 }
426 static void append_string(struct string *str, const char *s)
427 {
428 int len, newlen;
429 char *newstr;
431 if(!s || !*s) return;
433 len = strlen(s);
434 newlen = str->len + len;
435 if(!(newstr = malloc(newlen + 2))) { /* leave space for a possible newline */
436 fprintf(stderr, "shader composition: failed to append string of size %d\n", len);
437 abort();
438 }
440 if(str->text) {
441 memcpy(newstr, str->text, str->len);
442 }
443 memcpy(newstr + str->len, s, len + 1);
445 if(s[len - 1] != '\n') {
446 newstr[newlen] = '\n';
447 newstr[newlen + 1] = 0;
448 }
450 free(str->text);
451 str->text = newstr;
452 str->len = newlen;
453 }
455 void clear_shader_header(unsigned int type)
456 {
457 if(type) {
458 int idx = sdrtypeidx(type);
459 clear_string(&header[idx]);
460 } else {
461 int i;
462 for(i=0; i<NUM_SHADER_TYPES; i++) {
463 clear_string(&header[i]);
464 }
465 }
466 }
468 void clear_shader_footer(unsigned int type)
469 {
470 if(type) {
471 int idx = sdrtypeidx(type);
472 clear_string(&footer[idx]);
473 } else {
474 int i;
475 for(i=0; i<NUM_SHADER_TYPES; i++) {
476 clear_string(&footer[i]);
477 }
478 }
479 }
481 void add_shader_header(unsigned int type, const char *s)
482 {
483 if(type) {
484 int idx = sdrtypeidx(type);
485 append_string(&header[idx], s);
486 } else {
487 int i;
488 for(i=0; i<NUM_SHADER_TYPES; i++) {
489 append_string(&header[i], s);
490 }
491 }
492 }
494 void add_shader_footer(unsigned int type, const char *s)
495 {
496 if(type) {
497 int idx = sdrtypeidx(type);
498 append_string(&footer[idx], s);
499 } else {
500 int i;
501 for(i=0; i<NUM_SHADER_TYPES; i++) {
502 append_string(&footer[i], s);
503 }
504 }
505 }
507 const char *get_shader_header(unsigned int type)
508 {
509 int idx = sdrtypeidx(type);
510 return header[idx].text;
511 }
513 const char *get_shader_footer(unsigned int type)
514 {
515 int idx = sdrtypeidx(type);
516 return footer[idx].text;
517 }
519 static const char *sdrtypestr(unsigned int sdrtype)
520 {
521 switch(sdrtype) {
522 case GL_VERTEX_SHADER:
523 return "vertex";
524 case GL_FRAGMENT_SHADER:
525 return "pixel";
526 #ifdef GL_TESS_CONTROL_SHADER
527 case GL_TESS_CONTROL_SHADER:
528 return "tessellation control";
529 #endif
530 #ifdef GL_TESS_EVALUATION_SHADER
531 case GL_TESS_EVALUATION_SHADER:
532 return "tessellation evaluation";
533 #endif
534 #ifdef GL_GEOMETRY_SHADER
535 case GL_GEOMETRY_SHADER:
536 return "geometry";
537 #endif
539 default:
540 break;
541 }
542 return "<unknown>";
543 }
545 static int sdrtypeidx(unsigned int sdrtype)
546 {
547 switch(sdrtype) {
548 case GL_VERTEX_SHADER:
549 return 0;
550 case GL_FRAGMENT_SHADER:
551 return 1;
552 case GL_TESS_CONTROL_SHADER:
553 return 2;
554 case GL_TESS_EVALUATION_SHADER:
555 return 3;
556 case GL_GEOMETRY_SHADER:
557 return 4;
558 default:
559 break;
560 }
561 return 0;
562 }