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 BilateralFilter(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 |
|
---|
79 | for (int i = 0; i < NUM_SSAO_FILTERSAMPLES; ++ i)
|
---|
80 | {
|
---|
81 | offs = float4(texCoord + filterOffs[i] * scale, 0, 0);
|
---|
82 | aoSample = tex2Dlod(ssaoTex, offs);
|
---|
83 |
|
---|
84 | sampleNorm = normalize(tex2Dlod(normalsTex, offs).xyz);
|
---|
85 | depthFactor = clamp(1.0f - abs(1.0f - eyeSpaceDepth / aoSample.w), 1e-3f, 1.0f);
|
---|
86 | //sampleNorm = tex2Dlod(normalsTex, offs).xyz;
|
---|
87 |
|
---|
88 | w = filterWeights[i] * max(dot(sampleNorm, norm), .0f) * depthFactor;
|
---|
89 |
|
---|
90 | average += aoSample.x * w;
|
---|
91 | total_w += w;
|
---|
92 | }
|
---|
93 |
|
---|
94 | average *= 1.0f / max(total_w, 1e-6f);
|
---|
95 |
|
---|
96 | return average;
|
---|
97 | }
|
---|
98 |
|
---|
99 |
|
---|
100 | pixel combine(fragment IN,
|
---|
101 | uniform sampler2D colorsTex,
|
---|
102 | uniform sampler2D ssaoTex,
|
---|
103 | uniform sampler2D normalsTex,
|
---|
104 | uniform float2 filterOffs[NUM_SSAO_FILTERSAMPLES],
|
---|
105 | uniform float filterWeights[NUM_SSAO_FILTERSAMPLES]
|
---|
106 | )
|
---|
107 | {
|
---|
108 | pixel OUT;
|
---|
109 |
|
---|
110 | float4 col = tex2Dlod(colorsTex, float4(IN.texCoord, 0, 0));
|
---|
111 | float4 ao = tex2Dlod(ssaoTex, float4(IN.texCoord, 0, 0));
|
---|
112 |
|
---|
113 | if ((ao.y < 60.0f) && (col.w < 1e10f))
|
---|
114 | {
|
---|
115 | const static float scaleFactor = 10.0f;
|
---|
116 |
|
---|
117 | //ao.x = Filter(IN.texCoord, ssaoTex, filterOffs, filterWeights);
|
---|
118 | //ao.x = Filter(IN.texCoord, ssaoTex, filterOffs, filterWeights, 1.0f / (1.0f + ao.y));
|
---|
119 | ao.x = BilateralFilter(IN.texCoord, ao, ssaoTex, normalsTex, filterOffs, filterWeights, scaleFactor / (scaleFactor + ao.y));
|
---|
120 | //ao.x = BilateralFilter(IN.texCoord, ao, ssaoTex, normalsTex, filterOffs, filterWeights, 1.0f);
|
---|
121 | }
|
---|
122 |
|
---|
123 | OUT.illum_col = col * ao.x;
|
---|
124 | //OUT.illum_col = float4(ao.x, ao.x, ao.x, col.w);
|
---|
125 | //OUT.illum_col.xyz = float3(1.0f - ao.x, 1.0f - ao.y * 1e-2f, 1);
|
---|
126 | OUT.illum_col.w = col.w;
|
---|
127 |
|
---|
128 | return OUT;
|
---|
129 | }
|
---|