From f9c5c7c8a5eaa199dba19011f88f10f5387b8654 Mon Sep 17 00:00:00 2001 From: Themaister Date: Mon, 23 May 2011 17:41:52 +0200 Subject: [PATCH] Path to check initial texture with GLSL. --- gfx/gl.c | 29 ++++++++++++++++++++--------- gfx/gl_common.h | 8 ++++++++ gfx/shader_cg.c | 4 +++- gfx/shader_cg.h | 3 ++- gfx/shader_glsl.c | 47 +++++++++++++++++++++++++++++++++++++++++++++-- gfx/shader_glsl.h | 3 ++- 6 files changed, 80 insertions(+), 14 deletions(-) diff --git a/gfx/gl.c b/gfx/gl.c index fea1629bd8..eefe474cad 100644 --- a/gfx/gl.c +++ b/gfx/gl.c @@ -110,12 +110,9 @@ static inline bool load_gl_proc(void) { LOAD_SYM(glClientActiveTexture); LOAD_SYM(glActiveTexture); - return pglClientActiveTexture && pglActiveTexture; } #else -#define pglClientActiveTexture glClientActiveTexture -#define pglActiveTexture glActiveTexture static inline bool load_gl_proc(void) { return true; } #endif @@ -251,14 +248,21 @@ static void gl_shader_set_proj_matrix(void) static void gl_shader_set_params(unsigned width, unsigned height, unsigned tex_width, unsigned tex_height, unsigned out_width, unsigned out_height, - unsigned frame_count) + unsigned frame_count, + const struct gl_tex_info *info) { #ifdef HAVE_CG - gl_cg_set_params(width, height, tex_width, tex_height, out_width, out_height, frame_count); + gl_cg_set_params(width, height, + tex_width, tex_height, + out_width, out_height, + frame_count, info); #endif #ifdef HAVE_XML - gl_glsl_set_params(width, height, tex_width, tex_height, out_width, out_height, frame_count); + gl_glsl_set_params(width, height, + tex_width, tex_height, + out_width, out_height, + frame_count, info); #endif } @@ -767,8 +771,15 @@ static bool gl_frame(void *data, const void* frame, unsigned width, unsigned hei #endif } + struct gl_tex_info tex_info = { + .tex = gl->texture, + .input_size = {width, height}, + .tex_size = {gl->tex_w, gl->tex_h}, + .coord = gl->tex_coords + }; + 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); + gl_shader_set_params(width, height, gl->tex_w, gl->tex_h, gl->vp_width, gl->vp_height, gl->frame_count, &tex_info); if (width != gl->last_width || height != gl->last_height) // Res change. need to clear out texture. { @@ -834,7 +845,7 @@ static bool gl_frame(void *data, const void* frame, unsigned width, unsigned hei // Render to FBO with certain size. set_viewport(gl, rect->img_width, rect->img_height, true); - 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); + 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); glDrawArrays(GL_QUADS, 0, 4); } @@ -855,7 +866,7 @@ static bool gl_frame(void *data, const void* frame, unsigned width, unsigned hei glClear(GL_COLOR_BUFFER_BIT); gl->render_to_tex = false; set_viewport(gl, gl->win_width, gl->win_height, false); - 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); + 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); glBindTexture(GL_TEXTURE_2D, gl->fbo_texture[gl->fbo_pass - 1]); glDrawArrays(GL_QUADS, 0, 4); diff --git a/gfx/gl_common.h b/gfx/gl_common.h index a57bf7a13c..39a71afbd6 100644 --- a/gfx/gl_common.h +++ b/gfx/gl_common.h @@ -98,6 +98,14 @@ struct gl_fbo_scale bool valid; }; +struct gl_tex_info +{ + GLuint tex; + float input_size[2]; + float tex_size[2]; + const float *coord; +}; + // Windows ... <_< #if (defined(HAVE_XML) || defined(HAVE_CG)) && defined(_WIN32) extern PFNGLCLIENTACTIVETEXTUREPROC pglClientActiveTexture; diff --git a/gfx/shader_cg.c b/gfx/shader_cg.c index 7d2805c5ab..f75ca25c05 100644 --- a/gfx/shader_cg.c +++ b/gfx/shader_cg.c @@ -93,8 +93,10 @@ void gl_cg_set_proj_matrix(void) void gl_cg_set_params(unsigned width, unsigned height, unsigned tex_width, unsigned tex_height, unsigned out_width, unsigned out_height, - unsigned frame_count) + unsigned frame_count, + const struct gl_tex_info *info) { + (void)info; if (cg_active) { cgGLSetParameter2f(prg[active_index].vid_size_f, width, height); diff --git a/gfx/shader_cg.h b/gfx/shader_cg.h index 8f5ce2184b..47c4d26059 100644 --- a/gfx/shader_cg.h +++ b/gfx/shader_cg.h @@ -31,7 +31,8 @@ void gl_cg_set_proj_matrix(void); void gl_cg_set_params(unsigned width, unsigned height, unsigned tex_width, unsigned tex_height, unsigned out_width, unsigned out_height, - unsigned frame_count); + unsigned frame_count, + const struct gl_tex_info *info); void gl_cg_use(unsigned index); diff --git a/gfx/shader_glsl.c b/gfx/shader_glsl.c index 0bfff2e02f..ab960edeae 100644 --- a/gfx/shader_glsl.c +++ b/gfx/shader_glsl.c @@ -61,6 +61,9 @@ #define pglGetProgramInfoLog glGetProgramInfoLog #define pglDeleteProgram glDeleteProgram #define pglGetAttachedShaders glGetAttachedShaders +#define pglGetAttribLocation glGetAttribLocation +#define pglEnableVertexAttribArray glEnableVertexAttribArray +#define pglVertexAttribPointer glVertexAttribPointer #else static PFNGLCREATEPROGRAMPROC pglCreateProgram = NULL; static PFNGLUSEPROGRAMPROC pglUseProgram = NULL; @@ -81,6 +84,9 @@ static PFNGLGETPROGRAMIVPROC pglGetProgramiv = NULL; static PFNGLGETPROGRAMINFOLOGPROC pglGetProgramInfoLog = NULL; static PFNGLDELETEPROGRAMPROC pglDeleteProgram = NULL; static PFNGLGETATTACHEDSHADERSPROC pglGetAttachedShaders = NULL; +static PFNGLGETATTRIBLOCATIONPROC pglGetAttribLocation = NULL; +static PFNGLENABLEVERTEXATTRIBARRAYPROC pglEnableVertexAttribArray = NULL; +static PFNGLVERTEXATTRIBPOINTERPROC pglVertexAttribPointer = NULL; #endif #define MAX_PROGRAMS 16 @@ -597,6 +603,9 @@ bool gl_glsl_init(const char *path) LOAD_GL_SYM(GetProgramInfoLog); LOAD_GL_SYM(DeleteProgram); LOAD_GL_SYM(GetAttachedShaders); + LOAD_GL_SYM(GetAttribLocation); + LOAD_GL_SYM(EnableVertexAttribArray); + LOAD_GL_SYM(VertexAttribPointer); #endif SSNES_LOG("Checking GLSL shader support ...\n"); @@ -608,7 +617,9 @@ bool gl_glsl_init(const char *path) && pglDetachShader && pglLinkProgram && pglGetUniformLocation && pglUniform1i && pglUniform2fv && pglUniform4fv && pglGetShaderiv && pglGetShaderInfoLog && pglGetProgramiv && pglGetProgramInfoLog - && pglDeleteProgram && pglGetAttachedShaders; + && pglDeleteProgram && pglGetAttachedShaders + && pglGetAttribLocation && pglEnableVertexAttribArray + && pglVertexAttribPointer; #endif if (!shader_support) @@ -701,7 +712,8 @@ void gl_glsl_deinit(void) void gl_glsl_set_params(unsigned width, unsigned height, unsigned tex_width, unsigned tex_height, unsigned out_width, unsigned out_height, - unsigned frame_count) + unsigned frame_count, + const struct gl_tex_info *info) { if (glsl_enable && gl_program[active_index] > 0) { @@ -727,6 +739,37 @@ void gl_glsl_set_params(unsigned width, unsigned height, location = pglGetUniformLocation(gl_program[active_index], gl_teximage_uniforms[i]); pglUniform1i(location, i + 1); } + + // Set original texture unless we're in first pass (pointless). + if (active_index > 1) + { + pglActiveTexture(GL_TEXTURE0 + gl_teximage_cnt + 1); + glBindTexture(GL_TEXTURE_2D, info->tex); + + location = pglGetUniformLocation(gl_program[active_index], "rubyOrigTexture"); + pglUniform1i(location, gl_teximage_cnt + 1); + + location = pglGetUniformLocation(gl_program[active_index], "rubyOrigTextureSize"); + pglUniform2fv(location, 1, info->tex_size); + location = pglGetUniformLocation(gl_program[active_index], "rubyOrigInputSize"); + pglUniform2fv(location, 1, info->input_size); + + // Pass texture coordinates. + location = pglGetAttribLocation(gl_program[active_index], "rubyOrigTexCoord"); + if (location >= 0) + { + pglEnableVertexAttribArray(location); + pglVertexAttribPointer(location, 2, GL_FLOAT, GL_FALSE, 0, info->coord); + } + + pglActiveTexture(GL_TEXTURE0); + } + else + { + pglActiveTexture(GL_TEXTURE0 + gl_teximage_cnt + 1); + glBindTexture(GL_TEXTURE_2D, 0); + pglActiveTexture(GL_TEXTURE0); + } } } diff --git a/gfx/shader_glsl.h b/gfx/shader_glsl.h index e46a2f21a5..1aabd658ce 100644 --- a/gfx/shader_glsl.h +++ b/gfx/shader_glsl.h @@ -31,7 +31,8 @@ void gl_glsl_set_proj_matrix(void); void gl_glsl_set_params(unsigned width, unsigned height, unsigned tex_width, unsigned tex_height, unsigned out_width, unsigned out_height, - unsigned frame_counter); + unsigned frame_counter, + const struct gl_tex_info *info); void gl_glsl_use(unsigned index);