9 #if defined(unix) || defined(__unix__)
16 static const char *sdrtypestr(unsigned int sdrtype);
17 static int sdrtypeidx(unsigned int sdrtype);
20 unsigned int create_vertex_shader(const char *src)
22 return create_shader(src, GL_VERTEX_SHADER);
25 unsigned int create_pixel_shader(const char *src)
27 return create_shader(src, GL_FRAGMENT_SHADER);
30 unsigned int create_tessctl_shader(const char *src)
32 #ifdef GL_TESS_CONTROL_SHADER
33 return create_shader(src, GL_TESS_CONTROL_SHADER);
39 unsigned int create_tesseval_shader(const char *src)
41 #ifdef GL_TESS_EVALUATION_SHADER
42 return create_shader(src, GL_TESS_EVALUATION_SHADER);
48 unsigned int create_geometry_shader(const char *src)
50 #ifdef GL_GEOMETRY_SHADER
51 return create_shader(src, GL_GEOMETRY_SHADER);
57 unsigned int create_shader(const char *src, unsigned int sdr_type)
60 int success, info_len;
62 const char *src_str[3], *header, *footer;
63 int src_str_count = 0;
66 if((header = get_shader_header(sdr_type))) {
67 src_str[src_str_count++] = header;
69 src_str[src_str_count++] = src;
70 if((footer = get_shader_footer(sdr_type))) {
71 src_str[src_str_count++] = footer;
74 sdr = glCreateShader(sdr_type);
75 assert(glGetError() == GL_NO_ERROR);
76 glShaderSource(sdr, src_str_count, src_str, 0);
78 assert(err == GL_NO_ERROR);
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);
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;
96 fprintf(stderr, info_str ? "done: %s\n" : "done\n", info_str);
98 fprintf(stderr, info_str ? "failed: %s\n" : "failed\n", info_str);
107 void free_shader(unsigned int sdr)
112 unsigned int load_vertex_shader(const char *fname)
114 return load_shader(fname, GL_VERTEX_SHADER);
117 unsigned int load_pixel_shader(const char *fname)
119 return load_shader(fname, GL_FRAGMENT_SHADER);
122 unsigned int load_tessctl_shader(const char *fname)
124 #ifdef GL_TESS_CONTROL_SHADER
125 return load_shader(fname, GL_TESS_CONTROL_SHADER);
131 unsigned int load_tesseval_shader(const char *fname)
133 #ifdef GL_TESS_EVALUATION_SHADER
134 return load_shader(fname, GL_TESS_EVALUATION_SHADER);
140 unsigned int load_geometry_shader(const char *fname)
142 #ifdef GL_GEOMETRY_SHADER
143 return load_shader(fname, GL_GEOMETRY_SHADER);
149 unsigned int load_shader(const char *fname, unsigned int sdr_type)
156 if(!(fp = fopen(fname, "rb"))) {
157 fprintf(stderr, "failed to open shader %s: %s\n", fname, strerror(errno));
161 fseek(fp, 0, SEEK_END);
162 filesize = ftell(fp);
163 fseek(fp, 0, SEEK_SET);
165 if(!(src = malloc(filesize + 1))) {
169 fread(src, 1, filesize, fp);
173 fprintf(stderr, "compiling %s shader: %s... ", sdrtypestr(sdr_type), fname);
174 sdr = create_shader(src, sdr_type);
181 /* ---- gpu programs ---- */
183 unsigned int create_program(void)
185 unsigned int prog = glCreateProgram();
186 assert(glGetError() == GL_NO_ERROR);
190 unsigned int create_program_link(unsigned int sdr0, ...)
192 unsigned int prog, sdr;
195 if(!(prog = create_program())) {
199 attach_shader(prog, sdr0);
205 while((sdr = va_arg(ap, unsigned int))) {
206 attach_shader(prog, sdr);
213 if(link_program(prog) == -1) {
220 unsigned int create_program_load(const char *vfile, const char *pfile)
222 unsigned int vs = 0, ps = 0;
224 if(vfile && *vfile && !(vs = load_vertex_shader(vfile))) {
227 if(pfile && *pfile && !(ps = load_pixel_shader(pfile))) {
230 return create_program_link(vs, ps, 0);
233 void free_program(unsigned int sdr)
235 glDeleteProgram(sdr);
238 void attach_shader(unsigned int prog, unsigned int 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);
252 int link_program(unsigned int prog)
254 int linked, info_len, retval = 0;
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);
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;
273 fprintf(stderr, info_str ? "linking done: %s\n" : "linking done\n", info_str);
275 fprintf(stderr, info_str ? "linking failed: %s\n" : "linking failed\n", info_str);
283 int bind_program(unsigned int 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) {
295 return glGetError() == GL_NO_ERROR ? 0 : -1;
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) { \
309 if((loc = glGetUniformLocation(prog, name)) != -1)
311 #define END_UNIFORM_CODE \
312 if((unsigned int)curr_prog != prog) { \
313 bind_program(curr_prog); \
315 return loc == -1 ? -1 : 0
317 int get_uniform_loc(unsigned int prog, const char *name)
320 glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog);
321 if((unsigned int)curr_prog != prog && bind_program(prog) == -1) {
324 loc = glGetUniformLocation(prog, name);
325 if((unsigned int)curr_prog != prog) {
326 bind_program(curr_prog);
331 int set_uniform_int(unsigned int prog, const char *name, int val)
334 glUniform1i(loc, val);
339 int set_uniform_float(unsigned int prog, const char *name, float val)
342 glUniform1f(loc, val);
347 int set_uniform_float2(unsigned int prog, const char *name, float x, float y)
350 glUniform2f(loc, x, y);
355 int set_uniform_float3(unsigned int prog, const char *name, float x, float y, float z)
358 glUniform3f(loc, x, y, z);
363 int set_uniform_float4(unsigned int prog, const char *name, float x, float y, float z, float w)
366 glUniform4f(loc, x, y, z, w);
371 int set_uniform_matrix4(unsigned int prog, const char *name, const float *mat)
374 glUniformMatrix4fv(loc, 1, GL_FALSE, mat);
379 int set_uniform_matrix4_transposed(unsigned int prog, const char *name, const float *mat)
382 glUniformMatrix4fv(loc, 1, GL_TRUE, mat);
387 int get_attrib_loc(unsigned int prog, const char *name)
391 glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog);
392 if((unsigned int)curr_prog != prog && bind_program(prog) == -1) {
396 loc = glGetAttribLocation(prog, (char*)name);
398 if((unsigned int)curr_prog != prog) {
399 bind_program(curr_prog);
404 void set_attrib_float3(int attr_loc, float x, float y, float z)
406 glVertexAttrib3f(attr_loc, x, y, z);
409 /* ---- shader composition ---- */
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)
426 static void append_string(struct string *str, const char *s)
431 if(!s || !*s) return;
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);
441 memcpy(newstr, str->text, str->len);
443 memcpy(newstr + str->len, s, len + 1);
445 if(s[len - 1] != '\n') {
446 newstr[newlen] = '\n';
447 newstr[newlen + 1] = 0;
455 void clear_shader_header(unsigned int type)
458 int idx = sdrtypeidx(type);
459 clear_string(&header[idx]);
462 for(i=0; i<NUM_SHADER_TYPES; i++) {
463 clear_string(&header[i]);
468 void clear_shader_footer(unsigned int type)
471 int idx = sdrtypeidx(type);
472 clear_string(&footer[idx]);
475 for(i=0; i<NUM_SHADER_TYPES; i++) {
476 clear_string(&footer[i]);
481 void add_shader_header(unsigned int type, const char *s)
484 int idx = sdrtypeidx(type);
485 append_string(&header[idx], s);
488 for(i=0; i<NUM_SHADER_TYPES; i++) {
489 append_string(&header[i], s);
494 void add_shader_footer(unsigned int type, const char *s)
497 int idx = sdrtypeidx(type);
498 append_string(&footer[idx], s);
501 for(i=0; i<NUM_SHADER_TYPES; i++) {
502 append_string(&footer[i], s);
507 const char *get_shader_header(unsigned int type)
509 int idx = sdrtypeidx(type);
510 return header[idx].text;
513 const char *get_shader_footer(unsigned int type)
515 int idx = sdrtypeidx(type);
516 return footer[idx].text;
519 static const char *sdrtypestr(unsigned int sdrtype)
522 case GL_VERTEX_SHADER:
524 case GL_FRAGMENT_SHADER:
526 #ifdef GL_TESS_CONTROL_SHADER
527 case GL_TESS_CONTROL_SHADER:
528 return "tessellation control";
530 #ifdef GL_TESS_EVALUATION_SHADER
531 case GL_TESS_EVALUATION_SHADER:
532 return "tessellation evaluation";
534 #ifdef GL_GEOMETRY_SHADER
535 case GL_GEOMETRY_SHADER:
545 static int sdrtypeidx(unsigned int sdrtype)
548 case GL_VERTEX_SHADER:
550 case GL_FRAGMENT_SHADER:
552 case GL_TESS_CONTROL_SHADER:
554 case GL_TESS_EVALUATION_SHADER:
556 case GL_GEOMETRY_SHADER: