]> gitweb.factorcode.org Git - factor.git/blob - extra/gpu/effects/blur/blur.factor
factor: more using trim
[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: destructors gpu.render gpu.shaders gpu.state gpu.textures
4 gpu.util images kernel math math.rectangles sequences ;
5 IN: gpu.effects.blur
6
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
49 UNIFORM-TUPLE: blur-uniforms
50     { "texture"    texture-uniform f }
51     { "horizontal" bool-uniform    f }
52     { "blurSize"   float-uniform   f } ;
53
54 GLSL-PROGRAM: blur-program window-vertex-shader blur-fragment-shader window-vertex-format ;
55
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 ;
65
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
71     target-texture ;
72
73 : horizontal-blur ( texture -- texture ) t blur ; inline
74
75 : vertical-blur ( texture -- texture ) f blur ; inline
76
77 : discompose ( quot1 quot2 -- compose )
78     '[ @ &dispose @ ] with-destructors ; inline
79
80 : gaussian-blur ( texture -- texture )
81     [ horizontal-blur ] [ vertical-blur ] discompose ;