CMake Ray Tracer
A simple ray tracer written in pure CMake. Inspired by raytracer.hpp. More information can be found at my blog.
Usage
The ray tracer writes its output to stderr
, so you can use it with:
cmake -Dimage_width=64 -Dimage_height=64 -Dnum_procs=4 -P CMakeLists.txt 2> image.ppm
Which writes the output to image.ppm
. Then use an image viewer capable of opening PPM files (or this) to view.
Alternatively, the ray tracer can directly write PNG files courtesy of @benmcmorran (PowerShell required for now). Use the use_png
variable to set the name of the PNG file without the extension. The example below will write output to render.png
.
cmake -Dimage_width=64 -Dimage_height=64 -Dnum_procs=4 -Duse_png=render -P CMakeLists.txt
num_procs
controls the number of worker processes spawned. It is recommended to set this to a value no greater than the number of cores in your CPU, for maximum performance.
For now, to keep the code simple, you are required to keep image_width
, image_height
and num_procs
as powers of 2, otherwise the image may not be fully formed. If not specified, these arguments default to the values shown above.
Performance
Using cmake 3.19.2 on Linux 5.4 on a i5-10210U (4 cores, 8 threads), running this command:
for X in 1 2 4 8 16 32 64 128 256 512 ; do echo SIZE $X ; time cmake -Dimage_width=$X -Dimage_height=$X -Dnum_procs=8 -P CMakeLists.txt 2> image_size_${X}.ppm ; done
PowerShell alternative:
1, 2, 4, 8, 16, 32, 64, 128, 256, 512 | foreach {Write-Host $_; (Measure-Command { cmake "-Dimage_width=$_" "-Dimage_height=$_" "-Dnum_procs=$Env:NUMBER_OF_PROCESSORS" -P CMakeLists.txt 2> image_size_$_.ppm }).TotalSeconds}
Figures reported by time
command below (reformatted). As usual, the "real" time is the wall clock time. The others are summed on all processors.
size | real | user | sys |
---|---|---|---|
1 | 0,054s | 0,130s | 0,051s |
2 | 0,035s | 0,098s | 0,041s |
4 | 0,077s | 0,274s | 0,018s |
8 | 0,106s | 0,460s | 0,023s |
16 | 0,367s | 1,871s | 0,059s |
32 | 1,296s | 7,617s | 0,132s |
64 | 5,175s | 29,455s | 0,356s |
128 | 21,093s | 2m06,299s | 1,566s |
256 | 1m33,395s | 9m21,875s | 5,999s |
512 | 7m23,094s | 45m36,327s | 32,588s |
Contributing
All contributions (issue, PRs) are welcome. This project is licensed under the MIT license.