1 | #include "../shaderenv.h"
|
---|
2 |
|
---|
3 |
|
---|
4 | struct fragment
|
---|
5 | {
|
---|
6 | float2 texCoord: TEXCOORD0;
|
---|
7 | float3 view: TEXCOORD1;
|
---|
8 | };
|
---|
9 |
|
---|
10 |
|
---|
11 | struct pixel
|
---|
12 | {
|
---|
13 | float4 illum_col: COLOR0;
|
---|
14 | };
|
---|
15 |
|
---|
16 |
|
---|
17 |
|
---|
18 | float Filter(float2 texCoord,
|
---|
19 | uniform sampler2D ssaoTex,
|
---|
20 | uniform float2 filterOffs[NUM_SSAO_FILTERSAMPLES],
|
---|
21 | uniform float filterWeights[NUM_SSAO_FILTERSAMPLES])
|
---|
22 | {
|
---|
23 | float average = .0f;
|
---|
24 | float w = .0f;
|
---|
25 |
|
---|
26 | for (int i = 0; i < NUM_SSAO_FILTERSAMPLES; ++ i)
|
---|
27 | {
|
---|
28 | average += filterWeights[i] * tex2Dlod(ssaoTex, float4(texCoord + filterOffs[i], 0, 0)).x;
|
---|
29 | w += filterWeights[i];
|
---|
30 | }
|
---|
31 |
|
---|
32 | average *= 1.0f / (float)w;
|
---|
33 |
|
---|
34 | return average;
|
---|
35 | }
|
---|
36 |
|
---|
37 |
|
---|
38 | float Filter(float2 texCoord,
|
---|
39 | uniform sampler2D ssaoTex,
|
---|
40 | uniform float2 filterOffs[NUM_SSAO_FILTERSAMPLES],
|
---|
41 | uniform float filterWeights[NUM_SSAO_FILTERSAMPLES],
|
---|
42 | float scale)
|
---|
43 | {
|
---|
44 | float average = .0f;
|
---|
45 | float w = .0f;
|
---|
46 |
|
---|
47 | for (int i = 0; i < NUM_SSAO_FILTERSAMPLES; ++ i)
|
---|
48 | {
|
---|
49 | average += filterWeights[i] * tex2Dlod(ssaoTex, float4(texCoord + filterOffs[i] * scale, 0, 0)).x;
|
---|
50 | w += filterWeights[i];
|
---|
51 | }
|
---|
52 |
|
---|
53 | average *= 1.0f / (float)w;
|
---|
54 |
|
---|
55 | return average;
|
---|
56 | }
|
---|
57 |
|
---|
58 |
|
---|
59 | float DiscontinuityFilter(float2 texCoord,
|
---|
60 | float4 ao,
|
---|
61 | uniform sampler2D ssaoTex,
|
---|
62 | uniform sampler2D normalsTex,
|
---|
63 | uniform float2 filterOffs[NUM_SSAO_FILTERSAMPLES],
|
---|
64 | uniform float filterWeights[NUM_SSAO_FILTERSAMPLES],
|
---|
65 | float scale)
|
---|
66 | {
|
---|
67 | float average = .0f;
|
---|
68 | float total_w = .0f;
|
---|
69 |
|
---|
70 | const float eyeSpaceDepth = ao.w;
|
---|
71 | const float3 norm = normalize(tex2Dlod(normalsTex, float4(texCoord, 0, 0)).xyz);
|
---|
72 |
|
---|
73 | float4 aoSample;
|
---|
74 | float3 sampleNorm;
|
---|
75 | float w;
|
---|
76 | float4 offs;
|
---|
77 | float depthFactor;
|
---|
78 | float normalFactor;
|
---|
79 |
|
---|
80 | for (int i = 0; i < NUM_SSAO_FILTERSAMPLES; ++ i)
|
---|
81 | {
|
---|
82 | offs = float4(texCoord + filterOffs[i] * scale, 0, 0);
|
---|
83 | aoSample = tex2Dlod(ssaoTex, offs);
|
---|
84 |
|
---|
85 | sampleNorm = normalize(tex2Dlod(normalsTex, offs).xyz);
|
---|
86 |
|
---|
87 | //depthFactor = clamp(1.0f - abs(1.0f - eyeSpaceDepth / aoSample.w), 1e-3f, 1.0f);
|
---|
88 |
|
---|
89 | depthFactor = max(step(5e-2f, 1.0f - abs(1.0f - eyeSpaceDepth / aoSample.w)), 1e-3f);
|
---|
90 | normalFactor = max(step(0.5f, dot(sampleNorm, norm)), 1e-3f);
|
---|
91 |
|
---|
92 | w = filterWeights[i] * normalFactor * depthFactor;
|
---|
93 | //w = filterWeights[i] * depthFactor;
|
---|
94 |
|
---|
95 | average += aoSample.x * w;
|
---|
96 | total_w += w;
|
---|
97 | }
|
---|
98 |
|
---|
99 | average *= 1.0f / max(total_w, 1e-6f);
|
---|
100 |
|
---|
101 | return average;
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 | pixel combine(fragment IN,
|
---|
106 | uniform sampler2D colorsTex,
|
---|
107 | uniform sampler2D ssaoTex,
|
---|
108 | uniform sampler2D normalsTex,
|
---|
109 | uniform float2 filterOffs[NUM_SSAO_FILTERSAMPLES],
|
---|
110 | uniform float filterWeights[NUM_SSAO_FILTERSAMPLES]
|
---|
111 | )
|
---|
112 | {
|
---|
113 | pixel OUT;
|
---|
114 |
|
---|
115 | float4 col = tex2Dlod(colorsTex, float4(IN.texCoord, 0, 0));
|
---|
116 | float4 ao = tex2Dlod(ssaoTex, float4(IN.texCoord, 0, 0));
|
---|
117 |
|
---|
118 | //if ((ao.y < 60.0f) && (col.w < 1e10f))
|
---|
119 | if (col.w < 1e10f)
|
---|
120 | {
|
---|
121 | //const static float scaleFactor = 10.0f;
|
---|
122 | const static float scaleFactor = 50.0f;
|
---|
123 |
|
---|
124 | //ao.x = Filter(IN.texCoord, ssaoTex, filterOffs, filterWeights);
|
---|
125 | //ao.x = Filter(IN.texCoord, ssaoTex, filterOffs, filterWeights, 1.0f / (1.0f + ao.y));
|
---|
126 | ao.x = DiscontinuityFilter(IN.texCoord, ao, ssaoTex, normalsTex, filterOffs, filterWeights, scaleFactor / (scaleFactor + ao.y));
|
---|
127 | //ao.x = BilateralFilter(IN.texCoord, ao, ssaoTex, normalsTex, filterOffs, filterWeights, 1.0f);
|
---|
128 | }
|
---|
129 |
|
---|
130 | OUT.illum_col = col * ao.x;
|
---|
131 | //OUT.illum_col = float4(ao.x, ao.x, ao.x, col.w);
|
---|
132 | //OUT.illum_col.xyz = float3(1.0f - ao.x, 1.0f - ao.y * 1e-2f, 1);
|
---|
133 | OUT.illum_col.w = col.w;
|
---|
134 |
|
---|
135 | return OUT;
|
---|
136 | }
|
---|