[3109] | 1 | #include "../shaderenv.h"
|
---|
| 2 |
|
---|
| 3 |
|
---|
[3148] | 4 | /******************************************/
|
---|
| 5 | /* Filter for combining ssao with image */
|
---|
| 6 | /******************************************/
|
---|
| 7 |
|
---|
| 8 |
|
---|
[3109] | 9 | struct fragment
|
---|
| 10 | {
|
---|
| 11 | float2 texCoord: TEXCOORD0;
|
---|
| 12 | float3 view: TEXCOORD1;
|
---|
| 13 | };
|
---|
| 14 |
|
---|
| 15 |
|
---|
| 16 | struct pixel
|
---|
| 17 | {
|
---|
| 18 | float4 illum_col: COLOR0;
|
---|
| 19 | };
|
---|
| 20 |
|
---|
| 21 |
|
---|
[3148] | 22 | /** Filter taking into account depth and normal differences,
|
---|
| 23 | and convergence of a sample
|
---|
| 24 | */
|
---|
[3123] | 25 | float DiscontinuityFilter(float2 texCoord,
|
---|
| 26 | float4 ao,
|
---|
[3137] | 27 | float4 color,
|
---|
[3123] | 28 | uniform sampler2D ssaoTex,
|
---|
| 29 | uniform sampler2D normalsTex,
|
---|
[3137] | 30 | uniform sampler2D colorsTex,
|
---|
[3123] | 31 | uniform float2 filterOffs[NUM_SSAO_FILTERSAMPLES],
|
---|
| 32 | uniform float filterWeights[NUM_SSAO_FILTERSAMPLES],
|
---|
[3148] | 33 | float scale)
|
---|
[3109] | 34 | {
|
---|
| 35 | float average = .0f;
|
---|
| 36 | float total_w = .0f;
|
---|
| 37 |
|
---|
[3142] | 38 | const float eyeSpaceDepth = color.w;
|
---|
[3134] | 39 |
|
---|
[3137] | 40 | const float3 centerNormal = normalize(tex2Dlod(normalsTex, float4(texCoord, 0, 0)).xyz);
|
---|
| 41 | //const float3 centerNormal = tex2Dlod(normalsTex, float4(texCoord, 0, 0)).xyz;
|
---|
[3109] | 42 |
|
---|
| 43 | float4 aoSample;
|
---|
| 44 | float3 sampleNorm;
|
---|
[3134] | 45 | float3 samplePos;
|
---|
[3109] | 46 | float w;
|
---|
[3134] | 47 | float4 sampleTexCoord;
|
---|
[3120] | 48 | float depthFactor;
|
---|
[3123] | 49 | float normalFactor;
|
---|
[3134] | 50 | float convergenceFactor;
|
---|
[3137] | 51 | float sampleDepth;
|
---|
[3109] | 52 |
|
---|
| 53 | for (int i = 0; i < NUM_SSAO_FILTERSAMPLES; ++ i)
|
---|
| 54 | {
|
---|
[3134] | 55 | sampleTexCoord = float4(texCoord + filterOffs[i] * scale, 0, 0);
|
---|
[3137] | 56 |
|
---|
[3134] | 57 | aoSample = tex2Dlod(ssaoTex, sampleTexCoord);
|
---|
| 58 | sampleNorm = normalize(tex2Dlod(normalsTex, sampleTexCoord).xyz);
|
---|
[3137] | 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 |
|
---|
[3148] | 86 | /** Function combining image and indirect illumination buffer using a
|
---|
| 87 | depth and normal aware discontinuity filter.
|
---|
| 88 | */
|
---|
[3109] | 89 | pixel combine(fragment IN,
|
---|
| 90 | uniform sampler2D colorsTex,
|
---|
| 91 | uniform sampler2D ssaoTex,
|
---|
| 92 | uniform sampler2D normalsTex,
|
---|
| 93 | uniform float2 filterOffs[NUM_SSAO_FILTERSAMPLES],
|
---|
[3160] | 94 | uniform float filterWeights[NUM_SSAO_FILTERSAMPLES],
|
---|
| 95 | uniform float4x4 modelViewProj
|
---|
[3148] | 96 | )
|
---|
[3109] | 97 | {
|
---|
| 98 | pixel OUT;
|
---|
| 99 |
|
---|
| 100 | float4 col = tex2Dlod(colorsTex, float4(IN.texCoord, 0, 0));
|
---|
| 101 | float4 ao = tex2Dlod(ssaoTex, float4(IN.texCoord, 0, 0));
|
---|
| 102 |
|
---|
[3160] | 103 | // reconstruct position from the eye space depth
|
---|
[3161] | 104 | /*const float4 worldPos = float4(-IN.view * eyeSpaceDepth, 1.0f);
|
---|
[3160] | 105 | // compute w factor from projection in order to control filter size
|
---|
| 106 | const float4 projPos = mul(modelViewProj, worldPos);
|
---|
| 107 | const float distanceScale = 1.0f / projPos.w;
|
---|
[3161] | 108 | */
|
---|
[3160] | 109 |
|
---|
[3161] | 110 | const float eyeSpaceDepth = col.w;
|
---|
| 111 |
|
---|
| 112 | const float scaleFactor = 2.0f;
|
---|
| 113 | const float distanceScale = scaleFactor / (eyeSpaceDepth + scaleFactor);
|
---|
| 114 |
|
---|
[3160] | 115 | const float convergence = ao.y;
|
---|
| 116 |
|
---|
[3161] | 117 | const float adaptFactor = 5.0f;
|
---|
| 118 | const float convergenceScale = adaptFactor / (convergence + adaptFactor);
|
---|
| 119 |
|
---|
| 120 |
|
---|
[3160] | 121 | if ((ao.y < 100.0f) && (col.w < 1e10f))
|
---|
[3141] | 122 | //if (col.w < 1e10f)
|
---|
[3120] | 123 | {
|
---|
[3161] | 124 | ao.x = DiscontinuityFilter(IN.texCoord, ao, col, ssaoTex, normalsTex, colorsTex, filterOffs, filterWeights, convergenceScale * distanceScale);
|
---|
[3120] | 125 | }
|
---|
| 126 |
|
---|
[3162] | 127 | //OUT.illum_col.xyz = col.xyz * ao.x;
|
---|
| 128 | OUT.illum_col.xyz = float3(ao.x, ao.x, ao.x);
|
---|
[3133] | 129 | //OUT.illum_col.xyz = float3(0, clamp(1.0f - ao.y * 1e-2f, 0, 1), 1);
|
---|
[3109] | 130 | OUT.illum_col.w = col.w;
|
---|
| 131 |
|
---|
| 132 | return OUT;
|
---|
| 133 | }
|
---|