]> gitweb.factorcode.org Git - factor.git/commitdiff
change GLSL-PROGRAM: syntax to allow for vertex formats to be specified for link...
authorJoe Groff <arcata@gmail.com>
Wed, 10 Feb 2010 21:03:34 +0000 (13:03 -0800)
committerJoe Groff <arcata@gmail.com>
Wed, 10 Feb 2010 21:03:44 +0000 (13:03 -0800)
extra/gpu/demos/bunny/bunny.factor
extra/gpu/demos/raytrace/raytrace.factor
extra/gpu/shaders/shaders-docs.factor
extra/gpu/shaders/shaders.factor

index b2f619c3d1cd287d877475d5a2b93b188d4c61b3..332a33ff1e425cfcd3426b748a6b2691689f248e 100644 (file)
@@ -13,20 +13,33 @@ SPECIALIZED-ARRAY: float
 SPECIALIZED-VECTOR: uint
 IN: gpu.demos.bunny
 
+VERTEX-FORMAT: bunny-vertex
+    { "vertex" float-components 3 f }
+    { f        float-components 1 f }
+    { "normal" float-components 3 f }
+    { f        float-components 1 f } ;
+
+STRUCT: bunny-vertex-struct
+    { vertex float-4 }
+    { normal float-4 } ;
+
 GLSL-SHADER-FILE: bunny-vertex-shader vertex-shader "bunny.v.glsl"
 GLSL-SHADER-FILE: bunny-fragment-shader fragment-shader "bunny.f.glsl"
 GLSL-PROGRAM: bunny-program
-    bunny-vertex-shader bunny-fragment-shader ;
+    bunny-vertex-shader bunny-fragment-shader
+    bunny-vertex ;
 
 GLSL-SHADER-FILE: window-vertex-shader vertex-shader "window.v.glsl"
 
 GLSL-SHADER-FILE: sobel-fragment-shader fragment-shader "sobel.f.glsl"
 GLSL-PROGRAM: sobel-program
-    window-vertex-shader sobel-fragment-shader ;
+    window-vertex-shader sobel-fragment-shader
+    window-vertex ;
 
 GLSL-SHADER-FILE: loading-fragment-shader fragment-shader "loading.f.glsl"
 GLSL-PROGRAM: loading-program
-    window-vertex-shader loading-fragment-shader ;
+    window-vertex-shader loading-fragment-shader
+    window-vertex ;
 
 TUPLE: bunny-state
     vertexes
@@ -48,16 +61,6 @@ TUPLE: loading-state
 TUPLE: bunny-world < wasd-world
     bunny sobel loading ;
 
-VERTEX-FORMAT: bunny-vertex
-    { "vertex" float-components 3 f }
-    { f        float-components 1 f }
-    { "normal" float-components 3 f }
-    { f        float-components 1 f } ;
-
-STRUCT: bunny-vertex-struct
-    { vertex float-4 }
-    { normal float-4 } ;
-
 SPECIALIZED-VECTOR: bunny-vertex-struct
 
 UNIFORM-TUPLE: bunny-uniforms < mvp-uniforms
index 634d7a2fd97cc77446284ee5ccd29bf63565f265..2ad6c82d7cd3906991b7bdccf8055e8ec26a1321 100644 (file)
@@ -9,7 +9,8 @@ IN: gpu.demos.raytrace
 GLSL-SHADER-FILE: raytrace-vertex-shader vertex-shader "raytrace.v.glsl"
 GLSL-SHADER-FILE: raytrace-fragment-shader fragment-shader "raytrace.f.glsl"
 GLSL-PROGRAM: raytrace-program
-    raytrace-vertex-shader raytrace-fragment-shader ;
+    raytrace-vertex-shader raytrace-fragment-shader
+    window-vertex ;
 
 UNIFORM-TUPLE: sphere-uniforms
     { "center" vec3-uniform  f }
index ebc56afd693b364365aba926c66d9f7e28b6d3ac..4e5e8a178c9a1662fc476e1c1abb749f42e42b91 100644 (file)
@@ -24,9 +24,13 @@ HELP: <vertex-array>
 }
 { $description "Creates a new " { $link vertex-array } " to feed data to " { $snippet "program-instance" } " from the set of " { $link buffer } "s specified in " { $snippet "vertex-formats" } "." } ;
 
+HELP: feedback-format:
+{ $syntax "feedback-format: vertex-format" }
+{ $description "When used as part of a " { $link GLSL-PROGRAM: } " definition, this syntax specifies the " { $link vertex-format } " in which transform feedback output will be generated." } ;
+
 HELP: GLSL-PROGRAM:
