[2966] | 1 | #include "../shaderenv.h"
|
---|
[3193] | 2 | #include "common.h"
|
---|
[2966] | 3 |
|
---|
[2876] | 4 | struct fragment
|
---|
| 5 | {
|
---|
| 6 | // normalized screen position
|
---|
| 7 | float4 pos: WPOS;
|
---|
[3009] | 8 | float2 texCoord: TEXCOORD0;
|
---|
| 9 | float3 view: TEXCOORD1;
|
---|
[2876] | 10 | };
|
---|
| 11 |
|
---|
| 12 |
|
---|
| 13 | struct pixel
|
---|
| 14 | {
|
---|
| 15 | float4 color: COLOR0;
|
---|
[3167] | 16 | float3 normal: COLOR1;
|
---|
| 17 | float3 diffVal: COLOR2;
|
---|
[2876] | 18 | };
|
---|
| 19 |
|
---|
| 20 |
|
---|
| 21 | /** function for standard deferred shading
|
---|
| 22 | */
|
---|
| 23 | float4 shade(fragment IN,
|
---|
| 24 | uniform float4 color,
|
---|
| 25 | uniform float3 normal,
|
---|
[2952] | 26 | float3 lightDir)
|
---|
[2876] | 27 | {
|
---|
[2954] | 28 | // diffuse intensity
|
---|
| 29 | const float angle = saturate(dot(normal, lightDir));
|
---|
| 30 |
|
---|
| 31 | float4 lightDiffuse = glstate.light[0].diffuse;
|
---|
| 32 | float4 diffuse = angle * lightDiffuse;
|
---|
[2876] | 33 |
|
---|
| 34 | // global ambient
|
---|
[2954] | 35 | const float4 ambient = glstate.light[0].ambient;
|
---|
| 36 |
|
---|
[2968] | 37 | float4 outColor;
|
---|
[2967] | 38 |
|
---|
[3005] | 39 | // hack: prevent shading the sky
|
---|
[3009] | 40 | if (color.w > 1e19f) outColor = color;
|
---|
[2974] | 41 | else outColor = (ambient + diffuse) * color;
|
---|
[2968] | 42 |
|
---|
| 43 | return outColor;
|
---|
[2876] | 44 | }
|
---|
| 45 |
|
---|
| 46 |
|
---|
| 47 |
|
---|
| 48 | /** The mrt shader for standard rendering
|
---|
| 49 | */
|
---|
[2874] | 50 | pixel main(fragment IN,
|
---|
| 51 | uniform sampler2D colors,
|
---|
[2952] | 52 | uniform sampler2D normals,
|
---|
[2991] | 53 | uniform float3 lightDir
|
---|
[2874] | 54 | )
|
---|
| 55 | {
|
---|
| 56 | pixel OUT;
|
---|
| 57 |
|
---|
[3009] | 58 | float4 norm = tex2D(normals, IN.texCoord);
|
---|
| 59 | float4 color = tex2Dlod(colors, float4(IN.texCoord, 0, 0));
|
---|
| 60 |
|
---|
[2945] | 61 | float3 normal = normalize(norm.xyz);
|
---|
[3089] | 62 | float4 col = shade(IN, color, normal, lightDir);
|
---|
[2874] | 63 |
|
---|
[3099] | 64 | OUT.color = col;
|
---|
[3095] | 65 | // store scaled view vector so wie don't have to normalize for e.g., ssao
|
---|
[3195] | 66 | //OUT.color.w = color.w / length(IN.view);
|
---|
| 67 | OUT.color.w = color.w;
|
---|
| 68 |
|
---|
[2874] | 69 | return OUT;
|
---|
[2876] | 70 | }
|
---|
[2892] | 71 |
|
---|
[2944] | 72 |
|
---|
| 73 | float CalcShadowTerm(fragment IN,
|
---|
| 74 | uniform sampler2D shadowMap,
|
---|
[3025] | 75 | uniform float scale,
|
---|
[2944] | 76 | uniform float2 lightSpacePos,
|
---|
[2952] | 77 | uniform float depth,
|
---|
[2966] | 78 | uniform float2 samples[NUM_PCF_TABS],
|
---|
[3025] | 79 | uniform float weights[NUM_PCF_TABS],
|
---|
[2944] | 80 | uniform sampler2D noiseTexture
|
---|
| 81 | )
|
---|
| 82 | {
|
---|
[2954] | 83 | //float shadowDepth = tex2D(shadowMap, lightSpacePos).x;
|
---|
| 84 | //return step(depth, shadowDepth);
|
---|
[2944] | 85 |
|
---|
[3025] | 86 | float total_d = .0f;
|
---|
| 87 | float total_w = .0f;
|
---|
| 88 |
|
---|
[2966] | 89 | for (int i = 0; i < NUM_PCF_TABS; ++ i)
|
---|
[2944] | 90 | {
|
---|
[3284] | 91 | float2 offset;
|
---|
[3025] | 92 | const float w = weights[i];
|
---|
[2944] | 93 |
|
---|
| 94 | #if 1
|
---|
| 95 | ////////////////////
|
---|
| 96 | //-- add random noise: reflect around random normal vector (warning: slow!)
|
---|
| 97 |
|
---|
[3284] | 98 | float2 mynoise = tex2D(noiseTexture, IN.texCoord * 4.0f).xy;
|
---|
| 99 | //offset = myreflect(samples[i], mynoise);
|
---|
| 100 | offset = myrotate(samples[i], mynoise.x);
|
---|
[2944] | 101 | #else
|
---|
[3284] | 102 | offset = samples[i];
|
---|
[2944] | 103 | #endif
|
---|
| 104 | // weight with projected coordinate to reach similar kernel size for near and far
|
---|
[3284] | 105 | float2 texcoord = lightSpacePos + offset * scale;
|
---|
[2944] | 106 |
|
---|
| 107 | float shadowDepth = tex2D(shadowMap, texcoord).x;
|
---|
| 108 |
|
---|
[3025] | 109 | total_d += w * step(depth, shadowDepth);
|
---|
| 110 | total_w += w;
|
---|
[2944] | 111 | }
|
---|
| 112 |
|
---|
[3025] | 113 | total_d /= (float)total_w;
|
---|
[2944] | 114 |
|
---|
| 115 | return total_d;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
[3025] | 118 |
|
---|
[2892] | 119 | pixel main_shadow(fragment IN,
|
---|
| 120 | uniform sampler2D colors,
|
---|
| 121 | uniform sampler2D positions,
|
---|
| 122 | uniform sampler2D normals,
|
---|
| 123 | uniform sampler2D shadowMap,
|
---|
[2893] | 124 | uniform float4x4 shadowMatrix,
|
---|
[2952] | 125 | uniform float sampleWidth,
|
---|
[3092] | 126 | uniform sampler2D noiseTex,
|
---|
[2966] | 127 | uniform float2 samples[NUM_PCF_TABS],
|
---|
[3024] | 128 | uniform float weights[NUM_PCF_TABS],
|
---|
[3009] | 129 | uniform float3 lightDir,
|
---|
| 130 | uniform float3 eyePos,
|
---|
| 131 | uniform float3 bl,
|
---|
| 132 | uniform float3 br,
|
---|
| 133 | uniform float3 tl,
|
---|
| 134 | uniform float3 tr
|
---|
[2944] | 135 | )
|
---|
[2928] | 136 | {
|
---|
| 137 | pixel OUT;
|
---|
[2944] | 138 |
|
---|
[3168] | 139 | const float3 normal = tex2D(normals, IN.texCoord.xy);
|
---|
[3009] | 140 | float4 color = tex2Dlod(colors, float4(IN.texCoord, 0, 0));
|
---|
| 141 |
|
---|
| 142 | /// reconstruct position from the eye space depth
|
---|
| 143 | float3 viewDir = IN.view;
|
---|
[3018] | 144 | const float lenView = length(viewDir);
|
---|
| 145 | viewDir /= lenView;
|
---|
| 146 |
|
---|
[3009] | 147 | const float eyeDepth = tex2Dlod(colors, float4(IN.texCoord, 0, 0)).w;
|
---|
[3034] | 148 | const float4 worldPos = float4(eyePos - viewDir * eyeDepth, 1);
|
---|
[3009] | 149 |
|
---|
[2945] | 150 | // diffuse intensity
|
---|
| 151 | const float angle = saturate(dot(normal, lightDir));
|
---|
[2959] | 152 | const float4 lightDiffuse = glstate.light[0].diffuse;
|
---|
[3017] | 153 |
|
---|
[2954] | 154 | float4 diffuse = lightDiffuse * angle;
|
---|
| 155 |
|
---|
[3009] | 156 | // hack: prevent shadowing the sky
|
---|
| 157 | const bool useShading = (color.w < 1e19f);
|
---|
| 158 |
|
---|
[2945] | 159 | // calc diffuse illumination + shadow term
|
---|
[3009] | 160 | if (useShading &&
|
---|
| 161 | (angle > 1e-3f) // shadow only if diffuse color has some minimum intensity
|
---|
[2945] | 162 | )
|
---|
| 163 | {
|
---|
[3034] | 164 | float4 lightSpacePos = mul(shadowMatrix, worldPos);
|
---|
[2945] | 165 | lightSpacePos /= lightSpacePos.w;
|
---|
[2944] | 166 |
|
---|
[3092] | 167 | float shadowTerm = CalcShadowTerm(IN, shadowMap, sampleWidth, lightSpacePos.xy, lightSpacePos.z, samples, weights, noiseTex);
|
---|
[2945] | 168 | diffuse *= shadowTerm;
|
---|
[2944] | 169 | }
|
---|
[2945] | 170 |
|
---|
[2974] | 171 | // light ambient term
|
---|
[2954] | 172 | const float4 ambient = glstate.light[0].ambient;
|
---|
[3009] | 173 | // compute shading
|
---|
| 174 | OUT.color = useShading ? (ambient + diffuse) * color : color;
|
---|
[3087] | 175 | // store scaled view vector from now on so wie don't have to normalize later (e.g., for ssao)
|
---|
[3137] | 176 | //OUT.color.w = color.w / lenView;
|
---|
[3193] | 177 | OUT.color.w = color.w;
|
---|
[2991] | 178 |
|
---|
[2928] | 179 | return OUT;
|
---|
| 180 | }
|
---|
[2965] | 181 |
|
---|
[3155] | 182 |
|
---|
| 183 | float4 Output(fragment IN, uniform sampler2D colors): COLOR
|
---|
| 184 | {
|
---|
| 185 | return tex2Dlod(colors, float4(IN.texCoord, 0, 0));
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 |
|
---|
| 189 | float4 ScaleDepth(fragment IN,
|
---|
| 190 | uniform sampler2D colors): COLOR
|
---|
| 191 | {
|
---|
| 192 | float4 color = tex2Dlod(colors, float4(IN.texCoord, 0, 0));
|
---|
| 193 | // store scaled view vector so wie don't have to normalize for e.g., ssao
|
---|
| 194 | color.w /= length(IN.view);
|
---|
| 195 |
|
---|
| 196 | return color;
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 |
|
---|
| 200 | /** This shader computes the reprojection and checks
|
---|
| 201 | if the reprojected pixel from last frame is still
|
---|
| 202 | valid in the current frame
|
---|
| 203 | */
|
---|
[3206] | 204 | inline float2 PixelValid(sampler2D oldTex,
|
---|
[3155] | 205 | float4 color,
|
---|
[3198] | 206 | float3 difVec,
|
---|
[3155] | 207 | float2 texCoord,
|
---|
| 208 | float3 viewDir,
|
---|
| 209 | float3 oldEyePos,
|
---|
| 210 | float4x4 modelViewProj,
|
---|
| 211 | float4x4 oldModelViewProj,
|
---|
| 212 | float3 oldbl,
|
---|
| 213 | float3 oldbr,
|
---|
| 214 | float3 oldtl,
|
---|
| 215 | float3 oldtr
|
---|
| 216 | )
|
---|
[3081] | 217 | {
|
---|
[3155] | 218 | // reconstruct position from the eye space depth
|
---|
| 219 | const float eyeSpaceDepth = color.w;
|
---|
| 220 | const float4 worldPos = float4(-viewDir * eyeSpaceDepth, 1.0f);
|
---|
[3081] | 221 |
|
---|
[3155] | 222 |
|
---|
[3081] | 223 |
|
---|
| 224 | ////////////////
|
---|
| 225 | //-- calculcate the current projected posiion (also used for next frame)
|
---|
| 226 |
|
---|
[3155] | 227 | float4 projPos = mul(modelViewProj, worldPos);
|
---|
| 228 | const float invw = 1.0f / projPos.w;
|
---|
| 229 | projPos *= invw;
|
---|
[3081] | 230 |
|
---|
[3155] | 231 | // compute position from old frame for dynamic objects + translational portion
|
---|
[3227] | 232 | //const float3 translatedPos = difVec - oldEyePos + worldPos.xyz;
|
---|
[3306] | 233 | // don't use difVec here: want to detect if the actual pixel has changed => ssao changed
|
---|
[3230] | 234 | const float3 translatedPos = -oldEyePos + worldPos.xyz;
|
---|
[3081] | 235 |
|
---|
| 236 |
|
---|
| 237 | /////////////////
|
---|
[3155] | 238 | //-- reproject into old frame and calculate texture position of sample in old frame
|
---|
[3081] | 239 |
|
---|
[3155] | 240 | // note: the old model view matrix only holds the view orientation part
|
---|
[3156] | 241 | float4 backProjPos = mul(oldModelViewProj, float4(translatedPos, 1.0f));
|
---|
[3155] | 242 | backProjPos /= backProjPos.w;
|
---|
[3081] | 243 |
|
---|
| 244 | // fit from unit cube into 0 .. 1
|
---|
[3156] | 245 | const float2 oldTexCoords = backProjPos.xy * 0.5f + 0.5f;
|
---|
| 246 | //const float2 oldTexCoords = texCoord;
|
---|
[3081] | 247 | // retrieve the sample from the last frame
|
---|
[3155] | 248 | const float4 oldPixel = tex2Dlod(oldTex, float4(oldTexCoords, .0f, .0f));
|
---|
[3081] | 249 |
|
---|
[3155] | 250 | // calculate eye space position of sample in old frame
|
---|
| 251 | const float oldEyeSpaceDepth = oldPixel.w;
|
---|
[3081] | 252 |
|
---|
[3155] | 253 | // vector from eye pos to old sample
|
---|
[3156] | 254 | const float3 oldViewDir = Interpol(oldTexCoords, oldbl, oldbr, oldtl, oldtr);
|
---|
[3155] | 255 | const float invLen = 1.0f / length(oldViewDir);
|
---|
| 256 | const float projectedEyeSpaceDepth = invLen * length(translatedPos);
|
---|
| 257 |
|
---|
| 258 | const float depthDif = abs(1.0f - oldEyeSpaceDepth / projectedEyeSpaceDepth);
|
---|
[3198] | 259 | const float squaredLen = SqrLen(difVec);
|
---|
[3159] | 260 |
|
---|
[3204] | 261 | // test if this pixel was valid in the old frame
|
---|
[3306] | 262 | float IsPixelValid;
|
---|
[3155] | 263 |
|
---|
[3306] | 264 | // check if the pixel belonged to a dyanmic object in the last frame
|
---|
[3190] | 265 | const bool oldDynamic = (squaredLen > DYNAMIC_OBJECTS_THRESHOLD);
|
---|
| 266 | const bool newDynamic = (oldPixel.z > DYNAMIC_OBJECTS_THRESHOLD);
|
---|
[3189] | 267 |
|
---|
[3204] | 268 | // actually 0 means pixel is valid
|
---|
| 269 | const float pixelIsValid = 0.0f;
|
---|
[3207] | 270 | // means that we only use slight temporal coherence over some frames
|
---|
| 271 | // so that there si no noticeable drag
|
---|
[3204] | 272 | const float pixelCouldBeValid = 2.0f;
|
---|
[3207] | 273 | // this pixel information has to be discarded in order to not create artifacts
|
---|
[3306] | 274 | const float pixelIsNotValid = 100.0f;
|
---|
[3204] | 275 |
|
---|
[3306] | 276 | const float xOffs = 0;// 0.5f / 1024.0f;
|
---|
| 277 | const float yOffs = 0;// 0.5f / 768.0f;
|
---|
| 278 |
|
---|
| 279 | if ((oldTexCoords.x < xOffs) || (oldTexCoords.x > (1.0f - xOffs)) ||
|
---|
| 280 | (oldTexCoords.y < yOffs) || (oldTexCoords.y > (1.0f - yOffs))
|
---|
| 281 | )
|
---|
[3081] | 282 | {
|
---|
[3306] | 283 | IsPixelValid = pixelIsNotValid;
|
---|
[3081] | 284 | }
|
---|
[3306] | 285 | else if (// check if changed from dynamic to not dynamic object
|
---|
[3227] | 286 | ((oldDynamic && !newDynamic) || (!oldDynamic && newDynamic) ||
|
---|
| 287 | (
|
---|
[3306] | 288 | (oldEyeSpaceDepth < DEPTH_THRESHOLD) && (projectedEyeSpaceDepth < DEPTH_THRESHOLD) &&
|
---|
| 289 | // check if we have a dynamic object
|
---|
[3230] | 290 | (oldDynamic || newDynamic) &&
|
---|
[3306] | 291 | // and is a depth discontinuity
|
---|
[3227] | 292 | (depthDif > MIN_DEPTH_DIFF))))
|
---|
[3081] | 293 | {
|
---|
[3306] | 294 | IsPixelValid = pixelCouldBeValid;
|
---|
[3081] | 295 | }
|
---|
[3204] | 296 | else
|
---|
| 297 | {
|
---|
[3306] | 298 | IsPixelValid = pixelIsValid;
|
---|
[3204] | 299 | }
|
---|
[3081] | 300 |
|
---|
[3306] | 301 | return float2(IsPixelValid, abs(oldEyeSpaceDepth - projectedEyeSpaceDepth));
|
---|
[3081] | 302 | }
|
---|
| 303 |
|
---|
[3134] | 304 |
|
---|
[3204] | 305 | /** This function is called during downsampling of the buffers
|
---|
| 306 | for ssao.
|
---|
| 307 | */
|
---|
[3199] | 308 | pixel PrepareSsao(fragment IN,
|
---|
[3155] | 309 | uniform sampler2D colorsTex,
|
---|
[3199] | 310 | uniform sampler2D normalsTex,
|
---|
[3167] | 311 | uniform sampler2D diffVals,
|
---|
[3155] | 312 | uniform sampler2D oldTex,
|
---|
| 313 | uniform float4x4 modelViewProj,
|
---|
| 314 | uniform float4x4 oldModelViewProj,
|
---|
| 315 | uniform float3 oldbl,
|
---|
| 316 | uniform float3 oldbr,
|
---|
| 317 | uniform float3 oldtl,
|
---|
| 318 | uniform float3 oldtr,
|
---|
[3167] | 319 | uniform float3 oldEyePos
|
---|
[3199] | 320 | )
|
---|
[3155] | 321 | {
|
---|
[3199] | 322 | pixel pix;
|
---|
[3155] | 323 | float4 color = tex2Dlod(colorsTex, float4(IN.texCoord, 0, 0));
|
---|
| 324 | // store scaled view vector so wie don't have to normalize for e.g., ssao
|
---|
| 325 | color.w /= length(IN.view);
|
---|
[3137] | 326 |
|
---|
[3198] | 327 | const float4 difVec = tex2Dlod(diffVals, float4(IN.texCoord, 0, 0));
|
---|
[3226] | 328 | // normalize normal once more because of bilinear interpolation
|
---|
[3199] | 329 | const float3 normal = normalize(tex2Dlod(normalsTex, float4(IN.texCoord, 0, 0)).xyz);
|
---|
[3198] | 330 |
|
---|
[3155] | 331 | // do reprojection and filter out the pixels that are not save
|
---|
[3206] | 332 | float2 pValid = PixelValid(oldTex,
|
---|
[3192] | 333 | color,
|
---|
| 334 | difVec.xyz,
|
---|
| 335 | IN.texCoord,
|
---|
[3198] | 336 | IN.view,
|
---|
[3192] | 337 | oldEyePos,
|
---|
| 338 | modelViewProj,
|
---|
| 339 | oldModelViewProj,
|
---|
| 340 | oldbl, oldbr, oldtl, oldtr
|
---|
| 341 | );
|
---|
[3137] | 342 |
|
---|
[3199] | 343 | pix.color = color;
|
---|
[3206] | 344 | pix.color.xy = pValid.xy;
|
---|
[3212] | 345 | pix.color.z = color.w;
|
---|
| 346 |
|
---|
[3199] | 347 | pix.normal = normal;
|
---|
[3167] | 348 |
|
---|
[3199] | 349 | return pix;
|
---|
[3137] | 350 | }
|
---|
| 351 |
|
---|
| 352 |
|
---|
| 353 | float4 DownSample(fragment IN,
|
---|
| 354 | uniform sampler2D colors,
|
---|
| 355 | uniform float2 downSampleOffs[NUM_DOWNSAMPLES]): COLOR
|
---|
| 356 | {
|
---|
| 357 | // let bilinear filtering do its work
|
---|
| 358 | float4 color = tex2Dlod(colors, float4(IN.texCoord, 0, 0));
|
---|
| 359 | return color;
|
---|
| 360 | } |
---|