[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 | {
|
---|
| 91 | const float2 offset = samples[i];
|
---|
[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 |
|
---|
[3009] | 98 | float2 mynoise = tex2D(noiseTexture, IN.texCoord).xy;
|
---|
[2944] | 99 | const float2 offsetTransformed = myreflect(offset, mynoise);
|
---|
| 100 | #else
|
---|
| 101 | const float2 offsetTransformed = offset;
|
---|
| 102 | #endif
|
---|
| 103 | // weight with projected coordinate to reach similar kernel size for near and far
|
---|
[3025] | 104 | float2 texcoord = lightSpacePos + offsetTransformed * scale;
|
---|
[2944] | 105 |
|
---|
| 106 | float shadowDepth = tex2D(shadowMap, texcoord).x;
|
---|
| 107 |
|
---|
[3025] | 108 | total_d += w * step(depth, shadowDepth);
|
---|
| 109 | total_w += w;
|
---|
[2944] | 110 | }
|
---|
| 111 |
|
---|
[3025] | 112 | total_d /= (float)total_w;
|
---|
[2944] | 113 |
|
---|
| 114 | return total_d;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
[3025] | 117 |
|
---|
[2892] | 118 | pixel main_shadow(fragment IN,
|
---|
| 119 | uniform sampler2D colors,
|
---|
| 120 | uniform sampler2D positions,
|
---|
| 121 | uniform sampler2D normals,
|
---|
| 122 | uniform sampler2D shadowMap,
|
---|
[2893] | 123 | uniform float4x4 shadowMatrix,
|
---|
[2952] | 124 | uniform float sampleWidth,
|
---|
[3092] | 125 | uniform sampler2D noiseTex,
|
---|
[2966] | 126 | uniform float2 samples[NUM_PCF_TABS],
|
---|
[3024] | 127 | uniform float weights[NUM_PCF_TABS],
|
---|
[3009] | 128 | uniform float3 lightDir,
|
---|
| 129 | uniform float3 eyePos,
|
---|
| 130 | uniform float3 bl,
|
---|
| 131 | uniform float3 br,
|
---|
| 132 | uniform float3 tl,
|
---|
| 133 | uniform float3 tr
|
---|
[2944] | 134 | )
|
---|
[2928] | 135 | {
|
---|
| 136 | pixel OUT;
|
---|
[2944] | 137 |
|
---|
[3168] | 138 | const float3 normal = tex2D(normals, IN.texCoord.xy);
|
---|
[3009] | 139 | float4 color = tex2Dlod(colors, float4(IN.texCoord, 0, 0));
|
---|
| 140 |
|
---|
| 141 | /// reconstruct position from the eye space depth
|
---|
| 142 | float3 viewDir = IN.view;
|
---|
[3018] | 143 | const float lenView = length(viewDir);
|
---|
| 144 | viewDir /= lenView;
|
---|
| 145 |
|
---|
[3009] | 146 | const float eyeDepth = tex2Dlod(colors, float4(IN.texCoord, 0, 0)).w;
|
---|
[3034] | 147 | const float4 worldPos = float4(eyePos - viewDir * eyeDepth, 1);
|
---|
[3009] | 148 |
|
---|
[2945] | 149 | // diffuse intensity
|
---|
| 150 | const float angle = saturate(dot(normal, lightDir));
|
---|
[2959] | 151 | const float4 lightDiffuse = glstate.light[0].diffuse;
|
---|
[3017] | 152 |
|
---|
[2954] | 153 | float4 diffuse = lightDiffuse * angle;
|
---|
| 154 |
|
---|
[3009] | 155 | // hack: prevent shadowing the sky
|
---|
| 156 | const bool useShading = (color.w < 1e19f);
|
---|
| 157 |
|
---|
[2945] | 158 | // calc diffuse illumination + shadow term
|
---|
[3009] | 159 | if (useShading &&
|
---|
| 160 | (angle > 1e-3f) // shadow only if diffuse color has some minimum intensity
|
---|
[2945] | 161 | )
|
---|
| 162 | {
|
---|
[3034] | 163 | float4 lightSpacePos = mul(shadowMatrix, worldPos);
|
---|
[2945] | 164 | lightSpacePos /= lightSpacePos.w;
|
---|
[2944] | 165 |
|
---|
[3092] | 166 | float shadowTerm = CalcShadowTerm(IN, shadowMap, sampleWidth, lightSpacePos.xy, lightSpacePos.z, samples, weights, noiseTex);
|
---|
[2945] | 167 | diffuse *= shadowTerm;
|
---|
[2944] | 168 | }
|
---|
[2945] | 169 |
|
---|
[2974] | 170 | // light ambient term
|
---|
[2954] | 171 | const float4 ambient = glstate.light[0].ambient;
|
---|
[3009] | 172 | // compute shading
|
---|
| 173 | OUT.color = useShading ? (ambient + diffuse) * color : color;
|
---|
[3087] | 174 | // store scaled view vector from now on so wie don't have to normalize later (e.g., for ssao)
|
---|
[3137] | 175 | //OUT.color.w = color.w / lenView;
|
---|
[3193] | 176 | OUT.color.w = color.w;
|
---|
[2991] | 177 |
|
---|
[2928] | 178 | return OUT;
|
---|
| 179 | }
|
---|
[2965] | 180 |
|
---|
[3155] | 181 |
|
---|
| 182 | float4 Output(fragment IN, uniform sampler2D colors): COLOR
|
---|
| 183 | {
|
---|
| 184 | return tex2Dlod(colors, float4(IN.texCoord, 0, 0));
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 |
|
---|
| 188 | float4 ScaleDepth(fragment IN,
|
---|
| 189 | uniform sampler2D colors): COLOR
|
---|
| 190 | {
|
---|
| 191 | float4 color = tex2Dlod(colors, float4(IN.texCoord, 0, 0));
|
---|
| 192 | // store scaled view vector so wie don't have to normalize for e.g., ssao
|
---|
| 193 | color.w /= length(IN.view);
|
---|
| 194 |
|
---|
| 195 | return color;
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 |
|
---|
| 199 | /** This shader computes the reprojection and checks
|
---|
| 200 | if the reprojected pixel from last frame is still
|
---|
| 201 | valid in the current frame
|
---|
| 202 | */
|
---|
[3206] | 203 | inline float2 PixelValid(sampler2D oldTex,
|
---|
[3155] | 204 | float4 color,
|
---|
[3198] | 205 | float3 difVec,
|
---|
[3155] | 206 | float2 texCoord,
|
---|
| 207 | float3 viewDir,
|
---|
| 208 | float3 oldEyePos,
|
---|
| 209 | float4x4 modelViewProj,
|
---|
| 210 | float4x4 oldModelViewProj,
|
---|
| 211 | float3 oldbl,
|
---|
| 212 | float3 oldbr,
|
---|
| 213 | float3 oldtl,
|
---|
| 214 | float3 oldtr
|
---|
| 215 | )
|
---|
[3081] | 216 | {
|
---|
[3155] | 217 | // reconstruct position from the eye space depth
|
---|
| 218 | const float eyeSpaceDepth = color.w;
|
---|
| 219 | const float4 worldPos = float4(-viewDir * eyeSpaceDepth, 1.0f);
|
---|
[3081] | 220 |
|
---|
[3155] | 221 |
|
---|
[3081] | 222 |
|
---|
| 223 | ////////////////
|
---|
| 224 | //-- calculcate the current projected posiion (also used for next frame)
|
---|
| 225 |
|
---|
[3155] | 226 | float4 projPos = mul(modelViewProj, worldPos);
|
---|
| 227 | const float invw = 1.0f / projPos.w;
|
---|
| 228 | projPos *= invw;
|
---|
[3081] | 229 |
|
---|
[3155] | 230 | // compute position from old frame for dynamic objects + translational portion
|
---|
[3198] | 231 | const float3 translatedPos = difVec - oldEyePos + worldPos.xyz;
|
---|
[3081] | 232 |
|
---|
| 233 |
|
---|
| 234 | /////////////////
|
---|
[3155] | 235 | //-- reproject into old frame and calculate texture position of sample in old frame
|
---|
[3081] | 236 |
|
---|
[3155] | 237 | // note: the old model view matrix only holds the view orientation part
|
---|
[3156] | 238 | float4 backProjPos = mul(oldModelViewProj, float4(translatedPos, 1.0f));
|
---|
[3155] | 239 | backProjPos /= backProjPos.w;
|
---|
[3081] | 240 |
|
---|
| 241 | // fit from unit cube into 0 .. 1
|
---|
[3156] | 242 | const float2 oldTexCoords = backProjPos.xy * 0.5f + 0.5f;
|
---|
| 243 | //const float2 oldTexCoords = texCoord;
|
---|
[3081] | 244 | // retrieve the sample from the last frame
|
---|
[3155] | 245 | const float4 oldPixel = tex2Dlod(oldTex, float4(oldTexCoords, .0f, .0f));
|
---|
[3081] | 246 |
|
---|
[3155] | 247 | // calculate eye space position of sample in old frame
|
---|
| 248 | const float oldEyeSpaceDepth = oldPixel.w;
|
---|
[3081] | 249 |
|
---|
[3155] | 250 | // vector from eye pos to old sample
|
---|
[3156] | 251 | const float3 oldViewDir = Interpol(oldTexCoords, oldbl, oldbr, oldtl, oldtr);
|
---|
[3155] | 252 | const float invLen = 1.0f / length(oldViewDir);
|
---|
| 253 | const float projectedEyeSpaceDepth = invLen * length(translatedPos);
|
---|
| 254 |
|
---|
| 255 | const float depthDif = abs(1.0f - oldEyeSpaceDepth / projectedEyeSpaceDepth);
|
---|
[3198] | 256 | const float squaredLen = SqrLen(difVec);
|
---|
[3159] | 257 |
|
---|
[3204] | 258 | // test if this pixel was valid in the old frame
|
---|
| 259 | float pixelValid;
|
---|
[3155] | 260 |
|
---|
[3190] | 261 | const bool oldDynamic = (squaredLen > DYNAMIC_OBJECTS_THRESHOLD);
|
---|
| 262 | const bool newDynamic = (oldPixel.z > DYNAMIC_OBJECTS_THRESHOLD);
|
---|
[3189] | 263 |
|
---|
[3204] | 264 | // actually 0 means pixel is valid
|
---|
| 265 | const float pixelIsValid = 0.0f;
|
---|
[3207] | 266 | // means that we only use slight temporal coherence over some frames
|
---|
| 267 | // so that there si no noticeable drag
|
---|
[3204] | 268 | const float pixelCouldBeValid = 2.0f;
|
---|
[3207] | 269 | // this pixel information has to be discarded in order to not create artifacts
|
---|
[3204] | 270 | const float pixelNotValid = 10.0f;
|
---|
| 271 |
|
---|
[3206] | 272 | if ((oldTexCoords.x < .0f) || (oldTexCoords.x >= 1.0f) ||
|
---|
| 273 | (oldTexCoords.y < .0f) || (oldTexCoords.y >= 1.0f))
|
---|
[3081] | 274 | {
|
---|
[3204] | 275 | pixelValid = pixelNotValid;
|
---|
[3081] | 276 | }
|
---|
[3210] | 277 | else if (
|
---|
[3206] | 278 | // check if changed from dynamic to not dynamic object
|
---|
| 279 | (oldDynamic && !newDynamic) || (!oldDynamic && newDynamic) ||
|
---|
| 280 | // check if we have a dynamic object and is a depth discontinuity
|
---|
| 281 | ((oldDynamic || newDynamic) && (depthDif <= MIN_DEPTH_DIFF)))
|
---|
[3081] | 282 | {
|
---|
[3204] | 283 | pixelValid = pixelCouldBeValid;
|
---|
[3081] | 284 | }
|
---|
[3204] | 285 | else
|
---|
| 286 | {
|
---|
| 287 | pixelValid = pixelIsValid;
|
---|
| 288 | }
|
---|
[3081] | 289 |
|
---|
[3206] | 290 | return float2(pixelValid, abs(oldEyeSpaceDepth - projectedEyeSpaceDepth));
|
---|
[3081] | 291 | }
|
---|
| 292 |
|
---|
[3134] | 293 |
|
---|
[3204] | 294 | /** This function is called during downsampling of the buffers
|
---|
| 295 | for ssao.
|
---|
| 296 | */
|
---|
[3199] | 297 | pixel PrepareSsao(fragment IN,
|
---|
[3155] | 298 | uniform sampler2D colorsTex,
|
---|
[3199] | 299 | uniform sampler2D normalsTex,
|
---|
[3167] | 300 | uniform sampler2D diffVals,
|
---|
[3155] | 301 | uniform sampler2D oldTex,
|
---|
| 302 | uniform float4x4 modelViewProj,
|
---|
| 303 | uniform float4x4 oldModelViewProj,
|
---|
| 304 | uniform float3 oldbl,
|
---|
| 305 | uniform float3 oldbr,
|
---|
| 306 | uniform float3 oldtl,
|
---|
| 307 | uniform float3 oldtr,
|
---|
[3167] | 308 | uniform float3 oldEyePos
|
---|
[3199] | 309 | )
|
---|
[3155] | 310 | {
|
---|
[3199] | 311 | pixel pix;
|
---|
[3155] | 312 | float4 color = tex2Dlod(colorsTex, float4(IN.texCoord, 0, 0));
|
---|
| 313 | // store scaled view vector so wie don't have to normalize for e.g., ssao
|
---|
| 314 | color.w /= length(IN.view);
|
---|
[3137] | 315 |
|
---|
[3198] | 316 | const float4 difVec = tex2Dlod(diffVals, float4(IN.texCoord, 0, 0));
|
---|
[3226] | 317 | // normalize normal once more because of bilinear interpolation
|
---|
[3199] | 318 | const float3 normal = normalize(tex2Dlod(normalsTex, float4(IN.texCoord, 0, 0)).xyz);
|
---|
[3198] | 319 |
|
---|
[3155] | 320 | // do reprojection and filter out the pixels that are not save
|
---|
[3206] | 321 | float2 pValid = PixelValid(oldTex,
|
---|
[3192] | 322 | color,
|
---|
| 323 | difVec.xyz,
|
---|
| 324 | IN.texCoord,
|
---|
[3198] | 325 | IN.view,
|
---|
[3192] | 326 | oldEyePos,
|
---|
| 327 | modelViewProj,
|
---|
| 328 | oldModelViewProj,
|
---|
| 329 | oldbl, oldbr, oldtl, oldtr
|
---|
| 330 | );
|
---|
[3137] | 331 |
|
---|
[3199] | 332 | pix.color = color;
|
---|
[3206] | 333 | pix.color.xy = pValid.xy;
|
---|
[3212] | 334 | pix.color.z = color.w;
|
---|
| 335 |
|
---|
[3199] | 336 | pix.normal = normal;
|
---|
[3167] | 337 |
|
---|
[3199] | 338 | return pix;
|
---|
[3137] | 339 | }
|
---|
| 340 |
|
---|
| 341 |
|
---|
| 342 | float4 DownSample(fragment IN,
|
---|
| 343 | uniform sampler2D colors,
|
---|
| 344 | uniform float2 downSampleOffs[NUM_DOWNSAMPLES]): COLOR
|
---|
| 345 | {
|
---|
| 346 | // let bilinear filtering do its work
|
---|
| 347 | float4 color = tex2Dlod(colors, float4(IN.texCoord, 0, 0));
|
---|
| 348 | return color;
|
---|
| 349 | } |
---|