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

Revision 2992, 3.1 KB checked in by mattausch, 16 years ago (diff)

tone mapping working with using reconstructed depth

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
22float4 DownSample(frag IN,
23                                  uniform sampler2D colors,
24                                  float2 downSampleOffs[16]) : COLOR
25{   
26    float4 average = .0f;
27
28    for(int i = 0; i < 16; ++ i)
29    {
30        average += tex2D(colors, IN.texCoord + downSampleOffs[i]);
31    }
32       
33    average *= 1.0f / 16.0f;
34
35    return average;
36}
37   
38
39float4 GreyScaleDownSample(frag IN,
40                                                   uniform sampler2D colors
41                                                   ): COLOR
42{
43
44        // Compute the average of the 4 necessary samples
45        float average = .0f;
46        float maximum = .0f;
47
48        // the rgb weights
49        const float3 w = float3(0.299f, 0.587f, 0.114f);
50        //float3 w = float3(0.2125f, 0.7154f, 0.0721f);
51
52        float4 cols[4];
53
54        cols[0] = tex2D(colors, IN.lt);
55        cols[1] = tex2D(colors, IN.lb);
56        cols[2] = tex2D(colors, IN.rt);
57        cols[3] = tex2D(colors, IN.rb);
58
59        for (int i = 0; i < 4; ++ i)
60        {
61                const float intensity = dot(cols[i].rgb, w);
62
63                maximum = max(maximum, intensity);
64                average += log(1e-5f + intensity);
65        }
66
67        average = exp(average * 0.25f);
68
69        float4 hcols = cols[0] + cols[1] + cols[2] + cols[3];
70
71        return hcols * 0.25f;
72        //return float4(average, maximum, 0.0f, cols[0].w);
73
74        // Output the luminance to the render target
75        //return float4(average, maximum, 0.0f, 1.0f);
76}
77   
78
79pixel ToneMap(frag IN,
80                          uniform sampler2D colors,
81                          uniform float imageKey,
82                          uniform float whiteLum,
83                          uniform float middleGrey)
84{
85        pixel OUT;
86        float4 color = tex2D(colors, IN.texCoord);
87
88        const float pixLum = 0.2125f * color.x + 0.7154f * color.y + 0.0721f * color.z;
89       
90        // obtain new image key from highest mipmap level
91        float logLumScaled = tex2Dlod(colors, float4(.5f, .5f, 0, 99)).w;
92        float logLum = logLumScaled * LOGLUM_RANGE + MINLOGLUM;
93
94        float newImageKey = exp(logLum);
95
96        // adjust to middle gray
97        const float lum = middleGrey * pixLum / newImageKey;
98         
99        // map to range and calc burnout
100        const float scaleLum = lum * (1.0f + lum / whiteLum * whiteLum) / (1.0f + lum);
101
102        OUT.col = color * scaleLum / pixLum;
103        OUT.col.w = color.w;
104
105        return OUT;
106}
107
108
109pixel CalcAvgLogLum(frag IN, uniform sampler2D colors)
110{
111        ////////////
112        //-- write out logaritmic luminance for tone mapping
113
114        pixel OUT;
115
116        float4 color = tex2Dlod(colors, float4(IN.texCoord.xy, 0, 0));
117
118        OUT.col = color;
119
120        // the old loglum is stored in the hightest mipmap-level
121        float oldLogLum = tex2Dlod(colors, float4(IN.texCoord.xy, 0, MAX_LOD_LEVEL)).w;
122
123        // the intensity weights
124        const float3 w = float3(0.299f, 0.587f, 0.114f);
125
126        float lum = dot(color.rgb, w);
127        float logLum = log(1e-5f + lum);
128
129        float logLumOffset = MINLOGLUM * INV_LOGLUM_RANGE;
130        float logLumScaled = logLum * INV_LOGLUM_RANGE - logLumOffset;
131
132        if (oldLogLum > 0)
133                OUT.col.w = lerp(oldLogLum, logLumScaled, 0.1f);
134        else
135                OUT.col.w = logLumScaled;
136
137        return OUT;
138}
Note: See TracBrowser for help on using the repository browser.