bilateralH.comp 1.7 KB

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