Doing Raytracer in Crystal

Latest code you can parametrize by setting env vars. If you want the best possible quality, this is like a 20k image that was sampled to 10k:

> CRYSTAL_WORKERS=16 AA_SAMPLES=4 SIZE=10000 ADAPTIVE_AA=0 ./bin/raytracer
Completed in 16849.322 ms

> file crystal-raytracer.png
crystal-raytracer.png: PNG image data, 10000 x 10000, 8-bit/color RGB, non-interlaced

This is a small part of that image on 1:1 scale

When I make time, I’ll get latest code and try those settings.

1 Like

I tried this and it’s roughly the same speed, so I kept it cause it’s cleaner.

Doing some absurd microoptimizations I may have squeezed another 5% speedup (which is not noticeable at all)

Ok, I made some time and installed your new code and ran it.

First let me thank you for your excellent, easy to follow, instructions to do it.

Running each program on my older Lenovo, 5.1 GHz, 8C|16T laptop gives these rough results:

These are just eyeballed ballpark numbers from a few runs of each:

raytracer-500x500: ~9.1ms - 9.3ms
raytracer-1kx1k: ~34.4ms - 36.1ms
benchmark-500x500: ~9.2ms - 9.4ms
animated: ~84.5 FPS, ~10.ms - 12ms 

It’s curious that the raytracer 500x500 time is almost 3x slower than before. :thinking:

But I enjoyed the animations. So little code can produce such a nice effect.

This again was done with crystal-1.19.0-dev-1, with the final 1.19 supposed to be released today. It hasn’t been as I write this.

I’ll run them on my newer 5.4GHz, 16C|32T laptop later when I’m home.

Very nice work!

Oops, I missed you already tried benchmark.

You should not need to run it more than once, it uses benchmark.ips to run a while and average results.

Got 1.19 today; latest benchmark.cr code. Changed all Time.monotonic to Time.instant to get rid of deprecation warnings; compiled all three. Found cause of prior slower runs was spelling error. Did > CRYSTAL_WORKER… vs > CRYSTAL_WORKERS.. Hate when I do that.

Times now are what I expected, on Lenovo AMD 8845HS (8C|16T) @ 5.14GHz.

raytracer-500x500: ~3.4ms - 4.8ms
raytracer-1kx1k: ~13.4ms - 15.6.1ms
animated: ~168.5 FPS, ~5.5.ms - 6.8ms
=======================================================
➜ CRYSTAL_WORKERS=16 ./bin/benchmark                                               
Benchmarking 500x500 render...
Workers: 16

render 265.95  (  3.76ms) (± 5.80%)  4.31kB/op  fastest
========================================================

➜  raytracer_new CRYSTAL_WORKERS=16 ./bin/benchmark                                                        
Benchmarking 1000x1000 render...
Workers: 16

render  70.10  ( 14.26ms) (± 2.91%)  4.28kB/op  fastest
=========================================================

➜  raytracer_new CRYSTAL_WORKERS=16 ./bin/benchmark                                                           
Benchmarking 5000x5000 render...
Workers: 16

render   2.90  (344.89ms) (± 1.11%)  4.14kB/op  fastest

2 Likes

For completeness, here are times on my AMD 7945HX (16C|32T) @5.4GHz laptop.

animated: ~257.5 FPS, ~3.4ms - 4.2ms
=======================================================

➜  raytracer_new CRYSTAL_WORKERS=32 ./bin/benchmark
Benchmarking 500x500 render...
Workers: 32

render 435.44  (  2.30ms) (±10.96%)  7.99kB/op  fastest
=========================================================                                                                 

➜  raytracer_new CRYSTAL_WORKERS=32 ./bin/benchmark                                                        
Benchmarking 1000x1000 render...
Workers: 32

render 121.73  (  8.21ms) (± 6.55%)  7.81kB/op  fastest
==========================================================

➜  raytracer_new CRYSTAL_WORKERS=32 ./bin/benchmark
Benchmarking 5000x5000 render...
Workers: 32

render   5.26  (190.15ms) (± 0.76%)  10.5kB/op  fastest
➜  raytracer_new