[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 |
|
---|
[3216] | 23 | /** Filter taking into account depth, normal discontinuities
|
---|
| 24 | and ssao convergence of a sample (the higher the more reliably
|
---|
| 25 | has the sample a correct ssao value)
|
---|
[3148] | 26 | */
|
---|
[3123] | 27 | float DiscontinuityFilter(float2 texCoord,
|
---|
| 28 | float4 ao,
|
---|
[3137] | 29 | float4 color,
|
---|
[3123] | 30 | uniform sampler2D ssaoTex,
|
---|
| 31 | uniform sampler2D normalsTex,
|
---|
[3137] | 32 | uniform sampler2D colorsTex,
|
---|
[3167] | 33 | uniform float2 filterOffs[NUM_SSAO_FILTER_SAMPLES],
|
---|
[3216] | 34 | float scale,
|
---|
| 35 | float3 bl,
|
---|
| 36 | float3 br,
|
---|
| 37 | float3 tl,
|
---|
| 38 | float3 tr)
|
---|
[3109] | 39 | {
|
---|
| 40 | float average = .0f;
|
---|
| 41 | float total_w = .0f;
|
---|
| 42 |
|
---|
[3193] | 43 | //const float3 centerPos = ReconstructSamplePos(ssaoTex, texCoord, bl, br, tl, tr);
|
---|
| 44 | const float3 centerPos = ReconstructSamplePos(colorsTex, texCoord, bl, br, tl, tr);
|
---|
[3167] | 45 | const float3 centerNormal = tex2Dlod(normalsTex, float4(texCoord, 0, 0)).xyz;
|
---|
[3163] | 46 |
|
---|
| 47 | float4 aoSample;
|
---|
| 48 | float3 sampleNorm;
|
---|
| 49 | float3 samplePos;
|
---|
| 50 | float w;
|
---|
| 51 | float4 sampleTexCoord;
|
---|
| 52 | float spatialFactor;
|
---|
| 53 | float normalFactor;
|
---|
| 54 | float convergenceFactor;
|
---|
[3167] | 55 | float len;
|
---|
[3163] | 56 |
|
---|
[3192] | 57 | const float convergenceThresh = 200.0f;
|
---|
| 58 |
|
---|
[3167] | 59 | for (int i = 0; i < NUM_SSAO_FILTER_SAMPLES; ++ i)
|
---|
[3163] | 60 | {
|
---|
[3198] | 61 | sampleTexCoord = float4(texCoord + filterOffs[i] * scale, .0f, .0f);
|
---|
[3163] | 62 |
|
---|
| 63 | aoSample = tex2Dlod(ssaoTex, sampleTexCoord);
|
---|
[3212] | 64 |
|
---|
[3163] | 65 | // check spatial discontinuity
|
---|
[3198] | 66 | // note: using the depth from the color texture is not 100% correct as depth was
|
---|
[3193] | 67 | // not scaled with the interpolated view vector depth yet ...
|
---|
| 68 | samplePos = ReconstructSamplePos(colorsTex, sampleTexCoord.xy, bl, br, tl, tr);
|
---|
| 69 | //samplePos = ReconstructSamplePos(ssaoTex, sampleTexCoord.xy, bl, br, tl, tr);
|
---|
| 70 |
|
---|
[3297] | 71 | len = SqrLen(centerPos - samplePos);
|
---|
| 72 | //len = length(centerPos - samplePos);
|
---|
[3163] | 73 | spatialFactor = 1.0f / max(len, 1e-3f);
|
---|
| 74 |
|
---|
[3195] | 75 | convergenceFactor = aoSample.y + 1.0f;
|
---|
[3212] | 76 |
|
---|
| 77 | //sampleNorm = tex2Dlod(normalsTex, sampleTexCoord).xyz;
|
---|
| 78 | //normalFactor = max(step(.2f, dot(sampleNorm, centerNormal)), 1e-2f);
|
---|
| 79 |
|
---|
[3163] | 80 | // combine the weights
|
---|
[3195] | 81 | w = convergenceFactor * convergenceFactor * spatialFactor;// * normalFactor;
|
---|
[3198] | 82 |
|
---|
[3163] | 83 | average += aoSample.x * w;
|
---|
| 84 | total_w += w;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | average /= max(total_w, 1e-6f);
|
---|
| 88 |
|
---|
| 89 | return saturate(average);
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 |
|
---|
[3148] | 93 | /** Function combining image and indirect illumination buffer using a
|
---|
[3216] | 94 | depth and normal aware discontinuity filter. We assume that
|
---|
| 95 | we are using half resolution ssao for this version of the combineSsao
|
---|
[3148] | 96 | */
|
---|
[3216] | 97 | pixel CombineSsaoHalfRes(fragment IN,
|
---|
| 98 | uniform sampler2D colorsTex,
|
---|
| 99 | uniform sampler2D ssaoTex,
|
---|
| 100 | uniform sampler2D normalsTex,
|
---|
| 101 | uniform float2 filterOffs[NUM_SSAO_FILTER_SAMPLES],
|
---|
| 102 | uniform float filterWeights[NUM_SSAO_FILTER_SAMPLES],
|
---|
| 103 | uniform float ssaoFilterRadius,
|
---|
| 104 | uniform float4x4 modelViewProj,
|
---|
| 105 | uniform float3 bl,
|
---|
| 106 | uniform float3 br,
|
---|
| 107 | uniform float3 tl,
|
---|
| 108 | uniform float3 tr,
|
---|
[3299] | 109 | uniform float2 resolution
|
---|
[3216] | 110 | )
|
---|
[3109] | 111 | {
|
---|
| 112 | pixel OUT;
|
---|
| 113 |
|
---|
| 114 | float4 col = tex2Dlod(colorsTex, float4(IN.texCoord, 0, 0));
|
---|
[3204] | 115 | float4 ao = tex2Dlod(ssaoTex, float4(IN.texCoord, 0, 0));
|
---|
[3109] | 116 |
|
---|
[3216] | 117 | // the following has to be done for half resolution ssao:
|
---|
[3198] | 118 | // get the minimum convergence by exactly sampling the 4 surrounding
|
---|
| 119 | // texels in the old texture, otherwise flickering because convergence
|
---|
| 120 | // will be interpolated when upsampling and filter size does not match!
|
---|
[3216] | 121 |
|
---|
[3198] | 122 | float4 texelCenterConv;
|
---|
| 123 | const float xoffs = .5f / w; const float yoffs = .5f / h;
|
---|
[3195] | 124 |
|
---|
[3198] | 125 | // get position exactly between old texel centers
|
---|
[3232] | 126 | float2 center;
|
---|
[3204] | 127 | center.x = (floor(IN.texCoord.x * w - .5f) + 1.0f) / w;
|
---|
| 128 | center.y = (floor(IN.texCoord.y * h - .5f) + 1.0f) / h;
|
---|
[3205] | 129 |
|
---|
[3204] | 130 | texelCenterConv.x = tex2Dlod(ssaoTex, float4(center + float2( xoffs, yoffs), 0, 0)).y;
|
---|
| 131 | texelCenterConv.y = tex2Dlod(ssaoTex, float4(center + float2( xoffs, -yoffs), 0, 0)).y;
|
---|
| 132 | texelCenterConv.z = tex2Dlod(ssaoTex, float4(center + float2(-xoffs, -yoffs), 0, 0)).y;
|
---|
| 133 | texelCenterConv.w = tex2Dlod(ssaoTex, float4(center + float2(-xoffs, yoffs), 0, 0)).y;
|
---|
[3196] | 134 |
|
---|
[3198] | 135 | const float m1 = min(texelCenterConv.x, texelCenterConv.y);
|
---|
| 136 | const float m2 = min(texelCenterConv.z, texelCenterConv.w);
|
---|
| 137 |
|
---|
[3203] | 138 | const float minConvergence = min(m1, m2);
|
---|
[3232] | 139 | const float convergence = minConvergence;
|
---|
| 140 |
|
---|
| 141 |
|
---|
[3206] | 142 | // filter reaches size 1 pixel when sample size reaches threshold
|
---|
[3204] | 143 | // afterwards we do not use the filter anymore
|
---|
[3195] | 144 |
|
---|
[3167] | 145 | // filter up to a certain convergance value and leave out background (sky) by checking depth
|
---|
[3297] | 146 | if (//(convergence < SSAO_CONVERGENCE_THRESHOLD) &&
|
---|
[3304] | 147 | (col.w < DEPTH_THRESHOLD))
|
---|
[3164] | 148 | {
|
---|
| 149 | const float distanceScale = 1.0f;
|
---|
[3161] | 150 |
|
---|
[3203] | 151 | // descend to zero filter size after reaching thres pixels
|
---|
[3216] | 152 | const float convergenceWeight = SSAO_CONVERGENCE_THRESHOLD / (ssaoFilterRadius - 1.0f);
|
---|
[3195] | 153 | const float convergenceScale = convergenceWeight / (convergence + convergenceWeight);
|
---|
[3297] | 154 | const float scale = 1;//ssaoFilterRadius * convergenceScale * distanceScale;
|
---|
[3160] | 155 |
|
---|
[3167] | 156 | // the filtered ssao value
|
---|
[3271] | 157 | ao.x = DiscontinuityFilter(IN.texCoord, ao, col, ssaoTex, normalsTex, colorsTex, filterOffs, scale, bl, br, tl, tr);
|
---|
[3120] | 158 | }
|
---|
| 159 |
|
---|
[3225] | 160 | // just apply ssao if we are not in the sky
|
---|
[3304] | 161 | if (col.w < DEPTH_THRESHOLD)
|
---|
| 162 | {
|
---|
[3226] | 163 | OUT.illum_col.xyz = col.xyz * max(2e-2f, 1.0f - ao.x);
|
---|
| 164 | //OUT.illum_col.xyz = col.xyz * ao.x;
|
---|
[3304] | 165 | }
|
---|
[3192] | 166 | else
|
---|
[3304] | 167 | {
|
---|
[3192] | 168 | OUT.illum_col.xyz = col.xyz;
|
---|
[3304] | 169 | }
|
---|
[3192] | 170 |
|
---|
[3225] | 171 | //OUT.illum_col.xyz = float3(abs(ao.y * 1e2f), abs(ao.z * 1e2f), abs(ao.w * 1e2f));
|
---|
| 172 |
|
---|
| 173 | //if (convergence < (1.0f + NUM_SAMPLES * 10))
|
---|
| 174 | // OUT.illum_col.xyz = float3(1 - convergence / (NUM_SAMPLES * 10), convergence / (NUM_SAMPLES * 10), 0);
|
---|
| 175 |
|
---|
| 176 | //OUT.illum_col.xyz = float3(ao.z * 1e4f, ao.z * 1e4f, ao.z * 1e4f);
|
---|
| 177 |
|
---|
[3198] | 178 | //OUT.illum_col.xyz = float3(ao.x, ao.x, step(thres, convergence));
|
---|
[3204] | 179 | //OUT.illum_col.xyz = float3(abs(center.x - IN.texCoord.x) * 16.0f, abs(center.y - IN.texCoord.y) * 12.0f, 0);
|
---|
[3195] | 180 | //OUT.illum_col.xyz = float3(0, 1.0f - step(0.5f + NUM_SAMPLES, convergence), 1);
|
---|
[3225] | 181 |
|
---|
[3109] | 182 | OUT.illum_col.w = col.w;
|
---|
| 183 |
|
---|
| 184 | return OUT;
|
---|
[3296] | 185 | }
|
---|
| 186 |
|
---|
| 187 |
|
---|
| 188 | /** Function combining image and indirect illumination buffer using a
|
---|
[3301] | 189 | depth and normal aware discontinuity filter.
|
---|
[3296] | 190 | */
|
---|
| 191 | pixel CombineSsaoFullRes(fragment IN,
|
---|
| 192 | uniform sampler2D colorsTex,
|
---|
| 193 | uniform sampler2D ssaoTex,
|
---|
| 194 | uniform sampler2D normalsTex,
|
---|
| 195 | uniform float2 filterOffs[NUM_SSAO_FILTER_SAMPLES],
|
---|
| 196 | uniform float filterWeights[NUM_SSAO_FILTER_SAMPLES],
|
---|
| 197 | uniform float ssaoFilterRadius,
|
---|
| 198 | uniform float4x4 modelViewProj,
|
---|
| 199 | uniform float3 bl,
|
---|
| 200 | uniform float3 br,
|
---|
| 201 | uniform float3 tl,
|
---|
| 202 | uniform float3 tr,
|
---|
[3299] | 203 | uniform float2 resolution
|
---|
[3296] | 204 | )
|
---|
| 205 | {
|
---|
| 206 | pixel OUT;
|
---|
| 207 |
|
---|
| 208 | float4 col = tex2Dlod(colorsTex, float4(IN.texCoord, 0, 0));
|
---|
| 209 | float4 ao = tex2Dlod(ssaoTex, float4(IN.texCoord, 0, 0));
|
---|
| 210 |
|
---|
| 211 | // just take unfiltered convergence in current pixel
|
---|
| 212 | const float convergence = ao.y;
|
---|
| 213 |
|
---|
| 214 | // filter reaches size 1 pixel when sample size reaches threshold
|
---|
| 215 | // afterwards we do not use the filter anymore
|
---|
| 216 |
|
---|
| 217 | // filter up to a certain convergance value and leave out background (sky) by checking depth
|
---|
[3298] | 218 | if ((convergence < SSAO_CONVERGENCE_THRESHOLD) &&
|
---|
[3304] | 219 | (col.w < DEPTH_THRESHOLD))
|
---|
[3296] | 220 | {
|
---|
| 221 | const float distanceScale = 1.0f;
|
---|
| 222 |
|
---|
| 223 | // descend to zero filter size after reaching thres pixels
|
---|
| 224 | const float convergenceWeight = SSAO_CONVERGENCE_THRESHOLD / (ssaoFilterRadius - 1.0f);
|
---|
| 225 | const float convergenceScale = convergenceWeight / (convergence + convergenceWeight);
|
---|
[3298] | 226 | //const float scale = 1;//ssaoFilterRadius * convergenceScale * distanceScale;
|
---|
| 227 | const float scale = convergenceScale;
|
---|
[3296] | 228 |
|
---|
| 229 | // the filtered ssao value
|
---|
| 230 | ao.x = DiscontinuityFilter(IN.texCoord, ao, col, ssaoTex, normalsTex, colorsTex, filterOffs, scale, bl, br, tl, tr);
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | // just apply ssao if we are not in the sky
|
---|
[3304] | 234 | if (col.w < DEPTH_THRESHOLD)
|
---|
[3299] | 235 | {
|
---|
[3296] | 236 | OUT.illum_col.xyz = col.xyz * max(2e-2f, 1.0f - ao.x);
|
---|
| 237 | //OUT.illum_col.xyz = col.xyz * ao.x;
|
---|
[3299] | 238 | }
|
---|
[3296] | 239 | else
|
---|
[3299] | 240 | {
|
---|
[3296] | 241 | OUT.illum_col.xyz = col.xyz;
|
---|
[3299] | 242 | }
|
---|
[3296] | 243 |
|
---|
| 244 | OUT.illum_col.w = col.w;
|
---|
| 245 |
|
---|
| 246 | return OUT;
|
---|
[3198] | 247 | } |
---|