/extra/gpu/effects/blur/blur.factor

http://github.com/abeaumont/factor · Factor · 82 lines · 71 code · 9 blank · 2 comment · 12 complexity · cfaf2bcc4e9f6112de0733d63ca2f4e9 MD5 · raw file

  1. ! Copyright (C) 2010 Erik Charlebois.
  2. ! See http://factorcode.org/license.txt for BSD license.
  3. USING: arrays destructors fry gpu.framebuffers gpu.render gpu.shaders
  4. gpu.state gpu.textures gpu.util images kernel locals math
  5. math.rectangles sequences ;
  6. IN: gpu.effects.blur
  7. GLSL-SHADER: blur-fragment-shader fragment-shader
  8. uniform sampler2D texture;
  9. uniform bool horizontal;
  10. uniform float blurSize;
  11. varying vec2 texcoord;
  12. void main()
  13. {
  14. vec4 col = 0.16 * texture2D(texture, texcoord);
  15. if (horizontal)
  16. {
  17. vec2 blurX1 = vec2(blurSize, 0.0);
  18. vec2 blurX2 = vec2(blurSize * 2.0, 0.0);
  19. vec2 blurX3 = vec2(blurSize * 3.0, 0.0);
  20. vec2 blurX4 = vec2(blurSize * 4.0, 0.0);
  21. col += 0.15 * ( texture2D(texture, texcoord - blurX1)
  22. + texture2D(texture, texcoord + blurX1));
  23. col += 0.12 * ( texture2D(texture, texcoord - blurX2)
  24. + texture2D(texture, texcoord + blurX2));
  25. col += 0.09 * ( texture2D(texture, texcoord - blurX3)
  26. + texture2D(texture, texcoord + blurX3));
  27. col += 0.05 * ( texture2D(texture, texcoord - blurX4)
  28. + texture2D(texture, texcoord + blurX4));
  29. }
  30. else
  31. {
  32. vec2 blurY1 = vec2(0.0, blurSize);
  33. vec2 blurY2 = vec2(0.0, blurSize * 2.0);
  34. vec2 blurY3 = vec2(0.0, blurSize * 3.0);
  35. vec2 blurY4 = vec2(0.0, blurSize * 4.0);
  36. col += 0.15 * ( texture2D(texture, texcoord - blurY1)
  37. + texture2D(texture, texcoord + blurY1));
  38. col += 0.12 * ( texture2D(texture, texcoord - blurY2)
  39. + texture2D(texture, texcoord + blurY2));
  40. col += 0.09 * ( texture2D(texture, texcoord - blurY3)
  41. + texture2D(texture, texcoord + blurY3));
  42. col += 0.05 * ( texture2D(texture, texcoord - blurY4)
  43. + texture2D(texture, texcoord + blurY4));
  44. }
  45. gl_FragColor = col;
  46. }
  47. ;
  48. UNIFORM-TUPLE: blur-uniforms
  49. { "texture" texture-uniform f }
  50. { "horizontal" bool-uniform f }
  51. { "blurSize" float-uniform f } ;
  52. GLSL-PROGRAM: blur-program window-vertex-shader blur-fragment-shader window-vertex-format ;
  53. :: (blur) ( texture horizontal? framebuffer dim -- )
  54. { 0 0 } dim <rect> <viewport-state> set-gpu-state
  55. texture horizontal? 1.0 dim horizontal? [ first ] [ second ] if / blur-uniforms boa framebuffer {
  56. { "primitive-mode" [ 2drop triangle-strip-mode ] }
  57. { "uniforms" [ drop ] }
  58. { "vertex-array" [ 2drop blur-program <program-instance> <window-vertex-array> &dispose ] }
  59. { "indexes" [ 2drop T{ index-range f 0 4 } ] }
  60. { "framebuffer" [ nip ] }
  61. } 2<render-set> render ;
  62. :: blur ( texture horizontal? -- texture )
  63. texture 0 texture-dim :> dim
  64. dim RGB float-components <2d-render-texture> :> ( target-framebuffer target-texture )
  65. texture horizontal? target-framebuffer dim (blur)
  66. target-framebuffer dispose
  67. target-texture ;
  68. : horizontal-blur ( texture -- texture ) t blur ; inline
  69. : vertical-blur ( texture -- texture ) f blur ; inline
  70. : discompose ( quot1 quot2 -- compose )
  71. '[ @ &dispose @ ] with-destructors ; inline
  72. : gaussian-blur ( texture -- texture )
  73. [ horizontal-blur ] [ vertical-blur ] discompose ;