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

Revision 2974, 2.4 KB checked in by mattausch, 16 years ago (diff)
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
93        float logLumRange = MAXLOGLUM + MINLOGLUM;
94        //float logLumOffset = MINLOGLUM * logLumRange;
95        float logLum = logLumScaled * logLumRange - MINLOGLUM;
96
97        float newImageKey = exp(logLum);
98
99        // adjust to middle gray
100        const float lum = middleGrey * pixLum / newImageKey;
101         
102        // map to range and calc burnout
103        const float scaleLum = lum * (1.0f + lum / whiteLum * whiteLum) / (1.0f + lum);
104
105        OUT.col = color * scaleLum / pixLum;
106        OUT.col.w = color.w;
107
108        return OUT;
109}
Note: See TracBrowser for help on using the repository browser.