1 ! Copyright (C) 2008 Joe Groff.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel opengl.gl alien.c-types continuations namespaces
4 assocs alien alien.strings libc opengl math sequences combinators
5 macros arrays io.encodings.ascii fry specialized-arrays.uint
6 destructors accessors ;
9 : with-gl-shader-source-ptr ( string quot -- )
10 swap ascii malloc-string [ <void*> swap call ] keep free ; inline
12 : <gl-shader> ( source kind -- shader )
13 glCreateShader dup rot
14 [ 1 swap f glShaderSource ] with-gl-shader-source-ptr
15 [ glCompileShader ] keep
18 : (gl-shader?) ( object -- ? )
19 dup integer? [ glIsShader c-bool> ] [ drop f ] if ;
21 : gl-shader-get-int ( shader enum -- value )
22 0 <int> [ glGetShaderiv ] keep *int ;
24 : gl-shader-ok? ( shader -- ? )
25 GL_COMPILE_STATUS gl-shader-get-int c-bool> ;
27 : <vertex-shader> ( source -- vertex-shader )
28 GL_VERTEX_SHADER <gl-shader> ; inline
30 : (vertex-shader?) ( object -- ? )
32 [ GL_SHADER_TYPE gl-shader-get-int GL_VERTEX_SHADER = ]
35 : <fragment-shader> ( source -- fragment-shader )
36 GL_FRAGMENT_SHADER <gl-shader> ; inline
38 : (fragment-shader?) ( object -- ? )
40 [ GL_SHADER_TYPE gl-shader-get-int GL_FRAGMENT_SHADER = ]
43 : gl-shader-info-log-length ( shader -- log-length )
44 GL_INFO_LOG_LENGTH gl-shader-get-int ; inline
46 : gl-shader-info-log ( shader -- log )
47 dup gl-shader-info-log-length dup [
49 [ 0 <int> swap glGetShaderInfoLog ] keep
53 : check-gl-shader ( shader -- shader )
54 dup gl-shader-ok? [ dup gl-shader-info-log throw ] unless ;
56 : delete-gl-shader ( shader -- ) glDeleteShader ; inline
58 PREDICATE: gl-shader < integer (gl-shader?) ;
59 PREDICATE: vertex-shader < gl-shader (vertex-shader?) ;
60 PREDICATE: fragment-shader < gl-shader (fragment-shader?) ;
64 : <gl-program> ( shaders -- program )
66 [ dupd glAttachShader ] each
67 [ glLinkProgram ] keep
70 : (gl-program?) ( object -- ? )
71 dup integer? [ glIsProgram c-bool> ] [ drop f ] if ;
73 : gl-program-get-int ( program enum -- value )
74 0 <int> [ glGetProgramiv ] keep *int ;
76 : gl-program-ok? ( program -- ? )
77 GL_LINK_STATUS gl-program-get-int c-bool> ;
79 : gl-program-info-log-length ( program -- log-length )
80 GL_INFO_LOG_LENGTH gl-program-get-int ; inline
82 : gl-program-info-log ( program -- log )
83 dup gl-program-info-log-length dup [
85 [ 0 <int> swap glGetProgramInfoLog ] keep
89 : check-gl-program ( program -- program )
90 dup gl-program-ok? [ dup gl-program-info-log throw ] unless ;
92 : gl-program-shaders-length ( program -- shaders-length )
93 GL_ATTACHED_SHADERS gl-program-get-int ; inline
95 : gl-program-shaders ( program -- shaders )
96 dup gl-program-shaders-length
99 [ underlying>> glGetAttachedShaders ] keep ;
101 : delete-gl-program-only ( program -- )
102 glDeleteProgram ; inline
104 : detach-gl-program-shader ( program shader -- )
105 glDetachShader ; inline
107 : delete-gl-program ( program -- )
108 dup gl-program-shaders [
109 2dup detach-gl-program-shader delete-gl-shader
110 ] each delete-gl-program-only ;
112 : with-gl-program ( program quot -- )
113 over glUseProgram [ 0 glUseProgram ] [ ] cleanup ; inline
115 PREDICATE: gl-program < integer (gl-program?) ;
117 : <simple-gl-program> ( vertex-shader-source fragment-shader-source -- program )
118 [ <vertex-shader> check-gl-shader ]
119 [ <fragment-shader> check-gl-shader ] bi*
120 2array <gl-program> check-gl-program ;