-{ $syntax "GLSL-PROGRAM: program-name shader shader ... shader [vertex-format] ;" }
-{ $description "Defines a new " { $link program } " named " { $snippet "program-name" } ". When the program is instantiated with " { $link <program-instance> } ", it will link together instances of all of the specified " { $link shader } "s to create the program instance. A single " { $link vertex-array } " may optionally be specified; if the program is used to collect transform feedback, this format will be used for the output." }
+{ $syntax "GLSL-PROGRAM: program-name shader shader ... [vertex-format vertex-format ...] [feedback-format: vertex-format] ;" }
+{ $description "Defines a new " { $link program } " named " { $snippet "program-name" } ". When the program is instantiated with " { $link <program-instance> } ", it will link together instances of all of the specified " { $link shader } "s to create the program instance. If any " { $link vertex-format } "s are specified, their attributes will be pre-assigned attribute indexes at link time, to ensure that their indexes remain constant if the program is refreshed with " { $link refresh-program } ". A trasform feedback vertex format may optionally be specified with " { $link feedback-format: } "; if the program is used to collect transform feedback, the given vertex format will be used for the output." }
 { $notes "Transform feedback requires OpenGL 3.0 or one of the " { $snippet "GL_EXT_transform_feedback" } " or " { $snippet "GL_ARB_transform_feedback" } " extensions." } ;
 
 HELP: GLSL-SHADER-FILE:
index 890bb06a1fedc6c58365c778ba84e1233f949f96..e68586331d9c198bc9ecc91d4d718a4f6e7eeb4a 100644 (file)
@@ -2,7 +2,7 @@
 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 generic.parser gpu gpu.buffers hashtables
+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
 opengl opengl.gl opengl.shaders parser quotations sequences
@@ -45,6 +45,7 @@ TUPLE: program
     { filename read-only }
     { line integer read-only }
     { shaders array read-only }
+    { vertex-formats array read-only }
     { feedback-format ?vertex-format read-only }
     { instances hashtable read-only } ;
 
@@ -65,6 +66,9 @@ MEMO: attribute-index ( program-instance attribute-name -- index )
 MEMO: output-index ( program-instance output-name -- index )
     [ handle>> ] dip glGetFragDataLocation ;
 
+: vertex-format-attributes ( vertex-format -- attributes )
+    "vertex-format-attributes" word-prop ; inline    
+
 <PRIVATE
 
 TR: hyphens>underscores "-" "_" ;
@@ -200,6 +204,10 @@ GENERIC: link-feedback-format ( program-handle format -- )
 M: f link-feedback-format
     2drop ;
 
+: link-vertex-formats ( program-handle formats -- )
+    [ vertex-format-attributes [ name>> ] map sift ] map concat
+    swap '[ [ _ ] 2dip swap glBindAttribLocation ] each-index ; 
+
 GENERIC: (verify-feedback-format) ( program-instance format -- )
 
 M: f (verify-feedback-format)
@@ -305,7 +313,7 @@ SYNTAX: VERTEX-FORMAT:
     define-vertex-format ;
 
 : define-vertex-struct ( class vertex-format -- )
-    "vertex-format-attributes" word-prop [ vertex-attribute>struct-slot ] map
+    vertex-format-attributes [ vertex-attribute>struct-slot ] map
     define-struct-class ;
 
 SYNTAX: VERTEX-STRUCT:
@@ -360,8 +368,11 @@ DEFER: <shader-instance>
     [ compile-shader-error ] if ;
 
 : (link-program) ( program shader-instances -- program-instance )
-    [ [ handle>> ] map ] curry
-    [ feedback-format>> [ link-feedback-format ] curry ] bi (gl-program)
+    '[ _ [ handle>> ] map ]
+    [
+        [ vertex-formats>> ] [ feedback-format>> ] bi
+        '[ [ _ link-vertex-formats ] [ _ link-feedback-format ] bi ]
+    ] bi (gl-program)
     dup gl-program-ok?  [
         [ swap world get \ program-instance boa |dispose dup verify-feedback-format ]
         with-destructors window-resource
@@ -400,16 +411,27 @@ DEFER: <shader-instance>
     world get over instances>> at*
     [ nip ] [ drop link-program ] if ;
 
-: shaders-and-feedback-format ( words -- shaders feedback-format )
-    [ vertex-format? ] partition swap
-    [ [ def>> first ] map ] [
-        dup length 1 <=
-        [ [ f ] [ first ] if-empty ]
-        [ too-many-feedback-formats-error ] if
-    ] bi* ;
+TUPLE: feedback-format
+    { vertex-format ?vertex-format read-only } ;
+
+: validate-feedback-format ( sequence -- vertex-format/f )
+    dup length 1 <=
+    [ [ f ] [ first vertex-format>> ] if-empty ]
+    [ too-many-feedback-formats-error ] if ;
+
+: ?shader ( object -- shader/f )
+    dup word? [ def>> first dup shader? [ drop f ] unless ] [ drop f ] if ;
+
+: shaders-and-formats ( words -- shaders vertex-formats feedback-format )
+    [ [ ?shader ] map sift ]
+    [ [ vertex-format? ] filter ]
+    [ [ feedback-format? ] filter validate-feedback-format ] tri ;
 
 PRIVATE>
 
+SYNTAX: feedback-format:
+    scan-object feedback-format boa suffix! ;
+
 TYPED:: refresh-program ( program: program -- )
     program shaders>> [ refresh-shader-source ] each
     program instances>> [| world old-instance |
@@ -475,7 +497,7 @@ SYNTAX: GLSL-PROGRAM:
     dup old-instances [
         f
         lexer get line>>
-        \ ; parse-until >array shaders-and-feedback-format
+        \ ; parse-until >array shaders-and-formats
     ] dip
     program boa
     over reset-generic