[2966] | 1 | #include "../shaderenv.h"
|
---|
| 2 |
|
---|
| 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;
|
---|
| 16 | };
|
---|
| 17 |
|
---|
| 18 |
|
---|
[2944] | 19 | float2 myreflect(float2 pt, float2 n)
|
---|
| 20 | {
|
---|
| 21 | // distance to plane
|
---|
| 22 | float d = dot(n, pt);
|
---|
| 23 | // reflect around plane
|
---|
| 24 | float2 rpt = pt - d * 2.0f * n;
|
---|
| 25 |
|
---|
| 26 | return rpt;
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 |
|
---|
[2876] | 30 | /** function for standard deferred shading
|
---|
| 31 | */
|
---|
| 32 | float4 shade(fragment IN,
|
---|
| 33 | uniform float4 color,
|
---|
| 34 | uniform float3 normal,
|
---|
[2952] | 35 | float3 lightDir)
|
---|
[2876] | 36 | {
|
---|
[2954] | 37 | // diffuse intensity
|
---|
| 38 | const float angle = saturate(dot(normal, lightDir));
|
---|
| 39 |
|
---|
| 40 | float4 lightDiffuse = glstate.light[0].diffuse;
|
---|
| 41 | float4 diffuse = angle * lightDiffuse;
|
---|
[2876] | 42 |
|
---|
| 43 | // global ambient
|
---|
[2954] | 44 | const float4 ambient = glstate.light[0].ambient;
|
---|
| 45 |
|
---|
[2968] | 46 | float4 outColor;
|
---|
[2967] | 47 |
|
---|
[3005] | 48 | // hack: prevent shading the sky
|
---|
[3009] | 49 | if (color.w > 1e19f) outColor = color;
|
---|
[2974] | 50 | else outColor = (ambient + diffuse) * color;
|
---|
[2968] | 51 |
|
---|
| 52 | return outColor;
|
---|
[2876] | 53 | }
|
---|
| 54 |
|
---|
| 55 |
|
---|
| 56 |
|
---|
| 57 | /** The mrt shader for standard rendering
|
---|
| 58 | */
|
---|
[2874] | 59 | pixel main(fragment IN,
|
---|
| 60 | uniform sampler2D colors,
|
---|
[2952] | 61 | uniform sampler2D normals,
|
---|
[2991] | 62 | uniform float3 lightDir
|
---|
[2874] | 63 | )
|
---|
| 64 | {
|
---|
| 65 | pixel OUT;
|
---|
| 66 |
|
---|
[3009] | 67 | float4 norm = tex2D(normals, IN.texCoord);
|
---|
| 68 | float4 color = tex2Dlod(colors, float4(IN.texCoord, 0, 0));
|
---|
| 69 |
|
---|
[2945] | 70 | float3 normal = normalize(norm.xyz);
|
---|
[3089] | 71 | float4 col = shade(IN, color, normal, lightDir);
|
---|
[2874] | 72 |
|
---|
[3099] | 73 | OUT.color = col;
|
---|
| 74 | //OUT.color.xyz = normal * 0.5f + 0.5f;
|
---|
[3095] | 75 | // store scaled view vector so wie don't have to normalize for e.g., ssao
|
---|
[3097] | 76 | OUT.color.w = color.w / length(IN.view);
|
---|
[3098] | 77 |
|
---|
[2874] | 78 | return OUT;
|
---|
[2876] | 79 | }
|
---|
[2892] | 80 |
|
---|
[2944] | 81 |
|
---|
| 82 | float CalcShadowTerm(fragment IN,
|
---|
| 83 | uniform sampler2D shadowMap,
|
---|
[3025] | 84 | uniform float scale,
|
---|
[2944] | 85 | uniform float2 lightSpacePos,
|
---|
[2952] | 86 | uniform float depth,
|
---|
[2966] | 87 | uniform float2 samples[NUM_PCF_TABS],
|
---|
[3025] | 88 | uniform float weights[NUM_PCF_TABS],
|
---|
[2944] | 89 | uniform sampler2D noiseTexture
|
---|
| 90 | )
|
---|
| 91 | {
|
---|
[2954] | 92 | //float shadowDepth = tex2D(shadowMap, lightSpacePos).x;
|
---|
| 93 | //return step(depth, shadowDepth);
|
---|
[2944] | 94 |
|
---|
[3025] | 95 | float total_d = .0f;
|
---|
| 96 | float total_w = .0f;
|
---|
| 97 |
|
---|
[2966] | 98 | for (int i = 0; i < NUM_PCF_TABS; ++ i)
|
---|
[2944] | 99 | {
|
---|
| 100 | const float2 offset = samples[i];
|
---|
[3025] | 101 | const float w = weights[i];
|
---|
[2944] | 102 |
|
---|
| 103 | #if 1
|
---|
| 104 | ////////////////////
|
---|
| 105 | //-- add random noise: reflect around random normal vector (warning: slow!)
|
---|
| 106 |
|
---|
[3009] | 107 | float2 mynoise = tex2D(noiseTexture, IN.texCoord).xy;
|
---|
[2944] | 108 | const float2 offsetTransformed = myreflect(offset, mynoise);
|
---|
| 109 | #else
|
---|
| 110 | const float2 offsetTransformed = offset;
|
---|
| 111 | #endif
|
---|
| 112 | // weight with projected coordinate to reach similar kernel size for near and far
|
---|
[3025] | 113 | float2 texcoord = lightSpacePos + offsetTransformed * scale;
|
---|
[2944] | 114 |
|
---|
| 115 | float shadowDepth = tex2D(shadowMap, texcoord).x;
|
---|
| 116 |
|
---|
[3025] | 117 | total_d += w * step(depth, shadowDepth);
|
---|
| 118 | total_w += w;
|
---|
[2944] | 119 | }
|
---|
| 120 |
|
---|
[3025] | 121 | total_d /= (float)total_w;
|
---|
[2944] | 122 |
|
---|
| 123 | return total_d;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
[3025] | 126 |
|
---|
[3009] | 127 | inline float3 Interpol(float2 w, float3 bl, float3 br, float3 tl, float3 tr)
|
---|
| 128 | {
|
---|
| 129 | float3 x1 = lerp(bl, tl, w.y);
|
---|
| 130 | float3 x2 = lerp(br, tr, w.y);
|
---|
[3095] | 131 | float3 v = lerp(x1, x2, w.x);
|
---|
[2944] | 132 |
|
---|
[3009] | 133 | return v;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 |
|
---|
[2892] | 137 | pixel main_shadow(fragment IN,
|
---|
| 138 | uniform sampler2D colors,
|
---|
| 139 | uniform sampler2D positions,
|
---|
| 140 | uniform sampler2D normals,
|
---|
| 141 | uniform sampler2D shadowMap,
|
---|
[2893] | 142 | uniform float4x4 shadowMatrix,
|
---|
[2952] | 143 | uniform float sampleWidth,
|
---|
[3092] | 144 | uniform sampler2D noiseTex,
|
---|
[2966] | 145 | uniform float2 samples[NUM_PCF_TABS],
|
---|
[3024] | 146 | uniform float weights[NUM_PCF_TABS],
|
---|
[3009] | 147 | uniform float3 lightDir,
|
---|
| 148 | uniform float3 eyePos,
|
---|
| 149 | uniform float3 bl,
|
---|
| 150 | uniform float3 br,
|
---|
| 151 | uniform float3 tl,
|
---|
| 152 | uniform float3 tr
|
---|
[2944] | 153 | )
|
---|
[2928] | 154 | {
|
---|
| 155 | pixel OUT;
|
---|
[2944] | 156 |
|
---|
| 157 | float4 norm = tex2D(normals, IN.texCoord.xy);
|
---|
[2945] | 158 | const float3 normal = normalize(norm.xyz);
|
---|
[2944] | 159 |
|
---|
[3009] | 160 | float4 color = tex2Dlod(colors, float4(IN.texCoord, 0, 0));
|
---|
| 161 |
|
---|
| 162 | /// reconstruct position from the eye space depth
|
---|
| 163 | float3 viewDir = IN.view;
|
---|
[3018] | 164 | const float lenView = length(viewDir);
|
---|
| 165 | viewDir /= lenView;
|
---|
| 166 |
|
---|
[3009] | 167 | const float eyeDepth = tex2Dlod(colors, float4(IN.texCoord, 0, 0)).w;
|
---|
| 168 |
|
---|
[3034] | 169 | const float4 worldPos = float4(eyePos - viewDir * eyeDepth, 1);
|
---|
[3009] | 170 |
|
---|
[2945] | 171 | // diffuse intensity
|
---|
| 172 | const float angle = saturate(dot(normal, lightDir));
|
---|
[2959] | 173 | const float4 lightDiffuse = glstate.light[0].diffuse;
|
---|
[3017] | 174 |
|
---|
[2954] | 175 | float4 diffuse = lightDiffuse * angle;
|
---|
| 176 |
|
---|
[3009] | 177 | // hack: prevent shadowing the sky
|
---|
| 178 | const bool useShading = (color.w < 1e19f);
|
---|
| 179 |
|
---|
[2945] | 180 | // calc diffuse illumination + shadow term
|
---|
[3009] | 181 | if (useShading &&
|
---|
| 182 | (angle > 1e-3f) // shadow only if diffuse color has some minimum intensity
|
---|
[2945] | 183 | )
|
---|
| 184 | {
|
---|
[3034] | 185 | float4 lightSpacePos = mul(shadowMatrix, worldPos);
|
---|
[2945] | 186 | lightSpacePos /= lightSpacePos.w;
|
---|
[2944] | 187 |
|
---|
[3092] | 188 | float shadowTerm = CalcShadowTerm(IN, shadowMap, sampleWidth, lightSpacePos.xy, lightSpacePos.z, samples, weights, noiseTex);
|
---|
| 189 | //float shadowTerm = CalcShadowTerm(IN, shadowMap, sampleWidth, lightSpacePos.xy, lightSpacePos.z, samples, noiseTex);
|
---|
[2944] | 190 |
|
---|
[2945] | 191 | diffuse *= shadowTerm;
|
---|
[2944] | 192 | }
|
---|
[2945] | 193 |
|
---|
[2974] | 194 | // light ambient term
|
---|
[2954] | 195 | const float4 ambient = glstate.light[0].ambient;
|
---|
[3009] | 196 | // compute shading
|
---|
| 197 | OUT.color = useShading ? (ambient + diffuse) * color : color;
|
---|
[3087] | 198 | // store scaled view vector from now on so wie don't have to normalize later (e.g., for ssao)
|
---|
[3018] | 199 | OUT.color.w = color.w / lenView;
|
---|
[2991] | 200 |
|
---|
[2928] | 201 | return OUT;
|
---|
| 202 | }
|
---|
[2965] | 203 |
|
---|
[3081] | 204 | #if 0
|
---|
| 205 | /** This shader computes the reprojection and stores reprojected color / depth values
|
---|
| 206 | as well as a boolean that
|
---|
| 207 | */
|
---|
[3087] | 208 | pixel Reproject(fragment IN,
|
---|
| 209 | uniform sampler2D colors,
|
---|
| 210 | uniform sampler2D normals)
|
---|
[3081] | 211 | {
|
---|
| 212 | float4 norm = tex2Dlod(normals, float4(IN.texCoord, 0 ,0));
|
---|
| 213 | const float3 normal = normalize(norm.xyz);
|
---|
| 214 |
|
---|
| 215 | /// reconstruct position from the eye space depth
|
---|
| 216 | float3 viewDir = IN.view;
|
---|
| 217 | const float eyeDepth = tex2Dlod(colors, float4(IN.texCoord, 0, 0)).w;
|
---|
| 218 | const float3 eyeSpacePos = -viewDir * eyeDepth;
|
---|
| 219 | const float4 worldPos = float4(eyePos + eyeSpacePos, 1.0f);
|
---|
| 220 |
|
---|
| 221 |
|
---|
| 222 | ////////////////
|
---|
| 223 | //-- calculcate the current projected posiion (also used for next frame)
|
---|
| 224 |
|
---|
| 225 | float4 currentPos = mul(modelViewProj, worldPos);
|
---|
| 226 |
|
---|
| 227 | const float w = SAMPLE_RADIUS / currentPos.w;
|
---|
| 228 | currentPos /= currentPos.w;
|
---|
| 229 |
|
---|
| 230 | const float precisionScale = 1e-3f;
|
---|
| 231 | const float currentDepth = currentPos.z * precisionScale;
|
---|
| 232 |
|
---|
[3104] | 233 | const float2 ao = ssao(IN, colors, noiseTex, samples, normal,
|
---|
[3092] | 234 | eyeSpacePos, w, bl, br, tl, tr, normalize(viewDir));
|
---|
[3081] | 235 |
|
---|
| 236 |
|
---|
| 237 | /////////////////
|
---|
| 238 | //-- compute temporally smoothing
|
---|
| 239 |
|
---|
| 240 |
|
---|
| 241 | // reproject new frame into old one
|
---|
| 242 |
|
---|
| 243 | // calculate projected depth
|
---|
| 244 | float4 projPos = mul(oldModelViewProj, worldPos);
|
---|
| 245 | projPos /= projPos.w;
|
---|
| 246 |
|
---|
| 247 | // the current depth projected into the old frame
|
---|
| 248 | const float projDepth = projPos.z * precisionScale;
|
---|
| 249 | // fit from unit cube into 0 .. 1
|
---|
| 250 | const float2 tex = projPos.xy * 0.5f + 0.5f;
|
---|
| 251 | // retrieve the sample from the last frame
|
---|
| 252 | float4 oldCol = tex2D(oldTex, tex);
|
---|
| 253 |
|
---|
| 254 | const float oldDepth = oldCol.z;
|
---|
| 255 | //const float depthDif = 1.0f - projDepth / oldDepth;
|
---|
| 256 | const float depthDif = projDepth - oldDepth;
|
---|
| 257 |
|
---|
| 258 | //const float oldNumSamples = oldCol.y;
|
---|
| 259 | const float oldWeight = clamp(oldCol.y, 0, temporalCoherence);
|
---|
| 260 |
|
---|
| 261 | float newWeight;
|
---|
| 262 |
|
---|
| 263 | // the number of valid samples in this frame
|
---|
| 264 | //const float newNumSamples = ao.y;
|
---|
| 265 |
|
---|
| 266 | if ((temporalCoherence > 0)
|
---|
| 267 | && (tex.x >= 0.0f) && (tex.x < 1.0f)
|
---|
| 268 | && (tex.y >= 0.0f) && (tex.y < 1.0f)
|
---|
| 269 | && (abs(depthDif) < MIN_DEPTH_DIFF)
|
---|
| 270 | && (abs(oldCol.x - ao.x) < 0.1f)
|
---|
| 271 | // if visibility changed in the surrounding area we have to recompute
|
---|
| 272 | //&& (oldNumSamples > 0.8f * newNumSamples)
|
---|
| 273 | )
|
---|
| 274 | {
|
---|
| 275 | // increase the weight for convergence
|
---|
| 276 | newWeight = oldWeight + 1.0f;
|
---|
| 277 | OUT.illum_col.x = (ao.x + oldCol.x * oldWeight) / newWeight;
|
---|
| 278 | //if (!(oldNumSamples > ao.y - 1.5f)) newWeight = 0;
|
---|
| 279 | }
|
---|
| 280 | else
|
---|
| 281 | {
|
---|
| 282 | OUT.illum_col.x = ao.x;
|
---|
| 283 | newWeight = .0f;
|
---|
| 284 | }
|
---|
| 285 |
|
---|
| 286 | OUT.illum_col.y = newWeight;
|
---|
| 287 | OUT.illum_col.z = currentDepth;
|
---|
| 288 |
|
---|
| 289 | return OUT;
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 292 | #endif |
---|