1 ! Factor port of the raytracer benchmark from
2 ! http://www.ffconsultancy.com/free/ray_tracer/languages.html
4 USING: arrays accessors io io.files io.files.temp
5 io.encodings.binary kernel math math.constants math.functions
6 math.vectors math.vectors.simd math.parser make sequences
7 sequences.private words hints classes.struct ;
9 IN: benchmark.raytracer-simd
13 ! Normalized { -1 -3 2 }.
22 CONSTANT: oversampling 4
28 : delta ( -- n ) epsilon sqrt ; inline
30 TUPLE: ray { orig double-4 read-only } { dir double-4 read-only } ;
34 TUPLE: hit { normal double-4 read-only } { lambda float read-only } ;
38 GENERIC: intersect-scene ( hit ray scene -- hit )
40 TUPLE: sphere { center double-4 read-only } { radius float read-only } ;
44 : sphere-v ( sphere ray -- v )
45 [ center>> ] [ orig>> ] bi* v- ; inline
47 : sphere-b ( v ray -- b )
50 : sphere-d ( sphere b v -- d )
51 [ radius>> sq ] [ sq ] [ norm-sq ] tri* - + ; inline
53 : -+ ( x y -- x-y x+y )
54 [ - ] [ + ] 2bi ; inline
56 : sphere-t ( b d -- t )
58 [ 2drop 1/0. ] [ [ [ 0.0 > ] keep ] dip ? ] if ; inline
60 : sphere-b&v ( sphere ray -- b v )
61 [ sphere-v ] [ nip ] 2bi
62 [ sphere-b ] [ drop ] 2bi ; inline
64 : ray-sphere ( sphere ray -- t )
65 [ drop ] [ sphere-b&v ] 2bi
66 [ drop ] [ sphere-d ] 3bi
67 dup 0.0 < [ 3drop 1/0. ] [ sqrt sphere-t nip ] if ; inline
69 : if-ray-sphere ( hit ray sphere quot -- hit )
70 #! quot: hit ray sphere l -- hit
72 [ ] [ swap ray-sphere nip ] [ 2drop lambda>> ] 3tri
74 ] dip [ 3drop ] if ; inline
76 : sphere-n ( ray sphere l -- n )
77 [ [ orig>> ] [ dir>> ] bi ] [ center>> ] [ ] tri*
78 swap [ v*n ] dip v- v+ ; inline
80 M: sphere intersect-scene ( hit ray sphere -- hit )
81 [ [ sphere-n normalize ] keep <hit> nip ] if-ray-sphere ;
83 HINTS: M\ sphere intersect-scene { hit ray sphere } ;
85 TUPLE: group < sphere { objs array read-only } ;
87 : <group> ( objs bound -- group )
88 [ center>> ] [ radius>> ] bi rot group boa ; inline
90 : make-group ( bound quot -- )
91 swap [ { } make ] dip <group> ; inline
93 M: group intersect-scene ( hit ray group -- hit )
94 [ drop objs>> [ intersect-scene ] with each ] if-ray-sphere ;
96 HINTS: M\ group intersect-scene { hit ray group } ;
98 CONSTANT: initial-hit T{ hit f double-4{ 0.0 0.0 0.0 0.0 } 1/0. }
100 : initial-intersect ( ray scene -- hit )
101 [ initial-hit ] 2dip intersect-scene ; inline
103 : ray-o ( ray hit -- o )
104 [ [ orig>> ] [ normal>> delta v*n ] bi* ]
105 [ [ dir>> ] [ lambda>> ] bi* v*n ]
108 : sray-intersect ( ray scene hit -- ray )
109 swap [ ray-o light vneg <ray> ] dip initial-intersect ; inline
111 : ray-g ( hit -- g ) normal>> light v. ; inline
113 : cast-ray ( ray scene -- g )
114 2dup initial-intersect dup lambda>> 1/0. = [
117 [ sray-intersect lambda>> 1/0. = ] keep swap
118 [ ray-g neg ] [ drop 0.0 ] if
121 : create-center ( c r d -- c2 )
122 [ 3.0 12.0 sqrt / * ] dip n*v v+ ; inline
124 DEFER: create ( level c r -- scene )
126 : create-step ( level c r d -- scene )
127 over [ create-center ] dip 2.0 / [ 1 - ] 2dip create ;
129 : create-offsets ( quot -- )
131 double-4{ -1.0 1.0 -1.0 0.0 }
132 double-4{ 1.0 1.0 -1.0 0.0 }
133 double-4{ -1.0 1.0 1.0 0.0 }
134 double-4{ 1.0 1.0 1.0 0.0 }
137 : create-bound ( c r -- sphere ) 3.0 * <sphere> ;
139 : create-group ( level c r -- scene )
142 [ [ 3dup ] dip create-step , ] create-offsets 3drop
145 : create ( level c r -- scene )
146 pick 1 = [ <sphere> nip ] [ create-group ] if ;
148 : ss-point ( dx dy -- point )
149 [ oversampling /f ] bi@ 0.0 0.0 double-4-boa ;
151 : ss-grid ( -- ss-grid )
152 oversampling [ oversampling [ ss-point ] with map ] map ;
154 : ray-grid ( point ss-grid -- ray-grid )
156 [ v+ normalize double-4{ 0.0 0.0 -4.0 0.0 } swap <ray> ] with map
159 : ray-pixel ( scene point -- n )
160 ss-grid ray-grid [ 0.0 ] 2dip
161 [ [ swap cast-ray + ] with each ] with each ;
163 : pixel-grid ( -- grid )
166 [ size 0.5 * - ] bi@ swap size
171 : pgm-header ( w h -- )
172 "P5\n" % swap # " " % # "\n255\n" % ;
174 : pgm-pixel ( n -- ) 255 * 0.5 + >fixnum , ;
176 : ray-trace ( scene -- pixels )
177 pixel-grid [ [ ray-pixel ] with map ] with map ;
180 levels double-4{ 0.0 -1.0 0.0 0.0 } 1.0 create ray-trace [
182 [ [ oversampling sq / pgm-pixel ] each ] each
185 : raytracer-main ( -- )
186 run "raytracer.pnm" temp-file binary set-file-contents ;