Smallpt: Global Illumination in Swift
Swift C++
Latest commit 709c286 Oct 27, 2014 @jackpal Remove unneeded collect queue.
I added it on speculation that Swift arrays were not threadsafe for
independent element access. But it turned out that the crashes I
was seeing were caused by recursion overflowing thread stacks.

README.md

Small Path Tracer in Swift

This is a straightforward translation of http://www.kevinbeason.com/smallpt/ to Swift.

Example Image

An example output image

Setup

  • Install Xcode 6.1
  • Install a PPM image viewing program such as Toy Viewer

Build and run

xcrun swiftc -O -sdk `xcrun --show-sdk-path --sdk macosx` smallpt.swift
./smallpt
open image.ppm

You can also run the swift file directly. And in either case you can supply the number of samples per pixel. (Default = 4 samples per pixel.):

./smallpt.swift 512 && open image.ppm

Performance

On a MacBook Pro (Retina, 15-inch, Early 2013):

Language time (seconds)
C++ single threaded 5.6 s
Swift single threaded 13.8 s (of which 5.6 s is rendering, 8 s is file output)
Swift GCD 9.8 s (of which 1 s is rendering, 8 s is file output)

Discussion

  • Swift file output seems to be very slow compared to C.
    • partly because it is unbuffered
    • partly because it is converting from Unicode to UTF8