From 508cfdbda762e2d8572d633a43a80fcea25adf55 Mon Sep 17 00:00:00 2001 From: Themaister Date: Sun, 3 Jul 2011 15:39:35 +0200 Subject: [PATCH] Start implementing access to previous texture. --- gfx/gl.c | 104 +++++++++++++++++++++++++++++----------------- gfx/gl_common.h | 6 +-- gfx/shader_cg.c | 29 ++++++++++++- gfx/shader_cg.h | 1 + gfx/shader_glsl.c | 40 +++++++++++++++--- gfx/shader_glsl.h | 1 + 6 files changed, 133 insertions(+), 48 deletions(-) diff --git a/gfx/gl.c b/gfx/gl.c index e142be8b24..2866d4baad 100644 --- a/gfx/gl.c +++ b/gfx/gl.c @@ -125,7 +125,9 @@ static inline bool load_gl_proc(void) { return true; } typedef struct gl { bool vsync; - GLuint texture; + GLuint texture[2]; + unsigned tex_index; // For use with PREV. + struct gl_tex_info prev_info; GLuint tex_filter; void *empty_buf; @@ -153,8 +155,8 @@ typedef struct gl unsigned win_height; unsigned vp_width, vp_out_width; unsigned vp_height, vp_out_height; - unsigned last_width; - unsigned last_height; + unsigned last_width[2]; + unsigned last_height[2]; unsigned tex_w, tex_h; GLfloat tex_coords[8]; #ifdef HAVE_FBO @@ -255,20 +257,21 @@ static void gl_shader_set_params(unsigned width, unsigned height, unsigned out_width, unsigned out_height, unsigned frame_count, const struct gl_tex_info *info, + const struct gl_tex_info *prev_info, const struct gl_tex_info *fbo_info, unsigned fbo_info_cnt) { #ifdef HAVE_CG gl_cg_set_params(width, height, tex_width, tex_height, out_width, out_height, - frame_count, info, fbo_info, fbo_info_cnt); + frame_count, info, prev_info, fbo_info, fbo_info_cnt); #endif #ifdef HAVE_XML gl_glsl_set_params(width, height, tex_width, tex_height, out_width, out_height, - frame_count, info, fbo_info, fbo_info_cnt); + frame_count, info, prev_info, fbo_info, fbo_info_cnt); #endif } @@ -338,7 +341,7 @@ static inline void gl_init_font(gl_t *gl, const char *font_path, unsigned font_s glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glBindTexture(GL_TEXTURE_2D, gl->texture); + glBindTexture(GL_TEXTURE_2D, gl->texture[gl->tex_index]); } else SSNES_WARN("Couldn't init font renderer with font \"%s\"...\n", font_path); @@ -653,7 +656,7 @@ static void gl_render_msg(gl_t *gl, const char *msg) // Go back to old rendering path. glTexCoordPointer(2, GL_FLOAT, 2 * sizeof(GLfloat), gl->tex_coords); glVertexPointer(2, GL_FLOAT, 2 * sizeof(GLfloat), vertexes_flipped); - glBindTexture(GL_TEXTURE_2D, gl->texture); + glBindTexture(GL_TEXTURE_2D, gl->texture[gl->tex_index]); glDisable(GL_BLEND); #endif } @@ -712,6 +715,10 @@ static bool gl_frame(void *data, const void* frame, unsigned width, unsigned hei gl_shader_use(1); gl->frame_count++; +#if defined(HAVE_XML) || defined(HAVE_CG) + glBindTexture(GL_TEXTURE_2D, gl->texture[gl->tex_index]); +#endif + #ifdef HAVE_FBO // Render to texture in first pass. if (gl->fbo_inited) @@ -767,7 +774,7 @@ static bool gl_frame(void *data, const void* frame, unsigned width, unsigned hei last_max_height = gl->fbo_rect[i].max_img_height; } - glBindTexture(GL_TEXTURE_2D, gl->texture); + glBindTexture(GL_TEXTURE_2D, gl->texture[gl->tex_index]); pglBindFramebuffer(GL_FRAMEBUFFER, gl->fbo[0]); gl->render_to_tex = true; set_viewport(gl, gl->fbo_rect[0].img_width, gl->fbo_rect[0].img_height, true); @@ -814,7 +821,7 @@ static bool gl_frame(void *data, const void* frame, unsigned width, unsigned hei } // Go back to what we're supposed to do, render to FBO #0 :D - glBindTexture(GL_TEXTURE_2D, gl->texture); + glBindTexture(GL_TEXTURE_2D, gl->texture[gl->tex_index]); pglBindFramebuffer(GL_FRAMEBUFFER, gl->fbo[0]); set_viewport(gl, gl->fbo_rect[0].img_width, gl->fbo_rect[0].img_height, true); } @@ -823,10 +830,10 @@ static bool gl_frame(void *data, const void* frame, unsigned width, unsigned hei #endif } - if ((width != gl->last_width || height != gl->last_height) && gl->empty_buf) // Res change. need to clear out texture. + if ((width != gl->last_width[gl->tex_index] || height != gl->last_height[gl->tex_index]) && gl->empty_buf) // Res change. need to clear out texture. { - gl->last_width = width; - gl->last_height = height; + gl->last_width[gl->tex_index] = width; + gl->last_height[gl->tex_index] = height; glPixelStorei(GL_UNPACK_ALIGNMENT, get_alignment(pitch)); glPixelStorei(GL_UNPACK_ROW_LENGTH, gl->tex_w); @@ -839,13 +846,13 @@ static bool gl_frame(void *data, const void* frame, unsigned width, unsigned hei set_texture_coords(gl->tex_coords, xamt, yamt); } - - // Work around a certain issue a Cg where not using TEXUNIT0 - // in shader causes cgGLEnableTextureParameter() causes it - // to bind to TEXUNIT0, to avoid really funny bugs, rebind - // our texture. -#ifdef HAVE_CG - glBindTexture(GL_TEXTURE_2D, gl->texture); +#if defined(HAVE_XML) || defined(HAVE_CG) + else if (width != gl->last_width[1 - gl->tex_index] || height != gl->last_height[1 - gl->tex_index]) + { + GLfloat xamt = (GLfloat)width / gl->tex_w; + GLfloat yamt = (GLfloat)height / gl->tex_h; + set_texture_coords(gl->tex_coords, xamt, yamt); + } #endif #ifdef HAVE_FBO @@ -861,7 +868,7 @@ static bool gl_frame(void *data, const void* frame, unsigned width, unsigned hei gl->texture_fmt, frame); struct gl_tex_info tex_info = { - .tex = gl->texture, + .tex = gl->texture[gl->tex_index], .input_size = {width, height}, .tex_size = {gl->tex_w, gl->tex_h} }; @@ -871,7 +878,7 @@ static bool gl_frame(void *data, const void* frame, unsigned width, unsigned hei glClear(GL_COLOR_BUFFER_BIT); gl_shader_set_params(width, height, gl->tex_w, gl->tex_h, gl->vp_width, gl->vp_height, gl->frame_count, - &tex_info, fbo_tex_info, fbo_tex_info_cnt); + &tex_info, &gl->prev_info, fbo_tex_info, fbo_tex_info_cnt); glDrawArrays(GL_QUADS, 0, 4); @@ -916,7 +923,7 @@ static bool gl_frame(void *data, const void* frame, unsigned width, unsigned hei gl_shader_set_params(prev_rect->img_width, prev_rect->img_height, prev_rect->width, prev_rect->height, gl->vp_width, gl->vp_height, gl->frame_count, - &tex_info, fbo_tex_info, fbo_tex_info_cnt); + &tex_info, &gl->prev_info, fbo_tex_info, fbo_tex_info_cnt); glDrawArrays(GL_QUADS, 0, 4); @@ -942,7 +949,7 @@ static bool gl_frame(void *data, const void* frame, unsigned width, unsigned hei gl_shader_set_params(prev_rect->img_width, prev_rect->img_height, prev_rect->width, prev_rect->height, gl->vp_width, gl->vp_height, gl->frame_count, - &tex_info, fbo_tex_info, fbo_tex_info_cnt); + &tex_info, &gl->prev_info, fbo_tex_info, fbo_tex_info_cnt); glVertexPointer(2, GL_FLOAT, 2 * sizeof(GLfloat), vertexes_flipped); glDrawArrays(GL_QUADS, 0, 4); @@ -951,6 +958,11 @@ static bool gl_frame(void *data, const void* frame, unsigned width, unsigned hei } #endif +#if defined(HAVE_XML) || defined(HAVE_CG) + memcpy(&gl->prev_info, &tex_info, sizeof(tex_info)); + gl->tex_index = 1 - gl->tex_index; +#endif + if (msg) gl_render_msg(gl, msg); @@ -970,7 +982,7 @@ static void gl_free(void *data) gl_shader_deinit(); glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY); - glDeleteTextures(1, &gl->texture); + glDeleteTextures(2, gl->texture); #ifdef HAVE_FBO if (gl->fbo_inited) @@ -1133,15 +1145,18 @@ static void* gl_init(const video_info_t *video, const input_driver_t **input, vo glMatrixMode(GL_MODELVIEW); glLoadIdentity(); - glGenTextures(1, &gl->texture); + glGenTextures(2, gl->texture); - glBindTexture(GL_TEXTURE_2D, gl->texture); + for (unsigned i = 0; i < 2; i++) + { + glBindTexture(GL_TEXTURE_2D, gl->texture[i]); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl->tex_filter); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl->tex_filter); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl->tex_filter); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl->tex_filter); + } glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); @@ -1155,15 +1170,30 @@ static void* gl_init(const video_info_t *video, const input_driver_t **input, vo gl->tex_w = 256 * video->input_scale; gl->tex_h = 256 * video->input_scale; - glTexImage2D(GL_TEXTURE_2D, - 0, GL_RGBA, gl->tex_w, gl->tex_h, 0, gl->texture_type, - gl->texture_fmt, NULL); - // Empty buffer that we use to clear out the texture with on res change. gl->empty_buf = calloc(gl->base_size, gl->tex_w * gl->tex_h); - gl->last_width = gl->tex_w; - gl->last_height = gl->tex_h; + for (unsigned i = 0; i < 2; i++) + { + glBindTexture(GL_TEXTURE_2D, gl->texture[i]); + glTexImage2D(GL_TEXTURE_2D, + 0, GL_RGBA, gl->tex_w, gl->tex_h, 0, gl->texture_type, + gl->texture_fmt, gl->empty_buf ? gl->empty_buf : NULL); + } + glBindTexture(GL_TEXTURE_2D, gl->texture[gl->tex_index]); + + for (unsigned i = 0; i < 2; i++) + { + gl->last_width[i] = gl->tex_w; + gl->last_height[i] = gl->tex_h; + } + + gl->prev_info.tex = gl->texture[1 - gl->tex_index]; + gl->prev_info.input_size[0] = gl->tex_w; + gl->prev_info.tex_size[0] = gl->tex_w; + gl->prev_info.input_size[1] = gl->tex_h; + gl->prev_info.tex_size[1] = gl->tex_h; + memcpy(gl->prev_info.coord, tex_coords, sizeof(tex_coords)); // Hook up SDL input driver to get SDL_QUIT events and RESIZE. sdl_input_t *sdl_input = input_sdl.init(); @@ -1222,7 +1252,7 @@ static bool gl_xml_shader(void *data, const char *path) gl->render_to_tex = false; gl->fbo_pass = 0; - glBindTexture(GL_TEXTURE_2D, gl->texture); + glBindTexture(GL_TEXTURE_2D, gl->texture[gl->tex_index]); } #endif diff --git a/gfx/gl_common.h b/gfx/gl_common.h index 2c6d14750b..300bb13786 100644 --- a/gfx/gl_common.h +++ b/gfx/gl_common.h @@ -101,9 +101,9 @@ struct gl_fbo_scale struct gl_tex_info { GLuint tex; - float input_size[2]; - float tex_size[2]; - float coord[8]; + GLfloat input_size[2]; + GLfloat tex_size[2]; + GLfloat coord[8]; }; // Not legal to cast void* to fn-pointer. Need dirty hack to be compilant. diff --git a/gfx/shader_cg.c b/gfx/shader_cg.c index 41f803ce8b..2cfe2130ce 100644 --- a/gfx/shader_cg.c +++ b/gfx/shader_cg.c @@ -112,6 +112,7 @@ struct cg_program struct cg_fbo_params fbo[MAX_SHADERS]; struct cg_fbo_params orig; + struct cg_fbo_params prev; }; #define FILTER_UNSPEC 0 @@ -148,6 +149,7 @@ void gl_cg_set_params(unsigned width, unsigned height, unsigned out_width, unsigned out_height, unsigned frame_count, const struct gl_tex_info *info, + const struct gl_tex_info *prev_info, const struct gl_tex_info *fbo_info, unsigned fbo_info_cnt) { @@ -169,7 +171,6 @@ void gl_cg_set_params(unsigned width, unsigned height, if (param) { cgGLSetTextureParameter(param, info->tex); - //fprintf(stderr, "ORIGtex = (%d) %d\n", cgGLGetTextureParameter(param), cgGLGetTextureEnum(param) - GL_TEXTURE0); cgGLEnableTextureParameter(param); } @@ -183,6 +184,24 @@ void gl_cg_set_params(unsigned width, unsigned height, cgGLEnableClientState(prg[active_index].orig.coord); } + // Set prev texture + param = prg[active_index].prev.tex; + if (param) + { + cgGLSetTextureParameter(param, prev_info->tex); + cgGLEnableTextureParameter(param); + } + + set_param_2f(prg[active_index].prev.vid_size_v, prev_info->input_size[0], prev_info->input_size[1]); + set_param_2f(prg[active_index].prev.vid_size_f, prev_info->input_size[0], prev_info->input_size[1]); + set_param_2f(prg[active_index].prev.tex_size_v, prev_info->tex_size[0], prev_info->tex_size[1]); + set_param_2f(prg[active_index].prev.tex_size_f, prev_info->tex_size[0], prev_info->tex_size[1]); + if (prg[active_index].prev.coord) + { + cgGLSetParameterPointer(prg[active_index].prev.coord, 2, GL_FLOAT, 0, prev_info->coord); + cgGLEnableClientState(prg[active_index].prev.coord); + } + // Set lookup textures. for (unsigned i = 0; i < lut_textures_num; i++) { @@ -191,7 +210,6 @@ void gl_cg_set_params(unsigned width, unsigned height, { cgGLSetTextureParameter(param, lut_textures[i]); cgGLEnableTextureParameter(param); - //fprintf(stderr, "LUTtex = (%d) %d\n", cgGLGetTextureParameter(param), cgGLGetTextureEnum(param) - GL_TEXTURE0); } } @@ -908,6 +926,13 @@ bool gl_cg_init(const char *path) prg[i].orig.tex_size_f = cgGetNamedParameter(prg[i].fprg, "ORIG.texture_size"); prg[i].orig.coord = cgGetNamedParameter(prg[i].vprg, "ORIG.tex_coord"); + prg[i].prev.tex = cgGetNamedParameter(prg[i].fprg, "PREV.texture"); + prg[i].prev.vid_size_v = cgGetNamedParameter(prg[i].vprg, "PREV.video_size"); + prg[i].prev.vid_size_f = cgGetNamedParameter(prg[i].fprg, "PREV.video_size"); + prg[i].prev.tex_size_v = cgGetNamedParameter(prg[i].vprg, "PREV.texture_size"); + prg[i].prev.tex_size_f = cgGetNamedParameter(prg[i].fprg, "PREV.texture_size"); + prg[i].prev.coord = cgGetNamedParameter(prg[i].vprg, "PREV.tex_coord"); + for (unsigned j = 0; j < i - 1; j++) { char attr_buf[64]; diff --git a/gfx/shader_cg.h b/gfx/shader_cg.h index 306f65f17b..f0d8147c80 100644 --- a/gfx/shader_cg.h +++ b/gfx/shader_cg.h @@ -33,6 +33,7 @@ void gl_cg_set_params(unsigned width, unsigned height, unsigned out_width, unsigned out_height, unsigned frame_count, const struct gl_tex_info *info, + const struct gl_tex_info *prev_info, const struct gl_tex_info *fbo_info, unsigned fbo_info_cnt); void gl_cg_use(unsigned index); diff --git a/gfx/shader_glsl.c b/gfx/shader_glsl.c index caf360129d..0a5196ceee 100644 --- a/gfx/shader_glsl.c +++ b/gfx/shader_glsl.c @@ -1005,8 +1005,16 @@ void gl_glsl_set_params(unsigned width, unsigned height, unsigned out_width, unsigned out_height, unsigned frame_count, const struct gl_tex_info *info, + const struct gl_tex_info *prev_info, const struct gl_tex_info *fbo_info, unsigned fbo_info_cnt) { + // We enforce a certain layout for our various texture types in the texunits. + // Unit 0: Regular SNES frame (rubyTexture). + // Unit 1-A: LUT textures. + // Unit A+1: Previous texture. + // Unit A+2: Original texture. + // Unit A+3-B: FBO textures. + if (glsl_enable && gl_program[active_index] > 0) { GLint location; @@ -1032,15 +1040,34 @@ void gl_glsl_set_params(unsigned width, unsigned height, pglUniform1i(location, i + 1); } + // Set previous texture. + pglActiveTexture(GL_TEXTURE0 + gl_teximage_cnt + 1); + glBindTexture(GL_TEXTURE_2D, prev_info->tex); + location = pglGetUniformLocation(gl_program[active_index], "rubyPrevTexture"); + pglUniform1i(location, gl_teximage_cnt + 1); + + location = pglGetUniformLocation(gl_program[active_index], "rubyPrevTextureSize"); + pglUniform2fv(location, 1, prev_info->tex_size); + location = pglGetUniformLocation(gl_program[active_index], "rubyPrevInputSize"); + pglUniform2fv(location, 1, prev_info->input_size); + + // Pass texture coordinates. + location = pglGetAttribLocation(gl_program[active_index], "rubyPrevTexCoord"); + if (location >= 0) + { + pglEnableVertexAttribArray(location); + pglVertexAttribPointer(location, 2, GL_FLOAT, GL_FALSE, 0, prev_info->coord); + } + // Set original texture unless we're in first pass (pointless). if (active_index > 1) { // Bind original texture. - pglActiveTexture(GL_TEXTURE0 + gl_teximage_cnt + 1); + pglActiveTexture(GL_TEXTURE0 + gl_teximage_cnt + 2); glBindTexture(GL_TEXTURE_2D, info->tex); location = pglGetUniformLocation(gl_program[active_index], "rubyOrigTexture"); - pglUniform1i(location, gl_teximage_cnt + 1); + pglUniform1i(location, gl_teximage_cnt + 2); location = pglGetUniformLocation(gl_program[active_index], "rubyOrigTextureSize"); pglUniform2fv(location, 1, info->tex_size); @@ -1055,7 +1082,7 @@ void gl_glsl_set_params(unsigned width, unsigned height, pglVertexAttribPointer(location, 2, GL_FLOAT, GL_FALSE, 0, info->coord); } - GLuint base_tex = GL_TEXTURE0 + gl_teximage_cnt + 2; + GLuint base_tex = GL_TEXTURE0 + gl_teximage_cnt + 3; // Bind new texture in the chain. if (fbo_info_cnt > 0) @@ -1093,14 +1120,15 @@ void gl_glsl_set_params(unsigned width, unsigned height, else { // First pass, so unbind everything to avoid collitions. - pglActiveTexture(GL_TEXTURE0 + gl_teximage_cnt + 1); + // Unbind ORIG. + pglActiveTexture(GL_TEXTURE0 + gl_teximage_cnt + 2); glBindTexture(GL_TEXTURE_2D, 0); - GLuint base_tex = GL_TEXTURE0 + gl_teximage_cnt + 2; + GLuint base_tex = GL_TEXTURE0 + gl_teximage_cnt + 3; // Unbind any lurking FBO passes. // Rendering to a texture that is bound to a texture unit // sounds very shaky ... ;) - for (int i = 0; i < gl_num_programs; i++) + for (unsigned i = 0; i < gl_num_programs; i++) { pglActiveTexture(GL_TEXTURE0 + base_tex + i); glBindTexture(GL_TEXTURE_2D, 0); diff --git a/gfx/shader_glsl.h b/gfx/shader_glsl.h index d0d85ead8b..ad77c57100 100644 --- a/gfx/shader_glsl.h +++ b/gfx/shader_glsl.h @@ -33,6 +33,7 @@ void gl_glsl_set_params(unsigned width, unsigned height, unsigned out_width, unsigned out_height, unsigned frame_counter, const struct gl_tex_info *info, + const struct gl_tex_info *prev_info, const struct gl_tex_info *fbo_info, unsigned fbo_info_cnt); void gl_glsl_use(unsigned index);