From 4346339e4eada7712c66db7ed3050bd76d5f5254 Mon Sep 17 00:00:00 2001
From: chromaticpipe <chromaticpipe@gmail.com>
Date: Sat, 5 Apr 2025 12:57:09 -0500
Subject: [PATCH] Port MSAA and A2C from Kart birdhouse

See https://git.do.srb2.org/bird/Kart-Public/-/commit/6de9344875ff5d1b140cc4345cf155a15cc1102f
---
 src/hardware/hw_defs.h           |  1 +
 src/hardware/hw_main.c           |  9 +++++++++
 src/hardware/r_opengl/r_opengl.c |  9 +++++++++
 src/sdl/i_video.c                | 15 +++++++++++++++
 4 files changed, 34 insertions(+)

diff --git a/src/hardware/hw_defs.h b/src/hardware/hw_defs.h
index 77d7133b67..5295da653e 100644
--- a/src/hardware/hw_defs.h
+++ b/src/hardware/hw_defs.h
@@ -292,6 +292,7 @@ enum hwdsetspecialstate
 	HWD_SET_TEXTUREFILTERMODE,
 	HWD_SET_TEXTUREANISOTROPICMODE,
 	HWD_SET_WIREFRAME,
+	HWD_SET_MSAA,
 	HWD_NUMSTATE
 };
 
diff --git a/src/hardware/hw_main.c b/src/hardware/hw_main.c
index 69198a648a..faba050335 100644
--- a/src/hardware/hw_main.c
+++ b/src/hardware/hw_main.c
@@ -44,6 +44,7 @@
 #include "../r_translation.h"
 #include "../d_main.h"
 #include "../p_slopes.h"
+#include "../m_argv.h" // parm functions for msaa
 
 // ==========================================================================
 // the hardware driver object
@@ -5916,6 +5917,14 @@ void HWR_Startup(void)
 		HWR_SetShaderState();
 		HWR_LoadAllCustomShaders();
 		HWR_TogglePaletteRendering();
+
+		if (M_CheckParm("-msaa"))
+		{
+			if (M_CheckParm("-a2c"))
+				HWD.pfnSetSpecialState(HWD_SET_MSAA, 2);
+			else
+				HWD.pfnSetSpecialState(HWD_SET_MSAA, 1);
+		}
 	}
 
 	gl_init = true;
diff --git a/src/hardware/r_opengl/r_opengl.c b/src/hardware/r_opengl/r_opengl.c
index f2267dfacd..11dec6f90a 100644
--- a/src/hardware/r_opengl/r_opengl.c
+++ b/src/hardware/r_opengl/r_opengl.c
@@ -2277,6 +2277,15 @@ EXPORT void HWRAPI(SetSpecialState) (hwdspecialstate_t IdState, INT32 Value)
 			if (maximumAnisotropy)
 				Flush(); //??? if we want to change filter mode by texture, remove this
 			break;
+			
+		case HWD_SET_MSAA:
+			if (Value)
+			{
+				pglEnable(GL_MULTISAMPLE);
+				if (Value == 2)
+					pglEnable(GL_SAMPLE_ALPHA_TO_COVERAGE);
+			}
+			break;
 
 		case HWD_SET_WIREFRAME:
 			pglPolygonMode(GL_FRONT_AND_BACK, Value ? GL_LINE : GL_FILL);
diff --git a/src/sdl/i_video.c b/src/sdl/i_video.c
index 3e8b3a0b6b..981069c03b 100644
--- a/src/sdl/i_video.c
+++ b/src/sdl/i_video.c
@@ -98,6 +98,10 @@ consvar_t cv_vidwait = CVAR_INIT ("vid_wait", "On", "Synchronize framerate with
 static consvar_t cv_stretch = CVAR_INIT ("stretch", "Off", NULL, CV_SAVE|CV_NOSHOWHELP, CV_OnOff, NULL);
 static consvar_t cv_alwaysgrabmouse = CVAR_INIT ("alwaysgrabmouse", "Off", "If on, the game will never let go of the mouse, even when unused", CV_SAVE, CV_OnOff, NULL);
 
+// these cant be used since config is read after window creation, so need to use command line parameter instead
+//static CV_PossibleValue_t msaa_cons_t[] = {{0, "Off"}, {2, "2X"}, {4, "4X"}, {8, "8X"}, {16, "16X"}, {0, NULL}};
+//consvar_t cv_msaa = CVAR_INIT("msaa", "Off", "If on, MSAA antialiasing will be used, reducing jagged lines at the cost of performance", CV_SAVE, msaa_cons_t, NULL);
+
 UINT8 graphics_started = 0; // Is used in console.c and screen.c
 
 // To disable fullscreen at startup
@@ -1591,6 +1595,17 @@ static SDL_bool Impl_CreateWindow(SDL_bool fullscreen)
 #ifdef HWRENDER
 	flags |= SDL_WINDOW_OPENGL;
 
+	if (M_CheckParm("-msaa") && M_IsNextParm())
+	{
+		unsigned int value;
+		const char* str = M_GetNextParm();
+		if (sscanf(str, "%u", &value))
+		{
+			SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
+			SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, value);
+		}
+	}
+
 	// Without a 24-bit depth buffer many visuals are ruined by z-fighting.
 	// Some GPU drivers may give us a 16-bit depth buffer since the
 	// default value for SDL_GL_DEPTH_SIZE is 16.
-- 
GitLab