Skip to content

Commit

Permalink
Add FXAA support (r_fxaa 1)
Browse files Browse the repository at this point in the history
  • Loading branch information
viciious committed Aug 4, 2013
1 parent cb40c72 commit f9476dd
Show file tree
Hide file tree
Showing 10 changed files with 194 additions and 41 deletions.
82 changes: 82 additions & 0 deletions glsl/defaultFXAA.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// FXAA shader
// Original algorithm and code by Timothy Lottes

#define FXAA_SPAN_MAX 8.0
#define FXAA_REDUCE_MUL 1.0/8.0
#define FXAA_REDUCE_MIN 1.0/128.0

#include "include/uniforms.glsl"

varying vec2 v_TexCoord;

#ifdef VERTEX_SHADER

#include "include/attributes.glsl"
#include "include/vtransform.glsl"

void main(void)
{
gl_Position = u_ModelViewProjectionMatrix * a_Position;
v_TexCoord = a_TexCoord;
}

#endif // VERTEX_SHADER

#ifdef FRAGMENT_SHADER
// Fragment shader

uniform sampler2D u_BaseTexture;

void main(void)
{
#define Tex() texture2D(u_BaseTexture, v_TexCoord).rgb
#define TexOfs(ofs) texture2D(u_BaseTexture, v_TexCoord + ofs).rgb
#define TexOfsInv(ofs) texture2D(u_BaseTexture, v_TexCoord + ofs*invViewportSize).rgb

vec2 invViewportSize = vec2(1.0) / vec2(u_Viewport.zw);
myhalf3 rgbNW = TexOfsInv(vec2(-1.0,-1.0));
myhalf3 rgbNE = TexOfsInv(vec2(1.0,-1.0));
myhalf3 rgbSW = TexOfsInv(vec2(-1.0,1.0));
myhalf3 rgbSE = TexOfsInv(vec2(1.0,1.0));
myhalf3 rgbM = Tex();

myhalf3 luma = myhalf3(0.299, 0.587, 0.114);
myhalf lumaNW = dot(rgbNW, luma);
myhalf lumaNE = dot(rgbNE, luma);
myhalf lumaSW = dot(rgbSW, luma);
myhalf lumaSE = dot(rgbSE, luma);
myhalf lumaM = dot(rgbM, luma);

myhalf lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE)));
myhalf lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE)));

myhalf2 dir;
dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE));
dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE));

myhalf dirReduce = max(
(lumaNW + lumaNE + lumaSW + lumaSE) * (0.25 * FXAA_REDUCE_MUL),
FXAA_REDUCE_MIN);

myhalf rcpDirMin = 1.0/(min(abs(dir.x), abs(dir.y)) + dirReduce);

dir = min(myhalf2( FXAA_SPAN_MAX, FXAA_SPAN_MAX),
max(myhalf2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX),
dir * rcpDirMin)) * myhalf2(invViewportSize);

myhalf3 rgbA = (1.0/2.0) * (
TexOfs(dir * (1.0/3.0 - 0.5)) +
TexOfs(dir * (2.0/3.0 - 0.5)));
myhalf3 rgbB = rgbA * (1.0/2.0) + (1.0/4.0) * (
TexOfs(dir * (0.0/3.0 - 0.5)) +
TexOfs(dir * (3.0/3.0 - 0.5)));
myhalf lumaB = dot(rgbB, luma);

if((lumaB < lumaMin) || (lumaB > lumaMax)){
gl_FragColor.rgb = vec3(rgbA);
}else{
gl_FragColor.rgb = vec3(rgbB);
}
}

#endif // FRAGMENT_SHADER
30 changes: 30 additions & 0 deletions source/ref_gl/r_backend_program.c
Original file line number Diff line number Diff line change
Expand Up @@ -1571,6 +1571,33 @@ static void RB_RenderMeshGLSL_Fog( const shaderpass_t *pass, r_glslfeat_t progra
}
}

/*
* RB_RenderMeshGLSL_FXAA
*/
static void RB_RenderMeshGLSL_FXAA( const shaderpass_t *pass, r_glslfeat_t programFeatures )
{
int state;
int program;
mat4_t texMatrix = { 0 };

// set shaderpass state (blending, depthwrite, etc)
state = rb.currentShaderState | pass->flags;

RB_SetState( state );

RB_BindTexture( 0, pass->anim_frames[0] );

// update uniforms
program = RP_RegisterProgram( GLSL_PROGRAM_TYPE_FXAA, NULL,
rb.currentShader->name, rb.currentShader->deforms, rb.currentShader->numdeforms, programFeatures );
if( RB_BindProgram( program ) )
{
RB_UpdateCommonUniforms( program, pass, texMatrix );

RB_DrawElementsReal();
}
}

