]> git.rm.cloudns.org Git - xonotic/darkplaces.git/commitdiff
Patch from graphitemaster adding r_fxaa cvar, this enables the popular pseudo-antiali...
authorhavoc <havoc@d7cf8633-e32d-0410-b094-e92efae38249>
Sat, 30 Nov 2013 02:49:09 +0000 (02:49 +0000)
committerhavoc <havoc@d7cf8633-e32d-0410-b094-e92efae38249>
Sat, 30 Nov 2013 02:49:09 +0000 (02:49 +0000)
git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@12029 d7cf8633-e32d-0410-b094-e92efae38249

gl_rmain.c
model_brush.c
render.h
shader_glsl.h
shader_hlsl.h

index 81287ccb794e7826f03cb40b4cb003b6cba3eee9..338950aab2c805b3a621852cebfb6f48b8557685 100644 (file)
@@ -881,8 +881,9 @@ enum
        SHADERSTATICPARM_SHADOWSAMPLER = 10, ///< sampler
        SHADERSTATICPARM_CELSHADING = 11, ///< celshading (alternative diffuse and specular math)
        SHADERSTATICPARM_CELOUTLINES = 12, ///< celoutline (depth buffer analysis to produce outlines)
+       SHADERSTATICPARM_FXAA = 13 ///< fast approximate anti aliasing
 };
-#define SHADERSTATICPARMS_COUNT 13
+#define SHADERSTATICPARMS_COUNT 14
 
 static const char *shaderstaticparmstrings_list[SHADERSTATICPARMS_COUNT];
 static int shaderstaticparms_count = 0;
@@ -916,6 +917,8 @@ qboolean R_CompileShader_CheckStaticParms(void)
                if (r_glsl_postprocess_uservec4_enable.integer)
                        R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_POSTPROCESS_USERVEC4);
        }
+       if (r_fxaa.integer)
+               R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_FXAA);
        if (r_glsl_offsetmapping_lod.integer && r_glsl_offsetmapping_lod_distance.integer > 0)
                R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_OFFSETMAPPING_USELOD);
 
@@ -956,6 +959,7 @@ static void R_CompileShader_AddStaticParms(unsigned int mode, unsigned int permu
        R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_SHADOWSAMPLER, "USESHADOWSAMPLER");
        R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_CELSHADING, "USECELSHADING");
        R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_CELOUTLINES, "USECELOUTLINES");
+       R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_FXAA, "USEFXAA");
 }
 
 /// information about each possible shader permutation
