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

Revision 3125, 3.9 KB checked in by mattausch, 16 years ago (diff)

changed tm so no values out of bounds, somehow occlusion error missing again

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