/*
* RB_RenderMeshGLSLProgrammed
*/
Expand Down Expand Up @@ -1616,6 +1643,9 @@ void RB_RenderMeshGLSLProgrammed( const shaderpass_t *pass, int programType )
case GLSL_PROGRAM_TYPE_FOG:
RB_RenderMeshGLSL_Fog( pass, features );
break;
case GLSL_PROGRAM_TYPE_FXAA:
RB_RenderMeshGLSL_FXAA( pass, features );
break;
default:
Com_DPrintf( S_COLOR_YELLOW "WARNING: Unknown GLSL program type %i\n", programType );
return;
Expand Down
16 changes: 16 additions & 0 deletions source/ref_gl/r_framebuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,22 @@ void R_DetachTextureFromFBObject( qboolean depth )
qglFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, FBO_ATTACHMENT(depth), GL_TEXTURE_2D, 0, 0 );
}

/*
* R_GetFBObjectTextureAttachment
*/
image_t *R_GetFBObjectTextureAttachment( int object, qboolean depth )
{
r_fbo_t *fbo;

assert( object > 0 && object <= r_num_framebuffer_objects );
if( object <= 0 || object > r_num_framebuffer_objects ) {
return;
}

fbo = r_framebuffer_objects + object - 1;
return depth ? fbo->depthTexture : fbo->colorTexture;
}

