1 ! Copyright (C) 2010 Erik Charlebois.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: destructors gpu.render gpu.shaders gpu.state gpu.textures
4 gpu.util images kernel math math.rectangles sequences ;
7 GLSL-SHADER: blur-fragment-shader fragment-shader
8 uniform sampler2D texture;
9 uniform bool horizontal;
10 uniform float blurSize;
11 varying vec2 texcoord;
14 vec4 col = 0.16 * texture2D(texture, texcoord);
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));
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));
49 UNIFORM-TUPLE: blur-uniforms
50 { "texture" texture-uniform f }
51 { "horizontal" bool-uniform f }
52 { "blurSize" float-uniform f } ;
54 GLSL-PROGRAM: blur-program window-vertex-shader blur-fragment-shader window-vertex-format ;
56 :: (blur) ( texture horizontal? framebuffer dim -- )
57 { 0 0 } dim <rect> <viewport-state> set-gpu-state
58 texture horizontal? 1.0 dim horizontal? [ first ] [ second ] if / blur-uniforms boa framebuffer {
59 { "primitive-mode" [ 2drop triangle-strip-mode ] }
60 { "uniforms" [ drop ] }
61 { "vertex-array" [ 2drop blur-program <program-instance> <window-vertex-array> &dispose ] }
62 { "indexes" [ 2drop T{ index-range f 0 4 } ] }
63 { "framebuffer" [ nip ] }
64 } 2<render-set> render ;
66 :: blur ( texture horizontal? -- texture )
67 texture 0 texture-dim :> dim
68 dim RGB float-components <2d-render-texture> :> ( target-framebuffer target-texture )
69 texture horizontal? target-framebuffer dim (blur)
70 target-framebuffer dispose
73 : horizontal-blur ( texture -- texture ) t blur ; inline
75 : vertical-blur ( texture -- texture ) f blur ; inline
77 : discompose ( quot1 quot2 -- compose )
78 '[ @ &dispose @ ] with-destructors ; inline
80 : gaussian-blur ( texture -- texture )
81 [ horizontal-blur ] [ vertical-blur ] discompose ;