Browse Source

Added threshold value to the edge filter.

Lasse Öörni 14 years ago
parent
commit
3bb01cb0ed

+ 1 - 1
Engine/Graphics/Renderer.cpp

@@ -266,7 +266,7 @@ Renderer::Renderer(Context* context) :
     shadowMapHiresDepth_(false),
     shadowMapHiresDepth_(false),
     reuseShadowMaps_(true),
     reuseShadowMaps_(true),
     dynamicInstancing_(true),
     dynamicInstancing_(true),
-    edgeFilter_(EdgeFilterParameters(0.5f, 1.0f, 0.75f)),
+    edgeFilter_(EdgeFilterParameters(0.4f, 0.5f, 0.8f)),
     maxOccluderTriangles_(5000),
     maxOccluderTriangles_(5000),
     occlusionBufferSize_(256),
     occlusionBufferSize_(256),
     occluderSizeThreshold_(0.1f),
     occluderSizeThreshold_(0.1f),

+ 1 - 1
Engine/Graphics/View.cpp

@@ -889,7 +889,7 @@ void View::RenderBatchesDeferred()
         #endif
         #endif
         
         
         renderer_->DrawFullScreenQuad(*camera_, renderer_->GetVertexShader("Ambient"),
         renderer_->DrawFullScreenQuad(*camera_, renderer_->GetVertexShader("Ambient"),
-            renderer_->GetPixelShader(pixelShaderName), false, shaderParameters_);
+            renderer_->GetPixelShader("Ambient"), false, shaderParameters_);
         
         
         #ifdef USE_OPENGL
         #ifdef USE_OPENGL
         graphics_->SetStencilTest(false);
         graphics_->SetStencilTest(false);

+ 1 - 2
SourceAssets/GLSLShaders/Ambient.frag

@@ -17,8 +17,7 @@ void main()
         float linearDepth = ReconstructDepth(depth);
         float linearDepth = ReconstructDepth(depth);
     #endif
     #endif
 
 
-    // Store coarse linear depth to alpha channel for deferred antialiasing
-    gl_FragColor = vec4(ambientColor + GetFogFactor(linearDepth) * cFogColor, linearDepth);
+    gl_FragColor = vec4(ambientColor + GetFogFactor(linearDepth) * cFogColor, 1.0);
     // Copy the actual hardware depth value with slight bias to the destination depth buffer
     // Copy the actual hardware depth value with slight bias to the destination depth buffer
     gl_FragDepth = min(depth + 0.0000001, 1.0);
     gl_FragDepth = min(depth + 0.0000001, 1.0);
 }
 }

+ 29 - 24
SourceAssets/GLSLShaders/EdgeFilter.frag

@@ -36,33 +36,38 @@ void main()
     float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE)));
     float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE)));
     float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE)));
     float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE)));
 
 
-    vec2 dir;
-    dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE));
-    dir.y =  ((lumaNW + lumaSW) - (lumaNE + lumaSE));
+    if (((lumaMax - lumaMin) / lumaMin) >= cEdgeFilterParams.y)
+    {
+        vec2 dir;
+        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)) * cSampleOffsets.xy;
+        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)) * cSampleOffsets.xy;
 
 
-    dir *= cEdgeFilterParams.z;
+        dir *= cEdgeFilterParams.z;
 
 
-    vec3 rgbA = (1.0/2.0) * (
-        texture2D(sDiffBuffer, vScreenPos + dir * (1.0/3.0 - 0.5)).xyz +
-        texture2D(sDiffBuffer, vScreenPos + dir * (2.0/3.0 - 0.5)).xyz);
-    vec3 rgbB = rgbA * (1.0/2.0) + (1.0/4.0) * (
-        texture2D(sDiffBuffer, vScreenPos + dir * (0.0/3.0 - 0.5)).xyz +
-        texture2D(sDiffBuffer, vScreenPos + dir * (3.0/3.0 - 0.5)).xyz);
-    float lumaB = dot(rgbB, luma);
+        vec3 rgbA = (1.0/2.0) * (
+            texture2D(sDiffBuffer, vScreenPos + dir * (1.0/3.0 - 0.5)).xyz +
+            texture2D(sDiffBuffer, vScreenPos + dir * (2.0/3.0 - 0.5)).xyz);
+        vec3 rgbB = rgbA * (1.0/2.0) + (1.0/4.0) * (
+            texture2D(sDiffBuffer, vScreenPos + dir * (0.0/3.0 - 0.5)).xyz +
+            texture2D(sDiffBuffer, vScreenPos + dir * (3.0/3.0 - 0.5)).xyz);
+        float lumaB = dot(rgbB, luma);
+
+        vec3 rgbOut;
+        if((lumaB < lumaMin) || (lumaB > lumaMax))
+            rgbOut = rgbA;
+        else
+            rgbOut = rgbB;
     
     
-    vec3 rgbOut;
-    if((lumaB < lumaMin) || (lumaB > lumaMax))
-        rgbOut = rgbA;
+        gl_FragColor = vec4(rgbOut, 1.0);
+    }
     else
     else
-        rgbOut = rgbB;
-    
-    gl_FragColor = vec4(rgbOut, 1.0);
+        gl_FragColor = vec4(rgbM, 1.0);
 }
 }

+ 1 - 3
SourceAssets/HLSLShaders/Ambient.hlsl

