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

Revision 2982, 5.4 KB checked in by mattausch, 16 years ago (diff)

problematic: merging

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