RayTracer

2024

Jai, Rendering

This project was a novelty to me. Not only I was doing something completely new and awesome, I also had the chance to get to use Jai: A low-level programming language (that still is in Beta) that aims to be a better language for programming video-games than C++. Following the great series Ray Tracing in One Weekend I was able to quickly get a grasp of what was needed to create my scene with a background and an object in it.

At the end of the project, I was able to implement Diffuse, Dielectric, Emissive and Metallic materials that interact with the environment in a physically correct manner. I have implemented a basic BVH tree algorithm to speed up the rendering process, implemented spheres, planes and cubes to fill up our scene and played around with random texture generation with Perlin noise.

Optimizations

Besides the BVH tree optimization mentioned above, there were other two big optimizations done compared to the architecture on those books: Multi-threading and removal of OOP.

The first one is fairly straight-forward, the authors used a single-threaded architecture which I followed, until the final render took an excruciating 3 hours and 30 minutes to render. Nowadays, we are blessed with multi-core CPU’s and not taking advantage of such wonderful hardware might be considered a sin by some. By quickly changing to a multi-threaded architecture (and running the build on release, allowing the compiler to apply some code optimizations as well), the scene render time went from those +3 hours to 105 seconds.

The second optimization, is that the authors relied heavily on OOP and heap allocations, even if sometimes the object would not need to be heap allocated. We know that using inheritance and virtual methods affects performance negatively. By my newly and rapidly growing reluctance to use OOP in every programming paradigm and the nature of Jai not being Object Oriented, I created a different architecture without inheritance nor virtual methods. I believe that this optimization alone would not save as much time as the multi-threaded solution did , but I know for a fact that affected the performance positively.

After following the first book and applying the optimizations mentioned above, I managed to render this (1920x1080) image in 105 seconds!

Wrapping up this project

As I reached a level where I felt that I could finish this study project, I decided to make a final scene with lots of objects. After 3 hours rendering, this was the final image:

The final render of this project. It took 3.5 hours to finish (1920x1080).

As a closing thought, I really am enjoying my 2024 journey on Rendering and it is becoming an increasing passion because it is teaching me so many new and different things, as well as making me a better programmer by making me take in consideration perfomance and code architecture to provide the best results.