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

Revision 2968, 3.3 KB checked in by mattausch, 16 years ago (diff)
Line 
1#include "../shaderenv.h"
2
3struct v2p
4{
5        float2 c:  TEXCOORD0; // center
6        float2 lt: TEXCOORD1; // left top
7        float2 rb: TEXCOORD2; // right bottom
8        float2 rt: TEXCOORD3; // right top
9        float2 lb: TEXCOORD4; // left bottom
10        float4 lr: TEXCOORD5; // left / right
11        float4 tb: TEXCOORD6; // top / bottom
12};
13
14// the barrier for detecting a discontinuity
15uniform float4 e_barrier = float4(5e-5, 5e-5, 0, 0); // x = normal, y = depth
16// the weights for normal / depth discontinuity
17uniform float4 e_weights = float4(0.5f, 0.5f, 1.0f, 1.0f); // x = normal, y = depth
18//uniform float4 e_weights = float4(1.0f, 1.0f, 1.0f, 1.0f); // x = normal, y = depth
19uniform float4 e_kernel = float4(0.5f, 1.0f, 1.0f, 1.0f);
20
21
22
23float3 ToneMap(float imageKey, float whiteLum, float3 color)
24{
25        float3 outCol;
26        const float pixLum = 0.2125f * color.x + 0.7154f * color.y + 0.0721f * color.z;
27       
28        // adjust to middle gray
29        const float lum = MIDDLE_GRAY * pixLum / imageKey;
30         
31        // map to range and calc burnout
32        const float burnedLum = lum * (1.0f + lum / whiteLum * whiteLum) / (1.0f + lum);
33       
34        outCol = color * burnedLum / pixLum;
35        //vColor.rgb += 0.6f * vBloom;
36       
37        return outCol;
38}
39
40
41float4 main(v2p IN,
42                        uniform sampler2D colors,
43                        uniform sampler2D normals,
44                        uniform float imageKey,
45                        uniform float whiteLum
46                        ): COLOR
47{
48        //return tex2D(colors, IN.c.xy);
49
50        // normal discontinuity filter
51        float3 nc = (float3)tex2D(normals, IN.c.xy);
52
53        float4 nd;
54        nd.x = dot(nc, float3(tex2D(normals, IN.lt.xy)));
55        nd.y = dot(nc, float3(tex2D(normals, IN.rb.xy)));
56        nd.z = dot(nc, float3(tex2D(normals, IN.rt.xy)));
57        nd.w = dot(nc, float3(tex2D(normals, IN.lb.xy)));
58
59        nd -= e_barrier.x;
60        nd = step((float4)0.0f, nd);
61
62        float ne = saturate(dot(nd, e_weights.x));
63
64        // construct opposite coordinates
65        float4 lrr = IN.lr.wzyx;
66        float4 tbr = IN.tb.wzyx;
67
68        // depth filter: compute gradient difference:
69        // (c - sample) + (c - opposite sample)
70
71        float4 dc = float4(tex2D(colors, IN.c).w);
72
73        float pos_lt = (float)tex2D(colors, IN.lt.xy).w;
74        float pos_rb = (float)tex2D(colors, IN.rb.xy).w;
75
76        float pos_lb = (float)tex2D(colors, IN.lb.xy).w;
77        float pos_rt = (float)tex2D(colors, IN.rt.xy).w;
78
79        float pos_l = (float)tex2D(colors, IN.lr.xy).w;
80        float pos_r = (float)tex2D(colors, lrr.xy).w;
81
82        float pos_t = (float)tex2D(colors, IN.tb.xy).w;
83        float pos_b = (float)tex2D(colors, tbr.xy).w;
84
85        float4 dd;
86
87        dd.x = pos_lt + pos_rb;
88        dd.y = pos_lb + pos_rt;
89        dd.z = pos_l + pos_r;
90        dd.w = pos_t + pos_b;
91
92        dd = abs(2.0f * dc - dd) - e_barrier.y;
93        dd = step(dd, (float4)0.0f);
94
95        float de = saturate(dot(dd, e_weights.y));
96
97        // weight: 0 = no aa, 1 = full antialiasing
98        float w = (1.0f - de * ne) * e_kernel.x;
99
100        // smoothed color
101        // (a - c) * w + c = a * w + c * (1 - w)
102        float2 offset = IN.c.xy * (1.0f - w);
103
104        float4 s0 = tex2Dlod(colors, float4(offset + IN.lt.xy * w, 0, 0));
105        float4 s1 = tex2Dlod(colors, float4(offset + IN.rb.xy * w, 0, 0));
106        float4 s2 = tex2Dlod(colors, float4(offset + IN.rt.xy * w, 0, 0));
107        float4 s3 = tex2Dlod(colors, float4(offset + IN.lb.xy * w, 0, 0));
108
109        float4 sc = tex2Dlod(colors, float4(IN.c.xy, 0, 0));
110
111        float4 col = (s0 + s1 + s2 + s3 + sc) * 0.2f;
112        //return (s0 + s1 + s2 + s3) * 0.25f;
113
114        //return float4(w);
115        col.xyz = ToneMap(imageKey, whiteLum, col.xyz);
116
117        return col;
118}
Note: See TracBrowser for help on using the repository browser.