Skip to content

Commit

Permalink
GLM: Add FXAA option.
Browse files Browse the repository at this point in the history
Default disabled, ideally used with vid_framebuffer 2 to
avoid adding AA to HUD.

NVIDIA FXAA Shader v3.11 BSD license, imported verbatim.
  • Loading branch information
dsvensson committed Oct 28, 2024
1 parent c3b9c43 commit dac5f85
Show file tree
Hide file tree
Showing 8 changed files with 2,199 additions and 6 deletions.
80 changes: 80 additions & 0 deletions help_variables.json
Original file line number Diff line number Diff line change
Expand Up @@ -21781,6 +21781,86 @@
"remarks": "Setting vid_framebuffer_scale overrides custom FBO width/height.",
"type": "integer"
},
"vid_framebuffer_fxaa": {
"default": "0",
"desc": "Fast Approximate Anti-Aliasing. Enable vid_framebuffer 2 to avoid filtering HUD. Typically between 1-6.",
"group-id": "52",
"type": "integer",
"values": [
{
"description": "Disabled.",
"name": "0"
},
{
"description": "Medium dither, quality 1 (fastest).",
"name": "1"
},
{
"description": "Medium dither, quality 2.",
"name": "2"
},
{
"description": "Medium dither, quality 3.",
"name": "3"
},
{
"description": "Medium dither, quality 4.",
"name": "4"
},
{
"description": "Medium dither, quality 5.",
"name": "5"
},
{
"description": "Medium dither, quality 6 (highest quality).",
"name": "6"
},
{
"description": "Less dither, quality 1 (fastest).",
"name": "7"
},
{
"description": "Less dither, quality 2.",
"name": "8"
},
{
"description": "Less dither, quality 3.",
"name": "9"
},
{
"description": "Less dither, quality 4.",
"name": "10"
},
{
"description": "Less dither, quality 5.",
"name": "11"
},
{
"description": "Less dither, quality 6.",
"name": "12"
},
{
"description": "Less dither, quality 7.",
"name": "13"
},
{
"description": "Less dither, quality 8.",
"name": "14"
},
{
"description": "Less dither, quality 9.",
"name": "15"
},
{
"description": "Less dither, quality 10 (highest quality).",
"name": "16"
},
{
"description": "No dither (expensive).",
"name": "17"
}
]
},
"vid_fullscreen": {
"default": "1",
"desc": "Toggles between fullscreen and windowed mode.",
Expand Down
16 changes: 15 additions & 1 deletion src/glm_framebuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ 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_TONEMAP 4
#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 @@ -80,13 +82,19 @@ 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 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_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

if (R_ProgramRecompileNeeded(r_program_post_process, post_process_flags)) {
static char included_definitions[512];
Expand All @@ -101,6 +109,12 @@ qbool GLM_CompilePostProcessProgram(void)
if (post_process_flags & POST_PROCESS_TONEMAP) {
strlcat(included_definitions, "#define EZ_POSTPROCESS_TONEMAP\n", sizeof(included_definitions));
}
if (post_process_flags & POST_PROCESS_FXAA) {
char buffer[33];
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, buffer, sizeof(included_definitions));
}

// Initialise program for drawing image
R_ProgramCompileWithInclude(r_program_post_process, included_definitions);
Expand Down
4 changes: 2 additions & 2 deletions src/glm_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ typedef struct uniform_block_frame_constants_s {
// [4-byte break]
int r_width;
int r_height;
float r_inv_width;
float r_inv_height;
float r_zFar;
float r_zNear;

Expand All @@ -73,8 +75,6 @@ typedef struct uniform_block_frame_constants_s {
// camangles [0]

float camangles[3]; // [1] [2]
float padding[2];

} uniform_block_frame_constants_t;

#define MAX_WORLDMODEL_BATCH 64
Expand Down
2 changes: 2 additions & 0 deletions src/glm_misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ void GLM_PreRenderView(void)
// Window constants
frameConstants.r_width = VID_ScaledWidth3D();
frameConstants.r_height = VID_ScaledHeight3D();
frameConstants.r_inv_width = 1.0f / (float) frameConstants.r_width;
frameConstants.r_inv_height = 1.0f / (float) frameConstants.r_height;
frameConstants.r_zFar = R_FarPlaneZ();
frameConstants.r_zNear = R_NearPlaneZ();

Expand Down
4 changes: 2 additions & 2 deletions src/glsl/common.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ layout(std140, binding=EZQ_GL_BINDINGPOINT_FRAMECONSTANTS) uniform GlobalState {
// [4-byte break]
int r_width;
int r_height;
float r_inv_width;
float r_inv_height;
float r_zFar;
float r_zNear;

Expand All @@ -70,8 +72,6 @@ layout(std140, binding=EZQ_GL_BINDINGPOINT_FRAMECONSTANTS) uniform GlobalState {
// camAngles.x

vec3 camAngles; // camAngles.yz
float padding1;
float padding2;
};

struct WorldDrawInfo {
Expand Down
Loading

0 comments on commit dac5f85

Please sign in to comment.