Ignore:
Timestamp:
10/09/08 11:19:33 (16 years ago)
Author:
mattausch
Message:
 
File:
1 edited

Legend:

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

    r3016 r3017  
    1111struct fragment 
    1212{ 
    13          // normalized screen position 
    14         float4 pos: WPOS; 
    1513        float2 texCoord: TEXCOORD0;  
    1614        float3 view: TEXCOORD1; 
     
    4644 
    4745// reconstruct world space position 
    48 inline float3 ReconstructSamplePosition( 
    49                                                                                 uniform sampler2D colors, 
    50                                                                                 float2 texcoord,  
    51                                                                                 float3 bl, float3 br, float3 tl, float3 tr) 
    52 { 
    53 #if USE_EYE_SPACE_DEPTH 
    54         float eyeSpaceDepth = tex2Dlod(colors, float4(texcoord, 0, 0)).w; 
    55         //float3 rotView = normalize(Interpol(texcoord, bl, br, tl, tr)); 
    56         float3 rotView = Interpol(texcoord, bl, br, tl, tr); 
    57          
    58         float3 samplePos = -rotView * eyeSpaceDepth; 
    59 #else 
    60         float3 samplePos = tex2Dlod(colors, float4(texcoord, 0, SSAO_MIPMAP_LEVEL)).xyz; 
    61 #endif 
     46inline float3 ReconstructSamplePos(uniform sampler2D colors, 
     47                                                                   float2 texcoord,  
     48                                                                   float3 bl, float3 br, float3 tl, float3 tr) 
     49{ 
     50        const float eyeSpaceDepth = tex2Dlod(colors, float4(texcoord, 0, 0)).w; 
     51        float3 viewVec = Interpol(texcoord, bl, br, tl, tr); 
     52        float3 samplePos = -viewVec * eyeSpaceDepth; 
     53 
    6254        return samplePos; 
    6355} 
     
    7668                   uniform float3 br, 
    7769                   uniform float3 tl, 
    78                    uniform float3 tr 
    79                    //, uniform float3 viewDir 
     70                   uniform float3 tr,  
     71                   uniform float3 viewDir 
    8072                   ) 
    8173{ 
     
    9688                // add random noise: reflect around random normal vector (warning: slow!) 
    9789 
    98                 float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy).xy; 
     90                float2 mynoise = tex2D(noiseTexture, IN.texCoord).xy; 
    9991                const float2 offsetTransformed = myreflect(offset, mynoise); 
    10092#else 
     
    10294#endif 
    10395                // weight with projected coordinate to reach similar kernel size for near and far 
    104                 float2 texcoord = IN.texCoord.xy + offsetTransformed * AREA_SIZE * scaleFactor; 
     96                float2 texcoord = IN.texCoord.xy + offsetTransformed * scaleFactor; 
    10597 
    10698                //if ((texcoord.x <= 1.0f) && (texcoord.x >= 0.0f) && (texcoord.y <= 1.0f) && (texcoord.y >= 0.0f))++ numSamples; 
    10799 
    108                 float3 samplePos = ReconstructSamplePosition(colors, texcoord, bl, br, tl, tr); 
    109  
    110  
    111                 /////// 
    112                 //-- compute contribution of current sample taking into account direction and angle 
    113  
    114                 float3 dirSample = samplePos - centerPosition.xyz; 
     100                const float3 samplePos = ReconstructSamplePos(colors, texcoord, bl, br, tl, tr); 
     101 
     102 
     103                //////////////// 
     104                //-- compute contribution of sample using the direction and angle 
     105 
     106                float3 dirSample = samplePos - centerPosition; 
    115107                const float lengthSample = length(dirSample); 
    116108 
     
    118110 
    119111                // angle between current normal and direction to sample controls AO intensity. 
    120                 const float cos_angle = max(dot(nDirSample, currentNormal), 0.0f); 
     112                const float cosAngle = max(dot(nDirSample, currentNormal), 0.0f); 
    121113 
    122114                // the distance_scale offset is used to avoid singularity that occurs at global illumination when  
    123115                // the distance to a sample approaches zero 
    124                 const float intensity =  
    125                         //(SAMPLE_INTENSITY * DISTANCE_SCALE) / (DISTANCE_SCALE + lengthSample * lengthSample); 
    126                         SAMPLE_INTENSITY / (DISTANCE_SCALE + lengthSample * lengthSample); 
    127  
    128 #if 0 
     116                const float intensity = SAMPLE_INTENSITY / (DISTANCE_SCALE + lengthSample * lengthSample); 
     117 
     118#if 1 
    129119                // if surface normal perpenticular to view dir, approx. half of the samples will not count 
    130120                // => compensate for this (on the other hand, projected sampling area could be larger!) 
    131                 const float viewCorrection = 1.0f + VIEW_CORRECTION_SCALE * (1.0f - dot(currentViewDir, currentNormal)); 
    132                 total_ao += cos_angle * intensity * viewCorrection; 
     121                const float viewCorrection = 1.0f + VIEW_CORRECTION_SCALE * dot(viewDir, currentNormal); 
     122                total_ao += cosAngle * intensity * viewCorrection; 
     123#else 
     124                total_ao += cosAngle * intensity; 
    133125#endif 
    134                 total_ao += cos_angle * intensity; 
    135126        } 
    136127 
     
    138129} 
    139130 
     131#pragma position_invariant main 
    140132 
    141133/** The mrt shader for screen space ambient occlusion 
     
    143135pixel main(fragment IN,  
    144136                   uniform sampler2D colors, 
    145                    uniform sampler2D positions, 
    146137                   uniform sampler2D normals, 
    147138                   uniform sampler2D noiseTexture, 
     
    161152 
    162153        float4 norm = tex2Dlod(normals, float4(IN.texCoord, 0 ,0)); 
    163         float3 normal = normalize(norm.xyz); 
    164  
    165         // the w coordinate from the persp. projection 
    166         float w = norm.w; 
    167  
    168  
    169 #if USE_EYE_SPACE_DEPTH 
     154        const float3 normal = normalize(norm.xyz); 
     155 
    170156        /// reconstruct position from the eye space depth 
    171157        float3 viewDir = IN.view; 
    172158        const float eyeDepth = tex2Dlod(colors, float4(IN.texCoord, 0, 0)).w; 
    173  
    174159        const float3 eyeSpacePos = -viewDir * eyeDepth; 
    175  
    176         float3 centerPosition; 
    177         centerPosition.xyz = eyePos + eyeSpacePos; 
    178  
    179         const float2 ao = ssao(IN, colors, noiseTexture, samples, normal, eyeSpacePos, w, bl, br, tl, tr); 
    180  
    181 #else 
    182  
    183         // the current world position 
    184         const float3 centerPosition = tex2Dlod(positions, float4(IN.texCoord, 0, 0)).xyz; 
    185  
    186         const float2 ao = ssao(IN, positions, noiseTexture, samples, normal, centerPosition, w, bl, br, tl, tr); 
    187  
    188 #endif 
     160        const float3 centerPosition = eyePos + eyeSpacePos; 
     161 
     162        float4 realPos = float4(centerPosition, 1.0f); 
     163 
     164 
     165        //////////////// 
     166        //-- calculcate the current projected depth for next frame 
     167         
     168        float4 currentPos = mul(modelViewProj, realPos); 
     169         
     170        const float w = SAMPLE_RADIUS / currentPos.w; 
     171        currentPos /= currentPos.w; 
     172         
     173        const float currentDepth = currentPos.z * 1e-3f; 
     174 
     175        const float2 ao = ssao(IN, colors, noiseTexture, samples, normal, eyeSpacePos, w, bl, br, tl, tr, normalize(viewDir)); 
     176 
    189177 
    190178        ///////////////// 
    191179        //-- compute temporally smoothing 
    192180 
    193         float4 realPos = float4(centerPosition, 1.0f); 
    194  
    195  
    196         // calculcate the current projected depth for next frame 
    197         float4 currentPos = mul(modelViewProj, realPos); 
    198         currentPos /= currentPos.w; 
    199         const float currentDepth = currentPos.z; 
    200          
    201  
    202         /////////// 
    203         //-- reprojection new frame into old one  
     181 
     182        // reproject new frame into old one  
    204183         
    205184        // calculate projected depth 
     
    208187 
    209188        // the current depth projected into the old frame 
    210         const float projDepth = projPos.z; 
     189        const float projDepth = projPos.z * 1e-3f; 
    211190 
    212191        // fit from unit cube into 0 .. 1 
    213         float2 tex = (projPos.xy) * 0.5f + 0.5f; 
     192        const float2 tex = projPos.xy * 0.5f + 0.5f; 
    214193 
    215194        // retrieve the sample from the last frame 
    216195        float4 oldCol = tex2D(oldTex, tex); 
    217196 
    218         const float oldDepth = oldCol.w; 
    219         const float depthDif = 1.0f - projDepth / oldDepth; 
     197        const float oldDepth = oldCol.z; 
     198        //const float depthDif = 1.0f - projDepth / oldDepth; 
     199        const float depthDif = projDepth - oldDepth; 
    220200 
    221201 
    222202        //const float oldNumSamples = oldCol.y; 
    223         const float oldWeight = clamp(oldCol.z, 0, temporalCoherence); 
     203        const float oldWeight = clamp(oldCol.y, 0, temporalCoherence); 
    224204 
    225205        float newWeight; 
     
    227207        // the number of valid samples in this frame 
    228208        //const float newNumSamples = ao.y; 
    229  
    230209 
    231210        if (//(temporalCoherence > 0) && 
     
    239218                // increase the weight for convergence 
    240219                newWeight = oldWeight + 1.0f; 
    241                 OUT.illum_col.xy = (ao.xy + oldCol.xy * oldWeight) / newWeight; 
     220                OUT.illum_col.x = (ao.x + oldCol.x * oldWeight) / newWeight; 
    242221                //if (!(oldNumSamples > ao.y - 1.5f)) newWeight = 0; 
    243222        } 
    244223        else 
    245224        {        
    246                 OUT.illum_col.xy = ao.xy; 
     225                OUT.illum_col.x = ao.x; 
    247226                newWeight = 0; 
    248227        } 
    249228 
    250         OUT.illum_col.z = newWeight; 
    251         OUT.illum_col.w = currentDepth; 
     229        //OUT.illum_col.y=depthDif; 
     230        OUT.illum_col.y = newWeight; 
     231        OUT.illum_col.z = currentDepth; 
    252232 
    253233        return OUT; 
     234} 
     235 
     236 
     237float Filter(float2 texCoord,  
     238                         uniform sampler2D ssaoTex, 
     239                         uniform float2 filterOffs[NUM_DOWNSAMPLES], 
     240                         uniform float filterWeights[NUM_DOWNSAMPLES] 
     241) 
     242{ 
     243        float average = .0f; 
     244        float w = .0f; 
     245 
     246        for (int i = 0; i < NUM_DOWNSAMPLES; ++ i) 
     247        { 
     248                average += filterWeights[i] * tex2Dlod(ssaoTex, float4(texCoord + filterOffs[i], 0, 0)).x; 
     249                w += filterWeights[i]; 
     250        } 
     251 
     252        average *= 1.0f / (float)w; 
     253 
     254        return average; 
    254255} 
    255256 
     
    257258pixel combine(fragment IN,  
    258259                          uniform sampler2D colors, 
    259                           uniform sampler2D ssaoTex) 
     260                          uniform sampler2D ssaoTex, 
     261                          uniform float2 filterOffs[NUM_DOWNSAMPLES], 
     262                          uniform float filterWeights[NUM_DOWNSAMPLES] 
     263                          ) 
    260264{ 
    261265        pixel OUT; 
    262266 
    263267        float4 col = tex2Dlod(colors, float4(IN.texCoord, 0, 0)); 
    264         float4 ao = tex2Dlod(ssaoTex, float4(IN.texCoord, 0, 0)); 
     268        float3 ao = tex2Dlod(ssaoTex, float4(IN.texCoord, 0, 0)); 
     269 
     270        if (ao.y < 2000.0f) 
     271                ao.x = Filter(IN.texCoord, ssaoTex, filterOffs, filterWeights); 
    265272 
    266273        OUT.illum_col = col * ao.x; 
    267         //OUT.illum_col = float4(ao.x,ao.x,ao.x, ao.w); 
     274        //OUT.illum_col.xyz = float3(ao.x,1-ao.y*1e-2f, 0); 
    268275        OUT.illum_col.w = col.w; 
    269276 
Note: See TracChangeset for help on using the changeset viewer.