]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - shader_glsl.h
Patch from graphitemaster adding r_fxaa cvar, this enables the popular pseudo-antiali...
[xonotic/darkplaces.git] / shader_glsl.h
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",