nuclear@3: #include nuclear@3: #include nuclear@3: #include nuclear@3: #include nuclear@3: #include nuclear@3: #include nuclear@3: #include nuclear@3: nuclear@3: #if defined(unix) || defined(__unix__) nuclear@3: #include nuclear@3: #include nuclear@3: #endif /* unix */ nuclear@3: nuclear@3: #include "sdr.h" nuclear@3: nuclear@3: static const char *sdrtypestr(unsigned int sdrtype); nuclear@3: nuclear@3: unsigned int create_vertex_shader(const char *src) nuclear@3: { nuclear@3: return create_shader(src, GL_VERTEX_SHADER); nuclear@3: } nuclear@3: nuclear@3: unsigned int create_pixel_shader(const char *src) nuclear@3: { nuclear@3: return create_shader(src, GL_FRAGMENT_SHADER); nuclear@3: } nuclear@3: nuclear@3: unsigned int create_tessctl_shader(const char *src) nuclear@3: { nuclear@3: return create_shader(src, GL_TESS_CONTROL_SHADER); nuclear@3: } nuclear@3: nuclear@3: unsigned int create_tesseval_shader(const char *src) nuclear@3: { nuclear@3: return create_shader(src, GL_TESS_EVALUATION_SHADER); nuclear@3: } nuclear@3: nuclear@3: unsigned int create_geometry_shader(const char *src) nuclear@3: { nuclear@3: return create_shader(src, GL_GEOMETRY_SHADER); nuclear@3: } nuclear@3: nuclear@3: unsigned int create_shader(const char *src, unsigned int sdr_type) nuclear@3: { nuclear@3: unsigned int sdr; nuclear@3: int success, info_len; nuclear@3: char *info_str = 0; nuclear@3: GLenum err; nuclear@3: nuclear@3: sdr = glCreateShader(sdr_type); nuclear@3: assert(glGetError() == GL_NO_ERROR); nuclear@3: glShaderSource(sdr, 1, &src, 0); nuclear@3: err = glGetError(); nuclear@3: assert(err == GL_NO_ERROR); nuclear@3: glCompileShader(sdr); nuclear@3: assert(glGetError() == GL_NO_ERROR); nuclear@3: nuclear@3: glGetShaderiv(sdr, GL_COMPILE_STATUS, &success); nuclear@3: assert(glGetError() == GL_NO_ERROR); nuclear@3: glGetShaderiv(sdr, GL_INFO_LOG_LENGTH, &info_len); nuclear@3: assert(glGetError() == GL_NO_ERROR); nuclear@3: nuclear@3: if(info_len) { nuclear@3: if((info_str = malloc(info_len + 1))) { nuclear@3: glGetShaderInfoLog(sdr, info_len, 0, info_str); nuclear@3: assert(glGetError() == GL_NO_ERROR); nuclear@3: } nuclear@3: } nuclear@3: nuclear@3: if(success) { nuclear@3: fprintf(stderr, info_str ? "done: %s\n" : "done\n", info_str); nuclear@3: } else { nuclear@3: fprintf(stderr, info_str ? "failed: %s\n" : "failed\n", info_str); nuclear@3: glDeleteShader(sdr); nuclear@3: sdr = 0; nuclear@3: } nuclear@3: nuclear@3: free(info_str); nuclear@3: return sdr; nuclear@3: } nuclear@3: nuclear@3: void free_shader(unsigned int sdr) nuclear@3: { nuclear@3: glDeleteShader(sdr); nuclear@3: } nuclear@3: nuclear@3: unsigned int load_vertex_shader(const char *fname) nuclear@3: { nuclear@3: return load_shader(fname, GL_VERTEX_SHADER); nuclear@3: } nuclear@3: nuclear@3: unsigned int load_pixel_shader(const char *fname) nuclear@3: { nuclear@3: return load_shader(fname, GL_FRAGMENT_SHADER); nuclear@3: } nuclear@3: nuclear@3: unsigned int load_tessctl_shader(const char *fname) nuclear@3: { nuclear@3: return load_shader(fname, GL_TESS_CONTROL_SHADER); nuclear@3: } nuclear@3: nuclear@3: unsigned int load_tesseval_shader(const char *fname) nuclear@3: { nuclear@3: return load_shader(fname, GL_TESS_EVALUATION_SHADER); nuclear@3: } nuclear@3: nuclear@3: unsigned int load_geometry_shader(const char *fname) nuclear@3: { nuclear@3: return load_shader(fname, GL_GEOMETRY_SHADER); nuclear@3: } nuclear@3: nuclear@3: unsigned int load_shader(const char *fname, unsigned int sdr_type) nuclear@3: { nuclear@3: #if defined(unix) || defined(__unix__) nuclear@3: struct stat st; nuclear@3: #endif nuclear@3: unsigned int sdr; nuclear@3: size_t filesize; nuclear@3: FILE *fp; nuclear@3: char *src; nuclear@3: nuclear@3: if(!(fp = fopen(fname, "r"))) { nuclear@3: fprintf(stderr, "failed to open shader %s: %s\n", fname, strerror(errno)); nuclear@3: return 0; nuclear@3: } nuclear@3: nuclear@3: #if defined(unix) || defined(__unix__) nuclear@3: fstat(fileno(fp), &st); nuclear@3: filesize = st.st_size; nuclear@3: #else nuclear@3: fseek(fp, 0, SEEK_END); nuclear@3: filesize = ftell(fp); nuclear@3: fseek(fp, 0, SEEK_SET); nuclear@3: #endif /* unix */ nuclear@3: nuclear@3: if(!(src = malloc(filesize + 1))) { nuclear@3: fclose(fp); nuclear@3: return 0; nuclear@3: } nuclear@3: fread(src, 1, filesize, fp); nuclear@3: src[filesize] = 0; nuclear@3: fclose(fp); nuclear@3: nuclear@3: fprintf(stderr, "compiling %s shader: %s... ", sdrtypestr(sdr_type), fname); nuclear@3: sdr = create_shader(src, sdr_type); nuclear@3: nuclear@3: free(src); nuclear@3: return sdr; nuclear@3: } nuclear@3: nuclear@3: nuclear@3: unsigned int get_vertex_shader(const char *fname) nuclear@3: { nuclear@3: return get_shader(fname, GL_VERTEX_SHADER); nuclear@3: } nuclear@3: nuclear@3: unsigned int get_pixel_shader(const char *fname) nuclear@3: { nuclear@3: return get_shader(fname, GL_FRAGMENT_SHADER); nuclear@3: } nuclear@3: nuclear@3: unsigned int get_tessctl_shader(const char *fname) nuclear@3: { nuclear@3: return get_shader(fname, GL_TESS_CONTROL_SHADER); nuclear@3: } nuclear@3: nuclear@3: unsigned int get_tesseval_shader(const char *fname) nuclear@3: { nuclear@3: return get_shader(fname, GL_TESS_EVALUATION_SHADER); nuclear@3: } nuclear@3: nuclear@3: unsigned int get_geometry_shader(const char *fname) nuclear@3: { nuclear@3: return get_shader(fname, GL_GEOMETRY_SHADER); nuclear@3: } nuclear@3: nuclear@3: unsigned int get_shader(const char *fname, unsigned int sdr_type) nuclear@3: { nuclear@3: unsigned int sdr; nuclear@3: if(!(sdr = load_shader(fname, sdr_type))) { nuclear@3: return 0; nuclear@3: } nuclear@3: return sdr; nuclear@3: } nuclear@3: nuclear@3: nuclear@3: /* ---- gpu programs ---- */ nuclear@3: nuclear@3: unsigned int create_program(void) nuclear@3: { nuclear@3: unsigned int prog = glCreateProgram(); nuclear@3: assert(glGetError() == GL_NO_ERROR); nuclear@3: return prog; nuclear@3: } nuclear@3: nuclear@3: unsigned int create_program_link(unsigned int sdr0, ...) nuclear@3: { nuclear@3: unsigned int prog, sdr; nuclear@3: va_list ap; nuclear@3: nuclear@3: if(!(prog = create_program())) { nuclear@3: return 0; nuclear@3: } nuclear@3: nuclear@3: attach_shader(prog, sdr0); nuclear@3: if(glGetError()) { nuclear@3: return 0; nuclear@3: } nuclear@3: nuclear@3: va_start(ap, sdr0); nuclear@3: while((sdr = va_arg(ap, unsigned int))) { nuclear@3: attach_shader(prog, sdr); nuclear@3: if(glGetError()) { nuclear@3: return 0; nuclear@3: } nuclear@3: } nuclear@3: va_end(ap); nuclear@3: nuclear@3: if(link_program(prog) == -1) { nuclear@3: free_program(prog); nuclear@3: return 0; nuclear@3: } nuclear@3: return prog; nuclear@3: } nuclear@3: nuclear@3: unsigned int create_program_load(const char *vfile, const char *pfile) nuclear@3: { nuclear@3: unsigned int vs = 0, ps = 0; nuclear@3: nuclear@3: if(vfile && *vfile && !(vs = get_vertex_shader(vfile))) { nuclear@3: return 0; nuclear@3: } nuclear@3: if(pfile && *pfile && !(ps = get_pixel_shader(pfile))) { nuclear@3: return 0; nuclear@3: } nuclear@3: return create_program_link(vs, ps, 0); nuclear@3: } nuclear@3: nuclear@3: void free_program(unsigned int sdr) nuclear@3: { nuclear@3: glDeleteProgram(sdr); nuclear@3: } nuclear@3: nuclear@3: void attach_shader(unsigned int prog, unsigned int sdr) nuclear@3: { nuclear@3: glAttachShader(prog, sdr); nuclear@3: assert(glGetError() == GL_NO_ERROR); nuclear@3: } nuclear@3: nuclear@3: int link_program(unsigned int prog) nuclear@3: { nuclear@3: int linked, info_len, retval = 0; nuclear@3: char *info_str = 0; nuclear@3: nuclear@3: glLinkProgram(prog); nuclear@3: assert(glGetError() == GL_NO_ERROR); nuclear@3: glGetProgramiv(prog, GL_LINK_STATUS, &linked); nuclear@3: assert(glGetError() == GL_NO_ERROR); nuclear@3: glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &info_len); nuclear@3: assert(glGetError() == GL_NO_ERROR); nuclear@3: nuclear@3: if(info_len) { nuclear@3: if((info_str = malloc(info_len + 1))) { nuclear@3: glGetProgramInfoLog(prog, info_len, 0, info_str); nuclear@3: assert(glGetError() == GL_NO_ERROR); nuclear@3: } nuclear@3: } nuclear@3: nuclear@3: if(linked) { nuclear@3: fprintf(stderr, info_str ? "linking done: %s\n" : "linking done\n", info_str); nuclear@3: } else { nuclear@3: fprintf(stderr, info_str ? "linking failed: %s\n" : "linking failed\n", info_str); nuclear@3: retval = -1; nuclear@3: } nuclear@3: nuclear@3: free(info_str); nuclear@3: return retval; nuclear@3: } nuclear@3: nuclear@3: int bind_program(unsigned int prog) nuclear@3: { nuclear@3: GLenum err; nuclear@3: nuclear@3: glUseProgram(prog); nuclear@3: if(prog && (err = glGetError()) != GL_NO_ERROR) { nuclear@3: /* maybe the program is not linked, try linking first */ nuclear@3: if(err == GL_INVALID_OPERATION) { nuclear@3: if(link_program(prog) == -1) { nuclear@3: return -1; nuclear@3: } nuclear@3: glUseProgram(prog); nuclear@3: return glGetError() == GL_NO_ERROR ? 0 : -1; nuclear@3: } nuclear@3: return -1; nuclear@3: } nuclear@3: return 0; nuclear@3: } nuclear@3: nuclear@3: /* ugly but I'm not going to write the same bloody code over and over */ nuclear@3: #define BEGIN_UNIFORM_CODE \ nuclear@3: int loc, curr_prog; \ nuclear@3: glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog); \ nuclear@3: if((unsigned int)curr_prog != prog && bind_program(prog) == -1) { \ nuclear@3: return -1; \ nuclear@3: } \ nuclear@3: if((loc = glGetUniformLocation(prog, name)) != -1) nuclear@3: nuclear@3: #define END_UNIFORM_CODE \ nuclear@3: if((unsigned int)curr_prog != prog) { \ nuclear@3: bind_program(curr_prog); \ nuclear@3: } \ nuclear@3: return loc == -1 ? -1 : 0 nuclear@3: nuclear@3: int set_uniform_int(unsigned int prog, const char *name, int val) nuclear@3: { nuclear@3: BEGIN_UNIFORM_CODE { nuclear@3: glUniform1i(loc, val); nuclear@3: } nuclear@3: END_UNIFORM_CODE; nuclear@3: } nuclear@3: nuclear@3: int set_uniform_float(unsigned int prog, const char *name, float val) nuclear@3: { nuclear@3: BEGIN_UNIFORM_CODE { nuclear@3: glUniform1f(loc, val); nuclear@3: } nuclear@3: END_UNIFORM_CODE; nuclear@3: } nuclear@3: nuclear@3: int set_uniform_float2(unsigned int prog, const char *name, float x, float y) nuclear@3: { nuclear@3: BEGIN_UNIFORM_CODE { nuclear@3: glUniform2f(loc, x, y); nuclear@3: } nuclear@3: END_UNIFORM_CODE; nuclear@3: } nuclear@3: nuclear@3: int set_uniform_float3(unsigned int prog, const char *name, float x, float y, float z) nuclear@3: { nuclear@3: BEGIN_UNIFORM_CODE { nuclear@3: glUniform3f(loc, x, y, z); nuclear@3: } nuclear@3: END_UNIFORM_CODE; nuclear@3: } nuclear@3: nuclear@3: int set_uniform_float4(unsigned int prog, const char *name, float x, float y, float z, float w) nuclear@3: { nuclear@3: BEGIN_UNIFORM_CODE { nuclear@3: glUniform4f(loc, x, y, z, w); nuclear@3: } nuclear@3: END_UNIFORM_CODE; nuclear@3: } nuclear@3: nuclear@3: int set_uniform_matrix4(unsigned int prog, const char *name, float *mat) nuclear@3: { nuclear@3: BEGIN_UNIFORM_CODE { nuclear@3: glUniformMatrix4fv(loc, 1, GL_FALSE, mat); nuclear@3: } nuclear@3: END_UNIFORM_CODE; nuclear@3: } nuclear@3: nuclear@3: int set_uniform_matrix4_transposed(unsigned int prog, const char *name, float *mat) nuclear@3: { nuclear@3: BEGIN_UNIFORM_CODE { nuclear@3: glUniformMatrix4fv(loc, 1, GL_TRUE, mat); nuclear@3: } nuclear@3: END_UNIFORM_CODE; nuclear@3: } nuclear@3: nuclear@3: int get_attrib_loc(unsigned int prog, const char *name) nuclear@3: { nuclear@3: int loc, curr_prog; nuclear@3: nuclear@3: glGetIntegerv(GL_CURRENT_PROGRAM, &curr_prog); nuclear@3: if((unsigned int)curr_prog != prog && bind_program(prog) == -1) { nuclear@3: return -1; nuclear@3: } nuclear@3: nuclear@3: loc = glGetAttribLocation(prog, (char*)name); nuclear@3: nuclear@3: if((unsigned int)curr_prog != prog) { nuclear@3: bind_program(curr_prog); nuclear@3: } nuclear@3: return loc; nuclear@3: } nuclear@3: nuclear@3: void set_attrib_float3(int attr_loc, float x, float y, float z) nuclear@3: { nuclear@3: glVertexAttrib3f(attr_loc, x, y, z); nuclear@3: } nuclear@3: nuclear@3: static const char *sdrtypestr(unsigned int sdrtype) nuclear@3: { nuclear@3: switch(sdrtype) { nuclear@3: case GL_VERTEX_SHADER: nuclear@3: return "vertex"; nuclear@3: case GL_FRAGMENT_SHADER: nuclear@3: return "pixel"; nuclear@3: case GL_TESS_CONTROL_SHADER: nuclear@3: return "tessellation control"; nuclear@3: case GL_TESS_EVALUATION_SHADER: nuclear@3: return "tessellation evaluation"; nuclear@3: case GL_GEOMETRY_SHADER: nuclear@3: return "geometry"; nuclear@3: nuclear@3: default: nuclear@3: break; nuclear@3: } nuclear@3: return ""; nuclear@3: }