From c906d26b131882a8d6826216c320b8b834681da0 Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Thu, 7 Feb 2008 22:43:05 -0800 Subject: [PATCH] Do dynamic lookup of OpenGL 1.2+ functions on all platforms. Use fallback extension names when the official name is not available, e.g., if glUseProgram is missing try glUseProgramObjectARB instead --- extra/hash2/hash2-docs.factor | 2 +- extra/opengl/gl/extensions/extensions.factor | 46 ++++++++++++++++++++ extra/opengl/gl/gl.factor | 12 +++-- extra/opengl/gl/macosx/macosx.factor | 6 +++ extra/opengl/gl/unix/unix.factor | 12 ++--- extra/opengl/gl/windows/windows.factor | 37 ++-------------- extra/sequences/lib/lib.factor | 13 +++++- extra/x11/glx/glx.factor | 2 +- 8 files changed, 79 insertions(+), 51 deletions(-) create mode 100644 extra/opengl/gl/extensions/extensions.factor create mode 100644 extra/opengl/gl/macosx/macosx.factor diff --git a/extra/hash2/hash2-docs.factor b/extra/hash2/hash2-docs.factor index 5bcbb7cc11..b3e1a53cea 100644 --- a/extra/hash2/hash2-docs.factor +++ b/extra/hash2/hash2-docs.factor @@ -1,7 +1,7 @@ USING: help.syntax help.markup ; IN: hash2 -ARTICLE: { "hash2" "intro" } +ARTICLE: { "hash2" "intro" } "hash2 Vocabulary" "The hash2 vocabulary specifies a simple minimal datastructure for hash tables with two integers as keys. These hash tables are fixed size and do not conform to the associative mapping protocol. Words used in creating and manipulating these hash tables include:" { $subsection } { $subsection hash2 } diff --git a/extra/opengl/gl/extensions/extensions.factor b/extra/opengl/gl/extensions/extensions.factor new file mode 100644 index 0000000000..e05e3a1af5 --- /dev/null +++ b/extra/opengl/gl/extensions/extensions.factor @@ -0,0 +1,46 @@ +USING: alien alien.syntax combinators kernel parser sequences +system words namespaces hashtables init math arrays assocs +sequences.lib continuations ; +<< { + { [ windows? ] [ "opengl.gl.windows" ] } + { [ macosx? ] [ "opengl.gl.macosx" ] } + { [ unix? ] [ "opengl.gl.unix" ] } + { [ t ] [ "Unknown OpenGL platform" throw ] } +} cond use+ >> +IN: opengl.gl.extensions + +SYMBOL: +gl-function-number-counter+ +SYMBOL: +gl-function-pointers+ + +: reset-gl-function-number-counter ( -- ) + 0 +gl-function-number-counter+ set-global ; +: reset-gl-function-pointers ( -- ) + 100 +gl-function-pointers+ set-global ; + +[ reset-gl-function-pointers ] "opengl.gl init hook" add-init-hook +reset-gl-function-pointers +reset-gl-function-number-counter + +: gl-function-number ( -- n ) + +gl-function-number-counter+ get-global + dup 1+ +gl-function-number-counter+ set-global ; + +: gl-function-pointer ( names n -- funptr ) + gl-function-context 2array dup +gl-function-pointers+ get-global at + [ 2nip ] [ + >r [ gl-function-address ] attempt-each + dup [ "OpenGL function not available" throw ] unless + dup r> + +gl-function-pointers+ get-global set-at + ] if* ; + +: GL-FUNCTION: + gl-function-calling-convention + scan + scan dup + scan drop "}" parse-tokens swap add* + gl-function-number + [ gl-function-pointer ] 2curry swap + ";" parse-tokens [ "()" subseq? not ] subset + define-indirect + ; parsing diff --git a/extra/opengl/gl/gl.factor b/extra/opengl/gl/gl.factor index 5b3dade851..59b2422d73 100644 --- a/extra/opengl/gl/gl.factor +++ b/extra/opengl/gl/gl.factor @@ -3,8 +3,8 @@ ! This file is based on the gl.h that comes with xorg-x11 6.8.2 -USING: alien alien.syntax kernel parser sequences system words ; -<< windows? "opengl.gl.windows" "opengl.gl.unix" ? use+ >> +USING: alien alien.syntax combinators kernel parser sequences +system words opengl.gl.extensions ; IN: opengl.gl @@ -1119,9 +1119,7 @@ FUNCTION: void glLoadName ( GLuint name ) ; FUNCTION: void glPushName ( GLuint name ) ; FUNCTION: void glPopName ( ) ; - -! OpenGL extension functions - +<< reset-gl-function-number-counter >> ! OpenGL 1.2 @@ -1273,7 +1271,7 @@ GL-FUNCTION: void glTexSubImage3D { glTexSubImage3DEXT } ( GLenum target, GLint : GL_DOT3_RGBA HEX: 86AF ; inline : GL_MULTISAMPLE_BIT HEX: 20000000 ; inline -GL-FUNCTION: void glActiveTexture { glActiveTextureARB }( GLenum texture ) ; +GL-FUNCTION: void glActiveTexture { glActiveTextureARB } ( GLenum texture ) ; GL-FUNCTION: void glClientActiveTexture { glClientActiveTextureARB } ( GLenum texture ) ; GL-FUNCTION: void glCompressedTexImage1D { glCompressedTexImage1DARB } ( GLenum target, GLint level, GLenum internalformat, GLsizei width, GLint border, GLsizei imageSize, GLvoid* data ) ; GL-FUNCTION: void glCompressedTexImage2D { glCompressedTexImage2DARB } ( GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLint border, GLsizei imageSize, GLvoid* data ) ; @@ -1607,7 +1605,7 @@ GL-FUNCTION: void glGetVertexAttribdv { glGetVertexAttribdvARB } ( GLuint index, GL-FUNCTION: void glGetVertexAttribfv { glGetVertexAttribfvARB } ( GLuint index, GLenum pname, GLfloat* params ) ; GL-FUNCTION: void glGetVertexAttribiv { glGetVertexAttribivARB } ( GLuint index, GLenum pname, GLint* params ) ; GL-FUNCTION: GLboolean glIsProgram { glIsProgramARB } ( GLuint program ) ; -GL-FUNCTION: GLboolean glIsShader { glIsShaderARB }( GLuint shader ) ; +GL-FUNCTION: GLboolean glIsShader { glIsShaderARB } ( GLuint shader ) ; GL-FUNCTION: void glLinkProgram { glLinkProgramARB } ( GLuint program ) ; GL-FUNCTION: void glShaderSource { glShaderSourceARB } ( GLuint shader, GLsizei count, GLchar** strings, GLint* lengths ) ; GL-FUNCTION: void glStencilFuncSeparate { glStencilFuncSeparateATI } ( GLenum frontfunc, GLenum backfunc, GLint ref, GLuint mask ) ; diff --git a/extra/opengl/gl/macosx/macosx.factor b/extra/opengl/gl/macosx/macosx.factor new file mode 100644 index 0000000000..3d4cb6ae93 --- /dev/null +++ b/extra/opengl/gl/macosx/macosx.factor @@ -0,0 +1,6 @@ +USING: kernel alien ; +IN: opengl.gl.macosx + +: gl-function-context ( -- context ) 0 ; inline +: gl-function-address ( name -- address ) "gl" load-library dlsym ; inline +: gl-function-calling-convention ( -- str ) "cdecl" ; inline diff --git a/extra/opengl/gl/unix/unix.factor b/extra/opengl/gl/unix/unix.factor index 2ee4558a5e..d36e4a2906 100644 --- a/extra/opengl/gl/unix/unix.factor +++ b/extra/opengl/gl/unix/unix.factor @@ -1,10 +1,6 @@ -USING: alien.syntax alien.syntax.private kernel - namespaces parser sequences syntax words ; - +USING: kernel x11.glx ; IN: opengl.gl.unix -: GL-FUNCTION: - scan "c-library" get scan - scan drop "}" parse-tokens drop - ";" parse-tokens [ "()" subseq? not ] subset - define-function ; parsing +: gl-function-context ( -- context ) glXGetCurrentContext ; inline +: gl-function-address ( name -- address ) glXGetProcAddress ; inline +: gl-function-calling-convention ( -- str ) "cdecl" ; inline diff --git a/extra/opengl/gl/windows/windows.factor b/extra/opengl/gl/windows/windows.factor index fc0bca00bb..cc59167539 100755 --- a/extra/opengl/gl/windows/windows.factor +++ b/extra/opengl/gl/windows/windows.factor @@ -1,35 +1,6 @@ -USING: alien alien.syntax arrays assocs hashtables init kernel - libc math namespaces parser sequences syntax system vectors - windows.opengl32 ; - +USING: kernel windows.opengl32 ; IN: opengl.gl.windows - gl-function-pointers set ] "opengl.gl.windows init hook" add-init-hook - -: gl-function-number ( -- n ) - gl-function-number-counter get - dup 1+ gl-function-number-counter set ; - -: gl-function-pointer ( name n -- funptr ) - wglGetCurrentContext 2array dup gl-function-pointers get at - [ -rot 2drop ] - [ >r wglGetProcAddress dup r> gl-function-pointers get set-at ] - if* ; - -PRIVATE> - -: GL-FUNCTION: - "stdcall" - scan - scan - dup gl-function-number [ gl-function-pointer ] 2curry swap - scan drop "}" parse-tokens drop - ";" parse-tokens [ "()" subseq? not ] subset - define-indirect - ; parsing +: gl-function-context ( -- context ) wglGetCurrentContext alien-address ; inline +: gl-function-address ( name -- address ) wglGetProcAddress ; inline +: gl-function-calling-convention ( -- str ) "stdcall" ; inline diff --git a/extra/sequences/lib/lib.factor b/extra/sequences/lib/lib.factor index d89c5eec89..b761bf957f 100755 --- a/extra/sequences/lib/lib.factor +++ b/extra/sequences/lib/lib.factor @@ -1,6 +1,6 @@ USING: combinators.lib kernel sequences math namespaces assocs random sequences.private shuffle math.functions mirrors -arrays math.parser sorting strings ascii ; +arrays math.parser math.private sorting strings ascii ; IN: sequences.lib ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @@ -153,3 +153,14 @@ PRIVATE> [ = [ ] [ drop f ] if ] curry 2map [ ] subset ; + + + +: attempt-each ( seq quot -- result ) + (each) iterate-prep (attempt-each-integer) ; inline \ No newline at end of file diff --git a/extra/x11/glx/glx.factor b/extra/x11/glx/glx.factor index 2b1d05e2e4..becf6fad28 100644 --- a/extra/x11/glx/glx.factor +++ b/extra/x11/glx/glx.factor @@ -42,7 +42,7 @@ FUNCTION: GLXContext glXCreateContext ( Display* dpy, XVisualInfo* vis, GLXConte FUNCTION: GLXPixmap glXCreateGLXPixmap ( Display* dpy, XVisualInfo* vis, Pixmap pixmap ) ; FUNCTION: void glXDestroyContext ( Display* dpy, GLXContext ctx ) ; FUNCTION: void glXDestroyGLXPixmap ( Display* dpy, GLXPixmap pix ) ; -FUNCTION: int glXGetConfig ( Display* dpy, XVisualInfo* vis, int attrib, int* value) ; +FUNCTION: int glXGetConfig ( Display* dpy, XVisualInfo* vis, int attrib, int* value ) ; FUNCTION: GLXContext glXGetCurrentContext ( ) ; FUNCTION: GLXDrawable glXGetCurrentDrawable ( ) ; FUNCTION: bool glXIsDirect ( Display* dpy, GLXContext ctx ) ;