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

Revision 2985, 5.6 KB checked in by mattausch, 16 years ago (diff)

trying out linear depth instead of positions

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        if (position.w > 1e19f) outColor = color;
51        //if (emmisive > 1.5f) outColor = color;
52        else outColor = (ambient + diffuse) * color;
53
54        return outColor;
55        //return saturate((((ambient + diffuse)) * (1.0f - emmisive) + emmisive) * color);
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
79        float3 normal = normalize(norm.xyz);
80
81        float4 col = shade(IN, color, position, normal, amb, lightDir);
82       
83        OUT.color = col;
84
85OUT.color.w = color.w;
86
87
88        ////////////
89        //-- write out logaritmic luminance for tone mapping
90/*
91        float4 oldColor = tex2Dlod(colors, float4(IN.texCoord.xy, 0, MAX_LOD_LEVEL));
92
93        const float3 w = float3(0.299f, 0.587f, 0.114f);
94
95        float lum = dot(OUT.color.rgb, w);
96        float logLum = log(1e-5f + lum);
97
98        float logLumOffset = MINLOGLUM * INV_LOGLUM_RANGE;
99        float logLumScaled = logLum * INV_LOGLUM_RANGE - logLumOffset;
100
101        if (oldColor.w > 0)
102                OUT.color.w = lerp(oldColor.w, logLumScaled, 0.1f);
103        else
104                OUT.color.w = logLumScaled;
105*/
106        return OUT;
107}
108
109
110float CalcShadowTerm(fragment IN,
111                                         uniform sampler2D shadowMap,
112                                         uniform float w,
113                                         uniform float2 lightSpacePos,
114                                         uniform float depth,
115                                         uniform float2 samples[NUM_PCF_TABS],
116                                         uniform sampler2D noiseTexture
117                                         )
118{
119        //float shadowDepth = tex2D(shadowMap, lightSpacePos).x;
120        //return step(depth, shadowDepth);
121
122        float total_d = 0.0;
123       
124        for (int i = 0; i < NUM_PCF_TABS; ++ i)
125        {
126                const float2 offset = samples[i];
127
128#if 1
129                ////////////////////
130                //-- add random noise: reflect around random normal vector (warning: slow!)
131
132                float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy).xy;
133                const float2 offsetTransformed = myreflect(offset, mynoise);
134#else
135                const float2 offsetTransformed = offset;
136#endif
137                // weight with projected coordinate to reach similar kernel size for near and far
138                float2 texcoord = lightSpacePos + offsetTransformed * w;
139
140                float shadowDepth = tex2D(shadowMap, texcoord).x;
141
142                total_d += step(depth, shadowDepth);
143        }
144
145        total_d /= (float)NUM_PCF_TABS;
146
147        return total_d;
148}
149
150
151pixel main_shadow(fragment IN,
152                                  uniform sampler2D colors,
153                                  uniform sampler2D positions,
154                                  uniform sampler2D normals,               
155                                  uniform sampler2D shadowMap,
156                                  uniform float4x4 shadowMatrix,
157                                  uniform float maxDepth,
158                                  uniform float sampleWidth,
159                                  uniform sampler2D noiseTexture,
160                                  uniform float2 samples[NUM_PCF_TABS],
161                                  uniform float3 lightDir,
162                                  uniform sampler2D oldColors
163                                  )
164{
165        pixel OUT;
166
167        float4 norm = tex2D(normals, IN.texCoord.xy);
168        float4 color = tex2Dlod(colors, float4(IN.texCoord.xy, 0, 0));
169        float4 position = tex2D(positions, IN.texCoord.xy);
170       
171        const float3 normal = normalize(norm.xyz);
172       
173        // hack: an emmisive color term
174        float emmisive = color.w;
175
176        // diffuse intensity
177        const float angle = saturate(dot(normal, lightDir));
178        //float4 lightDiffuse = float4(1.0f, 1.0f, 0.9f, 1.0f);
179        const float4 lightDiffuse = glstate.light[0].diffuse;
180        //float4(1.0f, 1.0f, 0.9f, 1.0f);
181
182        float4 diffuse = lightDiffuse * angle;
183
184        // calc diffuse illumination + shadow term
185        //if ((emmisive < 1.5f) // hack: prevent shadowing the sky     
186        if (
187                (position.w > 1e19f)
188                && (angle > 1e-3f) // shadow only if diffuse color has some minimum intensity
189                )
190        {
191                position *= maxDepth;
192                position.w = 1.0f;
193
194                float4 lightSpacePos = mul(shadowMatrix, position);
195                lightSpacePos /= lightSpacePos.w;
196
197                float shadowTerm = CalcShadowTerm(IN, shadowMap, sampleWidth, lightSpacePos.xy, lightSpacePos.z, samples, noiseTexture);
198
199                diffuse *= shadowTerm;
200        }
201
202        // light ambient term
203        const float4 ambient = glstate.light[0].ambient;
204       
205        // base lighting
206        //OUT.color = (emmisive > 1.5f) ? color: (ambient + diffuse) * color;
207        OUT.color = (position.w > 1e19f) ? color: (ambient + diffuse) * color;
208
209
210        ////////////
211        //-- write out logaritmic luminance for tone mapping
212
213        float4 oldColor = tex2Dlod(colors, float4(IN.texCoord.xy, 0, MAX_LOD_LEVEL));
214
215        const float3 w = float3(0.299f, 0.587f, 0.114f);
216
217        float lum = dot(OUT.color.rgb, w);
218        float logLum = log(1e-5f + lum);
219
220        float logLumOffset = MINLOGLUM * INV_LOGLUM_RANGE;
221        float logLumScaled = logLum * INV_LOGLUM_RANGE - logLumOffset;
222
223        if (oldColor.w > 0)
224                OUT.color.w = lerp(oldColor.w, logLumScaled, 0.1f);
225        else
226                OUT.color.w = logLumScaled;
227       
228        return OUT;
229}
230
Note: See TracBrowser for help on using the repository browser.