1 ! Factor port of the raytracer benchmark from
2 ! http://www.ffconsultancy.com/languages/ray_tracer/index.html
4 USING: arrays accessors generalizations io io.files io.files.temp
5 io.encodings.binary kernel math math.constants math.functions
6 math.vectors math.vectors.simd math.vectors.simd.cords
7 math.parser make sequences words combinators ;
8 IN: benchmark.raytracer-simd
10 << SYNTAX: no-compile last-word t "no-compile" set-word-prop ; >>
14 ! Normalized { -1 -3 2 }.
23 CONSTANT: oversampling 4
29 : delta ( -- n ) epsilon sqrt ; inline no-compile
31 TUPLE: ray { orig double-4 read-only } { dir double-4 read-only } ;
35 TUPLE: hit { normal double-4 read-only } { lambda float read-only } ;
39 TUPLE: sphere { center double-4 read-only } { radius float read-only } ;
43 : sphere-v ( sphere ray -- v ) [ center>> ] [ orig>> ] bi* v- ; inline no-compile
45 : sphere-b ( v ray -- b ) dir>> vdot ; inline no-compile
47 : sphere-d ( sphere b v -- d ) [ radius>> sq ] [ sq ] [ norm-sq ] tri* - + ; inline no-compile
49 : -+ ( x y -- x-y x+y ) [ - ] [ + ] 2bi ; inline no-compile
51 : sphere-t ( b d -- t )
53 [ 2drop 1/0. ] [ [ [ 0.0 > ] keep ] dip ? ] if ; inline no-compile
55 : sphere-b&v ( sphere ray -- b v )
56 [ sphere-v ] [ nip ] 2bi
57 [ sphere-b ] [ drop ] 2bi ; inline no-compile
59 : ray-sphere ( sphere ray -- t )
60 [ drop ] [ sphere-b&v ] 2bi
61 [ drop ] [ sphere-d ] 3bi
62 dup 0.0 < [ 3drop 1/0. ] [ sqrt sphere-t nip ] if ; inline no-compile
64 : if-ray-sphere ( hit ray sphere quot: ( hit ray sphere l -- hit ) -- hit )
66 [ ] [ swap ray-sphere nip ] [ 2drop lambda>> ] 3tri
68 ] dip [ 3drop ] if ; inline no-compile
70 : sphere-n ( ray sphere l -- n )
71 [ [ orig>> ] [ dir>> ] bi ] [ center>> ] [ ] tri*
72 swap [ v*n ] dip v- v+ ; inline no-compile
74 TUPLE: group < sphere { objs array read-only } ;
76 : <group> ( objs bound -- group )
77 swap [ [ center>> ] [ radius>> ] bi ] dip group boa ; inline no-compile
79 : make-group ( bound quot -- )
80 swap [ { } make ] dip <group> ; inline no-compile
82 : intersect-scene ( hit ray scene -- hit )
84 { [ dup group? ] [ [ drop objs>> [ intersect-scene ] with each ] if-ray-sphere ] }
85 { [ dup sphere? ] [ [ [ sphere-n normalize ] keep <hit> nip ] if-ray-sphere ] }
86 } cond ; inline recursive no-compile
88 CONSTANT: initial-hit T{ hit f double-4{ 0.0 0.0 0.0 0.0 } 1/0. }
90 : initial-intersect ( ray scene -- hit )
91 [ initial-hit ] 2dip intersect-scene ; inline no-compile
93 : ray-o ( ray hit -- o )
94 [ [ orig>> ] [ normal>> delta v*n ] bi* ]
95 [ [ dir>> ] [ lambda>> ] bi* v*n ]
96 2bi v+ v+ ; inline no-compile
98 : sray-intersect ( ray scene hit -- ray )
99 swap [ ray-o light vneg <ray> ] dip initial-intersect ; inline no-compile
101 : ray-g ( hit -- g ) normal>> light vdot ; inline no-compile
103 : cast-ray ( ray scene -- g )
104 2dup initial-intersect dup lambda>> 1/0. = [
107 [ sray-intersect lambda>> 1/0. = ] keep swap
108 [ ray-g neg ] [ drop 0.0 ] if
109 ] if ; inline no-compile
111 : create-center ( c r d -- c2 )
112 [ 3.0 12.0 sqrt / * ] dip n*v v+ ; inline no-compile
116 : create-step ( level c r d -- scene )
117 over [ create-center ] dip 2.0 / [ 1 - ] 2dip create ;
119 CONSTANT: create-offsets
121 double-4{ -1.0 1.0 -1.0 0.0 }
122 double-4{ 1.0 1.0 -1.0 0.0 }
123 double-4{ -1.0 1.0 1.0 0.0 }
124 double-4{ 1.0 1.0 1.0 0.0 }
127 : create-bound ( c r -- sphere ) 3.0 * <sphere> ;
129 : create-group ( level c r -- scene )
132 create-offsets [ create-step , ] 3 nwith each
135 : create ( level c r -- scene )
136 pick 1 = [ <sphere> nip ] [ create-group ] if ;
138 : ss-point ( dx dy -- point )
139 [ oversampling /f ] bi@ 0.0 0.0 double-4-boa ; inline no-compile
141 : ray-pixel ( scene point -- ray-grid )
143 oversampling <iota> [
144 oversampling <iota> [
145 ss-point v+ normalize
146 double-4{ 0.0 0.0 -4.0 0.0 } swap <ray>
149 ] 2with each ; inline no-compile
151 : ray-trace ( scene -- grid )
152 size <iota> <reversed> [
154 [ size 0.5 * - ] bi@ swap size
155 0.0 double-4-boa ray-pixel
159 : pgm-header ( w h -- )
160 "P5\n" % swap # " " % # "\n255\n" % ;
162 : pgm-pixel ( n -- ) 255 * 0.5 + >fixnum , ;
164 : run-raytracer-simd ( -- string )
165 levels double-4{ 0.0 -1.0 0.0 0.0 } 1.0 create ray-trace [
167 [ [ oversampling sq / pgm-pixel ] each ] each
170 : raytracer-simd-benchmark ( -- )
171 run-raytracer-simd "raytracer.pnm" temp-file binary set-file-contents ;
173 MAIN: raytracer-simd-benchmark