/*
* R_DisableFBObjectDrawBuffer
*/
Expand Down
18 changes: 11 additions & 7 deletions source/ref_gl/r_image.c
Original file line number Diff line number Diff line change
Expand Up @@ -2586,14 +2586,15 @@ static void R_InitStretchRawTexture( void )
*/
static void R_InitScreenTexturesPair( const char *name, image_t **color, image_t **depth )
{
R_InitViewportTexture( color, name, 0,
glConfig.width, glConfig.height, 0,
IT_NOFILTERING|IT_NOCOMPRESS|IT_NOPICMIP|IT_NOMIPMAP, 3 );

if( *color ) {
if( color ) {
R_InitViewportTexture( color, name, 0,
glConfig.width, glConfig.height, 0,
IT_NOCOMPRESS|IT_NOPICMIP|IT_NOMIPMAP, 3 );
}
if( depth && *color ) {
R_InitViewportTexture( depth, va( "%s_depth", name ), 0,
(*color)->upload_width, (*color)->upload_height, 0,
IT_NOFILTERING|IT_NOCOMPRESS|IT_NOPICMIP|IT_NOMIPMAP|IT_DEPTH|IT_CLAMP, 1 );
IT_NOCOMPRESS|IT_NOPICMIP|IT_NOMIPMAP|IT_DEPTH|IT_CLAMP, 1 );

R_AttachTextureToFBObject( (*color)->fbo, *depth );
}
Expand All @@ -2605,7 +2606,8 @@ static void R_InitScreenTexturesPair( const char *name, image_t **color, image_t
static void R_InitScreenTextures( void )
{
R_InitScreenTexturesPair( "r_screentex", &r_screentexture, &r_screendepthtexture );
R_InitScreenTexturesPair( "r_screentexcopy", &r_screentexturecopy, &r_screendepthtexturecopy );
R_InitScreenTexturesPair( "r_screentexcopy", &r_screentexturecopy, &r_screendepthtexturecopy );
R_InitScreenTexturesPair( "r_screenfxaacopy", &r_screenfxaacopy, NULL );
}

/*
Expand Down Expand Up @@ -2662,6 +2664,7 @@ static void R_TouchBuiltinTextures( void )
R_TouchImage( r_screendepthtexture );
R_TouchImage( r_screentexturecopy );
R_TouchImage( r_screendepthtexturecopy );
R_TouchImage( r_screenfxaacopy );
}

/*
Expand All @@ -2677,6 +2680,7 @@ static void R_ReleaseBuiltinTextures( void )
r_coronatexture = NULL;
r_screentexture = r_screendepthtexture = NULL;
r_screentexturecopy = r_screendepthtexturecopy = NULL;
r_screenfxaacopy = NULL;
}

//=======================================================
Expand Down
6 changes: 5 additions & 1 deletion source/ref_gl/r_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ extern image_t *r_screentexture;
extern image_t *r_screendepthtexture;
extern image_t *r_screentexturecopy;
extern image_t *r_screendepthtexturecopy;
extern image_t *r_screenfxaacopy;

extern unsigned int r_pvsframecount;
extern unsigned int r_framecount;
Expand Down Expand Up @@ -327,6 +328,8 @@ extern cvar_t *r_outlines_cutoff;
extern cvar_t *r_soft_particles;
extern cvar_t *r_soft_particles_scale;

extern cvar_t *r_fxaa;

extern cvar_t *r_lodbias;
extern cvar_t *r_lodscale;

Expand Down Expand Up @@ -417,6 +420,7 @@ void R_UseFBObject( int object );
int R_ActiveFBObject( void );
void R_AttachTextureToFBObject( int object, image_t *texture );
void R_DetachTextureFromFBObject( qboolean depth );
image_t *R_GetFBObjectTextureAttachment( int object, qboolean depth );
void R_DisableFBObjectDrawBuffer( void );
void R_CopyFBObject( int dest, int bitMask, int mode );
qboolean R_CheckFBObjectStatus( void );
Expand Down Expand Up @@ -488,7 +492,7 @@ void R_BeginStretchBatch( const shader_t *shader, float x_offset, float y_offse
void R_EndStretchBatch( void );
void R_DrawStretchPic( int x, int y, int w, int h, float s1, float t1, float s2, float t2, const vec4_t color, const shader_t *shader );
void R_DrawStretchRaw( int x, int y, int w, int h, int cols, int rows, qbyte *data );
void R_DrawStretchQuick( int x, int y, int w, int h, float s1, float t1, float s2, float t2, const vec4_t color, image_t *image );
void R_DrawStretchQuick( int x, int y, int w, int h, float s1, float t1, float s2, float t2, const vec4_t color, int program_type, image_t *image );

#define NUM_CUSTOMCOLORS 16
void R_InitCustomColors( void );
Expand Down
8 changes: 6 additions & 2 deletions source/ref_gl/r_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ image_t *r_screentexture;
image_t *r_screendepthtexture;
image_t *r_screentexturecopy;
image_t *r_screendepthtexturecopy;
image_t *r_screenfxaacopy;

unsigned int r_pvsframecount; // bumped when going to a new PVS
unsigned int r_framecount; // used for dlight push checking
Expand Down Expand Up @@ -767,7 +768,8 @@ void R_DrawRotatedStretchPic( int x, int y, int w, int h, float s1, float t1, fl
/*
* R_DrawStretchPic
*/
void R_DrawStretchPic( int x, int y, int w, int h, float s1, float t1, float s2, float t2, const vec4_t color, const shader_t *shader )
void R_DrawStretchPic( int x, int y, int w, int h, float s1, float t1, float s2, float t2,
const vec4_t color, const shader_t *shader )
{
R_DrawRotatedStretchPic( x, y, w, h, s1, t1, s2, t2, 0, color, shader );
}
Expand All @@ -785,7 +787,8 @@ void R_DrawStretchRaw( int x, int y, int w, int h, int cols, int rows, qbyte *da
/*
* R_DrawStretchImage
*/
void R_DrawStretchQuick( int x, int y, int w, int h, float s1, float t1, float s2, float t2, const vec4_t color, image_t *image )
void R_DrawStretchQuick( int x, int y, int w, int h, float s1, float t1, float s2, float t2,
const vec4_t color, int program_type, image_t *image )
{
static char *s_name = "$builtinimage";
static shaderpass_t p;
Expand All @@ -801,6 +804,7 @@ void R_DrawStretchQuick( int x, int y, int w, int h, float s1, float t1, float s
p.alphagen.type = ALPHA_GEN_IDENTITY;
p.tcgen = TC_GEN_BASE;
p.anim_frames[0] = image;
p.program_type = program_type;

R_DrawRotatedStretchPic( x, y, w, h, s1, t1, s2, t2, 0, color, &s );

Expand Down
36 changes: 9 additions & 27 deletions source/ref_gl/r_program.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,6 @@ static void RP_BindAttrbibutesLocations( glsl_program_t *program );
static void RP_PrecachePrograms( void );
static void RP_StorePrecacheList( void );

static const char *r_defaultMaterialProgram;
static const char *r_defaultDistortionGLSLProgram;
static const char *r_defaultRGBShadowGLSLProgram;
static const char *r_defaultShadowmapGLSLProgram;
static const char *r_defaultOutlineGLSLProgram;
static const char *r_defaultQ3AShaderProgram;
static const char *r_defaultCellshadeProgram;
static const char *r_defaultFogProgram;

/*
* RP_Init
*/
Expand All @@ -174,6 +165,7 @@ void RP_Init( void )
RP_RegisterProgram( GLSL_PROGRAM_TYPE_Q3A_SHADER, DEFAULT_GLSL_Q3A_SHADER_PROGRAM, NULL, NULL, 0, 0 );
RP_RegisterProgram( GLSL_PROGRAM_TYPE_CELLSHADE, DEFAULT_GLSL_CELLSHADE_PROGRAM, NULL, NULL, 0, 0 );
RP_RegisterProgram( GLSL_PROGRAM_TYPE_FOG, DEFAULT_GLSL_FOG_PROGRAM, NULL, NULL, 0, 0 );
RP_RegisterProgram( GLSL_PROGRAM_TYPE_FXAA, DEFAULT_GLSL_FXAA_PROGRAM, NULL, NULL, 0, 0 );

// check whether compilation of the shader with GPU skinning succeeds, if not, disable GPU bone transforms
if( glConfig.maxGLSLBones ) {
Expand Down Expand Up @@ -668,6 +660,11 @@ static const glsl_feature_t glsl_features_fog[] =
{ 0, NULL, NULL }
};

static const glsl_feature_t glsl_features_fxaa[] =
{
{ 0, NULL, NULL }
};

static const glsl_feature_t * const glsl_programtypes_features[] =
{
// GLSL_PROGRAM_TYPE_NONE
Expand All @@ -689,7 +686,9 @@ static const glsl_feature_t * const glsl_programtypes_features[] =
// GLSL_PROGRAM_TYPE_CELLSHADE
glsl_features_cellshade,
// GLSL_PROGRAM_TYPE_FOG
glsl_features_fog
glsl_features_fog,
// GLSL_PROGRAM_TYPE_FXAA
glsl_features_fxaa,
};

// ======================================================================================
Expand Down Expand Up @@ -1488,23 +1487,6 @@ void RP_ProgramList_f( void )
Com_Printf( "%i programs total\n", i );
}

/*
* RP_ProgramDump_f
*/
#define DUMP_PROGRAM(p) { int file; if( FS_FOpenFile( va("glsl/%s.glsl", #p), &file, FS_WRITE ) != -1 ) { FS_Print( file, r_##p ); FS_FCloseFile( file ); } }
void RP_ProgramDump_f( void )
{
DUMP_PROGRAM( defaultMaterialProgram );
DUMP_PROGRAM( defaultDistortionGLSLProgram );
DUMP_PROGRAM( defaultRGBShadowGLSLProgram );
DUMP_PROGRAM( defaultShadowmapGLSLProgram );
DUMP_PROGRAM( defaultOutlineGLSLProgram );
DUMP_PROGRAM( defaultQ3AShaderProgram );
DUMP_PROGRAM( defaultCellshadeProgram );
DUMP_PROGRAM( defaultFogProgram );
}
#undef DUMP_PROGRAM

/*
* RP_UpdateShaderUniforms
*/
Expand Down
3 changes: 2 additions & 1 deletion source/ref_gl/r_program.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ typedef quint64 r_glslfeat_t;
#define DEFAULT_GLSL_Q3A_SHADER_PROGRAM "defaultQ3AShader"
#define DEFAULT_GLSL_CELLSHADE_PROGRAM "defaultCellshade"
#define DEFAULT_GLSL_FOG_PROGRAM "defaultFog"
#define DEFAULT_GLSL_FXAA_PROGRAM "defaultFXAA"

// program types
enum
Expand All @@ -48,6 +49,7 @@ enum
GLSL_PROGRAM_TYPE_Q3A_SHADER,
GLSL_PROGRAM_TYPE_CELLSHADE,
GLSL_PROGRAM_TYPE_FOG,
GLSL_PROGRAM_TYPE_FXAA,

GLSL_PROGRAM_TYPE_MAXTYPE
};
Expand Down Expand Up @@ -163,7 +165,6 @@ void RP_Init( void );
void RP_Shutdown( void );

void RP_ProgramList_f( void );
void RP_ProgramDump_f( void );

int RP_FindProgram( const char *name );
int RP_RegisterProgram( int type, const char *name, const char *deformsKey,
Expand Down
5 changes: 4 additions & 1 deletion source/ref_gl/r_register.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ cvar_t *r_outlines_cutoff;
cvar_t *r_soft_particles;
cvar_t *r_soft_particles_scale;

cvar_t *r_fxaa;

cvar_t *r_lodbias;
cvar_t *r_lodscale;

Expand Down Expand Up @@ -685,6 +687,8 @@ void R_Register( void )
r_soft_particles = Cvar_Get( "r_soft_particles", "1", CVAR_ARCHIVE );
r_soft_particles_scale = Cvar_Get( "r_soft_particles_scale", "0.01", CVAR_ARCHIVE );

r_fxaa = Cvar_Get( "r_fxaa", "0", CVAR_ARCHIVE );

r_lodbias = Cvar_Get( "r_lodbias", "0", CVAR_ARCHIVE );
r_lodscale = Cvar_Get( "r_lodscale", "5.0", CVAR_ARCHIVE );

Expand Down Expand Up @@ -730,7 +734,6 @@ void R_Register( void )
Cmd_AddCommand( "modellist", Mod_Modellist_f );
Cmd_AddCommand( "gfxinfo", R_GfxInfo_f );
Cmd_AddCommand( "glslprogramlist", RP_ProgramList_f );
Cmd_AddCommand( "glslprogramdump", RP_ProgramDump_f );
}

/*
Expand Down

0 comments on commit f9476dd

Please sign in to comment.