Skip to content

Commit

Permalink
CLASSIC: Add FXAA support.
Browse files Browse the repository at this point in the history
  • Loading branch information
dsvensson committed Dec 12, 2024
1 parent c3b9331 commit 7253905
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 10 deletions.
10 changes: 10 additions & 0 deletions src/gl_framebuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1001,3 +1001,13 @@ void GL_FramebufferDeleteAll(void)
GL_FramebufferEnsureDeleted(i);
}
}

int GL_FramebufferFxaaPreset(void)
{
static const int fxaa_cvar_to_preset[18] = {
0, 10, 11, 12, 13, 14, 15, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 39
};
extern cvar_t vid_framebuffer_fxaa;

return fxaa_cvar_to_preset[bound(0, vid_framebuffer_fxaa.integer, 17)];
}
1 change: 1 addition & 0 deletions src/gl_framebuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ qbool GL_FramebufferEndWorldNormals(framebuffer_id id);

int GL_FramebufferMultisamples(framebuffer_id framebuffer);
void GL_FramebufferDeleteAll(void);
int GL_FramebufferFxaaPreset(void);

#define USE_FRAMEBUFFER_SCREEN 1
#define USE_FRAMEBUFFER_3DONLY 2
Expand Down
40 changes: 38 additions & 2 deletions src/glc_framebuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,21 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

#define POST_PROCESS_PALETTE 1
#define POST_PROCESS_3DONLY 2
#define POST_PROCESS_FXAA 4

static texture_ref non_framebuffer_screen_texture;

