nuclear@0: #include nuclear@0: #include nuclear@0: #include nuclear@0: #include nuclear@0: #include nuclear@0: #include nuclear@0: #include "opengl.h" nuclear@0: nuclear@0: #if defined(unix) || defined(__unix__) nuclear@0: #include nuclear@0: #include nuclear@0: #endif /* unix */ nuclear@0: nuclear@0: #include "sdr.h" nuclear@0: nuclear@0: static const char *sdrtypestr(unsigned int sdrtype); nuclear@0: static int sdrtypeidx(unsigned int sdrtype); nuclear@0: nuclear@0: nuclear@0: unsigned int create_vertex_shader(const char *src) nuclear@0: { nuclear@0: return create_shader(src, GL_VERTEX_SHADER); nuclear@0: } nuclear@0: nuclear@0: unsigned int create_pixel_shader(const char *src) nuclear@0: { nuclear@0: return create_shader(src, GL_FRAGMENT_SHADER); nuclear@0: } nuclear@0: nuclear@0: unsigned int create_tessctl_shader(const char *src) nuclear@0: { nuclear@0: #ifdef GL_TESS_CONTROL_SHADER nuclear@0: return create_shader(src, GL_TESS_CONTROL_SHADER); nuclear@0: #else nuclear@0: return 0; nuclear@0: #endif nuclear@0: } nuclear@0: nuclear@0: unsigned int create_tesseval_shader(const char *src) nuclear@0: { nuclear@0: #ifdef GL_TESS_EVALUATION_SHADER nuclear@0: return create_shader(src, GL_TESS_EVALUATION_SHADER); nuclear@0: #else nuclear@0: return 0; nuclear@0: #endif nuclear@0: } nuclear@0: nuclear@0: unsigned int create_geometry_shader(const char *src) nuclear@0: { nuclear@0: #ifdef GL_GEOMETRY_SHADER nuclear@0: return create_shader(src, GL_GEOMETRY_SHADER); nuclear@0: #else nuclear@0: return 0; nuclear@0: #endif nuclear@0: } nuclear@0: nuclear@0: unsigned int create_shader(const char *src, unsigned int sdr_type) nuclear@0: { nuclear@0: unsigned int sdr; nuclear@0: int success, info_len; nuclear@0: char *info_str = 0; nuclear@0: const char *src_str[3], *header, *footer; nuclear@0: int src_str_count = 0; nuclear@0: GLenum err; nuclear@0: nuclear@0: if((header = get_shader_header(sdr_type))) { nuclear@0: src_str[src_str_count++] = header; nuclear@0: } nuclear@0: src_str[src_str_count++] = src; nuclear@0: if((footer = get_shader_footer(sdr_type))) { nuclear@0: src_str[src_str_count++] = footer; nuclear@0: } nuclear@0: nuclear@0: sdr = glCreateShader(sdr_type); nuclear@0: assert(glGetError() == GL_NO_ERROR); nuclear@0: glShaderSource(sdr, src_str_count, src_str, 0); nuclear@0: err = glGetError(); nuclear@0: assert(err == GL_NO_ERROR); nuclear@0: glCompileShader(sdr); nuclear@0: assert(glGetError() == GL_NO_ERROR); nuclear@0: nuclear@0: glGetShaderiv(sdr, GL_COMPILE_STATUS, &success); nuclear@0: assert(glGetError() == GL_NO_ERROR); nuclear@0: glGetShaderiv(sdr, GL_INFO_LOG_LENGTH, &info_len); nuclear@0: assert(glGetError() == GL_NO_ERROR); nuclear@0: nuclear@0: if(info_len) { nuclear@0: if((info_str = malloc(info_len + 1))) { nuclear@0: glGetShaderInfoLog(sdr, info_len, 0, info_str); nuclear@0: assert(glGetError() == GL_NO_ERROR); nuclear@0: info_str[info_len] = 0; nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: if(success) { nuclear@0: fprintf(stderr, info_str ? "done: %s\n" : "done\n", info_str); nuclear@0: } else { nuclear@0: fprintf(stderr, info_str ? "failed: %s\n" : "failed\n", info_str); nuclear@0: glDeleteShader(sdr); nuclear@0: sdr = 0; nuclear@0: } nuclear@0: nuclear@0: free(info_str); nuclear@0: return sdr; nuclear@0: } nuclear@0: nuclear@0: void free_shader(unsigned int sdr) nuclear@0: { nuclear@0: glDeleteShader(sdr); nuclear@0: } nuclear@0: nuclear@0: unsigned int load_vertex_shader(const char *fname) nuclear@0: { nuclear@0: return load_shader(fname, GL_VERTEX_SHADER); nuclear@0: } nuclear@0: nuclear@0: unsigned int load_pixel_shader(const char *fname) nuclear@0: { nuclear@0: return load_shader(fname, GL_FRAGMENT_SHADER); nuclear@0: } nuclear@0: nuclear@0: unsigned int load_tessctl_shader(const char *fname) nuclear@0: { nuclear@0: #ifdef GL_TESS_CONTROL_SHADER nuclear@0: return load_shader(fname, GL_TESS_CONTROL_SHADER); nuclear@0: #else nuclear@0: return 0; nuclear@0: #endif nuclear@0: } nuclear@0: nuclear@0: unsigned int load_tesseval_shader(const char *fname) nuclear@0: { nuclear@0: #ifdef GL_TESS_EVALUATION_SHADER nuclear@0: return load_shader(fname, GL_TESS_EVALUATION_SHADER); nuclear@0: #else nuclear@0: return 0; nuclear@0: #endif nuclear@0: } nuclear@0: nuclear@0: unsigned int load_geometry_shader(const char *fname) nuclear@0: { nuclear@0: #ifdef GL_GEOMETRY_SHADER nuclear@0: return load_shader(fname, GL_GEOMETRY_SHADER); nuclear@0: #else nuclear@0: return 0; nuclear@0: #endif nuclear@0: } nuclear@0: nuclear@0: unsigned int load_shader(const char *fname, unsigned int sdr_type) nuclear@0: { nuclear@0: unsigned int sdr; nuclear@0: size_t filesize; nuclear@0: FILE *fp; nuclear@0: char *src; nuclear@0: nuclear@0: if(!(fp = fopen(fname, "rb"))) { nuclear@0: fprintf(stderr, "failed to open shader %s: %s\n", fname, strerror(errno)); nuclear@0: return 0; nuclear@0: } nuclear@0: nuclear@0: fseek(fp, 0, SEEK_END); nuclear@0: filesize = ftell(fp); nuclear@0: fseek(fp, 0, SEEK_SET); nuclear@0: nuclear@0: if(!(src = malloc(filesize + 1))) { nuclear@0: fclose(fp); nuclear@0: return 0; nuclear@0: } nuclear@0: fread(src, 1, filesize, fp); nuclear@0: src[filesize] = 0; nuclear@0: fclose(fp); nuclear@0: nuclear@0: fprintf(stderr, "compiling %s shader: %s... ", sdrtypestr(sdr_type), fname); nuclear@0: sdr = create_shader(src, sdr_type); nuclear@0: nuclear@0: free(src); nuclear@0: return sdr; nuclear@0: } nuclear@0: nuclear@0: nuclear@0: /* ---- gpu programs ---- */ nuclear@0: nuclear@0: unsigned int create_program(void) nuclear@0: { nuclear@0: unsigned int prog = glCreateProgram(); nuclear@0: assert(glGetError() == GL_NO_ERROR); nuclear@0: return prog; nuclear@0: } nuclear@0: nuclear@0: unsigned int create_program_link(unsigned int sdr0, ...) nuclear@0: { nuclear@0: unsigned int prog, sdr; nuclear@0: va_list ap; nuclear@0: nuclear@0: if(!(prog = create_program())) { nuclear@0: return 0; nuclear@0: } nuclear@0: nuclear@0: attach_shader(prog, sdr0); nuclear@0: if(glGetError()) { nuclear@0: return 0; nuclear@0: } nuclear@0: nuclear@0: va_start(ap, sdr0); nuclear@0: while((sdr = va_arg(ap, unsigned int))) { nuclear@0: attach_shader(prog, sdr); nuclear@0: if(glGetError()) { nuclear@0: return 0; nuclear@0: } nuclear@0: } nuclear@0: va_end(ap); nuclear@0: nuclear@0: if(link_program(prog) == -1) { nuclear@0: free_program(prog); nuclear@0: return 0; nuclear@0: } nuclear@0: return prog; nuclear@0: } nuclear@0: nuclear@0: unsigned int create_program_load(const char *vfile, const char *pfile) nuclear@0: { nuclear@0: unsigned int vs = 0, ps = 0; nuclear@0: nuclear@0: if(vfile && *vfile && !(vs = load_vertex_shader(vfile))) { nuclear@0: return 0; nuclear@0: } nuclear@0: if(pfile && *pfile && !(ps = load_pixel_shader(pfile))) { nuclear@0: return 0; nuclear@0: } nuclear@0: return create_program_link(vs, ps, 0); nuclear@0: } nuclear@0: nuclear@0: void free_program(unsigned int sdr) nuclear@0: { nuclear@0: glDeleteProgram(sdr); nuclear@0: } nuclear@0: nuclear@0: void attach_shader(unsigned int prog, unsigned int sdr) nuclear@0: { nuclear@0: int err; nuclear@0: nuclear@0: if(prog && sdr) { nuclear@0: assert(glGetError() == GL_NO_ERROR); nuclear@0: glAttachShader(prog, sdr); nuclear@0: if((err = glGetError()) != GL_NO_ERROR) { nuclear@0: fprintf(stderr, "failed to attach shader %u to program %u (err: 0x%x)\n", sdr, prog, err); nuclear@0: abort(); nuclear@0: } nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: int link_program(unsigned int prog) nuclear@0: { nuclear@0: int linked, info_len, retval = 0; nuclear@0: char *info_str = 0; nuclear@0: nuclear@0: glLinkProgram(prog); nuclear@0: assert(glGetError() == GL_NO_ERROR); nuclear@0: glGetProgramiv(prog, GL_LINK_STATUS, &linked); nuclear@0: assert(glGetError() == GL_NO_ERROR); nuclear@0: glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &info_len); nuclear@0: assert(glGetError() == GL_NO_ERROR); nuclear@0: nuclear@0: if(info_len) { nuclear@0: if((info_str = malloc(info_len + 1))) { nuclear@0: glGetProgramInfoLog(prog, info_len, 0, info_str); nuclear@0: assert(glGetError() == GL_NO_ERROR); nuclear@0: info_str[info_len] = 0; nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: if(linked) { nuclear@0: fprintf(stderr, info_str ? "linking done: %s\n" : "linking done\n", info_str); nuclear@0: } else { nuclear@0: fprintf(stderr, info_str ? "linking failed: %s\n" : "linking failed\n", info_str); nuclear@0: retval = -1; nuclear@0: } nuclear@0: nuclear@0: free(info_str); nuclear@0: return retval; nuclear@0: } nuclear@0: nuclear@0: int bind_program(unsigned int prog) nuclear@0: { nuclear@0: GLenum err; nuclear@0: nuclear@0: glUseProgram(prog); nuclear@0: if(prog && (err = glGetError()) != GL_NO_ERROR) { nuclear@0: /* maybe the program is not linked, try linking first */ nuclear@0: if(err == GL_INVALID_OPERATION) { nuclear@0: if(link_program(prog) == -1) { nuclear@0: return -1; nuclear@0: } nuclear@0: glUseProgram(prog); nuclear@0: return glGetError() == GL_NO_ERROR ? 0 : -1; nuclear@0: } nuclear@0: return -1; nuclear@0: } nuclear@0: return 0; nuclear@0: } nuclear@0: nuclear@0: /* ugly but I'm not going to write the same bloody code over and over */ nuclear@0: #define BEGIN_UNIFORM_CODE \ nuclear@0: int loc, curr_prog; \ nuclear@0: glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog); \ nuclear@0: if((unsigned int)curr_prog != prog && bind_program(prog) == -1) { \ nuclear@0: return -1; \ nuclear@0: } \ nuclear@0: if((loc = glGetUniformLocation(prog, name)) != -1) nuclear@0: nuclear@0: #define END_UNIFORM_CODE \ nuclear@0: if((unsigned int)curr_prog != prog) { \ nuclear@0: bind_program(curr_prog); \ nuclear@0: } \ nuclear@0: return loc == -1 ? -1 : 0 nuclear@0: nuclear@0: int get_uniform_loc(unsigned int prog, const char *name) nuclear@0: { nuclear@0: int loc, curr_prog; nuclear@0: glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog); nuclear@0: if((unsigned int)curr_prog != prog && bind_program(prog) == -1) { nuclear@0: return -1; nuclear@0: } nuclear@0: loc = glGetUniformLocation(prog, name); nuclear@0: if((unsigned int)curr_prog != prog) { nuclear@0: bind_program(curr_prog); nuclear@0: } nuclear@0: return loc; nuclear@0: } nuclear@0: nuclear@0: int set_uniform_int(unsigned int prog, const char *name, int val) nuclear@0: { nuclear@0: BEGIN_UNIFORM_CODE { nuclear@0: glUniform1i(loc, val); nuclear@0: } nuclear@0: END_UNIFORM_CODE; nuclear@0: } nuclear@0: nuclear@0: int set_uniform_float(unsigned int prog, const char *name, float val) nuclear@0: { nuclear@0: BEGIN_UNIFORM_CODE { nuclear@0: glUniform1f(loc, val); nuclear@0: } nuclear@0: END_UNIFORM_CODE; nuclear@0: } nuclear@0: nuclear@0: int set_uniform_float2(unsigned int prog, const char *name, float x, float y) nuclear@0: { nuclear@0: BEGIN_UNIFORM_CODE { nuclear@0: glUniform2f(loc, x, y); nuclear@0: } nuclear@0: END_UNIFORM_CODE; nuclear@0: } nuclear@0: nuclear@0: int set_uniform_float3(unsigned int prog, const char *name, float x, float y, float z) nuclear@0: { nuclear@0: BEGIN_UNIFORM_CODE { nuclear@0: glUniform3f(loc, x, y, z); nuclear@0: } nuclear@0: END_UNIFORM_CODE; nuclear@0: } nuclear@0: nuclear@0: int set_uniform_float4(unsigned int prog, const char *name, float x, float y, float z, float w) nuclear@0: { nuclear@0: BEGIN_UNIFORM_CODE { nuclear@0: glUniform4f(loc, x, y, z, w); nuclear@0: } nuclear@0: END_UNIFORM_CODE; nuclear@0: } nuclear@0: nuclear@0: int set_uniform_matrix4(unsigned int prog, const char *name, const float *mat) nuclear@0: { nuclear@0: BEGIN_UNIFORM_CODE { nuclear@0: glUniformMatrix4fv(loc, 1, GL_FALSE, mat); nuclear@0: } nuclear@0: END_UNIFORM_CODE; nuclear@0: } nuclear@0: nuclear@0: int set_uniform_matrix4_transposed(unsigned int prog, const char *name, const float *mat) nuclear@0: { nuclear@0: BEGIN_UNIFORM_CODE { nuclear@0: glUniformMatrix4fv(loc, 1, GL_TRUE, mat); nuclear@0: } nuclear@0: END_UNIFORM_CODE; nuclear@0: } nuclear@0: nuclear@0: int get_attrib_loc(unsigned int prog, const char *name) nuclear@0: { nuclear@0: int loc, curr_prog; nuclear@0: nuclear@0: glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog); nuclear@0: if((unsigned int)curr_prog != prog && bind_program(prog) == -1) { nuclear@0: return -1; nuclear@0: } nuclear@0: nuclear@0: loc = glGetAttribLocation(prog, (char*)name); nuclear@0: nuclear@0: if((unsigned int)curr_prog != prog) { nuclear@0: bind_program(curr_prog); nuclear@0: } nuclear@0: return loc; nuclear@0: } nuclear@0: nuclear@0: void set_attrib_float3(int attr_loc, float x, float y, float z) nuclear@0: { nuclear@0: glVertexAttrib3f(attr_loc, x, y, z); nuclear@0: } nuclear@0: nuclear@0: /* ---- shader composition ---- */ nuclear@0: struct string { nuclear@0: char *text; nuclear@0: int len; nuclear@0: }; nuclear@0: nuclear@0: #define NUM_SHADER_TYPES 5 nuclear@0: static struct string header[NUM_SHADER_TYPES]; nuclear@0: static struct string footer[NUM_SHADER_TYPES]; nuclear@0: nuclear@0: static void clear_string(struct string *str) nuclear@0: { nuclear@0: free(str->text); nuclear@0: str->text = 0; nuclear@0: str->len = 0; nuclear@0: } nuclear@0: nuclear@0: static void append_string(struct string *str, const char *s) nuclear@0: { nuclear@0: int len, newlen; nuclear@0: char *newstr; nuclear@0: nuclear@0: if(!s || !*s) return; nuclear@0: nuclear@0: len = strlen(s); nuclear@0: newlen = str->len + len; nuclear@0: if(!(newstr = malloc(newlen + 2))) { /* leave space for a possible newline */ nuclear@0: fprintf(stderr, "shader composition: failed to append string of size %d\n", len); nuclear@0: abort(); nuclear@0: } nuclear@0: nuclear@0: if(str->text) { nuclear@0: memcpy(newstr, str->text, str->len); nuclear@0: } nuclear@0: memcpy(newstr + str->len, s, len + 1); nuclear@0: nuclear@0: if(s[len - 1] != '\n') { nuclear@0: newstr[newlen] = '\n'; nuclear@0: newstr[newlen + 1] = 0; nuclear@0: } nuclear@0: nuclear@0: free(str->text); nuclear@0: str->text = newstr; nuclear@0: str->len = newlen; nuclear@0: } nuclear@0: nuclear@0: void clear_shader_header(unsigned int type) nuclear@0: { nuclear@0: if(type) { nuclear@0: int idx = sdrtypeidx(type); nuclear@0: clear_string(&header[idx]); nuclear@0: } else { nuclear@0: int i; nuclear@0: for(i=0; i"; nuclear@0: } nuclear@0: nuclear@0: static int sdrtypeidx(unsigned int sdrtype) nuclear@0: { nuclear@0: switch(sdrtype) { nuclear@0: case GL_VERTEX_SHADER: nuclear@0: return 0; nuclear@0: case GL_FRAGMENT_SHADER: nuclear@0: return 1; nuclear@0: case GL_TESS_CONTROL_SHADER: nuclear@0: return 2; nuclear@0: case GL_TESS_EVALUATION_SHADER: nuclear@0: return 3; nuclear@0: case GL_GEOMETRY_SHADER: nuclear@0: return 4; nuclear@0: default: nuclear@0: break; nuclear@0: } nuclear@0: return 0; nuclear@0: }