@@ -17,9 +17,7 @@ void PS(float2 iScreenPos : TEXCOORD0,
 {
 {
     float4 diffInput = tex2D(sDiffBuffer, iScreenPos);
     float4 diffInput = tex2D(sDiffBuffer, iScreenPos);
     float depth = tex2D(sDepthBuffer, iScreenPos).r;
     float depth = tex2D(sDepthBuffer, iScreenPos).r;
-
     float3 ambientColor = cAmbientColor * diffInput.rgb;
     float3 ambientColor = cAmbientColor * diffInput.rgb;
 
 
-    // Store coarse depth to alpha channel for deferred antialiasing
-    oColor = float4(ambientColor + GetFogFactor(depth) * cFogColor, depth);
+    oColor = float4(ambientColor + GetFogFactor(depth) * cFogColor, 1.0);
 }
 }

+ 37 - 32
SourceAssets/HLSLShaders/EdgeFilter.hlsl

@@ -29,11 +29,11 @@ void PS(float2 iScreenPos : TEXCOORD0,
 
 
     float2 posOffset = cSampleOffsets.xy * cEdgeFilterParams.x;
     float2 posOffset = cSampleOffsets.xy * cEdgeFilterParams.x;
 
 
-    float3 rgbNW = tex2D(sDiffBuffer, iScreenPos + float2(-posOffset.x, -posOffset.y)).rgb;
-    float3 rgbNE = tex2D(sDiffBuffer, iScreenPos + float2(posOffset.x, -posOffset.y)).rgb;
-    float3 rgbSW = tex2D(sDiffBuffer, iScreenPos + float2(-posOffset.x, posOffset.y)).rgb;
-    float3 rgbSE = tex2D(sDiffBuffer, iScreenPos + float2(posOffset.x, posOffset.y)).rgb;
-    float3 rgbM  = tex2D(sDiffBuffer, iScreenPos).rgb;
+    float3 rgbNW = Sample(sDiffBuffer, iScreenPos + float2(-posOffset.x, -posOffset.y)).rgb;
+    float3 rgbNE = Sample(sDiffBuffer, iScreenPos + float2(posOffset.x, -posOffset.y)).rgb;
+    float3 rgbSW = Sample(sDiffBuffer, iScreenPos + float2(-posOffset.x, posOffset.y)).rgb;
+    float3 rgbSE = Sample(sDiffBuffer, iScreenPos + float2(posOffset.x, posOffset.y)).rgb;
+    float3 rgbM  = Sample(sDiffBuffer, iScreenPos).rgb;
 
 
     float3 luma = float3(0.299, 0.587, 0.114);
     float3 luma = float3(0.299, 0.587, 0.114);
     float lumaNW = dot(rgbNW, luma);
     float lumaNW = dot(rgbNW, luma);
@@ -45,33 +45,38 @@ void PS(float2 iScreenPos : TEXCOORD0,
     float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE)));
     float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE)));
     float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE)));
     float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE)));
 
 
-    float2 dir;
-    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(float2( FXAA_SPAN_MAX,  FXAA_SPAN_MAX),
-          max(float2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX),
-          dir * rcpDirMin)) * cSampleOffsets.xy;
-
-    dir *= cEdgeFilterParams.z;
-
-    float3 rgbA = (1.0/2.0) * (
-        tex2D(sDiffBuffer, iScreenPos + dir * (1.0/3.0 - 0.5)).xyz +
-        tex2D(sDiffBuffer, iScreenPos + dir * (2.0/3.0 - 0.5)).xyz);
-    float3 rgbB = rgbA * (1.0/2.0) + (1.0/4.0) * (
-        tex2D(sDiffBuffer, iScreenPos + dir * (0.0/3.0 - 0.5)).xyz +
-        tex2D(sDiffBuffer, iScreenPos + dir * (3.0/3.0 - 0.5)).xyz);
-    float lumaB = dot(rgbB, luma);
+    if (((lumaMax - lumaMin) / lumaMin) >= cEdgeFilterParams.y)
+    {
+        float2 dir;
+        dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE));
+        dir.y =  ((lumaNW + lumaSW) - (lumaNE + lumaSE));
     
     
-    float3 rgbOut;
-    if((lumaB < lumaMin) || (lumaB > lumaMax))
-        rgbOut = rgbA;
-    else
-        rgbOut = rgbB;
+        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(float2( FXAA_SPAN_MAX,  FXAA_SPAN_MAX),
+              max(float2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX),
+              dir * rcpDirMin)) * cSampleOffsets.xy;
     
     
-    oColor = float4(rgbOut, 1.0);
+        dir *= cEdgeFilterParams.z;
+    
+        float3 rgbA = (1.0/2.0) * (
+            Sample(sDiffBuffer, iScreenPos + dir * (1.0/3.0 - 0.5)).xyz +
+            Sample(sDiffBuffer, iScreenPos + dir * (2.0/3.0 - 0.5)).xyz);
+        float3 rgbB = rgbA * (1.0/2.0) + (1.0/4.0) * (
+            Sample(sDiffBuffer, iScreenPos + dir * (0.0/3.0 - 0.5)).xyz +
+            Sample(sDiffBuffer, iScreenPos + dir * (3.0/3.0 - 0.5)).xyz);
+        float lumaB = dot(rgbB, luma);
+        
+        float3 rgbOut;
+        if((lumaB < lumaMin) || (lumaB > lumaMax))
+            rgbOut = rgbA;
+        else
+            rgbOut = rgbB;
+
+        oColor = float4(rgbOut, 1.0);
+    }
+    else
+        oColor = float4(rgbM, 1.0);
 }
 }