From 6aa998ad5476637ec93e444872cdc81683e35f4e Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Wed, 10 Feb 2010 13:03:34 -0800 Subject: [PATCH] change GLSL-PROGRAM: syntax to allow for vertex formats to be specified for link-time attribute index assignment with glBindAttribLocation. the transform feedback format now needs to be marked with "feedback-format:" --- extra/gpu/demos/bunny/bunny.factor | 29 ++++++++------- extra/gpu/demos/raytrace/raytrace.factor | 3 +- extra/gpu/shaders/shaders-docs.factor | 8 +++-- extra/gpu/shaders/shaders.factor | 46 +++++++++++++++++------- 4 files changed, 58 insertions(+), 28 deletions(-) diff --git a/extra/gpu/demos/bunny/bunny.factor b/extra/gpu/demos/bunny/bunny.factor index b2f619c3d1..332a33ff1e 100644 --- a/extra/gpu/demos/bunny/bunny.factor +++ b/extra/gpu/demos/bunny/bunny.factor @@ -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 diff --git a/extra/gpu/demos/raytrace/raytrace.factor b/extra/gpu/demos/raytrace/raytrace.factor index 634d7a2fd9..2ad6c82d7c 100644 --- a/extra/gpu/demos/raytrace/raytrace.factor +++ b/extra/gpu/demos/raytrace/raytrace.factor @@ -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 } diff --git a/extra/gpu/shaders/shaders-docs.factor b/extra/gpu/shaders/shaders-docs.factor index ebc56afd69..4e5e8a178c 100644 --- a/extra/gpu/shaders/shaders-docs.factor +++ b/extra/gpu/shaders/shaders-docs.factor @@ -24,9 +24,13 @@ HELP: } { $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 } ", 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 } ", 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: diff --git a/extra/gpu/shaders/shaders.factor b/extra/gpu/shaders/shaders.factor index 890bb06a1f..e68586331d 100644 --- a/extra/gpu/shaders/shaders.factor +++ b/extra/gpu/shaders/shaders.factor @@ -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 + 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: [ 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: 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