]> gitweb.factorcode.org Git - factor.git/blob - extra/gpu/effects/blur/blur.factor
ed72b283749290486a23dc2f73dc72a82329c089
[factor.git] / extra / gpu / effects / blur / blur.factor
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
8 GLSL-SHADER: blur-fragment-shader fragment-shader
9 uniform sampler2D texture;
10 uniform bool horizontal;
11 uniform float blurSize;
12 varying vec2 texcoord;
13 void main()
14 {
15     vec4 col = 0.16 * texture2D(texture, texcoord);
16     if (horizontal)
17     {
18         vec2 blurX1 = vec2(blurSize, 0.0);
19         vec2 blurX2 = vec2(blurSize * 2.0, 0.0);
20         vec2 blurX3 = vec2(blurSize * 3.0, 0.0);
21         vec2 blurX4 = vec2(blurSize * 4.0, 0.0);
22         col += 0.15 * (  texture2D(texture, texcoord - blurX1)
23                        + texture2D(texture, texcoord + blurX1));
24         col += 0.12 * (  texture2D(texture, texcoord - blurX2)
25                        + texture2D(texture, texcoord + blurX2));
26         col += 0.09 * (  texture2D(texture, texcoord - blurX3)
27                        + texture2D(texture, texcoord + blurX3));
28         col += 0.05 * (  texture2D(texture, texcoord - blurX4)
29                        + texture2D(texture, texcoord + blurX4));
30     }
31     else
32     {
33         vec2 blurY1 = vec2(0.0, blurSize);
34         vec2 blurY2 = vec2(0.0, blurSize * 2.0);
35         vec2 blurY3 = vec2(0.0, blurSize * 3.0);
36         vec2 blurY4 = vec2(0.0, blurSize * 4.0);
37         col += 0.15 * (  texture2D(texture, texcoord - blurY1)
38                        + texture2D(texture, texcoord + blurY1));
39         col += 0.12 * (  texture2D(texture, texcoord - blurY2)
40                        + texture2D(texture, texcoord + blurY2));
41         col += 0.09 * (  texture2D(texture, texcoord - blurY3)
42                        + texture2D(texture, texcoord + blurY3));
43         col += 0.05 * (  texture2D(texture, texcoord - blurY4)
44                        + texture2D(texture, texcoord + blurY4));
45     }
46     gl_FragColor = col;
47 }
48 ;
49    
50 UNIFORM-TUPLE: blur-uniforms
51     { "texture"    texture-uniform f }
52     { "horizontal" bool-uniform    f }
53     { "blurSize"   float-uniform   f } ;
54
55 GLSL-PROGRAM: blur-program window-vertex-shader blur-fragment-shader window-vertex-format ;
56                         
57 :: (blur) ( texture horizontal? framebuffer dim -- )
58     { 0 0 } dim <rect> <viewport-state> set-gpu-state
59     texture horizontal? 1.0 dim horizontal? [ first ] [ second ] if / blur-uniforms boa framebuffer {
60         { "primitive-mode" [ 2drop triangle-strip-mode ] }
61         { "uniforms"       [ drop ] }
62         { "vertex-array"   [ 2drop blur-program <program-instance> <window-vertex-array> &dispose ] }
63         { "indexes"        [ 2drop T{ index-range f 0 4 } ] }
64         { "framebuffer"    [ nip ] }
65     } 2<render-set> render ;
66                          
67 :: blur ( texture horizontal? -- texture )
68     texture 0 texture-dim :> dim
69     dim RGB float-components <2d-render-texture> :> ( target-framebuffer target-texture )
70     texture horizontal? target-framebuffer dim (blur)
71     target-framebuffer dispose
72     target-texture ;
73                          
74 : horizontal-blur ( texture -- texture ) t blur ; inline
75                          
76 : vertical-blur ( texture -- texture ) f blur ; inline
77
78 : discompose ( quot1 quot2 -- compose )
79     '[ @ &dispose @ ] with-destructors ; inline
80
81 : gaussian-blur ( texture -- texture )
82     [ horizontal-blur ] [ vertical-blur ] discompose ;