From ce11431fdb10f7c8619104da2ff085226d2346bd Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Wed, 10 Feb 2010 15:27:57 -0800 Subject: [PATCH] gpu.shaders: change around vertex-array construction a bit. rename to and buffer>vertex-array to . add new that takes a single buffer and reads it with the program's linked vertex-format --- extra/gpu/demos/bunny/bunny.factor | 6 ++--- extra/gpu/shaders/shaders-docs.factor | 20 ++++++++++----- extra/gpu/shaders/shaders.factor | 35 ++++++++++++++++---------- extra/gpu/util/util.factor | 2 +- extra/model-viewer/model-viewer.factor | 2 +- 5 files changed, 41 insertions(+), 24 deletions(-) diff --git a/extra/gpu/demos/bunny/bunny.factor b/extra/gpu/demos/bunny/bunny.factor index 332a33ff1e..987d3d1507 100644 --- a/extra/gpu/demos/bunny/bunny.factor +++ b/extra/gpu/demos/bunny/bunny.factor @@ -163,7 +163,7 @@ CONSTANT: bunny-model-url "http://factorcode.org/bun_zipper.ply" : fill-bunny-state ( bunny-state -- ) dup [ vertexes>> ] [ indexes>> ] bi - [ bunny-program bunny-vertex buffer>vertex-array >>vertex-array ] + [ bunny-program >>vertex-array ] [ 0 ] [ uint-indexes >>index-elements ] tri* drop ; @@ -180,7 +180,7 @@ CONSTANT: bunny-model-url "http://factorcode.org/bun_zipper.ply" : ( window-vertex-buffer -- sobel-state ) sobel-state new - swap sobel-program window-vertex buffer>vertex-array >>vertex-array + swap sobel-program >>vertex-array RGBA half-components T{ texture-parameters { wrap clamp-texcoord-to-edge } @@ -207,7 +207,7 @@ CONSTANT: bunny-model-url "http://factorcode.org/bun_zipper.ply" : ( window-vertex-buffer -- loading-state ) loading-state new swap - loading-program window-vertex buffer>vertex-array >>vertex-array + loading-program >>vertex-array RGBA ubyte-components T{ texture-parameters { wrap clamp-texcoord-to-edge } diff --git a/extra/gpu/shaders/shaders-docs.factor b/extra/gpu/shaders/shaders-docs.factor index fde011dc12..fa3fbd6aa4 100644 --- a/extra/gpu/shaders/shaders-docs.factor +++ b/extra/gpu/shaders/shaders-docs.factor @@ -19,10 +19,11 @@ HELP: HELP: { $values - { "program-instance" program-instance } { "vertex-formats" "a list of " { $link buffer-ptr } "/" { $link vertex-format } " pairs" } + { "vertex-formats" "a list of " { $link buffer-ptr } "/" { $link vertex-format } " pairs" } + { "program-instance" program-instance } { "vertex-array" 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" } "." } ; +{ $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" } ". The first element of each pair in " { $snippet "vertex-formats" } " can be either a " { $link buffer-ptr } " or a " { $link buffer } "; in the latter case, vertex data in the associated format is read from the beginning of the buffer." } ; HELP: feedback-format: { $syntax "feedback-format: vertex-format" } @@ -66,14 +67,21 @@ HELP: attribute-index } { $description "Returns the numeric index of the vertex attribute named " { $snippet "attribute-name" } " in " { $snippet "program-instance" } "." } ; -HELP: +HELP: { $values - { "vertex-buffer" buffer } { "program-instance" program-instance } { "format" vertex-format } + { "vertex-buffer" "a " { $link buffer } " or " { $link buffer-ptr } } { "program-instance" program-instance } { "vertex-array" vertex-array } } -{ $description "Creates a new " { $link vertex-array } " from the entire contents of a single " { $link buffer } " in a single " { $link vertex-format } " for use with " { $snippet "program-instance" } "." } ; +{ $description "Creates a new " { $link vertex-array } " from the entire contents of a single " { $link buffer } " for use with a " { $link program-instance } ". The data in " { $snippet "buffer" } " is taken in the first " { $link vertex-format } " specified in the program instance's originating " { $link POSTPONE: GLSL-PROGRAM: } " definition. If the program has no associated vertex formats, an error will be thrown. To specify a different vertex format, use " { $link } "." } ; -{ vertex-array } related-words +HELP: +{ $values + { "vertex-buffer" "a " { $link buffer } " or " { $link buffer-ptr } } { "program-instance" program-instance } { "format" vertex-format } + { "vertex-array" vertex-array } +} +{ $description "Creates a new " { $link vertex-array } " from the entire contents of a single " { $link buffer } " for use with a " { $link program-instance } ". The data in " { $snippet "buffer" } " is taken in the specified " { $link vertex-format } "." } ; + +{ vertex-array } related-words HELP: compile-shader-error { $class-description "An error compiling the source for a " { $link shader } "." diff --git a/extra/gpu/shaders/shaders.factor b/extra/gpu/shaders/shaders.factor index e68586331d..0401584f7c 100644 --- a/extra/gpu/shaders/shaders.factor +++ b/extra/gpu/shaders/shaders.factor @@ -326,19 +326,28 @@ TUPLE: vertex-array < gpu-object M: vertex-array dispose [ [ delete-vertex-array ] when* f ] change-handle drop ; -: ( program-instance vertex-formats -- vertex-array ) - gen-vertex-array - [ glBindVertexArray [ first2 bind-vertex-format ] with each ] - [ -rot [ first buffer>> ] map vertex-array boa ] 3bi - window-resource ; inline +: ?>buffer-ptr ( buffer/ptr -- buffer-ptr ) + dup buffer-ptr? [ 0 ] unless ; inline +: ?>buffer ( buffer/ptr -- buffer ) + dup buffer? [ buffer>> ] unless ; inline -TYPED: buffer>vertex-array ( vertex-buffer: buffer - program-instance: program-instance - format: vertex-format - -- - vertex-array: vertex-array ) - [ swap ] dip - [ 0 ] dip 2array 1array ; 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-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-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 ; @@ -424,7 +433,7 @@ TUPLE: feedback-format : shaders-and-formats ( words -- shaders vertex-formats feedback-format ) [ [ ?shader ] map sift ] - [ [ vertex-format? ] filter ] + [ [ vertex-format-attributes ] filter ] [ [ feedback-format? ] filter validate-feedback-format ] tri ; PRIVATE> diff --git a/extra/gpu/util/util.factor b/extra/gpu/util/util.factor index ccabd3d308..d33fcf8c09 100644 --- a/extra/gpu/util/util.factor +++ b/extra/gpu/util/util.factor @@ -62,4 +62,4 @@ CONSTANT: window-vertexes byte-array>buffer ; inline : ( program-instance -- vertex-array ) - [ ] dip window-vertex buffer>vertex-array ; inline + [ ] dip window-vertex ; inline diff --git a/extra/model-viewer/model-viewer.factor b/extra/model-viewer/model-viewer.factor index 22a80a136e..061ce07d1e 100644 --- a/extra/model-viewer/model-viewer.factor +++ b/extra/model-viewer/model-viewer.factor @@ -130,7 +130,7 @@ TUPLE: vbo [ [ [ vertex-buffer>> obj-program ] - [ vertex-format>> ] bi buffer>vertex-array + [ vertex-format>> ] bi ] map >>vertex-arrays drop ] [