! Copyright (C) 2008 Joe Groff.
! See http://factorcode.org/license.txt for BSD license.
USING: kernel opengl.gl alien.c-types continuations namespaces
-assocs alien alien.strings libc opengl math sequences combinators
-combinators.lib macros arrays io.encodings.ascii fry ;
+assocs alien alien.data alien.strings libc opengl math sequences combinators
+macros arrays io.encodings.ascii fry specialized-arrays
+destructors accessors ;
+SPECIALIZED-ARRAY: uint
IN: opengl.shaders
: with-gl-shader-source-ptr ( string quot -- )
dup integer? [ glIsShader c-bool> ] [ drop f ] if ;
: gl-shader-get-int ( shader enum -- value )
- 0 <int> [ glGetShaderiv ] keep *int ;
+ { int } [ glGetShaderiv ] [ ] with-out-parameters ;
: gl-shader-ok? ( shader -- ? )
GL_COMPILE_STATUS gl-shader-get-int c-bool> ;
: gl-shader-info-log ( shader -- log )
dup gl-shader-info-log-length dup [
+ 1 calloc &free
[ 0 <int> swap glGetShaderInfoLog ] keep
ascii alien>string
- ] with-malloc ;
+ ] with-destructors ;
: check-gl-shader ( shader -- shader )
dup gl-shader-ok? [ dup gl-shader-info-log throw ] unless ;
! Programs
+: (gl-program) ( shaders quot: ( gl-program -- ) -- program )
+ glCreateProgram
+ [
+ [ swap [ glAttachShader ] with each ]
+ [ swap call ] bi-curry bi*
+ ] [ glLinkProgram ] [ ] tri gl-error ; inline
+
+: <mrt-gl-program> ( shaders frag-data-locations -- program )
+ [ [ first2 swap glBindFragDataLocation ] with each ] curry (gl-program) ;
+
: <gl-program> ( shaders -- program )
- glCreateProgram swap
- [ dupd glAttachShader ] each
- [ glLinkProgram ] keep
- gl-error ;
+ [ drop ] (gl-program) ;
: (gl-program?) ( object -- ? )
dup integer? [ glIsProgram c-bool> ] [ drop f ] if ;
: gl-program-get-int ( program enum -- value )
- 0 <int> [ glGetProgramiv ] keep *int ;
+ { int } [ glGetProgramiv ] [ ] with-out-parameters ;
: gl-program-ok? ( program -- ? )
GL_LINK_STATUS gl-program-get-int c-bool> ;
: gl-program-info-log ( program -- log )
dup gl-program-info-log-length dup [
+ 1 calloc &free
[ 0 <int> swap glGetProgramInfoLog ] keep
ascii alien>string
- ] with-malloc ;
+ ] with-destructors ;
: check-gl-program ( program -- program )
dup gl-program-ok? [ dup gl-program-info-log throw ] unless ;
: gl-program-shaders-length ( program -- shaders-length )
GL_ATTACHED_SHADERS gl-program-get-int ; inline
+! On some macosx-x86-64 graphics drivers, glGetAttachedShaders tries to treat the
+! shaders parameter as a ulonglong array rather than a GLuint array as documented.
+! We hack around this by allocating a buffer twice the size and sifting out the zero
+! values
+
: gl-program-shaders ( program -- shaders )
- dup gl-program-shaders-length
- dup "GLuint" <c-array>
- 0 <int> swap
- [ glGetAttachedShaders ] { 3 1 } multikeep
- c-uint-array> ;
+ dup gl-program-shaders-length 2 *
+ 0 <int>
+ over <uint-array>
+ [ glGetAttachedShaders ] keep [ zero? not ] filter ;
: delete-gl-program-only ( program -- )
glDeleteProgram ; inline
PREDICATE: gl-program < integer (gl-program?) ;
: <simple-gl-program> ( vertex-shader-source fragment-shader-source -- program )
- >r <vertex-shader> check-gl-shader
- r> <fragment-shader> check-gl-shader
+ [ <vertex-shader> check-gl-shader ]
+ [ <fragment-shader> check-gl-shader ] bi*
2array <gl-program> check-gl-program ;