#include "../shaderenv.h" struct fragment { float2 texCoords: TEXCOORD0; }; // the barrier for detecting a discontinuity (x = normal, y = depth) //const uniform float4 barrier = float4(5e-5f, 5e-5f, 0, 0); const uniform float4 barrier = float4(0.6f, 9e-2f, 0, 0); // the weights for normal / depth discontinuity (x = normal, y = depth) const uniform float4 weights = float4(0.5f, 0.5f, 1.0f, 1.0f); //uniform float4 weights = float4(1.0f, 1.0f, 1.0f, 1.0f); // the maximal size of the filter kernel const uniform float kernel = 0.5f; float4 main(fragment IN, uniform sampler2D colors, uniform sampler2D normals, uniform float2 offsets[8]): COLOR { // center normal const float3 centerNormal = normalize(tex2Dlod(normals, float4(IN.texCoords, 0, 0)).xyz); // center color const float4 centerColor = tex2Dlod(colors, float4(IN.texCoords, 0, 0)); // center depth const float4 centerDepth = float4(centerColor.w); const float2 lt = IN.texCoords + offsets[0]; const float2 rb = IN.texCoords + offsets[1]; const float2 rt = IN.texCoords + offsets[2]; const float2 lb = IN.texCoords + offsets[3]; // compute normal discontinuities float4 nd; nd.x = dot(centerNormal, normalize(tex2Dlod(normals, float4(lt, 0, 0)).xyz)); nd.y = dot(centerNormal, normalize(tex2Dlod(normals, float4(rb, 0, 0)).xyz)); nd.z = dot(centerNormal, normalize(tex2Dlod(normals, float4(rt, 0, 0)).xyz)); nd.w = dot(centerNormal, normalize(tex2Dlod(normals, float4(lb, 0, 0)).xyz)); nd -= float4(barrier.x); nd = step(float4(0.0f), nd); // weight the influence of normal discontinuities //const float ne = saturate(dot(nd, weights.x)); const float ne = nd.x * nd.y *nd.z * nd.w * weights.x; #if 1 //////////// // depth filter => compute gradient difference // (c - sample) + (c - opposite sample) float depthVals[8]; for (int i = 0; i < 8; ++ i) { depthVals[i] = (float)tex2Dlod(colors, float4(IN.texCoords + offsets[i], 0, 0)).w; } float4 dd; // the gradients for (int i = 0; i < 4; ++ i) { dd = float4(depthVals[i * 2] + depthVals[i * 2 + 1]); } //dd = abs(2.0f * (1.0f - abs(1.0f - centerDepth / dd))) - barrier.y; dd = abs(2.0f * centerDepth - dd) - barrier.y; dd = step(dd, float4(0.0f)); const float de = saturate(dot(dd, weights.y)); /////////////////////////// // weight: 0 = no antialiasing, 1 = full antialiasing const float w = (1.0f - de * ne) * kernel.x; // smoothed color: the offset is big where discontinuities are // (a - c) * w + c = a * w + c * (1 - w) const float2 width = IN.texCoords * (1.0f - w); float4 s0 = tex2Dlod(colors, float4(width + lt * w, 0, 0)); float4 s1 = tex2Dlod(colors, float4(width + rb * w, 0, 0)); float4 s2 = tex2Dlod(colors, float4(width + rt * w, 0, 0)); float4 s3 = tex2Dlod(colors, float4(width + lb * w, 0, 0)); #endif //float4 col = (s0 + s1 + s2 + s3 + centerColor) * 0.2f; //float4 col = (s0 + s1 + s2 + s3) * 0.25f; float4 col = float4(ne.x, ne.x, ne.x, 0); //float4 col = float4(de.x, de.x, de.x, 0); //float4 col = float4(w, w, w, 0); //float4 col = centerColor; // push through the current depth col.w = centerDepth.x; return col; }