[3109] | 1 | #include "../shaderenv.h"
|
---|
[3163] | 2 | #include "common.h"
|
---|
[3109] | 3 |
|
---|
| 4 |
|
---|
[3163] | 5 | /*************************************************/
|
---|
| 6 | /* Filter for combining ssao with image */
|
---|
| 7 | /*************************************************/
|
---|
[3148] | 8 |
|
---|
| 9 |
|
---|
[3109] | 10 | struct fragment
|
---|
| 11 | {
|
---|
| 12 | float2 texCoord: TEXCOORD0;
|
---|
| 13 | float3 view: TEXCOORD1;
|
---|
| 14 | };
|
---|
| 15 |
|
---|
| 16 |
|
---|
| 17 | struct pixel
|
---|
| 18 | {
|
---|
| 19 | float4 illum_col: COLOR0;
|
---|
| 20 | };
|
---|
| 21 |
|
---|
| 22 |
|
---|
[3148] | 23 | /** Filter taking into account depth and normal differences,
|
---|
[3163] | 24 | and convergence of a sample
|
---|
[3148] | 25 | */
|
---|
[3123] | 26 | float DiscontinuityFilter(float2 texCoord,
|
---|
| 27 | float4 ao,
|
---|
[3137] | 28 | float4 color,
|
---|
[3123] | 29 | uniform sampler2D ssaoTex,
|
---|
| 30 | uniform sampler2D normalsTex,
|
---|
[3137] | 31 | uniform sampler2D colorsTex,
|
---|
[3167] | 32 | uniform float2 filterOffs[NUM_SSAO_FILTER_SAMPLES],
|
---|
| 33 | uniform float filterWeights[NUM_SSAO_FILTER_SAMPLES],
|
---|
[3163] | 34 | float scale
|
---|
| 35 | )
|
---|
[3109] | 36 | {
|
---|
| 37 | float average = .0f;
|
---|
| 38 | float total_w = .0f;
|
---|
| 39 |
|
---|
[3142] | 40 | const float eyeSpaceDepth = color.w;
|
---|
[3134] | 41 |
|
---|
[3167] | 42 | const float3 centerNormal = tex2Dlod(normalsTex, float4(texCoord, 0, 0)).xyz;
|
---|
[3109] | 43 |
|
---|
| 44 | float4 aoSample;
|
---|
| 45 | float3 sampleNorm;
|
---|
[3134] | 46 | float3 samplePos;
|
---|
[3109] | 47 | float w;
|
---|
[3134] | 48 | float4 sampleTexCoord;
|
---|
[3120] | 49 | float depthFactor;
|
---|
[3123] | 50 | float normalFactor;
|
---|
[3134] | 51 | float convergenceFactor;
|
---|
[3137] | 52 | float sampleDepth;
|
---|
[3109] | 53 |
|
---|
[3167] | 54 | for (int i = 0; i < NUM_SSAO_FILTER_SAMPLES; ++ i)
|
---|
[3109] | 55 | {
|
---|
[3134] | 56 | sampleTexCoord = float4(texCoord + filterOffs[i] * scale, 0, 0);
|
---|
[3137] | 57 |
|
---|
[3134] | 58 | aoSample = tex2Dlod(ssaoTex, sampleTexCoord);
|
---|
[3167] | 59 | sampleNorm = tex2Dlod(normalsTex, sampleTexCoord).xyz;
|
---|
[3109] | 60 |
|
---|
[3148] | 61 | // check depth discontinuity
|
---|
[3142] | 62 | sampleDepth = tex2Dlod(colorsTex, sampleTexCoord).w;
|
---|
| 63 | //sampleDepth = aoSample.w;
|
---|
[3148] | 64 |
|
---|
[3162] | 65 | //depthFactor = 1.0f / max(abs(eyeSpaceDepth - sampleDepth), 1e-2f);
|
---|
| 66 | depthFactor = 1.0f - step(1e-2f, abs(1.0f - eyeSpaceDepth / sampleDepth));
|
---|
| 67 | //normalFactor = max(step(0.6f, dot(sampleNorm, centerNormal)), 1e-3f);
|
---|
| 68 | normalFactor = max(dot(sampleNorm, centerNormal), 1e-3f);
|
---|
| 69 | //convergenceFactor = min(100.0f, aoSample.y);
|
---|
| 70 | convergenceFactor = aoSample.y;
|
---|
[3109] | 71 |
|
---|
[3148] | 72 | // combine the weights
|
---|
[3162] | 73 | w = filterWeights[i] * convergenceFactor * depthFactor * normalFactor;
|
---|
| 74 | //w = normalFactor * convergenceFactor;
|
---|
[3123] | 75 |
|
---|
[3134] | 76 | average += aoSample.x * w;
|
---|
[3109] | 77 | total_w += w;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[3162] | 80 | average /= max(total_w, 1e-6f);
|
---|
[3109] | 81 |
|
---|
[3155] | 82 | return saturate(average);
|
---|
[3109] | 83 | }
|
---|
| 84 |
|
---|
[3155] | 85 |
|
---|
[3163] | 86 |
|
---|
| 87 | /** Filter taking into account depth and normal differences,
|
---|
| 88 | and convergence of a sample
|
---|
| 89 | */
|
---|
| 90 | float DiscontinuityFilter2(float2 texCoord,
|
---|
| 91 | float4 ao,
|
---|
| 92 | float4 color,
|
---|
| 93 | uniform sampler2D ssaoTex,
|
---|
| 94 | uniform sampler2D normalsTex,
|
---|
| 95 | uniform sampler2D colorsTex,
|
---|
[3167] | 96 | uniform float2 filterOffs[NUM_SSAO_FILTER_SAMPLES],
|
---|
[3163] | 97 | float scale,
|
---|
| 98 | float3 bl,
|
---|
| 99 | float3 br,
|
---|
| 100 | float3 tl,
|
---|
| 101 | float3 tr)
|
---|
| 102 | {
|
---|
| 103 | float average = .0f;
|
---|
| 104 | float total_w = .0f;
|
---|
| 105 |
|
---|
| 106 | const float3 centerPos = ReconstructSamplePos(ssaoTex, texCoord, bl, br, tl, tr);
|
---|
[3167] | 107 | const float3 centerNormal = tex2Dlod(normalsTex, float4(texCoord, 0, 0)).xyz;
|
---|
[3163] | 108 |
|
---|
| 109 | float4 aoSample;
|
---|
| 110 | float3 sampleNorm;
|
---|
| 111 | float3 samplePos;
|
---|
| 112 | float w;
|
---|
| 113 | float4 sampleTexCoord;
|
---|
| 114 | float spatialFactor;
|
---|
| 115 | float normalFactor;
|
---|
| 116 | float convergenceFactor;
|
---|
[3167] | 117 | float len;
|
---|
[3163] | 118 |
|
---|
[3167] | 119 | for (int i = 0; i < NUM_SSAO_FILTER_SAMPLES; ++ i)
|
---|
[3163] | 120 | {
|
---|
| 121 | sampleTexCoord = float4(texCoord + filterOffs[i] * scale, 0, 0);
|
---|
| 122 |
|
---|
| 123 | aoSample = tex2Dlod(ssaoTex, sampleTexCoord);
|
---|
[3167] | 124 | sampleNorm = tex2Dlod(normalsTex, sampleTexCoord).xyz;
|
---|
[3163] | 125 |
|
---|
| 126 | // check spatial discontinuity
|
---|
| 127 | samplePos = ReconstructSamplePos(ssaoTex, sampleTexCoord.xy, bl, br, tl, tr);
|
---|
[3167] | 128 | len = min(SqrLen(centerPos - samplePos), 1e2f);
|
---|
[3163] | 129 |
|
---|
| 130 | spatialFactor = 1.0f / max(len, 1e-3f);
|
---|
| 131 |
|
---|
| 132 | //normalFactor = max(step(0.6f, dot(sampleNorm, centerNormal)), 1e-3f);
|
---|
| 133 | normalFactor = max(dot(sampleNorm, samplePos), 1e-3f);
|
---|
[3164] | 134 | //convergenceFactor = min(100.0f, aoSample.y);
|
---|
| 135 | convergenceFactor = aoSample.y;
|
---|
[3163] | 136 |
|
---|
| 137 | // combine the weights
|
---|
| 138 | w = convergenceFactor * spatialFactor * normalFactor;
|
---|
| 139 |
|
---|
| 140 | average += aoSample.x * w;
|
---|
| 141 | total_w += w;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | average /= max(total_w, 1e-6f);
|
---|
| 145 |
|
---|
| 146 | return saturate(average);
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 |
|
---|
[3148] | 150 | /** Function combining image and indirect illumination buffer using a
|
---|
| 151 | depth and normal aware discontinuity filter.
|
---|
| 152 | */
|
---|
[3109] | 153 | pixel combine(fragment IN,
|
---|
| 154 | uniform sampler2D colorsTex,
|
---|
| 155 | uniform sampler2D ssaoTex,
|
---|
| 156 | uniform sampler2D normalsTex,
|
---|
[3167] | 157 | uniform float2 filterOffs[NUM_SSAO_FILTER_SAMPLES],
|
---|
| 158 | uniform float filterWeights[NUM_SSAO_FILTER_SAMPLES],
|
---|
[3163] | 159 | uniform float4x4 modelViewProj,
|
---|
| 160 | uniform float3 bl,
|
---|
| 161 | uniform float3 br,
|
---|
| 162 | uniform float3 tl,
|
---|
| 163 | uniform float3 tr
|
---|
[3148] | 164 | )
|
---|
[3109] | 165 | {
|
---|
| 166 | pixel OUT;
|
---|
| 167 |
|
---|
| 168 | float4 col = tex2Dlod(colorsTex, float4(IN.texCoord, 0, 0));
|
---|
| 169 | float4 ao = tex2Dlod(ssaoTex, float4(IN.texCoord, 0, 0));
|
---|
| 170 |
|
---|
[3160] | 171 | // reconstruct position from the eye space depth
|
---|
[3161] | 172 | /*const float4 worldPos = float4(-IN.view * eyeSpaceDepth, 1.0f);
|
---|
[3160] | 173 | // compute w factor from projection in order to control filter size
|
---|
| 174 | const float4 projPos = mul(modelViewProj, worldPos);
|
---|
| 175 | const float distanceScale = 1.0f / projPos.w;
|
---|
[3161] | 176 | */
|
---|
[3160] | 177 |
|
---|
[3167] | 178 | // filter up to a certain convergance value and leave out background (sky) by checking depth
|
---|
[3164] | 179 | if ((ao.y < 100.0f) && (col.w < 1e10f))
|
---|
| 180 | //if (col.w < 1e10f)
|
---|
| 181 | {
|
---|
[3168] | 182 | //const float eyeSpaceDepth = col.w; const float distanceScaleWeight = 2.0f;
|
---|
[3164] | 183 | //const float distanceScale = distanceScaleWeight / (eyeSpaceDepth + distanceScaleWeight);
|
---|
| 184 | const float distanceScale = 1.0f;
|
---|
[3161] | 185 |
|
---|
[3164] | 186 | const float convergence = ao.y;
|
---|
[3167] | 187 | const float adaptWeight = 25.0f;
|
---|
| 188 | const float convergenceScale = SSAO_CONVERGENCE_WEIGHT / (convergence + SSAO_CONVERGENCE_WEIGHT);
|
---|
[3161] | 189 |
|
---|
[3167] | 190 | const float scale = NUM_SSAO_FILTER_WIDTH * convergenceScale * distanceScale;
|
---|
[3160] | 191 |
|
---|
[3167] | 192 | // the filtered ssao value
|
---|
[3164] | 193 | ao.x = DiscontinuityFilter2(IN.texCoord, ao, col, ssaoTex, normalsTex, colorsTex, filterOffs, scale, bl, br, tl, tr);
|
---|
[3120] | 194 | }
|
---|
| 195 |
|
---|
[3164] | 196 | OUT.illum_col.xyz = col.xyz * ao.x;
|
---|
| 197 | //OUT.illum_col.xyz = float3(ao.x, ao.x, ao.x);
|
---|
[3133] | 198 | //OUT.illum_col.xyz = float3(0, clamp(1.0f - ao.y * 1e-2f, 0, 1), 1);
|
---|
[3109] | 199 | OUT.illum_col.w = col.w;
|
---|
| 200 |
|
---|
| 201 | return OUT;
|
---|
| 202 | }
|
---|