! Factor port of the raytracer benchmark from
! http://www.ffconsultancy.com/languages/ray_tracer/index.html
-USING: arrays accessors io io.files io.files.temp
+USING: arrays accessors generalizations io.files io.files.temp
io.encodings.binary kernel math math.constants math.functions
-math.vectors math.vectors.simd math.vectors.simd.cords
-math.parser make sequences words combinators ;
+math.vectors math.vectors.simd.cords math.parser make sequences
+words combinators ;
IN: benchmark.raytracer-simd
-<< SYNTAX: no-compile word t "no-compile" set-word-prop ; >>
+<< SYNTAX: no-compile last-word t "no-compile" set-word-prop ; >>
! parameters
: sphere-v ( sphere ray -- v ) [ center>> ] [ orig>> ] bi* v- ; inline no-compile
-: sphere-b ( v ray -- b ) dir>> v. ; inline no-compile
+: sphere-b ( v ray -- b ) dir>> vdot ; inline no-compile
: sphere-d ( sphere b v -- d ) [ radius>> sq ] [ sq ] [ norm-sq ] tri* - + ; inline no-compile
: sray-intersect ( ray scene hit -- ray )
swap [ ray-o light vneg <ray> ] dip initial-intersect ; inline no-compile
-: ray-g ( hit -- g ) normal>> light v. ; inline no-compile
+: ray-g ( hit -- g ) normal>> light vdot ; inline no-compile
: cast-ray ( ray scene -- g )
2dup initial-intersect dup lambda>> 1/0. = [
: create-center ( c r d -- c2 )
[ 3.0 12.0 sqrt / * ] dip n*v v+ ; inline no-compile
-DEFER: create ( level c r -- scene )
+DEFER: create
: create-step ( level c r d -- scene )
over [ create-center ] dip 2.0 / [ 1 - ] 2dip create ;
: create-group ( level c r -- scene )
2dup create-bound [
2dup <sphere> ,
- create-offsets [ create-step , ] with with with each
+ create-offsets [ create-step , ] 3 nwith each
] make-group ;
: create ( level c r -- scene )
: ray-pixel ( scene point -- ray-grid )
[ 0.0 ] 2dip
- oversampling iota [
- oversampling iota [
+ oversampling <iota> [
+ oversampling <iota> [
ss-point v+ normalize
double-4{ 0.0 0.0 -4.0 0.0 } swap <ray>
swap cast-ray +
- ] with with with each
- ] with with each ; inline no-compile
+ ] 3 nwith each
+ ] 2with each ; inline no-compile
: ray-trace ( scene -- grid )
- size iota <reversed> [
- size iota [
+ size <iota> <reversed> [
+ size <iota> [
[ size 0.5 * - ] bi@ swap size
0.0 double-4-boa ray-pixel
- ] with with map
+ ] 2with map
] with map ;
: pgm-header ( w h -- )
: pgm-pixel ( n -- ) 255 * 0.5 + >fixnum , ;
-: run ( -- string )
+: run-raytracer-simd ( -- string )
levels double-4{ 0.0 -1.0 0.0 0.0 } 1.0 create ray-trace [
size size pgm-header
[ [ oversampling sq / pgm-pixel ] each ] each
] B{ } make ;
-: raytracer-main ( -- )
- run "raytracer.pnm" temp-file binary set-file-contents ;
+: raytracer-simd-benchmark ( -- )
+ run-raytracer-simd "raytracer.pnm" temp-file binary set-file-contents ;
-MAIN: raytracer-main
+MAIN: raytracer-simd-benchmark