1// From http://http.developer.nvidia.com/GPUGems3/gpugems3_ch40.html
2
3uniform sampler2D source;
4uniform sampler2D depth;
5uniform vec2 pixel;
6layout(r16f) volatile restrict writeonly uniform image2D dest;
7uniform float sigma = 5.;
8
9layout (local_size_x = 8, local_size_y = 8) in;
10
11shared float local_src[8 + 2 * 8][8];
12shared float local_depth[8 + 2 * 8][8];
13
14void main()
15{
16    int x = int(gl_LocalInvocationID.x), y = int(gl_LocalInvocationID.y);
17    ivec2 iuv = ivec2(gl_GlobalInvocationID.x, gl_GlobalInvocationID.y);
18    vec2 uv_m = (iuv - ivec2(8, 0)) * pixel;
19    vec2 uv = iuv * pixel;
20    vec2 uv_p = (iuv + ivec2(8, 0)) * pixel;
21
22    local_src[x][y] = texture(source, uv_m).x;
23    local_depth[x][y] = texture(depth, uv_m).x;
24    local_src[x + 8][y] = texture(source, uv).x;
25    local_depth[x + 8][y] = texture(depth, uv).x;
26    local_src[x + 16][y] = texture(source, uv_p).x;
27    local_depth[x + 16][y] = texture(depth, uv_p).x;
28
29    barrier();
30
31    float g0, g1, g2;
32    g0 = 1.0 / (sqrt(2.0 * 3.14) * sigma);
33    g1 = exp(-0.5 / (sigma * sigma));
34    g2 = g1 * g1;
35    float sum = local_src[x + 8][y] * g0;
36    float pixel_depth = local_depth[x + 8][y];
37    g0 *= g1;
38    g1 *= g2;
39    float tmp_weight, total_weight = g0;
40    for (int j = 1; j < 8; j++) {
41        tmp_weight = max(0.0, 1.0 - .001 * abs(local_depth[8 + x - j][y] - pixel_depth));
42        total_weight += g0 * tmp_weight;
43        sum += local_src[8 + x - j][y] * g0 * tmp_weight;
44        tmp_weight = max(0.0, 1.0 - .001 * abs(local_depth[8 + x + j][y] - pixel_depth));
45        total_weight += g0 * tmp_weight;
46        sum += local_src[8 + x + j][y] * g0 * tmp_weight;
47        g0 *= g1;
48        g1 *= g2;
49    }
50    imageStore(dest, iuv, vec4(sum / total_weight));
51}
52
53