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