]> gitweb.factorcode.org Git - factor.git/blob - extra/gpu/util/util.factor
2678f0452c950cd6213047bdc13eab9b159cf015
[factor.git] / extra / gpu / util / util.factor
1 ! (c)2009 Joe Groff bsd license
2 USING: arrays destructors gpu.buffers gpu.framebuffers gpu.render
3 gpu.shaders gpu.state gpu.textures images kernel locals math
4 math.rectangles opengl.gl sequences specialized-arrays ;
5 FROM: alien.c-types => float ;
6 SPECIALIZED-ARRAY: float
7 IN: gpu.util
8
9 CONSTANT: environment-cube-map-mv-matrices
10     H{
11         { +X {
12             {  0.0  0.0 -1.0  0.0 }
13             {  0.0 -1.0  0.0  0.0 }
14             { -1.0  0.0  0.0  0.0 }
15             {  0.0  0.0  0.0  1.0 }
16         } }
17         { +Y {
18             {  1.0  0.0  0.0  0.0 }
19             {  0.0  0.0  1.0  0.0 }
20             {  0.0 -1.0  0.0  0.0 }
21             {  0.0  0.0  0.0  1.0 }
22         } }
23         { +Z {
24             {  1.0  0.0  0.0  0.0 }
25             {  0.0 -1.0  0.0  0.0 }
26             {  0.0  0.0 -1.0  0.0 }
27             {  0.0  0.0  0.0  1.0 }
28         } }
29         { -X {
30             {  0.0  0.0  1.0  0.0 }
31             {  0.0 -1.0  0.0  0.0 }
32             {  1.0  0.0  0.0  0.0 }
33             {  0.0  0.0  0.0  1.0 }
34         } }
35         { -Y {
36             {  1.0  0.0  0.0  0.0 }
37             {  0.0  0.0 -1.0  0.0 }
38             {  0.0  1.0  0.0  0.0 }
39             {  0.0  0.0  0.0  1.0 }
40         } }
41         { -Z {
42             { -1.0  0.0  0.0  0.0 }
43             {  0.0 -1.0  0.0  0.0 }
44             {  0.0  0.0  1.0  0.0 }
45             {  0.0  0.0  0.0  1.0 }
46         } }
47     }
48     
49 GLSL-SHADER: window-vertex-shader vertex-shader
50 attribute vec2 vertex;
51 varying vec2 texcoord;
52 void main()
53 {
54     texcoord = vertex * vec2(0.5) + vec2(0.5);
55     gl_Position = vec4(vertex, 0.0, 1.0);
56 }
57 ;
58
59 GLSL-SHADER: window-fragment-shader fragment-shader
60 uniform sampler2D texture;
61 varying vec2 texcoord;
62 void main()
63 {
64     gl_FragColor = texture2D(texture, texcoord);
65 }
66 ;
67
68 VERTEX-FORMAT: window-vertex-format
69     { "vertex" float-components 2 f } ;
70
71 UNIFORM-TUPLE: window-uniforms
72     { "texture" texture-uniform f } ;
73
74 GLSL-PROGRAM: window-program window-vertex-shader window-fragment-shader window-vertex-format ;
75
76 GLSL-SHADER: window-point-vertex-shader vertex-shader
77 uniform float point_size;
78 attribute vec2 vertex;
79 void main()
80 {
81     gl_Position  = vec4(vertex, 0.0, 1.0);
82     gl_PointSize = point_size;
83 }
84 ;
85
86 GLSL-SHADER: window-point-fragment-shader fragment-shader
87 #version 120
88 uniform sampler2D texture;
89 void main()
90 {
91     gl_FragColor = texture2D(texture, gl_PointCoord);
92 }
93 ;
94
95 UNIFORM-TUPLE: window-point-uniforms
96     { "texture"    texture-uniform f }
97     { "point_size" float-uniform   f } ;
98
99 GLSL-PROGRAM: window-point-program window-point-vertex-shader window-point-fragment-shader window-vertex-format ;
100
101 CONSTANT: window-vertexes
102     float-array{
103         -1.0 -1.0
104         -1.0  1.0
105          1.0 -1.0
106          1.0  1.0
107     }
108
109 : <window-vertex-buffer> ( -- buffer )
110     window-vertexes 
111     static-upload draw-usage vertex-buffer
112     byte-array>buffer ; inline
113
114 : <window-vertex-array> ( program-instance -- vertex-array )
115     [ <window-vertex-buffer> ] dip window-vertex-format <vertex-array*> ; inline
116
117 :: <2d-render-texture> ( dim order type -- renderbuffer texture )
118     order type
119     T{ texture-parameters
120        { wrap clamp-texcoord-to-edge }
121        { min-filter filter-linear }
122        { min-mipmap-filter f } }
123     <texture-2d> [
124         0 <texture-2d-attachment> 1array f f dim <framebuffer>
125         dup { { default-attachment { 0 0 0 } } } clear-framebuffer
126     ] keep ;
127
128 : draw-texture ( texture dim -- )
129     { 0 0 } swap <rect> <viewport-state> set-gpu-state
130     {
131         { "primitive-mode" [ drop triangle-strip-mode ] }
132         { "uniforms"       [ window-uniforms boa ] }
133         { "vertex-array"   [ drop window-program <program-instance> <window-vertex-array> &dispose ] }
134         { "indexes"        [ drop T{ index-range f 0 4 } ] }
135     } <render-set> render ;
136
137 :: <streamed-vertex-array> ( verts program-instance -- vertex-array )
138     verts stream-upload draw-usage vertex-buffer byte-array>buffer &dispose
139     program-instance <vertex-array> &dispose ;
140
141 : (blended-point-sprite-batch) ( verts framebuffer texture point-size dim -- )
142     f eq-add func-one func-one <blend-mode> dup <blend-state> set-gpu-state
143     f origin-upper-left 1.0 <point-state> set-gpu-state
144     GL_POINT_SPRITE glEnable
145     { 0 0 } swap <rect> <viewport-state> set-gpu-state
146     window-point-uniforms boa {
147         { "primitive-mode" [ 3drop points-mode ] }
148         { "uniforms"       [ 2nip ] }
149         { "vertex-array"   [ 2drop window-point-program <program-instance> <streamed-vertex-array> ] }
150         { "indexes"        [ 2drop length 2 / 0 swap <index-range> ] }
151         { "framebuffer"    [ drop nip ] }
152     } 3<render-set> render ;
153     
154 :: blended-point-sprite-batch ( verts texture point-size dim -- texture )
155     dim RGB float-components <2d-render-texture> :> ( target-framebuffer target-texture )
156     verts target-framebuffer texture point-size dim (blended-point-sprite-batch)
157     target-framebuffer dispose
158     target-texture ;