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

Revision 3026, 5.1 KB checked in by mattausch, 16 years ago (diff)
RevLine 
[2966]1#include "../shaderenv.h"
2
3
[2876]4struct fragment
5{
6         // normalized screen position
7        float4 pos: WPOS;
[3009]8        float2 texCoord: TEXCOORD0;
9        float3 view: TEXCOORD1;
[2876]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 float3 normal,
[2959]35                         uniform float emmisive,
[2952]36                         float3 lightDir)
[2876]37{
[2954]38        // diffuse intensity
39        const float angle = saturate(dot(normal, lightDir));
40       
41        float4 lightDiffuse = glstate.light[0].diffuse;
42        float4 diffuse = angle * lightDiffuse;
[2876]43
44        // global ambient
[2954]45        const float4 ambient = glstate.light[0].ambient;
46       
[2968]47        float4 outColor;
[2967]48
[3005]49        // hack: prevent shading the sky
[3009]50        if (color.w > 1e19f) outColor = color;
[2984]51        //if (emmisive > 1.5f) outColor = color;
[2974]52        else outColor = (ambient + diffuse) * color;
[2968]53
54        return outColor;
[2876]55}
56
57
58
59/** The mrt shader for standard rendering
60*/
[2874]61pixel main(fragment IN,
62                   uniform sampler2D colors,
[2952]63                   uniform sampler2D normals,
[2991]64                   uniform float3 lightDir
[2874]65                   )
66{
67        pixel OUT;
68
[3009]69        float4 norm = tex2D(normals, IN.texCoord);
70        float4 color = tex2Dlod(colors, float4(IN.texCoord, 0, 0));
71       
[2874]72
73        // an ambient color term
[2974]74        float amb = color.w;
[2945]75        float3 normal = normalize(norm.xyz);
[3009]76        float4 col = shade(IN, color, normal, amb, lightDir);
[2874]77       
78        OUT.color = col;
[2985]79
[3018]80        // store scaled view vector from now on so wie don't have to normalize for e.g., ssao
81        float3 viewDir = IN.view;
82        const float lenView = length(viewDir);
83
84        OUT.color.w = color.w / lenView;
85
[2874]86        return OUT;
[2876]87}
[2892]88
[2944]89
90float CalcShadowTerm(fragment IN,
91                                         uniform sampler2D shadowMap,
[3025]92                                         uniform float scale,
[2944]93                                         uniform float2 lightSpacePos,
[2952]94                                         uniform float depth,
[2966]95                                         uniform float2 samples[NUM_PCF_TABS],
[3025]96                                         uniform float weights[NUM_PCF_TABS],
[2944]97                                         uniform sampler2D noiseTexture
98                                         )
99{
[2954]100        //float shadowDepth = tex2D(shadowMap, lightSpacePos).x;
101        //return step(depth, shadowDepth);
[2944]102
[3025]103        float total_d = .0f;
104        float total_w = .0f;
105
[2966]106        for (int i = 0; i < NUM_PCF_TABS; ++ i)
[2944]107        {
108                const float2 offset = samples[i];
[3025]109                const float w = weights[i];
[2944]110
111#if 1
112                ////////////////////
113                //-- add random noise: reflect around random normal vector (warning: slow!)
114
[3009]115                float2 mynoise = tex2D(noiseTexture, IN.texCoord).xy;
[2944]116                const float2 offsetTransformed = myreflect(offset, mynoise);
117#else
118                const float2 offsetTransformed = offset;
119#endif
120                // weight with projected coordinate to reach similar kernel size for near and far
[3025]121                float2 texcoord = lightSpacePos + offsetTransformed * scale;
[2944]122
123                float shadowDepth = tex2D(shadowMap, texcoord).x;
124
[3025]125                total_d += w * step(depth, shadowDepth);
126                total_w += w;
[2944]127        }
128
[3025]129        total_d /= (float)total_w;
[2944]130
131        return total_d;
132}
133
[3025]134
[3009]135inline float3 Interpol(float2 w, float3 bl, float3 br, float3 tl, float3 tr)
136{
137        float3 x1 = lerp(bl, tl, w.y);
138        float3 x2 = lerp(br, tr, w.y);
139        float3 v = lerp(x1, x2, w.x);
[2944]140
[3009]141        return v;
142}
143
144
[2892]145pixel main_shadow(fragment IN,
146                                  uniform sampler2D colors,
147                                  uniform sampler2D positions,
148                                  uniform sampler2D normals,               
149                                  uniform sampler2D shadowMap,
[2893]150                                  uniform float4x4 shadowMatrix,
[2952]151                                  uniform float sampleWidth,
[3026]152                                  uniform sampler2D noise,
[2966]153                                  uniform float2 samples[NUM_PCF_TABS],
[3024]154                                  uniform float weights[NUM_PCF_TABS],
[3009]155                                  uniform float3 lightDir,
156                                  uniform float3 eyePos,
157                                  uniform float3 bl,
158                                  uniform float3 br,
159                                  uniform float3 tl,
160                                  uniform float3 tr
[2944]161                                  )
[2928]162{
163        pixel OUT;
[2944]164
165        float4 norm = tex2D(normals, IN.texCoord.xy);
[2945]166        const float3 normal = normalize(norm.xyz);
[2944]167
[3009]168        float4 color = tex2Dlod(colors, float4(IN.texCoord, 0, 0));
169
170        /// reconstruct position from the eye space depth
171        float3 viewDir = IN.view;
[3018]172        const float lenView = length(viewDir);
173        viewDir /= lenView;
174
[3009]175        const float eyeDepth = tex2Dlod(colors, float4(IN.texCoord, 0, 0)).w;
176
177        float4 position;
178        position.xyz = eyePos - viewDir * eyeDepth;
179       
180       
[2945]181        // diffuse intensity
182        const float angle = saturate(dot(normal, lightDir));
[2959]183        const float4 lightDiffuse = glstate.light[0].diffuse;
[3017]184       
[2954]185        float4 diffuse = lightDiffuse * angle;
186
[3009]187        // hack: prevent shadowing the sky     
188        const bool useShading = (color.w < 1e19f);
189
[2945]190        // calc diffuse illumination + shadow term
[3009]191        if (useShading &&
192                (angle > 1e-3f) // shadow only if diffuse color has some minimum intensity
[2945]193                )
194        {
195                position.w = 1.0f;
[2944]196
[2945]197                float4 lightSpacePos = mul(shadowMatrix, position);
198                lightSpacePos /= lightSpacePos.w;
[2944]199
[3026]200                float shadowTerm = CalcShadowTerm(IN, shadowMap, sampleWidth, lightSpacePos.xy, lightSpacePos.z, samples, weights, noise);
201                //float shadowTerm = CalcShadowTerm(IN, shadowMap, sampleWidth, lightSpacePos.xy, lightSpacePos.z, samples, noise);
[2944]202
[2945]203                diffuse *= shadowTerm;
[2944]204        }
[2945]205
[2974]206        // light ambient term
[2954]207        const float4 ambient = glstate.light[0].ambient;
[3009]208        // compute shading
209        OUT.color = useShading ? (ambient + diffuse) * color : color;
[2945]210
[3018]211        // store scaled view vector from now on so wie don't have to normalize for e.g., ssao
212        OUT.color.w = color.w / lenView;
[2991]213
[2928]214        return OUT;
215}
[2965]216
Note: See TracBrowser for help on using the repository browser.