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