Add shaders in the mix. Can disable/enable shaders on the fly.

This commit is contained in:
Themaister 2011-01-23 00:48:47 +01:00
parent 2211dc73e1
commit 0c10f10edd
5 changed files with 89 additions and 13 deletions

View File

@ -114,7 +114,6 @@ static inline bool gl_shader_init(void)
return true;
}
/*
static inline void gl_shader_deactivate(void)
{
#ifdef HAVE_CG
@ -125,9 +124,7 @@ static inline void gl_shader_deactivate(void)
gl_glsl_deactivate();
#endif
}
*/
/*
static inline void gl_shader_activate(void)
{
#ifdef HAVE_CG
@ -138,7 +135,6 @@ static inline void gl_shader_activate(void)
gl_glsl_activate();
#endif
}
*/
static inline void gl_shader_deinit(void)
{
@ -180,7 +176,7 @@ static inline void gl_shader_set_params(unsigned width, unsigned height,
static inline void gl_init_font(gl_t *gl, const char *font_path)
{
#ifdef HAVE_FREETYPE
gl->font = font_renderer_new(font_path, 16);
gl->font = font_renderer_new(font_path, 48);
if (gl->font)
{
glGenTextures(1, &gl->font_tex);
@ -227,7 +223,7 @@ static void gl_render_msg(gl_t *gl, const char *msg)
GLfloat font_vertex[12];
// Deactivate custom shaders. Enable the font texture.
//gl_shader_deactivate();
gl_shader_deactivate();
glBindTexture(GL_TEXTURE_2D, gl->font_tex);
glVertexPointer(3, GL_FLOAT, 3 * sizeof(GLfloat), font_vertex);
glTexCoordPointer(2, GL_FLOAT, 2 * sizeof(GLfloat), tex_coords); // Use the static one (uses whole texture).
@ -243,10 +239,10 @@ static void gl_render_msg(gl_t *gl, const char *msg)
while (head != NULL)
{
GLfloat lx = (GLfloat)head->off_x / gl->vp_width + 0.200;
GLfloat hx = (GLfloat)(head->off_x + head->width) / gl->vp_width + 0.200;
GLfloat ly = (GLfloat)head->off_y / gl->vp_height + 0.200;
GLfloat hy = (GLfloat)(head->off_y + head->height) / gl->vp_height + 0.200;
GLfloat lx = (GLfloat)head->off_x / gl->vp_width + 0.05;
GLfloat hx = (GLfloat)(head->off_x + head->width) / gl->vp_width + 0.05;
GLfloat ly = (GLfloat)head->off_y / gl->vp_height + 0.05;
GLfloat hy = (GLfloat)(head->off_y + head->height) / gl->vp_height + 0.05;
font_vertex[0] = lx;
font_vertex[1] = ly;
@ -273,7 +269,7 @@ static void gl_render_msg(gl_t *gl, const char *msg)
glVertexPointer(3, GL_FLOAT, 3 * sizeof(GLfloat), vertexes);
glBindTexture(GL_TEXTURE_2D, gl->texture);
glDisable(GL_BLEND);
//gl_shader_activate();
gl_shader_activate();
#endif
}
//////////////
@ -395,7 +391,8 @@ static bool gl_frame(void *data, const uint16_t* frame, unsigned width, unsigned
GL_UNSIGNED_SHORT_1_5_5_5_REV, frame);
glDrawArrays(GL_QUADS, 0, 4);
gl_render_msg(gl, "hei paa deg");
if (msg)
gl_render_msg(gl, msg);
show_fps();
glFlush();

View File

@ -20,9 +20,45 @@
#include <Cg/cgGL.h>
#include "general.h"
// Used when we call deactivate() since just unbinding the program didn't seem to work... :(
static const char* stock_cg_program =
"void main_vertex"
"("
" float4 position : POSITION,"
" float4 color : COLOR,"
" float2 texCoord : TEXCOORD0,"
""
" uniform float4x4 modelViewProj,"
""
" out float4 oPosition : POSITION,"
" out float4 oColor : COLOR,"
" out float2 otexCoord : TEXCOORD"
")"
"{"
" oPosition = mul(modelViewProj, position);"
" oColor = color;"
" otexCoord = texCoord;"
"}"
""
""
"struct output "
"{"
" float4 color : COLOR;"
"};"
""
"output main_fragment(float2 texCoord : TEXCOORD0, uniform sampler2D decal : TEXUNIT0) "
"{"
" output OUT;"
" OUT.color = tex2D(decal, texCoord);"
" return OUT;"
"}";
static CGcontext cgCtx;
static CGprogram cgFPrg;
static CGprogram cgVPrg;
static CGprogram cgSFPrg;
static CGprogram cgSVPrg;
static CGprofile cgFProf;
static CGprofile cgVProf;
static CGparameter cg_video_size, cg_texture_size, cg_output_size;
@ -78,7 +114,9 @@ bool gl_cg_init(const char *path)
cgGLSetOptimalOptions(cgVProf);
cgFPrg = cgCreateProgramFromFile(cgCtx, CG_SOURCE, path, cgFProf, "main_fragment", 0);
cgVPrg = cgCreateProgramFromFile(cgCtx, CG_SOURCE, path, cgVProf, "main_vertex", 0);
if (cgFPrg == NULL || cgVPrg == NULL)
cgSFPrg = cgCreateProgram(cgCtx, CG_SOURCE, stock_cg_program, cgFProf, "main_fragment", 0);
cgSVPrg = cgCreateProgram(cgCtx, CG_SOURCE, stock_cg_program, cgVProf, "main_vertex", 0);
if (cgFPrg == NULL || cgVPrg == NULL || cgSFPrg == NULL || cgSVPrg == NULL)
{
CGerror err = cgGetError();
SSNES_ERR("CG error: %s\n", cgGetErrorString(err));
@ -86,6 +124,8 @@ bool gl_cg_init(const char *path)
}
cgGLLoadProgram(cgFPrg);
cgGLLoadProgram(cgVPrg);
cgGLLoadProgram(cgSFPrg);
cgGLLoadProgram(cgSVPrg);
cgGLEnableProfile(cgFProf);
cgGLEnableProfile(cgVProf);
cgGLBindProgram(cgFPrg);
@ -99,6 +139,27 @@ bool gl_cg_init(const char *path)
cg_Voutput_size = cgGetNamedParameter(cgVPrg, "IN.output_size");
cg_mvp_matrix = cgGetNamedParameter(cgVPrg, "modelViewProj");
cgGLSetStateMatrixParameter(cg_mvp_matrix, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);
cg_mvp_matrix = cgGetNamedParameter(cgSVPrg, "modelViewProj");
cgGLSetStateMatrixParameter(cg_mvp_matrix, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);
cg_active = true;
return true;
}
void gl_cg_activate(void)
{
if (cg_active)
{
cgGLBindProgram(cgFPrg);
cgGLBindProgram(cgVPrg);
}
}
// Just load a dummy shader.
void gl_cg_deactivate(void)
{
if (cg_active)
{
cgGLBindProgram(cgSFPrg);
cgGLBindProgram(cgSVPrg);
}
}

View File

@ -31,4 +31,7 @@ void gl_cg_set_params(unsigned width, unsigned height,
unsigned tex_width, unsigned tex_height,
unsigned out_width, unsigned out_height);
void gl_cg_activate(void);
void gl_cg_deactivate(void);
#endif

View File

@ -279,3 +279,15 @@ void gl_glsl_set_params(unsigned width, unsigned height,
void gl_glsl_set_proj_matrix(void)
{}
void gl_glsl_activate(void)
{
if (glsl_enable)
pglUseProgram(gl_program);
}
void gl_glsl_deactivate(void)
{
if (glsl_enable)
pglUseProgram(0);
}

View File

@ -31,4 +31,7 @@ void gl_glsl_set_params(unsigned width, unsigned height,
unsigned tex_width, unsigned tex_height,
unsigned out_width, unsigned out_height);
void gl_glsl_activate(void);
void gl_glsl_deactivate(void);
#endif