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

Revision 2867, 2.3 KB checked in by mattausch, 16 years ago (diff)
Line 
1struct v2p
2{
3        float2 c:  TEXCOORD0; // center
4        float2 lt: TEXCOORD1; // left top
5        float2 rb: TEXCOORD2; // right bottom
6        float2 rt: TEXCOORD3; // right top
7        float2 lb: TEXCOORD4; // left bottom
8        float4 lr: TEXCOORD5; // left / right
9        float4 tb: TEXCOORD6; // top / bottom
10};
11
12// the barrier for detecting a discontinuity
13uniform float4 e_barrier = float4(5e-3, 5e-3, 0, 0); // x = normal, y = depth
14// the weights for normal / depth discontinuity
15uniform float4 e_weights = float4(1.0f, 1.0f, 1.0f, 1.0f); // x = normal, y = depth
16uniform float4 e_kernel = float4(0.35f, 1.0f, 1.0f, 1.0f);
17
18
19float4 main(v2p IN,
20                        uniform sampler2D colors,
21                        uniform sampler2D normals
22                        ): COLOR
23{
24        //return tex2D(colors, IN.c.xy);
25        // normal discontinuity filter
26        float3 nc = (float3)tex2D(normals, IN.c.xy);
27
28        float4 nd;
29        nd.x = dot(nc, float3(tex2D(normals, IN.lt.xy)));
30        nd.y = dot(nc, float3(tex2D(normals, IN.rb.xy)));
31        nd.z = dot(nc, float3(tex2D(normals, IN.rt.xy)));
32        nd.w = dot(nc, float3(tex2D(normals, IN.lb.xy)));
33
34        nd -= e_barrier.x;
35        nd = step(0.0f, nd);
36
37        float ne = saturate(dot(nd, e_weights.x));
38
39        // opposite coordinates
40        float4 lrr = IN.lr.wzyx;
41        float4 tbr = IN.tb.wzyx;
42
43        // depth filter: compute gradient difference:
44        // (c - sample) + (c - opposite sample)
45
46        float4 dc = float4(tex2D(colors, IN.c).w);
47
48        float pos_lt = (float)tex2D(colors, IN.lt.xy).w;
49        float pos_rb = (float)tex2D(colors, IN.rb.xy).w;
50
51        float pos_lb = (float)tex2D(colors, IN.lb.xy).w;
52        float pos_rt = (float)tex2D(colors, IN.rt.xy).w;
53
54        float pos_l = (float)tex2D(colors, IN.lr.xy).w;
55        float pos_r = (float)tex2D(colors, lrr.xy).w;
56
57        float pos_t = (float)tex2D(colors, IN.tb.xy).w;
58        float pos_b = (float)tex2D(colors, tbr.xy).w;
59
60        float4 dd;
61
62        dd.x = pos_lt + pos_rb;
63        dd.y = pos_lb + pos_rt;
64        dd.z = pos_l + pos_r;
65        dd.w = pos_t + pos_b;
66
67        dd = abs(2.0f * dc - dd) - e_barrier.y;
68        dd = step(dd, 0.0f);
69
70        float de = saturate(dot(dd, e_weights.y));
71
72        // weight: 0 = no aa, 1 = full antialiasing
73        float w = (1.0f - de * ne) * e_kernel.x;
74
75        // smoothed color
76        // (a - c) * w + c = a * w + c * (1 - w)
77        float2 offset = IN.c.xy * (1.0f - w);
78
79        float4 s0 = tex2D(colors, offset + IN.lt.xy * w);
80        float4 s1 = tex2D(colors, offset + IN.rb.xy * w);
81        float4 s2 = tex2D(colors, offset + IN.rt.xy * w);
82        float4 s3 = tex2D(colors, offset + IN.lb.xy * w);
83
84        return (s0 + s1 + s2 + s3) / 4.0f;
85}
Note: See TracBrowser for help on using the repository browser.