idcrook / weeker_raytracer

ray tracing implementations. started with Peter Shirley's v2 Ray Tracing In One Weekend
49 stars 13 forks source link

Optix ROL final image "Twisted" Final scene from The Rest of Your Life book, rendered using Optix 6.5 version.

Started from C++ v2 code from Peter Shirley's Ray Tracing In One Weekend Book Series.

Cpp OptiX CUDA
In One Weekend Code Code Code
The Next Week Code Code Dev
Rest Of Life Code Code

There are multiple implementations in this repository.

Multi-platform CMake builds

Build

Using cmake as it supports multiple platforms and language environments.

The general flow:

cmake -B build src    # create build dir and generate
cmake --build build   # build all targets

# developer generation
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
    -B build src

# parallel builds
cmake --build build --parallel 7 \
    --target theNextWeekOptix

Using -DCMAKE_EXPORT_COMPILE_COMMANDS=ON generates compile_commands.json file so that editors and other tools can know the compiler flags, etc. I use emacs irony-mode and flycheck works, which is very helpful to me using C++.

Typically the number of parallel jobs (7 in example above) is the number of CPU cores in your system, plus one. On linux, to get the number of cores in your system:

grep "^cpu\\scores" /proc/cpuinfo | uniq |  awk '{print $4}'

For debugging CMake itself: -Wdev -Wdeprecated --warn-unused-vars --warn-uninitialized on the -B build generation step

Run

build target(s), then

# bang is used here for my zsh setup to clobber existing file
time ( build/program >! output/iname.ppm )

The time shell wrapper is obviated when program itself outputs its duration.

A .ppm image file is a non-binary (text) format.

Advanced example(s) with additional command line parameters

build/restOfLifeOptix -v -s 0 -dx 1120 -dy 1120 -ns 1024 >! output/test1.ppm

output

INFO: Display driver version: 435.21
INFO: OptiX RTX execution mode is ON.
INFO: Output image dimensions: 1120x1120
INFO: Number of rays sent per pixel: 1024
INFO: Scene number selected: 0
INFO: Scene description: Cornell box
INFO: Took 16.1509 seconds.

In this example

Not all executables have the same options, or possible have none at all.

Possible Future Implementations

Build C++ (Cpp)

cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -B build src

Cpp example images

Build CUDA C++ (Cuda)

Code based on https://github.com/rogerallen/raytracinginoneweekendincuda

cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON  -B build src

# target specific SM
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
      -DCMAKE_CUDA_FLAGS="-arch=sm_75" \
      -B build src

the CUDA targets

cmake --build build --target inOneWeekendCuda
cmake --build build --target theNextWeekCuda

Implementation stopped at the point of creating a BVH structure in CUDA. The qsort from the book was problematic to adapt since there is no direct equivalent in the thrust library that I could find.

CUDA example images

Build OptiX 6.5 (Optix)

Code based on

and then on earlier versions of

# or set other flags
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
    -DCMAKE_CUDA_FLAGS="--use_fast_math --generate-line-info" \
    -B build src

# Other CMAKE_CUDA_FLAGS
#   --relocatable-device-code=true
#   --verbose

build specific targets

cmake --build build --target inOneWeekendOptix --clean-first
cmake --build build --target theNextWeekOptix
cmake --build build --target restOfLifeOptix --parallel 7

Optix example images

Tested on

CUDA Tested on

OptiX Tested on

Same as CUDA above.

Optix 6.5.0 SDK installed at /usr/local/nvidia/NVIDIA-OptiX-SDK-6.5.0-linux64. See notes/ subdirectory for other details and hints.

Early performance comparisons

Ray Tracing In One Weekend final scene

Image Render examples

Image Renders (Optix GPU)

In One Weekend

IOW Optix final image 1200 x 600 pixels, 1K rays launched per pixel. Render time about 2.5 seconds using OptiX on an RTX card.

The Next Week

TNW lighting IOW image 2400 x 800 pixels with 9000 samples per pixel. took 67.9 seconds

TNW final image 3840 x 1080 pixels with 10240 samples per pixel. took 400 seconds

convert tnw-final_scene.png -resize 50% half_tnw-final_scene.png

Rest Of Life

TNW final image

convert output/rol-final-alum_1k.ppm -resize 50% rol-final-alum_1k.png

Image Renders (C++ Single Thread CPU)

In One Weekend

final image

1200x800 pixels, 20 rays per pixel. Image took about 12.3 minutes, without BVH partitioning. When generating same scene with BVH, took about 3 minutes.

The Next Week

final image 2

1000x1000 pixels with 2500 rays per pixel. took over 18 hours

Rest Of Life

final image 1000x1000 pixels with 500 rays per pixel. Took 1 hour, 8 minutes

Image Renders (CUDA)

cuda final image Above: inOneWeekendCuda output, around four seconds to render.