From 31d97a8ff7cddd6c0a23ca284d5c9c4c5f32112e Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Wed, 17 Feb 2010 11:26:32 -0800 Subject: [PATCH] fall back to manual gl vertex attribute management when GL_APPLE_vertex_array_object is not present, so gpu can support vanilla GL 2.0 --- extra/gpu/gpu.factor | 4 +- extra/gpu/render/render.factor | 5 +- extra/gpu/shaders/shaders.factor | 80 ++++++++++++++++++++++++++------ extra/gpu/state/state.factor | 4 -- 4 files changed, 71 insertions(+), 22 deletions(-) mode change 100644 => 100755 extra/gpu/gpu.factor mode change 100644 => 100755 extra/gpu/render/render.factor mode change 100644 => 100755 extra/gpu/shaders/shaders.factor mode change 100644 => 100755 extra/gpu/state/state.factor diff --git a/extra/gpu/gpu.factor b/extra/gpu/gpu.factor old mode 100644 new mode 100755 index 6a61e2ec4f..1d02b3f07a --- a/extra/gpu/gpu.factor +++ b/extra/gpu/gpu.factor @@ -9,10 +9,12 @@ TUPLE: gpu-object < identity-tuple handle ; VARIANT: gpu-api opengl-2 opengl-3 ; +SYMBOL: has-vertex-array-objects? + : set-gpu-api ( -- ) "2.0" require-gl-version "3.0" { { "GL_ARB_vertex_array_object" "GL_APPLE_vertex_array_object" } } - require-gl-version-or-extensions + has-gl-version-or-extensions? has-vertex-array-objects? set-global "3.0" has-gl-version? opengl-3 opengl-2 ? gpu-api set-global ; HOOK: init-gpu-api gpu-api ( -- ) diff --git a/extra/gpu/render/render.factor b/extra/gpu/render/render.factor old mode 100644 new mode 100755 index 1d4813ab54..2b7d75a3ae --- a/extra/gpu/render/render.factor +++ b/extra/gpu/render/render.factor @@ -520,9 +520,6 @@ SYNTAX: UNIFORM-TUPLE: > glBindVertexArray ; - : bind-unnamed-output-attachments ( framebuffer attachments -- ) [ gl-attachment ] with map dup length 1 = @@ -567,7 +564,7 @@ UNION: transform-feedback-output buffer buffer-range POSTPONE: f ; TUPLE: render-set { primitive-mode primitive-mode read-only } - { vertex-array vertex-array read-only } + { vertex-array vertex-array initial: T{ vertex-array-collection } read-only } { uniforms uniform-tuple read-only } { indexes vertex-indexes initial: T{ index-range } read-only } { instances ?integer initial: f read-only } diff --git a/extra/gpu/shaders/shaders.factor b/extra/gpu/shaders/shaders.factor old mode 100644 new mode 100755 index 0401584f7c..025acba896 --- a/extra/gpu/shaders/shaders.factor +++ b/extra/gpu/shaders/shaders.factor @@ -2,9 +2,9 @@ USING: accessors alien alien.c-types alien.data alien.strings arrays assocs byte-arrays classes.mixin classes.parser classes.singleton classes.struct combinators combinators.short-circuit -definitions destructors fry generic.parser gpu gpu.buffers hashtables -images io.encodings.ascii io.files io.pathnames kernel lexer -literals locals math math.parser memoize multiline namespaces +definitions destructors fry generic.parser gpu gpu.buffers gpu.private +gpu.state hashtables images io.encodings.ascii io.files io.pathnames +kernel lexer literals locals math math.parser memoize multiline namespaces opengl opengl.gl opengl.shaders parser quotations sequences specialized-arrays splitting strings tr ui.gadgets.worlds variants vectors vocabs vocabs.loader vocabs.parser words @@ -319,11 +319,18 @@ SYNTAX: VERTEX-FORMAT: SYNTAX: VERTEX-STRUCT: CREATE-CLASS scan-word define-vertex-struct ; -TUPLE: vertex-array < gpu-object +TUPLE: vertex-array-object < gpu-object { program-instance program-instance read-only } { vertex-buffers sequence read-only } ; -M: vertex-array dispose +TUPLE: vertex-array-collection + { vertex-formats sequence read-only } + { program-instance program-instance read-only } ; + +UNION: vertex-array + vertex-array-object vertex-array-collection ; + +M: vertex-array-object dispose [ [ delete-vertex-array ] when* f ] change-handle drop ; : ?>buffer-ptr ( buffer/ptr -- buffer-ptr ) @@ -331,26 +338,73 @@ M: vertex-array dispose : ?>buffer ( buffer/ptr -- buffer ) dup buffer? [ buffer>> ] unless ; inline -:: ( vertex-formats program-instance -- vertex-array ) +buffer-ptr ] dip 2array ] map ; inline + +: (bind-vertex-array) ( vertex-formats program-instance -- ) + '[ _ swap first2 bind-vertex-format ] each ; inline + +: (reset-vertex-array) ( -- ) + GL_MAX_VERTEX_ATTRIBS get-gl-int iota [ glDisableVertexAttribArray ] each ; inline + +:: ( vertex-formats program-instance -- vertex-array ) gen-vertex-array :> handle handle glBindVertexArray - vertex-formats [ program-instance swap first2 [ ?>buffer-ptr ] dip bind-vertex-format ] each - handle program-instance vertex-formats [ first ?>buffer ] map - vertex-array boa window-resource ; inline + vertex-formats normalize-vertex-formats program-instance (bind-vertex-array) -:: ( vertex-buffer program-instance format -- vertex-array ) + handle program-instance vertex-formats [ first ?>buffer ] map + vertex-array-object boa window-resource ; inline + +: ( vertex-formats program-instance -- vertex-array ) + [ normalize-vertex-formats ] dip vertex-array-collection boa ; inline + +:: ( vertex-buffer program-instance format -- vertex-array ) gen-vertex-array :> handle handle glBindVertexArray program-instance vertex-buffer ?>buffer-ptr format bind-vertex-format handle program-instance vertex-buffer ?>buffer 1array - vertex-array boa window-resource ; inline + vertex-array-object boa window-resource ; inline + +: ( vertex-buffer program-instance format -- vertex-array ) + swap [ [ ?>buffer-ptr ] dip 2array 1array ] dip ; inline + +PRIVATE> + +GENERIC: bind-vertex-array ( vertex-array -- ) + +M: vertex-array-object bind-vertex-array + handle>> glBindVertexArray ; inline + +M: vertex-array-collection bind-vertex-array + (reset-vertex-array) + [ vertex-formats>> ] [ program-instance>> ] bi (bind-vertex-array) ; inline + +: ( vertex-formats program-instance -- vertex-array ) + has-vertex-array-objects? get + [ ] + [ ] if ; inline + +: ( vertex-buffer program-instance format -- vertex-array ) + has-vertex-array-objects? get + [ ] + [ ] if ; inline : ( vertex-buffer program-instance -- vertex-array ) dup program>> vertex-formats>> first ; inline -TYPED: vertex-array-buffer ( vertex-array: vertex-array -- vertex-buffer: buffer ) - vertex-buffers>> first ; +GENERIC: vertex-array-buffers ( vertex-array -- buffers ) + +M: vertex-array-object vertex-array-buffers + vertex-buffers>> ; inline + +M: vertex-array-collection vertex-array-buffers + vertex-formats>> [ first buffer>> ] map ; inline + +: vertex-array-buffer ( vertex-array: vertex-array -- vertex-buffer: buffer ) + vertex-array-buffers first ; inline TUPLE: compile-shader-error shader log ; TUPLE: link-program-error program log ; diff --git a/extra/gpu/state/state.factor b/extra/gpu/state/state.factor old mode 100644 new mode 100755 index 3064ed4b82..db76774038 --- a/extra/gpu/state/state.factor +++ b/extra/gpu/state/state.factor @@ -415,8 +415,6 @@ M: mask-state set-gpu-state* [ [ set-gpu-state* ] each ] [ set-gpu-state* ] if ; inline - [ glGetBooleanv ] keep *uchar c-bool> ; : get-gl-int ( enum -- value ) @@ -437,8 +435,6 @@ M: mask-state set-gpu-state* : gl-enabled? ( enum -- ? ) glIsEnabled c-bool> ; -PRIVATE> - TYPED: get-viewport-state ( -- viewport-state: viewport-state ) GL_VIEWPORT get-gl-rect ;