- Timestamp:
- 10/09/08 11:19:33 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg
r3016 r3017 11 11 struct fragment 12 12 { 13 // normalized screen position14 float4 pos: WPOS;15 13 float2 texCoord: TEXCOORD0; 16 14 float3 view: TEXCOORD1; … … 46 44 47 45 // 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 46 inline 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 62 54 return samplePos; 63 55 } … … 76 68 uniform float3 br, 77 69 uniform float3 tl, 78 uniform float3 tr 79 //,uniform float3 viewDir70 uniform float3 tr, 71 uniform float3 viewDir 80 72 ) 81 73 { … … 96 88 // add random noise: reflect around random normal vector (warning: slow!) 97 89 98 float2 mynoise = tex2D(noiseTexture, IN.texCoord .xy).xy;90 float2 mynoise = tex2D(noiseTexture, IN.texCoord).xy; 99 91 const float2 offsetTransformed = myreflect(offset, mynoise); 100 92 #else … … 102 94 #endif 103 95 // 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; 105 97 106 98 //if ((texcoord.x <= 1.0f) && (texcoord.x >= 0.0f) && (texcoord.y <= 1.0f) && (texcoord.y >= 0.0f))++ numSamples; 107 99 108 float3 samplePos = ReconstructSamplePosition(colors, texcoord, bl, br, tl, tr);109 110 111 /////// 112 //-- compute contribution of current sample taking into accountdirection and angle113 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; 115 107 const float lengthSample = length(dirSample); 116 108 … … 118 110 119 111 // 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); 121 113 122 114 // the distance_scale offset is used to avoid singularity that occurs at global illumination when 123 115 // 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 129 119 // if surface normal perpenticular to view dir, approx. half of the samples will not count 130 120 // => 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; 133 125 #endif 134 total_ao += cos_angle * intensity;135 126 } 136 127 … … 138 129 } 139 130 131 #pragma position_invariant main 140 132 141 133 /** The mrt shader for screen space ambient occlusion … … 143 135 pixel main(fragment IN, 144 136 uniform sampler2D colors, 145 uniform sampler2D positions,146 137 uniform sampler2D normals, 147 138 uniform sampler2D noiseTexture, … … 161 152 162 153 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 170 156 /// reconstruct position from the eye space depth 171 157 float3 viewDir = IN.view; 172 158 const float eyeDepth = tex2Dlod(colors, float4(IN.texCoord, 0, 0)).w; 173 174 159 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 189 177 190 178 ///////////////// 191 179 //-- compute temporally smoothing 192 180 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 204 183 205 184 // calculate projected depth … … 208 187 209 188 // the current depth projected into the old frame 210 const float projDepth = projPos.z ;189 const float projDepth = projPos.z * 1e-3f; 211 190 212 191 // 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; 214 193 215 194 // retrieve the sample from the last frame 216 195 float4 oldCol = tex2D(oldTex, tex); 217 196 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; 220 200 221 201 222 202 //const float oldNumSamples = oldCol.y; 223 const float oldWeight = clamp(oldCol. z, 0, temporalCoherence);203 const float oldWeight = clamp(oldCol.y, 0, temporalCoherence); 224 204 225 205 float newWeight; … … 227 207 // the number of valid samples in this frame 228 208 //const float newNumSamples = ao.y; 229 230 209 231 210 if (//(temporalCoherence > 0) && … … 239 218 // increase the weight for convergence 240 219 newWeight = oldWeight + 1.0f; 241 OUT.illum_col.x y = (ao.xy + oldCol.xy* oldWeight) / newWeight;220 OUT.illum_col.x = (ao.x + oldCol.x * oldWeight) / newWeight; 242 221 //if (!(oldNumSamples > ao.y - 1.5f)) newWeight = 0; 243 222 } 244 223 else 245 224 { 246 OUT.illum_col.x y = ao.xy;225 OUT.illum_col.x = ao.x; 247 226 newWeight = 0; 248 227 } 249 228 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; 252 232 253 233 return OUT; 234 } 235 236 237 float 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; 254 255 } 255 256 … … 257 258 pixel combine(fragment IN, 258 259 uniform sampler2D colors, 259 uniform sampler2D ssaoTex) 260 uniform sampler2D ssaoTex, 261 uniform float2 filterOffs[NUM_DOWNSAMPLES], 262 uniform float filterWeights[NUM_DOWNSAMPLES] 263 ) 260 264 { 261 265 pixel OUT; 262 266 263 267 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); 265 272 266 273 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); 268 275 OUT.illum_col.w = col.w; 269 276
Note: See TracChangeset
for help on using the changeset viewer.