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

Revision 2973, 2.1 KB checked in by mattausch, 16 years ago (diff)
Line 
1#include "../shaderenv.h"
2
3
4struct fragment
5{
6         // normalized screen position
7        float4 pos: WPOS;
8        float4 texCoord: TEXCOORD0;
9
10        float2 lt: TEXCOORD1; // left top
11        float2 rb: TEXCOORD2; // right bottom
12        float2 rt: TEXCOORD3; // right top
13        float2 lb: TEXCOORD4; // left bottom
14};
15
16
17struct pixel
18{
19        float4 col: COLOR0;
20};
21
22
23float4 DownSample(fragment IN,
24                                  uniform sampler2D colors,
25                                  float2 downSampleOffs[16]) : COLOR
26{   
27    float4 average = .0f;
28
29    for(int i = 0; i < 16; ++ i)
30    {
31        average += tex2D(colors, IN.texCoord.xy + downSampleOffs[i]);
32    }
33       
34    average *= 1.0f / 16.0f;
35
36    return average;
37}
38   
39
40float4 GreyScaleDownSample(fragment IN,
41                                                   uniform sampler2D colors): COLOR
42{
43
44        // Compute the average of the 4 necessary samples
45        float average = 0.0f;
46        float maximum = -1e20;
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.texCoord.xy + IN.lt.xy);
55        cols[1] = tex2D(colors, IN.texCoord.xy + IN.lb.xy);
56        cols[2] = tex2D(colors, IN.texCoord.xy + IN.rt.xy);
57        cols[3] = tex2D(colors, IN.texCoord.xy + IN.rb.xy);
58
59
60        for (int i = 0; i < 4; ++ i)
61        {
62                const float intensity = dot(cols[i].rgb, w);
63
64                maximum = max(maximum, intensity);
65                average += (0.25f * log(1e-5f + intensity));
66        }
67
68        average = exp(average);
69
70        return tex2D(colors, IN.texCoord.xy);
71
72        // Output the luminance to the render target
73        return float4(average, maximum, 0.0f, 1.0f);
74}
75   
76
77pixel ToneMap(fragment IN,
78                          uniform sampler2D colors,
79                          uniform float imageKey,
80                          uniform float whiteLum,
81                          uniform float middleGrey)
82{
83        pixel OUT;
84        float4 color = tex2D(colors, IN.texCoord.xy);
85
86        const float pixLum = 0.2125f * color.x + 0.7154f * color.y + 0.0721f * color.z;
87       
88        // adjust to middle gray
89        const float lum = middleGrey * pixLum / imageKey;
90         
91        // map to range and calc burnout
92        const float scaleLum = lum * (1.0f + lum / whiteLum * whiteLum) / (1.0f + lum);
93
94        OUT.col = color * scaleLum / pixLum;
95        OUT.col.w = color.w;
96
97        return OUT;
98}
Note: See TracBrowser for help on using the repository browser.