From 0da9a3e3253e3673cba4d0c753273def79ff4e46 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Wed, 8 Jul 2015 14:31:07 +1000 Subject: [PATCH] Add new FXAA filter. --- DefaultPlugin/DefaultPlugin.cs | 1 + DefaultPlugin/DefaultPlugin.csproj | 1 + DefaultPlugin/Shaders/FXAAFilter.cs | 86 +++++++++++++++++++++++++++++ GraphicsAPI/PostProcessor.cs | 1 + 4 files changed, 89 insertions(+) create mode 100644 DefaultPlugin/Shaders/FXAAFilter.cs diff --git a/DefaultPlugin/DefaultPlugin.cs b/DefaultPlugin/DefaultPlugin.cs index 8b75b824f..c22afeb04 100644 --- a/DefaultPlugin/DefaultPlugin.cs +++ b/DefaultPlugin/DefaultPlugin.cs @@ -48,6 +48,7 @@ namespace DefaultPlugin { new PluginModule( PluginModuleType.MapRenderer, typeof( StandardMapRenderer ) ), new PluginModule( PluginModuleType.PostProcessingShader, typeof( GrayscaleFilter ) ), + new PluginModule( PluginModuleType.PostProcessingShader, typeof( FXAAFilter ) ), new PluginModule( PluginModuleType.NetworkProcessor, typeof( ClassicNetworkProcessor ) ), new PluginModule( PluginModuleType.BlockInfo, typeof( ClassicBlockInfo ) ), diff --git a/DefaultPlugin/DefaultPlugin.csproj b/DefaultPlugin/DefaultPlugin.csproj index f2fa5ab13..a8401073d 100644 --- a/DefaultPlugin/DefaultPlugin.csproj +++ b/DefaultPlugin/DefaultPlugin.csproj @@ -60,6 +60,7 @@ + diff --git a/DefaultPlugin/Shaders/FXAAFilter.cs b/DefaultPlugin/Shaders/FXAAFilter.cs new file mode 100644 index 000000000..57de429dc --- /dev/null +++ b/DefaultPlugin/Shaders/FXAAFilter.cs @@ -0,0 +1,86 @@ +using System; +using ClassicalSharp.GraphicsAPI; +using OpenTK; + +namespace DefaultPlugin.Shaders { + + public sealed class FXAAFilter : PostProcessingShader { + + public FXAAFilter() : base() { + FragmentSource = @" +#version 130 +in vec2 out_texcoords; +out vec4 final_colour; +uniform sampler2D texScene; +uniform vec2 invViewport; + +#define FXAA_REDUCE_MIN (1.0 / 128.0) +#define FXAA_REDUCE_MUL (1.0 / 8.0) +#define FXAA_SPAN_MAX 8.0 + +void main() { + vec2 fragCoord = out_texcoords / invViewport; + vec2 inverseVP = invViewport; + vec3 rgbNW = texture2D(texScene, (fragCoord + vec2(-1.0, -1.0)) * inverseVP).xyz; + vec3 rgbNE = texture2D(texScene, (fragCoord + vec2(1.0, -1.0)) * inverseVP).xyz; + vec3 rgbSW = texture2D(texScene, (fragCoord + vec2(-1.0, 1.0)) * inverseVP).xyz; + vec3 rgbSE = texture2D(texScene, (fragCoord + vec2(1.0, 1.0)) * inverseVP).xyz; + vec3 rgbM = texture2D(texScene, fragCoord * inverseVP).xyz; + + vec3 luma = vec3(0.299, 0.587, 0.114); + float lumaNW = dot(rgbNW, luma); + float lumaNE = dot(rgbNE, luma); + float lumaSW = dot(rgbSW, luma); + float lumaSE = dot(rgbSE, luma); + float lumaM = dot(rgbM, luma); + float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE))); + float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE))); + + vec2 dir = vec2( + dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE)); + dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE)); + + float dirReduce = max((lumaNW + lumaNE + lumaSW + lumaSE) * + (0.25 * FXAA_REDUCE_MUL), FXAA_REDUCE_MIN); + + float rcpDirMin = 1.0 / (min(abs(dir.x), abs(dir.y)) + dirReduce); + dir = min(vec2(FXAA_SPAN_MAX, FXAA_SPAN_MAX), + max(vec2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX), + dir * rcpDirMin)) * inverseVP; + + vec3 rgbA = 0.5 * ( + texture2D(texScene, fragCoord * inverseVP + dir * (1.0 / 3.0 - 0.5)).xyz + + texture2D(texScene, fragCoord * inverseVP + dir * (2.0 / 3.0 - 0.5)).xyz); + vec3 rgbB = rgbA * 0.5 + 0.25 * ( + texture2D(texScene, fragCoord * inverseVP + dir * -0.5).xyz + + texture2D(texScene, fragCoord * inverseVP + dir * 0.5).xyz); + + float lumaB = dot(rgbB, luma); + if ((lumaB < lumaMin) || (lumaB > lumaMax)) + final_colour = vec4(rgbA, 1.0); + else + final_colour = vec4(rgbB, 1.0); +} +"; + } + + int viewportLoc; + protected override void GetLocations() { + base.GetLocations(); + viewportLoc = GetUniformLocation( "invViewport" ); + } + + int width = 640, height = 480; + protected override void EnableVertexAttribStates( int stride ) { + base.EnableVertexAttribStates( stride ); + Vector2 invViewport = new Vector2( 1f / width, 1f / height ); + Console.WriteLine( invViewport ); + SetUniform( viewportLoc, ref invViewport ); + } + + protected override void RenderTargetResized( int newWidth, int newHeight ) { + width = newWidth; + height = newHeight; + } + } +} \ No newline at end of file diff --git a/GraphicsAPI/PostProcessor.cs b/GraphicsAPI/PostProcessor.cs index 72ecaf6fd..671704a63 100644 --- a/GraphicsAPI/PostProcessor.cs +++ b/GraphicsAPI/PostProcessor.cs @@ -31,6 +31,7 @@ namespace ClassicalSharp.GraphicsAPI { if( shader.ProgramId <= 0 ) { shader.Init( api ); } + shader.RenderTargetResized( window.Width, window.Height ); } void Resize( object sender, EventArgs e ) {