improve gpu.demos.bunny mesh generation performance

db4
Joe Groff 2009-10-22 16:53:40 -05:00
parent 84f203afcc
commit 27100ae094
1 changed files with 25 additions and 22 deletions

View File

@ -3,13 +3,15 @@ USING: accessors alien.c-types arrays classes.struct combinators
combinators.short-circuit game.worlds gpu gpu.buffers combinators.short-circuit game.worlds gpu gpu.buffers
gpu.util.wasd gpu.framebuffers gpu.render gpu.shaders gpu.state gpu.util.wasd gpu.framebuffers gpu.render gpu.shaders gpu.state
gpu.textures gpu.util grouping http.client images images.loader gpu.textures gpu.util grouping http.client images images.loader
io io.encodings.ascii io.files io.files.temp kernel math io io.encodings.ascii io.files io.files.temp kernel locals math
math.matrices math.parser math.vectors method-chains sequences math.matrices math.vectors.simd math.parser math.vectors
splitting threads ui ui.gadgets ui.gadgets.worlds method-chains sequences splitting threads ui ui.gadgets
ui.pixel-formats specialized-arrays specialized-vectors ; ui.gadgets.worlds ui.pixel-formats specialized-arrays
specialized-vectors ;
FROM: alien.c-types => float ; FROM: alien.c-types => float ;
SPECIALIZED-ARRAY: float SPECIALIZED-ARRAY: float
SPECIALIZED-VECTOR: uint SPECIALIZED-VECTOR: uint
SIMD: float
IN: gpu.demos.bunny IN: gpu.demos.bunny
GLSL-SHADER-FILE: bunny-vertex-shader vertex-shader "bunny.v.glsl" GLSL-SHADER-FILE: bunny-vertex-shader vertex-shader "bunny.v.glsl"
@ -52,7 +54,10 @@ VERTEX-FORMAT: bunny-vertex
{ f float-components 1 f } { f float-components 1 f }
{ "normal" float-components 3 f } { "normal" float-components 3 f }
{ f float-components 1 f } ; { f float-components 1 f } ;
VERTEX-STRUCT: bunny-vertex-struct bunny-vertex
STRUCT: bunny-vertex-struct
{ vertex float-4 }
{ normal float-4 } ;
SPECIALIZED-VECTOR: bunny-vertex-struct SPECIALIZED-VECTOR: bunny-vertex-struct
@ -75,42 +80,40 @@ UNIFORM-TUPLE: loading-uniforms
{ "loading-texture" texture-uniform f } ; { "loading-texture" texture-uniform f } ;
: numbers ( str -- seq ) : numbers ( str -- seq )
" " split [ string>number ] map sift ; " " split [ empty? not ] filter [ string>number ] map ; inline
: <bunny-vertex> ( vertex -- struct ) : <bunny-vertex> ( vertex -- struct )
bunny-vertex-struct <struct> bunny-vertex-struct <struct>
swap >float-array >>vertex ; inline swap first3 0.0 float-4-boa >>vertex ; inline
: (parse-bunny-model) ( vs is -- vs is ) : (parse-bunny-model) ( vs is -- vs is )
readln [ [
numbers { numbers {
{ [ dup length 5 = ] [ 3 head <bunny-vertex> pick push ] } { [ dup length 5 = ] [ 3 head <bunny-vertex> pick push ] }
{ [ dup first 3 = ] [ rest over push-all ] } { [ dup first 3 = ] [ rest over push-all ] }
[ drop ] [ drop ]
} cond (parse-bunny-model) } cond
] when* ; ] each-line ; inline
: parse-bunny-model ( -- vertexes indexes ) : parse-bunny-model ( -- vertexes indexes )
100000 <bunny-vertex-struct-vector> 100000 <bunny-vertex-struct-vector>
100000 <uint-vector> 100000 <uint-vector>
(parse-bunny-model) ; (parse-bunny-model) ; inline
: normal ( vertexes -- normal ) :: normal ( a b c -- normal )
[ [ second ] [ first ] bi v- ] c a v-
[ [ third ] [ first ] bi v- ] bi cross b a v- cross normalize ; inline
vneg normalize ; inline
: calc-bunny-normal ( vertexes indexes -- ) :: calc-bunny-normal ( a b c vertexes -- )
swap a b c [ vertexes nth vertex>> ] tri@ normal :> n
[ [ nth vertex>> ] curry { } map-as normal ] a b c [ vertexes nth [ n v+ ] change-normal drop ] tri@ ; inline
[ [ nth [ v+ ] change-normal drop ] curry with each ] 2bi ;
: calc-bunny-normals ( vertexes indexes -- ) : calc-bunny-normals ( vertexes indexes -- )
3 <groups> 3 <sliced-groups> swap
[ calc-bunny-normal ] with each ; [ [ first3 ] dip calc-bunny-normal ] curry each ; inline
: normalize-bunny-normals ( vertexes -- ) : normalize-bunny-normals ( vertexes -- )
[ [ normalize ] change-normal drop ] each ; [ [ normalize ] change-normal drop ] each ; inline
: bunny-data ( filename -- vertexes indexes ) : bunny-data ( filename -- vertexes indexes )
ascii [ parse-bunny-model ] with-file-reader ascii [ parse-bunny-model ] with-file-reader