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

Revision 2975, 2.3 KB checked in by mattausch, 16 years ago (diff)
RevLine 
[2972]1#include "../shaderenv.h"
2
3
[2974]4struct frag
[2972]5{
6         // normalized screen position
[2974]7        float2 texCoord: TEXCOORD0;
[2973]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
[2972]13};
14
15
[2973]16struct pixel
[2972]17{
[2973]18        float4 col: COLOR0;
19};
[2972]20
21
[2974]22float4 DownSample(frag IN,
[2972]23                                  uniform sampler2D colors,
24                                  float2 downSampleOffs[16]) : COLOR
25{   
26    float4 average = .0f;
27
28    for(int i = 0; i < 16; ++ i)
29    {
[2974]30        average += tex2D(colors, IN.texCoord + downSampleOffs[i]);
[2972]31    }
32       
33    average *= 1.0f / 16.0f;
34
35    return average;
36}
37   
38
[2974]39float4 GreyScaleDownSample(frag IN,
40                                                   uniform sampler2D colors
41                                                   ): COLOR
[2972]42{
43
44        // Compute the average of the 4 necessary samples
[2974]45        float average = .0f;
46        float maximum = .0f;
[2972]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
[2973]52        float4 cols[4];
53
[2974]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);
[2973]58
[2972]59        for (int i = 0; i < 4; ++ i)
60        {
[2973]61                const float intensity = dot(cols[i].rgb, w);
[2972]62
[2973]63                maximum = max(maximum, intensity);
[2974]64                average += log(1e-5f + intensity);
[2972]65        }
66
[2974]67        average = exp(average * 0.25f);
[2972]68
[2974]69        float4 hcols = cols[0] + cols[1] + cols[2] + cols[3];
[2973]70
[2974]71        return hcols * 0.25f;
72        //return float4(average, maximum, 0.0f, cols[0].w);
73
[2972]74        // Output the luminance to the render target
[2974]75        //return float4(average, maximum, 0.0f, 1.0f);
[2972]76}
[2973]77   
[2972]78
[2974]79pixel ToneMap(frag IN,
[2973]80                          uniform sampler2D colors,
81                          uniform float imageKey,
82                          uniform float whiteLum,
83                          uniform float middleGrey)
84{
85        pixel OUT;
[2974]86        float4 color = tex2D(colors, IN.texCoord);
[2973]87
88        const float pixLum = 0.2125f * color.x + 0.7154f * color.y + 0.0721f * color.z;
89       
[2974]90        // obtain new image key from highest mipmap level
91        float logLumScaled = tex2Dlod(colors, float4(.5f, .5f, 0, 99)).w;
[2975]92        float logLum = logLumScaled * LOGLUM_RANGE + MINLOGLUM;
[2974]93
94        float newImageKey = exp(logLum);
95
[2973]96        // adjust to middle gray
[2974]97        const float lum = middleGrey * pixLum / newImageKey;
[2973]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}
Note: See TracBrowser for help on using the repository browser.