source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/tonemap.cg @ 3103

Revision 3103, 4.4 KB checked in by mattausch, 16 years ago (diff)

still some error with ssao on edges
bilateral filter slow

Line 
1#include "../shaderenv.h"
2
3
4struct frag
5{
6         // normalized screen position
7        float2 texCoord: TEXCOORD0;
8
9        float2 lt: TEXCOORD1; // left top
10        float2 rb: TEXCOORD2; // right bottom
11        float2 rt: TEXCOORD3; // right top
12        float2 lb: TEXCOORD4; // left bottom
13};
14
15
16struct pixel
17{
18        float4 col: COLOR0;
19};
20
21
22/*float4 DownSample(frag IN,
23                                  uniform sampler2D colors,
24                                  uniform float2 downSampleOffs[9]): COLOR
25{   
26    float4 average = .0f;
27
28        const float4 avgColor = tex2Dlod(colors, float4(IN.texCoord, 0, 0));
29
30        float j = 0;
31        float4 color;
32
33    for (int i = 0; i < 9; ++ i)
34    {
35                color = tex2Dlod(colors, float4(IN.texCoord + downSampleOffs[i], 0, 0));
36                if (abs(color.w - avgColor.w) < 1e-3f)
37                {
38                        average += color;
39                        ++ j;
40                }
41    }
42       
43    //average *= 1.0f / 9.0f;
44    average *= 1.0f / j;
45
46    return average;
47}*/
48 
49
50// let bilinear filtering do its work
51float4 DownSample(frag IN,
52                                  uniform sampler2D colors,
53                                  uniform float2 downSampleOffs[NUM_DOWNSAMPLES]): COLOR
54{   
55        return tex2Dlod(colors, float4(IN.texCoord, 0, 0));
56/*
57    float4 average = .0f;
58
59    for (int i = 0; i < NUM_DOWNSAMPLES; ++ i)
60    {
61                average += tex2Dlod(colors, float4(IN.texCoord + downSampleOffs[i], 0, 0));
62    }
63       
64        average *= 1.0f / (float)NUM_DOWNSAMPLES;
65
66    return average;*/
67}
68
69
70/** Does the first downsampling step and on the same time calculates the
71        intensity.
72*/
73
74float4 GreyScaleDownSample(frag IN,
75                                                   uniform sampler2D colors,
76                                                   uniform float2 downSampleOffs[4]
77                                                   ): COLOR
78{
79
80        // Compute the average of the 4 necessary samples
81        float average = .0f;
82        float maximum = .0f;
83
84        // the rgb weights
85        const float3 w = float3(0.299f, 0.587f, 0.114f);
86        //float3 w = float3(0.2125f, 0.7154f, 0.0721f);
87
88        float4 color;
89
90        for (int i = 0; i < 4; ++ i)
91        {
92                color = tex2D(colors, downSampleOffs[i]);
93                const float intensity = dot(cols[i].rgb, w);
94
95                maximum = max(maximum, intensity);
96                average += log(1e-5f + intensity);
97        }
98
99        average *= 0.25f;
100
101        // Output the luminance to the render target
102        return float4(average, maximum, 0.0f, 1.0f);
103}
104   
105
106/** Used for downsampling the tone map parameters (average loglum, maximum)
107        to the next lower level. This has to be applied until there is only
108        a 1x1 texture which holds the required numbers.
109*/
110float4 DownSampleForToneMapping(frag IN,
111                                                                uniform sampler2D colors,
112                                                                uniform float2 downSampleOffs[4]): COLOR
113{   
114        float average = .0f;
115        float maximum = .0f;
116       
117        float4 color;
118
119        for (int i = 0; i < 4; ++ i)
120        {
121                color = tex2D(colors, downSampleOffs[i]);
122
123                maximum = max(maximum, color.y);
124                average += color.x;
125        }
126
127        average *= 1.0f / (float)NUM_DOWNSAMPLES;
128
129        return float4(average, maximum, 0.0f, 1.0f);
130}
131
132
133
134pixel ToneMap(frag IN,
135                          uniform sampler2D colors,
136                          uniform float imageKey,
137                          uniform float whiteLum,
138                          uniform float middleGrey)
139{
140        pixel OUT;
141       
142        float4 color = tex2D(colors, IN.texCoord);
143
144        const float pixLum = 0.2125f * color.x + 0.7154f * color.y + 0.0721f * color.z;
145       
146        // obtain new image key from highest mipmap level
147        float logLumScaled = tex2Dlod(colors, float4(.5f, .5f, 0, MAX_LOD_LEVEL)).w;
148        float logLum = logLumScaled * LOGLUM_RANGE + MINLOGLUM;
149
150        float newImageKey = exp(logLum);
151
152        // adjust to middle gray
153        const float lum = middleGrey * pixLum / newImageKey;
154        // map to range and calc burnout
155        const float scaleLum = lum * (1.0f + lum / whiteLum * whiteLum) / (1.0f + lum);
156
157        OUT.col = color * scaleLum / pixLum;
158        OUT.col.w = color.w;
159
160        return OUT;
161}
162
163
164pixel CalcAvgLogLum(frag IN, uniform sampler2D colors)
165{
166        ////////////
167        //-- write out logaritmic luminance for tone mapping
168
169        pixel OUT;
170
171        const float4 color = tex2Dlod(colors, float4(IN.texCoord.xy, 0, 0));
172        OUT.col = color;
173
174        // the old loglum is stored in the hightest mipmap-level
175        float oldLogLum = tex2Dlod(colors, float4(IN.texCoord.xy, 0, MAX_LOD_LEVEL)).w;
176
177        // the intensity weights
178        const float3 w = float3(0.299f, 0.587f, 0.114f);
179
180        float lum = dot(color.rgb, w);
181        float logLum = log(1e-5f + lum);
182
183        float logLumOffset = MINLOGLUM * INV_LOGLUM_RANGE;
184        float logLumScaled = logLum * INV_LOGLUM_RANGE - logLumOffset;
185
186        // exponential smoothing of tone mapping over time
187        if (oldLogLum > 1e-5f) // loglum from last frame too bright
188                OUT.col.w = lerp(oldLogLum, logLumScaled, 0.1f);
189        else
190                OUT.col.w = logLumScaled;
191
192        return OUT;
193}
Note: See TracBrowser for help on using the repository browser.