Skip to content

Commit

Permalink
Merge pull request QW-Group#973 from qw-ctf/fxaaclassic
Browse files Browse the repository at this point in the history
CLASSIC: Reuse the FXAA support.
  • Loading branch information
dsvensson authored Dec 12, 2024
2 parents b4b5213 + 0b3acff commit 3176582
Show file tree
Hide file tree
Showing 12 changed files with 2,187 additions and 2,093 deletions.
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,15 @@ set(server
source_group(TREE ${SOURCE_DIR} PREFIX "Source Files/server" FILES ${server})
source_group(TREE ${SOURCE_DIR} PREFIX "Header Files/server" FILES ${server_headers})

if(RENDERER_MODERN_OPENGL OR RENDERER_CLASSIC_OPENGL)
set(SHARED_GLSL_DIR ${SOURCE_DIR}/glsl/shared)
add_resources(shaders_shared
${SHARED_GLSL_DIR}/fxaa.h.glsl
)
source_group(TREE ${SOURCE_DIR} PREFIX "Source Files/shared_opengl" FILES ${shared_opengl})
source_group(TREE ${SOURCE_DIR} PREFIX "Header Files/shared_opengl" FILES ${shared_opengl_headers})
endif()

if(RENDERER_MODERN_OPENGL)
set(MODERN_GLSL_DIR ${SOURCE_DIR}/glsl)
add_resources(shaders_modern
Expand Down Expand Up @@ -852,6 +861,7 @@ target_compile_definitions(ezquake PRIVATE
target_link_libraries(ezquake PRIVATE
$<$<BOOL:${RENDERER_MODERN_OPENGL}>:shaders_modern>
$<$<BOOL:${RENDERER_CLASSIC_OPENGL}>:shaders_classic>
shaders_shared
documentation
git_version

Expand Down
3 changes: 3 additions & 0 deletions cmake/ResourceCompiler.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ string(LENGTH "${content}" content_length)
math(EXPR data_length "${content_length} / 2")

string(REGEX REPLACE "([0-9a-f][0-9a-f])" "0x\\1," data "${content}")

string(APPEND data "0x00,")

file(WRITE "${output_file}" "const unsigned char ${variable_name}[] = {\n${data}\n};\nconst unsigned int ${variable_name}_len = ${data_length};\n")
11 changes: 11 additions & 0 deletions src/gl_framebuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ static qbool GL_FramebufferCreateRenderingTexture(framebuffer_data_t* fb, fbtex_
GL_TexStorage2D(fb->texture[tex_id], 1, framebuffer_format, width, height, false);
renderer.TextureLabelSet(fb->texture[tex_id], label);
renderer.TextureWrapModeClamp(fb->texture[tex_id]);
renderer.TextureSetFiltering(fb->texture[tex_id], texture_minification_nearest, texture_magnification_nearest);
R_TextureSetFlag(fb->texture[tex_id], R_TextureGetFlag(fb->texture[tex_id]) | TEX_NO_TEXTUREMODE);
return true;
}
Expand Down Expand Up @@ -1001,3 +1002,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
4 changes: 4 additions & 0 deletions src/gl_program.c
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ static r_program_uniform_t program_uniforms[] = {
{ r_program_post_process_glc, "v_blend", 1, false },
// r_program_uniform_post_process_glc_contrast,
{ r_program_post_process_glc, "contrast", 1, false },
// r_program_uniform_post_process_glc_r_inv_width,
{ r_program_post_process_glc, "r_inv_width", 1, false },
// r_program_uniform_post_process_glc_r_inv_height,
{ r_program_post_process_glc, "r_inv_height", 1, false },
// r_program_uniform_sky_glc_cameraPosition,
{ r_program_sky_glc, "cameraPosition", 1, false },
// r_program_uniform_sky_glc_speedscale,
Expand Down
42 changes: 40 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 Expand Up @@ -89,6 +125,8 @@ void GLC_RenderFramebuffers(void)
R_ProgramUniform1f(r_program_uniform_post_process_glc_gamma, v_gamma.value);
R_ProgramUniform4fv(r_program_uniform_post_process_glc_v_blend, blend_values);
R_ProgramUniform1f(r_program_uniform_post_process_glc_contrast, bound(1, v_contrast.value, 3));
R_ProgramUniform1f(r_program_uniform_post_process_glc_r_inv_width, 1.0f / (float)VID_ScaledWidth3D());
R_ProgramUniform1f(r_program_uniform_post_process_glc_r_inv_height, 1.0f / (float)VID_ScaledHeight3D());
R_ApplyRenderingState(r_state_default_2d);

if (flip2d && flip3d) {
Expand Down
20 changes: 11 additions & 9 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,22 +81,18 @@ 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[512];
static char included_definitions[131072];

memset(included_definitions, 0, sizeof(included_definitions));
if (post_process_flags & POST_PROCESS_PALETTE) {
Expand All @@ -110,10 +105,17 @@ qbool GLM_CompilePostProcessProgram(void)
strlcat(included_definitions, "#define EZ_POSTPROCESS_TONEMAP\n", sizeof(included_definitions));
}
if (post_process_flags & POST_PROCESS_FXAA) {
extern const unsigned char fxaa_h_glsl[];
char buffer[33];
const char *settings =
"#define EZ_POSTPROCESS_FXAA\n" \
"#define FXAA_PC 1\n" \
"#define FXAA_GLSL_130 1\n" \
"#define FXAA_GREEN_AS_LUMA 1\n";
snprintf(buffer, sizeof(buffer), "#define FXAA_QUALITY__PRESET %d\n", fxaa_preset);
strlcat(included_definitions, "#define EZ_POSTPROCESS_FXAA\n", sizeof(included_definitions));
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
Expand Down
41 changes: 40 additions & 1 deletion src/glsl/glc/glc_post_process_screen.fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,51 @@ uniform sampler2D overlay;
uniform float gamma;
uniform vec4 v_blend;
uniform float contrast;
uniform float r_inv_width;
uniform float r_inv_height;

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(r_inv_width, r_inv_height), // 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
Loading

0 comments on commit 3176582

Please sign in to comment.