qbool GLC_CompilePostProcessProgram(void)
{
extern cvar_t vid_software_palette, vid_framebuffer;
int fxaa_preset = GL_FramebufferFxaaPreset();
int post_process_flags =
(vid_software_palette.integer ? POST_PROCESS_PALETTE : 0) |
(vid_framebuffer.integer == USE_FRAMEBUFFER_3DONLY ? POST_PROCESS_3DONLY : 0);
(vid_framebuffer.integer == USE_FRAMEBUFFER_3DONLY ? POST_PROCESS_3DONLY : 0) |
(fxaa_preset > 0 ? POST_PROCESS_FXAA : 0) | (fxaa_preset << 4); // mix in preset to detect change

if (R_ProgramRecompileNeeded(r_program_post_process_glc, post_process_flags)) {
static char included_definitions[512];
static char included_definitions[131072];

memset(included_definitions, 0, sizeof(included_definitions));
if (post_process_flags & POST_PROCESS_PALETTE) {
Expand All @@ -59,6 +62,39 @@ qbool GLC_CompilePostProcessProgram(void)
if (post_process_flags & POST_PROCESS_3DONLY) {
strlcat(included_definitions, "#define EZ_POSTPROCESS_OVERLAY\n", sizeof(included_definitions));
}
if (post_process_flags & POST_PROCESS_FXAA) {
qbool supported = true;
if (!SDL_GL_ExtensionSupported("GL_EXT_gpu_shader4"))
{
Com_Printf("WARNING: Missing GL_EXT_gpu_shader4, FXAA not available.");
supported = false;
}
#ifndef __APPLE__
if (!SDL_GL_ExtensionSupported("GL_ARB_gpu_shader5"))
{
Com_Printf("WARNING: Missing GL_ARB_gpu_shader5, FXAA not available.");
supported = false;
}
#endif
if (supported)
{
extern const unsigned char fxaa_h_glsl[];
char buffer[33];
const char *settings =
"#extension GL_EXT_gpu_shader4 : enable\n"
#ifndef __APPLE__
"#extension GL_ARB_gpu_shader5 : enable\n"
#endif
"#define EZ_POSTPROCESS_FXAA\n"
"#define FXAA_PC 1\n"
"#define FXAA_GLSL_120 1\n"
"#define FXAA_GREEN_AS_LUMA 1\n";
snprintf(buffer, sizeof(buffer), "#define FXAA_QUALITY__PRESET %d\n", fxaa_preset);
strlcat(included_definitions, settings, sizeof(included_definitions));
strlcat(included_definitions, buffer, sizeof(included_definitions));
strlcat(included_definitions, (const char *)fxaa_h_glsl, sizeof(included_definitions));
}
}

// Initialise program for drawing image
R_ProgramCompileWithInclude(r_program_post_process_glc, included_definitions);
Expand Down
9 changes: 2 additions & 7 deletions src/glm_framebuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#define POST_PROCESS_FXAA 8

extern cvar_t vid_software_palette, vid_framebuffer, vid_framebuffer_hdr, vid_framebuffer_hdr_tonemap, vid_framebuffer_multisample;
extern cvar_t vid_framebuffer_fxaa;
static texture_ref non_framebuffer_screen_texture;

qbool GLM_CompilePostProcessVAO(void)
Expand Down Expand Up @@ -82,19 +81,15 @@ qbool GLM_CompilePostProcessVAO(void)
return R_VertexArrayCreated(vao_postprocess);
}

static const int fxaa_cvar_to_preset[18] = {
0, 10, 11, 12, 13, 14, 15, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 39
};

// If this returns false then the framebuffer will be blitted instead
qbool GLM_CompilePostProcessProgram(void)
{
int fxaa_preset = fxaa_cvar_to_preset[bound(0, vid_framebuffer_fxaa.integer, 17)];
int fxaa_preset = GL_FramebufferFxaaPreset();
int post_process_flags =
(vid_software_palette.integer ? POST_PROCESS_PALETTE : 0) |
(vid_framebuffer.integer == USE_FRAMEBUFFER_3DONLY ? POST_PROCESS_3DONLY : 0) |
(vid_framebuffer_hdr.integer && vid_framebuffer_hdr_tonemap.integer ? POST_PROCESS_TONEMAP : 0) |
(vid_framebuffer_fxaa.integer ? POST_PROCESS_FXAA : 0) | (fxaa_preset << 4); // mix in preset to detect change
(fxaa_preset > 0 ? POST_PROCESS_FXAA : 0) | (fxaa_preset << 4); // mix in preset to detect change

if (R_ProgramRecompileNeeded(r_program_post_process, post_process_flags)) {
static char included_definitions[131072];
Expand Down
39 changes: 38 additions & 1 deletion src/glsl/glc/glc_post_process_screen.fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,46 @@ uniform float contrast;

varying vec2 TextureCoord;

vec4 sampleBase(void)
{
#ifdef EZ_POSTPROCESS_FXAA
// This is technically not the correct place to apply FXAA as it should sample
// a tonemapped, and gamma adjusted texture. However, the rendering pipeline
// prohibits this as this post_process_screen shader performs both tonemapping
// gamma, as well as combining HUD and world textures, so placing FXAA after
// this shader would apply FXAA to the HUD as well, which is detrimental.
// As it happens, applying FXAA to the world before tonemapping and gamma still
// looks ok, so it's still valuable without shuffling around shaders.
// Should world tonemap and gamma be broken out to a separate shader at some point
// that shader would ideally then also populate luma in the alpha channel, to be
// used by the FXAA algorithm instead of relying on green channel.
return FxaaPixelShader(
TextureCoord, // FxaaFloat2 pos,
FxaaFloat4(0.0f, 0.0f, 0.0f, 0.0f), // FxaaFloat4 fxaaConsolePosPos,
base, // FxaaTex tex,
base, // FxaaTex fxaaConsole360TexExpBiasNegOne,
base, // FxaaTex fxaaConsole360TexExpBiasNegTwo,
vec2(1.0/1280.0, 1.0/720.0), // FxaaFloat2 fxaaQualityRcpFrame,
FxaaFloat4(0.0f, 0.0f, 0.0f, 0.0f), // FxaaFloat4 fxaaConsoleRcpFrameOpt,
FxaaFloat4(0.0f, 0.0f, 0.0f, 0.0f), // FxaaFloat4 fxaaConsoleRcpFrameOpt2,
FxaaFloat4(0.0f, 0.0f, 0.0f, 0.0f), // FxaaFloat4 fxaaConsole360RcpFrameOpt2,
0.75f, // FxaaFloat fxaaQualitySubpix (default),
0.166f, // FxaaFloat fxaaQualityEdgeThreshold (default),
0.0f, // FxaaFloat fxaaQualityEdgeThresholdMin (default if green as luma),
0.0f, // FxaaFloat fxaaConsoleEdgeSharpness,
0.0f, // FxaaFloat fxaaConsoleEdgeThreshold,
0.0f, // FxaaFloat fxaaConsoleEdgeThresholdMin,
FxaaFloat4(0.0f, 0.0f, 0.0f, 0.0f) // FxaaFloat4 fxaaConsole360ConstDir,
);
#else
return texture2D(base, TextureCoord);
#endif
}


void main()
{
vec4 frag_colour = texture2D(base, TextureCoord);
vec4 frag_colour = sampleBase();

#ifdef EZ_POSTPROCESS_OVERLAY
vec4 add = texture2D(overlay, TextureCoord);
Expand Down

0 comments on commit 7253905

Please sign in to comment.