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 ;
8 QUALIFIED-WITH: alien.c-types c
10 IN: benchmark.raytracer-simd
14 ! Normalized { -1 -3 2 }.
23 CONSTANT: oversampling 4
29 : delta ( -- n ) epsilon sqrt ; inline
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 GENERIC: intersect-scene ( hit ray scene -- hit )
41 TUPLE: sphere { center double-4 read-only } { radius float read-only } ;
45 : sphere-v ( sphere ray -- v )
46 [ center>> ] [ orig>> ] bi* v- ; inline
48 : sphere-b ( v ray -- b )
51 : sphere-d ( sphere b v -- d )
52 [ radius>> sq ] [ sq ] [ norm-sq ] tri* - + ; inline
54 : -+ ( x y -- x-y x+y )
55 [ - ] [ + ] 2bi ; inline
57 : sphere-t ( b d -- t )
59 [ 2drop 1/0. ] [ [ [ 0.0 > ] keep ] dip ? ] if ; inline
61 : sphere-b&v ( sphere ray -- b v )
62 [ sphere-v ] [ nip ] 2bi
63 [ sphere-b ] [ drop ] 2bi ; inline
65 : ray-sphere ( sphere ray -- t )
66 [ drop ] [ sphere-b&v ] 2bi
67 [ drop ] [ sphere-d ] 3bi
68 dup 0.0 < [ 3drop 1/0. ] [ sqrt sphere-t nip ] if ; inline
70 : if-ray-sphere ( hit ray sphere quot -- hit )
71 #! quot: hit ray sphere l -- hit
73 [ ] [ swap ray-sphere nip ] [ 2drop lambda>> ] 3tri
75 ] dip [ 3drop ] if ; inline
77 : sphere-n ( ray sphere l -- n )
78 [ [ orig>> ] [ dir>> ] bi ] [ center>> ] [ ] tri*
79 swap [ v*n ] dip v- v+ ; inline
81 M: sphere intersect-scene ( hit ray sphere -- hit )
82 [ [ sphere-n normalize ] keep <hit> nip ] if-ray-sphere ;
84 HINTS: M\ sphere intersect-scene { hit ray sphere } ;
86 TUPLE: group < sphere { objs array read-only } ;
88 : <group> ( objs bound -- group )
89 [ center>> ] [ radius>> ] bi rot group boa ; inline
91 : make-group ( bound quot -- )
92 swap [ { } make ] dip <group> ; inline
94 M: group intersect-scene ( hit ray group -- hit )
95 [ drop objs>> [ intersect-scene ] with each ] if-ray-sphere ;
97 HINTS: M\ group intersect-scene { hit ray group } ;
99 CONSTANT: initial-hit T{ hit f double-4{ 0.0 0.0 0.0 0.0 } 1/0. }
101 : initial-intersect ( ray scene -- hit )
102 [ initial-hit ] 2dip intersect-scene ; inline
104 : ray-o ( ray hit -- o )
105 [ [ orig>> ] [ normal>> delta v*n ] bi* ]
106 [ [ dir>> ] [ lambda>> ] bi* v*n ]
109 : sray-intersect ( ray scene hit -- ray )
110 swap [ ray-o light vneg <ray> ] dip initial-intersect ; inline
112 : ray-g ( hit -- g ) normal>> light v. ; inline
114 : cast-ray ( ray scene -- g )
115 2dup initial-intersect dup lambda>> 1/0. = [
118 [ sray-intersect lambda>> 1/0. = ] keep swap
119 [ ray-g neg ] [ drop 0.0 ] if
122 : create-center ( c r d -- c2 )
123 [ 3.0 12.0 sqrt / * ] dip n*v v+ ; inline
125 DEFER: create ( level c r -- scene )
127 : create-step ( level c r d -- scene )
128 over [ create-center ] dip 2.0 / [ 1 - ] 2dip create ;
130 : create-offsets ( quot -- )
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 }
135 double-4{ 1.0 1.0 1.0 0.0 }
138 : create-bound ( c r -- sphere ) 3.0 * <sphere> ;
140 : create-group ( level c r -- scene )
143 [ [ 3dup ] dip create-step , ] create-offsets 3drop
146 : create ( level c r -- scene )
147 pick 1 = [ <sphere> nip ] [ create-group ] if ;
149 : ss-point ( dx dy -- point )
150 [ oversampling /f ] bi@ 0.0 0.0 double-4-boa ;
152 : ss-grid ( -- ss-grid )
153 oversampling [ oversampling [ ss-point ] with map ] map ;
155 : ray-grid ( point ss-grid -- ray-grid )
157 [ v+ normalize double-4{ 0.0 0.0 -4.0 0.0 } swap <ray> ] with map
160 : ray-pixel ( scene point -- n )
161 ss-grid ray-grid [ 0.0 ] 2dip
162 [ [ swap cast-ray + ] with each ] with each ;
164 : pixel-grid ( -- grid )
167 [ size 0.5 * - ] bi@ swap size
172 : pgm-header ( w h -- )
173 "P5\n" % swap # " " % # "\n255\n" % ;
175 : pgm-pixel ( n -- ) 255 * 0.5 + >fixnum , ;
177 : ray-trace ( scene -- pixels )
178 pixel-grid [ [ ray-pixel ] with map ] with map ;
181 levels double-4{ 0.0 -1.0 0.0 0.0 } 1.0 create ray-trace [
183 [ [ oversampling sq / pgm-pixel ] each ] each
186 : raytracer-main ( -- )
187 run "raytracer.pnm" temp-file binary set-file-contents ;