index b8e64da458f37cbd8582ee13b863ee3013112289..eba42f526e937d9191ff8708d46d1a1677872b3f 100644 (file)
@@ -39,6 +39,7 @@ cvar_t r_subdivisions_collision_mintess = {0, "r_subdivisions_collision_mintess"
 cvar_t r_subdivisions_collision_maxtess = {0, "r_subdivisions_collision_maxtess", "1024", "maximum number of subdivisions (prevents curves beyond a certain detail level, limits smoothing)"};
 cvar_t r_subdivisions_collision_maxvertices = {0, "r_subdivisions_collision_maxvertices", "4225", "maximum vertices allowed per subdivided curve"};
 cvar_t r_trippy = {0, "r_trippy", "0", "easter egg"};
+cvar_t r_fxaa = {CVAR_SAVE, "r_fxaa", "0", "fast approximate anti aliasing"};
 cvar_t mod_noshader_default_offsetmapping = {CVAR_SAVE, "mod_noshader_default_offsetmapping", "1", "use offsetmapping by default on all surfaces that are not using q3 shader files"};
 cvar_t mod_obj_orientation = {0, "mod_obj_orientation", "1", "fix orientation of OBJ models to the usual conventions (if zero, use coordinates as is)"};
 cvar_t mod_q3bsp_curves_collisions = {0, "mod_q3bsp_curves_collisions", "1", "enables collisions with curves (SLOW)"};
@@ -83,6 +84,7 @@ void Mod_BrushInit(void)
        Cvar_RegisterVariable(&r_subdivisions_collision_maxtess);
        Cvar_RegisterVariable(&r_subdivisions_collision_maxvertices);
        Cvar_RegisterVariable(&r_trippy);
+       Cvar_RegisterVariable(&r_fxaa);
        Cvar_RegisterVariable(&mod_noshader_default_offsetmapping);
        Cvar_RegisterVariable(&mod_obj_orientation);
        Cvar_RegisterVariable(&mod_q3bsp_curves_collisions);
index 090e92bc60bd3aa0df59f4160254008e5f9fd40f..b91bde74c16f009d266cb8196c55087456916a49 100644 (file)
--- a/render.h
+++ b/render.h
@@ -55,6 +55,7 @@ extern cvar_t gl_flashblend;
 extern cvar_t r_novis;
 
 extern cvar_t r_trippy;
+extern cvar_t r_fxaa;
 
 extern cvar_t r_lerpsprites;
 extern cvar_t r_lerpmodels;
index b8a441b0a4c3b5fd96761ff80c69d2a316ad1d87..1c6cb906768149fce8126ff71705baf75a5ec494 100644 (file)
 "// uniform mediump vec4 UserVec4;\n",
 "// uniform highp float ClientTime;\n",
 "uniform mediump vec2 PixelSize;\n",
+"\n",
+"#ifdef USEFXAA\n",
+"// graphitemaster: based off the white paper by Timothy Lottes\n",
+"// http://developer.download.nvidia.com/assets/gamedev/files/sdk/11/FXAA_WhitePaper.pdf\n",
+"vec4 fxaa(vec4 inColor, float maxspan)\n",
+"{\n",
+"      vec4 ret = inColor; // preserve old\n",
+"      float mulreduct = 1.0/maxspan;\n",
+"      float minreduct = (1.0 / 128.0);\n",
+"\n",
+"      // directions\n",
+"      vec3 NW = texture2D(Texture_First, TexCoord1 + (vec2(-1.0, -1.0) * PixelSize)).xyz;\n",
+"      vec3 NE = texture2D(Texture_First, TexCoord1 + (vec2(+1.0, -1.0) * PixelSize)).xyz;\n",
+"      vec3 SW = texture2D(Texture_First, TexCoord1 + (vec2(-1.0, +1.0) * PixelSize)).xyz;\n",
+"      vec3 SE = texture2D(Texture_First, TexCoord1 + (vec2(+1.0, +1.0) * PixelSize)).xyz;\n",
+"      vec3 M = texture2D(Texture_First, TexCoord1).xyz;\n",
+"\n",
+"      // luminance directions\n",
+"      vec3 luma = vec3(0.299, 0.587, 0.114);\n",
+"      float lNW = dot(NW, luma);\n",
+"      float lNE = dot(NE, luma);\n",
+"      float lSW = dot(SW, luma);\n",
+"      float lSE = dot(SE, luma);\n",
+"      float lM = dot(M, luma);\n",
+"      float lMin = min(lM, min(min(lNW, lNE), min(lSW, lSE)));\n",
+"      float lMax = max(lM, max(max(lNW, lNE), max(lSW, lSE)));\n",
+"\n",
+"      // direction and reciprocal\n",
+"      vec2 dir = vec2(-((lNW + lNE) - (lSW + lSE)), ((lNW + lSW) - (lNE + lSE)));\n",
+"      float rcp = 1.0/(min(abs(dir.x), abs(dir.y)) + max((lNW + lNE + lSW + lSE) * (0.25 * mulreduct), minreduct));\n",
+"\n",
+"      // span\n",
+"      dir = min(vec2(maxspan, maxspan), max(vec2(-maxspan, -maxspan), dir * rcp)) * PixelSize;\n",
+"\n",
+"      vec3 rA = (1.0/2.0) * (\n",
+"              texture2D(Texture_First, TexCoord1 + dir * (1.0/3.0 - 0.5)).xyz +\n",
+"              texture2D(Texture_First, TexCoord1 + dir * (2.0/3.0 - 0.5)).xyz);\n",
+"      vec3 rB = rA * (1.0/2.0) + (1.0/4.0) * (\n",
+"              texture2D(Texture_First, TexCoord1 + dir * (0.0/3.0 - 0.5)).xyz +\n",
+"              texture2D(Texture_First, TexCoord1 + dir * (3.0/3.0 - 0.5)).xyz);\n",
+"      float lB = dot(rB, luma);\n",
+"\n",
+"      ret.xyz = ((lB < lMin) || (lB > lMax)) ? rA : rB;\n",
+"      ret.a = 1.0;\n",
+"      return ret;\n",
+"}\n",
+"#endif\n",
+"\n",
 "void main(void)\n",
 "{\n",
 "      dp_FragColor = dp_texture2D(Texture_First, TexCoord1);\n",
 "\n",
+"#ifdef USEFXAA\n",
+"      dp_FragColor = fxaa(dp_FragColor, 8.0); // 8.0 can be changed for larger span\n",
+"#endif\n",
+"\n",
 "#ifdef USEPOSTPROCESSING\n",
 "// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want\n",
 "// this code does a blur with the radius specified in the first component of r_glsl_postprocess_uservec1 and blends it using the second component\n",
index 7881aa6a4980f02cd7ea9a93d9fe480c48641809..e81e603990d4912e3bb1d7545f4c875eb5fcce64 100644 (file)
 "#ifdef USETRIPPY\n",
 "// LordHavoc: based on shader code linked at: http://www.youtube.com/watch?v=JpksyojwqzE\n",
 "// tweaked scale\n",
-"float4 TrippyVertex(float4 position)\n",
+"float4 TrippyVertex\n",
 "(\n",
-"uniform float ClientTime : register(c2)\n",
+"float4 position,\n",
+"float ClientTime\n",
 ")\n",
 "{\n",
 "      float worldTime = ClientTime;\n",
 "(\n",
 "float4 gl_Vertex : POSITION,\n",
 "uniform float4x4 ModelViewProjectionMatrix : register(c8),\n",
+"uniform float ClientTime : register(c2),\n",
 "out float4 gl_Position : POSITION,\n",
 "out float Depth : TEXCOORD0\n",
 ")\n",
 "{\n",
 "      gl_Position = mul(ModelViewProjectionMatrix, gl_Vertex);\n",
 "#ifdef USETRIPPY\n",
-"      gl_Position = TrippyVertex(gl_Position);\n",
+"      gl_Position = TrippyVertex(gl_Position, ClientTime);\n",
 "#endif\n",
 "      Depth = gl_Position.z;\n",
 "}\n",
 "float4 gl_MultiTexCoord0 : TEXCOORD0,\n",
 "float4 gl_MultiTexCoord4 : TEXCOORD4,\n",
 "out float4 gl_Position : POSITION,\n",
-"out float2 TexCoord1 : TEXCOORD0,\n",
-"out float2 TexCoord2 : TEXCOORD1\n",
+"out float2 TexCoord1 : TEXCOORD0\n",
+"#ifdef USEBLOOM\n",
+", out float2 TexCoord2 : TEXCOORD1\n",
+"#endif\n",
 ")\n",
 "{\n",
 "      gl_Position = mul(ModelViewProjectionMatrix, gl_Vertex);\n",
 "#endif\n",
 "\n",
 "#ifdef FRAGMENT_SHADER\n",
+"#ifdef USEFXAA\n",
+"// graphitemaster: based off the white paper by Timothy Lottes\n",
+"// http://developer.download.nvidia.com/assets/gamedev/files/sdk/11/FXAA_WhitePaper.pdf\n",
+"float4 fxaa\n",
+"(\n",
+"      float4 inColor,\n",
+"      float maxspan,\n",
+"      float2 TexCoord1,\n",
+"      uniform sampler Texture_First,\n",
+"      uniform float2 PixelSize\n",
+")\n",
+"{\n",
+"      float4 ret = inColor; // preserve old\n",
+"      float mulreduct = 1.0/maxspan;\n",
+"      float minreduct = (1.0 / 128.0);\n",
+"\n",
+"      float3 NW = tex2D(Texture_First, TexCoord1 + (float2(-1.0, -1.0) * PixelSize)).xyz;\n",
+"      float3 NE = tex2D(Texture_First, TexCoord1 + (float2(+1.0, -1.0) * PixelSize)).xyz;\n",
+"      float3 SW = tex2D(Texture_First, TexCoord1 + (float2(-1.0, +1.0) * PixelSize)).xyz;\n",
+"      float3 SE = tex2D(Texture_First, TexCoord1 + (float2(+1.0, +1.0) * PixelSize)).xyz;\n",
+"      float3 M = tex2D(Texture_First, TexCoord1).xyz;\n",
+"\n",
+"      // luminance directions\n",
+"      float3 luma = float3(0.299, 0.587, 0.114);\n",
+"      float lNW = dot(NW, luma);\n",
+"      float lNE = dot(NE, luma);\n",
+"      float lSW = dot(SW, luma);\n",
+"      float lSE = dot(SE, luma);\n",
+"      float lM = dot(M, luma);\n",
+"      float lMin = min(lM, min(min(lNW, lNE), min(lSW, lSE)));\n",
+"      float lMax = max(lM, max(max(lNW, lNE), max(lSW, lSE)));\n",
+"\n",
+"      // direction and reciprocal\n",
+"      float2 dir = float2(-((lNW + lNE) - (lSW + lSE)), ((lNW + lSW) - (lNE + lSE)));\n",
+"      float rcp = 1.0/(min(abs(dir.x), abs(dir.y)) + max((lNW + lNE + lSW + lSE) * (0.25 * mulreduct), minreduct));\n",
+"\n",
+"      // span\n",
+"      dir = min(float2(maxspan, maxspan), max(float2(-maxspan, -maxspan), dir * rcp)) * PixelSize;\n",
+"\n",
+"      float3 rA = (1.0/2.0) * (\n",
+"              tex2D(Texture_First, TexCoord1 + dir * (1.0/3.0 - 0.5)).xyz +\n",
+"              tex2D(Texture_First, TexCoord1 + dir * (2.0/3.0 - 0.5)).xyz);\n",
+"      float3 rB = rA * (1.0/2.0) + (1.0/4.0) * (\n",
+"              tex2D(Texture_First, TexCoord1 + dir * (0.0/3.0 - 0.5)).xyz +\n",
+"              tex2D(Texture_First, TexCoord1 + dir * (3.0/3.0 - 0.5)).xyz);\n",
+"      float lB = dot(rB, luma);\n",
+"\n",
+"      ret.xyz = ((lB < lMin) || (lB > lMax)) ? rA : rB;\n",
+"      ret.a = 1.0;\n",
+"      return ret;\n",
+"}\n",
+"#endif\n",
+"\n",
 "void main\n",
 "(\n",
 "float2 TexCoord1 : TEXCOORD0,\n",
+"#ifdef USEBLOOM\n",
 "float2 TexCoord2 : TEXCOORD1,\n",
+"#endif\n",
 "uniform sampler Texture_First : register(s0),\n",
 "#ifdef USEBLOOM\n",
 "uniform sampler Texture_Second : register(s1),\n",
 "{\n",
 "      dp_FragColor = tex2D(Texture_First, TexCoord1);\n",
 "\n",
+"#ifdef USEFXAA\n",
+"      dp_FragColor = fxaa(dp_FragColor, 8.0, TexCoord1, Texture_First, PixelSize); // 8.0 can be changed for larger span\n",
+"#endif\n",
+"\n",
 "#ifdef USEPOSTPROCESSING\n",
 "// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want\n",
 "// this code does a blur with the radius specified in the first component of r_glsl_postprocess_uservec1 and blends it using the second component\n",
 "float4 gl_Color : COLOR0,\n",
 "float4 gl_MultiTexCoord0 : TEXCOORD0,\n",
 "float4 gl_MultiTexCoord1 : TEXCOORD1,\n",
+"uniform float ClientTime : register(c2),\n",
 "out float4 gl_Position : POSITION,\n",
 "#ifdef USEDIFFUSE\n",
 "out float2 TexCoord1 : TEXCOORD0,\n",
 "#endif\n",
 "      gl_Position = mul(ModelViewProjectionMatrix, gl_Vertex);\n",
 "#ifdef USETRIPPY\n",
-"      gl_Position = TrippyVertex(gl_Position);\n",
+"      gl_Position = TrippyVertex(gl_Position, ClientTime);\n",
 "#endif\n",
 "}\n",
 "#endif\n",
 "float4 gl_MultiTexCoord0 : TEXCOORD0,\n",
 "uniform float4x4 TexMatrix : register(c0),\n",
 "uniform float3 EyePosition : register(c24),\n",
+"uniform float ClientTime : register(c2),\n",
 "#ifdef USEALPHAGENVERTEX\n",
 "out float4 gl_FrontColor : COLOR,\n",
 "#endif\n",
 "      gl_Position = mul(ModelViewProjectionMatrix, gl_Vertex);\n",
 "      ModelViewProjectionPosition = gl_Position;\n",
 "#ifdef USETRIPPY\n",
-"      gl_Position = TrippyVertex(gl_Position);\n",
+"      gl_Position = TrippyVertex(gl_Position, ClientTime);\n",
 "#endif\n",
 "}\n",
 "#endif\n",
 "float4 gl_MultiTexCoord3 : TEXCOORD3,\n",
 "uniform float4x4 TexMatrix : register(c0),\n",
 "uniform float3 EyePosition : register(c24),\n",
+"uniform float ClientTime : register(c2),\n",
 "#ifdef USEALPHAGENVERTEX\n",
 "out float4 gl_FrontColor : COLOR,\n",
 "#endif\n",
 "      gl_Position = mul(ModelViewProjectionMatrix, gl_Vertex);\n",
 "      ModelViewProjectionPosition = gl_Position;\n",
 "#ifdef USETRIPPY\n",
-"      gl_Position = TrippyVertex(gl_Position);\n",
+"      gl_Position = TrippyVertex(gl_Position, ClientTime);\n",
 "#endif\n",
 "}\n",
 "#endif\n",
 "#ifdef USEOFFSETMAPPING\n",
 "uniform float3 EyePosition : register(c24),\n",
 "#endif\n",
+"uniform float ClientTime : register(c2),\n",
 "out float4 gl_Position : POSITION,\n",
 "#ifdef USEVERTEXTEXTUREBLEND\n",
 "out float4 gl_FrontColor : COLOR,\n",
 "      VectorR.xyz = mul(ModelViewMatrix, float4(gl_MultiTexCoord3.xyz, 0)).xyz;\n",
 "      gl_Position = mul(ModelViewProjectionMatrix, gl_Vertex);\n",
 "#ifdef USETRIPPY\n",
-"      gl_Position = TrippyVertex(gl_Position);\n",
+"      gl_Position = TrippyVertex(gl_Position, ClientTime);\n",
 "#endif\n",
 "      VectorR.w = mul(ModelViewMatrix, gl_Vertex).z;\n",
 "}\n",
 "#ifdef USESHADOWMAPORTHO\n",
 "uniform float4x4 ShadowMapMatrix : register(c16),\n",
 "#endif\n",
+"uniform float ClientTime : register(c2),\n",
 "#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND) || defined(MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR) || defined(USEALPHAGENVERTEX)\n",
 "out float4 gl_FrontColor : COLOR,\n",
 "#endif\n",
 "      ModelViewProjectionPosition = gl_Position;\n",
 "#endif\n",
 "#ifdef USETRIPPY\n",
-"      gl_Position = TrippyVertex(gl_Position);\n",
+"      gl_Position = TrippyVertex(gl_Position, ClientTime);\n",
 "#endif\n",
 "}\n",
 "#endif // VERTEX_SHADER\n",