# HG changeset patch # User John Tsiombikas # Date 1433987793 -10800 # Node ID 4ca4e3c5a754f9e4c4fbda799f2755ea449b41b5 # Parent c14613d27a3a10c256e30939a8ea503e069e9956 port to C++ completed, shader programs now use the SdrProg class diff -r c14613d27a3a -r 4ca4e3c5a754 sdr/android_cam_preview.p.glsl --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sdr/android_cam_preview.p.glsl Thu Jun 11 04:56:33 2015 +0300 @@ -0,0 +1,12 @@ +#extension GL_OES_EGL_image_external : require +precision mediump float; + +uniform samplerExternalOES tex; + +varying vec4 tex_coords; + +void main() +{ + vec4 texel = texture2D(tex, tex_coords.xy); + gl_FragColor = vec4(texel.xyz, 1.0); +} diff -r c14613d27a3a -r 4ca4e3c5a754 sdr/tex.p.glsl --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sdr/tex.p.glsl Thu Jun 11 04:56:33 2015 +0300 @@ -0,0 +1,11 @@ +precision mediump float; + +uniform sampler2D tex; + +varying vec4 tex_coords; + +void main() +{ + vec4 texel = texture2D(tex, tex_coords.xy); + gl_FragColor = texel; +} diff -r c14613d27a3a -r 4ca4e3c5a754 sdr/vertex.glsl --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sdr/vertex.glsl Thu Jun 11 04:56:33 2015 +0300 @@ -0,0 +1,12 @@ +attribute vec4 attr_vertex, attr_texcoord; + +uniform mat4 matrix_modelview, matrix_projection, matrix_texture; + +varying vec4 tex_coords; + +void main() +{ + mat4 mvp = matrix_projection * matrix_modelview; + gl_Position = mvp * attr_vertex; + tex_coords = matrix_texture * attr_texcoord; +} diff -r c14613d27a3a -r 4ca4e3c5a754 src/android/assman.c --- a/src/android/assman.c Thu Jun 11 02:53:43 2015 +0300 +++ b/src/android/assman.c Thu Jun 11 04:56:33 2015 +0300 @@ -52,6 +52,11 @@ return AAsset_seek((AAsset*)fp, offs, whence); } +long ass_ftell(ass_file *fp) +{ + return AAsset_seek((AAsset*)fp, 0, SEEK_SET); +} + size_t ass_fread(void *buf, size_t size, size_t count, ass_file *fp) { return AAsset_read((AAsset*)fp, buf, size * count) / size; diff -r c14613d27a3a -r 4ca4e3c5a754 src/assman.h --- a/src/assman.h Thu Jun 11 02:53:43 2015 +0300 +++ b/src/assman.h Thu Jun 11 04:56:33 2015 +0300 @@ -8,6 +8,7 @@ ass_file *ass_fopen(const char *fname, const char *mode); void ass_fclose(ass_file *fp); long ass_fseek(ass_file *fp, long offs, int whence); +long ass_ftell(ass_file *fp); size_t ass_fread(void *buf, size_t size, size_t count, ass_file *fp); diff -r c14613d27a3a -r 4ca4e3c5a754 src/camera.h --- a/src/camera.h Thu Jun 11 02:53:43 2015 +0300 +++ b/src/camera.h Thu Jun 11 04:56:33 2015 +0300 @@ -11,6 +11,10 @@ }; #endif +#ifdef __cplusplus +extern "C" { +#endif + int cam_init(void *platform_data); void cam_shutdown(void); @@ -26,4 +30,8 @@ int cam_take_picture(void); +#ifdef __cplusplus +} +#endif + #endif /* CAMERA_H_ */ diff -r c14613d27a3a -r 4ca4e3c5a754 src/game.cc --- a/src/game.cc Thu Jun 11 02:53:43 2015 +0300 +++ b/src/game.cc Thu Jun 11 04:56:33 2015 +0300 @@ -5,53 +5,19 @@ #include "opengl.h" #include "game.h" #include "camera.h" -#include "sdr.h" #include "sanegl.h" #include "texture.h" +#include "shader.h" -static void draw_quad(float hsz, float vsz, unsigned int sdr); +static void draw_quad(float hsz, float vsz); static int win_width, win_height; static float win_aspect; static int video_width, video_height; static float video_aspect; -static unsigned int sdrprog, sdrprog_test; -static int uloc_tex, uloc_test_tex; static struct texture *test_tex; -enum { ATTR_VERTEX, ATTR_TEXCOORD }; - -static const char *vsdr_source = - "attribute vec4 attr_vertex, attr_texcoord;\n" - "uniform mat4 matrix_modelview, matrix_projection, matrix_texture;\n" - "varying vec4 tex_coords;\n" - "void main()\n" - "{\n" - "\tmat4 mvp = matrix_projection * matrix_modelview;\n" - "\tgl_Position = mvp * attr_vertex;\n" - "\ttex_coords = matrix_texture * attr_texcoord;\n" - "}\n"; - -static const char *psdr_cam_source = - "#extension GL_OES_EGL_image_external : require\n" - "precision mediump float;\n" - "uniform samplerExternalOES tex;\n" - "varying vec4 tex_coords;\n" - "void main()\n" - "{\n" - "\tvec4 texel = texture2D(tex, tex_coords.xy);\n" - "\tgl_FragColor = vec4(texel.xyz, 1.0);\n" - "}\n"; - -static const char *psdr_tex_source = - "precision mediump float;\n" - "uniform sampler2D tex;\n" - "varying vec4 tex_coords;\n" - "void main()\n" - "{\n" - "\tvec4 texel = texture2D(tex, tex_coords.xy);\n" - "\tgl_FragColor = texel;\n" - "}\n"; +static SdrProg *sdr_cam, *sdr_tex; extern "C" int game_init(void) { @@ -62,36 +28,13 @@ glClearColor(0.4, 0.4, 0.4, 1); - if(!(vsdr = create_vertex_shader(vsdr_source))) - return -1; - assert(glGetError() == GL_NO_ERROR); - if(!(psdr_cam = create_pixel_shader(psdr_cam_source))) - return -1; - assert(glGetError() == GL_NO_ERROR); - if(!(psdr_tex = create_pixel_shader(psdr_tex_source))) - return -1; - assert(glGetError() == GL_NO_ERROR); - if(!(sdrprog = create_program_link(vsdr, psdr_cam, 0))) { - fprintf(stderr, "failed to create shader program\n"); + if(!(sdr_cam = get_sdrprog("sdr/vertex.glsl", "sdr/android_cam_preview.p.glsl"))) { return -1; } - if(!(sdrprog_test = create_program_link(vsdr, psdr_tex, 0))) { - fprintf(stderr, "failed to create test shader program\n"); + if(!(sdr_tex = get_sdrprog("sdr/vertex.glsl", "sdr/tex.p.glsl"))) { return -1; } - glUseProgram(sdrprog); - glBindAttribLocation(sdrprog, ATTR_VERTEX, "attr_vertex"); - glBindAttribLocation(sdrprog, ATTR_TEXCOORD, "attr_texcoord"); - uloc_tex = glGetUniformLocation(sdrprog, "tex"); - glLinkProgram(sdrprog); - - glUseProgram(sdrprog_test); - glBindAttribLocation(sdrprog_test, ATTR_VERTEX, "attr_vertex"); - glBindAttribLocation(sdrprog_test, ATTR_TEXCOORD, "attr_texcoord"); - uloc_test_tex = glGetUniformLocation(sdrprog_test, "tex"); - glLinkProgram(sdrprog_test); - if(!(test_tex = get_texture("data/opengl.png"))) { return -1; } @@ -111,7 +54,8 @@ extern "C" void game_shutdown(void) { cam_shutdown(); - free_program(sdrprog); + delete sdr_cam; + delete sdr_tex; } extern "C" void game_display(unsigned long msec) @@ -133,11 +77,8 @@ gl_matrix_mode(GL_TEXTURE); gl_load_matrixf(tex_matrix); - glUseProgram(sdrprog); + sdr_cam->bind(); glBindTexture(GL_TEXTURE_EXTERNAL_OES, tex); - if(uloc_tex >= 0) { - glUniform1i(uloc_tex, 0); - } if(video_aspect > win_aspect) { xscale = 1.0; @@ -146,7 +87,7 @@ xscale = video_aspect; yscale = 1.0; } - draw_quad(xscale, yscale, sdrprog); + draw_quad(xscale, yscale); gl_matrix_mode(GL_TEXTURE); gl_load_identity(); @@ -156,21 +97,18 @@ gl_load_identity(); gl_scalef((float)test_tex->width / (float)test_tex->height, 1, 1); - glUseProgram(sdrprog_test); + sdr_tex->bind(); glBindTexture(GL_TEXTURE_2D, test_tex->texid); - if(uloc_test_tex >= 0) { - glUniform1i(uloc_test_tex, 0); - } glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - draw_quad(0.5, 0.5, sdrprog_test); + draw_quad(0.5, 0.5); glDisable(GL_BLEND); } -static void draw_quad(float hsz, float vsz, unsigned int sdr) +static void draw_quad(float hsz, float vsz) { static const float varr[] = {-1, -1, 1, -1, 1, 1, -1, 1}; static const float tcarr[] = {0, 0, 1, 0, 1, 1, 0, 1}; @@ -179,17 +117,19 @@ gl_push_matrix(); gl_scalef(hsz, vsz, 1); - gl_apply_xform(sdr); + if(SdrProg::active) { + gl_apply_xform(SdrProg::active->get_globj()); + } - glEnableVertexAttribArray(ATTR_VERTEX); - glEnableVertexAttribArray(ATTR_TEXCOORD); - glVertexAttribPointer(ATTR_VERTEX, 2, GL_FLOAT, 0, 0, varr); - glVertexAttribPointer(ATTR_TEXCOORD, 2, GL_FLOAT, 0, 0, tcarr); + glEnableVertexAttribArray(SDR_ATTR_VERTEX); + glEnableVertexAttribArray(SDR_ATTR_TEXCOORD); + glVertexAttribPointer(SDR_ATTR_VERTEX, 2, GL_FLOAT, 0, 0, varr); + glVertexAttribPointer(SDR_ATTR_TEXCOORD, 2, GL_FLOAT, 0, 0, tcarr); glDrawArrays(GL_TRIANGLE_FAN, 0, 4); - glDisableVertexAttribArray(ATTR_VERTEX); - glDisableVertexAttribArray(ATTR_TEXCOORD); + glDisableVertexAttribArray(SDR_ATTR_VERTEX); + glDisableVertexAttribArray(SDR_ATTR_TEXCOORD); gl_pop_matrix(); } diff -r c14613d27a3a -r 4ca4e3c5a754 src/sdr.c --- a/src/sdr.c Thu Jun 11 02:53:43 2015 +0300 +++ b/src/sdr.c Thu Jun 11 04:56:33 2015 +0300 @@ -5,6 +5,7 @@ #include #include #include "opengl.h" +#include "assman.h" #if defined(unix) || defined(__unix__) #include @@ -136,35 +137,28 @@ unsigned int load_shader(const char *fname, unsigned int sdr_type) { -#if defined(unix) || defined(__unix__) - struct stat st; -#endif unsigned int sdr; size_t filesize; - FILE *fp; + ass_file *fp; char *src; - if(!(fp = fopen(fname, "rb"))) { + if(!(fp = ass_fopen(fname, "rb"))) { fprintf(stderr, "failed to open shader %s: %s\n", fname, strerror(errno)); return 0; } -#if defined(unix) || defined(__unix__) - fstat(fileno(fp), &st); - filesize = st.st_size; -#else - fseek(fp, 0, SEEK_END); - filesize = ftell(fp); - fseek(fp, 0, SEEK_SET); -#endif /* unix */ + filesize = ass_fseek(fp, 0, SEEK_END); + /*filesize = ass_ftell(fp);*/ + printf(" filesize: %ld\n", filesize); + ass_fseek(fp, 0, SEEK_SET); if(!(src = malloc(filesize + 1))) { - fclose(fp); + ass_fclose(fp); return 0; } - fread(src, 1, filesize, fp); + ass_fread(src, 1, filesize, fp); src[filesize] = 0; - fclose(fp); + ass_fclose(fp); fprintf(stderr, "compiling %s shader: %s... ", sdrtypestr(sdr_type), fname); sdr = create_shader(src, sdr_type); diff -r c14613d27a3a -r 4ca4e3c5a754 src/shader.cc --- a/src/shader.cc Thu Jun 11 02:53:43 2015 +0300 +++ b/src/shader.cc Thu Jun 11 04:56:33 2015 +0300 @@ -7,6 +7,8 @@ std::map sdrdb; +const SdrProg *SdrProg::active; + SdrProg::SdrProg() { prog = 0; @@ -59,10 +61,8 @@ return false; } - if(!link()) { - return false; - } - return true; + bind_default_attribs(); + return link(); } bool SdrProg::create(const char *vsrc, const char *psrc) @@ -91,29 +91,32 @@ bool SdrProg::load(const char *vfname, const char *pfname) { - unsigned int vs = load_vertex_shader(vfname); + unsigned int vs = get_shader(vfname, GL_VERTEX_SHADER); if(!vs) { return false; } - sdrdb[vfname] = vs; - unsigned int ps = load_pixel_shader(pfname); + unsigned int ps = get_shader(pfname, GL_FRAGMENT_SHADER); if(!ps) { return false; } - sdrdb[pfname] = ps; printf("creating shader program (%s, %s)\n", vfname, pfname); if(!(prog = create_program_link(vs, ps, 0))) { return false; } - valid = true; - return true; + + bind_default_attribs(); + return link(); } bool SdrProg::link() const { - return link_program(prog) != -1; + if(link_program(prog) == -1) { + return false; + } + valid = true; + return true; } int SdrProg::get_uniform(const char *name) const @@ -149,6 +152,24 @@ return true; } +void SdrProg::bind_default_attribs() const +{ + // XXX must be in sync with SdrDefaultAttrib enums in shader.h + static const char *def_names[] = { + "attr_vertex", + "attr_normal", + "attr_texcoord", + "attr_color", + "attr_tangent", + 0 + }; + + for(int i=0; def_names[i]; i++) { + bind_attrib(def_names[i], i); + } +} + + bool SdrProg::bind() const { if(!prog || (!valid && !link())) { @@ -161,10 +182,11 @@ fprintf(stderr, "failed to bind program %d\n", prog); return false; } + + active = this; return true; } - unsigned int get_shader(const char *name, unsigned int type) { std::map::const_iterator it = sdrdb.find(name); @@ -180,3 +202,13 @@ sdrdb[name] = sdr; return sdr; } + +SdrProg *get_sdrprog(const char *vfile, const char *pfile) +{ + SdrProg *sp = new SdrProg; + if(!sp->load(vfile, pfile)) { + delete sp; + return 0; + } + return sp; +} diff -r c14613d27a3a -r 4ca4e3c5a754 src/shader.h --- a/src/shader.h Thu Jun 11 02:53:43 2015 +0300 +++ b/src/shader.h Thu Jun 11 04:56:33 2015 +0300 @@ -3,6 +3,15 @@ #include +enum SdrDefaultAttrib { + SDR_ATTR_VERTEX, + SDR_ATTR_NORMAL, + SDR_ATTR_TEXCOORD, + SDR_ATTR_COLOR, + SDR_ATTR_TANGENT +}; + + class SdrProg { private: std::vector priv_sdr; @@ -10,6 +19,8 @@ mutable bool valid; public: + static const SdrProg *active; + SdrProg(); ~SdrProg(); @@ -27,10 +38,15 @@ int get_uniform(const char *name) const; int get_attrib(const char *name) const; bool bind_attrib(const char *name, int loc) const; + void bind_default_attribs() const; bool bind() const; + + unsigned int get_globj() const { return prog; } }; unsigned int get_shader(const char *name, unsigned int type); +SdrProg *get_sdrprog(const char *vfile, const char *pfile); + #endif /* SHADER_H_ */ diff -r c14613d27a3a -r 4ca4e3c5a754 src/texture.h --- a/src/texture.h Thu Jun 11 02:53:43 2015 +0300 +++ b/src/texture.h Thu Jun 11 04:56:33 2015 +0300 @@ -7,9 +7,17 @@ int img_width, img_height; /* in case we have a smaller image in a pow2 texture */ }; +#ifdef __cplusplus +extern "C" { +#endif + int load_texture(struct texture *tex, const char *fname); struct texture *get_texture(const char *fname); int gen_debug_texture(struct texture *tex); +#ifdef __cplusplus +} +#endif + #endif /* TEXTURE_H_ */