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

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

tone mapping working with using reconstructed depth

RevLine 
[2966]1#include "../shaderenv.h"
2
3
[2876]4struct fragment
5{
6         // normalized screen position
7        float4 pos: WPOS;
8        float4 texCoord: TEXCOORD0;
9        float3 view: COLOR0;
10};
11
12
13struct pixel
14{
15        float4 color: COLOR0;
16};
17
18
[2944]19float2 myreflect(float2 pt, float2 n)
20{
21        // distance to plane
22        float d = dot(n, pt);
23        // reflect around plane
24        float2 rpt = pt - d * 2.0f * n;
25
26        return rpt;
27}
28
29
[2876]30/** function for standard deferred shading
31*/
32float4 shade(fragment IN,
33                         uniform float4 color,
34                         uniform float4 position,
35                         uniform float3 normal,
[2959]36                         uniform float emmisive,
[2952]37                         float3 lightDir)
[2876]38{
[2954]39        // diffuse intensity
40        const float angle = saturate(dot(normal, lightDir));
41       
42        float4 lightDiffuse = glstate.light[0].diffuse;
43        float4 diffuse = angle * lightDiffuse;
[2876]44
45        // global ambient
[2954]46        const float4 ambient = glstate.light[0].ambient;
47       
[2968]48        float4 outColor;
[2967]49
[2984]50        if (position.w > 1e19f) outColor = color;
51        //if (emmisive > 1.5f) outColor = color;
[2974]52        else outColor = (ambient + diffuse) * color;
[2968]53
54        return outColor;
[2967]55        //return saturate((((ambient + diffuse)) * (1.0f - emmisive) + emmisive) * color);
[2876]56}
57
58
59
60/** The mrt shader for standard rendering
61*/
[2874]62pixel main(fragment IN,
63                   uniform sampler2D colors,
64                   uniform sampler2D positions,
[2952]65                   uniform sampler2D normals,
[2991]66                   uniform float3 lightDir
67                  //, uniform sampler2D oldColors
[2874]68                   )
69{
70        pixel OUT;
71
72        float4 norm = tex2D(normals, IN.texCoord.xy);
[2884]73        float4 color = tex2Dlod(colors, float4(IN.texCoord.xy, 0, 0));
[2874]74        float4 position = tex2D(positions, IN.texCoord.xy);
75
76        // an ambient color term
[2974]77        float amb = color.w;
[2945]78        float3 normal = normalize(norm.xyz);
[2952]79        float4 col = shade(IN, color, position, normal, amb, lightDir);
[2874]80       
81        OUT.color = col;
[2986]82       
[2992]83#if 1
[2985]84
[2986]85        OUT.color.w = color.w;
[2985]86
[2986]87#else
88
[2975]89        ////////////
90        //-- write out logaritmic luminance for tone mapping
[2986]91
[2991]92        // the old loglum is stored in the hightest mipmap-level
93        float oldLogLum = tex2Dlod(colors, float4(IN.texCoord.xy, 0, MAX_LOD_LEVEL)).w;
[2976]94
[2974]95        const float3 w = float3(0.299f, 0.587f, 0.114f);
96
97        float lum = dot(OUT.color.rgb, w);
98        float logLum = log(1e-5f + lum);
99
[2975]100        float logLumOffset = MINLOGLUM * INV_LOGLUM_RANGE;
101        float logLumScaled = logLum * INV_LOGLUM_RANGE - logLumOffset;
[2974]102
[2991]103        if (oldLogLum > 0)
104                OUT.color.w = lerp(oldLogLum, logLumScaled, 0.1f);
[2977]105        else
106                OUT.color.w = logLumScaled;
[2986]107
108#endif
109
[2874]110        return OUT;
[2876]111}
[2892]112
[2944]113
114float CalcShadowTerm(fragment IN,
115                                         uniform sampler2D shadowMap,
116                                         uniform float w,
117                                         uniform float2 lightSpacePos,
[2952]118                                         uniform float depth,
[2966]119                                         uniform float2 samples[NUM_PCF_TABS],
[2944]120                                         uniform sampler2D noiseTexture
121                                         )
122{
[2954]123        //float shadowDepth = tex2D(shadowMap, lightSpacePos).x;
124        //return step(depth, shadowDepth);
[2944]125
[2954]126        float total_d = 0.0;
127       
[2966]128        for (int i = 0; i < NUM_PCF_TABS; ++ i)
[2944]129        {
130                const float2 offset = samples[i];
131
132#if 1
133                ////////////////////
134                //-- add random noise: reflect around random normal vector (warning: slow!)
135
136                float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy).xy;
137                const float2 offsetTransformed = myreflect(offset, mynoise);
138#else
139                const float2 offsetTransformed = offset;
140#endif
141                // weight with projected coordinate to reach similar kernel size for near and far
142                float2 texcoord = lightSpacePos + offsetTransformed * w;
143
144                float shadowDepth = tex2D(shadowMap, texcoord).x;
145
[2945]146                total_d += step(depth, shadowDepth);
[2944]147        }
148
[2968]149        total_d /= (float)NUM_PCF_TABS;
[2944]150
151        return total_d;
152}
153
154
[2892]155pixel main_shadow(fragment IN,
156                                  uniform sampler2D colors,
157                                  uniform sampler2D positions,
158                                  uniform sampler2D normals,               
159                                  uniform sampler2D shadowMap,
[2893]160                                  uniform float4x4 shadowMatrix,
[2895]161                                  uniform float maxDepth,
[2952]162                                  uniform float sampleWidth,
[2944]163                                  uniform sampler2D noiseTexture,
[2966]164                                  uniform float2 samples[NUM_PCF_TABS],
[2991]165                                  uniform float3 lightDir
166                                  //, uniform sampler2D oldColors
[2944]167                                  )
[2928]168{
169        pixel OUT;
[2944]170
171        float4 norm = tex2D(normals, IN.texCoord.xy);
172        float4 color = tex2Dlod(colors, float4(IN.texCoord.xy, 0, 0));
173        float4 position = tex2D(positions, IN.texCoord.xy);
[2945]174       
175        const float3 normal = normalize(norm.xyz);
[2952]176       
[2945]177        // hack: an emmisive color term
[2974]178        float emmisive = color.w;
[2944]179
[2945]180        // diffuse intensity
181        const float angle = saturate(dot(normal, lightDir));
[2954]182        //float4 lightDiffuse = float4(1.0f, 1.0f, 0.9f, 1.0f);
[2959]183        const float4 lightDiffuse = glstate.light[0].diffuse;
[2954]184        //float4(1.0f, 1.0f, 0.9f, 1.0f);
[2944]185
[2954]186        float4 diffuse = lightDiffuse * angle;
187
[2945]188        // calc diffuse illumination + shadow term
[2984]189        //if ((emmisive < 1.5f) // hack: prevent shadowing the sky     
190        if (
[2991]191                (position.w < 1e19f)
[2952]192                && (angle > 1e-3f) // shadow only if diffuse color has some minimum intensity
[2945]193                )
194        {
195                position *= maxDepth;
196                position.w = 1.0f;
[2944]197
[2945]198                float4 lightSpacePos = mul(shadowMatrix, position);
199                lightSpacePos /= lightSpacePos.w;
[2944]200
[2945]201                float shadowTerm = CalcShadowTerm(IN, shadowMap, sampleWidth, lightSpacePos.xy, lightSpacePos.z, samples, noiseTexture);
[2944]202
[2945]203                diffuse *= shadowTerm;
[2944]204        }
[2945]205
[2974]206        // light ambient term
[2954]207        const float4 ambient = glstate.light[0].ambient;
[2944]208       
[2945]209        // base lighting
[2984]210        //OUT.color = (emmisive > 1.5f) ? color: (ambient + diffuse) * color;
211        OUT.color = (position.w > 1e19f) ? color: (ambient + diffuse) * color;
[2945]212
[2978]213
[2975]214        ////////////
215        //-- write out logaritmic luminance for tone mapping
216
[2992]217#if 1
[2991]218        OUT.color.w = color.w;
219#else
220
221        float oldLogLum = tex2Dlod(colors, float4(IN.texCoord.xy, 0, MAX_LOD_LEVEL)).w;
222
[2974]223        const float3 w = float3(0.299f, 0.587f, 0.114f);
[2944]224
[2974]225        float lum = dot(OUT.color.rgb, w);
226        float logLum = log(1e-5f + lum);
227
[2975]228        float logLumOffset = MINLOGLUM * INV_LOGLUM_RANGE;
229        float logLumScaled = logLum * INV_LOGLUM_RANGE - logLumOffset;
230
[2991]231        if (oldLogLum > 0)
232                OUT.color.w = lerp(oldLogLum, logLumScaled, 0.1f);
[2978]233        else
234                OUT.color.w = logLumScaled;
[2991]235#endif 
[2928]236        return OUT;
237}
[2965]238
Note: See TracBrowser for help on using the repository browser.