Changeset 3017 for GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders
- Timestamp:
- 10/09/08 11:19:33 (16 years ago)
- Location:
- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg
r3016 r3017 168 168 // diffuse intensity 169 169 const float angle = saturate(dot(normal, lightDir)); 170 //float4 lightDiffuse = float4(1.0f, 1.0f, 0.9f, 1.0f);171 170 const float4 lightDiffuse = glstate.light[0].diffuse; 172 //float4(1.0f, 1.0f, 0.9f, 1.0f); 173 171 174 172 float4 diffuse = lightDiffuse * angle; 175 173 -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/globillum.cg
r3016 r3017 8 8 struct fragment 9 9 { 10 // normalized screen position11 float4 pos: WPOS;12 10 float2 texCoord: TEXCOORD0; 13 11 float3 view: TEXCOORD1; … … 68 66 uniform float3 tl, 69 67 uniform float3 tr 70 //, uniform float3 viewDir68 , uniform float3 viewDir 71 69 ) 72 70 { … … 98 96 #endif 99 97 // weight with projected coordinate to reach similar kernel size for near and far 100 float2 texcoord = IN.texCoord.xy + offsetTransformed * AREA_SIZE *w;98 float2 texcoord = IN.texCoord.xy + offsetTransformed * w; 101 99 102 100 //if ((texcoord.x <= 1.0f) && (texcoord.x >= 0.0f) && (texcoord.y <= 1.0f) && (texcoord.y >= 0.0f)) ++ numSamples; … … 115 113 const float3 sample_color = sample.xyz; 116 114 115 // distance between current position and sample position controls AO intensity. 116 117 117 float3 vector_to_sample = sample_position - centerPosition.xyz; 118 118 const float length_to_sample = length(vector_to_sample); … … 124 124 125 125 const float denom = (DISTANCE_SCALE + length_to_sample * length_to_sample); 126 // distance between current position and sample position controls AO intensity. 126 127 127 const float ssao_intensity = SAMPLE_INTENSITY / denom; 128 129 128 const float illum_intensity = ILLUM_INTENSITY / denom; 130 129 131 // if normal perpenticular to view dir, only half of the samples count 132 #if 0 133 const float view_correction = 1.0f + VIEW_CORRECTION_SCALE * (1.0f - dot(currentViewDir, currentNormal)); 134 total_color.w -= cos_angle * distance_intensity * view_correction; 135 total_color.xyz += cos_angle * distance_intensity * view_correction * sample_color * ILLUM_INTENSITY; 136 #endif 130 // if normal perpenticular to view dir, only the samples approx count half 131 #if 1 132 const float view_correction = 1.0f + VIEW_CORRECTION_SCALE * dot(viewDir, currentNormal); 133 134 total_ao += cos_angle * ssao_intensity * view_correction; 135 total_color += cos_angle * illum_intensity * sample_color * view_correction; 136 #else 137 137 total_ao += cos_angle * ssao_intensity; 138 138 total_color += cos_angle * illum_intensity * sample_color; 139 #endif 139 140 } 140 141 … … 170 171 float3 normal = normalize(norm.xyz); 171 172 172 // the w coordinate from the persp. projection 173 float w = norm.w; 174 175 173 176 174 ///////////// 177 175 //-- reconstruct position from the eye space depth … … 182 180 const float3 eyeSpacePos = -viewDir * eyeDepth; 183 181 184 float3 centerPosition = eyePos + eyeSpacePos; 185 //const float3 centerPosition = tex2D(positions, IN.texCoord.xy).xyz; 186 182 const float3 centerPosition = eyePos + eyeSpacePos; 183 const float4 realPos = float4(centerPosition, 1.0f); 184 185 // calculcate the current projected depth for next frame 186 float4 currentPos = mul(modelViewProj, realPos); 187 const float w = SAMPLE_RADIUS / currentPos.w; 188 currentPos /= currentPos.w; 189 const float currentDepth = currentPos.z * 1e-3f; 187 190 188 191 /////////// 189 192 //-- compute color bleeding + ao 190 193 191 GiStruct gi = globIllum(IN, colors, noiseTexture, samples, normal, eyeSpacePos, w, bl, br, tl, tr );194 GiStruct gi = globIllum(IN, colors, noiseTexture, samples, normal, eyeSpacePos, w, bl, br, tl, tr, normalize(IN.view)); 192 195 193 196 … … 195 198 //-- compute temporally smoothing 196 199 197 float4 realPos = float4(centerPosition, 1.0f); 198 199 200 // calculcate the current projected depth for next frame 201 float4 currentPos = mul(modelViewProj, realPos); 202 currentPos /= currentPos.w; 203 const float currentDepth = currentPos.z; 204 205 206 /////////// 207 //-- reprojection new frame into old one 208 200 201 // reprojection new frame into old one 209 202 // calculate projected depth 210 203 float4 projPos = mul(oldModelViewProj, realPos); … … 212 205 213 206 // the current depth projected into the old frame 214 const float projDepth = projPos.z ;207 const float projDepth = projPos.z * 1e-3f; 215 208 216 209 // fit from unit cube into 0 .. 1 217 float2 tex = (projPos.xy)* 0.5f + 0.5f;210 float2 tex = projPos.xy * 0.5f + 0.5f; 218 211 219 212 // retrieve the sample from the last frame … … 221 214 float4 oldIllum = tex2D(oldIllumTex, tex); 222 215 223 const float oldDepth = oldSsao.w; 224 const float depthDif = 1.0f - projDepth / oldDepth; 225 226 float oldWeight = clamp(oldSsao.z, 0, temporalCoherence); 216 const float oldDepth = oldSsao.z; 217 //const float depthDif = 1.0f - projDepth / oldDepth; 218 const float depthDif = projDepth - oldDepth; 219 220 float oldWeight = clamp(oldSsao.y, 0, temporalCoherence); 227 221 228 222 float newWeight; 229 223 230 224 //const float oldNumSamples = oldSsao.y; 231 //const float oldAvgDepth = oldSsao.z; 232 225 233 226 if (//(temporalCoherence > 0.0f) && 234 227 (tex.x >= 0.0f) && (tex.x < 1.0f) && … … 241 234 newWeight = oldWeight + 1; 242 235 243 OUT.ssao_col.x y = (gi.ao + oldSsao.xy* oldWeight) / newWeight;236 OUT.ssao_col.x = (gi.ao + oldSsao.x * oldWeight) / newWeight; 244 237 OUT.illum_col.xyz = (gi.illum + oldIllum.xyz * oldWeight) / newWeight; 245 238 } … … 248 241 newWeight = 0; 249 242 250 OUT.ssao_col.x y = gi.ao.xy;243 OUT.ssao_col.x = gi.ao.x; 251 244 OUT.illum_col.xyz = gi.illum; 252 245 } 253 246 254 OUT.ssao_col. z= newWeight;255 OUT.ssao_col. w= currentDepth;247 OUT.ssao_col.y = newWeight; 248 OUT.ssao_col.z = currentDepth; 256 249 257 250 return OUT; -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/mrt.cg
r3016 r3017 17 17 float4 worldPos: TEXCOORD1; // world position 18 18 float3 normal: TEXCOORD2; 19 float4 mypos: TEXCOORD3;19 float4 projPos: TEXCOORD3; 20 20 }; 21 21 … … 28 28 float4 texCoord: TEXCOORD0; 29 29 30 float4 projPos: WPOS;30 float4 winPos: WPOS; 31 31 float4 worldPos: TEXCOORD1; // world position 32 32 float3 normal: TEXCOORD2; 33 float4 mypos: TEXCOORD3;33 float4 projPos: TEXCOORD3; 34 34 }; 35 35 … … 38 38 { 39 39 float4 col: COLOR0; 40 float 4norm: COLOR1;40 float3 norm: COLOR1; 41 41 float3 pos: COLOR2; 42 42 }; … … 59 59 60 60 OUT.normal = IN.normal; 61 OUT. mypos = OUT.position;61 OUT.projPos = OUT.position; 62 62 63 63 return OUT; … … 75 75 } 76 76 77 //#pragma position_invariant fragtex 77 78 78 79 pixel fragtex(fragin IN, … … 96 97 // hack: use combination of emmisive + diffuse (emmisive used as constant ambient term) 97 98 pix.col = (glstate.material.emission + glstate.material.diffuse) * texColor; 98 // save world space normal in third rt 99 pix.norm.xyz = IN.normal; 100 // store projection coordinates with positions (used for ssao) 101 pix.norm.w = IN.projPos.w; 99 // save world space normal in rt 100 pix.norm = IN.normal; 102 101 103 const float4 projPos = IN.mypos / IN.mypos.w;104 105 102 // hack: squeeze some information about ambient into the texture 106 103 //pix.col.w = glstate.material.emission.x; 107 104 108 105 // compute eye linear depth 109 const float 2 screenCoord = projPos.xy * 0.5f + 0.5f;110 const float magView = length(Interpol(screenCoord, bl, br, tl, tr));111 pix.col.w = length(eyePos - IN.worldPos.xyz) / magView;112 113 #if 0 114 // save world position in second render target115 pix.pos = IN.worldPos.xyz * maxDepth;116 #endif 106 const float4 projPos = IN.projPos / IN.projPos.w; 107 108 float2 screenCoord = projPos.xy * 0.5f + 0.5f; 109 110 const float3 viewVec = Interpol(screenCoord, bl, br, tl, tr); 111 const float invMagView = 1.0f / length(viewVec); 112 // note: has to done in this order, otherwise strange precision problems! 113 pix.col.w = invMagView * length(eyePos - IN.worldPos.xyz); 117 114 118 115 return pix; … … 132 129 pix.col = glstate.material.diffuse + glstate.material.emission; 133 130 134 pix.norm.xyz = IN.normal; 135 136 // store projection coordinates with positions (used for ssao) 137 pix.norm.w = IN.mypos.w; 138 139 const float4 projPos = IN.mypos / IN.mypos.w; 131 pix.norm = IN.normal; 140 132 141 133 // hack: squeeze some information about the ambient term into the target … … 143 135 144 136 // compute eye linear depth and scale with lenght to avoid sqr root in pixel shader 137 const float4 projPos = IN.projPos / IN.projPos.w; 138 145 139 float2 screenCoord = projPos.xy * 0.5f + 0.5f; 146 140 const float magView = length(Interpol(screenCoord, bl, br, tl, tr)); 147 pix.col.w = length(eyePos - IN.worldPos.xyz) / magView; 148 149 #if 0 150 pix.pos = IN.worldPos.xyz * maxDepth; 151 #endif 141 pix.col.w = length(eyePos - IN.worldPos.xyz) / magView; 152 142 153 143 return pix; -
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 -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao3d.cg
r3010 r3017 64 64 65 65 float3 mynoise = (float3)tex2D(noiseTexture, IN.texCoord.xy); 66 const float3 offsetTransformed = myreflect(offset, mynoise) * AREA_SIZE;67 //const float3 offsetTransformed = offset * AREA_SIZE;66 const float3 offsetTransformed = myreflect(offset, mynoise) * SAMPLE_RADIUS; 67 //const float3 offsetTransformed = offset * SAMPLE_RADIUS; 68 68 69 69 // compute position -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/tonemap.cg
r3016 r3017 46 46 return average; 47 47 }*/ 48 48 49 50 // let bilinear filtering do its work 49 51 float4 DownSample(frag IN, 50 52 uniform sampler2D colors,
Note: See TracChangeset
for help on using the changeset viewer.