Ignore:
Timestamp:
11/10/08 18:43:06 (16 years ago)
Author:
mattausch
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg

    r3114 r3115  
    6666 
    6767inline float ComputeDifference(float2 offset, 
    68                                                            uniform sampler2D oldTex, 
    69                                                            const uniform float4x4 oldModelViewProj, 
    70                                                            uniform sampler2D colors, 
    71                                                            uniform sampler2D noiseTex, 
    72                                                            uniform float scaleFactor, 
    73                                                            uniform float3 bl, 
    74                                                            uniform float3 br, 
    75                                                            uniform float3 tl, 
    76                                                            uniform float3 tr,  
     68                                                           sampler2D oldTex, 
     69                                                           float4x4 oldModelViewProj, 
     70                                                           sampler2D colors, 
     71                                                           sampler2D noiseTex, 
     72                                                           float scaleFactor, 
     73                                                           float3 bl, 
     74                                                           float3 br, 
     75                                                           float3 tl, 
     76                                                           float3 tr,  
    7777                                                           float2 texcoord0, 
    7878                                                           float3 oldEyePos, 
    79                                                            uniform float3 oldbl, 
    80                                                            uniform float3 oldbr, 
    81                                                            uniform float3 oldtl, 
    82                                                            uniform float3 oldtr, 
     79                                                           float3 oldbl, 
     80                                                           float3 oldbr, 
     81                                                           float3 oldtl, 
     82                                                           float3 oldtr, 
    8383                                                           float eyeSpaceDepth 
    8484                                                           ) 
    8585{ 
    86         const float2 mynoise = tex2Dlod(noiseTex, texcoord0).xy; 
     86        const float2 mynoise = tex2Dlod(noiseTex, float4(texcoord0, 0, 0)).xy; 
    8787 
    8888        const float2 offsetTransformed = myreflect(offset, mynoise); 
     
    9393        const float3 viewVec = Interpol(texCoord, bl, br, tl, tr); 
    9494        const float3 samplePos = -viewVec * sampleEyeSpaceDepth; 
    95  
    9695        const float3 translatedPos = samplePos - oldEyePos; 
     96 
    9797        // reproject into old frame and calculate projected depth 
    9898        float4 projPos = mul(oldModelViewProj, float4(translatedPos, 1.0f)); 
    9999        projPos /= projPos.w; 
    100  
    101100        // fit from unit cube into 0 .. 1 
    102101        const float2 oldTexCoords = projPos.xy * 0.5f + 0.5f; 
     
    105104 
    106105        const float oldEyeSpaceDepth = oldPixel.w; 
     106         
     107        // projected linear depth 
    107108        const float3 oldViewVec = Interpol(oldTexCoords, oldbl, oldbr, oldtl, oldtr); 
    108  
    109109        const float invlen = 1.0f / length(oldViewVec); 
    110         const float projectedEyeSpaceDepth = length(translatedPos) * invlen; 
    111          
    112         float depthDif = (abs(eyeSpaceDepth - sampleEyeSpaceDepth) > 5.0f) ?  
    113                 0 : abs(oldEyeSpaceDepth - projectedEyeSpaceDepth); 
     110        const float projectedEyeSpaceDepth = invlen * length(translatedPos); 
     111 
     112        float depthDif = (abs(eyeSpaceDepth - sampleEyeSpaceDepth) > 5.0f) ? 0 :  
     113                abs(1.0f - oldEyeSpaceDepth / projectedEyeSpaceDepth); 
    114114 
    115115        return depthDif; 
     
    117117 
    118118 
    119 /** This shader computes the reprojection and stores reprojected color / depth values 
    120         as well as a boolean that  
     119/** This shader computes the reprojection and stores  
     120        reprojected color / depth values as well as a boolean that  
    121121*/ 
    122122inline float4 temporalSmoothing(float4 worldPos, 
     
    148148 
    149149        // compute position from old frame for dynamic objects + translational portion 
    150         const float3 translatedPt = worldPos.xyz - oldEyePos + diffVec; 
     150        const float3 translatedPos = worldPos.xyz - oldEyePos + diffVec; 
    151151 
    152152 
     
    155155 
    156156        // note: the old model view matrix only holds the view orientation part 
    157         float4 backProjPos = mul(oldModelViewProj, float4(translatedPt, 1.0f)); 
     157        float4 backProjPos = mul(oldModelViewProj, float4(translatedPos, 1.0f)); 
    158158        backProjPos /= backProjPos.w; 
    159159         
     
    172172        const float3 viewVec = Interpol(oldTexCoords, oldbl, oldbr, oldtl, oldtr); 
    173173        const float invLen = 1.0f / length(viewVec); 
    174         const float projectedEyeSpaceDepth = invLen * length(translatedPt); 
    175          
    176         //const float depthDif = abs(oldEyeSpaceDepth - projectedEyeSpaceDepth); 
     174        const float projectedEyeSpaceDepth = invLen * length(translatedPos); 
     175         
    177176        const float depthDif = abs(1.0f - oldEyeSpaceDepth / projectedEyeSpaceDepth); 
    178         //const float depthDif = abs(projectedEyeSpaceDepth - oldEyeSpaceDepth ) / projectedEyeSpaceDepth; 
    179177 
    180178#else 
     
    182180        const float oldDepth = oldPixel.w; 
    183181        const float projectedDepth = projPos.z; 
    184         //const float projectedDepth = invW * worldPos.z; 
    185  
    186182        // vector from eye pos to old sample  
    187183        const float depthDif = abs(projectedDepth - oldDepth); 
     
    189185 
    190186        float notValid = 0.5f; 
    191  
    192         /* 
    193         for (int i = 0; i < NUM_SAMPLES; ++ i)  
     187        //float overallDepth = 0; 
     188        const float squaredLen = diffVec.x * diffVec.x + diffVec.y * diffVec.y + diffVec.z * diffVec.z; 
     189 
     190        if (squaredLen < 1e-8f) // object not dynamic 
    194191        { 
    195                 float sampleDif = ComputeDifference(samples[i], 
    196                                           oldTex, 
    197                                                           oldModelViewProj, 
    198                                                           colors, 
    199                                                           noiseTex, 
    200                                                           scaleFactor, 
    201                                                           bl, br, tl, tr,  
    202                                                           texcoord0, 
    203                                                           oldEyePos, 
    204                                                           oldbl, oldbr, oldtl, oldtr, 
    205                                                           eyeSpaceDepth); 
    206  
    207                 if (sampleDif >= 5e-2f) ++ notValid; 
    208         } 
    209         */ 
     192                for (int i = 0; i < NUM_SAMPLES; ++ i)  
     193                { 
     194                        float sampleDif = ComputeDifference(samples[i], 
     195                                                                                                oldTex, 
     196                                                                                                oldModelViewProj, 
     197                                                                                                colors, 
     198                                                                                                noiseTex, 
     199                                                                                                scaleFactor, 
     200                                                                                                bl, br, tl, tr,  
     201                                                                                                texcoord0, 
     202                                                                                                oldEyePos, 
     203                                                                                                oldbl, oldbr, oldtl, oldtr, 
     204                                                                                                eyeSpaceDepth 
     205                                                                                                ); 
     206                        //overallDepth += sampleDif; 
     207 
     208                        if (sampleDif >= MIN_DEPTH_DIFF) ++ notValid; 
     209                } 
     210        } 
     211 
    210212 
    211213        // the number of valid samples in this frame 
     
    243245        illum_col.w = projectedDepth; 
    244246#endif 
    245  
     247        //overallDepth = clamp(overallDepth*1e3, 0, 1); 
    246248        illum_col.z = invW; 
    247         //illum_col.x = length(diffVec*50.0f); 
     249        //illum_col.x = overallDepth; 
    248250 
    249251        return illum_col; 
     
    370372        // note: this should be done with the stencil buffer 
    371373        if (eyeSpaceDepth < 1e10f) 
     374        { 
    372375                ao = ssao(IN, colors, noiseTex, samples, normal,  
    373376                          eyeSpacePos.xyz, scaleFactor, bl, br, tl, tr, normalize(viewDir)); 
    374  
     377        } 
    375378 
    376379        //float3 id = tex2Dlod(attribsTex, float4(IN.texCoord, 0, 0)).xyz; 
     
    409412        for (int i = 0; i < NUM_SSAO_FILTERSAMPLES; ++ i) 
    410413        {        
    411                 average += filterWeights[i] * tex2Dlod(ssaoTex, float4(texCoord + filterOffs[i] * scale, 0, 0)).x; 
     414                average += filterWeights[i] * tex2Dlod(ssaoTex, float4(texCoord + filterOffs[i] * scale * 2, 0, 0)).x; 
    412415                w += filterWeights[i]; 
    413416        } 
     
    434437        //      ao.x = Filter(IN.texCoord, ssaoTex, filterOffs, filterWeights, 1.0f / (1.0f + ao.y));//ao.z); 
    435438 
    436         //OUT.illum_col = col * ao.x; 
     439        OUT.illum_col = col * ao.x; 
    437440        //OUT.illum_col = float4(ao.y, ao.y, ao.y, col.w); 
    438441        //OUT.illum_col = float4(ao.x, ao.y, ao.z, col.w); 
    439         OUT.illum_col = float4(ao.x, ao.x, ao.x, col.w); 
     442        //OUT.illum_col = float4(ao.x, 0, 0, col.w); 
    440443        //OUT.illum_col.xyz = float3(1.0f - ao.x, 1.0f - ao.y * 1e-2f, 1); 
    441444        //OUT.illum_col.xyz = float3(1.0f - ao.x, ao.y, 0); 
Note: See TracChangeset for help on using the changeset viewer.