source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/antialiasing.cg @ 3136

Revision 3136, 3.2 KB checked in by mattausch, 16 years ago (diff)

fixed antialiasing, but ssao not working at that point (depth)

Line 
1#include "../shaderenv.h"
2
3struct fragment
4{
5        float2 texCoords:  TEXCOORD0;
6};
7
8// the barrier for detecting a discontinuity (x = normal, y = depth)
9//const uniform float4 barrier = float4(5e-5f, 5e-5f, 0, 0);
10const uniform float4 barrier = float4(0.6f, 9e-2f, 0, 0);
11// the weights for normal / depth discontinuity (x = normal, y = depth)
12const uniform float4 weights = float4(0.5f, 0.5f, 1.0f, 1.0f);
13//uniform float4 weights = float4(1.0f, 1.0f, 1.0f, 1.0f);
14// the maximal size of the filter kernel
15const uniform float kernel = 0.5f;
16
17
18
19float4 main(fragment IN,
20                        uniform sampler2D colors,
21                        uniform sampler2D normals,
22                        uniform float2 offsets[8]): COLOR
23{
24        // center normal
25        const float3 centerNormal = normalize(tex2Dlod(normals, float4(IN.texCoords, 0, 0)).xyz);
26        // center color
27        const float4 centerColor = tex2Dlod(colors, float4(IN.texCoords, 0, 0));
28        // center depth
29        const float4 centerDepth = float4(centerColor.w);
30       
31        const float2 lt = IN.texCoords + offsets[0];
32        const float2 rb = IN.texCoords + offsets[1];
33        const float2 rt = IN.texCoords + offsets[2];
34        const float2 lb = IN.texCoords + offsets[3];
35
36        // compute normal discontinuities
37        float4 nd;
38
39        nd.x = dot(centerNormal, normalize(tex2Dlod(normals, float4(lt, 0, 0)).xyz));
40        nd.y = dot(centerNormal, normalize(tex2Dlod(normals, float4(rb, 0, 0)).xyz));
41        nd.z = dot(centerNormal, normalize(tex2Dlod(normals, float4(rt, 0, 0)).xyz));
42        nd.w = dot(centerNormal, normalize(tex2Dlod(normals, float4(lb, 0, 0)).xyz));
43
44        nd -= float4(barrier.x);
45        nd = step(float4(0.0f), nd);
46
47        // weight the influence of normal discontinuities
48        //const float ne = saturate(dot(nd, weights.x));
49        const float ne = nd.x * nd.y *nd.z * nd.w * weights.x;
50
51#if 1
52        ////////////
53        // depth filter => compute gradient difference
54        // (c - sample) + (c - opposite sample)
55
56        float depthVals[8];
57
58        for (int i = 0; i < 8; ++ i)
59        {       
60                depthVals[i] = (float)tex2Dlod(colors, float4(IN.texCoords + offsets[i], 0, 0)).w;
61        }
62
63        float4 dd; // the gradients
64
65        for (int i = 0; i < 4; ++ i)
66        {
67                dd = float4(depthVals[i * 2] + depthVals[i * 2 + 1]);
68        }
69
70        //dd = abs(2.0f * (1.0f - abs(1.0f - centerDepth / dd))) - barrier.y;
71        dd = abs(2.0f * centerDepth - dd) - barrier.y;
72        dd = step(dd, float4(0.0f));
73
74        const float de = saturate(dot(dd, weights.y));
75
76
77        ///////////////////////////
78
79        // weight: 0 = no antialiasing, 1 = full antialiasing
80        const float w = (1.0f - de * ne) * kernel.x;
81
82        // smoothed color: the offset is big where discontinuities are
83        // (a - c) * w + c = a * w + c * (1 - w)
84        const float2 width = IN.texCoords * (1.0f - w);
85
86        float4 s0 = tex2Dlod(colors, float4(width + lt * w, 0, 0));
87        float4 s1 = tex2Dlod(colors, float4(width + rb * w, 0, 0));
88        float4 s2 = tex2Dlod(colors, float4(width + rt * w, 0, 0));
89        float4 s3 = tex2Dlod(colors, float4(width + lb * w, 0, 0));
90#endif
91
92        //float4 col = (s0 + s1 + s2 + s3 + centerColor) * 0.2f;
93        //float4 col = (s0 + s1 + s2 + s3) * 0.25f;
94        float4 col = float4(ne.x, ne.x, ne.x, 0);
95        //float4 col = float4(de.x, de.x, de.x, 0);
96        //float4 col = float4(w, w, w, 0);
97        //float4 col = centerColor;
98        // push through the current depth
99        col.w = centerDepth.x;
100
101        return col;
102}
Note: See TracBrowser for help on using the repository browser.