10 #if defined(unix) || defined(__unix__)
17 static const char *sdrtypestr(unsigned int sdrtype);
18 static int sdrtypeidx(unsigned int sdrtype);
21 unsigned int create_vertex_shader(const char *src)
23 return create_shader(src, GL_VERTEX_SHADER);
26 unsigned int create_pixel_shader(const char *src)
28 return create_shader(src, GL_FRAGMENT_SHADER);
31 unsigned int create_tessctl_shader(const char *src)
33 #ifdef GL_TESS_CONTROL_SHADER
34 return create_shader(src, GL_TESS_CONTROL_SHADER);
40 unsigned int create_tesseval_shader(const char *src)
42 #ifdef GL_TESS_EVALUATION_SHADER
43 return create_shader(src, GL_TESS_EVALUATION_SHADER);
49 unsigned int create_geometry_shader(const char *src)
51 #ifdef GL_GEOMETRY_SHADER
52 return create_shader(src, GL_GEOMETRY_SHADER);
58 unsigned int create_shader(const char *src, unsigned int sdr_type)
61 int success, info_len;
63 const char *src_str[3], *header, *footer;
64 int src_str_count = 0;
67 if((header = get_shader_header(sdr_type))) {
68 src_str[src_str_count++] = header;
70 src_str[src_str_count++] = src;
71 if((footer = get_shader_footer(sdr_type))) {
72 src_str[src_str_count++] = footer;
75 sdr = glCreateShader(sdr_type);
76 assert(glGetError() == GL_NO_ERROR);
77 glShaderSource(sdr, src_str_count, src_str, 0);
79 assert(err == GL_NO_ERROR);
81 assert(glGetError() == GL_NO_ERROR);
83 glGetShaderiv(sdr, GL_COMPILE_STATUS, &success);
84 assert(glGetError() == GL_NO_ERROR);
85 glGetShaderiv(sdr, GL_INFO_LOG_LENGTH, &info_len);
86 assert(glGetError() == GL_NO_ERROR);
89 if((info_str = malloc(info_len + 1))) {
90 glGetShaderInfoLog(sdr, info_len, 0, info_str);
91 assert(glGetError() == GL_NO_ERROR);
92 info_str[info_len] = 0;
97 info_log(info_str ? "done: %s\n" : "done\n", info_str);
99 error_log(info_str ? "failed: %s\n" : "failed\n", info_str);
108 void free_shader(unsigned int sdr)
113 unsigned int load_vertex_shader(const char *fname)
115 return load_shader(fname, GL_VERTEX_SHADER);
118 unsigned int load_pixel_shader(const char *fname)
120 return load_shader(fname, GL_FRAGMENT_SHADER);
123 unsigned int load_tessctl_shader(const char *fname)
125 #ifdef GL_TESS_CONTROL_SHADER
126 return load_shader(fname, GL_TESS_CONTROL_SHADER);
132 unsigned int load_tesseval_shader(const char *fname)
134 #ifdef GL_TESS_EVALUATION_SHADER
135 return load_shader(fname, GL_TESS_EVALUATION_SHADER);
141 unsigned int load_geometry_shader(const char *fname)
143 #ifdef GL_GEOMETRY_SHADER
144 return load_shader(fname, GL_GEOMETRY_SHADER);
150 unsigned int load_shader(const char *fname, unsigned int sdr_type)
157 if(!(fp = fopen(fname, "rb"))) {
158 error_log("failed to open shader %s: %s\n", fname, strerror(errno));
162 fseek(fp, 0, SEEK_END);
163 filesize = ftell(fp);
164 fseek(fp, 0, SEEK_SET);
166 if(!(src = malloc(filesize + 1))) {
170 fread(src, 1, filesize, fp);
174 info_log("compiling %s shader: %s... ", sdrtypestr(sdr_type), fname);
175 sdr = create_shader(src, sdr_type);
182 /* ---- gpu programs ---- */
184 unsigned int create_program(void)
186 unsigned int prog = glCreateProgram();
187 assert(glGetError() == GL_NO_ERROR);
191 unsigned int create_program_link(unsigned int sdr0, ...)
193 unsigned int prog, sdr;
196 if(!(prog = create_program())) {
200 attach_shader(prog, sdr0);
206 while((sdr = va_arg(ap, unsigned int))) {
207 attach_shader(prog, sdr);
214 if(link_program(prog) == -1) {
221 unsigned int create_program_load(const char *vfile, const char *pfile)
223 unsigned int vs = 0, ps = 0;
225 if(vfile && *vfile && !(vs = load_vertex_shader(vfile))) {
228 if(pfile && *pfile && !(ps = load_pixel_shader(pfile))) {
231 return create_program_link(vs, ps, 0);
234 void free_program(unsigned int sdr)
236 glDeleteProgram(sdr);
239 void attach_shader(unsigned int prog, unsigned int sdr)
244 assert(glGetError() == GL_NO_ERROR);
245 glAttachShader(prog, sdr);
246 if((err = glGetError()) != GL_NO_ERROR) {
247 error_log("failed to attach shader %u to program %u (err: 0x%x)\n", sdr, prog, err);
253 int link_program(unsigned int prog)
255 int linked, info_len, retval = 0;
259 assert(glGetError() == GL_NO_ERROR);
260 glGetProgramiv(prog, GL_LINK_STATUS, &linked);
261 assert(glGetError() == GL_NO_ERROR);
262 glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &info_len);
263 assert(glGetError() == GL_NO_ERROR);
266 if((info_str = malloc(info_len + 1))) {
267 glGetProgramInfoLog(prog, info_len, 0, info_str);
268 assert(glGetError() == GL_NO_ERROR);
269 info_str[info_len] = 0;
274 info_log(info_str ? "linking done: %s\n" : "linking done\n", info_str);
276 error_log(info_str ? "linking failed: %s\n" : "linking failed\n", info_str);
284 int bind_program(unsigned int prog)
289 if(prog && (err = glGetError()) != GL_NO_ERROR) {
290 /* maybe the program is not linked, try linking first */
291 if(err == GL_INVALID_OPERATION) {
292 if(link_program(prog) == -1) {
296 return glGetError() == GL_NO_ERROR ? 0 : -1;
303 /* ugly but I'm not going to write the same bloody code over and over */
304 #define BEGIN_UNIFORM_CODE \
305 int loc, curr_prog; \
306 glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog); \
307 if((unsigned int)curr_prog != prog && bind_program(prog) == -1) { \
310 if((loc = glGetUniformLocation(prog, name)) != -1)
312 #define END_UNIFORM_CODE \
313 if((unsigned int)curr_prog != prog) { \
314 bind_program(curr_prog); \
316 return loc == -1 ? -1 : 0
318 int get_uniform_loc(unsigned int prog, const char *name)
321 glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog);
322 if((unsigned int)curr_prog != prog && bind_program(prog) == -1) {
325 loc = glGetUniformLocation(prog, name);
326 if((unsigned int)curr_prog != prog) {
327 bind_program(curr_prog);
332 int set_uniform_int(unsigned int prog, const char *name, int val)
335 glUniform1i(loc, val);
340 int set_uniform_float(unsigned int prog, const char *name, float val)
343 glUniform1f(loc, val);
348 int set_uniform_float2(unsigned int prog, const char *name, float x, float y)
351 glUniform2f(loc, x, y);
356 int set_uniform_float3(unsigned int prog, const char *name, float x, float y, float z)
359 glUniform3f(loc, x, y, z);
364 int set_uniform_float4(unsigned int prog, const char *name, float x, float y, float z, float w)
367 glUniform4f(loc, x, y, z, w);
372 int set_uniform_matrix4(unsigned int prog, const char *name, const float *mat)
375 glUniformMatrix4fv(loc, 1, GL_FALSE, mat);
380 int set_uniform_matrix4_transposed(unsigned int prog, const char *name, const float *mat)
383 glUniformMatrix4fv(loc, 1, GL_TRUE, mat);
388 int get_attrib_loc(unsigned int prog, const char *name)
392 glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog);
393 if((unsigned int)curr_prog != prog && bind_program(prog) == -1) {
397 loc = glGetAttribLocation(prog, (char*)name);
399 if((unsigned int)curr_prog != prog) {
400 bind_program(curr_prog);
405 void set_attrib_float3(int attr_loc, float x, float y, float z)
407 glVertexAttrib3f(attr_loc, x, y, z);
410 /* ---- shader composition ---- */
416 #define NUM_SHADER_TYPES 5
417 static struct string header[NUM_SHADER_TYPES];
418 static struct string footer[NUM_SHADER_TYPES];
420 static void clear_string(struct string *str)
427 static void append_string(struct string *str, const char *s)
432 if(!s || !*s) return;
435 newlen = str->len + len;
436 if(!(newstr = malloc(newlen + 2))) { /* leave space for a possible newline */
437 error_log("shader composition: failed to append string of size %d\n", len);
442 memcpy(newstr, str->text, str->len);
444 memcpy(newstr + str->len, s, len + 1);
446 if(s[len - 1] != '\n') {
447 newstr[newlen] = '\n';
448 newstr[newlen + 1] = 0;
456 void clear_shader_header(unsigned int type)
459 int idx = sdrtypeidx(type);
460 clear_string(&header[idx]);
463 for(i=0; i<NUM_SHADER_TYPES; i++) {
464 clear_string(&header[i]);
469 void clear_shader_footer(unsigned int type)
472 int idx = sdrtypeidx(type);
473 clear_string(&footer[idx]);
476 for(i=0; i<NUM_SHADER_TYPES; i++) {
477 clear_string(&footer[i]);
482 void add_shader_header(unsigned int type, const char *s)
485 int idx = sdrtypeidx(type);
486 append_string(&header[idx], s);
489 for(i=0; i<NUM_SHADER_TYPES; i++) {
490 append_string(&header[i], s);
495 void add_shader_footer(unsigned int type, const char *s)
498 int idx = sdrtypeidx(type);
499 append_string(&footer[idx], s);
502 for(i=0; i<NUM_SHADER_TYPES; i++) {
503 append_string(&footer[i], s);
508 const char *get_shader_header(unsigned int type)
510 int idx = sdrtypeidx(type);
511 return header[idx].text;
514 const char *get_shader_footer(unsigned int type)
516 int idx = sdrtypeidx(type);
517 return footer[idx].text;
520 static const char *sdrtypestr(unsigned int sdrtype)
523 case GL_VERTEX_SHADER:
525 case GL_FRAGMENT_SHADER:
527 #ifdef GL_TESS_CONTROL_SHADER
528 case GL_TESS_CONTROL_SHADER:
529 return "tessellation control";
531 #ifdef GL_TESS_EVALUATION_SHADER
532 case GL_TESS_EVALUATION_SHADER:
533 return "tessellation evaluation";
535 #ifdef GL_GEOMETRY_SHADER
536 case GL_GEOMETRY_SHADER:
546 static int sdrtypeidx(unsigned int sdrtype)
549 case GL_VERTEX_SHADER:
551 case GL_FRAGMENT_SHADER:
553 case GL_TESS_CONTROL_SHADER:
555 case GL_TESS_EVALUATION_SHADER:
557 case GL_GEOMETRY_SHADER: