My project is about GPU accelerated ray tracing demostrations written in C++ and OpenCL, targeting multi-platform use.
There are two versions for my ray tracer:
- one is CPU-based, without OpenGL version. The default input is
Assets/nohier.lua
and the output isAssets/nohier.png
file. - Another is GPU-based, with OpenCL kernel source code and invokes OpenGL using glew and glut library. There is no input for client. The output is a static OpenGL window.
- On Windows 10 with x86_64 arhchitecture, the OpenCL version can be run by double click
Project1.exe
withopencl_kernel.cl
glew64.dll
andglut64.dll
in the same directory.
- recursive ray tracer for CPU
- Phong reflection model
- Lambertian reflectance
- specular recursive reflection for mirror effect
- iterative ray tracer for OpenCL (since OpenCL language do not allow recursion)
- Since OpenCL do not allow recursion, I need to transform my recursive ray-tracer to a iterative one. Luckily, by adding accumulator for color, and change ray direction after each iteration, it is sufficient for this transformation.
- There is a new way for dealing with pixels being black where the ray trapped inside the sphere. I add a exclude id as a new parameter to intersector. When calculating the shadow ray intersection, I let hitted object's id as the exclude id, then the ray can be reflected as normal.
- The C++ and OpenCL source code is adapted from Ray Tracey's blog here. I change the OpenCL kernel source code by adding my ray tracer and ray-sphere intersetion functions, though keep the OpenCL and OpenGL inter-operation header, API invocations, and helper functions as the same as the origin one.
- The pure C++ version is based on my CS488 A4, all written by me.
- On Windows 10
- For AMD GPUs, download AMD APP SDK, which is abandoned and unoffically hosted here, but contains all the libraries needed to compile OpenCL code, plus code samples and documentation.
- For Intel GPUs, download Intel SDK for OpenCL here.
- For Intel CPUs, download OpenCL Runtimes here.
- For Nvidia GPUs, download R465 and later drivers here
- On Linux, there could be some difficulties based on what distribution you use.
- On MacOS, please see https://developer.apple.com/opencl/ for support list and code samples.
- On Windows 10 for OpenCL version
I use MSVC with Visual Studio and AMD RX560 GPU for my OpenCL version and I installed AMD APP SDK 3.0.
- add
C:\Program Files (x86)\AMD APP SDK\3.0\include
to "Additional Include Directories" in Visual Studio. - add
C:\Program Files (x86)\AMD APP SDK\3.0\lib\x86_64\
to "Additional Library Directories" in linker->input. - Then press F5 for debug.
- add
- On Linux for the non-OpenCL version, run in A4 folder
make clean all
to compile, enter Asset folder and run by program by../A4
- Why choose OpenCL?
I choose it because the API supports multiple GPU vendor and operating systems. It compiles just-in-time, or offline if you wish.
Besides, it is a royalty-free framework for parallel programming using GPUs. - Why I did not use Vulkan which I written in proposal?
The implementation for ray tracing extension in Vulkan is vendor specific. Old GPUs do not have the ray tracing extension. Only NVIDIA RTX GPUs or AMD RDMA2 GPUs support it, unfortunately which I do not have. I could dig deeper to use Vulkan for some parallel mathematical operations, but it lacks documentation and requires some low-level hardware understanding for GPU. - Why I do not implement anti-aliasing in OpenCL by multi-sampling?
The first reason is that there is no random number generator in OpenCL.
I need to pass random number from C++ to OpenCL buffer. However, in this case the same working group get the same random number ! (The working-group means that they share local memory of computing tasks). The image will looks glitchy, where pixels' columns are not aligned.
- OpenCL is somewhat abandoned, at least for AMD and Apple, since they are not providing drivers, libraries and documentations anymore.
- OpenCL is very hard to debug, although OpenCL C language is a subset of C99 with some extensions, it do NOT allow for comment using
//
,size_t double goto
is missing for some implementations. The OpenCL source code is passed to source code as string then compile just-in-time by invoke API. - Implementation for OpenCL on Linux is not really hardware agonostic, because it contains bugs for detecting
libopencl.so
byopencl-icd
. Proprietary implementation did not work well on my GPU and Arch Linux, it will flickering with strange colors and go black which requiring me to reboot.