The goal of this project is to build a moderately capable ray tracer, able to render sphere and triangle meshes with various material types, using the GPU API Vulkan to interface with the GPU.
A common project to get programmers familiar with photorealistic rendering is to build a ray tracer, which is a program that simulates the path that individual light rays would take in a scene, and is able to output realistic-looking images because of it.
The main disadvantage of ray tracing is its speed, or rather lack of, as calculating the paths of all of the rays can become very expensive, especially if running on a CPU.
However, ray tracing can also be considered an Embarassingly Parallel problem, meaning that it can scale incredibly well with parallelization. This renders it an ideal candidate to be executed on graphics cards.
On top of make, GCC 12, and glslc, the project depends on glfw
, vulkan
, glm
, and yaml-cpp
system libraries.
GLFW version 3.4 is required for the project to work properly on modern desktop environments.
sudo dnf install vulkan vulkan-headers vulkan-loader-devel glm-devel glslc glfw glfw-devel yaml-cpp yaml-cpp-devel
Replace [your vulkan drivers] based on your device
sudo pacman -S [your vulkan drivers] vulkan-headers vulkan-validation-layers vulkan-swrast yaml-cpp glfw glm shaderc
Since Debian and Ubuntu ship older versions of the Vulkan headers, the full SDK needs to be downloaded from LunarG's website.
Instructions can be found on https://vulkan.lunarg.com/sdk/home#linux. Here's how to install the latest Vulkan SDK on Ubuntu 22:
wget -qO - https://packages.lunarg.com/lunarg-signing-key-pub.asc | sudo apt-key add -
sudo wget -qO /etc/apt/sources.list.d/lunarg-vulkan-1.4.304-noble.list https://packages.lunarg.com/vulkan/1.4.304/lunarg-vulkan-1.4.304-noble.list
sudo apt update
sudo apt install vulkan-sdk
Other dependencies are also needed to compile the project:
sudo apt install libyaml-cpp-dev libglfw3 libglfw3-dev libglm-dev
The project is compiled and run by the included make file:
make compile # to compile the project
make run # to run it
The program uses two different file formats to load and render the various scenes:
This file contains all of the various parameters needed by the ray tracer to load a scene and trace it.
The max_bvh_depth
parameters requires it to be changed in src/shaders/util/parameters.comp
. The shaders need to be recompiled after that.
# WARNING: Some of these parameters need the shaders parameters to be tweaked accordingly.
# Please update the appropriate parameters in src/shaders/util/parameters.comp
# General parameters
scene_file: "scenes/cornell.yaml" # file from which the scene will be loaded
shader_dir: "build/shaders" # directory containing compiled shaders
tile_size: 8 # size of tiles used for rendering.
# Vulkan parameters
use_llvmpipe: false # forces the rendering to happen on the CPU
use_validation_layers: false # enables vulkan validation layers
validation_layers: # list of enabled validation layers
- "VK_LAYER_KHRONOS_validation" # - standard error checking
- "VK_LAYER_LUNARG_api_dump" # - dump in stdout API calls
# BVH parameters
max_bvh_depth: 64 # maximum depth of the tree
bvh_split_attempts: 8 # amount of splits performed to find the optimal BVH.
# set to -1 to attempt all possible splits.
# Output parameters
offscreen_rendering: false # enable to render on headless devices
dump_file: "./dump.raw" # file where the final framebuffer will be dumped
frame_count: -1 # number frames to render before dumping the output
# set to -1 to let render indefinitely
This file represents a full scene to be ray traced:
version: '0.2'
camera:
resolution: [800, 600]
focal_length: 1.0
focus_distance: 4.8
aperture_radius: 0.2
location: [0, 0, 0]
rotation: [0, 0, 0]
scene:
- type: Sphere
material:
base_color: [0.5, 0.5, 0.5]
emission: [15, 6, 5]
reflectiveness: 0.96
roughness: 0.43
ior: 1.45
is_glass: false
smooth_shading: false
motion_blur: [0, 1.3, 0.4]
data:
radius: 99
center: [0, -100, 0]
- type: TriMesh
material:
base_color: [1, 0.99, 0.9]
data:
vertices: [1, 2, 3, ...]
normals: [4, 5, 6, ...]
Two additional programs are included in the repo to facilitate the usage of the ray tracer.
This program converts an obj file (and its associated .mlt) into the ray tracer's scene format.
Usage, while in obj-convert directory:
cargo run ./path/to/objfile.obj
This will result in a scene.yaml
file.
This program converts the framebuffer dump into a viewable PNG image.
Usage, while in dump-convert directory:
cargo run ./path/to/dump.raw imageWidth imageHeight
This will output a PNG image. Make sure that imageWidth and imageHeight are the correct dimensions of the framebuffer.
These showcase the depth of field as well as various materials such as glass.
The right screenshot is a render of "Low Poly Lake Scene" by EdwiixGG on Sketchfab