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

Revision 3005, 5.6 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        float3 view: COLOR0;
10};
11
12
13struct pixel
14{
15        float4 color: COLOR0;
16};
17
18
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
30/** function for standard deferred shading
31*/
32float4 shade(fragment IN,
33                         uniform float4 color,
34                         uniform float4 position,
35                         uniform float3 normal,
36                         uniform float emmisive,
37                         float3 lightDir)
38{
39        // diffuse intensity
40        const float angle = saturate(dot(normal, lightDir));
41       
42        float4 lightDiffuse = glstate.light[0].diffuse;
43        float4 diffuse = angle * lightDiffuse;
44
45        // global ambient
46        const float4 ambient = glstate.light[0].ambient;
47       
48        float4 outColor;
49
50        // hack: prevent shading the sky
51        if (position.z > 1e19f) outColor = color;
52        //if (emmisive > 1.5f) outColor = color;
53        else outColor = (ambient + diffuse) * color;
54
55        return outColor;
56}
57
58
59
60/** The mrt shader for standard rendering
61*/
62pixel main(fragment IN,
63                   uniform sampler2D colors,
64                   uniform sampler2D positions,
65                   uniform sampler2D normals,
66                   uniform float3 lightDir
67                  //, uniform sampler2D oldColors
68                   )
69{
70        pixel OUT;
71
72        float4 norm = tex2D(normals, IN.texCoord.xy);
73        float4 color = tex2Dlod(colors, float4(IN.texCoord.xy, 0, 0));
74        float4 position = tex2D(positions, IN.texCoord.xy);
75
76        // an ambient color term
77        float amb = color.w;
78        float3 normal = normalize(norm.xyz);
79        float4 col = shade(IN, color, position, normal, amb, lightDir);
80       
81        OUT.color = col;
82       
83#if 1
84
85        OUT.color.w = color.w;
86
87#else
88
89        ////////////
90        //-- write out logaritmic luminance for tone mapping
91
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;
94
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
100        float logLumOffset = MINLOGLUM * INV_LOGLUM_RANGE;
101        float logLumScaled = logLum * INV_LOGLUM_RANGE - logLumOffset;
102
103        if (oldLogLum > 0)
104                OUT.color.w = lerp(oldLogLum, logLumScaled, 0.1f);
105        else
106                OUT.color.w = logLumScaled;
107
108#endif
109
110        return OUT;
111}
112
113
114float CalcShadowTerm(fragment IN,
115                                         uniform sampler2D shadowMap,
116                                         uniform float w,
117                                         uniform float2 lightSpacePos,
118                                         uniform float depth,
119                                         uniform float2 samples[NUM_PCF_TABS],
120                                         uniform sampler2D noiseTexture
121                                         )
122{
123        //float shadowDepth = tex2D(shadowMap, lightSpacePos).x;
124        //return step(depth, shadowDepth);
125
126        float total_d = 0.0;
127       
128        for (int i = 0; i < NUM_PCF_TABS; ++ i)
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
146                total_d += step(depth, shadowDepth);
147        }
148
149        total_d /= (float)NUM_PCF_TABS;
150
151        return total_d;
152}
153
154
155pixel main_shadow(fragment IN,
156                                  uniform sampler2D colors,
157                                  uniform sampler2D positions,
158                                  uniform sampler2D normals,               
159                                  uniform sampler2D shadowMap,
160                                  uniform float4x4 shadowMatrix,
161                                  uniform float maxDepth,
162                                  uniform float sampleWidth,
163                                  uniform sampler2D noiseTexture,
164                                  uniform float2 samples[NUM_PCF_TABS],
165                                  uniform float3 lightDir
166                                  //, uniform sampler2D oldColors
167                                  )
168{
169        pixel OUT;
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);
174       
175        const float3 normal = normalize(norm.xyz);
176       
177        // hack: an emmisive color term
178        float emmisive = color.w;
179
180        // diffuse intensity
181        const float angle = saturate(dot(normal, lightDir));
182        //float4 lightDiffuse = float4(1.0f, 1.0f, 0.9f, 1.0f);
183        const float4 lightDiffuse = glstate.light[0].diffuse;
184        //float4(1.0f, 1.0f, 0.9f, 1.0f);
185
186        float4 diffuse = lightDiffuse * angle;
187
188        // calc diffuse illumination + shadow term
189        //if ((emmisive < 1.5f) // hack: prevent shadowing the sky     
190        if ((position.z < 1e19f)
191                && (angle > 1e-3f) // shadow only if diffuse color has some minimum intensity
192                )
193        {
194                position *= maxDepth;
195                position.w = 1.0f;
196
197                float4 lightSpacePos = mul(shadowMatrix, position);
198                lightSpacePos /= lightSpacePos.w;
199
200                float shadowTerm = CalcShadowTerm(IN, shadowMap, sampleWidth, lightSpacePos.xy, lightSpacePos.z, samples, noiseTexture);
201
202                diffuse *= shadowTerm;
203        }
204
205        // light ambient term
206        const float4 ambient = glstate.light[0].ambient;
207       
208        // base lighting
209        //OUT.color = (emmisive > 1.5f) ? color: (ambient + diffuse) * color;
210        OUT.color = (position.z > 1e19f) ? color: (ambient + diffuse) * color;
211
212
213        ////////////
214        //-- write out logaritmic luminance for tone mapping
215
216#if 1
217        OUT.color.w = color.w;
218#else
219
220        float oldLogLum = tex2Dlod(colors, float4(IN.texCoord.xy, 0, MAX_LOD_LEVEL)).w;
221
222        const float3 w = float3(0.299f, 0.587f, 0.114f);
223
224        float lum = dot(OUT.color.rgb, w);
225        float logLum = log(1e-5f + lum);
226
227        float logLumOffset = MINLOGLUM * INV_LOGLUM_RANGE;
228        float logLumScaled = logLum * INV_LOGLUM_RANGE - logLumOffset;
229
230        if (oldLogLum > 0)
231                OUT.color.w = lerp(oldLogLum, logLumScaled, 0.1f);
232        else
233                OUT.color.w = logLumScaled;
234#endif 
235        return OUT;
236}
237
Note: See TracBrowser for help on using the repository browser.